2 * Copyright © 2016 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
24 /* Lowering pass that lowers accesses to built-in uniform variables.
25 * Built-in uniforms are not necessarily packed the same way that
26 * normal uniform structs are, for example:
28 * struct gl_FogParameters {
36 * is packed into vec4[2], whereas the same struct would be packed
37 * (by gallium), as vec4[5] if it where not built-in. Because of
38 * this, we need to replace (for example) access like:
40 * vec1 ssa_1 = intrinsic load_var () (gl_Fog.start) ()
44 * vec4 ssa_2 = intrinsic load_var () (fog.params) ()
45 * vec1 ssa_1 = ssa_2.y
47 * with appropriate substitutions in the uniform variables list:
49 * decl_var uniform INTERP_MODE_NONE gl_FogParameters gl_Fog (0, 0)
53 * decl_var uniform INTERP_MODE_NONE vec4 state.fog.color (0, 0)
54 * decl_var uniform INTERP_MODE_NONE vec4 state.fog.params (0, 1)
56 * See in particular 'struct gl_builtin_uniform_element'.
59 #include "compiler/nir/nir.h"
60 #include "compiler/nir/nir_builder.h"
62 #include "compiler/glsl/ir.h"
64 #include "program/prog_instruction.h"
70 } lower_builtin_state
;
72 static const struct gl_builtin_uniform_element
*
73 get_element(const struct gl_builtin_uniform_desc
*desc
, nir_deref_var
*deref
)
75 nir_deref
*tail
= &deref
->deref
;
77 if ((desc
->num_elements
== 1) && (desc
->elements
[0].field
== NULL
))
80 /* we handle arrays in get_variable(): */
81 if (tail
->child
->deref_type
== nir_deref_type_array
)
84 /* don't need to deal w/ non-struct or array of non-struct: */
88 if (tail
->child
->deref_type
!= nir_deref_type_struct
)
91 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
->child
);
93 assert(deref_struct
->index
< desc
->num_elements
);
95 return &desc
->elements
[deref_struct
->index
];
99 get_variable(lower_builtin_state
*state
, nir_deref_var
*deref
,
100 const struct gl_builtin_uniform_element
*element
)
102 nir_shader
*shader
= state
->shader
;
103 int tokens
[STATE_LENGTH
];
105 memcpy(tokens
, element
->tokens
, sizeof(tokens
));
107 if (deref
->deref
.child
->deref_type
== nir_deref_type_array
) {
108 nir_deref_array
*darr
= nir_deref_as_array(deref
->deref
.child
);
110 assert(darr
->deref_array_type
== nir_deref_array_type_direct
);
112 /* we need to fixup the array index slot: */
114 case STATE_MODELVIEW_MATRIX
:
115 case STATE_PROJECTION_MATRIX
:
116 case STATE_MVP_MATRIX
:
117 case STATE_TEXTURE_MATRIX
:
118 case STATE_PROGRAM_MATRIX
:
120 case STATE_LIGHTPROD
:
122 case STATE_TEXENV_COLOR
:
123 case STATE_CLIPPLANE
:
124 tokens
[1] = darr
->base_offset
;
129 char *name
= _mesa_program_state_string((gl_state_index
*)tokens
);
131 nir_foreach_variable(var
, &shader
->uniforms
) {
132 if (strcmp(var
->name
, name
) == 0) {
138 /* variable doesn't exist yet, so create it: */
140 nir_variable_create(shader
, nir_var_uniform
, glsl_vec4_type(), name
);
142 var
->num_state_slots
= 1;
143 var
->state_slots
= ralloc_array(var
, nir_state_slot
, 1);
144 memcpy(var
->state_slots
[0].tokens
, tokens
,
145 sizeof(var
->state_slots
[0].tokens
));
153 lower_builtin_block(lower_builtin_state
*state
, nir_block
*block
)
155 nir_builder
*b
= &state
->builder
;
157 nir_foreach_instr_safe(instr
, block
) {
158 if (instr
->type
!= nir_instr_type_intrinsic
)
161 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
163 if (intrin
->intrinsic
!= nir_intrinsic_load_var
)
166 nir_variable
*var
= intrin
->variables
[0]->var
;
167 if (var
->data
.mode
!= nir_var_uniform
)
170 /* built-in's will always start with "gl_" */
171 if (strncmp(var
->name
, "gl_", 3) != 0)
174 const struct gl_builtin_uniform_desc
*desc
=
175 _mesa_glsl_get_builtin_uniform_desc(var
->name
);
177 /* if no descriptor, it isn't something we need to handle specially: */
181 const struct gl_builtin_uniform_element
*element
=
182 get_element(desc
, intrin
->variables
[0]);
184 /* matrix elements (array_deref) do not need special handling: */
188 /* remove existing var from uniform list: */
189 exec_node_remove(&var
->node
);
190 /* the _self_link() ensures we can remove multiple times, rather than
191 * trying to keep track of what we have already removed:
193 exec_node_self_link(&var
->node
);
195 nir_variable
*new_var
=
196 get_variable(state
, intrin
->variables
[0], element
);
198 b
->cursor
= nir_before_instr(instr
);
200 nir_ssa_def
*def
= nir_load_var(b
, new_var
);
202 /* swizzle the result: */
204 for (unsigned i
= 0; i
< 4; i
++) {
205 swiz
[i
] = GET_SWZ(element
->swizzle
, i
);
206 assert(swiz
[i
] <= SWIZZLE_W
);
208 def
= nir_swizzle(b
, def
, swiz
, intrin
->num_components
, true);
210 /* and rewrite uses of original instruction: */
211 assert(intrin
->dest
.is_ssa
);
212 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
, nir_src_for_ssa(def
));
214 /* at this point intrin should be unused. We need to remove it
215 * (rather than waiting for DCE pass) to avoid dangling reference
216 * to remove'd var. And we have to remove the original uniform
217 * var since we don't want it to get uniform space allocated.
219 exec_node_remove(&intrin
->instr
.node
);
226 lower_builtin_impl(lower_builtin_state
*state
, nir_function_impl
*impl
)
228 nir_builder_init(&state
->builder
, impl
);
229 state
->mem_ctx
= ralloc_parent(impl
);
231 nir_foreach_block(block
, impl
) {
232 lower_builtin_block(state
, block
);
235 nir_metadata_preserve(impl
, nir_metadata_block_index
|
236 nir_metadata_dominance
);
240 st_nir_lower_builtin(nir_shader
*shader
)
242 lower_builtin_state state
;
243 state
.shader
= shader
;
244 nir_foreach_function(function
, shader
) {
246 lower_builtin_impl(&state
, function
->impl
);