Blender V5.3
vk_command_builder.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
10#include "BLI_index_range.hh"
11#include "vk_backend.hh"
12#include "vk_render_graph.hh"
13#include "vk_to_string.hh"
14
15#include <sstream>
16
18
19static VkImageLayout to_default_image_layout(VkImageAspectFlags aspect, bool use_local_read)
20{
21 if (aspect & VK_IMAGE_ASPECT_DEPTH_BIT) {
22 if (aspect & VK_IMAGE_ASPECT_STENCIL_BIT) {
23 return VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
24 }
25 return VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL;
26 }
27 if (aspect & VK_IMAGE_ASPECT_STENCIL_BIT) {
28 return VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL;
29 }
30 return use_local_read ? VK_IMAGE_LAYOUT_RENDERING_LOCAL_READ_KHR :
31 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
32}
33
34/* -------------------------------------------------------------------- */
37
39 VKCommandBufferInterface &command_buffer,
40 Span<NodeHandle> node_handles)
41{
42 groups_init(render_graph, node_handles);
43 groups_extract_barriers(
44 render_graph, node_handles, command_buffer.use_dynamic_rendering_local_read);
45}
46
48 VKCommandBufferInterface &command_buffer,
49 Span<NodeHandle> node_handles)
50{
51 groups_build_commands(render_graph, command_buffer, node_handles);
52}
53
54void VKCommandBuilder::groups_init(const VKRenderGraph &render_graph,
55 Span<NodeHandle> node_handles)
56{
57 group_nodes_.clear();
58 IndexRange nodes_range = node_handles.index_range();
59 while (!nodes_range.is_empty()) {
60 IndexRange node_group = nodes_range.slice(0, 1);
61 NodeHandle node_handle = node_handles[nodes_range.first()];
62 const VKRenderGraphNode &node = render_graph.nodes_[node_handle];
63 while (node_type_is_rendering(node.type) && node_group.size() < nodes_range.size()) {
64 NodeHandle node_handle = node_handles[nodes_range[node_group.size()]];
65 const VKRenderGraphNode &node = render_graph.nodes_[node_handle];
67 break;
68 }
69 node_group = nodes_range.slice(0, node_group.size() + 1);
70 }
71
72 group_nodes_.append(node_group);
73 nodes_range = nodes_range.drop_front(node_group.size());
74 }
75}
76
77void VKCommandBuilder::groups_extract_barriers(VKRenderGraph &render_graph,
78 Span<NodeHandle> node_handles,
79 bool use_local_read)
80{
81 barrier_list_.clear();
82 vk_buffer_memory_barriers_.clear();
83 vk_image_memory_barriers_.clear();
84
85 ImageTracker image_tracker(*this);
86
87 /* Extract barriers. */
88 group_pre_barriers_.clear();
89 group_post_barriers_.clear();
90 node_pre_barriers_.resize(node_handles.size());
91
92 /* Keep track of the post barriers that needs to be added. The pre barriers will be stored
93 * directly in `barrier_list_` but may not mingle with the pre barriers. Most barriers are
94 * group pre barriers. */
95 Vector<Barrier> post_barriers;
96 /* Keep track of the node pre barriers that needs to be added. The pre barriers will be stored
97 * directly in `barrier_list_` but may not mingle with the group barriers. */
98 Vector<Barrier> node_pre_barriers;
99
100 NodeHandle rendering_scope;
101 bool rendering_active = false;
102
103 for (const int64_t group_index : group_nodes_.index_range()) {
104 /* Extract the pre-barriers of this group. */
105 Barriers group_pre_barriers(barrier_list_.size(), 0);
106 const GroupNodes &node_group = group_nodes_[group_index];
107 for (const int64_t group_node_index : node_group) {
108 NodeHandle node_handle = node_handles[group_node_index];
109 VKRenderGraphNode &node = render_graph.nodes_[node_handle];
110 Barrier barrier = {};
111 build_pipeline_barriers(
112 render_graph, node_handle, node.pipeline_stage_get(), image_tracker, barrier);
113 if (!barrier.is_empty()) {
114#if 0
115 std::cout << __func__ << ": node_group=" << group_index
116 << ", node_group_range=" << node_group.first() << "-" << node_group.last()
117 << ", node_handle=" << node_handle << ", node_type=" << node.type
118 << ", debug_group=" << render_graph.full_debug_group(node_handle) << "\n";
119 std::cout << __func__ << ": " << to_string_barrier(barrier);
120#endif
121 barrier_list_.append(barrier);
122 }
123
124 /* Reset the tracked layout of aliased images to undefined once their memory can be reused.
125 */
126 if (node.type == VKNodeType::SYNCHRONIZATION &&
127 node.synchronization.reset_layout_to_undefined)
128 {
129 for (const VKRenderGraphImage &link : render_graph.linked_images(node_handle)) {
130 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_image_resource(
131 link.resource.handle);
132 resource.barrier_state.image_layout = VK_IMAGE_LAYOUT_UNDEFINED;
133 }
134 }
135
136 /* Check for additional barriers when resuming rendering.
137 *
138 * Between suspending rendering and resuming the state/layout of resources can change and
139 * require additional barriers.
140 */
141 else if (node.type == VKNodeType::BEGIN_RENDERING) {
142 /* Begin rendering scope. */
143 BLI_assert(!rendering_active);
144 rendering_scope = node_handle;
145 rendering_active = true;
146 image_tracker.begin(render_graph, node_handle);
147 }
148
149 else if (node.type == VKNodeType::END_RENDERING) {
150 /* End rendering scope. */
151 BLI_assert(rendering_active);
152
153 /* Save rendering scope handle for resource state update below. */
154 const NodeHandle end_rendering_scope = rendering_scope;
155 rendering_scope = 0;
156 rendering_active = false;
157
158 /* The render pass storeOp writes each attachment with COLOR_ATTACHMENT_WRITE_BIT (or
159 * DEPTH_STENCIL_ATTACHMENT_WRITE_BIT). END_RENDERING has VKResourceType::NONE and
160 * declares no resource dependencies, so these implicit writes are invisible to the
161 * resource state tracker. Propagate them here so the next scope's pipeline barrier
162 * correctly synchronizes with the storeOp, avoiding WRITE_AFTER_WRITE hazards. */
163 for (const VKRenderGraphImage &link : render_graph.linked_images(end_rendering_scope)) {
164 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_image_resource(
165 link.resource.handle);
166 VKResourceBarrierState &state = resource.barrier_state;
167 if (link.vk_access_flags & VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT) {
168 state.vk_access |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
169 }
170 if (link.vk_access_flags & VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT) {
171 state.vk_access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
172 }
173 }
174
175 /* Any specific layout changes needs to be reverted, so the global resource state tracker
176 * reflects the correct state. These barriers needs to be added as node post barriers. We
177 * assume that END_RENDERING is always the last node of a group. */
178 Barrier barrier = {};
179 image_tracker.end(barrier, use_local_read);
180 if (!barrier.is_empty()) {
181 post_barriers.append(barrier);
182 }
183
184 /* After image_tracker.end() reverts tracked image layouts back to the default layout,
185 * sync the global resource state tracker so subsequent barriers use the correct
186 * oldLayout. */
187 for (const VKRenderGraphImage &link : render_graph.linked_images(end_rendering_scope)) {
188 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_image_resource(
189 link.resource.handle);
190 if (resource.use_subresource_tracking()) {
191 VKResourceBarrierState &state = resource.barrier_state;
192 state.image_layout = to_default_image_layout(link.vk_image_aspect, use_local_read);
193 }
194 }
195 }
196
197 else if (rendering_active && !node_type_is_within_rendering(node.type)) {
198 /* Suspend active rendering scope. */
199 rendering_active = false;
200
201 /* Any specific layout changes needs to be reverted, so the global resource state tracker
202 * reflects the correct state. These barriers needs to be added as node post barriers.
203 */
204 Barrier barrier = {};
205 image_tracker.suspend(barrier, use_local_read);
206 if (!barrier.is_empty()) {
207 post_barriers.append(barrier);
208 }
209
210 /* Sync resource state layout after suspend reverts tracked images. */
211 for (const VKRenderGraphImage &link : render_graph.linked_images(rendering_scope)) {
212 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_image_resource(
213 link.resource.handle);
214 if (resource.use_subresource_tracking()) {
215 VKResourceBarrierState &state = resource.barrier_state;
216 state.image_layout = to_default_image_layout(link.vk_image_aspect, use_local_read);
217 }
218 }
219 }
220
221 else if (!rendering_active && node_type_is_within_rendering(node.type)) {
222 /* Resume rendering scope. */
223 VKRenderGraphNode &rendering_node = render_graph.nodes_[rendering_scope];
224 Barrier barrier = {};
225 build_pipeline_barriers(render_graph,
226 rendering_scope,
227 rendering_node.pipeline_stage_get(),
228 image_tracker,
229 barrier);
230 if (!barrier.is_empty()) {
231 barrier_list_.append(barrier);
232 }
233
234 /* Resume layered tracking. Each layer that has an override will be transition back to
235 * the layer specific image layout. */
236 barrier = {};
237 image_tracker.resume(barrier, use_local_read);
238 if (!barrier.is_empty()) {
239 barrier_list_.append(barrier);
240 }
241
242 rendering_active = true;
243 }
244
245 /* Extract pre barriers for nodes. */
246 if (use_local_read && node_type_is_within_rendering(node.type) &&
247 node_has_input_attachments(render_graph, node_handle))
248 {
249 Barrier barrier = {};
250 build_pipeline_barriers(
251 render_graph, node_handle, node.pipeline_stage_get(), image_tracker, barrier, true);
252 if (!barrier.is_empty()) {
253 node_pre_barriers.append(barrier);
254 }
255 }
256 }
257 if (rendering_active) {
258 /* Suspend layered image tracker. When active the next group will always be a compute/data
259 * transfer group.
260 *
261 * Any specific layout changes needs to be reverted, so the global resource state tracker
262 * reflects the correct state. These barriers needs to be added as node post barriers.
263 */
264 Barrier barrier = {};
265 image_tracker.suspend(barrier, use_local_read);
266 if (!barrier.is_empty()) {
267 post_barriers.append(barrier);
268 }
269
270 /* Sync resource state layout after the tracker reverts tracked images. */
271 for (const VKRenderGraphImage &link : render_graph.linked_images(rendering_scope)) {
272 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_image_resource(
273 link.resource.handle);
274 if (resource.use_subresource_tracking()) {
275 VKResourceBarrierState &state = resource.barrier_state;
276 state.image_layout = to_default_image_layout(link.vk_image_aspect, use_local_read);
277 }
278 }
279
280 rendering_active = false;
281 }
282
283 /* After all node barriers have been extracted, check if depth/stencil attachments in
284 * BEGIN_RENDERING were transitioned to a non-optimal layout by group pre-barriers. If so,
285 * fix up VkRenderingAttachmentInfo.imageLayout to match the actual GPU layout, and add an
286 * acquire barrier when LOAD_OP_LOAD is used to ensure memory visibility. */
287 {
288 NodeHandle first_handle = node_handles[node_group.first()];
289 VKRenderGraphNode &first_node = render_graph.nodes_[first_handle];
290 if (first_node.type == VKNodeType::BEGIN_RENDERING) {
291 VKBeginRenderingData &begin_data =
292 render_graph.storage_.begin_rendering[first_node.storage_index];
293
294 for (const VKRenderGraphImage &link : render_graph.linked_images(first_handle)) {
295 if (!(link.vk_image_aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)))
296 {
297 continue;
298 }
299 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_image_resource(
300 link.resource.handle);
301 VkImageLayout gpu_layout = resource.barrier_state.image_layout;
302 VkImageLayout optimal_layout = to_default_image_layout(link.vk_image_aspect,
303 use_local_read);
304 if (gpu_layout == optimal_layout) {
305 continue;
306 }
307
308 /* Fix up the imageLayout to reflect the actual GPU layout after all pre-barriers. */
309 if (link.vk_image_aspect & VK_IMAGE_ASPECT_DEPTH_BIT &&
310 begin_data.vk_rendering_info.pDepthAttachment != nullptr)
311 {
312 begin_data.depth_attachment.imageLayout = gpu_layout;
313 }
314 if (link.vk_image_aspect & VK_IMAGE_ASPECT_STENCIL_BIT &&
315 begin_data.vk_rendering_info.pStencilAttachment != nullptr)
316 {
317 begin_data.stencil_attachment.imageLayout = gpu_layout;
318 }
319
320 /* When LOAD_OP_LOAD is used with a non-optimal layout, the existing layout transition
321 * barriers may not provide proper memory visibility for the load operation. Add an
322 * acquire barrier to ensure prior writes are visible before the depth/stencil test. */
323 bool depth_load = (link.vk_image_aspect & VK_IMAGE_ASPECT_DEPTH_BIT) &&
324 begin_data.vk_rendering_info.pDepthAttachment != nullptr &&
325 begin_data.depth_attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD;
326 bool stencil_load = (link.vk_image_aspect & VK_IMAGE_ASPECT_STENCIL_BIT) &&
327 begin_data.vk_rendering_info.pStencilAttachment != nullptr &&
328 begin_data.stencil_attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD;
329 if (!depth_load && !stencil_load) {
330 continue;
331 }
332
333 Barrier barrier = {};
334 reset_barriers(barrier);
335 barrier.src_stage_mask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
336 barrier.dst_stage_mask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
337 barrier.image_memory_barriers = IndexRange(vk_image_memory_barriers_.size(), 0);
338 add_image_barrier(resource.image.vk_image,
339 barrier,
340 VK_ACCESS_MEMORY_WRITE_BIT,
341 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
342 gpu_layout,
343 gpu_layout,
344 link.vk_image_aspect,
345 {});
346 barrier.image_memory_barriers = barrier.image_memory_barriers.with_new_end(
347 vk_image_memory_barriers_.size());
348 barrier_list_.append(barrier);
349 }
350 }
351 }
352
353 /* Update the group pre and post barriers. Pre barriers are already stored in the
354 * barrier_list_. The post barriers are appended after the pre barriers. */
355 int64_t barrier_list_size = barrier_list_.size();
356 group_pre_barriers_.append(group_pre_barriers.with_new_end(barrier_list_size));
357 barrier_list_.extend(std::move(post_barriers));
358 post_barriers = {};
359 group_post_barriers_.append(
360 IndexRange::from_begin_end(barrier_list_size, barrier_list_.size()));
361 if (!node_pre_barriers.is_empty()) {
362 barrier_list_size = barrier_list_.size();
363 barrier_list_.extend(std::move(node_pre_barriers));
364 node_pre_barriers = {};
365 /* Shift all node pre barrier references to the new location in the barrier_list_. */
366 for (const int64_t group_node_index : node_group) {
367 NodeHandle node_handle = node_handles[group_node_index];
368 if (!node_pre_barriers_[node_handle].is_empty()) {
369 node_pre_barriers_[node_handle] = IndexRange::from_begin_size(
370 node_pre_barriers_[node_handle].start() + barrier_list_size, 1);
371 }
372 }
373 }
374 }
375
376 BLI_assert(group_pre_barriers_.size() == group_nodes_.size());
377 BLI_assert(group_post_barriers_.size() == group_nodes_.size());
378}
379
380void VKCommandBuilder::groups_build_commands(VKRenderGraph &render_graph,
381 VKCommandBufferInterface &command_buffer,
382 Span<NodeHandle> node_handles)
383{
384 DebugGroups debug_groups = {};
385 VKBoundPipelines active_pipelines = {};
386
387 NodeHandle rendering_scope = 0;
388 bool rendering_active = false;
389
390 for (int64_t group_index : group_nodes_.index_range()) {
391 IndexRange group_nodes = group_nodes_[group_index];
392 Span<NodeHandle> group_node_handles = node_handles.slice(group_nodes);
393
394 /* Record group pre barriers. */
395 for (BarrierIndex barrier_index : group_pre_barriers_[group_index]) {
396 BLI_assert_msg(!rendering_active,
397 "Pre group barriers must be executed outside a rendering scope.");
398 Barrier &barrier = barrier_list_[barrier_index];
399#if 0
400 std::cout << __func__ << ": node_group=" << group_index
401 << ", node_group_range=" << group_node_handles.first() << "-"
402 << group_node_handles.last() << ", pre_barrier=(" << to_string_barrier(barrier)
403 << ")\n";
404#endif
405 send_pipeline_barriers(command_buffer, barrier, false);
406 }
407
408 /* Record group node commands. */
409 for (NodeHandle node_handle : group_node_handles) {
410 VKRenderGraphNode &node = render_graph.nodes_[node_handle];
411
412 if (G.debug & G_DEBUG_GPU) {
413 activate_debug_group(render_graph, command_buffer, debug_groups, node_handle);
414 }
415
416 if (node.type == VKNodeType::BEGIN_RENDERING) {
417 rendering_scope = node_handle;
418 rendering_active = true;
419 }
420
421 else if (node.type == VKNodeType::END_RENDERING) {
422 rendering_active = false;
423 }
424 else if (node_type_is_within_rendering(node.type)) {
425 if (!rendering_active) {
426 /* Restart rendering scope. */
427 VKRenderGraphNode &rendering_node = render_graph.nodes_[rendering_scope];
429 render_graph.storage_.begin_rendering[rendering_node.storage_index]);
430 rendering_node.build_commands(command_buffer, render_graph.storage_, active_pipelines);
431 rendering_active = true;
432 }
433 }
434
435 /* Record group node barriers. (VK_EXT_dynamic_rendering_local_read) */
436 for (BarrierIndex node_pre_barrier_index : node_pre_barriers_[node_handle]) {
437 Barrier &barrier = barrier_list_[node_pre_barrier_index];
438#if 0
439 std::cout << __func__ << ": node_group=" << group_index
440 << ", node_group_range=" << group_node_handles.first() << "-"
441 << group_node_handles.last() << ", node_pre_barrier=(" << to_string_barrier(barrier)
442 << ")\n";
443#endif
444 /* TODO: Barrier should already contain the changes for local read. */
445 send_pipeline_barriers(command_buffer, barrier, true);
446 }
447
448#if 0
449 std::cout << __func__ << ": node_group=" << group_index
450 << ", node_group_range=" << group_node_handles.first() << "-"
451 << group_node_handles.last() << ", node_handle=" << node_handle
452 << ", node_type=" << node.type
453 << ", debug group=" << render_graph.full_debug_group(node_handle) << "\n";
454#endif
455 node.build_commands(command_buffer, render_graph.storage_, active_pipelines);
456 }
457
458 if (rendering_active) {
459 /* Suspend rendering as the next node group will contain data transfer/dispatch commands.
460 */
461 rendering_active = false;
462 command_buffer.end_rendering();
463 }
464
465 /* Record group post barriers. */
466 for (BarrierIndex barrier_index : group_post_barriers_[group_index]) {
467 BLI_assert_msg(!rendering_active,
468 "Post group barriers must be executed outside a rendering scope.");
469 Barrier &barrier = barrier_list_[barrier_index];
470#if 0
471 std::cout << __func__ << ": node_group=" << group_index
472 << ", node_group_range=" << group_node_handles.first() << "-"
473 << group_node_handles.last() << ", post_barrier=(" << to_string_barrier(barrier)
474 << ")\n";
475#endif
476 send_pipeline_barriers(command_buffer, barrier, false);
477 }
478 }
479
480 finish_debug_groups(command_buffer, debug_groups);
481}
482
483bool VKCommandBuilder::node_has_input_attachments(const VKRenderGraph &render_graph,
484 NodeHandle node_handle)
485{
486 const Span<VKRenderGraphImage> images = render_graph.linked_images(node_handle);
487 return std::ranges::any_of(images.begin(), images.end(), [](const VKRenderGraphImage &input) {
488 return input.vk_access_flags & VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
489 });
490}
491
492void VKCommandBuilder::activate_debug_group(VKRenderGraph &render_graph,
493 VKCommandBufferInterface &command_buffer,
494 DebugGroups &debug_groups,
495 NodeHandle node_handle)
496{
497 VKRenderGraph::DebugGroupID debug_group = render_graph.debug_.node_group_map[node_handle];
498 if (debug_group == debug_groups.active_debug_group_id) {
499 return;
500 }
501
502 /* Determine the number of pops and pushes that will happen on the debug stack. */
503 int num_ends = 0;
504 int num_begins = 0;
505
506 if (debug_group == -1) {
507 num_ends = debug_groups.debug_level;
508 }
509 else {
510 Vector<VKRenderGraph::DebugGroupNameID> &to_group =
511 render_graph.debug_.used_groups[debug_group];
512 if (debug_groups.active_debug_group_id != -1) {
513 Vector<VKRenderGraph::DebugGroupNameID> &from_group =
514 render_graph.debug_.used_groups[debug_groups.active_debug_group_id];
515
516 num_ends = max_ii(from_group.size() - to_group.size(), 0);
517 int num_checks = min_ii(from_group.size(), to_group.size());
518 for (int index : IndexRange(num_checks)) {
519 if (from_group[index] != to_group[index]) {
520 num_ends += num_checks - index;
521 break;
522 }
523 }
524 }
525
526 num_begins = to_group.size() - (debug_groups.debug_level - num_ends);
527 }
528
529 /* Perform the pops from the debug stack. */
530 for (int index = 0; index < num_ends; index++) {
531 command_buffer.end_debug_utils_label();
532 }
533 debug_groups.debug_level -= num_ends;
534
535 /* Perform the pushes to the debug stack. */
536 if (num_begins > 0) {
537 Vector<VKRenderGraph::DebugGroupNameID> &to_group =
538 render_graph.debug_.used_groups[debug_group];
539 VkDebugUtilsLabelEXT debug_utils_label = {};
540 debug_utils_label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
541 for (int index : IndexRange(debug_groups.debug_level, num_begins)) {
542 const VKRenderGraph::DebugGroup &debug_group = render_graph.debug_.groups[to_group[index]];
543 debug_utils_label.pLabelName = debug_group.name.c_str();
544 copy_v4_v4(debug_utils_label.color, debug_group.color);
545 command_buffer.begin_debug_utils_label(&debug_utils_label);
546 }
547 }
548
549 debug_groups.debug_level += num_begins;
550 debug_groups.active_debug_group_id = debug_group;
551}
552
553void VKCommandBuilder::finish_debug_groups(VKCommandBufferInterface &command_buffer,
554 DebugGroups &debug_groups)
555{
556 for (int i = 0; i < debug_groups.debug_level; i++) {
557 command_buffer.end_debug_utils_label();
558 }
559 debug_groups.debug_level = 0;
560}
561
562void VKCommandBuilder::build_pipeline_barriers(VKRenderGraph &render_graph,
563 NodeHandle node_handle,
564 VkPipelineStageFlags pipeline_stage,
565 ImageTracker &image_tracker,
566 Barrier &r_barrier,
567 bool within_rendering)
568{
569 reset_barriers(r_barrier);
570 add_image_barriers(
571 render_graph, node_handle, pipeline_stage, image_tracker, r_barrier, within_rendering);
572 add_buffer_barriers(render_graph, node_handle, pipeline_stage, r_barrier);
573}
574
576
577/* -------------------------------------------------------------------- */
580
581void VKCommandBuilder::reset_barriers(Barrier &r_barrier)
582{
583 r_barrier.dst_stage_mask = r_barrier.src_stage_mask = VK_PIPELINE_STAGE_NONE;
584}
585
586void VKCommandBuilder::send_pipeline_barriers(VKCommandBufferInterface &command_buffer,
587 const Barrier &barrier,
588 bool within_rendering)
589{
590 if (barrier.is_empty()) {
591 return;
592 }
593
594 /* When no resources have been used, we can start the barrier at the top of the pipeline.
595 * It is not allowed to set it to None. */
596 /* TODO: VK_KHR_synchronization2 allows setting src_stage_mask to NONE. */
597 /* When no resources have been used, we can start the barrier at the top of the pipeline.
598 * It is not allowed to set it to None. */
599 VkPipelineStageFlags src_stage_mask = (barrier.src_stage_mask == VK_PIPELINE_STAGE_NONE) ?
600 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
601 VkPipelineStageFlagBits(barrier.src_stage_mask);
602
603 VkPipelineStageFlags dst_stage_mask = barrier.dst_stage_mask;
604 /* TODO: this should be done during barrier extraction making within_rendering obsolete. */
605 if (within_rendering) {
606 /* See: VUID - `vkCmdPipelineBarrier` - `srcStageMask` - 09556
607 * If `vkCmdPipelineBarrier` is called within a render pass instance started with
608 * `vkCmdBeginRendering`, this command must only specify frame-buffer-space stages in
609 * `srcStageMask` and `dstStageMask`. */
610 src_stage_mask = dst_stage_mask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT |
611 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
612 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT |
613 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
614 }
615
616 Span<VkBufferMemoryBarrier> buffer_barriers = vk_buffer_memory_barriers_.as_span().slice(
617 barrier.buffer_memory_barriers);
618 Span<VkImageMemoryBarrier> image_barriers = vk_image_memory_barriers_.as_span().slice(
619 barrier.image_memory_barriers);
620
621 command_buffer.pipeline_barrier(src_stage_mask,
622 dst_stage_mask,
623 VK_DEPENDENCY_BY_REGION_BIT,
624 0,
625 nullptr,
626 buffer_barriers.size(),
627 buffer_barriers.data(),
628 image_barriers.size(),
629 image_barriers.data());
630}
631
632void VKCommandBuilder::add_buffer_barriers(VKRenderGraph &render_graph,
633 NodeHandle node_handle,
634 VkPipelineStageFlags node_stages,
635 Barrier &r_barrier)
636{
637 r_barrier.buffer_memory_barriers = IndexRange(vk_buffer_memory_barriers_.size(), 0);
638 add_buffer_read_barriers(render_graph, node_handle, node_stages, r_barrier);
639 add_buffer_write_barriers(render_graph, node_handle, node_stages, r_barrier);
640 r_barrier.buffer_memory_barriers = r_barrier.buffer_memory_barriers.with_new_end(
641 vk_buffer_memory_barriers_.size());
642}
643
644void VKCommandBuilder::add_buffer_read_barriers(VKRenderGraph &render_graph,
645 NodeHandle node_handle,
646 VkPipelineStageFlags node_stages,
647 Barrier &r_barrier)
648{
649 for (const VKRenderGraphBuffer &link : render_graph.linked_buffers(node_handle)) {
650 if (link.has_write_access()) {
651 continue;
652 }
653 const ResourceWithStamp &versioned_resource = link.resource;
654 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_buffer_resource(
655 versioned_resource.handle);
656 VKResourceBarrierState &resource_state = resource.barrier_state;
657 const bool is_first_read = resource_state.is_new_stamp();
658 if (!is_first_read &&
659 (resource_state.vk_access & link.vk_access_flags) == link.vk_access_flags &&
660 (resource_state.vk_pipeline_stages & node_stages) == node_stages)
661 {
662 /* Has already been covered in a previous call no need to add this one. */
663 continue;
664 }
665
666 const VkAccessFlags wait_access = resource_state.vk_access;
667
668 r_barrier.src_stage_mask |= resource_state.vk_pipeline_stages;
669 r_barrier.dst_stage_mask |= node_stages;
670
671 if (is_first_read) {
672 resource_state.vk_access = link.vk_access_flags;
673 resource_state.vk_pipeline_stages = node_stages;
674 }
675 else {
676 resource_state.vk_access |= link.vk_access_flags;
677 resource_state.vk_pipeline_stages |= node_stages;
678 }
679
680 if (wait_access != VK_ACCESS_NONE) {
681 add_buffer_barrier(resource.buffer.vk_buffer, r_barrier, wait_access, link.vk_access_flags);
682 }
683 }
684}
685
686void VKCommandBuilder::add_buffer_write_barriers(VKRenderGraph &render_graph,
687 NodeHandle node_handle,
688 VkPipelineStageFlags node_stages,
689 Barrier &r_barrier)
690{
691 for (const VKRenderGraphBuffer &link : render_graph.linked_buffers(node_handle)) {
692 if (!link.has_write_access()) {
693 continue;
694 }
695 const ResourceWithStamp &versioned_resource = link.resource;
696 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_buffer_resource(
697 versioned_resource.handle);
698 VKResourceBarrierState &resource_state = resource.barrier_state;
699 const VkAccessFlags wait_access = resource_state.vk_access;
700
701 r_barrier.src_stage_mask |= resource_state.vk_pipeline_stages;
702 r_barrier.dst_stage_mask |= node_stages;
703
704 resource_state.vk_access = link.vk_access_flags;
705 resource_state.vk_pipeline_stages = node_stages;
706
707 if (wait_access != VK_ACCESS_NONE) {
708 add_buffer_barrier(resource.buffer.vk_buffer, r_barrier, wait_access, link.vk_access_flags);
709 }
710 }
711}
712
713void VKCommandBuilder::add_buffer_barrier(VkBuffer vk_buffer,
714 Barrier &r_barrier,
715 VkAccessFlags src_access_mask,
716 VkAccessFlags dst_access_mask)
717{
718 for (VkBufferMemoryBarrier &vk_buffer_memory_barrier :
719 vk_buffer_memory_barriers_.as_mutable_span().drop_front(
720 r_barrier.buffer_memory_barriers.start()))
721 {
722 if (vk_buffer_memory_barrier.buffer == vk_buffer) {
723 /* When registering read/write buffers, it can be that the node internally requires
724 * read/write. In this case we adjust the dstAccessMask of the read barrier. */
725 if ((vk_buffer_memory_barrier.dstAccessMask & src_access_mask) == src_access_mask) {
726 vk_buffer_memory_barrier.dstAccessMask |= dst_access_mask;
727 return;
728 }
729 /* When re-registering resources we can skip if access mask already contain all the flags.
730 */
731 if ((vk_buffer_memory_barrier.dstAccessMask & dst_access_mask) == dst_access_mask &&
732 (vk_buffer_memory_barrier.srcAccessMask & src_access_mask) == src_access_mask)
733 {
734 return;
735 }
736 }
737 }
738
739 vk_buffer_memory_barriers_.append({VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
740 nullptr,
741 src_access_mask,
742 dst_access_mask,
743 VK_QUEUE_FAMILY_IGNORED,
744 VK_QUEUE_FAMILY_IGNORED,
745 vk_buffer,
746 0,
747 VK_WHOLE_SIZE});
748}
749
750void VKCommandBuilder::add_image_barriers(VKRenderGraph &render_graph,
751 NodeHandle node_handle,
752 VkPipelineStageFlags node_stages,
753 ImageTracker &image_tracker,
754 Barrier &r_barrier,
755 bool within_rendering)
756{
757 r_barrier.image_memory_barriers = IndexRange(vk_image_memory_barriers_.size(), 0);
758 add_image_read_barriers(
759 render_graph, node_handle, node_stages, image_tracker, r_barrier, within_rendering);
760 add_image_write_barriers(
761 render_graph, node_handle, node_stages, image_tracker, r_barrier, within_rendering);
762 r_barrier.image_memory_barriers = r_barrier.image_memory_barriers.with_new_end(
763 vk_image_memory_barriers_.size());
764}
765
766void VKCommandBuilder::add_image_read_barriers(VKRenderGraph &render_graph,
767 NodeHandle node_handle,
768 VkPipelineStageFlags node_stages,
769 ImageTracker &image_tracker,
770 Barrier &r_barrier,
771 bool within_rendering)
772{
773 for (const VKRenderGraphImage &link : render_graph.linked_images(node_handle)) {
774 if (link.has_write_access()) {
775 continue;
776 }
777 const ResourceWithStamp &versioned_resource = link.resource;
778 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_image_resource(
779 versioned_resource.handle);
780 VKResourceBarrierState &resource_state = resource.barrier_state;
781 const bool is_first_read = resource_state.is_new_stamp();
782 if ((!is_first_read) &&
783 (resource_state.vk_access & link.vk_access_flags) == link.vk_access_flags &&
784 (resource_state.vk_pipeline_stages & node_stages) == node_stages &&
785 resource_state.image_layout == link.vk_image_layout)
786 {
787 /* Has already been covered in previous barrier no need to add this one. */
788 continue;
789 }
790 if (within_rendering && link.vk_image_layout != VK_IMAGE_LAYOUT_RENDERING_LOCAL_READ_KHR) {
791 /* Allow only local read barriers inside rendering scope */
792 continue;
793 }
794
795 /* Check if this image is being tracked as layered color attachment. In that case we are not
796 * allowed to update the resource state as it will be reverted by the image tracker. */
797 if (image_tracker.contains(resource.image.vk_image)) {
798 if (resource_state.image_layout != link.vk_image_layout) {
799 image_tracker.update(resource.image.vk_image,
800 link.subimage,
801 resource_state.image_layout,
802 link.vk_image_layout,
803 link.vk_image_aspect,
804 r_barrier);
805 resource_state.image_layout = link.vk_image_layout;
806 }
807 /* Image tracker only tracks layout changes. Access flag synchronization is still needed
808 * to avoid WRITE->READ hazards between rendering scopes. See #158501. */
809 VkAccessFlags wait_access = resource_state.vk_access;
810 r_barrier.src_stage_mask |= resource_state.vk_pipeline_stages;
811 r_barrier.dst_stage_mask |= node_stages;
812 if (is_first_read) {
813 resource_state.vk_access = link.vk_access_flags;
814 resource_state.vk_pipeline_stages = node_stages;
815 }
816 else {
817 resource_state.vk_access |= link.vk_access_flags;
818 resource_state.vk_pipeline_stages |= node_stages;
819 }
820 if (wait_access != VK_ACCESS_NONE && (wait_access & ~link.vk_access_flags) != 0) {
821 add_image_barrier(resource.image.vk_image,
822 r_barrier,
823 wait_access,
824 link.vk_access_flags,
825 resource_state.image_layout,
826 link.vk_image_layout,
827 link.vk_image_aspect,
828 {});
829 }
830 continue;
831 }
832
833 VkAccessFlags wait_access = resource_state.vk_access;
834
835 r_barrier.src_stage_mask |= resource_state.vk_pipeline_stages;
836 r_barrier.dst_stage_mask |= node_stages;
837
838 if (is_first_read) {
839 resource_state.vk_access = link.vk_access_flags;
840 resource_state.vk_pipeline_stages = node_stages;
841 }
842 else {
843 resource_state.vk_access |= link.vk_access_flags;
844 resource_state.vk_pipeline_stages |= node_stages;
845 }
846
847 if (wait_access != VK_ACCESS_NONE || link.vk_image_layout != resource_state.image_layout) {
848 add_image_barrier(resource.image.vk_image,
849 r_barrier,
850 wait_access,
851 link.vk_access_flags,
852 resource_state.image_layout,
853 link.vk_image_layout,
854 link.vk_image_aspect,
855 {});
856 resource_state.image_layout = link.vk_image_layout;
857 }
858 }
859}
860
861void VKCommandBuilder::add_image_write_barriers(VKRenderGraph &render_graph,
862 NodeHandle node_handle,
863 VkPipelineStageFlags node_stages,
864 ImageTracker &image_tracker,
865 Barrier &r_barrier,
866 bool within_rendering)
867{
868 for (const VKRenderGraphImage &link : render_graph.linked_images(node_handle)) {
869 if (!link.has_write_access()) {
870 continue;
871 }
872 const ResourceWithStamp &versioned_resource = link.resource;
873 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_image_resource(
874 versioned_resource.handle);
875 VKResourceBarrierState &resource_state = resource.barrier_state;
876 const VkAccessFlags wait_access = resource_state.vk_access;
877 if (within_rendering && link.vk_image_layout != VK_IMAGE_LAYOUT_RENDERING_LOCAL_READ_KHR) {
878 /* Allow only local read barriers inside rendering scope */
879 continue;
880 }
881
882 /* Check if this image is being tracked as layered color attachment. In that case we are not
883 * allowed to update the resource state as it will be reverted by the image tracker. */
884 if (image_tracker.contains(resource.image.vk_image)) {
885 if (resource_state.image_layout != link.vk_image_layout) {
886 image_tracker.update(resource.image.vk_image,
887 link.subimage,
888 resource_state.image_layout,
889 link.vk_image_layout,
890 link.vk_image_aspect,
891 r_barrier);
892 resource_state.image_layout = link.vk_image_layout;
893 }
894 /* Image tracker only tracks layout changes. Access flag synchronization is still needed
895 * to avoid READ->WRITE hazards between rendering scopes. See #158501. */
896 r_barrier.src_stage_mask |= resource_state.vk_pipeline_stages;
897 r_barrier.dst_stage_mask |= node_stages;
898 resource_state.vk_access = link.vk_access_flags;
899 resource_state.vk_pipeline_stages = node_stages;
900 if (wait_access != VK_ACCESS_NONE && (wait_access & ~link.vk_access_flags) != 0) {
901 add_image_barrier(resource.image.vk_image,
902 r_barrier,
903 wait_access,
904 link.vk_access_flags,
905 resource_state.image_layout,
906 link.vk_image_layout,
907 link.vk_image_aspect,
908 {});
909 }
910 continue;
911 }
912
913 r_barrier.src_stage_mask |= resource_state.vk_pipeline_stages;
914 r_barrier.dst_stage_mask |= node_stages;
915
916 resource_state.vk_access = link.vk_access_flags;
917 resource_state.vk_pipeline_stages = node_stages;
918
919 if (wait_access != VK_ACCESS_NONE || link.vk_image_layout != resource_state.image_layout) {
920 add_image_barrier(resource.image.vk_image,
921 r_barrier,
922 wait_access,
923 link.vk_access_flags,
924 resource_state.image_layout,
925 link.vk_image_layout,
926 link.vk_image_aspect,
927 {});
928 resource_state.image_layout = link.vk_image_layout;
929 }
930 }
931}
932
933void VKCommandBuilder::add_image_barrier(VkImage vk_image,
934 Barrier &r_barrier,
935 VkAccessFlags src_access_mask,
936 VkAccessFlags dst_access_mask,
937 VkImageLayout old_image_layout,
938 VkImageLayout new_image_layout,
939 VkImageAspectFlags aspect_mask,
940 const VKSubImageRange &subimage)
941{
942 BLI_assert(aspect_mask != VK_IMAGE_ASPECT_NONE);
943 for (VkImageMemoryBarrier &vk_image_memory_barrier :
944 vk_image_memory_barriers_.as_mutable_span().drop_front(
945 r_barrier.image_memory_barriers.start()))
946 {
947 if (vk_image_memory_barrier.image == vk_image) {
948 /* When registering read/write buffers, it can be that the node internally requires
949 * read/write. In this case we adjust the dstAccessMask of the read barrier. An example is
950 * EEVEE update HIZ compute shader and shadow tagging. */
951 if ((vk_image_memory_barrier.dstAccessMask & src_access_mask) == src_access_mask) {
952 vk_image_memory_barrier.dstAccessMask |= dst_access_mask;
953 vk_image_memory_barrier.srcAccessMask |= src_access_mask;
954 /* When the existing barrier's newLayout matches the new barrier's
955 * oldLayout, update to the combined layout transition. For example,
956 * a read barrier (UNDEFINED->TRANSFER_DST) followed by a write
957 * barrier (TRANSFER_DST->GENERAL) becomes UNDEFINED->GENERAL. */
958 if (old_image_layout == vk_image_memory_barrier.newLayout) {
959 vk_image_memory_barrier.newLayout = new_image_layout;
960 }
961 return;
962 }
963 /* When re-registering resources we can skip if access mask already contain all the flags.
964 */
965 if ((vk_image_memory_barrier.dstAccessMask & dst_access_mask) == dst_access_mask &&
966 (vk_image_memory_barrier.srcAccessMask & src_access_mask) == src_access_mask &&
967 old_image_layout == new_image_layout)
968 {
969 return;
970 }
971 }
972 }
973
974 vk_image_memory_barriers_.append({VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
975 nullptr,
976 src_access_mask,
977 dst_access_mask,
978 old_image_layout,
979 new_image_layout,
980 VK_QUEUE_FAMILY_IGNORED,
981 VK_QUEUE_FAMILY_IGNORED,
982 vk_image,
983 {aspect_mask,
984 subimage.mipmap_level,
985 subimage.mipmap_count,
986 subimage.layer_base,
987 subimage.layer_count}});
988}
989
991
992/* -------------------------------------------------------------------- */
995
996void VKCommandBuilder::ImageTracker::begin(const VKRenderGraph &render_graph,
997 NodeHandle node_handle)
998{
999 BLI_assert(render_graph.nodes_[node_handle].type == VKNodeType::BEGIN_RENDERING);
1000 tracked_attachments.clear();
1001 changes.clear();
1002
1003 for (const VKRenderGraphImage &link : render_graph.linked_images(node_handle)) {
1004 VKResourceStateTracker::Resource &resource = render_graph.resources_.get_image_resource(
1005 link.resource.handle);
1006 if (resource.use_subresource_tracking()) {
1007 tracked_attachments.add(resource.image.vk_image);
1008 }
1009 }
1010}
1011
1012void VKCommandBuilder::ImageTracker::update(VkImage vk_image,
1013 const VKSubImageRange &subimage,
1014 VkImageLayout old_layout,
1015 VkImageLayout new_layout,
1016 VkImageAspectFlags aspect,
1017 Barrier &r_barrier)
1018{
1019 for (const SubImageChange &change : changes) {
1020 if (change.vk_image == vk_image && ((subimage.layer_count != VK_REMAINING_ARRAY_LAYERS &&
1021 change.subimage.layer_base == subimage.layer_base) ||
1022 (subimage.mipmap_count != VK_REMAINING_MIP_LEVELS &&
1023 change.subimage.mipmap_level == subimage.mipmap_level)))
1024 {
1026 change.vk_image_layout == new_layout,
1027 "We don't support more that one change of the same subimage multiple times during a "
1028 "rendering scope.");
1029 /* Early exit as layer is in correct layout. This is a normal case as we expect multiple
1030 * draw commands to take place during a rendering scope with the same layer access. */
1031 return;
1032 }
1033 }
1034
1035 changes.append({vk_image, new_layout, aspect, subimage});
1036
1037 /* We should be able to do better. BOTTOM/TOP is really a worst case barrier. */
1038 r_barrier.src_stage_mask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1039 r_barrier.dst_stage_mask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1040 VkAccessFlags dst_access_mask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT |
1041 VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
1042 if (aspect & VK_IMAGE_ASPECT_COLOR_BIT) {
1043 dst_access_mask |= VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
1044 }
1045 if (aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
1046 dst_access_mask |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
1047 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
1048 }
1049 /* NOTE: src_access_mask uses a conservative VK_ACCESS_TRANSFER_WRITE_BIT as a
1050 * placeholder. The actual prior access flags are added by the sync barrier logic
1051 * in add_image_read_barriers/add_image_write_barriers (via wait_access from the
1052 * resource state tracker) and merged into this barrier by add_image_barrier's merge.
1053 * TODO: Pass the actual tracked wait_access here instead, removing the redundant
1054 * sync barrier and this placeholder. */
1055 command_builder.add_image_barrier(vk_image,
1056 r_barrier,
1057 VK_ACCESS_TRANSFER_WRITE_BIT,
1058 dst_access_mask,
1059 old_layout,
1060 new_layout,
1061 aspect,
1062 subimage);
1063}
1064
1065void VKCommandBuilder::ImageTracker::end(Barrier &r_barrier, bool use_local_read)
1066{
1067 suspend(r_barrier, use_local_read);
1068 tracked_attachments.clear();
1069 changes.clear();
1070}
1071
1072void VKCommandBuilder::ImageTracker::suspend(Barrier &r_barrier, bool use_local_read)
1073
1074{
1075 if (changes.is_empty()) {
1076 return;
1077 }
1078
1079 command_builder.reset_barriers(r_barrier);
1080 /* We should be able to do better. BOTTOM/TOP is really a worst case barrier. */
1081 r_barrier.src_stage_mask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1082 r_barrier.dst_stage_mask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1083 int64_t start_index = command_builder.vk_image_memory_barriers_.size();
1084 r_barrier.image_memory_barriers = IndexRange::from_begin_size(start_index, 0);
1085 for (const SubImageChange &change : changes) {
1086 command_builder.add_image_barrier(
1087 change.vk_image,
1088 r_barrier,
1089 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT |
1090 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
1091 VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT,
1092 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT |
1093 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
1094 VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT,
1095 change.vk_image_layout,
1096 to_default_image_layout(change.vk_image_aspect, use_local_read),
1097 change.vk_image_aspect,
1098 change.subimage);
1099 r_barrier.image_memory_barriers = r_barrier.image_memory_barriers.with_new_end(
1100 command_builder.vk_image_memory_barriers_.size());
1101
1102#if 0
1103 std::cout << __func__ << ": transition layout image=" << change.vk_image
1104 << ", layer=" << change.subimage.layer_base
1105 << ", count=" << change.subimage.layer_count
1106 << ", from_layout=" << to_string(change.vk_image_layout)
1107 << ", to_layout=" << to_string(
1108 to_default_image_layout(change.vk_image_aspect, use_local_read))
1109 << "\n";
1110#endif
1111 }
1112}
1113
1114void VKCommandBuilder::ImageTracker::resume(Barrier &r_barrier, bool use_local_read)
1115{
1116 if (changes.is_empty()) {
1117 return;
1118 }
1119
1120 command_builder.reset_barriers(r_barrier);
1121 /* We should be able to do better. BOTTOM/TOP is really a worst case barrier. */
1122 r_barrier.src_stage_mask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1123 r_barrier.dst_stage_mask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1124 int64_t start_index = command_builder.vk_image_memory_barriers_.size();
1125 r_barrier.image_memory_barriers = IndexRange::from_begin_size(start_index, 0);
1126
1127 for (const SubImageChange &change : changes) {
1128 command_builder.add_image_barrier(
1129 change.vk_image,
1130 r_barrier,
1131 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT |
1132 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
1133 VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT,
1134 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT |
1135 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
1136 VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT,
1137 to_default_image_layout(change.vk_image_aspect, use_local_read),
1138 change.vk_image_layout,
1139 change.vk_image_aspect,
1140 change.subimage);
1141#if 0
1142 std::cout << __func__ << ": transition layout image=" << change.vk_image
1143 << ", layer=" << change.subimage.layer_base
1144 << ", count=" << change.subimage.layer_count
1145 << ", from_layout=" << to_string(
1146 to_default_image_layout(change.vk_image_aspect, use_local_read))
1147 << ", to_layout=" << to_string(change.vk_image_layout) << "\n";
1148#endif
1149 }
1150}
1152
1153/* -------------------------------------------------------------------- */
1156
1157std::string VKCommandBuilder::to_string_barrier(const Barrier &barrier)
1158{
1159 std::stringstream ss;
1160 ss << "src_stage_mask=" << to_string_vk_pipeline_stage_flags(barrier.src_stage_mask)
1161 << ", dst_stage_mask=" << to_string_vk_pipeline_stage_flags(barrier.dst_stage_mask) << "\n";
1162 for (const VkBufferMemoryBarrier &buffer_memory_barrier :
1163 vk_buffer_memory_barriers_.as_span().slice(barrier.buffer_memory_barriers))
1164 {
1165 ss << " - src_access_mask=" << to_string_vk_access_flags(buffer_memory_barrier.srcAccessMask)
1166 << ", dst_access_mask=" << to_string_vk_access_flags(buffer_memory_barrier.dstAccessMask)
1167 << ", vk_buffer=" << to_string(buffer_memory_barrier.buffer) << "\n";
1168 }
1169
1170 for (const VkImageMemoryBarrier &image_memory_barrier :
1171 vk_image_memory_barriers_.as_span().slice(barrier.image_memory_barriers))
1172 {
1173 ss << " - src_access_mask=" << to_string_vk_access_flags(image_memory_barrier.srcAccessMask)
1174 << ", dst_access_mask=" << to_string_vk_access_flags(image_memory_barrier.dstAccessMask)
1175 << ", vk_image=" << to_string(image_memory_barrier.image)
1176 << ", old_layout=" << to_string(image_memory_barrier.oldLayout)
1177 << ", new_layout=" << to_string(image_memory_barrier.newLayout)
1178 << ", subresource_range=" << to_string(image_memory_barrier.subresourceRange, 2) << "\n";
1179 }
1180
1181 return ss.str();
1182}
1183
1185
1186} // namespace blender::gpu::render_graph
#define BLI_assert(a)
#define BLI_assert_msg(a, msg)
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr const T * begin() const
Definition BLI_span.hh:224
constexpr int64_t first() const
constexpr int64_t size() const
constexpr bool is_empty() const
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr IndexRange()=default
static constexpr IndexRange from_begin_size(const int64_t begin, const int64_t size)
constexpr IndexRange slice(int64_t start, int64_t size) const
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
static void reconfigure_for_restart(VKBeginRenderingData &begin_rendering_data)
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)
#define index(i)
void barrier()
const int state
BLI_INLINE bool node_type_is_rendering(VKNodeType node_type)
BLI_INLINE bool node_type_is_within_rendering(VKNodeType node_type)
static VkImageLayout to_default_image_layout(VkImageAspectFlags aspect, bool use_local_read)
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
std::string to_string_vk_pipeline_stage_flags(const VkPipelineStageFlags vk_pipeline_stage_flags)
std::string to_string_vk_access_flags(const VkAccessFlags vk_access_flags)
__int64 int64_t
Definition BKE_text.h:90
MINLINE int max_ii(int a, int b)
Global G
Definition blender.cc:57
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE int min_ii(int a, int b)