From b82de2b4d702789620f84c70577aa3e106d6ae4f Mon Sep 17 00:00:00 2001 From: Andreas Baierl Date: Fri, 26 Apr 2019 15:01:43 +0200 Subject: [PATCH] nir: add rcp(w) lowering for gl_FragCoord On some hardware (e.g. Mali400) the shader needs to apply some transformations for correct gl_FragCoord handling. The lowering actions look like the following in pseudocode: gl_FragCoord.xyz = gl_FragCoord_orig.xyz gl_FragCoord.w = 1.0 / gl_FragCoord_orig.w Add this lowering as a nir pass in preparation for using it in the driver. Signed-off-by: Andreas Baierl Reviewed-by: Qiang Yu Reviewed-by: Eric Anholt --- src/compiler/Makefile.sources | 1 + src/compiler/nir/meson.build | 1 + src/compiler/nir/nir.h | 1 + src/compiler/nir/nir_lower_fragcoord_wtrans.c | 81 +++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 src/compiler/nir/nir_lower_fragcoord_wtrans.c diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index 35dd7ff1abe..a8e5f6280db 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -243,6 +243,7 @@ NIR_FILES = \ nir/nir_lower_constant_initializers.c \ nir/nir_lower_double_ops.c \ nir/nir_lower_drawpixels.c \ + nir/nir_lower_fragcoord_wtrans.c \ nir/nir_lower_frexp.c \ nir/nir_lower_global_vars_to_local.c \ nir/nir_lower_gs_intrinsics.c \ diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build index d6e7b2cc167..625f1508850 100644 --- a/src/compiler/nir/meson.build +++ b/src/compiler/nir/meson.build @@ -123,6 +123,7 @@ files_libnir = files( 'nir_lower_constant_initializers.c', 'nir_lower_double_ops.c', 'nir_lower_drawpixels.c', + 'nir_lower_fragcoord_wtrans.c', 'nir_lower_frexp.c', 'nir_lower_global_vars_to_local.c', 'nir_lower_gs_intrinsics.c', diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index e878a63409d..0ba90caad38 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -3159,6 +3159,7 @@ void nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask); void nir_lower_io_to_scalar_early(nir_shader *shader, nir_variable_mode mask); bool nir_lower_io_to_vector(nir_shader *shader, nir_variable_mode mask); +void nir_lower_fragcoord_wtrans(nir_shader *shader); void nir_lower_viewport_transform(nir_shader *shader); bool nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier); diff --git a/src/compiler/nir/nir_lower_fragcoord_wtrans.c b/src/compiler/nir/nir_lower_fragcoord_wtrans.c new file mode 100644 index 00000000000..b8c0b0b24e5 --- /dev/null +++ b/src/compiler/nir/nir_lower_fragcoord_wtrans.c @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2019 Andreas Baierl + * + * 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" + +/* Lower gl_FragCoord and transform the w component + * according to the following pseudocode: + * + * gl_FragCoord.xyz = gl_FragCoord_orig.xyz + * gl_FragCoord.w = 1.0 / gl_FragCoord_orig.w + * + * To trigger the transformation, gl_FragCoord currently has to be treated + * as a system value with PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL enabled. + */ + +static void +lower_fragcoord_wtrans(nir_builder *b, nir_intrinsic_instr *intr) +{ + assert(intr->dest.is_ssa); + + b->cursor = nir_before_instr(&intr->instr); + + nir_ssa_def *fragcoord_in = nir_load_frag_coord(b); + nir_ssa_def *w_rcp = nir_frcp(b, nir_channel(b, fragcoord_in, 3)); + nir_ssa_def *fragcoord_wtrans = nir_vec4(b, + nir_channel(b, fragcoord_in, 0), + nir_channel(b, fragcoord_in, 1), + nir_channel(b, fragcoord_in, 2), + w_rcp); + nir_ssa_def_rewrite_uses(&intr->dest.ssa, + nir_src_for_ssa(fragcoord_wtrans)); +} + +void +nir_lower_fragcoord_wtrans(nir_shader *shader) +{ + assert(shader->info.stage == MESA_SHADER_FRAGMENT); + + nir_foreach_function(func, shader) { + if (!func->impl) + continue; + + nir_builder b; + nir_builder_init(&b, func->impl); + nir_foreach_block(block, func->impl) { + nir_foreach_instr_safe(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + if (intr->intrinsic != nir_intrinsic_load_frag_coord) + continue; + + lower_fragcoord_wtrans(&b, intr); + } + } + nir_metadata_preserve(func->impl, nir_metadata_block_index | + nir_metadata_dominance); + } +} -- 2.30.2