1 /*
2 * Copyright © 2016 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 /* 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:
27 *
28 * struct gl_FogParameters {
29 * vec4 color;
30 * float density;
31 * float start;
32 * float end;
33 * float scale;
34 * };
35 *
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:
39 *
40 * vec1 ssa_1 = intrinsic load_var () (gl_Fog.start) ()
41 *
42 * with:
43 *
44 * vec4 ssa_2 = intrinsic load_var () (fog.params) ()
45 * vec1 ssa_1 = ssa_2.y
46 *
47 * with appropriate substitutions in the uniform variables list:
48 *
49 * decl_var uniform INTERP_MODE_NONE gl_FogParameters gl_Fog (0, 0)
50 *
51 * would become:
52 *
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)
55 *
56 * See in particular 'struct gl_builtin_uniform_element'.
57 */
58
59 #include "compiler/nir/nir.h"
60 #include "compiler/nir/nir_builder.h"
61 #include "compiler/nir/nir_deref.h"
62 #include "st_nir.h"
63 #include "compiler/glsl/ir.h"
64 #include "uniforms.h"
65 #include "program/prog_instruction.h"
66
67 static const struct gl_builtin_uniform_element *
get_element(const struct gl_builtin_uniform_desc * desc,nir_deref_path * path)68 get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path)
69 {
70 int idx = 1;
71
72 assert(path->path[0]->deref_type == nir_deref_type_var);
73
74 if ((desc->num_elements == 1) && (desc->elements[0].field == NULL))
75 return NULL;
76
77 /* we handle arrays in get_variable(): */
78 if (path->path[idx]->deref_type == nir_deref_type_array)
79 idx++;
80
81 /* don't need to deal w/ non-struct or array of non-struct: */
82 if (!path->path[idx])
83 return NULL;
84
85 if (path->path[idx]->deref_type != nir_deref_type_struct)
86 return NULL;
87
88 assert(path->path[idx]->strct.index < desc->num_elements);
89
90 return &desc->elements[path->path[idx]->strct.index ];
91 }
92
93 static nir_variable *
get_variable(nir_builder * b,nir_deref_path * path,const struct gl_builtin_uniform_element * element)94 get_variable(nir_builder *b, nir_deref_path *path,
95 const struct gl_builtin_uniform_element *element)
96 {
97 nir_shader *shader = b->shader;
98 gl_state_index16 tokens[STATE_LENGTH];
99 int idx = 1;
100
101 memcpy(tokens, element->tokens, sizeof(tokens));
102
103 if (path->path[idx]->deref_type == nir_deref_type_array) {
104 /* we need to fixup the array index slot: */
105 switch (tokens[0]) {
106 case STATE_MODELVIEW_MATRIX:
107 case STATE_MODELVIEW_MATRIX_INVERSE:
108 case STATE_MODELVIEW_MATRIX_TRANSPOSE:
109 case STATE_MODELVIEW_MATRIX_INVTRANS:
110 case STATE_PROJECTION_MATRIX:
111 case STATE_PROJECTION_MATRIX_INVERSE:
112 case STATE_PROJECTION_MATRIX_TRANSPOSE:
113 case STATE_PROJECTION_MATRIX_INVTRANS:
114 case STATE_MVP_MATRIX:
115 case STATE_MVP_MATRIX_INVERSE:
116 case STATE_MVP_MATRIX_TRANSPOSE:
117 case STATE_MVP_MATRIX_INVTRANS:
118 case STATE_TEXTURE_MATRIX:
119 case STATE_TEXTURE_MATRIX_INVERSE:
120 case STATE_TEXTURE_MATRIX_TRANSPOSE:
121 case STATE_TEXTURE_MATRIX_INVTRANS:
122 case STATE_PROGRAM_MATRIX:
123 case STATE_PROGRAM_MATRIX_INVERSE:
124 case STATE_PROGRAM_MATRIX_TRANSPOSE:
125 case STATE_PROGRAM_MATRIX_INVTRANS:
126 case STATE_LIGHT:
127 case STATE_LIGHTPROD:
128 case STATE_TEXGEN:
129 case STATE_TEXENV_COLOR:
130 case STATE_CLIPPLANE:
131 tokens[1] = nir_src_as_uint(path->path[idx]->arr.index);
132 break;
133 }
134 }
135
136 char *name = _mesa_program_state_string(tokens);
137
138 nir_foreach_uniform_variable(var, shader) {
139 if (strcmp(var->name, name) == 0) {
140 free(name);
141 return var;
142 }
143 }
144
145 /* variable doesn't exist yet, so create it: */
146 nir_variable *var =
147 nir_variable_create(shader, nir_var_uniform, glsl_vec4_type(), name);
148
149 var->num_state_slots = 1;
150 var->state_slots = rzalloc_array(var, nir_state_slot, 1);
151 memcpy(var->state_slots[0].tokens, tokens,
152 sizeof(var->state_slots[0].tokens));
153
154 free(name);
155
156 return var;
157 }
158
159 static bool
lower_builtin_instr(nir_builder * b,nir_instr * instr,UNUSED void * _data)160 lower_builtin_instr(nir_builder *b, nir_instr *instr, UNUSED void *_data)
161 {
162 if (instr->type != nir_instr_type_intrinsic)
163 return false;
164
165 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
166 if (intrin->intrinsic != nir_intrinsic_load_deref)
167 return false;
168
169 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
170 if (!nir_deref_mode_is(deref, nir_var_uniform))
171 return false;
172
173 /* built-in's will always start with "gl_" */
174 nir_variable *var = nir_deref_instr_get_variable(deref);
175 if (strncmp(var->name, "gl_", 3) != 0)
176 return false;
177
178 const struct gl_builtin_uniform_desc *desc =
179 _mesa_glsl_get_builtin_uniform_desc(var->name);
180
181 /* if no descriptor, it isn't something we need to handle specially: */
182 if (!desc)
183 return false;
184
185 nir_deref_path path;
186 nir_deref_path_init(&path, nir_src_as_deref(intrin->src[0]), NULL);
187
188 const struct gl_builtin_uniform_element *element = get_element(desc, &path);
189
190 /* matrix elements (array_deref) do not need special handling: */
191 if (!element) {
192 nir_deref_path_finish(&path);
193 return false;
194 }
195
196 /* remove existing var from uniform list: */
197 exec_node_remove(&var->node);
198 /* the _self_link() ensures we can remove multiple times, rather than
199 * trying to keep track of what we have already removed:
200 */
201 exec_node_self_link(&var->node);
202
203 nir_variable *new_var = get_variable(b, &path, element);
204 nir_deref_path_finish(&path);
205
206 b->cursor = nir_before_instr(instr);
207
208 nir_ssa_def *def = nir_load_var(b, new_var);
209
210 /* swizzle the result: */
211 unsigned swiz[NIR_MAX_VEC_COMPONENTS] = {0};
212 for (unsigned i = 0; i < 4; i++) {
213 swiz[i] = GET_SWZ(element->swizzle, i);
214 assert(swiz[i] <= SWIZZLE_W);
215 }
216 def = nir_swizzle(b, def, swiz, intrin->num_components);
217
218 /* and rewrite uses of original instruction: */
219 assert(intrin->dest.is_ssa);
220 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, def);
221
222 /* at this point intrin should be unused. We need to remove it
223 * (rather than waiting for DCE pass) to avoid dangling reference
224 * to remove'd var. And we have to remove the original uniform
225 * var since we don't want it to get uniform space allocated.
226 */
227 nir_instr_remove(&intrin->instr);
228
229 return true;
230 }
231
232 void
st_nir_lower_builtin(nir_shader * shader)233 st_nir_lower_builtin(nir_shader *shader)
234 {
235 struct set *vars = _mesa_pointer_set_create(NULL);
236
237 nir_foreach_uniform_variable(var, shader) {
238 /* built-in's will always start with "gl_" */
239 if (strncmp(var->name, "gl_", 3) == 0)
240 _mesa_set_add(vars, var);
241 }
242
243 if (vars->entries > 0) {
244 /* at this point, array uniforms have been split into separate
245 * nir_variable structs where possible. this codepath can't handle
246 * dynamic array indexing, however, so all indirect uniform derefs must
247 * be eliminated beforehand to avoid trying to lower one of those
248 * builtins
249 */
250 nir_lower_indirect_var_derefs(shader, vars);
251
252 if (nir_shader_instructions_pass(shader, lower_builtin_instr,
253 nir_metadata_block_index |
254 nir_metadata_dominance, NULL))
255 nir_remove_dead_derefs(shader);
256 }
257
258 _mesa_set_destroy(vars, NULL);
259 }
260