mesa/st/nir: convert lower_builtins to deref instructions
[mesa.git] / src / mesa / state_tracker / st_nir_lower_builtin.c
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 typedef struct {
68 nir_shader *shader;
69 nir_builder builder;
70 void *mem_ctx;
71 } lower_builtin_state;
72
73 static const struct gl_builtin_uniform_element *
74 get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path)
75 {
76 int idx = 1;
77
78 assert(path->path[0]->deref_type == nir_deref_type_var);
79
80 if ((desc->num_elements == 1) && (desc->elements[0].field == NULL))
81 return NULL;
82
83 /* we handle arrays in get_variable(): */
84 if (path->path[idx]->deref_type == nir_deref_type_array)
85 idx++;
86
87 /* don't need to deal w/ non-struct or array of non-struct: */
88 if (!path->path[idx])
89 return NULL;
90
91 if (path->path[idx]->deref_type != nir_deref_type_struct)
92 return NULL;
93
94 assert(path->path[idx]->strct.index < desc->num_elements);
95
96 return &desc->elements[path->path[idx]->strct.index ];
97 }
98
99 static nir_variable *
100 get_variable(lower_builtin_state *state, nir_deref_path *path,
101 const struct gl_builtin_uniform_element *element)
102 {
103 nir_shader *shader = state->shader;
104 gl_state_index16 tokens[STATE_LENGTH];
105 int idx = 1;
106
107 memcpy(tokens, element->tokens, sizeof(tokens));
108
109 if (path->path[idx]->deref_type == nir_deref_type_array) {
110 nir_const_value *c = nir_src_as_const_value(path->path[idx]->arr.index);
111
112 assert(c);
113
114 /* we need to fixup the array index slot: */
115 switch (tokens[0]) {
116 case STATE_MODELVIEW_MATRIX:
117 case STATE_PROJECTION_MATRIX:
118 case STATE_MVP_MATRIX:
119 case STATE_TEXTURE_MATRIX:
120 case STATE_PROGRAM_MATRIX:
121 case STATE_LIGHT:
122 case STATE_LIGHTPROD:
123 case STATE_TEXGEN:
124 case STATE_TEXENV_COLOR:
125 case STATE_CLIPPLANE:
126 tokens[1] = c->u32[0];
127 break;
128 }
129 }
130
131 char *name = _mesa_program_state_string(tokens);
132
133 nir_foreach_variable(var, &shader->uniforms) {
134 if (strcmp(var->name, name) == 0) {
135 free(name);
136 return var;
137 }
138 }
139
140 /* variable doesn't exist yet, so create it: */
141 nir_variable *var =
142 nir_variable_create(shader, nir_var_uniform, glsl_vec4_type(), name);
143
144 var->num_state_slots = 1;
145 var->state_slots = ralloc_array(var, nir_state_slot, 1);
146 memcpy(var->state_slots[0].tokens, tokens,
147 sizeof(var->state_slots[0].tokens));
148
149 free(name);
150
151 return var;
152 }
153
154 static bool
155 lower_builtin_block(lower_builtin_state *state, nir_block *block)
156 {
157 nir_builder *b = &state->builder;
158 bool progress = false;
159
160 nir_foreach_instr_safe(instr, block) {
161 if (instr->type != nir_instr_type_intrinsic)
162 continue;
163
164 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
165
166 if (intrin->intrinsic != nir_intrinsic_load_deref)
167 continue;
168
169 nir_variable *var =
170 nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[0]));
171 if (var->data.mode != nir_var_uniform)
172 continue;
173
174 /* built-in's will always start with "gl_" */
175 if (strncmp(var->name, "gl_", 3) != 0)
176 continue;
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 continue;
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 continue;
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(state, &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[4];
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, true);
217
218 /* and rewrite uses of original instruction: */
219 assert(intrin->dest.is_ssa);
220 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_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 progress = true;
230 }
231
232 return progress;
233 }
234
235 static void
236 lower_builtin_impl(lower_builtin_state *state, nir_function_impl *impl)
237 {
238 nir_builder_init(&state->builder, impl);
239 state->mem_ctx = ralloc_parent(impl);
240
241 bool progress = false;
242 nir_foreach_block(block, impl) {
243 progress |= lower_builtin_block(state, block);
244 }
245
246 if (progress)
247 nir_remove_dead_derefs_impl(impl);
248
249 nir_metadata_preserve(impl, nir_metadata_block_index |
250 nir_metadata_dominance);
251 }
252
253 void
254 st_nir_lower_builtin(nir_shader *shader)
255 {
256 nir_assert_unlowered_derefs(shader, nir_lower_load_store_derefs);
257 lower_builtin_state state;
258 state.shader = shader;
259 nir_foreach_function(function, shader) {
260 if (function->impl)
261 lower_builtin_impl(&state, function->impl);
262 }
263 }