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_program.h"
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"
50 type_size(const struct glsl_type
*type
)
52 return type
->count_attribute_slots(false);
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.
60 st_nir_fixup_varying_slots(struct st_context
*st
, struct exec_list
*var_list
)
62 if (st
->needs_texcoord_semantic
)
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
;
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)
81 st_nir_assign_vs_in_locations(struct gl_program
*prog
, nir_shader
*nir
)
83 unsigned attr
, num_inputs
= 0;
84 unsigned input_to_index
[VERT_ATTRIB_MAX
] = {0};
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
;
91 if ((prog
->info
.double_inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
92 /* add placeholder for second part of a double attribute */
96 input_to_index
[attr
] = ~0;
100 /* bit of a hack, mirroring st_translate_vertex_program */
101 input_to_index
[VERT_ATTRIB_EDGEFLAG
] = num_inputs
;
104 nir_foreach_variable_safe(var
, &nir
->inputs
) {
105 attr
= var
->data
.location
;
106 assert(attr
< ARRAY_SIZE(input_to_index
));
108 if (input_to_index
[attr
] != ~0u) {
109 var
->data
.driver_location
= input_to_index
[attr
];
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
117 exec_node_remove(&var
->node
);
118 var
->data
.mode
= nir_var_global
;
119 exec_list_push_tail(&nir
->globals
, &var
->node
);
125 st_nir_lookup_parameter_index(const struct gl_program_parameter_list
*params
,
128 int loc
= _mesa_lookup_parameter_index(params
, name
);
130 /* is there a better way to do this? If we have something like:
138 * Then what we get in prog->Parameters looks like:
140 * 0: Name=color.f, Type=6, DataType=1406, Size=1
141 * 1: Name=color.v, Type=6, DataType=8b52, Size=4
143 * So the name doesn't match up and _mesa_lookup_parameter_index()
144 * fails. In this case just find the first matching "color.*"..
146 * Note for arrays you could end up w/ color[n].f, for example.
148 * glsl_to_tgsi works slightly differently in this regard. It is
149 * emitting something more low level, so it just translates the
150 * params list 1:1 to CONST[] regs. Going from GLSL IR to TGSI,
151 * it just calculates the additional offset of struct field members
152 * in glsl_to_tgsi_visitor::visit(ir_dereference_record *ir) or
153 * glsl_to_tgsi_visitor::visit(ir_dereference_array *ir). It never
154 * needs to work backwards to get base var loc from the param-list
155 * which already has them separated out.
158 int namelen
= strlen(name
);
159 for (unsigned i
= 0; i
< params
->NumParameters
; i
++) {
160 struct gl_program_parameter
*p
= ¶ms
->Parameters
[i
];
161 if ((strncmp(p
->Name
, name
, namelen
) == 0) &&
162 ((p
->Name
[namelen
] == '.') || (p
->Name
[namelen
] == '['))) {
173 st_nir_assign_uniform_locations(struct gl_program
*prog
,
174 struct gl_shader_program
*shader_program
,
175 struct exec_list
*uniform_list
, unsigned *size
)
181 nir_foreach_variable(uniform
, uniform_list
) {
185 * UBO's have their own address spaces, so don't count them towards the
186 * number of global uniforms
188 if ((uniform
->data
.mode
== nir_var_uniform
|| uniform
->data
.mode
== nir_var_shader_storage
) &&
189 uniform
->interface_type
!= NULL
)
192 if (uniform
->type
->is_sampler() || uniform
->type
->is_image()) {
194 bool found
= shader_program
->UniformHash
->get(val
, uniform
->name
);
195 if (uniform
->type
->is_sampler())
200 (void) found
; /* silence unused var warning */
201 /* this ensure that nir_lower_samplers looks at the correct
202 * shader_program->UniformStorage[location]:
204 uniform
->data
.location
= val
;
205 } else if (strncmp(uniform
->name
, "gl_", 3) == 0) {
206 const gl_state_index
*const stateTokens
= (gl_state_index
*)uniform
->state_slots
[0].tokens
;
207 /* This state reference has already been setup by ir_to_mesa, but we'll
208 * get the same index back here.
210 loc
= _mesa_add_state_reference(prog
->Parameters
, stateTokens
);
212 loc
= st_nir_lookup_parameter_index(prog
->Parameters
, uniform
->name
);
215 uniform
->data
.driver_location
= loc
;
217 max
= MAX2(max
, loc
+ type_size(uniform
->type
));
224 /* First half of converting glsl_to_nir.. this leaves things in a pre-
225 * nir_lower_io state, so that shader variants can more easily insert/
226 * replace variables, etc.
229 st_glsl_to_nir(struct st_context
*st
, struct gl_program
*prog
,
230 struct gl_shader_program
*shader_program
,
231 gl_shader_stage stage
)
233 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
234 enum pipe_shader_type ptarget
= pipe_shader_type_from_mesa(stage
);
235 const nir_shader_compiler_options
*options
;
238 assert(pscreen
->get_compiler_options
); /* drivers using NIR must implement this */
240 options
= (const nir_shader_compiler_options
*)
241 pscreen
->get_compiler_options(pscreen
, PIPE_SHADER_IR_NIR
, ptarget
);
247 nir
= glsl_to_nir(shader_program
, stage
, options
);
249 /* Make a pass over the IR to add state references for any built-in
250 * uniforms that are used. This has to be done now (during linking).
251 * Code generation doesn't happen until the first time this shader is
252 * used for rendering. Waiting until then to generate the parameters is
253 * too late. At that point, the values for the built-in uniforms won't
254 * get sent to the shader.
256 nir_foreach_variable(var
, &nir
->uniforms
) {
257 if (strncmp(var
->name
, "gl_", 3) == 0) {
258 const nir_state_slot
*const slots
= var
->state_slots
;
259 assert(var
->state_slots
!= NULL
);
261 for (unsigned int i
= 0; i
< var
->num_state_slots
; i
++) {
262 _mesa_add_state_reference(prog
->Parameters
,
263 (gl_state_index
*)slots
[i
].tokens
);
268 /* Avoid reallocation of the program parameter list, because the uniform
269 * storage is only associated with the original parameter list.
270 * This should be enough for Bitmap and DrawPixels constants.
272 _mesa_reserve_parameter_storage(prog
->Parameters
, 8);
274 /* This has to be done last. Any operation the can cause
275 * prog->ParameterValues to get reallocated (e.g., anything that adds a
276 * program constant) has to happen before creating this linkage.
278 _mesa_associate_uniform_storage(st
->ctx
, shader_program
, prog
, true);
280 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
281 nir_shader_get_entrypoint(nir
),
283 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
284 NIR_PASS_V(nir
, nir_split_var_copies
);
285 NIR_PASS_V(nir
, nir_lower_var_copies
);
287 /* fragment shaders may need : */
288 if (stage
== MESA_SHADER_FRAGMENT
) {
289 static const gl_state_index wposTransformState
[STATE_LENGTH
] = {
290 STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
292 nir_lower_wpos_ytransform_options wpos_options
= { { 0 } };
293 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
295 memcpy(wpos_options
.state_tokens
, wposTransformState
,
296 sizeof(wpos_options
.state_tokens
));
297 wpos_options
.fs_coord_origin_upper_left
=
298 pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
);
299 wpos_options
.fs_coord_origin_lower_left
=
300 pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
301 wpos_options
.fs_coord_pixel_center_integer
=
302 pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
303 wpos_options
.fs_coord_pixel_center_half_integer
=
304 pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
);
306 if (nir_lower_wpos_ytransform(nir
, &wpos_options
)) {
307 nir_validate_shader(nir
);
308 _mesa_add_state_reference(prog
->Parameters
, wposTransformState
);
312 NIR_PASS_V(nir
, nir_lower_system_values
);
314 nir_shader_gather_info(nir
, nir_shader_get_entrypoint(nir
));
315 prog
->info
= nir
->info
;
317 st_set_prog_affected_state_flags(prog
);
319 NIR_PASS_V(nir
, st_nir_lower_builtin
);
320 NIR_PASS_V(nir
, nir_lower_atomics
, shader_program
);
322 if (st
->ctx
->_Shader
->Flags
& GLSL_DUMP
) {
324 _mesa_log("NIR IR for linked %s program %d:\n",
325 _mesa_shader_stage_to_string(stage
),
326 shader_program
->Name
);
327 nir_print_shader(nir
, _mesa_get_log_file());
336 /* TODO any better helper somewhere to sort a list? */
339 insert_sorted(struct exec_list
*var_list
, nir_variable
*new_var
)
341 nir_foreach_variable(var
, var_list
) {
342 if (var
->data
.location
> new_var
->data
.location
) {
343 exec_node_insert_node_before(&var
->node
, &new_var
->node
);
347 exec_list_push_tail(var_list
, &new_var
->node
);
351 sort_varyings(struct exec_list
*var_list
)
353 struct exec_list new_list
;
354 exec_list_make_empty(&new_list
);
355 nir_foreach_variable_safe(var
, var_list
) {
356 exec_node_remove(&var
->node
);
357 insert_sorted(&new_list
, var
);
359 exec_list_move_nodes_to(&new_list
, var_list
);
362 /* Second half of preparing nir from glsl, which happens after shader
366 st_finalize_nir(struct st_context
*st
, struct gl_program
*prog
,
367 struct gl_shader_program
*shader_program
, nir_shader
*nir
)
369 struct pipe_screen
*screen
= st
->pipe
->screen
;
371 NIR_PASS_V(nir
, nir_split_var_copies
);
372 NIR_PASS_V(nir
, nir_lower_var_copies
);
373 NIR_PASS_V(nir
, nir_lower_io_types
);
375 if (nir
->info
.stage
== MESA_SHADER_VERTEX
) {
376 /* Needs special handling so drvloc matches the vbo state: */
377 st_nir_assign_vs_in_locations(prog
, nir
);
378 /* Re-lower global vars, to deal with any dead VS inputs. */
379 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
381 sort_varyings(&nir
->outputs
);
382 nir_assign_var_locations(&nir
->outputs
,
385 st_nir_fixup_varying_slots(st
, &nir
->outputs
);
386 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
387 sort_varyings(&nir
->inputs
);
388 nir_assign_var_locations(&nir
->inputs
,
391 st_nir_fixup_varying_slots(st
, &nir
->inputs
);
392 nir_assign_var_locations(&nir
->outputs
,
395 } else if (nir
->info
.stage
== MESA_SHADER_COMPUTE
) {
398 unreachable("invalid shader type for tgsi bypass\n");
401 NIR_PASS_V(nir
, nir_lower_atomics_to_ssbo
,
402 st
->ctx
->Const
.Program
[nir
->info
.stage
].MaxAtomicBuffers
);
404 st_nir_assign_uniform_locations(prog
, shader_program
,
405 &nir
->uniforms
, &nir
->num_uniforms
);
407 if (screen
->get_param(screen
, PIPE_CAP_NIR_SAMPLERS_AS_DEREF
))
408 NIR_PASS_V(nir
, nir_lower_samplers_as_deref
, shader_program
);
410 NIR_PASS_V(nir
, nir_lower_samplers
, shader_program
);
414 st_nir_get_mesa_program(struct gl_context
*ctx
,
415 struct gl_shader_program
*shader_program
,
416 struct gl_linked_shader
*shader
)
418 struct st_context
*st
= st_context(ctx
);
419 struct gl_program
*prog
;
421 validate_ir_tree(shader
->ir
);
423 prog
= shader
->Program
;
425 prog
->Parameters
= _mesa_new_parameter_list();
427 _mesa_copy_linked_program_data(shader_program
, shader
);
428 _mesa_generate_parameters_list_for_uniforms(ctx
, shader_program
, shader
,
431 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
433 _mesa_log("GLSL IR for linked %s program %d:\n",
434 _mesa_shader_stage_to_string(shader
->Stage
),
435 shader_program
->Name
);
436 _mesa_print_ir(_mesa_get_log_file(), shader
->ir
, NULL
);
440 prog
->ExternalSamplersUsed
= gl_external_samplers(prog
);
441 _mesa_update_shader_textures_used(shader_program
, prog
);
443 struct st_vertex_program
*stvp
;
444 struct st_common_program
*stp
;
445 struct st_fragment_program
*stfp
;
446 struct st_compute_program
*stcp
;
448 nir_shader
*nir
= st_glsl_to_nir(st
, prog
, shader_program
, shader
->Stage
);
450 switch (shader
->Stage
) {
451 case MESA_SHADER_VERTEX
:
452 stvp
= (struct st_vertex_program
*)prog
;
453 stvp
->shader_program
= shader_program
;
454 stvp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
455 stvp
->tgsi
.ir
.nir
= nir
;
457 case MESA_SHADER_GEOMETRY
:
458 case MESA_SHADER_TESS_CTRL
:
459 case MESA_SHADER_TESS_EVAL
:
460 stp
= (struct st_common_program
*)prog
;
461 stp
->shader_program
= shader_program
;
462 stp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
463 stp
->tgsi
.ir
.nir
= nir
;
465 case MESA_SHADER_FRAGMENT
:
466 stfp
= (struct st_fragment_program
*)prog
;
467 stfp
->shader_program
= shader_program
;
468 stfp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
469 stfp
->tgsi
.ir
.nir
= nir
;
471 case MESA_SHADER_COMPUTE
:
472 stcp
= (struct st_compute_program
*)prog
;
473 stcp
->shader_program
= shader_program
;
474 stcp
->tgsi
.ir_type
= PIPE_SHADER_IR_NIR
;
475 stcp
->tgsi
.prog
= nir_shader_clone(NULL
, nir
);
478 assert(!"should not be reached");