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
);
41 nir_variable_mode mode
;
45 nir_assign_var_locations(struct exec_list
*var_list
, unsigned *size
,
46 int (*type_size
)(const struct glsl_type
*))
48 unsigned location
= 0;
50 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
52 * UBO's have their own address spaces, so don't count them towards the
53 * number of global uniforms
55 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
56 var
->interface_type
!= NULL
)
59 var
->data
.driver_location
= location
;
60 location
+= type_size(var
->type
);
67 deref_has_indirect(nir_deref_var
*deref
)
69 for (nir_deref
*tail
= deref
->deref
.child
; tail
; tail
= tail
->child
) {
70 if (tail
->deref_type
== nir_deref_type_array
) {
71 nir_deref_array
*arr
= nir_deref_as_array(tail
);
72 if (arr
->deref_array_type
== nir_deref_array_type_indirect
)
81 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
, nir_src
*indirect
,
82 struct lower_io_state
*state
)
84 bool found_indirect
= false;
85 unsigned base_offset
= 0;
87 nir_builder
*b
= &state
->builder
;
88 b
->cursor
= nir_before_instr(instr
);
90 nir_deref
*tail
= &deref
->deref
;
91 while (tail
->child
!= NULL
) {
92 const struct glsl_type
*parent_type
= tail
->type
;
95 if (tail
->deref_type
== nir_deref_type_array
) {
96 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
97 unsigned size
= state
->type_size(tail
->type
);
99 base_offset
+= size
* deref_array
->base_offset
;
101 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
103 nir_imul(b
, nir_imm_int(b
, size
),
104 nir_ssa_for_src(b
, deref_array
->indirect
, 1));
106 if (found_indirect
) {
108 nir_iadd(b
, nir_ssa_for_src(b
, *indirect
, 1), mul
);
112 indirect
->is_ssa
= true;
113 found_indirect
= true;
115 } else if (tail
->deref_type
== nir_deref_type_struct
) {
116 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
118 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
120 state
->type_size(glsl_get_struct_field(parent_type
, i
));
128 static nir_intrinsic_op
129 load_op(nir_variable_mode mode
, bool has_indirect
)
133 case nir_var_shader_in
:
134 op
= has_indirect
? nir_intrinsic_load_input_indirect
:
135 nir_intrinsic_load_input
;
137 case nir_var_uniform
:
138 op
= has_indirect
? nir_intrinsic_load_uniform_indirect
:
139 nir_intrinsic_load_uniform
;
142 unreachable("Unknown variable mode");
148 nir_lower_io_block(nir_block
*block
, void *void_state
)
150 struct lower_io_state
*state
= void_state
;
152 nir_foreach_instr_safe(block
, instr
) {
153 if (instr
->type
!= nir_instr_type_intrinsic
)
156 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
158 if (intrin
->intrinsic
!= nir_intrinsic_load_var
&&
159 intrin
->intrinsic
!= nir_intrinsic_store_var
)
162 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
164 if (state
->mode
!= -1 && state
->mode
!= mode
)
167 switch (intrin
->intrinsic
) {
168 case nir_intrinsic_load_var
: {
169 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
172 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
174 nir_intrinsic_instr
*load
=
175 nir_intrinsic_instr_create(state
->mem_ctx
,
176 load_op(mode
, has_indirect
));
177 load
->num_components
= intrin
->num_components
;
180 unsigned offset
= get_io_offset(intrin
->variables
[0],
181 &intrin
->instr
, &indirect
, state
);
183 unsigned location
= intrin
->variables
[0]->var
->data
.driver_location
;
184 if (mode
== nir_var_uniform
) {
185 load
->const_index
[0] = location
;
186 load
->const_index
[1] = offset
;
188 load
->const_index
[0] = location
+ offset
;
192 load
->src
[0] = indirect
;
194 if (intrin
->dest
.is_ssa
) {
195 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
196 intrin
->num_components
, NULL
);
197 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
198 nir_src_for_ssa(&load
->dest
.ssa
));
200 nir_dest_copy(&load
->dest
, &intrin
->dest
, state
->mem_ctx
);
203 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
204 nir_instr_remove(&intrin
->instr
);
208 case nir_intrinsic_store_var
: {
209 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
212 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
214 nir_intrinsic_op store_op
;
216 store_op
= nir_intrinsic_store_output_indirect
;
218 store_op
= nir_intrinsic_store_output
;
221 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
223 store
->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 store
->const_index
[0] = offset
;
232 nir_src_copy(&store
->src
[0], &intrin
->src
[0], store
);
235 store
->src
[1] = indirect
;
237 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
238 nir_instr_remove(&intrin
->instr
);
251 nir_lower_io_impl(nir_function_impl
*impl
,
252 nir_variable_mode mode
,
253 int (*type_size
)(const struct glsl_type
*))
255 struct lower_io_state state
;
257 nir_builder_init(&state
.builder
, impl
);
258 state
.mem_ctx
= ralloc_parent(impl
);
260 state
.type_size
= type_size
;
262 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
264 nir_metadata_preserve(impl
, nir_metadata_block_index
|
265 nir_metadata_dominance
);
269 nir_lower_io(nir_shader
*shader
, nir_variable_mode mode
,
270 int (*type_size
)(const struct glsl_type
*))
272 nir_foreach_overload(shader
, overload
) {
274 nir_lower_io_impl(overload
->impl
, mode
, type_size
);