From f318cab4a1661d08d5596ff72401fe669ff3b04d Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Fri, 23 Jun 2023 10:23:43 -0400 Subject: [PATCH] nir: Add lower_frag_coord_to_pixel_coord pass We've open coded this in a few backends. Signed-off-by: Alyssa Rosenzweig Reviewed-by: Boris Brezillon Part-of: --- src/compiler/nir/meson.build | 1 + src/compiler/nir/nir.h | 1 + .../nir/nir_lower_frag_coord_to_pixel_coord.c | 39 ++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 src/compiler/nir/nir_lower_frag_coord_to_pixel_coord.c diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build index 3147892..87d46eb 100644 --- a/src/compiler/nir/meson.build +++ b/src/compiler/nir/meson.build @@ -156,6 +156,7 @@ files_libnir = files( 'nir_lower_flrp.c', 'nir_lower_fp16_conv.c', 'nir_lower_fragcoord_wtrans.c', + 'nir_lower_frag_coord_to_pixel_coord.c', 'nir_lower_fragcolor.c', 'nir_lower_frexp.c', 'nir_lower_global_vars_to_local.c', diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index b1b52f4..fd113ad 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -5252,6 +5252,7 @@ nir_shader * nir_create_passthrough_gs(const nir_shader_compiler_options *option bool nir_lower_fragcolor(nir_shader *shader, unsigned max_cbufs); bool nir_lower_fragcoord_wtrans(nir_shader *shader); +bool nir_lower_frag_coord_to_pixel_coord(nir_shader *shader); bool nir_lower_viewport_transform(nir_shader *shader); bool nir_lower_uniforms_to_ubo(nir_shader *shader, bool dword_packed, bool load_vec4); diff --git a/src/compiler/nir/nir_lower_frag_coord_to_pixel_coord.c b/src/compiler/nir/nir_lower_frag_coord_to_pixel_coord.c new file mode 100644 index 0000000..20033f4 --- /dev/null +++ b/src/compiler/nir/nir_lower_frag_coord_to_pixel_coord.c @@ -0,0 +1,39 @@ +/* + * Copyright 2023 Valve Corpoation + * SPDX-License-Identifier: MIT + */ + +#include "nir.h" +#include "nir_builder.h" +#include "nir_builder_opcodes.h" + +static bool +lower(nir_builder *b, nir_instr *instr, UNUSED void *data) +{ + if (instr->type != nir_instr_type_intrinsic) + return false; + + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + if (intr->intrinsic != nir_intrinsic_load_frag_coord) + return false; + + /* load_pixel_coord gives the top-left corner of the pixel, but frag_coord + * should return the centre of the pixel. + */ + b->cursor = nir_before_instr(instr); + nir_ssa_def *top_left_xy = nir_u2f32(b, nir_load_pixel_coord(b)); + nir_ssa_def *xy = nir_fadd_imm(b, top_left_xy, 0.5); + + nir_ssa_def *vec = nir_vec4(b, nir_channel(b, xy, 0), nir_channel(b, xy, 1), + nir_load_frag_coord_zw(b, .component = 2), + nir_load_frag_coord_zw(b, .component = 3)); + nir_ssa_def_rewrite_uses(&intr->dest.ssa, vec); + return true; +} + +bool +nir_lower_frag_coord_to_pixel_coord(nir_shader *shader) +{ + return nir_shader_instructions_pass( + shader, lower, nir_metadata_block_index | nir_metadata_dominance, NULL); +} -- 2.7.4