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