radeonsi/nir: don't run si_nir_opts again if there is no change
[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 #include "compiler/nir/nir_builder.h"
35 #include "compiler/nir/nir_deref.h"
36
37 static nir_variable* tex_get_texture_var(nir_tex_instr *instr)
38 {
39 for (unsigned i = 0; i < instr->num_srcs; i++) {
40 switch (instr->src[i].src_type) {
41 case nir_tex_src_texture_deref:
42 return nir_deref_instr_get_variable(nir_src_as_deref(instr->src[i].src));
43 default:
44 break;
45 }
46 }
47
48 return NULL;
49 }
50
51 static nir_variable* intrinsic_get_var(nir_intrinsic_instr *instr)
52 {
53 return nir_deref_instr_get_variable(nir_src_as_deref(instr->src[0]));
54 }
55
56 static void gather_usage_helper(const nir_deref_instr **deref_ptr,
57 unsigned location,
58 uint8_t mask,
59 uint8_t *usage_mask)
60 {
61 for (; *deref_ptr; deref_ptr++) {
62 const nir_deref_instr *deref = *deref_ptr;
63 switch (deref->deref_type) {
64 case nir_deref_type_array: {
65 unsigned elem_size =
66 glsl_count_attribute_slots(deref->type, false);
67 if (nir_src_is_const(deref->arr.index)) {
68 location += elem_size * nir_src_as_uint(deref->arr.index);
69 } else {
70 unsigned array_elems =
71 glsl_get_length(deref_ptr[-1]->type);
72 for (unsigned i = 0; i < array_elems; i++) {
73 gather_usage_helper(deref_ptr + 1,
74 location + elem_size * i,
75 mask, usage_mask);
76 }
77 return;
78 }
79 break;
80 }
81 case nir_deref_type_struct: {
82 const struct glsl_type *parent_type =
83 deref_ptr[-1]->type;
84 unsigned index = deref->strct.index;
85 for (unsigned i = 0; i < index; i++) {
86 const struct glsl_type *ft = glsl_get_struct_field(parent_type, i);
87 location += glsl_count_attribute_slots(ft, false);
88 }
89 break;
90 }
91 default:
92 unreachable("Unhandled deref type in gather_components_used_helper");
93 }
94 }
95
96 usage_mask[location] |= mask & 0xf;
97 if (mask & 0xf0)
98 usage_mask[location + 1] |= (mask >> 4) & 0xf;
99 }
100
101 static void gather_usage(const nir_deref_instr *deref,
102 uint8_t mask,
103 uint8_t *usage_mask)
104 {
105 nir_deref_path path;
106 nir_deref_path_init(&path, (nir_deref_instr *)deref, NULL);
107
108 unsigned location_frac = path.path[0]->var->data.location_frac;
109 if (glsl_type_is_64bit(deref->type)) {
110 uint8_t new_mask = 0;
111 for (unsigned i = 0; i < 4; i++) {
112 if (mask & (1 << i))
113 new_mask |= 0x3 << (2 * i);
114 }
115 mask = new_mask << location_frac;
116 } else {
117 mask <<= location_frac;
118 mask &= 0xf;
119 }
120
121 gather_usage_helper((const nir_deref_instr **)&path.path[1],
122 path.path[0]->var->data.driver_location,
123 mask, usage_mask);
124
125 nir_deref_path_finish(&path);
126 }
127
128 static void gather_intrinsic_load_deref_input_info(const nir_shader *nir,
129 const nir_intrinsic_instr *instr,
130 const nir_deref_instr *deref,
131 struct tgsi_shader_info *info)
132 {
133 switch (nir->info.stage) {
134 case MESA_SHADER_VERTEX:
135 gather_usage(deref, nir_ssa_def_components_read(&instr->dest.ssa),
136 info->input_usage_mask);
137 default:;
138 }
139 }
140
141 static void gather_intrinsic_load_deref_output_info(const nir_shader *nir,
142 const nir_intrinsic_instr *instr,
143 nir_variable *var,
144 struct tgsi_shader_info *info)
145 {
146 assert(var && var->data.mode == nir_var_shader_out);
147
148 switch (nir->info.stage) {
149 case MESA_SHADER_TESS_CTRL:
150 if (var->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
151 var->data.location == VARYING_SLOT_TESS_LEVEL_OUTER)
152 info->reads_tessfactor_outputs = true;
153 else if (var->data.patch)
154 info->reads_perpatch_outputs = true;
155 else
156 info->reads_pervertex_outputs = true;
157 break;
158
159 case MESA_SHADER_FRAGMENT:
160 if (var->data.fb_fetch_output)
161 info->uses_fbfetch = true;
162 break;
163 default:;
164 }
165 }
166
167 static void gather_intrinsic_store_deref_output_info(const nir_shader *nir,
168 const nir_intrinsic_instr *instr,
169 const nir_deref_instr *deref,
170 struct tgsi_shader_info *info)
171 {
172 switch (nir->info.stage) {
173 case MESA_SHADER_VERTEX: /* needed by LS, ES */
174 case MESA_SHADER_TESS_EVAL: /* needed by ES */
175 case MESA_SHADER_GEOMETRY:
176 gather_usage(deref, nir_intrinsic_write_mask(instr),
177 info->output_usagemask);
178 break;
179 default:;
180 }
181 }
182
183 static void scan_instruction(const struct nir_shader *nir,
184 struct tgsi_shader_info *info,
185 nir_instr *instr)
186 {
187 if (instr->type == nir_instr_type_alu) {
188 nir_alu_instr *alu = nir_instr_as_alu(instr);
189
190 switch (alu->op) {
191 case nir_op_fddx:
192 case nir_op_fddy:
193 case nir_op_fddx_fine:
194 case nir_op_fddy_fine:
195 case nir_op_fddx_coarse:
196 case nir_op_fddy_coarse:
197 info->uses_derivatives = true;
198 break;
199 default:
200 break;
201 }
202 } else if (instr->type == nir_instr_type_tex) {
203 nir_tex_instr *tex = nir_instr_as_tex(instr);
204 nir_variable *texture = tex_get_texture_var(tex);
205
206 if (!texture) {
207 info->samplers_declared |=
208 u_bit_consecutive(tex->sampler_index, 1);
209 } else {
210 if (texture->data.bindless)
211 info->uses_bindless_samplers = true;
212 }
213
214 switch (tex->op) {
215 case nir_texop_tex:
216 case nir_texop_txb:
217 case nir_texop_lod:
218 info->uses_derivatives = true;
219 break;
220 default:
221 break;
222 }
223 } else if (instr->type == nir_instr_type_intrinsic) {
224 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
225
226 switch (intr->intrinsic) {
227 case nir_intrinsic_load_front_face:
228 info->uses_frontface = 1;
229 break;
230 case nir_intrinsic_load_instance_id:
231 info->uses_instanceid = 1;
232 break;
233 case nir_intrinsic_load_invocation_id:
234 info->uses_invocationid = true;
235 break;
236 case nir_intrinsic_load_num_work_groups:
237 info->uses_grid_size = true;
238 break;
239 case nir_intrinsic_load_local_group_size:
240 /* The block size is translated to IMM with a fixed block size. */
241 if (info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] == 0)
242 info->uses_block_size = true;
243 break;
244 case nir_intrinsic_load_local_invocation_id:
245 case nir_intrinsic_load_work_group_id: {
246 unsigned mask = nir_ssa_def_components_read(&intr->dest.ssa);
247 while (mask) {
248 unsigned i = u_bit_scan(&mask);
249
250 if (intr->intrinsic == nir_intrinsic_load_work_group_id)
251 info->uses_block_id[i] = true;
252 else
253 info->uses_thread_id[i] = true;
254 }
255 break;
256 }
257 case nir_intrinsic_load_vertex_id:
258 info->uses_vertexid = 1;
259 break;
260 case nir_intrinsic_load_vertex_id_zero_base:
261 info->uses_vertexid_nobase = 1;
262 break;
263 case nir_intrinsic_load_base_vertex:
264 info->uses_basevertex = 1;
265 break;
266 case nir_intrinsic_load_draw_id:
267 info->uses_drawid = 1;
268 break;
269 case nir_intrinsic_load_primitive_id:
270 info->uses_primid = 1;
271 break;
272 case nir_intrinsic_load_sample_mask_in:
273 info->reads_samplemask = true;
274 break;
275 case nir_intrinsic_load_tess_level_inner:
276 case nir_intrinsic_load_tess_level_outer:
277 info->reads_tess_factors = true;
278 break;
279 case nir_intrinsic_bindless_image_load:
280 info->uses_bindless_images = true;
281
282 if (nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_BUF)
283 info->uses_bindless_buffer_load = true;
284 else
285 info->uses_bindless_image_load = true;
286 break;
287 case nir_intrinsic_bindless_image_size:
288 case nir_intrinsic_bindless_image_samples:
289 info->uses_bindless_images = true;
290 break;
291 case nir_intrinsic_bindless_image_store:
292 info->uses_bindless_images = true;
293
294 if (nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_BUF)
295 info->uses_bindless_buffer_store = true;
296 else
297 info->uses_bindless_image_store = true;
298
299 info->writes_memory = true;
300 info->num_memory_instructions++; /* we only care about stores */
301 break;
302 case nir_intrinsic_image_deref_store:
303 info->writes_memory = true;
304 info->num_memory_instructions++; /* we only care about stores */
305 break;
306 case nir_intrinsic_bindless_image_atomic_add:
307 case nir_intrinsic_bindless_image_atomic_imin:
308 case nir_intrinsic_bindless_image_atomic_umin:
309 case nir_intrinsic_bindless_image_atomic_imax:
310 case nir_intrinsic_bindless_image_atomic_umax:
311 case nir_intrinsic_bindless_image_atomic_and:
312 case nir_intrinsic_bindless_image_atomic_or:
313 case nir_intrinsic_bindless_image_atomic_xor:
314 case nir_intrinsic_bindless_image_atomic_exchange:
315 case nir_intrinsic_bindless_image_atomic_comp_swap:
316 info->uses_bindless_images = true;
317
318 if (nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_BUF)
319 info->uses_bindless_buffer_atomic = true;
320 else
321 info->uses_bindless_image_atomic = true;
322
323 info->writes_memory = true;
324 info->num_memory_instructions++; /* we only care about stores */
325 break;
326 case nir_intrinsic_image_deref_atomic_add:
327 case nir_intrinsic_image_deref_atomic_imin:
328 case nir_intrinsic_image_deref_atomic_umin:
329 case nir_intrinsic_image_deref_atomic_imax:
330 case nir_intrinsic_image_deref_atomic_umax:
331 case nir_intrinsic_image_deref_atomic_and:
332 case nir_intrinsic_image_deref_atomic_or:
333 case nir_intrinsic_image_deref_atomic_xor:
334 case nir_intrinsic_image_deref_atomic_exchange:
335 case nir_intrinsic_image_deref_atomic_comp_swap:
336 case nir_intrinsic_image_deref_atomic_inc_wrap:
337 case nir_intrinsic_image_deref_atomic_dec_wrap:
338 info->writes_memory = true;
339 info->num_memory_instructions++; /* we only care about stores */
340 break;
341 case nir_intrinsic_store_ssbo:
342 case nir_intrinsic_ssbo_atomic_add:
343 case nir_intrinsic_ssbo_atomic_imin:
344 case nir_intrinsic_ssbo_atomic_umin:
345 case nir_intrinsic_ssbo_atomic_imax:
346 case nir_intrinsic_ssbo_atomic_umax:
347 case nir_intrinsic_ssbo_atomic_and:
348 case nir_intrinsic_ssbo_atomic_or:
349 case nir_intrinsic_ssbo_atomic_xor:
350 case nir_intrinsic_ssbo_atomic_exchange:
351 case nir_intrinsic_ssbo_atomic_comp_swap:
352 info->writes_memory = true;
353 info->num_memory_instructions++; /* we only care about stores */
354 break;
355 case nir_intrinsic_load_color0:
356 case nir_intrinsic_load_color1: {
357 unsigned index = intr->intrinsic == nir_intrinsic_load_color1;
358 uint8_t mask = nir_ssa_def_components_read(&intr->dest.ssa);
359 info->colors_read |= mask << (index * 4);
360 break;
361 }
362 case nir_intrinsic_load_barycentric_pixel:
363 case nir_intrinsic_load_barycentric_centroid:
364 case nir_intrinsic_load_barycentric_sample:
365 case nir_intrinsic_load_barycentric_at_offset: /* uses center */
366 case nir_intrinsic_load_barycentric_at_sample: { /* uses center */
367 unsigned mode = nir_intrinsic_interp_mode(intr);
368
369 if (mode == INTERP_MODE_FLAT)
370 break;
371
372 if (mode == INTERP_MODE_NOPERSPECTIVE) {
373 if (intr->intrinsic == nir_intrinsic_load_barycentric_sample)
374 info->uses_linear_sample = true;
375 else if (intr->intrinsic == nir_intrinsic_load_barycentric_centroid)
376 info->uses_linear_centroid = true;
377 else
378 info->uses_linear_center = true;
379
380 if (intr->intrinsic == nir_intrinsic_load_barycentric_at_sample)
381 info->uses_linear_opcode_interp_sample = true;
382 } else {
383 if (intr->intrinsic == nir_intrinsic_load_barycentric_sample)
384 info->uses_persp_sample = true;
385 else if (intr->intrinsic == nir_intrinsic_load_barycentric_centroid)
386 info->uses_persp_centroid = true;
387 else
388 info->uses_persp_center = true;
389
390 if (intr->intrinsic == nir_intrinsic_load_barycentric_at_sample)
391 info->uses_persp_opcode_interp_sample = true;
392 }
393 break;
394 }
395 case nir_intrinsic_load_deref: {
396 nir_variable *var = intrinsic_get_var(intr);
397 nir_variable_mode mode = var->data.mode;
398
399 if (mode == nir_var_shader_in) {
400 /* PS inputs use the interpolated load intrinsics. */
401 assert(nir->info.stage != MESA_SHADER_FRAGMENT);
402 gather_intrinsic_load_deref_input_info(nir, intr,
403 nir_src_as_deref(intr->src[0]), info);
404 } else if (mode == nir_var_shader_out) {
405 gather_intrinsic_load_deref_output_info(nir, intr, var, info);
406 }
407 break;
408 }
409 case nir_intrinsic_store_deref: {
410 nir_variable *var = intrinsic_get_var(intr);
411
412 if (var->data.mode == nir_var_shader_out)
413 gather_intrinsic_store_deref_output_info(nir, intr,
414 nir_src_as_deref(intr->src[0]), info);
415 break;
416 }
417 case nir_intrinsic_interp_deref_at_centroid:
418 case nir_intrinsic_interp_deref_at_sample:
419 case nir_intrinsic_interp_deref_at_offset:
420 unreachable("interp opcodes should have been lowered");
421 break;
422 default:
423 break;
424 }
425 }
426 }
427
428 void si_nir_scan_tess_ctrl(const struct nir_shader *nir,
429 struct tgsi_tessctrl_info *out)
430 {
431 memset(out, 0, sizeof(*out));
432
433 if (nir->info.stage != MESA_SHADER_TESS_CTRL)
434 return;
435
436 out->tessfactors_are_def_in_all_invocs =
437 ac_are_tessfactors_def_in_all_invocs(nir);
438 }
439
440 static void scan_output_slot(const nir_variable *var,
441 unsigned var_idx,
442 unsigned component, unsigned num_components,
443 struct tgsi_shader_info *info)
444 {
445 assert(component + num_components <= 4);
446 assert(component < 4);
447
448 unsigned semantic_name, semantic_index;
449
450 unsigned location = var->data.location + var_idx;
451 unsigned drv_location = var->data.driver_location + var_idx;
452
453 if (info->processor == PIPE_SHADER_FRAGMENT) {
454 tgsi_get_gl_frag_result_semantic(location,
455 &semantic_name, &semantic_index);
456
457 /* Adjust for dual source blending */
458 if (var->data.index > 0) {
459 semantic_index++;
460 }
461 } else {
462 tgsi_get_gl_varying_semantic(location, true,
463 &semantic_name, &semantic_index);
464 }
465
466 ubyte usagemask = ((1 << num_components) - 1) << component;
467
468 unsigned gs_out_streams;
469 if (var->data.stream & NIR_STREAM_PACKED) {
470 gs_out_streams = var->data.stream & ~NIR_STREAM_PACKED;
471 } else {
472 assert(var->data.stream < 4);
473 gs_out_streams = 0;
474 for (unsigned j = 0; j < num_components; ++j)
475 gs_out_streams |= var->data.stream << (2 * (component + j));
476 }
477
478 unsigned streamx = gs_out_streams & 3;
479 unsigned streamy = (gs_out_streams >> 2) & 3;
480 unsigned streamz = (gs_out_streams >> 4) & 3;
481 unsigned streamw = (gs_out_streams >> 6) & 3;
482
483 if (usagemask & TGSI_WRITEMASK_X) {
484 info->output_streams[drv_location] |= streamx;
485 info->num_stream_output_components[streamx]++;
486 }
487 if (usagemask & TGSI_WRITEMASK_Y) {
488 info->output_streams[drv_location] |= streamy << 2;
489 info->num_stream_output_components[streamy]++;
490 }
491 if (usagemask & TGSI_WRITEMASK_Z) {
492 info->output_streams[drv_location] |= streamz << 4;
493 info->num_stream_output_components[streamz]++;
494 }
495 if (usagemask & TGSI_WRITEMASK_W) {
496 info->output_streams[drv_location] |= streamw << 6;
497 info->num_stream_output_components[streamw]++;
498 }
499
500 info->output_semantic_name[drv_location] = semantic_name;
501 info->output_semantic_index[drv_location] = semantic_index;
502
503 switch (semantic_name) {
504 case TGSI_SEMANTIC_PRIMID:
505 info->writes_primid = true;
506 break;
507 case TGSI_SEMANTIC_VIEWPORT_INDEX:
508 info->writes_viewport_index = true;
509 break;
510 case TGSI_SEMANTIC_LAYER:
511 info->writes_layer = true;
512 break;
513 case TGSI_SEMANTIC_PSIZE:
514 info->writes_psize = true;
515 break;
516 case TGSI_SEMANTIC_CLIPVERTEX:
517 info->writes_clipvertex = true;
518 break;
519 case TGSI_SEMANTIC_COLOR:
520 info->colors_written |= 1 << semantic_index;
521 break;
522 case TGSI_SEMANTIC_STENCIL:
523 info->writes_stencil = true;
524 break;
525 case TGSI_SEMANTIC_SAMPLEMASK:
526 info->writes_samplemask = true;
527 break;
528 case TGSI_SEMANTIC_EDGEFLAG:
529 info->writes_edgeflag = true;
530 break;
531 case TGSI_SEMANTIC_POSITION:
532 if (info->processor == PIPE_SHADER_FRAGMENT)
533 info->writes_z = true;
534 else
535 info->writes_position = true;
536 break;
537 }
538 }
539
540 static void scan_output_helper(const nir_variable *var,
541 unsigned location,
542 const struct glsl_type *type,
543 struct tgsi_shader_info *info)
544 {
545 if (glsl_type_is_struct(type)) {
546 for (unsigned i = 0; i < glsl_get_length(type); i++) {
547 const struct glsl_type *ft = glsl_get_struct_field(type, i);
548 scan_output_helper(var, location, ft, info);
549 location += glsl_count_attribute_slots(ft, false);
550 }
551 } else if (glsl_type_is_array_or_matrix(type)) {
552 const struct glsl_type *elem_type =
553 glsl_get_array_element(type);
554 unsigned num_elems = glsl_get_length(type);
555 if (var->data.compact) {
556 assert(glsl_type_is_scalar(elem_type));
557 assert(glsl_get_bit_size(elem_type) == 32);
558 unsigned component = var->data.location_frac;
559 scan_output_slot(var, location, component,
560 MIN2(num_elems, 4 - component), info);
561 if (component + num_elems > 4) {
562 scan_output_slot(var, location + 1, 0,
563 component + num_elems - 4, info);
564 }
565
566 } else {
567 unsigned elem_count = glsl_count_attribute_slots(elem_type, false);
568 for (unsigned i = 0; i < num_elems; i++) {
569 scan_output_helper(var, location, elem_type, info);
570 location += elem_count;
571 }
572 }
573 } else if (glsl_type_is_dual_slot(type)) {
574 unsigned component = var->data.location_frac;
575 scan_output_slot(var, location, component, 4 - component, info);
576 scan_output_slot(var, location + 1, 0, component + 2 * glsl_get_components(type) - 4,
577 info);
578 } else {
579 unsigned component = var->data.location_frac;
580 assert(glsl_type_is_vector_or_scalar(type));
581 unsigned num_components = glsl_get_components(type);
582 if (glsl_type_is_64bit(type))
583 num_components *= 2;
584 scan_output_slot(var, location, component, num_components, info);
585 }
586 }
587
588 void si_nir_scan_shader(const struct nir_shader *nir,
589 struct tgsi_shader_info *info)
590 {
591 nir_function *func;
592 unsigned i;
593
594 info->processor = pipe_shader_type_from_mesa(nir->info.stage);
595 info->num_tokens = 2; /* indicate that the shader is non-empty */
596 info->num_instructions = 2;
597
598 info->properties[TGSI_PROPERTY_NEXT_SHADER] =
599 pipe_shader_type_from_mesa(nir->info.next_stage);
600
601 if (nir->info.stage == MESA_SHADER_VERTEX) {
602 info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION] =
603 nir->info.vs.window_space_position;
604 info->properties[TGSI_PROPERTY_VS_BLIT_SGPRS_AMD] =
605 nir->info.vs.blit_sgprs_amd;
606 }
607
608 if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
609 info->properties[TGSI_PROPERTY_TCS_VERTICES_OUT] =
610 nir->info.tess.tcs_vertices_out;
611 }
612
613 if (nir->info.stage == MESA_SHADER_TESS_EVAL) {
614 if (nir->info.tess.primitive_mode == GL_ISOLINES)
615 info->properties[TGSI_PROPERTY_TES_PRIM_MODE] = PIPE_PRIM_LINES;
616 else
617 info->properties[TGSI_PROPERTY_TES_PRIM_MODE] = nir->info.tess.primitive_mode;
618
619 STATIC_ASSERT((TESS_SPACING_EQUAL + 1) % 3 == PIPE_TESS_SPACING_EQUAL);
620 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD + 1) % 3 ==
621 PIPE_TESS_SPACING_FRACTIONAL_ODD);
622 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN + 1) % 3 ==
623 PIPE_TESS_SPACING_FRACTIONAL_EVEN);
624
625 info->properties[TGSI_PROPERTY_TES_SPACING] = (nir->info.tess.spacing + 1) % 3;
626 info->properties[TGSI_PROPERTY_TES_VERTEX_ORDER_CW] = !nir->info.tess.ccw;
627 info->properties[TGSI_PROPERTY_TES_POINT_MODE] = nir->info.tess.point_mode;
628 }
629
630 if (nir->info.stage == MESA_SHADER_GEOMETRY) {
631 info->properties[TGSI_PROPERTY_GS_INPUT_PRIM] = nir->info.gs.input_primitive;
632 info->properties[TGSI_PROPERTY_GS_OUTPUT_PRIM] = nir->info.gs.output_primitive;
633 info->properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES] = nir->info.gs.vertices_out;
634 info->properties[TGSI_PROPERTY_GS_INVOCATIONS] = nir->info.gs.invocations;
635 }
636
637 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
638 info->properties[TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL] =
639 nir->info.fs.early_fragment_tests | nir->info.fs.post_depth_coverage;
640 info->properties[TGSI_PROPERTY_FS_POST_DEPTH_COVERAGE] = nir->info.fs.post_depth_coverage;
641
642 if (nir->info.fs.pixel_center_integer) {
643 info->properties[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER] =
644 TGSI_FS_COORD_PIXEL_CENTER_INTEGER;
645 }
646
647 if (nir->info.fs.depth_layout != FRAG_DEPTH_LAYOUT_NONE) {
648 switch (nir->info.fs.depth_layout) {
649 case FRAG_DEPTH_LAYOUT_ANY:
650 info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_ANY;
651 break;
652 case FRAG_DEPTH_LAYOUT_GREATER:
653 info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_GREATER;
654 break;
655 case FRAG_DEPTH_LAYOUT_LESS:
656 info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_LESS;
657 break;
658 case FRAG_DEPTH_LAYOUT_UNCHANGED:
659 info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_UNCHANGED;
660 break;
661 default:
662 unreachable("Unknow depth layout");
663 }
664 }
665 }
666
667 if (gl_shader_stage_is_compute(nir->info.stage)) {
668 info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] = nir->info.cs.local_size[0];
669 info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT] = nir->info.cs.local_size[1];
670 info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH] = nir->info.cs.local_size[2];
671 info->properties[TGSI_PROPERTY_CS_USER_DATA_COMPONENTS_AMD] = nir->info.cs.user_data_components_amd;
672 }
673
674 i = 0;
675 uint64_t processed_inputs = 0;
676 nir_foreach_variable(variable, &nir->inputs) {
677 unsigned semantic_name, semantic_index;
678
679 const struct glsl_type *type = variable->type;
680 if (nir_is_per_vertex_io(variable, nir->info.stage)) {
681 assert(glsl_type_is_array(type));
682 type = glsl_get_array_element(type);
683 }
684
685 unsigned attrib_count = glsl_count_attribute_slots(type,
686 nir->info.stage == MESA_SHADER_VERTEX);
687
688 i = variable->data.driver_location;
689
690 /* Vertex shader inputs don't have semantics. The state
691 * tracker has already mapped them to attributes via
692 * variable->data.driver_location.
693 */
694 if (nir->info.stage == MESA_SHADER_VERTEX)
695 continue;
696
697 for (unsigned j = 0; j < attrib_count; j++, i++) {
698
699 if (processed_inputs & ((uint64_t)1 << i))
700 continue;
701
702 processed_inputs |= ((uint64_t)1 << i);
703
704 tgsi_get_gl_varying_semantic(variable->data.location + j, true,
705 &semantic_name, &semantic_index);
706
707 info->input_semantic_name[i] = semantic_name;
708 info->input_semantic_index[i] = semantic_index;
709
710 if (semantic_name == TGSI_SEMANTIC_PRIMID)
711 info->uses_primid = true;
712
713 if (semantic_name == TGSI_SEMANTIC_COLOR) {
714 /* We only need this for color inputs. */
715 if (variable->data.sample)
716 info->input_interpolate_loc[i] = TGSI_INTERPOLATE_LOC_SAMPLE;
717 else if (variable->data.centroid)
718 info->input_interpolate_loc[i] = TGSI_INTERPOLATE_LOC_CENTROID;
719 else
720 info->input_interpolate_loc[i] = TGSI_INTERPOLATE_LOC_CENTER;
721 }
722
723 enum glsl_base_type base_type =
724 glsl_get_base_type(glsl_without_array(variable->type));
725
726 switch (variable->data.interpolation) {
727 case INTERP_MODE_NONE:
728 if (glsl_base_type_is_integer(base_type)) {
729 info->input_interpolate[i] = TGSI_INTERPOLATE_CONSTANT;
730 break;
731 }
732
733 if (semantic_name == TGSI_SEMANTIC_COLOR) {
734 info->input_interpolate[i] = TGSI_INTERPOLATE_COLOR;
735 break;
736 }
737 /* fall-through */
738
739 case INTERP_MODE_SMOOTH:
740 assert(!glsl_base_type_is_integer(base_type));
741
742 info->input_interpolate[i] = TGSI_INTERPOLATE_PERSPECTIVE;
743 break;
744
745 case INTERP_MODE_NOPERSPECTIVE:
746 assert(!glsl_base_type_is_integer(base_type));
747
748 info->input_interpolate[i] = TGSI_INTERPOLATE_LINEAR;
749 break;
750
751 case INTERP_MODE_FLAT:
752 info->input_interpolate[i] = TGSI_INTERPOLATE_CONSTANT;
753 break;
754 }
755 }
756 }
757
758 nir_foreach_variable(variable, &nir->outputs) {
759 const struct glsl_type *type = variable->type;
760 if (nir_is_per_vertex_io(variable, nir->info.stage)) {
761 assert(glsl_type_is_array(type));
762 type = glsl_get_array_element(type);
763 }
764
765 ASSERTED unsigned attrib_count = glsl_count_attribute_slots(type, false);
766 scan_output_helper(variable, 0, type, info);
767
768 unsigned loc = variable->data.location;
769 if (nir->info.stage == MESA_SHADER_FRAGMENT &&
770 loc == FRAG_RESULT_COLOR &&
771 nir->info.outputs_written & (1ull << loc)) {
772 assert(attrib_count == 1);
773 info->properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] = true;
774 }
775 }
776
777 info->num_inputs = nir->num_inputs;
778 info->num_outputs = nir->num_outputs;
779
780 info->const_file_max[0] = nir->num_uniforms - 1;
781 info->shader_buffers_declared = u_bit_consecutive(0, nir->info.num_ssbos);
782 info->const_buffers_declared = u_bit_consecutive(1, nir->info.num_ubos);
783 if (nir->num_uniforms > 0)
784 info->const_buffers_declared |= 1;
785 info->images_declared = u_bit_consecutive(0, nir->info.num_images);
786 info->msaa_images_declared = u_bit_consecutive(0, nir->info.last_msaa_image + 1);
787 info->samplers_declared = nir->info.textures_used;
788
789 info->num_written_clipdistance = nir->info.clip_distance_array_size;
790 info->num_written_culldistance = nir->info.cull_distance_array_size;
791 info->clipdist_writemask = u_bit_consecutive(0, info->num_written_clipdistance);
792 info->culldist_writemask = u_bit_consecutive(0, info->num_written_culldistance);
793
794 if (info->processor == PIPE_SHADER_FRAGMENT)
795 info->uses_kill = nir->info.fs.uses_discard;
796
797 func = (struct nir_function *)exec_list_get_head_const(&nir->functions);
798 nir_foreach_block(block, func->impl) {
799 nir_foreach_instr(instr, block)
800 scan_instruction(nir, info, instr);
801 }
802 }
803
804 static void
805 si_nir_opts(struct nir_shader *nir)
806 {
807 bool progress;
808
809 do {
810 progress = false;
811
812 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
813
814 NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
815 NIR_PASS(progress, nir, nir_opt_dead_write_vars);
816
817 NIR_PASS_V(nir, nir_lower_alu_to_scalar, NULL, NULL);
818 NIR_PASS_V(nir, nir_lower_phis_to_scalar);
819
820 /* (Constant) copy propagation is needed for txf with offsets. */
821 NIR_PASS(progress, nir, nir_copy_prop);
822 NIR_PASS(progress, nir, nir_opt_remove_phis);
823 NIR_PASS(progress, nir, nir_opt_dce);
824 if (nir_opt_trivial_continues(nir)) {
825 progress = true;
826 NIR_PASS(progress, nir, nir_copy_prop);
827 NIR_PASS(progress, nir, nir_opt_dce);
828 }
829 NIR_PASS(progress, nir, nir_opt_if, true);
830 NIR_PASS(progress, nir, nir_opt_dead_cf);
831 NIR_PASS(progress, nir, nir_opt_cse);
832 NIR_PASS(progress, nir, nir_opt_peephole_select, 8, true, true);
833
834 /* Needed for algebraic lowering */
835 NIR_PASS(progress, nir, nir_opt_algebraic);
836 NIR_PASS(progress, nir, nir_opt_constant_folding);
837
838 if (!nir->info.flrp_lowered) {
839 unsigned lower_flrp =
840 (nir->options->lower_flrp16 ? 16 : 0) |
841 (nir->options->lower_flrp32 ? 32 : 0) |
842 (nir->options->lower_flrp64 ? 64 : 0);
843 assert(lower_flrp);
844 bool lower_flrp_progress = false;
845
846 NIR_PASS(lower_flrp_progress, nir, nir_lower_flrp,
847 lower_flrp,
848 false /* always_precise */,
849 nir->options->lower_ffma);
850 if (lower_flrp_progress) {
851 NIR_PASS(progress, nir,
852 nir_opt_constant_folding);
853 progress = true;
854 }
855
856 /* Nothing should rematerialize any flrps, so we only
857 * need to do this lowering once.
858 */
859 nir->info.flrp_lowered = true;
860 }
861
862 NIR_PASS(progress, nir, nir_opt_undef);
863 NIR_PASS(progress, nir, nir_opt_conditional_discard);
864 if (nir->options->max_unroll_iterations) {
865 NIR_PASS(progress, nir, nir_opt_loop_unroll, 0);
866 }
867 } while (progress);
868 }
869
870 static int
871 type_size_vec4(const struct glsl_type *type, bool bindless)
872 {
873 return glsl_count_attribute_slots(type, false);
874 }
875
876 static void
877 si_nir_lower_color(nir_shader *nir)
878 {
879 nir_function_impl *entrypoint = nir_shader_get_entrypoint(nir);
880
881 nir_builder b;
882 nir_builder_init(&b, entrypoint);
883
884 nir_foreach_block(block, entrypoint) {
885 nir_foreach_instr_safe(instr, block) {
886 if (instr->type != nir_instr_type_intrinsic)
887 continue;
888
889 nir_intrinsic_instr *intrin =
890 nir_instr_as_intrinsic(instr);
891
892 if (intrin->intrinsic != nir_intrinsic_load_deref)
893 continue;
894
895 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
896 if (deref->mode != nir_var_shader_in)
897 continue;
898
899 b.cursor = nir_before_instr(instr);
900 nir_variable *var = nir_deref_instr_get_variable(deref);
901 nir_ssa_def *def;
902
903 if (var->data.location == VARYING_SLOT_COL0) {
904 def = nir_load_color0(&b);
905 } else if (var->data.location == VARYING_SLOT_COL1) {
906 def = nir_load_color1(&b);
907 } else {
908 continue;
909 }
910
911 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(def));
912 nir_instr_remove(instr);
913 }
914 }
915 }
916
917 static void si_nir_lower_ps_inputs(struct nir_shader *nir)
918 {
919 if (nir->info.stage != MESA_SHADER_FRAGMENT)
920 return;
921
922 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
923 nir_shader_get_entrypoint(nir), false, true);
924
925 /* Since we're doing nir_lower_io_to_temporaries late, we need
926 * to lower all the copy_deref's introduced by
927 * lower_io_to_temporaries before calling nir_lower_io.
928 */
929 NIR_PASS_V(nir, nir_split_var_copies);
930 NIR_PASS_V(nir, nir_lower_var_copies);
931 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
932
933 si_nir_lower_color(nir);
934 NIR_PASS_V(nir, nir_lower_io, nir_var_shader_in, type_size_vec4, 0);
935
936 /* This pass needs actual constants */
937 NIR_PASS_V(nir, nir_opt_constant_folding);
938 NIR_PASS_V(nir, nir_io_add_const_offset_to_base,
939 nir_var_shader_in);
940 }
941
942 void si_nir_adjust_driver_locations(struct nir_shader *nir)
943 {
944 /* Adjust the driver location of inputs and outputs. The state tracker
945 * interprets them as slots, while the ac/nir backend interprets them
946 * as individual components.
947 */
948 if (nir->info.stage != MESA_SHADER_FRAGMENT) {
949 nir_foreach_variable(variable, &nir->inputs)
950 variable->data.driver_location *= 4;
951 }
952
953 nir_foreach_variable(variable, &nir->outputs) {
954 variable->data.driver_location *= 4;
955
956 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
957 if (variable->data.location == FRAG_RESULT_DEPTH)
958 variable->data.driver_location += 2;
959 else if (variable->data.location == FRAG_RESULT_STENCIL)
960 variable->data.driver_location += 1;
961 }
962 }
963 }
964
965 /**
966 * Perform "lowering" operations on the NIR that are run once when the shader
967 * selector is created.
968 */
969 static void si_lower_nir(struct si_screen *sscreen, struct nir_shader *nir)
970 {
971 /* Perform lowerings (and optimizations) of code.
972 *
973 * Performance considerations aside, we must:
974 * - lower certain ALU operations
975 * - ensure constant offsets for texture instructions are folded
976 * and copy-propagated
977 */
978
979 static const struct nir_lower_tex_options lower_tex_options = {
980 .lower_txp = ~0u,
981 };
982 NIR_PASS_V(nir, nir_lower_tex, &lower_tex_options);
983
984 const nir_lower_subgroups_options subgroups_options = {
985 .subgroup_size = 64,
986 .ballot_bit_size = 64,
987 .lower_to_scalar = true,
988 .lower_subgroup_masks = true,
989 .lower_vote_trivial = false,
990 .lower_vote_eq_to_ballot = true,
991 };
992 NIR_PASS_V(nir, nir_lower_subgroups, &subgroups_options);
993
994 /* Lower load constants to scalar and then clean up the mess */
995 NIR_PASS_V(nir, nir_lower_load_const_to_scalar);
996 NIR_PASS_V(nir, nir_lower_var_copies);
997 NIR_PASS_V(nir, nir_lower_pack);
998 NIR_PASS_V(nir, nir_opt_access);
999 si_nir_opts(nir);
1000
1001 /* Lower large variables that are always constant with load_constant
1002 * intrinsics, which get turned into PC-relative loads from a data
1003 * section next to the shader.
1004 *
1005 * st/mesa calls finalize_nir twice, but we can't call this pass twice.
1006 */
1007 bool changed = false;
1008 if (!nir->constant_data) {
1009 NIR_PASS(changed, nir, nir_opt_large_constants,
1010 glsl_get_natural_size_align_bytes, 16);
1011 }
1012
1013 changed |= ac_lower_indirect_derefs(nir, sscreen->info.chip_class);
1014 if (changed)
1015 si_nir_opts(nir);
1016
1017 NIR_PASS_V(nir, nir_lower_bool_to_int32);
1018 NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp);
1019 }
1020
1021 void si_finalize_nir(struct pipe_screen *screen, void *nirptr, bool optimize)
1022 {
1023 struct si_screen *sscreen = (struct si_screen *)screen;
1024 struct nir_shader *nir = (struct nir_shader *)nirptr;
1025
1026 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
1027 si_nir_lower_ps_inputs(nir);
1028 si_lower_nir(sscreen, nir);
1029 }
1030
1031 static void declare_nir_input_vs(struct si_shader_context *ctx,
1032 struct nir_variable *variable,
1033 unsigned input_index,
1034 LLVMValueRef out[4])
1035 {
1036 si_llvm_load_input_vs(ctx, input_index, out);
1037 }
1038
1039 LLVMValueRef
1040 si_nir_lookup_interp_param(struct ac_shader_abi *abi,
1041 enum glsl_interp_mode interp, unsigned location)
1042 {
1043 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1044
1045 switch (interp) {
1046 case INTERP_MODE_FLAT:
1047 return NULL;
1048 case INTERP_MODE_SMOOTH:
1049 case INTERP_MODE_NONE:
1050 if (location == INTERP_CENTER)
1051 return ac_get_arg(&ctx->ac, ctx->args.persp_center);
1052 else if (location == INTERP_CENTROID)
1053 return ctx->abi.persp_centroid;
1054 else if (location == INTERP_SAMPLE)
1055 return ac_get_arg(&ctx->ac, ctx->args.persp_sample);
1056 break;
1057 case INTERP_MODE_NOPERSPECTIVE:
1058 if (location == INTERP_CENTER)
1059 return ac_get_arg(&ctx->ac, ctx->args.linear_center);
1060 else if (location == INTERP_CENTROID)
1061 return ac_get_arg(&ctx->ac, ctx->args.linear_centroid);
1062 else if (location == INTERP_SAMPLE)
1063 return ac_get_arg(&ctx->ac, ctx->args.linear_sample);
1064 break;
1065 default:
1066 assert(!"Unhandled interpolation mode.");
1067 }
1068 return NULL;
1069 }
1070
1071 static LLVMValueRef
1072 si_nir_load_sampler_desc(struct ac_shader_abi *abi,
1073 unsigned descriptor_set, unsigned base_index,
1074 unsigned constant_index, LLVMValueRef dynamic_index,
1075 enum ac_descriptor_type desc_type, bool image,
1076 bool write, bool bindless)
1077 {
1078 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1079 LLVMBuilderRef builder = ctx->ac.builder;
1080 unsigned const_index = base_index + constant_index;
1081
1082 assert(!descriptor_set);
1083 assert(desc_type <= AC_DESC_BUFFER);
1084
1085 if (bindless) {
1086 LLVMValueRef list = ac_get_arg(&ctx->ac, ctx->bindless_samplers_and_images);
1087
1088 /* dynamic_index is the bindless handle */
1089 if (image) {
1090 /* Bindless image descriptors use 16-dword slots. */
1091 dynamic_index = LLVMBuildMul(ctx->ac.builder, dynamic_index,
1092 LLVMConstInt(ctx->i64, 2, 0), "");
1093 /* FMASK is right after the image. */
1094 if (desc_type == AC_DESC_FMASK) {
1095 dynamic_index = LLVMBuildAdd(ctx->ac.builder, dynamic_index,
1096 ctx->i32_1, "");
1097 }
1098
1099 return si_load_image_desc(ctx, list, dynamic_index, desc_type,
1100 write, true);
1101 }
1102
1103 /* Since bindless handle arithmetic can contain an unsigned integer
1104 * wraparound and si_load_sampler_desc assumes there isn't any,
1105 * use GEP without "inbounds" (inside ac_build_pointer_add)
1106 * to prevent incorrect code generation and hangs.
1107 */
1108 dynamic_index = LLVMBuildMul(ctx->ac.builder, dynamic_index,
1109 LLVMConstInt(ctx->i64, 2, 0), "");
1110 list = ac_build_pointer_add(&ctx->ac, list, dynamic_index);
1111 return si_load_sampler_desc(ctx, list, ctx->i32_0, desc_type);
1112 }
1113
1114 unsigned num_slots = image ? ctx->num_images : ctx->num_samplers;
1115 assert(const_index < num_slots || dynamic_index);
1116
1117 LLVMValueRef list = ac_get_arg(&ctx->ac, ctx->samplers_and_images);
1118 LLVMValueRef index = LLVMConstInt(ctx->ac.i32, const_index, false);
1119
1120 if (dynamic_index) {
1121 index = LLVMBuildAdd(builder, index, dynamic_index, "");
1122
1123 /* From the GL_ARB_shader_image_load_store extension spec:
1124 *
1125 * If a shader performs an image load, store, or atomic
1126 * operation using an image variable declared as an array,
1127 * and if the index used to select an individual element is
1128 * negative or greater than or equal to the size of the
1129 * array, the results of the operation are undefined but may
1130 * not lead to termination.
1131 */
1132 index = si_llvm_bound_index(ctx, index, num_slots);
1133 }
1134
1135 if (image) {
1136 /* FMASKs are separate from images. */
1137 if (desc_type == AC_DESC_FMASK) {
1138 index = LLVMBuildAdd(ctx->ac.builder, index,
1139 LLVMConstInt(ctx->i32, SI_NUM_IMAGES, 0), "");
1140 }
1141 index = LLVMBuildSub(ctx->ac.builder,
1142 LLVMConstInt(ctx->i32, SI_NUM_IMAGE_SLOTS - 1, 0),
1143 index, "");
1144 return si_load_image_desc(ctx, list, index, desc_type, write, false);
1145 }
1146
1147 index = LLVMBuildAdd(ctx->ac.builder, index,
1148 LLVMConstInt(ctx->i32, SI_NUM_IMAGE_SLOTS / 2, 0), "");
1149 return si_load_sampler_desc(ctx, list, index, desc_type);
1150 }
1151
1152 static void bitcast_inputs(struct si_shader_context *ctx,
1153 LLVMValueRef data[4],
1154 unsigned input_idx)
1155 {
1156 for (unsigned chan = 0; chan < 4; chan++) {
1157 ctx->inputs[input_idx + chan] =
1158 LLVMBuildBitCast(ctx->ac.builder, data[chan], ctx->ac.i32, "");
1159 }
1160 }
1161
1162 bool si_nir_build_llvm(struct si_shader_context *ctx, struct nir_shader *nir)
1163 {
1164 struct tgsi_shader_info *info = &ctx->shader->selector->info;
1165
1166 if (nir->info.stage == MESA_SHADER_VERTEX) {
1167 uint64_t processed_inputs = 0;
1168 nir_foreach_variable(variable, &nir->inputs) {
1169 unsigned attrib_count = glsl_count_attribute_slots(variable->type,
1170 true);
1171 unsigned input_idx = variable->data.driver_location;
1172
1173 LLVMValueRef data[4];
1174 unsigned loc = variable->data.location;
1175
1176 for (unsigned i = 0; i < attrib_count; i++) {
1177 /* Packed components share the same location so skip
1178 * them if we have already processed the location.
1179 */
1180 if (processed_inputs & ((uint64_t)1 << (loc + i))) {
1181 input_idx += 4;
1182 continue;
1183 }
1184
1185 declare_nir_input_vs(ctx, variable, input_idx / 4, data);
1186 bitcast_inputs(ctx, data, input_idx);
1187 if (glsl_type_is_dual_slot(variable->type)) {
1188 input_idx += 4;
1189 declare_nir_input_vs(ctx, variable, input_idx / 4, data);
1190 bitcast_inputs(ctx, data, input_idx);
1191 }
1192
1193 processed_inputs |= ((uint64_t)1 << (loc + i));
1194 input_idx += 4;
1195 }
1196 }
1197 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
1198 unsigned colors_read =
1199 ctx->shader->selector->info.colors_read;
1200 LLVMValueRef main_fn = ctx->main_fn;
1201
1202 LLVMValueRef undef = LLVMGetUndef(ctx->f32);
1203
1204 unsigned offset = SI_PARAM_POS_FIXED_PT + 1;
1205
1206 if (colors_read & 0x0f) {
1207 unsigned mask = colors_read & 0x0f;
1208 LLVMValueRef values[4];
1209 values[0] = mask & 0x1 ? LLVMGetParam(main_fn, offset++) : undef;
1210 values[1] = mask & 0x2 ? LLVMGetParam(main_fn, offset++) : undef;
1211 values[2] = mask & 0x4 ? LLVMGetParam(main_fn, offset++) : undef;
1212 values[3] = mask & 0x8 ? LLVMGetParam(main_fn, offset++) : undef;
1213 ctx->abi.color0 =
1214 ac_to_integer(&ctx->ac,
1215 ac_build_gather_values(&ctx->ac, values, 4));
1216 }
1217 if (colors_read & 0xf0) {
1218 unsigned mask = (colors_read & 0xf0) >> 4;
1219 LLVMValueRef values[4];
1220 values[0] = mask & 0x1 ? LLVMGetParam(main_fn, offset++) : undef;
1221 values[1] = mask & 0x2 ? LLVMGetParam(main_fn, offset++) : undef;
1222 values[2] = mask & 0x4 ? LLVMGetParam(main_fn, offset++) : undef;
1223 values[3] = mask & 0x8 ? LLVMGetParam(main_fn, offset++) : undef;
1224 ctx->abi.color1 =
1225 ac_to_integer(&ctx->ac,
1226 ac_build_gather_values(&ctx->ac, values, 4));
1227 }
1228
1229 ctx->abi.interp_at_sample_force_center =
1230 ctx->shader->key.mono.u.ps.interpolate_at_sample_force_center;
1231 } else if (nir->info.stage == MESA_SHADER_COMPUTE) {
1232 if (nir->info.cs.user_data_components_amd) {
1233 ctx->abi.user_data = ac_get_arg(&ctx->ac, ctx->cs_user_data);
1234 ctx->abi.user_data = ac_build_expand_to_vec4(&ctx->ac, ctx->abi.user_data,
1235 nir->info.cs.user_data_components_amd);
1236 }
1237 }
1238
1239 ctx->abi.inputs = &ctx->inputs[0];
1240 ctx->abi.load_sampler_desc = si_nir_load_sampler_desc;
1241 ctx->abi.clamp_shadow_reference = true;
1242 ctx->abi.robust_buffer_access = true;
1243
1244 ctx->num_samplers = util_last_bit(info->samplers_declared);
1245 ctx->num_images = util_last_bit(info->images_declared);
1246
1247 if (ctx->shader->selector->info.properties[TGSI_PROPERTY_CS_LOCAL_SIZE]) {
1248 assert(gl_shader_stage_is_compute(nir->info.stage));
1249 si_declare_compute_memory(ctx);
1250 }
1251 ac_nir_translate(&ctx->ac, &ctx->abi, &ctx->args, nir);
1252
1253 return true;
1254 }