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 *
get_element(const struct gl_builtin_uniform_desc * desc,nir_deref_path * path)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 *
get_variable(lower_builtin_state * state,nir_deref_path * path,const struct gl_builtin_uniform_element * element)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       /* we need to fixup the array index slot: */
111       switch (tokens[0]) {
112       case STATE_MODELVIEW_MATRIX:
113       case STATE_PROJECTION_MATRIX:
114       case STATE_MVP_MATRIX:
115       case STATE_TEXTURE_MATRIX:
116       case STATE_PROGRAM_MATRIX:
117       case STATE_LIGHT:
118       case STATE_LIGHTPROD:
119       case STATE_TEXGEN:
120       case STATE_TEXENV_COLOR:
121       case STATE_CLIPPLANE:
122          tokens[1] = nir_src_as_uint(path->path[idx]->arr.index);
123          break;
124       }
125    }
126 
127    char *name = _mesa_program_state_string(tokens);
128 
129    nir_foreach_uniform_variable(var, shader) {
130       if (strcmp(var->name, name) == 0) {
131          free(name);
132          return var;
133       }
134    }
135 
136    /* variable doesn't exist yet, so create it: */
137    nir_variable *var =
138       nir_variable_create(shader, nir_var_uniform, glsl_vec4_type(), name);
139 
140    var->num_state_slots = 1;
141    var->state_slots = rzalloc_array(var, nir_state_slot, 1);
142    memcpy(var->state_slots[0].tokens, tokens,
143           sizeof(var->state_slots[0].tokens));
144 
145    free(name);
146 
147    return var;
148 }
149 
150 static bool
lower_builtin_block(lower_builtin_state * state,nir_block * block)151 lower_builtin_block(lower_builtin_state *state, nir_block *block)
152 {
153    nir_builder *b = &state->builder;
154    bool progress = false;
155 
156    nir_foreach_instr_safe(instr, block) {
157       if (instr->type != nir_instr_type_intrinsic)
158          continue;
159 
160       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
161 
162       if (intrin->intrinsic != nir_intrinsic_load_deref)
163          continue;
164 
165       nir_variable *var =
166          nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[0]));
167       if (var->data.mode != nir_var_uniform)
168          continue;
169 
170       /* built-in's will always start with "gl_" */
171       if (strncmp(var->name, "gl_", 3) != 0)
172          continue;
173 
174       const struct gl_builtin_uniform_desc *desc =
175          _mesa_glsl_get_builtin_uniform_desc(var->name);
176 
177       /* if no descriptor, it isn't something we need to handle specially: */
178       if (!desc)
179          continue;
180 
181       nir_deref_path path;
182       nir_deref_path_init(&path, nir_src_as_deref(intrin->src[0]), NULL);
183 
184       const struct gl_builtin_uniform_element *element = get_element(desc, &path);
185 
186       /* matrix elements (array_deref) do not need special handling: */
187       if (!element) {
188          nir_deref_path_finish(&path);
189          continue;
190       }
191 
192       /* remove existing var from uniform list: */
193       exec_node_remove(&var->node);
194       /* the _self_link() ensures we can remove multiple times, rather than
195        * trying to keep track of what we have already removed:
196        */
197       exec_node_self_link(&var->node);
198 
199       nir_variable *new_var = get_variable(state, &path, element);
200       nir_deref_path_finish(&path);
201 
202       b->cursor = nir_before_instr(instr);
203 
204       nir_ssa_def *def = nir_load_var(b, new_var);
205 
206       /* swizzle the result: */
207       unsigned swiz[NIR_MAX_VEC_COMPONENTS] = {0};
208       for (unsigned i = 0; i < 4; i++) {
209          swiz[i] = GET_SWZ(element->swizzle, i);
210          assert(swiz[i] <= SWIZZLE_W);
211       }
212       def = nir_swizzle(b, def, swiz, intrin->num_components);
213 
214       /* and rewrite uses of original instruction: */
215       assert(intrin->dest.is_ssa);
216       nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(def));
217 
218       /* at this point intrin should be unused.  We need to remove it
219        * (rather than waiting for DCE pass) to avoid dangling reference
220        * to remove'd var.  And we have to remove the original uniform
221        * var since we don't want it to get uniform space allocated.
222        */
223       nir_instr_remove(&intrin->instr);
224 
225       progress = true;
226    }
227 
228    return progress;
229 }
230 
231 static void
lower_builtin_impl(lower_builtin_state * state,nir_function_impl * impl)232 lower_builtin_impl(lower_builtin_state *state, nir_function_impl *impl)
233 {
234    nir_builder_init(&state->builder, impl);
235    state->mem_ctx = ralloc_parent(impl);
236 
237    bool progress = false;
238    nir_foreach_block(block, impl) {
239       progress |= lower_builtin_block(state, block);
240    }
241 
242    if (progress)
243       nir_remove_dead_derefs_impl(impl);
244 
245    nir_metadata_preserve(impl, nir_metadata_block_index |
246                                nir_metadata_dominance);
247 }
248 
249 void
st_nir_lower_builtin(nir_shader * shader)250 st_nir_lower_builtin(nir_shader *shader)
251 {
252    lower_builtin_state state;
253    state.shader = shader;
254    nir_foreach_function(function, shader) {
255       if (function->impl)
256          lower_builtin_impl(&state, function->impl);
257    }
258 }
259