1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2021 Valve Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci#include "nir/nir_builder.h"
24bf215546Sopenharmony_ci#include "radv_meta.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_cistatic nir_shader *
27bf215546Sopenharmony_cibuild_fmask_copy_compute_shader(struct radv_device *dev, int samples)
28bf215546Sopenharmony_ci{
29bf215546Sopenharmony_ci   const struct glsl_type *sampler_type = glsl_sampler_type(GLSL_SAMPLER_DIM_MS, false, false, GLSL_TYPE_FLOAT);
30bf215546Sopenharmony_ci   const struct glsl_type *img_type = glsl_image_type(GLSL_SAMPLER_DIM_MS, false, GLSL_TYPE_FLOAT);
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci   nir_builder b =
33bf215546Sopenharmony_ci      radv_meta_init_shader(dev, MESA_SHADER_COMPUTE, "meta_fmask_copy_cs_-%d", samples);
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci   b.shader->info.workgroup_size[0] = 8;
36bf215546Sopenharmony_ci   b.shader->info.workgroup_size[1] = 8;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   nir_variable *input_img = nir_variable_create(b.shader, nir_var_uniform, sampler_type, "s_tex");
39bf215546Sopenharmony_ci   input_img->data.descriptor_set = 0;
40bf215546Sopenharmony_ci   input_img->data.binding = 0;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   nir_variable *output_img = nir_variable_create(b.shader, nir_var_uniform, img_type, "out_img");
43bf215546Sopenharmony_ci   output_img->data.descriptor_set = 0;
44bf215546Sopenharmony_ci   output_img->data.binding = 1;
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci   nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);
47bf215546Sopenharmony_ci   nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);
48bf215546Sopenharmony_ci   nir_ssa_def *block_size =
49bf215546Sopenharmony_ci      nir_imm_ivec3(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],
50bf215546Sopenharmony_ci                    b.shader->info.workgroup_size[2]);
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   /* Get coordinates. */
55bf215546Sopenharmony_ci   nir_ssa_def *src_coord = nir_channels(&b, global_id, 0x3);
56bf215546Sopenharmony_ci   nir_ssa_def *dst_coord = nir_vec4(&b, nir_channel(&b, src_coord, 0),
57bf215546Sopenharmony_ci                                         nir_channel(&b, src_coord, 1),
58bf215546Sopenharmony_ci                                         nir_ssa_undef(&b, 1, 32),
59bf215546Sopenharmony_ci                                         nir_ssa_undef(&b, 1, 32));
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   nir_ssa_def *input_img_deref = &nir_build_deref_var(&b, input_img)->dest.ssa;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   /* Fetch the mask for this fragment. */
64bf215546Sopenharmony_ci   nir_tex_instr *frag_mask_fetch = nir_tex_instr_create(b.shader, 3);
65bf215546Sopenharmony_ci   frag_mask_fetch->sampler_dim = GLSL_SAMPLER_DIM_MS;
66bf215546Sopenharmony_ci   frag_mask_fetch->op = nir_texop_fragment_mask_fetch_amd;
67bf215546Sopenharmony_ci   frag_mask_fetch->src[0].src_type = nir_tex_src_coord;
68bf215546Sopenharmony_ci   frag_mask_fetch->src[0].src = nir_src_for_ssa(src_coord);
69bf215546Sopenharmony_ci   frag_mask_fetch->src[1].src_type = nir_tex_src_lod;
70bf215546Sopenharmony_ci   frag_mask_fetch->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));
71bf215546Sopenharmony_ci   frag_mask_fetch->src[2].src_type = nir_tex_src_texture_deref;
72bf215546Sopenharmony_ci   frag_mask_fetch->src[2].src = nir_src_for_ssa(input_img_deref);
73bf215546Sopenharmony_ci   frag_mask_fetch->dest_type = nir_type_uint32;
74bf215546Sopenharmony_ci   frag_mask_fetch->is_array = false;
75bf215546Sopenharmony_ci   frag_mask_fetch->coord_components = 2;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   nir_ssa_dest_init(&frag_mask_fetch->instr, &frag_mask_fetch->dest, 1, 32, "frag_mask_fetch");
78bf215546Sopenharmony_ci   nir_builder_instr_insert(&b, &frag_mask_fetch->instr);
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   nir_ssa_def *frag_mask = &frag_mask_fetch->dest.ssa;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   /* Get the maximum sample used in this fragment. */
83bf215546Sopenharmony_ci   nir_ssa_def *max_sample_index = nir_imm_int(&b, 0);
84bf215546Sopenharmony_ci   for (uint32_t s = 0; s < samples; s++) {
85bf215546Sopenharmony_ci      /* max_sample_index = MAX2(max_sample_index, (frag_mask >> (s * 4)) & 0xf) */
86bf215546Sopenharmony_ci      max_sample_index = nir_umax(&b, max_sample_index,
87bf215546Sopenharmony_ci                              nir_ubitfield_extract(&b, frag_mask, nir_imm_int(&b, 4 * s),
88bf215546Sopenharmony_ci                                                    nir_imm_int(&b, 4)));
89bf215546Sopenharmony_ci   }
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   nir_variable *counter = nir_local_variable_create(b.impl, glsl_int_type(), "counter");
92bf215546Sopenharmony_ci   nir_store_var(&b, counter, nir_imm_int(&b, 0), 0x1);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   nir_loop *loop = nir_push_loop(&b);
95bf215546Sopenharmony_ci   {
96bf215546Sopenharmony_ci      nir_ssa_def *sample_id = nir_load_var(&b, counter);
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci      nir_tex_instr *frag_fetch = nir_tex_instr_create(b.shader, 4);
99bf215546Sopenharmony_ci      frag_fetch->sampler_dim = GLSL_SAMPLER_DIM_MS;
100bf215546Sopenharmony_ci      frag_fetch->op = nir_texop_fragment_fetch_amd;
101bf215546Sopenharmony_ci      frag_fetch->src[0].src_type = nir_tex_src_coord;
102bf215546Sopenharmony_ci      frag_fetch->src[0].src = nir_src_for_ssa(src_coord);
103bf215546Sopenharmony_ci      frag_fetch->src[1].src_type = nir_tex_src_lod;
104bf215546Sopenharmony_ci      frag_fetch->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));
105bf215546Sopenharmony_ci      frag_fetch->src[2].src_type = nir_tex_src_texture_deref;
106bf215546Sopenharmony_ci      frag_fetch->src[2].src = nir_src_for_ssa(input_img_deref);
107bf215546Sopenharmony_ci      frag_fetch->src[3].src_type = nir_tex_src_ms_index;
108bf215546Sopenharmony_ci      frag_fetch->src[3].src = nir_src_for_ssa(sample_id);
109bf215546Sopenharmony_ci      frag_fetch->dest_type = nir_type_float32;
110bf215546Sopenharmony_ci      frag_fetch->is_array = false;
111bf215546Sopenharmony_ci      frag_fetch->coord_components = 2;
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci      nir_ssa_dest_init(&frag_fetch->instr, &frag_fetch->dest, 4, 32, "frag_fetch");
114bf215546Sopenharmony_ci      nir_builder_instr_insert(&b, &frag_fetch->instr);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci      nir_ssa_def *outval = &frag_fetch->dest.ssa;
117bf215546Sopenharmony_ci      nir_image_deref_store(&b, &nir_build_deref_var(&b, output_img)->dest.ssa, dst_coord,
118bf215546Sopenharmony_ci                            sample_id, outval, nir_imm_int(&b, 0),
119bf215546Sopenharmony_ci                            .image_dim = GLSL_SAMPLER_DIM_MS);
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci      radv_break_on_count(&b, counter, max_sample_index);
122bf215546Sopenharmony_ci   }
123bf215546Sopenharmony_ci   nir_pop_loop(&b, loop);
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   return b.shader;
126bf215546Sopenharmony_ci}
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_civoid
129bf215546Sopenharmony_ciradv_device_finish_meta_fmask_copy_state(struct radv_device *device)
130bf215546Sopenharmony_ci{
131bf215546Sopenharmony_ci   struct radv_meta_state *state = &device->meta_state;
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   radv_DestroyPipelineLayout(radv_device_to_handle(device), state->fmask_copy.p_layout,
134bf215546Sopenharmony_ci                              &state->alloc);
135bf215546Sopenharmony_ci   device->vk.dispatch_table.DestroyDescriptorSetLayout(radv_device_to_handle(device),
136bf215546Sopenharmony_ci                                                        state->fmask_copy.ds_layout, &state->alloc);
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; ++i) {
139bf215546Sopenharmony_ci      radv_DestroyPipeline(radv_device_to_handle(device), state->fmask_copy.pipeline[i], &state->alloc);
140bf215546Sopenharmony_ci   }
141bf215546Sopenharmony_ci}
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_cistatic VkResult
144bf215546Sopenharmony_cicreate_fmask_copy_pipeline(struct radv_device *device, int samples, VkPipeline *pipeline)
145bf215546Sopenharmony_ci{
146bf215546Sopenharmony_ci   struct radv_meta_state *state = &device->meta_state;
147bf215546Sopenharmony_ci   nir_shader *cs = build_fmask_copy_compute_shader(device, samples);
148bf215546Sopenharmony_ci   VkResult result;
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci   VkPipelineShaderStageCreateInfo pipeline_shader_stage = {
151bf215546Sopenharmony_ci      .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
152bf215546Sopenharmony_ci      .stage = VK_SHADER_STAGE_COMPUTE_BIT,
153bf215546Sopenharmony_ci      .module = vk_shader_module_handle_from_nir(cs),
154bf215546Sopenharmony_ci      .pName = "main",
155bf215546Sopenharmony_ci      .pSpecializationInfo = NULL,
156bf215546Sopenharmony_ci   };
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci   VkComputePipelineCreateInfo vk_pipeline_info = {
159bf215546Sopenharmony_ci      .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
160bf215546Sopenharmony_ci      .stage = pipeline_shader_stage,
161bf215546Sopenharmony_ci      .flags = 0,
162bf215546Sopenharmony_ci      .layout = state->fmask_copy.p_layout,
163bf215546Sopenharmony_ci   };
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   result = radv_CreateComputePipelines(radv_device_to_handle(device),
166bf215546Sopenharmony_ci                                        radv_pipeline_cache_to_handle(&state->cache), 1,
167bf215546Sopenharmony_ci                                        &vk_pipeline_info, NULL, pipeline);
168bf215546Sopenharmony_ci   ralloc_free(cs);
169bf215546Sopenharmony_ci   return result;
170bf215546Sopenharmony_ci}
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ciVkResult
173bf215546Sopenharmony_ciradv_device_init_meta_fmask_copy_state(struct radv_device *device)
174bf215546Sopenharmony_ci{
175bf215546Sopenharmony_ci   VkResult result;
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci   VkDescriptorSetLayoutCreateInfo ds_create_info = {
178bf215546Sopenharmony_ci      .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
179bf215546Sopenharmony_ci      .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,
180bf215546Sopenharmony_ci      .bindingCount = 2,
181bf215546Sopenharmony_ci      .pBindings = (VkDescriptorSetLayoutBinding[]){
182bf215546Sopenharmony_ci         {.binding = 0,
183bf215546Sopenharmony_ci          .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
184bf215546Sopenharmony_ci          .descriptorCount = 1,
185bf215546Sopenharmony_ci          .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
186bf215546Sopenharmony_ci          .pImmutableSamplers = NULL},
187bf215546Sopenharmony_ci         {.binding = 1,
188bf215546Sopenharmony_ci          .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
189bf215546Sopenharmony_ci          .descriptorCount = 1,
190bf215546Sopenharmony_ci          .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
191bf215546Sopenharmony_ci          .pImmutableSamplers = NULL},
192bf215546Sopenharmony_ci      }};
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ci   result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info,
195bf215546Sopenharmony_ci                                           &device->meta_state.alloc,
196bf215546Sopenharmony_ci                                           &device->meta_state.fmask_copy.ds_layout);
197bf215546Sopenharmony_ci   if (result != VK_SUCCESS)
198bf215546Sopenharmony_ci      return result;
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci   VkPipelineLayoutCreateInfo pl_create_info = {
201bf215546Sopenharmony_ci      .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
202bf215546Sopenharmony_ci      .setLayoutCount = 1,
203bf215546Sopenharmony_ci      .pSetLayouts = &device->meta_state.fmask_copy.ds_layout,
204bf215546Sopenharmony_ci      .pushConstantRangeCount = 0,
205bf215546Sopenharmony_ci      .pPushConstantRanges = NULL
206bf215546Sopenharmony_ci   };
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci   result =
209bf215546Sopenharmony_ci      radv_CreatePipelineLayout(radv_device_to_handle(device), &pl_create_info,
210bf215546Sopenharmony_ci                                &device->meta_state.alloc, &device->meta_state.fmask_copy.p_layout);
211bf215546Sopenharmony_ci   if (result != VK_SUCCESS)
212bf215546Sopenharmony_ci      return result;
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci   for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; i++) {
215bf215546Sopenharmony_ci      uint32_t samples = 1 << i;
216bf215546Sopenharmony_ci      result = create_fmask_copy_pipeline(device, samples, &device->meta_state.fmask_copy.pipeline[i]);
217bf215546Sopenharmony_ci      if (result != VK_SUCCESS)
218bf215546Sopenharmony_ci         return result;
219bf215546Sopenharmony_ci   }
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci   return VK_SUCCESS;
222bf215546Sopenharmony_ci}
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_cistatic void
225bf215546Sopenharmony_ciradv_fixup_copy_dst_metadata(struct radv_cmd_buffer *cmd_buffer, const struct radv_image *src_image,
226bf215546Sopenharmony_ci                             const struct radv_image *dst_image)
227bf215546Sopenharmony_ci{
228bf215546Sopenharmony_ci   uint64_t src_offset, dst_offset, size;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci   assert(src_image->planes[0].surface.cmask_size == dst_image->planes[0].surface.cmask_size &&
231bf215546Sopenharmony_ci          src_image->planes[0].surface.fmask_size == dst_image->planes[0].surface.fmask_size);
232bf215546Sopenharmony_ci   assert(src_image->planes[0].surface.fmask_offset + src_image->planes[0].surface.fmask_size ==
233bf215546Sopenharmony_ci          src_image->planes[0].surface.cmask_offset &&
234bf215546Sopenharmony_ci          dst_image->planes[0].surface.fmask_offset + dst_image->planes[0].surface.fmask_size ==
235bf215546Sopenharmony_ci          dst_image->planes[0].surface.cmask_offset);
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci   /* Copy CMASK+FMASK. */
238bf215546Sopenharmony_ci   size = src_image->planes[0].surface.cmask_size + src_image->planes[0].surface.fmask_size;
239bf215546Sopenharmony_ci   src_offset = src_image->bindings[0].offset + src_image->planes[0].surface.fmask_offset;
240bf215546Sopenharmony_ci   dst_offset = dst_image->bindings[0].offset + dst_image->planes[0].surface.fmask_offset;
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci   radv_copy_buffer(cmd_buffer, src_image->bindings[0].bo, dst_image->bindings[0].bo,
243bf215546Sopenharmony_ci                    src_offset, dst_offset, size);
244bf215546Sopenharmony_ci}
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_cibool
247bf215546Sopenharmony_ciradv_can_use_fmask_copy(struct radv_cmd_buffer *cmd_buffer,
248bf215546Sopenharmony_ci                        const struct radv_image *src_image, const struct radv_image *dst_image,
249bf215546Sopenharmony_ci                        unsigned num_rects, const struct radv_meta_blit2d_rect *rects)
250bf215546Sopenharmony_ci{
251bf215546Sopenharmony_ci   /* TODO: Test on pre GFX10 chips. */
252bf215546Sopenharmony_ci   if (cmd_buffer->device->physical_device->rad_info.gfx_level < GFX10)
253bf215546Sopenharmony_ci      return false;
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci   /* TODO: Add support for layers. */
256bf215546Sopenharmony_ci   if (src_image->info.array_size != 1 || dst_image->info.array_size != 1)
257bf215546Sopenharmony_ci      return false;
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci   /* Source/destination images must have FMASK. */
260bf215546Sopenharmony_ci   if (!radv_image_has_fmask(src_image) || !radv_image_has_fmask(dst_image))
261bf215546Sopenharmony_ci      return false;
262bf215546Sopenharmony_ci
263bf215546Sopenharmony_ci   /* Source/destination images must have identical TC-compat mode. */
264bf215546Sopenharmony_ci   if (radv_image_is_tc_compat_cmask(src_image) != radv_image_is_tc_compat_cmask(dst_image))
265bf215546Sopenharmony_ci      return false;
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci   /* The region must be a whole image copy. */
268bf215546Sopenharmony_ci   if (num_rects != 1 ||
269bf215546Sopenharmony_ci       (rects[0].src_x || rects[0].src_y || rects[0].dst_x || rects[0].dst_y ||
270bf215546Sopenharmony_ci        rects[0].width != src_image->info.width || rects[0].height != src_image->info.height))
271bf215546Sopenharmony_ci      return false;
272bf215546Sopenharmony_ci
273bf215546Sopenharmony_ci   /* Source/destination images must have identical size. */
274bf215546Sopenharmony_ci   if (src_image->info.width != dst_image->info.width ||
275bf215546Sopenharmony_ci       src_image->info.height != dst_image->info.height)
276bf215546Sopenharmony_ci      return false;
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci   /* Source/destination images must have identical swizzle. */
279bf215546Sopenharmony_ci   if (src_image->planes[0].surface.fmask_tile_swizzle !=
280bf215546Sopenharmony_ci       dst_image->planes[0].surface.fmask_tile_swizzle ||
281bf215546Sopenharmony_ci       src_image->planes[0].surface.u.gfx9.color.fmask_swizzle_mode !=
282bf215546Sopenharmony_ci       dst_image->planes[0].surface.u.gfx9.color.fmask_swizzle_mode)
283bf215546Sopenharmony_ci      return false;
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   return true;
286bf215546Sopenharmony_ci}
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_civoid
289bf215546Sopenharmony_ciradv_fmask_copy(struct radv_cmd_buffer *cmd_buffer, struct radv_meta_blit2d_surf *src,
290bf215546Sopenharmony_ci                struct radv_meta_blit2d_surf *dst)
291bf215546Sopenharmony_ci{
292bf215546Sopenharmony_ci   struct radv_device *device = cmd_buffer->device;
293bf215546Sopenharmony_ci   struct radv_image_view src_iview, dst_iview;
294bf215546Sopenharmony_ci   uint32_t samples = src->image->info.samples;
295bf215546Sopenharmony_ci   uint32_t samples_log2 = ffs(samples) - 1;
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_ci   radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,
298bf215546Sopenharmony_ci                        cmd_buffer->device->meta_state.fmask_copy.pipeline[samples_log2]);
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   radv_image_view_init(&src_iview, device,
301bf215546Sopenharmony_ci                        &(VkImageViewCreateInfo){
302bf215546Sopenharmony_ci                           .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
303bf215546Sopenharmony_ci                           .image = radv_image_to_handle(src->image),
304bf215546Sopenharmony_ci                           .viewType = radv_meta_get_view_type(src->image),
305bf215546Sopenharmony_ci                           .format = vk_format_no_srgb(src->image->vk.format),
306bf215546Sopenharmony_ci                           .subresourceRange =
307bf215546Sopenharmony_ci                              {
308bf215546Sopenharmony_ci                                 .aspectMask = src->aspect_mask,
309bf215546Sopenharmony_ci                                 .baseMipLevel = 0,
310bf215546Sopenharmony_ci                                 .levelCount = 1,
311bf215546Sopenharmony_ci                                 .baseArrayLayer = 0,
312bf215546Sopenharmony_ci                                 .layerCount = 1,
313bf215546Sopenharmony_ci                              },
314bf215546Sopenharmony_ci                        },
315bf215546Sopenharmony_ci                        0, NULL);
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci   radv_image_view_init(&dst_iview, device,
318bf215546Sopenharmony_ci                        &(VkImageViewCreateInfo){
319bf215546Sopenharmony_ci                           .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
320bf215546Sopenharmony_ci                           .image = radv_image_to_handle(dst->image),
321bf215546Sopenharmony_ci                           .viewType = radv_meta_get_view_type(dst->image),
322bf215546Sopenharmony_ci                           .format = vk_format_no_srgb(dst->image->vk.format),
323bf215546Sopenharmony_ci                           .subresourceRange =
324bf215546Sopenharmony_ci                              {
325bf215546Sopenharmony_ci                                 .aspectMask = dst->aspect_mask,
326bf215546Sopenharmony_ci                                 .baseMipLevel = 0,
327bf215546Sopenharmony_ci                                 .levelCount = 1,
328bf215546Sopenharmony_ci                                 .baseArrayLayer = 0,
329bf215546Sopenharmony_ci                                 .layerCount = 1,
330bf215546Sopenharmony_ci                              },
331bf215546Sopenharmony_ci                        },
332bf215546Sopenharmony_ci                        0, NULL);
333bf215546Sopenharmony_ci
334bf215546Sopenharmony_ci   radv_meta_push_descriptor_set(
335bf215546Sopenharmony_ci      cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,
336bf215546Sopenharmony_ci      cmd_buffer->device->meta_state.fmask_copy.p_layout, 0, /* set */
337bf215546Sopenharmony_ci      2,                                                     /* descriptorWriteCount */
338bf215546Sopenharmony_ci      (VkWriteDescriptorSet[]){{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
339bf215546Sopenharmony_ci                                .dstBinding = 0,
340bf215546Sopenharmony_ci                                .dstArrayElement = 0,
341bf215546Sopenharmony_ci                                .descriptorCount = 1,
342bf215546Sopenharmony_ci                                .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
343bf215546Sopenharmony_ci                                .pImageInfo =
344bf215546Sopenharmony_ci                                   (VkDescriptorImageInfo[]){
345bf215546Sopenharmony_ci                                      {.sampler = VK_NULL_HANDLE,
346bf215546Sopenharmony_ci                                       .imageView = radv_image_view_to_handle(&src_iview),
347bf215546Sopenharmony_ci                                       .imageLayout = VK_IMAGE_LAYOUT_GENERAL},
348bf215546Sopenharmony_ci                                   }},
349bf215546Sopenharmony_ci                               {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
350bf215546Sopenharmony_ci                                .dstBinding = 1,
351bf215546Sopenharmony_ci                                .dstArrayElement = 0,
352bf215546Sopenharmony_ci                                .descriptorCount = 1,
353bf215546Sopenharmony_ci                                .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
354bf215546Sopenharmony_ci                                .pImageInfo = (VkDescriptorImageInfo[]){
355bf215546Sopenharmony_ci                                   {.sampler = VK_NULL_HANDLE,
356bf215546Sopenharmony_ci                                    .imageView = radv_image_view_to_handle(&dst_iview),
357bf215546Sopenharmony_ci                                    .imageLayout = VK_IMAGE_LAYOUT_GENERAL},
358bf215546Sopenharmony_ci                                }}});
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci   radv_unaligned_dispatch(cmd_buffer, src->image->info.width, src->image->info.height, 1);
361bf215546Sopenharmony_ci
362bf215546Sopenharmony_ci   /* Fixup destination image metadata by copying CMASK/FMASK from the source image. */
363bf215546Sopenharmony_ci   radv_fixup_copy_dst_metadata(cmd_buffer, src->image, dst->image);
364bf215546Sopenharmony_ci}
365