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
:
112 case GLSL_TYPE_ERROR
:
113 case GLSL_TYPE_DOUBLE
:
114 unreachable("not reached");
121 type_size(const struct glsl_type
*type
, bool is_scalar
)
124 return type_size_scalar(type
);
126 return type_size_vec4(type
);
130 nir_assign_var_locations(struct exec_list
*var_list
, unsigned *size
, bool is_scalar
)
132 unsigned location
= 0;
134 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
136 * UBO's have their own address spaces, so don't count them towards the
137 * number of global uniforms
139 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
140 var
->interface_type
!= NULL
)
143 var
->data
.driver_location
= location
;
144 location
+= type_size(var
->type
, is_scalar
);
151 deref_has_indirect(nir_deref_var
*deref
)
153 for (nir_deref
*tail
= deref
->deref
.child
; tail
; tail
= tail
->child
) {
154 if (tail
->deref_type
== nir_deref_type_array
) {
155 nir_deref_array
*arr
= nir_deref_as_array(tail
);
156 if (arr
->deref_array_type
== nir_deref_array_type_indirect
)
165 mark_indirect_uses_block(nir_block
*block
, void *void_state
)
167 struct set
*indirect_set
= void_state
;
169 nir_foreach_instr(block
, instr
) {
170 if (instr
->type
!= nir_instr_type_intrinsic
)
173 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
176 i
< nir_intrinsic_infos
[intrin
->intrinsic
].num_variables
; i
++) {
177 if (deref_has_indirect(intrin
->variables
[i
]))
178 _mesa_set_add(indirect_set
, intrin
->variables
[i
]->var
);
185 /* Identical to nir_assign_var_locations_packed except that it assigns
186 * locations to the variables that are used 100% directly first and then
187 * assigns locations to variables that are used indirectly.
190 nir_assign_var_locations_direct_first(nir_shader
*shader
,
191 struct exec_list
*var_list
,
192 unsigned *direct_size
,
196 struct set
*indirect_set
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
197 _mesa_key_pointer_equal
);
199 nir_foreach_overload(shader
, overload
) {
201 nir_foreach_block(overload
->impl
, mark_indirect_uses_block
,
205 unsigned location
= 0;
207 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
208 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
209 var
->interface_type
!= NULL
)
212 if (_mesa_set_search(indirect_set
, var
))
215 var
->data
.driver_location
= location
;
216 location
+= type_size(var
->type
, is_scalar
);
219 *direct_size
= location
;
221 foreach_list_typed(nir_variable
, var
, node
, var_list
) {
222 if ((var
->data
.mode
== nir_var_uniform
|| var
->data
.mode
== nir_var_shader_storage
) &&
223 var
->interface_type
!= NULL
)
226 if (!_mesa_set_search(indirect_set
, var
))
229 var
->data
.driver_location
= location
;
230 location
+= type_size(var
->type
, is_scalar
);
235 _mesa_set_destroy(indirect_set
, NULL
);
239 get_io_offset(nir_deref_var
*deref
, nir_instr
*instr
, nir_src
*indirect
,
240 struct lower_io_state
*state
)
242 bool found_indirect
= false;
243 unsigned base_offset
= 0;
245 nir_deref
*tail
= &deref
->deref
;
246 while (tail
->child
!= NULL
) {
247 const struct glsl_type
*parent_type
= tail
->type
;
250 if (tail
->deref_type
== nir_deref_type_array
) {
251 nir_deref_array
*deref_array
= nir_deref_as_array(tail
);
252 unsigned size
= type_size(tail
->type
, state
->is_scalar
);
254 base_offset
+= size
* deref_array
->base_offset
;
256 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
257 nir_load_const_instr
*load_const
=
258 nir_load_const_instr_create(state
->mem_ctx
, 1);
259 load_const
->value
.u
[0] = size
;
260 nir_instr_insert_before(instr
, &load_const
->instr
);
262 nir_alu_instr
*mul
= nir_alu_instr_create(state
->mem_ctx
,
264 mul
->src
[0].src
.is_ssa
= true;
265 mul
->src
[0].src
.ssa
= &load_const
->def
;
266 nir_src_copy(&mul
->src
[1].src
, &deref_array
->indirect
,
268 mul
->dest
.write_mask
= 1;
269 nir_ssa_dest_init(&mul
->instr
, &mul
->dest
.dest
, 1, NULL
);
270 nir_instr_insert_before(instr
, &mul
->instr
);
272 if (found_indirect
) {
273 nir_alu_instr
*add
= nir_alu_instr_create(state
->mem_ctx
,
275 add
->src
[0].src
= *indirect
;
276 add
->src
[1].src
.is_ssa
= true;
277 add
->src
[1].src
.ssa
= &mul
->dest
.dest
.ssa
;
278 add
->dest
.write_mask
= 1;
279 nir_ssa_dest_init(&add
->instr
, &add
->dest
.dest
, 1, NULL
);
280 nir_instr_insert_before(instr
, &add
->instr
);
282 indirect
->is_ssa
= true;
283 indirect
->ssa
= &add
->dest
.dest
.ssa
;
285 indirect
->is_ssa
= true;
286 indirect
->ssa
= &mul
->dest
.dest
.ssa
;
287 found_indirect
= true;
290 } else if (tail
->deref_type
== nir_deref_type_struct
) {
291 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
293 for (unsigned i
= 0; i
< deref_struct
->index
; i
++)
294 base_offset
+= type_size(glsl_get_struct_field(parent_type
, i
),
303 nir_lower_io_block(nir_block
*block
, void *void_state
)
305 struct lower_io_state
*state
= void_state
;
307 nir_foreach_instr_safe(block
, instr
) {
308 if (instr
->type
!= nir_instr_type_intrinsic
)
311 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
313 switch (intrin
->intrinsic
) {
314 case nir_intrinsic_load_var
: {
315 nir_variable_mode mode
= intrin
->variables
[0]->var
->data
.mode
;
316 if (mode
!= nir_var_shader_in
&& mode
!= nir_var_uniform
)
319 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
321 /* Figure out the opcode */
322 nir_intrinsic_op load_op
;
324 case nir_var_shader_in
:
325 load_op
= has_indirect
? nir_intrinsic_load_input_indirect
:
326 nir_intrinsic_load_input
;
328 case nir_var_uniform
:
329 load_op
= has_indirect
? nir_intrinsic_load_uniform_indirect
:
330 nir_intrinsic_load_uniform
;
333 unreachable("Unknown variable mode");
336 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(state
->mem_ctx
,
338 load
->num_components
= intrin
->num_components
;
341 unsigned offset
= get_io_offset(intrin
->variables
[0],
342 &intrin
->instr
, &indirect
, state
);
343 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
345 load
->const_index
[0] = offset
;
348 load
->src
[0] = indirect
;
350 if (intrin
->dest
.is_ssa
) {
351 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
352 intrin
->num_components
, NULL
);
353 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
354 nir_src_for_ssa(&load
->dest
.ssa
),
357 nir_dest_copy(&load
->dest
, &intrin
->dest
, state
->mem_ctx
);
360 nir_instr_insert_before(&intrin
->instr
, &load
->instr
);
361 nir_instr_remove(&intrin
->instr
);
365 case nir_intrinsic_store_var
: {
366 if (intrin
->variables
[0]->var
->data
.mode
!= nir_var_shader_out
)
369 bool has_indirect
= deref_has_indirect(intrin
->variables
[0]);
371 nir_intrinsic_op store_op
;
373 store_op
= nir_intrinsic_store_output_indirect
;
375 store_op
= nir_intrinsic_store_output
;
378 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(state
->mem_ctx
,
380 store
->num_components
= intrin
->num_components
;
383 unsigned offset
= get_io_offset(intrin
->variables
[0],
384 &intrin
->instr
, &indirect
, state
);
385 offset
+= intrin
->variables
[0]->var
->data
.driver_location
;
387 store
->const_index
[0] = offset
;
389 nir_src_copy(&store
->src
[0], &intrin
->src
[0], state
->mem_ctx
);
392 store
->src
[1] = indirect
;
394 nir_instr_insert_before(&intrin
->instr
, &store
->instr
);
395 nir_instr_remove(&intrin
->instr
);
408 nir_lower_io_impl(nir_function_impl
*impl
, bool is_scalar
)
410 struct lower_io_state state
;
412 state
.mem_ctx
= ralloc_parent(impl
);
413 state
.is_scalar
= is_scalar
;
415 nir_foreach_block(impl
, nir_lower_io_block
, &state
);
417 nir_metadata_preserve(impl
, nir_metadata_block_index
|
418 nir_metadata_dominance
);
422 nir_lower_io(nir_shader
*shader
, bool is_scalar
)
424 nir_foreach_overload(shader
, overload
) {
426 nir_lower_io_impl(overload
->impl
, is_scalar
);