v3dv: stencil state fixes

First this makes it so we only clear dirty stencil state if we actually
emit the stencil packets. Second, now we check if we need to emit stencil
whenever a new pipeline is bound, since a new pipeline may not change the
dynamic stencil state but might still be changing other aspects of stencil,
which means that even if the dynamic stencil state is not dirty, we might
still need to emit new stencil packets.

This fixes a regression in VkRunner test depth-buffer.vk_shader_test after
we dropped the redundant emission of stencil state, since that redundant
emission was happening unconditionally whenever we had a new pipeline.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6766>
diff --git a/src/broadcom/vulkan/v3dv_cmd_buffer.c b/src/broadcom/vulkan/v3dv_cmd_buffer.c
index e414ac9..d9ba711 100644
--- a/src/broadcom/vulkan/v3dv_cmd_buffer.c
+++ b/src/broadcom/vulkan/v3dv_cmd_buffer.c
@@ -2004,6 +2004,7 @@
                                            V3DV_DYNAMIC_STENCIL_WRITE_MASK |
                                            V3DV_DYNAMIC_STENCIL_REFERENCE;
 
+   bool emitted_stencil = false;
    for (uint32_t i = 0; i < 2; i++) {
       if (pipeline->emit_stencil_cfg[i]) {
          if (dynamic_state->mask & dynamic_stencil_states) {
@@ -2028,14 +2029,18 @@
          } else {
             cl_emit_prepacked(&job->bcl, &pipeline->stencil_cfg[i]);
          }
+
+         emitted_stencil = true;
       }
    }
 
-   const uint32_t dynamic_stencil_dirty_flags =
-      V3DV_CMD_DIRTY_STENCIL_COMPARE_MASK |
-      V3DV_CMD_DIRTY_STENCIL_WRITE_MASK |
-      V3DV_CMD_DIRTY_STENCIL_REFERENCE;
-   cmd_buffer->state.dirty &= ~dynamic_stencil_dirty_flags;
+   if (emitted_stencil) {
+      const uint32_t dynamic_stencil_dirty_flags =
+               V3DV_CMD_DIRTY_STENCIL_COMPARE_MASK |
+               V3DV_CMD_DIRTY_STENCIL_WRITE_MASK |
+               V3DV_CMD_DIRTY_STENCIL_REFERENCE;
+      cmd_buffer->state.dirty &= ~dynamic_stencil_dirty_flags;
+   }
 }
 
 static void
@@ -2444,7 +2449,7 @@
       V3DV_CMD_DIRTY_STENCIL_COMPARE_MASK |
       V3DV_CMD_DIRTY_STENCIL_WRITE_MASK |
       V3DV_CMD_DIRTY_STENCIL_REFERENCE;
-   if (*dirty & dynamic_stencil_dirty_flags)
+   if (*dirty & (V3DV_CMD_DIRTY_PIPELINE | dynamic_stencil_dirty_flags))
       emit_stencil(cmd_buffer);
 
    if (*dirty & (V3DV_CMD_DIRTY_PIPELINE | V3DV_CMD_DIRTY_BLEND_CONSTANTS))