nir/lower_uniforms_to_ubo: Use nir_foreach_variable_with_modes
[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 assert(instr->dest.ssa.bit_size >= 8);
69 nir_intrinsic_set_align(load, instr->dest.ssa.bit_size / 8, 0);
70 nir_ssa_dest_init(&load->instr, &load->dest,
71 load->num_components, instr->dest.ssa.bit_size,
72 instr->dest.ssa.name);
73 nir_builder_instr_insert(b, &load->instr);
74 nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(&load->dest.ssa));
75
76 nir_instr_remove(&instr->instr);
77 return true;
78 }
79
80 return false;
81 }
82
83 bool
84 nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier)
85 {
86 bool progress = false;
87
88 nir_foreach_function(function, shader) {
89 if (function->impl) {
90 nir_builder builder;
91 nir_builder_init(&builder, function->impl);
92 nir_foreach_block(block, function->impl) {
93 nir_foreach_instr_safe(instr, block) {
94 if (instr->type == nir_instr_type_intrinsic)
95 progress |= lower_instr(nir_instr_as_intrinsic(instr),
96 &builder,
97 multiplier);
98 }
99 }
100
101 nir_metadata_preserve(function->impl, nir_metadata_block_index |
102 nir_metadata_dominance);
103 }
104 }
105
106 if (progress) {
107 if (!shader->info.first_ubo_is_default_ubo) {
108 nir_foreach_variable_with_modes(var, shader, nir_var_mem_ubo)
109 var->data.binding++;
110 }
111 shader->info.num_ubos++;
112
113 if (shader->num_uniforms > 0) {
114 const struct glsl_type *type = glsl_array_type(glsl_vec4_type(),
115 shader->num_uniforms, 0);
116 nir_variable *ubo = nir_variable_create(shader, nir_var_mem_ubo, type,
117 "uniform_0");
118 ubo->data.binding = 0;
119
120 struct glsl_struct_field field = {
121 .type = type,
122 .name = "data",
123 .location = -1,
124 };
125 ubo->interface_type =
126 glsl_interface_type(&field, 1, GLSL_INTERFACE_PACKING_STD430,
127 false, "__ubo0_interface");
128 }
129 }
130
131 shader->info.first_ubo_is_default_ubo = true;
132 return progress;
133 }