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 nir_foreach_variable(var
, 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 * Returns true if we're processing a stage whose inputs are arrays indexed
68 * by a vertex number (such as geometry shader inputs).
71 stage_uses_per_vertex_inputs(struct lower_io_state
*state
)
73 gl_shader_stage stage
= state
->builder
.shader
->stage
;
74 return stage
== MESA_SHADER_GEOMETRY
;
78 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
,
79 nir_ssa_def
**vertex_index
,
80 nir_ssa_def
**out_indirect
,
81 struct lower_io_state
*state
)
83 nir_ssa_def
*indirect
= NULL
;
84 unsigned base_offset
= 0;
86 nir_builder
*b
= &state
->builder
;
87 b
->cursor
= nir_before_instr(instr
);
89 nir_deref
*tail
= &deref
->deref
;
91 /* For per-vertex input arrays (i.e. geometry shader inputs), keep the
92 * outermost array index separate. Process the rest normally.
94 if (vertex_index
!= NULL
) {
96 assert(tail
->deref_type
== nir_deref_type_array
);
97 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
99 nir_ssa_def
*vtx
= nir_imm_int(b
, deref_array
->base_offset
);
100 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
101 vtx
= nir_iadd(b
, vtx
, nir_ssa_for_src(b
, deref_array
->indirect
, 1));
106 while (tail
->child
!= NULL
) {
107 const struct glsl_type
*parent_type
= tail
->type
;
110 if (tail
->deref_type
== nir_deref_type_array
) {
111 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
112 unsigned size
= state
->type_size(tail
->type
);
114 base_offset
+= size
* deref_array
->base_offset
;
116 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
118 nir_imul(b
, nir_imm_int(b
, size
),
119 nir_ssa_for_src(b
, deref_array
->indirect
, 1));
121 indirect
= indirect
? nir_iadd(b
, indirect
, mul
) : mul
;
123 } else if (tail
->deref_type
== nir_deref_type_struct
) {
124 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
126 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
128 state
->type_size(glsl_get_struct_field(parent_type
, i
));
133 *out_indirect
= indirect
;
137 static nir_intrinsic_op
138 load_op(struct lower_io_state
*state
,
139 nir_variable_mode mode
, bool per_vertex
, bool has_indirect
)
143 case nir_var_shader_in
:
145 op
= has_indirect
? nir_intrinsic_load_per_vertex_input_indirect
:
146 nir_intrinsic_load_per_vertex_input
;
148 op
= has_indirect
? nir_intrinsic_load_input_indirect
:
149 nir_intrinsic_load_input
;
152 case nir_var_uniform
:
153 op
= has_indirect
? nir_intrinsic_load_uniform_indirect
:
154 nir_intrinsic_load_uniform
;
157 unreachable("Unknown variable mode");
163 nir_lower_io_block(nir_block
*block
, void *void_state
)
165 struct lower_io_state
*state
= void_state
;
167 nir_foreach_instr_safe(block
, instr
) {
168 if (instr
->type
!= nir_instr_type_intrinsic
)
171 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
173 if (intrin
->intrinsic
!= nir_intrinsic_load_var
&&
174 intrin
->intrinsic
!= nir_intrinsic_store_var
)
177 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
179 if (state
->mode
!= -1 && state
->mode
!= mode
)
182 switch (intrin
->intrinsic
) {
183 case nir_intrinsic_load_var
: {
184 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
187 bool per_vertex
= stage_uses_per_vertex_inputs(state
) &&
188 mode
== nir_var_shader_in
;
190 nir_ssa_def
*indirect
;
191 nir_ssa_def
*vertex_index
;
193 unsigned offset
= get_io_offset(intrin
->variables
[0], &intrin
->instr
,
194 per_vertex
? &vertex_index
: NULL
,
197 nir_intrinsic_instr
*load
=
198 nir_intrinsic_instr_create(state
->mem_ctx
,
199 load_op(state
, mode
, per_vertex
,
201 load
->num_components
= intrin
->num_components
;
203 unsigned location
= intrin
->variables
[0]->var
->data
.driver_location
;
204 if (mode
== nir_var_uniform
) {
205 load
->const_index
[0] = location
;
206 load
->const_index
[1] = offset
;
208 load
->const_index
[0] = location
+ offset
;
212 load
->src
[0] = nir_src_for_ssa(vertex_index
);
215 load
->src
[per_vertex
? 1 : 0] = nir_src_for_ssa(indirect
);
217 if (intrin
->dest
.is_ssa
) {
218 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
219 intrin
->num_components
, NULL
);
220 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
221 nir_src_for_ssa(&load
->dest
.ssa
));
223 nir_dest_copy(&load
->dest
, &intrin
->dest
, state
->mem_ctx
);
226 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
227 nir_instr_remove(&intrin
->instr
);
231 case nir_intrinsic_store_var
: {
232 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
235 nir_ssa_def
*indirect
;
237 unsigned offset
= get_io_offset(intrin
->variables
[0], &intrin
->instr
,
238 NULL
, &indirect
, state
);
239 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
241 nir_intrinsic_op store_op
;
243 store_op
= nir_intrinsic_store_output_indirect
;
245 store_op
= nir_intrinsic_store_output
;
248 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
250 store
->num_components
= intrin
->num_components
;
251 store
->const_index
[0] = offset
;
253 nir_src_copy(&store
->src
[0], &intrin
->src
[0], store
);
256 store
->src
[1] = nir_src_for_ssa(indirect
);
258 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
259 nir_instr_remove(&intrin
->instr
);
272 nir_lower_io_impl(nir_function_impl
*impl
,
273 nir_variable_mode mode
,
274 int (*type_size
)(const struct glsl_type
*))
276 struct lower_io_state state
;
278 nir_builder_init(&state
.builder
, impl
);
279 state
.mem_ctx
= ralloc_parent(impl
);
281 state
.type_size
= type_size
;
283 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
285 nir_metadata_preserve(impl
, nir_metadata_block_index
|
286 nir_metadata_dominance
);
290 nir_lower_io(nir_shader
*shader
, nir_variable_mode mode
,
291 int (*type_size
)(const struct glsl_type
*))
293 nir_foreach_overload(shader
, overload
) {
295 nir_lower_io_impl(overload
->impl
, mode
, type_size
);