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_ssa_def *def;
34
35 nir_builder_init(&b, impl);
36 b.cursor = nir_before_cf_list(&impl->body);
37
38 /* The edge flag is the last input in st/mesa. */
39 assert(shader->num_inputs == util_bitcount64(shader->info.inputs_read));
40
41 /* Lowered IO only uses intrinsics. It doesn't use variables. */
42 if (shader->info.io_lowered) {
43 assert(shader->num_outputs ==
44 util_bitcount64(shader->info.outputs_written));
45
46 /* Load an edge flag. */
47 nir_intrinsic_instr *load =
48 nir_intrinsic_instr_create(shader, nir_intrinsic_load_input);
49 load->num_components = 1;
50 load->src[0] = nir_src_for_ssa(nir_imm_int(&b, 0));
51 nir_ssa_dest_init(&load->instr, &load->dest,
52 load->num_components, 32, NULL);
53
54 nir_intrinsic_set_base(load, shader->num_inputs++);
55 nir_intrinsic_set_component(load, 0);
56 nir_intrinsic_set_dest_type(load, nir_type_float32);
57
58 nir_io_semantics load_sem = {0};
59 load_sem.location = VERT_ATTRIB_EDGEFLAG;
60 load_sem.num_slots = 1;
61 nir_intrinsic_set_io_semantics(load, load_sem);
62 nir_builder_instr_insert(&b, &load->instr);
63
64 /* Store an edge flag. */
65 nir_intrinsic_instr *store =
66 nir_intrinsic_instr_create(shader, nir_intrinsic_store_output);
67 store->num_components = 1;
68 store->src[0] = nir_src_for_ssa(&load->dest.ssa);
69 store->src[1] = nir_src_for_ssa(nir_imm_int(&b, 0));
70
71 nir_intrinsic_set_base(store, shader->num_outputs++);
72 nir_intrinsic_set_component(store, 0);
73 nir_intrinsic_set_src_type(store, nir_type_float32);
74 nir_intrinsic_set_write_mask(store, 0x1);
75
76 nir_io_semantics semantics = {0};
77 semantics.location = VARYING_SLOT_EDGE;
78 semantics.num_slots = 1;
79 nir_intrinsic_set_io_semantics(store, semantics);
80 nir_builder_instr_insert(&b, &store->instr);
81
82 nir_metadata_preserve(impl, nir_metadata_block_index |
83 nir_metadata_dominance);
84 return;
85 }
86
87 in = nir_variable_create(shader, nir_var_shader_in,
88 glsl_vec4_type(), "edgeflag_in");
89 in->data.location = VERT_ATTRIB_EDGEFLAG;
90
91 in->data.driver_location = shader->num_inputs++;
92 shader->info.inputs_read |= BITFIELD64_BIT(VERT_ATTRIB_EDGEFLAG);
93
94 out = nir_variable_create(shader, nir_var_shader_out,
95 glsl_vec4_type(), "edgeflag_out");
96 out->data.location = VARYING_SLOT_EDGE;
97
98 def = nir_load_var(&b, in);
99 nir_store_var(&b, out, def, 0xf);
100
101 nir_metadata_preserve(impl, nir_metadata_block_index |
102 nir_metadata_dominance);
103 }
104
nir_lower_passthrough_edgeflags(nir_shader * shader)105 void nir_lower_passthrough_edgeflags(nir_shader *shader)
106 {
107 lower_impl(nir_shader_get_entrypoint(shader));
108 }
109