panfrost: XMLify viewport

Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Reviewed-by: Tomeu Vizoso <tomeu.vizoso@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6195>
diff --git a/src/gallium/drivers/panfrost/pan_cmdstream.c b/src/gallium/drivers/panfrost/pan_cmdstream.c
index bf1efa4..d2091e6 100644
--- a/src/gallium/drivers/panfrost/pan_cmdstream.c
+++ b/src/gallium/drivers/panfrost/pan_cmdstream.c
@@ -958,128 +958,55 @@
         postfix->shader = shader_ptr;
 }
 
-static void
-panfrost_mali_viewport_init(struct panfrost_context *ctx,
-                            struct mali_viewport *mvp)
-{
-        const struct pipe_viewport_state *vp = &ctx->pipe_viewport;
-
-        /* Clip bounds are encoded as floats. The viewport itself is encoded as
-         * (somewhat) asymmetric ints. */
-
-        const struct pipe_scissor_state *ss = &ctx->scissor;
-
-        memset(mvp, 0, sizeof(*mvp));
-
-        /* By default, do no viewport clipping, i.e. clip to (-inf, inf) in
-         * each direction. Clipping to the viewport in theory should work, but
-         * in practice causes issues when we're not explicitly trying to
-         * scissor */
-
-        *mvp = (struct mali_viewport) {
-                .clip_minx = -INFINITY,
-                .clip_miny = -INFINITY,
-                .clip_maxx = INFINITY,
-                .clip_maxy = INFINITY,
-        };
-
-        /* Always scissor to the viewport by default. */
-        float vp_minx = (int) (vp->translate[0] - fabsf(vp->scale[0]));
-        float vp_maxx = (int) (vp->translate[0] + fabsf(vp->scale[0]));
-
-        float vp_miny = (int) (vp->translate[1] - fabsf(vp->scale[1]));
-        float vp_maxy = (int) (vp->translate[1] + fabsf(vp->scale[1]));
-
-        float minz = (vp->translate[2] - fabsf(vp->scale[2]));
-        float maxz = (vp->translate[2] + fabsf(vp->scale[2]));
-
-        /* Apply the scissor test */
-
-        unsigned minx, miny, maxx, maxy;
-
-        if (ss && ctx->rasterizer && ctx->rasterizer->base.scissor) {
-                minx = MAX2(ss->minx, vp_minx);
-                miny = MAX2(ss->miny, vp_miny);
-                maxx = MIN2(ss->maxx, vp_maxx);
-                maxy = MIN2(ss->maxy, vp_maxy);
-        } else {
-                minx = vp_minx;
-                miny = vp_miny;
-                maxx = vp_maxx;
-                maxy = vp_maxy;
-        }
-
-        /* Hardware needs the min/max to be strictly ordered, so flip if we
-         * need to. The viewport transformation in the vertex shader will
-         * handle the negatives if we don't */
-
-        if (miny > maxy) {
-                unsigned temp = miny;
-                miny = maxy;
-                maxy = temp;
-        }
-
-        if (minx > maxx) {
-                unsigned temp = minx;
-                minx = maxx;
-                maxx = temp;
-        }
-
-        if (minz > maxz) {
-                float temp = minz;
-                minz = maxz;
-                maxz = temp;
-        }
-
-        /* Clamp to the framebuffer size as a last check */
-
-        minx = MIN2(ctx->pipe_framebuffer.width, minx);
-        maxx = MIN2(ctx->pipe_framebuffer.width, maxx);
-
-        miny = MIN2(ctx->pipe_framebuffer.height, miny);
-        maxy = MIN2(ctx->pipe_framebuffer.height, maxy);
-
-        /* Upload */
-
-        mvp->viewport0[0] = minx;
-        mvp->viewport1[0] = MALI_POSITIVE(maxx);
-
-        mvp->viewport0[1] = miny;
-        mvp->viewport1[1] = MALI_POSITIVE(maxy);
-
-        bool clip_near = true;
-        bool clip_far = true;
-
-        if (ctx->rasterizer) {
-                clip_near = ctx->rasterizer->base.depth_clip_near;
-                clip_far = ctx->rasterizer->base.depth_clip_far;
-        }
-
-        mvp->clip_minz = clip_near ? minz : -INFINITY;
-        mvp->clip_maxz = clip_far ? maxz : INFINITY;
-}
-
 void
 panfrost_emit_viewport(struct panfrost_batch *batch,
                        struct mali_vertex_tiler_postfix *tiler_postfix)
 {
         struct panfrost_context *ctx = batch->ctx;
-        struct mali_viewport mvp;
+        const struct pipe_viewport_state *vp = &ctx->pipe_viewport;
+        const struct pipe_scissor_state *ss = &ctx->scissor;
+        const struct pipe_rasterizer_state *rast = &ctx->rasterizer->base;
+        const struct pipe_framebuffer_state *fb = &ctx->pipe_framebuffer;
 
-        panfrost_mali_viewport_init(batch->ctx,  &mvp);
+        /* Derive min/max from translate/scale. Note since |x| >= 0 by
+         * definition, we have that -|x| <= |x| hence translate - |scale| <=
+         * translate + |scale|, so the ordering is correct here. */
+        float vp_minx = (int) (vp->translate[0] - fabsf(vp->scale[0]));
+        float vp_maxx = (int) (vp->translate[0] + fabsf(vp->scale[0]));
+        float vp_miny = (int) (vp->translate[1] - fabsf(vp->scale[1]));
+        float vp_maxy = (int) (vp->translate[1] + fabsf(vp->scale[1]));
+        float minz = (vp->translate[2] - fabsf(vp->scale[2]));
+        float maxz = (vp->translate[2] + fabsf(vp->scale[2]));
 
-        /* Update the job, unless we're doing wallpapering (whose lack of
-         * scissor we can ignore, since if we "miss" a tile of wallpaper, it'll
-         * just... be faster :) */
+        /* Scissor to the intersection of viewport and to the scissor, clamped
+         * to the framebuffer */
 
-        if (!ctx->wallpaper_batch)
-                panfrost_batch_union_scissor(batch, mvp.viewport0[0],
-                                             mvp.viewport0[1],
-                                             mvp.viewport1[0] + 1,
-                                             mvp.viewport1[1] + 1);
+        unsigned minx = MIN2(fb->width, vp_minx);
+        unsigned maxx = MIN2(fb->width, vp_maxx);
+        unsigned miny = MIN2(fb->height, vp_miny);
+        unsigned maxy = MIN2(fb->height, vp_maxy);
 
-        tiler_postfix->viewport = panfrost_pool_upload(&batch->pool, &mvp,
-                                                            sizeof(mvp));
+        if (ss && rast && rast->scissor) {
+                minx = MAX2(ss->minx, minx);
+                miny = MAX2(ss->miny, miny);
+                maxx = MIN2(ss->maxx, maxx);
+                maxy = MIN2(ss->maxy, maxy);
+        }
+
+        struct panfrost_transfer T = panfrost_pool_alloc(&batch->pool, MALI_VIEWPORT_LENGTH);
+
+        pan_pack(T.cpu, VIEWPORT, cfg) {
+                cfg.scissor_minimum_x = minx;
+                cfg.scissor_minimum_y = miny;
+                cfg.scissor_maximum_x = maxx - 1;
+                cfg.scissor_maximum_y = maxy - 1;
+
+                cfg.minimum_z = rast->depth_clip_near ? minz : -INFINITY;
+                cfg.maximum_z = rast->depth_clip_far ? maxz : INFINITY;
+        }
+
+        tiler_postfix->viewport = T.gpu;
+        panfrost_batch_union_scissor(batch, minx, miny, maxx, maxy);
 }
 
 static mali_ptr
diff --git a/src/panfrost/include/panfrost-job.h b/src/panfrost/include/panfrost-job.h
index db92c13..8a9fb68 100644
--- a/src/panfrost/include/panfrost-job.h
+++ b/src/panfrost/include/panfrost-job.h
@@ -1375,25 +1375,6 @@
         uint64_t zero4;
 } __attribute__((packed));
 
-/* viewport0/viewport1 form the arguments to glViewport. viewport1 is
- * modified by MALI_POSITIVE; viewport0 is as-is.
- */
-
-struct mali_viewport {
-        /* XY clipping planes */
-        float clip_minx;
-        float clip_miny;
-        float clip_maxx;
-        float clip_maxy;
-
-        /* Depth clipping planes */
-        float clip_minz;
-        float clip_maxz;
-
-        u16 viewport0[2];
-        u16 viewport1[2];
-} __attribute__((packed));
-
 /* From presentations, 16x16 tiles externally. Use shift for fast computation
  * of tile numbers. */
 
diff --git a/src/panfrost/lib/decode.c b/src/panfrost/lib/decode.c
index fa9e6c5..83d762d 100644
--- a/src/panfrost/lib/decode.c
+++ b/src/panfrost/lib/decode.c
@@ -2768,31 +2768,8 @@
         } else
                 pandecode_msg("XXX: missing shader descriptor\n");
 
-        if (p->viewport) {
-                struct pandecode_mapped_memory *fmem = pandecode_find_mapped_gpu_mem_containing(p->viewport);
-                struct mali_viewport *PANDECODE_PTR_VAR(f, fmem, p->viewport);
-
-                pandecode_log("struct mali_viewport viewport_%"PRIx64"_%d%s = {\n", p->viewport, job_no, suffix);
-                pandecode_indent++;
-
-                pandecode_prop("clip_minx = %f", f->clip_minx);
-                pandecode_prop("clip_miny = %f", f->clip_miny);
-                pandecode_prop("clip_minz = %f", f->clip_minz);
-                pandecode_prop("clip_maxx = %f", f->clip_maxx);
-                pandecode_prop("clip_maxy = %f", f->clip_maxy);
-                pandecode_prop("clip_maxz = %f", f->clip_maxz);
-
-                /* Only the higher coordinates are MALI_POSITIVE scaled */
-
-                pandecode_prop("viewport0 = { %d, %d }",
-                               f->viewport0[0], f->viewport0[1]);
-
-                pandecode_prop("viewport1 = { MALI_POSITIVE(%d), MALI_POSITIVE(%d) }",
-                               f->viewport1[0] + 1, f->viewport1[1] + 1);
-
-                pandecode_indent--;
-                pandecode_log("};\n");
-        }
+        if (p->viewport)
+                DUMP_ADDR("Viewport", VIEWPORT, p->viewport, 1);
 
         unsigned max_attr_index = 0;
 
diff --git a/src/panfrost/lib/midgard.xml b/src/panfrost/lib/midgard.xml
index e811c27..f6df1d5 100644
--- a/src/panfrost/lib/midgard.xml
+++ b/src/panfrost/lib/midgard.xml
@@ -60,4 +60,17 @@
     <value name="Mirrored Clamp to Border" value="15"/>
   </enum>
 
+  <struct name="Viewport">
+    <field name="Minimum X" size="32" start="0:0" default="-INFINITY" type="float"/>
+    <field name="Minimum Y" size="32" start="1:0" default="-INFINITY" type="float"/>
+    <field name="Maximum X" size="32" start="2:0" default="+INFINITY" type="float"/>
+    <field name="Maximum Y" size="32" start="3:0" default="+INFINITY" type="float"/>
+    <field name="Minimum Z" size="32" start="4:0" default="0.0" type="float"/>
+    <field name="Maximum Z" size="32" start="5:0" default="1.0" type="float"/>
+    <field name="Scissor Minimum X" size="16" start="6:0" default="0" type="uint"/>
+    <field name="Scissor Minimum Y" size="16" start="6:16" default="0" type="uint"/>
+    <field name="Scissor Maximum X" size="16" start="7:0" type="uint"/>
+    <field name="Scissor Maximum Y" size="16" start="7:16" type="uint"/>
+  </struct>
+
 </panxml>
diff --git a/src/panfrost/lib/pan_blit.c b/src/panfrost/lib/pan_blit.c
index 041059e..cbf58f4 100644
--- a/src/panfrost/lib/pan_blit.c
+++ b/src/panfrost/lib/pan_blit.c
@@ -186,17 +186,12 @@
         unsigned width = u_minify(image->width0, image->first_level);
         unsigned height = u_minify(image->height0, image->first_level);
 
-        struct mali_viewport viewport = {
-                .clip_minx = -INFINITY,
-                .clip_miny = -INFINITY,
-                .clip_maxx = INFINITY,
-                .clip_maxy = INFINITY,
-                .clip_minz = 0.0,
-                .clip_maxz = 1.0,
+        struct panfrost_transfer viewport = panfrost_pool_alloc(pool, MALI_VIEWPORT_LENGTH);
 
-                .viewport0 = { 0, 0 },
-                .viewport1 = { MALI_POSITIVE(width), MALI_POSITIVE(height) }
-        };
+        pan_pack(viewport.cpu, VIEWPORT, cfg) {
+                cfg.scissor_maximum_x = width - 1; /* Inclusive */
+                cfg.scissor_maximum_y = height - 1;
+        }
 
         union mali_attr varying = {
 		.elements = coordinates | MALI_ATTR_LINEAR,
@@ -358,7 +353,7 @@
                         .shader = shader_meta_t.gpu,
                         .varyings = panfrost_pool_upload(pool, &varying, sizeof(varying)),
                         .varying_meta = panfrost_pool_upload(pool, &varying_meta, sizeof(varying_meta)),
-                        .viewport = panfrost_pool_upload(pool, &viewport, sizeof(viewport)),
+                        .viewport = viewport.gpu,
                         .shared_memory = fbd
                 }
         };