Blender V5.3
vk_resource_state_tracker.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include "BKE_global.hh"
12
13#include "BLI_index_range.hh"
14
16
18
19/* -------------------------------------------------------------------- */
22ResourceHandle VKResourceStateTracker::create_resource_slot()
23{
24 ResourceHandle handle;
25 static Resource new_resource = {};
26 if (unused_handles_.is_empty()) {
27 handle = resources_.append_and_get_index(new_resource);
28 allocations_.append(VK_NULL_HANDLE);
29 }
30 else {
31 handle = unused_handles_.pop_last();
32 resources_[handle] = new_resource;
33 allocations_[handle] = VK_NULL_HANDLE;
34 }
35
36 return handle;
37}
38
40 bool use_subresource_tracking,
41 VKResourceBarrierState barrier_state,
42 VmaAllocation allocation,
43 const char *name)
44{
46 std::scoped_lock lock(mutex);
47 BLI_assert_msg(!image_resources_.contains(vk_image),
48 "Image resource is added twice to the render graph.");
49 ResourceHandle handle = create_resource_slot();
50 Resource &resource = resources_[handle];
51 image_resources_.add_new(vk_image, handle);
52
53 resource = {
55 .image = {.vk_image = vk_image, .use_subresource_tracking = use_subresource_tracking},
56 .ref_count = 1,
57 .stamp = 0,
58 .barrier_state = barrier_state,
59 };
60 allocations_[handle] = allocation;
61
62#ifndef NDEBUG
63 if (name) {
64 resource.name = name;
65 }
66#endif
67 return handle;
68}
69
71 bool use_subresource_tracking,
72 VmaAllocation allocation,
73 const char *name)
74{
75 return add_image(vk_image, use_subresource_tracking, {}, allocation, name);
76}
77
79 bool use_subresource_tracking,
80 VmaAllocation allocation,
81 const char *name)
82{
83 return add_image(vk_image,
84 use_subresource_tracking,
85 {VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
86 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
87 VK_IMAGE_LAYOUT_UNDEFINED},
88 allocation,
89 name);
90}
91
93{
94 return add_image(vk_image,
95 false,
96 {
97 VK_ACCESS_NONE,
98 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
99 },
100 VK_NULL_HANDLE,
101 name);
102}
103
105 VmaAllocation allocation,
106 const char *name)
107{
108 UNUSED_VARS_NDEBUG(name);
109 std::scoped_lock lock(mutex);
110 ResourceHandle handle = create_resource_slot();
111 Resource &resource = resources_[handle];
112
113 resource = {
115 .buffer = {.vk_buffer = vk_buffer},
116 .ref_count = 1,
117 .stamp = 0,
118 .barrier_state = {},
119 };
120 allocations_[handle] = allocation;
121
122#ifndef NDEBUG
123 if (name) {
124 resource.name = name;
125 }
126#endif
127
128 return handle;
129}
130
132
133/* -------------------------------------------------------------------- */
136
138 VkImageLayout vk_image_layout)
139{
140 std::scoped_lock lock(mutex);
141 Resource &resource = get_image_resource(image_handle);
142 resource.barrier_state.image_layout = vk_image_layout;
143}
144
146
147/* -------------------------------------------------------------------- */
150
152{
153 std::scoped_lock lock(mutex);
154 deref_resource_impl(resource_handle);
155}
156
157void VKResourceStateTracker::deref_resources(const BitVector<> &resource_usages)
158{
159 std::scoped_lock lock(mutex);
160 for (ResourceHandle handle : IndexRange(resource_usages.size())) {
161 if (resource_usages[handle]) {
162 deref_resource_impl(handle);
163 }
164 }
165}
166
167void VKResourceStateTracker::ref_resource_impl(ResourceHandle resource_handle)
168{
169 Resource &resource = get_resource(resource_handle);
170 resource.ref_count += 1;
171}
172
173void VKResourceStateTracker::deref_resource_impl(ResourceHandle resource_handle)
174{
175 Resource &resource = get_resource(resource_handle);
176 BLI_assert(resource.ref_count != 0);
177 resource.ref_count -= 1;
178 if (resource.ref_count == 0) {
179 BLI_assert(!discardable_handles_.contains(resource_handle));
180 discardable_handles_.append(resource_handle);
181 }
182}
183
185{
186 std::scoped_lock lock(mutex);
187 BLI_assert(resources_[buffer_handle].ref_count == 0);
188 BLI_assert(!discardable_handles_.contains(buffer_handle));
189 BLI_assert(resources_[buffer_handle].views.is_empty());
190 VkBuffer vk_buffer = get_buffer_resource(buffer_handle).buffer.vk_buffer;
191 resources_[buffer_handle] = {
192 .type = VKResourceType::NONE,
193 .buffer = {.vk_buffer = VK_NULL_HANDLE},
194 .ref_count = 0,
195 .stamp = 0,
196 };
197 allocations_[buffer_handle] = VK_NULL_HANDLE;
198 unused_handles_.append(buffer_handle);
199
200 return vk_buffer;
201}
202
204{
205 std::scoped_lock lock(mutex);
206 BLI_assert(resources_[image_handle].ref_count == 0);
207 BLI_assert(resources_[image_handle].views.is_empty());
208 VkImage vk_image = get_image_resource(image_handle).image.vk_image;
209 image_resources_.pop(vk_image);
210 resources_[image_handle] = {
211 .type = VKResourceType::NONE,
212 .image = {.vk_image = VK_NULL_HANDLE, .use_subresource_tracking = false},
213 .ref_count = 0,
214 .stamp = 0,
215 };
216 allocations_[image_handle] = VK_NULL_HANDLE;
217 unused_handles_.append(image_handle);
218}
219
221{
222 std::scoped_lock lock(mutex);
223 BLI_assert(resources_[image_handle].ref_count == 1);
224 BLI_assert(resources_[image_handle].views.is_empty());
225 VkImage vk_image = get_image_resource(image_handle).image.vk_image;
226 image_resources_.pop(vk_image);
227 resources_[image_handle] = {
228 .type = VKResourceType::NONE,
229 .image = {.vk_image = VK_NULL_HANDLE, .use_subresource_tracking = false},
230 .ref_count = 0,
231 .stamp = 0,
232 };
233 allocations_[image_handle] = VK_NULL_HANDLE;
234 unused_handles_.append(image_handle);
235}
236
239{
240 std::scoped_lock lock(mutex);
241 Vector<DiscardableResource> discardable;
243 for (ResourceHandle handle : discardable_handles_) {
244 Resource &resource = resources_[handle];
246 if (resource.ref_count != 0) {
247 /* A new reference was acquired after the resource was marked discardable, so it stays
248 * registered and is collected by a later pop. */
249 keep.append(handle);
250 continue;
251 }
252 DiscardableResource discardable_resource = {
253 .handle = handle,
254 .type = resource.type,
255 .allocation = allocations_[handle],
256 };
257 if (resource.type == VKResourceType::BUFFER) {
258 discardable_resource.vk_buffer = resource.buffer.vk_buffer;
259 }
260 else {
261 discardable_resource.vk_image = resource.image.vk_image;
262 }
263 discardable_resource.views = std::move(resource.views);
264 discardable.append(std::move(discardable_resource));
265 }
266 discardable_handles_ = std::move(keep);
267 return discardable;
268}
269
271
273{
274 std::ostream &os = std::cout;
275 os << "VKResourceStateTracker\n";
276 os << " resources=(" << resources_.size() << "/" << resources_.capacity() << ")\n";
277 os << " images=(" << image_resources_.size() << "/" << image_resources_.capacity() << ")\n";
278 os << " discardable=(" << discardable_handles_.size() << "/" << discardable_handles_.capacity()
279 << ")\n";
280 os << " unused=(" << unused_handles_.size() << "/" << unused_handles_.capacity() << ")\n";
281}
282
283} // namespace blender::gpu::render_graph
volatile int lock
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
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.
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)
uint64_t ResourceHandle
Definition vk_common.hh:68
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int size() const
Definition vector.hh:130