Blender V5.3
vk_framebuffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_color_c.hh"
10
11#include "vk_backend.hh"
12#include "vk_context.hh"
13#include "vk_framebuffer.hh"
14#include "vk_state_manager.hh"
15#include "vk_texture.hh"
16
17namespace blender::gpu {
18
23{
24 return {GPU_LOADACTION_LOAD, GPU_STOREACTION_STORE, {0.0f, 0.0f, 0.0f, 0.0f}};
25}
26
27/* -------------------------------------------------------------------- */
30
32 : FrameBuffer(name),
35{
36 size_set(1, 1);
37 srgb_ = false;
38 enabled_srgb_ = false;
39}
40
42{
43 VKContext *context = VKContext::get();
44 if (context && context->active_framebuffer_get() == this) {
45 context->deactivate_framebuffer();
46 }
47}
48
50
51void VKFrameBuffer::bind(bool enabled_srgb)
52{
53 VKContext &context = *VKContext::get();
54 /* Updating attachments can issue pipeline barriers, this should be done outside the render pass.
55 * When done inside a render pass there should be a self-dependency between sub-passes on the
56 * active render pass. As the active render pass isn't aware of the new render pass (and should
57 * not) it is better to deactivate it before updating the attachments. For more information check
58 * `VkSubpassDependency`. */
59 if (context.has_active_framebuffer()) {
60 context.deactivate_framebuffer();
61 }
62
63 context.activate_framebuffer(*this);
67
68 enabled_srgb_ = enabled_srgb;
69 Shader::set_framebuffer_srgb_target(enabled_srgb && srgb_);
70 load_stores.fill(default_load_store());
71 attachment_states_.fill(GPU_ATTACHMENT_WRITE);
72}
73
75{
76 return this->multi_viewport_ ? GPU_MAX_VIEWPORTS : 1;
77}
78
80{
81 BLI_assert(r_viewports.is_empty());
82 for (int64_t index : IndexRange(this->multi_viewport_ ? GPU_MAX_VIEWPORTS : 1)) {
83 VkViewport viewport;
84 viewport.x = viewport_[index][0];
85 viewport.y = viewport_[index][1];
86 viewport.width = viewport_[index][2];
87 viewport.height = viewport_[index][3];
88 viewport.minDepth = 0.0f;
89 viewport.maxDepth = 1.0f;
90 r_viewports.append(viewport);
91 }
92}
93
94void VKFrameBuffer::render_area_update(VkRect2D &render_area) const
95{
96 if (scissor_test_get()) {
97 int scissor_rect[4];
98 scissor_get(scissor_rect);
99 render_area.offset.x = clamp_i(scissor_rect[0], 0, width_);
100 render_area.offset.y = clamp_i(scissor_rect[1], 0, height_);
101 render_area.extent.width = clamp_i(scissor_rect[2], 1, max_ii(1, width_ - scissor_rect[0]));
102 render_area.extent.height = clamp_i(scissor_rect[3], 1, max_ii(1, height_ - scissor_rect[1]));
103 }
104 else {
105 render_area.offset.x = 0;
106 render_area.offset.y = 0;
107 render_area.extent.width = width_;
108 render_area.extent.height = height_;
109 }
110
111#ifndef NDEBUG
112 const VKDevice &device = VKBackend::get().device;
113 BLI_assert(render_area.offset.x + render_area.extent.width <=
114 device.physical_device_properties_get().limits.maxFramebufferWidth);
115 BLI_assert(render_area.offset.y + render_area.extent.height <=
116 device.physical_device_properties_get().limits.maxFramebufferHeight);
117#endif
118}
119
121 Vector<VkRect2D, GPU_MAX_VIEWPORTS> &r_render_areas) const
122{
123 BLI_assert(r_render_areas.is_empty());
124 VkRect2D render_area;
125 render_area_update(render_area);
126 r_render_areas.append_n_times(render_area, this->multi_viewport_ ? GPU_MAX_VIEWPORTS : 1);
127}
128
129bool VKFrameBuffer::check(char /*err_out*/[256])
130{
131 return true;
132}
133
134void VKFrameBuffer::build_clear_attachments_depth_stencil(
135 const GPUFrameBufferBits buffers,
136 float clear_depth,
137 uint32_t clear_stencil,
139{
140 VkImageAspectFlags aspect_mask = (buffers & GPU_DEPTH_BIT ? VK_IMAGE_ASPECT_DEPTH_BIT : 0) |
141 (buffers & GPU_STENCIL_BIT ? VK_IMAGE_ASPECT_STENCIL_BIT : 0);
142
143 VkClearAttachment &clear_attachment =
144 clear_attachments.attachments[clear_attachments.attachment_count++];
145 clear_attachment.aspectMask = aspect_mask;
146 clear_attachment.clearValue.depthStencil.depth = clear_depth;
147 clear_attachment.clearValue.depthStencil.stencil = clear_stencil;
148 clear_attachment.colorAttachment = 0;
149}
150
151void VKFrameBuffer::build_clear_attachments_color(
152 Span<double4> clear_colors,
154{
155 const bool missing_srgb_clear_conversion =
157 for (int color_slot = 0; color_slot < GPU_FB_MAX_COLOR_ATTACHMENT; color_slot++) {
158 const GPUAttachment &attachment = attachments_[GPU_FB_COLOR_ATTACHMENT0 + color_slot];
159 if (attachment.tex == nullptr) {
160 continue;
161 }
162 VKTexture *color_texture = unwrap(attachment.tex);
163 const bool is_srgb = color_texture &&
164 (color_texture->format_flag_get() & GPU_FORMAT_SRGB) != 0;
165 VkClearAttachment &clear_attachment =
166 clear_attachments.attachments[clear_attachments.attachment_count++];
167 clear_attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
168 clear_attachment.colorAttachment = color_slot;
170 double4 clear_color = clear_colors[color_slot];
171 /* Legacy AMD drivers don't perform the sRGB conversion when clearing an sRGB attachment, so
172 * the clear color is pre-encoded into sRGB space. Non-sRGB attachments keep the linear clear
173 * color. */
174 if (srgb_ && missing_srgb_clear_conversion && is_srgb) {
175 clear_color.x = srgb_to_linearrgb(float(clear_color.x));
176 clear_color.y = srgb_to_linearrgb(float(clear_color.y));
177 clear_color.z = srgb_to_linearrgb(float(clear_color.z));
178 }
179 clear_attachment.clearValue.color = to_vk_clear_color_value(data_format, clear_color);
180 }
181}
182
183/* -------------------------------------------------------------------- */
186
188{
189 VKContext &context = *VKContext::get();
190 rendering_ensure(context);
191 context.render_graph().add_node(clear_attachments);
192}
193
195 const double4 clear_color,
196 float clear_depth,
197 uint clear_stencil)
198{
200 render_area_update(clear_attachments.vk_clear_rect.rect);
201 clear_attachments.vk_clear_rect.baseArrayLayer = 0;
202 clear_attachments.vk_clear_rect.layerCount = 1;
203
204 if (buffers & (GPU_DEPTH_BIT | GPU_STENCIL_BIT)) {
205 VKContext &context = *VKContext::get();
206 GPUWriteMask needed_mask = GPU_WRITE_NONE;
207 if (buffers & GPU_DEPTH_BIT) {
208 needed_mask |= GPU_WRITE_DEPTH;
209 }
210 if (buffers & GPU_STENCIL_BIT) {
211 needed_mask |= GPU_WRITE_STENCIL;
212 }
213
214 /* Clearing depth via #vkCmdClearAttachments requires a render pass with write depth or stencil
215 * enabled. When not enabled, clearing should be done via texture directly. */
216 /* WORKAROUND: Clearing depth attachment when using dynamic rendering are not working on AMD
217 * official drivers.
218 * See #129265 */
219 if ((context.state_manager_get().state.write_mask & needed_mask) == needed_mask &&
221 {
222 build_clear_attachments_depth_stencil(
223 buffers, clear_depth, clear_stencil, clear_attachments);
224 }
225 else {
226 const GPUAttachment &attachment = depth_attachment();
227 VKTexture *depth_texture = unwrap(unwrap(attachment.tex));
228 if (depth_texture != nullptr) {
229 depth_texture->clear_depth_stencil(
230 buffers,
231 clear_depth,
232 clear_stencil,
233 attachment.layer == -1 ? std::nullopt : std::make_optional(attachment.layer));
234 }
235 }
236 }
237
238 if (buffers & GPU_COLOR_BIT) {
240 for (int color_slot = 0; color_slot < GPU_FB_MAX_COLOR_ATTACHMENT; color_slot++) {
241 clear_colors[color_slot] = clear_color;
242 }
243 build_clear_attachments_color(Span<double4>(clear_colors, GPU_FB_MAX_COLOR_ATTACHMENT),
244 clear_attachments);
245 }
246
247 if (clear_attachments.attachment_count) {
248 clear(clear_attachments);
249 }
250}
251
253{
255 render_area_update(clear_attachments.vk_clear_rect.rect);
256 clear_attachments.vk_clear_rect.baseArrayLayer = 0;
257 clear_attachments.vk_clear_rect.layerCount = 1;
258
259 build_clear_attachments_color(clear_cols, clear_attachments);
260 if (clear_attachments.attachment_count) {
261 clear(clear_attachments);
262 }
263}
264
265void VKFrameBuffer::clear_attachment(GPUAttachmentType /*type*/, const double4 /*clear_value*/)
266{
267 /* Clearing of a single attachment was added to implement `clear_multi` in OpenGL. As
268 * `clear_multi` is supported in Vulkan it isn't needed to implement this method.
269 */
271}
272
274
275/* -------------------------------------------------------------------- */
278
280{
281 load_stores[type] = ls;
282}
283
284static VkAttachmentLoadOp to_vk_attachment_load_op(GPULoadOp load_op)
285{
286 switch (load_op) {
288 return VK_ATTACHMENT_LOAD_OP_DONT_CARE;
290 return VK_ATTACHMENT_LOAD_OP_CLEAR;
292 return VK_ATTACHMENT_LOAD_OP_LOAD;
293 }
295 return VK_ATTACHMENT_LOAD_OP_LOAD;
296}
297
298static VkAttachmentStoreOp to_vk_attachment_store_op(GPUStoreOp store_op)
299{
300 switch (store_op) {
302 return VK_ATTACHMENT_STORE_OP_DONT_CARE;
304 return VK_ATTACHMENT_STORE_OP_STORE;
305 }
307 return VK_ATTACHMENT_STORE_OP_STORE;
308}
309
310static void set_load_store(VkRenderingAttachmentInfo &r_rendering_attachment,
311 eGPUDataFormat data_format,
312 const GPULoadStore &ls)
313{
314 r_rendering_attachment.clearValue.color = to_vk_clear_color_value(data_format, ls.clear_value);
315 r_rendering_attachment.loadOp = to_vk_attachment_load_op(ls.load_action);
316 r_rendering_attachment.storeOp = to_vk_attachment_store_op(ls.store_action);
317}
318
320
321/* -------------------------------------------------------------------- */
324
326 Span<GPUAttachmentState> color_attachment_states)
327{
328 const VKDevice &device = VKBackend::get().device;
329 const bool supports_local_read = device.extensions_get().dynamic_rendering_local_read;
330
331 attachment_states_[GPU_FB_DEPTH_ATTACHMENT] = depth_attachment_state;
332 attachment_states_.as_mutable_span()
333 .slice(GPU_FB_COLOR_ATTACHMENT0, color_attachment_states.size())
334 .copy_from(color_attachment_states);
335
336 if (supports_local_read) {
337 VKContext &context = *VKContext::get();
338 rendering_ensure(context);
339
340 for (int index : IndexRange(color_attachment_states.size())) {
341 if (color_attachment_states[index] == GPU_ATTACHMENT_READ) {
343 if (texture) {
344 context.state_manager_get().image_bind(texture, index);
345 }
346 }
347 }
348 is_rendering_ = false;
349 }
350 else {
351 VKContext &context = *VKContext::get();
352 rendering_end(context);
353
354 for (int index : IndexRange(color_attachment_states.size())) {
355 if (color_attachment_states[index] == GPU_ATTACHMENT_READ) {
357 if (texture) {
358 context.state_manager_get().texture_bind(
360 }
361 }
362 }
363 }
364}
365
367
368/* -------------------------------------------------------------------- */
371
373 eGPUDataFormat format,
374 const int area[4],
375 int /*channel_len*/,
376 int slot,
377 void *r_data)
378{
379 GPUAttachment *attachment = nullptr;
380 switch (plane) {
381 case GPU_COLOR_BIT:
382 attachment = &attachments_[GPU_FB_COLOR_ATTACHMENT0 + slot];
383 break;
384
385 case GPU_DEPTH_BIT:
389 break;
390
391 default:
393 return;
394 }
395
396 VKTexture *texture = unwrap(unwrap(attachment->tex));
398 "Trying to read back texture from framebuffer, but no texture is available in "
399 "requested slot.");
400 if (texture == nullptr) {
401 return;
402 }
403 const int region[6] = {area[0], area[1], 0, area[0] + area[2], area[1] + area[3], 1};
404 IndexRange layers(max_ii(attachment->layer, 0), 1);
405 texture->read_sub(0, format, region, layers, r_data);
406}
407
409
410/* -------------------------------------------------------------------- */
413
414static void blit_aspect(VKContext &context,
415 VKTexture &dst_texture,
416 VKTexture &src_texture,
417 int dst_offset_x,
418 int dst_offset_y,
419 VkImageAspectFlags image_aspect)
420{
421 /* Prefer texture copy, as some platforms don't support using D32_SFLOAT_S8_UINT to be used as
422 * a blit destination. */
423 if (dst_offset_x == 0 && dst_offset_y == 0 &&
424 dst_texture.device_format_get() == src_texture.device_format_get() &&
425 src_texture.width_get() == dst_texture.width_get() &&
426 src_texture.height_get() == dst_texture.height_get())
427 {
428 src_texture.copy_to(dst_texture, IndexRange(1), image_aspect);
429 return;
430 }
431
433
434 blit_image.src_image = src_texture.vk_image_handle();
435 blit_image.dst_image = dst_texture.vk_image_handle();
436 blit_image.filter = VK_FILTER_NEAREST;
437
438 VkImageBlit &region = blit_image.region;
439 region.srcSubresource.aspectMask = image_aspect;
440 region.srcSubresource.mipLevel = 0;
441 region.srcSubresource.baseArrayLayer = 0;
442 region.srcSubresource.layerCount = 1;
443 region.srcOffsets[0].x = 0;
444 region.srcOffsets[0].y = 0;
445 region.srcOffsets[0].z = 0;
446 region.srcOffsets[1].x = src_texture.width_get();
447 region.srcOffsets[1].y = src_texture.height_get();
448 region.srcOffsets[1].z = 1;
449
450 region.dstSubresource.aspectMask = image_aspect;
451 region.dstSubresource.mipLevel = 0;
452 region.dstSubresource.baseArrayLayer = 0;
453 region.dstSubresource.layerCount = 1;
454 region.dstOffsets[0].x = clamp_i(dst_offset_x, 0, dst_texture.width_get());
455 region.dstOffsets[0].y = clamp_i(dst_offset_y, 0, dst_texture.height_get());
456 region.dstOffsets[0].z = 0;
457 region.dstOffsets[1].x = clamp_i(
458 dst_offset_x + src_texture.width_get(), 0, dst_texture.width_get());
459 region.dstOffsets[1].y = clamp_i(
460 dst_offset_y + src_texture.height_get(), 0, dst_texture.height_get());
461 region.dstOffsets[1].z = 1;
462
463 /* Early exit when no pixels needs to be blitted. This should never occur, but has happened
464 * during development as retina displays are not yet detected and the intermediate back-buffer
465 * would be to small, resulting in cropping the full blit source image. */
466 if (region.dstOffsets[0].x == region.dstOffsets[1].x ||
467 region.dstOffsets[0].y == region.dstOffsets[1].y ||
468 region.dstOffsets[0].z == region.dstOffsets[1].z)
469 {
470 return;
471 }
472
473 context.render_graph().add_node(blit_image);
474}
475
477 int src_slot,
478 FrameBuffer *dst,
479 int dst_slot,
480 int dst_offset_x,
481 int dst_offset_y)
482{
483 BLI_assert(dst);
485 "VKFrameBuffer::blit_to only supports a single color or depth aspect.");
486 UNUSED_VARS_NDEBUG(planes);
487
488 VKContext &context = *VKContext::get();
489 if (!context.has_active_framebuffer()) {
491 return;
492 }
493
494 VKFrameBuffer &dst_framebuffer = *unwrap(dst);
495 if (planes & GPU_COLOR_BIT) {
496 const GPUAttachment &src_attachment = attachments_[GPU_FB_COLOR_ATTACHMENT0 + src_slot];
497 const GPUAttachment &dst_attachment =
498 dst_framebuffer.attachments_[GPU_FB_COLOR_ATTACHMENT0 + dst_slot];
499 if (src_attachment.tex && dst_attachment.tex) {
500 VKTexture &src_texture = *unwrap(unwrap(src_attachment.tex));
501 VKTexture &dst_texture = *unwrap(unwrap(dst_attachment.tex));
502 blit_aspect(context,
503 dst_texture,
504 src_texture,
505 dst_offset_x,
506 dst_offset_y,
507 VK_IMAGE_ASPECT_COLOR_BIT);
508 }
509 }
510
511 if (planes & GPU_DEPTH_BIT) {
512 /* Retrieve source texture. */
516 const GPUAttachment &dst_attachment =
519 dst_framebuffer.attachments_[GPU_FB_DEPTH_ATTACHMENT];
520 if (src_attachment.tex && dst_attachment.tex) {
521 VKTexture &src_texture = *unwrap(unwrap(src_attachment.tex));
522 VKTexture &dst_texture = *unwrap(unwrap(dst_attachment.tex));
523 blit_aspect(context,
524 dst_texture,
525 src_texture,
526 dst_offset_x,
527 dst_offset_y,
528 VK_IMAGE_ASPECT_DEPTH_BIT);
529 }
530 }
531}
532
534
535/* -------------------------------------------------------------------- */
538
540{
541 if (!dirty_attachments_) {
542 return;
543 }
544
545 for (int i = 0; i < GPU_FB_MAX_ATTACHMENT; i++) {
546 GPUAttachment &attachment = attachments_[i];
547 if (attachment.tex) {
548 int size[3];
549 GPU_texture_get_mipmap_size(attachment.tex, attachment.mip, size);
550 size_set(size[0], size[1]);
551 return;
552 }
553 }
554}
555
557{
560 if (texture) {
561 srgb_ = (texture->format_flag_get() & GPU_FORMAT_SRGB) != 0;
562 return;
563 }
564 }
565}
566
568{
569 int size = 0;
570 for (int color_slot : IndexRange(GPU_FB_MAX_COLOR_ATTACHMENT)) {
571 if (color_tex(color_slot) != nullptr) {
572 size = max_ii(color_slot + 1, size);
573 }
574 }
575 return size;
576}
577
579
581{
582 is_rendering_ = false;
583}
584
586 const VKExtensions &extensions)
587{
588 const VKDevice &device = VKBackend::get().device;
589 const bool supports_local_read = device.extensions_get().dynamic_rendering_local_read;
590
591 depth_attachment_format_ = VK_FORMAT_UNDEFINED;
592 stencil_attachment_format_ = VK_FORMAT_UNDEFINED;
593
595 render_graph::VKBeginRenderingNode::CreateInfo begin_rendering(access_info);
596 begin_rendering.node_data.vk_rendering_info.sType = VK_STRUCTURE_TYPE_RENDERING_INFO;
597 begin_rendering.node_data.vk_rendering_info.layerCount = 1;
598 render_area_update(begin_rendering.node_data.vk_rendering_info.renderArea);
599
600 color_attachment_formats_.clear();
601 int32_t max_filled_slot_index = -1;
602 for (int color_attachment_index :
604 {
605 const GPUAttachment &attachment = attachments_[color_attachment_index];
606 if (attachment.tex == nullptr) {
607 color_attachment_formats_.append(VK_FORMAT_UNDEFINED);
608 VkRenderingAttachmentInfo &attachment_info =
609 begin_rendering.node_data.color_attachments[begin_rendering.node_data.vk_rendering_info
610 .colorAttachmentCount++];
611 attachment_info = {VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
612 nullptr,
613 VK_NULL_HANDLE,
614 VK_IMAGE_LAYOUT_UNDEFINED,
615 VK_RESOLVE_MODE_NONE,
616 VK_NULL_HANDLE,
617 VK_IMAGE_LAYOUT_UNDEFINED,
618 VK_ATTACHMENT_LOAD_OP_DONT_CARE,
619 VK_ATTACHMENT_STORE_OP_DONT_CARE};
620 continue;
621 }
622 max_filled_slot_index = color_attachment_index - GPU_FB_COLOR_ATTACHMENT0;
623
624 VKTexture &color_texture = *unwrap(unwrap(attachment.tex));
626 "Texture is used as an attachment, but doesn't have the "
627 "GPU_TEXTURE_USAGE_ATTACHMENT flag.");
628 /* To support `gpu_Layer` we need to set the layerCount to the number of layers it can
629 * access.
630 */
631 int layer_count = color_texture.layer_count();
632 if (attachment.layer == -1 && layer_count != 1) {
633 begin_rendering.node_data.vk_rendering_info.layerCount = max_ii(
634 begin_rendering.node_data.vk_rendering_info.layerCount, layer_count);
635 }
636
637 VkRenderingAttachmentInfo &attachment_info =
638 begin_rendering.node_data
639 .color_attachments[begin_rendering.node_data.vk_rendering_info.colorAttachmentCount++];
640 attachment_info.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO;
641
642 VkImageView vk_image_view = VK_NULL_HANDLE;
643 uint32_t layer_base = max_ii(attachment.layer, 0);
644 GPUAttachmentState attachment_state = attachment_states_[color_attachment_index];
645 VkFormat vk_format = to_vk_format(color_texture.device_format_get());
646 if (srgb_ && !enabled_srgb_) {
647 if (vk_format == VK_FORMAT_R8G8B8A8_SRGB) {
648 vk_format = VK_FORMAT_R8G8B8A8_UNORM;
649 }
650 if (vk_format == VK_FORMAT_R8G8B8_SRGB) {
651 vk_format = VK_FORMAT_R8G8B8_UNORM;
652 }
653 }
654 if (attachment_state == GPU_ATTACHMENT_WRITE) {
655 VKImageViewInfo image_view_info = {
657 IndexRange(layer_base,
658 layer_count != 1 ? max_ii(layer_count - layer_base, 1) : layer_count),
659 IndexRange(attachment.mip, 1),
660 {{'r', 'g', 'b', 'a'}},
662 vk_format,
663 VK_IMAGE_ASPECT_COLOR_BIT};
664 const VKImageView &image_view = color_texture.image_view_get(image_view_info);
665 vk_image_view = image_view.vk_handle();
666 }
667 attachment_info.imageView = vk_image_view;
668 attachment_info.imageLayout = supports_local_read ? VK_IMAGE_LAYOUT_RENDERING_LOCAL_READ_KHR :
669 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
670
671 eGPUDataFormat data_format = to_texture_data_format(color_texture.format_get());
672
673 set_load_store(attachment_info, data_format, load_stores[color_attachment_index]);
674
675 access_info.images.append(
676 {color_texture.vk_image_handle(),
677 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
678 VK_IMAGE_ASPECT_COLOR_BIT,
679 {uint32_t(attachment.mip),
680 1,
681 layer_base,
682 uint32_t(max_ii(layer_count - layer_base, 1))}});
683 color_attachment_formats_.append(
684 (!extensions.dynamic_rendering_unused_attachments && vk_image_view == VK_NULL_HANDLE) ?
685 VK_FORMAT_UNDEFINED :
686 vk_format);
687 }
688 uint32_t color_attachment_size = uint32_t(max_filled_slot_index + 1);
689 color_attachment_formats_.resize(color_attachment_size);
690
691 begin_rendering.node_data.vk_rendering_info.colorAttachmentCount = color_attachment_size;
692 begin_rendering.node_data.vk_rendering_info.pColorAttachments =
693 begin_rendering.node_data.color_attachments;
694
695 for (int depth_attachment_index : IndexRange(GPU_FB_DEPTH_ATTACHMENT, 2)) {
696 const GPUAttachment &attachment = attachments_[depth_attachment_index];
697
698 if (attachment.tex == nullptr) {
699 continue;
700 }
701 bool is_stencil_attachment = depth_attachment_index == GPU_FB_DEPTH_STENCIL_ATTACHMENT;
702 VKTexture &depth_texture = *unwrap(unwrap(attachment.tex));
704 "Texture is used as an attachment, but doesn't have the "
705 "GPU_TEXTURE_USAGE_ATTACHMENT flag.");
706 bool is_depth_stencil_attachment = to_vk_image_aspect_flag_bits(
707 depth_texture.device_format_get()) &
708 VK_IMAGE_ASPECT_STENCIL_BIT;
709 VkImageLayout vk_image_layout = is_depth_stencil_attachment ?
710 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL :
711 VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL;
712 GPUAttachmentState attachment_state = attachment_states_[GPU_FB_DEPTH_ATTACHMENT];
713 VkImageView depth_image_view = VK_NULL_HANDLE;
714 if (attachment_state == GPU_ATTACHMENT_WRITE) {
715 VKImageViewInfo image_view_info = {
717 IndexRange(max_ii(attachment.layer, 0), 1),
718 IndexRange(attachment.mip, 1),
719 {{'r', 'g', 'b', 'a'}},
721 to_vk_format(depth_texture.device_format_get()),
722 is_stencil_attachment ? static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_STENCIL_BIT) :
723 static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_DEPTH_BIT)};
724 depth_image_view = depth_texture.image_view_get(image_view_info).vk_handle();
725 }
726 VkFormat vk_format = (!extensions.dynamic_rendering_unused_attachments &&
727 depth_image_view == VK_NULL_HANDLE) ?
728 VK_FORMAT_UNDEFINED :
729 to_vk_format(depth_texture.device_format_get());
730
731 /* TODO: we should be able to use a single attachment info and only set the
732 * #pDepthAttachment/#pStencilAttachment to the same struct.
733 * But perhaps the stencil clear op might be different. */
734 {
735 VkRenderingAttachmentInfo &attachment_info = begin_rendering.node_data.depth_attachment;
736 attachment_info.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO;
737 attachment_info.imageView = depth_image_view;
738 attachment_info.imageLayout = vk_image_layout;
739
740 set_load_store(attachment_info, GPU_DATA_FLOAT, load_stores[depth_attachment_index]);
741 depth_attachment_format_ = vk_format;
742 begin_rendering.node_data.vk_rendering_info.pDepthAttachment =
743 &begin_rendering.node_data.depth_attachment;
744 }
745
746 if (is_stencil_attachment) {
747 VkRenderingAttachmentInfo &attachment_info = begin_rendering.node_data.stencil_attachment;
748 attachment_info.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO;
749 attachment_info.imageView = depth_image_view;
750 attachment_info.imageLayout = vk_image_layout;
751
752 set_load_store(attachment_info, GPU_DATA_UINT, load_stores[depth_attachment_index]);
753 stencil_attachment_format_ = vk_format;
754 begin_rendering.node_data.vk_rendering_info.pStencilAttachment =
755 &begin_rendering.node_data.stencil_attachment;
756 }
757
758 access_info.images.append(
759 {depth_texture.vk_image_handle(),
760 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
761 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
762 is_depth_stencil_attachment ?
763 static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_DEPTH_BIT |
764 VK_IMAGE_ASPECT_STENCIL_BIT) :
765 static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_DEPTH_BIT),
766 {uint32_t(attachment.mip), 1, uint32_t(max_ii(attachment.layer, 0)), 1}});
767 break;
768 }
769
770 context.render_graph().add_node(begin_rendering);
771}
772
774{
775 if (!dirty_state_ && is_rendering_) {
776 return;
777 }
778
779 if (is_rendering_) {
780 rendering_end(context);
781 }
782
783 const VKExtensions &extensions = VKBackend::get().device.extensions_get();
784 is_rendering_ = true;
785 rendering_ensure_dynamic_rendering(context, extensions);
786 dirty_attachments_ = false;
787 dirty_state_ = false;
789 load_stores.fill(default_load_store());
790}
791
793{
794 return depth_attachment_format_;
795}
797{
798 return stencil_attachment_format_;
799};
801{
802 return color_attachment_formats_.as_span();
803}
804
806{
807 if (!is_rendering_ && use_explicit_load_store_) {
808 rendering_ensure(context);
809 }
810
811 if (is_rendering_) {
813 context.render_graph().add_node(end_rendering);
814 is_rendering_ = false;
815 }
816}
817
818} // namespace blender::gpu
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
void size_set(int width, int height)
gpu::Texture * color_tex(int slot) const
const GPUAttachment & depth_attachment() const
void scissor_get(int r_scissor[4]) const
FrameBuffer(const char *name)
GPUAttachment attachments_[GPU_FB_MAX_ATTACHMENT]
int viewport_[GPU_MAX_VIEWPORTS][4]
static void set_framebuffer_srgb_target(int use_srgb_to_linear)
eGPUTextureUsage usage_get() const
TextureFormat format_get() const
static VKBackend & get()
static VKContext * get()
const VKExtensions & extensions_get() const
Definition vk_device.hh:398
const VkPhysicalDeviceProperties & physical_device_properties_get() const
Definition vk_device.hh:294
const VKWorkarounds & workarounds_get() const
Definition vk_device.hh:394
void rendering_ensure_dynamic_rendering(VKContext &context, const VKExtensions &extensions)
void rendering_ensure(VKContext &context)
VKFrameBuffer(const char *name)
VkFormat stencil_attachment_format_get() const
void subpass_transition_impl(const GPUAttachmentState depth_attachment_state, Span< GPUAttachmentState > color_attachment_states) override
void read(GPUFrameBufferBits planes, eGPUDataFormat format, const int area[4], int channel_len, int slot, void *r_data) override
void render_area_update(VkRect2D &render_area) const
void bind(bool enabled_srgb) override
VkFormat depth_attachment_format_get() const
void clear_multi(Span< double4 > clear_cols) override
void blit_to(GPUFrameBufferBits planes, int src_slot, FrameBuffer *dst, int dst_slot, int dst_offset_x, int dst_offset_y) override
void clear(GPUFrameBufferBits buffers, const double4 clear_color, float clear_depth, uint clear_stencil) override
void rendering_end(VKContext &context)
void vk_viewports_append(Vector< VkViewport, GPU_MAX_VIEWPORTS > &r_viewports) const
bool check(char err_out[256]) override
void attachment_set_loadstore_op(GPUAttachmentType type, GPULoadStore) override
int color_attachments_resource_size() const
void vk_render_areas_append(Vector< VkRect2D, GPU_MAX_VIEWPORTS > &r_render_areas) const
void clear_attachment(GPUAttachmentType type, const double4 clear_value) override
Span< VkFormat > color_attachment_formats_get() const
Get color attachment formats in used by the framebuffer.
VkImageView vk_handle() const
void copy_to(Texture *texture, IndexRange mip_levels) override
void clear_depth_stencil(const GPUFrameBufferBits buffer, float clear_depth, uint clear_stencil, std::optional< int > layer)
TextureFormat device_format_get() const
VkImage vk_image_handle() const
const VKImageView & image_view_get(const VKImageViewInfo &info)
#define GPU_FB_MAX_COLOR_ATTACHMENT
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define VK_FORMAT_R8G8B8A8_UNORM
#define VK_FORMAT_R8G8B8_SRGB
#define VK_FORMAT_R8G8B8A8_SRGB
#define VK_FORMAT_R8G8B8_UNORM
int context(const bContext *C, const char *member, bContextDataResult *result)
constexpr GPULoadStore default_load_store()
VkClearColorValue to_vk_clear_color_value(const eGPUDataFormat format, const double4 data)
Definition vk_common.cc:611
static VkAttachmentLoadOp to_vk_attachment_load_op(GPULoadOp load_op)
VkFormat to_vk_format(const TextureFormat format)
Definition vk_common.cc:157
eGPUDataFormat to_texture_data_format(TextureFormat tex_format)
BLI_INLINE MTLTexture * unwrap(Texture *tex)
static void set_load_store(VkRenderingAttachmentInfo &r_rendering_attachment, eGPUDataFormat data_format, const GPULoadStore &ls)
VkImageAspectFlags to_vk_image_aspect_flag_bits(const TextureFormat format)
Definition vk_common.cc:16
static void blit_aspect(VKContext &context, VKTexture &dst_texture, VKTexture &src_texture, int dst_offset_x, int dst_offset_y, VkImageAspectFlags image_aspect)
static VkAttachmentStoreOp to_vk_attachment_store_op(GPUStoreOp store_op)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
gpu::TextureFormat GPU_texture_format(const gpu::Texture *texture)
unsigned int uint
Definition BKE_text.h:55
float srgb_to_linearrgb(float c)
@ GPU_LOADACTION_DONT_CARE
MINLINE int max_ii(int a, int b)
@ GPU_DRIVER_OFFICIAL
@ GPU_STOREACTION_DONT_CARE
unsigned int uint32_t
Definition BKE_text.h:81
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
@ GPU_TEXTURE_USAGE_ATTACHMENT
@ GPU_WRITE_NONE
Definition GPU_state.hh:20
@ GPU_WRITE_STENCIL
Definition GPU_state.hh:26
@ GPU_WRITE_DEPTH
Definition GPU_state.hh:25
signed int int32_t
Definition BKE_text.h:78
MINLINE int clamp_i(int value, int min, int max)
VecBase< double, 4 > double4
void GPU_texture_get_mipmap_size(gpu::Texture *texture, int mip_level, int *r_size)
static constexpr GPUSamplerState default_sampler()