Blender V4.3
vk_resource_state_tracker.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
23#pragma once
24
25#include <mutex>
26
27#include "BLI_map.hh"
28#include "BLI_vector.hh"
29
30#include "vk_common.hh"
31
37// #define VK_RESOURCE_STATE_TRACKER_VALIDATION
38
40
41class VKCommandBuilder;
42struct VKRenderGraphLink;
43class VKScheduler;
44
46
55
65
69enum class VKResourceType { NONE = (0 << 0), IMAGE = (1 << 0), BUFFER = (1 << 1) };
71
75enum class ResourceOwner {
84
92};
93
99 VkAccessFlags vk_access = VK_ACCESS_NONE;
100 /* Last known pipeline stage. Will be reset by the last write. Reads will accumulate flags. */
101 VkPipelineStageFlags vk_pipeline_stages = VK_PIPELINE_STAGE_NONE;
103 VkImageLayout image_layout = VK_IMAGE_LAYOUT_UNDEFINED;
104
105 bool is_new_stamp() const
106 {
107 return bool(vk_access &
108 (VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
109 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT |
110 VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_MEMORY_WRITE_BIT));
111 }
112};
113
122 /* When a command buffer is reset the resources are re-synced.
123 * During the syncing the command builder attributes are resized to reduce reallocations. */
124 friend class VKCommandBuilder;
125 friend struct VKRenderGraphLink;
126 friend class VKScheduler;
127
135 struct Resource {
137 VKResourceType type;
138 union {
139 struct {
141 VkBuffer vk_buffer = VK_NULL_HANDLE;
142 } buffer;
143
144 struct {
146 VkImage vk_image = VK_NULL_HANDLE;
148 uint32_t layer_count = 0;
149
156 VkImageLayout vk_image_layout = VK_IMAGE_LAYOUT_UNDEFINED;
157 } image;
158 };
159
161 ModificationStamp stamp = 0;
162
165
169 VKResourceBarrierState barrier_state;
170
171#ifndef NDEBUG
172 const char *name;
173#endif
174
183 void reset_image_layout()
184 {
186 barrier_state.image_layout = image.vk_image_layout;
187 }
188
200 bool has_multiple_layers()
201 {
202 if (type == VKResourceType::BUFFER) {
203 return false;
204 }
205 return image.layer_count > 1;
206 }
207 };
208
209 Map<ResourceHandle, Resource> resources_;
210 Vector<ResourceHandle> unused_handles_;
211 Map<VkImage, ResourceHandle> image_resources_;
212 Map<VkBuffer, ResourceHandle> buffer_resources_;
213
214 public:
223 std::mutex mutex;
224
231 void add_buffer(VkBuffer vk_buffer, const char *name = nullptr);
232
239 void add_image(VkImage vk_image,
240 uint32_t layer_count,
241 VkImageLayout vk_image_layout,
242 ResourceOwner owner,
243 const char *name = nullptr);
244
251 void remove_image(VkImage vk_image);
252
259 void remove_buffer(VkBuffer vk_buffer);
260
272
284
293 ResourceWithStamp get_buffer(VkBuffer vk_buffer) const;
294
303 ResourceWithStamp get_image(VkImage vk_image) const;
304
314 void reset_image_layouts();
315
318 {
319 return resources_.lookup(resource_handle).type;
320 }
321
322 private:
326 static ResourceWithStamp get_stamp(ResourceHandle handle, const Resource &resource);
327
331 static ResourceWithStamp get_and_increase_stamp(ResourceHandle handle, Resource &resource);
332
333 ResourceHandle create_resource_slot();
334
335#ifdef VK_RESOURCE_STATE_TRACKER_VALIDATION
336 void validate() const;
337#endif
338};
339
340} // namespace blender::gpu::render_graph
#define BLI_assert(a)
Definition BLI_assert.h:50
#define ENUM_OPERATORS(_type, _max)
VKResourceType resource_type_get(ResourceHandle resource_handle) const
void add_image(VkImage vk_image, uint32_t layer_count, VkImageLayout vk_image_layout, ResourceOwner owner, const char *name=nullptr)
ResourceWithStamp get_image_and_increase_stamp(VkImage vk_image)
ResourceWithStamp get_buffer_and_increase_stamp(VkBuffer vk_buffer)
ResourceWithStamp get_buffer(VkBuffer vk_buffer) const
void add_buffer(VkBuffer vk_buffer, const char *name=nullptr)
ResourceWithStamp get_image(VkImage vk_image) const
unsigned int uint32_t
Definition stdint.h:80
unsigned __int64 uint64_t
Definition stdint.h:90
Definition iris.cc:23