aa0d9fe84b20b1bd90eda59630b986aca5fda9d6
[mesa.git] / src / compiler / 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 bool face_sysval;
36 struct {
37 nir_variable *front; /* COLn */
38 nir_variable *back; /* BFCn */
39 } colors[MAX_COLORS];
40 nir_variable *face;
41 int colors_count;
42 } lower_2side_state;
43
44
45 /* Lowering pass for fragment shaders to emulated two-sided-color. For
46 * each COLOR input, a corresponding BCOLOR input is created, and bcsel
47 * instruction used to select front or back color based on FACE.
48 */
49
50 static nir_variable *
51 create_input(nir_shader *shader, gl_varying_slot slot,
52 enum glsl_interp_mode interpolation)
53 {
54 nir_variable *var = nir_variable_create(shader, nir_var_shader_in,
55 glsl_vec4_type(), NULL);
56
57 var->data.driver_location = shader->num_inputs++;
58 var->name = ralloc_asprintf(var, "in_%d", var->data.driver_location);
59 var->data.index = 0;
60 var->data.location = slot;
61 var->data.interpolation = interpolation;
62
63 return var;
64 }
65
66 static nir_variable *
67 create_face_input(nir_shader *shader)
68 {
69 nir_foreach_shader_in_variable(var, shader) {
70 if (var->data.location == VARYING_SLOT_FACE)
71 return var;
72 }
73
74 nir_variable *var = nir_variable_create(shader, nir_var_shader_in,
75 glsl_bool_type(), "gl_FrontFacing");
76
77 var->data.driver_location = shader->num_inputs++;
78 var->data.index = 0;
79 var->data.location = VARYING_SLOT_FACE;
80 var->data.interpolation = INTERP_MODE_FLAT;
81
82 return var;
83 }
84
85 static nir_ssa_def *
86 load_input(nir_builder *b, nir_variable *in)
87 {
88 nir_intrinsic_instr *load;
89
90 load = nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_input);
91 load->num_components = 4;
92 nir_intrinsic_set_base(load, in->data.driver_location);
93 load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0));
94 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
95 nir_builder_instr_insert(b, &load->instr);
96
97 return &load->dest.ssa;
98 }
99
100 static int
101 setup_inputs(lower_2side_state *state)
102 {
103 /* find color inputs: */
104 nir_foreach_shader_in_variable(var, state->shader) {
105 switch (var->data.location) {
106 case VARYING_SLOT_COL0:
107 case VARYING_SLOT_COL1:
108 assert(state->colors_count < ARRAY_SIZE(state->colors));
109 state->colors[state->colors_count].front = var;
110 state->colors_count++;
111 break;
112 }
113 }
114
115 /* if we don't have any color inputs, nothing to do: */
116 if (state->colors_count == 0)
117 return -1;
118
119 /* add required back-face color inputs: */
120 for (int i = 0; i < state->colors_count; i++) {
121 gl_varying_slot slot;
122
123 if (state->colors[i].front->data.location == VARYING_SLOT_COL0)
124 slot = VARYING_SLOT_BFC0;
125 else
126 slot = VARYING_SLOT_BFC1;
127
128 state->colors[i].back = create_input(
129 state->shader, slot,
130 state->colors[i].front->data.interpolation);
131 }
132
133 if (!state->face_sysval)
134 state->face = create_face_input(state->shader);
135
136 return 0;
137 }
138
139 static bool
140 nir_lower_two_sided_color_block(nir_block *block,
141 lower_2side_state *state)
142 {
143 nir_builder *b = &state->b;
144
145 nir_foreach_instr_safe(instr, block) {
146 if (instr->type != nir_instr_type_intrinsic)
147 continue;
148
149 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
150
151 int idx;
152 if (intr->intrinsic == nir_intrinsic_load_input) {
153 for (idx = 0; idx < state->colors_count; idx++) {
154 unsigned drvloc =
155 state->colors[idx].front->data.driver_location;
156 if (nir_intrinsic_base(intr) == drvloc) {
157 assert(nir_src_is_const(intr->src[0]));
158 break;
159 }
160 }
161 } else if (intr->intrinsic == nir_intrinsic_load_deref) {
162 nir_variable *var = nir_intrinsic_get_var(intr, 0);
163 if (var->data.mode != nir_var_shader_in)
164 continue;
165
166 for (idx = 0; idx < state->colors_count; idx++) {
167 unsigned loc = state->colors[idx].front->data.location;
168 if (var->data.location == loc)
169 break;
170 }
171 } else
172 continue;
173
174 if (idx == state->colors_count)
175 continue;
176
177 /* replace load_input(COLn) with
178 * bcsel(load_system_value(FACE), load_input(COLn), load_input(BFCn))
179 */
180 b->cursor = nir_before_instr(&intr->instr);
181 /* gl_FrontFace is a boolean but the intrinsic constructor creates
182 * 32-bit value by default.
183 */
184 nir_ssa_def *face;
185 if (state->face_sysval)
186 face = nir_load_front_face(b, 1);
187 else
188 face = nir_load_var(b, state->face);
189
190 nir_ssa_def *front, *back;
191 if (intr->intrinsic == nir_intrinsic_load_deref) {
192 front = nir_load_var(b, state->colors[idx].front);
193 back = nir_load_var(b, state->colors[idx].back);
194 } else {
195 front = load_input(b, state->colors[idx].front);
196 back = load_input(b, state->colors[idx].back);
197 }
198 nir_ssa_def *color = nir_bcsel(b, face, front, back);
199
200 assert(intr->dest.is_ssa);
201 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(color));
202 }
203
204 return true;
205 }
206
207 static void
208 nir_lower_two_sided_color_impl(nir_function_impl *impl,
209 lower_2side_state *state)
210 {
211 nir_builder *b = &state->b;
212
213 nir_builder_init(b, impl);
214
215 nir_foreach_block(block, impl) {
216 nir_lower_two_sided_color_block(block, state);
217 }
218
219 nir_metadata_preserve(impl, nir_metadata_block_index |
220 nir_metadata_dominance);
221 }
222
223 void
224 nir_lower_two_sided_color(nir_shader *shader, bool face_sysval)
225 {
226 lower_2side_state state = {
227 .shader = shader,
228 .face_sysval = face_sysval,
229 };
230
231 if (shader->info.stage != MESA_SHADER_FRAGMENT)
232 return;
233
234 if (setup_inputs(&state) != 0)
235 return;
236
237 nir_foreach_function(function, shader) {
238 if (function->impl)
239 nir_lower_two_sided_color_impl(function->impl, &state);
240 }
241
242 }