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 * Note that nir_intrinsic_load_uniform base/ranges can be set in different
30 * units, and the multiplier argument caters to supporting these different
31 * units.
32 *
33 * For example:
34 * - st_glsl_to_nir for PIPE_CAP_PACKED_UNIFORMS uses dwords (4 bytes) so the
35 * multiplier should be 4
36 * - st_glsl_to_nir for !PIPE_CAP_PACKED_UNIFORMS uses vec4s so the
37 * multiplier should be 16
38 * - tgsi_to_nir uses vec4s, so the multiplier should be 16
39 */
40
41 #include "nir.h"
42 #include "nir_builder.h"
43
44 static bool
lower_instr(nir_intrinsic_instr * instr,nir_builder * b,int multiplier)45 lower_instr(nir_intrinsic_instr *instr, nir_builder *b, int multiplier)
46 {
47 b->cursor = nir_before_instr(&instr->instr);
48
49 /* Increase all UBO binding points by 1. */
50 if (instr->intrinsic == nir_intrinsic_load_ubo &&
51 !b->shader->info.first_ubo_is_default_ubo) {
52 nir_ssa_def *old_idx = nir_ssa_for_src(b, instr->src[0], 1);
53 nir_ssa_def *new_idx = nir_iadd(b, old_idx, nir_imm_int(b, 1));
54 nir_instr_rewrite_src(&instr->instr, &instr->src[0],
55 nir_src_for_ssa(new_idx));
56 return true;
57 }
58
59 if (instr->intrinsic == nir_intrinsic_load_uniform) {
60 nir_ssa_def *ubo_idx = nir_imm_int(b, 0);
61 nir_ssa_def *ubo_offset =
62 nir_iadd(b, nir_imm_int(b, multiplier * nir_intrinsic_base(instr)),
63 nir_imul(b, nir_imm_int(b, multiplier),
64 nir_ssa_for_src(b, instr->src[0], 1)));
65
66 nir_intrinsic_instr *load =
67 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
68 load->num_components = instr->num_components;
69 load->src[0] = nir_src_for_ssa(ubo_idx);
70 load->src[1] = nir_src_for_ssa(ubo_offset);
71 assert(instr->dest.ssa.bit_size >= 8);
72
73 /* If it's const, set the alignment to our known constant offset. If
74 * not, set it to a pessimistic value based on the multiplier (or the
75 * scalar size, for qword loads).
76 *
77 * We could potentially set up stricter alignments for indirects by
78 * knowing what features are enabled in the APIs (see comment in
79 * nir_lower_ubo_vec4.c)
80 */
81 if (nir_src_is_const(instr->src[0])) {
82 nir_intrinsic_set_align(load, NIR_ALIGN_MUL_MAX,
83 (nir_src_as_uint(instr->src[0]) +
84 nir_intrinsic_base(instr) * multiplier) %
85 NIR_ALIGN_MUL_MAX);
86 } else {
87 nir_intrinsic_set_align(load, MAX2(multiplier,
88 instr->dest.ssa.bit_size / 8), 0);
89 }
90 nir_ssa_dest_init(&load->instr, &load->dest,
91 load->num_components, instr->dest.ssa.bit_size,
92 instr->dest.ssa.name);
93 nir_builder_instr_insert(b, &load->instr);
94 nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(&load->dest.ssa));
95
96 nir_intrinsic_set_range_base(load, nir_intrinsic_base(instr) * multiplier);
97 nir_intrinsic_set_range(load, nir_intrinsic_range(instr) * multiplier);
98
99 nir_instr_remove(&instr->instr);
100 return true;
101 }
102
103 return false;
104 }
105
106 bool
nir_lower_uniforms_to_ubo(nir_shader * shader,int multiplier)107 nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier)
108 {
109 bool progress = false;
110
111 nir_foreach_function(function, shader) {
112 if (function->impl) {
113 nir_builder builder;
114 nir_builder_init(&builder, function->impl);
115 nir_foreach_block(block, function->impl) {
116 nir_foreach_instr_safe(instr, block) {
117 if (instr->type == nir_instr_type_intrinsic)
118 progress |= lower_instr(nir_instr_as_intrinsic(instr),
119 &builder,
120 multiplier);
121 }
122 }
123
124 nir_metadata_preserve(function->impl, nir_metadata_block_index |
125 nir_metadata_dominance);
126 }
127 }
128
129 if (progress) {
130 if (!shader->info.first_ubo_is_default_ubo) {
131 nir_foreach_variable_with_modes(var, shader, nir_var_mem_ubo) {
132 var->data.binding++;
133 /* only increment location for ubo arrays */
134 if (glsl_without_array(var->type) == var->interface_type &&
135 glsl_type_is_array(var->type))
136 var->data.location++;
137 }
138 }
139 shader->info.num_ubos++;
140
141 if (shader->num_uniforms > 0) {
142 const struct glsl_type *type = glsl_array_type(glsl_vec4_type(),
143 shader->num_uniforms, 0);
144 nir_variable *ubo = nir_variable_create(shader, nir_var_mem_ubo, type,
145 "uniform_0");
146 ubo->data.binding = 0;
147
148 struct glsl_struct_field field = {
149 .type = type,
150 .name = "data",
151 .location = -1,
152 };
153 ubo->interface_type =
154 glsl_interface_type(&field, 1, GLSL_INTERFACE_PACKING_STD430,
155 false, "__ubo0_interface");
156 }
157 }
158
159 shader->info.first_ubo_is_default_ubo = true;
160 return progress;
161 }
162