• 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 UBO accesses of UBO binding point 0. Both
26  * the base and the offset are interpreted as 16-byte units.
27  *
28  * Simultaneously, remap existing UBO accesses by increasing their binding
29  * point by 1.
30  */
31 
32 #include "nir.h"
33 #include "nir_builder.h"
34 
35 static bool
lower_instr(nir_intrinsic_instr * instr,nir_builder * b)36 lower_instr(nir_intrinsic_instr *instr, nir_builder *b)
37 {
38    b->cursor = nir_before_instr(&instr->instr);
39 
40    if (instr->intrinsic == nir_intrinsic_load_ubo) {
41       nir_ssa_def *old_idx = nir_ssa_for_src(b, instr->src[0], 1);
42       nir_ssa_def *new_idx = nir_iadd(b, old_idx, nir_imm_int(b, 1));
43       nir_instr_rewrite_src(&instr->instr, &instr->src[0],
44                             nir_src_for_ssa(new_idx));
45       return true;
46    }
47 
48    if (instr->intrinsic == nir_intrinsic_load_uniform) {
49       nir_ssa_def *ubo_idx = nir_imm_int(b, 0);
50       nir_ssa_def *ubo_offset =
51          nir_imul(b, nir_imm_int(b, 16),
52                   nir_iadd(b, nir_imm_int(b, nir_intrinsic_base(instr)),
53                            nir_ssa_for_src(b, instr->src[0], 1)));
54 
55       nir_intrinsic_instr *load =
56          nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
57       load->num_components = instr->num_components;
58       load->src[0] = nir_src_for_ssa(ubo_idx);
59       load->src[1] = nir_src_for_ssa(ubo_offset);
60       nir_ssa_dest_init(&load->instr, &load->dest,
61                         load->num_components, instr->dest.ssa.bit_size,
62                         instr->dest.ssa.name);
63       nir_builder_instr_insert(b, &load->instr);
64       nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(&load->dest.ssa));
65 
66       nir_instr_remove(&instr->instr);
67       return true;
68    }
69 
70    return false;
71 }
72 
73 bool
nir_lower_uniforms_to_ubo(nir_shader * shader)74 nir_lower_uniforms_to_ubo(nir_shader *shader)
75 {
76    bool progress = false;
77 
78    nir_foreach_function(function, shader) {
79       if (function->impl) {
80          nir_builder builder;
81          nir_builder_init(&builder, function->impl);
82          nir_foreach_block(block, function->impl) {
83             nir_foreach_instr_safe(instr, block) {
84                if (instr->type == nir_instr_type_intrinsic)
85                   progress |= lower_instr(nir_instr_as_intrinsic(instr),
86                                           &builder);
87             }
88          }
89 
90          nir_metadata_preserve(function->impl, nir_metadata_block_index |
91                                                nir_metadata_dominance);
92       }
93    }
94 
95    return progress;
96 }
97 
98