xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_passthrough_edgeflags.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 void
lower_impl(nir_function_impl * impl)28 lower_impl(nir_function_impl *impl)
29 {
30    nir_shader *shader = impl->function->shader;
31    nir_builder b;
32    nir_variable *in, *out;
33    nir_def *def;
34 
35    b = nir_builder_at(nir_before_impl(impl));
36 
37    /* The edge flag is the last input in st/mesa.  This code is also called by
38     * i965 which calls it before any input locations are assigned.
39     */
40    assert(shader->num_inputs == 0 ||
41           shader->num_inputs == util_bitcount64(shader->info.inputs_read));
42 
43    /* Lowered IO only uses intrinsics. It doesn't use variables. */
44    if (shader->info.io_lowered) {
45       assert(shader->num_outputs ==
46              util_bitcount64(shader->info.outputs_written));
47 
48       /* Load an edge flag. */
49       nir_io_semantics load_sem = { 0 };
50       load_sem.location = VERT_ATTRIB_EDGEFLAG;
51       load_sem.num_slots = 1;
52 
53       nir_def *load =
54          nir_load_input(&b, 1, 32, nir_imm_int(&b, 0),
55                         .base = shader->num_inputs++,
56                         .component = 0,
57                         .dest_type = nir_type_float32,
58                         .io_semantics = load_sem);
59 
60       /* Store an edge flag. */
61       nir_io_semantics semantics = { 0 };
62       semantics.location = VARYING_SLOT_EDGE;
63       semantics.num_slots = 1;
64 
65       nir_store_output(&b, load, nir_imm_int(&b, 0),
66                        .base = shader->num_outputs++,
67                        .component = 0,
68                        .io_semantics = semantics,
69                        .src_type = nir_type_float32,
70                        .write_mask = 0x1);
71 
72       nir_metadata_preserve(impl, nir_metadata_control_flow);
73       return;
74    }
75 
76    in = nir_create_variable_with_location(b.shader, nir_var_shader_in,
77                                           VERT_ATTRIB_EDGEFLAG, glsl_vec4_type());
78    shader->info.inputs_read |= VERT_BIT_EDGEFLAG;
79 
80    out = nir_create_variable_with_location(b.shader, nir_var_shader_out,
81                                            VARYING_SLOT_EDGE, glsl_vec4_type());
82    shader->info.outputs_written |= VARYING_BIT_EDGE;
83 
84    def = nir_load_var(&b, in);
85    nir_store_var(&b, out, def, 0xf);
86 
87    nir_metadata_preserve(impl, nir_metadata_control_flow);
88 }
89 
90 bool
nir_lower_passthrough_edgeflags(nir_shader * shader)91 nir_lower_passthrough_edgeflags(nir_shader *shader)
92 {
93    assert(shader->info.stage == MESA_SHADER_VERTEX);
94 
95    shader->info.vs.needs_edge_flag = true;
96 
97    lower_impl(nir_shader_get_entrypoint(shader));
98    return true;
99 }
100