Blender V5.3
vk_device_submission.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <chrono>
10#include <condition_variable>
11#include <thread>
12
13#include "BLI_mutex.hh"
14
15#include "vk_device.hh"
16#include "vk_to_string.hh"
17
18#include "CLG_log.h"
19
20namespace blender {
21
22static CLG_LogRef LOG = {"gpu.vulkan"};
23
24namespace gpu {
25
26/* -------------------------------------------------------------------- */
29
35
46
48 VKDiscardPool &context_discard_pool,
49 bool submit_to_device,
50 bool wait_for_submission,
51 bool wait_for_completion,
52 VkPipelineStageFlags wait_dst_stage_mask,
53 VkSemaphore wait_semaphore,
54 VkSemaphore signal_semaphore,
55 VkFence signal_fence)
56{
57 /* Syncing input flags. */
58 /* When we wait for completion/submission we must submit to device. */
59 submit_to_device |= wait_for_completion;
60 submit_to_device |= wait_for_submission;
61 /* We don't need to wait for submission when waiting for completion. */
62 wait_for_submission &= !wait_for_completion;
63
64 VKRenderGraphSubmitTask *submit_task = MEM_new<VKRenderGraphSubmitTask>(__func__);
65 submit_task->render_graph = render_graph;
66 submit_task->submit_to_device = submit_to_device;
67 submit_task->wait_dst_stage_mask = wait_dst_stage_mask;
68 submit_task->wait_semaphore = wait_semaphore;
69 submit_task->signal_semaphore = signal_semaphore;
70 submit_task->signal_fence = signal_fence;
71 submit_task->wait_for_submission = nullptr;
72
73 VKRenderGraphWait wait_condition{};
74 if (wait_for_submission) {
75 submit_task->wait_for_submission = &wait_condition;
76 }
77 TimelineValue timeline = 0;
78 {
79 std::scoped_lock lock(orphaned_data.mutex_get());
80 timeline = submit_task->timeline = submit_to_device ? ++timeline_value_ : timeline_value_ + 1;
81 orphaned_data.timeline_ = timeline;
82 orphaned_data.move_data(context_discard_pool, timeline);
84 submitted_render_graphs_, submit_task, BLI_THREAD_QUEUE_WORK_PRIORITY_NORMAL);
85 }
86 submit_task = nullptr;
87
88 if (wait_for_submission) {
89 std::unique_lock<Mutex> lock(wait_condition.is_submitted_mutex);
90 wait_condition.is_submitted_condition.wait(lock, [&] { return wait_condition.is_submitted; });
91 }
92
93 if (wait_for_completion) {
94 wait_for_timeline(timeline);
95 }
96 return timeline;
97}
98
100{
101 if (timeline == 0) {
102 return;
103 }
104 VkSemaphoreWaitInfo vk_semaphore_wait_info = {
105 VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO, nullptr, 0, 1, &vk_timeline_semaphore_, &timeline};
106 VkResult wait_result = functions.vkWaitSemaphoresKHR(
107 vk_device_, &vk_semaphore_wait_info, UINT64_MAX);
108 if (wait_result != VK_SUCCESS) {
110 &LOG, "Vulkan: failed to wait for synchronization timeline [%s]", to_string(wait_result));
111 }
112}
113
115{
116 std::scoped_lock lock(*queue_mutex_);
117 functions.vkQueueWaitIdle(vk_queue_);
118}
119
121{
123 BLI_thread_queue_pop_timeout(unused_render_graphs_, 0));
124 if (render_graph) {
125 return render_graph;
126 }
127
128 std::scoped_lock lock(resources.mutex);
129 render_graph = MEM_new<render_graph::VKRenderGraph>(__func__, resources);
130 render_graphs_.append(render_graph);
131 return render_graph;
132}
133
135{
136 CLOG_TRACE(&LOG, "Submission runner has started");
137
138 VkCommandPool vk_command_pool = VK_NULL_HANDLE;
139 VkCommandPoolCreateInfo vk_command_pool_create_info = {
140 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
141 nullptr,
142 VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
143 device->vk_queue_family_};
144 device->functions.vkCreateCommandPool(
145 device->vk_device_, &vk_command_pool_create_info, nullptr, &vk_command_pool);
146
148 render_graph::VKCommandBuilder command_builder;
149 Vector<VkCommandBuffer> command_buffers_unused;
150 TimelineResources<VkCommandBuffer> command_buffers_in_use;
151 VkCommandBuffer vk_command_buffer = VK_NULL_HANDLE;
152 std::optional<render_graph::VKCommandBufferWrapper> command_buffer;
153 uint64_t previous_gc_timeline = 0;
154 uint64_t num_nodes = 0;
155
156 CLOG_TRACE(&LOG, "Submission runner initialized");
157 while (!device->submission_thread_should_exit_) {
158 VKRenderGraphSubmitTask *submit_task = static_cast<VKRenderGraphSubmitTask *>(
159 BLI_thread_queue_pop_timeout(device->submitted_render_graphs_, 1));
160 if (submit_task == nullptr) {
161 continue;
162 }
163 uint64_t current_timeline = device->submission_finished_timeline_get();
164 if (assign_if_different(previous_gc_timeline, current_timeline)) {
165 device->orphaned_data.destroy_discarded_resources(*device, current_timeline);
166 }
167
168 if (!command_buffer.has_value()) {
169 /* Check for completed command buffers that can be reused. */
170 if (command_buffers_unused.is_empty()) {
171 command_buffers_in_use.remove_old(current_timeline,
172 [&](VkCommandBuffer vk_command_buffer) {
173 command_buffers_unused.append(vk_command_buffer);
174 });
175 }
176
177 /* Create new command buffers when there are no left to be reused. */
178 if (command_buffers_unused.is_empty()) {
179 command_buffers_unused.resize(10, VK_NULL_HANDLE);
180 VkCommandBufferAllocateInfo vk_command_buffer_allocate_info = {
181 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
182 nullptr,
183 vk_command_pool,
184 VK_COMMAND_BUFFER_LEVEL_PRIMARY,
185 10};
186 device->functions.vkAllocateCommandBuffers(
187 device->vk_device_, &vk_command_buffer_allocate_info, command_buffers_unused.data());
188 };
189
190 vk_command_buffer = command_buffers_unused.pop_last();
191 command_buffer = std::make_optional<render_graph::VKCommandBufferWrapper>(
192 vk_command_buffer, device->functions, device->extensions_, device->workarounds_);
193 command_buffer->begin_recording();
194 }
195
196 BLI_assert(vk_command_buffer != VK_NULL_HANDLE);
197
200 {
201 std::scoped_lock lock_resources(device->resources.mutex);
202 command_builder.build_nodes(render_graph, *command_buffer, node_handles);
203 }
204 command_builder.record_commands(render_graph, *command_buffer, node_handles);
205 num_nodes += node_handles.size();
206
207 if (submit_task->submit_to_device) {
208 /* Finalize current command buffer. */
209 command_buffer->end_recording();
210
211 uint32_t wait_semaphore_len = submit_task->wait_semaphore == VK_NULL_HANDLE ? 1 : 2;
212 VkSemaphore wait_semaphores[2] = {device->vk_timeline_semaphore_,
213 submit_task->wait_semaphore};
214 uint64_t wait_semaphore_values[2] = {submit_task->timeline - 1, 0};
215
216 uint32_t signal_semaphore_len = submit_task->signal_semaphore == VK_NULL_HANDLE ? 1 : 2;
217 VkSemaphore signal_semaphores[2] = {device->vk_timeline_semaphore_,
218 submit_task->signal_semaphore};
219 uint64_t signal_semaphore_values[2] = {submit_task->timeline, 0};
220 VkPipelineStageFlags pipeline_stage_flags[2] = {VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
221 submit_task->wait_dst_stage_mask};
222
223 VkTimelineSemaphoreSubmitInfo vk_timeline_semaphore_submit_info = {
224 VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO,
225 nullptr,
226 wait_semaphore_len,
227 wait_semaphore_values,
228 signal_semaphore_len,
229 signal_semaphore_values};
230 VkSubmitInfo vk_submit_info = {VK_STRUCTURE_TYPE_SUBMIT_INFO,
231 &vk_timeline_semaphore_submit_info,
232 wait_semaphore_len,
233 wait_semaphores,
234 pipeline_stage_flags,
235 1,
236 &vk_command_buffer,
237 signal_semaphore_len,
238 signal_semaphores};
239
240 CLOG_TRACE(&LOG, "Submitting %u render graph nodes to device.", uint32_t(num_nodes));
241 num_nodes = 0;
242
243 {
244 std::scoped_lock lock_queue(*device->queue_mutex_);
245 device->functions.vkQueueSubmit(
246 device->vk_queue_, 1, &vk_submit_info, submit_task->signal_fence);
247 }
248 command_buffers_in_use.append_timeline(submit_task->timeline, vk_command_buffer);
249 vk_command_buffer = VK_NULL_HANDLE;
250 command_buffer.reset();
251 }
252
253 /* Release the graph's references; zero-ref resources become discardable and are flushed
254 * below. */
255 render_graph.deref_resources();
256
257 /* Discard zero-ref resources, stamped with this submission's timeline: their last use is
258 * in this task's commands. */
259 {
262 device->orphaned_data.discard_resources(discardable, submit_task->timeline);
263 }
264
265 render_graph.reset();
266 /* Signal only after the graph's resource references are released. Otherwise swapchain images
267 * aren't dereffed in correct order. */
268 if (submit_task->wait_for_submission != nullptr) {
269 std::unique_lock<Mutex> lock(submit_task->wait_for_submission->is_submitted_mutex);
270 submit_task->wait_for_submission->is_submitted = true;
271 submit_task->wait_for_submission->is_submitted_condition.notify_one();
272 }
273
274 BLI_thread_queue_push(device->unused_render_graphs_,
275 std::move(submit_task->render_graph),
277 MEM_delete<VKRenderGraphSubmitTask>(submit_task);
278 }
279 CLOG_TRACE(&LOG, "Submission runner is being canceled");
280
281 /* Clear command buffers and pool */
282 {
283 std::scoped_lock lock(*device->queue_mutex_);
284 device->functions.vkDeviceWaitIdle(device->vk_device_);
285 }
286 command_buffers_in_use.remove_old(UINT64_MAX, [&](VkCommandBuffer vk_command_buffer) {
287 command_buffers_unused.append(vk_command_buffer);
288 });
289 device->functions.vkFreeCommandBuffers(device->vk_device_,
290 vk_command_pool,
291 command_buffers_unused.size(),
292 command_buffers_unused.data());
293 device->functions.vkDestroyCommandPool(device->vk_device_, vk_command_pool, nullptr);
294 CLOG_TRACE(&LOG, "Submission runner finished");
295}
296
297void VKDevice::init_submission_thread()
298{
299 CLOG_TRACE(&LOG, "Create submission thread");
300 submission_thread_should_exit_ = false;
301 submitted_render_graphs_ = BLI_thread_queue_init();
302 unused_render_graphs_ = BLI_thread_queue_init();
303
304 VkSemaphoreTypeCreateInfo vk_semaphore_type_create_info = {
305 VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO, nullptr, VK_SEMAPHORE_TYPE_TIMELINE, 0};
306 VkSemaphoreCreateInfo vk_semaphore_create_info = {
307 VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, &vk_semaphore_type_create_info, 0};
308 functions.vkCreateSemaphore(
309 vk_device_, &vk_semaphore_create_info, nullptr, &vk_timeline_semaphore_);
310
311 submission_thread_ = std::thread(VKDevice::submission_runner, this);
312}
313
314void VKDevice::deinit_submission_thread()
315{
316 CLOG_TRACE(&LOG, "Stopping submission thread");
317 submission_thread_should_exit_ = true;
318 CLOG_TRACE(&LOG, "Waiting for completion");
319 submission_thread_.join();
320
321 while (!BLI_thread_queue_is_empty(submitted_render_graphs_)) {
322 VKRenderGraphSubmitTask *submit_task = static_cast<VKRenderGraphSubmitTask *>(
323 BLI_thread_queue_pop(submitted_render_graphs_));
324 MEM_delete<VKRenderGraphSubmitTask>(submit_task);
325 }
326 BLI_thread_queue_free(submitted_render_graphs_);
327 submitted_render_graphs_ = nullptr;
328 BLI_thread_queue_free(unused_render_graphs_);
329 unused_render_graphs_ = nullptr;
330
331 functions.vkDestroySemaphore(vk_device_, vk_timeline_semaphore_, nullptr);
332 vk_timeline_semaphore_ = VK_NULL_HANDLE;
333}
334
336
337} // namespace gpu
338} // namespace blender
#define UINT64_MAX
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_TRACE(clg_ref,...)
Definition CLG_log.h:192
volatile int lock
constexpr int64_t size() const
Definition BLI_span.hh:256
void remove_old(TimelineValue current_timeline, Deleter deleter)
void append_timeline(TimelineValue timeline, Item item)
render_graph::VKResourceStateTracker resources
Definition vk_device.hh:270
TimelineValue submission_finished_timeline_get() const
Definition vk_device.hh:431
render_graph::VKRenderGraph * render_graph_new()
static void submission_runner(VKDevice *device)
VKDiscardPool orphaned_data
Definition vk_device.hh:271
VolkDeviceTable functions
Definition vk_device.hh:280
void wait_for_timeline(TimelineValue timeline)
TimelineValue render_graph_submit(render_graph::VKRenderGraph *render_graph, VKDiscardPool &context_discard_pool, bool submit_to_device, bool wait_for_submission, bool wait_for_completion, VkPipelineStageFlags wait_dst_stage_mask, VkSemaphore wait_semaphore, VkSemaphore signal_semaphore, VkFence signal_fence)
void move_data(VKDiscardPool &src_pool, TimelineValue timeline)
void discard_resources(Span< render_graph::VKResourceStateTracker::DiscardableResource > discardable, TimelineValue timeline)
void destroy_discarded_resources(VKDevice &device, TimelineValue current_timeline)
void record_commands(VKRenderGraph &render_graph, VKCommandBufferInterface &command_buffer, Span< NodeHandle > node_handles)
void build_nodes(VKRenderGraph &render_graph, VKCommandBufferInterface &command_buffer, Span< NodeHandle > node_handles)
Span< NodeHandle > select_nodes(const VKRenderGraph &render_graph)
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
uint64_t TimelineValue
Definition vk_common.hh:39
static CLG_LogRef LOG
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
@ BLI_THREAD_QUEUE_WORK_PRIORITY_NORMAL
bool assign_if_different(T &old_value, T &&new_value)
void * BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms)
Definition threads.cc:787
bool BLI_thread_queue_is_empty(ThreadQueue *queue)
Definition threads.cc:844
unsigned int uint32_t
Definition BKE_text.h:81
void BLI_thread_queue_free(ThreadQueue *queue)
Definition threads.cc:638
ThreadQueue * BLI_thread_queue_init()
Definition threads.cc:627
void * BLI_thread_queue_pop(ThreadQueue *queue)
Definition threads.cc:717
std::mutex Mutex
Definition BLI_mutex.hh:51
static CLG_LogRef LOG
Definition logging.cpp:12
uint64_t BLI_thread_queue_push(ThreadQueue *queue, void *work, ThreadQueueWorkPriority priority)
Definition threads.cc:648
render_graph::VKRenderGraph * render_graph
std::condition_variable_any is_submitted_condition
int size() const
Definition vector.hh:130
void resize(int new_size)
Definition vector.hh:94