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_system_value(&vs_b
, nir_intrinsic_load_instance_id
, 0);
85 nir_ssa_def
*base_instance
= nir_load_system_value(&vs_b
, nir_intrinsic_load_base_instance
, 0);
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 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 VkPipeline
*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 radv_device_finish_meta_clear_state(struct radv_device
*device
)
293 struct radv_meta_state
*state
= &device
->meta_state
;
295 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->clear
); ++i
) {
296 for (uint32_t j
= 0; j
< ARRAY_SIZE(state
->clear
[i
].color_pipelines
); ++j
) {
297 radv_DestroyPipeline(radv_device_to_handle(device
),
298 state
->clear
[i
].color_pipelines
[j
],
300 radv_DestroyRenderPass(radv_device_to_handle(device
),
301 state
->clear
[i
].render_pass
[j
],
305 for (uint32_t j
= 0; j
< NUM_DEPTH_CLEAR_PIPELINES
; j
++) {
306 radv_DestroyPipeline(radv_device_to_handle(device
),
307 state
->clear
[i
].depth_only_pipeline
[j
],
309 radv_DestroyPipeline(radv_device_to_handle(device
),
310 state
->clear
[i
].stencil_only_pipeline
[j
],
312 radv_DestroyPipeline(radv_device_to_handle(device
),
313 state
->clear
[i
].depthstencil_pipeline
[j
],
316 radv_DestroyRenderPass(radv_device_to_handle(device
),
317 state
->clear
[i
].depthstencil_rp
,
320 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
321 state
->clear_color_p_layout
,
323 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
324 state
->clear_depth_p_layout
,
329 emit_color_clear(struct radv_cmd_buffer
*cmd_buffer
,
330 const VkClearAttachment
*clear_att
,
331 const VkClearRect
*clear_rect
,
334 struct radv_device
*device
= cmd_buffer
->device
;
335 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
336 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
337 const uint32_t subpass_att
= clear_att
->colorAttachment
;
338 const uint32_t pass_att
= subpass
->color_attachments
[subpass_att
].attachment
;
339 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
340 const uint32_t samples
= iview
->image
->info
.samples
;
341 const uint32_t samples_log2
= ffs(samples
) - 1;
342 unsigned fs_key
= radv_format_meta_fs_key(iview
->vk_format
);
343 VkClearColorValue clear_value
= clear_att
->clearValue
.color
;
344 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
348 radv_finishme("color clears incomplete");
352 pipeline
= device
->meta_state
.clear
[samples_log2
].color_pipelines
[fs_key
];
354 radv_finishme("color clears incomplete");
357 assert(samples_log2
< ARRAY_SIZE(device
->meta_state
.clear
));
359 assert(clear_att
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
360 assert(clear_att
->colorAttachment
< subpass
->color_count
);
362 radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer
),
363 device
->meta_state
.clear_color_p_layout
,
364 VK_SHADER_STAGE_FRAGMENT_BIT
, 0, 16,
367 struct radv_subpass clear_subpass
= {
369 .color_attachments
= (VkAttachmentReference
[]) {
370 subpass
->color_attachments
[clear_att
->colorAttachment
]
372 .depth_stencil_attachment
= (VkAttachmentReference
) { VK_ATTACHMENT_UNUSED
, VK_IMAGE_LAYOUT_UNDEFINED
}
375 radv_cmd_buffer_set_subpass(cmd_buffer
, &clear_subpass
, false);
377 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
380 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
381 .x
= clear_rect
->rect
.offset
.x
,
382 .y
= clear_rect
->rect
.offset
.y
,
383 .width
= clear_rect
->rect
.extent
.width
,
384 .height
= clear_rect
->rect
.extent
.height
,
389 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &clear_rect
->rect
);
393 for_each_bit(i
, view_mask
)
394 radv_CmdDraw(cmd_buffer_h
, 3, 1, 0, i
);
396 radv_CmdDraw(cmd_buffer_h
, 3, clear_rect
->layerCount
, 0, clear_rect
->baseArrayLayer
);
399 radv_cmd_buffer_set_subpass(cmd_buffer
, subpass
, false);
404 build_depthstencil_shader(struct nir_shader
**out_vs
, struct nir_shader
**out_fs
)
406 nir_builder vs_b
, fs_b
;
408 nir_builder_init_simple_shader(&vs_b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
409 nir_builder_init_simple_shader(&fs_b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
411 vs_b
.shader
->info
.name
= ralloc_strdup(vs_b
.shader
, "meta_clear_depthstencil_vs");
412 fs_b
.shader
->info
.name
= ralloc_strdup(fs_b
.shader
, "meta_clear_depthstencil_fs");
413 const struct glsl_type
*position_out_type
= glsl_vec4_type();
415 nir_variable
*vs_out_pos
=
416 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, position_out_type
,
418 vs_out_pos
->data
.location
= VARYING_SLOT_POS
;
420 nir_intrinsic_instr
*in_color_load
= nir_intrinsic_instr_create(vs_b
.shader
, nir_intrinsic_load_push_constant
);
421 nir_intrinsic_set_base(in_color_load
, 0);
422 nir_intrinsic_set_range(in_color_load
, 4);
423 in_color_load
->src
[0] = nir_src_for_ssa(nir_imm_int(&vs_b
, 0));
424 in_color_load
->num_components
= 1;
425 nir_ssa_dest_init(&in_color_load
->instr
, &in_color_load
->dest
, 1, 32, "depth value");
426 nir_builder_instr_insert(&vs_b
, &in_color_load
->instr
);
428 nir_ssa_def
*outvec
= radv_meta_gen_rect_vertices_comp2(&vs_b
, &in_color_load
->dest
.ssa
);
429 nir_store_var(&vs_b
, vs_out_pos
, outvec
, 0xf);
431 const struct glsl_type
*layer_type
= glsl_int_type();
432 nir_variable
*vs_out_layer
=
433 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, layer_type
,
435 vs_out_layer
->data
.location
= VARYING_SLOT_LAYER
;
436 vs_out_layer
->data
.interpolation
= INTERP_MODE_FLAT
;
437 nir_ssa_def
*inst_id
= nir_load_system_value(&vs_b
, nir_intrinsic_load_instance_id
, 0);
438 nir_ssa_def
*base_instance
= nir_load_system_value(&vs_b
, nir_intrinsic_load_base_instance
, 0);
440 nir_ssa_def
*layer_id
= nir_iadd(&vs_b
, inst_id
, base_instance
);
441 nir_store_var(&vs_b
, vs_out_layer
, layer_id
, 0x1);
443 *out_vs
= vs_b
.shader
;
444 *out_fs
= fs_b
.shader
;
448 create_depthstencil_renderpass(struct radv_device
*device
,
450 VkRenderPass
*render_pass
)
452 return radv_CreateRenderPass(radv_device_to_handle(device
),
453 &(VkRenderPassCreateInfo
) {
454 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
455 .attachmentCount
= 1,
456 .pAttachments
= &(VkAttachmentDescription
) {
457 .format
= VK_FORMAT_D32_SFLOAT_S8_UINT
,
459 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
460 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
461 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
462 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
465 .pSubpasses
= &(VkSubpassDescription
) {
466 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
467 .inputAttachmentCount
= 0,
468 .colorAttachmentCount
= 0,
469 .pColorAttachments
= NULL
,
470 .pResolveAttachments
= NULL
,
471 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
473 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
475 .preserveAttachmentCount
= 1,
476 .pPreserveAttachments
= (uint32_t[]) { 0 },
478 .dependencyCount
= 0,
479 }, &device
->meta_state
.alloc
, render_pass
);
483 create_depthstencil_pipeline(struct radv_device
*device
,
484 VkImageAspectFlags aspects
,
487 VkPipeline
*pipeline
,
488 VkRenderPass render_pass
)
490 struct nir_shader
*vs_nir
, *fs_nir
;
492 build_depthstencil_shader(&vs_nir
, &fs_nir
);
494 const VkPipelineVertexInputStateCreateInfo vi_state
= {
495 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
496 .vertexBindingDescriptionCount
= 0,
497 .vertexAttributeDescriptionCount
= 0,
500 const VkPipelineDepthStencilStateCreateInfo ds_state
= {
501 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
502 .depthTestEnable
= (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
),
503 .depthCompareOp
= VK_COMPARE_OP_ALWAYS
,
504 .depthWriteEnable
= (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
),
505 .depthBoundsTestEnable
= false,
506 .stencilTestEnable
= (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
),
508 .passOp
= VK_STENCIL_OP_REPLACE
,
509 .compareOp
= VK_COMPARE_OP_ALWAYS
,
510 .writeMask
= UINT32_MAX
,
511 .reference
= 0, /* dynamic */
513 .back
= { 0 /* dont care */ },
516 const VkPipelineColorBlendStateCreateInfo cb_state
= {
517 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
518 .logicOpEnable
= false,
519 .attachmentCount
= 0,
520 .pAttachments
= NULL
,
523 struct radv_graphics_pipeline_create_info extra
= {
524 .use_rectlist
= true,
527 if (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
528 extra
.db_depth_clear
= index
== DEPTH_CLEAR_SLOW
? false : true;
529 extra
.db_depth_disable_expclear
= index
== DEPTH_CLEAR_FAST_NO_EXPCLEAR
? true : false;
531 if (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
532 extra
.db_stencil_clear
= index
== DEPTH_CLEAR_SLOW
? false : true;
533 extra
.db_stencil_disable_expclear
= index
== DEPTH_CLEAR_FAST_NO_EXPCLEAR
? true : false;
535 result
= create_pipeline(device
, radv_render_pass_from_handle(render_pass
),
536 samples
, vs_nir
, fs_nir
, &vi_state
, &ds_state
, &cb_state
,
537 device
->meta_state
.clear_depth_p_layout
,
538 &extra
, &device
->meta_state
.alloc
, pipeline
);
542 static bool depth_view_can_fast_clear(struct radv_cmd_buffer
*cmd_buffer
,
543 const struct radv_image_view
*iview
,
544 VkImageAspectFlags aspects
,
545 VkImageLayout layout
,
546 const VkClearRect
*clear_rect
,
547 VkClearDepthStencilValue clear_value
)
549 uint32_t queue_mask
= radv_image_queue_family_mask(iview
->image
,
550 cmd_buffer
->queue_family_index
,
551 cmd_buffer
->queue_family_index
);
552 if (clear_rect
->rect
.offset
.x
|| clear_rect
->rect
.offset
.y
||
553 clear_rect
->rect
.extent
.width
!= iview
->extent
.width
||
554 clear_rect
->rect
.extent
.height
!= iview
->extent
.height
)
556 if (iview
->image
->tc_compatible_htile
&&
557 (((aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) && clear_value
.depth
!= 0.0 &&
558 clear_value
.depth
!= 1.0) ||
559 ((aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) && clear_value
.stencil
!= 0)))
561 if (iview
->image
->surface
.htile_size
&&
562 iview
->base_mip
== 0 &&
563 iview
->base_layer
== 0 &&
564 radv_layout_is_htile_compressed(iview
->image
, layout
, queue_mask
) &&
565 !radv_image_extent_compare(iview
->image
, &iview
->extent
))
571 pick_depthstencil_pipeline(struct radv_cmd_buffer
*cmd_buffer
,
572 struct radv_meta_state
*meta_state
,
573 const struct radv_image_view
*iview
,
575 VkImageAspectFlags aspects
,
576 VkImageLayout layout
,
577 const VkClearRect
*clear_rect
,
578 VkClearDepthStencilValue clear_value
)
580 bool fast
= depth_view_can_fast_clear(cmd_buffer
, iview
, aspects
, layout
, clear_rect
, clear_value
);
581 int index
= DEPTH_CLEAR_SLOW
;
584 /* we don't know the previous clear values, so we always have
585 * the NO_EXPCLEAR path */
586 index
= DEPTH_CLEAR_FAST_NO_EXPCLEAR
;
590 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
591 return meta_state
->clear
[samples_log2
].depthstencil_pipeline
[index
];
592 case VK_IMAGE_ASPECT_DEPTH_BIT
:
593 return meta_state
->clear
[samples_log2
].depth_only_pipeline
[index
];
594 case VK_IMAGE_ASPECT_STENCIL_BIT
:
595 return meta_state
->clear
[samples_log2
].stencil_only_pipeline
[index
];
597 unreachable("expected depth or stencil aspect");
601 emit_depthstencil_clear(struct radv_cmd_buffer
*cmd_buffer
,
602 const VkClearAttachment
*clear_att
,
603 const VkClearRect
*clear_rect
)
605 struct radv_device
*device
= cmd_buffer
->device
;
606 struct radv_meta_state
*meta_state
= &device
->meta_state
;
607 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
608 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
609 const uint32_t pass_att
= subpass
->depth_stencil_attachment
.attachment
;
610 VkClearDepthStencilValue clear_value
= clear_att
->clearValue
.depthStencil
;
611 VkImageAspectFlags aspects
= clear_att
->aspectMask
;
612 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
613 const uint32_t samples
= iview
->image
->info
.samples
;
614 const uint32_t samples_log2
= ffs(samples
) - 1;
615 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
617 assert(pass_att
!= VK_ATTACHMENT_UNUSED
);
619 if (!(aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
))
620 clear_value
.depth
= 1.0f
;
622 radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer
),
623 device
->meta_state
.clear_depth_p_layout
,
624 VK_SHADER_STAGE_VERTEX_BIT
, 0, 4,
627 if (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
628 radv_CmdSetStencilReference(cmd_buffer_h
, VK_STENCIL_FACE_FRONT_BIT
,
629 clear_value
.stencil
);
632 VkPipeline pipeline
= pick_depthstencil_pipeline(cmd_buffer
,
637 subpass
->depth_stencil_attachment
.layout
,
641 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
644 if (depth_view_can_fast_clear(cmd_buffer
, iview
, aspects
,
645 subpass
->depth_stencil_attachment
.layout
,
646 clear_rect
, clear_value
))
647 radv_set_depth_clear_regs(cmd_buffer
, iview
->image
, clear_value
, aspects
);
649 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
650 .x
= clear_rect
->rect
.offset
.x
,
651 .y
= clear_rect
->rect
.offset
.y
,
652 .width
= clear_rect
->rect
.extent
.width
,
653 .height
= clear_rect
->rect
.extent
.height
,
658 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &clear_rect
->rect
);
660 radv_CmdDraw(cmd_buffer_h
, 3, clear_rect
->layerCount
, 0, clear_rect
->baseArrayLayer
);
664 emit_fast_htile_clear(struct radv_cmd_buffer
*cmd_buffer
,
665 const VkClearAttachment
*clear_att
,
666 const VkClearRect
*clear_rect
,
667 enum radv_cmd_flush_bits
*pre_flush
,
668 enum radv_cmd_flush_bits
*post_flush
)
670 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
671 const uint32_t pass_att
= subpass
->depth_stencil_attachment
.attachment
;
672 VkImageLayout image_layout
= subpass
->depth_stencil_attachment
.layout
;
673 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
674 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
675 VkClearDepthStencilValue clear_value
= clear_att
->clearValue
.depthStencil
;
676 VkImageAspectFlags aspects
= clear_att
->aspectMask
;
677 uint32_t clear_word
, flush_bits
;
679 if (!iview
->image
->surface
.htile_size
)
682 if (cmd_buffer
->device
->instance
->debug_flags
& RADV_DEBUG_NO_FAST_CLEARS
)
685 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
)))
688 /* don't fast clear 3D */
689 if (iview
->image
->type
== VK_IMAGE_TYPE_3D
)
692 /* all layers are bound */
693 if (iview
->base_layer
> 0)
695 if (iview
->image
->info
.array_size
!= iview
->layer_count
)
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 flush_bits
= radv_fill_buffer(cmd_buffer
, iview
->image
->bo
,
730 iview
->image
->offset
+ iview
->image
->htile_offset
,
731 iview
->image
->surface
.htile_size
, clear_word
);
733 radv_set_depth_clear_regs(cmd_buffer
, iview
->image
, clear_value
, aspects
);
735 *post_flush
|= flush_bits
;
737 cmd_buffer
->state
.flush_bits
|= flush_bits
;
745 static VkFormat pipeline_formats
[] = {
746 VK_FORMAT_R8G8B8A8_UNORM
,
747 VK_FORMAT_R8G8B8A8_UINT
,
748 VK_FORMAT_R8G8B8A8_SINT
,
749 VK_FORMAT_A2R10G10B10_UINT_PACK32
,
750 VK_FORMAT_A2R10G10B10_SINT_PACK32
,
751 VK_FORMAT_R16G16B16A16_UNORM
,
752 VK_FORMAT_R16G16B16A16_SNORM
,
753 VK_FORMAT_R16G16B16A16_UINT
,
754 VK_FORMAT_R16G16B16A16_SINT
,
755 VK_FORMAT_R32_SFLOAT
,
756 VK_FORMAT_R32G32_SFLOAT
,
757 VK_FORMAT_R32G32B32A32_SFLOAT
761 radv_device_init_meta_clear_state(struct radv_device
*device
)
764 struct radv_meta_state
*state
= &device
->meta_state
;
766 VkPipelineLayoutCreateInfo pl_color_create_info
= {
767 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
769 .pushConstantRangeCount
= 1,
770 .pPushConstantRanges
= &(VkPushConstantRange
){VK_SHADER_STAGE_FRAGMENT_BIT
, 0, 16},
773 res
= radv_CreatePipelineLayout(radv_device_to_handle(device
),
774 &pl_color_create_info
,
775 &device
->meta_state
.alloc
,
776 &device
->meta_state
.clear_color_p_layout
);
777 if (res
!= VK_SUCCESS
)
780 VkPipelineLayoutCreateInfo pl_depth_create_info
= {
781 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
783 .pushConstantRangeCount
= 1,
784 .pPushConstantRanges
= &(VkPushConstantRange
){VK_SHADER_STAGE_VERTEX_BIT
, 0, 4},
787 res
= radv_CreatePipelineLayout(radv_device_to_handle(device
),
788 &pl_depth_create_info
,
789 &device
->meta_state
.alloc
,
790 &device
->meta_state
.clear_depth_p_layout
);
791 if (res
!= VK_SUCCESS
)
794 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->clear
); ++i
) {
795 uint32_t samples
= 1 << i
;
796 for (uint32_t j
= 0; j
< ARRAY_SIZE(pipeline_formats
); ++j
) {
797 VkFormat format
= pipeline_formats
[j
];
798 unsigned fs_key
= radv_format_meta_fs_key(format
);
799 assert(!state
->clear
[i
].color_pipelines
[fs_key
]);
801 res
= create_color_renderpass(device
, format
, samples
,
802 &state
->clear
[i
].render_pass
[fs_key
]);
803 if (res
!= VK_SUCCESS
)
806 res
= create_color_pipeline(device
, samples
, 0, &state
->clear
[i
].color_pipelines
[fs_key
],
807 state
->clear
[i
].render_pass
[fs_key
]);
808 if (res
!= VK_SUCCESS
)
813 res
= create_depthstencil_renderpass(device
,
815 &state
->clear
[i
].depthstencil_rp
);
816 if (res
!= VK_SUCCESS
)
819 for (uint32_t j
= 0; j
< NUM_DEPTH_CLEAR_PIPELINES
; j
++) {
820 res
= create_depthstencil_pipeline(device
,
821 VK_IMAGE_ASPECT_DEPTH_BIT
,
824 &state
->clear
[i
].depth_only_pipeline
[j
],
825 state
->clear
[i
].depthstencil_rp
);
826 if (res
!= VK_SUCCESS
)
829 res
= create_depthstencil_pipeline(device
,
830 VK_IMAGE_ASPECT_STENCIL_BIT
,
833 &state
->clear
[i
].stencil_only_pipeline
[j
],
834 state
->clear
[i
].depthstencil_rp
);
835 if (res
!= VK_SUCCESS
)
838 res
= create_depthstencil_pipeline(device
,
839 VK_IMAGE_ASPECT_DEPTH_BIT
|
840 VK_IMAGE_ASPECT_STENCIL_BIT
,
843 &state
->clear
[i
].depthstencil_pipeline
[j
],
844 state
->clear
[i
].depthstencil_rp
);
845 if (res
!= VK_SUCCESS
)
852 radv_device_finish_meta_clear_state(device
);
856 static void vi_get_fast_clear_parameters(VkFormat format
,
857 const VkClearColorValue
*clear_value
,
858 uint32_t* reset_value
,
859 bool *can_avoid_fast_clear_elim
)
863 bool main_value
= false;
864 bool extra_value
= false;
866 *can_avoid_fast_clear_elim
= false;
868 *reset_value
= 0x20202020U
;
870 const struct vk_format_description
*desc
= vk_format_description(format
);
871 if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
||
872 format
== VK_FORMAT_R5G6B5_UNORM_PACK16
||
873 format
== VK_FORMAT_B5G6R5_UNORM_PACK16
)
875 else if (desc
->layout
== VK_FORMAT_LAYOUT_PLAIN
) {
876 if (radv_translate_colorswap(format
, false) <= 1)
877 extra_channel
= desc
->nr_channels
- 1;
883 for (i
= 0; i
< 4; i
++) {
884 int index
= desc
->swizzle
[i
] - VK_SWIZZLE_X
;
885 if (desc
->swizzle
[i
] < VK_SWIZZLE_X
||
886 desc
->swizzle
[i
] > VK_SWIZZLE_W
)
889 if (desc
->channel
[i
].pure_integer
&&
890 desc
->channel
[i
].type
== VK_FORMAT_TYPE_SIGNED
) {
891 /* Use the maximum value for clamping the clear color. */
892 int max
= u_bit_consecutive(0, desc
->channel
[i
].size
- 1);
894 values
[i
] = clear_value
->int32
[i
] != 0;
895 if (clear_value
->int32
[i
] != 0 && MIN2(clear_value
->int32
[i
], max
) != max
)
897 } else if (desc
->channel
[i
].pure_integer
&&
898 desc
->channel
[i
].type
== VK_FORMAT_TYPE_UNSIGNED
) {
899 /* Use the maximum value for clamping the clear color. */
900 unsigned max
= u_bit_consecutive(0, desc
->channel
[i
].size
);
902 values
[i
] = clear_value
->uint32
[i
] != 0U;
903 if (clear_value
->uint32
[i
] != 0U && MIN2(clear_value
->uint32
[i
], max
) != max
)
906 values
[i
] = clear_value
->float32
[i
] != 0.0F
;
907 if (clear_value
->float32
[i
] != 0.0F
&& clear_value
->float32
[i
] != 1.0F
)
911 if (index
== extra_channel
)
912 extra_value
= values
[i
];
914 main_value
= values
[i
];
917 for (int i
= 0; i
< 4; ++i
)
918 if (values
[i
] != main_value
&&
919 desc
->swizzle
[i
] - VK_SWIZZLE_X
!= extra_channel
&&
920 desc
->swizzle
[i
] >= VK_SWIZZLE_X
&&
921 desc
->swizzle
[i
] <= VK_SWIZZLE_W
)
924 *can_avoid_fast_clear_elim
= true;
926 *reset_value
|= 0x80808080U
;
929 *reset_value
|= 0x40404040U
;
934 emit_fast_color_clear(struct radv_cmd_buffer
*cmd_buffer
,
935 const VkClearAttachment
*clear_att
,
936 const VkClearRect
*clear_rect
,
937 enum radv_cmd_flush_bits
*pre_flush
,
938 enum radv_cmd_flush_bits
*post_flush
,
941 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
942 const uint32_t subpass_att
= clear_att
->colorAttachment
;
943 const uint32_t pass_att
= subpass
->color_attachments
[subpass_att
].attachment
;
944 VkImageLayout image_layout
= subpass
->color_attachments
[subpass_att
].layout
;
945 const struct radv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
946 const struct radv_image_view
*iview
= fb
->attachments
[pass_att
].attachment
;
947 VkClearColorValue clear_value
= clear_att
->clearValue
.color
;
948 uint32_t clear_color
[2], flush_bits
;
951 if (!iview
->image
->cmask
.size
&& !iview
->image
->surface
.dcc_size
)
954 if (cmd_buffer
->device
->instance
->debug_flags
& RADV_DEBUG_NO_FAST_CLEARS
)
957 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
)))
960 /* don't fast clear 3D */
961 if (iview
->image
->type
== VK_IMAGE_TYPE_3D
)
964 /* all layers are bound */
965 if (iview
->base_layer
> 0)
967 if (iview
->image
->info
.array_size
!= iview
->layer_count
)
970 if (iview
->image
->info
.levels
> 1)
973 if (iview
->image
->surface
.is_linear
)
975 if (!radv_image_extent_compare(iview
->image
, &iview
->extent
))
978 if (clear_rect
->rect
.offset
.x
|| clear_rect
->rect
.offset
.y
||
979 clear_rect
->rect
.extent
.width
!= iview
->image
->info
.width
||
980 clear_rect
->rect
.extent
.height
!= iview
->image
->info
.height
)
983 if (view_mask
&& (iview
->image
->info
.array_size
>= 32 ||
984 (1u << iview
->image
->info
.array_size
) - 1u != view_mask
))
986 if (!view_mask
&& clear_rect
->baseArrayLayer
!= 0)
988 if (!view_mask
&& clear_rect
->layerCount
!= iview
->image
->info
.array_size
)
991 /* RB+ doesn't work with CMASK fast clear on Stoney. */
992 if (!iview
->image
->surface
.dcc_size
&&
993 cmd_buffer
->device
->physical_device
->rad_info
.family
== CHIP_STONEY
)
997 ret
= radv_format_pack_clear_color(iview
->image
->vk_format
,
998 clear_color
, &clear_value
);
1003 cmd_buffer
->state
.flush_bits
|= (RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
1004 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
) & ~ *pre_flush
;
1005 *pre_flush
|= cmd_buffer
->state
.flush_bits
;
1007 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
1008 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
1009 /* clear cmask buffer */
1010 if (iview
->image
->surface
.dcc_size
) {
1011 uint32_t reset_value
;
1012 bool can_avoid_fast_clear_elim
;
1013 vi_get_fast_clear_parameters(iview
->image
->vk_format
,
1014 &clear_value
, &reset_value
,
1015 &can_avoid_fast_clear_elim
);
1017 flush_bits
= radv_fill_buffer(cmd_buffer
, iview
->image
->bo
,
1018 iview
->image
->offset
+ iview
->image
->dcc_offset
,
1019 iview
->image
->surface
.dcc_size
, reset_value
);
1020 radv_set_dcc_need_cmask_elim_pred(cmd_buffer
, iview
->image
,
1021 !can_avoid_fast_clear_elim
);
1023 flush_bits
= radv_fill_buffer(cmd_buffer
, iview
->image
->bo
,
1024 iview
->image
->offset
+ iview
->image
->cmask
.offset
,
1025 iview
->image
->cmask
.size
, 0);
1029 *post_flush
|= flush_bits
;
1031 cmd_buffer
->state
.flush_bits
|= flush_bits
;
1034 radv_set_color_clear_regs(cmd_buffer
, iview
->image
, subpass_att
, clear_color
);
1042 * The parameters mean that same as those in vkCmdClearAttachments.
1045 emit_clear(struct radv_cmd_buffer
*cmd_buffer
,
1046 const VkClearAttachment
*clear_att
,
1047 const VkClearRect
*clear_rect
,
1048 enum radv_cmd_flush_bits
*pre_flush
,
1049 enum radv_cmd_flush_bits
*post_flush
,
1052 if (clear_att
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
1053 if (!emit_fast_color_clear(cmd_buffer
, clear_att
, clear_rect
,
1054 pre_flush
, post_flush
, view_mask
))
1055 emit_color_clear(cmd_buffer
, clear_att
, clear_rect
, view_mask
);
1057 assert(clear_att
->aspectMask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
1058 VK_IMAGE_ASPECT_STENCIL_BIT
));
1059 if (!emit_fast_htile_clear(cmd_buffer
, clear_att
, clear_rect
,
1060 pre_flush
, post_flush
))
1061 emit_depthstencil_clear(cmd_buffer
, clear_att
, clear_rect
);
1066 radv_attachment_needs_clear(struct radv_cmd_state
*cmd_state
, uint32_t a
)
1068 uint32_t view_mask
= cmd_state
->subpass
->view_mask
;
1069 return (a
!= VK_ATTACHMENT_UNUSED
&&
1070 cmd_state
->attachments
[a
].pending_clear_aspects
&&
1071 (!view_mask
|| (view_mask
& ~cmd_state
->attachments
[a
].cleared_views
)));
1075 radv_subpass_needs_clear(struct radv_cmd_buffer
*cmd_buffer
)
1077 struct radv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
1080 if (!cmd_state
->subpass
)
1083 for (uint32_t i
= 0; i
< cmd_state
->subpass
->color_count
; ++i
) {
1084 a
= cmd_state
->subpass
->color_attachments
[i
].attachment
;
1085 if (radv_attachment_needs_clear(cmd_state
, a
))
1089 a
= cmd_state
->subpass
->depth_stencil_attachment
.attachment
;
1090 return radv_attachment_needs_clear(cmd_state
, a
);
1094 radv_subpass_clear_attachment(struct radv_cmd_buffer
*cmd_buffer
,
1095 struct radv_attachment_state
*attachment
,
1096 const VkClearAttachment
*clear_att
,
1097 enum radv_cmd_flush_bits
*pre_flush
,
1098 enum radv_cmd_flush_bits
*post_flush
)
1100 struct radv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
1101 uint32_t view_mask
= cmd_state
->subpass
->view_mask
;
1103 VkClearRect clear_rect
= {
1104 .rect
= cmd_state
->render_area
,
1105 .baseArrayLayer
= 0,
1106 .layerCount
= cmd_state
->framebuffer
->layers
,
1109 emit_clear(cmd_buffer
, clear_att
, &clear_rect
, pre_flush
, post_flush
,
1110 view_mask
& ~attachment
->cleared_views
);
1112 attachment
->cleared_views
|= view_mask
;
1114 attachment
->pending_clear_aspects
= 0;
1118 * Emit any pending attachment clears for the current subpass.
1120 * @see radv_attachment_state::pending_clear_aspects
1123 radv_cmd_buffer_clear_subpass(struct radv_cmd_buffer
*cmd_buffer
)
1125 struct radv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
1126 struct radv_meta_saved_state saved_state
;
1127 enum radv_cmd_flush_bits pre_flush
= 0;
1128 enum radv_cmd_flush_bits post_flush
= 0;
1130 if (!radv_subpass_needs_clear(cmd_buffer
))
1133 radv_meta_save(&saved_state
, cmd_buffer
,
1134 RADV_META_SAVE_GRAPHICS_PIPELINE
|
1135 RADV_META_SAVE_CONSTANTS
);
1137 for (uint32_t i
= 0; i
< cmd_state
->subpass
->color_count
; ++i
) {
1138 uint32_t a
= cmd_state
->subpass
->color_attachments
[i
].attachment
;
1140 if (!radv_attachment_needs_clear(cmd_state
, a
))
1143 assert(cmd_state
->attachments
[a
].pending_clear_aspects
==
1144 VK_IMAGE_ASPECT_COLOR_BIT
);
1146 VkClearAttachment clear_att
= {
1147 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1148 .colorAttachment
= i
, /* Use attachment index relative to subpass */
1149 .clearValue
= cmd_state
->attachments
[a
].clear_value
,
1152 radv_subpass_clear_attachment(cmd_buffer
,
1153 &cmd_state
->attachments
[a
],
1154 &clear_att
, &pre_flush
,
1158 uint32_t ds
= cmd_state
->subpass
->depth_stencil_attachment
.attachment
;
1159 if (radv_attachment_needs_clear(cmd_state
, ds
)) {
1160 VkClearAttachment clear_att
= {
1161 .aspectMask
= cmd_state
->attachments
[ds
].pending_clear_aspects
,
1162 .clearValue
= cmd_state
->attachments
[ds
].clear_value
,
1165 radv_subpass_clear_attachment(cmd_buffer
,
1166 &cmd_state
->attachments
[ds
],
1167 &clear_att
, &pre_flush
,
1171 radv_meta_restore(&saved_state
, cmd_buffer
);
1172 cmd_buffer
->state
.flush_bits
|= post_flush
;
1176 radv_clear_image_layer(struct radv_cmd_buffer
*cmd_buffer
,
1177 struct radv_image
*image
,
1178 VkImageLayout image_layout
,
1179 const VkImageSubresourceRange
*range
,
1180 VkFormat format
, int level
, int layer
,
1181 const VkClearValue
*clear_val
)
1183 VkDevice device_h
= radv_device_to_handle(cmd_buffer
->device
);
1184 struct radv_image_view iview
;
1185 uint32_t width
= radv_minify(image
->info
.width
, range
->baseMipLevel
+ level
);
1186 uint32_t height
= radv_minify(image
->info
.height
, range
->baseMipLevel
+ level
);
1188 radv_image_view_init(&iview
, cmd_buffer
->device
,
1189 &(VkImageViewCreateInfo
) {
1190 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1191 .image
= radv_image_to_handle(image
),
1192 .viewType
= radv_meta_get_view_type(image
),
1194 .subresourceRange
= {
1195 .aspectMask
= range
->aspectMask
,
1196 .baseMipLevel
= range
->baseMipLevel
+ level
,
1198 .baseArrayLayer
= range
->baseArrayLayer
+ layer
,
1204 radv_CreateFramebuffer(device_h
,
1205 &(VkFramebufferCreateInfo
) {
1206 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
1207 .attachmentCount
= 1,
1208 .pAttachments
= (VkImageView
[]) {
1209 radv_image_view_to_handle(&iview
),
1215 &cmd_buffer
->pool
->alloc
,
1218 VkAttachmentDescription att_desc
= {
1219 .format
= iview
.vk_format
,
1220 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
1221 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
1222 .stencilLoadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
1223 .stencilStoreOp
= VK_ATTACHMENT_STORE_OP_STORE
,
1224 .initialLayout
= image_layout
,
1225 .finalLayout
= image_layout
,
1228 VkSubpassDescription subpass_desc
= {
1229 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
1230 .inputAttachmentCount
= 0,
1231 .colorAttachmentCount
= 0,
1232 .pColorAttachments
= NULL
,
1233 .pResolveAttachments
= NULL
,
1234 .pDepthStencilAttachment
= NULL
,
1235 .preserveAttachmentCount
= 0,
1236 .pPreserveAttachments
= NULL
,
1239 const VkAttachmentReference att_ref
= {
1241 .layout
= image_layout
,
1244 if (range
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
1245 subpass_desc
.colorAttachmentCount
= 1;
1246 subpass_desc
.pColorAttachments
= &att_ref
;
1248 subpass_desc
.pDepthStencilAttachment
= &att_ref
;
1252 radv_CreateRenderPass(device_h
,
1253 &(VkRenderPassCreateInfo
) {
1254 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
1255 .attachmentCount
= 1,
1256 .pAttachments
= &att_desc
,
1258 .pSubpasses
= &subpass_desc
,
1260 &cmd_buffer
->pool
->alloc
,
1263 radv_CmdBeginRenderPass(radv_cmd_buffer_to_handle(cmd_buffer
),
1264 &(VkRenderPassBeginInfo
) {
1265 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
1267 .offset
= { 0, 0, },
1275 .clearValueCount
= 0,
1276 .pClearValues
= NULL
,
1278 VK_SUBPASS_CONTENTS_INLINE
);
1280 VkClearAttachment clear_att
= {
1281 .aspectMask
= range
->aspectMask
,
1282 .colorAttachment
= 0,
1283 .clearValue
= *clear_val
,
1286 VkClearRect clear_rect
= {
1289 .extent
= { width
, height
},
1291 .baseArrayLayer
= range
->baseArrayLayer
,
1292 .layerCount
= 1, /* FINISHME: clear multi-layer framebuffer */
1295 emit_clear(cmd_buffer
, &clear_att
, &clear_rect
, NULL
, NULL
, 0);
1297 radv_CmdEndRenderPass(radv_cmd_buffer_to_handle(cmd_buffer
));
1298 radv_DestroyRenderPass(device_h
, pass
,
1299 &cmd_buffer
->pool
->alloc
);
1300 radv_DestroyFramebuffer(device_h
, fb
,
1301 &cmd_buffer
->pool
->alloc
);
1304 radv_cmd_clear_image(struct radv_cmd_buffer
*cmd_buffer
,
1305 struct radv_image
*image
,
1306 VkImageLayout image_layout
,
1307 const VkClearValue
*clear_value
,
1308 uint32_t range_count
,
1309 const VkImageSubresourceRange
*ranges
,
1312 VkFormat format
= image
->vk_format
;
1313 VkClearValue internal_clear_value
= *clear_value
;
1315 if (format
== VK_FORMAT_E5B9G9R9_UFLOAT_PACK32
) {
1317 format
= VK_FORMAT_R32_UINT
;
1318 value
= float3_to_rgb9e5(clear_value
->color
.float32
);
1319 internal_clear_value
.color
.uint32
[0] = value
;
1322 if (format
== VK_FORMAT_R4G4_UNORM_PACK8
) {
1324 format
= VK_FORMAT_R8_UINT
;
1325 r
= float_to_ubyte(clear_value
->color
.float32
[0]) >> 4;
1326 g
= float_to_ubyte(clear_value
->color
.float32
[1]) >> 4;
1327 internal_clear_value
.color
.uint32
[0] = (r
<< 4) | (g
& 0xf);
1330 for (uint32_t r
= 0; r
< range_count
; r
++) {
1331 const VkImageSubresourceRange
*range
= &ranges
[r
];
1332 for (uint32_t l
= 0; l
< radv_get_levelCount(image
, range
); ++l
) {
1333 const uint32_t layer_count
= image
->type
== VK_IMAGE_TYPE_3D
?
1334 radv_minify(image
->info
.depth
, range
->baseMipLevel
+ l
) :
1335 radv_get_layerCount(image
, range
);
1336 for (uint32_t s
= 0; s
< layer_count
; ++s
) {
1339 struct radv_meta_blit2d_surf surf
;
1340 surf
.format
= format
;
1342 surf
.level
= range
->baseMipLevel
+ l
;
1343 surf
.layer
= range
->baseArrayLayer
+ s
;
1344 surf
.aspect_mask
= range
->aspectMask
;
1345 radv_meta_clear_image_cs(cmd_buffer
, &surf
,
1346 &internal_clear_value
.color
);
1348 radv_clear_image_layer(cmd_buffer
, image
, image_layout
,
1349 range
, format
, l
, s
, &internal_clear_value
);
1356 void radv_CmdClearColorImage(
1357 VkCommandBuffer commandBuffer
,
1359 VkImageLayout imageLayout
,
1360 const VkClearColorValue
* pColor
,
1361 uint32_t rangeCount
,
1362 const VkImageSubresourceRange
* pRanges
)
1364 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1365 RADV_FROM_HANDLE(radv_image
, image
, image_h
);
1366 struct radv_meta_saved_state saved_state
;
1367 bool cs
= cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
;
1370 radv_meta_save(&saved_state
, cmd_buffer
,
1371 RADV_META_SAVE_COMPUTE_PIPELINE
|
1372 RADV_META_SAVE_CONSTANTS
|
1373 RADV_META_SAVE_DESCRIPTORS
);
1375 radv_meta_save(&saved_state
, cmd_buffer
,
1376 RADV_META_SAVE_GRAPHICS_PIPELINE
|
1377 RADV_META_SAVE_CONSTANTS
);
1380 radv_cmd_clear_image(cmd_buffer
, image
, imageLayout
,
1381 (const VkClearValue
*) pColor
,
1382 rangeCount
, pRanges
, cs
);
1384 radv_meta_restore(&saved_state
, cmd_buffer
);
1387 void radv_CmdClearDepthStencilImage(
1388 VkCommandBuffer commandBuffer
,
1390 VkImageLayout imageLayout
,
1391 const VkClearDepthStencilValue
* pDepthStencil
,
1392 uint32_t rangeCount
,
1393 const VkImageSubresourceRange
* pRanges
)
1395 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1396 RADV_FROM_HANDLE(radv_image
, image
, image_h
);
1397 struct radv_meta_saved_state saved_state
;
1399 radv_meta_save(&saved_state
, cmd_buffer
,
1400 RADV_META_SAVE_GRAPHICS_PIPELINE
|
1401 RADV_META_SAVE_CONSTANTS
);
1403 radv_cmd_clear_image(cmd_buffer
, image
, imageLayout
,
1404 (const VkClearValue
*) pDepthStencil
,
1405 rangeCount
, pRanges
, false);
1407 radv_meta_restore(&saved_state
, cmd_buffer
);
1410 void radv_CmdClearAttachments(
1411 VkCommandBuffer commandBuffer
,
1412 uint32_t attachmentCount
,
1413 const VkClearAttachment
* pAttachments
,
1415 const VkClearRect
* pRects
)
1417 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1418 struct radv_meta_saved_state saved_state
;
1419 enum radv_cmd_flush_bits pre_flush
= 0;
1420 enum radv_cmd_flush_bits post_flush
= 0;
1422 if (!cmd_buffer
->state
.subpass
)
1425 radv_meta_save(&saved_state
, cmd_buffer
,
1426 RADV_META_SAVE_GRAPHICS_PIPELINE
|
1427 RADV_META_SAVE_CONSTANTS
);
1429 /* FINISHME: We can do better than this dumb loop. It thrashes too much
1432 for (uint32_t a
= 0; a
< attachmentCount
; ++a
) {
1433 for (uint32_t r
= 0; r
< rectCount
; ++r
) {
1434 emit_clear(cmd_buffer
, &pAttachments
[a
], &pRects
[r
], &pre_flush
, &post_flush
,
1435 cmd_buffer
->state
.subpass
->view_mask
);
1439 radv_meta_restore(&saved_state
, cmd_buffer
);
1440 cmd_buffer
->state
.flush_bits
|= post_flush
;