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. Both
26 * the base and the offset are interpreted as 16-byte units.
28 * Simultaneously, remap existing UBO accesses by increasing their binding
32 #include "compiler/nir/nir.h"
33 #include "compiler/nir/nir_builder.h"
36 #include "program/prog_parameter.h"
39 lower_instr(nir_intrinsic_instr
*instr
, nir_builder
*b
)
41 b
->cursor
= nir_before_instr(&instr
->instr
);
43 if (instr
->intrinsic
== nir_intrinsic_load_ubo
) {
44 nir_ssa_def
*old_idx
= nir_ssa_for_src(b
, instr
->src
[0], 1);
45 nir_ssa_def
*new_idx
= nir_iadd(b
, old_idx
, nir_imm_int(b
, 1));
46 nir_instr_rewrite_src(&instr
->instr
, &instr
->src
[0],
47 nir_src_for_ssa(new_idx
));
51 if (instr
->intrinsic
== nir_intrinsic_load_uniform
) {
52 nir_ssa_def
*ubo_idx
= nir_imm_int(b
, 0);
53 nir_ssa_def
*ubo_offset
=
54 nir_iadd(b
, nir_imm_int(b
, 4 * nir_intrinsic_base(instr
)),
55 nir_imul(b
, nir_imm_int(b
, 4),
56 nir_ssa_for_src(b
, instr
->src
[0], 1)));
58 nir_intrinsic_instr
*load
=
59 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_ubo
);
60 load
->num_components
= instr
->num_components
;
61 load
->src
[0] = nir_src_for_ssa(ubo_idx
);
62 load
->src
[1] = nir_src_for_ssa(ubo_offset
);
63 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
64 load
->num_components
, instr
->dest
.ssa
.bit_size
,
65 instr
->dest
.ssa
.name
);
66 nir_builder_instr_insert(b
, &load
->instr
);
67 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
, nir_src_for_ssa(&load
->dest
.ssa
));
69 nir_instr_remove(&instr
->instr
);
77 st_nir_lower_uniforms_to_ubo(nir_shader
*shader
)
79 bool progress
= false;
81 nir_foreach_function(function
, shader
) {
84 nir_builder_init(&builder
, function
->impl
);
85 nir_foreach_block(block
, function
->impl
) {
86 nir_foreach_instr_safe(instr
, block
) {
87 if (instr
->type
== nir_instr_type_intrinsic
)
88 progress
|= lower_instr(nir_instr_as_intrinsic(instr
),
93 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
94 nir_metadata_dominance
);