• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2021 Valve Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #define AC_SURFACE_INCLUDE_NIR
8 #include "ac_surface.h"
9 
10 #include "radv_meta.h"
11 #include "vk_common_entrypoints.h"
12 #include "vk_format.h"
13 
14 static nir_shader *
build_copy_vrs_htile_shader(struct radv_device * device,struct radeon_surf * surf)15 build_copy_vrs_htile_shader(struct radv_device *device, struct radeon_surf *surf)
16 {
17    const struct radv_physical_device *pdev = radv_device_physical(device);
18    nir_builder b = radv_meta_init_shader(device, MESA_SHADER_COMPUTE, "meta_copy_vrs_htile");
19    b.shader->info.workgroup_size[0] = 8;
20    b.shader->info.workgroup_size[1] = 8;
21 
22    /* Get coordinates. */
23    nir_def *global_id = get_global_ids(&b, 2);
24 
25    nir_def *offset = nir_load_push_constant(&b, 2, 32, nir_imm_int(&b, 0), .range = 8);
26 
27    /* Multiply the coordinates by the HTILE block size. */
28    nir_def *coord = nir_iadd(&b, nir_imul_imm(&b, global_id, 8), offset);
29 
30    /* Load constants. */
31    nir_def *constants = nir_load_push_constant(&b, 3, 32, nir_imm_int(&b, 8), .range = 20);
32    nir_def *htile_pitch = nir_channel(&b, constants, 0);
33    nir_def *htile_slice_size = nir_channel(&b, constants, 1);
34    nir_def *read_htile_value = nir_channel(&b, constants, 2);
35 
36    /* Get the HTILE addr from coordinates. */
37    nir_def *zero = nir_imm_int(&b, 0);
38    nir_def *htile_addr =
39       ac_nir_htile_addr_from_coord(&b, &pdev->info, &surf->u.gfx9.zs.htile_equation, htile_pitch, htile_slice_size,
40                                    nir_channel(&b, coord, 0), nir_channel(&b, coord, 1), zero, zero);
41 
42    /* Set up the input VRS image descriptor. */
43    const struct glsl_type *vrs_sampler_type = glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);
44    nir_variable *input_vrs_img = nir_variable_create(b.shader, nir_var_uniform, vrs_sampler_type, "input_vrs_image");
45    input_vrs_img->data.descriptor_set = 0;
46    input_vrs_img->data.binding = 0;
47 
48    /* Load the VRS rates from the 2D image. */
49    nir_def *value = nir_txf_deref(&b, nir_build_deref_var(&b, input_vrs_img), global_id, NULL);
50 
51    /* Extract the X/Y rates and clamp them because the maximum supported VRS rate is 2x2 (1x1 in
52     * hardware).
53     *
54     * VRS rate X = min(value >> 2, 1)
55     * VRS rate Y = min(value & 3, 1)
56     */
57    nir_def *x_rate = nir_ushr_imm(&b, nir_channel(&b, value, 0), 2);
58    x_rate = nir_umin(&b, x_rate, nir_imm_int(&b, 1));
59 
60    nir_def *y_rate = nir_iand_imm(&b, nir_channel(&b, value, 0), 3);
61    y_rate = nir_umin(&b, y_rate, nir_imm_int(&b, 1));
62 
63    /* Compute the final VRS rate. */
64    nir_def *vrs_rates = nir_ior(&b, nir_ishl_imm(&b, y_rate, 10), nir_ishl_imm(&b, x_rate, 6));
65 
66    /* Load the HTILE buffer descriptor. */
67    nir_def *htile_buf = radv_meta_load_descriptor(&b, 0, 1);
68 
69    /* Load the HTILE value if requested, otherwise use the default value. */
70    nir_variable *htile_value = nir_local_variable_create(b.impl, glsl_int_type(), "htile_value");
71 
72    nir_push_if(&b, nir_ieq_imm(&b, read_htile_value, 1));
73    {
74       /* Load the existing HTILE 32-bit value for this 8x8 pixels area. */
75       nir_def *input_value = nir_load_ssbo(&b, 1, 32, htile_buf, htile_addr);
76 
77       /* Clear the 4-bit VRS rates. */
78       nir_store_var(&b, htile_value, nir_iand_imm(&b, input_value, 0xfffff33f), 0x1);
79    }
80    nir_push_else(&b, NULL);
81    {
82       nir_store_var(&b, htile_value, nir_imm_int(&b, 0xfffff33f), 0x1);
83    }
84    nir_pop_if(&b, NULL);
85 
86    /* Set the VRS rates loaded from the image. */
87    nir_def *output_value = nir_ior(&b, nir_load_var(&b, htile_value), vrs_rates);
88 
89    /* Store the updated HTILE 32-bit which contains the VRS rates. */
90    nir_store_ssbo(&b, output_value, htile_buf, htile_addr, .access = ACCESS_NON_READABLE);
91 
92    return b.shader;
93 }
94 
95 static VkResult
get_pipeline(struct radv_device * device,struct radv_image * image,VkPipeline * pipeline_out,VkPipelineLayout * layout_out)96 get_pipeline(struct radv_device *device, struct radv_image *image, VkPipeline *pipeline_out,
97              VkPipelineLayout *layout_out)
98 {
99    enum radv_meta_object_key_type key = RADV_META_OBJECT_KEY_COPY_VRS_HTILE;
100    VkResult result;
101 
102    const VkDescriptorSetLayoutBinding bindings[] = {
103       {
104          .binding = 0,
105          .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
106          .descriptorCount = 1,
107          .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
108       },
109       {
110          .binding = 1,
111          .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
112          .descriptorCount = 1,
113          .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
114       },
115    };
116 
117    const VkDescriptorSetLayoutCreateInfo desc_info = {
118       .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
119       .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT,
120       .bindingCount = 2,
121       .pBindings = bindings,
122    };
123 
124    const VkPushConstantRange pc_range = {
125       .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
126       .size = 20,
127    };
128 
129    result = vk_meta_get_pipeline_layout(&device->vk, &device->meta_state.device, &desc_info, &pc_range, &key,
130                                         sizeof(key), layout_out);
131    if (result != VK_SUCCESS)
132       return result;
133 
134    VkPipeline pipeline_from_cache = vk_meta_lookup_pipeline(&device->meta_state.device, &key, sizeof(key));
135    if (pipeline_from_cache != VK_NULL_HANDLE) {
136       *pipeline_out = pipeline_from_cache;
137       return VK_SUCCESS;
138    }
139 
140    nir_shader *cs = build_copy_vrs_htile_shader(device, &image->planes[0].surface);
141 
142    const VkPipelineShaderStageCreateInfo stage_info = {
143       .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
144       .stage = VK_SHADER_STAGE_COMPUTE_BIT,
145       .module = vk_shader_module_handle_from_nir(cs),
146       .pName = "main",
147       .pSpecializationInfo = NULL,
148    };
149 
150    const VkComputePipelineCreateInfo pipeline_info = {
151       .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
152       .stage = stage_info,
153       .flags = 0,
154       .layout = *layout_out,
155    };
156 
157    result = vk_meta_create_compute_pipeline(&device->vk, &device->meta_state.device, &pipeline_info, &key, sizeof(key),
158                                             pipeline_out);
159 
160    ralloc_free(cs);
161    return result;
162 }
163 
164 void
radv_copy_vrs_htile(struct radv_cmd_buffer * cmd_buffer,struct radv_image_view * vrs_iview,const VkRect2D * rect,struct radv_image * dst_image,struct radv_buffer * htile_buffer,bool read_htile_value)165 radv_copy_vrs_htile(struct radv_cmd_buffer *cmd_buffer, struct radv_image_view *vrs_iview, const VkRect2D *rect,
166                     struct radv_image *dst_image, struct radv_buffer *htile_buffer, bool read_htile_value)
167 {
168    struct radv_device *device = radv_cmd_buffer_device(cmd_buffer);
169    struct radv_meta_saved_state saved_state;
170    VkPipelineLayout layout;
171    VkPipeline pipeline;
172    VkResult result;
173 
174    assert(radv_image_has_htile(dst_image));
175 
176    result = get_pipeline(device, dst_image, &pipeline, &layout);
177    if (result != VK_SUCCESS) {
178       vk_command_buffer_set_error(&cmd_buffer->vk, result);
179       return;
180    }
181 
182    cmd_buffer->state.flush_bits |=
183       radv_src_access_flush(cmd_buffer, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
184                             VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, 0, NULL, NULL) |
185       radv_dst_access_flush(cmd_buffer, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_SHADER_READ_BIT, 0, NULL,
186                             NULL);
187 
188    radv_meta_save(&saved_state, cmd_buffer,
189                   RADV_META_SAVE_COMPUTE_PIPELINE | RADV_META_SAVE_CONSTANTS | RADV_META_SAVE_DESCRIPTORS);
190 
191    radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
192 
193    radv_meta_push_descriptor_set(
194       cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, layout, 0, 2,
195       (VkWriteDescriptorSet[]){{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
196                                 .dstBinding = 0,
197                                 .dstArrayElement = 0,
198                                 .descriptorCount = 1,
199                                 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
200                                 .pImageInfo =
201                                    (VkDescriptorImageInfo[]){
202                                       {
203                                          .sampler = VK_NULL_HANDLE,
204                                          .imageView = radv_image_view_to_handle(vrs_iview),
205                                          .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
206                                       },
207                                    }},
208                                {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
209                                 .dstBinding = 1,
210                                 .dstArrayElement = 0,
211                                 .descriptorCount = 1,
212                                 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
213                                 .pBufferInfo = &(VkDescriptorBufferInfo){.buffer = radv_buffer_to_handle(htile_buffer),
214                                                                          .offset = 0,
215                                                                          .range = htile_buffer->vk.size}}});
216 
217    const unsigned constants[5] = {
218       rect->offset.x,
219       rect->offset.y,
220       dst_image->planes[0].surface.meta_pitch,
221       dst_image->planes[0].surface.meta_slice_size,
222       read_htile_value,
223    };
224 
225    vk_common_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer), layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 20,
226                               constants);
227 
228    uint32_t width = DIV_ROUND_UP(rect->extent.width, 8);
229    uint32_t height = DIV_ROUND_UP(rect->extent.height, 8);
230 
231    radv_unaligned_dispatch(cmd_buffer, width, height, 1);
232 
233    radv_meta_restore(&saved_state, cmd_buffer);
234 
235    cmd_buffer->state.flush_bits |= RADV_CMD_FLAG_CS_PARTIAL_FLUSH | RADV_CMD_FLAG_INV_VCACHE |
236                                    radv_src_access_flush(cmd_buffer, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
237                                                          VK_ACCESS_2_SHADER_WRITE_BIT, 0, NULL, NULL);
238 }
239