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_SUBROUTINE
:
67 case GLSL_TYPE_SAMPLER
:
69 case GLSL_TYPE_ATOMIC_UINT
:
73 case GLSL_TYPE_DOUBLE
:
75 case GLSL_TYPE_INTERFACE
:
76 unreachable("not reached");
83 type_size_scalar(const struct glsl_type
*type
)
87 switch (glsl_get_base_type(type
)) {
92 return glsl_get_components(type
);
94 return type_size_scalar(glsl_get_array_element(type
)) * glsl_get_length(type
);
95 case GLSL_TYPE_STRUCT
:
97 for (i
= 0; i
< glsl_get_length(type
); i
++) {
98 size
+= type_size_scalar(glsl_get_struct_field(type
, i
));
101 case GLSL_TYPE_SUBROUTINE
:
103 case GLSL_TYPE_SAMPLER
:
105 case GLSL_TYPE_ATOMIC_UINT
:
107 case GLSL_TYPE_INTERFACE
:
109 case GLSL_TYPE_IMAGE
:
111 case GLSL_TYPE_FUNCTION
:
113 case GLSL_TYPE_ERROR
:
114 case GLSL_TYPE_DOUBLE
:
115 unreachable("not reached");
122 type_size(const struct glsl_type
*type
, bool is_scalar
)
125 return type_size_scalar(type
);
127 return type_size_vec4(type
);
131 nir_assign_var_locations(struct exec_list
*var_list
, unsigned *size
, bool is_scalar
)
133 unsigned location
= 0;
135 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
137 * UBO's have their own address spaces, so don't count them towards the
138 * number of global uniforms
140 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
141 var
->interface_type
!= NULL
)
144 var
->data
.driver_location
= location
;
145 location
+= type_size(var
->type
, is_scalar
);
152 deref_has_indirect(nir_deref_var
*deref
)
154 for (nir_deref
*tail
= deref
->deref
.child
; tail
; tail
= tail
->child
) {
155 if (tail
->deref_type
== nir_deref_type_array
) {
156 nir_deref_array
*arr
= nir_deref_as_array(tail
);
157 if (arr
->deref_array_type
== nir_deref_array_type_indirect
)
166 mark_indirect_uses_block(nir_block
*block
, void *void_state
)
168 struct set
*indirect_set
= void_state
;
170 nir_foreach_instr(block
, instr
) {
171 if (instr
->type
!= nir_instr_type_intrinsic
)
174 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
177 i
< nir_intrinsic_infos
[intrin
->intrinsic
].num_variables
; i
++) {
178 if (deref_has_indirect(intrin
->variables
[i
]))
179 _mesa_set_add(indirect_set
, intrin
->variables
[i
]->var
);
186 /* Identical to nir_assign_var_locations_packed except that it assigns
187 * locations to the variables that are used 100% directly first and then
188 * assigns locations to variables that are used indirectly.
191 nir_assign_var_locations_direct_first(nir_shader
*shader
,
192 struct exec_list
*var_list
,
193 unsigned *direct_size
,
197 struct set
*indirect_set
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
198 _mesa_key_pointer_equal
);
200 nir_foreach_overload(shader
, overload
) {
202 nir_foreach_block(overload
->impl
, mark_indirect_uses_block
,
206 unsigned location
= 0;
208 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
209 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
210 var
->interface_type
!= NULL
)
213 if (_mesa_set_search(indirect_set
, var
))
216 var
->data
.driver_location
= location
;
217 location
+= type_size(var
->type
, is_scalar
);
220 *direct_size
= location
;
222 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
223 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
224 var
->interface_type
!= NULL
)
227 if (!_mesa_set_search(indirect_set
, var
))
230 var
->data
.driver_location
= location
;
231 location
+= type_size(var
->type
, is_scalar
);
236 _mesa_set_destroy(indirect_set
, NULL
);
240 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
, nir_src
*indirect
,
241 struct lower_io_state
*state
)
243 bool found_indirect
= false;
244 unsigned base_offset
= 0;
246 nir_deref
*tail
= &deref
->deref
;
247 while (tail
->child
!= NULL
) {
248 const struct glsl_type
*parent_type
= tail
->type
;
251 if (tail
->deref_type
== nir_deref_type_array
) {
252 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
253 unsigned size
= type_size(tail
->type
, state
->is_scalar
);
255 base_offset
+= size
* deref_array
->base_offset
;
257 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
258 nir_load_const_instr
*load_const
=
259 nir_load_const_instr_create(state
->mem_ctx
, 1);
260 load_const
->value
.u
[0] = size
;
261 nir_instr_insert_before(instr
, &load_const
->instr
);
263 nir_alu_instr
*mul
= nir_alu_instr_create(state
->mem_ctx
,
265 mul
->src
[0].src
.is_ssa
= true;
266 mul
->src
[0].src
.ssa
= &load_const
->def
;
267 nir_src_copy(&mul
->src
[1].src
, &deref_array
->indirect
,
269 mul
->dest
.write_mask
= 1;
270 nir_ssa_dest_init(&mul
->instr
, &mul
->dest
.dest
, 1, NULL
);
271 nir_instr_insert_before(instr
, &mul
->instr
);
273 if (found_indirect
) {
274 nir_alu_instr
*add
= nir_alu_instr_create(state
->mem_ctx
,
276 add
->src
[0].src
= *indirect
;
277 add
->src
[1].src
.is_ssa
= true;
278 add
->src
[1].src
.ssa
= &mul
->dest
.dest
.ssa
;
279 add
->dest
.write_mask
= 1;
280 nir_ssa_dest_init(&add
->instr
, &add
->dest
.dest
, 1, NULL
);
281 nir_instr_insert_before(instr
, &add
->instr
);
283 indirect
->is_ssa
= true;
284 indirect
->ssa
= &add
->dest
.dest
.ssa
;
286 indirect
->is_ssa
= true;
287 indirect
->ssa
= &mul
->dest
.dest
.ssa
;
288 found_indirect
= true;
291 } else if (tail
->deref_type
== nir_deref_type_struct
) {
292 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
294 for (unsigned i
= 0; i
< deref_struct
->index
; i
++)
295 base_offset
+= type_size(glsl_get_struct_field(parent_type
, i
),
304 nir_lower_io_block(nir_block
*block
, void *void_state
)
306 struct lower_io_state
*state
= void_state
;
308 nir_foreach_instr_safe(block
, instr
) {
309 if (instr
->type
!= nir_instr_type_intrinsic
)
312 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
314 switch (intrin
->intrinsic
) {
315 case nir_intrinsic_load_var
: {
316 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
317 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
320 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
322 /* Figure out the opcode */
323 nir_intrinsic_op load_op
;
325 case nir_var_shader_in
:
326 load_op
= has_indirect
? nir_intrinsic_load_input_indirect
:
327 nir_intrinsic_load_input
;
329 case nir_var_uniform
:
330 load_op
= has_indirect
? nir_intrinsic_load_uniform_indirect
:
331 nir_intrinsic_load_uniform
;
334 unreachable("Unknown variable mode");
337 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(state
->mem_ctx
,
339 load
->num_components
= intrin
->num_components
;
342 unsigned offset
= get_io_offset(intrin
->variables
[0],
343 &intrin
->instr
, &indirect
, state
);
344 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
346 load
->const_index
[0] = offset
;
349 load
->src
[0] = indirect
;
351 if (intrin
->dest
.is_ssa
) {
352 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
353 intrin
->num_components
, NULL
);
354 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
355 nir_src_for_ssa(&load
->dest
.ssa
),
358 nir_dest_copy(&load
->dest
, &intrin
->dest
, state
->mem_ctx
);
361 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
362 nir_instr_remove(&intrin
->instr
);
366 case nir_intrinsic_store_var
: {
367 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
370 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
372 nir_intrinsic_op store_op
;
374 store_op
= nir_intrinsic_store_output_indirect
;
376 store_op
= nir_intrinsic_store_output
;
379 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
381 store
->num_components
= intrin
->num_components
;
384 unsigned offset
= get_io_offset(intrin
->variables
[0],
385 &intrin
->instr
, &indirect
, state
);
386 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
388 store
->const_index
[0] = offset
;
390 nir_src_copy(&store
->src
[0], &intrin
->src
[0], state
->mem_ctx
);
393 store
->src
[1] = indirect
;
395 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
396 nir_instr_remove(&intrin
->instr
);
409 nir_lower_io_impl(nir_function_impl
*impl
, bool is_scalar
)
411 struct lower_io_state state
;
413 state
.mem_ctx
= ralloc_parent(impl
);
414 state
.is_scalar
= is_scalar
;
416 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
418 nir_metadata_preserve(impl
, nir_metadata_block_index
|
419 nir_metadata_dominance
);
423 nir_lower_io(nir_shader
*shader
, bool is_scalar
)
425 nir_foreach_overload(shader
, overload
) {
427 nir_lower_io_impl(overload
->impl
, is_scalar
);