Merge branch 'master' of ../mesa into vulkan
[mesa.git] / src / glsl / nir / nir_lower_two_sided_color.c
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "nir.h"
28 #include "nir_builder.h"
29
30 #define MAX_COLORS 2 /* VARYING_SLOT_COL0/COL1 */
31
32 typedef struct {
33 nir_builder b;
34 nir_shader *shader;
35 nir_variable *face;
36 struct {
37 nir_variable *front; /* COLn */
38 nir_variable *back; /* BFCn */
39 } colors[MAX_COLORS];
40 int colors_count;
41 } lower_2side_state;
42
43
44 /* Lowering pass for fragment shaders to emulated two-sided-color. For
45 * each COLOR input, a corresponding BCOLOR input is created, and bcsel
46 * instruction used to select front or back color based on FACE.
47 */
48
49 static nir_variable *
50 create_input(nir_shader *shader, unsigned drvloc, gl_varying_slot slot)
51 {
52 nir_variable *var = rzalloc(shader, nir_variable);
53
54 var->data.driver_location = drvloc;
55 var->type = glsl_vec4_type();
56 var->data.mode = nir_var_shader_in;
57 var->name = ralloc_asprintf(var, "in_%d", drvloc);
58 var->data.index = 0;
59 var->data.location = slot;
60
61 exec_list_push_tail(&shader->inputs, &var->node);
62
63 return var;
64 }
65
66 static nir_ssa_def *
67 load_input(nir_builder *b, nir_variable *in)
68 {
69 nir_intrinsic_instr *load;
70
71 load = nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_input);
72 load->num_components = 4;
73 load->const_index[0] = in->data.driver_location;
74 nir_ssa_dest_init(&load->instr, &load->dest, 4, NULL);
75 nir_builder_instr_insert(b, &load->instr);
76
77 return &load->dest.ssa;
78 }
79
80 static int
81 setup_inputs(lower_2side_state *state)
82 {
83 int maxloc = -1;
84
85 /* find color/face inputs: */
86 foreach_list_typed(nir_variable, var, node, &state->shader->inputs) {
87 int loc = var->data.driver_location;
88
89 /* keep track of last used driver-location.. we'll be
90 * appending BCLr/FACE after last existing input:
91 */
92 maxloc = MAX2(maxloc, loc);
93
94 switch (var->data.location) {
95 case VARYING_SLOT_COL0:
96 case VARYING_SLOT_COL1:
97 assert(state->colors_count < ARRAY_SIZE(state->colors));
98 state->colors[state->colors_count].front = var;
99 state->colors_count++;
100 break;
101 case VARYING_SLOT_FACE:
102 state->face = var;
103 break;
104 }
105 }
106
107 /* if we don't have any color inputs, nothing to do: */
108 if (state->colors_count == 0)
109 return -1;
110
111 /* if we don't already have one, insert a FACE input: */
112 if (!state->face) {
113 state->face = create_input(state->shader, ++maxloc, VARYING_SLOT_FACE);
114 state->face->data.interpolation = INTERP_QUALIFIER_FLAT;
115 }
116
117 /* add required back-face color inputs: */
118 for (int i = 0; i < state->colors_count; i++) {
119 gl_varying_slot slot;
120
121 if (state->colors[i].front->data.location == VARYING_SLOT_COL0)
122 slot = VARYING_SLOT_BFC0;
123 else
124 slot = VARYING_SLOT_BFC1;
125
126 state->colors[i].back = create_input(state->shader, ++maxloc, slot);
127 }
128
129 return 0;
130 }
131
132 static bool
133 nir_lower_two_sided_color_block(nir_block *block, void *void_state)
134 {
135 lower_2side_state *state = void_state;
136 nir_builder *b = &state->b;
137
138 nir_foreach_instr_safe(block, instr) {
139 if (instr->type != nir_instr_type_intrinsic)
140 continue;
141
142 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
143
144 if (intr->intrinsic != nir_intrinsic_load_input)
145 continue;
146
147 int idx;
148 for (idx = 0; idx < state->colors_count; idx++) {
149 unsigned drvloc =
150 state->colors[idx].front->data.driver_location;
151 if (intr->const_index[0] == drvloc) {
152 break;
153 }
154 }
155
156 if (idx == state->colors_count)
157 continue;
158
159 /* replace load_input(COLn) with
160 * bcsel(load_input(FACE), load_input(COLn), load_input(BFCn))
161 */
162 b->cursor = nir_before_instr(&intr->instr);
163 nir_ssa_def *face = nir_channel(b, load_input(b, state->face), 0);
164 nir_ssa_def *front = load_input(b, state->colors[idx].front);
165 nir_ssa_def *back = load_input(b, state->colors[idx].back);
166 nir_ssa_def *cond = nir_flt(b, face, nir_imm_float(b, 0.0));
167 nir_ssa_def *color = nir_bcsel(b, cond, back, front);
168
169 assert(intr->dest.is_ssa);
170 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(color));
171 }
172
173 return true;
174 }
175
176 static void
177 nir_lower_two_sided_color_impl(nir_function_impl *impl,
178 lower_2side_state *state)
179 {
180 nir_builder *b = &state->b;
181
182 nir_builder_init(b, impl);
183
184 nir_foreach_block(impl, nir_lower_two_sided_color_block, state);
185
186 nir_metadata_preserve(impl, nir_metadata_block_index |
187 nir_metadata_dominance);
188 }
189
190 void
191 nir_lower_two_sided_color(nir_shader *shader)
192 {
193 lower_2side_state state = {
194 .shader = shader,
195 };
196
197 if (shader->stage != MESA_SHADER_FRAGMENT)
198 return;
199
200 if (setup_inputs(&state) != 0)
201 return;
202
203 nir_foreach_overload(shader, overload) {
204 if (overload->impl)
205 nir_lower_two_sided_color_impl(overload->impl, &state);
206 }
207
208 }