nir/lower_wpos_center: clean up

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/29585>
This commit is contained in:
Alyssa Rosenzweig 2024-06-06 12:12:38 -04:00 committed by Marge Bot
parent 3beae0f98e
commit 31127d7b02

View file

@ -26,50 +26,33 @@
#include "nir_builder.h"
/**
* This pass adds <0.5, 0.5> to all uses of gl_FragCoord.
* This pass adds sample position to gl_FragCoord, intended for Vulkan drivers
* on hardware which provides an integer pixel center. Vulkan mandates that the
* pixel center must be half-integer, and also that the coordinate system's
* origin must be upper left. This means that there's no need for a uniform - we
* can always just add a constant. In the case that sample shading is enabled,
* Vulkan expects FragCoord to include sample positions.
*
* Run before nir_lower_io().
*
* For a more full featured pass, consider using nir_lower_wpos_ytransform(),
* which can handle pixel center integer / half integer, and origin lower
* left / upper left transformations.
*
* This simple pass is primarily intended for use by Vulkan drivers on
* hardware which provides an integer pixel center. Vulkan mandates that
* the pixel center must be half-integer, and also that the coordinate
* system's origin must be upper left. This means that there's no need
* for a uniform - we can always just add a constant. In the case that
* sample shading is enabled, Vulkan expects FragCoord to include sample
* positions.
*/
static void
update_fragcoord(nir_builder *b, nir_intrinsic_instr *intr)
{
nir_def *wpos = &intr->def;
b->cursor = nir_after_instr(&intr->instr);
nir_def *spos = nir_load_sample_pos_or_center(b);
wpos = nir_fadd(b, wpos,
nir_vec4(b,
nir_channel(b, spos, 0),
nir_channel(b, spos, 1),
nir_imm_float(b, 0.0f),
nir_imm_float(b, 0.0f)));
nir_def_rewrite_uses_after(&intr->def, wpos,
wpos->parent_instr);
}
static bool
lower_wpos_center_instr(nir_builder *b, nir_intrinsic_instr *intr, void *data)
{
if (intr->intrinsic != nir_intrinsic_load_frag_coord)
return false;
update_fragcoord(b, intr);
nir_def *wpos = &intr->def;
b->cursor = nir_after_instr(&intr->instr);
nir_def *spos = nir_load_sample_pos_or_center(b);
wpos = nir_fadd(b, wpos, nir_pad_vector_imm_int(b, spos, 0, 4));
nir_def_rewrite_uses_after(&intr->def, wpos, wpos->parent_instr);
return true;
}
@ -79,7 +62,7 @@ nir_lower_wpos_center(nir_shader *shader)
assert(shader->info.stage == MESA_SHADER_FRAGMENT);
return nir_shader_intrinsics_pass(shader, lower_wpos_center_instr,
nir_metadata_block_index |
nir_metadata_dominance,
NULL);
nir_metadata_block_index |
nir_metadata_dominance,
NULL);
}