From 33fd9e5d8a6b85e20d34f89df655fa5a75eca431 Mon Sep 17 00:00:00 2001 From: Danylo Piliaiev Date: Tue, 22 Dec 2020 14:37:45 +0200 Subject: [PATCH] nir: account for point-coord origin when lowering it MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The resulting point-coord origin not only depends on whether the draw buffer is flipped but also on GL_POINT_SPRITE_COORD_ORIGIN state. Which makes its transform differ from a transform of wpos. On freedreno fixes: gl-3.2-pointsprite-origin gl-3.2-pointsprite-origin -fbo Fixes: d934d320 "nir: Add flipping of gl_PointCoord.y in nir_lower_wpos_ytransform." Signed-off-by: Danylo Piliaiev Acked-by: Marek Olšák Reviewed-by: Jose Maria Casanova Crespo Part-of: --- src/compiler/nir/meson.build | 1 + src/compiler/nir/nir.h | 8 +- src/compiler/nir/nir_lower_pntc_ytransform.c | 134 +++++++++++++++++++++++++++ src/compiler/nir/nir_lower_wpos_ytransform.c | 29 ------ src/mesa/program/prog_statevars.c | 18 ++++ src/mesa/program/prog_statevars.h | 1 + src/mesa/state_tracker/st_glsl_to_nir.cpp | 8 ++ 7 files changed, 169 insertions(+), 30 deletions(-) create mode 100644 src/compiler/nir/nir_lower_pntc_ytransform.c diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build index 9c90eaa..05670fb 100644 --- a/src/compiler/nir/meson.build +++ b/src/compiler/nir/meson.build @@ -175,6 +175,7 @@ files_libnir = files( 'nir_lower_passthrough_edgeflags.c', 'nir_lower_patch_vertices.c', 'nir_lower_phis_to_scalar.c', + 'nir_lower_pntc_ytransform.c', 'nir_lower_point_size.c', 'nir_lower_point_size_mov.c', 'nir_lower_printf.c', diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index aa26e43..4760247 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -3237,7 +3237,10 @@ typedef struct nir_shader_compiler_options { bool lower_device_index_to_zero; - /* Set if nir_lower_wpos_ytransform() should also invert gl_PointCoord. */ + /* Set if nir_lower_pntc_ytransform() should invert gl_PointCoord. + * Either when frame buffer is flipped or GL_POINT_SPRITE_COORD_ORIGIN + * is GL_LOWER_LEFT. + */ bool lower_wpos_pntc; /** @@ -4759,6 +4762,9 @@ bool nir_lower_wpos_ytransform(nir_shader *shader, const nir_lower_wpos_ytransform_options *options); bool nir_lower_wpos_center(nir_shader *shader, const bool for_sample_shading); +bool nir_lower_pntc_ytransform(nir_shader *shader, + const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH]); + bool nir_lower_wrmasks(nir_shader *shader, nir_instr_filter_cb cb, const void *data); bool nir_lower_fb_read(nir_shader *shader); diff --git a/src/compiler/nir/nir_lower_pntc_ytransform.c b/src/compiler/nir/nir_lower_pntc_ytransform.c new file mode 100644 index 0000000..f83d465 --- /dev/null +++ b/src/compiler/nir/nir_lower_pntc_ytransform.c @@ -0,0 +1,134 @@ +/* + * Copyright © 2020 Igalia S.L. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "nir.h" +#include "nir_builder.h" +#include "program/prog_instruction.h" + +/* Lower gl_PointCoord to account for user requested point-coord origin + * and for whether draw buffer is flipped. + */ + +typedef struct { + const gl_state_index16 *pntc_state_tokens; + nir_shader *shader; + nir_builder b; + nir_variable *pntc_transform; +} lower_pntc_ytransform_state; + +static nir_ssa_def * +get_pntc_transform(lower_pntc_ytransform_state *state) +{ + if (state->pntc_transform == NULL) { + /* NOTE: name must be prefixed w/ "gl_" to trigger slot based + * special handling in uniform setup: + */ + nir_variable *var = nir_variable_create(state->shader, + nir_var_uniform, + glsl_vec4_type(), + "gl_PntcYTransform"); + + var->num_state_slots = 1; + var->state_slots = ralloc_array(var, nir_state_slot, 1); + var->state_slots[0].swizzle = SWIZZLE_XYZW; + memcpy(var->state_slots[0].tokens, state->pntc_state_tokens, + sizeof(var->state_slots[0].tokens)); + var->data.how_declared = nir_var_hidden; + state->pntc_transform = var; + } + return nir_load_var(&state->b, state->pntc_transform); +} + +static void +lower_load_pointcoord(lower_pntc_ytransform_state *state, + nir_intrinsic_instr *intr) +{ + nir_builder *b = &state->b; + b->cursor = nir_after_instr(&intr->instr); + + nir_ssa_def *pntc = &intr->dest.ssa; + nir_ssa_def *transform = get_pntc_transform(state); + nir_ssa_def *y = nir_channel(b, pntc, 1); + /* The offset is 1 if we're flipping, 0 otherwise. */ + nir_ssa_def *offset = nir_channel(b, transform, 1); + /* Flip the sign of y if we're flipping. */ + nir_ssa_def *scaled = nir_fmul(b, y, nir_channel(b, transform, 0)); + + /* Reassemble the vector. */ + nir_ssa_def *flipped_pntc = nir_vec2(b, + nir_channel(b, pntc, 0), + nir_fadd(b, offset, scaled)); + + nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, nir_src_for_ssa(flipped_pntc), + flipped_pntc->parent_instr); +} + +static void +lower_pntc_ytransform_block(lower_pntc_ytransform_state *state, + nir_block *block) +{ + nir_foreach_instr_safe(instr, block) { + if (instr->type == nir_instr_type_intrinsic) { + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + if (intr->intrinsic == nir_intrinsic_load_deref) { + nir_deref_instr *deref = nir_src_as_deref(intr->src[0]); + nir_variable *var = nir_deref_instr_get_variable(deref); + + if (var->data.mode == nir_var_shader_in && + var->data.location == VARYING_SLOT_PNTC) { + lower_load_pointcoord(state, intr); + } + } + } + } +} + +bool +nir_lower_pntc_ytransform(nir_shader *shader, + const gl_state_index16 pntc_state_tokens[][STATE_LENGTH]) +{ + if (!shader->options->lower_wpos_pntc) + return false; + + lower_pntc_ytransform_state state = { + .pntc_state_tokens = *pntc_state_tokens, + .shader = shader, + .pntc_transform = NULL, + }; + + assert(shader->info.stage == MESA_SHADER_FRAGMENT); + + nir_foreach_function(function, shader) { + if (function->impl) { + nir_builder_init(&state.b, function->impl); + + nir_foreach_block(block, function->impl) { + lower_pntc_ytransform_block(&state, block); + } + nir_metadata_preserve(function->impl, nir_metadata_block_index | + nir_metadata_dominance); + } + } + + return state.pntc_transform != NULL; +} diff --git a/src/compiler/nir/nir_lower_wpos_ytransform.c b/src/compiler/nir/nir_lower_wpos_ytransform.c index 2771f2a..56ae978 100644 --- a/src/compiler/nir/nir_lower_wpos_ytransform.c +++ b/src/compiler/nir/nir_lower_wpos_ytransform.c @@ -232,31 +232,6 @@ lower_fragcoord(lower_wpos_ytransform_state *state, nir_intrinsic_instr *intr) emit_wpos_adjustment(state, intr, invert, adjX, adjY); } -static void -lower_load_pointcoord(lower_wpos_ytransform_state *state, - nir_intrinsic_instr *intr) -{ - nir_builder *b = &state->b; - b->cursor = nir_after_instr(&intr->instr); - - nir_ssa_def *pntc = &intr->dest.ssa; - nir_ssa_def *transform = get_transform(state); - nir_ssa_def *y = nir_channel(b, pntc, 1); - /* The offset is 1 if we're flipping, 0 otherwise. */ - nir_ssa_def *offset = nir_fmax(b, nir_channel(b, transform, 2), - nir_imm_float(b, 0.0)); - /* Flip the sign of y if we're flipping. */ - nir_ssa_def *scaled = nir_fmul(b, y, nir_channel(b, transform, 0)); - - /* Reassemble the vector. */ - nir_ssa_def *flipped_pntc = nir_vec2(b, - nir_channel(b, pntc, 0), - nir_fadd(b, offset, scaled)); - - nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, nir_src_for_ssa(flipped_pntc), - flipped_pntc->parent_instr); -} - /* turns 'fddy(p)' into 'fddy(fmul(p, transform.x))' */ static void lower_fddy(lower_wpos_ytransform_state *state, nir_alu_instr *fddy) @@ -339,10 +314,6 @@ lower_wpos_ytransform_block(lower_wpos_ytransform_state *state, nir_block *block } else if (var->data.mode == nir_var_system_value && var->data.location == SYSTEM_VALUE_SAMPLE_POS) { lower_load_sample_pos(state, intr); - } else if (var->data.mode == nir_var_shader_in && - var->data.location == VARYING_SLOT_PNTC && - state->shader->options->lower_wpos_pntc) { - lower_load_pointcoord(state, intr); } } else if (intr->intrinsic == nir_intrinsic_load_frag_coord) { lower_fragcoord(state, intr); diff --git a/src/mesa/program/prog_statevars.c b/src/mesa/program/prog_statevars.c index 64bff6f..e2c9039 100644 --- a/src/mesa/program/prog_statevars.c +++ b/src/mesa/program/prog_statevars.c @@ -636,6 +636,18 @@ fetch_state(struct gl_context *ctx, const gl_state_index16 state[], } return; + case STATE_FB_PNTC_Y_TRANSFORM: + { + bool flip_y = (ctx->Point.SpriteOrigin == GL_LOWER_LEFT) ^ + (ctx->DrawBuffer->FlipY); + + value[0] = flip_y ? -1.0F : 1.0F; + value[1] = flip_y ? 1.0F : 0.0F; + value[2] = 0.0F; + value[3] = 0.0F; + } + return; + case STATE_TCS_PATCH_VERTICES_IN: val[0].i = ctx->TessCtrlProgram.patch_vertices; return; @@ -802,6 +814,9 @@ _mesa_program_state_flags(const gl_state_index16 state[STATE_LENGTH]) case STATE_FB_WPOS_Y_TRANSFORM: return _NEW_BUFFERS; + case STATE_FB_PNTC_Y_TRANSFORM: + return _NEW_BUFFERS | _NEW_POINT; + case STATE_ADVANCED_BLENDING_MODE: return _NEW_COLOR; @@ -1057,6 +1072,9 @@ append_token(char *dst, gl_state_index k) case STATE_FB_WPOS_Y_TRANSFORM: append(dst, "FbWposYTransform"); break; + case STATE_FB_PNTC_Y_TRANSFORM: + append(dst, "PntcYTransform"); + break; case STATE_ADVANCED_BLENDING_MODE: append(dst, "AdvancedBlendingMode"); break; diff --git a/src/mesa/program/prog_statevars.h b/src/mesa/program/prog_statevars.h index 8832d4e..13c8ba0 100644 --- a/src/mesa/program/prog_statevars.h +++ b/src/mesa/program/prog_statevars.h @@ -145,6 +145,7 @@ typedef enum gl_state_index_ { STATE_PT_BIAS, /**< Pixel transfer RGBA bias */ STATE_FB_SIZE, /**< (width-1, height-1, 0, 0) */ STATE_FB_WPOS_Y_TRANSFORM, /**< (1, 0, -1, height) if a FBO is bound, (-1, height, 1, 0) otherwise */ + STATE_FB_PNTC_Y_TRANSFORM, /**< (1, 0, 0, 0) if point origin is upper left, (-1, 1, 0, 0) otherwise */ STATE_TCS_PATCH_VERTICES_IN, /**< gl_PatchVerticesIn for TCS (integer) */ STATE_TES_PATCH_VERTICES_IN, /**< gl_PatchVerticesIn for TES (integer) */ /** diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp b/src/mesa/state_tracker/st_glsl_to_nir.cpp index a473cfd..82dede2 100644 --- a/src/mesa/state_tracker/st_glsl_to_nir.cpp +++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp @@ -691,6 +691,14 @@ st_nir_lower_wpos_ytransform(struct nir_shader *nir, nir_validate_shader(nir, "after nir_lower_wpos_ytransform"); _mesa_add_state_reference(prog->Parameters, wposTransformState); } + + static const gl_state_index16 pntcTransformState[STATE_LENGTH] = { + STATE_INTERNAL, STATE_FB_PNTC_Y_TRANSFORM + }; + + if (nir_lower_pntc_ytransform(nir, &pntcTransformState)) { + _mesa_add_state_reference(prog->Parameters, pntcTransformState); + } } bool -- 2.7.4