2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 #include "radv_meta.h"
28 #include "radv_private.h"
29 #include "vk_format.h"
30 #include "nir/nir_builder.h"
37 const struct glsl_type
*vec4
= glsl_vec4_type();
39 nir_variable
*f_color
; /* vec4, fragment output color */
41 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
42 b
.shader
->info
.name
= ralloc_asprintf(b
.shader
,
45 f_color
= nir_variable_create(b
.shader
, nir_var_shader_out
, vec4
,
47 f_color
->data
.location
= FRAG_RESULT_DATA0
;
48 nir_store_var(&b
, f_color
, nir_imm_vec4(&b
, 0.0, 0.0, 0.0, 1.0), 0xf);
54 create_pass(struct radv_device
*device
, VkFormat vk_format
, VkRenderPass
*pass
)
57 VkDevice device_h
= radv_device_to_handle(device
);
58 const VkAllocationCallbacks
*alloc
= &device
->meta_state
.alloc
;
59 VkAttachmentDescription attachments
[2];
62 for (i
= 0; i
< 2; i
++) {
63 attachments
[i
].format
= vk_format
;
64 attachments
[i
].samples
= 1;
65 attachments
[i
].loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
;
66 attachments
[i
].storeOp
= VK_ATTACHMENT_STORE_OP_STORE
;
68 attachments
[0].initialLayout
= VK_IMAGE_LAYOUT_GENERAL
;
69 attachments
[0].finalLayout
= VK_IMAGE_LAYOUT_GENERAL
;
70 attachments
[1].initialLayout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
71 attachments
[1].finalLayout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
73 result
= radv_CreateRenderPass(device_h
,
74 &(VkRenderPassCreateInfo
) {
75 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
77 .pAttachments
= attachments
,
79 .pSubpasses
= &(VkSubpassDescription
) {
80 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
81 .inputAttachmentCount
= 0,
82 .colorAttachmentCount
= 2,
83 .pColorAttachments
= (VkAttachmentReference
[]) {
86 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
90 .layout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
,
93 .pResolveAttachments
= NULL
,
94 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
95 .attachment
= VK_ATTACHMENT_UNUSED
,
97 .preserveAttachmentCount
= 0,
98 .pPreserveAttachments
= NULL
,
100 .dependencyCount
= 0,
109 create_pipeline(struct radv_device
*device
,
110 VkShaderModule vs_module_h
,
111 VkPipeline
*pipeline
,
115 VkDevice device_h
= radv_device_to_handle(device
);
117 struct radv_shader_module fs_module
= {
118 .nir
= build_nir_fs(),
121 if (!fs_module
.nir
) {
122 /* XXX: Need more accurate error */
123 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
127 VkPipelineLayoutCreateInfo pl_create_info
= {
128 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
131 .pushConstantRangeCount
= 0,
132 .pPushConstantRanges
= NULL
,
135 if (!device
->meta_state
.resolve
.p_layout
) {
136 result
= radv_CreatePipelineLayout(radv_device_to_handle(device
),
138 &device
->meta_state
.alloc
,
139 &device
->meta_state
.resolve
.p_layout
);
140 if (result
!= VK_SUCCESS
)
144 result
= radv_graphics_pipeline_create(device_h
,
145 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
146 &(VkGraphicsPipelineCreateInfo
) {
147 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
149 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
151 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
152 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
153 .module
= vs_module_h
,
157 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
158 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
159 .module
= radv_shader_module_to_handle(&fs_module
),
163 .pVertexInputState
= &(VkPipelineVertexInputStateCreateInfo
) {
164 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
165 .vertexBindingDescriptionCount
= 0,
166 .vertexAttributeDescriptionCount
= 0,
168 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
169 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
170 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
171 .primitiveRestartEnable
= false,
173 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
174 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
178 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
179 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
180 .depthClampEnable
= false,
181 .rasterizerDiscardEnable
= false,
182 .polygonMode
= VK_POLYGON_MODE_FILL
,
183 .cullMode
= VK_CULL_MODE_NONE
,
184 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
,
186 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
187 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
188 .rasterizationSamples
= 1,
189 .sampleShadingEnable
= false,
191 .alphaToCoverageEnable
= false,
192 .alphaToOneEnable
= false,
194 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
195 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
196 .logicOpEnable
= false,
197 .attachmentCount
= 2,
198 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
200 .colorWriteMask
= VK_COLOR_COMPONENT_R_BIT
|
201 VK_COLOR_COMPONENT_G_BIT
|
202 VK_COLOR_COMPONENT_B_BIT
|
203 VK_COLOR_COMPONENT_A_BIT
,
211 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
212 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
213 .dynamicStateCount
= 2,
214 .pDynamicStates
= (VkDynamicState
[]) {
215 VK_DYNAMIC_STATE_VIEWPORT
,
216 VK_DYNAMIC_STATE_SCISSOR
,
219 .layout
= device
->meta_state
.resolve
.p_layout
,
223 &(struct radv_graphics_pipeline_create_info
) {
224 .use_rectlist
= true,
225 .custom_blend_mode
= V_028808_CB_RESOLVE
,
227 &device
->meta_state
.alloc
, pipeline
);
228 if (result
!= VK_SUCCESS
)
234 ralloc_free(fs_module
.nir
);
239 radv_device_finish_meta_resolve_state(struct radv_device
*device
)
241 struct radv_meta_state
*state
= &device
->meta_state
;
243 for (uint32_t j
= 0; j
< NUM_META_FS_KEYS
; j
++) {
244 radv_DestroyRenderPass(radv_device_to_handle(device
),
245 state
->resolve
.pass
[j
], &state
->alloc
);
246 radv_DestroyPipeline(radv_device_to_handle(device
),
247 state
->resolve
.pipeline
[j
], &state
->alloc
);
249 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
250 state
->resolve
.p_layout
, &state
->alloc
);
255 radv_device_init_meta_resolve_state(struct radv_device
*device
, bool on_demand
)
260 VkResult res
= VK_SUCCESS
;
261 struct radv_meta_state
*state
= &device
->meta_state
;
262 struct radv_shader_module vs_module
= { .nir
= radv_meta_build_nir_vs_generate_vertices() };
263 if (!vs_module
.nir
) {
264 /* XXX: Need more accurate error */
265 res
= VK_ERROR_OUT_OF_HOST_MEMORY
;
269 for (uint32_t i
= 0; i
< NUM_META_FS_KEYS
; ++i
) {
270 VkFormat format
= radv_fs_key_format_exemplars
[i
];
271 unsigned fs_key
= radv_format_meta_fs_key(format
);
272 res
= create_pass(device
, format
, &state
->resolve
.pass
[fs_key
]);
273 if (res
!= VK_SUCCESS
)
276 VkShaderModule vs_module_h
= radv_shader_module_to_handle(&vs_module
);
277 res
= create_pipeline(device
, vs_module_h
,
278 &state
->resolve
.pipeline
[fs_key
], state
->resolve
.pass
[fs_key
]);
279 if (res
!= VK_SUCCESS
)
286 radv_device_finish_meta_resolve_state(device
);
289 ralloc_free(vs_module
.nir
);
295 emit_resolve(struct radv_cmd_buffer
*cmd_buffer
,
297 const VkOffset2D
*dest_offset
,
298 const VkExtent2D
*resolve_extent
)
300 struct radv_device
*device
= cmd_buffer
->device
;
301 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
302 unsigned fs_key
= radv_format_meta_fs_key(vk_format
);
304 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
;
306 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
307 device
->meta_state
.resolve
.pipeline
[fs_key
]);
309 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
312 .width
= resolve_extent
->width
,
313 .height
= resolve_extent
->height
,
318 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkRect2D
) {
319 .offset
= *dest_offset
,
320 .extent
= *resolve_extent
,
323 radv_CmdDraw(cmd_buffer_h
, 3, 1, 0, 0);
324 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
;
327 enum radv_resolve_method
{
333 static void radv_pick_resolve_method_images(struct radv_image
*src_image
,
335 struct radv_image
*dest_image
,
336 VkImageLayout dest_image_layout
,
337 struct radv_cmd_buffer
*cmd_buffer
,
338 enum radv_resolve_method
*method
)
341 uint32_t queue_mask
= radv_image_queue_family_mask(dest_image
,
342 cmd_buffer
->queue_family_index
,
343 cmd_buffer
->queue_family_index
);
345 if (vk_format_is_color(src_format
)) {
346 if (src_format
== VK_FORMAT_R16G16_UNORM
||
347 src_format
== VK_FORMAT_R16G16_SNORM
)
348 *method
= RESOLVE_COMPUTE
;
349 else if (vk_format_is_int(src_format
))
350 *method
= RESOLVE_COMPUTE
;
351 else if (src_image
->info
.array_size
> 1 ||
352 dest_image
->info
.array_size
> 1)
353 *method
= RESOLVE_COMPUTE
;
355 if (radv_layout_dcc_compressed(dest_image
, dest_image_layout
, queue_mask
)) {
356 *method
= RESOLVE_FRAGMENT
;
357 } else if (dest_image
->planes
[0].surface
.micro_tile_mode
!=
358 src_image
->planes
[0].surface
.micro_tile_mode
) {
359 *method
= RESOLVE_COMPUTE
;
362 if (src_image
->info
.array_size
> 1 ||
363 dest_image
->info
.array_size
> 1)
364 *method
= RESOLVE_COMPUTE
;
366 *method
= RESOLVE_FRAGMENT
;
371 build_resolve_pipeline(struct radv_device
*device
,
374 VkResult result
= VK_SUCCESS
;
376 if (device
->meta_state
.resolve
.pipeline
[fs_key
])
379 mtx_lock(&device
->meta_state
.mtx
);
380 if (device
->meta_state
.resolve
.pipeline
[fs_key
]) {
381 mtx_unlock(&device
->meta_state
.mtx
);
385 struct radv_shader_module vs_module
= { .nir
= radv_meta_build_nir_vs_generate_vertices() };
387 result
= create_pass(device
, radv_fs_key_format_exemplars
[fs_key
], &device
->meta_state
.resolve
.pass
[fs_key
]);
388 if (result
!= VK_SUCCESS
)
391 VkShaderModule vs_module_h
= radv_shader_module_to_handle(&vs_module
);
392 result
= create_pipeline(device
, vs_module_h
, &device
->meta_state
.resolve
.pipeline
[fs_key
], device
->meta_state
.resolve
.pass
[fs_key
]);
395 ralloc_free(vs_module
.nir
);
396 mtx_unlock(&device
->meta_state
.mtx
);
400 void radv_CmdResolveImage(
401 VkCommandBuffer cmd_buffer_h
,
403 VkImageLayout src_image_layout
,
404 VkImage dest_image_h
,
405 VkImageLayout dest_image_layout
,
406 uint32_t region_count
,
407 const VkImageResolve
* regions
)
409 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, cmd_buffer_h
);
410 RADV_FROM_HANDLE(radv_image
, src_image
, src_image_h
);
411 RADV_FROM_HANDLE(radv_image
, dest_image
, dest_image_h
);
412 struct radv_device
*device
= cmd_buffer
->device
;
413 struct radv_meta_saved_state saved_state
;
414 VkDevice device_h
= radv_device_to_handle(device
);
415 enum radv_resolve_method resolve_method
= RESOLVE_HW
;
416 /* we can use the hw resolve only for single full resolves */
417 if (region_count
== 1) {
418 if (regions
[0].srcOffset
.x
||
419 regions
[0].srcOffset
.y
||
420 regions
[0].srcOffset
.z
)
421 resolve_method
= RESOLVE_COMPUTE
;
422 if (regions
[0].dstOffset
.x
||
423 regions
[0].dstOffset
.y
||
424 regions
[0].dstOffset
.z
)
425 resolve_method
= RESOLVE_COMPUTE
;
427 if (regions
[0].extent
.width
!= src_image
->info
.width
||
428 regions
[0].extent
.height
!= src_image
->info
.height
||
429 regions
[0].extent
.depth
!= src_image
->info
.depth
)
430 resolve_method
= RESOLVE_COMPUTE
;
432 resolve_method
= RESOLVE_COMPUTE
;
434 radv_pick_resolve_method_images(src_image
, src_image
->vk_format
,
435 dest_image
, dest_image_layout
,
436 cmd_buffer
, &resolve_method
);
438 if (resolve_method
== RESOLVE_FRAGMENT
) {
439 radv_meta_resolve_fragment_image(cmd_buffer
,
444 region_count
, regions
);
448 if (resolve_method
== RESOLVE_COMPUTE
) {
449 radv_meta_resolve_compute_image(cmd_buffer
,
451 src_image
->vk_format
,
454 dest_image
->vk_format
,
456 region_count
, regions
);
460 radv_meta_save(&saved_state
, cmd_buffer
,
461 RADV_META_SAVE_GRAPHICS_PIPELINE
);
463 assert(src_image
->info
.samples
> 1);
464 if (src_image
->info
.samples
<= 1) {
465 /* this causes GPU hangs if we get past here */
466 fprintf(stderr
, "radv: Illegal resolve operation (src not multisampled), will hang GPU.");
469 assert(dest_image
->info
.samples
== 1);
471 if (src_image
->info
.array_size
> 1)
472 radv_finishme("vkCmdResolveImage: multisample array images");
474 unsigned fs_key
= radv_format_meta_fs_key(dest_image
->vk_format
);
475 for (uint32_t r
= 0; r
< region_count
; ++r
) {
476 const VkImageResolve
*region
= ®ions
[r
];
478 /* From the Vulkan 1.0 spec:
480 * - The aspectMask member of srcSubresource and dstSubresource must
481 * only contain VK_IMAGE_ASPECT_COLOR_BIT
483 * - The layerCount member of srcSubresource and dstSubresource must
486 assert(region
->srcSubresource
.aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
487 assert(region
->dstSubresource
.aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
488 assert(region
->srcSubresource
.layerCount
==
489 region
->dstSubresource
.layerCount
);
491 const uint32_t src_base_layer
=
492 radv_meta_get_iview_layer(src_image
, ®ion
->srcSubresource
,
495 const uint32_t dest_base_layer
=
496 radv_meta_get_iview_layer(dest_image
, ®ion
->dstSubresource
,
500 * From Vulkan 1.0.6 spec: 18.6 Resolving Multisample Images
502 * extent is the size in texels of the source image to resolve in width,
503 * height and depth. 1D images use only x and width. 2D images use x, y,
504 * width and height. 3D images use x, y, z, width, height and depth.
506 * srcOffset and dstOffset select the initial x, y, and z offsets in
507 * texels of the sub-regions of the source and destination image data.
508 * extent is the size in texels of the source image to resolve in width,
509 * height and depth. 1D images use only x and width. 2D images use x, y,
510 * width and height. 3D images use x, y, z, width, height and depth.
512 const struct VkExtent3D extent
=
513 radv_sanitize_image_extent(src_image
->type
, region
->extent
);
514 const struct VkOffset3D dstOffset
=
515 radv_sanitize_image_offset(dest_image
->type
, region
->dstOffset
);
517 if (radv_dcc_enabled(dest_image
, region
->dstSubresource
.mipLevel
)) {
518 VkImageSubresourceRange range
= {
519 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
520 .baseMipLevel
= region
->dstSubresource
.mipLevel
,
522 .baseArrayLayer
= dest_base_layer
,
523 .layerCount
= region
->dstSubresource
.layerCount
,
526 radv_initialize_dcc(cmd_buffer
, dest_image
, &range
, 0xffffffff);
529 for (uint32_t layer
= 0; layer
< region
->srcSubresource
.layerCount
;
532 VkResult ret
= build_resolve_pipeline(device
, fs_key
);
533 if (ret
!= VK_SUCCESS
) {
534 cmd_buffer
->record_result
= ret
;
538 struct radv_image_view src_iview
;
539 radv_image_view_init(&src_iview
, cmd_buffer
->device
,
540 &(VkImageViewCreateInfo
) {
541 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
542 .image
= src_image_h
,
543 .viewType
= radv_meta_get_view_type(src_image
),
544 .format
= src_image
->vk_format
,
545 .subresourceRange
= {
546 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
547 .baseMipLevel
= region
->srcSubresource
.mipLevel
,
549 .baseArrayLayer
= src_base_layer
+ layer
,
554 struct radv_image_view dest_iview
;
555 radv_image_view_init(&dest_iview
, cmd_buffer
->device
,
556 &(VkImageViewCreateInfo
) {
557 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
558 .image
= dest_image_h
,
559 .viewType
= radv_meta_get_view_type(dest_image
),
560 .format
= dest_image
->vk_format
,
561 .subresourceRange
= {
562 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
563 .baseMipLevel
= region
->dstSubresource
.mipLevel
,
565 .baseArrayLayer
= dest_base_layer
+ layer
,
571 radv_CreateFramebuffer(device_h
,
572 &(VkFramebufferCreateInfo
) {
573 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
574 .attachmentCount
= 2,
575 .pAttachments
= (VkImageView
[]) {
576 radv_image_view_to_handle(&src_iview
),
577 radv_image_view_to_handle(&dest_iview
),
579 .width
= radv_minify(dest_image
->info
.width
,
580 region
->dstSubresource
.mipLevel
),
581 .height
= radv_minify(dest_image
->info
.height
,
582 region
->dstSubresource
.mipLevel
),
585 &cmd_buffer
->pool
->alloc
,
588 radv_CmdBeginRenderPass(cmd_buffer_h
,
589 &(VkRenderPassBeginInfo
) {
590 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
591 .renderPass
= device
->meta_state
.resolve
.pass
[fs_key
],
603 .clearValueCount
= 0,
604 .pClearValues
= NULL
,
606 VK_SUBPASS_CONTENTS_INLINE
);
608 emit_resolve(cmd_buffer
,
609 dest_iview
.vk_format
,
615 .width
= extent
.width
,
616 .height
= extent
.height
,
619 radv_CmdEndRenderPass(cmd_buffer_h
);
621 radv_DestroyFramebuffer(device_h
, fb_h
,
622 &cmd_buffer
->pool
->alloc
);
626 radv_meta_restore(&saved_state
, cmd_buffer
);
630 * Emit any needed resolves for the current subpass.
633 radv_cmd_buffer_resolve_subpass(struct radv_cmd_buffer
*cmd_buffer
)
635 struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
636 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
637 struct radv_meta_saved_state saved_state
;
638 enum radv_resolve_method resolve_method
= RESOLVE_HW
;
640 if (subpass
->ds_resolve_attachment
) {
641 struct radv_subpass_attachment src_att
= *subpass
->depth_stencil_attachment
;
642 struct radv_subpass_attachment dst_att
= *subpass
->ds_resolve_attachment
;
643 struct radv_image_view
*src_iview
=
644 cmd_buffer
->state
.framebuffer
->attachments
[src_att
.attachment
].attachment
;
645 struct radv_image_view
*dst_iview
=
646 cmd_buffer
->state
.framebuffer
->attachments
[dst_att
.attachment
].attachment
;
648 radv_pick_resolve_method_images(src_iview
->image
,
649 src_iview
->vk_format
,
655 if ((src_iview
->aspect_mask
& VK_IMAGE_ASPECT_DEPTH_BIT
) &&
656 subpass
->depth_resolve_mode
!= VK_RESOLVE_MODE_NONE_KHR
) {
657 if (resolve_method
== RESOLVE_FRAGMENT
) {
658 radv_depth_stencil_resolve_subpass_fs(cmd_buffer
,
659 VK_IMAGE_ASPECT_DEPTH_BIT
,
660 subpass
->depth_resolve_mode
);
662 assert(resolve_method
== RESOLVE_COMPUTE
);
663 radv_depth_stencil_resolve_subpass_cs(cmd_buffer
,
664 VK_IMAGE_ASPECT_DEPTH_BIT
,
665 subpass
->depth_resolve_mode
);
669 if ((src_iview
->aspect_mask
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
670 subpass
->stencil_resolve_mode
!= VK_RESOLVE_MODE_NONE_KHR
) {
671 if (resolve_method
== RESOLVE_FRAGMENT
) {
672 radv_depth_stencil_resolve_subpass_fs(cmd_buffer
,
673 VK_IMAGE_ASPECT_STENCIL_BIT
,
674 subpass
->stencil_resolve_mode
);
676 assert(resolve_method
== RESOLVE_COMPUTE
);
677 radv_depth_stencil_resolve_subpass_cs(cmd_buffer
,
678 VK_IMAGE_ASPECT_STENCIL_BIT
,
679 subpass
->stencil_resolve_mode
);
684 if (!subpass
->has_color_resolve
)
687 for (uint32_t i
= 0; i
< subpass
->color_count
; ++i
) {
688 struct radv_subpass_attachment src_att
= subpass
->color_attachments
[i
];
689 struct radv_subpass_attachment dest_att
= subpass
->resolve_attachments
[i
];
691 if (dest_att
.attachment
== VK_ATTACHMENT_UNUSED
)
694 /* Make sure to not clear color attachments after resolves. */
695 cmd_buffer
->state
.attachments
[dest_att
.attachment
].pending_clear_aspects
= 0;
697 struct radv_image
*dst_img
= cmd_buffer
->state
.framebuffer
->attachments
[dest_att
.attachment
].attachment
->image
;
698 struct radv_image_view
*src_iview
= cmd_buffer
->state
.framebuffer
->attachments
[src_att
.attachment
].attachment
;
699 struct radv_image
*src_img
= src_iview
->image
;
701 radv_pick_resolve_method_images(src_img
, src_iview
->vk_format
,
702 dst_img
, dest_att
.layout
,
703 cmd_buffer
, &resolve_method
);
705 if (resolve_method
== RESOLVE_FRAGMENT
) {
710 if (resolve_method
== RESOLVE_COMPUTE
) {
711 radv_cmd_buffer_resolve_subpass_cs(cmd_buffer
);
713 } else if (resolve_method
== RESOLVE_FRAGMENT
) {
714 radv_cmd_buffer_resolve_subpass_fs(cmd_buffer
);
718 radv_meta_save(&saved_state
, cmd_buffer
,
719 RADV_META_SAVE_GRAPHICS_PIPELINE
);
721 for (uint32_t i
= 0; i
< subpass
->color_count
; ++i
) {
722 struct radv_subpass_attachment src_att
= subpass
->color_attachments
[i
];
723 struct radv_subpass_attachment dest_att
= subpass
->resolve_attachments
[i
];
725 if (dest_att
.attachment
== VK_ATTACHMENT_UNUSED
)
728 struct radv_image_view
*dest_iview
= cmd_buffer
->state
.framebuffer
->attachments
[dest_att
.attachment
].attachment
;
729 struct radv_image
*dst_img
= dest_iview
->image
;
731 if (radv_dcc_enabled(dst_img
, dest_iview
->base_mip
)) {
732 VkImageSubresourceRange range
= {
733 .aspectMask
= dest_iview
->aspect_mask
,
734 .baseMipLevel
= dest_iview
->base_mip
,
735 .levelCount
= dest_iview
->level_count
,
736 .baseArrayLayer
= dest_iview
->base_layer
,
737 .layerCount
= dest_iview
->layer_count
,
740 radv_initialize_dcc(cmd_buffer
, dst_img
, &range
, 0xffffffff);
741 cmd_buffer
->state
.attachments
[dest_att
.attachment
].current_layout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
744 struct radv_subpass resolve_subpass
= {
746 .color_attachments
= (struct radv_subpass_attachment
[]) { src_att
, dest_att
},
747 .depth_stencil_attachment
= NULL
,
750 radv_cmd_buffer_set_subpass(cmd_buffer
, &resolve_subpass
);
752 VkResult ret
= build_resolve_pipeline(cmd_buffer
->device
, radv_format_meta_fs_key(dest_iview
->vk_format
));
753 if (ret
!= VK_SUCCESS
) {
754 cmd_buffer
->record_result
= ret
;
758 emit_resolve(cmd_buffer
,
759 dest_iview
->vk_format
,
760 &(VkOffset2D
) { 0, 0 },
761 &(VkExtent2D
) { fb
->width
, fb
->height
});
764 radv_cmd_buffer_set_subpass(cmd_buffer
, subpass
);
766 radv_meta_restore(&saved_state
, cmd_buffer
);
770 * Decompress CMask/FMask before resolving a multisampled source image inside a
774 radv_decompress_resolve_subpass_src(struct radv_cmd_buffer
*cmd_buffer
)
776 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
777 struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
778 uint32_t layer_count
= fb
->layers
;
780 if (subpass
->view_mask
)
781 layer_count
= util_last_bit(subpass
->view_mask
);
783 for (uint32_t i
= 0; i
< subpass
->color_count
; ++i
) {
784 struct radv_subpass_attachment src_att
= subpass
->color_attachments
[i
];
785 struct radv_subpass_attachment dest_att
= subpass
->resolve_attachments
[i
];
787 if (dest_att
.attachment
== VK_ATTACHMENT_UNUSED
)
790 struct radv_image_view
*src_iview
=
791 fb
->attachments
[src_att
.attachment
].attachment
;
792 struct radv_image
*src_image
= src_iview
->image
;
794 VkImageResolve region
= {};
795 region
.srcSubresource
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
796 region
.srcSubresource
.mipLevel
= 0;
797 region
.srcSubresource
.baseArrayLayer
= src_iview
->base_layer
;
798 region
.srcSubresource
.layerCount
= layer_count
;
800 radv_decompress_resolve_src(cmd_buffer
, src_image
,
801 src_att
.layout
, 1, ®ion
);
804 if (subpass
->ds_resolve_attachment
) {
805 struct radv_subpass_attachment src_att
= *subpass
->depth_stencil_attachment
;
806 struct radv_image_view
*src_iview
=
807 fb
->attachments
[src_att
.attachment
].attachment
;
808 struct radv_image
*src_image
= src_iview
->image
;
810 VkImageResolve region
= {};
811 region
.srcSubresource
.aspectMask
= VK_IMAGE_ASPECT_DEPTH_BIT
;
812 region
.srcSubresource
.mipLevel
= 0;
813 region
.srcSubresource
.baseArrayLayer
= src_iview
->base_layer
;
814 region
.srcSubresource
.layerCount
= layer_count
;
816 radv_decompress_resolve_src(cmd_buffer
, src_image
,
817 src_att
.layout
, 1, ®ion
);
821 static struct radv_sample_locations_state
*
822 radv_get_resolve_sample_locations(struct radv_cmd_buffer
*cmd_buffer
)
824 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
825 uint32_t subpass_id
= radv_get_subpass_id(cmd_buffer
);
827 for (uint32_t i
= 0; i
< state
->num_subpass_sample_locs
; i
++) {
828 if (state
->subpass_sample_locs
[i
].subpass_idx
== subpass_id
)
829 return &state
->subpass_sample_locs
[i
].sample_location
;
836 * Decompress CMask/FMask before resolving a multisampled source image.
839 radv_decompress_resolve_src(struct radv_cmd_buffer
*cmd_buffer
,
840 struct radv_image
*src_image
,
841 VkImageLayout src_image_layout
,
842 uint32_t region_count
,
843 const VkImageResolve
*regions
)
845 for (uint32_t r
= 0; r
< region_count
; ++r
) {
846 const VkImageResolve
*region
= ®ions
[r
];
847 const uint32_t src_base_layer
=
848 radv_meta_get_iview_layer(src_image
, ®ion
->srcSubresource
,
851 VkImageMemoryBarrier barrier
= {};
852 barrier
.srcAccessMask
= VK_ACCESS_TRANSFER_WRITE_BIT
;
853 barrier
.dstAccessMask
= VK_ACCESS_TRANSFER_READ_BIT
;
854 barrier
.oldLayout
= src_image_layout
;
855 barrier
.newLayout
= VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
;
856 barrier
.image
= radv_image_to_handle(src_image
);
857 barrier
.subresourceRange
= (VkImageSubresourceRange
) {
858 .aspectMask
= region
->srcSubresource
.aspectMask
,
859 .baseMipLevel
= region
->srcSubresource
.mipLevel
,
861 .baseArrayLayer
= src_base_layer
,
862 .layerCount
= region
->srcSubresource
.layerCount
,
865 if (src_image
->flags
& VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT
) {
866 /* If the depth/stencil image uses different sample
867 * locations, we need them during HTILE decompressions.
869 struct radv_sample_locations_state
*sample_locs
=
870 radv_get_resolve_sample_locations(cmd_buffer
);
872 barrier
.pNext
= &(VkSampleLocationsInfoEXT
) {
873 .sType
= VK_STRUCTURE_TYPE_SAMPLE_LOCATIONS_INFO_EXT
,
874 .sampleLocationsPerPixel
= sample_locs
->per_pixel
,
875 .sampleLocationGridSize
= sample_locs
->grid_size
,
876 .sampleLocationsCount
= sample_locs
->count
,
877 .pSampleLocations
= sample_locs
->locations
,
881 radv_CmdPipelineBarrier(radv_cmd_buffer_to_handle(cmd_buffer
),
882 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
,
883 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
,
884 false, 0, NULL
, 0, NULL
, 1, &barrier
);