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
37 typedef struct {
38 const nir_lower_wpos_ytransform_options *options;
39 nir_shader *shader;
40 nir_builder b;
41 nir_variable *transform;
42 nir_def *load;
43 } lower_wpos_ytransform_state;
44
45 static nir_def *
get_transform(lower_wpos_ytransform_state * state)46 get_transform(lower_wpos_ytransform_state *state)
47 {
48 if (state->transform == NULL) {
49 /* NOTE: name must be prefixed w/ "gl_" to trigger slot based
50 * special handling in uniform setup:
51 */
52 nir_variable *var = nir_state_variable_create(state->shader,
53 glsl_vec4_type(),
54 "gl_FbWposYTransform",
55 state->options->state_tokens);
56
57 var->data.how_declared = nir_var_hidden;
58 state->transform = var;
59 state->b.cursor = nir_before_impl(nir_shader_get_entrypoint(state->b.shader));
60 state->load = nir_load_var(&state->b, state->transform);
61 }
62 return state->load;
63 }
64
65 /* NIR equiv of TGSI CMP instruction: */
66 static nir_def *
nir_cmp(nir_builder * b,nir_def * src0,nir_def * src1,nir_def * src2)67 nir_cmp(nir_builder *b, nir_def *src0, nir_def *src1, nir_def *src2)
68 {
69 return nir_bcsel(b, nir_flt_imm(b, src0, 0.0), src1, src2);
70 }
71
72 /* see emit_wpos_adjustment() in st_mesa_to_tgsi.c */
73 static void
emit_wpos_adjustment(lower_wpos_ytransform_state * state,nir_intrinsic_instr * intr,bool invert,float adjX,float adjY[2])74 emit_wpos_adjustment(lower_wpos_ytransform_state *state,
75 nir_intrinsic_instr *intr, bool invert,
76 float adjX, float adjY[2])
77 {
78 nir_builder *b = &state->b;
79 nir_def *wpos_temp_x = NULL, *wpos_temp_y = NULL, *wpos_temp, *wpos_input[4] = {NULL};
80 nir_def *wpostrans = get_transform(state);
81
82 unsigned c = 0;
83 const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
84 if (info->index_map[NIR_INTRINSIC_COMPONENT]) {
85 c = nir_intrinsic_component(intr);
86 /* this pass only alters the first two components */
87 if (c > 1)
88 return;
89 }
90
91 b->cursor = nir_after_instr(&intr->instr);
92 for (unsigned i = 0; i < intr->num_components; i++)
93 wpos_input[i + c] = nir_channel(b, &intr->def, i);
94
95 /* First, apply the coordinate shift: */
96 if (adjX || adjY[0] || adjY[1]) {
97 if (wpos_input[0])
98 wpos_temp_x = nir_fadd(b, wpos_input[0], nir_imm_float(b, adjX));
99 if (wpos_input[1] && adjY[0] != adjY[1]) {
100 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
101 * depending on whether inversion is actually going to be applied
102 * or not, which is determined by testing against the inversion
103 * state variable used below, which will be either +1 or -1.
104 */
105 nir_def *adj_temp = nir_cmp(b,
106 nir_channel(b, wpostrans, invert ? 2 : 0),
107 nir_imm_float(b, adjY[0]),
108 nir_imm_float(b, adjY[1]));
109
110 wpos_temp_y = nir_fadd(b, wpos_input[1], adj_temp);
111 } else if (wpos_input[1]) {
112 wpos_temp_y = nir_fadd(b, wpos_input[1], nir_imm_float(b, adjY[0]));
113 }
114 } else {
115 /* MOV wpos_temp, input[wpos]
116 */
117 wpos_temp_x = wpos_input[0];
118 wpos_temp_y = wpos_input[1];
119 }
120
121 if (wpos_temp_y) {
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 /* wpos_temp.y = wpos_temp * wpostrans.xxxx + wpostrans.yyyy */
127 wpos_temp_y = nir_fadd(b, nir_fmul(b, wpos_temp_y, nir_channel(b, wpostrans, 0)),
128 nir_channel(b, wpostrans, 1));
129 } else {
130 /* wpos_temp.y = wpos_temp * wpostrans.zzzz + wpostrans.wwww */
131 wpos_temp_y = nir_fadd(b, nir_fmul(b, wpos_temp_y, nir_channel(b, wpostrans, 2)),
132 nir_channel(b, wpostrans, 3));
133 }
134 }
135
136 wpos_input[0] = wpos_temp_x;
137 wpos_input[1] = wpos_temp_y;
138 wpos_temp = intr->num_components > 1 ?
139 nir_vec(b, &wpos_input[c], intr->num_components) :
140 wpos_input[c];
141
142 nir_def_rewrite_uses_after(&intr->def,
143 wpos_temp,
144 wpos_temp->parent_instr);
145 }
146
147 static void
lower_fragcoord(lower_wpos_ytransform_state * state,nir_intrinsic_instr * intr)148 lower_fragcoord(lower_wpos_ytransform_state *state, nir_intrinsic_instr *intr)
149 {
150 const nir_lower_wpos_ytransform_options *options = state->options;
151 float adjX = 0.0f;
152 float adjY[2] = { 0.0f, 0.0f };
153 bool invert = false;
154
155 /* Based on logic in emit_wpos():
156 *
157 * Query the pixel center conventions supported by the pipe driver and set
158 * adjX, adjY to help out if it cannot handle the requested one internally.
159 *
160 * The bias of the y-coordinate depends on whether y-inversion takes place
161 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
162 * drawing to an FBO (causes additional inversion), and whether the pipe
163 * driver origin and the requested origin differ (the latter condition is
164 * stored in the 'invert' variable).
165 *
166 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
167 *
168 * center shift only:
169 * i -> h: +0.5
170 * h -> i: -0.5
171 *
172 * inversion only:
173 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
174 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
175 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
176 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
177 *
178 * inversion and center shift:
179 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
180 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
181 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
182 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
183 */
184
185 if (state->shader->info.fs.origin_upper_left) {
186 /* Fragment shader wants origin in upper-left */
187 if (options->fs_coord_origin_upper_left) {
188 /* the driver supports upper-left origin */
189 } else if (options->fs_coord_origin_lower_left) {
190 /* the driver supports lower-left origin, need to invert Y */
191 invert = true;
192 } else {
193 unreachable("invalid options");
194 }
195 } else {
196 /* Fragment shader wants origin in lower-left */
197 if (options->fs_coord_origin_lower_left) {
198 /* the driver supports lower-left origin */
199 } else if (options->fs_coord_origin_upper_left) {
200 /* the driver supports upper-left origin, need to invert Y */
201 invert = true;
202 } else {
203 unreachable("invalid options");
204 }
205 }
206
207 if (state->shader->info.fs.pixel_center_integer) {
208 /* Fragment shader wants pixel center integer */
209 if (options->fs_coord_pixel_center_integer) {
210 /* the driver supports pixel center integer */
211 adjY[1] = 1.0f;
212 } else if (options->fs_coord_pixel_center_half_integer) {
213 /* the driver supports pixel center half integer, need to bias X,Y */
214 adjX = -0.5f;
215 adjY[0] = -0.5f;
216 adjY[1] = 0.5f;
217 } else {
218 unreachable("invalid options");
219 }
220 } else {
221 /* Fragment shader wants pixel center half integer */
222 if (options->fs_coord_pixel_center_half_integer) {
223 /* the driver supports pixel center half integer */
224 } else if (options->fs_coord_pixel_center_integer) {
225 /* the driver supports pixel center integer, need to bias X,Y */
226 adjX = adjY[0] = adjY[1] = 0.5f;
227 } else {
228 unreachable("invalid options");
229 }
230 }
231
232 emit_wpos_adjustment(state, intr, invert, adjX, adjY);
233 }
234
235 /* turns 'fddy(p)' into 'fddy(fmul(p, transform.x))' */
236 static void
lower_fddy(lower_wpos_ytransform_state * state,nir_alu_instr * fddy)237 lower_fddy(lower_wpos_ytransform_state *state, nir_alu_instr *fddy)
238 {
239 nir_builder *b = &state->b;
240 nir_def *p, *pt, *trans;
241 nir_def *wpostrans = get_transform(state);
242
243 b->cursor = nir_before_instr(&fddy->instr);
244
245 p = nir_ssa_for_alu_src(b, fddy, 0);
246 trans = nir_channel(b, wpostrans, 0);
247 if (p->bit_size == 16)
248 trans = nir_f2f16(b, trans);
249
250 pt = nir_fmul(b, p, trans);
251
252 nir_src_rewrite(&fddy->src[0].src, pt);
253
254 for (unsigned i = 0; i < 4; i++)
255 fddy->src[0].swizzle[i] = MIN2(i, pt->num_components - 1);
256 }
257
258 static void
lower_ddy(lower_wpos_ytransform_state * state,nir_intrinsic_instr * ddy)259 lower_ddy(lower_wpos_ytransform_state *state, nir_intrinsic_instr *ddy)
260 {
261 nir_builder *b = &state->b;
262 nir_def *wpostrans = get_transform(state);
263
264 b->cursor = nir_before_instr(&ddy->instr);
265
266 nir_def *p = ddy->src[0].ssa;
267 nir_def *trans = nir_f2fN(b, nir_channel(b, wpostrans, 0), p->bit_size);
268 nir_def *pt = nir_fmul(b, p, trans);
269
270 nir_src_rewrite(&ddy->src[0], pt);
271 }
272
273 /* Multiply interp_deref_at_offset's or load_barycentric_at_offset's offset
274 * by transform.x to flip it.
275 */
276 static void
lower_interp_deref_or_load_baryc_at_offset(lower_wpos_ytransform_state * state,nir_intrinsic_instr * intr,unsigned offset_src)277 lower_interp_deref_or_load_baryc_at_offset(lower_wpos_ytransform_state *state,
278 nir_intrinsic_instr *intr,
279 unsigned offset_src)
280 {
281 nir_builder *b = &state->b;
282 nir_def *offset;
283 nir_def *flip_y;
284 nir_def *wpostrans = get_transform(state);
285
286 b->cursor = nir_before_instr(&intr->instr);
287
288 offset = intr->src[offset_src].ssa;
289 flip_y = nir_fmul(b, nir_channel(b, offset, 1),
290 nir_channel(b, wpostrans, 0));
291 nir_src_rewrite(&intr->src[offset_src],
292 nir_vec2(b, nir_channel(b, offset, 0), flip_y));
293 }
294
295 static void
lower_load_sample_pos(lower_wpos_ytransform_state * state,nir_intrinsic_instr * intr)296 lower_load_sample_pos(lower_wpos_ytransform_state *state,
297 nir_intrinsic_instr *intr)
298 {
299 nir_builder *b = &state->b;
300 nir_def *wpostrans = get_transform(state);
301 b->cursor = nir_after_instr(&intr->instr);
302
303 nir_def *pos = &intr->def;
304 nir_def *scale = nir_channel(b, wpostrans, 0);
305 nir_def *neg_scale = nir_channel(b, wpostrans, 2);
306 /* Either y or 1-y for scale equal to 1 or -1 respectively. */
307 nir_def *flipped_y =
308 nir_fadd(b, nir_fmax(b, neg_scale, nir_imm_float(b, 0.0)),
309 nir_fmul(b, nir_channel(b, pos, 1), scale));
310 nir_def *flipped_pos = nir_vec2(b, nir_channel(b, pos, 0), flipped_y);
311
312 nir_def_rewrite_uses_after(&intr->def, flipped_pos,
313 flipped_pos->parent_instr);
314 }
315
316 static bool
lower_wpos_ytransform_instr(nir_builder * b,nir_instr * instr,void * data)317 lower_wpos_ytransform_instr(nir_builder *b, nir_instr *instr,
318 void *data)
319 {
320 lower_wpos_ytransform_state *state = data;
321 state->b = *b;
322
323 if (instr->type == nir_instr_type_intrinsic) {
324 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
325 if (intr->intrinsic == nir_intrinsic_load_deref) {
326 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
327 nir_variable *var = nir_deref_instr_get_variable(deref);
328 if ((var->data.mode == nir_var_shader_in &&
329 var->data.location == VARYING_SLOT_POS) ||
330 (var->data.mode == nir_var_system_value &&
331 var->data.location == SYSTEM_VALUE_FRAG_COORD)) {
332 /* gl_FragCoord should not have array/struct derefs: */
333 lower_fragcoord(state, intr);
334 } else if (var->data.mode == nir_var_system_value &&
335 var->data.location == SYSTEM_VALUE_SAMPLE_POS) {
336 lower_load_sample_pos(state, intr);
337 }
338 } else if (intr->intrinsic == nir_intrinsic_load_interpolated_input) {
339 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
340 if (sem.location == VARYING_SLOT_POS)
341 lower_fragcoord(state, intr);
342 } else if (intr->intrinsic == nir_intrinsic_load_frag_coord) {
343 lower_fragcoord(state, intr);
344 } else if (intr->intrinsic == nir_intrinsic_load_sample_pos) {
345 lower_load_sample_pos(state, intr);
346 } else if (intr->intrinsic == nir_intrinsic_interp_deref_at_offset) {
347 lower_interp_deref_or_load_baryc_at_offset(state, intr, 1);
348 } else if (intr->intrinsic == nir_intrinsic_load_barycentric_at_offset) {
349 lower_interp_deref_or_load_baryc_at_offset(state, intr, 0);
350 } else if (intr->intrinsic == nir_intrinsic_ddy ||
351 intr->intrinsic == nir_intrinsic_ddy_fine ||
352 intr->intrinsic == nir_intrinsic_ddy_coarse) {
353 lower_ddy(state, intr);
354 }
355 } else if (instr->type == nir_instr_type_alu) {
356 nir_alu_instr *alu = nir_instr_as_alu(instr);
357 if (alu->op == nir_op_fddy ||
358 alu->op == nir_op_fddy_fine ||
359 alu->op == nir_op_fddy_coarse)
360 lower_fddy(state, alu);
361 }
362
363 return state->transform != NULL;
364 }
365
366 bool
nir_lower_wpos_ytransform(nir_shader * shader,const nir_lower_wpos_ytransform_options * options)367 nir_lower_wpos_ytransform(nir_shader *shader,
368 const nir_lower_wpos_ytransform_options *options)
369 {
370 lower_wpos_ytransform_state state = {
371 .options = options,
372 .shader = shader,
373 };
374
375 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
376
377 return nir_shader_instructions_pass(shader,
378 lower_wpos_ytransform_instr,
379 nir_metadata_control_flow,
380 &state);
381 }
382