fa1fbbf3a3da278d32dd4c99f8382091cbcc5593
[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_glsl_types.h"
41 #include "st_program.h"
42
43 #include "compiler/nir/nir.h"
44 #include "compiler/glsl_types.h"
45 #include "compiler/glsl/glsl_to_nir.h"
46 #include "compiler/glsl/gl_nir.h"
47 #include "compiler/glsl/ir.h"
48 #include "compiler/glsl/string_to_uint_map.h"
49
50
51 static int
52 type_size(const struct glsl_type *type)
53 {
54 return type->count_attribute_slots(false);
55 }
56
57 /* Depending on PIPE_CAP_TGSI_TEXCOORD (st->needs_texcoord_semantic) we
58 * may need to fix up varying slots so the glsl->nir path is aligned
59 * with the anything->tgsi->nir path.
60 */
61 static void
62 st_nir_fixup_varying_slots(struct st_context *st, struct exec_list *var_list)
63 {
64 if (st->needs_texcoord_semantic)
65 return;
66
67 nir_foreach_variable(var, var_list) {
68 if (var->data.location >= VARYING_SLOT_VAR0) {
69 var->data.location += 9;
70 } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
71 (var->data.location <= VARYING_SLOT_TEX7)) {
72 var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
73 }
74 }
75 }
76
77 /* input location assignment for VS inputs must be handled specially, so
78 * that it is aligned w/ st's vbo state.
79 * (This isn't the case with, for ex, FS inputs, which only need to agree
80 * on varying-slot w/ the VS outputs)
81 */
82 static void
83 st_nir_assign_vs_in_locations(struct gl_program *prog, nir_shader *nir)
84 {
85 unsigned attr, num_inputs = 0;
86 unsigned input_to_index[VERT_ATTRIB_MAX] = {0};
87
88 /* TODO de-duplicate w/ similar code in st_translate_vertex_program()? */
89 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
90 if ((prog->info.inputs_read & BITFIELD64_BIT(attr)) != 0) {
91 input_to_index[attr] = num_inputs;
92 num_inputs++;
93 if ((prog->info.vs.double_inputs_read & BITFIELD64_BIT(attr)) != 0) {
94 /* add placeholder for second part of a double attribute */
95 num_inputs++;
96 }
97 } else {
98 input_to_index[attr] = ~0;
99 }
100 }
101
102 /* bit of a hack, mirroring st_translate_vertex_program */
103 input_to_index[VERT_ATTRIB_EDGEFLAG] = num_inputs;
104
105 nir->num_inputs = 0;
106 nir_foreach_variable_safe(var, &nir->inputs) {
107 attr = var->data.location;
108 assert(attr < ARRAY_SIZE(input_to_index));
109
110 if (input_to_index[attr] != ~0u) {
111 var->data.driver_location = input_to_index[attr];
112 nir->num_inputs++;
113 } else {
114 /* Move unused input variables to the globals list (with no
115 * initialization), to avoid confusing drivers looking through the
116 * inputs array and expecting to find inputs with a driver_location
117 * set.
118 */
119 exec_node_remove(&var->node);
120 var->data.mode = nir_var_global;
121 exec_list_push_tail(&nir->globals, &var->node);
122 }
123 }
124 }
125
126 static void
127 st_nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
128 gl_shader_stage stage)
129 {
130 unsigned location = 0;
131 unsigned assigned_locations[VARYING_SLOT_TESS_MAX];
132 uint64_t processed_locs[2] = {0};
133
134 const int base = stage == MESA_SHADER_FRAGMENT ?
135 (int) FRAG_RESULT_DATA0 : (int) VARYING_SLOT_VAR0;
136
137 int UNUSED last_loc = 0;
138 nir_foreach_variable(var, var_list) {
139
140 const struct glsl_type *type = var->type;
141 if (nir_is_per_vertex_io(var, stage)) {
142 assert(glsl_type_is_array(type));
143 type = glsl_get_array_element(type);
144 }
145
146 unsigned var_size = type_size(type);
147
148 /* Builtins don't allow component packing so we only need to worry about
149 * user defined varyings sharing the same location.
150 */
151 bool processed = false;
152 if (var->data.location >= base) {
153 unsigned glsl_location = var->data.location - base;
154
155 for (unsigned i = 0; i < var_size; i++) {
156 if (processed_locs[var->data.index] &
157 ((uint64_t)1 << (glsl_location + i)))
158 processed = true;
159 else
160 processed_locs[var->data.index] |=
161 ((uint64_t)1 << (glsl_location + i));
162 }
163 }
164
165 /* Because component packing allows varyings to share the same location
166 * we may have already have processed this location.
167 */
168 if (processed) {
169 unsigned driver_location = assigned_locations[var->data.location];
170 var->data.driver_location = driver_location;
171 *size += type_size(type);
172
173 /* An array may be packed such that is crosses multiple other arrays
174 * or variables, we need to make sure we have allocated the elements
175 * consecutively if the previously proccessed var was shorter than
176 * the current array we are processing.
177 *
178 * NOTE: The code below assumes the var list is ordered in ascending
179 * location order.
180 */
181 assert(last_loc <= var->data.location);
182 last_loc = var->data.location;
183 unsigned last_slot_location = driver_location + var_size;
184 if (last_slot_location > location) {
185 unsigned num_unallocated_slots = last_slot_location - location;
186 unsigned first_unallocated_slot = var_size - num_unallocated_slots;
187 for (unsigned i = first_unallocated_slot; i < num_unallocated_slots; i++) {
188 assigned_locations[var->data.location + i] = location;
189 location++;
190 }
191 }
192 continue;
193 }
194
195 for (unsigned i = 0; i < var_size; i++) {
196 assigned_locations[var->data.location + i] = location + i;
197 }
198
199 var->data.driver_location = location;
200 location += var_size;
201 }
202
203 *size += location;
204 }
205
206 static int
207 st_nir_lookup_parameter_index(const struct gl_program_parameter_list *params,
208 const char *name)
209 {
210 int loc = _mesa_lookup_parameter_index(params, name);
211
212 /* is there a better way to do this? If we have something like:
213 *
214 * struct S {
215 * float f;
216 * vec4 v;
217 * };
218 * uniform S color;
219 *
220 * Then what we get in prog->Parameters looks like:
221 *
222 * 0: Name=color.f, Type=6, DataType=1406, Size=1
223 * 1: Name=color.v, Type=6, DataType=8b52, Size=4
224 *
225 * So the name doesn't match up and _mesa_lookup_parameter_index()
226 * fails. In this case just find the first matching "color.*"..
227 *
228 * Note for arrays you could end up w/ color[n].f, for example.
229 *
230 * glsl_to_tgsi works slightly differently in this regard. It is
231 * emitting something more low level, so it just translates the
232 * params list 1:1 to CONST[] regs. Going from GLSL IR to TGSI,
233 * it just calculates the additional offset of struct field members
234 * in glsl_to_tgsi_visitor::visit(ir_dereference_record *ir) or
235 * glsl_to_tgsi_visitor::visit(ir_dereference_array *ir). It never
236 * needs to work backwards to get base var loc from the param-list
237 * which already has them separated out.
238 */
239 if (loc < 0) {
240 int namelen = strlen(name);
241 for (unsigned i = 0; i < params->NumParameters; i++) {
242 struct gl_program_parameter *p = &params->Parameters[i];
243 if ((strncmp(p->Name, name, namelen) == 0) &&
244 ((p->Name[namelen] == '.') || (p->Name[namelen] == '['))) {
245 loc = i;
246 break;
247 }
248 }
249 }
250
251 return loc;
252 }
253
254 static void
255 st_nir_assign_uniform_locations(struct gl_context *ctx,
256 struct gl_program *prog,
257 struct gl_shader_program *shader_program,
258 struct exec_list *uniform_list, unsigned *size)
259 {
260 int max = 0;
261 int shaderidx = 0;
262 int imageidx = 0;
263
264 nir_foreach_variable(uniform, uniform_list) {
265 int loc;
266
267 /*
268 * UBO's have their own address spaces, so don't count them towards the
269 * number of global uniforms
270 */
271 if ((uniform->data.mode == nir_var_uniform || uniform->data.mode == nir_var_shader_storage) &&
272 uniform->interface_type != NULL)
273 continue;
274
275 const struct glsl_type *type = glsl_without_array(uniform->type);
276 if (!uniform->data.bindless && (type->is_sampler() || type->is_image())) {
277 if (type->is_sampler()) {
278 loc = shaderidx;
279 shaderidx += type_size(uniform->type);
280 } else {
281 loc = imageidx;
282 imageidx += type_size(uniform->type);
283 }
284 } else if (strncmp(uniform->name, "gl_", 3) == 0) {
285 const gl_state_index16 *const stateTokens = uniform->state_slots[0].tokens;
286 /* This state reference has already been setup by ir_to_mesa, but we'll
287 * get the same index back here.
288 */
289
290 unsigned comps;
291 if (glsl_type_is_struct(type)) {
292 comps = 4;
293 } else {
294 comps = glsl_get_vector_elements(type);
295 }
296
297 if (ctx->Const.PackedDriverUniformStorage) {
298 loc = _mesa_add_sized_state_reference(prog->Parameters,
299 stateTokens, comps, false);
300 loc = prog->Parameters->ParameterValueOffset[loc];
301 } else {
302 loc = _mesa_add_state_reference(prog->Parameters, stateTokens);
303 }
304 } else {
305 loc = st_nir_lookup_parameter_index(prog->Parameters, uniform->name);
306
307 if (ctx->Const.PackedDriverUniformStorage) {
308 loc = prog->Parameters->ParameterValueOffset[loc];
309 }
310 }
311
312 uniform->data.driver_location = loc;
313
314 max = MAX2(max, loc + type_size(uniform->type));
315 }
316 *size = max;
317 }
318
319 void
320 st_nir_opts(nir_shader *nir, bool scalar)
321 {
322 bool progress;
323 do {
324 progress = false;
325
326 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
327
328 if (scalar) {
329 NIR_PASS_V(nir, nir_lower_alu_to_scalar);
330 NIR_PASS_V(nir, nir_lower_phis_to_scalar);
331 }
332
333 NIR_PASS_V(nir, nir_lower_alu);
334 NIR_PASS_V(nir, nir_lower_pack);
335 NIR_PASS(progress, nir, nir_copy_prop);
336 NIR_PASS(progress, nir, nir_opt_remove_phis);
337 NIR_PASS(progress, nir, nir_opt_dce);
338 if (nir_opt_trivial_continues(nir)) {
339 progress = true;
340 NIR_PASS(progress, nir, nir_copy_prop);
341 NIR_PASS(progress, nir, nir_opt_dce);
342 }
343 NIR_PASS(progress, nir, nir_opt_if);
344 NIR_PASS(progress, nir, nir_opt_dead_cf);
345 NIR_PASS(progress, nir, nir_opt_cse);
346 NIR_PASS(progress, nir, nir_opt_peephole_select, 8);
347
348 NIR_PASS(progress, nir, nir_opt_algebraic);
349 NIR_PASS(progress, nir, nir_opt_constant_folding);
350
351 NIR_PASS(progress, nir, nir_opt_undef);
352 NIR_PASS(progress, nir, nir_opt_conditional_discard);
353 if (nir->options->max_unroll_iterations) {
354 NIR_PASS(progress, nir, nir_opt_loop_unroll, (nir_variable_mode)0);
355 }
356 } while (progress);
357 }
358
359 /* First third of converting glsl_to_nir.. this leaves things in a pre-
360 * nir_lower_io state, so that shader variants can more easily insert/
361 * replace variables, etc.
362 */
363 static nir_shader *
364 st_glsl_to_nir(struct st_context *st, struct gl_program *prog,
365 struct gl_shader_program *shader_program,
366 gl_shader_stage stage)
367 {
368 const nir_shader_compiler_options *options =
369 st->ctx->Const.ShaderCompilerOptions[prog->info.stage].NirOptions;
370 enum pipe_shader_type type = pipe_shader_type_from_mesa(stage);
371 struct pipe_screen *screen = st->pipe->screen;
372 bool is_scalar = screen->get_shader_param(screen, type, PIPE_SHADER_CAP_SCALAR_ISA);
373 assert(options);
374
375 if (prog->nir)
376 return prog->nir;
377
378 nir_shader *nir = glsl_to_nir(shader_program, stage, options);
379
380 /* Set the next shader stage hint for VS and TES. */
381 if (!nir->info.separate_shader &&
382 (nir->info.stage == MESA_SHADER_VERTEX ||
383 nir->info.stage == MESA_SHADER_TESS_EVAL)) {
384
385 unsigned prev_stages = (1 << (prog->info.stage + 1)) - 1;
386 unsigned stages_mask =
387 ~prev_stages & shader_program->data->linked_stages;
388
389 nir->info.next_stage = stages_mask ?
390 (gl_shader_stage) ffs(stages_mask) : MESA_SHADER_FRAGMENT;
391 } else {
392 nir->info.next_stage = MESA_SHADER_FRAGMENT;
393 }
394
395 nir_variable_mode mask =
396 (nir_variable_mode) (nir_var_shader_in | nir_var_shader_out);
397 nir_remove_dead_variables(nir, mask);
398
399 if (options->lower_all_io_to_temps ||
400 nir->info.stage == MESA_SHADER_VERTEX ||
401 nir->info.stage == MESA_SHADER_GEOMETRY) {
402 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
403 nir_shader_get_entrypoint(nir),
404 true, true);
405 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
406 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
407 nir_shader_get_entrypoint(nir),
408 true, false);
409 }
410
411 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
412 NIR_PASS_V(nir, nir_split_var_copies);
413 NIR_PASS_V(nir, nir_lower_var_copies);
414
415 st_nir_opts(nir, is_scalar);
416
417 return nir;
418 }
419
420 /* Second third of converting glsl_to_nir. This creates uniforms, gathers
421 * info on varyings, etc after NIR link time opts have been applied.
422 */
423 static void
424 st_glsl_to_nir_post_opts(struct st_context *st, struct gl_program *prog,
425 struct gl_shader_program *shader_program)
426 {
427 nir_shader *nir = prog->nir;
428
429 /* Make a pass over the IR to add state references for any built-in
430 * uniforms that are used. This has to be done now (during linking).
431 * Code generation doesn't happen until the first time this shader is
432 * used for rendering. Waiting until then to generate the parameters is
433 * too late. At that point, the values for the built-in uniforms won't
434 * get sent to the shader.
435 */
436 nir_foreach_variable(var, &nir->uniforms) {
437 if (strncmp(var->name, "gl_", 3) == 0) {
438 const nir_state_slot *const slots = var->state_slots;
439 assert(var->state_slots != NULL);
440
441 const struct glsl_type *type = glsl_without_array(var->type);
442 for (unsigned int i = 0; i < var->num_state_slots; i++) {
443 unsigned comps;
444 if (glsl_type_is_struct(type)) {
445 /* Builtin struct require specical handling for now we just
446 * make all members vec4. See st_nir_lower_builtin.
447 */
448 comps = 4;
449 } else {
450 comps = glsl_get_vector_elements(type);
451 }
452
453 if (st->ctx->Const.PackedDriverUniformStorage) {
454 _mesa_add_sized_state_reference(prog->Parameters,
455 slots[i].tokens,
456 comps, false);
457 } else {
458 _mesa_add_state_reference(prog->Parameters,
459 slots[i].tokens);
460 }
461 }
462 }
463 }
464
465 /* Avoid reallocation of the program parameter list, because the uniform
466 * storage is only associated with the original parameter list.
467 * This should be enough for Bitmap and DrawPixels constants.
468 */
469 _mesa_reserve_parameter_storage(prog->Parameters, 8);
470
471 /* This has to be done last. Any operation the can cause
472 * prog->ParameterValues to get reallocated (e.g., anything that adds a
473 * program constant) has to happen before creating this linkage.
474 */
475 _mesa_associate_uniform_storage(st->ctx, shader_program, prog, true);
476
477 st_set_prog_affected_state_flags(prog);
478
479 NIR_PASS_V(nir, st_nir_lower_builtin);
480 NIR_PASS_V(nir, gl_nir_lower_atomics, shader_program, true);
481
482 if (st->ctx->_Shader->Flags & GLSL_DUMP) {
483 _mesa_log("\n");
484 _mesa_log("NIR IR for linked %s program %d:\n",
485 _mesa_shader_stage_to_string(prog->info.stage),
486 shader_program->Name);
487 nir_print_shader(nir, _mesa_get_log_file());
488 _mesa_log("\n\n");
489 }
490 }
491
492 /* TODO any better helper somewhere to sort a list? */
493
494 static void
495 insert_sorted(struct exec_list *var_list, nir_variable *new_var)
496 {
497 nir_foreach_variable(var, var_list) {
498 if (var->data.location > new_var->data.location) {
499 exec_node_insert_node_before(&var->node, &new_var->node);
500 return;
501 }
502 }
503 exec_list_push_tail(var_list, &new_var->node);
504 }
505
506 static void
507 sort_varyings(struct exec_list *var_list)
508 {
509 struct exec_list new_list;
510 exec_list_make_empty(&new_list);
511 nir_foreach_variable_safe(var, var_list) {
512 exec_node_remove(&var->node);
513 insert_sorted(&new_list, var);
514 }
515 exec_list_move_nodes_to(&new_list, var_list);
516 }
517
518 static void
519 set_st_program(struct gl_program *prog,
520 struct gl_shader_program *shader_program,
521 nir_shader *nir)
522 {
523 struct st_vertex_program *stvp;
524 struct st_common_program *stp;
525 struct st_fragment_program *stfp;
526 struct st_compute_program *stcp;
527
528 switch (prog->info.stage) {
529 case MESA_SHADER_VERTEX:
530 stvp = (struct st_vertex_program *)prog;
531 stvp->shader_program = shader_program;
532 stvp->tgsi.type = PIPE_SHADER_IR_NIR;
533 stvp->tgsi.ir.nir = nir;
534 break;
535 case MESA_SHADER_GEOMETRY:
536 case MESA_SHADER_TESS_CTRL:
537 case MESA_SHADER_TESS_EVAL:
538 stp = (struct st_common_program *)prog;
539 stp->shader_program = shader_program;
540 stp->tgsi.type = PIPE_SHADER_IR_NIR;
541 stp->tgsi.ir.nir = nir;
542 break;
543 case MESA_SHADER_FRAGMENT:
544 stfp = (struct st_fragment_program *)prog;
545 stfp->shader_program = shader_program;
546 stfp->tgsi.type = PIPE_SHADER_IR_NIR;
547 stfp->tgsi.ir.nir = nir;
548 break;
549 case MESA_SHADER_COMPUTE:
550 stcp = (struct st_compute_program *)prog;
551 stcp->shader_program = shader_program;
552 stcp->tgsi.ir_type = PIPE_SHADER_IR_NIR;
553 stcp->tgsi.prog = nir;
554 break;
555 default:
556 unreachable("unknown shader stage");
557 }
558 }
559
560 static void
561 st_nir_get_mesa_program(struct gl_context *ctx,
562 struct gl_shader_program *shader_program,
563 struct gl_linked_shader *shader)
564 {
565 struct st_context *st = st_context(ctx);
566 struct gl_program *prog;
567
568 validate_ir_tree(shader->ir);
569
570 prog = shader->Program;
571
572 prog->Parameters = _mesa_new_parameter_list();
573
574 _mesa_copy_linked_program_data(shader_program, shader);
575 _mesa_generate_parameters_list_for_uniforms(ctx, shader_program, shader,
576 prog->Parameters);
577
578 if (ctx->_Shader->Flags & GLSL_DUMP) {
579 _mesa_log("\n");
580 _mesa_log("GLSL IR for linked %s program %d:\n",
581 _mesa_shader_stage_to_string(shader->Stage),
582 shader_program->Name);
583 _mesa_print_ir(_mesa_get_log_file(), shader->ir, NULL);
584 _mesa_log("\n\n");
585 }
586
587 prog->ExternalSamplersUsed = gl_external_samplers(prog);
588 _mesa_update_shader_textures_used(shader_program, prog);
589
590 nir_shader *nir = st_glsl_to_nir(st, prog, shader_program, shader->Stage);
591
592 nir_lower_deref_instrs(nir, (nir_lower_deref_flags)~0);
593
594 set_st_program(prog, shader_program, nir);
595 prog->nir = nir;
596 }
597
598 static void
599 st_nir_link_shaders(nir_shader **producer, nir_shader **consumer, bool scalar)
600 {
601 nir_lower_io_arrays_to_elements(*producer, *consumer);
602
603 NIR_PASS_V(*producer, nir_remove_dead_variables, nir_var_shader_out);
604 NIR_PASS_V(*consumer, nir_remove_dead_variables, nir_var_shader_in);
605
606 if (nir_remove_unused_varyings(*producer, *consumer)) {
607 NIR_PASS_V(*producer, nir_lower_global_vars_to_local);
608 NIR_PASS_V(*consumer, nir_lower_global_vars_to_local);
609
610 /* The backend might not be able to handle indirects on
611 * temporaries so we need to lower indirects on any of the
612 * varyings we have demoted here.
613 *
614 * TODO: radeonsi shouldn't need to do this, however LLVM isn't
615 * currently smart enough to handle indirects without causing excess
616 * spilling causing the gpu to hang.
617 *
618 * See the following thread for more details of the problem:
619 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
620 */
621 nir_variable_mode indirect_mask = nir_var_local;
622
623 NIR_PASS_V(*producer, nir_lower_indirect_derefs, indirect_mask);
624 NIR_PASS_V(*consumer, nir_lower_indirect_derefs, indirect_mask);
625
626 st_nir_opts(*producer, scalar);
627 st_nir_opts(*consumer, scalar);
628 }
629 }
630
631 extern "C" {
632
633 bool
634 st_link_nir(struct gl_context *ctx,
635 struct gl_shader_program *shader_program)
636 {
637 struct st_context *st = st_context(ctx);
638 struct pipe_screen *screen = st->pipe->screen;
639 bool is_scalar[MESA_SHADER_STAGES];
640
641 /* Determine scalar property of each shader stage */
642 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
643 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
644 enum pipe_shader_type type;
645
646 if (shader == NULL)
647 continue;
648
649 type = pipe_shader_type_from_mesa(shader->Stage);
650 is_scalar[i] = screen->get_shader_param(screen, type, PIPE_SHADER_CAP_SCALAR_ISA);
651 }
652
653 /* Determine first and last stage. */
654 unsigned first = MESA_SHADER_STAGES;
655 unsigned last = 0;
656 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
657 if (!shader_program->_LinkedShaders[i])
658 continue;
659 if (first == MESA_SHADER_STAGES)
660 first = i;
661 last = i;
662 }
663
664 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
665 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
666 if (shader == NULL)
667 continue;
668
669 st_nir_get_mesa_program(ctx, shader_program, shader);
670
671 nir_variable_mode mask = (nir_variable_mode) 0;
672 if (i != first)
673 mask = (nir_variable_mode)(mask | nir_var_shader_in);
674
675 if (i != last)
676 mask = (nir_variable_mode)(mask | nir_var_shader_out);
677
678 nir_shader *nir = shader->Program->nir;
679 NIR_PASS_V(nir, nir_lower_io_to_scalar_early, mask);
680 st_nir_opts(nir, is_scalar[i]);
681 }
682
683 /* Linking the stages in the opposite order (from fragment to vertex)
684 * ensures that inter-shader outputs written to in an earlier stage
685 * are eliminated if they are (transitively) not used in a later
686 * stage.
687 */
688 int next = last;
689 for (int i = next - 1; i >= 0; i--) {
690 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
691 if (shader == NULL)
692 continue;
693
694 st_nir_link_shaders(&shader->Program->nir,
695 &shader_program->_LinkedShaders[next]->Program->nir,
696 is_scalar[i]);
697 next = i;
698 }
699
700 int prev = -1;
701 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
702 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
703 if (shader == NULL)
704 continue;
705
706 nir_shader *nir = shader->Program->nir;
707
708 /* fragment shaders may need : */
709 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
710 static const gl_state_index16 wposTransformState[STATE_LENGTH] = {
711 STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM
712 };
713 nir_lower_wpos_ytransform_options wpos_options = { { 0 } };
714 struct pipe_screen *pscreen = st->pipe->screen;
715
716 memcpy(wpos_options.state_tokens, wposTransformState,
717 sizeof(wpos_options.state_tokens));
718 wpos_options.fs_coord_origin_upper_left =
719 pscreen->get_param(pscreen,
720 PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT);
721 wpos_options.fs_coord_origin_lower_left =
722 pscreen->get_param(pscreen,
723 PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
724 wpos_options.fs_coord_pixel_center_integer =
725 pscreen->get_param(pscreen,
726 PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
727 wpos_options.fs_coord_pixel_center_half_integer =
728 pscreen->get_param(pscreen,
729 PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER);
730
731 if (nir_lower_wpos_ytransform(nir, &wpos_options)) {
732 nir_validate_shader(nir);
733 _mesa_add_state_reference(shader->Program->Parameters,
734 wposTransformState);
735 }
736 }
737
738 NIR_PASS_V(nir, nir_lower_system_values);
739
740 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
741 shader->Program->info = nir->info;
742
743 if (prev != -1) {
744 nir_compact_varyings(shader_program->_LinkedShaders[prev]->Program->nir,
745 nir, ctx->API != API_OPENGL_COMPAT);
746 }
747 prev = i;
748 }
749
750 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
751 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
752 if (shader == NULL)
753 continue;
754
755 st_glsl_to_nir_post_opts(st, shader->Program, shader_program);
756
757 assert(shader->Program);
758 if (!ctx->Driver.ProgramStringNotify(ctx,
759 _mesa_shader_stage_to_program(i),
760 shader->Program)) {
761 _mesa_reference_program(ctx, &shader->Program, NULL);
762 return false;
763 }
764 }
765
766 return true;
767 }
768
769 /* Last third of preparing nir from glsl, which happens after shader
770 * variant lowering.
771 */
772 void
773 st_finalize_nir(struct st_context *st, struct gl_program *prog,
774 struct gl_shader_program *shader_program, nir_shader *nir)
775 {
776 struct pipe_screen *screen = st->pipe->screen;
777 const nir_shader_compiler_options *options =
778 st->ctx->Const.ShaderCompilerOptions[prog->info.stage].NirOptions;
779
780 NIR_PASS_V(nir, nir_split_var_copies);
781 NIR_PASS_V(nir, nir_lower_var_copies);
782 if (options->lower_all_io_to_temps ||
783 nir->info.stage == MESA_SHADER_VERTEX ||
784 nir->info.stage == MESA_SHADER_GEOMETRY) {
785 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
786 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
787 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, true);
788 }
789
790 if (nir->info.stage == MESA_SHADER_VERTEX) {
791 /* Needs special handling so drvloc matches the vbo state: */
792 st_nir_assign_vs_in_locations(prog, nir);
793 /* Re-lower global vars, to deal with any dead VS inputs. */
794 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
795
796 sort_varyings(&nir->outputs);
797 st_nir_assign_var_locations(&nir->outputs,
798 &nir->num_outputs,
799 nir->info.stage);
800 st_nir_fixup_varying_slots(st, &nir->outputs);
801 } else if (nir->info.stage == MESA_SHADER_GEOMETRY ||
802 nir->info.stage == MESA_SHADER_TESS_CTRL ||
803 nir->info.stage == MESA_SHADER_TESS_EVAL) {
804 sort_varyings(&nir->inputs);
805 st_nir_assign_var_locations(&nir->inputs,
806 &nir->num_inputs,
807 nir->info.stage);
808 st_nir_fixup_varying_slots(st, &nir->inputs);
809
810 sort_varyings(&nir->outputs);
811 st_nir_assign_var_locations(&nir->outputs,
812 &nir->num_outputs,
813 nir->info.stage);
814 st_nir_fixup_varying_slots(st, &nir->outputs);
815 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
816 sort_varyings(&nir->inputs);
817 st_nir_assign_var_locations(&nir->inputs,
818 &nir->num_inputs,
819 nir->info.stage);
820 st_nir_fixup_varying_slots(st, &nir->inputs);
821 st_nir_assign_var_locations(&nir->outputs,
822 &nir->num_outputs,
823 nir->info.stage);
824 } else if (nir->info.stage == MESA_SHADER_COMPUTE) {
825 /* TODO? */
826 } else {
827 unreachable("invalid shader type for tgsi bypass\n");
828 }
829
830 NIR_PASS_V(nir, nir_lower_atomics_to_ssbo,
831 st->ctx->Const.Program[nir->info.stage].MaxAtomicBuffers);
832
833 st_nir_assign_uniform_locations(st->ctx, prog, shader_program,
834 &nir->uniforms, &nir->num_uniforms);
835
836 if (st->ctx->Const.PackedDriverUniformStorage) {
837 NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_type_dword_size,
838 (nir_lower_io_options)0);
839 NIR_PASS_V(nir, st_nir_lower_uniforms_to_ubo);
840 }
841
842 if (screen->get_param(screen, PIPE_CAP_NIR_SAMPLERS_AS_DEREF))
843 NIR_PASS_V(nir, gl_nir_lower_samplers_as_deref, shader_program);
844 else
845 NIR_PASS_V(nir, gl_nir_lower_samplers, shader_program);
846 }
847
848 } /* extern "C" */