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