panfrost: XMLify stencil test

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 8376caf..8f6142f 100644
--- a/src/gallium/drivers/panfrost/pan_cmdstream.c
+++ b/src/gallium/drivers/panfrost/pan_cmdstream.c
@@ -519,15 +519,15 @@
 
 static void
 panfrost_make_stencil_state(const struct pipe_stencil_state *in,
-                            struct mali_stencil_test *out)
+                            void *out)
 {
-        out->ref = 0; /* Gallium gets it from elsewhere */
-
-        out->mask = in->valuemask;
-        out->func = panfrost_translate_compare_func(in->func);
-        out->sfail = panfrost_translate_stencil_op(in->fail_op);
-        out->dpfail = panfrost_translate_stencil_op(in->zfail_op);
-        out->dppass = panfrost_translate_stencil_op(in->zpass_op);
+        pan_pack(out, STENCIL, cfg) {
+                cfg.mask = in->valuemask;
+                cfg.compare_function = panfrost_translate_compare_func(in->func);
+                cfg.stencil_fail = panfrost_translate_stencil_op(in->fail_op);
+                cfg.depth_fail = panfrost_translate_stencil_op(in->zfail_op);
+                cfg.depth_pass = panfrost_translate_stencil_op(in->zpass_op);
+        }
 }
 
 static void
@@ -605,7 +605,10 @@
                 panfrost_make_stencil_state(&zsa->stencil[0],
                                             &fragmeta->stencil_front);
                 fragmeta->stencil_mask_front = zsa->stencil[0].writemask;
-                fragmeta->stencil_front.ref = ctx->stencil_ref.ref_value[0];
+
+                /* Bottom 8-bits of stencil state is the stencil ref, ref is no
+                 * more than 8-bits. Be extra careful. */
+                fragmeta->stencil_front.opaque[0] |= ctx->stencil_ref.ref_value[0];
 
                 /* If back-stencil is not enabled, use the front values */
 
@@ -613,11 +616,10 @@
                         panfrost_make_stencil_state(&zsa->stencil[1],
                                                     &fragmeta->stencil_back);
                         fragmeta->stencil_mask_back = zsa->stencil[1].writemask;
-                        fragmeta->stencil_back.ref = ctx->stencil_ref.ref_value[1];
+                        fragmeta->stencil_back.opaque[0] |= ctx->stencil_ref.ref_value[1];
                 } else {
                         fragmeta->stencil_back = fragmeta->stencil_front;
                         fragmeta->stencil_mask_back = fragmeta->stencil_mask_front;
-                        fragmeta->stencil_back.ref = fragmeta->stencil_front.ref;
                 }
 
                 if (zsa->depth.enabled)
diff --git a/src/panfrost/include/panfrost-job.h b/src/panfrost/include/panfrost-job.h
index 6d409a5..7f8e85e 100644
--- a/src/panfrost/include/panfrost-job.h
+++ b/src/panfrost/include/panfrost-job.h
@@ -85,19 +85,6 @@
 #define MALI_DEPTH_RANGE_B	(1 << 13)
 #define MALI_NO_MSAA		(1 << 14)
 
-/* Stencil test state is all encoded in a single u32, just with a lot of
- * enums... */
-
-struct mali_stencil_test {
-        unsigned ref  			: 8;
-        unsigned mask 			: 8;
-        enum mali_func func 		: 3;
-        enum mali_stencil_op sfail 	: 3;
-        enum mali_stencil_op dpfail 	: 3;
-        enum mali_stencil_op dppass 	: 3;
-        unsigned zero			: 4;
-} __attribute__((packed));
-
 #define MALI_MASK_R (1 << 0)
 #define MALI_MASK_G (1 << 1)
 #define MALI_MASK_B (1 << 2)
@@ -590,8 +577,8 @@
         u8 stencil_mask_back;
         u16 unknown2_4;
 
-        struct mali_stencil_test stencil_front;
-        struct mali_stencil_test stencil_back;
+        struct mali_stencil_packed stencil_front;
+        struct mali_stencil_packed stencil_back;
 
         union {
                 struct {
diff --git a/src/panfrost/lib/decode.c b/src/panfrost/lib/decode.c
index 83d762d..487d23c 100644
--- a/src/panfrost/lib/decode.c
+++ b/src/panfrost/lib/decode.c
@@ -1589,36 +1589,6 @@
 }
 
 static void
-pandecode_stencil(const char *name, const struct mali_stencil_test *stencil)
-{
-        unsigned any_nonzero =
-                stencil->ref | stencil->mask | stencil->func |
-                stencil->sfail | stencil->dpfail | stencil->dppass;
-
-        if (any_nonzero == 0)
-                return;
-
-        const char *func = mali_func_as_str(stencil->func);
-        const char *sfail = mali_stencil_op_as_str(stencil->sfail);
-        const char *dpfail = mali_stencil_op_as_str(stencil->dpfail);
-        const char *dppass = mali_stencil_op_as_str(stencil->dppass);
-
-        if (stencil->zero)
-                pandecode_msg("XXX: stencil zero tripped: %X\n", stencil->zero);
-
-        pandecode_log(".stencil_%s = {\n", name);
-        pandecode_indent++;
-        pandecode_prop("ref = %d", stencil->ref);
-        pandecode_prop("mask = 0x%02X", stencil->mask);
-        pandecode_prop("func = %s", func);
-        pandecode_prop("sfail = %s", sfail);
-        pandecode_prop("dpfail = %s", dpfail);
-        pandecode_prop("dppass = %s", dppass);
-        pandecode_indent--;
-        pandecode_log("},\n");
-}
-
-static void
 pandecode_blend_equation(const struct mali_blend_equation *blend)
 {
         if (blend->zero1)
@@ -2705,8 +2675,8 @@
                         pandecode_prop("stencil_mask_back = 0x%02X", s->stencil_mask_back);
                 }
 
-                pandecode_stencil("front", &s->stencil_front);
-                pandecode_stencil("back", &s->stencil_back);
+                DUMP_CL("Stencil front", STENCIL, &s->stencil_front, 1);
+                DUMP_CL("Stencil back", STENCIL, &s->stencil_back, 1);
 
                 if (is_bifrost) {
                         pandecode_log(".bifrost2 = {\n");
diff --git a/src/panfrost/lib/midgard.xml b/src/panfrost/lib/midgard.xml
index 54493a4..d3a40da 100644
--- a/src/panfrost/lib/midgard.xml
+++ b/src/panfrost/lib/midgard.xml
@@ -60,6 +60,15 @@
     <value name="Mirrored Clamp to Border" value="15"/>
   </enum>
 
+  <struct name="Stencil">
+    <field name="Reference Value" size="8" start="0" type="uint"/>
+    <field name="Mask" size="8" start="8" type="uint" default="0xFF"/>
+    <field name="Compare Function" size="3" start="16" type="Func"/>
+    <field name="Stencil Fail" size="3" start="19" type="Stencil Op"/>
+    <field name="Depth Fail" size="3" start="22" type="Stencil Op"/>
+    <field name="Depth Pass" size="3" start="25" type="Stencil Op"/>
+  </struct>
+
   <struct name="Uniform Buffer">
     <field name="Entries" size="12" start="0" type="uint" modifier="minus(1)"/>
     <field name="Pointer" size="52" start="12" type="address" modifier="shr(4)" element="16" count="Entries"/>
diff --git a/src/panfrost/lib/pan_blit.c b/src/panfrost/lib/pan_blit.c
index cbf58f4..b93fcee 100644
--- a/src/panfrost/lib/pan_blit.c
+++ b/src/panfrost/lib/pan_blit.c
@@ -206,12 +206,12 @@
                 .format = MALI_RGBA32F
         };
 
-        struct mali_stencil_test stencil = {
-                .mask = 0xFF,
-                .func = MALI_FUNC_ALWAYS,
-                .sfail = MALI_STENCIL_OP_REPLACE,
-                .dpfail = MALI_STENCIL_OP_REPLACE,
-                .dppass = MALI_STENCIL_OP_REPLACE,
+        struct mali_stencil_packed stencil;
+        pan_pack(&stencil, STENCIL, cfg) {
+                cfg.compare_function = MALI_FUNC_ALWAYS;
+                cfg.stencil_fail = MALI_STENCIL_OP_REPLACE;
+                cfg.depth_fail = MALI_STENCIL_OP_REPLACE;
+                cfg.depth_pass = MALI_STENCIL_OP_REPLACE;
         };
 
         union midgard_blend replace = {