• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 nir_load_ubo or nir_load_ubo_vec4 accesses
26  * of UBO binding point 0. Simultaneously, remap existing UBO accesses by
27  * increasing their binding point by 1.
28  *
29  * For PIPE_CAP_PACKED_UNIFORMS, dword_packed should be set to indicate that
30  * nir_intrinsic_load_uniform is in increments of dwords instead of vec4s.
31  *
32  * If load_vec4 is set, then nir_intrinsic_load_ubo_vec4 will be generated
33  * instead of nir_intrinsic_load_ubo, saving addressing math for hardawre
34  * needing aligned vec4 loads in increments of vec4s (such as TGSI CONST file
35  * loads).
36  */
37 
38 #include "nir.h"
39 #include "nir_builder.h"
40 
41 static bool
lower_instr(nir_intrinsic_instr * instr,nir_builder * b,bool dword_packed,bool load_vec4)42 lower_instr(nir_intrinsic_instr *instr, nir_builder *b, bool dword_packed, bool load_vec4)
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 *uniform_offset = nir_ssa_for_src(b, instr->src[0], 1);
59 
60       assert(instr->dest.ssa.bit_size >= 8);
61       nir_ssa_def *load_result;
62       if (load_vec4) {
63          /* No asking us to generate load_vec4 when you've packed your uniforms
64           * as dwords instead of vec4s.
65           */
66          assert(!dword_packed);
67          load_result = nir_load_ubo_vec4(b, instr->num_components, instr->dest.ssa.bit_size,
68                                          ubo_idx, uniform_offset, .base=nir_intrinsic_base(instr));
69       } else {
70          /* For PIPE_CAP_PACKED_UNIFORMS, the uniforms are packed with the
71           * base/offset in dword units instead of vec4 units.
72           */
73          int multiplier = dword_packed ? 4 : 16;
74          load_result = nir_load_ubo(b, instr->num_components, instr->dest.ssa.bit_size,
75                              ubo_idx,
76                              nir_iadd_imm(b, nir_imul_imm(b, uniform_offset, multiplier),
77                                           nir_intrinsic_base(instr) * multiplier));
78          nir_intrinsic_instr *load = nir_instr_as_intrinsic(load_result->parent_instr);
79 
80          /* If it's const, set the alignment to our known constant offset.  If
81           * not, set it to a pessimistic value based on the multiplier (or the
82           * scalar size, for qword loads).
83           *
84           * We could potentially set up stricter alignments for indirects by
85           * knowing what features are enabled in the APIs (see comment in
86           * nir_lower_ubo_vec4.c)
87           */
88          if (nir_src_is_const(instr->src[0])) {
89             nir_intrinsic_set_align(load, NIR_ALIGN_MUL_MAX,
90                                     (nir_src_as_uint(instr->src[0]) +
91                                     nir_intrinsic_base(instr) * multiplier) %
92                                     NIR_ALIGN_MUL_MAX);
93          } else {
94             nir_intrinsic_set_align(load, MAX2(multiplier,
95                                              instr->dest.ssa.bit_size / 8), 0);
96          }
97 
98          nir_intrinsic_set_range_base(load, nir_intrinsic_base(instr) * multiplier);
99          nir_intrinsic_set_range(load, nir_intrinsic_range(instr) * multiplier);
100       }
101       nir_ssa_def_rewrite_uses(&instr->dest.ssa, load_result);
102 
103       nir_instr_remove(&instr->instr);
104       return true;
105    }
106 
107    return false;
108 }
109 
110 bool
nir_lower_uniforms_to_ubo(nir_shader * shader,bool dword_packed,bool load_vec4)111 nir_lower_uniforms_to_ubo(nir_shader *shader, bool dword_packed, bool load_vec4)
112 {
113    bool progress = false;
114 
115    nir_foreach_function(function, shader) {
116       if (function->impl) {
117          nir_builder builder;
118          nir_builder_init(&builder, function->impl);
119          nir_foreach_block(block, function->impl) {
120             nir_foreach_instr_safe(instr, block) {
121                if (instr->type == nir_instr_type_intrinsic)
122                   progress |= lower_instr(nir_instr_as_intrinsic(instr),
123                                           &builder,
124                                           dword_packed, load_vec4);
125             }
126          }
127 
128          nir_metadata_preserve(function->impl, nir_metadata_block_index |
129                                                nir_metadata_dominance);
130       }
131    }
132 
133    if (progress) {
134       if (!shader->info.first_ubo_is_default_ubo) {
135          nir_foreach_variable_with_modes(var, shader, nir_var_mem_ubo) {
136             var->data.binding++;
137             if (var->data.driver_location != -1)
138                var->data.driver_location++;
139             /* only increment location for ubo arrays */
140             if (glsl_without_array(var->type) == var->interface_type &&
141                 glsl_type_is_array(var->type))
142                var->data.location++;
143          }
144       }
145       shader->info.num_ubos++;
146 
147       if (shader->num_uniforms > 0) {
148          const struct glsl_type *type = glsl_array_type(glsl_vec4_type(),
149                                                         shader->num_uniforms, 16);
150          nir_variable *ubo = nir_variable_create(shader, nir_var_mem_ubo, type,
151                                                  "uniform_0");
152          ubo->data.binding = 0;
153          ubo->data.explicit_binding = 1;
154 
155          struct glsl_struct_field field = {
156             .type = type,
157             .name = "data",
158             .location = -1,
159          };
160          ubo->interface_type =
161                glsl_interface_type(&field, 1, GLSL_INTERFACE_PACKING_STD430,
162                                    false, "__ubo0_interface");
163       }
164    }
165 
166    shader->info.first_ubo_is_default_ubo = true;
167    return progress;
168 }
169