Blender V4.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
9#include "BLI_index_range.hh"
10
12
14
15/* -------------------------------------------------------------------- */
18ResourceHandle VKResourceStateTracker::create_resource_slot()
19{
20 ResourceHandle handle;
21 if (unused_handles_.is_empty()) {
22 handle = resources_.size();
23 }
24 else {
25 handle = unused_handles_.pop_last();
26 }
27
28 Resource new_resource = {};
29 resources_.add_new(handle, new_resource);
30 return handle;
31}
32
34 uint32_t layer_count,
35 VkImageLayout vk_image_layout,
36 ResourceOwner owner,
37 const char *name)
38{
40 BLI_assert_msg(!image_resources_.contains(vk_image),
41 "Image resource is added twice to the render graph.");
42 std::scoped_lock lock(mutex);
43 ResourceHandle handle = create_resource_slot();
44 Resource &resource = resources_.lookup(handle);
45 image_resources_.add_new(vk_image, handle);
46
47 resource.type = VKResourceType::IMAGE;
48 resource.owner = owner;
49 resource.image.vk_image = vk_image;
50 resource.image.layer_count = layer_count;
51 resource.image.vk_image_layout = vk_image_layout;
52 resource.stamp = 0;
53#ifndef NDEBUG
54 resource.name = name;
55#endif
56
57#ifdef VK_RESOURCE_STATE_TRACKER_VALIDATION
58 validate();
59#endif
60}
61
62void VKResourceStateTracker::add_buffer(VkBuffer vk_buffer, const char *name)
63{
65 BLI_assert_msg(!buffer_resources_.contains(vk_buffer),
66 "Buffer resource is added twice to the render graph.");
67 std::scoped_lock lock(mutex);
68 ResourceHandle handle = create_resource_slot();
69 Resource &resource = resources_.lookup(handle);
70 buffer_resources_.add_new(vk_buffer, handle);
71
72 resource.type = VKResourceType::BUFFER;
73 resource.owner = ResourceOwner::APPLICATION;
74 resource.buffer.vk_buffer = vk_buffer;
75 resource.stamp = 0;
76#ifndef NDEBUG
77 resource.name = name;
78#endif
79
80#ifdef VK_RESOURCE_STATE_TRACKER_VALIDATION
81 validate();
82#endif
83}
84
87/* -------------------------------------------------------------------- */
92{
93 std::scoped_lock lock(mutex);
94 ResourceHandle handle = buffer_resources_.pop(vk_buffer);
95 resources_.pop(handle);
96 unused_handles_.append(handle);
97
98#ifdef VK_RESOURCE_STATE_TRACKER_VALIDATION
99 validate();
100#endif
101}
102
104{
105 std::scoped_lock lock(mutex);
106 ResourceHandle handle = image_resources_.pop(vk_image);
107 resources_.pop(handle);
108 unused_handles_.append(handle);
109
110#ifdef VK_RESOURCE_STATE_TRACKER_VALIDATION
111 validate();
112#endif
113}
114
117ResourceWithStamp VKResourceStateTracker::get_stamp(ResourceHandle handle,
118 const Resource &resource)
119{
121 result.handle = handle;
122 result.stamp = resource.stamp;
123 return result;
124}
125
126ResourceWithStamp VKResourceStateTracker::get_and_increase_stamp(ResourceHandle handle,
127 Resource &resource)
128{
129 ResourceWithStamp result = get_stamp(handle, resource);
130 resource.stamp += 1;
131 return result;
132}
133
135{
136 ResourceHandle handle = image_resources_.lookup(vk_image);
137 Resource &resource = resources_.lookup(handle);
138 return get_and_increase_stamp(handle, resource);
139}
140
142{
143 ResourceHandle handle = buffer_resources_.lookup(vk_buffer);
144 Resource &resource = resources_.lookup(handle);
145 return get_and_increase_stamp(handle, resource);
146}
147
149{
150 ResourceHandle handle = buffer_resources_.lookup(vk_buffer);
151 const Resource &resource = resources_.lookup(handle);
152 return get_stamp(handle, resource);
153}
154
156{
157 ResourceHandle handle = image_resources_.lookup(vk_image);
158 const Resource &resource = resources_.lookup(handle);
159 return get_stamp(handle, resource);
160}
161
163{
164 for (ResourceHandle image_handle : image_resources_.values()) {
165 VKResourceStateTracker::Resource &resource = resources_.lookup(image_handle);
166 if (resource.owner == ResourceOwner::SWAP_CHAIN) {
167 resource.reset_image_layout();
168 }
169 }
170}
171
172#ifdef VK_RESOURCE_STATE_TRACKER_VALIDATION
173void VKResourceStateTracker::validate() const
174{
175 for (const Map<VkImage, ResourceHandle>::Item &item : image_resources_.items()) {
176 for (ResourceHandle buffer_handle : buffer_resources_.values()) {
177 BLI_assert(item.value != buffer_handle);
178 }
179 BLI_assert(resources_.contains(item.value));
180 const Resource &resource = resources_.lookup(item.value);
181 BLI_assert(resource.type == VKResourceType::IMAGE);
182 }
183
184 for (const Map<VkBuffer, ResourceHandle>::Item &item : buffer_resources_.items()) {
185 for (ResourceHandle image_handle : image_resources_.values()) {
186 BLI_assert(item.value != image_handle);
187 }
188 BLI_assert(resources_.contains(item.value));
189 const Resource &resource = resources_.lookup(item.value);
190 BLI_assert(resource.type == VKResourceType::BUFFER);
191 }
192
193 BLI_assert(resources_.size() == image_resources_.size() + buffer_resources_.size());
194}
195#endif
196
197} // namespace blender::gpu::render_graph
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
#define UNUSED_VARS_NDEBUG(...)
volatile int lock
Value pop(const Key &key)
Definition BLI_map.hh:378
const Value & lookup(const Key &key) const
Definition BLI_map.hh:506
ValueIterator values() const
Definition BLI_map.hh:846
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:241
int64_t size() const
Definition BLI_map.hh:927
bool contains(const Key &key) const
Definition BLI_map.hh:329
void append(const T &value)
bool is_empty() 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