Commit b82de2b4 authored by Andreas Baierl's avatar Andreas Baierl Committed by Qiang Yu
Browse files

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's avatarAndreas Baierl <ichgeh@imkreisrum.de>
Reviewed-by: Qiang Yu's avatarQiang Yu <yuq825@gmail.com>
Reviewed-by: Emma Anholt's avatarEric Anholt <eric@anholt.net>
parent 7050eccd
......@@ -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 \
......
......@@ -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',
......
......@@ -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);
......
/*
* 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);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment