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_def *load =
50 nir_load_input(&b, 1, 32, nir_imm_int(&b, 0),
51 .base = shader->num_inputs++,
52 .io_semantics.location = VERT_ATTRIB_EDGEFLAG);
53
54 /* Store an edge flag. */
55 nir_store_output(&b, load, nir_imm_int(&b, 0),
56 .base = shader->num_outputs++,
57 .io_semantics.location = VARYING_SLOT_EDGE);
58
59 nir_metadata_preserve(impl, nir_metadata_control_flow);
60 return;
61 }
62
63 in = nir_create_variable_with_location(b.shader, nir_var_shader_in,
64 VERT_ATTRIB_EDGEFLAG, glsl_vec4_type());
65 shader->info.inputs_read |= VERT_BIT_EDGEFLAG;
66
67 out = nir_create_variable_with_location(b.shader, nir_var_shader_out,
68 VARYING_SLOT_EDGE, glsl_vec4_type());
69 shader->info.outputs_written |= VARYING_BIT_EDGE;
70
71 def = nir_load_var(&b, in);
72 nir_store_var(&b, out, def, 0xf);
73
74 nir_metadata_preserve(impl, nir_metadata_control_flow);
75 }
76
77 bool
nir_lower_passthrough_edgeflags(nir_shader * shader)78 nir_lower_passthrough_edgeflags(nir_shader *shader)
79 {
80 assert(shader->info.stage == MESA_SHADER_VERTEX);
81
82 shader->info.vs.needs_edge_flag = true;
83
84 lower_impl(nir_shader_get_entrypoint(shader));
85 return true;
86 }
87