glthread: don't prefix variable_data with const
[mesa.git] / src / amd / vulkan / radv_meta_fast_clear.c
1 /*
2 * Copyright © 2016 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 <assert.h>
25 #include <stdbool.h>
26
27 #include "radv_meta.h"
28 #include "radv_private.h"
29 #include "sid.h"
30
31
32 static nir_shader *
33 build_dcc_decompress_compute_shader(struct radv_device *dev)
34 {
35 nir_builder b;
36 const struct glsl_type *buf_type = glsl_sampler_type(GLSL_SAMPLER_DIM_2D,
37 false,
38 false,
39 GLSL_TYPE_FLOAT);
40 const struct glsl_type *img_type = glsl_sampler_type(GLSL_SAMPLER_DIM_2D,
41 false,
42 false,
43 GLSL_TYPE_FLOAT);
44 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_COMPUTE, NULL);
45 b.shader->info.name = ralloc_strdup(b.shader, "dcc_decompress_compute");
46
47 /* We need at least 16/16/1 to cover an entire DCC block in a single workgroup. */
48 b.shader->info.cs.local_size[0] = 16;
49 b.shader->info.cs.local_size[1] = 16;
50 b.shader->info.cs.local_size[2] = 1;
51 nir_variable *input_img = nir_variable_create(b.shader, nir_var_uniform,
52 buf_type, "s_tex");
53 input_img->data.descriptor_set = 0;
54 input_img->data.binding = 0;
55
56 nir_variable *output_img = nir_variable_create(b.shader, nir_var_uniform,
57 img_type, "out_img");
58 output_img->data.descriptor_set = 0;
59 output_img->data.binding = 1;
60
61 nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);
62 nir_ssa_def *wg_id = nir_load_work_group_id(&b);
63 nir_ssa_def *block_size = nir_imm_ivec4(&b,
64 b.shader->info.cs.local_size[0],
65 b.shader->info.cs.local_size[1],
66 b.shader->info.cs.local_size[2], 0);
67
68 nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);
69 nir_ssa_def *input_img_deref = &nir_build_deref_var(&b, input_img)->dest.ssa;
70
71 nir_tex_instr *tex = nir_tex_instr_create(b.shader, 3);
72 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
73 tex->op = nir_texop_txf;
74 tex->src[0].src_type = nir_tex_src_coord;
75 tex->src[0].src = nir_src_for_ssa(nir_channels(&b, global_id, 3));
76 tex->src[1].src_type = nir_tex_src_lod;
77 tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));
78 tex->src[2].src_type = nir_tex_src_texture_deref;
79 tex->src[2].src = nir_src_for_ssa(input_img_deref);
80 tex->dest_type = nir_type_float;
81 tex->is_array = false;
82 tex->coord_components = 2;
83
84 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");
85 nir_builder_instr_insert(&b, &tex->instr);
86
87 nir_intrinsic_instr *membar = nir_intrinsic_instr_create(b.shader, nir_intrinsic_memory_barrier);
88 nir_builder_instr_insert(&b, &membar->instr);
89
90 nir_intrinsic_instr *bar = nir_intrinsic_instr_create(b.shader, nir_intrinsic_control_barrier);
91 nir_builder_instr_insert(&b, &bar->instr);
92
93 nir_ssa_def *outval = &tex->dest.ssa;
94 nir_intrinsic_instr *store = nir_intrinsic_instr_create(b.shader, nir_intrinsic_image_deref_store);
95 store->num_components = 4;
96 store->src[0] = nir_src_for_ssa(&nir_build_deref_var(&b, output_img)->dest.ssa);
97 store->src[1] = nir_src_for_ssa(global_id);
98 store->src[2] = nir_src_for_ssa(nir_ssa_undef(&b, 1, 32));
99 store->src[3] = nir_src_for_ssa(outval);
100 store->src[4] = nir_src_for_ssa(nir_imm_int(&b, 0));
101
102 nir_builder_instr_insert(&b, &store->instr);
103 return b.shader;
104 }
105
106 static VkResult
107 create_dcc_compress_compute(struct radv_device *device)
108 {
109 VkResult result = VK_SUCCESS;
110 struct radv_shader_module cs = { .nir = NULL };
111
112 cs.nir = build_dcc_decompress_compute_shader(device);
113
114 VkDescriptorSetLayoutCreateInfo ds_create_info = {
115 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
116 .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,
117 .bindingCount = 2,
118 .pBindings = (VkDescriptorSetLayoutBinding[]) {
119 {
120 .binding = 0,
121 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
122 .descriptorCount = 1,
123 .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
124 .pImmutableSamplers = NULL
125 },
126 {
127 .binding = 1,
128 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
129 .descriptorCount = 1,
130 .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
131 .pImmutableSamplers = NULL
132 },
133 }
134 };
135
136 result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device),
137 &ds_create_info,
138 &device->meta_state.alloc,
139 &device->meta_state.fast_clear_flush.dcc_decompress_compute_ds_layout);
140 if (result != VK_SUCCESS)
141 goto cleanup;
142
143
144 VkPipelineLayoutCreateInfo pl_create_info = {
145 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
146 .setLayoutCount = 1,
147 .pSetLayouts = &device->meta_state.fast_clear_flush.dcc_decompress_compute_ds_layout,
148 .pushConstantRangeCount = 1,
149 .pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_COMPUTE_BIT, 0, 8},
150 };
151
152 result = radv_CreatePipelineLayout(radv_device_to_handle(device),
153 &pl_create_info,
154 &device->meta_state.alloc,
155 &device->meta_state.fast_clear_flush.dcc_decompress_compute_p_layout);
156 if (result != VK_SUCCESS)
157 goto cleanup;
158
159 /* compute shader */
160
161 VkPipelineShaderStageCreateInfo pipeline_shader_stage = {
162 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
163 .stage = VK_SHADER_STAGE_COMPUTE_BIT,
164 .module = radv_shader_module_to_handle(&cs),
165 .pName = "main",
166 .pSpecializationInfo = NULL,
167 };
168
169 VkComputePipelineCreateInfo vk_pipeline_info = {
170 .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
171 .stage = pipeline_shader_stage,
172 .flags = 0,
173 .layout = device->meta_state.fast_clear_flush.dcc_decompress_compute_p_layout,
174 };
175
176 result = radv_CreateComputePipelines(radv_device_to_handle(device),
177 radv_pipeline_cache_to_handle(&device->meta_state.cache),
178 1, &vk_pipeline_info, NULL,
179 &device->meta_state.fast_clear_flush.dcc_decompress_compute_pipeline);
180 if (result != VK_SUCCESS)
181 goto cleanup;
182
183 cleanup:
184 ralloc_free(cs.nir);
185 return result;
186 }
187
188 static VkResult
189 create_pass(struct radv_device *device)
190 {
191 VkResult result;
192 VkDevice device_h = radv_device_to_handle(device);
193 const VkAllocationCallbacks *alloc = &device->meta_state.alloc;
194 VkAttachmentDescription attachment;
195
196 attachment.format = VK_FORMAT_UNDEFINED;
197 attachment.samples = 1;
198 attachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
199 attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
200 attachment.initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
201 attachment.finalLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
202
203 result = radv_CreateRenderPass(device_h,
204 &(VkRenderPassCreateInfo) {
205 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
206 .attachmentCount = 1,
207 .pAttachments = &attachment,
208 .subpassCount = 1,
209 .pSubpasses = &(VkSubpassDescription) {
210 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
211 .inputAttachmentCount = 0,
212 .colorAttachmentCount = 1,
213 .pColorAttachments = (VkAttachmentReference[]) {
214 {
215 .attachment = 0,
216 .layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
217 },
218 },
219 .pResolveAttachments = NULL,
220 .pDepthStencilAttachment = &(VkAttachmentReference) {
221 .attachment = VK_ATTACHMENT_UNUSED,
222 },
223 .preserveAttachmentCount = 0,
224 .pPreserveAttachments = NULL,
225 },
226 .dependencyCount = 2,
227 .pDependencies = (VkSubpassDependency[]) {
228 {
229 .srcSubpass = VK_SUBPASS_EXTERNAL,
230 .dstSubpass = 0,
231 .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
232 .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
233 .srcAccessMask = 0,
234 .dstAccessMask = 0,
235 .dependencyFlags = 0
236 },
237 {
238 .srcSubpass = 0,
239 .dstSubpass = VK_SUBPASS_EXTERNAL,
240 .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
241 .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
242 .srcAccessMask = 0,
243 .dstAccessMask = 0,
244 .dependencyFlags = 0
245 }
246 },
247 },
248 alloc,
249 &device->meta_state.fast_clear_flush.pass);
250
251 return result;
252 }
253
254 static VkResult
255 create_pipeline_layout(struct radv_device *device, VkPipelineLayout *layout)
256 {
257 VkPipelineLayoutCreateInfo pl_create_info = {
258 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
259 .setLayoutCount = 0,
260 .pSetLayouts = NULL,
261 .pushConstantRangeCount = 0,
262 .pPushConstantRanges = NULL,
263 };
264
265 return radv_CreatePipelineLayout(radv_device_to_handle(device),
266 &pl_create_info,
267 &device->meta_state.alloc,
268 layout);
269 }
270
271 static VkResult
272 create_pipeline(struct radv_device *device,
273 VkShaderModule vs_module_h,
274 VkPipelineLayout layout)
275 {
276 VkResult result;
277 VkDevice device_h = radv_device_to_handle(device);
278
279 struct radv_shader_module fs_module = {
280 .nir = radv_meta_build_nir_fs_noop(),
281 };
282
283 if (!fs_module.nir) {
284 /* XXX: Need more accurate error */
285 result = VK_ERROR_OUT_OF_HOST_MEMORY;
286 goto cleanup;
287 }
288
289 const VkPipelineShaderStageCreateInfo stages[2] = {
290 {
291 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
292 .stage = VK_SHADER_STAGE_VERTEX_BIT,
293 .module = vs_module_h,
294 .pName = "main",
295 },
296 {
297 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
298 .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
299 .module = radv_shader_module_to_handle(&fs_module),
300 .pName = "main",
301 },
302 };
303
304 const VkPipelineVertexInputStateCreateInfo vi_state = {
305 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
306 .vertexBindingDescriptionCount = 0,
307 .vertexAttributeDescriptionCount = 0,
308 };
309
310 const VkPipelineInputAssemblyStateCreateInfo ia_state = {
311 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
312 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
313 .primitiveRestartEnable = false,
314 };
315
316 const VkPipelineColorBlendStateCreateInfo blend_state = {
317 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
318 .logicOpEnable = false,
319 .attachmentCount = 1,
320 .pAttachments = (VkPipelineColorBlendAttachmentState []) {
321 {
322 .colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
323 VK_COLOR_COMPONENT_G_BIT |
324 VK_COLOR_COMPONENT_B_BIT |
325 VK_COLOR_COMPONENT_A_BIT,
326 },
327 }
328 };
329 const VkPipelineRasterizationStateCreateInfo rs_state = {
330 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
331 .depthClampEnable = false,
332 .rasterizerDiscardEnable = false,
333 .polygonMode = VK_POLYGON_MODE_FILL,
334 .cullMode = VK_CULL_MODE_NONE,
335 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
336 };
337
338 result = radv_graphics_pipeline_create(device_h,
339 radv_pipeline_cache_to_handle(&device->meta_state.cache),
340 &(VkGraphicsPipelineCreateInfo) {
341 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
342 .stageCount = 2,
343 .pStages = stages,
344
345 .pVertexInputState = &vi_state,
346 .pInputAssemblyState = &ia_state,
347
348 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
349 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
350 .viewportCount = 1,
351 .scissorCount = 1,
352 },
353 .pRasterizationState = &rs_state,
354 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
355 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
356 .rasterizationSamples = 1,
357 .sampleShadingEnable = false,
358 .pSampleMask = NULL,
359 .alphaToCoverageEnable = false,
360 .alphaToOneEnable = false,
361 },
362 .pColorBlendState = &blend_state,
363 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
364 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
365 .dynamicStateCount = 2,
366 .pDynamicStates = (VkDynamicState[]) {
367 VK_DYNAMIC_STATE_VIEWPORT,
368 VK_DYNAMIC_STATE_SCISSOR,
369 },
370 },
371 .layout = layout,
372 .renderPass = device->meta_state.fast_clear_flush.pass,
373 .subpass = 0,
374 },
375 &(struct radv_graphics_pipeline_create_info) {
376 .use_rectlist = true,
377 .custom_blend_mode = V_028808_CB_ELIMINATE_FAST_CLEAR,
378 },
379 &device->meta_state.alloc,
380 &device->meta_state.fast_clear_flush.cmask_eliminate_pipeline);
381 if (result != VK_SUCCESS)
382 goto cleanup;
383
384 result = radv_graphics_pipeline_create(device_h,
385 radv_pipeline_cache_to_handle(&device->meta_state.cache),
386 &(VkGraphicsPipelineCreateInfo) {
387 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
388 .stageCount = 2,
389 .pStages = stages,
390
391 .pVertexInputState = &vi_state,
392 .pInputAssemblyState = &ia_state,
393
394 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
395 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
396 .viewportCount = 1,
397 .scissorCount = 1,
398 },
399 .pRasterizationState = &rs_state,
400 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
401 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
402 .rasterizationSamples = 1,
403 .sampleShadingEnable = false,
404 .pSampleMask = NULL,
405 .alphaToCoverageEnable = false,
406 .alphaToOneEnable = false,
407 },
408 .pColorBlendState = &blend_state,
409 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
410 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
411 .dynamicStateCount = 2,
412 .pDynamicStates = (VkDynamicState[]) {
413 VK_DYNAMIC_STATE_VIEWPORT,
414 VK_DYNAMIC_STATE_SCISSOR,
415 },
416 },
417 .layout = layout,
418 .renderPass = device->meta_state.fast_clear_flush.pass,
419 .subpass = 0,
420 },
421 &(struct radv_graphics_pipeline_create_info) {
422 .use_rectlist = true,
423 .custom_blend_mode = V_028808_CB_FMASK_DECOMPRESS,
424 },
425 &device->meta_state.alloc,
426 &device->meta_state.fast_clear_flush.fmask_decompress_pipeline);
427 if (result != VK_SUCCESS)
428 goto cleanup;
429
430 result = radv_graphics_pipeline_create(device_h,
431 radv_pipeline_cache_to_handle(&device->meta_state.cache),
432 &(VkGraphicsPipelineCreateInfo) {
433 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
434 .stageCount = 2,
435 .pStages = stages,
436
437 .pVertexInputState = &vi_state,
438 .pInputAssemblyState = &ia_state,
439
440 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
441 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
442 .viewportCount = 1,
443 .scissorCount = 1,
444 },
445 .pRasterizationState = &rs_state,
446 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
447 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
448 .rasterizationSamples = 1,
449 .sampleShadingEnable = false,
450 .pSampleMask = NULL,
451 .alphaToCoverageEnable = false,
452 .alphaToOneEnable = false,
453 },
454 .pColorBlendState = &blend_state,
455 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
456 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
457 .dynamicStateCount = 2,
458 .pDynamicStates = (VkDynamicState[]) {
459 VK_DYNAMIC_STATE_VIEWPORT,
460 VK_DYNAMIC_STATE_SCISSOR,
461 },
462 },
463 .layout = layout,
464 .renderPass = device->meta_state.fast_clear_flush.pass,
465 .subpass = 0,
466 },
467 &(struct radv_graphics_pipeline_create_info) {
468 .use_rectlist = true,
469 .custom_blend_mode = V_028808_CB_DCC_DECOMPRESS,
470 },
471 &device->meta_state.alloc,
472 &device->meta_state.fast_clear_flush.dcc_decompress_pipeline);
473 if (result != VK_SUCCESS)
474 goto cleanup;
475
476 goto cleanup;
477
478 cleanup:
479 ralloc_free(fs_module.nir);
480 return result;
481 }
482
483 void
484 radv_device_finish_meta_fast_clear_flush_state(struct radv_device *device)
485 {
486 struct radv_meta_state *state = &device->meta_state;
487
488 radv_DestroyPipeline(radv_device_to_handle(device),
489 state->fast_clear_flush.dcc_decompress_pipeline,
490 &state->alloc);
491 radv_DestroyPipeline(radv_device_to_handle(device),
492 state->fast_clear_flush.fmask_decompress_pipeline,
493 &state->alloc);
494 radv_DestroyPipeline(radv_device_to_handle(device),
495 state->fast_clear_flush.cmask_eliminate_pipeline,
496 &state->alloc);
497 radv_DestroyRenderPass(radv_device_to_handle(device),
498 state->fast_clear_flush.pass, &state->alloc);
499 radv_DestroyPipelineLayout(radv_device_to_handle(device),
500 state->fast_clear_flush.p_layout,
501 &state->alloc);
502
503 radv_DestroyPipeline(radv_device_to_handle(device),
504 state->fast_clear_flush.dcc_decompress_compute_pipeline,
505 &state->alloc);
506 radv_DestroyPipelineLayout(radv_device_to_handle(device),
507 state->fast_clear_flush.dcc_decompress_compute_p_layout,
508 &state->alloc);
509 radv_DestroyDescriptorSetLayout(radv_device_to_handle(device),
510 state->fast_clear_flush.dcc_decompress_compute_ds_layout,
511 &state->alloc);
512 }
513
514 static VkResult
515 radv_device_init_meta_fast_clear_flush_state_internal(struct radv_device *device)
516 {
517 VkResult res = VK_SUCCESS;
518
519 mtx_lock(&device->meta_state.mtx);
520 if (device->meta_state.fast_clear_flush.cmask_eliminate_pipeline) {
521 mtx_unlock(&device->meta_state.mtx);
522 return VK_SUCCESS;
523 }
524
525 struct radv_shader_module vs_module = { .nir = radv_meta_build_nir_vs_generate_vertices() };
526 if (!vs_module.nir) {
527 /* XXX: Need more accurate error */
528 res = VK_ERROR_OUT_OF_HOST_MEMORY;
529 goto fail;
530 }
531
532 res = create_pass(device);
533 if (res != VK_SUCCESS)
534 goto fail;
535
536 res = create_pipeline_layout(device,
537 &device->meta_state.fast_clear_flush.p_layout);
538 if (res != VK_SUCCESS)
539 goto fail;
540
541 VkShaderModule vs_module_h = radv_shader_module_to_handle(&vs_module);
542 res = create_pipeline(device, vs_module_h,
543 device->meta_state.fast_clear_flush.p_layout);
544 if (res != VK_SUCCESS)
545 goto fail;
546
547 res = create_dcc_compress_compute(device);
548 if (res != VK_SUCCESS)
549 goto fail;
550
551 goto cleanup;
552
553 fail:
554 radv_device_finish_meta_fast_clear_flush_state(device);
555
556 cleanup:
557 ralloc_free(vs_module.nir);
558 mtx_unlock(&device->meta_state.mtx);
559
560 return res;
561 }
562
563
564 VkResult
565 radv_device_init_meta_fast_clear_flush_state(struct radv_device *device, bool on_demand)
566 {
567 if (on_demand)
568 return VK_SUCCESS;
569
570 return radv_device_init_meta_fast_clear_flush_state_internal(device);
571 }
572
573 static void
574 radv_emit_set_predication_state_from_image(struct radv_cmd_buffer *cmd_buffer,
575 struct radv_image *image,
576 uint64_t pred_offset, bool value)
577 {
578 uint64_t va = 0;
579
580 if (value) {
581 va = radv_buffer_get_va(image->bo) + image->offset;
582 va += pred_offset;
583 }
584
585 si_emit_set_predication_state(cmd_buffer, true, va);
586 }
587
588 static void
589 radv_process_color_image_layer(struct radv_cmd_buffer *cmd_buffer,
590 struct radv_image *image,
591 const VkImageSubresourceRange *range,
592 int level, int layer)
593 {
594 struct radv_device *device = cmd_buffer->device;
595 struct radv_image_view iview;
596 uint32_t width, height;
597
598 width = radv_minify(image->info.width, range->baseMipLevel + level);
599 height = radv_minify(image->info.height, range->baseMipLevel + level);
600
601 radv_image_view_init(&iview, device,
602 &(VkImageViewCreateInfo) {
603 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
604 .image = radv_image_to_handle(image),
605 .viewType = radv_meta_get_view_type(image),
606 .format = image->vk_format,
607 .subresourceRange = {
608 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
609 .baseMipLevel = range->baseMipLevel + level,
610 .levelCount = 1,
611 .baseArrayLayer = range->baseArrayLayer + layer,
612 .layerCount = 1,
613 },
614 }, NULL);
615
616 VkFramebuffer fb_h;
617 radv_CreateFramebuffer(radv_device_to_handle(device),
618 &(VkFramebufferCreateInfo) {
619 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
620 .attachmentCount = 1,
621 .pAttachments = (VkImageView[]) {
622 radv_image_view_to_handle(&iview)
623 },
624 .width = width,
625 .height = height,
626 .layers = 1
627 }, &cmd_buffer->pool->alloc, &fb_h);
628
629 radv_CmdBeginRenderPass(radv_cmd_buffer_to_handle(cmd_buffer),
630 &(VkRenderPassBeginInfo) {
631 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
632 .renderPass = device->meta_state.fast_clear_flush.pass,
633 .framebuffer = fb_h,
634 .renderArea = {
635 .offset = {
636 0,
637 0,
638 },
639 .extent = {
640 width,
641 height,
642 }
643 },
644 .clearValueCount = 0,
645 .pClearValues = NULL,
646 }, VK_SUBPASS_CONTENTS_INLINE);
647
648 radv_CmdDraw(radv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
649
650 cmd_buffer->state.flush_bits |= RADV_CMD_FLAG_FLUSH_AND_INV_CB |
651 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META;
652
653 radv_CmdEndRenderPass(radv_cmd_buffer_to_handle(cmd_buffer));
654
655 radv_DestroyFramebuffer(radv_device_to_handle(device), fb_h,
656 &cmd_buffer->pool->alloc);
657 }
658
659 static void
660 radv_process_color_image(struct radv_cmd_buffer *cmd_buffer,
661 struct radv_image *image,
662 const VkImageSubresourceRange *subresourceRange,
663 bool decompress_dcc)
664 {
665 struct radv_meta_saved_state saved_state;
666 VkPipeline *pipeline;
667
668 if (decompress_dcc && radv_dcc_enabled(image, subresourceRange->baseMipLevel)) {
669 pipeline = &cmd_buffer->device->meta_state.fast_clear_flush.dcc_decompress_pipeline;
670 } else if (radv_image_has_fmask(image) && !image->tc_compatible_cmask) {
671 pipeline = &cmd_buffer->device->meta_state.fast_clear_flush.fmask_decompress_pipeline;
672 } else {
673 pipeline = &cmd_buffer->device->meta_state.fast_clear_flush.cmask_eliminate_pipeline;
674 }
675
676 if (!*pipeline) {
677 VkResult ret;
678
679 ret = radv_device_init_meta_fast_clear_flush_state_internal(cmd_buffer->device);
680 if (ret != VK_SUCCESS) {
681 cmd_buffer->record_result = ret;
682 return;
683 }
684 }
685
686 radv_meta_save(&saved_state, cmd_buffer,
687 RADV_META_SAVE_GRAPHICS_PIPELINE |
688 RADV_META_SAVE_PASS);
689
690 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer),
691 VK_PIPELINE_BIND_POINT_GRAPHICS, *pipeline);
692
693 for (uint32_t l = 0; l < radv_get_levelCount(image, subresourceRange); ++l) {
694 uint32_t width, height;
695
696 /* Do not decompress levels without DCC. */
697 if (decompress_dcc &&
698 !radv_dcc_enabled(image, subresourceRange->baseMipLevel + l))
699 continue;
700
701 width = radv_minify(image->info.width,
702 subresourceRange->baseMipLevel + l);
703 height = radv_minify(image->info.height,
704 subresourceRange->baseMipLevel + l);
705
706 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer), 0, 1,
707 &(VkViewport) {
708 .x = 0,
709 .y = 0,
710 .width = width,
711 .height = height,
712 .minDepth = 0.0f,
713 .maxDepth = 1.0f
714 });
715
716 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer), 0, 1,
717 &(VkRect2D) {
718 .offset = { 0, 0 },
719 .extent = { width, height },
720 });
721
722 for (uint32_t s = 0; s < radv_get_layerCount(image, subresourceRange); s++) {
723 radv_process_color_image_layer(cmd_buffer, image,
724 subresourceRange, l, s);
725 }
726 }
727
728 radv_meta_restore(&saved_state, cmd_buffer);
729 }
730
731 static void
732 radv_emit_color_decompress(struct radv_cmd_buffer *cmd_buffer,
733 struct radv_image *image,
734 const VkImageSubresourceRange *subresourceRange,
735 bool decompress_dcc)
736 {
737 bool old_predicating = false;
738
739 assert(cmd_buffer->queue_family_index == RADV_QUEUE_GENERAL);
740
741 if (radv_dcc_enabled(image, subresourceRange->baseMipLevel)) {
742 uint64_t pred_offset = decompress_dcc ? image->dcc_pred_offset :
743 image->fce_pred_offset;
744 pred_offset += 8 * subresourceRange->baseMipLevel;
745
746 old_predicating = cmd_buffer->state.predicating;
747
748 radv_emit_set_predication_state_from_image(cmd_buffer, image, pred_offset, true);
749 cmd_buffer->state.predicating = true;
750 }
751
752 radv_process_color_image(cmd_buffer, image, subresourceRange,
753 decompress_dcc);
754
755 if (radv_dcc_enabled(image, subresourceRange->baseMipLevel)) {
756 uint64_t pred_offset = decompress_dcc ? image->dcc_pred_offset :
757 image->fce_pred_offset;
758 pred_offset += 8 * subresourceRange->baseMipLevel;
759
760 cmd_buffer->state.predicating = old_predicating;
761
762 radv_emit_set_predication_state_from_image(cmd_buffer, image, pred_offset, false);
763
764 if (cmd_buffer->state.predication_type != -1) {
765 /* Restore previous conditional rendering user state. */
766 si_emit_set_predication_state(cmd_buffer,
767 cmd_buffer->state.predication_type,
768 cmd_buffer->state.predication_va);
769 }
770 }
771
772 if (radv_dcc_enabled(image, subresourceRange->baseMipLevel)) {
773 /* Clear the image's fast-clear eliminate predicate because
774 * FMASK and DCC also imply a fast-clear eliminate.
775 */
776 radv_update_fce_metadata(cmd_buffer, image, subresourceRange, false);
777
778 /* Mark the image as being decompressed. */
779 if (decompress_dcc)
780 radv_update_dcc_metadata(cmd_buffer, image, subresourceRange, false);
781 }
782 }
783
784 void
785 radv_fast_clear_flush_image_inplace(struct radv_cmd_buffer *cmd_buffer,
786 struct radv_image *image,
787 const VkImageSubresourceRange *subresourceRange)
788 {
789 radv_emit_color_decompress(cmd_buffer, image, subresourceRange, false);
790 }
791
792 static void
793 radv_decompress_dcc_gfx(struct radv_cmd_buffer *cmd_buffer,
794 struct radv_image *image,
795 const VkImageSubresourceRange *subresourceRange)
796 {
797 radv_emit_color_decompress(cmd_buffer, image, subresourceRange, true);
798 }
799
800 static void
801 radv_decompress_dcc_compute(struct radv_cmd_buffer *cmd_buffer,
802 struct radv_image *image,
803 const VkImageSubresourceRange *subresourceRange)
804 {
805 struct radv_meta_saved_state saved_state;
806 struct radv_image_view load_iview = {0};
807 struct radv_image_view store_iview = {0};
808 struct radv_device *device = cmd_buffer->device;
809
810 /* This assumes the image is 2d with 1 layer */
811 struct radv_cmd_state *state = &cmd_buffer->state;
812
813 state->flush_bits |= RADV_CMD_FLAG_FLUSH_AND_INV_CB |
814 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META;
815
816 if (!cmd_buffer->device->meta_state.fast_clear_flush.cmask_eliminate_pipeline) {
817 VkResult ret = radv_device_init_meta_fast_clear_flush_state_internal(cmd_buffer->device);
818 if (ret != VK_SUCCESS) {
819 cmd_buffer->record_result = ret;
820 return;
821 }
822 }
823
824 radv_meta_save(&saved_state, cmd_buffer, RADV_META_SAVE_DESCRIPTORS |
825 RADV_META_SAVE_COMPUTE_PIPELINE);
826
827 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer),
828 VK_PIPELINE_BIND_POINT_COMPUTE,
829 device->meta_state.fast_clear_flush.dcc_decompress_compute_pipeline);
830
831 for (uint32_t l = 0; l < radv_get_levelCount(image, subresourceRange); l++) {
832 uint32_t width, height;
833
834 /* Do not decompress levels without DCC. */
835 if (!radv_dcc_enabled(image, subresourceRange->baseMipLevel + l))
836 continue;
837
838 width = radv_minify(image->info.width,
839 subresourceRange->baseMipLevel + l);
840 height = radv_minify(image->info.height,
841 subresourceRange->baseMipLevel + l);
842
843 for (uint32_t s = 0; s < radv_get_layerCount(image, subresourceRange); s++) {
844 radv_image_view_init(&load_iview, cmd_buffer->device,
845 &(VkImageViewCreateInfo) {
846 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
847 .image = radv_image_to_handle(image),
848 .viewType = VK_IMAGE_VIEW_TYPE_2D,
849 .format = image->vk_format,
850 .subresourceRange = {
851 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
852 .baseMipLevel = subresourceRange->baseMipLevel + l,
853 .levelCount = 1,
854 .baseArrayLayer = subresourceRange->baseArrayLayer + s,
855 .layerCount = 1
856 },
857 }, NULL);
858 radv_image_view_init(&store_iview, cmd_buffer->device,
859 &(VkImageViewCreateInfo) {
860 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
861 .image = radv_image_to_handle(image),
862 .viewType = VK_IMAGE_VIEW_TYPE_2D,
863 .format = image->vk_format,
864 .subresourceRange = {
865 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
866 .baseMipLevel = subresourceRange->baseMipLevel + l,
867 .levelCount = 1,
868 .baseArrayLayer = subresourceRange->baseArrayLayer + s,
869 .layerCount = 1
870 },
871 }, &(struct radv_image_view_extra_create_info) {
872 .disable_compression = true
873 });
874
875 radv_meta_push_descriptor_set(cmd_buffer,
876 VK_PIPELINE_BIND_POINT_COMPUTE,
877 device->meta_state.fast_clear_flush.dcc_decompress_compute_p_layout,
878 0, /* set */
879 2, /* descriptorWriteCount */
880 (VkWriteDescriptorSet[]) {
881 {
882 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
883 .dstBinding = 0,
884 .dstArrayElement = 0,
885 .descriptorCount = 1,
886 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
887 .pImageInfo = (VkDescriptorImageInfo[]) {
888 {
889 .sampler = VK_NULL_HANDLE,
890 .imageView = radv_image_view_to_handle(&load_iview),
891 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
892 },
893 }
894 },
895 {
896 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
897 .dstBinding = 1,
898 .dstArrayElement = 0,
899 .descriptorCount = 1,
900 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
901 .pImageInfo = (VkDescriptorImageInfo[]) {
902 {
903 .sampler = VK_NULL_HANDLE,
904 .imageView = radv_image_view_to_handle(&store_iview),
905 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
906 },
907 }
908 }
909 });
910
911 radv_unaligned_dispatch(cmd_buffer, width, height, 1);
912 }
913 }
914
915 /* Mark this image as actually being decompressed. */
916 radv_update_dcc_metadata(cmd_buffer, image, subresourceRange, false);
917
918 /* The fill buffer below does its own saving */
919 radv_meta_restore(&saved_state, cmd_buffer);
920
921 state->flush_bits |= RADV_CMD_FLAG_CS_PARTIAL_FLUSH |
922 RADV_CMD_FLAG_INV_VCACHE;
923
924
925 /* Initialize the DCC metadata as "fully expanded". */
926 radv_initialize_dcc(cmd_buffer, image, subresourceRange, 0xffffffff);
927 }
928
929 void
930 radv_decompress_dcc(struct radv_cmd_buffer *cmd_buffer,
931 struct radv_image *image,
932 const VkImageSubresourceRange *subresourceRange)
933 {
934 if (cmd_buffer->queue_family_index == RADV_QUEUE_GENERAL)
935 radv_decompress_dcc_gfx(cmd_buffer, image, subresourceRange);
936 else
937 radv_decompress_dcc_compute(cmd_buffer, image, subresourceRange);
938 }