Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/amd/vulkan/radv_meta_fmask_expand.c
7417 views
1
/*
2
* Copyright © 2019 Valve Corporation
3
* Copyright © 2018 Red Hat
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the "Software"),
7
* to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
* and/or sell copies of the Software, and to permit persons to whom the
10
* Software is furnished to do so, subject to the following conditions:
11
*
12
* The above copyright notice and this permission notice (including the next
13
* paragraph) shall be included in all copies or substantial portions of the
14
* Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
* IN THE SOFTWARE.
23
*/
24
25
#include "radv_meta.h"
26
#include "radv_private.h"
27
#include "vk_format.h"
28
29
static nir_shader *
30
build_fmask_expand_compute_shader(struct radv_device *device, int samples)
31
{
32
const struct glsl_type *type =
33
glsl_sampler_type(GLSL_SAMPLER_DIM_MS, false, true, GLSL_TYPE_FLOAT);
34
const struct glsl_type *img_type = glsl_image_type(GLSL_SAMPLER_DIM_MS, true, GLSL_TYPE_FLOAT);
35
36
nir_builder b =
37
nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, NULL, "meta_fmask_expand_cs-%d", samples);
38
b.shader->info.workgroup_size[0] = 8;
39
b.shader->info.workgroup_size[1] = 8;
40
b.shader->info.workgroup_size[2] = 1;
41
42
nir_variable *input_img = nir_variable_create(b.shader, nir_var_uniform, type, "s_tex");
43
input_img->data.descriptor_set = 0;
44
input_img->data.binding = 0;
45
46
nir_variable *output_img = nir_variable_create(b.shader, nir_var_uniform, img_type, "out_img");
47
output_img->data.descriptor_set = 0;
48
output_img->data.binding = 1;
49
output_img->data.access = ACCESS_NON_READABLE;
50
51
nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);
52
nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);
53
nir_ssa_def *block_size =
54
nir_imm_ivec4(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],
55
b.shader->info.workgroup_size[2], 0);
56
57
nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);
58
nir_ssa_def *layer_id = nir_channel(&b, wg_id, 2);
59
60
nir_ssa_def *input_img_deref = &nir_build_deref_var(&b, input_img)->dest.ssa;
61
nir_ssa_def *output_img_deref = &nir_build_deref_var(&b, output_img)->dest.ssa;
62
63
nir_ssa_def *tex_coord =
64
nir_vec3(&b, nir_channel(&b, global_id, 0), nir_channel(&b, global_id, 1), layer_id);
65
66
nir_tex_instr *tex_instr[8];
67
for (uint32_t i = 0; i < samples; i++) {
68
tex_instr[i] = nir_tex_instr_create(b.shader, 3);
69
70
nir_tex_instr *tex = tex_instr[i];
71
tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
72
tex->op = nir_texop_txf_ms;
73
tex->src[0].src_type = nir_tex_src_coord;
74
tex->src[0].src = nir_src_for_ssa(tex_coord);
75
tex->src[1].src_type = nir_tex_src_ms_index;
76
tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, i));
77
tex->src[2].src_type = nir_tex_src_texture_deref;
78
tex->src[2].src = nir_src_for_ssa(input_img_deref);
79
tex->dest_type = nir_type_float32;
80
tex->is_array = true;
81
tex->coord_components = 3;
82
83
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");
84
nir_builder_instr_insert(&b, &tex->instr);
85
}
86
87
nir_ssa_def *img_coord =
88
nir_vec4(&b, nir_channel(&b, tex_coord, 0), nir_channel(&b, tex_coord, 1),
89
nir_channel(&b, tex_coord, 2), nir_imm_int(&b, 0));
90
91
for (uint32_t i = 0; i < samples; i++) {
92
nir_ssa_def *outval = &tex_instr[i]->dest.ssa;
93
94
nir_image_deref_store(&b, output_img_deref, img_coord, nir_imm_int(&b, i), outval,
95
nir_imm_int(&b, 0));
96
}
97
98
return b.shader;
99
}
100
101
void
102
radv_expand_fmask_image_inplace(struct radv_cmd_buffer *cmd_buffer, struct radv_image *image,
103
const VkImageSubresourceRange *subresourceRange)
104
{
105
struct radv_device *device = cmd_buffer->device;
106
struct radv_meta_saved_state saved_state;
107
const uint32_t samples = image->info.samples;
108
const uint32_t samples_log2 = ffs(samples) - 1;
109
unsigned layer_count = radv_get_layerCount(image, subresourceRange);
110
struct radv_image_view iview;
111
112
radv_meta_save(&saved_state, cmd_buffer,
113
RADV_META_SAVE_COMPUTE_PIPELINE | RADV_META_SAVE_DESCRIPTORS);
114
115
VkPipeline pipeline = device->meta_state.fmask_expand.pipeline[samples_log2];
116
117
radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,
118
pipeline);
119
120
cmd_buffer->state.flush_bits |= radv_dst_access_flush(
121
cmd_buffer, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, image);
122
123
radv_image_view_init(&iview, device,
124
&(VkImageViewCreateInfo){
125
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
126
.image = radv_image_to_handle(image),
127
.viewType = radv_meta_get_view_type(image),
128
.format = vk_format_no_srgb(image->vk_format),
129
.subresourceRange =
130
{
131
.aspectMask = subresourceRange->aspectMask,
132
.baseMipLevel = 0,
133
.levelCount = 1,
134
.baseArrayLayer = subresourceRange->baseArrayLayer,
135
.layerCount = layer_count,
136
},
137
},
138
NULL);
139
140
radv_meta_push_descriptor_set(
141
cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,
142
cmd_buffer->device->meta_state.fmask_expand.p_layout, 0, /* set */
143
2, /* descriptorWriteCount */
144
(VkWriteDescriptorSet[]){{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
145
.dstBinding = 0,
146
.dstArrayElement = 0,
147
.descriptorCount = 1,
148
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
149
.pImageInfo =
150
(VkDescriptorImageInfo[]){
151
{.sampler = VK_NULL_HANDLE,
152
.imageView = radv_image_view_to_handle(&iview),
153
.imageLayout = VK_IMAGE_LAYOUT_GENERAL},
154
}},
155
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
156
.dstBinding = 1,
157
.dstArrayElement = 0,
158
.descriptorCount = 1,
159
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
160
.pImageInfo = (VkDescriptorImageInfo[]){
161
{.sampler = VK_NULL_HANDLE,
162
.imageView = radv_image_view_to_handle(&iview),
163
.imageLayout = VK_IMAGE_LAYOUT_GENERAL},
164
}}});
165
166
radv_unaligned_dispatch(cmd_buffer, image->info.width, image->info.height, layer_count);
167
168
radv_meta_restore(&saved_state, cmd_buffer);
169
170
cmd_buffer->state.flush_bits |=
171
RADV_CMD_FLAG_CS_PARTIAL_FLUSH |
172
radv_src_access_flush(cmd_buffer, VK_ACCESS_SHADER_WRITE_BIT, image);
173
174
/* Re-initialize FMASK in fully expanded mode. */
175
cmd_buffer->state.flush_bits |= radv_init_fmask(cmd_buffer, image, subresourceRange);
176
}
177
178
void
179
radv_device_finish_meta_fmask_expand_state(struct radv_device *device)
180
{
181
struct radv_meta_state *state = &device->meta_state;
182
183
for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; ++i) {
184
radv_DestroyPipeline(radv_device_to_handle(device), state->fmask_expand.pipeline[i],
185
&state->alloc);
186
}
187
radv_DestroyPipelineLayout(radv_device_to_handle(device), state->fmask_expand.p_layout,
188
&state->alloc);
189
190
radv_DestroyDescriptorSetLayout(radv_device_to_handle(device), state->fmask_expand.ds_layout,
191
&state->alloc);
192
}
193
194
static VkResult
195
create_fmask_expand_pipeline(struct radv_device *device, int samples, VkPipeline *pipeline)
196
{
197
struct radv_meta_state *state = &device->meta_state;
198
VkResult result;
199
nir_shader *cs = build_fmask_expand_compute_shader(device, samples);
200
;
201
202
VkPipelineShaderStageCreateInfo pipeline_shader_stage = {
203
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
204
.stage = VK_SHADER_STAGE_COMPUTE_BIT,
205
.module = vk_shader_module_handle_from_nir(cs),
206
.pName = "main",
207
.pSpecializationInfo = NULL,
208
};
209
210
VkComputePipelineCreateInfo vk_pipeline_info = {
211
.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
212
.stage = pipeline_shader_stage,
213
.flags = 0,
214
.layout = state->fmask_expand.p_layout,
215
};
216
217
result = radv_CreateComputePipelines(radv_device_to_handle(device),
218
radv_pipeline_cache_to_handle(&state->cache), 1,
219
&vk_pipeline_info, NULL, pipeline);
220
221
ralloc_free(cs);
222
return result;
223
}
224
225
VkResult
226
radv_device_init_meta_fmask_expand_state(struct radv_device *device)
227
{
228
struct radv_meta_state *state = &device->meta_state;
229
VkResult result;
230
231
VkDescriptorSetLayoutCreateInfo ds_create_info = {
232
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
233
.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,
234
.bindingCount = 2,
235
.pBindings = (VkDescriptorSetLayoutBinding[]){
236
{.binding = 0,
237
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
238
.descriptorCount = 1,
239
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
240
.pImmutableSamplers = NULL},
241
{.binding = 1,
242
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
243
.descriptorCount = 1,
244
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
245
.pImmutableSamplers = NULL},
246
}};
247
248
result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info,
249
&state->alloc, &state->fmask_expand.ds_layout);
250
if (result != VK_SUCCESS)
251
goto fail;
252
253
VkPipelineLayoutCreateInfo color_create_info = {
254
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
255
.setLayoutCount = 1,
256
.pSetLayouts = &state->fmask_expand.ds_layout,
257
.pushConstantRangeCount = 0,
258
.pPushConstantRanges = NULL,
259
};
260
261
result = radv_CreatePipelineLayout(radv_device_to_handle(device), &color_create_info,
262
&state->alloc, &state->fmask_expand.p_layout);
263
if (result != VK_SUCCESS)
264
goto fail;
265
266
for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; i++) {
267
uint32_t samples = 1 << i;
268
result = create_fmask_expand_pipeline(device, samples, &state->fmask_expand.pipeline[i]);
269
if (result != VK_SUCCESS)
270
goto fail;
271
}
272
273
return VK_SUCCESS;
274
fail:
275
radv_device_finish_meta_fmask_expand_state(device);
276
return result;
277
}
278
279