Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_shader_nir.c
4570 views
1
/*
2
* Copyright 2017 Advanced Micro Devices, Inc.
3
* All Rights Reserved.
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
* on the rights to use, copy, modify, merge, publish, distribute, sub
9
* license, and/or sell copies of the Software, and to permit persons to whom
10
* the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22
* USE OR OTHER DEALINGS IN THE SOFTWARE.
23
*/
24
25
#include "ac_nir_to_llvm.h"
26
#include "ac_nir.h"
27
#include "compiler/nir/nir.h"
28
#include "compiler/nir/nir_builder.h"
29
#include "compiler/nir/nir_deref.h"
30
#include "compiler/nir_types.h"
31
#include "si_pipe.h"
32
#include "si_shader_internal.h"
33
#include "tgsi/tgsi_from_mesa.h"
34
35
static const nir_src *get_texture_src(nir_tex_instr *instr, nir_tex_src_type type)
36
{
37
for (unsigned i = 0; i < instr->num_srcs; i++) {
38
if (instr->src[i].src_type == type)
39
return &instr->src[i].src;
40
}
41
return NULL;
42
}
43
44
static void scan_io_usage(struct si_shader_info *info, nir_intrinsic_instr *intr,
45
bool is_input)
46
{
47
unsigned interp = INTERP_MODE_FLAT; /* load_input uses flat shading */
48
49
if (intr->intrinsic == nir_intrinsic_load_interpolated_input) {
50
nir_intrinsic_instr *baryc = nir_instr_as_intrinsic(intr->src[0].ssa->parent_instr);
51
52
if (baryc) {
53
if (nir_intrinsic_infos[baryc->intrinsic].index_map[NIR_INTRINSIC_INTERP_MODE] > 0)
54
interp = nir_intrinsic_interp_mode(baryc);
55
else
56
unreachable("unknown barycentric intrinsic");
57
} else {
58
unreachable("unknown barycentric expression");
59
}
60
}
61
62
unsigned mask, bit_size;
63
bool is_output_load;
64
65
if (nir_intrinsic_has_write_mask(intr)) {
66
mask = nir_intrinsic_write_mask(intr); /* store */
67
bit_size = nir_src_bit_size(intr->src[0]);
68
is_output_load = false;
69
} else {
70
mask = nir_ssa_def_components_read(&intr->dest.ssa); /* load */
71
bit_size = intr->dest.ssa.bit_size;
72
is_output_load = !is_input;
73
}
74
assert(bit_size != 64 && !(mask & ~0xf) && "64-bit IO should have been lowered");
75
76
/* Convert the 16-bit component mask to a 32-bit component mask except for VS inputs
77
* where the mask is untyped.
78
*/
79
if (bit_size == 16 && !is_input) {
80
unsigned new_mask = 0;
81
for (unsigned i = 0; i < 4; i++) {
82
if (mask & (1 << i))
83
new_mask |= 0x1 << (i / 2);
84
}
85
mask = new_mask;
86
}
87
88
mask <<= nir_intrinsic_component(intr);
89
90
nir_src offset = *nir_get_io_offset_src(intr);
91
bool indirect = !nir_src_is_const(offset);
92
if (!indirect)
93
assert(nir_src_as_uint(offset) == 0);
94
95
unsigned semantic = 0;
96
/* VS doesn't have semantics. */
97
if (info->stage != MESA_SHADER_VERTEX || !is_input)
98
semantic = nir_intrinsic_io_semantics(intr).location;
99
100
if (info->stage == MESA_SHADER_FRAGMENT && !is_input) {
101
/* Never use FRAG_RESULT_COLOR directly. */
102
if (semantic == FRAG_RESULT_COLOR)
103
semantic = FRAG_RESULT_DATA0;
104
semantic += nir_intrinsic_io_semantics(intr).dual_source_blend_index;
105
}
106
107
unsigned driver_location = nir_intrinsic_base(intr);
108
unsigned num_slots = indirect ? nir_intrinsic_io_semantics(intr).num_slots : 1;
109
110
if (is_input) {
111
assert(driver_location + num_slots <= ARRAY_SIZE(info->input_usage_mask));
112
113
for (unsigned i = 0; i < num_slots; i++) {
114
unsigned loc = driver_location + i;
115
116
info->input_semantic[loc] = semantic + i;
117
info->input_interpolate[loc] = interp;
118
119
if (mask) {
120
info->input_usage_mask[loc] |= mask;
121
if (bit_size == 16) {
122
if (nir_intrinsic_io_semantics(intr).high_16bits)
123
info->input_fp16_lo_hi_valid[loc] |= 0x2;
124
else
125
info->input_fp16_lo_hi_valid[loc] |= 0x1;
126
}
127
info->num_inputs = MAX2(info->num_inputs, loc + 1);
128
}
129
}
130
} else {
131
/* Outputs. */
132
assert(driver_location + num_slots <= ARRAY_SIZE(info->output_usagemask));
133
assert(semantic + num_slots < ARRAY_SIZE(info->output_semantic_to_slot));
134
135
for (unsigned i = 0; i < num_slots; i++) {
136
unsigned loc = driver_location + i;
137
138
info->output_semantic[loc] = semantic + i;
139
info->output_semantic_to_slot[semantic + i] = loc;
140
141
if (is_output_load) {
142
/* Output loads have only a few things that we need to track. */
143
info->output_readmask[loc] |= mask;
144
} else if (mask) {
145
/* Output stores. */
146
unsigned gs_streams = (uint32_t)nir_intrinsic_io_semantics(intr).gs_streams <<
147
(nir_intrinsic_component(intr) * 2);
148
unsigned new_mask = mask & ~info->output_usagemask[loc];
149
150
for (unsigned i = 0; i < 4; i++) {
151
unsigned stream = (gs_streams >> (i * 2)) & 0x3;
152
153
if (new_mask & (1 << i)) {
154
info->output_streams[loc] |= stream << (i * 2);
155
info->num_stream_output_components[stream]++;
156
}
157
}
158
159
if (nir_intrinsic_has_src_type(intr))
160
info->output_type[loc] = nir_intrinsic_src_type(intr);
161
else if (nir_intrinsic_has_dest_type(intr))
162
info->output_type[loc] = nir_intrinsic_dest_type(intr);
163
else
164
info->output_type[loc] = nir_type_float32;
165
166
info->output_usagemask[loc] |= mask;
167
info->num_outputs = MAX2(info->num_outputs, loc + 1);
168
169
if (info->stage == MESA_SHADER_FRAGMENT &&
170
semantic >= FRAG_RESULT_DATA0 && semantic <= FRAG_RESULT_DATA7) {
171
unsigned index = semantic - FRAG_RESULT_DATA0;
172
173
if (nir_intrinsic_src_type(intr) == nir_type_float16)
174
info->output_color_types |= SI_TYPE_FLOAT16 << (index * 2);
175
else if (nir_intrinsic_src_type(intr) == nir_type_int16)
176
info->output_color_types |= SI_TYPE_INT16 << (index * 2);
177
else if (nir_intrinsic_src_type(intr) == nir_type_uint16)
178
info->output_color_types |= SI_TYPE_UINT16 << (index * 2);
179
}
180
}
181
}
182
}
183
}
184
185
static bool is_bindless_handle_indirect(nir_instr *src)
186
{
187
/* Check if the bindless handle comes from indirect load_ubo. */
188
if (src->type == nir_instr_type_intrinsic &&
189
nir_instr_as_intrinsic(src)->intrinsic == nir_intrinsic_load_ubo) {
190
if (!nir_src_is_const(nir_instr_as_intrinsic(src)->src[0]))
191
return true;
192
} else {
193
/* Some other instruction. Return the worst-case result. */
194
return true;
195
}
196
return false;
197
}
198
199
static void scan_instruction(const struct nir_shader *nir, struct si_shader_info *info,
200
nir_instr *instr)
201
{
202
if (instr->type == nir_instr_type_tex) {
203
nir_tex_instr *tex = nir_instr_as_tex(instr);
204
const nir_src *handle = get_texture_src(tex, nir_tex_src_texture_handle);
205
206
/* Gather the types of used VMEM instructions that return something. */
207
switch (tex->op) {
208
case nir_texop_tex:
209
case nir_texop_txb:
210
case nir_texop_txl:
211
case nir_texop_txd:
212
case nir_texop_lod:
213
case nir_texop_tg4:
214
info->uses_vmem_return_type_sampler_or_bvh = true;
215
break;
216
default:
217
info->uses_vmem_return_type_other = true;
218
break;
219
}
220
221
if (handle) {
222
info->uses_bindless_samplers = true;
223
224
if (is_bindless_handle_indirect(handle->ssa->parent_instr))
225
info->uses_indirect_descriptor = true;
226
} else {
227
const nir_src *deref = get_texture_src(tex, nir_tex_src_texture_deref);
228
229
if (nir_deref_instr_has_indirect(nir_src_as_deref(*deref)))
230
info->uses_indirect_descriptor = true;
231
}
232
} else if (instr->type == nir_instr_type_intrinsic) {
233
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
234
const char *intr_name = nir_intrinsic_infos[intr->intrinsic].name;
235
bool is_ssbo = strstr(intr_name, "ssbo");
236
bool is_image = strstr(intr_name, "image_deref");
237
bool is_bindless_image = strstr(intr_name, "bindless_image");
238
239
/* Gather the types of used VMEM instructions that return something. */
240
if (nir_intrinsic_infos[intr->intrinsic].has_dest) {
241
switch (intr->intrinsic) {
242
case nir_intrinsic_load_ubo:
243
if (!nir_src_is_const(intr->src[1]))
244
info->uses_vmem_return_type_other = true;
245
break;
246
247
case nir_intrinsic_load_barycentric_at_sample: /* This loads sample positions. */
248
case nir_intrinsic_load_tess_level_outer: /* TES input read from memory */
249
case nir_intrinsic_load_tess_level_inner: /* TES input read from memory */
250
info->uses_vmem_return_type_other = true;
251
break;
252
253
case nir_intrinsic_load_input:
254
case nir_intrinsic_load_input_vertex:
255
case nir_intrinsic_load_per_vertex_input:
256
if (nir->info.stage == MESA_SHADER_VERTEX ||
257
nir->info.stage == MESA_SHADER_TESS_EVAL)
258
info->uses_vmem_return_type_other = true;
259
break;
260
261
default:
262
if (is_image ||
263
is_bindless_image ||
264
is_ssbo ||
265
strstr(intr_name, "global") ||
266
strstr(intr_name, "scratch"))
267
info->uses_vmem_return_type_other = true;
268
break;
269
}
270
}
271
272
if (is_bindless_image)
273
info->uses_bindless_images = true;
274
275
if (strstr(intr_name, "image_atomic") ||
276
strstr(intr_name, "image_store") ||
277
strstr(intr_name, "image_deref_atomic") ||
278
strstr(intr_name, "image_deref_store") ||
279
strstr(intr_name, "ssbo_atomic") ||
280
intr->intrinsic == nir_intrinsic_store_ssbo)
281
info->num_memory_stores++;
282
283
284
if (is_image && nir_deref_instr_has_indirect(nir_src_as_deref(intr->src[0])))
285
info->uses_indirect_descriptor = true;
286
287
if (is_bindless_image && is_bindless_handle_indirect(intr->src[0].ssa->parent_instr))
288
info->uses_indirect_descriptor = true;
289
290
if (intr->intrinsic != nir_intrinsic_store_ssbo && is_ssbo &&
291
!nir_src_is_const(intr->src[0]))
292
info->uses_indirect_descriptor = true;
293
294
switch (intr->intrinsic) {
295
case nir_intrinsic_store_ssbo:
296
if (!nir_src_is_const(intr->src[1]))
297
info->uses_indirect_descriptor = true;
298
break;
299
case nir_intrinsic_load_ubo:
300
if (!nir_src_is_const(intr->src[0]))
301
info->uses_indirect_descriptor = true;
302
break;
303
case nir_intrinsic_load_local_invocation_id:
304
case nir_intrinsic_load_workgroup_id: {
305
unsigned mask = nir_ssa_def_components_read(&intr->dest.ssa);
306
while (mask) {
307
unsigned i = u_bit_scan(&mask);
308
309
if (intr->intrinsic == nir_intrinsic_load_workgroup_id)
310
info->uses_block_id[i] = true;
311
else
312
info->uses_thread_id[i] = true;
313
}
314
break;
315
}
316
case nir_intrinsic_load_color0:
317
case nir_intrinsic_load_color1: {
318
unsigned index = intr->intrinsic == nir_intrinsic_load_color1;
319
uint8_t mask = nir_ssa_def_components_read(&intr->dest.ssa);
320
info->colors_read |= mask << (index * 4);
321
322
switch (info->color_interpolate[index]) {
323
case INTERP_MODE_SMOOTH:
324
if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_SAMPLE)
325
info->uses_persp_sample = true;
326
else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTROID)
327
info->uses_persp_centroid = true;
328
else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTER)
329
info->uses_persp_center = true;
330
break;
331
case INTERP_MODE_NOPERSPECTIVE:
332
if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_SAMPLE)
333
info->uses_linear_sample = true;
334
else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTROID)
335
info->uses_linear_centroid = true;
336
else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTER)
337
info->uses_linear_center = true;
338
break;
339
case INTERP_MODE_COLOR:
340
/* We don't know the final value. This will be FLAT if flatshading is enabled
341
* in the rasterizer state, otherwise it will be SMOOTH.
342
*/
343
info->uses_interp_color = true;
344
if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_SAMPLE)
345
info->uses_persp_sample_color = true;
346
else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTROID)
347
info->uses_persp_centroid_color = true;
348
else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTER)
349
info->uses_persp_center_color = true;
350
break;
351
}
352
break;
353
}
354
case nir_intrinsic_load_barycentric_at_offset: /* uses center */
355
case nir_intrinsic_load_barycentric_at_sample: /* uses center */
356
if (nir_intrinsic_interp_mode(intr) == INTERP_MODE_FLAT)
357
break;
358
359
if (nir_intrinsic_interp_mode(intr) == INTERP_MODE_NOPERSPECTIVE) {
360
info->uses_linear_center = true;
361
} else {
362
info->uses_persp_center = true;
363
}
364
if (intr->intrinsic == nir_intrinsic_load_barycentric_at_sample)
365
info->uses_interp_at_sample = true;
366
break;
367
case nir_intrinsic_load_input:
368
case nir_intrinsic_load_per_vertex_input:
369
case nir_intrinsic_load_input_vertex:
370
case nir_intrinsic_load_interpolated_input:
371
scan_io_usage(info, intr, true);
372
break;
373
case nir_intrinsic_load_output:
374
case nir_intrinsic_load_per_vertex_output:
375
case nir_intrinsic_store_output:
376
case nir_intrinsic_store_per_vertex_output:
377
scan_io_usage(info, intr, false);
378
break;
379
case nir_intrinsic_load_deref:
380
case nir_intrinsic_store_deref:
381
case nir_intrinsic_interp_deref_at_centroid:
382
case nir_intrinsic_interp_deref_at_sample:
383
case nir_intrinsic_interp_deref_at_offset:
384
unreachable("these opcodes should have been lowered");
385
break;
386
default:
387
break;
388
}
389
}
390
}
391
392
void si_nir_scan_shader(const struct nir_shader *nir, struct si_shader_info *info)
393
{
394
nir_function *func;
395
396
info->base = nir->info;
397
info->stage = nir->info.stage;
398
399
if (nir->info.stage == MESA_SHADER_TESS_EVAL) {
400
if (info->base.tess.primitive_mode == GL_ISOLINES)
401
info->base.tess.primitive_mode = GL_LINES;
402
}
403
404
if (nir->info.stage == MESA_SHADER_FRAGMENT) {
405
/* post_depth_coverage implies early_fragment_tests */
406
info->base.fs.early_fragment_tests |= info->base.fs.post_depth_coverage;
407
408
info->color_interpolate[0] = nir->info.fs.color0_interp;
409
info->color_interpolate[1] = nir->info.fs.color1_interp;
410
for (unsigned i = 0; i < 2; i++) {
411
if (info->color_interpolate[i] == INTERP_MODE_NONE)
412
info->color_interpolate[i] = INTERP_MODE_COLOR;
413
}
414
415
info->color_interpolate_loc[0] = nir->info.fs.color0_sample ? TGSI_INTERPOLATE_LOC_SAMPLE :
416
nir->info.fs.color0_centroid ? TGSI_INTERPOLATE_LOC_CENTROID :
417
TGSI_INTERPOLATE_LOC_CENTER;
418
info->color_interpolate_loc[1] = nir->info.fs.color1_sample ? TGSI_INTERPOLATE_LOC_SAMPLE :
419
nir->info.fs.color1_centroid ? TGSI_INTERPOLATE_LOC_CENTROID :
420
TGSI_INTERPOLATE_LOC_CENTER;
421
/* Set an invalid value. Will be determined at draw time if needed when the expected
422
* conditions are met.
423
*/
424
info->writes_1_if_tex_is_1 = nir->info.writes_memory ? 0 : 0xff;
425
}
426
427
info->constbuf0_num_slots = nir->num_uniforms;
428
429
if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
430
info->tessfactors_are_def_in_all_invocs = ac_are_tessfactors_def_in_all_invocs(nir);
431
}
432
433
info->uses_frontface = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_FRONT_FACE);
434
info->uses_instanceid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_INSTANCE_ID);
435
info->uses_base_vertex = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BASE_VERTEX);
436
info->uses_base_instance = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BASE_INSTANCE);
437
info->uses_invocationid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_INVOCATION_ID);
438
info->uses_grid_size = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_NUM_WORKGROUPS);
439
info->uses_subgroup_info = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_LOCAL_INVOCATION_INDEX) ||
440
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_SUBGROUP_ID) ||
441
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_NUM_SUBGROUPS);
442
info->uses_variable_block_size = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_WORKGROUP_SIZE);
443
info->uses_drawid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_DRAW_ID);
444
info->uses_primid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_PRIMITIVE_ID) ||
445
nir->info.inputs_read & VARYING_BIT_PRIMITIVE_ID;
446
info->reads_samplemask = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_SAMPLE_MASK_IN);
447
info->reads_tess_factors = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_TESS_LEVEL_INNER) ||
448
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_TESS_LEVEL_OUTER);
449
info->uses_linear_sample = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_LINEAR_SAMPLE);
450
info->uses_linear_centroid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID);
451
info->uses_linear_center = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_LINEAR_PIXEL);
452
info->uses_persp_sample = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE);
453
info->uses_persp_centroid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID);
454
info->uses_persp_center = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL);
455
456
if (nir->info.stage == MESA_SHADER_FRAGMENT) {
457
info->writes_z = nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH);
458
info->writes_stencil = nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL);
459
info->writes_samplemask = nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK);
460
461
info->colors_written = nir->info.outputs_written >> FRAG_RESULT_DATA0;
462
if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
463
info->color0_writes_all_cbufs = true;
464
info->colors_written |= 0x1;
465
}
466
if (nir->info.fs.color_is_dual_source)
467
info->colors_written |= 0x2;
468
} else {
469
info->writes_primid = nir->info.outputs_written & VARYING_BIT_PRIMITIVE_ID;
470
info->writes_viewport_index = nir->info.outputs_written & VARYING_BIT_VIEWPORT;
471
info->writes_layer = nir->info.outputs_written & VARYING_BIT_LAYER;
472
info->writes_psize = nir->info.outputs_written & VARYING_BIT_PSIZ;
473
info->writes_clipvertex = nir->info.outputs_written & VARYING_BIT_CLIP_VERTEX;
474
info->writes_edgeflag = nir->info.outputs_written & VARYING_BIT_EDGE;
475
info->writes_position = nir->info.outputs_written & VARYING_BIT_POS;
476
}
477
478
memset(info->output_semantic_to_slot, -1, sizeof(info->output_semantic_to_slot));
479
480
func = (struct nir_function *)exec_list_get_head_const(&nir->functions);
481
nir_foreach_block (block, func->impl) {
482
nir_foreach_instr (instr, block)
483
scan_instruction(nir, info, instr);
484
}
485
486
if (nir->info.stage == MESA_SHADER_FRAGMENT) {
487
info->allow_flat_shading = !(info->uses_persp_center || info->uses_persp_centroid ||
488
info->uses_persp_sample || info->uses_linear_center ||
489
info->uses_linear_centroid || info->uses_linear_sample ||
490
info->uses_interp_at_sample || nir->info.writes_memory ||
491
nir->info.fs.uses_fbfetch_output ||
492
nir->info.fs.needs_quad_helper_invocations ||
493
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_FRAG_COORD) ||
494
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_POINT_COORD) ||
495
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_SAMPLE_ID) ||
496
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_SAMPLE_POS) ||
497
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_SAMPLE_MASK_IN) ||
498
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_HELPER_INVOCATION));
499
}
500
501
/* Add color inputs to the list of inputs. */
502
if (nir->info.stage == MESA_SHADER_FRAGMENT) {
503
for (unsigned i = 0; i < 2; i++) {
504
if ((info->colors_read >> (i * 4)) & 0xf) {
505
info->input_semantic[info->num_inputs] = VARYING_SLOT_COL0 + i;
506
info->input_interpolate[info->num_inputs] = info->color_interpolate[i];
507
info->input_usage_mask[info->num_inputs] = info->colors_read >> (i * 4);
508
info->num_inputs++;
509
}
510
}
511
}
512
513
/* Trim output read masks based on write masks. */
514
for (unsigned i = 0; i < info->num_outputs; i++)
515
info->output_readmask[i] &= info->output_usagemask[i];
516
}
517
518
static bool si_alu_to_scalar_filter(const nir_instr *instr, const void *data)
519
{
520
struct si_screen *sscreen = (struct si_screen *)data;
521
522
if (sscreen->options.fp16 &&
523
instr->type == nir_instr_type_alu) {
524
nir_alu_instr *alu = nir_instr_as_alu(instr);
525
526
if (alu->dest.dest.is_ssa &&
527
alu->dest.dest.ssa.bit_size == 16 &&
528
alu->dest.dest.ssa.num_components == 2)
529
return false;
530
}
531
532
return true;
533
}
534
535
void si_nir_opts(struct si_screen *sscreen, struct nir_shader *nir, bool first)
536
{
537
bool progress;
538
539
NIR_PASS_V(nir, nir_lower_vars_to_ssa);
540
NIR_PASS_V(nir, nir_lower_alu_to_scalar, si_alu_to_scalar_filter, sscreen);
541
NIR_PASS_V(nir, nir_lower_phis_to_scalar, false);
542
543
do {
544
progress = false;
545
bool lower_alu_to_scalar = false;
546
bool lower_phis_to_scalar = false;
547
548
if (first) {
549
NIR_PASS(progress, nir, nir_split_array_vars, nir_var_function_temp);
550
NIR_PASS(lower_alu_to_scalar, nir, nir_shrink_vec_array_vars, nir_var_function_temp);
551
NIR_PASS(progress, nir, nir_opt_find_array_copies);
552
}
553
NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
554
NIR_PASS(progress, nir, nir_opt_dead_write_vars);
555
556
NIR_PASS(lower_alu_to_scalar, nir, nir_opt_trivial_continues);
557
/* (Constant) copy propagation is needed for txf with offsets. */
558
NIR_PASS(progress, nir, nir_copy_prop);
559
NIR_PASS(progress, nir, nir_opt_remove_phis);
560
NIR_PASS(progress, nir, nir_opt_dce);
561
NIR_PASS(lower_phis_to_scalar, nir, nir_opt_if, true);
562
NIR_PASS(progress, nir, nir_opt_dead_cf);
563
564
if (lower_alu_to_scalar)
565
NIR_PASS_V(nir, nir_lower_alu_to_scalar, si_alu_to_scalar_filter, sscreen);
566
if (lower_phis_to_scalar)
567
NIR_PASS_V(nir, nir_lower_phis_to_scalar, false);
568
progress |= lower_alu_to_scalar | lower_phis_to_scalar;
569
570
NIR_PASS(progress, nir, nir_opt_cse);
571
NIR_PASS(progress, nir, nir_opt_peephole_select, 8, true, true);
572
573
/* Needed for algebraic lowering */
574
NIR_PASS(progress, nir, nir_opt_algebraic);
575
NIR_PASS(progress, nir, nir_opt_constant_folding);
576
577
if (!nir->info.flrp_lowered) {
578
unsigned lower_flrp = (nir->options->lower_flrp16 ? 16 : 0) |
579
(nir->options->lower_flrp32 ? 32 : 0) |
580
(nir->options->lower_flrp64 ? 64 : 0);
581
assert(lower_flrp);
582
bool lower_flrp_progress = false;
583
584
NIR_PASS(lower_flrp_progress, nir, nir_lower_flrp, lower_flrp, false /* always_precise */);
585
if (lower_flrp_progress) {
586
NIR_PASS(progress, nir, nir_opt_constant_folding);
587
progress = true;
588
}
589
590
/* Nothing should rematerialize any flrps, so we only
591
* need to do this lowering once.
592
*/
593
nir->info.flrp_lowered = true;
594
}
595
596
NIR_PASS(progress, nir, nir_opt_undef);
597
NIR_PASS(progress, nir, nir_opt_conditional_discard);
598
if (nir->options->max_unroll_iterations) {
599
NIR_PASS(progress, nir, nir_opt_loop_unroll, 0);
600
}
601
602
if (nir->info.stage == MESA_SHADER_FRAGMENT)
603
NIR_PASS_V(nir, nir_opt_move_discards_to_top);
604
605
if (sscreen->options.fp16)
606
NIR_PASS(progress, nir, nir_opt_vectorize, NULL, NULL);
607
} while (progress);
608
609
NIR_PASS_V(nir, nir_lower_var_copies);
610
}
611
612
void si_nir_late_opts(nir_shader *nir)
613
{
614
bool more_late_algebraic = true;
615
while (more_late_algebraic) {
616
more_late_algebraic = false;
617
NIR_PASS(more_late_algebraic, nir, nir_opt_algebraic_late);
618
NIR_PASS_V(nir, nir_opt_constant_folding);
619
NIR_PASS_V(nir, nir_copy_prop);
620
NIR_PASS_V(nir, nir_opt_dce);
621
NIR_PASS_V(nir, nir_opt_cse);
622
}
623
}
624
625
static void si_late_optimize_16bit_samplers(struct si_screen *sscreen, nir_shader *nir)
626
{
627
/* Optimize and fix types of image_sample sources and destinations.
628
*
629
* The image_sample constraints are:
630
* nir_tex_src_coord: has_a16 ? select 16 or 32 : 32
631
* nir_tex_src_comparator: 32
632
* nir_tex_src_offset: 32
633
* nir_tex_src_bias: 32
634
* nir_tex_src_lod: match coord
635
* nir_tex_src_min_lod: match coord
636
* nir_tex_src_ms_index: match coord
637
* nir_tex_src_ddx: has_g16 && coord == 32 ? select 16 or 32 : match coord
638
* nir_tex_src_ddy: match ddy
639
*
640
* coord and ddx are selected optimally. The types of the rest are legalized
641
* based on those two.
642
*/
643
/* TODO: The constraints can't represent the ddx constraint. */
644
/*bool has_g16 = sscreen->info.chip_class >= GFX10 && LLVM_VERSION_MAJOR >= 12;*/
645
bool has_g16 = false;
646
nir_tex_src_type_constraints tex_constraints = {
647
[nir_tex_src_comparator] = {true, 32},
648
[nir_tex_src_offset] = {true, 32},
649
[nir_tex_src_bias] = {true, 32},
650
[nir_tex_src_lod] = {true, 0, nir_tex_src_coord},
651
[nir_tex_src_min_lod] = {true, 0, nir_tex_src_coord},
652
[nir_tex_src_ms_index] = {true, 0, nir_tex_src_coord},
653
[nir_tex_src_ddx] = {!has_g16, 0, nir_tex_src_coord},
654
[nir_tex_src_ddy] = {true, 0, has_g16 ? nir_tex_src_ddx : nir_tex_src_coord},
655
};
656
bool changed = false;
657
658
NIR_PASS(changed, nir, nir_fold_16bit_sampler_conversions,
659
(1 << nir_tex_src_coord) |
660
(has_g16 ? 1 << nir_tex_src_ddx : 0));
661
NIR_PASS(changed, nir, nir_legalize_16bit_sampler_srcs, tex_constraints);
662
663
if (changed) {
664
si_nir_opts(sscreen, nir, false);
665
si_nir_late_opts(nir);
666
}
667
}
668
669
static int type_size_vec4(const struct glsl_type *type, bool bindless)
670
{
671
return glsl_count_attribute_slots(type, false);
672
}
673
674
static void si_nir_lower_color(nir_shader *nir)
675
{
676
nir_function_impl *entrypoint = nir_shader_get_entrypoint(nir);
677
678
nir_builder b;
679
nir_builder_init(&b, entrypoint);
680
681
nir_foreach_block (block, entrypoint) {
682
nir_foreach_instr_safe (instr, block) {
683
if (instr->type != nir_instr_type_intrinsic)
684
continue;
685
686
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
687
688
if (intrin->intrinsic != nir_intrinsic_load_deref)
689
continue;
690
691
nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
692
if (!nir_deref_mode_is(deref, nir_var_shader_in))
693
continue;
694
695
b.cursor = nir_before_instr(instr);
696
nir_variable *var = nir_deref_instr_get_variable(deref);
697
nir_ssa_def *def;
698
699
if (var->data.location == VARYING_SLOT_COL0) {
700
def = nir_load_color0(&b);
701
nir->info.fs.color0_interp = var->data.interpolation;
702
nir->info.fs.color0_sample = var->data.sample;
703
nir->info.fs.color0_centroid = var->data.centroid;
704
} else if (var->data.location == VARYING_SLOT_COL1) {
705
def = nir_load_color1(&b);
706
nir->info.fs.color1_interp = var->data.interpolation;
707
nir->info.fs.color1_sample = var->data.sample;
708
nir->info.fs.color1_centroid = var->data.centroid;
709
} else {
710
continue;
711
}
712
713
nir_ssa_def_rewrite_uses(&intrin->dest.ssa, def);
714
nir_instr_remove(instr);
715
}
716
}
717
}
718
719
static void si_lower_io(struct nir_shader *nir)
720
{
721
/* HW supports indirect indexing for: | Enabled in driver
722
* -------------------------------------------------------
723
* TCS inputs | Yes
724
* TES inputs | Yes
725
* GS inputs | No
726
* -------------------------------------------------------
727
* VS outputs before TCS | No
728
* TCS outputs | Yes
729
* VS/TES outputs before GS | No
730
*/
731
bool has_indirect_inputs = nir->info.stage == MESA_SHADER_TESS_CTRL ||
732
nir->info.stage == MESA_SHADER_TESS_EVAL;
733
bool has_indirect_outputs = nir->info.stage == MESA_SHADER_TESS_CTRL;
734
735
if (!has_indirect_inputs || !has_indirect_outputs) {
736
NIR_PASS_V(nir, nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir),
737
!has_indirect_outputs, !has_indirect_inputs);
738
739
/* Since we're doing nir_lower_io_to_temporaries late, we need
740
* to lower all the copy_deref's introduced by
741
* lower_io_to_temporaries before calling nir_lower_io.
742
*/
743
NIR_PASS_V(nir, nir_split_var_copies);
744
NIR_PASS_V(nir, nir_lower_var_copies);
745
NIR_PASS_V(nir, nir_lower_global_vars_to_local);
746
}
747
748
/* The vectorization must be done after nir_lower_io_to_temporaries, because
749
* nir_lower_io_to_temporaries after vectorization breaks:
750
* piglit/bin/arb_gpu_shader5-interpolateAtOffset -auto -fbo
751
* TODO: It's probably a bug in nir_lower_io_to_temporaries.
752
*
753
* The vectorizer can only vectorize this:
754
* op src0.x, src1.x
755
* op src0.y, src1.y
756
*
757
* So it requires that inputs are already vectors and it must be the same
758
* vector between instructions. The vectorizer doesn't create vectors
759
* from independent scalar sources, so vectorize inputs.
760
*
761
* TODO: The pass fails this for VS: assert(b.shader->info.stage != MESA_SHADER_VERTEX);
762
*/
763
if (nir->info.stage != MESA_SHADER_VERTEX)
764
NIR_PASS_V(nir, nir_lower_io_to_vector, nir_var_shader_in);
765
766
/* Vectorize outputs, so that we don't split vectors before storing outputs. */
767
/* TODO: The pass fails an assertion for other shader stages. */
768
if (nir->info.stage == MESA_SHADER_TESS_CTRL ||
769
nir->info.stage == MESA_SHADER_FRAGMENT)
770
NIR_PASS_V(nir, nir_lower_io_to_vector, nir_var_shader_out);
771
772
if (nir->info.stage == MESA_SHADER_FRAGMENT)
773
si_nir_lower_color(nir);
774
775
NIR_PASS_V(nir, nir_lower_io, nir_var_shader_out | nir_var_shader_in,
776
type_size_vec4, nir_lower_io_lower_64bit_to_32);
777
nir->info.io_lowered = true;
778
779
/* This pass needs actual constants */
780
NIR_PASS_V(nir, nir_opt_constant_folding);
781
NIR_PASS_V(nir, nir_io_add_const_offset_to_base, nir_var_shader_in |
782
nir_var_shader_out);
783
784
/* Remove dead derefs, so that nir_validate doesn't fail. */
785
NIR_PASS_V(nir, nir_opt_dce);
786
787
/* Remove input and output nir_variables, because we don't need them
788
* anymore. Also remove uniforms, because those should have been lowered
789
* to UBOs already.
790
*/
791
unsigned modes = nir_var_shader_in | nir_var_shader_out | nir_var_uniform;
792
nir_foreach_variable_with_modes_safe(var, nir, modes) {
793
if (var->data.mode == nir_var_uniform &&
794
(glsl_type_get_image_count(var->type) ||
795
glsl_type_get_sampler_count(var->type)))
796
continue;
797
798
exec_node_remove(&var->node);
799
}
800
}
801
802
/**
803
* Perform "lowering" operations on the NIR that are run once when the shader
804
* selector is created.
805
*/
806
static void si_lower_nir(struct si_screen *sscreen, struct nir_shader *nir)
807
{
808
/* Perform lowerings (and optimizations) of code.
809
*
810
* Performance considerations aside, we must:
811
* - lower certain ALU operations
812
* - ensure constant offsets for texture instructions are folded
813
* and copy-propagated
814
*/
815
816
static const struct nir_lower_tex_options lower_tex_options = {
817
.lower_txp = ~0u,
818
};
819
NIR_PASS_V(nir, nir_lower_tex, &lower_tex_options);
820
821
const nir_lower_subgroups_options subgroups_options = {
822
.subgroup_size = 64,
823
.ballot_bit_size = 64,
824
.ballot_components = 1,
825
.lower_to_scalar = true,
826
.lower_subgroup_masks = true,
827
.lower_vote_trivial = false,
828
.lower_vote_eq = true,
829
.lower_elect = true,
830
};
831
NIR_PASS_V(nir, nir_lower_subgroups, &subgroups_options);
832
833
NIR_PASS_V(nir, nir_lower_discard_or_demote,
834
sscreen->debug_flags & DBG(FS_CORRECT_DERIVS_AFTER_KILL));
835
836
/* Lower load constants to scalar and then clean up the mess */
837
NIR_PASS_V(nir, nir_lower_load_const_to_scalar);
838
NIR_PASS_V(nir, nir_lower_var_copies);
839
NIR_PASS_V(nir, nir_opt_intrinsics);
840
NIR_PASS_V(nir, nir_lower_system_values);
841
NIR_PASS_V(nir, nir_lower_compute_system_values, NULL);
842
843
if (nir->info.stage == MESA_SHADER_COMPUTE) {
844
if (nir->info.cs.derivative_group == DERIVATIVE_GROUP_QUADS) {
845
/* If we are shuffling local_invocation_id for quad derivatives, we
846
* need to derive local_invocation_index from local_invocation_id
847
* first, so that the value corresponds to the shuffled
848
* local_invocation_id.
849
*/
850
nir_lower_compute_system_values_options options = {0};
851
options.lower_local_invocation_index = true;
852
NIR_PASS_V(nir, nir_lower_compute_system_values, &options);
853
}
854
855
nir_opt_cse(nir); /* CSE load_local_invocation_id */
856
nir_lower_compute_system_values_options options = {0};
857
options.shuffle_local_ids_for_quad_derivatives = true;
858
NIR_PASS_V(nir, nir_lower_compute_system_values, &options);
859
}
860
861
if (sscreen->b.get_shader_param(&sscreen->b, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_FP16)) {
862
NIR_PASS_V(nir, nir_lower_mediump_io,
863
/* TODO: LLVM fails to compile this test if VS inputs are 16-bit:
864
* dEQP-GLES31.functional.shaders.builtin_functions.integer.bitfieldinsert.uvec3_lowp_geometry
865
*/
866
(nir->info.stage != MESA_SHADER_VERTEX ? nir_var_shader_in : 0) | nir_var_shader_out,
867
BITFIELD64_BIT(VARYING_SLOT_PNTC) | BITFIELD64_RANGE(VARYING_SLOT_VAR0, 32),
868
true);
869
}
870
871
si_nir_opts(sscreen, nir, true);
872
873
/* Lower large variables that are always constant with load_constant
874
* intrinsics, which get turned into PC-relative loads from a data
875
* section next to the shader.
876
*
877
* st/mesa calls finalize_nir twice, but we can't call this pass twice.
878
*/
879
bool changed = false;
880
if (!nir->constant_data) {
881
/* The pass crashes if there are dead temps of lowered IO interface types. */
882
NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp, NULL);
883
NIR_PASS(changed, nir, nir_opt_large_constants, glsl_get_natural_size_align_bytes, 16);
884
}
885
886
changed |= ac_nir_lower_indirect_derefs(nir, sscreen->info.chip_class);
887
if (changed)
888
si_nir_opts(sscreen, nir, false);
889
890
/* Run late optimizations to fuse ffma and eliminate 16-bit conversions. */
891
si_nir_late_opts(nir);
892
893
if (sscreen->b.get_shader_param(&sscreen->b, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_FP16))
894
si_late_optimize_16bit_samplers(sscreen, nir);
895
896
NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp, NULL);
897
}
898
899
void si_finalize_nir(struct pipe_screen *screen, void *nirptr, bool optimize)
900
{
901
struct si_screen *sscreen = (struct si_screen *)screen;
902
struct nir_shader *nir = (struct nir_shader *)nirptr;
903
904
si_lower_io(nir);
905
si_lower_nir(sscreen, nir);
906
nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
907
908
if (sscreen->options.inline_uniforms)
909
nir_find_inlinable_uniforms(nir);
910
}
911
912