xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_flatshade.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2015 Red Hat
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_builder.h"
26 
27 static bool
check_location(int location)28 check_location(int location)
29 {
30    return location == VARYING_SLOT_COL0 ||
31           location == VARYING_SLOT_COL1 ||
32           location == VARYING_SLOT_BFC0 ||
33           location == VARYING_SLOT_BFC1;
34 }
35 
36 static bool
lower_input(nir_shader * shader,nir_variable * var)37 lower_input(nir_shader *shader, nir_variable *var)
38 {
39    if (var->data.interpolation == INTERP_MODE_NONE &&
40        check_location(var->data.location))
41       var->data.interpolation = INTERP_MODE_FLAT;
42    return true;
43 }
44 
45 static bool
lower_input_io(nir_builder * b,nir_intrinsic_instr * intr,void * data)46 lower_input_io(nir_builder *b, nir_intrinsic_instr *intr, void *data)
47 {
48    if (intr->intrinsic != nir_intrinsic_load_interpolated_input)
49       return false;;
50    nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
51    if (!check_location(sem.location))
52       return false;
53    nir_def *interp = intr->src[0].ssa;
54    nir_intrinsic_instr *interp_intr = nir_instr_as_intrinsic(interp->parent_instr);
55    if (nir_intrinsic_interp_mode(interp_intr) != INTERP_MODE_NONE)
56       return false;
57    b->cursor = nir_before_instr(&intr->instr);
58    nir_def *load = nir_load_input(b, intr->num_components,
59                                   intr->def.bit_size, intr->src[1].ssa);
60    nir_intrinsic_instr *new_intr = nir_instr_as_intrinsic(load->parent_instr);
61    nir_intrinsic_copy_const_indices(new_intr, intr);
62    nir_def_replace(&intr->def, load);
63    return true;
64 }
65 bool
nir_lower_flatshade(nir_shader * shader)66 nir_lower_flatshade(nir_shader *shader)
67 {
68    bool progress = false;
69 
70    if (shader->info.io_lowered) {
71       progress = nir_shader_intrinsics_pass(shader, lower_input_io,
72                                             nir_metadata_all, NULL);
73    } else {
74       nir_foreach_shader_in_variable(var, shader) {
75          progress |= lower_input(shader, var);
76       }
77    }
78 
79    /* Interpolation doesn't affect any metadata */
80    nir_shader_preserve_all_metadata(shader);
81    return progress;
82 }
83