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 is_per_vertex_input(struct lower_io_state
*state
, nir_variable
*var
)
73 gl_shader_stage stage
= state
->builder
.shader
->stage
;
75 return var
->data
.mode
== nir_var_shader_in
&& !var
->data
.patch
&&
76 (stage
== MESA_SHADER_TESS_CTRL
||
77 stage
== MESA_SHADER_TESS_EVAL
||
78 stage
== MESA_SHADER_GEOMETRY
);
82 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
,
83 nir_ssa_def
**vertex_index
,
84 nir_ssa_def
**out_indirect
,
85 struct lower_io_state
*state
)
87 nir_ssa_def
*indirect
= NULL
;
88 unsigned base_offset
= 0;
90 nir_builder
*b
= &state
->builder
;
91 b
->cursor
= nir_before_instr(instr
);
93 nir_deref
*tail
= &deref
->deref
;
95 /* For per-vertex input arrays (i.e. geometry shader inputs), keep the
96 * outermost array index separate. Process the rest normally.
98 if (vertex_index
!= NULL
) {
100 assert(tail
->deref_type
== nir_deref_type_array
);
101 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
103 nir_ssa_def
*vtx
= nir_imm_int(b
, deref_array
->base_offset
);
104 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
105 vtx
= nir_iadd(b
, vtx
, nir_ssa_for_src(b
, deref_array
->indirect
, 1));
110 while (tail
->child
!= NULL
) {
111 const struct glsl_type
*parent_type
= tail
->type
;
114 if (tail
->deref_type
== nir_deref_type_array
) {
115 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
116 unsigned size
= state
->type_size(tail
->type
);
118 base_offset
+= size
* deref_array
->base_offset
;
120 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
122 nir_imul(b
, nir_imm_int(b
, size
),
123 nir_ssa_for_src(b
, deref_array
->indirect
, 1));
125 indirect
= indirect
? nir_iadd(b
, indirect
, mul
) : mul
;
127 } else if (tail
->deref_type
== nir_deref_type_struct
) {
128 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
130 for (unsigned i
= 0; i
< deref_struct
->index
; i
++) {
132 state
->type_size(glsl_get_struct_field(parent_type
, i
));
137 *out_indirect
= indirect
;
141 static nir_intrinsic_op
142 load_op(struct lower_io_state
*state
,
143 nir_variable_mode mode
, bool per_vertex
, bool has_indirect
)
147 case nir_var_shader_in
:
149 op
= has_indirect
? nir_intrinsic_load_per_vertex_input_indirect
:
150 nir_intrinsic_load_per_vertex_input
;
152 op
= has_indirect
? nir_intrinsic_load_input_indirect
:
153 nir_intrinsic_load_input
;
156 case nir_var_uniform
:
157 op
= has_indirect
? nir_intrinsic_load_uniform_indirect
:
158 nir_intrinsic_load_uniform
;
161 unreachable("Unknown variable mode");
167 nir_lower_io_block(nir_block
*block
, void *void_state
)
169 struct lower_io_state
*state
= void_state
;
171 nir_foreach_instr_safe(block
, instr
) {
172 if (instr
->type
!= nir_instr_type_intrinsic
)
175 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
177 if (intrin
->intrinsic
!= nir_intrinsic_load_var
&&
178 intrin
->intrinsic
!= nir_intrinsic_store_var
)
181 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
183 if (state
->mode
!= -1 && state
->mode
!= mode
)
186 switch (intrin
->intrinsic
) {
187 case nir_intrinsic_load_var
: {
188 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
192 is_per_vertex_input(state
, intrin
->variables
[0]->var
);
194 nir_ssa_def
*indirect
;
195 nir_ssa_def
*vertex_index
;
197 unsigned offset
= get_io_offset(intrin
->variables
[0], &intrin
->instr
,
198 per_vertex
? &vertex_index
: NULL
,
201 nir_intrinsic_instr
*load
=
202 nir_intrinsic_instr_create(state
->mem_ctx
,
203 load_op(state
, mode
, per_vertex
,
205 load
->num_components
= intrin
->num_components
;
207 unsigned location
= intrin
->variables
[0]->var
->data
.driver_location
;
208 if (mode
== nir_var_uniform
) {
209 load
->const_index
[0] = location
;
210 load
->const_index
[1] = offset
;
212 load
->const_index
[0] = location
+ offset
;
216 load
->src
[0] = nir_src_for_ssa(vertex_index
);
219 load
->src
[per_vertex
? 1 : 0] = nir_src_for_ssa(indirect
);
221 if (intrin
->dest
.is_ssa
) {
222 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
223 intrin
->num_components
, NULL
);
224 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
225 nir_src_for_ssa(&load
->dest
.ssa
));
227 nir_dest_copy(&load
->dest
, &intrin
->dest
, state
->mem_ctx
);
230 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
231 nir_instr_remove(&intrin
->instr
);
235 case nir_intrinsic_store_var
: {
236 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
239 nir_ssa_def
*indirect
;
241 unsigned offset
= get_io_offset(intrin
->variables
[0], &intrin
->instr
,
242 NULL
, &indirect
, state
);
243 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
245 nir_intrinsic_op store_op
;
247 store_op
= nir_intrinsic_store_output_indirect
;
249 store_op
= nir_intrinsic_store_output
;
252 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
254 store
->num_components
= intrin
->num_components
;
255 store
->const_index
[0] = offset
;
257 nir_src_copy(&store
->src
[0], &intrin
->src
[0], store
);
260 store
->src
[1] = nir_src_for_ssa(indirect
);
262 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
263 nir_instr_remove(&intrin
->instr
);
276 nir_lower_io_impl(nir_function_impl
*impl
,
277 nir_variable_mode mode
,
278 int (*type_size
)(const struct glsl_type
*))
280 struct lower_io_state state
;
282 nir_builder_init(&state
.builder
, impl
);
283 state
.mem_ctx
= ralloc_parent(impl
);
285 state
.type_size
= type_size
;
287 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
289 nir_metadata_preserve(impl
, nir_metadata_block_index
|
290 nir_metadata_dominance
);
294 nir_lower_io(nir_shader
*shader
, nir_variable_mode mode
,
295 int (*type_size
)(const struct glsl_type
*))
297 nir_foreach_overload(shader
, overload
) {
299 nir_lower_io_impl(overload
->impl
, mode
, type_size
);