zink: clamp zs samplers to XXXX swizzle for all non-zero/one swizzles
authorMike Blumenkrantz <michael.blumenkrantz@gmail.com>
Wed, 5 May 2021 13:40:51 +0000 (09:40 -0400)
committerMarge Bot <eric+marge@anholt.net>
Thu, 6 May 2021 12:09:38 +0000 (12:09 +0000)
Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10649>

src/gallium/drivers/zink/zink_context.c

index db9e879..cadaaaa 100644 (file)
@@ -509,6 +509,21 @@ clamp_void_swizzle(const struct util_format_description *desc, enum pipe_swizzle
    return swizzle;
 }
 
+ALWAYS_INLINE static enum pipe_swizzle
+clamp_zs_swizzle(enum pipe_swizzle swizzle)
+{
+   switch (swizzle) {
+   case PIPE_SWIZZLE_X:
+   case PIPE_SWIZZLE_Y:
+   case PIPE_SWIZZLE_Z:
+   case PIPE_SWIZZLE_W:
+      return PIPE_SWIZZLE_X;
+   default:
+      break;
+   }
+   return swizzle;
+}
+
 static struct pipe_sampler_view *
 zink_create_sampler_view(struct pipe_context *pctx, struct pipe_resource *pres,
                          const struct pipe_sampler_view *state)
@@ -530,33 +545,29 @@ zink_create_sampler_view(struct pipe_context *pctx, struct pipe_resource *pres,
       ivci.image = res->obj->image;
       ivci.viewType = image_view_type(state->target);
 
-      ivci.components.r = component_mapping(sampler_view->base.swizzle_r);
-      ivci.components.g = component_mapping(sampler_view->base.swizzle_g);
-      ivci.components.b = component_mapping(sampler_view->base.swizzle_b);
-      ivci.components.a = component_mapping(sampler_view->base.swizzle_a);
-
       ivci.subresourceRange.aspectMask = sampler_aspect_from_format(state->format);
       ivci.format = zink_get_format(screen, state->format);
       /* samplers for stencil aspects of packed formats need to always use stencil swizzle */
-      if (ivci.subresourceRange.aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT) {
-         ivci.components.g = VK_COMPONENT_SWIZZLE_R;
+      if (ivci.subresourceRange.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
+         ivci.components.r = component_mapping(clamp_zs_swizzle(sampler_view->base.swizzle_r));
+         ivci.components.g = component_mapping(clamp_zs_swizzle(sampler_view->base.swizzle_g));
+         ivci.components.b = component_mapping(clamp_zs_swizzle(sampler_view->base.swizzle_b));
+         ivci.components.a = component_mapping(clamp_zs_swizzle(sampler_view->base.swizzle_a));
       } else {
          /* if we have e.g., R8G8B8X8, then we have to ignore alpha since we're just emulating
           * these formats
           */
-         if (ivci.subresourceRange.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
-             const struct util_format_description *desc = util_format_description(state->format);
-             if (util_format_is_rgba8_variant(desc)) {
-                sampler_view->base.swizzle_r = clamp_void_swizzle(desc, sampler_view->base.swizzle_r);
-                sampler_view->base.swizzle_g = clamp_void_swizzle(desc, sampler_view->base.swizzle_g);
-                sampler_view->base.swizzle_b = clamp_void_swizzle(desc, sampler_view->base.swizzle_b);
-                sampler_view->base.swizzle_a = clamp_void_swizzle(desc, sampler_view->base.swizzle_a);
-                ivci.components.r = component_mapping(sampler_view->base.swizzle_r);
-                ivci.components.g = component_mapping(sampler_view->base.swizzle_g);
-                ivci.components.b = component_mapping(sampler_view->base.swizzle_b);
-                ivci.components.a = component_mapping(sampler_view->base.swizzle_a);
-             }
-         }
+          const struct util_format_description *desc = util_format_description(state->format);
+          if (util_format_is_rgba8_variant(desc)) {
+             sampler_view->base.swizzle_r = clamp_void_swizzle(desc, sampler_view->base.swizzle_r);
+             sampler_view->base.swizzle_g = clamp_void_swizzle(desc, sampler_view->base.swizzle_g);
+             sampler_view->base.swizzle_b = clamp_void_swizzle(desc, sampler_view->base.swizzle_b);
+             sampler_view->base.swizzle_a = clamp_void_swizzle(desc, sampler_view->base.swizzle_a);
+          }
+          ivci.components.r = component_mapping(sampler_view->base.swizzle_r);
+          ivci.components.g = component_mapping(sampler_view->base.swizzle_g);
+          ivci.components.b = component_mapping(sampler_view->base.swizzle_b);
+          ivci.components.a = component_mapping(sampler_view->base.swizzle_a);
       }
       assert(ivci.format);