<p dir="ltr"></p>
<p dir="ltr">On Nov 17, 2016 11:34 AM, "Kenneth Graunke" <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>> wrote:<br>
><br>
> There's no point in performing depth writes when the depth test<br>
> comparison function is set to GL_EQUAL - it would just write out<br>
> the same value that's already there (if it is written at all). While<br>
> this is harmless from a functional perspective, it hurts performance.<br>
> Obviously, writing to memory is not free, but there's another more<br>
> subtle impact as well: it can prevent early depth optimizations.<br>
><br>
> Depth writes aren't supposed to happen for pixels that are killed<br>
> by fragment shader discard statements or the alpha test. So, with<br>
> depth writes enabled and either of those, the pixel shader must be<br>
> invoked to determine whether or not to perform the write. This is<br>
> fairly stupid in the EQUAL case - we're running a shader to decide<br>
> whether to replace the existing depth value with itself.<br>
><br>
> By disabling these pointless writes, we allow early depth even with<br>
> discards and alpha testing, allowing the hardware to skip the pixel<br>
> shader altogether if the depth test fails.<br>
><br>
> Improves performance of Unigine Valley:<br>
><br>
> - Skylake GT2: +17.8%<br>
> - Broadwell GT3e: +11.5%<br>
> - Cherrytrail: +19.4%<br>
><br>
> Huge thanks to Mark Janes for building frameretrace [1], the performance<br>
> analysis tool that helped us find this issue, and to Robert Bragg for<br>
> providing us performance metrics on Linux. Mark also spent the time to<br>
> analyze Valley performance on Windows vs. Linux and discovered a<br>
> discrepancy in early depth test metrics. Once he had isolated a draw<br>
> call and drawn attention to the problem, fixing it was pretty simple.<br>
><br>
> [1] <a href="https://github.com/janesma/apitrace/wiki/frameretrace-branch">https://github.com/janesma/apitrace/wiki/frameretrace-branch</a><br>
><br>
> Signed-off-by: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>><br>
> ---<br>
> src/mesa/drivers/dri/i965/brw_cc.c | 2 +-<br>
> src/mesa/drivers/dri/i965/brw_context.h | 7 +++++++<br>
> src/mesa/drivers/dri/i965/brw_draw.c | 2 +-<br>
> src/mesa/drivers/dri/i965/brw_wm.c | 2 +-<br>
> src/mesa/drivers/dri/i965/gen6_depthstencil.c | 2 +-<br>
> src/mesa/drivers/dri/i965/gen7_misc_state.c | 2 +-<br>
> src/mesa/drivers/dri/i965/gen8_depth_state.c | 4 ++--<br>
> src/mesa/drivers/dri/i965/gen8_wm_depth_stencil.c | 2 +-<br>
> 8 files changed, 15 insertions(+), 8 deletions(-)<br>
><br>
> diff --git a/src/mesa/drivers/dri/i965/brw_cc.c b/src/mesa/drivers/dri/i965/brw_cc.c<br>
> index b11d7c8..c3b00e1 100644<br>
> --- a/src/mesa/drivers/dri/i965/brw_cc.c<br>
> +++ b/src/mesa/drivers/dri/i965/brw_cc.c<br>
> @@ -225,7 +225,7 @@ static void upload_cc_unit(struct brw_context *brw)<br>
> cc->cc2.depth_test = 1;<br>
> cc->cc2.depth_test_function =<br>
> intel_translate_compare_func(ctx->Depth.Func);<br>
> - cc->cc2.depth_write_enable = ctx->Depth.Mask;<br>
> + cc->cc2.depth_write_enable = brw_depth_writes_enabled(brw);<br>
> }<br>
><br>
> if (brw->stats_wm || unlikely(INTEL_DEBUG & DEBUG_STATS))<br>
> diff --git a/src/mesa/drivers/dri/i965/brw_context.h b/src/mesa/drivers/dri/i965/brw_context.h<br>
> index 7604d26..a268c94 100644<br>
> --- a/src/mesa/drivers/dri/i965/brw_context.h<br>
> +++ b/src/mesa/drivers/dri/i965/brw_context.h<br>
> @@ -1714,6 +1714,13 @@ bool brw_lower_texture_gradients(struct brw_context *brw,<br>
> extern const char * const conditional_modifier[16];<br>
> extern const char *const pred_ctrl_align16[16];<br>
><br>
> +static inline bool<br>
> +brw_depth_writes_enabled(const struct brw_context *brw)<br>
> +{</p>
<p dir="ltr">It might be good to have an abbreviated version of the commit message as a comment here. Other than that, rb</p>
<p dir="ltr">> + const struct gl_context *ctx = &brw->ctx;<br>
> + return ctx->Depth.Test && ctx->Depth.Mask && ctx->Depth.Func != GL_EQUAL;<br>
> +}<br>
> +<br>
> void<br>
> brw_emit_depthbuffer(struct brw_context *brw);<br>
><br>
> diff --git a/src/mesa/drivers/dri/i965/brw_draw.c b/src/mesa/drivers/dri/i965/brw_draw.c<br>
> index b093020..7904ef5 100644<br>
> --- a/src/mesa/drivers/dri/i965/brw_draw.c<br>
> +++ b/src/mesa/drivers/dri/i965/brw_draw.c<br>
> @@ -372,7 +372,7 @@ brw_postdraw_set_buffers_need_resolve(struct brw_context *brw)<br>
> front_irb->need_downsample = true;<br>
> if (back_irb)<br>
> back_irb->need_downsample = true;<br>
> - if (depth_irb && ctx->Depth.Mask) {<br>
> + if (depth_irb && brw_depth_writes_enabled(brw)) {<br>
> intel_renderbuffer_att_set_needs_depth_resolve(depth_att);<br>
> brw_render_cache_set_add_bo(brw, depth_irb->mt->bo);<br>
> }<br>
> diff --git a/src/mesa/drivers/dri/i965/brw_wm.c b/src/mesa/drivers/dri/i965/brw_wm.c<br>
> index e6f68c4..dab2e33 100644<br>
> --- a/src/mesa/drivers/dri/i965/brw_wm.c<br>
> +++ b/src/mesa/drivers/dri/i965/brw_wm.c<br>
> @@ -460,7 +460,7 @@ brw_wm_populate_key(struct brw_context *brw, struct brw_wm_prog_key *key)<br>
> if (ctx->Depth.Test)<br>
> lookup |= IZ_DEPTH_TEST_ENABLE_BIT;<br>
><br>
> - if (ctx->Depth.Test && ctx->Depth.Mask) /* ?? */<br>
> + if (brw_depth_writes_enabled(brw))<br>
> lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;<br>
><br>
> /* _NEW_STENCIL | _NEW_BUFFERS */<br>
> diff --git a/src/mesa/drivers/dri/i965/gen6_depthstencil.c b/src/mesa/drivers/dri/i965/gen6_depthstencil.c<br>
> index a3de844..79d4d5d 100644<br>
> --- a/src/mesa/drivers/dri/i965/gen6_depthstencil.c<br>
> +++ b/src/mesa/drivers/dri/i965/gen6_depthstencil.c<br>
> @@ -83,7 +83,7 @@ gen6_upload_depth_stencil_state(struct brw_context *brw)<br>
> if (ctx->Depth.Test && depth_irb) {<br>
> ds->ds2.depth_test_enable = ctx->Depth.Test;<br>
> ds->ds2.depth_test_func = intel_translate_compare_func(ctx->Depth.Func);<br>
> - ds->ds2.depth_write_enable = ctx->Depth.Mask;<br>
> + ds->ds2.depth_write_enable = brw_depth_writes_enabled(brw);<br>
> }<br>
><br>
> /* Point the GPU at the new indirect state. */<br>
> diff --git a/src/mesa/drivers/dri/i965/gen7_misc_state.c b/src/mesa/drivers/dri/i965/gen7_misc_state.c<br>
> index 7bd5cd5..af9be66 100644<br>
> --- a/src/mesa/drivers/dri/i965/gen7_misc_state.c<br>
> +++ b/src/mesa/drivers/dri/i965/gen7_misc_state.c<br>
> @@ -109,7 +109,7 @@ gen7_emit_depth_stencil_hiz(struct brw_context *brw,<br>
> (depthbuffer_format << 18) |<br>
> ((hiz ? 1 : 0) << 22) |<br>
> ((stencil_mt != NULL && ctx->Stencil._WriteEnabled) << 27) |<br>
> - ((ctx->Depth.Mask != 0) << 28) |<br>
> + (brw_depth_writes_enabled(brw) << 28) |<br>
> (surftype << 29));<br>
><br>
> /* 3DSTATE_DEPTH_BUFFER dw2 */<br>
> diff --git a/src/mesa/drivers/dri/i965/gen8_depth_state.c b/src/mesa/drivers/dri/i965/gen8_depth_state.c<br>
> index 3604aee..14689f4 100644<br>
> --- a/src/mesa/drivers/dri/i965/gen8_depth_state.c<br>
> +++ b/src/mesa/drivers/dri/i965/gen8_depth_state.c<br>
> @@ -218,7 +218,7 @@ gen8_emit_depth_stencil_hiz(struct brw_context *brw,<br>
> }<br>
><br>
> emit_depth_packets(brw, depth_mt, brw_depthbuffer_format(brw), surftype,<br>
> - ctx->Depth.Mask != 0,<br>
> + brw_depth_writes_enabled(brw),<br>
> stencil_mt, ctx->Stencil._WriteEnabled,<br>
> hiz, width, height, depth, lod, min_array_element);<br>
> }<br>
> @@ -280,7 +280,7 @@ pma_fix_enable(const struct brw_context *brw)<br>
> * 3DSTATE_WM_DEPTH_STENCIL::DepthWriteEnable &&<br>
> * 3DSTATE_DEPTH_BUFFER::DEPTH_WRITE_ENABLE.<br>
> */<br>
> - const bool depth_writes_enabled = ctx->Depth.Mask;<br>
> + const bool depth_writes_enabled = brw_depth_writes_enabled(brw);<br>
><br>
> /* _NEW_STENCIL:<br>
> * !DEPTH_STENCIL_STATE::Stencil Buffer Write Enable ||<br>
> diff --git a/src/mesa/drivers/dri/i965/gen8_wm_depth_stencil.c b/src/mesa/drivers/dri/i965/gen8_wm_depth_stencil.c<br>
> index e49103c..9a6c9e0 100644<br>
> --- a/src/mesa/drivers/dri/i965/gen8_wm_depth_stencil.c<br>
> +++ b/src/mesa/drivers/dri/i965/gen8_wm_depth_stencil.c<br>
> @@ -90,7 +90,7 @@ gen8_upload_wm_depth_stencil(struct brw_context *brw)<br>
> GEN8_WM_DS_DEPTH_TEST_ENABLE |<br>
> FUNC(ctx->Depth.Func) << GEN8_WM_DS_DEPTH_FUNC_SHIFT;<br>
><br>
> - if (ctx->Depth.Mask)<br>
> + if (brw_depth_writes_enabled(brw))<br>
> dw1 |= GEN8_WM_DS_DEPTH_BUFFER_WRITE_ENABLE;<br>
> }<br>
><br>
> --<br>
> 2.10.2<br>
><br>
> _______________________________________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> <a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br></p>