1 /*
2 * Copyright (c) 2019 Qiang Yu <yuq825@gmail.com>
3 *
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:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
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.
22 *
23 */
24
25 #include "nir.h"
26 #include "nir_builder.h"
27 #include "lima_ir.h"
28
29 static void
lower_load_uniform_to_scalar(nir_builder * b,nir_intrinsic_instr * intr)30 lower_load_uniform_to_scalar(nir_builder *b, nir_intrinsic_instr *intr)
31 {
32 b->cursor = nir_before_instr(&intr->instr);
33
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;
41
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);
44 nir_intrinsic_set_dest_type(chan_intr, nir_intrinsic_dest_type(intr));
45
46 chan_intr->src[0] =
47 nir_src_for_ssa(nir_imul_imm(b, intr->src[0].ssa, 4));
48
49 nir_builder_instr_insert(b, &chan_intr->instr);
50
51 loads[i] = &chan_intr->dest.ssa;
52 }
53
54 nir_ssa_def_rewrite_uses(&intr->dest.ssa,
55 nir_vec(b, loads, intr->num_components));
56 nir_instr_remove(&intr->instr);
57 }
58
59 void
lima_nir_lower_uniform_to_scalar(nir_shader * shader)60 lima_nir_lower_uniform_to_scalar(nir_shader *shader)
61 {
62 nir_foreach_function(function, shader) {
63 if (function->impl) {
64 nir_builder b;
65 nir_builder_init(&b, function->impl);
66
67 nir_foreach_block(block, function->impl) {
68 nir_foreach_instr_safe(instr, block) {
69 if (instr->type != nir_instr_type_intrinsic)
70 continue;
71
72 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
73
74 if (intr->intrinsic != nir_intrinsic_load_uniform)
75 continue;
76
77 lower_load_uniform_to_scalar(&b, intr);
78 }
79 }
80 }
81 }
82 }
83