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 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
, nir_src
*indirect
,
81 struct lower_io_state
*state
)
83 bool found_indirect
= false;
84 unsigned base_offset
= 0;
86 nir_builder
*b
= &state
->builder
;
87 b
->cursor
= nir_before_instr(instr
);
89 nir_deref
*tail
= &deref
->deref
;
90 while (tail
->child
!= NULL
) {
91 const struct glsl_type
*parent_type
= tail
->type
;
94 if (tail
->deref_type
== nir_deref_type_array
) {
95 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
96 unsigned size
= state
->type_size(tail
->type
);
98 base_offset
+= size
* deref_array
->base_offset
;
100 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
102 nir_imul(b
, nir_imm_int(b
, size
),
103 nir_ssa_for_src(b
, deref_array
->indirect
, 1));
105 if (found_indirect
) {
107 nir_iadd(b
, nir_ssa_for_src(b
, *indirect
, 1), mul
);
111 indirect
->is_ssa
= true;
112 found_indirect
= true;
114 } else if (tail
->deref_type
== nir_deref_type_struct
) {
115 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
117 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
119 state
->type_size(glsl_get_struct_field(parent_type
, i
));
127 static nir_intrinsic_op
128 load_op(nir_variable_mode mode
, bool has_indirect
)
132 case nir_var_shader_in
:
133 op
= has_indirect
? nir_intrinsic_load_input_indirect
:
134 nir_intrinsic_load_input
;
136 case nir_var_uniform
:
137 op
= has_indirect
? nir_intrinsic_load_uniform_indirect
:
138 nir_intrinsic_load_uniform
;
141 unreachable("Unknown variable mode");
147 nir_lower_io_block(nir_block
*block
, void *void_state
)
149 struct lower_io_state
*state
= void_state
;
151 nir_foreach_instr_safe(block
, instr
) {
152 if (instr
->type
!= nir_instr_type_intrinsic
)
155 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
157 switch (intrin
->intrinsic
) {
158 case nir_intrinsic_load_var
: {
159 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
160 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
163 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
165 nir_intrinsic_instr
*load
=
166 nir_intrinsic_instr_create(state
->mem_ctx
,
167 load_op(mode
, has_indirect
));
168 load
->num_components
= intrin
->num_components
;
171 unsigned offset
= get_io_offset(intrin
->variables
[0],
172 &intrin
->instr
, &indirect
, state
);
174 unsigned location
= intrin
->variables
[0]->var
->data
.driver_location
;
175 if (mode
== nir_var_uniform
) {
176 load
->const_index
[0] = location
;
177 load
->const_index
[1] = offset
;
179 load
->const_index
[0] = location
+ offset
;
183 load
->src
[0] = indirect
;
185 if (intrin
->dest
.is_ssa
) {
186 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
187 intrin
->num_components
, NULL
);
188 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
189 nir_src_for_ssa(&load
->dest
.ssa
));
191 nir_dest_copy(&load
->dest
, &intrin
->dest
, state
->mem_ctx
);
194 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
195 nir_instr_remove(&intrin
->instr
);
199 case nir_intrinsic_store_var
: {
200 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
203 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
205 nir_intrinsic_op store_op
;
207 store_op
= nir_intrinsic_store_output_indirect
;
209 store_op
= nir_intrinsic_store_output
;
212 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
214 store
->num_components
= intrin
->num_components
;
217 unsigned offset
= get_io_offset(intrin
->variables
[0],
218 &intrin
->instr
, &indirect
, state
);
219 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
221 store
->const_index
[0] = offset
;
223 nir_src_copy(&store
->src
[0], &intrin
->src
[0], store
);
226 store
->src
[1] = indirect
;
228 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
229 nir_instr_remove(&intrin
->instr
);
242 nir_lower_io_impl(nir_function_impl
*impl
, int(*type_size
)(const struct glsl_type
*))
244 struct lower_io_state state
;
246 nir_builder_init(&state
.builder
, impl
);
247 state
.mem_ctx
= ralloc_parent(impl
);
248 state
.type_size
= type_size
;
250 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
252 nir_metadata_preserve(impl
, nir_metadata_block_index
|
253 nir_metadata_dominance
);
257 nir_lower_io(nir_shader
*shader
, int(*type_size
)(const struct glsl_type
*))
259 nir_foreach_overload(shader
, overload
) {
261 nir_lower_io_impl(overload
->impl
, type_size
);