2 * Copyright © 2015 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
26 struct lower_push_constants_state
{
32 lower_push_constants_block(nir_block
*block
, void *void_state
)
34 struct lower_push_constants_state
*state
= void_state
;
36 nir_foreach_instr(block
, instr
) {
37 if (instr
->type
!= nir_instr_type_intrinsic
)
40 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
42 /* TODO: Handle indirect push constants */
43 if (intrin
->intrinsic
!= nir_intrinsic_load_push_constant
)
46 assert(intrin
->const_index
[0] % 4 == 0);
47 unsigned dword_offset
= intrin
->const_index
[0] / 4;
49 /* We just turn them into uniform loads with the appropreate offset */
50 intrin
->intrinsic
= nir_intrinsic_load_uniform
;
51 intrin
->const_index
[0] = 0;
52 if (state
->is_scalar
) {
53 intrin
->const_index
[1] = dword_offset
;
55 unsigned shift
= dword_offset
% 4;
56 /* Can't cross the vec4 boundary */
57 assert(shift
+ intrin
->num_components
<= 4);
59 /* vec4 shifts are in units of vec4's */
60 intrin
->const_index
[1] = dword_offset
/ 4;
63 /* If there's a non-zero shift then we need to load a whole vec4
64 * and use a move to swizzle it into place.
66 assert(intrin
->dest
.is_ssa
);
67 nir_alu_instr
*mov
= nir_alu_instr_create(state
->shader
,
69 mov
->src
[0].src
= nir_src_for_ssa(&intrin
->dest
.ssa
);
70 for (unsigned i
= 0; i
< intrin
->num_components
; i
++)
71 mov
->src
[0].swizzle
[i
] = i
+ shift
;
72 mov
->dest
.write_mask
= (1 << intrin
->num_components
) - 1;
73 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
,
74 intrin
->num_components
, NULL
);
76 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
77 nir_src_for_ssa(&mov
->dest
.dest
.ssa
));
78 nir_instr_insert_after(&intrin
->instr
, &mov
->instr
);
80 /* Stomp the number of components to 4 */
81 intrin
->num_components
= 4;
82 intrin
->dest
.ssa
.num_components
= 4;
91 anv_nir_lower_push_constants(nir_shader
*shader
, bool is_scalar
)
93 struct lower_push_constants_state state
= {
95 .is_scalar
= is_scalar
,
98 nir_foreach_overload(shader
, overload
) {
100 nir_foreach_block(overload
->impl
, lower_push_constants_block
, &state
);
103 assert(shader
->num_uniforms
% 4 == 0);
105 shader
->num_uniforms
/= 4;
107 shader
->num_uniforms
= DIV_ROUND_UP(shader
->num_uniforms
, 16);