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 "nir/nir_builder.h"
36 const struct glsl_type
*vec4
= glsl_vec4_type();
38 nir_variable
*f_color
; /* vec4, fragment output color */
40 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
41 b
.shader
->info
.name
= ralloc_asprintf(b
.shader
,
44 f_color
= nir_variable_create(b
.shader
, nir_var_shader_out
, vec4
,
46 f_color
->data
.location
= FRAG_RESULT_DATA0
;
47 nir_store_var(&b
, f_color
, nir_imm_vec4(&b
, 0.0, 0.0, 0.0, 1.0), 0xf);
53 create_pass(struct radv_device
*device
)
56 VkDevice device_h
= radv_device_to_handle(device
);
57 const VkAllocationCallbacks
*alloc
= &device
->meta_state
.alloc
;
58 VkAttachmentDescription attachments
[2];
61 for (i
= 0; i
< 2; i
++) {
62 attachments
[i
].format
= VK_FORMAT_UNDEFINED
;
63 attachments
[i
].samples
= 1;
64 attachments
[i
].loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
;
65 attachments
[i
].storeOp
= VK_ATTACHMENT_STORE_OP_STORE
;
67 attachments
[0].initialLayout
= VK_IMAGE_LAYOUT_GENERAL
;
68 attachments
[0].finalLayout
= VK_IMAGE_LAYOUT_GENERAL
;
69 attachments
[1].initialLayout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
70 attachments
[1].finalLayout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
72 result
= radv_CreateRenderPass(device_h
,
73 &(VkRenderPassCreateInfo
) {
74 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
76 .pAttachments
= attachments
,
78 .pSubpasses
= &(VkSubpassDescription
) {
79 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
80 .inputAttachmentCount
= 0,
81 .colorAttachmentCount
= 2,
82 .pColorAttachments
= (VkAttachmentReference
[]) {
85 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
89 .layout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
,
92 .pResolveAttachments
= NULL
,
93 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
94 .attachment
= VK_ATTACHMENT_UNUSED
,
96 .preserveAttachmentCount
= 0,
97 .pPreserveAttachments
= NULL
,
102 &device
->meta_state
.resolve
.pass
);
108 create_pipeline(struct radv_device
*device
,
109 VkShaderModule vs_module_h
)
112 VkDevice device_h
= radv_device_to_handle(device
);
114 struct radv_shader_module fs_module
= {
115 .nir
= build_nir_fs(),
118 if (!fs_module
.nir
) {
119 /* XXX: Need more accurate error */
120 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
124 result
= radv_graphics_pipeline_create(device_h
,
125 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
126 &(VkGraphicsPipelineCreateInfo
) {
127 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
129 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
131 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
132 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
133 .module
= vs_module_h
,
137 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
138 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
139 .module
= radv_shader_module_to_handle(&fs_module
),
143 .pVertexInputState
= &(VkPipelineVertexInputStateCreateInfo
) {
144 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
145 .vertexBindingDescriptionCount
= 0,
146 .vertexAttributeDescriptionCount
= 0,
148 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
149 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
150 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
151 .primitiveRestartEnable
= false,
153 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
154 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
158 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
159 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
160 .depthClampEnable
= false,
161 .rasterizerDiscardEnable
= false,
162 .polygonMode
= VK_POLYGON_MODE_FILL
,
163 .cullMode
= VK_CULL_MODE_NONE
,
164 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
,
166 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
167 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
168 .rasterizationSamples
= 1,
169 .sampleShadingEnable
= false,
171 .alphaToCoverageEnable
= false,
172 .alphaToOneEnable
= false,
174 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
175 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
176 .logicOpEnable
= false,
177 .attachmentCount
= 2,
178 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
180 .colorWriteMask
= VK_COLOR_COMPONENT_R_BIT
|
181 VK_COLOR_COMPONENT_G_BIT
|
182 VK_COLOR_COMPONENT_B_BIT
|
183 VK_COLOR_COMPONENT_A_BIT
,
191 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
192 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
193 .dynamicStateCount
= 2,
194 .pDynamicStates
= (VkDynamicState
[]) {
195 VK_DYNAMIC_STATE_VIEWPORT
,
196 VK_DYNAMIC_STATE_SCISSOR
,
199 .renderPass
= device
->meta_state
.resolve
.pass
,
202 &(struct radv_graphics_pipeline_create_info
) {
203 .use_rectlist
= true,
204 .custom_blend_mode
= V_028808_CB_RESOLVE
,
206 &device
->meta_state
.alloc
,
207 &device
->meta_state
.resolve
.pipeline
);
208 if (result
!= VK_SUCCESS
)
214 ralloc_free(fs_module
.nir
);
219 radv_device_finish_meta_resolve_state(struct radv_device
*device
)
221 struct radv_meta_state
*state
= &device
->meta_state
;
222 VkDevice device_h
= radv_device_to_handle(device
);
223 VkRenderPass pass_h
= device
->meta_state
.resolve
.pass
;
224 const VkAllocationCallbacks
*alloc
= &device
->meta_state
.alloc
;
227 radv_DestroyRenderPass(device_h
, pass_h
,
228 &device
->meta_state
.alloc
);
230 VkPipeline pipeline_h
= state
->resolve
.pipeline
;
232 radv_DestroyPipeline(device_h
, pipeline_h
, alloc
);
237 radv_device_init_meta_resolve_state(struct radv_device
*device
)
239 VkResult res
= VK_SUCCESS
;
241 struct radv_shader_module vs_module
= { .nir
= radv_meta_build_nir_vs_generate_vertices() };
242 if (!vs_module
.nir
) {
243 /* XXX: Need more accurate error */
244 res
= VK_ERROR_OUT_OF_HOST_MEMORY
;
248 res
= create_pass(device
);
249 if (res
!= VK_SUCCESS
)
252 VkShaderModule vs_module_h
= radv_shader_module_to_handle(&vs_module
);
253 res
= create_pipeline(device
, vs_module_h
);
254 if (res
!= VK_SUCCESS
)
260 radv_device_finish_meta_resolve_state(device
);
263 ralloc_free(vs_module
.nir
);
269 emit_resolve(struct radv_cmd_buffer
*cmd_buffer
,
270 const VkOffset2D
*dest_offset
,
271 const VkExtent2D
*resolve_extent
)
273 struct radv_device
*device
= cmd_buffer
->device
;
274 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
276 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
;
278 VkPipeline pipeline_h
= device
->meta_state
.resolve
.pipeline
;
279 RADV_FROM_HANDLE(radv_pipeline
, pipeline
, pipeline_h
);
281 if (cmd_buffer
->state
.pipeline
!= pipeline
) {
282 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
286 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
289 .width
= resolve_extent
->width
,
290 .height
= resolve_extent
->height
,
295 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkRect2D
) {
296 .offset
= *dest_offset
,
297 .extent
= *resolve_extent
,
300 radv_CmdDraw(cmd_buffer_h
, 3, 1, 0, 0);
301 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
;
304 enum radv_resolve_method
{
310 static void radv_pick_resolve_method_images(struct radv_image
*src_image
,
311 struct radv_image
*dest_image
,
312 enum radv_resolve_method
*method
)
315 if (dest_image
->surface
.micro_tile_mode
!= src_image
->surface
.micro_tile_mode
) {
316 if (dest_image
->surface
.num_dcc_levels
> 0)
317 *method
= RESOLVE_FRAGMENT
;
319 *method
= RESOLVE_COMPUTE
;
323 void radv_CmdResolveImage(
324 VkCommandBuffer cmd_buffer_h
,
326 VkImageLayout src_image_layout
,
327 VkImage dest_image_h
,
328 VkImageLayout dest_image_layout
,
329 uint32_t region_count
,
330 const VkImageResolve
* regions
)
332 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, cmd_buffer_h
);
333 RADV_FROM_HANDLE(radv_image
, src_image
, src_image_h
);
334 RADV_FROM_HANDLE(radv_image
, dest_image
, dest_image_h
);
335 struct radv_device
*device
= cmd_buffer
->device
;
336 struct radv_meta_saved_state saved_state
;
337 VkDevice device_h
= radv_device_to_handle(device
);
338 enum radv_resolve_method resolve_method
= RESOLVE_HW
;
339 /* we can use the hw resolve only for single full resolves */
340 if (region_count
== 1) {
341 if (regions
[0].srcOffset
.x
||
342 regions
[0].srcOffset
.y
||
343 regions
[0].srcOffset
.z
)
344 resolve_method
= RESOLVE_COMPUTE
;
345 if (regions
[0].dstOffset
.x
||
346 regions
[0].dstOffset
.y
||
347 regions
[0].dstOffset
.z
)
348 resolve_method
= RESOLVE_COMPUTE
;
350 if (regions
[0].extent
.width
!= src_image
->info
.width
||
351 regions
[0].extent
.height
!= src_image
->info
.height
||
352 regions
[0].extent
.depth
!= src_image
->info
.depth
)
353 resolve_method
= RESOLVE_COMPUTE
;
355 resolve_method
= RESOLVE_COMPUTE
;
357 radv_pick_resolve_method_images(src_image
, dest_image
,
360 if (resolve_method
== RESOLVE_FRAGMENT
) {
361 radv_meta_resolve_fragment_image(cmd_buffer
,
366 region_count
, regions
);
370 if (resolve_method
== RESOLVE_COMPUTE
) {
371 radv_meta_resolve_compute_image(cmd_buffer
,
376 region_count
, regions
);
380 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state
, cmd_buffer
);
382 assert(src_image
->info
.samples
> 1);
383 if (src_image
->info
.samples
<= 1) {
384 /* this causes GPU hangs if we get past here */
385 fprintf(stderr
, "radv: Illegal resolve operation (src not multisampled), will hang GPU.");
388 assert(dest_image
->info
.samples
== 1);
390 if (src_image
->info
.samples
>= 16) {
391 /* See commit aa3f9aaf31e9056a255f9e0472ebdfdaa60abe54 for the
392 * glBlitFramebuffer workaround for samples >= 16.
394 radv_finishme("vkCmdResolveImage: need interpolation workaround when "
398 if (src_image
->info
.array_size
> 1)
399 radv_finishme("vkCmdResolveImage: multisample array images");
401 if (dest_image
->surface
.dcc_size
) {
402 radv_initialize_dcc(cmd_buffer
, dest_image
, 0xffffffff);
404 for (uint32_t r
= 0; r
< region_count
; ++r
) {
405 const VkImageResolve
*region
= ®ions
[r
];
407 /* From the Vulkan 1.0 spec:
409 * - The aspectMask member of srcSubresource and dstSubresource must
410 * only contain VK_IMAGE_ASPECT_COLOR_BIT
412 * - The layerCount member of srcSubresource and dstSubresource must
415 assert(region
->srcSubresource
.aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
416 assert(region
->dstSubresource
.aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
417 assert(region
->srcSubresource
.layerCount
==
418 region
->dstSubresource
.layerCount
);
420 const uint32_t src_base_layer
=
421 radv_meta_get_iview_layer(src_image
, ®ion
->srcSubresource
,
424 const uint32_t dest_base_layer
=
425 radv_meta_get_iview_layer(dest_image
, ®ion
->dstSubresource
,
429 * From Vulkan 1.0.6 spec: 18.6 Resolving Multisample Images
431 * extent is the size in texels of the source image to resolve in width,
432 * height and depth. 1D images use only x and width. 2D images use x, y,
433 * width and height. 3D images use x, y, z, width, height and depth.
435 * srcOffset and dstOffset select the initial x, y, and z offsets in
436 * texels of the sub-regions of the source and destination image data.
437 * extent is the size in texels of the source image to resolve in width,
438 * height and depth. 1D images use only x and width. 2D images use x, y,
439 * width and height. 3D images use x, y, z, width, height and depth.
441 const struct VkExtent3D extent
=
442 radv_sanitize_image_extent(src_image
->type
, region
->extent
);
443 const struct VkOffset3D dstOffset
=
444 radv_sanitize_image_offset(dest_image
->type
, region
->dstOffset
);
447 for (uint32_t layer
= 0; layer
< region
->srcSubresource
.layerCount
;
450 struct radv_image_view src_iview
;
451 radv_image_view_init(&src_iview
, cmd_buffer
->device
,
452 &(VkImageViewCreateInfo
) {
453 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
454 .image
= src_image_h
,
455 .viewType
= radv_meta_get_view_type(src_image
),
456 .format
= src_image
->vk_format
,
457 .subresourceRange
= {
458 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
459 .baseMipLevel
= region
->srcSubresource
.mipLevel
,
461 .baseArrayLayer
= src_base_layer
+ layer
,
466 struct radv_image_view dest_iview
;
467 radv_image_view_init(&dest_iview
, cmd_buffer
->device
,
468 &(VkImageViewCreateInfo
) {
469 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
470 .image
= dest_image_h
,
471 .viewType
= radv_meta_get_view_type(dest_image
),
472 .format
= dest_image
->vk_format
,
473 .subresourceRange
= {
474 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
475 .baseMipLevel
= region
->dstSubresource
.mipLevel
,
477 .baseArrayLayer
= dest_base_layer
+ layer
,
483 radv_CreateFramebuffer(device_h
,
484 &(VkFramebufferCreateInfo
) {
485 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
486 .attachmentCount
= 2,
487 .pAttachments
= (VkImageView
[]) {
488 radv_image_view_to_handle(&src_iview
),
489 radv_image_view_to_handle(&dest_iview
),
491 .width
= radv_minify(dest_image
->info
.width
,
492 region
->dstSubresource
.mipLevel
),
493 .height
= radv_minify(dest_image
->info
.height
,
494 region
->dstSubresource
.mipLevel
),
497 &cmd_buffer
->pool
->alloc
,
500 radv_CmdBeginRenderPass(cmd_buffer_h
,
501 &(VkRenderPassBeginInfo
) {
502 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
503 .renderPass
= device
->meta_state
.resolve
.pass
,
515 .clearValueCount
= 0,
516 .pClearValues
= NULL
,
518 VK_SUBPASS_CONTENTS_INLINE
);
520 emit_resolve(cmd_buffer
,
526 .width
= extent
.width
,
527 .height
= extent
.height
,
530 radv_CmdEndRenderPass(cmd_buffer_h
);
532 radv_DestroyFramebuffer(device_h
, fb_h
,
533 &cmd_buffer
->pool
->alloc
);
537 radv_meta_restore(&saved_state
, cmd_buffer
);
541 * Emit any needed resolves for the current subpass.
544 radv_cmd_buffer_resolve_subpass(struct radv_cmd_buffer
*cmd_buffer
)
546 struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
547 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
548 struct radv_meta_saved_state saved_state
;
549 enum radv_resolve_method resolve_method
= RESOLVE_HW
;
551 /* FINISHME(perf): Skip clears for resolve attachments.
553 * From the Vulkan 1.0 spec:
555 * If the first use of an attachment in a render pass is as a resolve
556 * attachment, then the loadOp is effectively ignored as the resolve is
557 * guaranteed to overwrite all pixels in the render area.
560 if (!subpass
->has_resolve
)
563 for (uint32_t i
= 0; i
< subpass
->color_count
; ++i
) {
564 VkAttachmentReference src_att
= subpass
->color_attachments
[i
];
565 VkAttachmentReference dest_att
= subpass
->resolve_attachments
[i
];
567 if (src_att
.attachment
== VK_ATTACHMENT_UNUSED
||
568 dest_att
.attachment
== VK_ATTACHMENT_UNUSED
)
571 struct radv_image
*dst_img
= cmd_buffer
->state
.framebuffer
->attachments
[dest_att
.attachment
].attachment
->image
;
572 struct radv_image
*src_img
= cmd_buffer
->state
.framebuffer
->attachments
[src_att
.attachment
].attachment
->image
;
574 radv_pick_resolve_method_images(dst_img
, src_img
, &resolve_method
);
575 if (resolve_method
== RESOLVE_FRAGMENT
) {
580 if (resolve_method
== RESOLVE_COMPUTE
) {
581 radv_cmd_buffer_resolve_subpass_cs(cmd_buffer
);
583 } else if (resolve_method
== RESOLVE_FRAGMENT
) {
584 radv_cmd_buffer_resolve_subpass_fs(cmd_buffer
);
588 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state
, cmd_buffer
);
590 for (uint32_t i
= 0; i
< subpass
->color_count
; ++i
) {
591 VkAttachmentReference src_att
= subpass
->color_attachments
[i
];
592 VkAttachmentReference dest_att
= subpass
->resolve_attachments
[i
];
594 if (src_att
.attachment
== VK_ATTACHMENT_UNUSED
||
595 dest_att
.attachment
== VK_ATTACHMENT_UNUSED
)
598 struct radv_image
*dst_img
= cmd_buffer
->state
.framebuffer
->attachments
[dest_att
.attachment
].attachment
->image
;
600 if (dst_img
->surface
.dcc_size
) {
601 radv_initialize_dcc(cmd_buffer
, dst_img
, 0xffffffff);
602 cmd_buffer
->state
.attachments
[dest_att
.attachment
].current_layout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
605 struct radv_subpass resolve_subpass
= {
607 .color_attachments
= (VkAttachmentReference
[]) { src_att
, dest_att
},
608 .depth_stencil_attachment
= { .attachment
= VK_ATTACHMENT_UNUSED
},
611 radv_cmd_buffer_set_subpass(cmd_buffer
, &resolve_subpass
, false);
613 emit_resolve(cmd_buffer
,
614 &(VkOffset2D
) { 0, 0 },
615 &(VkExtent2D
) { fb
->width
, fb
->height
});
618 cmd_buffer
->state
.subpass
= subpass
;
619 radv_meta_restore(&saved_state
, cmd_buffer
);