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 /** Wrapper around nir_state_variable_create to pick the name automatically. */
68 nir_variable *
st_nir_state_variable_create(nir_shader * shader,const struct glsl_type * type,const gl_state_index16 tokens[STATE_LENGTH])69 st_nir_state_variable_create(nir_shader *shader,
70 const struct glsl_type *type,
71 const gl_state_index16 tokens[STATE_LENGTH])
72 {
73 char *name = _mesa_program_state_string(tokens);
74 nir_variable *var = nir_state_variable_create(shader, type, name, tokens);
75 free(name);
76 return var;
77 }
78
79 static const struct gl_builtin_uniform_element *
get_element(const struct gl_builtin_uniform_desc * desc,nir_deref_path * path)80 get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path)
81 {
82 int idx = 1;
83
84 assert(path->path[0]->deref_type == nir_deref_type_var);
85
86 if ((desc->num_elements == 1) && (desc->elements[0].field == NULL))
87 return NULL;
88
89 /* we handle arrays in get_variable(): */
90 if (path->path[idx]->deref_type == nir_deref_type_array)
91 idx++;
92
93 /* don't need to deal w/ non-struct or array of non-struct: */
94 if (!path->path[idx])
95 return NULL;
96
97 if (path->path[idx]->deref_type != nir_deref_type_struct)
98 return NULL;
99
100 assert(path->path[idx]->strct.index < desc->num_elements);
101
102 return &desc->elements[path->path[idx]->strct.index ];
103 }
104
105 static nir_variable *
get_variable(nir_builder * b,nir_deref_path * path,const struct gl_builtin_uniform_element * element)106 get_variable(nir_builder *b, nir_deref_path *path,
107 const struct gl_builtin_uniform_element *element)
108 {
109 nir_shader *shader = b->shader;
110 gl_state_index16 tokens[STATE_LENGTH];
111 int idx = 1;
112
113 memcpy(tokens, element->tokens, sizeof(tokens));
114
115 if (path->path[idx]->deref_type == nir_deref_type_array) {
116 /* we need to fixup the array index slot: */
117 switch (tokens[0]) {
118 case STATE_MODELVIEW_MATRIX:
119 case STATE_MODELVIEW_MATRIX_INVERSE:
120 case STATE_MODELVIEW_MATRIX_TRANSPOSE:
121 case STATE_MODELVIEW_MATRIX_INVTRANS:
122 case STATE_PROJECTION_MATRIX:
123 case STATE_PROJECTION_MATRIX_INVERSE:
124 case STATE_PROJECTION_MATRIX_TRANSPOSE:
125 case STATE_PROJECTION_MATRIX_INVTRANS:
126 case STATE_MVP_MATRIX:
127 case STATE_MVP_MATRIX_INVERSE:
128 case STATE_MVP_MATRIX_TRANSPOSE:
129 case STATE_MVP_MATRIX_INVTRANS:
130 case STATE_TEXTURE_MATRIX:
131 case STATE_TEXTURE_MATRIX_INVERSE:
132 case STATE_TEXTURE_MATRIX_TRANSPOSE:
133 case STATE_TEXTURE_MATRIX_INVTRANS:
134 case STATE_PROGRAM_MATRIX:
135 case STATE_PROGRAM_MATRIX_INVERSE:
136 case STATE_PROGRAM_MATRIX_TRANSPOSE:
137 case STATE_PROGRAM_MATRIX_INVTRANS:
138 case STATE_LIGHT:
139 case STATE_LIGHTPROD:
140 case STATE_TEXGEN:
141 case STATE_TEXENV_COLOR:
142 case STATE_CLIPPLANE:
143 tokens[1] = nir_src_as_uint(path->path[idx]->arr.index);
144 break;
145 }
146 }
147
148 nir_variable *var = nir_find_state_variable(shader, tokens);
149 if (var)
150 return var;
151
152 /* variable doesn't exist yet, so create it: */
153 return st_nir_state_variable_create(shader, glsl_vec4_type(), tokens);
154 }
155
156 static bool
lower_builtin_instr(nir_builder * b,nir_intrinsic_instr * intrin,UNUSED void * _data)157 lower_builtin_instr(nir_builder *b, nir_intrinsic_instr *intrin,
158 UNUSED void *_data)
159 {
160 if (intrin->intrinsic != nir_intrinsic_load_deref)
161 return false;
162
163 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
164 if (!nir_deref_mode_is(deref, nir_var_uniform))
165 return false;
166
167 /* built-in's will always start with "gl_" */
168 nir_variable *var = nir_deref_instr_get_variable(deref);
169 if (strncmp(var->name, "gl_", 3) != 0)
170 return false;
171
172 const struct gl_builtin_uniform_desc *desc =
173 _mesa_glsl_get_builtin_uniform_desc(var->name);
174
175 /* if no descriptor, it isn't something we need to handle specially: */
176 if (!desc)
177 return false;
178
179 nir_deref_path path;
180 nir_deref_path_init(&path, nir_src_as_deref(intrin->src[0]), NULL);
181
182 const struct gl_builtin_uniform_element *element = get_element(desc, &path);
183
184 /* matrix elements (array_deref) do not need special handling: */
185 if (!element) {
186 nir_deref_path_finish(&path);
187 return false;
188 }
189
190 /* remove existing var from uniform list: */
191 exec_node_remove(&var->node);
192 /* the _self_link() ensures we can remove multiple times, rather than
193 * trying to keep track of what we have already removed:
194 */
195 exec_node_self_link(&var->node);
196
197 nir_variable *new_var = get_variable(b, &path, element);
198 nir_deref_path_finish(&path);
199
200 b->cursor = nir_before_instr(&intrin->instr);
201
202 nir_def *def = nir_load_var(b, new_var);
203
204 /* swizzle the result: */
205 unsigned swiz[NIR_MAX_VEC_COMPONENTS] = {0};
206 for (unsigned i = 0; i < 4; i++) {
207 swiz[i] = GET_SWZ(element->swizzle, i);
208 assert(swiz[i] <= SWIZZLE_W);
209 }
210 def = nir_swizzle(b, def, swiz, intrin->num_components);
211
212 /* and rewrite uses of original instruction: */
213 nir_def_replace(&intrin->def, def);
214
215 return true;
216 }
217
218 bool
st_nir_lower_builtin(nir_shader * shader)219 st_nir_lower_builtin(nir_shader *shader)
220 {
221 bool progress = false;
222 struct set *vars = _mesa_pointer_set_create(NULL);
223
224 nir_foreach_uniform_variable(var, shader) {
225 /* built-in's will always start with "gl_" */
226 if (strncmp(var->name, "gl_", 3) == 0)
227 _mesa_set_add(vars, var);
228 }
229
230 if (vars->entries > 0) {
231 /* at this point, array uniforms have been split into separate
232 * nir_variable structs where possible. this codepath can't handle
233 * dynamic array indexing, however, so all indirect uniform derefs must
234 * be eliminated beforehand to avoid trying to lower one of those
235 * builtins
236 */
237 progress |= nir_lower_indirect_var_derefs(shader, vars);
238
239 if (nir_shader_intrinsics_pass(shader, lower_builtin_instr,
240 nir_metadata_control_flow, NULL)) {
241 nir_remove_dead_derefs(shader);
242 progress = true;
243 }
244 } else {
245 nir_shader_preserve_all_metadata(shader);
246 }
247
248 _mesa_set_destroy(vars, NULL);
249 return progress;
250 }
251