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_meta.h"
25 #include "radv_private.h"
26 #include "nir/nir_builder.h"
28 #include "util/format_rgb9e5.h"
29 #include "vk_format.h"
33 DEPTH_CLEAR_FAST_EXPCLEAR
,
34 DEPTH_CLEAR_FAST_NO_EXPCLEAR
38 build_color_shaders(struct nir_shader
**out_vs
,
39 struct nir_shader
**out_fs
,
45 nir_builder_init_simple_shader(&vs_b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
46 nir_builder_init_simple_shader(&fs_b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
48 vs_b
.shader
->info
.name
= ralloc_strdup(vs_b
.shader
, "meta_clear_color_vs");
49 fs_b
.shader
->info
.name
= ralloc_strdup(fs_b
.shader
, "meta_clear_color_fs");
51 const struct glsl_type
*position_type
= glsl_vec4_type();
52 const struct glsl_type
*color_type
= glsl_vec4_type();
54 nir_variable
*vs_out_pos
=
55 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, position_type
,
57 vs_out_pos
->data
.location
= VARYING_SLOT_POS
;
59 nir_intrinsic_instr
*in_color_load
= nir_intrinsic_instr_create(fs_b
.shader
, nir_intrinsic_load_push_constant
);
60 nir_intrinsic_set_base(in_color_load
, 0);
61 nir_intrinsic_set_range(in_color_load
, 16);
62 in_color_load
->src
[0] = nir_src_for_ssa(nir_imm_int(&fs_b
, 0));
63 in_color_load
->num_components
= 4;
64 nir_ssa_dest_init(&in_color_load
->instr
, &in_color_load
->dest
, 4, 32, "clear color");
65 nir_builder_instr_insert(&fs_b
, &in_color_load
->instr
);
67 nir_variable
*fs_out_color
=
68 nir_variable_create(fs_b
.shader
, nir_var_shader_out
, color_type
,
70 fs_out_color
->data
.location
= FRAG_RESULT_DATA0
+ frag_output
;
72 nir_store_var(&fs_b
, fs_out_color
, &in_color_load
->dest
.ssa
, 0xf);
74 nir_ssa_def
*outvec
= radv_meta_gen_rect_vertices(&vs_b
);
75 nir_store_var(&vs_b
, vs_out_pos
, outvec
, 0xf);
77 const struct glsl_type
*layer_type
= glsl_int_type();
78 nir_variable
*vs_out_layer
=
79 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, layer_type
,
81 vs_out_layer
->data
.location
= VARYING_SLOT_LAYER
;
82 vs_out_layer
->data
.interpolation
= INTERP_MODE_FLAT
;
83 nir_ssa_def
*inst_id
= nir_load_system_value(&vs_b
, nir_intrinsic_load_instance_id
, 0);
85 nir_store_var(&vs_b
, vs_out_layer
, inst_id
, 0x1);
87 *out_vs
= vs_b
.shader
;
88 *out_fs
= fs_b
.shader
;
92 create_pipeline(struct radv_device
*device
,
93 struct radv_render_pass
*render_pass
,
95 struct nir_shader
*vs_nir
,
96 struct nir_shader
*fs_nir
,
97 const VkPipelineVertexInputStateCreateInfo
*vi_state
,
98 const VkPipelineDepthStencilStateCreateInfo
*ds_state
,
99 const VkPipelineColorBlendStateCreateInfo
*cb_state
,
100 const VkPipelineLayout layout
,
101 const struct radv_graphics_pipeline_create_info
*extra
,
102 const VkAllocationCallbacks
*alloc
,
103 struct radv_pipeline
**pipeline
)
105 VkDevice device_h
= radv_device_to_handle(device
);
108 struct radv_shader_module vs_m
= { .nir
= vs_nir
};
109 struct radv_shader_module fs_m
= { .nir
= fs_nir
};
111 VkPipeline pipeline_h
= VK_NULL_HANDLE
;
112 result
= radv_graphics_pipeline_create(device_h
,
113 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
114 &(VkGraphicsPipelineCreateInfo
) {
115 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
116 .stageCount
= fs_nir
? 2 : 1,
117 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
119 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
120 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
121 .module
= radv_shader_module_to_handle(&vs_m
),
125 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
126 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
127 .module
= radv_shader_module_to_handle(&fs_m
),
131 .pVertexInputState
= vi_state
,
132 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
133 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
134 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
135 .primitiveRestartEnable
= false,
137 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
138 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
142 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
143 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
144 .rasterizerDiscardEnable
= false,
145 .polygonMode
= VK_POLYGON_MODE_FILL
,
146 .cullMode
= VK_CULL_MODE_NONE
,
147 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
,
148 .depthBiasEnable
= false,
150 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
151 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
152 .rasterizationSamples
= samples
,
153 .sampleShadingEnable
= false,
155 .alphaToCoverageEnable
= false,
156 .alphaToOneEnable
= false,
158 .pDepthStencilState
= ds_state
,
159 .pColorBlendState
= cb_state
,
160 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
161 /* The meta clear pipeline declares all state as dynamic.
162 * As a consequence, vkCmdBindPipeline writes no dynamic state
163 * to the cmd buffer. Therefore, at the end of the meta clear,
164 * we need only restore dynamic state was vkCmdSet.
166 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
167 .dynamicStateCount
= 8,
168 .pDynamicStates
= (VkDynamicState
[]) {
169 /* Everything except stencil write mask */
170 VK_DYNAMIC_STATE_VIEWPORT
,
171 VK_DYNAMIC_STATE_SCISSOR
,
172 VK_DYNAMIC_STATE_LINE_WIDTH
,
173 VK_DYNAMIC_STATE_DEPTH_BIAS
,
174 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
175 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
176 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
177 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
182 .renderPass
= radv_render_pass_to_handle(render_pass
),
192 *pipeline
= radv_pipeline_from_handle(pipeline_h
);
198 create_color_renderpass(struct radv_device
*device
,
203 return radv_CreateRenderPass(radv_device_to_handle(device
),
204 &(VkRenderPassCreateInfo
) {
205 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
206 .attachmentCount
= 1,
207 .pAttachments
= &(VkAttachmentDescription
) {
210 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
211 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
212 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
213 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
216 .pSubpasses
= &(VkSubpassDescription
) {
217 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
218 .inputAttachmentCount
= 0,
219 .colorAttachmentCount
= 1,
220 .pColorAttachments
= &(VkAttachmentReference
) {
222 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
224 .pResolveAttachments
= NULL
,
225 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
226 .attachment
= VK_ATTACHMENT_UNUSED
,
227 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
229 .preserveAttachmentCount
= 1,
230 .pPreserveAttachments
= (uint32_t[]) { 0 },
232 .dependencyCount
= 0,
233 }, &device
->meta_state
.alloc
, pass
);
237 create_color_pipeline(struct radv_device
*device
,
239 uint32_t frag_output
,
240 struct radv_pipeline
**pipeline
,
243 struct nir_shader
*vs_nir
;
244 struct nir_shader
*fs_nir
;
246 build_color_shaders(&vs_nir
, &fs_nir
, frag_output
);
248 const VkPipelineVertexInputStateCreateInfo vi_state
= {
249 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
250 .vertexBindingDescriptionCount
= 0,
251 .vertexAttributeDescriptionCount
= 0,
254 const VkPipelineDepthStencilStateCreateInfo ds_state
= {
255 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
256 .depthTestEnable
= false,
257 .depthWriteEnable
= false,
258 .depthBoundsTestEnable
= false,
259 .stencilTestEnable
= false,
262 VkPipelineColorBlendAttachmentState blend_attachment_state
[MAX_RTS
] = { 0 };
263 blend_attachment_state
[frag_output
] = (VkPipelineColorBlendAttachmentState
) {
264 .blendEnable
= false,
265 .colorWriteMask
= VK_COLOR_COMPONENT_A_BIT
|
266 VK_COLOR_COMPONENT_R_BIT
|
267 VK_COLOR_COMPONENT_G_BIT
|
268 VK_COLOR_COMPONENT_B_BIT
,
271 const VkPipelineColorBlendStateCreateInfo cb_state
= {
272 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
273 .logicOpEnable
= false,
274 .attachmentCount
= MAX_RTS
,
275 .pAttachments
= blend_attachment_state
279 struct radv_graphics_pipeline_create_info extra
= {
280 .use_rectlist
= true,
282 result
= create_pipeline(device
, radv_render_pass_from_handle(pass
),
283 samples
, vs_nir
, fs_nir
, &vi_state
, &ds_state
, &cb_state
,
284 device
->meta_state
.clear_color_p_layout
,
285 &extra
, &device
->meta_state
.alloc
, pipeline
);
291 destroy_pipeline(struct radv_device
*device
, struct radv_pipeline
*pipeline
)
296 radv_DestroyPipeline(radv_device_to_handle(device
),
297 radv_pipeline_to_handle(pipeline
),
298 &device
->meta_state
.alloc
);
303 destroy_render_pass(struct radv_device
*device
, VkRenderPass renderpass
)
305 radv_DestroyRenderPass(radv_device_to_handle(device
), renderpass
,
306 &device
->meta_state
.alloc
);
310 radv_device_finish_meta_clear_state(struct radv_device
*device
)
312 struct radv_meta_state
*state
= &device
->meta_state
;
314 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->clear
); ++i
) {
315 for (uint32_t j
= 0; j
< ARRAY_SIZE(state
->clear
[i
].color_pipelines
); ++j
) {
316 destroy_pipeline(device
, state
->clear
[i
].color_pipelines
[j
]);
317 destroy_render_pass(device
, state
->clear
[i
].render_pass
[j
]);
320 for (uint32_t j
= 0; j
< NUM_DEPTH_CLEAR_PIPELINES
; j
++) {
321 destroy_pipeline(device
, state
->clear
[i
].depth_only_pipeline
[j
]);
322 destroy_pipeline(device
, state
->clear
[i
].stencil_only_pipeline
[j
]);
323 destroy_pipeline(device
, state
->clear
[i
].depthstencil_pipeline
[j
]);
325 destroy_render_pass(device
, state
->clear
[i
].depthstencil_rp
);
327 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
328 state
->clear_color_p_layout
,
330 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
331 state
->clear_depth_p_layout
,
336 emit_color_clear(struct radv_cmd_buffer
*cmd_buffer
,
337 const VkClearAttachment
*clear_att
,
338 const VkClearRect
*clear_rect
)
340 struct radv_device
*device
= cmd_buffer
->device
;
341 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
342 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
343 const uint32_t subpass_att
= clear_att
->colorAttachment
;
344 const uint32_t pass_att
= subpass
->color_attachments
[subpass_att
].attachment
;
345 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
346 const uint32_t samples
= iview
->image
->info
.samples
;
347 const uint32_t samples_log2
= ffs(samples
) - 1;
348 unsigned fs_key
= radv_format_meta_fs_key(iview
->vk_format
);
349 struct radv_pipeline
*pipeline
;
350 VkClearColorValue clear_value
= clear_att
->clearValue
.color
;
351 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
352 VkPipeline pipeline_h
;
355 radv_finishme("color clears incomplete");
358 pipeline
= device
->meta_state
.clear
[samples_log2
].color_pipelines
[fs_key
];
359 pipeline_h
= radv_pipeline_to_handle(pipeline
);
362 radv_finishme("color clears incomplete");
365 assert(samples_log2
< ARRAY_SIZE(device
->meta_state
.clear
));
367 assert(clear_att
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
368 assert(clear_att
->colorAttachment
< subpass
->color_count
);
370 radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer
),
371 device
->meta_state
.clear_color_p_layout
,
372 VK_SHADER_STAGE_FRAGMENT_BIT
, 0, 16,
375 struct radv_subpass clear_subpass
= {
377 .color_attachments
= (VkAttachmentReference
[]) {
378 subpass
->color_attachments
[clear_att
->colorAttachment
]
380 .depth_stencil_attachment
= (VkAttachmentReference
) { VK_ATTACHMENT_UNUSED
, VK_IMAGE_LAYOUT_UNDEFINED
}
383 radv_cmd_buffer_set_subpass(cmd_buffer
, &clear_subpass
, false);
385 if (cmd_buffer
->state
.pipeline
!= pipeline
) {
386 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
390 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
391 .x
= clear_rect
->rect
.offset
.x
,
392 .y
= clear_rect
->rect
.offset
.y
,
393 .width
= clear_rect
->rect
.extent
.width
,
394 .height
= clear_rect
->rect
.extent
.height
,
399 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &clear_rect
->rect
);
401 radv_CmdDraw(cmd_buffer_h
, 3, clear_rect
->layerCount
, 0, 0);
403 radv_cmd_buffer_set_subpass(cmd_buffer
, subpass
, false);
408 build_depthstencil_shader(struct nir_shader
**out_vs
, struct nir_shader
**out_fs
)
410 nir_builder vs_b
, fs_b
;
412 nir_builder_init_simple_shader(&vs_b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
413 nir_builder_init_simple_shader(&fs_b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
415 vs_b
.shader
->info
.name
= ralloc_strdup(vs_b
.shader
, "meta_clear_depthstencil_vs");
416 fs_b
.shader
->info
.name
= ralloc_strdup(fs_b
.shader
, "meta_clear_depthstencil_fs");
417 const struct glsl_type
*position_out_type
= glsl_vec4_type();
419 nir_variable
*vs_out_pos
=
420 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, position_out_type
,
422 vs_out_pos
->data
.location
= VARYING_SLOT_POS
;
424 nir_intrinsic_instr
*in_color_load
= nir_intrinsic_instr_create(vs_b
.shader
, nir_intrinsic_load_push_constant
);
425 nir_intrinsic_set_base(in_color_load
, 0);
426 nir_intrinsic_set_range(in_color_load
, 4);
427 in_color_load
->src
[0] = nir_src_for_ssa(nir_imm_int(&vs_b
, 0));
428 in_color_load
->num_components
= 1;
429 nir_ssa_dest_init(&in_color_load
->instr
, &in_color_load
->dest
, 1, 32, "depth value");
430 nir_builder_instr_insert(&vs_b
, &in_color_load
->instr
);
432 nir_ssa_def
*outvec
= radv_meta_gen_rect_vertices_comp2(&vs_b
, &in_color_load
->dest
.ssa
);
433 nir_store_var(&vs_b
, vs_out_pos
, outvec
, 0xf);
435 const struct glsl_type
*layer_type
= glsl_int_type();
436 nir_variable
*vs_out_layer
=
437 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, layer_type
,
439 vs_out_layer
->data
.location
= VARYING_SLOT_LAYER
;
440 vs_out_layer
->data
.interpolation
= INTERP_MODE_FLAT
;
441 nir_ssa_def
*inst_id
= nir_load_system_value(&vs_b
, nir_intrinsic_load_instance_id
, 0);
442 nir_store_var(&vs_b
, vs_out_layer
, inst_id
, 0x1);
444 *out_vs
= vs_b
.shader
;
445 *out_fs
= fs_b
.shader
;
449 create_depthstencil_renderpass(struct radv_device
*device
,
451 VkRenderPass
*render_pass
)
453 return radv_CreateRenderPass(radv_device_to_handle(device
),
454 &(VkRenderPassCreateInfo
) {
455 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
456 .attachmentCount
= 1,
457 .pAttachments
= &(VkAttachmentDescription
) {
458 .format
= VK_FORMAT_D32_SFLOAT_S8_UINT
,
460 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
461 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
462 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
463 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
466 .pSubpasses
= &(VkSubpassDescription
) {
467 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
468 .inputAttachmentCount
= 0,
469 .colorAttachmentCount
= 0,
470 .pColorAttachments
= NULL
,
471 .pResolveAttachments
= NULL
,
472 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
474 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
476 .preserveAttachmentCount
= 1,
477 .pPreserveAttachments
= (uint32_t[]) { 0 },
479 .dependencyCount
= 0,
480 }, &device
->meta_state
.alloc
, render_pass
);
484 create_depthstencil_pipeline(struct radv_device
*device
,
485 VkImageAspectFlags aspects
,
488 struct radv_pipeline
**pipeline
,
489 VkRenderPass render_pass
)
491 struct nir_shader
*vs_nir
, *fs_nir
;
493 build_depthstencil_shader(&vs_nir
, &fs_nir
);
495 const VkPipelineVertexInputStateCreateInfo vi_state
= {
496 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
497 .vertexBindingDescriptionCount
= 0,
498 .vertexAttributeDescriptionCount
= 0,
501 const VkPipelineDepthStencilStateCreateInfo ds_state
= {
502 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
503 .depthTestEnable
= (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
),
504 .depthCompareOp
= VK_COMPARE_OP_ALWAYS
,
505 .depthWriteEnable
= (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
),
506 .depthBoundsTestEnable
= false,
507 .stencilTestEnable
= (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
),
509 .passOp
= VK_STENCIL_OP_REPLACE
,
510 .compareOp
= VK_COMPARE_OP_ALWAYS
,
511 .writeMask
= UINT32_MAX
,
512 .reference
= 0, /* dynamic */
514 .back
= { 0 /* dont care */ },
517 const VkPipelineColorBlendStateCreateInfo cb_state
= {
518 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
519 .logicOpEnable
= false,
520 .attachmentCount
= 0,
521 .pAttachments
= NULL
,
524 struct radv_graphics_pipeline_create_info extra
= {
525 .use_rectlist
= true,
528 if (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
529 extra
.db_depth_clear
= index
== DEPTH_CLEAR_SLOW
? false : true;
530 extra
.db_depth_disable_expclear
= index
== DEPTH_CLEAR_FAST_NO_EXPCLEAR
? true : false;
532 if (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
533 extra
.db_stencil_clear
= index
== DEPTH_CLEAR_SLOW
? false : true;
534 extra
.db_stencil_disable_expclear
= index
== DEPTH_CLEAR_FAST_NO_EXPCLEAR
? true : false;
536 result
= create_pipeline(device
, radv_render_pass_from_handle(render_pass
),
537 samples
, vs_nir
, fs_nir
, &vi_state
, &ds_state
, &cb_state
,
538 device
->meta_state
.clear_depth_p_layout
,
539 &extra
, &device
->meta_state
.alloc
, pipeline
);
543 static bool depth_view_can_fast_clear(struct radv_cmd_buffer
*cmd_buffer
,
544 const struct radv_image_view
*iview
,
545 VkImageLayout layout
,
546 const VkClearRect
*clear_rect
)
548 uint32_t queue_mask
= radv_image_queue_family_mask(iview
->image
,
549 cmd_buffer
->queue_family_index
,
550 cmd_buffer
->queue_family_index
);
551 if (clear_rect
->rect
.offset
.x
|| clear_rect
->rect
.offset
.y
||
552 clear_rect
->rect
.extent
.width
!= iview
->extent
.width
||
553 clear_rect
->rect
.extent
.height
!= iview
->extent
.height
)
555 if (iview
->image
->surface
.htile_size
&&
556 iview
->base_mip
== 0 &&
557 iview
->base_layer
== 0 &&
558 radv_layout_is_htile_compressed(iview
->image
, layout
, queue_mask
) &&
559 !radv_image_extent_compare(iview
->image
, &iview
->extent
))
564 static struct radv_pipeline
*
565 pick_depthstencil_pipeline(struct radv_cmd_buffer
*cmd_buffer
,
566 struct radv_meta_state
*meta_state
,
567 const struct radv_image_view
*iview
,
569 VkImageAspectFlags aspects
,
570 VkImageLayout layout
,
571 const VkClearRect
*clear_rect
,
572 VkClearDepthStencilValue clear_value
)
574 bool fast
= depth_view_can_fast_clear(cmd_buffer
, iview
, layout
, clear_rect
);
575 int index
= DEPTH_CLEAR_SLOW
;
578 /* we don't know the previous clear values, so we always have
579 * the NO_EXPCLEAR path */
580 index
= DEPTH_CLEAR_FAST_NO_EXPCLEAR
;
584 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
585 return meta_state
->clear
[samples_log2
].depthstencil_pipeline
[index
];
586 case VK_IMAGE_ASPECT_DEPTH_BIT
:
587 return meta_state
->clear
[samples_log2
].depth_only_pipeline
[index
];
588 case VK_IMAGE_ASPECT_STENCIL_BIT
:
589 return meta_state
->clear
[samples_log2
].stencil_only_pipeline
[index
];
591 unreachable("expected depth or stencil aspect");
595 emit_depthstencil_clear(struct radv_cmd_buffer
*cmd_buffer
,
596 const VkClearAttachment
*clear_att
,
597 const VkClearRect
*clear_rect
)
599 struct radv_device
*device
= cmd_buffer
->device
;
600 struct radv_meta_state
*meta_state
= &device
->meta_state
;
601 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
602 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
603 const uint32_t pass_att
= subpass
->depth_stencil_attachment
.attachment
;
604 VkClearDepthStencilValue clear_value
= clear_att
->clearValue
.depthStencil
;
605 VkImageAspectFlags aspects
= clear_att
->aspectMask
;
606 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
607 const uint32_t samples
= iview
->image
->info
.samples
;
608 const uint32_t samples_log2
= ffs(samples
) - 1;
609 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
611 assert(aspects
== VK_IMAGE_ASPECT_DEPTH_BIT
||
612 aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
||
613 aspects
== (VK_IMAGE_ASPECT_DEPTH_BIT
|
614 VK_IMAGE_ASPECT_STENCIL_BIT
));
615 assert(pass_att
!= VK_ATTACHMENT_UNUSED
);
617 if (!(aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
))
618 clear_value
.depth
= 1.0f
;
620 radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer
),
621 device
->meta_state
.clear_depth_p_layout
,
622 VK_SHADER_STAGE_VERTEX_BIT
, 0, 4,
625 if (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
626 radv_CmdSetStencilReference(cmd_buffer_h
, VK_STENCIL_FACE_FRONT_BIT
,
627 clear_value
.stencil
);
630 struct radv_pipeline
*pipeline
= pick_depthstencil_pipeline(cmd_buffer
,
635 subpass
->depth_stencil_attachment
.layout
,
638 if (cmd_buffer
->state
.pipeline
!= pipeline
) {
639 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
640 radv_pipeline_to_handle(pipeline
));
643 if (depth_view_can_fast_clear(cmd_buffer
, iview
, subpass
->depth_stencil_attachment
.layout
, clear_rect
))
644 radv_set_depth_clear_regs(cmd_buffer
, iview
->image
, clear_value
, aspects
);
646 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
647 .x
= clear_rect
->rect
.offset
.x
,
648 .y
= clear_rect
->rect
.offset
.y
,
649 .width
= clear_rect
->rect
.extent
.width
,
650 .height
= clear_rect
->rect
.extent
.height
,
655 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &clear_rect
->rect
);
657 radv_CmdDraw(cmd_buffer_h
, 3, clear_rect
->layerCount
, 0, 0);
661 emit_fast_htile_clear(struct radv_cmd_buffer
*cmd_buffer
,
662 const VkClearAttachment
*clear_att
,
663 const VkClearRect
*clear_rect
,
664 enum radv_cmd_flush_bits
*pre_flush
,
665 enum radv_cmd_flush_bits
*post_flush
)
667 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
668 const uint32_t pass_att
= subpass
->depth_stencil_attachment
.attachment
;
669 VkImageLayout image_layout
= subpass
->depth_stencil_attachment
.layout
;
670 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
671 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
672 VkClearDepthStencilValue clear_value
= clear_att
->clearValue
.depthStencil
;
673 VkImageAspectFlags aspects
= clear_att
->aspectMask
;
676 if (!iview
->image
->surface
.htile_size
)
679 if (cmd_buffer
->device
->debug_flags
& RADV_DEBUG_NO_FAST_CLEARS
)
682 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
)))
685 /* don't fast clear 3D */
686 if (iview
->image
->type
== VK_IMAGE_TYPE_3D
)
689 /* all layers are bound */
690 if (iview
->base_layer
> 0)
692 if (iview
->image
->info
.array_size
!= iview
->layer_count
)
695 if (iview
->image
->info
.levels
> 1)
698 if (!radv_image_extent_compare(iview
->image
, &iview
->extent
))
701 if (clear_rect
->rect
.offset
.x
|| clear_rect
->rect
.offset
.y
||
702 clear_rect
->rect
.extent
.width
!= iview
->image
->info
.width
||
703 clear_rect
->rect
.extent
.height
!= iview
->image
->info
.height
)
706 if (clear_rect
->baseArrayLayer
!= 0)
708 if (clear_rect
->layerCount
!= iview
->image
->info
.array_size
)
711 if ((clear_value
.depth
!= 0.0 && clear_value
.depth
!= 1.0) || !(aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
))
714 if (vk_format_aspects(iview
->image
->vk_format
) & VK_IMAGE_ASPECT_STENCIL_BIT
) {
715 if (clear_value
.stencil
!= 0 || !(aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
))
717 clear_word
= clear_value
.depth
? 0xfffc0000 : 0;
719 clear_word
= clear_value
.depth
? 0xfffffff0 : 0;
722 cmd_buffer
->state
.flush_bits
|= (RADV_CMD_FLAG_FLUSH_AND_INV_DB
|
723 RADV_CMD_FLAG_FLUSH_AND_INV_DB_META
) & ~ *pre_flush
;
724 *pre_flush
|= cmd_buffer
->state
.flush_bits
;
726 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_DB
|
727 RADV_CMD_FLAG_FLUSH_AND_INV_DB_META
;
729 radv_fill_buffer(cmd_buffer
, iview
->image
->bo
,
730 iview
->image
->offset
+ iview
->image
->htile_offset
,
731 iview
->image
->surface
.htile_size
, clear_word
);
734 radv_set_depth_clear_regs(cmd_buffer
, iview
->image
, clear_value
, aspects
);
736 *post_flush
|= RADV_CMD_FLAG_CS_PARTIAL_FLUSH
|
737 RADV_CMD_FLAG_INV_VMEM_L1
|
738 RADV_CMD_FLAG_WRITEBACK_GLOBAL_L2
;
740 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_CS_PARTIAL_FLUSH
|
741 RADV_CMD_FLAG_INV_VMEM_L1
|
742 RADV_CMD_FLAG_WRITEBACK_GLOBAL_L2
;
748 static VkFormat pipeline_formats
[] = {
749 VK_FORMAT_R8G8B8A8_UNORM
,
750 VK_FORMAT_R8G8B8A8_UINT
,
751 VK_FORMAT_R8G8B8A8_SINT
,
752 VK_FORMAT_R16G16B16A16_UNORM
,
753 VK_FORMAT_R16G16B16A16_SNORM
,
754 VK_FORMAT_R16G16B16A16_UINT
,
755 VK_FORMAT_R16G16B16A16_SINT
,
756 VK_FORMAT_R32_SFLOAT
,
757 VK_FORMAT_R32G32_SFLOAT
,
758 VK_FORMAT_R32G32B32A32_SFLOAT
762 radv_device_init_meta_clear_state(struct radv_device
*device
)
765 struct radv_meta_state
*state
= &device
->meta_state
;
767 memset(&device
->meta_state
.clear
, 0, sizeof(device
->meta_state
.clear
));
769 VkPipelineLayoutCreateInfo pl_color_create_info
= {
770 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
772 .pushConstantRangeCount
= 1,
773 .pPushConstantRanges
= &(VkPushConstantRange
){VK_SHADER_STAGE_FRAGMENT_BIT
, 0, 16},
776 res
= radv_CreatePipelineLayout(radv_device_to_handle(device
),
777 &pl_color_create_info
,
778 &device
->meta_state
.alloc
,
779 &device
->meta_state
.clear_color_p_layout
);
780 if (res
!= VK_SUCCESS
)
783 VkPipelineLayoutCreateInfo pl_depth_create_info
= {
784 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
786 .pushConstantRangeCount
= 1,
787 .pPushConstantRanges
= &(VkPushConstantRange
){VK_SHADER_STAGE_VERTEX_BIT
, 0, 4},
790 res
= radv_CreatePipelineLayout(radv_device_to_handle(device
),
791 &pl_depth_create_info
,
792 &device
->meta_state
.alloc
,
793 &device
->meta_state
.clear_depth_p_layout
);
794 if (res
!= VK_SUCCESS
)
797 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->clear
); ++i
) {
798 uint32_t samples
= 1 << i
;
799 for (uint32_t j
= 0; j
< ARRAY_SIZE(pipeline_formats
); ++j
) {
800 VkFormat format
= pipeline_formats
[j
];
801 unsigned fs_key
= radv_format_meta_fs_key(format
);
802 assert(!state
->clear
[i
].color_pipelines
[fs_key
]);
804 res
= create_color_renderpass(device
, format
, samples
,
805 &state
->clear
[i
].render_pass
[fs_key
]);
806 if (res
!= VK_SUCCESS
)
809 res
= create_color_pipeline(device
, samples
, 0, &state
->clear
[i
].color_pipelines
[fs_key
],
810 state
->clear
[i
].render_pass
[fs_key
]);
811 if (res
!= VK_SUCCESS
)
816 res
= create_depthstencil_renderpass(device
,
818 &state
->clear
[i
].depthstencil_rp
);
819 if (res
!= VK_SUCCESS
)
822 for (uint32_t j
= 0; j
< NUM_DEPTH_CLEAR_PIPELINES
; j
++) {
823 res
= create_depthstencil_pipeline(device
,
824 VK_IMAGE_ASPECT_DEPTH_BIT
,
827 &state
->clear
[i
].depth_only_pipeline
[j
],
828 state
->clear
[i
].depthstencil_rp
);
829 if (res
!= VK_SUCCESS
)
832 res
= create_depthstencil_pipeline(device
,
833 VK_IMAGE_ASPECT_STENCIL_BIT
,
836 &state
->clear
[i
].stencil_only_pipeline
[j
],
837 state
->clear
[i
].depthstencil_rp
);
838 if (res
!= VK_SUCCESS
)
841 res
= create_depthstencil_pipeline(device
,
842 VK_IMAGE_ASPECT_DEPTH_BIT
|
843 VK_IMAGE_ASPECT_STENCIL_BIT
,
846 &state
->clear
[i
].depthstencil_pipeline
[j
],
847 state
->clear
[i
].depthstencil_rp
);
848 if (res
!= VK_SUCCESS
)
855 radv_device_finish_meta_clear_state(device
);
859 static void vi_get_fast_clear_parameters(VkFormat format
,
860 const VkClearColorValue
*clear_value
,
861 uint32_t* reset_value
,
862 bool *can_avoid_fast_clear_elim
)
866 bool main_value
= false;
867 bool extra_value
= false;
869 *can_avoid_fast_clear_elim
= false;
871 *reset_value
= 0x20202020U
;
873 const struct vk_format_description
*desc
= vk_format_description(format
);
874 if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
||
875 format
== VK_FORMAT_R5G6B5_UNORM_PACK16
||
876 format
== VK_FORMAT_B5G6R5_UNORM_PACK16
)
878 else if (desc
->layout
== VK_FORMAT_LAYOUT_PLAIN
) {
879 if (radv_translate_colorswap(format
, false) <= 1)
880 extra_channel
= desc
->nr_channels
- 1;
886 for (i
= 0; i
< 4; i
++) {
887 int index
= desc
->swizzle
[i
] - VK_SWIZZLE_X
;
888 if (desc
->swizzle
[i
] < VK_SWIZZLE_X
||
889 desc
->swizzle
[i
] > VK_SWIZZLE_W
)
892 if (desc
->channel
[i
].pure_integer
&&
893 desc
->channel
[i
].type
== VK_FORMAT_TYPE_SIGNED
) {
894 /* Use the maximum value for clamping the clear color. */
895 int max
= u_bit_consecutive(0, desc
->channel
[i
].size
- 1);
897 values
[i
] = clear_value
->int32
[i
] != 0;
898 if (clear_value
->int32
[i
] != 0 && MIN2(clear_value
->int32
[i
], max
) != max
)
900 } else if (desc
->channel
[i
].pure_integer
&&
901 desc
->channel
[i
].type
== VK_FORMAT_TYPE_UNSIGNED
) {
902 /* Use the maximum value for clamping the clear color. */
903 unsigned max
= u_bit_consecutive(0, desc
->channel
[i
].size
);
905 values
[i
] = clear_value
->uint32
[i
] != 0U;
906 if (clear_value
->uint32
[i
] != 0U && MIN2(clear_value
->uint32
[i
], max
) != max
)
909 values
[i
] = clear_value
->float32
[i
] != 0.0F
;
910 if (clear_value
->float32
[i
] != 0.0F
&& clear_value
->float32
[i
] != 1.0F
)
914 if (index
== extra_channel
)
915 extra_value
= values
[i
];
917 main_value
= values
[i
];
920 for (int i
= 0; i
< 4; ++i
)
921 if (values
[i
] != main_value
&&
922 desc
->swizzle
[i
] - VK_SWIZZLE_X
!= extra_channel
&&
923 desc
->swizzle
[i
] >= VK_SWIZZLE_X
&&
924 desc
->swizzle
[i
] <= VK_SWIZZLE_W
)
927 *can_avoid_fast_clear_elim
= true;
929 *reset_value
|= 0x80808080U
;
932 *reset_value
|= 0x40404040U
;
937 emit_fast_color_clear(struct radv_cmd_buffer
*cmd_buffer
,
938 const VkClearAttachment
*clear_att
,
939 const VkClearRect
*clear_rect
,
940 enum radv_cmd_flush_bits
*pre_flush
,
941 enum radv_cmd_flush_bits
*post_flush
)
943 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
944 const uint32_t subpass_att
= clear_att
->colorAttachment
;
945 const uint32_t pass_att
= subpass
->color_attachments
[subpass_att
].attachment
;
946 VkImageLayout image_layout
= subpass
->color_attachments
[subpass_att
].layout
;
947 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
948 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
949 VkClearColorValue clear_value
= clear_att
->clearValue
.color
;
950 uint32_t clear_color
[2];
953 if (!iview
->image
->cmask
.size
&& !iview
->image
->surface
.dcc_size
)
956 if (cmd_buffer
->device
->debug_flags
& RADV_DEBUG_NO_FAST_CLEARS
)
959 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
)))
962 /* don't fast clear 3D */
963 if (iview
->image
->type
== VK_IMAGE_TYPE_3D
)
966 /* all layers are bound */
967 if (iview
->base_layer
> 0)
969 if (iview
->image
->info
.array_size
!= iview
->layer_count
)
972 if (iview
->image
->info
.levels
> 1)
975 if (iview
->image
->surface
.u
.legacy
.level
[0].mode
< RADEON_SURF_MODE_1D
)
977 if (!radv_image_extent_compare(iview
->image
, &iview
->extent
))
980 if (clear_rect
->rect
.offset
.x
|| clear_rect
->rect
.offset
.y
||
981 clear_rect
->rect
.extent
.width
!= iview
->image
->info
.width
||
982 clear_rect
->rect
.extent
.height
!= iview
->image
->info
.height
)
985 if (clear_rect
->baseArrayLayer
!= 0)
987 if (clear_rect
->layerCount
!= iview
->image
->info
.array_size
)
990 /* RB+ doesn't work with CMASK fast clear on Stoney. */
991 if (!iview
->image
->surface
.dcc_size
&&
992 cmd_buffer
->device
->physical_device
->rad_info
.family
== CHIP_STONEY
)
996 ret
= radv_format_pack_clear_color(iview
->image
->vk_format
,
997 clear_color
, &clear_value
);
1002 cmd_buffer
->state
.flush_bits
|= (RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
1003 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
) & ~ *pre_flush
;
1004 *pre_flush
|= cmd_buffer
->state
.flush_bits
;
1006 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
1007 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
1008 /* clear cmask buffer */
1009 if (iview
->image
->surface
.dcc_size
) {
1010 uint32_t reset_value
;
1011 bool can_avoid_fast_clear_elim
;
1012 vi_get_fast_clear_parameters(iview
->image
->vk_format
,
1013 &clear_value
, &reset_value
,
1014 &can_avoid_fast_clear_elim
);
1016 radv_fill_buffer(cmd_buffer
, iview
->image
->bo
,
1017 iview
->image
->offset
+ iview
->image
->dcc_offset
,
1018 iview
->image
->surface
.dcc_size
, reset_value
);
1019 radv_set_dcc_need_cmask_elim_pred(cmd_buffer
, iview
->image
,
1020 !can_avoid_fast_clear_elim
);
1023 if (iview
->image
->surface
.bpe
> 8) {
1024 /* 128 bit formats not supported */
1027 radv_fill_buffer(cmd_buffer
, iview
->image
->bo
,
1028 iview
->image
->offset
+ iview
->image
->cmask
.offset
,
1029 iview
->image
->cmask
.size
, 0);
1033 *post_flush
|= RADV_CMD_FLAG_CS_PARTIAL_FLUSH
|
1034 RADV_CMD_FLAG_INV_VMEM_L1
|
1035 RADV_CMD_FLAG_WRITEBACK_GLOBAL_L2
;
1037 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_CS_PARTIAL_FLUSH
|
1038 RADV_CMD_FLAG_INV_VMEM_L1
|
1039 RADV_CMD_FLAG_WRITEBACK_GLOBAL_L2
;
1041 radv_set_color_clear_regs(cmd_buffer
, iview
->image
, subpass_att
, clear_color
);
1049 * The parameters mean that same as those in vkCmdClearAttachments.
1052 emit_clear(struct radv_cmd_buffer
*cmd_buffer
,
1053 const VkClearAttachment
*clear_att
,
1054 const VkClearRect
*clear_rect
,
1055 enum radv_cmd_flush_bits
*pre_flush
,
1056 enum radv_cmd_flush_bits
*post_flush
)
1058 if (clear_att
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
1060 if (!emit_fast_color_clear(cmd_buffer
, clear_att
, clear_rect
,
1061 pre_flush
, post_flush
))
1062 emit_color_clear(cmd_buffer
, clear_att
, clear_rect
);
1064 assert(clear_att
->aspectMask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
1065 VK_IMAGE_ASPECT_STENCIL_BIT
));
1066 if (!emit_fast_htile_clear(cmd_buffer
, clear_att
, clear_rect
,
1067 pre_flush
, post_flush
))
1068 emit_depthstencil_clear(cmd_buffer
, clear_att
, clear_rect
);
1073 subpass_needs_clear(const struct radv_cmd_buffer
*cmd_buffer
)
1075 const struct radv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
1078 if (!cmd_state
->subpass
)
1080 ds
= cmd_state
->subpass
->depth_stencil_attachment
.attachment
;
1081 for (uint32_t i
= 0; i
< cmd_state
->subpass
->color_count
; ++i
) {
1082 uint32_t a
= cmd_state
->subpass
->color_attachments
[i
].attachment
;
1083 if (a
!= VK_ATTACHMENT_UNUSED
&&
1084 cmd_state
->attachments
[a
].pending_clear_aspects
) {
1089 if (ds
!= VK_ATTACHMENT_UNUSED
&&
1090 cmd_state
->attachments
[ds
].pending_clear_aspects
) {
1098 * Emit any pending attachment clears for the current subpass.
1100 * @see radv_attachment_state::pending_clear_aspects
1103 radv_cmd_buffer_clear_subpass(struct radv_cmd_buffer
*cmd_buffer
)
1105 struct radv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
1106 struct radv_meta_saved_state saved_state
;
1107 enum radv_cmd_flush_bits pre_flush
= 0;
1108 enum radv_cmd_flush_bits post_flush
= 0;
1110 if (!subpass_needs_clear(cmd_buffer
))
1113 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state
, cmd_buffer
);
1115 VkClearRect clear_rect
= {
1116 .rect
= cmd_state
->render_area
,
1117 .baseArrayLayer
= 0,
1118 .layerCount
= cmd_state
->framebuffer
->layers
,
1121 for (uint32_t i
= 0; i
< cmd_state
->subpass
->color_count
; ++i
) {
1122 uint32_t a
= cmd_state
->subpass
->color_attachments
[i
].attachment
;
1124 if (a
== VK_ATTACHMENT_UNUSED
||
1125 !cmd_state
->attachments
[a
].pending_clear_aspects
)
1128 assert(cmd_state
->attachments
[a
].pending_clear_aspects
==
1129 VK_IMAGE_ASPECT_COLOR_BIT
);
1131 VkClearAttachment clear_att
= {
1132 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1133 .colorAttachment
= i
, /* Use attachment index relative to subpass */
1134 .clearValue
= cmd_state
->attachments
[a
].clear_value
,
1137 emit_clear(cmd_buffer
, &clear_att
, &clear_rect
, &pre_flush
, &post_flush
);
1138 cmd_state
->attachments
[a
].pending_clear_aspects
= 0;
1141 uint32_t ds
= cmd_state
->subpass
->depth_stencil_attachment
.attachment
;
1143 if (ds
!= VK_ATTACHMENT_UNUSED
) {
1145 if (cmd_state
->attachments
[ds
].pending_clear_aspects
) {
1147 VkClearAttachment clear_att
= {
1148 .aspectMask
= cmd_state
->attachments
[ds
].pending_clear_aspects
,
1149 .clearValue
= cmd_state
->attachments
[ds
].clear_value
,
1152 emit_clear(cmd_buffer
, &clear_att
, &clear_rect
,
1153 &pre_flush
, &post_flush
);
1154 cmd_state
->attachments
[ds
].pending_clear_aspects
= 0;
1158 radv_meta_restore(&saved_state
, cmd_buffer
);
1159 cmd_buffer
->state
.flush_bits
|= post_flush
;
1163 radv_clear_image_layer(struct radv_cmd_buffer
*cmd_buffer
,
1164 struct radv_image
*image
,
1165 VkImageLayout image_layout
,
1166 const VkImageSubresourceRange
*range
,
1167 VkFormat format
, int level
, int layer
,
1168 const VkClearValue
*clear_val
)
1170 VkDevice device_h
= radv_device_to_handle(cmd_buffer
->device
);
1171 struct radv_image_view iview
;
1172 radv_image_view_init(&iview
, cmd_buffer
->device
,
1173 &(VkImageViewCreateInfo
) {
1174 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1175 .image
= radv_image_to_handle(image
),
1176 .viewType
= radv_meta_get_view_type(image
),
1178 .subresourceRange
= {
1179 .aspectMask
= range
->aspectMask
,
1180 .baseMipLevel
= range
->baseMipLevel
+ level
,
1182 .baseArrayLayer
= range
->baseArrayLayer
+ layer
,
1188 radv_CreateFramebuffer(device_h
,
1189 &(VkFramebufferCreateInfo
) {
1190 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
1191 .attachmentCount
= 1,
1192 .pAttachments
= (VkImageView
[]) {
1193 radv_image_view_to_handle(&iview
),
1195 .width
= iview
.extent
.width
,
1196 .height
= iview
.extent
.height
,
1199 &cmd_buffer
->pool
->alloc
,
1202 VkAttachmentDescription att_desc
= {
1203 .format
= iview
.vk_format
,
1204 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
1205 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
1206 .stencilLoadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
1207 .stencilStoreOp
= VK_ATTACHMENT_STORE_OP_STORE
,
1208 .initialLayout
= image_layout
,
1209 .finalLayout
= image_layout
,
1212 VkSubpassDescription subpass_desc
= {
1213 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
1214 .inputAttachmentCount
= 0,
1215 .colorAttachmentCount
= 0,
1216 .pColorAttachments
= NULL
,
1217 .pResolveAttachments
= NULL
,
1218 .pDepthStencilAttachment
= NULL
,
1219 .preserveAttachmentCount
= 0,
1220 .pPreserveAttachments
= NULL
,
1223 const VkAttachmentReference att_ref
= {
1225 .layout
= image_layout
,
1228 if (range
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
1229 subpass_desc
.colorAttachmentCount
= 1;
1230 subpass_desc
.pColorAttachments
= &att_ref
;
1232 subpass_desc
.pDepthStencilAttachment
= &att_ref
;
1236 radv_CreateRenderPass(device_h
,
1237 &(VkRenderPassCreateInfo
) {
1238 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
1239 .attachmentCount
= 1,
1240 .pAttachments
= &att_desc
,
1242 .pSubpasses
= &subpass_desc
,
1244 &cmd_buffer
->pool
->alloc
,
1247 radv_CmdBeginRenderPass(radv_cmd_buffer_to_handle(cmd_buffer
),
1248 &(VkRenderPassBeginInfo
) {
1249 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
1251 .offset
= { 0, 0, },
1253 .width
= iview
.extent
.width
,
1254 .height
= iview
.extent
.height
,
1259 .clearValueCount
= 0,
1260 .pClearValues
= NULL
,
1262 VK_SUBPASS_CONTENTS_INLINE
);
1264 VkClearAttachment clear_att
= {
1265 .aspectMask
= range
->aspectMask
,
1266 .colorAttachment
= 0,
1267 .clearValue
= *clear_val
,
1270 VkClearRect clear_rect
= {
1273 .extent
= { iview
.extent
.width
, iview
.extent
.height
},
1275 .baseArrayLayer
= range
->baseArrayLayer
,
1276 .layerCount
= 1, /* FINISHME: clear multi-layer framebuffer */
1279 emit_clear(cmd_buffer
, &clear_att
, &clear_rect
, NULL
, NULL
);
1281 radv_CmdEndRenderPass(radv_cmd_buffer_to_handle(cmd_buffer
));
1282 radv_DestroyRenderPass(device_h
, pass
,
1283 &cmd_buffer
->pool
->alloc
);
1284 radv_DestroyFramebuffer(device_h
, fb
,
1285 &cmd_buffer
->pool
->alloc
);
1288 radv_cmd_clear_image(struct radv_cmd_buffer
*cmd_buffer
,
1289 struct radv_image
*image
,
1290 VkImageLayout image_layout
,
1291 const VkClearValue
*clear_value
,
1292 uint32_t range_count
,
1293 const VkImageSubresourceRange
*ranges
,
1296 VkFormat format
= image
->vk_format
;
1297 VkClearValue internal_clear_value
= *clear_value
;
1299 if (format
== VK_FORMAT_E5B9G9R9_UFLOAT_PACK32
) {
1301 format
= VK_FORMAT_R32_UINT
;
1302 value
= float3_to_rgb9e5(clear_value
->color
.float32
);
1303 internal_clear_value
.color
.uint32
[0] = value
;
1306 if (format
== VK_FORMAT_R4G4_UNORM_PACK8
) {
1308 format
= VK_FORMAT_R8_UINT
;
1309 r
= float_to_ubyte(clear_value
->color
.float32
[0]) >> 4;
1310 g
= float_to_ubyte(clear_value
->color
.float32
[1]) >> 4;
1311 internal_clear_value
.color
.uint32
[0] = (r
<< 4) | (g
& 0xf);
1314 for (uint32_t r
= 0; r
< range_count
; r
++) {
1315 const VkImageSubresourceRange
*range
= &ranges
[r
];
1316 for (uint32_t l
= 0; l
< radv_get_levelCount(image
, range
); ++l
) {
1317 const uint32_t layer_count
= image
->type
== VK_IMAGE_TYPE_3D
?
1318 radv_minify(image
->info
.depth
, range
->baseMipLevel
+ l
) :
1319 radv_get_layerCount(image
, range
);
1320 for (uint32_t s
= 0; s
< layer_count
; ++s
) {
1323 struct radv_meta_blit2d_surf surf
;
1324 surf
.format
= format
;
1326 surf
.level
= range
->baseMipLevel
+ l
;
1327 surf
.layer
= range
->baseArrayLayer
+ s
;
1328 surf
.aspect_mask
= range
->aspectMask
;
1329 radv_meta_clear_image_cs(cmd_buffer
, &surf
,
1330 &internal_clear_value
.color
);
1332 radv_clear_image_layer(cmd_buffer
, image
, image_layout
,
1333 range
, format
, l
, s
, &internal_clear_value
);
1340 union meta_saved_state
{
1341 struct radv_meta_saved_state gfx
;
1342 struct radv_meta_saved_compute_state compute
;
1345 void radv_CmdClearColorImage(
1346 VkCommandBuffer commandBuffer
,
1348 VkImageLayout imageLayout
,
1349 const VkClearColorValue
* pColor
,
1350 uint32_t rangeCount
,
1351 const VkImageSubresourceRange
* pRanges
)
1353 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1354 RADV_FROM_HANDLE(radv_image
, image
, image_h
);
1355 union meta_saved_state saved_state
;
1356 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
1359 radv_meta_begin_cleari(cmd_buffer
, &saved_state
.compute
);
1361 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state
.gfx
, cmd_buffer
);
1363 radv_cmd_clear_image(cmd_buffer
, image
, imageLayout
,
1364 (const VkClearValue
*) pColor
,
1365 rangeCount
, pRanges
, cs
);
1368 radv_meta_end_cleari(cmd_buffer
, &saved_state
.compute
);
1370 radv_meta_restore(&saved_state
.gfx
, cmd_buffer
);
1373 void radv_CmdClearDepthStencilImage(
1374 VkCommandBuffer commandBuffer
,
1376 VkImageLayout imageLayout
,
1377 const VkClearDepthStencilValue
* pDepthStencil
,
1378 uint32_t rangeCount
,
1379 const VkImageSubresourceRange
* pRanges
)
1381 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1382 RADV_FROM_HANDLE(radv_image
, image
, image_h
);
1383 struct radv_meta_saved_state saved_state
;
1385 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state
, cmd_buffer
);
1387 radv_cmd_clear_image(cmd_buffer
, image
, imageLayout
,
1388 (const VkClearValue
*) pDepthStencil
,
1389 rangeCount
, pRanges
, false);
1391 radv_meta_restore(&saved_state
, cmd_buffer
);
1394 void radv_CmdClearAttachments(
1395 VkCommandBuffer commandBuffer
,
1396 uint32_t attachmentCount
,
1397 const VkClearAttachment
* pAttachments
,
1399 const VkClearRect
* pRects
)
1401 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1402 struct radv_meta_saved_state saved_state
;
1403 enum radv_cmd_flush_bits pre_flush
= 0;
1404 enum radv_cmd_flush_bits post_flush
= 0;
1406 if (!cmd_buffer
->state
.subpass
)
1409 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state
, cmd_buffer
);
1411 /* FINISHME: We can do better than this dumb loop. It thrashes too much
1414 for (uint32_t a
= 0; a
< attachmentCount
; ++a
) {
1415 for (uint32_t r
= 0; r
< rectCount
; ++r
) {
1416 emit_clear(cmd_buffer
, &pAttachments
[a
], &pRects
[r
], &pre_flush
, &post_flush
);
1420 radv_meta_restore(&saved_state
, cmd_buffer
);
1421 cmd_buffer
->state
.flush_bits
|= post_flush
;