7f3cfdccc8670be9322a0942b478563f00a8b606
[mesa.git] / src / amd / vulkan / radv_meta_clear.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "radv_meta.h"
25 #include "radv_private.h"
26 #include "nir/nir_builder.h"
27
28 #include "util/format_rgb9e5.h"
29 #include "vk_format.h"
30
31 enum {
32 DEPTH_CLEAR_SLOW,
33 DEPTH_CLEAR_FAST_EXPCLEAR,
34 DEPTH_CLEAR_FAST_NO_EXPCLEAR
35 };
36
37 static void
38 build_color_shaders(struct nir_shader **out_vs,
39 struct nir_shader **out_fs,
40 uint32_t frag_output)
41 {
42 nir_builder vs_b;
43 nir_builder fs_b;
44
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);
47
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");
50
51 const struct glsl_type *position_type = glsl_vec4_type();
52 const struct glsl_type *color_type = glsl_vec4_type();
53
54 nir_variable *vs_out_pos =
55 nir_variable_create(vs_b.shader, nir_var_shader_out, position_type,
56 "gl_Position");
57 vs_out_pos->data.location = VARYING_SLOT_POS;
58
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);
66
67 nir_variable *fs_out_color =
68 nir_variable_create(fs_b.shader, nir_var_shader_out, color_type,
69 "f_color");
70 fs_out_color->data.location = FRAG_RESULT_DATA0 + frag_output;
71
72 nir_store_var(&fs_b, fs_out_color, &in_color_load->dest.ssa, 0xf);
73
74 nir_ssa_def *outvec = radv_meta_gen_rect_vertices(&vs_b);
75 nir_store_var(&vs_b, vs_out_pos, outvec, 0xf);
76
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,
80 "v_layer");
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);
84
85 nir_store_var(&vs_b, vs_out_layer, inst_id, 0x1);
86
87 *out_vs = vs_b.shader;
88 *out_fs = fs_b.shader;
89 }
90
91 static VkResult
92 create_pipeline(struct radv_device *device,
93 struct radv_render_pass *render_pass,
94 uint32_t samples,
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)
104 {
105 VkDevice device_h = radv_device_to_handle(device);
106 VkResult result;
107
108 struct radv_shader_module vs_m = { .nir = vs_nir };
109 struct radv_shader_module fs_m = { .nir = fs_nir };
110
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[]) {
118 {
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),
122 .pName = "main",
123 },
124 {
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),
128 .pName = "main",
129 },
130 },
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,
136 },
137 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
138 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
139 .viewportCount = 1,
140 .scissorCount = 1,
141 },
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,
149 },
150 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
151 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
152 .rasterizationSamples = samples,
153 .sampleShadingEnable = false,
154 .pSampleMask = NULL,
155 .alphaToCoverageEnable = false,
156 .alphaToOneEnable = false,
157 },
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.
165 */
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,
178 },
179 },
180 .layout = layout,
181 .flags = 0,
182 .renderPass = radv_render_pass_to_handle(render_pass),
183 .subpass = 0,
184 },
185 extra,
186 alloc,
187 &pipeline_h);
188
189 ralloc_free(vs_nir);
190 ralloc_free(fs_nir);
191
192 *pipeline = radv_pipeline_from_handle(pipeline_h);
193
194 return result;
195 }
196
197 static VkResult
198 create_color_renderpass(struct radv_device *device,
199 VkFormat vk_format,
200 uint32_t samples,
201 VkRenderPass *pass)
202 {
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) {
208 .format = vk_format,
209 .samples = samples,
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,
214 },
215 .subpassCount = 1,
216 .pSubpasses = &(VkSubpassDescription) {
217 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
218 .inputAttachmentCount = 0,
219 .colorAttachmentCount = 1,
220 .pColorAttachments = &(VkAttachmentReference) {
221 .attachment = 0,
222 .layout = VK_IMAGE_LAYOUT_GENERAL,
223 },
224 .pResolveAttachments = NULL,
225 .pDepthStencilAttachment = &(VkAttachmentReference) {
226 .attachment = VK_ATTACHMENT_UNUSED,
227 .layout = VK_IMAGE_LAYOUT_GENERAL,
228 },
229 .preserveAttachmentCount = 1,
230 .pPreserveAttachments = (uint32_t[]) { 0 },
231 },
232 .dependencyCount = 0,
233 }, &device->meta_state.alloc, pass);
234 }
235
236 static VkResult
237 create_color_pipeline(struct radv_device *device,
238 uint32_t samples,
239 uint32_t frag_output,
240 struct radv_pipeline **pipeline,
241 VkRenderPass pass)
242 {
243 struct nir_shader *vs_nir;
244 struct nir_shader *fs_nir;
245 VkResult result;
246 build_color_shaders(&vs_nir, &fs_nir, frag_output);
247
248 const VkPipelineVertexInputStateCreateInfo vi_state = {
249 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
250 .vertexBindingDescriptionCount = 0,
251 .vertexAttributeDescriptionCount = 0,
252 };
253
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,
260 };
261
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,
269 };
270
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
276 };
277
278
279 struct radv_graphics_pipeline_create_info extra = {
280 .use_rectlist = true,
281 };
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);
286
287 return result;
288 }
289
290 static void
291 destroy_pipeline(struct radv_device *device, struct radv_pipeline *pipeline)
292 {
293 if (!pipeline)
294 return;
295
296 radv_DestroyPipeline(radv_device_to_handle(device),
297 radv_pipeline_to_handle(pipeline),
298 &device->meta_state.alloc);
299
300 }
301
302 static void
303 destroy_render_pass(struct radv_device *device, VkRenderPass renderpass)
304 {
305 radv_DestroyRenderPass(radv_device_to_handle(device), renderpass,
306 &device->meta_state.alloc);
307 }
308
309 void
310 radv_device_finish_meta_clear_state(struct radv_device *device)
311 {
312 struct radv_meta_state *state = &device->meta_state;
313
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]);
318 }
319
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]);
324 }
325 destroy_render_pass(device, state->clear[i].depthstencil_rp);
326 }
327 radv_DestroyPipelineLayout(radv_device_to_handle(device),
328 state->clear_color_p_layout,
329 &state->alloc);
330 radv_DestroyPipelineLayout(radv_device_to_handle(device),
331 state->clear_depth_p_layout,
332 &state->alloc);
333 }
334
335 static void
336 emit_color_clear(struct radv_cmd_buffer *cmd_buffer,
337 const VkClearAttachment *clear_att,
338 const VkClearRect *clear_rect)
339 {
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;
353
354 if (fs_key == -1) {
355 radv_finishme("color clears incomplete");
356 return;
357 }
358 pipeline = device->meta_state.clear[samples_log2].color_pipelines[fs_key];
359 pipeline_h = radv_pipeline_to_handle(pipeline);
360
361 if (!pipeline) {
362 radv_finishme("color clears incomplete");
363 return;
364 }
365 assert(samples_log2 < ARRAY_SIZE(device->meta_state.clear));
366 assert(pipeline);
367 assert(clear_att->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
368 assert(clear_att->colorAttachment < subpass->color_count);
369
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,
373 &clear_value);
374
375 struct radv_subpass clear_subpass = {
376 .color_count = 1,
377 .color_attachments = (VkAttachmentReference[]) {
378 subpass->color_attachments[clear_att->colorAttachment]
379 },
380 .depth_stencil_attachment = (VkAttachmentReference) { VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_UNDEFINED }
381 };
382
383 radv_cmd_buffer_set_subpass(cmd_buffer, &clear_subpass, false);
384
385 if (cmd_buffer->state.pipeline != pipeline) {
386 radv_CmdBindPipeline(cmd_buffer_h, VK_PIPELINE_BIND_POINT_GRAPHICS,
387 pipeline_h);
388 }
389
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,
395 .minDepth = 0.0f,
396 .maxDepth = 1.0f
397 });
398
399 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer), 0, 1, &clear_rect->rect);
400
401 radv_CmdDraw(cmd_buffer_h, 3, clear_rect->layerCount, 0, 0);
402
403 radv_cmd_buffer_set_subpass(cmd_buffer, subpass, false);
404 }
405
406
407 static void
408 build_depthstencil_shader(struct nir_shader **out_vs, struct nir_shader **out_fs)
409 {
410 nir_builder vs_b, fs_b;
411
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);
414
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();
418
419 nir_variable *vs_out_pos =
420 nir_variable_create(vs_b.shader, nir_var_shader_out, position_out_type,
421 "gl_Position");
422 vs_out_pos->data.location = VARYING_SLOT_POS;
423
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);
431
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);
434
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,
438 "v_layer");
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);
443
444 *out_vs = vs_b.shader;
445 *out_fs = fs_b.shader;
446 }
447
448 static VkResult
449 create_depthstencil_renderpass(struct radv_device *device,
450 uint32_t samples,
451 VkRenderPass *render_pass)
452 {
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,
459 .samples = samples,
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,
464 },
465 .subpassCount = 1,
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) {
473 .attachment = 0,
474 .layout = VK_IMAGE_LAYOUT_GENERAL,
475 },
476 .preserveAttachmentCount = 1,
477 .pPreserveAttachments = (uint32_t[]) { 0 },
478 },
479 .dependencyCount = 0,
480 }, &device->meta_state.alloc, render_pass);
481 }
482
483 static VkResult
484 create_depthstencil_pipeline(struct radv_device *device,
485 VkImageAspectFlags aspects,
486 uint32_t samples,
487 int index,
488 struct radv_pipeline **pipeline,
489 VkRenderPass render_pass)
490 {
491 struct nir_shader *vs_nir, *fs_nir;
492 VkResult result;
493 build_depthstencil_shader(&vs_nir, &fs_nir);
494
495 const VkPipelineVertexInputStateCreateInfo vi_state = {
496 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
497 .vertexBindingDescriptionCount = 0,
498 .vertexAttributeDescriptionCount = 0,
499 };
500
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),
508 .front = {
509 .passOp = VK_STENCIL_OP_REPLACE,
510 .compareOp = VK_COMPARE_OP_ALWAYS,
511 .writeMask = UINT32_MAX,
512 .reference = 0, /* dynamic */
513 },
514 .back = { 0 /* dont care */ },
515 };
516
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,
522 };
523
524 struct radv_graphics_pipeline_create_info extra = {
525 .use_rectlist = true,
526 };
527
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;
531 }
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;
535 }
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);
540 return result;
541 }
542
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)
547 {
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)
554 return false;
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))
560 return true;
561 return false;
562 }
563
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,
568 int samples_log2,
569 VkImageAspectFlags aspects,
570 VkImageLayout layout,
571 const VkClearRect *clear_rect,
572 VkClearDepthStencilValue clear_value)
573 {
574 bool fast = depth_view_can_fast_clear(cmd_buffer, iview, layout, clear_rect);
575 int index = DEPTH_CLEAR_SLOW;
576
577 if (fast) {
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;
581 }
582
583 switch (aspects) {
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];
590 }
591 unreachable("expected depth or stencil aspect");
592 }
593
594 static void
595 emit_depthstencil_clear(struct radv_cmd_buffer *cmd_buffer,
596 const VkClearAttachment *clear_att,
597 const VkClearRect *clear_rect)
598 {
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);
610
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);
616
617 if (!(aspects & VK_IMAGE_ASPECT_DEPTH_BIT))
618 clear_value.depth = 1.0f;
619
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,
623 &clear_value.depth);
624
625 if (aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
626 radv_CmdSetStencilReference(cmd_buffer_h, VK_STENCIL_FACE_FRONT_BIT,
627 clear_value.stencil);
628 }
629
630 struct radv_pipeline *pipeline = pick_depthstencil_pipeline(cmd_buffer,
631 meta_state,
632 iview,
633 samples_log2,
634 aspects,
635 subpass->depth_stencil_attachment.layout,
636 clear_rect,
637 clear_value);
638 if (cmd_buffer->state.pipeline != pipeline) {
639 radv_CmdBindPipeline(cmd_buffer_h, VK_PIPELINE_BIND_POINT_GRAPHICS,
640 radv_pipeline_to_handle(pipeline));
641 }
642
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);
645
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,
651 .minDepth = 0.0f,
652 .maxDepth = 1.0f
653 });
654
655 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer), 0, 1, &clear_rect->rect);
656
657 radv_CmdDraw(cmd_buffer_h, 3, clear_rect->layerCount, 0, 0);
658 }
659
660 static bool
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)
666 {
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;
674 uint32_t clear_word;
675
676 if (!iview->image->surface.htile_size)
677 return false;
678
679 if (cmd_buffer->device->debug_flags & RADV_DEBUG_NO_FAST_CLEARS)
680 return false;
681
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)))
683 goto fail;
684
685 /* don't fast clear 3D */
686 if (iview->image->type == VK_IMAGE_TYPE_3D)
687 goto fail;
688
689 /* all layers are bound */
690 if (iview->base_layer > 0)
691 goto fail;
692 if (iview->image->info.array_size != iview->layer_count)
693 goto fail;
694
695 if (iview->image->info.levels > 1)
696 goto fail;
697
698 if (!radv_image_extent_compare(iview->image, &iview->extent))
699 goto fail;
700
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)
704 goto fail;
705
706 if (clear_rect->baseArrayLayer != 0)
707 goto fail;
708 if (clear_rect->layerCount != iview->image->info.array_size)
709 goto fail;
710
711 if ((clear_value.depth != 0.0 && clear_value.depth != 1.0) || !(aspects & VK_IMAGE_ASPECT_DEPTH_BIT))
712 goto fail;
713
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))
716 goto fail;
717 clear_word = clear_value.depth ? 0xfffc0000 : 0;
718 } else
719 clear_word = clear_value.depth ? 0xfffffff0 : 0;
720
721 if (pre_flush) {
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;
725 } else
726 cmd_buffer->state.flush_bits |= RADV_CMD_FLAG_FLUSH_AND_INV_DB |
727 RADV_CMD_FLAG_FLUSH_AND_INV_DB_META;
728
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);
732
733
734 radv_set_depth_clear_regs(cmd_buffer, iview->image, clear_value, aspects);
735 if (post_flush)
736 *post_flush |= RADV_CMD_FLAG_CS_PARTIAL_FLUSH |
737 RADV_CMD_FLAG_INV_VMEM_L1 |
738 RADV_CMD_FLAG_WRITEBACK_GLOBAL_L2;
739 else
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;
743 return true;
744 fail:
745 return false;
746 }
747
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
759 };
760
761 VkResult
762 radv_device_init_meta_clear_state(struct radv_device *device)
763 {
764 VkResult res;
765 struct radv_meta_state *state = &device->meta_state;
766
767 memset(&device->meta_state.clear, 0, sizeof(device->meta_state.clear));
768
769 VkPipelineLayoutCreateInfo pl_color_create_info = {
770 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
771 .setLayoutCount = 0,
772 .pushConstantRangeCount = 1,
773 .pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_FRAGMENT_BIT, 0, 16},
774 };
775
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)
781 goto fail;
782
783 VkPipelineLayoutCreateInfo pl_depth_create_info = {
784 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
785 .setLayoutCount = 0,
786 .pushConstantRangeCount = 1,
787 .pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_VERTEX_BIT, 0, 4},
788 };
789
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)
795 goto fail;
796
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]);
803
804 res = create_color_renderpass(device, format, samples,
805 &state->clear[i].render_pass[fs_key]);
806 if (res != VK_SUCCESS)
807 goto fail;
808
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)
812 goto fail;
813
814 }
815
816 res = create_depthstencil_renderpass(device,
817 samples,
818 &state->clear[i].depthstencil_rp);
819 if (res != VK_SUCCESS)
820 goto fail;
821
822 for (uint32_t j = 0; j < NUM_DEPTH_CLEAR_PIPELINES; j++) {
823 res = create_depthstencil_pipeline(device,
824 VK_IMAGE_ASPECT_DEPTH_BIT,
825 samples,
826 j,
827 &state->clear[i].depth_only_pipeline[j],
828 state->clear[i].depthstencil_rp);
829 if (res != VK_SUCCESS)
830 goto fail;
831
832 res = create_depthstencil_pipeline(device,
833 VK_IMAGE_ASPECT_STENCIL_BIT,
834 samples,
835 j,
836 &state->clear[i].stencil_only_pipeline[j],
837 state->clear[i].depthstencil_rp);
838 if (res != VK_SUCCESS)
839 goto fail;
840
841 res = create_depthstencil_pipeline(device,
842 VK_IMAGE_ASPECT_DEPTH_BIT |
843 VK_IMAGE_ASPECT_STENCIL_BIT,
844 samples,
845 j,
846 &state->clear[i].depthstencil_pipeline[j],
847 state->clear[i].depthstencil_rp);
848 if (res != VK_SUCCESS)
849 goto fail;
850 }
851 }
852 return VK_SUCCESS;
853
854 fail:
855 radv_device_finish_meta_clear_state(device);
856 return res;
857 }
858
859 static bool
860 emit_fast_color_clear(struct radv_cmd_buffer *cmd_buffer,
861 const VkClearAttachment *clear_att,
862 const VkClearRect *clear_rect,
863 enum radv_cmd_flush_bits *pre_flush,
864 enum radv_cmd_flush_bits *post_flush)
865 {
866 const struct radv_subpass *subpass = cmd_buffer->state.subpass;
867 const uint32_t subpass_att = clear_att->colorAttachment;
868 const uint32_t pass_att = subpass->color_attachments[subpass_att].attachment;
869 VkImageLayout image_layout = subpass->color_attachments[subpass_att].layout;
870 const struct radv_framebuffer *fb = cmd_buffer->state.framebuffer;
871 const struct radv_image_view *iview = fb->attachments[pass_att].attachment;
872 VkClearColorValue clear_value = clear_att->clearValue.color;
873 uint32_t clear_color[2];
874 bool ret;
875
876 if (!iview->image->cmask.size && !iview->image->surface.dcc_size)
877 return false;
878
879 if (cmd_buffer->device->debug_flags & RADV_DEBUG_NO_FAST_CLEARS)
880 return false;
881
882 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)))
883 goto fail;
884 if (vk_format_get_blocksizebits(iview->image->vk_format) > 64)
885 goto fail;
886
887 /* don't fast clear 3D */
888 if (iview->image->type == VK_IMAGE_TYPE_3D)
889 goto fail;
890
891 /* all layers are bound */
892 if (iview->base_layer > 0)
893 goto fail;
894 if (iview->image->info.array_size != iview->layer_count)
895 goto fail;
896
897 if (iview->image->info.levels > 1)
898 goto fail;
899
900 if (iview->image->surface.u.legacy.level[0].mode < RADEON_SURF_MODE_1D)
901 goto fail;
902 if (!radv_image_extent_compare(iview->image, &iview->extent))
903 goto fail;
904
905 if (clear_rect->rect.offset.x || clear_rect->rect.offset.y ||
906 clear_rect->rect.extent.width != iview->image->info.width ||
907 clear_rect->rect.extent.height != iview->image->info.height)
908 goto fail;
909
910 if (clear_rect->baseArrayLayer != 0)
911 goto fail;
912 if (clear_rect->layerCount != iview->image->info.array_size)
913 goto fail;
914
915 /* RB+ doesn't work with CMASK fast clear on Stoney. */
916 if (!iview->image->surface.dcc_size &&
917 cmd_buffer->device->physical_device->rad_info.family == CHIP_STONEY)
918 goto fail;
919
920 /* DCC */
921 ret = radv_format_pack_clear_color(iview->image->vk_format,
922 clear_color, &clear_value);
923 if (ret == false)
924 goto fail;
925
926 if (pre_flush) {
927 cmd_buffer->state.flush_bits |= (RADV_CMD_FLAG_FLUSH_AND_INV_CB |
928 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META) & ~ *pre_flush;
929 *pre_flush |= cmd_buffer->state.flush_bits;
930 } else
931 cmd_buffer->state.flush_bits |= RADV_CMD_FLAG_FLUSH_AND_INV_CB |
932 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META;
933 /* clear cmask buffer */
934 if (iview->image->surface.dcc_size) {
935 radv_fill_buffer(cmd_buffer, iview->image->bo,
936 iview->image->offset + iview->image->dcc_offset,
937 iview->image->surface.dcc_size, 0x20202020);
938 } else {
939 radv_fill_buffer(cmd_buffer, iview->image->bo,
940 iview->image->offset + iview->image->cmask.offset,
941 iview->image->cmask.size, 0);
942 }
943
944 if (post_flush)
945 *post_flush |= RADV_CMD_FLAG_CS_PARTIAL_FLUSH |
946 RADV_CMD_FLAG_INV_VMEM_L1 |
947 RADV_CMD_FLAG_WRITEBACK_GLOBAL_L2;
948 else
949 cmd_buffer->state.flush_bits |= RADV_CMD_FLAG_CS_PARTIAL_FLUSH |
950 RADV_CMD_FLAG_INV_VMEM_L1 |
951 RADV_CMD_FLAG_WRITEBACK_GLOBAL_L2;
952
953 radv_set_color_clear_regs(cmd_buffer, iview->image, subpass_att, clear_color);
954
955 return true;
956 fail:
957 return false;
958 }
959
960 /**
961 * The parameters mean that same as those in vkCmdClearAttachments.
962 */
963 static void
964 emit_clear(struct radv_cmd_buffer *cmd_buffer,
965 const VkClearAttachment *clear_att,
966 const VkClearRect *clear_rect,
967 enum radv_cmd_flush_bits *pre_flush,
968 enum radv_cmd_flush_bits *post_flush)
969 {
970 if (clear_att->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
971
972 if (!emit_fast_color_clear(cmd_buffer, clear_att, clear_rect,
973 pre_flush, post_flush))
974 emit_color_clear(cmd_buffer, clear_att, clear_rect);
975 } else {
976 assert(clear_att->aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT |
977 VK_IMAGE_ASPECT_STENCIL_BIT));
978 if (!emit_fast_htile_clear(cmd_buffer, clear_att, clear_rect,
979 pre_flush, post_flush))
980 emit_depthstencil_clear(cmd_buffer, clear_att, clear_rect);
981 }
982 }
983
984 static bool
985 subpass_needs_clear(const struct radv_cmd_buffer *cmd_buffer)
986 {
987 const struct radv_cmd_state *cmd_state = &cmd_buffer->state;
988 uint32_t ds;
989
990 if (!cmd_state->subpass)
991 return false;
992 ds = cmd_state->subpass->depth_stencil_attachment.attachment;
993 for (uint32_t i = 0; i < cmd_state->subpass->color_count; ++i) {
994 uint32_t a = cmd_state->subpass->color_attachments[i].attachment;
995 if (cmd_state->attachments[a].pending_clear_aspects) {
996 return true;
997 }
998 }
999
1000 if (ds != VK_ATTACHMENT_UNUSED &&
1001 cmd_state->attachments[ds].pending_clear_aspects) {
1002 return true;
1003 }
1004
1005 return false;
1006 }
1007
1008 /**
1009 * Emit any pending attachment clears for the current subpass.
1010 *
1011 * @see radv_attachment_state::pending_clear_aspects
1012 */
1013 void
1014 radv_cmd_buffer_clear_subpass(struct radv_cmd_buffer *cmd_buffer)
1015 {
1016 struct radv_cmd_state *cmd_state = &cmd_buffer->state;
1017 struct radv_meta_saved_state saved_state;
1018 enum radv_cmd_flush_bits pre_flush = 0;
1019 enum radv_cmd_flush_bits post_flush = 0;
1020
1021 if (!subpass_needs_clear(cmd_buffer))
1022 return;
1023
1024 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state, cmd_buffer);
1025
1026 VkClearRect clear_rect = {
1027 .rect = cmd_state->render_area,
1028 .baseArrayLayer = 0,
1029 .layerCount = cmd_state->framebuffer->layers,
1030 };
1031
1032 for (uint32_t i = 0; i < cmd_state->subpass->color_count; ++i) {
1033 uint32_t a = cmd_state->subpass->color_attachments[i].attachment;
1034
1035 if (!cmd_state->attachments[a].pending_clear_aspects)
1036 continue;
1037
1038 assert(cmd_state->attachments[a].pending_clear_aspects ==
1039 VK_IMAGE_ASPECT_COLOR_BIT);
1040
1041 VkClearAttachment clear_att = {
1042 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1043 .colorAttachment = i, /* Use attachment index relative to subpass */
1044 .clearValue = cmd_state->attachments[a].clear_value,
1045 };
1046
1047 emit_clear(cmd_buffer, &clear_att, &clear_rect, &pre_flush, &post_flush);
1048 cmd_state->attachments[a].pending_clear_aspects = 0;
1049 }
1050
1051 uint32_t ds = cmd_state->subpass->depth_stencil_attachment.attachment;
1052
1053 if (ds != VK_ATTACHMENT_UNUSED) {
1054
1055 if (cmd_state->attachments[ds].pending_clear_aspects) {
1056
1057 VkClearAttachment clear_att = {
1058 .aspectMask = cmd_state->attachments[ds].pending_clear_aspects,
1059 .clearValue = cmd_state->attachments[ds].clear_value,
1060 };
1061
1062 emit_clear(cmd_buffer, &clear_att, &clear_rect,
1063 &pre_flush, &post_flush);
1064 cmd_state->attachments[ds].pending_clear_aspects = 0;
1065 }
1066 }
1067
1068 radv_meta_restore(&saved_state, cmd_buffer);
1069 cmd_buffer->state.flush_bits |= post_flush;
1070 }
1071
1072 static void
1073 radv_clear_image_layer(struct radv_cmd_buffer *cmd_buffer,
1074 struct radv_image *image,
1075 VkImageLayout image_layout,
1076 const VkImageSubresourceRange *range,
1077 VkFormat format, int level, int layer,
1078 const VkClearValue *clear_val)
1079 {
1080 VkDevice device_h = radv_device_to_handle(cmd_buffer->device);
1081 struct radv_image_view iview;
1082 radv_image_view_init(&iview, cmd_buffer->device,
1083 &(VkImageViewCreateInfo) {
1084 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1085 .image = radv_image_to_handle(image),
1086 .viewType = radv_meta_get_view_type(image),
1087 .format = format,
1088 .subresourceRange = {
1089 .aspectMask = range->aspectMask,
1090 .baseMipLevel = range->baseMipLevel + level,
1091 .levelCount = 1,
1092 .baseArrayLayer = range->baseArrayLayer + layer,
1093 .layerCount = 1
1094 },
1095 });
1096
1097 VkFramebuffer fb;
1098 radv_CreateFramebuffer(device_h,
1099 &(VkFramebufferCreateInfo) {
1100 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1101 .attachmentCount = 1,
1102 .pAttachments = (VkImageView[]) {
1103 radv_image_view_to_handle(&iview),
1104 },
1105 .width = iview.extent.width,
1106 .height = iview.extent.height,
1107 .layers = 1
1108 },
1109 &cmd_buffer->pool->alloc,
1110 &fb);
1111
1112 VkAttachmentDescription att_desc = {
1113 .format = iview.vk_format,
1114 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
1115 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1116 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
1117 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE,
1118 .initialLayout = image_layout,
1119 .finalLayout = image_layout,
1120 };
1121
1122 VkSubpassDescription subpass_desc = {
1123 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1124 .inputAttachmentCount = 0,
1125 .colorAttachmentCount = 0,
1126 .pColorAttachments = NULL,
1127 .pResolveAttachments = NULL,
1128 .pDepthStencilAttachment = NULL,
1129 .preserveAttachmentCount = 0,
1130 .pPreserveAttachments = NULL,
1131 };
1132
1133 const VkAttachmentReference att_ref = {
1134 .attachment = 0,
1135 .layout = image_layout,
1136 };
1137
1138 if (range->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
1139 subpass_desc.colorAttachmentCount = 1;
1140 subpass_desc.pColorAttachments = &att_ref;
1141 } else {
1142 subpass_desc.pDepthStencilAttachment = &att_ref;
1143 }
1144
1145 VkRenderPass pass;
1146 radv_CreateRenderPass(device_h,
1147 &(VkRenderPassCreateInfo) {
1148 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1149 .attachmentCount = 1,
1150 .pAttachments = &att_desc,
1151 .subpassCount = 1,
1152 .pSubpasses = &subpass_desc,
1153 },
1154 &cmd_buffer->pool->alloc,
1155 &pass);
1156
1157 radv_CmdBeginRenderPass(radv_cmd_buffer_to_handle(cmd_buffer),
1158 &(VkRenderPassBeginInfo) {
1159 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
1160 .renderArea = {
1161 .offset = { 0, 0, },
1162 .extent = {
1163 .width = iview.extent.width,
1164 .height = iview.extent.height,
1165 },
1166 },
1167 .renderPass = pass,
1168 .framebuffer = fb,
1169 .clearValueCount = 0,
1170 .pClearValues = NULL,
1171 },
1172 VK_SUBPASS_CONTENTS_INLINE);
1173
1174 VkClearAttachment clear_att = {
1175 .aspectMask = range->aspectMask,
1176 .colorAttachment = 0,
1177 .clearValue = *clear_val,
1178 };
1179
1180 VkClearRect clear_rect = {
1181 .rect = {
1182 .offset = { 0, 0 },
1183 .extent = { iview.extent.width, iview.extent.height },
1184 },
1185 .baseArrayLayer = range->baseArrayLayer,
1186 .layerCount = 1, /* FINISHME: clear multi-layer framebuffer */
1187 };
1188
1189 emit_clear(cmd_buffer, &clear_att, &clear_rect, NULL, NULL);
1190
1191 radv_CmdEndRenderPass(radv_cmd_buffer_to_handle(cmd_buffer));
1192 radv_DestroyRenderPass(device_h, pass,
1193 &cmd_buffer->pool->alloc);
1194 radv_DestroyFramebuffer(device_h, fb,
1195 &cmd_buffer->pool->alloc);
1196 }
1197 static void
1198 radv_cmd_clear_image(struct radv_cmd_buffer *cmd_buffer,
1199 struct radv_image *image,
1200 VkImageLayout image_layout,
1201 const VkClearValue *clear_value,
1202 uint32_t range_count,
1203 const VkImageSubresourceRange *ranges,
1204 bool cs)
1205 {
1206 VkFormat format = image->vk_format;
1207 VkClearValue internal_clear_value = *clear_value;
1208
1209 if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
1210 uint32_t value;
1211 format = VK_FORMAT_R32_UINT;
1212 value = float3_to_rgb9e5(clear_value->color.float32);
1213 internal_clear_value.color.uint32[0] = value;
1214 }
1215
1216 if (format == VK_FORMAT_R4G4_UNORM_PACK8) {
1217 uint8_t r, g;
1218 format = VK_FORMAT_R8_UINT;
1219 r = float_to_ubyte(clear_value->color.float32[0]) >> 4;
1220 g = float_to_ubyte(clear_value->color.float32[1]) >> 4;
1221 internal_clear_value.color.uint32[0] = (r << 4) | (g & 0xf);
1222 }
1223
1224 for (uint32_t r = 0; r < range_count; r++) {
1225 const VkImageSubresourceRange *range = &ranges[r];
1226 for (uint32_t l = 0; l < radv_get_levelCount(image, range); ++l) {
1227 const uint32_t layer_count = image->type == VK_IMAGE_TYPE_3D ?
1228 radv_minify(image->info.depth, range->baseMipLevel + l) :
1229 radv_get_layerCount(image, range);
1230 for (uint32_t s = 0; s < layer_count; ++s) {
1231
1232 if (cs) {
1233 struct radv_meta_blit2d_surf surf;
1234 surf.format = format;
1235 surf.image = image;
1236 surf.level = range->baseMipLevel + l;
1237 surf.layer = range->baseArrayLayer + s;
1238 surf.aspect_mask = range->aspectMask;
1239 radv_meta_clear_image_cs(cmd_buffer, &surf,
1240 &internal_clear_value.color);
1241 } else {
1242 radv_clear_image_layer(cmd_buffer, image, image_layout,
1243 range, format, l, s, &internal_clear_value);
1244 }
1245 }
1246 }
1247 }
1248 }
1249
1250 union meta_saved_state {
1251 struct radv_meta_saved_state gfx;
1252 struct radv_meta_saved_compute_state compute;
1253 };
1254
1255 void radv_CmdClearColorImage(
1256 VkCommandBuffer commandBuffer,
1257 VkImage image_h,
1258 VkImageLayout imageLayout,
1259 const VkClearColorValue* pColor,
1260 uint32_t rangeCount,
1261 const VkImageSubresourceRange* pRanges)
1262 {
1263 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
1264 RADV_FROM_HANDLE(radv_image, image, image_h);
1265 union meta_saved_state saved_state;
1266 bool cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE;
1267
1268 if (cs)
1269 radv_meta_begin_cleari(cmd_buffer, &saved_state.compute);
1270 else
1271 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state.gfx, cmd_buffer);
1272
1273 radv_cmd_clear_image(cmd_buffer, image, imageLayout,
1274 (const VkClearValue *) pColor,
1275 rangeCount, pRanges, cs);
1276
1277 if (cs)
1278 radv_meta_end_cleari(cmd_buffer, &saved_state.compute);
1279 else
1280 radv_meta_restore(&saved_state.gfx, cmd_buffer);
1281 }
1282
1283 void radv_CmdClearDepthStencilImage(
1284 VkCommandBuffer commandBuffer,
1285 VkImage image_h,
1286 VkImageLayout imageLayout,
1287 const VkClearDepthStencilValue* pDepthStencil,
1288 uint32_t rangeCount,
1289 const VkImageSubresourceRange* pRanges)
1290 {
1291 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
1292 RADV_FROM_HANDLE(radv_image, image, image_h);
1293 struct radv_meta_saved_state saved_state;
1294
1295 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state, cmd_buffer);
1296
1297 radv_cmd_clear_image(cmd_buffer, image, imageLayout,
1298 (const VkClearValue *) pDepthStencil,
1299 rangeCount, pRanges, false);
1300
1301 radv_meta_restore(&saved_state, cmd_buffer);
1302 }
1303
1304 void radv_CmdClearAttachments(
1305 VkCommandBuffer commandBuffer,
1306 uint32_t attachmentCount,
1307 const VkClearAttachment* pAttachments,
1308 uint32_t rectCount,
1309 const VkClearRect* pRects)
1310 {
1311 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
1312 struct radv_meta_saved_state saved_state;
1313 enum radv_cmd_flush_bits pre_flush = 0;
1314 enum radv_cmd_flush_bits post_flush = 0;
1315
1316 if (!cmd_buffer->state.subpass)
1317 return;
1318
1319 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state, cmd_buffer);
1320
1321 /* FINISHME: We can do better than this dumb loop. It thrashes too much
1322 * state.
1323 */
1324 for (uint32_t a = 0; a < attachmentCount; ++a) {
1325 for (uint32_t r = 0; r < rectCount; ++r) {
1326 emit_clear(cmd_buffer, &pAttachments[a], &pRects[r], &pre_flush, &post_flush);
1327 }
1328 }
1329
1330 radv_meta_restore(&saved_state, cmd_buffer);
1331 cmd_buffer->state.flush_bits |= post_flush;
1332 }