Blender V5.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
22
23#pragma once
24
25#include "BLI_bit_vector.hh"
26#include "BLI_enum_flags.hh"
27#include "BLI_map.hh"
28#include "BLI_mutex.hh"
29#include "BLI_vector.hh"
30
31#include "vk_common.hh"
32
34
36struct VKRenderGraphLink;
37class VKScheduler;
38class VKRenderGraph;
39
48
58
62enum class VKResourceType { NONE = (0 << 0), IMAGE = (1 << 0), BUFFER = (1 << 1) };
64
70 VkAccessFlags vk_access = VK_ACCESS_NONE;
71 /* Last known pipeline stage. Will be reset by the last write. Reads will accumulate flags. */
72 VkPipelineStageFlags vk_pipeline_stages = VK_PIPELINE_STAGE_NONE;
74 VkImageLayout image_layout = VK_IMAGE_LAYOUT_UNDEFINED;
75
76 bool is_new_stamp() const
77 {
78 return bool(vk_access &
79 (VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
80 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT |
81 VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_MEMORY_WRITE_BIT));
82 }
83};
84
93 /* When a command buffer is reset the resources are re-synced.
94 * During the syncing the command builder attributes are resized to reduce reallocations. */
95 friend class VKCommandBuilder;
96 friend struct VKRenderGraphLink;
97 friend class VKScheduler;
98 friend class VKRenderGraph;
99
107 struct Resource {
109 VKResourceType type;
110 union {
111 struct {
113 VkBuffer vk_buffer = VK_NULL_HANDLE;
114 } buffer;
115
116 struct {
118 VkImage vk_image = VK_NULL_HANDLE;
120 bool use_subresource_tracking = false;
121 } image;
122 };
123
124 int ref_count = 0;
126 ModificationStamp stamp = 0;
127
131 VKResourceBarrierState barrier_state = {};
132
137
138#ifndef NDEBUG
139 std::string name;
140#endif
141
152 bool use_subresource_tracking()
153 {
154 if (type == VKResourceType::BUFFER) {
155 return false;
156 }
157 return image.use_subresource_tracking;
158 }
159 };
160
161 Vector<Resource> resources_;
168 Vector<VmaAllocation> allocations_;
169 Vector<ResourceHandle> unused_handles_;
170 Map<VkImage, ResourceHandle> image_resources_;
174 Vector<ResourceHandle> discardable_handles_;
175
176 public:
186
193 ResourceHandle add_buffer(VkBuffer vk_buffer,
194 VmaAllocation allocation = VK_NULL_HANDLE,
195 const char *name = nullptr);
196
203 ResourceHandle add_image(VkImage vk_image,
204 bool use_subresource_tracking,
205 VmaAllocation allocation = VK_NULL_HANDLE,
206 const char *name = nullptr);
207
214 ResourceHandle add_aliased_image(VkImage vk_image,
215 bool use_subresource_tracking,
216 VmaAllocation allocation = VK_NULL_HANDLE,
217 const char *name = nullptr);
218 ResourceHandle add_swapchain_image(VkImage vk_image, const char *name = nullptr);
219
223 template<typename VkView> void register_view(ResourceHandle resource_handle, VkView vk_view)
224 {
225 std::scoped_lock lock(mutex);
226 Resource &resource = get_resource(resource_handle);
227 resource.views.append(vk_view);
228 }
229
240 void update_image_layout(ResourceHandle image_handle, VkImageLayout vk_image_layout);
241
249 VmaAllocation allocation = VK_NULL_HANDLE;
250 union {
251 VkBuffer vk_buffer = VK_NULL_HANDLE;
252 VkImage vk_image;
253 };
254
256 };
257
264 void deref_resource(ResourceHandle resource_handle);
265
273
277 void remove_image(ResourceHandle image_handle);
278
282 VkBuffer remove_buffer(ResourceHandle buffer_handle);
283
284 inline const VkImage get_vk_image(ResourceHandle image_handle)
285 {
286 std::scoped_lock lock(mutex);
287 return get_image_resource(image_handle).image.vk_image;
288 }
289
293 void remove_swapchain_image(ResourceHandle image_handle);
294
300 {
301 std::scoped_lock lock(mutex);
302 return std::move(get_resource(resource_handle).views);
303 }
304
316 {
317 ResourceHandle handle = image_resources_.lookup(vk_image);
318 return {.handle = handle, .stamp = resources_[handle].stamp++};
319 }
320
332 {
333 return {.handle = buffer_handle, .stamp = resources_[buffer_handle].stamp++};
334 }
335
344 inline ResourceWithStamp get_buffer(ResourceHandle buffer_handle) const
345 {
346 return ResourceWithStamp{.handle = buffer_handle, .stamp = resources_[buffer_handle].stamp};
347 }
348
357 inline ResourceWithStamp get_image(VkImage vk_image) const
358 {
359 ResourceHandle handle = image_resources_.lookup(vk_image);
360 return {.handle = handle, .stamp = resources_[handle].stamp};
361 }
362
364
365 void debug_print() const;
366
367 private:
368 ResourceHandle add_image(VkImage vk_image,
369 bool use_subresource_tracking,
370 VKResourceBarrierState barrier_state,
371 VmaAllocation allocation,
372 const char *name = nullptr);
373
374 ResourceHandle create_resource_slot();
375
382 void deref_resources(const BitVector<> &resource_usages);
383
389 void deref_resource_impl(ResourceHandle resource_handle);
390
396 void ref_resource_impl(ResourceHandle resource_handle);
397
403 inline Resource &get_image_resource(ResourceHandle resource_handle)
404 {
405 BLI_assert(resources_[resource_handle].type == VKResourceType::IMAGE);
406 return resources_[resource_handle];
407 }
408 inline const Resource &get_image_resource(ResourceHandle resource_handle) const
409 {
410 BLI_assert(resources_[resource_handle].type == VKResourceType::IMAGE);
411 return resources_[resource_handle];
412 }
413 inline Resource &get_resource(ResourceHandle resource_handle)
414 {
415 BLI_assert(resources_[resource_handle].type == VKResourceType::IMAGE ||
416 resources_[resource_handle].type == VKResourceType::BUFFER);
417 return resources_[resource_handle];
418 }
424 inline Resource &get_buffer_resource(ResourceHandle resource_handle)
425 {
426 BLI_assert(resources_[resource_handle].type == VKResourceType::BUFFER);
427 return resources_[resource_handle];
428 }
429 inline const Resource &get_buffer_resource(ResourceHandle resource_handle) const
430 {
431 BLI_assert(resources_[resource_handle].type == VKResourceType::BUFFER);
432 return resources_[resource_handle];
433 }
434};
435
436} // namespace blender::gpu::render_graph
volatile int lock
#define BLI_assert(a)
ResourceWithStamp get_image_and_increase_stamp(VkImage vk_image)
Vector< std::variant< VkImageView, VkBufferView > > take_views(ResourceHandle resource_handle)
ResourceHandle add_image(VkImage vk_image, bool use_subresource_tracking, VmaAllocation allocation=VK_NULL_HANDLE, const char *name=nullptr)
ResourceHandle add_swapchain_image(VkImage vk_image, const char *name=nullptr)
ResourceHandle add_aliased_image(VkImage vk_image, bool use_subresource_tracking, VmaAllocation allocation=VK_NULL_HANDLE, const char *name=nullptr)
Register an image resource that can have aliased memory.
ResourceWithStamp get_buffer_and_increase_stamp(ResourceHandle buffer_handle)
void update_image_layout(ResourceHandle image_handle, VkImageLayout vk_image_layout)
ResourceHandle add_buffer(VkBuffer vk_buffer, VmaAllocation allocation=VK_NULL_HANDLE, const char *name=nullptr)
ResourceWithStamp get_image(VkImage vk_image) const
void register_view(ResourceHandle resource_handle, VkView vk_view)
ResourceWithStamp get_buffer(ResourceHandle buffer_handle) const
const VkImage get_vk_image(ResourceHandle image_handle)
ENUM_OPERATORS(VKResourceType)
uint64_t ResourceHandle
Definition vk_common.hh:68
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
std::mutex Mutex
Definition BLI_mutex.hh:51
struct blender::gpu::render_graph::VKResourceStateTracker::Resource::@177040105330030242205017041203256313364370336353::@075324102042273175374335256354061311203303015264 buffer
struct blender::gpu::render_graph::VKResourceStateTracker::Resource::@177040105330030242205017041203256313364370336353::@002335340160126334256077322350133252234176304034 image