xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_interpolation.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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_instr(nir_builder * b,nir_instr * instr,void * cb_data)38 nir_lower_interpolation_instr(nir_builder *b, nir_instr *instr, void *cb_data)
39 {
40    nir_lower_interpolation_options options =
41       *(nir_lower_interpolation_options *)cb_data;
42 
43    if (instr->type != nir_instr_type_intrinsic)
44       return false;
45 
46    nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
47 
48    if (intr->intrinsic != nir_intrinsic_load_interpolated_input)
49       return false;
50 
51    nir_intrinsic_instr *bary_intrinsic =
52       nir_instr_as_intrinsic(intr->src[0].ssa->parent_instr);
53 
54    /* Leave VARYING_SLOT_POS alone */
55    if (nir_intrinsic_base(intr) == VARYING_SLOT_POS)
56       return false;
57 
58    const enum glsl_interp_mode interp_mode =
59       nir_intrinsic_interp_mode(bary_intrinsic);
60 
61    /* We need actual interpolation modes by the time we get here */
62    assert(interp_mode != INTERP_MODE_NONE);
63 
64    /* Only lower for inputs that need interpolation */
65    if (interp_mode != INTERP_MODE_SMOOTH &&
66        interp_mode != INTERP_MODE_NOPERSPECTIVE)
67       return false;
68 
69    nir_intrinsic_op op = bary_intrinsic->intrinsic;
70 
71    switch (op) {
72    case nir_intrinsic_load_barycentric_at_sample:
73       if (options & nir_lower_interpolation_at_sample)
74          break;
75       return false;
76    case nir_intrinsic_load_barycentric_at_offset:
77       if (options & nir_lower_interpolation_at_offset)
78          break;
79       return false;
80    case nir_intrinsic_load_barycentric_centroid:
81       if (options & nir_lower_interpolation_centroid)
82          break;
83       return false;
84    case nir_intrinsic_load_barycentric_pixel:
85       if (options & nir_lower_interpolation_pixel)
86          break;
87       return false;
88    case nir_intrinsic_load_barycentric_sample:
89       if (options & nir_lower_interpolation_sample)
90          break;
91       return false;
92    default:
93       return false;
94    }
95 
96    b->cursor = nir_before_instr(instr);
97 
98    nir_def *comps[NIR_MAX_VEC_COMPONENTS];
99    for (int i = 0; i < intr->num_components; i++) {
100       nir_def *iid =
101          nir_load_fs_input_interp_deltas(b, 32, intr->src[1].ssa,
102                                          .base = nir_intrinsic_base(intr),
103                                          .component = (nir_intrinsic_component(intr) + i),
104                                          .io_semantics = nir_intrinsic_io_semantics(intr));
105 
106       nir_def *bary = intr->src[0].ssa;
107       nir_def *val;
108 
109       val = nir_ffma(b, nir_channel(b, bary, 1),
110                      nir_channel(b, iid, 1),
111                      nir_channel(b, iid, 0));
112       val = nir_ffma(b, nir_channel(b, bary, 0),
113                      nir_channel(b, iid, 2),
114                      val);
115 
116       comps[i] = val;
117    }
118    nir_def *vec = nir_vec(b, comps, intr->num_components);
119    nir_def_rewrite_uses(&intr->def, vec);
120 
121    return true;
122 }
123 
124 bool
nir_lower_interpolation(nir_shader * shader,nir_lower_interpolation_options options)125 nir_lower_interpolation(nir_shader *shader, nir_lower_interpolation_options options)
126 {
127    return nir_shader_instructions_pass(shader, nir_lower_interpolation_instr,
128                                        nir_metadata_control_flow,
129                                        &options);
130 }
131