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