Mesa (main): zink: enforce viewport depth clamping

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Jun 30 23:10:28 UTC 2022


Module: Mesa
Branch: main
Commit: 90c5eea22b69306e463decd405029944b4a0d53b
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=90c5eea22b69306e463decd405029944b4a0d53b

Author: Mike Blumenkrantz <michael.blumenkrantz at gmail.com>
Date:   Thu Jun 30 13:54:42 2022 -0400

zink: enforce viewport depth clamping

VUID-VkViewport-minDepth-01234 specifies that depth must be in the range [0.0, 1.0],
so the viewport must always be clamped to this range

this affects texture clears using u_blitter, as this expects to be able
to use the GL range of [-1.0, 1.0], so pass the depth value as though it's
been de-converted back to a GL z coordinate to account for viewport transform

cc: mesa-stable

fixes #6757

Reviewed-by: Dave Airlie <airlied at redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17319>

---

 src/gallium/drivers/zink/zink_clear.c  | 10 +++++++++-
 src/gallium/drivers/zink/zink_draw.cpp | 10 ++++++----
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/gallium/drivers/zink/zink_clear.c b/src/gallium/drivers/zink/zink_clear.c
index c1fa0106431..8433b5c62ef 100644
--- a/src/gallium/drivers/zink/zink_clear.c
+++ b/src/gallium/drivers/zink/zink_clear.c
@@ -444,7 +444,15 @@ zink_clear_texture(struct pipe_context *pctx,
             flags |= PIPE_CLEAR_STENCIL;
          surf = create_clear_surface(pctx, pres, level, box);
          zink_blit_begin(ctx, ZINK_BLIT_SAVE_FB | ZINK_BLIT_SAVE_FS);
-         util_blitter_clear_depth_stencil(ctx->blitter, surf, flags, depth, stencil, box->x, box->y, box->width, box->height);
+         /* Vulkan requires depth to be in the range of [0.0, 1.0], while GL uses
+          * the viewport range of [-1.0, 1.0], creating a mismatch during u_blitter rendering;
+          * to account for this, de-convert depth back to GL for viewport transform:
+
+            depth = (depth * 2) - 1
+
+          * this yields the correct result after the viewport has been clamped
+          */
+         util_blitter_clear_depth_stencil(ctx->blitter, surf, flags, (depth * 2) - 1, stencil, box->x, box->y, box->width, box->height);
       }
    }
    /* this will never destroy the surface */
diff --git a/src/gallium/drivers/zink/zink_draw.cpp b/src/gallium/drivers/zink/zink_draw.cpp
index dff057dc5c2..743531e4a1b 100644
--- a/src/gallium/drivers/zink/zink_draw.cpp
+++ b/src/gallium/drivers/zink/zink_draw.cpp
@@ -569,10 +569,12 @@ zink_draw(struct pipe_context *pctx,
             ctx->vp_state.viewport_states[i].translate[1] - ctx->vp_state.viewport_states[i].scale[1],
             MAX2(ctx->vp_state.viewport_states[i].scale[0] * 2, 1),
             ctx->vp_state.viewport_states[i].scale[1] * 2,
-            ctx->rast_state->base.clip_halfz ?
-               ctx->vp_state.viewport_states[i].translate[2] :
-               ctx->vp_state.viewport_states[i].translate[2] - ctx->vp_state.viewport_states[i].scale[2],
-            ctx->vp_state.viewport_states[i].translate[2] + ctx->vp_state.viewport_states[i].scale[2]
+            CLAMP(ctx->rast_state->base.clip_halfz ?
+                  ctx->vp_state.viewport_states[i].translate[2] :
+                  ctx->vp_state.viewport_states[i].translate[2] - ctx->vp_state.viewport_states[i].scale[2],
+                  0, 1),
+            CLAMP(ctx->vp_state.viewport_states[i].translate[2] + ctx->vp_state.viewport_states[i].scale[2],
+                  0, 1)
          };
          viewports[i] = viewport;
       }



More information about the mesa-commit mailing list