From 5b951bcdd7faf1cbfc189a953c33334378ada3df Mon Sep 17 00:00:00 2001 From: Jose Maria Casanova Crespo Date: Mon, 28 Oct 2024 13:02:44 +0100 Subject: [PATCH] v3d: Enable Early-Z with discards when depth updates are disabled The Early-Z optimization is disabled when there is a discard instruction in the shader used in the draw call. But if discard is the only reason to disable Early-Z, and at draw call time the updates in the draw call are disabled we can enable Early-Z using a shader variant. If there are occlussion queries active we also need to disable Early-z optimization. So this patch enables Early-Z in this scenario. The performance improvement is significant when running gfxbench benchmark showing an average improvement of 11.15% fps_avg helped: gl_gfxbench_aztec_high.trace: 3.13 -> 3.73 (19.13%) fps_avg helped: gl_gfxbench_aztec.trace: 4.82 -> 5.68 (17.88%) fps_avg helped: gl_gfxbench_manhattan31.trace: 5.10 -> 6.00 (17.59%) fps_avg helped: gl_gfxbench_manhattan.trace: 7.24 -> 8.36 (15.52%) fps_avg helped: gl_gfxbench_trex.trace: 19.25 -> 20.17 ( 4.81%) Reviewed-by: Iago Toral Quiroga Cc: mesa-stable Part-of: --- src/broadcom/compiler/nir_to_vir.c | 5 +++-- src/broadcom/compiler/v3d_compiler.h | 1 + src/gallium/drivers/v3d/v3d_program.c | 5 +++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/broadcom/compiler/nir_to_vir.c b/src/broadcom/compiler/nir_to_vir.c index 110af3becb2..99935db961a 100644 --- a/src/broadcom/compiler/nir_to_vir.c +++ b/src/broadcom/compiler/nir_to_vir.c @@ -1973,11 +1973,12 @@ emit_frag_end(struct v3d_compile *c) */ if (c->output_position_index == -1 && !(c->s->info.num_images || c->s->info.num_ssbos) && - !c->s->info.fs.uses_discard && !c->fs_key->sample_alpha_to_coverage && c->output_sample_mask_index == -1 && has_any_tlb_color_write) { - c->s->info.fs.early_fragment_tests = true; + c->s->info.fs.early_fragment_tests = + !c->s->info.fs.uses_discard || + c->fs_key->can_earlyz_with_discard; } /* By default, Z buffer writes are implicit using the Z values produced diff --git a/src/broadcom/compiler/v3d_compiler.h b/src/broadcom/compiler/v3d_compiler.h index c28027799eb..063a3ae06ba 100644 --- a/src/broadcom/compiler/v3d_compiler.h +++ b/src/broadcom/compiler/v3d_compiler.h @@ -426,6 +426,7 @@ struct v3d_fs_key { bool msaa; bool sample_alpha_to_coverage; bool sample_alpha_to_one; + bool can_earlyz_with_discard; /* Mask of which color render targets are present. */ uint8_t cbufs; uint8_t swap_color_rb; diff --git a/src/gallium/drivers/v3d/v3d_program.c b/src/gallium/drivers/v3d/v3d_program.c index e8fd2414918..c990ccf0ca8 100644 --- a/src/gallium/drivers/v3d/v3d_program.c +++ b/src/gallium/drivers/v3d/v3d_program.c @@ -649,6 +649,7 @@ v3d_update_compiled_fs(struct v3d_context *v3d, uint8_t prim_mode) V3D_DIRTY_BLEND | V3D_DIRTY_FRAMEBUFFER | V3D_DIRTY_ZSA | + V3D_DIRTY_OQ | V3D_DIRTY_RASTERIZER | V3D_DIRTY_SAMPLE_STATE | V3D_DIRTY_FRAGTEX | @@ -677,6 +678,10 @@ v3d_update_compiled_fs(struct v3d_context *v3d, uint8_t prim_mode) } key->swap_color_rb = v3d->swap_color_rb; + key->can_earlyz_with_discard = s->info.fs.uses_discard && + (!v3d->zsa || !job->zsbuf || !v3d->zsa->base.depth_enabled || + !v3d->zsa->base.depth_writemask) && + !(v3d->active_queries && v3d->current_oq); for (int i = 0; i < v3d->framebuffer.nr_cbufs; i++) { struct pipe_surface *cbuf = v3d->framebuffer.cbufs[i];