<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 9, 2016 at 12:33 PM, Rob Clark <span dir="ltr"><<a href="mailto:robdclark@gmail.com" target="_blank">robdclark@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
<br>
Signed-off-by: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
Reviewed-by: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br>
---<br>
src/compiler/Makefile.sources | 1 +<br>
src/compiler/nir/nir.h | 11 +<br>
src/compiler/nir/nir_lower_wpos_ytransform.c | 310 +++++++++++++++++++++++++++<br>
3 files changed, 322 insertions(+)<br>
create mode 100644 src/compiler/nir/nir_lower_wpos_ytransform.c<br>
<br>
diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources<br>
index 2a52319..b542a1a 100644<br>
--- a/src/compiler/Makefile.sources<br>
+++ b/src/compiler/Makefile.sources<br>
@@ -208,6 +208,7 @@ NIR_FILES = \<br>
nir/nir_lower_vars_to_ssa.c \<br>
nir/nir_lower_var_copies.c \<br>
nir/nir_lower_vec_to_movs.c \<br>
+ nir/nir_lower_wpos_ytransform.c \<br>
nir/nir_metadata.c \<br>
nir/nir_move_vec_src_uses_to_dest.c \<br>
nir/nir_normalize_cubemap_coords.c \<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index 8a616d4..474ba63 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2374,6 +2374,17 @@ void nir_lower_two_sided_color(nir_shader *shader);<br>
<br>
void nir_lower_clamp_color_outputs(nir_shader *shader);<br>
<br>
+typedef struct nir_lower_wpos_ytransform_options {<br>
+ int state_tokens[5];<br>
+ bool fs_coord_origin_upper_left :1;<br>
+ bool fs_coord_origin_lower_left :1;<br>
+ bool fs_coord_pixel_center_integer :1;<br>
+ bool fs_coord_pixel_center_half_integer :1;<br></blockquote><div><br></div><div>Drive-by commentary: Why are we using two booleans for one boolean here? All hardware should be either lower-left or upper-left and I'm going to hazard that the other two are mutually exclusive as well. The pass certainly seems to assume so.<br><br></div><div>Let's just make it two booleans. If we come across hardware that puts the pixel center at 0.75, 0.25 then we can make fs_coord_pixel_center an enum.<br></div><div>--Jason<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+} nir_lower_wpos_ytransform_options;<br>
+<br>
+bool nir_lower_wpos_ytransform(nir_shader *shader,<br>
+ const nir_lower_wpos_ytransform_options *options);<br>
+<br>
void nir_lower_atomics(nir_shader *shader,<br>
const struct gl_shader_program *shader_program);<br>
void nir_lower_to_source_mods(nir_shader *shader);<br>
diff --git a/src/compiler/nir/nir_lower_wpos_ytransform.c b/src/compiler/nir/nir_lower_wpos_ytransform.c<br>
new file mode 100644<br>
index 0000000..1d53530<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_lower_wpos_ytransform.c<br>
@@ -0,0 +1,310 @@<br>
+/*<br>
+ * Copyright © 2015 Red Hat<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,<br>
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br>
+ * SOFTWARE.<br>
+ */<br>
+<br>
+#include "nir.h"<br>
+#include "nir_builder.h"<br>
+<br>
+/* Lower gl_FragCoord (and fddy) to account for driver's requested coordinate-<br>
+ * origin and pixel-center vs. shader. If transformation is required, a<br>
+ * gl_FbWposYTransform uniform is inserted (with the specified state-slots)<br>
+ * and additional instructions are inserted to transform gl_FragCoord (and<br>
+ * fddy src arg).<br>
+ *<br>
+ * This is based on the logic in emit_wpos()/emit_wpos_adjustment() in TGSI<br>
+ * compiler.<br>
+ *<br>
+ * Run before nir_lower_io.<br>
+ */<br>
+<br>
+typedef struct {<br>
+ const nir_lower_wpos_ytransform_options *options;<br>
+ nir_shader *shader;<br>
+ nir_builder b;<br>
+ nir_variable *transform;<br>
+} lower_wpos_ytransform_state;<br>
+<br>
+static nir_ssa_def *<br>
+get_transform(lower_wpos_ytransform_state *state)<br>
+{<br>
+ if (state->transform == NULL) {<br>
+ /* NOTE: name must be prefixed w/ "gl_" to trigger slot based<br>
+ * special handling in uniform setup:<br>
+ */<br>
+ nir_variable *var = nir_variable_create(state->shader,<br>
+ nir_var_uniform,<br>
+ glsl_vec4_type(),<br>
+ "gl_FbWposYTransform");<br>
+<br>
+ var->num_state_slots = 1;<br>
+ var->state_slots = ralloc_array(var, nir_state_slot, 1);<br>
+ memcpy(var->state_slots[0].tokens, state->options->state_tokens,<br>
+ sizeof(var->state_slots[0].tokens));<br>
+<br>
+ state->transform = var;<br>
+ }<br>
+ return nir_load_var(&state->b, state->transform);<br>
+}<br>
+<br>
+/* NIR equiv of TGSI CMP instruction: */<br>
+static nir_ssa_def *<br>
+nir_cmp(nir_builder *b, nir_ssa_def *src0, nir_ssa_def *src1, nir_ssa_def *src2)<br>
+{<br>
+ return nir_bcsel(b, nir_flt(b, src0, nir_imm_float(b, 0.0)), src1, src2);<br>
+}<br>
+<br>
+/* see emit_wpos_adjustment() in st_mesa_to_tgsi.c */<br>
+static void<br>
+emit_wpos_adjustment(lower_wpos_ytransform_state *state,<br>
+ nir_intrinsic_instr *intr,<br>
+ bool invert, float adjX, float adjY[2])<br>
+{<br>
+ nir_builder *b = &state->b;<br>
+ nir_variable *fragcoord = intr->variables[0]->var;<br>
+ nir_ssa_def *wpostrans, *wpos_temp, *wpos_temp_y, *wpos_input;<br>
+<br>
+ assert(intr->dest.is_ssa);<br>
+<br>
+ b->cursor = nir_before_instr(&intr->instr);<br>
+<br>
+ wpostrans = get_transform(state);<br>
+ wpos_input = nir_load_var(b, fragcoord);<br>
+<br>
+ /* First, apply the coordinate shift: */<br>
+ if (adjX || adjY[0] || adjY[1]) {<br>
+ if (adjY[0] != adjY[1]) {<br>
+ /* Adjust the y coordinate by adjY[1] or adjY[0] respectively<br>
+ * depending on whether inversion is actually going to be applied<br>
+ * or not, which is determined by testing against the inversion<br>
+ * state variable used below, which will be either +1 or -1.<br>
+ */<br>
+ nir_ssa_def *adj_temp;<br>
+<br>
+ adj_temp = nir_cmp(b,<br>
+ nir_channel(b, wpostrans, invert ? 2 : 0),<br>
+ nir_imm_vec4(b, adjX, adjY[0], 0.0f, 0.0f),<br>
+ nir_imm_vec4(b, adjX, adjY[1], 0.0f, 0.0f));<br>
+<br>
+ wpos_temp = nir_fadd(b, wpos_input, adj_temp);<br>
+ } else {<br>
+ wpos_temp = nir_fadd(b,<br>
+ wpos_input,<br>
+ nir_imm_vec4(b, adjX, adjY[0], 0.0f, 0.0f));<br>
+ }<br>
+ wpos_input = wpos_temp;<br>
+ } else {<br>
+ /* MOV wpos_temp, input[wpos]<br>
+ */<br>
+ wpos_temp = wpos_input;<br>
+ }<br>
+<br>
+ /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be<br>
+ * inversion/identity, or the other way around if we're drawing to an FBO.<br>
+ */<br>
+ if (invert) {<br>
+ /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy<br>
+ */<br>
+ wpos_temp_y = nir_ffma(b,<br>
+ nir_channel(b, wpos_temp, 1),<br>
+ nir_channel(b, wpostrans, 0),<br>
+ nir_channel(b, wpostrans, 1));<br>
+ } else {<br>
+ /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww<br>
+ */<br>
+ wpos_temp_y = nir_ffma(b,<br>
+ nir_channel(b, wpos_temp, 1),<br>
+ nir_channel(b, wpostrans, 2),<br>
+ nir_channel(b, wpostrans, 3));<br>
+ }<br>
+<br>
+ wpos_temp = nir_vec4(b,<br>
+ nir_channel(b, wpos_temp, 0),<br>
+ wpos_temp_y,<br>
+ nir_channel(b, wpos_temp, 2),<br>
+ nir_channel(b, wpos_temp, 3));<br>
+<br>
+ nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(wpos_temp));<br>
+}<br>
+<br>
+static void<br>
+lower_fragcoord(lower_wpos_ytransform_state *state, nir_intrinsic_instr *intr)<br>
+{<br>
+ const nir_lower_wpos_ytransform_options *options = state->options;<br>
+ nir_variable *fragcoord = intr->variables[0]->var;<br>
+ float adjX = 0.0f;<br>
+ float adjY[2] = { 0.0f, 0.0f };<br>
+ bool invert = false;<br>
+<br>
+ /* Based on logic in emit_wpos():<br>
+ *<br>
+ * Query the pixel center conventions supported by the pipe driver and set<br>
+ * adjX, adjY to help out if it cannot handle the requested one internally.<br>
+ *<br>
+ * The bias of the y-coordinate depends on whether y-inversion takes place<br>
+ * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are<br>
+ * drawing to an FBO (causes additional inversion), and whether the the pipe<br>
+ * driver origin and the requested origin differ (the latter condition is<br>
+ * stored in the 'invert' variable).<br>
+ *<br>
+ * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):<br>
+ *<br>
+ * center shift only:<br>
+ * i -> h: +0.5<br>
+ * h -> i: -0.5<br>
+ *<br>
+ * inversion only:<br>
+ * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99<br>
+ * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5<br>
+ * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0<br>
+ * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5<br>
+ *<br>
+ * inversion and center shift:<br>
+ * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5<br>
+ * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99<br>
+ * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5<br>
+ * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0<br>
+ */<br>
+<br>
+ if (fragcoord->data.origin_upper_left) {<br>
+ /* Fragment shader wants origin in upper-left */<br>
+ if (options->fs_coord_origin_upper_left) {<br>
+ /* the driver supports upper-left origin */<br>
+ } else if (options->fs_coord_origin_lower_left) {<br>
+ /* the driver supports lower-left origin, need to invert Y */<br>
+ invert = true;<br>
+ } else {<br>
+ unreachable("invalid options");<br>
+ }<br>
+ } else {<br>
+ /* Fragment shader wants origin in lower-left */<br>
+ if (options->fs_coord_origin_lower_left) {<br>
+ /* the driver supports lower-left origin */<br>
+ } else if (options->fs_coord_origin_upper_left) {<br>
+ /* the driver supports upper-left origin, need to invert Y */<br>
+ invert = true;<br>
+ } else {<br>
+ unreachable("invalid options");<br>
+ }<br>
+ }<br>
+<br>
+ if (fragcoord->data.pixel_center_integer) {<br>
+ /* Fragment shader wants pixel center integer */<br>
+ if (options->fs_coord_pixel_center_integer) {<br>
+ /* the driver supports pixel center integer */<br>
+ adjY[1] = 1.0f;<br>
+ } else if (options->fs_coord_pixel_center_half_integer) {<br>
+ /* the driver supports pixel center half integer, need to bias X,Y */<br>
+ adjX = -0.5f;<br>
+ adjY[0] = -0.5f;<br>
+ adjY[1] = 0.5f;<br>
+ } else {<br>
+ unreachable("invalid options");<br>
+ }<br>
+ } else {<br>
+ /* Fragment shader wants pixel center half integer */<br>
+ if (options->fs_coord_pixel_center_half_integer) {<br>
+ /* the driver supports pixel center half integer */<br>
+ } else if (options->fs_coord_pixel_center_integer) {<br>
+ /* the driver supports pixel center integer, need to bias X,Y */<br>
+ adjX = adjY[0] = adjY[1] = 0.5f;<br>
+ } else {<br>
+ unreachable("invalid options");<br>
+ }<br>
+ }<br>
+<br>
+ emit_wpos_adjustment(state, intr, invert, adjX, adjY);<br>
+}<br>
+<br>
+/* turns 'fddy(p)' into 'fddy(fmul(p, transform.x))' */<br>
+static void<br>
+lower_fddy(lower_wpos_ytransform_state *state, nir_alu_instr *fddy)<br>
+{<br>
+ nir_builder *b = &state->b;<br>
+ nir_ssa_def *p, *pt, *trans;<br>
+<br>
+ b->cursor = nir_before_instr(&fddy->instr);<br>
+<br>
+ p = nir_ssa_for_alu_src(b, fddy, 0);<br>
+ trans = get_transform(state);<br>
+ pt = nir_fmul(b, p, nir_channel(b, trans, 0));<br>
+<br>
+ nir_instr_rewrite_src(&fddy->instr,<br>
+ &fddy->src[0].src,<br>
+ nir_src_for_ssa(pt));<br>
+}<br>
+<br>
+static bool<br>
+lower_wpos_ytransform_block(lower_wpos_ytransform_state *state, nir_block *block)<br>
+{<br>
+ nir_foreach_instr_safe(instr, block) {<br>
+ if (instr->type == nir_instr_type_intrinsic) {<br>
+ nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);<br>
+ if (intr->intrinsic == nir_intrinsic_load_var) {<br>
+ nir_deref_var *dvar = intr->variables[0];<br>
+ nir_variable *var = dvar->var;<br>
+<br>
+ if (strcmp(var->name, "gl_FragCoord") == 0) {<br>
+ /* gl_FragCoord should not have array/struct deref's: */<br>
+ assert(dvar->deref.child == NULL);<br>
+ lower_fragcoord(state, intr);<br>
+ }<br>
+ }<br>
+ } else if (instr->type == nir_instr_type_alu) {<br>
+ nir_alu_instr *alu = nir_instr_as_alu(instr);<br>
+ if (alu->op == nir_op_fddy)<br>
+ lower_fddy(state, alu);<br>
+ }<br>
+ }<br>
+<br>
+ return true;<br>
+}<br>
+<br>
+static void<br>
+lower_wpos_ytransform_impl(lower_wpos_ytransform_state *state, nir_function_impl *impl)<br>
+{<br>
+ nir_builder_init(&state->b, impl);<br>
+<br>
+ nir_foreach_block(block, impl) {<br>
+ lower_wpos_ytransform_block(state, block);<br>
+ }<br>
+ nir_metadata_preserve(impl, nir_metadata_block_index |<br>
+ nir_metadata_dominance);<br>
+}<br>
+<br>
+bool<br>
+nir_lower_wpos_ytransform(nir_shader *shader,<br>
+ const nir_lower_wpos_ytransform_options *options)<br>
+{<br>
+ lower_wpos_ytransform_state state = {<br>
+ .options = options,<br>
+ .shader = shader,<br>
+ };<br>
+<br>
+ assert(shader->stage == MESA_SHADER_FRAGMENT);<br>
+<br>
+ nir_foreach_function(function, shader) {<br>
+ if (function->impl)<br>
+ lower_wpos_ytransform_impl(&state, function->impl);<br>
+ }<br>
+<br>
+ return state.transform != NULL;<br>
+}<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.5.5<br>
<br>
</font></span></blockquote></div><br></div></div>