2 * Copyright © 2015 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
24 #include "radv_debug.h"
25 #include "radv_meta.h"
26 #include "radv_private.h"
27 #include "nir/nir_builder.h"
29 #include "util/format_rgb9e5.h"
30 #include "vk_format.h"
34 DEPTH_CLEAR_FAST_EXPCLEAR
,
35 DEPTH_CLEAR_FAST_NO_EXPCLEAR
39 build_color_shaders(struct nir_shader
**out_vs
,
40 struct nir_shader
**out_fs
,
46 nir_builder_init_simple_shader(&vs_b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
47 nir_builder_init_simple_shader(&fs_b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
49 vs_b
.shader
->info
.name
= ralloc_strdup(vs_b
.shader
, "meta_clear_color_vs");
50 fs_b
.shader
->info
.name
= ralloc_strdup(fs_b
.shader
, "meta_clear_color_fs");
52 const struct glsl_type
*position_type
= glsl_vec4_type();
53 const struct glsl_type
*color_type
= glsl_vec4_type();
55 nir_variable
*vs_out_pos
=
56 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, position_type
,
58 vs_out_pos
->data
.location
= VARYING_SLOT_POS
;
60 nir_intrinsic_instr
*in_color_load
= nir_intrinsic_instr_create(fs_b
.shader
, nir_intrinsic_load_push_constant
);
61 nir_intrinsic_set_base(in_color_load
, 0);
62 nir_intrinsic_set_range(in_color_load
, 16);
63 in_color_load
->src
[0] = nir_src_for_ssa(nir_imm_int(&fs_b
, 0));
64 in_color_load
->num_components
= 4;
65 nir_ssa_dest_init(&in_color_load
->instr
, &in_color_load
->dest
, 4, 32, "clear color");
66 nir_builder_instr_insert(&fs_b
, &in_color_load
->instr
);
68 nir_variable
*fs_out_color
=
69 nir_variable_create(fs_b
.shader
, nir_var_shader_out
, color_type
,
71 fs_out_color
->data
.location
= FRAG_RESULT_DATA0
+ frag_output
;
73 nir_store_var(&fs_b
, fs_out_color
, &in_color_load
->dest
.ssa
, 0xf);
75 nir_ssa_def
*outvec
= radv_meta_gen_rect_vertices(&vs_b
);
76 nir_store_var(&vs_b
, vs_out_pos
, outvec
, 0xf);
78 const struct glsl_type
*layer_type
= glsl_int_type();
79 nir_variable
*vs_out_layer
=
80 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, layer_type
,
82 vs_out_layer
->data
.location
= VARYING_SLOT_LAYER
;
83 vs_out_layer
->data
.interpolation
= INTERP_MODE_FLAT
;
84 nir_ssa_def
*inst_id
= nir_load_instance_id(&vs_b
);
85 nir_ssa_def
*base_instance
= nir_load_base_instance(&vs_b
);
87 nir_ssa_def
*layer_id
= nir_iadd(&vs_b
, inst_id
, base_instance
);
88 nir_store_var(&vs_b
, vs_out_layer
, layer_id
, 0x1);
90 *out_vs
= vs_b
.shader
;
91 *out_fs
= fs_b
.shader
;
95 create_pipeline(struct radv_device
*device
,
96 struct radv_render_pass
*render_pass
,
98 struct nir_shader
*vs_nir
,
99 struct nir_shader
*fs_nir
,
100 const VkPipelineVertexInputStateCreateInfo
*vi_state
,
101 const VkPipelineDepthStencilStateCreateInfo
*ds_state
,
102 const VkPipelineColorBlendStateCreateInfo
*cb_state
,
103 const VkPipelineLayout layout
,
104 const struct radv_graphics_pipeline_create_info
*extra
,
105 const VkAllocationCallbacks
*alloc
,
106 VkPipeline
*pipeline
)
108 VkDevice device_h
= radv_device_to_handle(device
);
111 struct radv_shader_module vs_m
= { .nir
= vs_nir
};
112 struct radv_shader_module fs_m
= { .nir
= fs_nir
};
114 result
= radv_graphics_pipeline_create(device_h
,
115 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
116 &(VkGraphicsPipelineCreateInfo
) {
117 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
118 .stageCount
= fs_nir
? 2 : 1,
119 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
121 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
122 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
123 .module
= radv_shader_module_to_handle(&vs_m
),
127 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
128 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
129 .module
= radv_shader_module_to_handle(&fs_m
),
133 .pVertexInputState
= vi_state
,
134 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
135 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
136 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
137 .primitiveRestartEnable
= false,
139 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
140 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
144 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
145 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
146 .rasterizerDiscardEnable
= false,
147 .polygonMode
= VK_POLYGON_MODE_FILL
,
148 .cullMode
= VK_CULL_MODE_NONE
,
149 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
,
150 .depthBiasEnable
= false,
152 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
153 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
154 .rasterizationSamples
= samples
,
155 .sampleShadingEnable
= false,
157 .alphaToCoverageEnable
= false,
158 .alphaToOneEnable
= false,
160 .pDepthStencilState
= ds_state
,
161 .pColorBlendState
= cb_state
,
162 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
163 /* The meta clear pipeline declares all state as dynamic.
164 * As a consequence, vkCmdBindPipeline writes no dynamic state
165 * to the cmd buffer. Therefore, at the end of the meta clear,
166 * we need only restore dynamic state was vkCmdSet.
168 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
169 .dynamicStateCount
= 8,
170 .pDynamicStates
= (VkDynamicState
[]) {
171 /* Everything except stencil write mask */
172 VK_DYNAMIC_STATE_VIEWPORT
,
173 VK_DYNAMIC_STATE_SCISSOR
,
174 VK_DYNAMIC_STATE_LINE_WIDTH
,
175 VK_DYNAMIC_STATE_DEPTH_BIAS
,
176 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
177 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
178 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
179 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
184 .renderPass
= radv_render_pass_to_handle(render_pass
),
198 create_color_renderpass(struct radv_device
*device
,
203 mtx_lock(&device
->meta_state
.mtx
);
205 mtx_unlock (&device
->meta_state
.mtx
);
209 VkResult result
= radv_CreateRenderPass(radv_device_to_handle(device
),
210 &(VkRenderPassCreateInfo
) {
211 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
212 .attachmentCount
= 1,
213 .pAttachments
= &(VkAttachmentDescription
) {
216 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
217 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
218 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
219 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
222 .pSubpasses
= &(VkSubpassDescription
) {
223 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
224 .inputAttachmentCount
= 0,
225 .colorAttachmentCount
= 1,
226 .pColorAttachments
= &(VkAttachmentReference
) {
228 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
230 .pResolveAttachments
= NULL
,
231 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
232 .attachment
= VK_ATTACHMENT_UNUSED
,
233 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
235 .preserveAttachmentCount
= 0,
236 .pPreserveAttachments
= NULL
,
238 .dependencyCount
= 0,
239 }, &device
->meta_state
.alloc
, pass
);
240 mtx_unlock(&device
->meta_state
.mtx
);
245 create_color_pipeline(struct radv_device
*device
,
247 uint32_t frag_output
,
248 VkPipeline
*pipeline
,
251 struct nir_shader
*vs_nir
;
252 struct nir_shader
*fs_nir
;
255 mtx_lock(&device
->meta_state
.mtx
);
257 mtx_unlock(&device
->meta_state
.mtx
);
261 build_color_shaders(&vs_nir
, &fs_nir
, frag_output
);
263 const VkPipelineVertexInputStateCreateInfo vi_state
= {
264 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
265 .vertexBindingDescriptionCount
= 0,
266 .vertexAttributeDescriptionCount
= 0,
269 const VkPipelineDepthStencilStateCreateInfo ds_state
= {
270 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
271 .depthTestEnable
= false,
272 .depthWriteEnable
= false,
273 .depthBoundsTestEnable
= false,
274 .stencilTestEnable
= false,
277 VkPipelineColorBlendAttachmentState blend_attachment_state
[MAX_RTS
] = { 0 };
278 blend_attachment_state
[frag_output
] = (VkPipelineColorBlendAttachmentState
) {
279 .blendEnable
= false,
280 .colorWriteMask
= VK_COLOR_COMPONENT_A_BIT
|
281 VK_COLOR_COMPONENT_R_BIT
|
282 VK_COLOR_COMPONENT_G_BIT
|
283 VK_COLOR_COMPONENT_B_BIT
,
286 const VkPipelineColorBlendStateCreateInfo cb_state
= {
287 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
288 .logicOpEnable
= false,
289 .attachmentCount
= MAX_RTS
,
290 .pAttachments
= blend_attachment_state
294 struct radv_graphics_pipeline_create_info extra
= {
295 .use_rectlist
= true,
297 result
= create_pipeline(device
, radv_render_pass_from_handle(pass
),
298 samples
, vs_nir
, fs_nir
, &vi_state
, &ds_state
, &cb_state
,
299 device
->meta_state
.clear_color_p_layout
,
300 &extra
, &device
->meta_state
.alloc
, pipeline
);
302 mtx_unlock(&device
->meta_state
.mtx
);
307 finish_meta_clear_htile_mask_state(struct radv_device
*device
)
309 struct radv_meta_state
*state
= &device
->meta_state
;
311 radv_DestroyPipeline(radv_device_to_handle(device
),
312 state
->clear_htile_mask_pipeline
,
314 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
315 state
->clear_htile_mask_p_layout
,
317 radv_DestroyDescriptorSetLayout(radv_device_to_handle(device
),
318 state
->clear_htile_mask_ds_layout
,
323 radv_device_finish_meta_clear_state(struct radv_device
*device
)
325 struct radv_meta_state
*state
= &device
->meta_state
;
327 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->clear
); ++i
) {
328 for (uint32_t j
= 0; j
< ARRAY_SIZE(state
->clear
[i
].color_pipelines
); ++j
) {
329 radv_DestroyPipeline(radv_device_to_handle(device
),
330 state
->clear
[i
].color_pipelines
[j
],
332 radv_DestroyRenderPass(radv_device_to_handle(device
),
333 state
->clear
[i
].render_pass
[j
],
337 for (uint32_t j
= 0; j
< NUM_DEPTH_CLEAR_PIPELINES
; j
++) {
338 radv_DestroyPipeline(radv_device_to_handle(device
),
339 state
->clear
[i
].depth_only_pipeline
[j
],
341 radv_DestroyPipeline(radv_device_to_handle(device
),
342 state
->clear
[i
].stencil_only_pipeline
[j
],
344 radv_DestroyPipeline(radv_device_to_handle(device
),
345 state
->clear
[i
].depthstencil_pipeline
[j
],
348 radv_DestroyRenderPass(radv_device_to_handle(device
),
349 state
->clear
[i
].depthstencil_rp
,
352 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
353 state
->clear_color_p_layout
,
355 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
356 state
->clear_depth_p_layout
,
359 finish_meta_clear_htile_mask_state(device
);
363 emit_color_clear(struct radv_cmd_buffer
*cmd_buffer
,
364 const VkClearAttachment
*clear_att
,
365 const VkClearRect
*clear_rect
,
368 struct radv_device
*device
= cmd_buffer
->device
;
369 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
370 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
371 const uint32_t subpass_att
= clear_att
->colorAttachment
;
372 const uint32_t pass_att
= subpass
->color_attachments
[subpass_att
].attachment
;
373 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
374 const uint32_t samples
= iview
->image
->info
.samples
;
375 const uint32_t samples_log2
= ffs(samples
) - 1;
376 unsigned fs_key
= radv_format_meta_fs_key(iview
->vk_format
);
377 VkClearColorValue clear_value
= clear_att
->clearValue
.color
;
378 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
382 radv_finishme("color clears incomplete");
386 if (device
->meta_state
.clear
[samples_log2
].render_pass
[fs_key
] == VK_NULL_HANDLE
) {
387 VkResult ret
= create_color_renderpass(device
, radv_fs_key_format_exemplars
[fs_key
],
389 &device
->meta_state
.clear
[samples_log2
].render_pass
[fs_key
]);
390 if (ret
!= VK_SUCCESS
) {
391 cmd_buffer
->record_result
= ret
;
396 if (device
->meta_state
.clear
[samples_log2
].color_pipelines
[fs_key
] == VK_NULL_HANDLE
) {
397 VkResult ret
= create_color_pipeline(device
, samples
, 0,
398 &device
->meta_state
.clear
[samples_log2
].color_pipelines
[fs_key
],
399 device
->meta_state
.clear
[samples_log2
].render_pass
[fs_key
]);
400 if (ret
!= VK_SUCCESS
) {
401 cmd_buffer
->record_result
= ret
;
406 pipeline
= device
->meta_state
.clear
[samples_log2
].color_pipelines
[fs_key
];
408 radv_finishme("color clears incomplete");
411 assert(samples_log2
< ARRAY_SIZE(device
->meta_state
.clear
));
413 assert(clear_att
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
414 assert(clear_att
->colorAttachment
< subpass
->color_count
);
416 radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer
),
417 device
->meta_state
.clear_color_p_layout
,
418 VK_SHADER_STAGE_FRAGMENT_BIT
, 0, 16,
421 struct radv_subpass clear_subpass
= {
423 .color_attachments
= (struct radv_subpass_attachment
[]) {
424 subpass
->color_attachments
[clear_att
->colorAttachment
]
426 .depth_stencil_attachment
= NULL
,
429 radv_cmd_buffer_set_subpass(cmd_buffer
, &clear_subpass
);
431 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
434 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
435 .x
= clear_rect
->rect
.offset
.x
,
436 .y
= clear_rect
->rect
.offset
.y
,
437 .width
= clear_rect
->rect
.extent
.width
,
438 .height
= clear_rect
->rect
.extent
.height
,
443 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &clear_rect
->rect
);
447 for_each_bit(i
, view_mask
)
448 radv_CmdDraw(cmd_buffer_h
, 3, 1, 0, i
);
450 radv_CmdDraw(cmd_buffer_h
, 3, clear_rect
->layerCount
, 0, clear_rect
->baseArrayLayer
);
453 radv_cmd_buffer_set_subpass(cmd_buffer
, subpass
);
458 build_depthstencil_shader(struct nir_shader
**out_vs
, struct nir_shader
**out_fs
)
460 nir_builder vs_b
, fs_b
;
462 nir_builder_init_simple_shader(&vs_b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
463 nir_builder_init_simple_shader(&fs_b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
465 vs_b
.shader
->info
.name
= ralloc_strdup(vs_b
.shader
, "meta_clear_depthstencil_vs");
466 fs_b
.shader
->info
.name
= ralloc_strdup(fs_b
.shader
, "meta_clear_depthstencil_fs");
467 const struct glsl_type
*position_out_type
= glsl_vec4_type();
469 nir_variable
*vs_out_pos
=
470 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, position_out_type
,
472 vs_out_pos
->data
.location
= VARYING_SLOT_POS
;
474 nir_intrinsic_instr
*in_color_load
= nir_intrinsic_instr_create(vs_b
.shader
, nir_intrinsic_load_push_constant
);
475 nir_intrinsic_set_base(in_color_load
, 0);
476 nir_intrinsic_set_range(in_color_load
, 4);
477 in_color_load
->src
[0] = nir_src_for_ssa(nir_imm_int(&vs_b
, 0));
478 in_color_load
->num_components
= 1;
479 nir_ssa_dest_init(&in_color_load
->instr
, &in_color_load
->dest
, 1, 32, "depth value");
480 nir_builder_instr_insert(&vs_b
, &in_color_load
->instr
);
482 nir_ssa_def
*outvec
= radv_meta_gen_rect_vertices_comp2(&vs_b
, &in_color_load
->dest
.ssa
);
483 nir_store_var(&vs_b
, vs_out_pos
, outvec
, 0xf);
485 const struct glsl_type
*layer_type
= glsl_int_type();
486 nir_variable
*vs_out_layer
=
487 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, layer_type
,
489 vs_out_layer
->data
.location
= VARYING_SLOT_LAYER
;
490 vs_out_layer
->data
.interpolation
= INTERP_MODE_FLAT
;
491 nir_ssa_def
*inst_id
= nir_load_instance_id(&vs_b
);
492 nir_ssa_def
*base_instance
= nir_load_base_instance(&vs_b
);
494 nir_ssa_def
*layer_id
= nir_iadd(&vs_b
, inst_id
, base_instance
);
495 nir_store_var(&vs_b
, vs_out_layer
, layer_id
, 0x1);
497 *out_vs
= vs_b
.shader
;
498 *out_fs
= fs_b
.shader
;
502 create_depthstencil_renderpass(struct radv_device
*device
,
504 VkRenderPass
*render_pass
)
506 mtx_lock(&device
->meta_state
.mtx
);
508 mtx_unlock(&device
->meta_state
.mtx
);
512 VkResult result
= radv_CreateRenderPass(radv_device_to_handle(device
),
513 &(VkRenderPassCreateInfo
) {
514 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
515 .attachmentCount
= 1,
516 .pAttachments
= &(VkAttachmentDescription
) {
517 .format
= VK_FORMAT_D32_SFLOAT_S8_UINT
,
519 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
520 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
521 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
522 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
525 .pSubpasses
= &(VkSubpassDescription
) {
526 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
527 .inputAttachmentCount
= 0,
528 .colorAttachmentCount
= 0,
529 .pColorAttachments
= NULL
,
530 .pResolveAttachments
= NULL
,
531 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
533 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
535 .preserveAttachmentCount
= 0,
536 .pPreserveAttachments
= NULL
,
538 .dependencyCount
= 0,
539 }, &device
->meta_state
.alloc
, render_pass
);
540 mtx_unlock(&device
->meta_state
.mtx
);
545 create_depthstencil_pipeline(struct radv_device
*device
,
546 VkImageAspectFlags aspects
,
549 VkPipeline
*pipeline
,
550 VkRenderPass render_pass
)
552 struct nir_shader
*vs_nir
, *fs_nir
;
555 mtx_lock(&device
->meta_state
.mtx
);
557 mtx_unlock(&device
->meta_state
.mtx
);
561 build_depthstencil_shader(&vs_nir
, &fs_nir
);
563 const VkPipelineVertexInputStateCreateInfo vi_state
= {
564 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
565 .vertexBindingDescriptionCount
= 0,
566 .vertexAttributeDescriptionCount
= 0,
569 const VkPipelineDepthStencilStateCreateInfo ds_state
= {
570 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
571 .depthTestEnable
= (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
),
572 .depthCompareOp
= VK_COMPARE_OP_ALWAYS
,
573 .depthWriteEnable
= (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
),
574 .depthBoundsTestEnable
= false,
575 .stencilTestEnable
= (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
),
577 .passOp
= VK_STENCIL_OP_REPLACE
,
578 .compareOp
= VK_COMPARE_OP_ALWAYS
,
579 .writeMask
= UINT32_MAX
,
580 .reference
= 0, /* dynamic */
582 .back
= { 0 /* dont care */ },
585 const VkPipelineColorBlendStateCreateInfo cb_state
= {
586 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
587 .logicOpEnable
= false,
588 .attachmentCount
= 0,
589 .pAttachments
= NULL
,
592 struct radv_graphics_pipeline_create_info extra
= {
593 .use_rectlist
= true,
596 if (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
597 extra
.db_depth_clear
= index
== DEPTH_CLEAR_SLOW
? false : true;
598 extra
.db_depth_disable_expclear
= index
== DEPTH_CLEAR_FAST_NO_EXPCLEAR
? true : false;
600 if (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
601 extra
.db_stencil_clear
= index
== DEPTH_CLEAR_SLOW
? false : true;
602 extra
.db_stencil_disable_expclear
= index
== DEPTH_CLEAR_FAST_NO_EXPCLEAR
? true : false;
604 result
= create_pipeline(device
, radv_render_pass_from_handle(render_pass
),
605 samples
, vs_nir
, fs_nir
, &vi_state
, &ds_state
, &cb_state
,
606 device
->meta_state
.clear_depth_p_layout
,
607 &extra
, &device
->meta_state
.alloc
, pipeline
);
609 mtx_unlock(&device
->meta_state
.mtx
);
613 static bool depth_view_can_fast_clear(struct radv_cmd_buffer
*cmd_buffer
,
614 const struct radv_image_view
*iview
,
615 VkImageAspectFlags aspects
,
616 VkImageLayout layout
,
617 const VkClearRect
*clear_rect
,
618 VkClearDepthStencilValue clear_value
)
620 uint32_t queue_mask
= radv_image_queue_family_mask(iview
->image
,
621 cmd_buffer
->queue_family_index
,
622 cmd_buffer
->queue_family_index
);
623 if (clear_rect
->rect
.offset
.x
|| clear_rect
->rect
.offset
.y
||
624 clear_rect
->rect
.extent
.width
!= iview
->extent
.width
||
625 clear_rect
->rect
.extent
.height
!= iview
->extent
.height
)
627 if (radv_image_is_tc_compat_htile(iview
->image
) &&
628 (((aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) && clear_value
.depth
!= 0.0 &&
629 clear_value
.depth
!= 1.0) ||
630 ((aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) && clear_value
.stencil
!= 0)))
632 if (radv_image_has_htile(iview
->image
) &&
633 iview
->base_mip
== 0 &&
634 iview
->base_layer
== 0 &&
635 radv_layout_is_htile_compressed(iview
->image
, layout
, queue_mask
) &&
636 !radv_image_extent_compare(iview
->image
, &iview
->extent
))
642 pick_depthstencil_pipeline(struct radv_cmd_buffer
*cmd_buffer
,
643 struct radv_meta_state
*meta_state
,
644 const struct radv_image_view
*iview
,
646 VkImageAspectFlags aspects
,
647 VkImageLayout layout
,
648 const VkClearRect
*clear_rect
,
649 VkClearDepthStencilValue clear_value
)
651 bool fast
= depth_view_can_fast_clear(cmd_buffer
, iview
, aspects
, layout
, clear_rect
, clear_value
);
652 int index
= DEPTH_CLEAR_SLOW
;
653 VkPipeline
*pipeline
;
656 /* we don't know the previous clear values, so we always have
657 * the NO_EXPCLEAR path */
658 index
= DEPTH_CLEAR_FAST_NO_EXPCLEAR
;
662 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
663 pipeline
= &meta_state
->clear
[samples_log2
].depthstencil_pipeline
[index
];
665 case VK_IMAGE_ASPECT_DEPTH_BIT
:
666 pipeline
= &meta_state
->clear
[samples_log2
].depth_only_pipeline
[index
];
668 case VK_IMAGE_ASPECT_STENCIL_BIT
:
669 pipeline
= &meta_state
->clear
[samples_log2
].stencil_only_pipeline
[index
];
672 unreachable("expected depth or stencil aspect");
675 if (cmd_buffer
->device
->meta_state
.clear
[samples_log2
].depthstencil_rp
== VK_NULL_HANDLE
) {
676 VkResult ret
= create_depthstencil_renderpass(cmd_buffer
->device
, 1u << samples_log2
,
677 &cmd_buffer
->device
->meta_state
.clear
[samples_log2
].depthstencil_rp
);
678 if (ret
!= VK_SUCCESS
) {
679 cmd_buffer
->record_result
= ret
;
680 return VK_NULL_HANDLE
;
684 if (*pipeline
== VK_NULL_HANDLE
) {
685 VkResult ret
= create_depthstencil_pipeline(cmd_buffer
->device
, aspects
, 1u << samples_log2
, index
,
686 pipeline
, cmd_buffer
->device
->meta_state
.clear
[samples_log2
].depthstencil_rp
);
687 if (ret
!= VK_SUCCESS
) {
688 cmd_buffer
->record_result
= ret
;
689 return VK_NULL_HANDLE
;
696 emit_depthstencil_clear(struct radv_cmd_buffer
*cmd_buffer
,
697 const VkClearAttachment
*clear_att
,
698 const VkClearRect
*clear_rect
,
701 struct radv_device
*device
= cmd_buffer
->device
;
702 struct radv_meta_state
*meta_state
= &device
->meta_state
;
703 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
704 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
705 const uint32_t pass_att
= subpass
->depth_stencil_attachment
->attachment
;
706 VkClearDepthStencilValue clear_value
= clear_att
->clearValue
.depthStencil
;
707 VkImageAspectFlags aspects
= clear_att
->aspectMask
;
708 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
709 const uint32_t samples
= iview
->image
->info
.samples
;
710 const uint32_t samples_log2
= ffs(samples
) - 1;
711 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
713 assert(pass_att
!= VK_ATTACHMENT_UNUSED
);
715 if (!(aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
))
716 clear_value
.depth
= 1.0f
;
718 radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer
),
719 device
->meta_state
.clear_depth_p_layout
,
720 VK_SHADER_STAGE_VERTEX_BIT
, 0, 4,
723 uint32_t prev_reference
= cmd_buffer
->state
.dynamic
.stencil_reference
.front
;
724 if (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
725 radv_CmdSetStencilReference(cmd_buffer_h
, VK_STENCIL_FACE_FRONT_BIT
,
726 clear_value
.stencil
);
729 VkPipeline pipeline
= pick_depthstencil_pipeline(cmd_buffer
,
734 subpass
->depth_stencil_attachment
->layout
,
740 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
743 if (depth_view_can_fast_clear(cmd_buffer
, iview
, aspects
,
744 subpass
->depth_stencil_attachment
->layout
,
745 clear_rect
, clear_value
))
746 radv_update_ds_clear_metadata(cmd_buffer
, iview
->image
,
747 clear_value
, aspects
);
749 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
750 .x
= clear_rect
->rect
.offset
.x
,
751 .y
= clear_rect
->rect
.offset
.y
,
752 .width
= clear_rect
->rect
.extent
.width
,
753 .height
= clear_rect
->rect
.extent
.height
,
758 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &clear_rect
->rect
);
762 for_each_bit(i
, view_mask
)
763 radv_CmdDraw(cmd_buffer_h
, 3, 1, 0, i
);
765 radv_CmdDraw(cmd_buffer_h
, 3, clear_rect
->layerCount
, 0, clear_rect
->baseArrayLayer
);
768 if (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
769 radv_CmdSetStencilReference(cmd_buffer_h
, VK_STENCIL_FACE_FRONT_BIT
,
775 clear_htile_mask(struct radv_cmd_buffer
*cmd_buffer
,
776 struct radeon_winsys_bo
*bo
, uint64_t offset
, uint64_t size
,
777 uint32_t htile_value
, uint32_t htile_mask
)
779 struct radv_device
*device
= cmd_buffer
->device
;
780 struct radv_meta_state
*state
= &device
->meta_state
;
781 uint64_t block_count
= round_up_u64(size
, 1024);
782 struct radv_meta_saved_state saved_state
;
784 radv_meta_save(&saved_state
, cmd_buffer
,
785 RADV_META_SAVE_COMPUTE_PIPELINE
|
786 RADV_META_SAVE_CONSTANTS
|
787 RADV_META_SAVE_DESCRIPTORS
);
789 struct radv_buffer dst_buffer
= {
795 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer
),
796 VK_PIPELINE_BIND_POINT_COMPUTE
,
797 state
->clear_htile_mask_pipeline
);
799 radv_meta_push_descriptor_set(cmd_buffer
, VK_PIPELINE_BIND_POINT_COMPUTE
,
800 state
->clear_htile_mask_p_layout
,
802 1, /* descriptorWriteCount */
803 (VkWriteDescriptorSet
[]) {
805 .sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
,
807 .dstArrayElement
= 0,
808 .descriptorCount
= 1,
809 .descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
,
810 .pBufferInfo
= &(VkDescriptorBufferInfo
) {
811 .buffer
= radv_buffer_to_handle(&dst_buffer
),
818 const unsigned constants
[2] = {
819 htile_value
& htile_mask
,
823 radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer
),
824 state
->clear_htile_mask_p_layout
,
825 VK_SHADER_STAGE_COMPUTE_BIT
, 0, 8,
828 radv_CmdDispatch(radv_cmd_buffer_to_handle(cmd_buffer
), block_count
, 1, 1);
830 radv_meta_restore(&saved_state
, cmd_buffer
);
832 return RADV_CMD_FLAG_CS_PARTIAL_FLUSH
|
833 RADV_CMD_FLAG_INV_VMEM_L1
|
834 RADV_CMD_FLAG_WRITEBACK_GLOBAL_L2
;
838 radv_get_htile_fast_clear_value(const struct radv_image
*image
,
839 VkClearDepthStencilValue value
)
841 uint32_t clear_value
;
843 if (!image
->surface
.has_stencil
) {
844 clear_value
= value
.depth
? 0xfffffff0 : 0;
846 clear_value
= value
.depth
? 0xfffc0000 : 0;
853 radv_get_htile_mask(const struct radv_image
*image
, VkImageAspectFlags aspects
)
857 if (!image
->surface
.has_stencil
) {
858 /* All the HTILE buffer is used when there is no stencil. */
861 if (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
)
863 if (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
)
871 radv_is_fast_clear_depth_allowed(VkClearDepthStencilValue value
)
873 return value
.depth
== 1.0f
|| value
.depth
== 0.0f
;
877 radv_is_fast_clear_stencil_allowed(VkClearDepthStencilValue value
)
879 return value
.stencil
== 0;
883 * Determine if the given image can be fast cleared.
886 radv_image_can_fast_clear(struct radv_device
*device
, struct radv_image
*image
)
888 if (device
->instance
->debug_flags
& RADV_DEBUG_NO_FAST_CLEARS
)
891 if (vk_format_is_color(image
->vk_format
)) {
892 if (!radv_image_has_cmask(image
) && !radv_image_has_dcc(image
))
895 /* RB+ doesn't work with CMASK fast clear on Stoney. */
896 if (!radv_image_has_dcc(image
) &&
897 device
->physical_device
->rad_info
.family
== CHIP_STONEY
)
900 if (!radv_image_has_htile(image
))
904 /* Do not fast clears 3D images. */
905 if (image
->type
== VK_IMAGE_TYPE_3D
)
912 * Determine if the given image view can be fast cleared.
915 radv_image_view_can_fast_clear(struct radv_device
*device
,
916 const struct radv_image_view
*iview
)
918 struct radv_image
*image
= iview
->image
;
920 /* Only fast clear if the image itself can be fast cleared. */
921 if (!radv_image_can_fast_clear(device
, image
))
924 /* Only fast clear if all layers are bound. */
925 if (iview
->base_layer
> 0 ||
926 iview
->layer_count
!= image
->info
.array_size
)
929 /* Only fast clear if the view covers the whole image. */
930 if (!radv_image_extent_compare(image
, &iview
->extent
))
937 radv_can_fast_clear_depth(struct radv_cmd_buffer
*cmd_buffer
,
938 const struct radv_image_view
*iview
,
939 VkImageLayout image_layout
,
940 VkImageAspectFlags aspects
,
941 const VkClearRect
*clear_rect
,
942 const VkClearDepthStencilValue clear_value
,
945 if (!radv_image_view_can_fast_clear(cmd_buffer
->device
, iview
))
948 if (!radv_layout_is_htile_compressed(iview
->image
, image_layout
, radv_image_queue_family_mask(iview
->image
, cmd_buffer
->queue_family_index
, cmd_buffer
->queue_family_index
)))
951 if (clear_rect
->rect
.offset
.x
|| clear_rect
->rect
.offset
.y
||
952 clear_rect
->rect
.extent
.width
!= iview
->image
->info
.width
||
953 clear_rect
->rect
.extent
.height
!= iview
->image
->info
.height
)
956 if (view_mask
&& (iview
->image
->info
.array_size
>= 32 ||
957 (1u << iview
->image
->info
.array_size
) - 1u != view_mask
))
959 if (!view_mask
&& clear_rect
->baseArrayLayer
!= 0)
961 if (!view_mask
&& clear_rect
->layerCount
!= iview
->image
->info
.array_size
)
964 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
< GFX9
&&
965 (!(aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) ||
966 ((vk_format_aspects(iview
->image
->vk_format
) & VK_IMAGE_ASPECT_STENCIL_BIT
) &&
967 !(aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
))))
970 if (((aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) &&
971 !radv_is_fast_clear_depth_allowed(clear_value
)) ||
972 ((aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
973 !radv_is_fast_clear_stencil_allowed(clear_value
)))
980 radv_fast_clear_depth(struct radv_cmd_buffer
*cmd_buffer
,
981 const struct radv_image_view
*iview
,
982 const VkClearAttachment
*clear_att
,
983 enum radv_cmd_flush_bits
*pre_flush
,
984 enum radv_cmd_flush_bits
*post_flush
)
986 VkClearDepthStencilValue clear_value
= clear_att
->clearValue
.depthStencil
;
987 VkImageAspectFlags aspects
= clear_att
->aspectMask
;
988 uint32_t clear_word
, flush_bits
;
991 clear_word
= radv_get_htile_fast_clear_value(iview
->image
, clear_value
);
992 htile_mask
= radv_get_htile_mask(iview
->image
, aspects
);
995 cmd_buffer
->state
.flush_bits
|= (RADV_CMD_FLAG_FLUSH_AND_INV_DB
|
996 RADV_CMD_FLAG_FLUSH_AND_INV_DB_META
) & ~ *pre_flush
;
997 *pre_flush
|= cmd_buffer
->state
.flush_bits
;
1000 if (htile_mask
== UINT_MAX
) {
1001 /* Clear the whole HTILE buffer. */
1002 flush_bits
= radv_fill_buffer(cmd_buffer
, iview
->image
->bo
,
1003 iview
->image
->offset
+ iview
->image
->htile_offset
,
1004 iview
->image
->surface
.htile_size
, clear_word
);
1006 /* Only clear depth or stencil bytes in the HTILE buffer. */
1007 assert(cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX9
);
1008 flush_bits
= clear_htile_mask(cmd_buffer
, iview
->image
->bo
,
1009 iview
->image
->offset
+ iview
->image
->htile_offset
,
1010 iview
->image
->surface
.htile_size
, clear_word
,
1014 radv_update_ds_clear_metadata(cmd_buffer
, iview
->image
, clear_value
, aspects
);
1016 *post_flush
|= flush_bits
;
1021 build_clear_htile_mask_shader()
1025 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_COMPUTE
, NULL
);
1026 b
.shader
->info
.name
= ralloc_strdup(b
.shader
, "meta_clear_htile_mask");
1027 b
.shader
->info
.cs
.local_size
[0] = 64;
1028 b
.shader
->info
.cs
.local_size
[1] = 1;
1029 b
.shader
->info
.cs
.local_size
[2] = 1;
1031 nir_ssa_def
*invoc_id
= nir_load_local_invocation_id(&b
);
1032 nir_ssa_def
*wg_id
= nir_load_work_group_id(&b
);
1033 nir_ssa_def
*block_size
= nir_imm_ivec4(&b
,
1034 b
.shader
->info
.cs
.local_size
[0],
1035 b
.shader
->info
.cs
.local_size
[1],
1036 b
.shader
->info
.cs
.local_size
[2], 0);
1038 nir_ssa_def
*global_id
= nir_iadd(&b
, nir_imul(&b
, wg_id
, block_size
), invoc_id
);
1040 nir_ssa_def
*offset
= nir_imul(&b
, global_id
, nir_imm_int(&b
, 16));
1041 offset
= nir_channel(&b
, offset
, 0);
1043 nir_intrinsic_instr
*buf
=
1044 nir_intrinsic_instr_create(b
.shader
,
1045 nir_intrinsic_vulkan_resource_index
);
1047 buf
->src
[0] = nir_src_for_ssa(nir_imm_int(&b
, 0));
1048 nir_intrinsic_set_desc_set(buf
, 0);
1049 nir_intrinsic_set_binding(buf
, 0);
1050 nir_ssa_dest_init(&buf
->instr
, &buf
->dest
, 1, 32, NULL
);
1051 nir_builder_instr_insert(&b
, &buf
->instr
);
1053 nir_intrinsic_instr
*constants
=
1054 nir_intrinsic_instr_create(b
.shader
,
1055 nir_intrinsic_load_push_constant
);
1056 nir_intrinsic_set_base(constants
, 0);
1057 nir_intrinsic_set_range(constants
, 8);
1058 constants
->src
[0] = nir_src_for_ssa(nir_imm_int(&b
, 0));
1059 constants
->num_components
= 2;
1060 nir_ssa_dest_init(&constants
->instr
, &constants
->dest
, 2, 32, "constants");
1061 nir_builder_instr_insert(&b
, &constants
->instr
);
1063 nir_intrinsic_instr
*load
=
1064 nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_load_ssbo
);
1065 load
->src
[0] = nir_src_for_ssa(&buf
->dest
.ssa
);
1066 load
->src
[1] = nir_src_for_ssa(offset
);
1067 nir_ssa_dest_init(&load
->instr
, &load
->dest
, 4, 32, NULL
);
1068 load
->num_components
= 4;
1069 nir_builder_instr_insert(&b
, &load
->instr
);
1071 /* data = (data & ~htile_mask) | (htile_value & htile_mask) */
1073 nir_iand(&b
, &load
->dest
.ssa
,
1074 nir_channel(&b
, &constants
->dest
.ssa
, 1));
1075 data
= nir_ior(&b
, data
, nir_channel(&b
, &constants
->dest
.ssa
, 0));
1077 nir_intrinsic_instr
*store
=
1078 nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_store_ssbo
);
1079 store
->src
[0] = nir_src_for_ssa(data
);
1080 store
->src
[1] = nir_src_for_ssa(&buf
->dest
.ssa
);
1081 store
->src
[2] = nir_src_for_ssa(offset
);
1082 nir_intrinsic_set_write_mask(store
, 0xf);
1083 store
->num_components
= 4;
1084 nir_builder_instr_insert(&b
, &store
->instr
);
1090 init_meta_clear_htile_mask_state(struct radv_device
*device
)
1092 struct radv_meta_state
*state
= &device
->meta_state
;
1093 struct radv_shader_module cs
= { .nir
= NULL
};
1096 cs
.nir
= build_clear_htile_mask_shader();
1098 VkDescriptorSetLayoutCreateInfo ds_layout_info
= {
1099 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
,
1100 .flags
= VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
,
1102 .pBindings
= (VkDescriptorSetLayoutBinding
[]) {
1105 .descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
,
1106 .descriptorCount
= 1,
1107 .stageFlags
= VK_SHADER_STAGE_COMPUTE_BIT
,
1108 .pImmutableSamplers
= NULL
1113 result
= radv_CreateDescriptorSetLayout(radv_device_to_handle(device
),
1114 &ds_layout_info
, &state
->alloc
,
1115 &state
->clear_htile_mask_ds_layout
);
1116 if (result
!= VK_SUCCESS
)
1119 VkPipelineLayoutCreateInfo p_layout_info
= {
1120 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
1121 .setLayoutCount
= 1,
1122 .pSetLayouts
= &state
->clear_htile_mask_ds_layout
,
1123 .pushConstantRangeCount
= 1,
1124 .pPushConstantRanges
= &(VkPushConstantRange
){
1125 VK_SHADER_STAGE_COMPUTE_BIT
, 0, 8,
1129 result
= radv_CreatePipelineLayout(radv_device_to_handle(device
),
1130 &p_layout_info
, &state
->alloc
,
1131 &state
->clear_htile_mask_p_layout
);
1132 if (result
!= VK_SUCCESS
)
1135 VkPipelineShaderStageCreateInfo shader_stage
= {
1136 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
1137 .stage
= VK_SHADER_STAGE_COMPUTE_BIT
,
1138 .module
= radv_shader_module_to_handle(&cs
),
1140 .pSpecializationInfo
= NULL
,
1143 VkComputePipelineCreateInfo pipeline_info
= {
1144 .sType
= VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO
,
1145 .stage
= shader_stage
,
1147 .layout
= state
->clear_htile_mask_p_layout
,
1150 result
= radv_CreateComputePipelines(radv_device_to_handle(device
),
1151 radv_pipeline_cache_to_handle(&state
->cache
),
1152 1, &pipeline_info
, NULL
,
1153 &state
->clear_htile_mask_pipeline
);
1155 ralloc_free(cs
.nir
);
1158 ralloc_free(cs
.nir
);
1163 radv_device_init_meta_clear_state(struct radv_device
*device
, bool on_demand
)
1166 struct radv_meta_state
*state
= &device
->meta_state
;
1168 VkPipelineLayoutCreateInfo pl_color_create_info
= {
1169 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
1170 .setLayoutCount
= 0,
1171 .pushConstantRangeCount
= 1,
1172 .pPushConstantRanges
= &(VkPushConstantRange
){VK_SHADER_STAGE_FRAGMENT_BIT
, 0, 16},
1175 res
= radv_CreatePipelineLayout(radv_device_to_handle(device
),
1176 &pl_color_create_info
,
1177 &device
->meta_state
.alloc
,
1178 &device
->meta_state
.clear_color_p_layout
);
1179 if (res
!= VK_SUCCESS
)
1182 VkPipelineLayoutCreateInfo pl_depth_create_info
= {
1183 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
1184 .setLayoutCount
= 0,
1185 .pushConstantRangeCount
= 1,
1186 .pPushConstantRanges
= &(VkPushConstantRange
){VK_SHADER_STAGE_VERTEX_BIT
, 0, 4},
1189 res
= radv_CreatePipelineLayout(radv_device_to_handle(device
),
1190 &pl_depth_create_info
,
1191 &device
->meta_state
.alloc
,
1192 &device
->meta_state
.clear_depth_p_layout
);
1193 if (res
!= VK_SUCCESS
)
1196 res
= init_meta_clear_htile_mask_state(device
);
1197 if (res
!= VK_SUCCESS
)
1203 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->clear
); ++i
) {
1204 uint32_t samples
= 1 << i
;
1205 for (uint32_t j
= 0; j
< NUM_META_FS_KEYS
; ++j
) {
1206 VkFormat format
= radv_fs_key_format_exemplars
[j
];
1207 unsigned fs_key
= radv_format_meta_fs_key(format
);
1208 assert(!state
->clear
[i
].color_pipelines
[fs_key
]);
1210 res
= create_color_renderpass(device
, format
, samples
,
1211 &state
->clear
[i
].render_pass
[fs_key
]);
1212 if (res
!= VK_SUCCESS
)
1215 res
= create_color_pipeline(device
, samples
, 0, &state
->clear
[i
].color_pipelines
[fs_key
],
1216 state
->clear
[i
].render_pass
[fs_key
]);
1217 if (res
!= VK_SUCCESS
)
1222 res
= create_depthstencil_renderpass(device
,
1224 &state
->clear
[i
].depthstencil_rp
);
1225 if (res
!= VK_SUCCESS
)
1228 for (uint32_t j
= 0; j
< NUM_DEPTH_CLEAR_PIPELINES
; j
++) {
1229 res
= create_depthstencil_pipeline(device
,
1230 VK_IMAGE_ASPECT_DEPTH_BIT
,
1233 &state
->clear
[i
].depth_only_pipeline
[j
],
1234 state
->clear
[i
].depthstencil_rp
);
1235 if (res
!= VK_SUCCESS
)
1238 res
= create_depthstencil_pipeline(device
,
1239 VK_IMAGE_ASPECT_STENCIL_BIT
,
1242 &state
->clear
[i
].stencil_only_pipeline
[j
],
1243 state
->clear
[i
].depthstencil_rp
);
1244 if (res
!= VK_SUCCESS
)
1247 res
= create_depthstencil_pipeline(device
,
1248 VK_IMAGE_ASPECT_DEPTH_BIT
|
1249 VK_IMAGE_ASPECT_STENCIL_BIT
,
1252 &state
->clear
[i
].depthstencil_pipeline
[j
],
1253 state
->clear
[i
].depthstencil_rp
);
1254 if (res
!= VK_SUCCESS
)
1261 radv_device_finish_meta_clear_state(device
);
1266 radv_get_cmask_fast_clear_value(const struct radv_image
*image
)
1268 uint32_t value
= 0; /* Default value when no DCC. */
1270 /* The fast-clear value is different for images that have both DCC and
1273 if (radv_image_has_dcc(image
)) {
1274 /* DCC fast clear with MSAA should clear CMASK to 0xC. */
1275 return image
->info
.samples
> 1 ? 0xcccccccc : 0xffffffff;
1282 radv_clear_cmask(struct radv_cmd_buffer
*cmd_buffer
,
1283 struct radv_image
*image
, uint32_t value
)
1285 return radv_fill_buffer(cmd_buffer
, image
->bo
,
1286 image
->offset
+ image
->cmask
.offset
,
1287 image
->cmask
.size
, value
);
1292 radv_clear_fmask(struct radv_cmd_buffer
*cmd_buffer
,
1293 struct radv_image
*image
, uint32_t value
)
1295 return radv_fill_buffer(cmd_buffer
, image
->bo
,
1296 image
->offset
+ image
->fmask
.offset
,
1297 image
->fmask
.size
, value
);
1301 radv_clear_dcc(struct radv_cmd_buffer
*cmd_buffer
,
1302 struct radv_image
*image
, uint32_t value
)
1304 /* Mark the image as being compressed. */
1305 radv_update_dcc_metadata(cmd_buffer
, image
, true);
1307 return radv_fill_buffer(cmd_buffer
, image
->bo
,
1308 image
->offset
+ image
->dcc_offset
,
1309 image
->surface
.dcc_size
, value
);
1312 static void vi_get_fast_clear_parameters(VkFormat format
,
1313 const VkClearColorValue
*clear_value
,
1314 uint32_t* reset_value
,
1315 bool *can_avoid_fast_clear_elim
)
1317 bool values
[4] = {};
1319 bool main_value
= false;
1320 bool extra_value
= false;
1322 *can_avoid_fast_clear_elim
= false;
1324 *reset_value
= 0x20202020U
;
1326 const struct vk_format_description
*desc
= vk_format_description(format
);
1327 if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
||
1328 format
== VK_FORMAT_R5G6B5_UNORM_PACK16
||
1329 format
== VK_FORMAT_B5G6R5_UNORM_PACK16
)
1331 else if (desc
->layout
== VK_FORMAT_LAYOUT_PLAIN
) {
1332 if (radv_translate_colorswap(format
, false) <= 1)
1333 extra_channel
= desc
->nr_channels
- 1;
1339 for (i
= 0; i
< 4; i
++) {
1340 int index
= desc
->swizzle
[i
] - VK_SWIZZLE_X
;
1341 if (desc
->swizzle
[i
] < VK_SWIZZLE_X
||
1342 desc
->swizzle
[i
] > VK_SWIZZLE_W
)
1345 if (desc
->channel
[i
].pure_integer
&&
1346 desc
->channel
[i
].type
== VK_FORMAT_TYPE_SIGNED
) {
1347 /* Use the maximum value for clamping the clear color. */
1348 int max
= u_bit_consecutive(0, desc
->channel
[i
].size
- 1);
1350 values
[i
] = clear_value
->int32
[i
] != 0;
1351 if (clear_value
->int32
[i
] != 0 && MIN2(clear_value
->int32
[i
], max
) != max
)
1353 } else if (desc
->channel
[i
].pure_integer
&&
1354 desc
->channel
[i
].type
== VK_FORMAT_TYPE_UNSIGNED
) {
1355 /* Use the maximum value for clamping the clear color. */
1356 unsigned max
= u_bit_consecutive(0, desc
->channel
[i
].size
);
1358 values
[i
] = clear_value
->uint32
[i
] != 0U;
1359 if (clear_value
->uint32
[i
] != 0U && MIN2(clear_value
->uint32
[i
], max
) != max
)
1362 values
[i
] = clear_value
->float32
[i
] != 0.0F
;
1363 if (clear_value
->float32
[i
] != 0.0F
&& clear_value
->float32
[i
] != 1.0F
)
1367 if (index
== extra_channel
)
1368 extra_value
= values
[i
];
1370 main_value
= values
[i
];
1373 for (int i
= 0; i
< 4; ++i
)
1374 if (values
[i
] != main_value
&&
1375 desc
->swizzle
[i
] - VK_SWIZZLE_X
!= extra_channel
&&
1376 desc
->swizzle
[i
] >= VK_SWIZZLE_X
&&
1377 desc
->swizzle
[i
] <= VK_SWIZZLE_W
)
1380 *can_avoid_fast_clear_elim
= true;
1382 *reset_value
|= 0x80808080U
;
1385 *reset_value
|= 0x40404040U
;
1390 radv_can_fast_clear_color(struct radv_cmd_buffer
*cmd_buffer
,
1391 const struct radv_image_view
*iview
,
1392 VkImageLayout image_layout
,
1393 const VkClearRect
*clear_rect
,
1394 VkClearColorValue clear_value
,
1397 uint32_t clear_color
[2];
1399 if (!radv_image_view_can_fast_clear(cmd_buffer
->device
, iview
))
1402 if (!radv_layout_can_fast_clear(iview
->image
, image_layout
, radv_image_queue_family_mask(iview
->image
, cmd_buffer
->queue_family_index
, cmd_buffer
->queue_family_index
)))
1405 if (clear_rect
->rect
.offset
.x
|| clear_rect
->rect
.offset
.y
||
1406 clear_rect
->rect
.extent
.width
!= iview
->image
->info
.width
||
1407 clear_rect
->rect
.extent
.height
!= iview
->image
->info
.height
)
1410 if (view_mask
&& (iview
->image
->info
.array_size
>= 32 ||
1411 (1u << iview
->image
->info
.array_size
) - 1u != view_mask
))
1413 if (!view_mask
&& clear_rect
->baseArrayLayer
!= 0)
1415 if (!view_mask
&& clear_rect
->layerCount
!= iview
->image
->info
.array_size
)
1419 if (!radv_format_pack_clear_color(iview
->vk_format
,
1420 clear_color
, &clear_value
))
1423 if (radv_image_has_dcc(iview
->image
)) {
1424 bool can_avoid_fast_clear_elim
;
1425 uint32_t reset_value
;
1427 vi_get_fast_clear_parameters(iview
->vk_format
,
1428 &clear_value
, &reset_value
,
1429 &can_avoid_fast_clear_elim
);
1431 if (iview
->image
->info
.samples
> 1) {
1432 /* DCC fast clear with MSAA should clear CMASK. */
1433 /* FIXME: This doesn't work for now. There is a
1434 * hardware bug with fast clears and DCC for MSAA
1435 * textures. AMDVLK has a workaround but it doesn't
1436 * seem to work here. Note that we might emit useless
1437 * CB flushes but that shouldn't matter.
1439 if (!can_avoid_fast_clear_elim
)
1449 radv_fast_clear_color(struct radv_cmd_buffer
*cmd_buffer
,
1450 const struct radv_image_view
*iview
,
1451 const VkClearAttachment
*clear_att
,
1452 uint32_t subpass_att
,
1453 enum radv_cmd_flush_bits
*pre_flush
,
1454 enum radv_cmd_flush_bits
*post_flush
)
1456 VkClearColorValue clear_value
= clear_att
->clearValue
.color
;
1457 uint32_t clear_color
[2], flush_bits
= 0;
1458 uint32_t cmask_clear_value
;
1461 cmd_buffer
->state
.flush_bits
|= (RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
1462 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
) & ~ *pre_flush
;
1463 *pre_flush
|= cmd_buffer
->state
.flush_bits
;
1467 radv_format_pack_clear_color(iview
->vk_format
, clear_color
, &clear_value
);
1469 cmask_clear_value
= radv_get_cmask_fast_clear_value(iview
->image
);
1471 /* clear cmask buffer */
1472 if (radv_image_has_dcc(iview
->image
)) {
1473 uint32_t reset_value
;
1474 bool can_avoid_fast_clear_elim
;
1475 bool need_decompress_pass
= false;
1477 vi_get_fast_clear_parameters(iview
->vk_format
,
1478 &clear_value
, &reset_value
,
1479 &can_avoid_fast_clear_elim
);
1481 if (radv_image_has_cmask(iview
->image
)) {
1482 flush_bits
= radv_clear_cmask(cmd_buffer
, iview
->image
,
1485 need_decompress_pass
= true;
1488 if (!can_avoid_fast_clear_elim
)
1489 need_decompress_pass
= true;
1491 flush_bits
|= radv_clear_dcc(cmd_buffer
, iview
->image
, reset_value
);
1493 radv_update_fce_metadata(cmd_buffer
, iview
->image
,
1494 need_decompress_pass
);
1496 flush_bits
= radv_clear_cmask(cmd_buffer
, iview
->image
,
1501 *post_flush
|= flush_bits
;
1504 radv_update_color_clear_metadata(cmd_buffer
, iview
->image
, subpass_att
,
1509 * The parameters mean that same as those in vkCmdClearAttachments.
1512 emit_clear(struct radv_cmd_buffer
*cmd_buffer
,
1513 const VkClearAttachment
*clear_att
,
1514 const VkClearRect
*clear_rect
,
1515 enum radv_cmd_flush_bits
*pre_flush
,
1516 enum radv_cmd_flush_bits
*post_flush
,
1519 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
1520 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
1521 VkImageAspectFlags aspects
= clear_att
->aspectMask
;
1523 if (aspects
& VK_IMAGE_ASPECT_COLOR_BIT
) {
1524 const uint32_t subpass_att
= clear_att
->colorAttachment
;
1525 const uint32_t pass_att
= subpass
->color_attachments
[subpass_att
].attachment
;
1526 VkImageLayout image_layout
= subpass
->color_attachments
[subpass_att
].layout
;
1527 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
1528 VkClearColorValue clear_value
= clear_att
->clearValue
.color
;
1530 if (radv_can_fast_clear_color(cmd_buffer
, iview
, image_layout
,
1531 clear_rect
, clear_value
, view_mask
)) {
1532 radv_fast_clear_color(cmd_buffer
, iview
, clear_att
,
1533 subpass_att
, pre_flush
,
1536 emit_color_clear(cmd_buffer
, clear_att
, clear_rect
, view_mask
);
1539 const uint32_t pass_att
= subpass
->depth_stencil_attachment
->attachment
;
1540 VkImageLayout image_layout
= subpass
->depth_stencil_attachment
->layout
;
1541 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
1542 VkClearDepthStencilValue clear_value
= clear_att
->clearValue
.depthStencil
;
1544 assert(aspects
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
1545 VK_IMAGE_ASPECT_STENCIL_BIT
));
1547 if (radv_can_fast_clear_depth(cmd_buffer
, iview
, image_layout
,
1548 aspects
, clear_rect
, clear_value
,
1550 radv_fast_clear_depth(cmd_buffer
, iview
, clear_att
,
1551 pre_flush
, post_flush
);
1553 emit_depthstencil_clear(cmd_buffer
, clear_att
, clear_rect
,
1560 radv_attachment_needs_clear(struct radv_cmd_state
*cmd_state
, uint32_t a
)
1562 uint32_t view_mask
= cmd_state
->subpass
->view_mask
;
1563 return (a
!= VK_ATTACHMENT_UNUSED
&&
1564 cmd_state
->attachments
[a
].pending_clear_aspects
&&
1565 (!view_mask
|| (view_mask
& ~cmd_state
->attachments
[a
].cleared_views
)));
1569 radv_subpass_needs_clear(struct radv_cmd_buffer
*cmd_buffer
)
1571 struct radv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
1574 if (!cmd_state
->subpass
)
1577 for (uint32_t i
= 0; i
< cmd_state
->subpass
->color_count
; ++i
) {
1578 a
= cmd_state
->subpass
->color_attachments
[i
].attachment
;
1579 if (radv_attachment_needs_clear(cmd_state
, a
))
1583 if (!cmd_state
->subpass
->depth_stencil_attachment
)
1586 a
= cmd_state
->subpass
->depth_stencil_attachment
->attachment
;
1587 return radv_attachment_needs_clear(cmd_state
, a
);
1591 radv_subpass_clear_attachment(struct radv_cmd_buffer
*cmd_buffer
,
1592 struct radv_attachment_state
*attachment
,
1593 const VkClearAttachment
*clear_att
,
1594 enum radv_cmd_flush_bits
*pre_flush
,
1595 enum radv_cmd_flush_bits
*post_flush
)
1597 struct radv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
1598 uint32_t view_mask
= cmd_state
->subpass
->view_mask
;
1600 VkClearRect clear_rect
= {
1601 .rect
= cmd_state
->render_area
,
1602 .baseArrayLayer
= 0,
1603 .layerCount
= cmd_state
->framebuffer
->layers
,
1606 emit_clear(cmd_buffer
, clear_att
, &clear_rect
, pre_flush
, post_flush
,
1607 view_mask
& ~attachment
->cleared_views
);
1609 attachment
->cleared_views
|= view_mask
;
1611 attachment
->pending_clear_aspects
= 0;
1615 * Emit any pending attachment clears for the current subpass.
1617 * @see radv_attachment_state::pending_clear_aspects
1620 radv_cmd_buffer_clear_subpass(struct radv_cmd_buffer
*cmd_buffer
)
1622 struct radv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
1623 struct radv_meta_saved_state saved_state
;
1624 enum radv_cmd_flush_bits pre_flush
= 0;
1625 enum radv_cmd_flush_bits post_flush
= 0;
1627 if (!radv_subpass_needs_clear(cmd_buffer
))
1630 radv_meta_save(&saved_state
, cmd_buffer
,
1631 RADV_META_SAVE_GRAPHICS_PIPELINE
|
1632 RADV_META_SAVE_CONSTANTS
);
1634 for (uint32_t i
= 0; i
< cmd_state
->subpass
->color_count
; ++i
) {
1635 uint32_t a
= cmd_state
->subpass
->color_attachments
[i
].attachment
;
1637 if (!radv_attachment_needs_clear(cmd_state
, a
))
1640 assert(cmd_state
->attachments
[a
].pending_clear_aspects
==
1641 VK_IMAGE_ASPECT_COLOR_BIT
);
1643 VkClearAttachment clear_att
= {
1644 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1645 .colorAttachment
= i
, /* Use attachment index relative to subpass */
1646 .clearValue
= cmd_state
->attachments
[a
].clear_value
,
1649 radv_subpass_clear_attachment(cmd_buffer
,
1650 &cmd_state
->attachments
[a
],
1651 &clear_att
, &pre_flush
,
1655 if (cmd_state
->subpass
->depth_stencil_attachment
) {
1656 uint32_t ds
= cmd_state
->subpass
->depth_stencil_attachment
->attachment
;
1657 if (radv_attachment_needs_clear(cmd_state
, ds
)) {
1658 VkClearAttachment clear_att
= {
1659 .aspectMask
= cmd_state
->attachments
[ds
].pending_clear_aspects
,
1660 .clearValue
= cmd_state
->attachments
[ds
].clear_value
,
1663 radv_subpass_clear_attachment(cmd_buffer
,
1664 &cmd_state
->attachments
[ds
],
1665 &clear_att
, &pre_flush
,
1670 radv_meta_restore(&saved_state
, cmd_buffer
);
1671 cmd_buffer
->state
.flush_bits
|= post_flush
;
1675 radv_clear_image_layer(struct radv_cmd_buffer
*cmd_buffer
,
1676 struct radv_image
*image
,
1677 VkImageLayout image_layout
,
1678 const VkImageSubresourceRange
*range
,
1679 VkFormat format
, int level
, int layer
,
1680 const VkClearValue
*clear_val
)
1682 VkDevice device_h
= radv_device_to_handle(cmd_buffer
->device
);
1683 struct radv_image_view iview
;
1684 uint32_t width
= radv_minify(image
->info
.width
, range
->baseMipLevel
+ level
);
1685 uint32_t height
= radv_minify(image
->info
.height
, range
->baseMipLevel
+ level
);
1687 radv_image_view_init(&iview
, cmd_buffer
->device
,
1688 &(VkImageViewCreateInfo
) {
1689 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1690 .image
= radv_image_to_handle(image
),
1691 .viewType
= radv_meta_get_view_type(image
),
1693 .subresourceRange
= {
1694 .aspectMask
= range
->aspectMask
,
1695 .baseMipLevel
= range
->baseMipLevel
+ level
,
1697 .baseArrayLayer
= range
->baseArrayLayer
+ layer
,
1703 radv_CreateFramebuffer(device_h
,
1704 &(VkFramebufferCreateInfo
) {
1705 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
1706 .attachmentCount
= 1,
1707 .pAttachments
= (VkImageView
[]) {
1708 radv_image_view_to_handle(&iview
),
1714 &cmd_buffer
->pool
->alloc
,
1717 VkAttachmentDescription att_desc
= {
1718 .format
= iview
.vk_format
,
1719 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
1720 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
1721 .stencilLoadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
1722 .stencilStoreOp
= VK_ATTACHMENT_STORE_OP_STORE
,
1723 .initialLayout
= image_layout
,
1724 .finalLayout
= image_layout
,
1727 VkSubpassDescription subpass_desc
= {
1728 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
1729 .inputAttachmentCount
= 0,
1730 .colorAttachmentCount
= 0,
1731 .pColorAttachments
= NULL
,
1732 .pResolveAttachments
= NULL
,
1733 .pDepthStencilAttachment
= NULL
,
1734 .preserveAttachmentCount
= 0,
1735 .pPreserveAttachments
= NULL
,
1738 const VkAttachmentReference att_ref
= {
1740 .layout
= image_layout
,
1743 if (range
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
1744 subpass_desc
.colorAttachmentCount
= 1;
1745 subpass_desc
.pColorAttachments
= &att_ref
;
1747 subpass_desc
.pDepthStencilAttachment
= &att_ref
;
1751 radv_CreateRenderPass(device_h
,
1752 &(VkRenderPassCreateInfo
) {
1753 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
1754 .attachmentCount
= 1,
1755 .pAttachments
= &att_desc
,
1757 .pSubpasses
= &subpass_desc
,
1759 &cmd_buffer
->pool
->alloc
,
1762 radv_CmdBeginRenderPass(radv_cmd_buffer_to_handle(cmd_buffer
),
1763 &(VkRenderPassBeginInfo
) {
1764 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
1766 .offset
= { 0, 0, },
1774 .clearValueCount
= 0,
1775 .pClearValues
= NULL
,
1777 VK_SUBPASS_CONTENTS_INLINE
);
1779 VkClearAttachment clear_att
= {
1780 .aspectMask
= range
->aspectMask
,
1781 .colorAttachment
= 0,
1782 .clearValue
= *clear_val
,
1785 VkClearRect clear_rect
= {
1788 .extent
= { width
, height
},
1790 .baseArrayLayer
= range
->baseArrayLayer
,
1791 .layerCount
= 1, /* FINISHME: clear multi-layer framebuffer */
1794 emit_clear(cmd_buffer
, &clear_att
, &clear_rect
, NULL
, NULL
, 0);
1796 radv_CmdEndRenderPass(radv_cmd_buffer_to_handle(cmd_buffer
));
1797 radv_DestroyRenderPass(device_h
, pass
,
1798 &cmd_buffer
->pool
->alloc
);
1799 radv_DestroyFramebuffer(device_h
, fb
,
1800 &cmd_buffer
->pool
->alloc
);
1804 * Return TRUE if a fast color or depth clear has been performed.
1807 radv_fast_clear_range(struct radv_cmd_buffer
*cmd_buffer
,
1808 struct radv_image
*image
,
1810 VkImageLayout image_layout
,
1811 const VkImageSubresourceRange
*range
,
1812 const VkClearValue
*clear_val
)
1814 struct radv_image_view iview
;
1816 radv_image_view_init(&iview
, cmd_buffer
->device
,
1817 &(VkImageViewCreateInfo
) {
1818 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1819 .image
= radv_image_to_handle(image
),
1820 .viewType
= radv_meta_get_view_type(image
),
1821 .format
= image
->vk_format
,
1822 .subresourceRange
= {
1823 .aspectMask
= range
->aspectMask
,
1824 .baseMipLevel
= range
->baseMipLevel
,
1825 .levelCount
= range
->levelCount
,
1826 .baseArrayLayer
= range
->baseArrayLayer
,
1827 .layerCount
= range
->layerCount
,
1831 VkClearRect clear_rect
= {
1835 radv_minify(image
->info
.width
, range
->baseMipLevel
),
1836 radv_minify(image
->info
.height
, range
->baseMipLevel
),
1839 .baseArrayLayer
= range
->baseArrayLayer
,
1840 .layerCount
= range
->layerCount
,
1843 VkClearAttachment clear_att
= {
1844 .aspectMask
= range
->aspectMask
,
1845 .colorAttachment
= 0,
1846 .clearValue
= *clear_val
,
1849 if (vk_format_is_color(format
)) {
1850 if (radv_can_fast_clear_color(cmd_buffer
, &iview
,
1851 image_layout
, &clear_rect
,
1852 clear_att
.clearValue
.color
, 0)) {
1853 radv_fast_clear_color(cmd_buffer
, &iview
, &clear_att
,
1854 clear_att
.colorAttachment
,
1859 if (radv_can_fast_clear_depth(cmd_buffer
, &iview
, image_layout
,
1860 range
->aspectMask
, &clear_rect
,
1861 clear_att
.clearValue
.depthStencil
, 0)) {
1862 radv_fast_clear_depth(cmd_buffer
, &iview
, &clear_att
,
1872 radv_cmd_clear_image(struct radv_cmd_buffer
*cmd_buffer
,
1873 struct radv_image
*image
,
1874 VkImageLayout image_layout
,
1875 const VkClearValue
*clear_value
,
1876 uint32_t range_count
,
1877 const VkImageSubresourceRange
*ranges
,
1880 VkFormat format
= image
->vk_format
;
1881 VkClearValue internal_clear_value
= *clear_value
;
1883 if (format
== VK_FORMAT_E5B9G9R9_UFLOAT_PACK32
) {
1885 format
= VK_FORMAT_R32_UINT
;
1886 value
= float3_to_rgb9e5(clear_value
->color
.float32
);
1887 internal_clear_value
.color
.uint32
[0] = value
;
1890 if (format
== VK_FORMAT_R4G4_UNORM_PACK8
) {
1892 format
= VK_FORMAT_R8_UINT
;
1893 r
= float_to_ubyte(clear_value
->color
.float32
[0]) >> 4;
1894 g
= float_to_ubyte(clear_value
->color
.float32
[1]) >> 4;
1895 internal_clear_value
.color
.uint32
[0] = (r
<< 4) | (g
& 0xf);
1898 if (format
== VK_FORMAT_R32G32B32_UINT
||
1899 format
== VK_FORMAT_R32G32B32_SINT
||
1900 format
== VK_FORMAT_R32G32B32_SFLOAT
)
1903 for (uint32_t r
= 0; r
< range_count
; r
++) {
1904 const VkImageSubresourceRange
*range
= &ranges
[r
];
1906 /* Try to perform a fast clear first, otherwise fallback to
1910 radv_fast_clear_range(cmd_buffer
, image
, format
,
1911 image_layout
, range
,
1912 &internal_clear_value
)) {
1916 for (uint32_t l
= 0; l
< radv_get_levelCount(image
, range
); ++l
) {
1917 const uint32_t layer_count
= image
->type
== VK_IMAGE_TYPE_3D
?
1918 radv_minify(image
->info
.depth
, range
->baseMipLevel
+ l
) :
1919 radv_get_layerCount(image
, range
);
1920 for (uint32_t s
= 0; s
< layer_count
; ++s
) {
1923 struct radv_meta_blit2d_surf surf
;
1924 surf
.format
= format
;
1926 surf
.level
= range
->baseMipLevel
+ l
;
1927 surf
.layer
= range
->baseArrayLayer
+ s
;
1928 surf
.aspect_mask
= range
->aspectMask
;
1929 radv_meta_clear_image_cs(cmd_buffer
, &surf
,
1930 &internal_clear_value
.color
);
1932 radv_clear_image_layer(cmd_buffer
, image
, image_layout
,
1933 range
, format
, l
, s
, &internal_clear_value
);
1940 void radv_CmdClearColorImage(
1941 VkCommandBuffer commandBuffer
,
1943 VkImageLayout imageLayout
,
1944 const VkClearColorValue
* pColor
,
1945 uint32_t rangeCount
,
1946 const VkImageSubresourceRange
* pRanges
)
1948 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1949 RADV_FROM_HANDLE(radv_image
, image
, image_h
);
1950 struct radv_meta_saved_state saved_state
;
1951 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
1954 radv_meta_save(&saved_state
, cmd_buffer
,
1955 RADV_META_SAVE_COMPUTE_PIPELINE
|
1956 RADV_META_SAVE_CONSTANTS
|
1957 RADV_META_SAVE_DESCRIPTORS
);
1959 radv_meta_save(&saved_state
, cmd_buffer
,
1960 RADV_META_SAVE_GRAPHICS_PIPELINE
|
1961 RADV_META_SAVE_CONSTANTS
);
1964 radv_cmd_clear_image(cmd_buffer
, image
, imageLayout
,
1965 (const VkClearValue
*) pColor
,
1966 rangeCount
, pRanges
, cs
);
1968 radv_meta_restore(&saved_state
, cmd_buffer
);
1971 void radv_CmdClearDepthStencilImage(
1972 VkCommandBuffer commandBuffer
,
1974 VkImageLayout imageLayout
,
1975 const VkClearDepthStencilValue
* pDepthStencil
,
1976 uint32_t rangeCount
,
1977 const VkImageSubresourceRange
* pRanges
)
1979 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1980 RADV_FROM_HANDLE(radv_image
, image
, image_h
);
1981 struct radv_meta_saved_state saved_state
;
1983 radv_meta_save(&saved_state
, cmd_buffer
,
1984 RADV_META_SAVE_GRAPHICS_PIPELINE
|
1985 RADV_META_SAVE_CONSTANTS
);
1987 radv_cmd_clear_image(cmd_buffer
, image
, imageLayout
,
1988 (const VkClearValue
*) pDepthStencil
,
1989 rangeCount
, pRanges
, false);
1991 radv_meta_restore(&saved_state
, cmd_buffer
);
1994 void radv_CmdClearAttachments(
1995 VkCommandBuffer commandBuffer
,
1996 uint32_t attachmentCount
,
1997 const VkClearAttachment
* pAttachments
,
1999 const VkClearRect
* pRects
)
2001 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
2002 struct radv_meta_saved_state saved_state
;
2003 enum radv_cmd_flush_bits pre_flush
= 0;
2004 enum radv_cmd_flush_bits post_flush
= 0;
2006 if (!cmd_buffer
->state
.subpass
)
2009 radv_meta_save(&saved_state
, cmd_buffer
,
2010 RADV_META_SAVE_GRAPHICS_PIPELINE
|
2011 RADV_META_SAVE_CONSTANTS
);
2013 /* FINISHME: We can do better than this dumb loop. It thrashes too much
2016 for (uint32_t a
= 0; a
< attachmentCount
; ++a
) {
2017 for (uint32_t r
= 0; r
< rectCount
; ++r
) {
2018 emit_clear(cmd_buffer
, &pAttachments
[a
], &pRects
[r
], &pre_flush
, &post_flush
,
2019 cmd_buffer
->state
.subpass
->view_mask
);
2023 radv_meta_restore(&saved_state
, cmd_buffer
);
2024 cmd_buffer
->state
.flush_bits
|= post_flush
;