• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_io(nir_builder * b,nir_intrinsic_instr * intr,void * data)37 lower_input_io(nir_builder *b, nir_intrinsic_instr *intr, void *data)
38 {
39    if (intr->intrinsic != nir_intrinsic_load_interpolated_input)
40       return false;;
41    nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
42    if (!check_location(sem.location))
43       return false;
44    nir_def *interp = intr->src[0].ssa;
45    nir_intrinsic_instr *interp_intr = nir_instr_as_intrinsic(interp->parent_instr);
46    if (nir_intrinsic_interp_mode(interp_intr) != INTERP_MODE_NONE)
47       return false;
48    b->cursor = nir_before_instr(&intr->instr);
49    nir_def *load = nir_load_input(b, intr->num_components,
50                                   intr->def.bit_size, intr->src[1].ssa);
51    nir_intrinsic_instr *new_intr = nir_instr_as_intrinsic(load->parent_instr);
52    nir_intrinsic_copy_const_indices(new_intr, intr);
53    nir_def_replace(&intr->def, load);
54    return true;
55 }
56 
57 bool
nir_lower_flatshade(nir_shader * shader)58 nir_lower_flatshade(nir_shader *shader)
59 {
60    assert(shader->info.io_lowered);
61    return nir_shader_intrinsics_pass(shader, lower_input_io, nir_metadata_all,
62                                      NULL);
63 }
64