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.
35 #include "nir_builder.h"
37 struct lower_io_state
{
40 int (*type_size
)(const struct glsl_type
*type
);
44 nir_assign_var_locations(struct exec_list
*var_list
, unsigned *size
,
45 int (*type_size
)(const struct glsl_type
*))
47 unsigned location
= 0;
49 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
51 * UBO's have their own address spaces, so don't count them towards the
52 * number of global uniforms
54 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
55 var
->interface_type
!= NULL
)
58 var
->data
.driver_location
= location
;
59 location
+= type_size(var
->type
);
66 deref_has_indirect(nir_deref_var
*deref
)
68 for (nir_deref
*tail
= deref
->deref
.child
; tail
; tail
= tail
->child
) {
69 if (tail
->deref_type
== nir_deref_type_array
) {
70 nir_deref_array
*arr
= nir_deref_as_array(tail
);
71 if (arr
->deref_array_type
== nir_deref_array_type_indirect
)
80 mark_indirect_uses_block(nir_block
*block
, void *void_state
)
82 struct set
*indirect_set
= void_state
;
84 nir_foreach_instr(block
, instr
) {
85 if (instr
->type
!= nir_instr_type_intrinsic
)
88 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
91 i
< nir_intrinsic_infos
[intrin
->intrinsic
].num_variables
; i
++) {
92 if (deref_has_indirect(intrin
->variables
[i
]))
93 _mesa_set_add(indirect_set
, intrin
->variables
[i
]->var
);
100 /* Identical to nir_assign_var_locations_packed except that it assigns
101 * locations to the variables that are used 100% directly first and then
102 * assigns locations to variables that are used indirectly.
105 nir_assign_var_locations_direct_first(nir_shader
*shader
,
106 struct exec_list
*var_list
,
107 unsigned *direct_size
,
109 int (*type_size
)(const struct glsl_type
*))
111 struct set
*indirect_set
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
112 _mesa_key_pointer_equal
);
114 nir_foreach_overload(shader
, overload
) {
116 nir_foreach_block(overload
->impl
, mark_indirect_uses_block
,
120 unsigned location
= 0;
122 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
123 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
124 var
->interface_type
!= NULL
)
127 if (_mesa_set_search(indirect_set
, var
))
130 var
->data
.driver_location
= location
;
131 location
+= type_size(var
->type
);
134 *direct_size
= location
;
136 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
137 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
138 var
->interface_type
!= NULL
)
141 if (!_mesa_set_search(indirect_set
, var
))
144 var
->data
.driver_location
= location
;
145 location
+= type_size(var
->type
);
150 _mesa_set_destroy(indirect_set
, NULL
);
154 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
, nir_src
*indirect
,
155 struct lower_io_state
*state
)
157 bool found_indirect
= false;
158 unsigned base_offset
= 0;
160 nir_builder
*b
= &state
->builder
;
161 nir_builder_insert_before_instr(b
, instr
);
163 nir_deref
*tail
= &deref
->deref
;
164 while (tail
->child
!= NULL
) {
165 const struct glsl_type
*parent_type
= tail
->type
;
168 if (tail
->deref_type
== nir_deref_type_array
) {
169 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
170 unsigned size
= state
->type_size(tail
->type
);
172 base_offset
+= size
* deref_array
->base_offset
;
174 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
176 nir_imul(b
, nir_imm_int(b
, size
),
177 nir_ssa_for_src(b
, deref_array
->indirect
, 1));
179 if (found_indirect
) {
181 nir_iadd(b
, nir_ssa_for_src(b
, *indirect
, 1), mul
);
185 indirect
->is_ssa
= true;
186 found_indirect
= true;
188 } else if (tail
->deref_type
== nir_deref_type_struct
) {
189 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
191 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
193 state
->type_size(glsl_get_struct_field(parent_type
, i
));
201 static nir_intrinsic_op
202 load_op(nir_variable_mode mode
, bool has_indirect
)
206 case nir_var_shader_in
:
207 op
= has_indirect
? nir_intrinsic_load_input_indirect
:
208 nir_intrinsic_load_input
;
210 case nir_var_uniform
:
211 op
= has_indirect
? nir_intrinsic_load_uniform_indirect
:
212 nir_intrinsic_load_uniform
;
215 unreachable("Unknown variable mode");
221 nir_lower_io_block(nir_block
*block
, void *void_state
)
223 struct lower_io_state
*state
= void_state
;
225 nir_foreach_instr_safe(block
, instr
) {
226 if (instr
->type
!= nir_instr_type_intrinsic
)
229 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
231 switch (intrin
->intrinsic
) {
232 case nir_intrinsic_load_var
: {
233 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
234 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
237 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
239 nir_intrinsic_instr
*load
=
240 nir_intrinsic_instr_create(state
->mem_ctx
,
241 load_op(mode
, has_indirect
));
242 load
->num_components
= intrin
->num_components
;
245 unsigned offset
= get_io_offset(intrin
->variables
[0],
246 &intrin
->instr
, &indirect
, state
);
248 unsigned location
= intrin
->variables
[0]->var
->data
.driver_location
;
249 if (mode
== nir_var_uniform
) {
250 load
->const_index
[0] = location
;
251 load
->const_index
[1] = offset
;
253 load
->const_index
[0] = location
+ offset
;
257 load
->src
[0] = indirect
;
259 if (intrin
->dest
.is_ssa
) {
260 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
261 intrin
->num_components
, NULL
);
262 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
263 nir_src_for_ssa(&load
->dest
.ssa
),
266 nir_dest_copy(&load
->dest
, &intrin
->dest
, state
->mem_ctx
);
269 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
270 nir_instr_remove(&intrin
->instr
);
274 case nir_intrinsic_store_var
: {
275 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
278 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
280 nir_intrinsic_op store_op
;
282 store_op
= nir_intrinsic_store_output_indirect
;
284 store_op
= nir_intrinsic_store_output
;
287 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
289 store
->num_components
= intrin
->num_components
;
292 unsigned offset
= get_io_offset(intrin
->variables
[0],
293 &intrin
->instr
, &indirect
, state
);
294 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
296 store
->const_index
[0] = offset
;
298 nir_src_copy(&store
->src
[0], &intrin
->src
[0], state
->mem_ctx
);
301 store
->src
[1] = indirect
;
303 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
304 nir_instr_remove(&intrin
->instr
);
317 nir_lower_io_impl(nir_function_impl
*impl
, int(*type_size
)(const struct glsl_type
*))
319 struct lower_io_state state
;
321 nir_builder_init(&state
.builder
, impl
);
322 state
.mem_ctx
= ralloc_parent(impl
);
323 state
.type_size
= type_size
;
325 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
327 nir_metadata_preserve(impl
, nir_metadata_block_index
|
328 nir_metadata_dominance
);
332 nir_lower_io(nir_shader
*shader
, int(*type_size
)(const struct glsl_type
*))
334 nir_foreach_overload(shader
, overload
) {
336 nir_lower_io_impl(overload
->impl
, type_size
);