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 = nir_builder_at(nir_before_impl(impl));
32
33 /* The edge flag is the last input in st/mesa. This code is also called by
34 * i965 which calls it before any input locations are assigned.
35 */
36 assert(shader->num_inputs == 0 ||
37 shader->num_inputs == util_bitcount64(shader->info.inputs_read));
38
39 assert(shader->num_outputs ==
40 util_bitcount64(shader->info.outputs_written));
41
42 /* Load an edge flag. */
43 nir_def *load = nir_load_input(&b, 1, 32, nir_imm_int(&b, 0),
44 .base = shader->num_inputs++,
45 .io_semantics.location = VERT_ATTRIB_EDGEFLAG);
46
47 /* Store an edge flag. */
48 nir_store_output(&b, load, nir_imm_int(&b, 0),
49 .base = shader->num_outputs++,
50 .io_semantics.location = VARYING_SLOT_EDGE);
51
52 nir_metadata_preserve(impl, nir_metadata_control_flow);
53 }
54
55 bool
nir_lower_passthrough_edgeflags(nir_shader * shader)56 nir_lower_passthrough_edgeflags(nir_shader *shader)
57 {
58 assert(shader->info.stage == MESA_SHADER_VERTEX);
59 assert(shader->info.io_lowered);
60
61 shader->info.vs.needs_edge_flag = true;
62
63 lower_impl(nir_shader_get_entrypoint(shader));
64 return true;
65 }
66