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
:
72 case GLSL_TYPE_DOUBLE
:
73 unreachable("not reached");
80 assign_var_locations(struct hash_table
*ht
, unsigned *size
)
82 unsigned location
= 0;
84 struct hash_entry
*entry
;
85 hash_table_foreach(ht
, entry
) {
86 nir_variable
*var
= (nir_variable
*) entry
->data
;
89 * UBO's have their own address spaces, so don't count them towards the
90 * number of global uniforms
92 if (var
->data
.mode
== nir_var_uniform
&& var
->interface_type
!= NULL
)
95 var
->data
.driver_location
= location
;
96 location
+= type_size(var
->type
);
103 assign_var_locations_shader(nir_shader
*shader
)
105 assign_var_locations(shader
->inputs
, &shader
->num_inputs
);
106 assign_var_locations(shader
->outputs
, &shader
->num_outputs
);
107 assign_var_locations(shader
->uniforms
, &shader
->num_uniforms
);
111 deref_has_indirect(nir_deref_var
*deref
)
113 for (nir_deref
*tail
= deref
->deref
.child
; tail
; tail
= tail
->child
) {
114 if (tail
->deref_type
== nir_deref_type_array
) {
115 nir_deref_array
*arr
= nir_deref_as_array(tail
);
116 if (arr
->deref_array_type
== nir_deref_array_type_indirect
)
125 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
, nir_src
*indirect
,
126 struct lower_io_state
*state
)
128 bool found_indirect
= false;
129 unsigned base_offset
= 0;
131 nir_deref
*tail
= &deref
->deref
;
132 while (tail
->child
!= NULL
) {
133 const struct glsl_type
*parent_type
= tail
->type
;
136 if (tail
->deref_type
== nir_deref_type_array
) {
137 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
138 unsigned size
= type_size(tail
->type
);
140 base_offset
+= size
* deref_array
->base_offset
;
142 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
143 nir_load_const_instr
*load_const
=
144 nir_load_const_instr_create(state
->mem_ctx
, 1);
145 load_const
->value
.u
[0] = size
;
146 nir_instr_insert_before(instr
, &load_const
->instr
);
148 nir_alu_instr
*mul
= nir_alu_instr_create(state
->mem_ctx
,
150 mul
->src
[0].src
.is_ssa
= true;
151 mul
->src
[0].src
.ssa
= &load_const
->def
;
152 nir_src_copy(&mul
->src
[1].src
, &deref_array
->indirect
,
154 mul
->dest
.write_mask
= 1;
155 nir_ssa_dest_init(&mul
->instr
, &mul
->dest
.dest
, 1, NULL
);
156 nir_instr_insert_before(instr
, &mul
->instr
);
158 if (found_indirect
) {
159 nir_alu_instr
*add
= nir_alu_instr_create(state
->mem_ctx
,
161 add
->src
[0].src
= *indirect
;
162 add
->src
[1].src
.is_ssa
= true;
163 add
->src
[1].src
.ssa
= &mul
->dest
.dest
.ssa
;
164 add
->dest
.write_mask
= 1;
165 nir_ssa_dest_init(&add
->instr
, &add
->dest
.dest
, 1, NULL
);
166 nir_instr_insert_before(instr
, &add
->instr
);
168 indirect
->is_ssa
= true;
169 indirect
->ssa
= &add
->dest
.dest
.ssa
;
171 indirect
->is_ssa
= true;
172 indirect
->ssa
= &mul
->dest
.dest
.ssa
;
173 found_indirect
= true;
176 } else if (tail
->deref_type
== nir_deref_type_struct
) {
177 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
179 for (unsigned i
= 0; i
< deref_struct
->index
; i
++)
180 base_offset
+= type_size(glsl_get_struct_field(parent_type
, i
));
188 nir_lower_io_block(nir_block
*block
, void *void_state
)
190 struct lower_io_state
*state
= void_state
;
192 nir_foreach_instr_safe(block
, instr
) {
193 if (instr
->type
!= nir_instr_type_intrinsic
)
196 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
198 switch (intrin
->intrinsic
) {
199 case nir_intrinsic_load_var
: {
200 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
201 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
204 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
206 /* Figure out the opcode */
207 nir_intrinsic_op load_op
;
209 case nir_var_shader_in
:
210 load_op
= has_indirect
? nir_intrinsic_load_input_indirect
:
211 nir_intrinsic_load_input
;
213 case nir_var_uniform
:
214 load_op
= has_indirect
? nir_intrinsic_load_uniform_indirect
:
215 nir_intrinsic_load_uniform
;
218 unreachable("Unknown variable mode");
221 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(state
->mem_ctx
,
223 load
->num_components
= intrin
->num_components
;
226 unsigned offset
= get_io_offset(intrin
->variables
[0],
227 &intrin
->instr
, &indirect
, state
);
228 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
230 load
->const_index
[0] = offset
;
231 load
->const_index
[1] = 1;
234 load
->src
[0] = indirect
;
236 if (intrin
->dest
.is_ssa
) {
237 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
238 intrin
->num_components
, NULL
);
239 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
240 nir_src_for_ssa(&load
->dest
.ssa
),
243 nir_dest_copy(&load
->dest
, &intrin
->dest
, state
->mem_ctx
);
246 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
247 nir_instr_remove(&intrin
->instr
);
251 case nir_intrinsic_store_var
: {
252 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
255 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
257 nir_intrinsic_op store_op
;
259 store_op
= nir_intrinsic_store_output_indirect
;
261 store_op
= nir_intrinsic_store_output
;
264 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
266 store
->num_components
= intrin
->num_components
;
269 unsigned offset
= get_io_offset(intrin
->variables
[0],
270 &intrin
->instr
, &indirect
, state
);
271 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
273 store
->const_index
[0] = offset
;
274 store
->const_index
[1] = 1;
276 nir_src_copy(&store
->src
[0], &intrin
->src
[0], state
->mem_ctx
);
279 store
->src
[1] = indirect
;
281 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
282 nir_instr_remove(&intrin
->instr
);
295 nir_lower_io_impl(nir_function_impl
*impl
)
297 struct lower_io_state state
;
299 state
.mem_ctx
= ralloc_parent(impl
);
301 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
303 nir_metadata_preserve(impl
, nir_metadata_block_index
|
304 nir_metadata_dominance
);
308 nir_lower_io(nir_shader
*shader
)
310 assign_var_locations_shader(shader
);
312 nir_foreach_overload(shader
, overload
) {
314 nir_lower_io_impl(overload
->impl
);