2 * Copyright (c) 2019 Qiang Yu <yuq825@gmail.com>
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
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 NON-INFRINGEMENT. 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
21 * DEALINGS IN THE SOFTWARE.
26 #include "nir_builder.h"
30 lower_load_uniform_to_scalar(nir_builder
*b
, nir_intrinsic_instr
*intr
)
32 b
->cursor
= nir_before_instr(&intr
->instr
);
34 nir_ssa_def
*loads
[4];
35 for (unsigned i
= 0; i
< intr
->num_components
; i
++) {
36 nir_intrinsic_instr
*chan_intr
=
37 nir_intrinsic_instr_create(b
->shader
, intr
->intrinsic
);
38 nir_ssa_dest_init(&chan_intr
->instr
, &chan_intr
->dest
,
39 1, intr
->dest
.ssa
.bit_size
, NULL
);
40 chan_intr
->num_components
= 1;
42 nir_intrinsic_set_base(chan_intr
, nir_intrinsic_base(intr
) * 4 + i
);
43 nir_intrinsic_set_range(chan_intr
, nir_intrinsic_range(intr
) * 4);
46 nir_src_for_ssa(nir_fmul_imm(b
, intr
->src
[0].ssa
, 4));
48 nir_builder_instr_insert(b
, &chan_intr
->instr
);
50 loads
[i
] = &chan_intr
->dest
.ssa
;
53 nir_ssa_def_rewrite_uses(&intr
->dest
.ssa
,
54 nir_src_for_ssa(nir_vec(b
, loads
,
55 intr
->num_components
)));
56 nir_instr_remove(&intr
->instr
);
60 lima_nir_lower_uniform_to_scalar(nir_shader
*shader
)
62 nir_foreach_function(function
, shader
) {
65 nir_builder_init(&b
, function
->impl
);
67 nir_foreach_block(block
, function
->impl
) {
68 nir_foreach_instr_safe(instr
, block
) {
69 if (instr
->type
!= nir_instr_type_intrinsic
)
72 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
74 if (intr
->intrinsic
!= nir_intrinsic_load_uniform
)
77 lower_load_uniform_to_scalar(&b
, intr
);