1 /*
2 * Copyright © 2021 Valve 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 #define AC_SURFACE_INCLUDE_NIR
25 #include "ac_surface.h"
26
27 #include "radv_meta.h"
28 #include "radv_private.h"
29 #include "vk_format.h"
30
31 void
radv_device_finish_meta_copy_vrs_htile_state(struct radv_device * device)32 radv_device_finish_meta_copy_vrs_htile_state(struct radv_device *device)
33 {
34 struct radv_meta_state *state = &device->meta_state;
35
36 radv_DestroyPipeline(radv_device_to_handle(device), state->copy_vrs_htile_pipeline,
37 &state->alloc);
38 radv_DestroyPipelineLayout(radv_device_to_handle(device), state->copy_vrs_htile_p_layout,
39 &state->alloc);
40 radv_DestroyDescriptorSetLayout(radv_device_to_handle(device), state->copy_vrs_htile_ds_layout,
41 &state->alloc);
42 }
43
44 static nir_shader *
build_copy_vrs_htile_shader(struct radv_device * device,struct radeon_surf * surf)45 build_copy_vrs_htile_shader(struct radv_device *device, struct radeon_surf *surf)
46 {
47 nir_builder b = radv_meta_init_shader(MESA_SHADER_COMPUTE, "meta_copy_vrs_htile");
48 b.shader->info.workgroup_size[0] = 8;
49 b.shader->info.workgroup_size[1] = 8;
50
51 /* Get coordinates. */
52 nir_ssa_def *global_id = get_global_ids(&b, 2);
53
54 /* Multiply the coordinates by the HTILE block size. */
55 nir_ssa_def *coord = nir_imul(&b, global_id, nir_imm_ivec2(&b, 8, 8));
56
57 /* Load constants. */
58 nir_ssa_def *constants = nir_load_push_constant(&b, 3, 32, nir_imm_int(&b, 0), .range = 12);
59 nir_ssa_def *htile_pitch = nir_channel(&b, constants, 0);
60 nir_ssa_def *htile_slice_size = nir_channel(&b, constants, 1);
61 nir_ssa_def *read_htile_value = nir_channel(&b, constants, 2);
62
63 /* Get the HTILE addr from coordinates. */
64 nir_ssa_def *zero = nir_imm_int(&b, 0);
65 nir_ssa_def *htile_addr = ac_nir_htile_addr_from_coord(
66 &b, &device->physical_device->rad_info, &surf->u.gfx9.zs.htile_equation, htile_pitch,
67 htile_slice_size, nir_channel(&b, coord, 0), nir_channel(&b, coord, 1), zero, zero);
68
69 /* Set up the input VRS image descriptor. */
70 const struct glsl_type *vrs_sampler_type =
71 glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);
72 nir_variable *input_vrs_img =
73 nir_variable_create(b.shader, nir_var_uniform, vrs_sampler_type, "input_vrs_image");
74 input_vrs_img->data.descriptor_set = 0;
75 input_vrs_img->data.binding = 0;
76
77 nir_ssa_def *input_vrs_img_deref = &nir_build_deref_var(&b, input_vrs_img)->dest.ssa;
78
79 /* Load the VRS rates from the 2D image. */
80 nir_tex_instr *tex = nir_tex_instr_create(b.shader, 3);
81 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
82 tex->op = nir_texop_txf;
83 tex->src[0].src_type = nir_tex_src_coord;
84 tex->src[0].src = nir_src_for_ssa(global_id);
85 tex->src[1].src_type = nir_tex_src_lod;
86 tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));
87 tex->src[2].src_type = nir_tex_src_texture_deref;
88 tex->src[2].src = nir_src_for_ssa(input_vrs_img_deref);
89 tex->dest_type = nir_type_float32;
90 tex->is_array = false;
91 tex->coord_components = 2;
92
93 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");
94 nir_builder_instr_insert(&b, &tex->instr);
95
96 /* Extract the X/Y rates and clamp them because the maximum supported VRS rate is 2x2 (1x1 in
97 * hardware).
98 *
99 * VRS rate X = min(value >> 2, 1)
100 * VRS rate Y = min(value & 3, 1)
101 */
102 nir_ssa_def *x_rate = nir_ushr(&b, nir_channel(&b, &tex->dest.ssa, 0), nir_imm_int(&b, 2));
103 x_rate = nir_umin(&b, x_rate, nir_imm_int(&b, 1));
104
105 nir_ssa_def *y_rate = nir_iand(&b, nir_channel(&b, &tex->dest.ssa, 0), nir_imm_int(&b, 3));
106 y_rate = nir_umin(&b, y_rate, nir_imm_int(&b, 1));
107
108 /* Compute the final VRS rate. */
109 nir_ssa_def *vrs_rates = nir_ior(&b, nir_ishl(&b, y_rate, nir_imm_int(&b, 10)),
110 nir_ishl(&b, x_rate, nir_imm_int(&b, 6)));
111
112 /* Load the HTILE buffer descriptor. */
113 nir_ssa_def *htile_buf = radv_meta_load_descriptor(&b, 0, 1);
114
115 /* Load the HTILE value if requested, otherwise use the default value. */
116 nir_variable *htile_value = nir_local_variable_create(b.impl, glsl_int_type(), "htile_value");
117
118 nir_push_if(&b, nir_ieq(&b, read_htile_value, nir_imm_int(&b, 1)));
119 {
120 /* Load the existing HTILE 32-bit value for this 8x8 pixels area. */
121 nir_ssa_def *input_value = nir_load_ssbo(&b, 1, 32, htile_buf, htile_addr);
122
123 /* Clear the 4-bit VRS rates. */
124 nir_store_var(&b, htile_value, nir_iand(&b, input_value, nir_imm_int(&b, 0xfffff33f)), 0x1);
125 }
126 nir_push_else(&b, NULL);
127 {
128 nir_store_var(&b, htile_value, nir_imm_int(&b, 0xfffff33f), 0x1);
129 }
130 nir_pop_if(&b, NULL);
131
132 /* Set the VRS rates loaded from the image. */
133 nir_ssa_def *output_value = nir_ior(&b, nir_load_var(&b, htile_value), vrs_rates);
134
135 /* Store the updated HTILE 32-bit which contains the VRS rates. */
136 nir_store_ssbo(&b, output_value, htile_buf, htile_addr, .access = ACCESS_NON_READABLE);
137
138 return b.shader;
139 }
140
141 static VkResult
radv_device_init_meta_copy_vrs_htile_state(struct radv_device * device,struct radeon_surf * surf)142 radv_device_init_meta_copy_vrs_htile_state(struct radv_device *device,
143 struct radeon_surf *surf)
144 {
145 struct radv_meta_state *state = &device->meta_state;
146 nir_shader *cs = build_copy_vrs_htile_shader(device, surf);
147 VkResult result;
148
149 VkDescriptorSetLayoutCreateInfo ds_layout_info = {
150 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
151 .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,
152 .bindingCount = 2,
153 .pBindings = (VkDescriptorSetLayoutBinding[]){
154 {.binding = 0,
155 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
156 .descriptorCount = 1,
157 .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
158 .pImmutableSamplers = NULL},
159 {.binding = 1,
160 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
161 .descriptorCount = 1,
162 .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
163 .pImmutableSamplers = NULL},
164 }};
165
166 result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_layout_info,
167 &state->alloc, &state->copy_vrs_htile_ds_layout);
168 if (result != VK_SUCCESS)
169 goto fail;
170
171 VkPipelineLayoutCreateInfo p_layout_info = {
172 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
173 .setLayoutCount = 1,
174 .pSetLayouts = &state->copy_vrs_htile_ds_layout,
175 .pushConstantRangeCount = 1,
176 .pPushConstantRanges =
177 &(VkPushConstantRange){
178 VK_SHADER_STAGE_COMPUTE_BIT,
179 0,
180 12,
181 },
182 };
183
184 result = radv_CreatePipelineLayout(radv_device_to_handle(device), &p_layout_info, &state->alloc,
185 &state->copy_vrs_htile_p_layout);
186 if (result != VK_SUCCESS)
187 goto fail;
188
189 VkPipelineShaderStageCreateInfo shader_stage = {
190 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
191 .stage = VK_SHADER_STAGE_COMPUTE_BIT,
192 .module = vk_shader_module_handle_from_nir(cs),
193 .pName = "main",
194 .pSpecializationInfo = NULL,
195 };
196
197 VkComputePipelineCreateInfo pipeline_info = {
198 .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
199 .stage = shader_stage,
200 .flags = 0,
201 .layout = state->copy_vrs_htile_p_layout,
202 };
203
204 result = radv_CreateComputePipelines(radv_device_to_handle(device),
205 radv_pipeline_cache_to_handle(&state->cache), 1,
206 &pipeline_info, NULL, &state->copy_vrs_htile_pipeline);
207 fail:
208 ralloc_free(cs);
209 return result;
210 }
211
212 void
radv_copy_vrs_htile(struct radv_cmd_buffer * cmd_buffer,struct radv_image * vrs_image,VkExtent2D * extent,struct radv_image * dst_image,struct radv_buffer * htile_buffer,bool read_htile_value)213 radv_copy_vrs_htile(struct radv_cmd_buffer *cmd_buffer, struct radv_image *vrs_image,
214 VkExtent2D *extent, struct radv_image *dst_image,
215 struct radv_buffer *htile_buffer, bool read_htile_value)
216 {
217 struct radv_device *device = cmd_buffer->device;
218 struct radv_meta_state *state = &device->meta_state;
219 struct radv_meta_saved_state saved_state;
220 struct radv_image_view vrs_iview;
221
222 assert(radv_image_has_htile(dst_image));
223
224 if (!cmd_buffer->device->meta_state.copy_vrs_htile_pipeline) {
225 VkResult ret = radv_device_init_meta_copy_vrs_htile_state(cmd_buffer->device,
226 &dst_image->planes[0].surface);
227 if (ret != VK_SUCCESS) {
228 cmd_buffer->record_result = ret;
229 return;
230 }
231 }
232
233 cmd_buffer->state.flush_bits |=
234 radv_src_access_flush(cmd_buffer, VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT_KHR, NULL) |
235 radv_dst_access_flush(cmd_buffer, VK_ACCESS_2_SHADER_READ_BIT_KHR, NULL);
236
237 radv_meta_save(
238 &saved_state, cmd_buffer,
239 RADV_META_SAVE_COMPUTE_PIPELINE | RADV_META_SAVE_CONSTANTS | RADV_META_SAVE_DESCRIPTORS);
240
241 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,
242 state->copy_vrs_htile_pipeline);
243
244 radv_image_view_init(&vrs_iview, cmd_buffer->device,
245 &(VkImageViewCreateInfo){
246 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
247 .image = radv_image_to_handle(vrs_image),
248 .viewType = VK_IMAGE_VIEW_TYPE_2D,
249 .format = vrs_image->vk_format,
250 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
251 .baseMipLevel = 0,
252 .levelCount = 1,
253 .baseArrayLayer = 0,
254 .layerCount = 1},
255 },
256 NULL);
257
258 radv_meta_push_descriptor_set(
259 cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, state->copy_vrs_htile_p_layout, 0, /* set */
260 2, /* descriptorWriteCount */
261 (VkWriteDescriptorSet[]){
262 {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
263 .dstBinding = 0,
264 .dstArrayElement = 0,
265 .descriptorCount = 1,
266 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
267 .pImageInfo =
268 (VkDescriptorImageInfo[]){
269 {
270 .sampler = VK_NULL_HANDLE,
271 .imageView = radv_image_view_to_handle(&vrs_iview),
272 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
273 },
274 }},
275 {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
276 .dstBinding = 1,
277 .dstArrayElement = 0,
278 .descriptorCount = 1,
279 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
280 .pBufferInfo = &(VkDescriptorBufferInfo){.buffer = radv_buffer_to_handle(htile_buffer),
281 .offset = 0,
282 .range = htile_buffer->size}}});
283
284 const unsigned constants[3] = {
285 dst_image->planes[0].surface.meta_pitch, dst_image->planes[0].surface.meta_slice_size,
286 read_htile_value,
287 };
288
289 radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer), state->copy_vrs_htile_p_layout,
290 VK_SHADER_STAGE_COMPUTE_BIT, 0, 12, constants);
291
292 uint32_t width = DIV_ROUND_UP(extent->width, 8);
293 uint32_t height = DIV_ROUND_UP(extent->height, 8);
294
295 radv_unaligned_dispatch(cmd_buffer, width, height, 1);
296
297 radv_image_view_finish(&vrs_iview);
298
299 radv_meta_restore(&saved_state, cmd_buffer);
300
301 cmd_buffer->state.flush_bits |=
302 RADV_CMD_FLAG_CS_PARTIAL_FLUSH | RADV_CMD_FLAG_INV_VCACHE |
303 radv_src_access_flush(cmd_buffer, VK_ACCESS_2_SHADER_WRITE_BIT_KHR, NULL);
304 }
305