st/glsl_to_nir: skip forced array splitting for tcs
[mesa.git] / src / mesa / state_tracker / st_glsl_to_nir.cpp
1 /*
2 * Copyright © 2015 Red Hat
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "st_nir.h"
25
26 #include "pipe/p_defines.h"
27 #include "pipe/p_screen.h"
28 #include "pipe/p_context.h"
29
30 #include "program/program.h"
31 #include "program/prog_statevars.h"
32 #include "program/prog_parameter.h"
33 #include "program/ir_to_mesa.h"
34 #include "main/mtypes.h"
35 #include "main/errors.h"
36 #include "main/shaderapi.h"
37 #include "main/uniforms.h"
38
39 #include "st_context.h"
40 #include "st_program.h"
41
42 #include "compiler/nir/nir.h"
43 #include "compiler/glsl_types.h"
44 #include "compiler/glsl/glsl_to_nir.h"
45 #include "compiler/glsl/ir.h"
46 #include "compiler/glsl/string_to_uint_map.h"
47
48
49 static int
50 type_size(const struct glsl_type *type)
51 {
52 return type->count_attribute_slots(false);
53 }
54
55 /* Depending on PIPE_CAP_TGSI_TEXCOORD (st->needs_texcoord_semantic) we
56 * may need to fix up varying slots so the glsl->nir path is aligned
57 * with the anything->tgsi->nir path.
58 */
59 static void
60 st_nir_fixup_varying_slots(struct st_context *st, struct exec_list *var_list)
61 {
62 if (st->needs_texcoord_semantic)
63 return;
64
65 nir_foreach_variable(var, var_list) {
66 if (var->data.location >= VARYING_SLOT_VAR0) {
67 var->data.location += 9;
68 } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
69 (var->data.location <= VARYING_SLOT_TEX7)) {
70 var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
71 }
72 }
73 }
74
75 /* input location assignment for VS inputs must be handled specially, so
76 * that it is aligned w/ st's vbo state.
77 * (This isn't the case with, for ex, FS inputs, which only need to agree
78 * on varying-slot w/ the VS outputs)
79 */
80 static void
81 st_nir_assign_vs_in_locations(struct gl_program *prog, nir_shader *nir)
82 {
83 unsigned attr, num_inputs = 0;
84 unsigned input_to_index[VERT_ATTRIB_MAX] = {0};
85
86 /* TODO de-duplicate w/ similar code in st_translate_vertex_program()? */
87 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
88 if ((prog->info.inputs_read & BITFIELD64_BIT(attr)) != 0) {
89 input_to_index[attr] = num_inputs;
90 num_inputs++;
91 if ((prog->info.double_inputs_read & BITFIELD64_BIT(attr)) != 0) {
92 /* add placeholder for second part of a double attribute */
93 num_inputs++;
94 }
95 } else {
96 input_to_index[attr] = ~0;
97 }
98 }
99
100 /* bit of a hack, mirroring st_translate_vertex_program */
101 input_to_index[VERT_ATTRIB_EDGEFLAG] = num_inputs;
102
103 nir->num_inputs = 0;
104 nir_foreach_variable_safe(var, &nir->inputs) {
105 attr = var->data.location;
106 assert(attr < ARRAY_SIZE(input_to_index));
107
108 if (input_to_index[attr] != ~0u) {
109 var->data.driver_location = input_to_index[attr];
110 nir->num_inputs++;
111 } else {
112 /* Move unused input variables to the globals list (with no
113 * initialization), to avoid confusing drivers looking through the
114 * inputs array and expecting to find inputs with a driver_location
115 * set.
116 */
117 exec_node_remove(&var->node);
118 var->data.mode = nir_var_global;
119 exec_list_push_tail(&nir->globals, &var->node);
120 }
121 }
122 }
123
124 static void
125 st_nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
126 gl_shader_stage stage)
127 {
128 unsigned location = 0;
129 unsigned assigned_locations[VARYING_SLOT_MAX];
130 uint64_t processed_locs = 0;
131
132 nir_foreach_variable(var, var_list) {
133
134 const struct glsl_type *type = var->type;
135 if (nir_is_per_vertex_io(var, stage)) {
136 assert(glsl_type_is_array(type));
137 type = glsl_get_array_element(type);
138 }
139
140 /* Because component packing allows varyings to share the same location
141 * we may have already have processed this location.
142 */
143 if (var->data.location >= VARYING_SLOT_VAR0 &&
144 processed_locs & ((uint64_t)1 << var->data.location)) {
145 var->data.driver_location = assigned_locations[var->data.location];
146 *size += type_size(type);
147 continue;
148 }
149
150 assigned_locations[var->data.location] = location;
151 var->data.driver_location = location;
152 location += type_size(type);
153
154 processed_locs |= ((uint64_t)1 << var->data.location);
155 }
156
157 *size += location;
158 }
159
160 static int
161 st_nir_lookup_parameter_index(const struct gl_program_parameter_list *params,
162 const char *name)
163 {
164 int loc = _mesa_lookup_parameter_index(params, name);
165
166 /* is there a better way to do this? If we have something like:
167 *
168 * struct S {
169 * float f;
170 * vec4 v;
171 * };
172 * uniform S color;
173 *
174 * Then what we get in prog->Parameters looks like:
175 *
176 * 0: Name=color.f, Type=6, DataType=1406, Size=1
177 * 1: Name=color.v, Type=6, DataType=8b52, Size=4
178 *
179 * So the name doesn't match up and _mesa_lookup_parameter_index()
180 * fails. In this case just find the first matching "color.*"..
181 *
182 * Note for arrays you could end up w/ color[n].f, for example.
183 *
184 * glsl_to_tgsi works slightly differently in this regard. It is
185 * emitting something more low level, so it just translates the
186 * params list 1:1 to CONST[] regs. Going from GLSL IR to TGSI,
187 * it just calculates the additional offset of struct field members
188 * in glsl_to_tgsi_visitor::visit(ir_dereference_record *ir) or
189 * glsl_to_tgsi_visitor::visit(ir_dereference_array *ir). It never
190 * needs to work backwards to get base var loc from the param-list
191 * which already has them separated out.
192 */
193 if (loc < 0) {
194 int namelen = strlen(name);
195 for (unsigned i = 0; i < params->NumParameters; i++) {
196 struct gl_program_parameter *p = &params->Parameters[i];
197 if ((strncmp(p->Name, name, namelen) == 0) &&
198 ((p->Name[namelen] == '.') || (p->Name[namelen] == '['))) {
199 loc = i;
200 break;
201 }
202 }
203 }
204
205 return loc;
206 }
207
208 static void
209 st_nir_assign_uniform_locations(struct gl_program *prog,
210 struct gl_shader_program *shader_program,
211 struct exec_list *uniform_list, unsigned *size)
212 {
213 int max = 0;
214 int shaderidx = 0;
215 int imageidx = 0;
216
217 nir_foreach_variable(uniform, uniform_list) {
218 int loc;
219
220 /*
221 * UBO's have their own address spaces, so don't count them towards the
222 * number of global uniforms
223 */
224 if ((uniform->data.mode == nir_var_uniform || uniform->data.mode == nir_var_shader_storage) &&
225 uniform->interface_type != NULL)
226 continue;
227
228 if (uniform->type->is_sampler() || uniform->type->is_image()) {
229 unsigned val = 0;
230 bool found = shader_program->UniformHash->get(val, uniform->name);
231 if (uniform->type->is_sampler())
232 loc = shaderidx++;
233 else
234 loc = imageidx++;
235 assert(found);
236 (void) found; /* silence unused var warning */
237 /* this ensure that nir_lower_samplers looks at the correct
238 * shader_program->UniformStorage[location]:
239 */
240 uniform->data.location = val;
241 } else if (strncmp(uniform->name, "gl_", 3) == 0) {
242 const gl_state_index *const stateTokens = (gl_state_index *)uniform->state_slots[0].tokens;
243 /* This state reference has already been setup by ir_to_mesa, but we'll
244 * get the same index back here.
245 */
246 loc = _mesa_add_state_reference(prog->Parameters, stateTokens);
247 } else {
248 loc = st_nir_lookup_parameter_index(prog->Parameters, uniform->name);
249 }
250
251 uniform->data.driver_location = loc;
252
253 max = MAX2(max, loc + type_size(uniform->type));
254 }
255 *size = max;
256 }
257
258 static void
259 st_nir_opts(nir_shader *nir)
260 {
261 bool progress;
262 do {
263 progress = false;
264
265 NIR_PASS(progress, nir, nir_copy_prop);
266 NIR_PASS(progress, nir, nir_opt_remove_phis);
267 NIR_PASS(progress, nir, nir_opt_dce);
268 if (nir_opt_trivial_continues(nir)) {
269 progress = true;
270 NIR_PASS(progress, nir, nir_copy_prop);
271 NIR_PASS(progress, nir, nir_opt_dce);
272 }
273 NIR_PASS(progress, nir, nir_opt_if);
274 NIR_PASS(progress, nir, nir_opt_dead_cf);
275 NIR_PASS(progress, nir, nir_opt_cse);
276 NIR_PASS(progress, nir, nir_opt_peephole_select, 8);
277
278 NIR_PASS(progress, nir, nir_opt_algebraic);
279 NIR_PASS(progress, nir, nir_opt_constant_folding);
280
281 NIR_PASS(progress, nir, nir_opt_undef);
282 NIR_PASS(progress, nir, nir_opt_conditional_discard);
283 if (nir->options->max_unroll_iterations) {
284 NIR_PASS(progress, nir, nir_opt_loop_unroll, (nir_variable_mode)0);
285 }
286 } while (progress);
287 }
288
289 /* First third of converting glsl_to_nir.. this leaves things in a pre-
290 * nir_lower_io state, so that shader variants can more easily insert/
291 * replace variables, etc.
292 */
293 static nir_shader *
294 st_glsl_to_nir(struct st_context *st, struct gl_program *prog,
295 struct gl_shader_program *shader_program,
296 gl_shader_stage stage)
297 {
298 struct pipe_screen *pscreen = st->pipe->screen;
299 enum pipe_shader_type ptarget = pipe_shader_type_from_mesa(stage);
300 const nir_shader_compiler_options *options;
301
302 assert(pscreen->get_compiler_options); /* drivers using NIR must implement this */
303
304 options = (const nir_shader_compiler_options *)
305 pscreen->get_compiler_options(pscreen, PIPE_SHADER_IR_NIR, ptarget);
306 assert(options);
307
308 if (prog->nir)
309 return prog->nir;
310
311 nir_shader *nir = glsl_to_nir(shader_program, stage, options);
312
313 st_nir_opts(nir);
314
315 return nir;
316 }
317
318 /* Second third of converting glsl_to_nir. This creates uniforms, gathers
319 * info on varyings, etc after NIR link time opts have been applied.
320 */
321 static void
322 st_glsl_to_nir_post_opts(struct st_context *st, struct gl_program *prog,
323 struct gl_shader_program *shader_program)
324 {
325 nir_shader *nir = prog->nir;
326
327 /* Make a pass over the IR to add state references for any built-in
328 * uniforms that are used. This has to be done now (during linking).
329 * Code generation doesn't happen until the first time this shader is
330 * used for rendering. Waiting until then to generate the parameters is
331 * too late. At that point, the values for the built-in uniforms won't
332 * get sent to the shader.
333 */
334 nir_foreach_variable(var, &nir->uniforms) {
335 if (strncmp(var->name, "gl_", 3) == 0) {
336 const nir_state_slot *const slots = var->state_slots;
337 assert(var->state_slots != NULL);
338
339 for (unsigned int i = 0; i < var->num_state_slots; i++) {
340 _mesa_add_state_reference(prog->Parameters,
341 (gl_state_index *)slots[i].tokens);
342 }
343 }
344 }
345
346 /* Avoid reallocation of the program parameter list, because the uniform
347 * storage is only associated with the original parameter list.
348 * This should be enough for Bitmap and DrawPixels constants.
349 */
350 _mesa_reserve_parameter_storage(prog->Parameters, 8);
351
352 /* This has to be done last. Any operation the can cause
353 * prog->ParameterValues to get reallocated (e.g., anything that adds a
354 * program constant) has to happen before creating this linkage.
355 */
356 _mesa_associate_uniform_storage(st->ctx, shader_program, prog, true);
357
358 st_set_prog_affected_state_flags(prog);
359
360 NIR_PASS_V(nir, st_nir_lower_builtin);
361 NIR_PASS_V(nir, nir_lower_atomics, shader_program);
362
363 if (st->ctx->_Shader->Flags & GLSL_DUMP) {
364 _mesa_log("\n");
365 _mesa_log("NIR IR for linked %s program %d:\n",
366 _mesa_shader_stage_to_string(prog->info.stage),
367 shader_program->Name);
368 nir_print_shader(nir, _mesa_get_log_file());
369 _mesa_log("\n\n");
370 }
371 }
372
373 /* TODO any better helper somewhere to sort a list? */
374
375 static void
376 insert_sorted(struct exec_list *var_list, nir_variable *new_var)
377 {
378 nir_foreach_variable(var, var_list) {
379 if (var->data.location > new_var->data.location) {
380 exec_node_insert_node_before(&var->node, &new_var->node);
381 return;
382 }
383 }
384 exec_list_push_tail(var_list, &new_var->node);
385 }
386
387 static void
388 sort_varyings(struct exec_list *var_list)
389 {
390 struct exec_list new_list;
391 exec_list_make_empty(&new_list);
392 nir_foreach_variable_safe(var, var_list) {
393 exec_node_remove(&var->node);
394 insert_sorted(&new_list, var);
395 }
396 exec_list_move_nodes_to(&new_list, var_list);
397 }
398
399 static void
400 set_st_program(struct gl_program *prog,
401 struct gl_shader_program *shader_program,
402 nir_shader *nir)
403 {
404 struct st_vertex_program *stvp;
405 struct st_common_program *stp;
406 struct st_fragment_program *stfp;
407 struct st_compute_program *stcp;
408
409 switch (prog->info.stage) {
410 case MESA_SHADER_VERTEX:
411 stvp = (struct st_vertex_program *)prog;
412 stvp->shader_program = shader_program;
413 stvp->tgsi.type = PIPE_SHADER_IR_NIR;
414 stvp->tgsi.ir.nir = nir;
415 break;
416 case MESA_SHADER_GEOMETRY:
417 case MESA_SHADER_TESS_CTRL:
418 case MESA_SHADER_TESS_EVAL:
419 stp = (struct st_common_program *)prog;
420 stp->shader_program = shader_program;
421 stp->tgsi.type = PIPE_SHADER_IR_NIR;
422 stp->tgsi.ir.nir = nir;
423 break;
424 case MESA_SHADER_FRAGMENT:
425 stfp = (struct st_fragment_program *)prog;
426 stfp->shader_program = shader_program;
427 stfp->tgsi.type = PIPE_SHADER_IR_NIR;
428 stfp->tgsi.ir.nir = nir;
429 break;
430 case MESA_SHADER_COMPUTE:
431 stcp = (struct st_compute_program *)prog;
432 stcp->shader_program = shader_program;
433 stcp->tgsi.ir_type = PIPE_SHADER_IR_NIR;
434 stcp->tgsi.prog = nir;
435 break;
436 default:
437 unreachable("unknown shader stage");
438 }
439 }
440
441 static void
442 st_nir_get_mesa_program(struct gl_context *ctx,
443 struct gl_shader_program *shader_program,
444 struct gl_linked_shader *shader)
445 {
446 struct st_context *st = st_context(ctx);
447 struct gl_program *prog;
448
449 validate_ir_tree(shader->ir);
450
451 prog = shader->Program;
452
453 prog->Parameters = _mesa_new_parameter_list();
454
455 _mesa_copy_linked_program_data(shader_program, shader);
456 _mesa_generate_parameters_list_for_uniforms(ctx, shader_program, shader,
457 prog->Parameters);
458
459 if (ctx->_Shader->Flags & GLSL_DUMP) {
460 _mesa_log("\n");
461 _mesa_log("GLSL IR for linked %s program %d:\n",
462 _mesa_shader_stage_to_string(shader->Stage),
463 shader_program->Name);
464 _mesa_print_ir(_mesa_get_log_file(), shader->ir, NULL);
465 _mesa_log("\n\n");
466 }
467
468 prog->ExternalSamplersUsed = gl_external_samplers(prog);
469 _mesa_update_shader_textures_used(shader_program, prog);
470
471 nir_shader *nir = st_glsl_to_nir(st, prog, shader_program, shader->Stage);
472
473 set_st_program(prog, shader_program, nir);
474 prog->nir = nir;
475
476 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
477 nir_shader_get_entrypoint(nir),
478 true, true);
479 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
480 NIR_PASS_V(nir, nir_split_var_copies);
481 NIR_PASS_V(nir, nir_lower_var_copies);
482 }
483
484 static void
485 st_nir_link_shaders(nir_shader **producer, nir_shader **consumer)
486 {
487 nir_lower_io_arrays_to_elements(*producer, *consumer);
488
489 NIR_PASS_V(*producer, nir_remove_dead_variables, nir_var_shader_out);
490 NIR_PASS_V(*consumer, nir_remove_dead_variables, nir_var_shader_in);
491
492 if (nir_remove_unused_varyings(*producer, *consumer)) {
493 NIR_PASS_V(*producer, nir_lower_global_vars_to_local);
494 NIR_PASS_V(*consumer, nir_lower_global_vars_to_local);
495
496 /* The backend might not be able to handle indirects on
497 * temporaries so we need to lower indirects on any of the
498 * varyings we have demoted here.
499 *
500 * TODO: radeonsi shouldn't need to do this, however LLVM isn't
501 * currently smart enough to handle indirects without causing excess
502 * spilling causing the gpu to hang.
503 *
504 * See the following thread for more details of the problem:
505 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
506 */
507 nir_variable_mode indirect_mask = nir_var_local;
508
509 NIR_PASS_V(*producer, nir_lower_indirect_derefs, indirect_mask);
510 NIR_PASS_V(*consumer, nir_lower_indirect_derefs, indirect_mask);
511
512 st_nir_opts(*producer);
513 st_nir_opts(*consumer);
514 }
515 }
516
517 extern "C" {
518
519 bool
520 st_link_nir(struct gl_context *ctx,
521 struct gl_shader_program *shader_program)
522 {
523 struct st_context *st = st_context(ctx);
524
525 /* Determine first and last stage. */
526 unsigned first = MESA_SHADER_STAGES;
527 unsigned last = 0;
528 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
529 if (!shader_program->_LinkedShaders[i])
530 continue;
531 if (first == MESA_SHADER_STAGES)
532 first = i;
533 last = i;
534 }
535
536 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
537 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
538 if (shader == NULL)
539 continue;
540
541 st_nir_get_mesa_program(ctx, shader_program, shader);
542
543 nir_variable_mode mask = (nir_variable_mode) 0;
544 if (i != first)
545 mask = (nir_variable_mode)(mask | nir_var_shader_in);
546
547 if (i != last)
548 mask = (nir_variable_mode)(mask | nir_var_shader_out);
549
550 nir_shader *nir = shader->Program->nir;
551 nir_lower_io_to_scalar_early(nir, mask);
552 st_nir_opts(nir);
553 }
554
555 /* Linking the stages in the opposite order (from fragment to vertex)
556 * ensures that inter-shader outputs written to in an earlier stage
557 * are eliminated if they are (transitively) not used in a later
558 * stage.
559 */
560 int next = last;
561 for (int i = next - 1; i >= 0; i--) {
562 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
563 if (shader == NULL)
564 continue;
565
566 st_nir_link_shaders(&shader->Program->nir,
567 &shader_program->_LinkedShaders[next]->Program->nir);
568 next = i;
569 }
570
571 int prev = -1;
572 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
573 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
574 if (shader == NULL)
575 continue;
576
577 nir_shader *nir = shader->Program->nir;
578
579 /* fragment shaders may need : */
580 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
581 static const gl_state_index wposTransformState[STATE_LENGTH] = {
582 STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM
583 };
584 nir_lower_wpos_ytransform_options wpos_options = { { 0 } };
585 struct pipe_screen *pscreen = st->pipe->screen;
586
587 memcpy(wpos_options.state_tokens, wposTransformState,
588 sizeof(wpos_options.state_tokens));
589 wpos_options.fs_coord_origin_upper_left =
590 pscreen->get_param(pscreen,
591 PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT);
592 wpos_options.fs_coord_origin_lower_left =
593 pscreen->get_param(pscreen,
594 PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
595 wpos_options.fs_coord_pixel_center_integer =
596 pscreen->get_param(pscreen,
597 PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
598 wpos_options.fs_coord_pixel_center_half_integer =
599 pscreen->get_param(pscreen,
600 PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER);
601
602 if (nir_lower_wpos_ytransform(nir, &wpos_options)) {
603 nir_validate_shader(nir);
604 _mesa_add_state_reference(shader->Program->Parameters,
605 wposTransformState);
606 }
607 }
608
609 NIR_PASS_V(nir, nir_lower_system_values);
610
611 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
612 shader->Program->info = nir->info;
613
614 if (prev != -1) {
615 nir_compact_varyings(shader_program->_LinkedShaders[prev]->Program->nir,
616 nir, ctx->API != API_OPENGL_COMPAT);
617 }
618 prev = i;
619
620 st_glsl_to_nir_post_opts(st, shader->Program, shader_program);
621
622 assert(shader->Program);
623 if (!ctx->Driver.ProgramStringNotify(ctx,
624 _mesa_shader_stage_to_program(i),
625 shader->Program)) {
626 _mesa_reference_program(ctx, &shader->Program, NULL);
627 return false;
628 }
629 }
630
631 return true;
632 }
633
634 /* Last third of preparing nir from glsl, which happens after shader
635 * variant lowering.
636 */
637 void
638 st_finalize_nir(struct st_context *st, struct gl_program *prog,
639 struct gl_shader_program *shader_program, nir_shader *nir)
640 {
641 struct pipe_screen *screen = st->pipe->screen;
642
643 NIR_PASS_V(nir, nir_split_var_copies);
644 NIR_PASS_V(nir, nir_lower_var_copies);
645 if (nir->info.stage != MESA_SHADER_TESS_CTRL)
646 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects);
647
648 if (nir->info.stage == MESA_SHADER_VERTEX) {
649 /* Needs special handling so drvloc matches the vbo state: */
650 st_nir_assign_vs_in_locations(prog, nir);
651 /* Re-lower global vars, to deal with any dead VS inputs. */
652 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
653
654 sort_varyings(&nir->outputs);
655 st_nir_assign_var_locations(&nir->outputs,
656 &nir->num_outputs,
657 nir->info.stage);
658 st_nir_fixup_varying_slots(st, &nir->outputs);
659 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
660 sort_varyings(&nir->inputs);
661 st_nir_assign_var_locations(&nir->inputs,
662 &nir->num_inputs,
663 nir->info.stage);
664 st_nir_fixup_varying_slots(st, &nir->inputs);
665
666 sort_varyings(&nir->outputs);
667 st_nir_assign_var_locations(&nir->outputs,
668 &nir->num_outputs,
669 nir->info.stage);
670 st_nir_fixup_varying_slots(st, &nir->outputs);
671 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
672 sort_varyings(&nir->inputs);
673 st_nir_assign_var_locations(&nir->inputs,
674 &nir->num_inputs,
675 nir->info.stage);
676 st_nir_fixup_varying_slots(st, &nir->inputs);
677 st_nir_assign_var_locations(&nir->outputs,
678 &nir->num_outputs,
679 nir->info.stage);
680 } else if (nir->info.stage == MESA_SHADER_COMPUTE) {
681 /* TODO? */
682 } else {
683 unreachable("invalid shader type for tgsi bypass\n");
684 }
685
686 NIR_PASS_V(nir, nir_lower_atomics_to_ssbo,
687 st->ctx->Const.Program[nir->info.stage].MaxAtomicBuffers);
688
689 st_nir_assign_uniform_locations(prog, shader_program,
690 &nir->uniforms, &nir->num_uniforms);
691
692 if (screen->get_param(screen, PIPE_CAP_NIR_SAMPLERS_AS_DEREF))
693 NIR_PASS_V(nir, nir_lower_samplers_as_deref, shader_program);
694 else
695 NIR_PASS_V(nir, nir_lower_samplers, shader_program);
696 }
697
698 } /* extern "C" */