Blender V5.3
vk_resource_pool.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 "vk_resource_pool.hh"
10#include "vk_backend.hh"
11#include "vk_context.hh"
12
13namespace blender::gpu {
14
19
21{
22 src_pool.allocations_.update_timeline(timeline);
23 src_pool.buffer_views_.update_timeline(timeline);
24 src_pool.buffers_.update_timeline(timeline);
25 src_pool.image_views_.update_timeline(timeline);
26 src_pool.images_.update_timeline(timeline);
27 src_pool.shader_modules_.update_timeline(timeline);
28 src_pool.pipelines_.update_timeline(timeline);
29 src_pool.pipeline_layouts_.update_timeline(timeline);
30 src_pool.descriptor_pools_.update_timeline(timeline);
31 src_pool.swapchain_images_.update_timeline(timeline);
32 src_pool.acceleration_structures_.update_timeline(timeline);
33
34 allocations_.extend(std::move(src_pool.allocations_));
35 buffer_views_.extend(std::move(src_pool.buffer_views_));
36 buffers_.extend(std::move(src_pool.buffers_));
37 image_views_.extend(std::move(src_pool.image_views_));
38 images_.extend(std::move(src_pool.images_));
39 shader_modules_.extend(std::move(src_pool.shader_modules_));
40 pipelines_.extend(std::move(src_pool.pipelines_));
41 pipeline_layouts_.extend(std::move(src_pool.pipeline_layouts_));
42 descriptor_pools_.extend(std::move(src_pool.descriptor_pools_));
43 swapchain_images_.extend(std::move(src_pool.swapchain_images_));
44 acceleration_structures_.extend(std::move(src_pool.acceleration_structures_));
45}
46
48{
49 std::scoped_lock mutex(mutex_);
50 swapchain_images_.append_timeline(timeline_, image_handle);
51}
52
53void VKDiscardPool::discard_image_view(VkImageView vk_image_view)
54{
55 std::scoped_lock mutex(mutex_);
56 image_views_.append_timeline(timeline_, vk_image_view);
57}
60 TimelineValue timeline)
61{
62 std::scoped_lock mutex(mutex_);
63 for (const render_graph::VKResourceStateTracker::DiscardableResource &discardable_resource :
64 discardable)
65 {
66 if (discardable_resource.type == render_graph::VKResourceType::IMAGE) {
67 /* Scheduling the same resource twice is a bug; the tracker pops each resource only once. */
69 std::find_if(images_.begin(),
70 images_.end(),
71 [&](const std::pair<TimelineValue, std::pair<ResourceHandle, VmaAllocation>>
72 &entry) {
73 return entry.second.first == discardable_resource.handle;
74 }) == images_.end());
75 for (const std::variant<VkImageView, VkBufferView> &view : discardable_resource.views) {
76 image_views_.append_timeline(timeline, std::get<VkImageView>(view));
77 }
78 images_.append_timeline(
79 timeline, std::pair(discardable_resource.handle, discardable_resource.allocation));
80 }
81 else if (discardable_resource.type == render_graph::VKResourceType::BUFFER) {
82 /* Scheduling the same resource twice is a bug; the tracker pops each resource only once. */
84 std::find_if(buffers_.begin(),
85 buffers_.end(),
86 [&](const std::pair<TimelineValue, std::pair<ResourceHandle, VmaAllocation>>
87 &entry) {
88 return entry.second.first == discardable_resource.handle;
89 }) == buffers_.end());
90 for (const std::variant<VkImageView, VkBufferView> &view : discardable_resource.views) {
91 buffer_views_.append_timeline(timeline, std::get<VkBufferView>(view));
92 }
93 buffers_.append_timeline(
94 timeline, std::pair(discardable_resource.handle, discardable_resource.allocation));
95 }
96 }
97}
98
99void VKDiscardPool::discard_allocation(VmaAllocation vma_allocation)
100{
101 std::scoped_lock mutex(mutex_);
102 allocations_.append_timeline(timeline_, vma_allocation);
103}
104
105void VKDiscardPool::discard_shader_module(VkShaderModule vk_shader_module)
106{
107 std::scoped_lock mutex(mutex_);
108 shader_modules_.append_timeline(timeline_, vk_shader_module);
109}
110void VKDiscardPool::discard_pipeline(VkPipeline vk_pipeline)
111{
112 std::scoped_lock mutex(mutex_);
113 pipelines_.append_timeline(timeline_, vk_pipeline);
114}
115void VKDiscardPool::discard_pipeline_layout(VkPipelineLayout vk_pipeline_layout)
116{
117 std::scoped_lock mutex(mutex_);
118 pipeline_layouts_.append_timeline(timeline_, vk_pipeline_layout);
119}
120
121void VKDiscardPool::discard_descriptor_pool_for_reuse(VkDescriptorPool vk_descriptor_pool,
122 VKDescriptorPools *descriptor_pools)
123{
124 std::scoped_lock mutex(mutex_);
125 descriptor_pools_.append_timeline(timeline_, std::pair(vk_descriptor_pool, descriptor_pools));
126}
127
129 VkAccelerationStructureKHR vk_acceleration_structure)
130{
131 std::scoped_lock mutex(mutex_);
132 acceleration_structures_.append_timeline(timeline_, vk_acceleration_structure);
133}
134
136{
137 std::scoped_lock mutex(mutex_);
138
139 swapchain_images_.remove_old(current_timeline, [&](ResourceHandle image_handle) {
140 /* Destroy the image's registered views before unregistering it. */
141 for (const std::variant<VkImageView, VkBufferView> &view :
142 device.resources.take_views(image_handle))
143 {
144 device.functions.vkDestroyImageView(
145 device.vk_handle(), std::get<VkImageView>(view), nullptr);
146 }
147 device.resources.remove_swapchain_image(image_handle);
148 });
149 image_views_.remove_old(current_timeline, [&](VkImageView vk_image_view) {
150 device.functions.vkDestroyImageView(device.vk_handle(), vk_image_view, nullptr);
151 });
152
153 allocations_.remove_old(current_timeline, [&](VmaAllocation vma_allocation) {
154 vmaFreeMemory(device.mem_allocator_get(), vma_allocation);
155 });
156 images_.remove_old(
157 current_timeline, [&](std::pair<ResourceHandle, VmaAllocation> image_allocation) {
158 VkImage vk_image = device.resources.get_vk_image(image_allocation.first);
159 device.resources.remove_image(image_allocation.first);
160 vmaDestroyImage(device.mem_allocator_get(), vk_image, image_allocation.second);
161 });
162 buffer_views_.remove_old(current_timeline, [&](VkBufferView vk_buffer_view) {
163 device.functions.vkDestroyBufferView(device.vk_handle(), vk_buffer_view, nullptr);
164 });
165
166 buffers_.remove_old(
167 current_timeline, [&](std::pair<ResourceHandle, VmaAllocation> buffer_allocation) {
168 VkBuffer vk_buffer = device.resources.remove_buffer(buffer_allocation.first);
169 vmaDestroyBuffer(device.mem_allocator_get(), vk_buffer, buffer_allocation.second);
170 });
171
172 pipelines_.remove_old(current_timeline, [&](VkPipeline vk_pipeline) {
173 device.functions.vkDestroyPipeline(device.vk_handle(), vk_pipeline, nullptr);
174 });
175
176 pipeline_layouts_.remove_old(current_timeline, [&](VkPipelineLayout vk_pipeline_layout) {
177 device.functions.vkDestroyPipelineLayout(device.vk_handle(), vk_pipeline_layout, nullptr);
178 });
179
180 shader_modules_.remove_old(current_timeline, [&](VkShaderModule vk_shader_module) {
181 device.functions.vkDestroyShaderModule(device.vk_handle(), vk_shader_module, nullptr);
182 });
183
184 descriptor_pools_.remove_old(
185 current_timeline, [&](std::pair<VkDescriptorPool, VKDescriptorPools *> descriptor_pool) {
186 descriptor_pool.second->recycle(descriptor_pool.first);
187 });
188
189 acceleration_structures_.remove_old(
190 current_timeline, [&](VkAccelerationStructureKHR vk_acceleration_structure) {
191 device.functions.vkDestroyAccelerationStructureKHR(
192 device.vk_handle(), vk_acceleration_structure, nullptr);
193 });
194}
195
197{
198 VKContext *context = VKContext::get();
199 if (context != nullptr) {
200 return context->discard_pool;
201 }
202
203 VKDevice &device = VKBackend::get().device;
204 if (G.is_rendering) {
205 return device.orphaned_data_render;
206 }
207 return device.orphaned_data;
208}
209
210std::ostream &operator<<(std::ostream &os, const VKDiscardPool &discard_pool)
211{
212 if (discard_pool.images_.is_empty() && discard_pool.buffers_.is_empty() &&
213 discard_pool.image_views_.is_empty() && discard_pool.buffer_views_.is_empty() &&
214 discard_pool.shader_modules_.is_empty() && discard_pool.pipeline_layouts_.is_empty() &&
215 discard_pool.descriptor_pools_.is_empty())
216 {
217 return os;
218 }
219 os << " Discardable resources: ";
220 if (!discard_pool.allocations_.is_empty()) {
221 os << "VmaAllocation=" << discard_pool.allocations_.size() << " ";
222 }
223 if (!discard_pool.images_.is_empty()) {
224 os << "VkImage=" << discard_pool.images_.size() << " ";
225 }
226 if (!discard_pool.image_views_.is_empty()) {
227 os << "VkImageView=" << discard_pool.image_views_.size() << " ";
228 }
229 if (!discard_pool.buffers_.is_empty()) {
230 os << "VkBuffer=" << discard_pool.buffers_.size() << " ";
231 }
232 if (!discard_pool.buffer_views_.is_empty()) {
233 os << "VkBufferViews=" << discard_pool.buffer_views_.size() << " ";
234 }
235 if (!discard_pool.shader_modules_.is_empty()) {
236 os << "VkShaderModule=" << discard_pool.shader_modules_.size() << " ";
237 }
238 if (!discard_pool.pipeline_layouts_.is_empty()) {
239 os << "VkPipelineLayout=" << discard_pool.pipeline_layouts_.size() << " ";
240 }
241 if (!discard_pool.descriptor_pools_.is_empty()) {
242 os << "VkDescriptorPool=" << discard_pool.descriptor_pools_.size();
243 }
244 return os;
245}
246
247} // namespace blender::gpu
#define UINT64_MAX
void update_timeline(TimelineValue timeline)
static VKBackend & get()
static VKContext * get()
render_graph::VKResourceStateTracker resources
Definition vk_device.hh:270
VmaAllocator mem_allocator_get() const
Definition vk_device.hh:348
VkDevice vk_handle() const
Definition vk_device.hh:338
VKDiscardPool orphaned_data
Definition vk_device.hh:271
VolkDeviceTable functions
Definition vk_device.hh:280
VKDiscardPool orphaned_data_render
Definition vk_device.hh:273
void discard_swapchain_image(ResourceHandle image_handle)
void discard_image_view(VkImageView vk_image_view)
void move_data(VKDiscardPool &src_pool, TimelineValue timeline)
void discard_descriptor_pool_for_reuse(VkDescriptorPool vk_descriptor_pool, VKDescriptorPools *descriptor_pools)
static VKDiscardPool & discard_pool_get()
void discard_shader_module(VkShaderModule vk_shader_module)
void discard_resources(Span< render_graph::VKResourceStateTracker::DiscardableResource > discardable, TimelineValue timeline)
void deinit(VKDevice &device)
void discard_allocation(VmaAllocation vma_allocation)
void discard_pipeline(VkPipeline vk_pipeline)
void discard_pipeline_layout(VkPipelineLayout vk_pipeline_layout)
void destroy_discarded_resources(VKDevice &device, TimelineValue current_timeline)
void discard_acceleration_structure(VkAccelerationStructureKHR vk_acceleration_structure)
friend std::ostream & operator<<(std::ostream &os, const VKDiscardPool &discard_pool)
Vector< std::variant< VkImageView, VkBufferView > > take_views(ResourceHandle resource_handle)
const VkImage get_vk_image(ResourceHandle image_handle)
uint64_t TimelineValue
Definition vk_common.hh:39
uint64_t ResourceHandle
Definition vk_common.hh:68
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Global G
Definition blender.cc:57