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