1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2015 Red Hat
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "nir.h"
25bf215546Sopenharmony_ci#include "nir_builder.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_cistatic void
28bf215546Sopenharmony_cilower_impl(nir_function_impl *impl)
29bf215546Sopenharmony_ci{
30bf215546Sopenharmony_ci   nir_shader *shader = impl->function->shader;
31bf215546Sopenharmony_ci   nir_builder b;
32bf215546Sopenharmony_ci   nir_variable *in, *out;
33bf215546Sopenharmony_ci   nir_ssa_def *def;
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci   nir_builder_init(&b, impl);
36bf215546Sopenharmony_ci   b.cursor = nir_before_cf_list(&impl->body);
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   /* The edge flag is the last input in st/mesa.  This code is also called by
39bf215546Sopenharmony_ci    * i965 which calls it before any input locations are assigned.
40bf215546Sopenharmony_ci    */
41bf215546Sopenharmony_ci   assert(shader->num_inputs == 0 ||
42bf215546Sopenharmony_ci          shader->num_inputs == util_bitcount64(shader->info.inputs_read));
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   /* Lowered IO only uses intrinsics. It doesn't use variables. */
45bf215546Sopenharmony_ci   if (shader->info.io_lowered) {
46bf215546Sopenharmony_ci      assert(shader->num_outputs ==
47bf215546Sopenharmony_ci             util_bitcount64(shader->info.outputs_written));
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci      /* Load an edge flag. */
50bf215546Sopenharmony_ci      nir_io_semantics load_sem = {0};
51bf215546Sopenharmony_ci      load_sem.location = VERT_ATTRIB_EDGEFLAG;
52bf215546Sopenharmony_ci      load_sem.num_slots = 1;
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci      nir_ssa_def *load =
55bf215546Sopenharmony_ci         nir_load_input(&b, 1, 32, nir_imm_int(&b, 0),
56bf215546Sopenharmony_ci                        .base = shader->num_inputs++,
57bf215546Sopenharmony_ci                        .component = 0,
58bf215546Sopenharmony_ci                        .dest_type = nir_type_float32,
59bf215546Sopenharmony_ci                        .io_semantics = load_sem);
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci      /* Store an edge flag. */
62bf215546Sopenharmony_ci      nir_io_semantics semantics = {0};
63bf215546Sopenharmony_ci      semantics.location = VARYING_SLOT_EDGE;
64bf215546Sopenharmony_ci      semantics.num_slots = 1;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci      nir_store_output(&b, load, nir_imm_int(&b, 0),
67bf215546Sopenharmony_ci                       .base = shader->num_outputs++,
68bf215546Sopenharmony_ci                       .component = 0,
69bf215546Sopenharmony_ci                       .io_semantics = semantics,
70bf215546Sopenharmony_ci                       .src_type = nir_type_float32,
71bf215546Sopenharmony_ci                       .write_mask = 0x1);
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_block_index |
74bf215546Sopenharmony_ci                                  nir_metadata_dominance);
75bf215546Sopenharmony_ci      return;
76bf215546Sopenharmony_ci   }
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   in  = nir_variable_create(shader, nir_var_shader_in,
79bf215546Sopenharmony_ci                             glsl_vec4_type(), "edgeflag_in");
80bf215546Sopenharmony_ci   in->data.location = VERT_ATTRIB_EDGEFLAG;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   in->data.driver_location = shader->num_inputs++;
83bf215546Sopenharmony_ci   shader->info.inputs_read |= VERT_BIT_EDGEFLAG;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   out = nir_variable_create(shader, nir_var_shader_out,
86bf215546Sopenharmony_ci                             glsl_vec4_type(), "edgeflag_out");
87bf215546Sopenharmony_ci   out->data.location = VARYING_SLOT_EDGE;
88bf215546Sopenharmony_ci   shader->info.outputs_written |= VARYING_BIT_EDGE;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   def = nir_load_var(&b, in);
91bf215546Sopenharmony_ci   nir_store_var(&b, out, def, 0xf);
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   nir_metadata_preserve(impl, nir_metadata_block_index |
94bf215546Sopenharmony_ci                               nir_metadata_dominance);
95bf215546Sopenharmony_ci}
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_civoid nir_lower_passthrough_edgeflags(nir_shader *shader)
98bf215546Sopenharmony_ci{
99bf215546Sopenharmony_ci   assert(shader->info.stage == MESA_SHADER_VERTEX);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   shader->info.vs.needs_edge_flag = true;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   lower_impl(nir_shader_get_entrypoint(shader));
104bf215546Sopenharmony_ci}
105