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 VkPipelineLayoutCreateInfo pl_create_info
= {
125 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
128 .pushConstantRangeCount
= 0,
129 .pPushConstantRanges
= NULL
,
132 result
= radv_CreatePipelineLayout(radv_device_to_handle(device
),
134 &device
->meta_state
.alloc
,
135 &device
->meta_state
.resolve
.p_layout
);
136 if (result
!= VK_SUCCESS
)
139 result
= radv_graphics_pipeline_create(device_h
,
140 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
141 &(VkGraphicsPipelineCreateInfo
) {
142 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
144 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
146 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
147 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
148 .module
= vs_module_h
,
152 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
153 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
154 .module
= radv_shader_module_to_handle(&fs_module
),
158 .pVertexInputState
= &(VkPipelineVertexInputStateCreateInfo
) {
159 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
160 .vertexBindingDescriptionCount
= 0,
161 .vertexAttributeDescriptionCount
= 0,
163 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
164 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
165 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
166 .primitiveRestartEnable
= false,
168 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
169 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
173 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
174 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
175 .depthClampEnable
= false,
176 .rasterizerDiscardEnable
= false,
177 .polygonMode
= VK_POLYGON_MODE_FILL
,
178 .cullMode
= VK_CULL_MODE_NONE
,
179 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
,
181 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
182 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
183 .rasterizationSamples
= 1,
184 .sampleShadingEnable
= false,
186 .alphaToCoverageEnable
= false,
187 .alphaToOneEnable
= false,
189 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
190 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
191 .logicOpEnable
= false,
192 .attachmentCount
= 2,
193 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
195 .colorWriteMask
= VK_COLOR_COMPONENT_R_BIT
|
196 VK_COLOR_COMPONENT_G_BIT
|
197 VK_COLOR_COMPONENT_B_BIT
|
198 VK_COLOR_COMPONENT_A_BIT
,
206 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
207 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
208 .dynamicStateCount
= 2,
209 .pDynamicStates
= (VkDynamicState
[]) {
210 VK_DYNAMIC_STATE_VIEWPORT
,
211 VK_DYNAMIC_STATE_SCISSOR
,
214 .layout
= device
->meta_state
.resolve
.p_layout
,
215 .renderPass
= device
->meta_state
.resolve
.pass
,
218 &(struct radv_graphics_pipeline_create_info
) {
219 .use_rectlist
= true,
220 .custom_blend_mode
= V_028808_CB_RESOLVE
,
222 &device
->meta_state
.alloc
,
223 &device
->meta_state
.resolve
.pipeline
);
224 if (result
!= VK_SUCCESS
)
230 ralloc_free(fs_module
.nir
);
235 radv_device_finish_meta_resolve_state(struct radv_device
*device
)
237 struct radv_meta_state
*state
= &device
->meta_state
;
239 radv_DestroyRenderPass(radv_device_to_handle(device
),
240 state
->resolve
.pass
, &state
->alloc
);
241 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
242 state
->resolve
.p_layout
, &state
->alloc
);
243 radv_DestroyPipeline(radv_device_to_handle(device
),
244 state
->resolve
.pipeline
, &state
->alloc
);
248 radv_device_init_meta_resolve_state(struct radv_device
*device
)
250 VkResult res
= VK_SUCCESS
;
252 struct radv_shader_module vs_module
= { .nir
= radv_meta_build_nir_vs_generate_vertices() };
253 if (!vs_module
.nir
) {
254 /* XXX: Need more accurate error */
255 res
= VK_ERROR_OUT_OF_HOST_MEMORY
;
259 res
= create_pass(device
);
260 if (res
!= VK_SUCCESS
)
263 VkShaderModule vs_module_h
= radv_shader_module_to_handle(&vs_module
);
264 res
= create_pipeline(device
, vs_module_h
);
265 if (res
!= VK_SUCCESS
)
271 radv_device_finish_meta_resolve_state(device
);
274 ralloc_free(vs_module
.nir
);
280 emit_resolve(struct radv_cmd_buffer
*cmd_buffer
,
281 const VkOffset2D
*dest_offset
,
282 const VkExtent2D
*resolve_extent
)
284 struct radv_device
*device
= cmd_buffer
->device
;
285 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
287 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
;
289 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
290 device
->meta_state
.resolve
.pipeline
);
292 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
295 .width
= resolve_extent
->width
,
296 .height
= resolve_extent
->height
,
301 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkRect2D
) {
302 .offset
= *dest_offset
,
303 .extent
= *resolve_extent
,
306 radv_CmdDraw(cmd_buffer_h
, 3, 1, 0, 0);
307 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
;
310 enum radv_resolve_method
{
316 static void radv_pick_resolve_method_images(struct radv_image
*src_image
,
317 struct radv_image
*dest_image
,
318 enum radv_resolve_method
*method
)
321 if (dest_image
->surface
.micro_tile_mode
!= src_image
->surface
.micro_tile_mode
) {
322 if (dest_image
->surface
.num_dcc_levels
> 0)
323 *method
= RESOLVE_FRAGMENT
;
325 *method
= RESOLVE_COMPUTE
;
329 void radv_CmdResolveImage(
330 VkCommandBuffer cmd_buffer_h
,
332 VkImageLayout src_image_layout
,
333 VkImage dest_image_h
,
334 VkImageLayout dest_image_layout
,
335 uint32_t region_count
,
336 const VkImageResolve
* regions
)
338 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, cmd_buffer_h
);
339 RADV_FROM_HANDLE(radv_image
, src_image
, src_image_h
);
340 RADV_FROM_HANDLE(radv_image
, dest_image
, dest_image_h
);
341 struct radv_device
*device
= cmd_buffer
->device
;
342 struct radv_meta_saved_state saved_state
;
343 VkDevice device_h
= radv_device_to_handle(device
);
344 enum radv_resolve_method resolve_method
= RESOLVE_HW
;
345 /* we can use the hw resolve only for single full resolves */
346 if (region_count
== 1) {
347 if (regions
[0].srcOffset
.x
||
348 regions
[0].srcOffset
.y
||
349 regions
[0].srcOffset
.z
)
350 resolve_method
= RESOLVE_COMPUTE
;
351 if (regions
[0].dstOffset
.x
||
352 regions
[0].dstOffset
.y
||
353 regions
[0].dstOffset
.z
)
354 resolve_method
= RESOLVE_COMPUTE
;
356 if (regions
[0].extent
.width
!= src_image
->info
.width
||
357 regions
[0].extent
.height
!= src_image
->info
.height
||
358 regions
[0].extent
.depth
!= src_image
->info
.depth
)
359 resolve_method
= RESOLVE_COMPUTE
;
361 resolve_method
= RESOLVE_COMPUTE
;
363 radv_pick_resolve_method_images(src_image
, dest_image
,
366 if (resolve_method
== RESOLVE_FRAGMENT
) {
367 radv_meta_resolve_fragment_image(cmd_buffer
,
372 region_count
, regions
);
376 if (resolve_method
== RESOLVE_COMPUTE
) {
377 radv_meta_resolve_compute_image(cmd_buffer
,
382 region_count
, regions
);
386 radv_meta_save(&saved_state
, cmd_buffer
,
387 RADV_META_SAVE_GRAPHICS_PIPELINE
);
389 assert(src_image
->info
.samples
> 1);
390 if (src_image
->info
.samples
<= 1) {
391 /* this causes GPU hangs if we get past here */
392 fprintf(stderr
, "radv: Illegal resolve operation (src not multisampled), will hang GPU.");
395 assert(dest_image
->info
.samples
== 1);
397 if (src_image
->info
.samples
>= 16) {
398 /* See commit aa3f9aaf31e9056a255f9e0472ebdfdaa60abe54 for the
399 * glBlitFramebuffer workaround for samples >= 16.
401 radv_finishme("vkCmdResolveImage: need interpolation workaround when "
405 if (src_image
->info
.array_size
> 1)
406 radv_finishme("vkCmdResolveImage: multisample array images");
408 if (dest_image
->surface
.dcc_size
) {
409 radv_initialize_dcc(cmd_buffer
, dest_image
, 0xffffffff);
411 for (uint32_t r
= 0; r
< region_count
; ++r
) {
412 const VkImageResolve
*region
= ®ions
[r
];
414 /* From the Vulkan 1.0 spec:
416 * - The aspectMask member of srcSubresource and dstSubresource must
417 * only contain VK_IMAGE_ASPECT_COLOR_BIT
419 * - The layerCount member of srcSubresource and dstSubresource must
422 assert(region
->srcSubresource
.aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
423 assert(region
->dstSubresource
.aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
424 assert(region
->srcSubresource
.layerCount
==
425 region
->dstSubresource
.layerCount
);
427 const uint32_t src_base_layer
=
428 radv_meta_get_iview_layer(src_image
, ®ion
->srcSubresource
,
431 const uint32_t dest_base_layer
=
432 radv_meta_get_iview_layer(dest_image
, ®ion
->dstSubresource
,
436 * From Vulkan 1.0.6 spec: 18.6 Resolving Multisample Images
438 * extent is the size in texels of the source image to resolve in width,
439 * height and depth. 1D images use only x and width. 2D images use x, y,
440 * width and height. 3D images use x, y, z, width, height and depth.
442 * srcOffset and dstOffset select the initial x, y, and z offsets in
443 * texels of the sub-regions of the source and destination image data.
444 * extent is the size in texels of the source image to resolve in width,
445 * height and depth. 1D images use only x and width. 2D images use x, y,
446 * width and height. 3D images use x, y, z, width, height and depth.
448 const struct VkExtent3D extent
=
449 radv_sanitize_image_extent(src_image
->type
, region
->extent
);
450 const struct VkOffset3D dstOffset
=
451 radv_sanitize_image_offset(dest_image
->type
, region
->dstOffset
);
454 for (uint32_t layer
= 0; layer
< region
->srcSubresource
.layerCount
;
457 struct radv_image_view src_iview
;
458 radv_image_view_init(&src_iview
, cmd_buffer
->device
,
459 &(VkImageViewCreateInfo
) {
460 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
461 .image
= src_image_h
,
462 .viewType
= radv_meta_get_view_type(src_image
),
463 .format
= src_image
->vk_format
,
464 .subresourceRange
= {
465 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
466 .baseMipLevel
= region
->srcSubresource
.mipLevel
,
468 .baseArrayLayer
= src_base_layer
+ layer
,
473 struct radv_image_view dest_iview
;
474 radv_image_view_init(&dest_iview
, cmd_buffer
->device
,
475 &(VkImageViewCreateInfo
) {
476 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
477 .image
= dest_image_h
,
478 .viewType
= radv_meta_get_view_type(dest_image
),
479 .format
= dest_image
->vk_format
,
480 .subresourceRange
= {
481 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
482 .baseMipLevel
= region
->dstSubresource
.mipLevel
,
484 .baseArrayLayer
= dest_base_layer
+ layer
,
490 radv_CreateFramebuffer(device_h
,
491 &(VkFramebufferCreateInfo
) {
492 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
493 .attachmentCount
= 2,
494 .pAttachments
= (VkImageView
[]) {
495 radv_image_view_to_handle(&src_iview
),
496 radv_image_view_to_handle(&dest_iview
),
498 .width
= radv_minify(dest_image
->info
.width
,
499 region
->dstSubresource
.mipLevel
),
500 .height
= radv_minify(dest_image
->info
.height
,
501 region
->dstSubresource
.mipLevel
),
504 &cmd_buffer
->pool
->alloc
,
507 radv_CmdBeginRenderPass(cmd_buffer_h
,
508 &(VkRenderPassBeginInfo
) {
509 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
510 .renderPass
= device
->meta_state
.resolve
.pass
,
522 .clearValueCount
= 0,
523 .pClearValues
= NULL
,
525 VK_SUBPASS_CONTENTS_INLINE
);
527 emit_resolve(cmd_buffer
,
533 .width
= extent
.width
,
534 .height
= extent
.height
,
537 radv_CmdEndRenderPass(cmd_buffer_h
);
539 radv_DestroyFramebuffer(device_h
, fb_h
,
540 &cmd_buffer
->pool
->alloc
);
544 radv_meta_restore(&saved_state
, cmd_buffer
);
548 * Emit any needed resolves for the current subpass.
551 radv_cmd_buffer_resolve_subpass(struct radv_cmd_buffer
*cmd_buffer
)
553 struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
554 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
555 struct radv_meta_saved_state saved_state
;
556 enum radv_resolve_method resolve_method
= RESOLVE_HW
;
558 /* FINISHME(perf): Skip clears for resolve attachments.
560 * From the Vulkan 1.0 spec:
562 * If the first use of an attachment in a render pass is as a resolve
563 * attachment, then the loadOp is effectively ignored as the resolve is
564 * guaranteed to overwrite all pixels in the render area.
567 if (!subpass
->has_resolve
)
570 for (uint32_t i
= 0; i
< subpass
->color_count
; ++i
) {
571 VkAttachmentReference src_att
= subpass
->color_attachments
[i
];
572 VkAttachmentReference dest_att
= subpass
->resolve_attachments
[i
];
574 if (src_att
.attachment
== VK_ATTACHMENT_UNUSED
||
575 dest_att
.attachment
== VK_ATTACHMENT_UNUSED
)
578 struct radv_image
*dst_img
= cmd_buffer
->state
.framebuffer
->attachments
[dest_att
.attachment
].attachment
->image
;
579 struct radv_image
*src_img
= cmd_buffer
->state
.framebuffer
->attachments
[src_att
.attachment
].attachment
->image
;
581 radv_pick_resolve_method_images(dst_img
, src_img
, &resolve_method
);
582 if (resolve_method
== RESOLVE_FRAGMENT
) {
587 if (resolve_method
== RESOLVE_COMPUTE
) {
588 radv_cmd_buffer_resolve_subpass_cs(cmd_buffer
);
590 } else if (resolve_method
== RESOLVE_FRAGMENT
) {
591 radv_cmd_buffer_resolve_subpass_fs(cmd_buffer
);
595 radv_meta_save(&saved_state
, cmd_buffer
,
596 RADV_META_SAVE_GRAPHICS_PIPELINE
);
598 for (uint32_t i
= 0; i
< subpass
->color_count
; ++i
) {
599 VkAttachmentReference src_att
= subpass
->color_attachments
[i
];
600 VkAttachmentReference dest_att
= subpass
->resolve_attachments
[i
];
602 if (src_att
.attachment
== VK_ATTACHMENT_UNUSED
||
603 dest_att
.attachment
== VK_ATTACHMENT_UNUSED
)
606 struct radv_image
*dst_img
= cmd_buffer
->state
.framebuffer
->attachments
[dest_att
.attachment
].attachment
->image
;
608 if (dst_img
->surface
.dcc_size
) {
609 radv_initialize_dcc(cmd_buffer
, dst_img
, 0xffffffff);
610 cmd_buffer
->state
.attachments
[dest_att
.attachment
].current_layout
= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
613 struct radv_subpass resolve_subpass
= {
615 .color_attachments
= (VkAttachmentReference
[]) { src_att
, dest_att
},
616 .depth_stencil_attachment
= { .attachment
= VK_ATTACHMENT_UNUSED
},
619 radv_cmd_buffer_set_subpass(cmd_buffer
, &resolve_subpass
, false);
621 emit_resolve(cmd_buffer
,
622 &(VkOffset2D
) { 0, 0 },
623 &(VkExtent2D
) { fb
->width
, fb
->height
});
626 cmd_buffer
->state
.subpass
= subpass
;
627 radv_meta_restore(&saved_state
, cmd_buffer
);