• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2020 Mike Blumenkrantz
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  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
25  */
26 
27 #include "nir.h"
28 #include "nir_builder.h"
29 
30 bool nir_lower_dynamic_bo_access(nir_shader *shader);
31 /**
32  * This pass converts dynamic UBO/SSBO block indices to constant indices by generating
33  * conditional chains which reduce to single values.
34  *
35  * This is needed by anything which intends to convert GLSL-like shaders to SPIRV,
36  * as SPIRV requires explicit load points for UBO/SSBO variables and has no instruction for
37  * loading based on an offset in the underlying driver's binding table
38  */
39 
40 
41 /* generate a single ssa value which conditionally selects the right value that
42  * was previously loaded by the load_ubo conditional chain
43  */
44 static nir_ssa_def *
recursive_generate_bo_ssa_def(nir_builder * b,nir_intrinsic_instr * instr,nir_ssa_def * index,unsigned start,unsigned end)45 recursive_generate_bo_ssa_def(nir_builder *b, nir_intrinsic_instr *instr, nir_ssa_def *index, unsigned start, unsigned end)
46 {
47    if (start == end - 1) {
48       /* block index src is 1 for this op */
49       unsigned block_idx = instr->intrinsic == nir_intrinsic_store_ssbo;
50       nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(b->shader, instr->intrinsic);
51       new_instr->src[block_idx] = nir_src_for_ssa(nir_imm_int(b, start));
52       for (unsigned i = 0; i < nir_intrinsic_infos[instr->intrinsic].num_srcs; i++) {
53          if (i != block_idx)
54             nir_src_copy(&new_instr->src[i], &instr->src[i], &new_instr->instr);
55       }
56       if (instr->intrinsic != nir_intrinsic_load_ubo_vec4) {
57          nir_intrinsic_set_align(new_instr, nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));
58          if (instr->intrinsic != nir_intrinsic_load_ssbo)
59             nir_intrinsic_set_range(new_instr, nir_intrinsic_range(instr));
60       }
61       new_instr->num_components = instr->num_components;
62       if (instr->intrinsic != nir_intrinsic_store_ssbo)
63          nir_ssa_dest_init(&new_instr->instr, &new_instr->dest,
64                            nir_dest_num_components(instr->dest),
65                            nir_dest_bit_size(instr->dest), NULL);
66       nir_builder_instr_insert(b, &new_instr->instr);
67       return &new_instr->dest.ssa;
68    }
69 
70    unsigned mid = start + (end - start) / 2;
71    return nir_build_alu(b, nir_op_bcsel, nir_build_alu(b, nir_op_ilt, index, nir_imm_int(b, mid), NULL, NULL),
72       recursive_generate_bo_ssa_def(b, instr, index, start, mid),
73       recursive_generate_bo_ssa_def(b, instr, index, mid, end),
74       NULL
75    );
76 }
77 
78 static bool
lower_dynamic_bo_access_instr(nir_intrinsic_instr * instr,nir_builder * b)79 lower_dynamic_bo_access_instr(nir_intrinsic_instr *instr, nir_builder *b)
80 {
81    if (instr->intrinsic != nir_intrinsic_load_ubo &&
82        instr->intrinsic != nir_intrinsic_load_ubo_vec4 &&
83        instr->intrinsic != nir_intrinsic_get_ssbo_size &&
84        instr->intrinsic != nir_intrinsic_load_ssbo &&
85        instr->intrinsic != nir_intrinsic_store_ssbo)
86       return false;
87    /* block index src is 1 for this op */
88    unsigned block_idx = instr->intrinsic == nir_intrinsic_store_ssbo;
89    if (nir_src_is_const(instr->src[block_idx]))
90       return false;
91    b->cursor = nir_after_instr(&instr->instr);
92    bool ssbo_mode = instr->intrinsic != nir_intrinsic_load_ubo && instr->intrinsic != nir_intrinsic_load_ubo_vec4;
93    unsigned first_idx = UINT_MAX, last_idx;
94    if (ssbo_mode) {
95       /* ssbo bindings don't always start at 0 */
96       nir_foreach_variable_with_modes(var, b->shader, nir_var_mem_ssbo) {
97          first_idx = var->data.binding;
98          break;
99       }
100       assert(first_idx != UINT_MAX);
101       last_idx = first_idx + b->shader->info.num_ssbos;
102    } else {
103       /* skip 0 index if uniform_0 is one we created previously */
104       first_idx = !b->shader->info.first_ubo_is_default_ubo;
105       last_idx = first_idx + b->shader->info.num_ubos;
106    }
107 
108    /* now create the composite dest with a bcsel chain based on the original value */
109    nir_ssa_def *new_dest = recursive_generate_bo_ssa_def(b, instr,
110                                                        instr->src[block_idx].ssa,
111                                                        first_idx, last_idx);
112 
113    if (instr->intrinsic != nir_intrinsic_store_ssbo)
114       /* now use the composite dest in all cases where the original dest (from the dynamic index)
115        * was used and remove the dynamically-indexed load_*bo instruction
116        */
117       nir_ssa_def_rewrite_uses_after(&instr->dest.ssa, nir_src_for_ssa(new_dest), &instr->instr);
118 
119    nir_instr_remove(&instr->instr);
120    return true;
121 }
122 
123 bool
nir_lower_dynamic_bo_access(nir_shader * shader)124 nir_lower_dynamic_bo_access(nir_shader *shader)
125 {
126    bool progress = false;
127 
128    nir_foreach_function(function, shader) {
129       if (function->impl) {
130          nir_builder builder;
131          nir_builder_init(&builder, function->impl);
132          nir_foreach_block(block, function->impl) {
133             nir_foreach_instr_safe(instr, block) {
134                if (instr->type == nir_instr_type_intrinsic)
135                   progress |= lower_dynamic_bo_access_instr(
136                                                   nir_instr_as_intrinsic(instr),
137                                                   &builder);
138             }
139          }
140 
141          nir_metadata_preserve(function->impl, nir_metadata_dominance);
142       }
143    }
144 
145    return progress;
146 }
147