radeonsi/nir: fix input processing for packed varyings
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_nir.c
1 /*
2 * Copyright 2017 Advanced Micro Devices, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "si_shader_internal.h"
25 #include "si_pipe.h"
26
27 #include "ac_nir_to_llvm.h"
28
29 #include "tgsi/tgsi_from_mesa.h"
30
31 #include "compiler/nir/nir.h"
32 #include "compiler/nir_types.h"
33
34
35 static void scan_instruction(struct tgsi_shader_info *info,
36 nir_instr *instr)
37 {
38 if (instr->type == nir_instr_type_alu) {
39 nir_alu_instr *alu = nir_instr_as_alu(instr);
40
41 switch (alu->op) {
42 case nir_op_fddx:
43 case nir_op_fddy:
44 case nir_op_fddx_fine:
45 case nir_op_fddy_fine:
46 case nir_op_fddx_coarse:
47 case nir_op_fddy_coarse:
48 info->uses_derivatives = true;
49 break;
50 default:
51 break;
52 }
53 } else if (instr->type == nir_instr_type_tex) {
54 nir_tex_instr *tex = nir_instr_as_tex(instr);
55
56 if (!tex->texture) {
57 info->samplers_declared |=
58 u_bit_consecutive(tex->sampler_index, 1);
59 }
60
61 switch (tex->op) {
62 case nir_texop_tex:
63 case nir_texop_txb:
64 case nir_texop_lod:
65 info->uses_derivatives = true;
66 break;
67 default:
68 break;
69 }
70 } else if (instr->type == nir_instr_type_intrinsic) {
71 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
72
73 switch (intr->intrinsic) {
74 case nir_intrinsic_load_front_face:
75 info->uses_frontface = 1;
76 break;
77 case nir_intrinsic_load_instance_id:
78 info->uses_instanceid = 1;
79 break;
80 case nir_intrinsic_load_invocation_id:
81 info->uses_invocationid = true;
82 break;
83 case nir_intrinsic_load_num_work_groups:
84 info->uses_grid_size = true;
85 break;
86 case nir_intrinsic_load_local_group_size:
87 /* The block size is translated to IMM with a fixed block size. */
88 if (info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] == 0)
89 info->uses_block_size = true;
90 break;
91 case nir_intrinsic_load_local_invocation_id:
92 case nir_intrinsic_load_work_group_id: {
93 unsigned mask = nir_ssa_def_components_read(&intr->dest.ssa);
94 while (mask) {
95 unsigned i = u_bit_scan(&mask);
96
97 if (intr->intrinsic == nir_intrinsic_load_work_group_id)
98 info->uses_block_id[i] = true;
99 else
100 info->uses_thread_id[i] = true;
101 }
102 break;
103 }
104 case nir_intrinsic_load_vertex_id:
105 info->uses_vertexid = 1;
106 break;
107 case nir_intrinsic_load_vertex_id_zero_base:
108 info->uses_vertexid_nobase = 1;
109 break;
110 case nir_intrinsic_load_base_vertex:
111 info->uses_basevertex = 1;
112 break;
113 case nir_intrinsic_load_primitive_id:
114 info->uses_primid = 1;
115 break;
116 case nir_intrinsic_load_sample_mask_in:
117 info->reads_samplemask = true;
118 break;
119 case nir_intrinsic_load_tess_level_inner:
120 case nir_intrinsic_load_tess_level_outer:
121 info->reads_tess_factors = true;
122 break;
123 case nir_intrinsic_image_var_store:
124 case nir_intrinsic_image_var_atomic_add:
125 case nir_intrinsic_image_var_atomic_min:
126 case nir_intrinsic_image_var_atomic_max:
127 case nir_intrinsic_image_var_atomic_and:
128 case nir_intrinsic_image_var_atomic_or:
129 case nir_intrinsic_image_var_atomic_xor:
130 case nir_intrinsic_image_var_atomic_exchange:
131 case nir_intrinsic_image_var_atomic_comp_swap:
132 case nir_intrinsic_store_ssbo:
133 case nir_intrinsic_ssbo_atomic_add:
134 case nir_intrinsic_ssbo_atomic_imin:
135 case nir_intrinsic_ssbo_atomic_umin:
136 case nir_intrinsic_ssbo_atomic_imax:
137 case nir_intrinsic_ssbo_atomic_umax:
138 case nir_intrinsic_ssbo_atomic_and:
139 case nir_intrinsic_ssbo_atomic_or:
140 case nir_intrinsic_ssbo_atomic_xor:
141 case nir_intrinsic_ssbo_atomic_exchange:
142 case nir_intrinsic_ssbo_atomic_comp_swap:
143 info->writes_memory = true;
144 break;
145 case nir_intrinsic_load_var: {
146 nir_variable *var = intr->variables[0]->var;
147 nir_variable_mode mode = var->data.mode;
148 enum glsl_base_type base_type =
149 glsl_get_base_type(glsl_without_array(var->type));
150
151 if (mode == nir_var_shader_in) {
152 switch (var->data.interpolation) {
153 case INTERP_MODE_NONE:
154 if (glsl_base_type_is_integer(base_type))
155 break;
156
157 /* fall-through */
158 case INTERP_MODE_SMOOTH:
159 if (var->data.sample)
160 info->uses_persp_sample = true;
161 else if (var->data.centroid)
162 info->uses_persp_centroid = true;
163 else
164 info->uses_persp_center = true;
165 break;
166
167 case INTERP_MODE_NOPERSPECTIVE:
168 if (var->data.sample)
169 info->uses_linear_sample = true;
170 else if (var->data.centroid)
171 info->uses_linear_centroid = true;
172 else
173 info->uses_linear_center = true;
174 break;
175 }
176 }
177 break;
178 }
179 case nir_intrinsic_interp_var_at_centroid:
180 case nir_intrinsic_interp_var_at_sample:
181 case nir_intrinsic_interp_var_at_offset: {
182 enum glsl_interp_mode interp =
183 intr->variables[0]->var->data.interpolation;
184 switch (interp) {
185 case INTERP_MODE_SMOOTH:
186 case INTERP_MODE_NONE:
187 if (intr->intrinsic == nir_intrinsic_interp_var_at_centroid)
188 info->uses_persp_opcode_interp_centroid = true;
189 else if (intr->intrinsic == nir_intrinsic_interp_var_at_sample)
190 info->uses_persp_opcode_interp_sample = true;
191 else
192 info->uses_persp_opcode_interp_offset = true;
193 break;
194 case INTERP_MODE_NOPERSPECTIVE:
195 if (intr->intrinsic == nir_intrinsic_interp_var_at_centroid)
196 info->uses_linear_opcode_interp_centroid = true;
197 else if (intr->intrinsic == nir_intrinsic_interp_var_at_sample)
198 info->uses_linear_opcode_interp_sample = true;
199 else
200 info->uses_linear_opcode_interp_offset = true;
201 break;
202 case INTERP_MODE_FLAT:
203 break;
204 default:
205 unreachable("Unsupported interpoation type");
206 }
207 break;
208 }
209 default:
210 break;
211 }
212 }
213 }
214
215 void si_nir_scan_tess_ctrl(const struct nir_shader *nir,
216 const struct tgsi_shader_info *info,
217 struct tgsi_tessctrl_info *out)
218 {
219 memset(out, 0, sizeof(*out));
220
221 if (nir->info.stage != MESA_SHADER_TESS_CTRL)
222 return;
223
224 /* Initial value = true. Here the pass will accumulate results from
225 * multiple segments surrounded by barriers. If tess factors aren't
226 * written at all, it's a shader bug and we don't care if this will be
227 * true.
228 */
229 out->tessfactors_are_def_in_all_invocs = true;
230
231 /* TODO: Implement scanning of tess factors, see tgsi backend. */
232 }
233
234 void si_nir_scan_shader(const struct nir_shader *nir,
235 struct tgsi_shader_info *info)
236 {
237 nir_function *func;
238 unsigned i;
239
240 info->processor = pipe_shader_type_from_mesa(nir->info.stage);
241 info->num_tokens = 2; /* indicate that the shader is non-empty */
242 info->num_instructions = 2;
243
244 if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
245 info->properties[TGSI_PROPERTY_TCS_VERTICES_OUT] =
246 nir->info.tess.tcs_vertices_out;
247 }
248
249 if (nir->info.stage == MESA_SHADER_TESS_EVAL) {
250 if (nir->info.tess.primitive_mode == GL_ISOLINES)
251 info->properties[TGSI_PROPERTY_TES_PRIM_MODE] = PIPE_PRIM_LINES;
252 else
253 info->properties[TGSI_PROPERTY_TES_PRIM_MODE] = nir->info.tess.primitive_mode;
254
255 STATIC_ASSERT((TESS_SPACING_EQUAL + 1) % 3 == PIPE_TESS_SPACING_EQUAL);
256 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD + 1) % 3 ==
257 PIPE_TESS_SPACING_FRACTIONAL_ODD);
258 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN + 1) % 3 ==
259 PIPE_TESS_SPACING_FRACTIONAL_EVEN);
260
261 info->properties[TGSI_PROPERTY_TES_SPACING] = (nir->info.tess.spacing + 1) % 3;
262 info->properties[TGSI_PROPERTY_TES_VERTEX_ORDER_CW] = !nir->info.tess.ccw;
263 info->properties[TGSI_PROPERTY_TES_POINT_MODE] = nir->info.tess.point_mode;
264 }
265
266 if (nir->info.stage == MESA_SHADER_GEOMETRY) {
267 info->properties[TGSI_PROPERTY_GS_INPUT_PRIM] = nir->info.gs.input_primitive;
268 info->properties[TGSI_PROPERTY_GS_OUTPUT_PRIM] = nir->info.gs.output_primitive;
269 info->properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES] = nir->info.gs.vertices_out;
270 info->properties[TGSI_PROPERTY_GS_INVOCATIONS] = nir->info.gs.invocations;
271 }
272
273 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
274 info->properties[TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL] =
275 nir->info.fs.early_fragment_tests | nir->info.fs.post_depth_coverage;
276 info->properties[TGSI_PROPERTY_FS_POST_DEPTH_COVERAGE] = nir->info.fs.post_depth_coverage;
277
278 if (nir->info.fs.pixel_center_integer) {
279 info->properties[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER] =
280 TGSI_FS_COORD_PIXEL_CENTER_INTEGER;
281 }
282
283 if (nir->info.fs.depth_layout != FRAG_DEPTH_LAYOUT_NONE) {
284 switch (nir->info.fs.depth_layout) {
285 case FRAG_DEPTH_LAYOUT_ANY:
286 info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_ANY;
287 break;
288 case FRAG_DEPTH_LAYOUT_GREATER:
289 info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_GREATER;
290 break;
291 case FRAG_DEPTH_LAYOUT_LESS:
292 info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_LESS;
293 break;
294 case FRAG_DEPTH_LAYOUT_UNCHANGED:
295 info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_UNCHANGED;
296 break;
297 default:
298 unreachable("Unknow depth layout");
299 }
300 }
301 }
302
303 if (nir->info.stage == MESA_SHADER_COMPUTE) {
304 info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] = nir->info.cs.local_size[0];
305 info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT] = nir->info.cs.local_size[1];
306 info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH] = nir->info.cs.local_size[2];
307 }
308
309 i = 0;
310 uint64_t processed_inputs = 0;
311 unsigned num_inputs = 0;
312 nir_foreach_variable(variable, &nir->inputs) {
313 unsigned semantic_name, semantic_index;
314
315 const struct glsl_type *type = variable->type;
316 if (nir_is_per_vertex_io(variable, nir->info.stage)) {
317 assert(glsl_type_is_array(type));
318 type = glsl_get_array_element(type);
319 }
320
321 unsigned attrib_count = glsl_count_attribute_slots(type,
322 nir->info.stage == MESA_SHADER_VERTEX);
323
324 i = variable->data.driver_location;
325
326 /* Vertex shader inputs don't have semantics. The state
327 * tracker has already mapped them to attributes via
328 * variable->data.driver_location.
329 */
330 if (nir->info.stage == MESA_SHADER_VERTEX) {
331 /* TODO: gather the actual input useage and remove this. */
332 info->input_usage_mask[i] = TGSI_WRITEMASK_XYZW;
333
334 if (glsl_type_is_dual_slot(variable->type)) {
335 num_inputs += 2;
336
337 /* TODO: gather the actual input useage and remove this. */
338 info->input_usage_mask[i+1] = TGSI_WRITEMASK_XYZW;
339 } else
340 num_inputs++;
341 continue;
342 }
343
344 /* Fragment shader position is a system value. */
345 if (nir->info.stage == MESA_SHADER_FRAGMENT &&
346 variable->data.location == VARYING_SLOT_POS) {
347 if (variable->data.pixel_center_integer)
348 info->properties[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER] =
349 TGSI_FS_COORD_PIXEL_CENTER_INTEGER;
350
351 num_inputs++;
352 continue;
353 }
354
355 for (unsigned j = 0; j < attrib_count; j++, i++) {
356
357 if (processed_inputs & ((uint64_t)1 << i))
358 continue;
359
360 processed_inputs |= ((uint64_t)1 << i);
361 num_inputs++;
362
363 tgsi_get_gl_varying_semantic(variable->data.location + j, true,
364 &semantic_name, &semantic_index);
365
366 info->input_semantic_name[i] = semantic_name;
367 info->input_semantic_index[i] = semantic_index;
368
369 if (semantic_name == TGSI_SEMANTIC_PRIMID)
370 info->uses_primid = true;
371
372 if (variable->data.sample)
373 info->input_interpolate_loc[i] = TGSI_INTERPOLATE_LOC_SAMPLE;
374 else if (variable->data.centroid)
375 info->input_interpolate_loc[i] = TGSI_INTERPOLATE_LOC_CENTROID;
376 else
377 info->input_interpolate_loc[i] = TGSI_INTERPOLATE_LOC_CENTER;
378
379 enum glsl_base_type base_type =
380 glsl_get_base_type(glsl_without_array(variable->type));
381
382 switch (variable->data.interpolation) {
383 case INTERP_MODE_NONE:
384 if (glsl_base_type_is_integer(base_type)) {
385 info->input_interpolate[i] = TGSI_INTERPOLATE_CONSTANT;
386 break;
387 }
388
389 if (semantic_name == TGSI_SEMANTIC_COLOR) {
390 info->input_interpolate[i] = TGSI_INTERPOLATE_COLOR;
391 break;
392 }
393 /* fall-through */
394
395 case INTERP_MODE_SMOOTH:
396 assert(!glsl_base_type_is_integer(base_type));
397
398 info->input_interpolate[i] = TGSI_INTERPOLATE_PERSPECTIVE;
399 break;
400
401 case INTERP_MODE_NOPERSPECTIVE:
402 assert(!glsl_base_type_is_integer(base_type));
403
404 info->input_interpolate[i] = TGSI_INTERPOLATE_LINEAR;
405 break;
406
407 case INTERP_MODE_FLAT:
408 info->input_interpolate[i] = TGSI_INTERPOLATE_CONSTANT;
409 break;
410 }
411
412 /* TODO make this more precise */
413 if (variable->data.location == VARYING_SLOT_COL0)
414 info->colors_read |= 0x0f;
415 else if (variable->data.location == VARYING_SLOT_COL1)
416 info->colors_read |= 0xf0;
417 }
418 }
419
420 info->num_inputs = num_inputs;
421
422
423 i = 0;
424 uint64_t processed_outputs = 0;
425 unsigned num_outputs = 0;
426 nir_foreach_variable(variable, &nir->outputs) {
427 unsigned semantic_name, semantic_index;
428
429 i = variable->data.driver_location;
430
431 const struct glsl_type *type = variable->type;
432 if (nir_is_per_vertex_io(variable, nir->info.stage)) {
433 assert(glsl_type_is_array(type));
434 type = glsl_get_array_element(type);
435 }
436
437 unsigned attrib_count = glsl_count_attribute_slots(type, false);
438 for (unsigned k = 0; k < attrib_count; k++, i++) {
439
440 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
441 tgsi_get_gl_frag_result_semantic(variable->data.location + k,
442 &semantic_name, &semantic_index);
443
444 /* Adjust for dual source blending */
445 if (variable->data.index > 0) {
446 semantic_index++;
447 }
448 } else {
449 tgsi_get_gl_varying_semantic(variable->data.location + k, true,
450 &semantic_name, &semantic_index);
451 }
452
453 unsigned num_components = 4;
454 unsigned vector_elements = glsl_get_vector_elements(glsl_without_array(variable->type));
455 if (vector_elements)
456 num_components = vector_elements;
457
458 unsigned component = variable->data.location_frac;
459 if (glsl_type_is_64bit(glsl_without_array(variable->type))) {
460 if (glsl_type_is_dual_slot(glsl_without_array(variable->type)) && k % 2) {
461 num_components = (num_components * 2) - 4;
462 component = 0;
463 } else {
464 num_components = MIN2(num_components * 2, 4);
465 }
466 }
467
468 ubyte usagemask = 0;
469 for (unsigned j = component; j < num_components + component; j++) {
470 switch (j) {
471 case 0:
472 usagemask |= TGSI_WRITEMASK_X;
473 break;
474 case 1:
475 usagemask |= TGSI_WRITEMASK_Y;
476 break;
477 case 2:
478 usagemask |= TGSI_WRITEMASK_Z;
479 break;
480 case 3:
481 usagemask |= TGSI_WRITEMASK_W;
482 break;
483 default:
484 unreachable("error calculating component index");
485 }
486 }
487
488 unsigned gs_out_streams;
489 if (variable->data.stream & (1u << 31)) {
490 gs_out_streams = variable->data.stream & ~(1u << 31);
491 } else {
492 assert(variable->data.stream < 4);
493 gs_out_streams = 0;
494 for (unsigned j = 0; j < num_components; ++j)
495 gs_out_streams |= variable->data.stream << (2 * (component + j));
496 }
497
498 unsigned streamx = gs_out_streams & 3;
499 unsigned streamy = (gs_out_streams >> 2) & 3;
500 unsigned streamz = (gs_out_streams >> 4) & 3;
501 unsigned streamw = (gs_out_streams >> 6) & 3;
502
503 if (usagemask & TGSI_WRITEMASK_X) {
504 info->output_usagemask[i] |= TGSI_WRITEMASK_X;
505 info->output_streams[i] |= streamx;
506 info->num_stream_output_components[streamx]++;
507 }
508 if (usagemask & TGSI_WRITEMASK_Y) {
509 info->output_usagemask[i] |= TGSI_WRITEMASK_Y;
510 info->output_streams[i] |= streamy << 2;
511 info->num_stream_output_components[streamy]++;
512 }
513 if (usagemask & TGSI_WRITEMASK_Z) {
514 info->output_usagemask[i] |= TGSI_WRITEMASK_Z;
515 info->output_streams[i] |= streamz << 4;
516 info->num_stream_output_components[streamz]++;
517 }
518 if (usagemask & TGSI_WRITEMASK_W) {
519 info->output_usagemask[i] |= TGSI_WRITEMASK_W;
520 info->output_streams[i] |= streamw << 6;
521 info->num_stream_output_components[streamw]++;
522 }
523
524 /* make sure we only count this location once against
525 * the num_outputs counter.
526 */
527 if (processed_outputs & ((uint64_t)1 << i))
528 continue;
529
530 processed_outputs |= ((uint64_t)1 << i);
531 num_outputs++;
532
533 info->output_semantic_name[i] = semantic_name;
534 info->output_semantic_index[i] = semantic_index;
535
536 switch (semantic_name) {
537 case TGSI_SEMANTIC_PRIMID:
538 info->writes_primid = true;
539 break;
540 case TGSI_SEMANTIC_VIEWPORT_INDEX:
541 info->writes_viewport_index = true;
542 break;
543 case TGSI_SEMANTIC_LAYER:
544 info->writes_layer = true;
545 break;
546 case TGSI_SEMANTIC_PSIZE:
547 info->writes_psize = true;
548 break;
549 case TGSI_SEMANTIC_CLIPVERTEX:
550 info->writes_clipvertex = true;
551 break;
552 case TGSI_SEMANTIC_COLOR:
553 info->colors_written |= 1 << semantic_index;
554 break;
555 case TGSI_SEMANTIC_STENCIL:
556 info->writes_stencil = true;
557 break;
558 case TGSI_SEMANTIC_SAMPLEMASK:
559 info->writes_samplemask = true;
560 break;
561 case TGSI_SEMANTIC_EDGEFLAG:
562 info->writes_edgeflag = true;
563 break;
564 case TGSI_SEMANTIC_POSITION:
565 if (info->processor == PIPE_SHADER_FRAGMENT)
566 info->writes_z = true;
567 else
568 info->writes_position = true;
569 break;
570 }
571
572 if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
573 switch (semantic_name) {
574 case TGSI_SEMANTIC_PATCH:
575 info->reads_perpatch_outputs = true;
576 break;
577 case TGSI_SEMANTIC_TESSINNER:
578 case TGSI_SEMANTIC_TESSOUTER:
579 info->reads_tessfactor_outputs = true;
580 break;
581 default:
582 info->reads_pervertex_outputs = true;
583 }
584 }
585 }
586
587 unsigned loc = variable->data.location;
588 if (loc == FRAG_RESULT_COLOR &&
589 nir->info.outputs_written & (1ull << loc)) {
590 assert(attrib_count == 1);
591 info->properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] = true;
592 }
593 }
594
595 info->num_outputs = num_outputs;
596
597 nir_foreach_variable(variable, &nir->uniforms) {
598 const struct glsl_type *type = variable->type;
599 enum glsl_base_type base_type =
600 glsl_get_base_type(glsl_without_array(type));
601 unsigned aoa_size = MAX2(1, glsl_get_aoa_size(type));
602
603 /* We rely on the fact that nir_lower_samplers_as_deref has
604 * eliminated struct dereferences.
605 */
606 if (base_type == GLSL_TYPE_SAMPLER)
607 info->samplers_declared |=
608 u_bit_consecutive(variable->data.binding, aoa_size);
609 else if (base_type == GLSL_TYPE_IMAGE)
610 info->images_declared |=
611 u_bit_consecutive(variable->data.binding, aoa_size);
612 }
613
614 info->num_written_clipdistance = nir->info.clip_distance_array_size;
615 info->num_written_culldistance = nir->info.cull_distance_array_size;
616 info->clipdist_writemask = u_bit_consecutive(0, info->num_written_clipdistance);
617 info->culldist_writemask = u_bit_consecutive(0, info->num_written_culldistance);
618
619 if (info->processor == PIPE_SHADER_FRAGMENT)
620 info->uses_kill = nir->info.fs.uses_discard;
621
622 /* TODO make this more accurate */
623 info->const_buffers_declared = u_bit_consecutive(0, SI_NUM_CONST_BUFFERS);
624 info->shader_buffers_declared = u_bit_consecutive(0, SI_NUM_SHADER_BUFFERS);
625
626 func = (struct nir_function *)exec_list_get_head_const(&nir->functions);
627 nir_foreach_block(block, func->impl) {
628 nir_foreach_instr(instr, block)
629 scan_instruction(info, instr);
630 }
631 }
632
633 /**
634 * Perform "lowering" operations on the NIR that are run once when the shader
635 * selector is created.
636 */
637 void
638 si_lower_nir(struct si_shader_selector* sel)
639 {
640 /* Adjust the driver location of inputs and outputs. The state tracker
641 * interprets them as slots, while the ac/nir backend interprets them
642 * as individual components.
643 */
644 nir_foreach_variable(variable, &sel->nir->inputs)
645 variable->data.driver_location *= 4;
646
647 nir_foreach_variable(variable, &sel->nir->outputs) {
648 variable->data.driver_location *= 4;
649
650 if (sel->nir->info.stage == MESA_SHADER_FRAGMENT) {
651 if (variable->data.location == FRAG_RESULT_DEPTH)
652 variable->data.driver_location += 2;
653 else if (variable->data.location == FRAG_RESULT_STENCIL)
654 variable->data.driver_location += 1;
655 }
656 }
657
658 /* Perform lowerings (and optimizations) of code.
659 *
660 * Performance considerations aside, we must:
661 * - lower certain ALU operations
662 * - ensure constant offsets for texture instructions are folded
663 * and copy-propagated
664 */
665 NIR_PASS_V(sel->nir, nir_lower_returns);
666 NIR_PASS_V(sel->nir, nir_lower_vars_to_ssa);
667 NIR_PASS_V(sel->nir, nir_lower_alu_to_scalar);
668 NIR_PASS_V(sel->nir, nir_lower_phis_to_scalar);
669
670 static const struct nir_lower_tex_options lower_tex_options = {
671 .lower_txp = ~0u,
672 };
673 NIR_PASS_V(sel->nir, nir_lower_tex, &lower_tex_options);
674
675 const nir_lower_subgroups_options subgroups_options = {
676 .subgroup_size = 64,
677 .ballot_bit_size = 64,
678 .lower_to_scalar = true,
679 .lower_subgroup_masks = true,
680 .lower_vote_trivial = false,
681 .lower_vote_eq_to_ballot = true,
682 };
683 NIR_PASS_V(sel->nir, nir_lower_subgroups, &subgroups_options);
684
685 ac_lower_indirect_derefs(sel->nir, sel->screen->info.chip_class);
686
687 bool progress;
688 do {
689 progress = false;
690
691 /* (Constant) copy propagation is needed for txf with offsets. */
692 NIR_PASS(progress, sel->nir, nir_copy_prop);
693 NIR_PASS(progress, sel->nir, nir_opt_remove_phis);
694 NIR_PASS(progress, sel->nir, nir_opt_dce);
695 if (nir_opt_trivial_continues(sel->nir)) {
696 progress = true;
697 NIR_PASS(progress, sel->nir, nir_copy_prop);
698 NIR_PASS(progress, sel->nir, nir_opt_dce);
699 }
700 NIR_PASS(progress, sel->nir, nir_opt_if);
701 NIR_PASS(progress, sel->nir, nir_opt_dead_cf);
702 NIR_PASS(progress, sel->nir, nir_opt_cse);
703 NIR_PASS(progress, sel->nir, nir_opt_peephole_select, 8);
704
705 /* Needed for algebraic lowering */
706 NIR_PASS(progress, sel->nir, nir_opt_algebraic);
707 NIR_PASS(progress, sel->nir, nir_opt_constant_folding);
708
709 NIR_PASS(progress, sel->nir, nir_opt_undef);
710 NIR_PASS(progress, sel->nir, nir_opt_conditional_discard);
711 if (sel->nir->options->max_unroll_iterations) {
712 NIR_PASS(progress, sel->nir, nir_opt_loop_unroll, 0);
713 }
714 } while (progress);
715 }
716
717 static void declare_nir_input_vs(struct si_shader_context *ctx,
718 struct nir_variable *variable,
719 unsigned input_index,
720 LLVMValueRef out[4])
721 {
722 si_llvm_load_input_vs(ctx, input_index, out);
723 }
724
725 static void declare_nir_input_fs(struct si_shader_context *ctx,
726 struct nir_variable *variable,
727 unsigned input_index,
728 LLVMValueRef out[4])
729 {
730 unsigned slot = variable->data.location;
731 if (slot == VARYING_SLOT_POS) {
732 out[0] = LLVMGetParam(ctx->main_fn, SI_PARAM_POS_X_FLOAT);
733 out[1] = LLVMGetParam(ctx->main_fn, SI_PARAM_POS_Y_FLOAT);
734 out[2] = LLVMGetParam(ctx->main_fn, SI_PARAM_POS_Z_FLOAT);
735 out[3] = ac_build_fdiv(&ctx->ac, ctx->ac.f32_1,
736 LLVMGetParam(ctx->main_fn, SI_PARAM_POS_W_FLOAT));
737 return;
738 }
739
740 si_llvm_load_input_fs(ctx, input_index, out);
741 }
742
743 LLVMValueRef
744 si_nir_lookup_interp_param(struct ac_shader_abi *abi,
745 enum glsl_interp_mode interp, unsigned location)
746 {
747 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
748 int interp_param_idx = -1;
749
750 switch (interp) {
751 case INTERP_MODE_FLAT:
752 return NULL;
753 case INTERP_MODE_SMOOTH:
754 case INTERP_MODE_NONE:
755 if (location == INTERP_CENTER)
756 interp_param_idx = SI_PARAM_PERSP_CENTER;
757 else if (location == INTERP_CENTROID)
758 interp_param_idx = SI_PARAM_PERSP_CENTROID;
759 else if (location == INTERP_SAMPLE)
760 interp_param_idx = SI_PARAM_PERSP_SAMPLE;
761 break;
762 case INTERP_MODE_NOPERSPECTIVE:
763 if (location == INTERP_CENTER)
764 interp_param_idx = SI_PARAM_LINEAR_CENTER;
765 else if (location == INTERP_CENTROID)
766 interp_param_idx = SI_PARAM_LINEAR_CENTROID;
767 else if (location == INTERP_SAMPLE)
768 interp_param_idx = SI_PARAM_LINEAR_SAMPLE;
769 break;
770 default:
771 assert(!"Unhandled interpolation mode.");
772 return NULL;
773 }
774
775 return interp_param_idx != -1 ?
776 LLVMGetParam(ctx->main_fn, interp_param_idx) : NULL;
777 }
778
779 static LLVMValueRef
780 si_nir_load_sampler_desc(struct ac_shader_abi *abi,
781 unsigned descriptor_set, unsigned base_index,
782 unsigned constant_index, LLVMValueRef dynamic_index,
783 enum ac_descriptor_type desc_type, bool image,
784 bool write)
785 {
786 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
787 LLVMBuilderRef builder = ctx->ac.builder;
788 LLVMValueRef list = LLVMGetParam(ctx->main_fn, ctx->param_samplers_and_images);
789 LLVMValueRef index = dynamic_index;
790
791 assert(!descriptor_set);
792
793 if (!index)
794 index = ctx->ac.i32_0;
795
796 index = LLVMBuildAdd(builder, index,
797 LLVMConstInt(ctx->ac.i32, base_index + constant_index, false),
798 "");
799
800 if (image) {
801 assert(desc_type == AC_DESC_IMAGE || desc_type == AC_DESC_BUFFER);
802 assert(base_index + constant_index < ctx->num_images);
803
804 if (dynamic_index)
805 index = si_llvm_bound_index(ctx, index, ctx->num_images);
806
807 index = LLVMBuildSub(ctx->gallivm.builder,
808 LLVMConstInt(ctx->i32, SI_NUM_IMAGES - 1, 0),
809 index, "");
810
811 /* TODO: be smarter about when we use dcc_off */
812 return si_load_image_desc(ctx, list, index, desc_type, write);
813 }
814
815 assert(base_index + constant_index < ctx->num_samplers);
816
817 if (dynamic_index)
818 index = si_llvm_bound_index(ctx, index, ctx->num_samplers);
819
820 index = LLVMBuildAdd(ctx->gallivm.builder, index,
821 LLVMConstInt(ctx->i32, SI_NUM_IMAGES / 2, 0), "");
822
823 return si_load_sampler_desc(ctx, list, index, desc_type);
824 }
825
826 static void bitcast_inputs(struct si_shader_context *ctx,
827 LLVMValueRef data[4],
828 unsigned input_idx)
829 {
830 for (unsigned chan = 0; chan < 4; chan++) {
831 ctx->inputs[input_idx + chan] =
832 LLVMBuildBitCast(ctx->ac.builder, data[chan], ctx->ac.i32, "");
833 }
834 }
835
836 bool si_nir_build_llvm(struct si_shader_context *ctx, struct nir_shader *nir)
837 {
838 struct tgsi_shader_info *info = &ctx->shader->selector->info;
839
840 if (nir->info.stage == MESA_SHADER_VERTEX ||
841 nir->info.stage == MESA_SHADER_FRAGMENT) {
842 uint64_t processed_inputs = 0;
843 nir_foreach_variable(variable, &nir->inputs) {
844 unsigned attrib_count = glsl_count_attribute_slots(variable->type,
845 nir->info.stage == MESA_SHADER_VERTEX);
846 unsigned input_idx = variable->data.driver_location;
847
848 LLVMValueRef data[4];
849 unsigned loc = variable->data.location;
850
851 for (unsigned i = 0; i < attrib_count; i++) {
852 /* Packed components share the same location so skip
853 * them if we have already processed the location.
854 */
855 if (processed_inputs & ((uint64_t)1 << (loc + i))) {
856 input_idx += 4;
857 continue;
858 }
859
860 if (nir->info.stage == MESA_SHADER_VERTEX) {
861 declare_nir_input_vs(ctx, variable, input_idx / 4, data);
862 bitcast_inputs(ctx, data, input_idx);
863 if (glsl_type_is_dual_slot(variable->type)) {
864 input_idx += 4;
865 declare_nir_input_vs(ctx, variable, input_idx / 4, data);
866 bitcast_inputs(ctx, data, input_idx);
867 }
868 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
869 declare_nir_input_fs(ctx, variable, input_idx / 4, data);
870 bitcast_inputs(ctx, data, input_idx);
871 }
872
873 processed_inputs |= ((uint64_t)1 << (loc + i));
874 input_idx += 4;
875 }
876 }
877 }
878
879 ctx->abi.inputs = &ctx->inputs[0];
880 ctx->abi.load_sampler_desc = si_nir_load_sampler_desc;
881 ctx->abi.clamp_shadow_reference = true;
882
883 ctx->num_samplers = util_last_bit(info->samplers_declared);
884 ctx->num_images = util_last_bit(info->images_declared);
885
886 if (ctx->shader->selector->local_size) {
887 assert(nir->info.stage == MESA_SHADER_COMPUTE);
888 si_declare_compute_memory(ctx);
889 }
890 ac_nir_translate(&ctx->ac, &ctx->abi, nir);
891
892 return true;
893 }