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"
61 #include "compiler/nir/nir_deref.h"
63 #include "compiler/glsl/ir.h"
65 #include "program/prog_instruction.h"
71 } lower_builtin_state
;
73 static const struct gl_builtin_uniform_element
*
74 get_element(const struct gl_builtin_uniform_desc
*desc
, nir_deref_path
*path
)
78 assert(path
->path
[0]->deref_type
== nir_deref_type_var
);
80 if ((desc
->num_elements
== 1) && (desc
->elements
[0].field
== NULL
))
83 /* we handle arrays in get_variable(): */
84 if (path
->path
[idx
]->deref_type
== nir_deref_type_array
)
87 /* don't need to deal w/ non-struct or array of non-struct: */
91 if (path
->path
[idx
]->deref_type
!= nir_deref_type_struct
)
94 assert(path
->path
[idx
]->strct
.index
< desc
->num_elements
);
96 return &desc
->elements
[path
->path
[idx
]->strct
.index
];
100 get_variable(lower_builtin_state
*state
, nir_deref_path
*path
,
101 const struct gl_builtin_uniform_element
*element
)
103 nir_shader
*shader
= state
->shader
;
104 gl_state_index16 tokens
[STATE_LENGTH
];
107 memcpy(tokens
, element
->tokens
, sizeof(tokens
));
109 if (path
->path
[idx
]->deref_type
== nir_deref_type_array
) {
110 /* we need to fixup the array index slot: */
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
:
118 case STATE_LIGHTPROD
:
120 case STATE_TEXENV_COLOR
:
121 case STATE_CLIPPLANE
:
122 tokens
[1] = nir_src_as_uint(path
->path
[idx
]->arr
.index
);
127 char *name
= _mesa_program_state_string(tokens
);
129 nir_foreach_variable(var
, &shader
->uniforms
) {
130 if (strcmp(var
->name
, name
) == 0) {
136 /* variable doesn't exist yet, so create it: */
138 nir_variable_create(shader
, nir_var_uniform
, glsl_vec4_type(), name
);
140 var
->num_state_slots
= 1;
141 var
->state_slots
= ralloc_array(var
, nir_state_slot
, 1);
142 memcpy(var
->state_slots
[0].tokens
, tokens
,
143 sizeof(var
->state_slots
[0].tokens
));
151 lower_builtin_block(lower_builtin_state
*state
, nir_block
*block
)
153 nir_builder
*b
= &state
->builder
;
154 bool progress
= false;
156 nir_foreach_instr_safe(instr
, block
) {
157 if (instr
->type
!= nir_instr_type_intrinsic
)
160 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
162 if (intrin
->intrinsic
!= nir_intrinsic_load_deref
)
166 nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
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: */
182 nir_deref_path_init(&path
, nir_src_as_deref(intrin
->src
[0]), NULL
);
184 const struct gl_builtin_uniform_element
*element
= get_element(desc
, &path
);
186 /* matrix elements (array_deref) do not need special handling: */
188 nir_deref_path_finish(&path
);
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:
197 exec_node_self_link(&var
->node
);
199 nir_variable
*new_var
= get_variable(state
, &path
, element
);
200 nir_deref_path_finish(&path
);
202 b
->cursor
= nir_before_instr(instr
);
204 nir_ssa_def
*def
= nir_load_var(b
, new_var
);
206 /* swizzle the result: */
208 for (unsigned i
= 0; i
< 4; i
++) {
209 swiz
[i
] = GET_SWZ(element
->swizzle
, i
);
210 assert(swiz
[i
] <= SWIZZLE_W
);
212 def
= nir_swizzle(b
, def
, swiz
, intrin
->num_components
, true);
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
));
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.
223 nir_instr_remove(&intrin
->instr
);
232 lower_builtin_impl(lower_builtin_state
*state
, nir_function_impl
*impl
)
234 nir_builder_init(&state
->builder
, impl
);
235 state
->mem_ctx
= ralloc_parent(impl
);
237 bool progress
= false;
238 nir_foreach_block(block
, impl
) {
239 progress
|= lower_builtin_block(state
, block
);
243 nir_remove_dead_derefs_impl(impl
);
245 nir_metadata_preserve(impl
, nir_metadata_block_index
|
246 nir_metadata_dominance
);
250 st_nir_lower_builtin(nir_shader
*shader
)
252 lower_builtin_state state
;
253 state
.shader
= shader
;
254 nir_foreach_function(function
, shader
) {
256 lower_builtin_impl(&state
, function
->impl
);