• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2019 Google, 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  * 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 
24 /*
25  * This lower pass lowers load_interpolated_input for various interpolation
26  * modes (as configured via nir_lower_interpolation_options bitmask) into
27  * load_attribute_deltas plus alu instructions:
28  *
29  *    vec3 ad = load_attribute_deltas(varying_slot)
30  *    float result = ad.x + ad.y * j + ad.z * i
31  *
32  */
33 
34 #include "nir.h"
35 #include "nir_builder.h"
36 
37 static bool
nir_lower_interpolation_block(nir_block * block,nir_builder * b,nir_lower_interpolation_options options)38 nir_lower_interpolation_block(nir_block *block, nir_builder *b,
39                               nir_lower_interpolation_options options)
40 {
41    bool progress = false;
42 
43    nir_foreach_instr_safe(instr, block) {
44       if (instr->type != nir_instr_type_intrinsic)
45          continue;
46 
47       nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
48 
49       if (intr->intrinsic != nir_intrinsic_load_interpolated_input)
50          continue;
51 
52       assert(intr->dest.is_ssa);
53       assert(intr->src[0].is_ssa);
54       assert(intr->src[1].is_ssa);
55 
56       nir_intrinsic_instr *bary_intrinsic =
57          nir_instr_as_intrinsic(intr->src[0].ssa->parent_instr);
58 
59       /* Leave VARYING_SLOT_POS alone */
60       if (nir_intrinsic_base(intr) == VARYING_SLOT_POS)
61          continue;
62 
63       const enum glsl_interp_mode interp_mode =
64          nir_intrinsic_interp_mode(bary_intrinsic);
65 
66       /* We need actual interpolation modes by the time we get here */
67       assert(interp_mode != INTERP_MODE_NONE);
68 
69       /* Only lower for inputs that need interpolation */
70       if (interp_mode != INTERP_MODE_SMOOTH &&
71           interp_mode != INTERP_MODE_NOPERSPECTIVE)
72          continue;
73 
74       nir_intrinsic_op op = bary_intrinsic->intrinsic;
75 
76       switch (op) {
77       case nir_intrinsic_load_barycentric_at_sample:
78          if (options & nir_lower_interpolation_at_sample)
79             break;
80          continue;
81       case nir_intrinsic_load_barycentric_at_offset:
82          if (options & nir_lower_interpolation_at_offset)
83             break;
84          continue;
85       case nir_intrinsic_load_barycentric_centroid:
86          if (options & nir_lower_interpolation_centroid)
87             break;
88          continue;
89       case nir_intrinsic_load_barycentric_pixel:
90          if (options & nir_lower_interpolation_pixel)
91             break;
92          continue;
93       case nir_intrinsic_load_barycentric_sample:
94          if (options & nir_lower_interpolation_sample)
95             break;
96          continue;
97       default:
98          continue;
99       }
100 
101       b->cursor = nir_before_instr(instr);
102 
103       nir_ssa_def *comps[NIR_MAX_VEC_COMPONENTS];
104       for (int i = 0; i < intr->num_components; i++) {
105          nir_ssa_def *iid =
106             nir_load_fs_input_interp_deltas(b, 32, intr->src[1].ssa,
107                                             .base = nir_intrinsic_base(intr),
108                                             .component = (nir_intrinsic_component(intr) + i),
109                                             .io_semantics = nir_intrinsic_io_semantics(intr));
110 
111          nir_ssa_def *bary = intr->src[0].ssa;
112          nir_ssa_def *val;
113 
114          val = nir_ffma(b, nir_channel(b, bary, 1),
115                            nir_channel(b, iid, 1),
116                            nir_channel(b, iid, 0));
117          val = nir_ffma(b, nir_channel(b, bary, 0),
118                            nir_channel(b, iid, 2),
119                            val);
120 
121          comps[i] = val;
122       }
123       nir_ssa_def *vec = nir_vec(b, comps, intr->num_components);
124       nir_ssa_def_rewrite_uses(&intr->dest.ssa, vec);
125 
126       progress = true;
127    }
128 
129    return progress;
130 }
131 
132 static bool
nir_lower_interpolation_impl(nir_function_impl * impl,nir_lower_interpolation_options options)133 nir_lower_interpolation_impl(nir_function_impl *impl,
134                              nir_lower_interpolation_options options)
135 {
136    bool progress = false;
137    nir_builder builder;
138    nir_builder_init(&builder, impl);
139 
140    nir_foreach_block(block, impl) {
141       progress |= nir_lower_interpolation_block(block, &builder, options);
142    }
143 
144    nir_metadata_preserve(impl, nir_metadata_block_index |
145                                nir_metadata_dominance);
146    return progress;
147 }
148 
149 bool
nir_lower_interpolation(nir_shader * shader,nir_lower_interpolation_options options)150 nir_lower_interpolation(nir_shader *shader, nir_lower_interpolation_options options)
151 {
152    bool progress = false;
153 
154    nir_foreach_function(function, shader) {
155       if (function->impl)
156          progress |= nir_lower_interpolation_impl(function->impl, options);
157    }
158 
159    return progress;
160 }
161