v3d,v3dv: fix viewport offset for negative viewport center
authorIago Toral Quiroga <itoral@igalia.com>
Tue, 6 Jun 2023 08:39:37 +0000 (10:39 +0200)
committerMarge Bot <emma+marge@anholt.net>
Wed, 7 Jun 2023 18:40:56 +0000 (18:40 +0000)
If the viewport center is not positive we can't express it
through the fine coordinates, which are unsigned, and we
need to use the coarse coordinates too.

Fixes new crashes in Vulkan CTS 1.3.6.0:
dEQP-VK.draw.renderpass.offscreen_viewport.*negative*

Reviewed-by: Alejandro PiƱeiro <apinheiro@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23489>

src/broadcom/vulkan/v3dvx_cmd_buffer.c
src/gallium/drivers/v3d/v3dx_emit.c

index 024decb..f182b79 100644 (file)
@@ -1103,8 +1103,28 @@ v3dX(cmd_buffer_emit_viewport)(struct v3dv_cmd_buffer *cmd_buffer)
    }
 
    cl_emit(&job->bcl, VIEWPORT_OFFSET, vp) {
-      vp.fine_x = vptranslate[0];
-      vp.fine_y = vptranslate[1];
+      float vp_fine_x = vptranslate[0];
+      float vp_fine_y = vptranslate[1];
+      int32_t vp_coarse_x = 0;
+      int32_t vp_coarse_y = 0;
+
+      /* The fine coordinates must be unsigned, but coarse can be signed */
+      if (unlikely(vp_fine_x < 0)) {
+         int32_t blocks_64 = DIV_ROUND_UP(fabsf(vp_fine_x), 64);
+         vp_fine_x += 64.0f * blocks_64;
+         vp_coarse_x -= blocks_64;
+      }
+
+      if (unlikely(vp_fine_y < 0)) {
+         int32_t blocks_64 = DIV_ROUND_UP(fabsf(vp_fine_y), 64);
+         vp_fine_y += 64.0f * blocks_64;
+         vp_coarse_y -= blocks_64;
+      }
+
+      vp.fine_x = vp_fine_x;
+      vp.fine_y = vp_fine_y;
+      vp.coarse_x = vp_coarse_x;
+      vp.coarse_y = vp_coarse_y;
    }
 
    cmd_buffer->state.dirty &= ~V3DV_CMD_DIRTY_VIEWPORT;
index 5303eaf..0ad3fb6 100644 (file)
@@ -593,8 +593,32 @@ v3dX(emit_state)(struct pipe_context *pctx)
                         vp.viewport_centre_y_coordinate =
                                 v3d->viewport.translate[1];
 #else
-                        vp.fine_x = v3d->viewport.translate[0];
-                        vp.fine_y = v3d->viewport.translate[1];
+                        float vp_fine_x = v3d->viewport.translate[0];
+                        float vp_fine_y = v3d->viewport.translate[1];
+                        int32_t vp_coarse_x = 0;
+                        int32_t vp_coarse_y = 0;
+
+                        /* The fine coordinates must be unsigned, but coarse
+                         * can be signed.
+                         */
+                        if (unlikely(vp_fine_x < 0)) {
+                                int32_t blocks_64 =
+                                        DIV_ROUND_UP(fabsf(vp_fine_x), 64);
+                                vp_fine_x += 64.0f * blocks_64;
+                                vp_coarse_x -= blocks_64;
+                        }
+
+                        if (unlikely(vp_fine_y < 0)) {
+                                int32_t blocks_64 =
+                                        DIV_ROUND_UP(fabsf(vp_fine_y), 64);
+                                vp_fine_y += 64.0f * blocks_64;
+                                vp_coarse_y -= blocks_64;
+                        }
+
+                        vp.fine_x = vp_fine_x;
+                        vp.fine_y = vp_fine_y;
+                        vp.coarse_x = vp_coarse_x;
+                        vp.coarse_y = vp_coarse_y;
 #endif
                 }
         }