Blender V5.0
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_map.hh"
26#include "BLI_mutex.hh"
27#include "BLI_vector.hh"
28
29#include "vk_common.hh"
30
36// #define VK_RESOURCE_STATE_TRACKER_VALIDATION
37
39
42class VKScheduler;
43
45
54
64
68enum class VKResourceType { NONE = (0 << 0), IMAGE = (1 << 0), BUFFER = (1 << 1) };
70
76 VkAccessFlags vk_access = VK_ACCESS_NONE;
77 /* Last known pipeline stage. Will be reset by the last write. Reads will accumulate flags. */
78 VkPipelineStageFlags vk_pipeline_stages = VK_PIPELINE_STAGE_NONE;
80 VkImageLayout image_layout = VK_IMAGE_LAYOUT_UNDEFINED;
81
82 bool is_new_stamp() const
83 {
84 return bool(vk_access &
85 (VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
86 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT |
87 VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_MEMORY_WRITE_BIT));
88 }
89};
90
99 /* When a command buffer is reset the resources are re-synced.
100 * During the syncing the command builder attributes are resized to reduce reallocations. */
101 friend class VKCommandBuilder;
102 friend struct VKRenderGraphLink;
103 friend class VKScheduler;
104
112 struct Resource {
114 VKResourceType type;
115 union {
116 struct {
118 VkBuffer vk_buffer = VK_NULL_HANDLE;
119 } buffer;
120
121 struct {
123 VkImage vk_image = VK_NULL_HANDLE;
125 bool use_subresource_tracking = false;
126 } image;
127 };
128
130 ModificationStamp stamp = 0;
131
135 VKResourceBarrierState barrier_state = {};
136
137#ifndef NDEBUG
138 const char *name = nullptr;
139#endif
140
151 bool use_subresource_tracking()
152 {
153 if (type == VKResourceType::BUFFER) {
154 return false;
155 }
156 return image.use_subresource_tracking;
157 }
158 };
159
160 Map<ResourceHandle, Resource> resources_;
161 Vector<ResourceHandle> unused_handles_;
162 Map<VkImage, ResourceHandle> image_resources_;
163 Map<VkBuffer, ResourceHandle> buffer_resources_;
164
165 public:
175
182 void add_buffer(VkBuffer vk_buffer, const char *name = nullptr);
183
190 void add_image(VkImage vk_image, bool use_subresource_tracking, const char *name = nullptr);
191 void add_swapchain_image(VkImage vk_image, const char *name = nullptr);
192
199 void remove_image(VkImage vk_image);
200
207 void remove_buffer(VkBuffer vk_buffer);
208
220
232
241 ResourceWithStamp get_buffer(VkBuffer vk_buffer) const;
242
251 ResourceWithStamp get_image(VkImage vk_image) const;
252
255 {
256 return resources_.lookup(resource_handle).type;
257 }
258
260
261 void debug_print() const;
262
263 private:
264 void add_image(VkImage vk_image,
265 bool use_subresource_tracking,
266 VKResourceBarrierState barrier_state,
267 const char *name = nullptr);
268
272 static ResourceWithStamp get_stamp(ResourceHandle handle, const Resource &resource);
273
277 static ResourceWithStamp get_and_increase_stamp(ResourceHandle handle, Resource &resource);
278
279 ResourceHandle create_resource_slot();
280
281#ifdef VK_RESOURCE_STATE_TRACKER_VALIDATION
282 void validate() const;
283#endif
284};
285
286} // namespace blender::gpu::render_graph
#define ENUM_OPERATORS(_type, _max)
unsigned long long int uint64_t
VKResourceType resource_type_get(ResourceHandle resource_handle) const
ResourceWithStamp get_image_and_increase_stamp(VkImage vk_image)
ResourceWithStamp get_buffer_and_increase_stamp(VkBuffer vk_buffer)
void add_image(VkImage vk_image, bool use_subresource_tracking, const char *name=nullptr)
ResourceWithStamp get_buffer(VkBuffer vk_buffer) const
void add_buffer(VkBuffer vk_buffer, const char *name=nullptr)
void add_swapchain_image(VkImage vk_image, const char *name=nullptr)
ResourceWithStamp get_image(VkImage vk_image) const
#define resource
std::mutex Mutex
Definition BLI_mutex.hh:47
const char * name