2 * Copyright © 2014 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
25 * Jason Ekstrand (jason@jlekstrand.net)
30 * This lowering pass converts references to input/output variables with
31 * loads/stores to actual input/output intrinsics.
33 * NOTE: This pass really only works for scalar backends at the moment due
34 * to the way it packes the input/output data.
39 struct lower_io_state
{
44 type_size(const struct glsl_type
*type
)
48 switch (glsl_get_base_type(type
)) {
53 return glsl_get_components(type
);
55 return type_size(glsl_get_array_element(type
)) * glsl_get_length(type
);
56 case GLSL_TYPE_STRUCT
:
58 for (i
= 0; i
< glsl_get_length(type
); i
++) {
59 size
+= type_size(glsl_get_struct_field(type
, i
));
62 case GLSL_TYPE_SAMPLER
:
64 case GLSL_TYPE_ATOMIC_UINT
:
66 case GLSL_TYPE_INTERFACE
:
70 case GLSL_TYPE_FUNCTION
:
73 case GLSL_TYPE_DOUBLE
:
74 unreachable("not reached");
81 nir_assign_var_locations_scalar(struct exec_list
*var_list
, unsigned *size
)
83 unsigned location
= 0;
85 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
87 * UBO's have their own address spaces, so don't count them towards the
88 * number of global uniforms
90 if (var
->data
.mode
== nir_var_uniform
&& var
->interface_type
!= NULL
)
93 var
->data
.driver_location
= location
;
94 location
+= type_size(var
->type
);
101 deref_has_indirect(nir_deref_var
*deref
)
103 for (nir_deref
*tail
= deref
->deref
.child
; tail
; tail
= tail
->child
) {
104 if (tail
->deref_type
== nir_deref_type_array
) {
105 nir_deref_array
*arr
= nir_deref_as_array(tail
);
106 if (arr
->deref_array_type
== nir_deref_array_type_indirect
)
115 mark_indirect_uses_block(nir_block
*block
, void *void_state
)
117 struct set
*indirect_set
= void_state
;
119 nir_foreach_instr(block
, instr
) {
120 if (instr
->type
!= nir_instr_type_intrinsic
)
123 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
126 i
< nir_intrinsic_infos
[intrin
->intrinsic
].num_variables
; i
++) {
127 if (deref_has_indirect(intrin
->variables
[i
]))
128 _mesa_set_add(indirect_set
, intrin
->variables
[i
]->var
);
135 /* Identical to nir_assign_var_locations_packed except that it assigns
136 * locations to the variables that are used 100% directly first and then
137 * assigns locations to variables that are used indirectly.
140 nir_assign_var_locations_scalar_direct_first(nir_shader
*shader
,
141 struct exec_list
*var_list
,
142 unsigned *direct_size
,
145 struct set
*indirect_set
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
146 _mesa_key_pointer_equal
);
148 nir_foreach_overload(shader
, overload
) {
150 nir_foreach_block(overload
->impl
, mark_indirect_uses_block
,
154 unsigned location
= 0;
156 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
157 if (var
->data
.mode
== nir_var_uniform
&& var
->interface_type
!= NULL
)
160 if (_mesa_set_search(indirect_set
, var
))
163 var
->data
.driver_location
= location
;
164 location
+= type_size(var
->type
);
167 *direct_size
= location
;
169 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
170 if (var
->data
.mode
== nir_var_uniform
&& var
->interface_type
!= NULL
)
173 if (!_mesa_set_search(indirect_set
, var
))
176 var
->data
.driver_location
= location
;
177 location
+= type_size(var
->type
);
182 _mesa_set_destroy(indirect_set
, NULL
);
186 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
, nir_src
*indirect
,
187 struct lower_io_state
*state
)
189 bool found_indirect
= false;
190 unsigned base_offset
= 0;
192 nir_deref
*tail
= &deref
->deref
;
193 while (tail
->child
!= NULL
) {
194 const struct glsl_type
*parent_type
= tail
->type
;
197 if (tail
->deref_type
== nir_deref_type_array
) {
198 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
199 unsigned size
= type_size(tail
->type
);
201 base_offset
+= size
* deref_array
->base_offset
;
203 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
204 nir_load_const_instr
*load_const
=
205 nir_load_const_instr_create(state
->mem_ctx
, 1);
206 load_const
->value
.u
[0] = size
;
207 nir_instr_insert_before(instr
, &load_const
->instr
);
209 nir_alu_instr
*mul
= nir_alu_instr_create(state
->mem_ctx
,
211 mul
->src
[0].src
.is_ssa
= true;
212 mul
->src
[0].src
.ssa
= &load_const
->def
;
213 nir_src_copy(&mul
->src
[1].src
, &deref_array
->indirect
,
215 mul
->dest
.write_mask
= 1;
216 nir_ssa_dest_init(&mul
->instr
, &mul
->dest
.dest
, 1, NULL
);
217 nir_instr_insert_before(instr
, &mul
->instr
);
219 if (found_indirect
) {
220 nir_alu_instr
*add
= nir_alu_instr_create(state
->mem_ctx
,
222 add
->src
[0].src
= *indirect
;
223 add
->src
[1].src
.is_ssa
= true;
224 add
->src
[1].src
.ssa
= &mul
->dest
.dest
.ssa
;
225 add
->dest
.write_mask
= 1;
226 nir_ssa_dest_init(&add
->instr
, &add
->dest
.dest
, 1, NULL
);
227 nir_instr_insert_before(instr
, &add
->instr
);
229 indirect
->is_ssa
= true;
230 indirect
->ssa
= &add
->dest
.dest
.ssa
;
232 indirect
->is_ssa
= true;
233 indirect
->ssa
= &mul
->dest
.dest
.ssa
;
234 found_indirect
= true;
237 } else if (tail
->deref_type
== nir_deref_type_struct
) {
238 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
240 for (unsigned i
= 0; i
< deref_struct
->index
; i
++)
241 base_offset
+= type_size(glsl_get_struct_field(parent_type
, i
));
249 nir_lower_io_block(nir_block
*block
, void *void_state
)
251 struct lower_io_state
*state
= void_state
;
253 nir_foreach_instr_safe(block
, instr
) {
254 if (instr
->type
!= nir_instr_type_intrinsic
)
257 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
259 switch (intrin
->intrinsic
) {
260 case nir_intrinsic_load_var
: {
261 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
262 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
265 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
267 /* Figure out the opcode */
268 nir_intrinsic_op load_op
;
270 case nir_var_shader_in
:
271 load_op
= has_indirect
? nir_intrinsic_load_input_indirect
:
272 nir_intrinsic_load_input
;
274 case nir_var_uniform
:
275 load_op
= has_indirect
? nir_intrinsic_load_uniform_indirect
:
276 nir_intrinsic_load_uniform
;
279 unreachable("Unknown variable mode");
282 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(state
->mem_ctx
,
284 load
->num_components
= intrin
->num_components
;
287 unsigned offset
= get_io_offset(intrin
->variables
[0],
288 &intrin
->instr
, &indirect
, state
);
289 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
291 load
->const_index
[0] = offset
;
292 load
->const_index
[1] = 1;
295 load
->src
[0] = indirect
;
297 if (intrin
->dest
.is_ssa
) {
298 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
299 intrin
->num_components
, NULL
);
300 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
301 nir_src_for_ssa(&load
->dest
.ssa
),
304 nir_dest_copy(&load
->dest
, &intrin
->dest
, state
->mem_ctx
);
307 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
308 nir_instr_remove(&intrin
->instr
);
312 case nir_intrinsic_store_var
: {
313 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
316 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
318 nir_intrinsic_op store_op
;
320 store_op
= nir_intrinsic_store_output_indirect
;
322 store_op
= nir_intrinsic_store_output
;
325 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
327 store
->num_components
= intrin
->num_components
;
330 unsigned offset
= get_io_offset(intrin
->variables
[0],
331 &intrin
->instr
, &indirect
, state
);
332 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
334 store
->const_index
[0] = offset
;
335 store
->const_index
[1] = 1;
337 nir_src_copy(&store
->src
[0], &intrin
->src
[0], state
->mem_ctx
);
340 store
->src
[1] = indirect
;
342 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
343 nir_instr_remove(&intrin
->instr
);
356 nir_lower_io_impl(nir_function_impl
*impl
)
358 struct lower_io_state state
;
360 state
.mem_ctx
= ralloc_parent(impl
);
362 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
364 nir_metadata_preserve(impl
, nir_metadata_block_index
|
365 nir_metadata_dominance
);
369 nir_lower_io(nir_shader
*shader
)
371 nir_foreach_overload(shader
, overload
) {
373 nir_lower_io_impl(overload
->impl
);