nir: Switch to using 1-bit Booleans for almost everything
[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 struct {
36 nir_variable *front; /* COLn */
37 nir_variable *back; /* BFCn */
38 } colors[MAX_COLORS];
39 int colors_count;
40 } lower_2side_state;
41
42
43 /* Lowering pass for fragment shaders to emulated two-sided-color. For
44 * each COLOR input, a corresponding BCOLOR input is created, and bcsel
45 * instruction used to select front or back color based on FACE.
46 */
47
48 static nir_variable *
49 create_input(nir_shader *shader, unsigned drvloc, gl_varying_slot slot,
50 enum glsl_interp_mode interpolation)
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 var->data.interpolation = interpolation;
61
62 exec_list_push_tail(&shader->inputs, &var->node);
63
64 shader->num_inputs++; /* TODO use type_size() */
65
66 return var;
67 }
68
69 static nir_ssa_def *
70 load_input(nir_builder *b, nir_variable *in)
71 {
72 nir_intrinsic_instr *load;
73
74 load = nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_input);
75 load->num_components = 4;
76 nir_intrinsic_set_base(load, in->data.driver_location);
77 load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0));
78 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
79 nir_builder_instr_insert(b, &load->instr);
80
81 return &load->dest.ssa;
82 }
83
84 static int
85 setup_inputs(lower_2side_state *state)
86 {
87 int maxloc = -1;
88
89 /* find color inputs: */
90 nir_foreach_variable(var, &state->shader->inputs) {
91 int loc = var->data.driver_location;
92
93 /* keep track of last used driver-location.. we'll be
94 * appending BCLr after last existing input:
95 */
96 maxloc = MAX2(maxloc, loc);
97
98 switch (var->data.location) {
99 case VARYING_SLOT_COL0:
100 case VARYING_SLOT_COL1:
101 assert(state->colors_count < ARRAY_SIZE(state->colors));
102 state->colors[state->colors_count].front = var;
103 state->colors_count++;
104 break;
105 }
106 }
107
108 /* if we don't have any color inputs, nothing to do: */
109 if (state->colors_count == 0)
110 return -1;
111
112 /* add required back-face color inputs: */
113 for (int i = 0; i < state->colors_count; i++) {
114 gl_varying_slot slot;
115
116 if (state->colors[i].front->data.location == VARYING_SLOT_COL0)
117 slot = VARYING_SLOT_BFC0;
118 else
119 slot = VARYING_SLOT_BFC1;
120
121 state->colors[i].back = create_input(
122 state->shader, ++maxloc, slot,
123 state->colors[i].front->data.interpolation);
124 }
125
126 return 0;
127 }
128
129 static bool
130 nir_lower_two_sided_color_block(nir_block *block,
131 lower_2side_state *state)
132 {
133 nir_builder *b = &state->b;
134
135 nir_foreach_instr_safe(instr, block) {
136 if (instr->type != nir_instr_type_intrinsic)
137 continue;
138
139 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
140
141 if (intr->intrinsic != nir_intrinsic_load_input)
142 continue;
143
144 int idx;
145 for (idx = 0; idx < state->colors_count; idx++) {
146 unsigned drvloc =
147 state->colors[idx].front->data.driver_location;
148 if (nir_intrinsic_base(intr) == drvloc) {
149 assert(nir_src_is_const(intr->src[0]));
150 break;
151 }
152 }
153
154 if (idx == state->colors_count)
155 continue;
156
157 /* replace load_input(COLn) with
158 * bcsel(load_system_value(FACE), load_input(COLn), load_input(BFCn))
159 */
160 b->cursor = nir_before_instr(&intr->instr);
161 nir_ssa_def *face = nir_load_front_face(b);
162 /* gl_FrontFace is a boolean but the intrinsic constructor creates
163 * 32-bit value by default.
164 */
165 face->bit_size = 1;
166 nir_ssa_def *front = load_input(b, state->colors[idx].front);
167 nir_ssa_def *back = load_input(b, state->colors[idx].back);
168 nir_ssa_def *color = nir_bcsel(b, face, front, back);
169
170 assert(intr->dest.is_ssa);
171 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(color));
172 }
173
174 return true;
175 }
176
177 static void
178 nir_lower_two_sided_color_impl(nir_function_impl *impl,
179 lower_2side_state *state)
180 {
181 nir_builder *b = &state->b;
182
183 nir_builder_init(b, impl);
184
185 nir_foreach_block(block, impl) {
186 nir_lower_two_sided_color_block(block, state);
187 }
188
189 nir_metadata_preserve(impl, nir_metadata_block_index |
190 nir_metadata_dominance);
191 }
192
193 void
194 nir_lower_two_sided_color(nir_shader *shader)
195 {
196 lower_2side_state state = {
197 .shader = shader,
198 };
199
200 if (shader->info.stage != MESA_SHADER_FRAGMENT)
201 return;
202
203 if (setup_inputs(&state) != 0)
204 return;
205
206 nir_foreach_function(function, shader) {
207 if (function->impl)
208 nir_lower_two_sided_color_impl(function->impl, &state);
209 }
210
211 }