nir: Switch the arguments to nir_foreach_function
[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 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 shader->num_inputs++; /* TODO use type_size() */
64
65 return var;
66 }
67
68 static nir_ssa_def *
69 load_input(nir_builder *b, nir_variable *in)
70 {
71 nir_intrinsic_instr *load;
72
73 load = nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_input);
74 load->num_components = 4;
75 nir_intrinsic_set_base(load, in->data.driver_location);
76 load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0));
77 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
78 nir_builder_instr_insert(b, &load->instr);
79
80 return &load->dest.ssa;
81 }
82
83 static int
84 setup_inputs(lower_2side_state *state)
85 {
86 int maxloc = -1;
87
88 /* find color/face inputs: */
89 nir_foreach_variable(var, &state->shader->inputs) {
90 int loc = var->data.driver_location;
91
92 /* keep track of last used driver-location.. we'll be
93 * appending BCLr/FACE after last existing input:
94 */
95 maxloc = MAX2(maxloc, loc);
96
97 switch (var->data.location) {
98 case VARYING_SLOT_COL0:
99 case VARYING_SLOT_COL1:
100 assert(state->colors_count < ARRAY_SIZE(state->colors));
101 state->colors[state->colors_count].front = var;
102 state->colors_count++;
103 break;
104 case VARYING_SLOT_FACE:
105 state->face = var;
106 break;
107 }
108 }
109
110 /* if we don't have any color inputs, nothing to do: */
111 if (state->colors_count == 0)
112 return -1;
113
114 /* if we don't already have one, insert a FACE input: */
115 if (!state->face) {
116 state->face = create_input(state->shader, ++maxloc, VARYING_SLOT_FACE);
117 state->face->data.interpolation = INTERP_QUALIFIER_FLAT;
118 }
119
120 /* add required back-face color inputs: */
121 for (int i = 0; i < state->colors_count; i++) {
122 gl_varying_slot slot;
123
124 if (state->colors[i].front->data.location == VARYING_SLOT_COL0)
125 slot = VARYING_SLOT_BFC0;
126 else
127 slot = VARYING_SLOT_BFC1;
128
129 state->colors[i].back = create_input(state->shader, ++maxloc, slot);
130 }
131
132 return 0;
133 }
134
135 static bool
136 nir_lower_two_sided_color_block(nir_block *block, void *void_state)
137 {
138 lower_2side_state *state = void_state;
139 nir_builder *b = &state->b;
140
141 nir_foreach_instr_safe(instr, block) {
142 if (instr->type != nir_instr_type_intrinsic)
143 continue;
144
145 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
146
147 if (intr->intrinsic != nir_intrinsic_load_input)
148 continue;
149
150 int idx;
151 for (idx = 0; idx < state->colors_count; idx++) {
152 unsigned drvloc =
153 state->colors[idx].front->data.driver_location;
154 if (nir_intrinsic_base(intr) == drvloc) {
155 assert(nir_src_as_const_value(intr->src[0]));
156 break;
157 }
158 }
159
160 if (idx == state->colors_count)
161 continue;
162
163 /* replace load_input(COLn) with
164 * bcsel(load_input(FACE), load_input(COLn), load_input(BFCn))
165 */
166 b->cursor = nir_before_instr(&intr->instr);
167 nir_ssa_def *face = nir_channel(b, load_input(b, state->face), 0);
168 nir_ssa_def *front = load_input(b, state->colors[idx].front);
169 nir_ssa_def *back = load_input(b, state->colors[idx].back);
170 nir_ssa_def *cond = nir_flt(b, face, nir_imm_float(b, 0.0));
171 nir_ssa_def *color = nir_bcsel(b, cond, back, front);
172
173 assert(intr->dest.is_ssa);
174 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(color));
175 }
176
177 return true;
178 }
179
180 static void
181 nir_lower_two_sided_color_impl(nir_function_impl *impl,
182 lower_2side_state *state)
183 {
184 nir_builder *b = &state->b;
185
186 nir_builder_init(b, impl);
187
188 nir_foreach_block_call(impl, nir_lower_two_sided_color_block, state);
189
190 nir_metadata_preserve(impl, nir_metadata_block_index |
191 nir_metadata_dominance);
192 }
193
194 void
195 nir_lower_two_sided_color(nir_shader *shader)
196 {
197 lower_2side_state state = {
198 .shader = shader,
199 };
200
201 if (shader->stage != MESA_SHADER_FRAGMENT)
202 return;
203
204 if (setup_inputs(&state) != 0)
205 return;
206
207 nir_foreach_function(function, shader) {
208 if (function->impl)
209 nir_lower_two_sided_color_impl(function->impl, &state);
210 }
211
212 }