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.
36 struct lower_io_state
{
42 type_size_vec4(const struct glsl_type
*type
)
47 switch (glsl_get_base_type(type
)) {
52 if (glsl_type_is_matrix(type
)) {
53 return glsl_get_matrix_columns(type
);
58 return type_size_vec4(glsl_get_array_element(type
)) * glsl_get_length(type
);
59 case GLSL_TYPE_STRUCT
:
61 for (i
= 0; i
< glsl_get_length(type
); i
++) {
62 size
+= type_size_vec4(glsl_get_struct_field(type
, i
));
65 case GLSL_TYPE_SAMPLER
:
67 case GLSL_TYPE_ATOMIC_UINT
:
71 case GLSL_TYPE_DOUBLE
:
73 case GLSL_TYPE_INTERFACE
:
74 unreachable("not reached");
81 type_size_scalar(const struct glsl_type
*type
)
85 switch (glsl_get_base_type(type
)) {
90 return glsl_get_components(type
);
92 return type_size_scalar(glsl_get_array_element(type
)) * glsl_get_length(type
);
93 case GLSL_TYPE_STRUCT
:
95 for (i
= 0; i
< glsl_get_length(type
); i
++) {
96 size
+= type_size_scalar(glsl_get_struct_field(type
, i
));
99 case GLSL_TYPE_SUBROUTINE
:
101 case GLSL_TYPE_SAMPLER
:
103 case GLSL_TYPE_ATOMIC_UINT
:
105 case GLSL_TYPE_INTERFACE
:
107 case GLSL_TYPE_IMAGE
:
110 case GLSL_TYPE_ERROR
:
111 case GLSL_TYPE_DOUBLE
:
112 unreachable("not reached");
119 type_size(const struct glsl_type
*type
, bool is_scalar
)
122 return type_size_scalar(type
);
124 return type_size_vec4(type
);
128 nir_assign_var_locations(struct exec_list
*var_list
, unsigned *size
, bool is_scalar
)
130 unsigned location
= 0;
132 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
134 * UBO's have their own address spaces, so don't count them towards the
135 * number of global uniforms
137 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
138 var
->interface_type
!= NULL
)
141 var
->data
.driver_location
= location
;
142 location
+= type_size(var
->type
, is_scalar
);
149 deref_has_indirect(nir_deref_var
*deref
)
151 for (nir_deref
*tail
= deref
->deref
.child
; tail
; tail
= tail
->child
) {
152 if (tail
->deref_type
== nir_deref_type_array
) {
153 nir_deref_array
*arr
= nir_deref_as_array(tail
);
154 if (arr
->deref_array_type
== nir_deref_array_type_indirect
)
163 mark_indirect_uses_block(nir_block
*block
, void *void_state
)
165 struct set
*indirect_set
= void_state
;
167 nir_foreach_instr(block
, instr
) {
168 if (instr
->type
!= nir_instr_type_intrinsic
)
171 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
174 i
< nir_intrinsic_infos
[intrin
->intrinsic
].num_variables
; i
++) {
175 if (deref_has_indirect(intrin
->variables
[i
]))
176 _mesa_set_add(indirect_set
, intrin
->variables
[i
]->var
);
183 /* Identical to nir_assign_var_locations_packed except that it assigns
184 * locations to the variables that are used 100% directly first and then
185 * assigns locations to variables that are used indirectly.
188 nir_assign_var_locations_direct_first(nir_shader
*shader
,
189 struct exec_list
*var_list
,
190 unsigned *direct_size
,
194 struct set
*indirect_set
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
195 _mesa_key_pointer_equal
);
197 nir_foreach_overload(shader
, overload
) {
199 nir_foreach_block(overload
->impl
, mark_indirect_uses_block
,
203 unsigned location
= 0;
205 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
206 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
207 var
->interface_type
!= NULL
)
210 if (_mesa_set_search(indirect_set
, var
))
213 var
->data
.driver_location
= location
;
214 location
+= type_size(var
->type
, is_scalar
);
217 *direct_size
= location
;
219 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
220 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
221 var
->interface_type
!= NULL
)
224 if (!_mesa_set_search(indirect_set
, var
))
227 var
->data
.driver_location
= location
;
228 location
+= type_size(var
->type
, is_scalar
);
233 _mesa_set_destroy(indirect_set
, NULL
);
237 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
, nir_src
*indirect
,
238 struct lower_io_state
*state
)
240 bool found_indirect
= false;
241 unsigned base_offset
= 0;
243 nir_deref
*tail
= &deref
->deref
;
244 while (tail
->child
!= NULL
) {
245 const struct glsl_type
*parent_type
= tail
->type
;
248 if (tail
->deref_type
== nir_deref_type_array
) {
249 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
250 unsigned size
= type_size(tail
->type
, state
->is_scalar
);
252 base_offset
+= size
* deref_array
->base_offset
;
254 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
255 nir_load_const_instr
*load_const
=
256 nir_load_const_instr_create(state
->mem_ctx
, 1);
257 load_const
->value
.u
[0] = size
;
258 nir_instr_insert_before(instr
, &load_const
->instr
);
260 nir_alu_instr
*mul
= nir_alu_instr_create(state
->mem_ctx
,
262 mul
->src
[0].src
.is_ssa
= true;
263 mul
->src
[0].src
.ssa
= &load_const
->def
;
264 nir_src_copy(&mul
->src
[1].src
, &deref_array
->indirect
,
266 mul
->dest
.write_mask
= 1;
267 nir_ssa_dest_init(&mul
->instr
, &mul
->dest
.dest
, 1, NULL
);
268 nir_instr_insert_before(instr
, &mul
->instr
);
270 if (found_indirect
) {
271 nir_alu_instr
*add
= nir_alu_instr_create(state
->mem_ctx
,
273 add
->src
[0].src
= *indirect
;
274 add
->src
[1].src
.is_ssa
= true;
275 add
->src
[1].src
.ssa
= &mul
->dest
.dest
.ssa
;
276 add
->dest
.write_mask
= 1;
277 nir_ssa_dest_init(&add
->instr
, &add
->dest
.dest
, 1, NULL
);
278 nir_instr_insert_before(instr
, &add
->instr
);
280 indirect
->is_ssa
= true;
281 indirect
->ssa
= &add
->dest
.dest
.ssa
;
283 indirect
->is_ssa
= true;
284 indirect
->ssa
= &mul
->dest
.dest
.ssa
;
285 found_indirect
= true;
288 } else if (tail
->deref_type
== nir_deref_type_struct
) {
289 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
291 for (unsigned i
= 0; i
< deref_struct
->index
; i
++)
292 base_offset
+= type_size(glsl_get_struct_field(parent_type
, i
),
301 nir_lower_io_block(nir_block
*block
, void *void_state
)
303 struct lower_io_state
*state
= void_state
;
305 nir_foreach_instr_safe(block
, instr
) {
306 if (instr
->type
!= nir_instr_type_intrinsic
)
309 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
311 switch (intrin
->intrinsic
) {
312 case nir_intrinsic_load_var
: {
313 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
314 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
317 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
319 /* Figure out the opcode */
320 nir_intrinsic_op load_op
;
322 case nir_var_shader_in
:
323 load_op
= has_indirect
? nir_intrinsic_load_input_indirect
:
324 nir_intrinsic_load_input
;
326 case nir_var_uniform
:
327 load_op
= has_indirect
? nir_intrinsic_load_uniform_indirect
:
328 nir_intrinsic_load_uniform
;
331 unreachable("Unknown variable mode");
334 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(state
->mem_ctx
,
336 load
->num_components
= intrin
->num_components
;
339 unsigned offset
= get_io_offset(intrin
->variables
[0],
340 &intrin
->instr
, &indirect
, state
);
341 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
343 load
->const_index
[0] = offset
;
346 load
->src
[0] = indirect
;
348 if (intrin
->dest
.is_ssa
) {
349 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
350 intrin
->num_components
, NULL
);
351 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
352 nir_src_for_ssa(&load
->dest
.ssa
),
355 nir_dest_copy(&load
->dest
, &intrin
->dest
, state
->mem_ctx
);
358 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
359 nir_instr_remove(&intrin
->instr
);
363 case nir_intrinsic_store_var
: {
364 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
367 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
369 nir_intrinsic_op store_op
;
371 store_op
= nir_intrinsic_store_output_indirect
;
373 store_op
= nir_intrinsic_store_output
;
376 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
378 store
->num_components
= intrin
->num_components
;
381 unsigned offset
= get_io_offset(intrin
->variables
[0],
382 &intrin
->instr
, &indirect
, state
);
383 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
385 store
->const_index
[0] = offset
;
387 nir_src_copy(&store
->src
[0], &intrin
->src
[0], state
->mem_ctx
);
390 store
->src
[1] = indirect
;
392 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
393 nir_instr_remove(&intrin
->instr
);
406 nir_lower_io_impl(nir_function_impl
*impl
, bool is_scalar
)
408 struct lower_io_state state
;
410 state
.mem_ctx
= ralloc_parent(impl
);
411 state
.is_scalar
= is_scalar
;
413 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
415 nir_metadata_preserve(impl
, nir_metadata_block_index
|
416 nir_metadata_dominance
);
420 nir_lower_io(nir_shader
*shader
, bool is_scalar
)
422 nir_foreach_overload(shader
, overload
) {
424 nir_lower_io_impl(overload
->impl
, is_scalar
);