2 * Copyright © 2015 Red Hat
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:
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
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
26 #include "pipe/p_defines.h"
27 #include "pipe/p_screen.h"
28 #include "pipe/p_context.h"
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"
39 #include "st_context.h"
40 #include "st_glsl_types.h"
41 #include "st_program.h"
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"
52 type_size(const struct glsl_type
*type
)
54 return type
->count_attribute_slots(false);
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.
62 st_nir_fixup_varying_slots(struct st_context
*st
, struct exec_list
*var_list
)
64 if (st
->needs_texcoord_semantic
)
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
;
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)
83 st_nir_assign_vs_in_locations(struct gl_program
*prog
, nir_shader
*nir
)
85 unsigned attr
, num_inputs
= 0;
86 unsigned input_to_index
[VERT_ATTRIB_MAX
] = {0};
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
;
93 if ((prog
->info
.vs
.double_inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
94 /* add placeholder for second part of a double attribute */
98 input_to_index
[attr
] = ~0;
102 /* bit of a hack, mirroring st_translate_vertex_program */
103 input_to_index
[VERT_ATTRIB_EDGEFLAG
] = num_inputs
;
106 nir_foreach_variable_safe(var
, &nir
->inputs
) {
107 attr
= var
->data
.location
;
108 assert(attr
< ARRAY_SIZE(input_to_index
));
110 if (input_to_index
[attr
] != ~0u) {
111 var
->data
.driver_location
= input_to_index
[attr
];
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
119 exec_node_remove(&var
->node
);
120 var
->data
.mode
= nir_var_global
;
121 exec_list_push_tail(&nir
->globals
, &var
->node
);
127 st_nir_assign_var_locations(struct exec_list
*var_list
, unsigned *size
,
128 gl_shader_stage stage
)
130 unsigned location
= 0;
131 unsigned assigned_locations
[VARYING_SLOT_TESS_MAX
];
132 uint64_t processed_locs
[2] = {0};
134 const int base
= stage
== MESA_SHADER_FRAGMENT
?
135 (int) FRAG_RESULT_DATA0
: (int) VARYING_SLOT_VAR0
;
137 int UNUSED last_loc
= 0;
138 nir_foreach_variable(var
, var_list
) {
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
);
146 unsigned var_size
= type_size(type
);
148 /* Builtins don't allow component packing so we only need to worry about
149 * user defined varyings sharing the same location.
151 bool processed
= false;
152 if (var
->data
.location
>= base
) {
153 unsigned glsl_location
= var
->data
.location
- base
;
155 for (unsigned i
= 0; i
< var_size
; i
++) {
156 if (processed_locs
[var
->data
.index
] &
157 ((uint64_t)1 << (glsl_location
+ i
)))
160 processed_locs
[var
->data
.index
] |=
161 ((uint64_t)1 << (glsl_location
+ i
));
165 /* Because component packing allows varyings to share the same location
166 * we may have already have processed this location.
169 unsigned driver_location
= assigned_locations
[var
->data
.location
];
170 var
->data
.driver_location
= driver_location
;
171 *size
+= type_size(type
);
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.
178 * NOTE: The code below assumes the var list is ordered in ascending
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
;
195 for (unsigned i
= 0; i
< var_size
; i
++) {
196 assigned_locations
[var
->data
.location
+ i
] = location
+ i
;
199 var
->data
.driver_location
= location
;
200 location
+= var_size
;
207 st_nir_lookup_parameter_index(const struct gl_program_parameter_list
*params
,
210 int loc
= _mesa_lookup_parameter_index(params
, name
);
212 /* is there a better way to do this? If we have something like:
220 * Then what we get in prog->Parameters looks like:
222 * 0: Name=color.f, Type=6, DataType=1406, Size=1
223 * 1: Name=color.v, Type=6, DataType=8b52, Size=4
225 * So the name doesn't match up and _mesa_lookup_parameter_index()
226 * fails. In this case just find the first matching "color.*"..
228 * Note for arrays you could end up w/ color[n].f, for example.
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.
240 int namelen
= strlen(name
);
241 for (unsigned i
= 0; i
< params
->NumParameters
; i
++) {
242 struct gl_program_parameter
*p
= ¶ms
->Parameters
[i
];
243 if ((strncmp(p
->Name
, name
, namelen
) == 0) &&
244 ((p
->Name
[namelen
] == '.') || (p
->Name
[namelen
] == '['))) {
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
)
264 nir_foreach_variable(uniform
, uniform_list
) {
268 * UBO's have their own address spaces, so don't count them towards the
269 * number of global uniforms
271 if ((uniform
->data
.mode
== nir_var_uniform
|| uniform
->data
.mode
== nir_var_shader_storage
) &&
272 uniform
->interface_type
!= NULL
)
275 if (!uniform
->data
.bindless
&&
276 (uniform
->type
->is_sampler() || uniform
->type
->is_image())) {
277 if (uniform
->type
->is_sampler())
281 } else if (strncmp(uniform
->name
, "gl_", 3) == 0) {
282 const gl_state_index16
*const stateTokens
= uniform
->state_slots
[0].tokens
;
283 /* This state reference has already been setup by ir_to_mesa, but we'll
284 * get the same index back here.
288 const struct glsl_type
*type
= glsl_without_array(uniform
->type
);
289 if (glsl_type_is_struct(type
)) {
292 comps
= glsl_get_vector_elements(type
);
295 if (ctx
->Const
.PackedDriverUniformStorage
) {
296 loc
= _mesa_add_sized_state_reference(prog
->Parameters
,
297 stateTokens
, comps
, false);
298 loc
= prog
->Parameters
->ParameterValueOffset
[loc
];
300 loc
= _mesa_add_state_reference(prog
->Parameters
, stateTokens
);
303 loc
= st_nir_lookup_parameter_index(prog
->Parameters
, uniform
->name
);
305 if (ctx
->Const
.PackedDriverUniformStorage
) {
306 loc
= prog
->Parameters
->ParameterValueOffset
[loc
];
310 uniform
->data
.driver_location
= loc
;
312 max
= MAX2(max
, loc
+ type_size(uniform
->type
));
318 st_nir_opts(nir_shader
*nir
)
324 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
325 NIR_PASS_V(nir
, nir_lower_alu_to_scalar
);
326 NIR_PASS_V(nir
, nir_lower_phis_to_scalar
);
328 NIR_PASS_V(nir
, nir_lower_alu
);
329 NIR_PASS_V(nir
, nir_lower_pack
);
330 NIR_PASS(progress
, nir
, nir_copy_prop
);
331 NIR_PASS(progress
, nir
, nir_opt_remove_phis
);
332 NIR_PASS(progress
, nir
, nir_opt_dce
);
333 if (nir_opt_trivial_continues(nir
)) {
335 NIR_PASS(progress
, nir
, nir_copy_prop
);
336 NIR_PASS(progress
, nir
, nir_opt_dce
);
338 NIR_PASS(progress
, nir
, nir_opt_if
);
339 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
340 NIR_PASS(progress
, nir
, nir_opt_cse
);
341 NIR_PASS(progress
, nir
, nir_opt_peephole_select
, 8);
343 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
344 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
346 NIR_PASS(progress
, nir
, nir_opt_undef
);
347 NIR_PASS(progress
, nir
, nir_opt_conditional_discard
);
348 if (nir
->options
->max_unroll_iterations
) {
349 NIR_PASS(progress
, nir
, nir_opt_loop_unroll
, (nir_variable_mode
)0);
354 /* First third of converting glsl_to_nir.. this leaves things in a pre-
355 * nir_lower_io state, so that shader variants can more easily insert/
356 * replace variables, etc.
359 st_glsl_to_nir(struct st_context
*st
, struct gl_program
*prog
,
360 struct gl_shader_program
*shader_program
,
361 gl_shader_stage stage
)
363 const nir_shader_compiler_options
*options
=
364 st
->ctx
->Const
.ShaderCompilerOptions
[prog
->info
.stage
].NirOptions
;
370 nir_shader
*nir
= glsl_to_nir(shader_program
, stage
, options
);
372 /* Set the next shader stage hint for VS and TES. */
373 if (!nir
->info
.separate_shader
&&
374 (nir
->info
.stage
== MESA_SHADER_VERTEX
||
375 nir
->info
.stage
== MESA_SHADER_TESS_EVAL
)) {
377 unsigned prev_stages
= (1 << (prog
->info
.stage
+ 1)) - 1;
378 unsigned stages_mask
=
379 ~prev_stages
& shader_program
->data
->linked_stages
;
381 nir
->info
.next_stage
= stages_mask
?
382 (gl_shader_stage
) ffs(stages_mask
) : MESA_SHADER_FRAGMENT
;
384 nir
->info
.next_stage
= MESA_SHADER_FRAGMENT
;
387 nir_variable_mode mask
=
388 (nir_variable_mode
) (nir_var_shader_in
| nir_var_shader_out
);
389 nir_remove_dead_variables(nir
, mask
);
391 if (options
->lower_all_io_to_temps
||
392 nir
->info
.stage
== MESA_SHADER_VERTEX
||
393 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
394 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
395 nir_shader_get_entrypoint(nir
),
397 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
398 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
399 nir_shader_get_entrypoint(nir
),
403 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
404 NIR_PASS_V(nir
, nir_split_var_copies
);
405 NIR_PASS_V(nir
, nir_lower_var_copies
);
412 /* Second third of converting glsl_to_nir. This creates uniforms, gathers
413 * info on varyings, etc after NIR link time opts have been applied.
416 st_glsl_to_nir_post_opts(struct st_context
*st
, struct gl_program
*prog
,
417 struct gl_shader_program
*shader_program
)
419 nir_shader
*nir
= prog
->nir
;
421 /* Make a pass over the IR to add state references for any built-in
422 * uniforms that are used. This has to be done now (during linking).
423 * Code generation doesn't happen until the first time this shader is
424 * used for rendering. Waiting until then to generate the parameters is
425 * too late. At that point, the values for the built-in uniforms won't
426 * get sent to the shader.
428 nir_foreach_variable(var
, &nir
->uniforms
) {
429 if (strncmp(var
->name
, "gl_", 3) == 0) {
430 const nir_state_slot
*const slots
= var
->state_slots
;
431 assert(var
->state_slots
!= NULL
);
433 const struct glsl_type
*type
= glsl_without_array(var
->type
);
434 for (unsigned int i
= 0; i
< var
->num_state_slots
; i
++) {
436 if (glsl_type_is_struct(type
)) {
437 /* Builtin struct require specical handling for now we just
438 * make all members vec4. See st_nir_lower_builtin.
442 comps
= glsl_get_vector_elements(type
);
445 if (st
->ctx
->Const
.PackedDriverUniformStorage
) {
446 _mesa_add_sized_state_reference(prog
->Parameters
,
450 _mesa_add_state_reference(prog
->Parameters
,
457 /* Avoid reallocation of the program parameter list, because the uniform
458 * storage is only associated with the original parameter list.
459 * This should be enough for Bitmap and DrawPixels constants.
461 _mesa_reserve_parameter_storage(prog
->Parameters
, 8);
463 /* This has to be done last. Any operation the can cause
464 * prog->ParameterValues to get reallocated (e.g., anything that adds a
465 * program constant) has to happen before creating this linkage.
467 _mesa_associate_uniform_storage(st
->ctx
, shader_program
, prog
, true);
469 st_set_prog_affected_state_flags(prog
);
471 NIR_PASS_V(nir
, st_nir_lower_builtin
);
472 NIR_PASS_V(nir
, gl_nir_lower_atomics
, shader_program
, true);
474 if (st
->ctx
->_Shader
->Flags
& GLSL_DUMP
) {
476 _mesa_log("NIR IR for linked %s program %d:\n",
477 _mesa_shader_stage_to_string(prog
->info
.stage
),
478 shader_program
->Name
);
479 nir_print_shader(nir
, _mesa_get_log_file());
484 /* TODO any better helper somewhere to sort a list? */
487 insert_sorted(struct exec_list
*var_list
, nir_variable
*new_var
)
489 nir_foreach_variable(var
, var_list
) {
490 if (var
->data
.location
> new_var
->data
.location
) {
491 exec_node_insert_node_before(&var
->node
, &new_var
->node
);
495 exec_list_push_tail(var_list
, &new_var
->node
);
499 sort_varyings(struct exec_list
*var_list
)
501 struct exec_list new_list
;
502 exec_list_make_empty(&new_list
);
503 nir_foreach_variable_safe(var
, var_list
) {
504 exec_node_remove(&var
->node
);
505 insert_sorted(&new_list
, var
);
507 exec_list_move_nodes_to(&new_list
, var_list
);
511 set_st_program(struct gl_program
*prog
,
512 struct gl_shader_program
*shader_program
,
515 struct st_vertex_program
*stvp
;
516 struct st_common_program
*stp
;
517 struct st_fragment_program
*stfp
;
518 struct st_compute_program
*stcp
;
520 switch (prog
->info
.stage
) {
521 case MESA_SHADER_VERTEX
:
522 stvp
= (struct st_vertex_program
*)prog
;
523 stvp
->shader_program
= shader_program
;
524 stvp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
525 stvp
->tgsi
.ir
.nir
= nir
;
527 case MESA_SHADER_GEOMETRY
:
528 case MESA_SHADER_TESS_CTRL
:
529 case MESA_SHADER_TESS_EVAL
:
530 stp
= (struct st_common_program
*)prog
;
531 stp
->shader_program
= shader_program
;
532 stp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
533 stp
->tgsi
.ir
.nir
= nir
;
535 case MESA_SHADER_FRAGMENT
:
536 stfp
= (struct st_fragment_program
*)prog
;
537 stfp
->shader_program
= shader_program
;
538 stfp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
539 stfp
->tgsi
.ir
.nir
= nir
;
541 case MESA_SHADER_COMPUTE
:
542 stcp
= (struct st_compute_program
*)prog
;
543 stcp
->shader_program
= shader_program
;
544 stcp
->tgsi
.ir_type
= PIPE_SHADER_IR_NIR
;
545 stcp
->tgsi
.prog
= nir
;
548 unreachable("unknown shader stage");
553 st_nir_get_mesa_program(struct gl_context
*ctx
,
554 struct gl_shader_program
*shader_program
,
555 struct gl_linked_shader
*shader
)
557 struct st_context
*st
= st_context(ctx
);
558 struct gl_program
*prog
;
560 validate_ir_tree(shader
->ir
);
562 prog
= shader
->Program
;
564 prog
->Parameters
= _mesa_new_parameter_list();
566 _mesa_copy_linked_program_data(shader_program
, shader
);
567 _mesa_generate_parameters_list_for_uniforms(ctx
, shader_program
, shader
,
570 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
572 _mesa_log("GLSL IR for linked %s program %d:\n",
573 _mesa_shader_stage_to_string(shader
->Stage
),
574 shader_program
->Name
);
575 _mesa_print_ir(_mesa_get_log_file(), shader
->ir
, NULL
);
579 prog
->ExternalSamplersUsed
= gl_external_samplers(prog
);
580 _mesa_update_shader_textures_used(shader_program
, prog
);
582 nir_shader
*nir
= st_glsl_to_nir(st
, prog
, shader_program
, shader
->Stage
);
584 set_st_program(prog
, shader_program
, nir
);
589 st_nir_link_shaders(nir_shader
**producer
, nir_shader
**consumer
)
591 nir_lower_io_arrays_to_elements(*producer
, *consumer
);
593 NIR_PASS_V(*producer
, nir_remove_dead_variables
, nir_var_shader_out
);
594 NIR_PASS_V(*consumer
, nir_remove_dead_variables
, nir_var_shader_in
);
596 if (nir_remove_unused_varyings(*producer
, *consumer
)) {
597 NIR_PASS_V(*producer
, nir_lower_global_vars_to_local
);
598 NIR_PASS_V(*consumer
, nir_lower_global_vars_to_local
);
600 /* The backend might not be able to handle indirects on
601 * temporaries so we need to lower indirects on any of the
602 * varyings we have demoted here.
604 * TODO: radeonsi shouldn't need to do this, however LLVM isn't
605 * currently smart enough to handle indirects without causing excess
606 * spilling causing the gpu to hang.
608 * See the following thread for more details of the problem:
609 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
611 nir_variable_mode indirect_mask
= nir_var_local
;
613 NIR_PASS_V(*producer
, nir_lower_indirect_derefs
, indirect_mask
);
614 NIR_PASS_V(*consumer
, nir_lower_indirect_derefs
, indirect_mask
);
616 st_nir_opts(*producer
);
617 st_nir_opts(*consumer
);
624 st_link_nir(struct gl_context
*ctx
,
625 struct gl_shader_program
*shader_program
)
627 struct st_context
*st
= st_context(ctx
);
629 /* Determine first and last stage. */
630 unsigned first
= MESA_SHADER_STAGES
;
632 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
633 if (!shader_program
->_LinkedShaders
[i
])
635 if (first
== MESA_SHADER_STAGES
)
640 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
641 struct gl_linked_shader
*shader
= shader_program
->_LinkedShaders
[i
];
645 st_nir_get_mesa_program(ctx
, shader_program
, shader
);
647 nir_variable_mode mask
= (nir_variable_mode
) 0;
649 mask
= (nir_variable_mode
)(mask
| nir_var_shader_in
);
652 mask
= (nir_variable_mode
)(mask
| nir_var_shader_out
);
654 nir_shader
*nir
= shader
->Program
->nir
;
655 NIR_PASS_V(nir
, nir_lower_io_to_scalar_early
, mask
);
659 /* Linking the stages in the opposite order (from fragment to vertex)
660 * ensures that inter-shader outputs written to in an earlier stage
661 * are eliminated if they are (transitively) not used in a later
665 for (int i
= next
- 1; i
>= 0; i
--) {
666 struct gl_linked_shader
*shader
= shader_program
->_LinkedShaders
[i
];
670 st_nir_link_shaders(&shader
->Program
->nir
,
671 &shader_program
->_LinkedShaders
[next
]->Program
->nir
);
676 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
677 struct gl_linked_shader
*shader
= shader_program
->_LinkedShaders
[i
];
681 nir_shader
*nir
= shader
->Program
->nir
;
683 /* fragment shaders may need : */
684 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
685 static const gl_state_index16 wposTransformState
[STATE_LENGTH
] = {
686 STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
688 nir_lower_wpos_ytransform_options wpos_options
= { { 0 } };
689 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
691 memcpy(wpos_options
.state_tokens
, wposTransformState
,
692 sizeof(wpos_options
.state_tokens
));
693 wpos_options
.fs_coord_origin_upper_left
=
694 pscreen
->get_param(pscreen
,
695 PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
);
696 wpos_options
.fs_coord_origin_lower_left
=
697 pscreen
->get_param(pscreen
,
698 PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
699 wpos_options
.fs_coord_pixel_center_integer
=
700 pscreen
->get_param(pscreen
,
701 PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
702 wpos_options
.fs_coord_pixel_center_half_integer
=
703 pscreen
->get_param(pscreen
,
704 PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
);
706 if (nir_lower_wpos_ytransform(nir
, &wpos_options
)) {
707 nir_validate_shader(nir
);
708 _mesa_add_state_reference(shader
->Program
->Parameters
,
713 NIR_PASS_V(nir
, nir_lower_system_values
);
715 nir_shader_gather_info(nir
, nir_shader_get_entrypoint(nir
));
716 shader
->Program
->info
= nir
->info
;
719 nir_compact_varyings(shader_program
->_LinkedShaders
[prev
]->Program
->nir
,
720 nir
, ctx
->API
!= API_OPENGL_COMPAT
);
725 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
726 struct gl_linked_shader
*shader
= shader_program
->_LinkedShaders
[i
];
730 st_glsl_to_nir_post_opts(st
, shader
->Program
, shader_program
);
732 assert(shader
->Program
);
733 if (!ctx
->Driver
.ProgramStringNotify(ctx
,
734 _mesa_shader_stage_to_program(i
),
736 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
744 /* Last third of preparing nir from glsl, which happens after shader
748 st_finalize_nir(struct st_context
*st
, struct gl_program
*prog
,
749 struct gl_shader_program
*shader_program
, nir_shader
*nir
)
751 struct pipe_screen
*screen
= st
->pipe
->screen
;
752 const nir_shader_compiler_options
*options
=
753 st
->ctx
->Const
.ShaderCompilerOptions
[prog
->info
.stage
].NirOptions
;
755 NIR_PASS_V(nir
, nir_split_var_copies
);
756 NIR_PASS_V(nir
, nir_lower_var_copies
);
757 if (options
->lower_all_io_to_temps
||
758 nir
->info
.stage
== MESA_SHADER_VERTEX
||
759 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
760 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, false);
761 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
762 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, true);
765 if (nir
->info
.stage
== MESA_SHADER_VERTEX
) {
766 /* Needs special handling so drvloc matches the vbo state: */
767 st_nir_assign_vs_in_locations(prog
, nir
);
768 /* Re-lower global vars, to deal with any dead VS inputs. */
769 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
771 sort_varyings(&nir
->outputs
);
772 st_nir_assign_var_locations(&nir
->outputs
,
775 st_nir_fixup_varying_slots(st
, &nir
->outputs
);
776 } else if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
777 nir
->info
.stage
== MESA_SHADER_TESS_CTRL
||
778 nir
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
779 sort_varyings(&nir
->inputs
);
780 st_nir_assign_var_locations(&nir
->inputs
,
783 st_nir_fixup_varying_slots(st
, &nir
->inputs
);
785 sort_varyings(&nir
->outputs
);
786 st_nir_assign_var_locations(&nir
->outputs
,
789 st_nir_fixup_varying_slots(st
, &nir
->outputs
);
790 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
791 sort_varyings(&nir
->inputs
);
792 st_nir_assign_var_locations(&nir
->inputs
,
795 st_nir_fixup_varying_slots(st
, &nir
->inputs
);
796 st_nir_assign_var_locations(&nir
->outputs
,
799 } else if (nir
->info
.stage
== MESA_SHADER_COMPUTE
) {
802 unreachable("invalid shader type for tgsi bypass\n");
805 NIR_PASS_V(nir
, nir_lower_atomics_to_ssbo
,
806 st
->ctx
->Const
.Program
[nir
->info
.stage
].MaxAtomicBuffers
);
808 st_nir_assign_uniform_locations(st
->ctx
, prog
, shader_program
,
809 &nir
->uniforms
, &nir
->num_uniforms
);
811 if (st
->ctx
->Const
.PackedDriverUniformStorage
) {
812 NIR_PASS_V(nir
, nir_lower_io
, nir_var_uniform
, st_glsl_type_dword_size
,
813 (nir_lower_io_options
)0);
814 NIR_PASS_V(nir
, st_nir_lower_uniforms_to_ubo
);
817 if (screen
->get_param(screen
, PIPE_CAP_NIR_SAMPLERS_AS_DEREF
))
818 NIR_PASS_V(nir
, gl_nir_lower_samplers_as_deref
, shader_program
);
820 NIR_PASS_V(nir
, gl_nir_lower_samplers
, shader_program
);