2 * Copyright 2017 Advanced Micro Devices, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Remap load_uniform intrinsics to UBO accesses of UBO binding point 0.
26 * Simultaneously, remap existing UBO accesses by increasing their binding
29 * Both the base and the offset are interpreted as 16-byte units.
31 * Note that locations can be set in different units, and the multiplier
32 * argument caters to supporting these different units.
34 * - st_glsl_to_nir uses dwords (4 bytes) so the multiplier should be 4
35 * - tgsi_to_nir uses bytes, so the multiplier should be 16
39 #include "nir_builder.h"
42 lower_instr(nir_intrinsic_instr
*instr
, nir_builder
*b
, int multiplier
)
44 b
->cursor
= nir_before_instr(&instr
->instr
);
46 if (instr
->intrinsic
== nir_intrinsic_load_ubo
) {
47 nir_ssa_def
*old_idx
= nir_ssa_for_src(b
, instr
->src
[0], 1);
48 nir_ssa_def
*new_idx
= nir_iadd(b
, old_idx
, nir_imm_int(b
, 1));
49 nir_instr_rewrite_src(&instr
->instr
, &instr
->src
[0],
50 nir_src_for_ssa(new_idx
));
54 if (instr
->intrinsic
== nir_intrinsic_load_uniform
) {
55 nir_ssa_def
*ubo_idx
= nir_imm_int(b
, 0);
56 nir_ssa_def
*ubo_offset
=
57 nir_iadd(b
, nir_imm_int(b
, multiplier
* nir_intrinsic_base(instr
)),
58 nir_imul(b
, nir_imm_int(b
, multiplier
),
59 nir_ssa_for_src(b
, instr
->src
[0], 1)));
61 nir_intrinsic_instr
*load
=
62 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_ubo
);
63 load
->num_components
= instr
->num_components
;
64 load
->src
[0] = nir_src_for_ssa(ubo_idx
);
65 load
->src
[1] = nir_src_for_ssa(ubo_offset
);
66 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
67 load
->num_components
, instr
->dest
.ssa
.bit_size
,
68 instr
->dest
.ssa
.name
);
69 nir_builder_instr_insert(b
, &load
->instr
);
70 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
, nir_src_for_ssa(&load
->dest
.ssa
));
72 nir_instr_remove(&instr
->instr
);
80 nir_lower_uniforms_to_ubo(nir_shader
*shader
, int multiplier
)
82 bool progress
= false;
84 nir_foreach_function(function
, shader
) {
87 nir_builder_init(&builder
, function
->impl
);
88 nir_foreach_block(block
, function
->impl
) {
89 nir_foreach_instr_safe(instr
, block
) {
90 if (instr
->type
== nir_instr_type_intrinsic
)
91 progress
|= lower_instr(nir_instr_as_intrinsic(instr
),
97 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
98 nir_metadata_dominance
);