nir: Fix fddy swizzles in nir_lower_wpos_ytransform().
[mesa.git] / src / compiler / nir / nir_lower_wpos_ytransform.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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "program/prog_instruction.h"
27
28 /* Lower gl_FragCoord (and fddy) to account for driver's requested coordinate-
29 * origin and pixel-center vs. shader. If transformation is required, a
30 * gl_FbWposYTransform uniform is inserted (with the specified state-slots)
31 * and additional instructions are inserted to transform gl_FragCoord (and
32 * fddy src arg).
33 *
34 * This is based on the logic in emit_wpos()/emit_wpos_adjustment() in TGSI
35 * compiler.
36 *
37 * Run before nir_lower_io.
38 */
39
40 typedef struct {
41 const nir_lower_wpos_ytransform_options *options;
42 nir_shader *shader;
43 nir_builder b;
44 nir_variable *transform;
45 } lower_wpos_ytransform_state;
46
47 static nir_ssa_def *
48 get_transform(lower_wpos_ytransform_state *state)
49 {
50 if (state->transform == NULL) {
51 /* NOTE: name must be prefixed w/ "gl_" to trigger slot based
52 * special handling in uniform setup:
53 */
54 nir_variable *var = nir_variable_create(state->shader,
55 nir_var_uniform,
56 glsl_vec4_type(),
57 "gl_FbWposYTransform");
58
59 var->num_state_slots = 1;
60 var->state_slots = ralloc_array(var, nir_state_slot, 1);
61 var->state_slots[0].swizzle = SWIZZLE_XYZW;
62 memcpy(var->state_slots[0].tokens, state->options->state_tokens,
63 sizeof(var->state_slots[0].tokens));
64
65 state->transform = var;
66 }
67 return nir_load_var(&state->b, state->transform);
68 }
69
70 /* NIR equiv of TGSI CMP instruction: */
71 static nir_ssa_def *
72 nir_cmp(nir_builder *b, nir_ssa_def *src0, nir_ssa_def *src1, nir_ssa_def *src2)
73 {
74 return nir_bcsel(b, nir_flt(b, src0, nir_imm_float(b, 0.0)), src1, src2);
75 }
76
77 /* see emit_wpos_adjustment() in st_mesa_to_tgsi.c */
78 static void
79 emit_wpos_adjustment(lower_wpos_ytransform_state *state,
80 nir_intrinsic_instr *intr,
81 bool invert, float adjX, float adjY[2])
82 {
83 nir_builder *b = &state->b;
84 nir_variable *fragcoord = intr->variables[0]->var;
85 nir_ssa_def *wpostrans, *wpos_temp, *wpos_temp_y, *wpos_input;
86
87 assert(intr->dest.is_ssa);
88
89 b->cursor = nir_before_instr(&intr->instr);
90
91 wpostrans = get_transform(state);
92 wpos_input = nir_load_var(b, fragcoord);
93
94 /* First, apply the coordinate shift: */
95 if (adjX || adjY[0] || adjY[1]) {
96 if (adjY[0] != adjY[1]) {
97 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
98 * depending on whether inversion is actually going to be applied
99 * or not, which is determined by testing against the inversion
100 * state variable used below, which will be either +1 or -1.
101 */
102 nir_ssa_def *adj_temp;
103
104 adj_temp = nir_cmp(b,
105 nir_channel(b, wpostrans, invert ? 2 : 0),
106 nir_imm_vec4(b, adjX, adjY[0], 0.0f, 0.0f),
107 nir_imm_vec4(b, adjX, adjY[1], 0.0f, 0.0f));
108
109 wpos_temp = nir_fadd(b, wpos_input, adj_temp);
110 } else {
111 wpos_temp = nir_fadd(b,
112 wpos_input,
113 nir_imm_vec4(b, adjX, adjY[0], 0.0f, 0.0f));
114 }
115 wpos_input = wpos_temp;
116 } else {
117 /* MOV wpos_temp, input[wpos]
118 */
119 wpos_temp = wpos_input;
120 }
121
122 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
123 * inversion/identity, or the other way around if we're drawing to an FBO.
124 */
125 if (invert) {
126 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
127 */
128 wpos_temp_y = nir_ffma(b,
129 nir_channel(b, wpos_temp, 1),
130 nir_channel(b, wpostrans, 0),
131 nir_channel(b, wpostrans, 1));
132 } else {
133 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
134 */
135 wpos_temp_y = nir_ffma(b,
136 nir_channel(b, wpos_temp, 1),
137 nir_channel(b, wpostrans, 2),
138 nir_channel(b, wpostrans, 3));
139 }
140
141 wpos_temp = nir_vec4(b,
142 nir_channel(b, wpos_temp, 0),
143 wpos_temp_y,
144 nir_channel(b, wpos_temp, 2),
145 nir_channel(b, wpos_temp, 3));
146
147 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(wpos_temp));
148 }
149
150 static void
151 lower_fragcoord(lower_wpos_ytransform_state *state, nir_intrinsic_instr *intr)
152 {
153 const nir_lower_wpos_ytransform_options *options = state->options;
154 nir_variable *fragcoord = intr->variables[0]->var;
155 float adjX = 0.0f;
156 float adjY[2] = { 0.0f, 0.0f };
157 bool invert = false;
158
159 /* Based on logic in emit_wpos():
160 *
161 * Query the pixel center conventions supported by the pipe driver and set
162 * adjX, adjY to help out if it cannot handle the requested one internally.
163 *
164 * The bias of the y-coordinate depends on whether y-inversion takes place
165 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
166 * drawing to an FBO (causes additional inversion), and whether the the pipe
167 * driver origin and the requested origin differ (the latter condition is
168 * stored in the 'invert' variable).
169 *
170 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
171 *
172 * center shift only:
173 * i -> h: +0.5
174 * h -> i: -0.5
175 *
176 * inversion only:
177 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
178 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
179 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
180 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
181 *
182 * inversion and center shift:
183 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
184 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
185 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
186 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
187 */
188
189 if (fragcoord->data.origin_upper_left) {
190 /* Fragment shader wants origin in upper-left */
191 if (options->fs_coord_origin_upper_left) {
192 /* the driver supports upper-left origin */
193 } else if (options->fs_coord_origin_lower_left) {
194 /* the driver supports lower-left origin, need to invert Y */
195 invert = true;
196 } else {
197 unreachable("invalid options");
198 }
199 } else {
200 /* Fragment shader wants origin in lower-left */
201 if (options->fs_coord_origin_lower_left) {
202 /* the driver supports lower-left origin */
203 } else if (options->fs_coord_origin_upper_left) {
204 /* the driver supports upper-left origin, need to invert Y */
205 invert = true;
206 } else {
207 unreachable("invalid options");
208 }
209 }
210
211 if (fragcoord->data.pixel_center_integer) {
212 /* Fragment shader wants pixel center integer */
213 if (options->fs_coord_pixel_center_integer) {
214 /* the driver supports pixel center integer */
215 adjY[1] = 1.0f;
216 } else if (options->fs_coord_pixel_center_half_integer) {
217 /* the driver supports pixel center half integer, need to bias X,Y */
218 adjX = -0.5f;
219 adjY[0] = -0.5f;
220 adjY[1] = 0.5f;
221 } else {
222 unreachable("invalid options");
223 }
224 } else {
225 /* Fragment shader wants pixel center half integer */
226 if (options->fs_coord_pixel_center_half_integer) {
227 /* the driver supports pixel center half integer */
228 } else if (options->fs_coord_pixel_center_integer) {
229 /* the driver supports pixel center integer, need to bias X,Y */
230 adjX = adjY[0] = adjY[1] = 0.5f;
231 } else {
232 unreachable("invalid options");
233 }
234 }
235
236 emit_wpos_adjustment(state, intr, invert, adjX, adjY);
237 }
238
239 /* turns 'fddy(p)' into 'fddy(fmul(p, transform.x))' */
240 static void
241 lower_fddy(lower_wpos_ytransform_state *state, nir_alu_instr *fddy)
242 {
243 nir_builder *b = &state->b;
244 nir_ssa_def *p, *pt, *trans;
245
246 b->cursor = nir_before_instr(&fddy->instr);
247
248 p = nir_ssa_for_alu_src(b, fddy, 0);
249 trans = get_transform(state);
250 pt = nir_fmul(b, p, nir_channel(b, trans, 0));
251
252 nir_instr_rewrite_src(&fddy->instr,
253 &fddy->src[0].src,
254 nir_src_for_ssa(pt));
255
256 for (unsigned i = 0; i < 4; i++)
257 fddy->src[0].swizzle[i] = MIN2(i, pt->num_components - 1);
258 }
259
260 static bool
261 lower_wpos_ytransform_block(lower_wpos_ytransform_state *state, nir_block *block)
262 {
263 nir_foreach_instr_safe(instr, block) {
264 if (instr->type == nir_instr_type_intrinsic) {
265 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
266 if (intr->intrinsic == nir_intrinsic_load_var) {
267 nir_deref_var *dvar = intr->variables[0];
268 nir_variable *var = dvar->var;
269
270 if (strcmp(var->name, "gl_FragCoord") == 0) {
271 /* gl_FragCoord should not have array/struct deref's: */
272 assert(dvar->deref.child == NULL);
273 lower_fragcoord(state, intr);
274 }
275 }
276 } else if (instr->type == nir_instr_type_alu) {
277 nir_alu_instr *alu = nir_instr_as_alu(instr);
278 if (alu->op == nir_op_fddy)
279 lower_fddy(state, alu);
280 }
281 }
282
283 return true;
284 }
285
286 static void
287 lower_wpos_ytransform_impl(lower_wpos_ytransform_state *state, nir_function_impl *impl)
288 {
289 nir_builder_init(&state->b, impl);
290
291 nir_foreach_block(block, impl) {
292 lower_wpos_ytransform_block(state, block);
293 }
294 nir_metadata_preserve(impl, nir_metadata_block_index |
295 nir_metadata_dominance);
296 }
297
298 bool
299 nir_lower_wpos_ytransform(nir_shader *shader,
300 const nir_lower_wpos_ytransform_options *options)
301 {
302 lower_wpos_ytransform_state state = {
303 .options = options,
304 .shader = shader,
305 };
306
307 assert(shader->stage == MESA_SHADER_FRAGMENT);
308
309 nir_foreach_function(function, shader) {
310 if (function->impl)
311 lower_wpos_ytransform_impl(&state, function->impl);
312 }
313
314 return state.transform != NULL;
315 }