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