Blender V4.5
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 uint32_t layer_count = 0;
126 } image;
127 };
128
130 ModificationStamp stamp = 0;
131
135 VKResourceBarrierState barrier_state;
136
137#ifndef NDEBUG
138 const char *name;
139#endif
140
152 bool has_multiple_layers()
153 {
154 if (type == VKResourceType::BUFFER) {
155 return false;
156 }
157 return image.layer_count > 1;
158 }
159 };
160
161 Map<ResourceHandle, Resource> resources_;
162 Vector<ResourceHandle> unused_handles_;
163 Map<VkImage, ResourceHandle> image_resources_;
164 Map<VkBuffer, ResourceHandle> buffer_resources_;
165
166 public:
176
183 void add_buffer(VkBuffer vk_buffer, const char *name = nullptr);
184
191 void add_image(VkImage vk_image, uint32_t layer_count, 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
261
262 void debug_print() const;
263
264 private:
268 static ResourceWithStamp get_stamp(ResourceHandle handle, const Resource &resource);
269
273 static ResourceWithStamp get_and_increase_stamp(ResourceHandle handle, Resource &resource);
274
275 ResourceHandle create_resource_slot();
276
277#ifdef VK_RESOURCE_STATE_TRACKER_VALIDATION
278 void validate() const;
279#endif
280};
281
282} // 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)
ResourceWithStamp get_buffer(VkBuffer vk_buffer) const
void add_image(VkImage vk_image, uint32_t layer_count, const char *name=nullptr)
void add_buffer(VkBuffer vk_buffer, const char *name=nullptr)
ResourceWithStamp get_image(VkImage vk_image) const
#define resource
std::mutex Mutex
Definition BLI_mutex.hh:47