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 unreachable("not reached");
79 assign_var_locations(struct hash_table
*ht
, unsigned *size
)
81 unsigned location
= 0;
83 struct hash_entry
*entry
;
84 hash_table_foreach(ht
, entry
) {
85 nir_variable
*var
= (nir_variable
*) entry
->data
;
88 * UBO's have their own address spaces, so don't count them towards the
89 * number of global uniforms
91 if (var
->data
.mode
== nir_var_uniform
&& var
->interface_type
!= NULL
)
94 var
->data
.driver_location
= location
;
95 location
+= type_size(var
->type
);
102 assign_var_locations_shader(nir_shader
*shader
)
104 assign_var_locations(shader
->inputs
, &shader
->num_inputs
);
105 assign_var_locations(shader
->outputs
, &shader
->num_outputs
);
106 assign_var_locations(shader
->uniforms
, &shader
->num_uniforms
);
110 deref_has_indirect(nir_deref_var
*deref
)
112 for (nir_deref
*tail
= deref
->deref
.child
; tail
; tail
= tail
->child
) {
113 if (tail
->deref_type
== nir_deref_type_array
) {
114 nir_deref_array
*arr
= nir_deref_as_array(tail
);
115 if (arr
->deref_array_type
== nir_deref_array_type_indirect
)
124 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
, nir_src
*indirect
,
125 struct lower_io_state
*state
)
127 bool found_indirect
= false;
128 unsigned base_offset
= 0;
130 nir_deref
*tail
= &deref
->deref
;
131 while (tail
->child
!= NULL
) {
132 const struct glsl_type
*parent_type
= tail
->type
;
135 if (tail
->deref_type
== nir_deref_type_array
) {
136 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
137 unsigned size
= type_size(tail
->type
);
139 base_offset
+= size
* deref_array
->base_offset
;
141 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
142 nir_load_const_instr
*load_const
=
143 nir_load_const_instr_create(state
->mem_ctx
);
144 load_const
->num_components
= 1;
145 load_const
->value
.u
[0] = size
;
146 load_const
->dest
.is_ssa
= true;
147 nir_ssa_def_init(&load_const
->instr
, &load_const
->dest
.ssa
,
149 nir_instr_insert_before(instr
, &load_const
->instr
);
151 nir_alu_instr
*mul
= nir_alu_instr_create(state
->mem_ctx
,
153 mul
->src
[0].src
.is_ssa
= true;
154 mul
->src
[0].src
.ssa
= &load_const
->dest
.ssa
;
155 mul
->src
[1].src
= nir_src_copy(deref_array
->indirect
,
157 mul
->dest
.write_mask
= 1;
158 mul
->dest
.dest
.is_ssa
= true;
159 nir_ssa_def_init(&mul
->instr
, &mul
->dest
.dest
.ssa
, 1, NULL
);
160 nir_instr_insert_before(instr
, &mul
->instr
);
162 if (found_indirect
) {
163 nir_alu_instr
*add
= nir_alu_instr_create(state
->mem_ctx
,
165 add
->src
[0].src
= *indirect
;
166 add
->src
[1].src
.is_ssa
= true;
167 add
->src
[1].src
.ssa
= &mul
->dest
.dest
.ssa
;
168 add
->dest
.write_mask
= 1;
169 add
->dest
.dest
.is_ssa
= true;
170 nir_ssa_def_init(&add
->instr
, &add
->dest
.dest
.ssa
, 1, NULL
);
171 nir_instr_insert_before(instr
, &add
->instr
);
173 indirect
->is_ssa
= true;
174 indirect
->ssa
= &add
->dest
.dest
.ssa
;
176 indirect
->is_ssa
= true;
177 indirect
->ssa
= &mul
->dest
.dest
.ssa
;
178 found_indirect
= true;
181 } else if (tail
->deref_type
== nir_deref_type_struct
) {
182 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
184 for (unsigned i
= 0; i
< deref_struct
->index
; i
++)
185 base_offset
+= type_size(glsl_get_struct_field(parent_type
, i
));
193 nir_lower_io_block(nir_block
*block
, void *void_state
)
195 struct lower_io_state
*state
= void_state
;
197 nir_foreach_instr_safe(block
, instr
) {
198 if (instr
->type
!= nir_instr_type_intrinsic
)
201 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
203 switch (intrin
->intrinsic
) {
204 case nir_intrinsic_load_var
: {
205 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
206 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
209 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
211 nir_intrinsic_op load_op
;
213 case nir_var_shader_in
:
215 load_op
= nir_intrinsic_load_input_indirect
;
217 load_op
= nir_intrinsic_load_input
;
220 case nir_var_uniform
:
222 load_op
= nir_intrinsic_load_uniform_indirect
;
224 load_op
= nir_intrinsic_load_uniform
;
228 unreachable("Unknown variable mode");
230 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(state
->mem_ctx
,
232 load
->num_components
= intrin
->num_components
;
235 unsigned offset
= get_io_offset(intrin
->variables
[0],
236 &intrin
->instr
, &indirect
, state
);
237 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
239 load
->const_index
[0] = offset
;
240 load
->const_index
[1] = 1;
243 load
->src
[0] = indirect
;
245 if (intrin
->dest
.is_ssa
) {
246 load
->dest
.is_ssa
= true;
247 nir_ssa_def_init(&load
->instr
, &load
->dest
.ssa
,
248 intrin
->num_components
, NULL
);
252 .ssa
= &load
->dest
.ssa
,
255 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
, new_src
,
258 load
->dest
= nir_dest_copy(intrin
->dest
, state
->mem_ctx
);
261 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
262 nir_instr_remove(&intrin
->instr
);
266 case nir_intrinsic_store_var
: {
267 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
270 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
272 nir_intrinsic_op store_op
;
274 store_op
= nir_intrinsic_store_output_indirect
;
276 store_op
= nir_intrinsic_store_output
;
279 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
281 store
->num_components
= intrin
->num_components
;
284 unsigned offset
= get_io_offset(intrin
->variables
[0],
285 &intrin
->instr
, &indirect
, state
);
286 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
288 store
->const_index
[0] = offset
;
289 store
->const_index
[1] = 1;
291 store
->src
[0] = nir_src_copy(intrin
->src
[0], state
->mem_ctx
);
294 store
->src
[1] = indirect
;
296 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
297 nir_instr_remove(&intrin
->instr
);
310 nir_lower_io_impl(nir_function_impl
*impl
)
312 struct lower_io_state state
;
314 state
.mem_ctx
= ralloc_parent(impl
);
316 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
320 nir_lower_io(nir_shader
*shader
)
322 assign_var_locations_shader(shader
);
324 nir_foreach_overload(shader
, overload
) {
326 nir_lower_io_impl(overload
->impl
);