nir: Support deref instructions in 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, nir_variable *fragcoord,
81 bool invert, float adjX, float adjY[2])
82 {
83 nir_builder *b = &state->b;
84 nir_ssa_def *wpostrans, *wpos_temp, *wpos_temp_y, *wpos_input;
85
86 assert(intr->dest.is_ssa);
87
88 b->cursor = nir_before_instr(&intr->instr);
89
90 wpostrans = get_transform(state);
91 wpos_input = nir_load_var(b, fragcoord);
92
93 /* First, apply the coordinate shift: */
94 if (adjX || adjY[0] || adjY[1]) {
95 if (adjY[0] != adjY[1]) {
96 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
97 * depending on whether inversion is actually going to be applied
98 * or not, which is determined by testing against the inversion
99 * state variable used below, which will be either +1 or -1.
100 */
101 nir_ssa_def *adj_temp;
102
103 adj_temp = nir_cmp(b,
104 nir_channel(b, wpostrans, invert ? 2 : 0),
105 nir_imm_vec4(b, adjX, adjY[0], 0.0f, 0.0f),
106 nir_imm_vec4(b, adjX, adjY[1], 0.0f, 0.0f));
107
108 wpos_temp = nir_fadd(b, wpos_input, adj_temp);
109 } else {
110 wpos_temp = nir_fadd(b,
111 wpos_input,
112 nir_imm_vec4(b, adjX, adjY[0], 0.0f, 0.0f));
113 }
114 wpos_input = wpos_temp;
115 } else {
116 /* MOV wpos_temp, input[wpos]
117 */
118 wpos_temp = wpos_input;
119 }
120
121 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
122 * inversion/identity, or the other way around if we're drawing to an FBO.
123 */
124 if (invert) {
125 /* wpos_temp.y = wpos_input * wpostrans.xxxx + wpostrans.yyyy */
126 wpos_temp_y = nir_fadd(b, nir_fmul(b, nir_channel(b, wpos_temp, 1),
127 nir_channel(b, wpostrans, 0)),
128 nir_channel(b, wpostrans, 1));
129 } else {
130 /* wpos_temp.y = wpos_input * wpostrans.zzzz + wpostrans.wwww */
131 wpos_temp_y = nir_fadd(b, nir_fmul(b, nir_channel(b, wpos_temp, 1),
132 nir_channel(b, wpostrans, 2)),
133 nir_channel(b, wpostrans, 3));
134 }
135
136 wpos_temp = nir_vec4(b,
137 nir_channel(b, wpos_temp, 0),
138 wpos_temp_y,
139 nir_channel(b, wpos_temp, 2),
140 nir_channel(b, wpos_temp, 3));
141
142 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(wpos_temp));
143 }
144
145 static void
146 lower_fragcoord(lower_wpos_ytransform_state *state,
147 nir_intrinsic_instr *intr, nir_variable *fragcoord)
148 {
149 const nir_lower_wpos_ytransform_options *options = state->options;
150 float adjX = 0.0f;
151 float adjY[2] = { 0.0f, 0.0f };
152 bool invert = false;
153
154 /* Based on logic in emit_wpos():
155 *
156 * Query the pixel center conventions supported by the pipe driver and set
157 * adjX, adjY to help out if it cannot handle the requested one internally.
158 *
159 * The bias of the y-coordinate depends on whether y-inversion takes place
160 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
161 * drawing to an FBO (causes additional inversion), and whether the pipe
162 * driver origin and the requested origin differ (the latter condition is
163 * stored in the 'invert' variable).
164 *
165 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
166 *
167 * center shift only:
168 * i -> h: +0.5
169 * h -> i: -0.5
170 *
171 * inversion only:
172 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
173 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
174 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
175 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
176 *
177 * inversion and center shift:
178 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
179 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
180 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
181 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
182 */
183
184 if (fragcoord->data.origin_upper_left) {
185 /* Fragment shader wants origin in upper-left */
186 if (options->fs_coord_origin_upper_left) {
187 /* the driver supports upper-left origin */
188 } else if (options->fs_coord_origin_lower_left) {
189 /* the driver supports lower-left origin, need to invert Y */
190 invert = true;
191 } else {
192 unreachable("invalid options");
193 }
194 } else {
195 /* Fragment shader wants origin in lower-left */
196 if (options->fs_coord_origin_lower_left) {
197 /* the driver supports lower-left origin */
198 } else if (options->fs_coord_origin_upper_left) {
199 /* the driver supports upper-left origin, need to invert Y */
200 invert = true;
201 } else {
202 unreachable("invalid options");
203 }
204 }
205
206 if (fragcoord->data.pixel_center_integer) {
207 /* Fragment shader wants pixel center integer */
208 if (options->fs_coord_pixel_center_integer) {
209 /* the driver supports pixel center integer */
210 adjY[1] = 1.0f;
211 } else if (options->fs_coord_pixel_center_half_integer) {
212 /* the driver supports pixel center half integer, need to bias X,Y */
213 adjX = -0.5f;
214 adjY[0] = -0.5f;
215 adjY[1] = 0.5f;
216 } else {
217 unreachable("invalid options");
218 }
219 } else {
220 /* Fragment shader wants pixel center half integer */
221 if (options->fs_coord_pixel_center_half_integer) {
222 /* the driver supports pixel center half integer */
223 } else if (options->fs_coord_pixel_center_integer) {
224 /* the driver supports pixel center integer, need to bias X,Y */
225 adjX = adjY[0] = adjY[1] = 0.5f;
226 } else {
227 unreachable("invalid options");
228 }
229 }
230
231 emit_wpos_adjustment(state, intr, fragcoord, invert, adjX, adjY);
232 }
233
234 /* turns 'fddy(p)' into 'fddy(fmul(p, transform.x))' */
235 static void
236 lower_fddy(lower_wpos_ytransform_state *state, nir_alu_instr *fddy)
237 {
238 nir_builder *b = &state->b;
239 nir_ssa_def *p, *pt, *trans;
240
241 b->cursor = nir_before_instr(&fddy->instr);
242
243 p = nir_ssa_for_alu_src(b, fddy, 0);
244 trans = get_transform(state);
245 pt = nir_fmul(b, p, nir_channel(b, trans, 0));
246
247 nir_instr_rewrite_src(&fddy->instr,
248 &fddy->src[0].src,
249 nir_src_for_ssa(pt));
250
251 for (unsigned i = 0; i < 4; i++)
252 fddy->src[0].swizzle[i] = MIN2(i, pt->num_components - 1);
253 }
254
255 /* Multiply interp_deref_at_offset's offset by transform.x to flip it. */
256 static void
257 lower_interp_deref_at_offset(lower_wpos_ytransform_state *state,
258 nir_intrinsic_instr *interp)
259 {
260 nir_builder *b = &state->b;
261 nir_ssa_def *offset;
262 nir_ssa_def *flip_y;
263
264 b->cursor = nir_before_instr(&interp->instr);
265
266 offset = nir_ssa_for_src(b, interp->src[1], 2);
267 flip_y = nir_fmul(b, nir_channel(b, offset, 1),
268 nir_channel(b, get_transform(state), 0));
269 nir_instr_rewrite_src(&interp->instr, &interp->src[1],
270 nir_src_for_ssa(nir_vec2(b, nir_channel(b, offset, 0),
271 flip_y)));
272 }
273
274 static void
275 lower_interp_var_at_offset(lower_wpos_ytransform_state *state,
276 nir_intrinsic_instr *interp)
277 {
278 nir_builder *b = &state->b;
279 nir_ssa_def *offset;
280 nir_ssa_def *flip_y;
281
282 b->cursor = nir_before_instr(&interp->instr);
283
284 offset = nir_ssa_for_src(b, interp->src[0], 2);
285 flip_y = nir_fmul(b, nir_channel(b, offset, 1),
286 nir_channel(b, get_transform(state), 0));
287 nir_instr_rewrite_src(&interp->instr, &interp->src[0],
288 nir_src_for_ssa(nir_vec2(b, nir_channel(b, offset, 0),
289 flip_y)));
290 }
291
292 static void
293 lower_load_sample_pos(lower_wpos_ytransform_state *state,
294 nir_intrinsic_instr *intr)
295 {
296 nir_builder *b = &state->b;
297 b->cursor = nir_after_instr(&intr->instr);
298
299 nir_ssa_def *pos = &intr->dest.ssa;
300 nir_ssa_def *scale = nir_channel(b, get_transform(state), 0);
301 nir_ssa_def *neg_scale = nir_channel(b, get_transform(state), 2);
302 /* Either y or 1-y for scale equal to 1 or -1 respectively. */
303 nir_ssa_def *flipped_y =
304 nir_fadd(b, nir_fmax(b, neg_scale, nir_imm_float(b, 0.0)),
305 nir_fmul(b, nir_channel(b, pos, 1), scale));
306 nir_ssa_def *flipped_pos = nir_vec2(b, nir_channel(b, pos, 0), flipped_y);
307
308 nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, nir_src_for_ssa(flipped_pos),
309 flipped_pos->parent_instr);
310 }
311
312 static void
313 lower_wpos_ytransform_block(lower_wpos_ytransform_state *state, nir_block *block)
314 {
315 nir_foreach_instr_safe(instr, block) {
316 if (instr->type == nir_instr_type_intrinsic) {
317 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
318 if (intr->intrinsic == nir_intrinsic_load_deref) {
319 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
320 nir_variable *var = nir_deref_instr_get_variable(deref);
321
322 if ((var->data.mode == nir_var_shader_in &&
323 var->data.location == VARYING_SLOT_POS) ||
324 (var->data.mode == nir_var_system_value &&
325 var->data.location == SYSTEM_VALUE_FRAG_COORD)) {
326 /* gl_FragCoord should not have array/struct derefs: */
327 lower_fragcoord(state, intr, var);
328 } else if (var->data.mode == nir_var_system_value &&
329 var->data.location == SYSTEM_VALUE_SAMPLE_POS) {
330 lower_load_sample_pos(state, intr);
331 }
332 } else if (intr->intrinsic == nir_intrinsic_load_var) {
333 nir_deref_var *dvar = intr->variables[0];
334 nir_variable *var = dvar->var;
335
336 if ((var->data.mode == nir_var_shader_in &&
337 var->data.location == VARYING_SLOT_POS) ||
338 (var->data.mode == nir_var_system_value &&
339 var->data.location == SYSTEM_VALUE_FRAG_COORD)) {
340 /* gl_FragCoord should not have array/struct derefs: */
341 assert(dvar->deref.child == NULL);
342 lower_fragcoord(state, intr, var);
343 } else if (var->data.mode == nir_var_system_value &&
344 var->data.location == SYSTEM_VALUE_SAMPLE_POS) {
345 assert(dvar->deref.child == NULL);
346 lower_load_sample_pos(state, intr);
347 }
348 } else if (intr->intrinsic == nir_intrinsic_load_frag_coord) {
349 lower_fragcoord(state, intr, NULL);
350 } else if (intr->intrinsic == nir_intrinsic_load_sample_pos) {
351 lower_load_sample_pos(state, intr);
352 } else if (intr->intrinsic == nir_intrinsic_interp_deref_at_offset) {
353 lower_interp_deref_at_offset(state, intr);
354 } else if (intr->intrinsic == nir_intrinsic_interp_var_at_offset) {
355 lower_interp_var_at_offset(state, intr);
356 }
357 } else if (instr->type == nir_instr_type_alu) {
358 nir_alu_instr *alu = nir_instr_as_alu(instr);
359 if (alu->op == nir_op_fddy ||
360 alu->op == nir_op_fddy_fine ||
361 alu->op == nir_op_fddy_coarse)
362 lower_fddy(state, alu);
363 }
364 }
365 }
366
367 static void
368 lower_wpos_ytransform_impl(lower_wpos_ytransform_state *state, nir_function_impl *impl)
369 {
370 nir_builder_init(&state->b, impl);
371
372 nir_foreach_block(block, impl) {
373 lower_wpos_ytransform_block(state, block);
374 }
375 nir_metadata_preserve(impl, nir_metadata_block_index |
376 nir_metadata_dominance);
377 }
378
379 bool
380 nir_lower_wpos_ytransform(nir_shader *shader,
381 const nir_lower_wpos_ytransform_options *options)
382 {
383 lower_wpos_ytransform_state state = {
384 .options = options,
385 .shader = shader,
386 };
387
388 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
389
390 nir_foreach_function(function, shader) {
391 if (function->impl)
392 lower_wpos_ytransform_impl(&state, function->impl);
393 }
394
395 return state.transform != NULL;
396 }