Rename nir_lower_constant_initializers to nir_lower_variable_initalizers
[mesa.git] / src / compiler / nir / nir_lower_uniforms_to_ubo.c
1 /*
2 * Copyright 2017 Advanced Micro Devices, Inc.
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 * 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:
10 *
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
13 * 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 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.
22 */
23
24 /*
25 * Remap load_uniform intrinsics to UBO accesses of UBO binding point 0.
26 * Simultaneously, remap existing UBO accesses by increasing their binding
27 * point by 1.
28 *
29 * Both the base and the offset are interpreted as 16-byte units.
30 *
31 * Note that locations can be set in different units, and the multiplier
32 * argument caters to supporting these different units.
33 * For example:
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
36 */
37
38 #include "nir.h"
39 #include "nir_builder.h"
40
41 static bool
42 lower_instr(nir_intrinsic_instr *instr, nir_builder *b, int multiplier)
43 {
44 b->cursor = nir_before_instr(&instr->instr);
45
46 /* Increase all UBO binding points by 1. */
47 if (instr->intrinsic == nir_intrinsic_load_ubo &&
48 !b->shader->info.first_ubo_is_default_ubo) {
49 nir_ssa_def *old_idx = nir_ssa_for_src(b, instr->src[0], 1);
50 nir_ssa_def *new_idx = nir_iadd(b, old_idx, nir_imm_int(b, 1));
51 nir_instr_rewrite_src(&instr->instr, &instr->src[0],
52 nir_src_for_ssa(new_idx));
53 return true;
54 }
55
56 if (instr->intrinsic == nir_intrinsic_load_uniform) {
57 nir_ssa_def *ubo_idx = nir_imm_int(b, 0);
58 nir_ssa_def *ubo_offset =
59 nir_iadd(b, nir_imm_int(b, multiplier * nir_intrinsic_base(instr)),
60 nir_imul(b, nir_imm_int(b, multiplier),
61 nir_ssa_for_src(b, instr->src[0], 1)));
62
63 nir_intrinsic_instr *load =
64 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
65 load->num_components = instr->num_components;
66 load->src[0] = nir_src_for_ssa(ubo_idx);
67 load->src[1] = nir_src_for_ssa(ubo_offset);
68 nir_ssa_dest_init(&load->instr, &load->dest,
69 load->num_components, instr->dest.ssa.bit_size,
70 instr->dest.ssa.name);
71 nir_builder_instr_insert(b, &load->instr);
72 nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(&load->dest.ssa));
73
74 nir_instr_remove(&instr->instr);
75 return true;
76 }
77
78 return false;
79 }
80
81 bool
82 nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier)
83 {
84 bool progress = false;
85
86 nir_foreach_function(function, shader) {
87 if (function->impl) {
88 nir_builder builder;
89 nir_builder_init(&builder, function->impl);
90 nir_foreach_block(block, function->impl) {
91 nir_foreach_instr_safe(instr, block) {
92 if (instr->type == nir_instr_type_intrinsic)
93 progress |= lower_instr(nir_instr_as_intrinsic(instr),
94 &builder,
95 multiplier);
96 }
97 }
98
99 nir_metadata_preserve(function->impl, nir_metadata_block_index |
100 nir_metadata_dominance);
101 }
102 }
103
104 shader->info.first_ubo_is_default_ubo = true;
105 return progress;
106 }
107
108