refactor(renderer): Consolidate MSAA depth/stencil settings

All the MSAA stencil settings and various subpasses were previously spaghettified directly into the GL backend. This PR organizes them into proper subpasses at the renderContext level, and introduces a PipelineState struct to describe as much state as possible (including stencil) in a backend-agnostic way.

This seriously cleans up the GL backend, paves the way to implementing MSAA in other backends, and improves batching & reordering now that each MSAA subpass is individually sorted into the intersection board.

Diffs=
689a455e67 refactor(renderer): Consolidate MSAA depth/stencil settings (#9436)

Co-authored-by: Chris Dalton <99840794+csmartdalton@users.noreply.github.com>
diff --git a/.rive_head b/.rive_head
index b4e17f2..11648ec 100644
--- a/.rive_head
+++ b/.rive_head
@@ -1 +1 @@
-099d761edd24a57e98239b6615ff4f26665dbc4f
+689a455e677b68ac54ce7af3bbc6bd28b617a7cc
diff --git a/renderer/include/rive/renderer/draw.hpp b/renderer/include/rive/renderer/draw.hpp
index f8551cd..767ac3c 100644
--- a/renderer/include/rive/renderer/draw.hpp
+++ b/renderer/include/rive/renderer/draw.hpp
@@ -32,7 +32,7 @@
 public:
     // Use a "fullscreen" bounding box that is reasonably larger than any
     // screen, but not so big that it runs the risk of overflowing.
-    constexpr static IAABB kFullscreenPixelBounds = {0, 0, 1 << 24, 1 << 24};
+    constexpr static IAABB FULLSCREEN_PIXEL_BOUNDS = {0, 0, 1 << 24, 1 << 24};
 
     enum class Type : uint8_t
     {
@@ -104,7 +104,7 @@
     const Draw* nextDstRead() const { return m_nextDstRead; }
 
     // Finalizes m_prepassCount and m_subpassCount.
-    virtual void determineSubpasses()
+    virtual void countSubpasses()
     {
         // The subclass must set m_prepassCount and m_subpassCount in this call
         // if they are not 0 & 1.
@@ -153,7 +153,7 @@
 
     // Before issuing the main draws, the renderContext may do a front-to-back
     // pass. Any draw who wants to participate in front-to-back rendering can
-    // register a positive prepass count during determineSubpasses().
+    // register a positive prepass count during countSubpasses().
     //
     // For prepasses, pushToRenderContext() gets called with subpassIndex
     // values: [-m_prepassCount, .., -1].
@@ -161,7 +161,7 @@
 
     // This is the number of low-level draws that the draw requires during main
     // (back-to-front) rendering. A draw can register the number of subpasses it
-    // requires during determineSubpasses().
+    // requires during countSubpasses().
     //
     // For subpasses, pushToRenderContext() gets called with subpassIndex
     // values: [0, .., m_subpassCount - 1].
@@ -253,7 +253,7 @@
     GrInnerFanTriangulator* triangulator() const { return m_triangulator; }
 
     bool allocateResources(RenderContext::LogicalFlush*) override;
-    void determineSubpasses() override;
+    void countSubpasses() override;
 
     void pushToRenderContext(RenderContext::LogicalFlush*,
                              int subpassIndex) override;
@@ -291,9 +291,28 @@
                                       RawPath*,
                                       TriangulatorAxis);
 
+    uint32_t allocateTessellationVertices(RenderContext::LogicalFlush* flush,
+                                          uint32_t tessVertexCount)
+    {
+        if (m_triangulator != nullptr)
+            return flush->allocateOuterCubicTessVertices(tessVertexCount);
+        else
+            return flush->allocateMidpointFanTessVertices(tessVertexCount);
+    }
+
+    // Calls LogicalFlush::pushOuterCubicsDraw() or
+    // LogicalFlush::pushMidpointFanDraw() for this PathDraw.
+    void pushTessellationDraw(
+        RenderContext::LogicalFlush*,
+        uint32_t tessVertexCount,
+        uint32_t tessLocation,
+        gpu::ShaderMiscFlags = gpu::ShaderMiscFlags::none);
+
+    // Pushes TessVertexSpans that will tessellate this PathDraw at the given
+    // location.
     void pushTessellationData(RenderContext::LogicalFlush*,
-                              uint32_t* tessVertexCount,
-                              uint32_t* tessLocation);
+                              uint32_t tessVertexCount,
+                              uint32_t tessLocation);
 
     // Pushes the contours and cubics to the renderContext for a
     // "midpointFanPatches" draw.
@@ -383,10 +402,17 @@
     // Unique ID used by shaders for the current frame.
     uint32_t m_pathID = 0;
 
-    // Used in clockwiseAtomic mode. The negative triangles get rendered in a
-    // separate prepass, and their tessellations need to be allocated before the
-    // main subpass pushes the path to the renderContext.
-    uint32_t m_prepassTessLocation = 0;
+    union
+    {
+        // Used in clockwiseAtomic mode. The negative triangles get rendered in
+        // a separate prepass, and their tessellations need to be allocated
+        // before the main subpass pushes the path to the renderContext.
+        uint32_t m_prepassTessLocation = 0;
+
+        // Used in msaa mode. Multiple msaa subpasses use the same tesellation
+        // data.
+        uint32_t m_msaaTessLocation;
+    };
 
     // Used to guarantee m_pathRef doesn't change for the entire time we hold
     // it.
diff --git a/renderer/include/rive/renderer/gl/gl_state.hpp b/renderer/include/rive/renderer/gl/gl_state.hpp
index 71fc3e1..c30956a 100644
--- a/renderer/include/rive/renderer/gl/gl_state.hpp
+++ b/renderer/include/rive/renderer/gl/gl_state.hpp
@@ -10,6 +10,8 @@
 
 namespace rive::gpu
 {
+struct PipelineState;
+
 // Lightweight wrapper around common GL state.
 class GLState : public RefCnt<GLState>
 {
@@ -23,6 +25,13 @@
 
     void invalidate();
 
+    void setDepthStencilEnabled(bool depthEnabled, bool stencilEnabled);
+    void setWriteMasks(bool colorWriteMask,
+                       bool depthWriteMask,
+                       uint8_t stencilWriteMask);
+    void setCullFace(GLenum);
+    void setPipelineState(const gpu::PipelineState&);
+
     enum class GLBlendMode
     {
         none = 0,
@@ -54,11 +63,6 @@
     }
     void disableBlending() { setGLBlendMode(GLBlendMode::none); }
 
-    void setWriteMasks(bool colorWriteMask,
-                       bool depthWriteMask,
-                       GLuint stencilWriteMask);
-    void setCullFace(GLenum);
-
     void bindProgram(GLuint);
     void bindVAO(GLuint);
     void bindBuffer(GLenum target, GLuint);
@@ -70,6 +74,8 @@
 private:
     const GLCapabilities m_capabilities;
     GLBlendMode m_blendMode;
+    bool m_depthTestEnabled;
+    bool m_stencilTestEnabled;
     bool m_colorWriteMask;
     bool m_depthWriteMask;
     GLuint m_stencilWriteMask;
@@ -82,6 +88,7 @@
     struct
     {
         bool blendEquation : 1;
+        bool depthStencilEnabled : 1;
         bool writeMasks : 1;
         bool cullFace : 1;
         bool boundProgramID : 1;
diff --git a/renderer/include/rive/renderer/gl/render_context_gl_impl.hpp b/renderer/include/rive/renderer/gl/render_context_gl_impl.hpp
index b3c8172..b37cb51 100644
--- a/renderer/include/rive/renderer/gl/render_context_gl_impl.hpp
+++ b/renderer/include/rive/renderer/gl/render_context_gl_impl.hpp
@@ -146,6 +146,85 @@
                         GLCapabilities,
                         std::unique_ptr<PixelLocalStorageImpl>);
 
+    std::unique_ptr<BufferRing> makeUniformBufferRing(
+        size_t capacityInBytes) override;
+    std::unique_ptr<BufferRing> makeStorageBufferRing(
+        size_t capacityInBytes,
+        gpu::StorageBufferStructure) override;
+    std::unique_ptr<BufferRing> makeVertexBufferRing(
+        size_t capacityInBytes) override;
+
+    void resizeGradientTexture(uint32_t width, uint32_t height) override;
+    void resizeTessellationTexture(uint32_t width, uint32_t height) override;
+    void resizeAtlasTexture(uint32_t width, uint32_t height) override;
+
+    void flush(const FlushDescriptor&) override;
+
+    // Issues the equivalent of glDrawElementsInstancedBaseInstanceEXT(),
+    // assuming no vertex attribs are instanced, indices are uint16_t, and
+    // applying workarounds for known driver bugs.
+    //
+    // If ANGLE_base_vertex_base_instance_shader_builtin is not supported,
+    // the intended value of gl_BaseInstance is supplied via
+    // baseInstanceUniformLocation.
+    void drawIndexedInstancedNoInstancedAttribs(
+        GLenum primitiveTopology,
+        uint32_t indexCount,
+        uint32_t baseIndex,
+        uint32_t instanceCount,
+        uint32_t baseInstance,
+        GLint baseInstanceUniformLocation);
+
+    GLCapabilities m_capabilities;
+
+    std::unique_ptr<PixelLocalStorageImpl> m_plsImpl;
+
+    // Gradient texture rendering.
+    glutils::Program m_colorRampProgram;
+    glutils::VAO m_colorRampVAO;
+    glutils::Framebuffer m_colorRampFBO;
+    GLuint m_gradientTexture = 0;
+
+    // Gaussian integral table for feathering.
+    glutils::Texture m_featherTexture;
+
+    // Tessellation texture rendering.
+    glutils::Program m_tessellateProgram;
+    glutils::VAO m_tessellateVAO;
+    glutils::Buffer m_tessSpanIndexBuffer;
+    glutils::Framebuffer m_tessellateFBO;
+    GLuint m_tessVertexTexture = 0;
+
+    // Atlas rendering.
+    class AtlasProgram
+    {
+    public:
+        void compile(GLuint vertexShaderID,
+                     const char* defines[],
+                     size_t numDefines,
+                     const char* sources[],
+                     size_t numSources,
+                     const GLCapabilities&,
+                     GLState* state);
+
+        operator GLuint() const { return m_program; }
+
+        GLint baseInstanceUniformLocation() const
+        {
+            return m_baseInstanceUniformLocation;
+        }
+
+    private:
+        glutils::Program m_program = glutils::Program::Zero();
+        GLint m_baseInstanceUniformLocation = -1;
+    };
+
+    glutils::Shader m_atlasVertexShader;
+    AtlasProgram m_atlasFillProgram;
+    AtlasProgram m_atlasStrokeProgram;
+    glutils::Texture m_atlasTexture = glutils::Texture::Zero();
+    glutils::Framebuffer m_atlasFBO;
+
     // Wraps a compiled GL shader of draw_path.glsl or draw_image_mesh.glsl,
     // either vertex or fragment, with a specific set of features enabled via
     // #define. The set of features to enable is dictated by ShaderFeatures.
@@ -198,70 +277,6 @@
         const rcp<GLState> m_state;
     };
 
-    std::unique_ptr<BufferRing> makeUniformBufferRing(
-        size_t capacityInBytes) override;
-    std::unique_ptr<BufferRing> makeStorageBufferRing(
-        size_t capacityInBytes,
-        gpu::StorageBufferStructure) override;
-    std::unique_ptr<BufferRing> makeVertexBufferRing(
-        size_t capacityInBytes) override;
-
-    void resizeGradientTexture(uint32_t width, uint32_t height) override;
-    void resizeTessellationTexture(uint32_t width, uint32_t height) override;
-    void resizeAtlasTexture(uint32_t width, uint32_t height) override;
-
-    void flush(const FlushDescriptor&) override;
-
-    GLCapabilities m_capabilities;
-
-    std::unique_ptr<PixelLocalStorageImpl> m_plsImpl;
-
-    // Gradient texture rendering.
-    glutils::Program m_colorRampProgram;
-    glutils::VAO m_colorRampVAO;
-    glutils::Framebuffer m_colorRampFBO;
-    GLuint m_gradientTexture = 0;
-
-    // Gaussian integral table for feathering.
-    glutils::Texture m_featherTexture;
-
-    // Tessellation texture rendering.
-    glutils::Program m_tessellateProgram;
-    glutils::VAO m_tessellateVAO;
-    glutils::Buffer m_tessSpanIndexBuffer;
-    glutils::Framebuffer m_tessellateFBO;
-    GLuint m_tessVertexTexture = 0;
-
-    // Atlas rendering.
-    class AtlasProgram
-    {
-    public:
-        void compile(GLuint vertexShaderID,
-                     const char* defines[],
-                     size_t numDefines,
-                     const char* sources[],
-                     size_t numSources,
-                     const GLCapabilities&,
-                     GLState* state);
-
-        operator GLuint() const { return m_program; }
-
-        GLint baseInstanceUniformLocation() const
-        {
-            return m_baseInstanceUniformLocation;
-        }
-
-    private:
-        glutils::Program m_program = glutils::Program::Zero();
-        GLint m_baseInstanceUniformLocation = -1;
-    };
-
-    glutils::Shader m_atlasVertexShader;
-    AtlasProgram m_atlasFillProgram;
-    AtlasProgram m_atlasStrokeProgram;
-    glutils::Texture m_atlasTexture = glutils::Texture::Zero();
-    glutils::Framebuffer m_atlasFBO;
-
     // Not all programs have a unique vertex shader, so we cache and reuse them
     // where possible.
     std::map<uint32_t, DrawShader> m_vertexShaders;
diff --git a/renderer/include/rive/renderer/gpu.hpp b/renderer/include/rive/renderer/gpu.hpp
index 095750c..f73b298 100644
--- a/renderer/include/rive/renderer/gpu.hpp
+++ b/renderer/include/rive/renderer/gpu.hpp
@@ -9,7 +9,6 @@
 #include "rive/math/mat2d.hpp"
 #include "rive/math/vec2d.hpp"
 #include "rive/math/simd.hpp"
-#include "rive/refcnt.hpp"
 #include "rive/shapes/paint/blend_mode.hpp"
 #include "rive/shapes/paint/color.hpp"
 #include "rive/renderer/trivial_block_allocator.hpp"
@@ -602,20 +601,45 @@
 
 enum class DrawType : uint8_t
 {
-    midpointFanPatches,         // Fills, strokes, feathered strokes.
-    midpointFanCenterAAPatches, // Feathered fills.
-    outerCurvePatches, // Just the outer curves of a path; the interior will be
-                       // triangulated.
+    // Fills, strokes, feathered strokes.
+    midpointFanPatches,
+
+    // Feathered fills.
+    midpointFanCenterAAPatches,
+
+    // Just the outer curves of a path; the interior will be triangulated.
+    outerCurvePatches,
+
     interiorTriangulation,
     atlasBlit,
     imageRect,
     imageMesh,
-    atomicInitialize, // Clear/init PLS data when we can't do it with existing
-                      // clear/load APIs.
-    atomicResolve, // Resolve PLS data to the final renderTarget color in atomic
-                   // mode.
-    stencilClipReset, // Clear or intersect (based on DrawContents) the stencil
-                      // clip bit.
+
+    // Clear/init PLS data when we can't do it with existing clear/load APIs.
+    atomicInitialize,
+
+    // Resolve PLS data to the final renderTarget color in atomic mode.
+    atomicResolve,
+
+    // MSAA strokes can't be merged with fills because they require their own
+    // dedicated stencil settings.
+    msaaStrokes,
+
+    // MSAA "fast" path: (effectively) single pass rendering.
+    msaaMidpointFanBorrowedCoverage,
+    msaaMidpointFans,
+    msaaMidpointFanStencilReset,
+
+    // MSAA "slow" path: stencil-then-cover.
+    msaaMidpointFanPathsStencil,
+    msaaMidpointFanPathsCover,
+
+    // MSAA interior triangulation is not currently supported, but this one draw
+    // type is included in order to support the "retrofittedcubictriangles" GM.
+    msaaOuterCubics,
+
+    // Clear or intersect (based on DrawContents) the stencil clip bit.
+    msaaStencilClipReset,
 };
 
 constexpr static bool DrawTypeIsImageDraw(DrawType drawType)
@@ -632,7 +656,14 @@
         case DrawType::atlasBlit:
         case DrawType::atomicInitialize:
         case DrawType::atomicResolve:
-        case DrawType::stencilClipReset:
+        case DrawType::msaaStrokes:
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaMidpointFanStencilReset:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaMidpointFanPathsCover:
+        case DrawType::msaaOuterCubics:
+        case DrawType::msaaStencilClipReset:
             return false;
     }
     RIVE_UNREACHABLE();
@@ -648,72 +679,62 @@
             return kMidpointFanCenterAAPatchIndexCount;
         case DrawType::outerCurvePatches:
             return kOuterCurvePatchIndexCount;
-        case DrawType::interiorTriangulation:
-        case DrawType::atlasBlit:
-        case DrawType::imageRect:
-        case DrawType::imageMesh:
-        case DrawType::atomicInitialize:
-        case DrawType::atomicResolve:
-        case DrawType::stencilClipReset:
-            RIVE_UNREACHABLE();
-    }
-    RIVE_UNREACHABLE();
-}
-
-constexpr static uint32_t PatchBorderIndexCount(DrawType drawType)
-{
-    switch (drawType)
-    {
-        case DrawType::midpointFanPatches:
+        case DrawType::msaaStrokes:
             return kMidpointFanPatchBorderIndexCount;
-        case DrawType::midpointFanCenterAAPatches:
-            return kMidpointFanCenterAAPatchBorderIndexCount;
-        case DrawType::outerCurvePatches:
-            return kOuterCurvePatchBorderIndexCount;
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaMidpointFanStencilReset:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaMidpointFanPathsCover:
+            return kMidpointFanPatchIndexCount -
+                   kMidpointFanPatchBorderIndexCount;
+        case DrawType::msaaOuterCubics:
+            return kOuterCurvePatchIndexCount -
+                   kOuterCurvePatchBorderIndexCount;
         case DrawType::interiorTriangulation:
         case DrawType::atlasBlit:
         case DrawType::imageRect:
         case DrawType::imageMesh:
         case DrawType::atomicInitialize:
         case DrawType::atomicResolve:
-        case DrawType::stencilClipReset:
+        case DrawType::msaaStencilClipReset:
             RIVE_UNREACHABLE();
     }
     RIVE_UNREACHABLE();
 }
 
-constexpr static uint32_t PatchFanIndexCount(DrawType drawType)
-{
-    return PatchIndexCount(drawType) - PatchBorderIndexCount(drawType);
-}
-
 constexpr static uint32_t PatchBaseIndex(DrawType drawType)
 {
     switch (drawType)
     {
         case DrawType::midpointFanPatches:
+        case DrawType::msaaStrokes:
             return kMidpointFanPatchBaseIndex;
         case DrawType::midpointFanCenterAAPatches:
             return kMidpointFanCenterAAPatchBaseIndex;
         case DrawType::outerCurvePatches:
             return kOuterCurvePatchBaseIndex;
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaMidpointFanStencilReset:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaMidpointFanPathsCover:
+            return kMidpointFanPatchBaseIndex +
+                   kMidpointFanPatchBorderIndexCount;
+        case DrawType::msaaOuterCubics:
+            return kOuterCurvePatchBaseIndex + kOuterCurvePatchBorderIndexCount;
         case DrawType::interiorTriangulation:
         case DrawType::atlasBlit:
         case DrawType::imageRect:
         case DrawType::imageMesh:
         case DrawType::atomicInitialize:
         case DrawType::atomicResolve:
-        case DrawType::stencilClipReset:
+        case DrawType::msaaStencilClipReset:
             RIVE_UNREACHABLE();
     }
     RIVE_UNREACHABLE();
 }
 
-constexpr static uint32_t PatchFanBaseIndex(DrawType drawType)
-{
-    return PatchBaseIndex(drawType) + PatchBorderIndexCount(drawType);
-}
-
 // Specifies what to do with the render target at the beginning of a flush.
 enum class LoadAction
 {
@@ -859,6 +880,13 @@
         case DrawType::midpointFanCenterAAPatches:
         case DrawType::outerCurvePatches:
         case DrawType::interiorTriangulation:
+        case DrawType::msaaStrokes:
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaMidpointFanStencilReset:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaMidpointFanPathsCover:
+        case DrawType::msaaOuterCubics:
         case DrawType::atomicResolve:
             mask = kAllShaderFeatures;
             break;
@@ -867,7 +895,7 @@
             mask = ShaderFeatures::ENABLE_CLIPPING |
                    ShaderFeatures::ENABLE_ADVANCED_BLEND;
             break;
-        case DrawType::stencilClipReset:
+        case DrawType::msaaStencilClipReset:
             mask = ShaderFeatures::NONE;
             break;
     }
@@ -1526,15 +1554,67 @@
     unknown
 };
 
+enum class StencilOp : uint8_t
+{
+    keep,
+    replace,
+    zero,
+    decrClamp,
+    incrWrap,
+    decrWrap
+};
+
+enum class StencilCompareOp : uint8_t
+{
+    less,
+    equal,
+    lessOrEqual,
+    notEqual,
+    always,
+};
+
+struct StencilFaceOps
+{
+    StencilOp failOp;
+    StencilOp passOp;
+    StencilOp depthFailOp;
+    StencilCompareOp compareOp;
+};
+
+enum class CullFace : uint8_t
+{
+    none,
+    clockwise,
+    counterclockwise,
+};
+
+// Common pipeline state that applies to every low-level draw and every backend.
+struct PipelineState
+{
+    bool colorWriteEnabled;
+    bool depthTestEnabled;
+    bool depthWriteEnabled;
+    bool stencilTestEnabled;
+    bool stencilDoubleSided;
+    uint8_t stencilCompareMask;
+    uint8_t stencilWriteMask;
+    uint8_t stencilReference;
+    StencilFaceOps stencilFrontOps;
+    StencilFaceOps stencilBackOps;
+    CullFace cullFace;
+};
+
+void get_pipeline_state(DrawType, InterlockMode, DrawContents, PipelineState*);
+
+float4 cast_f16_to_f32(uint16x4 x);
+uint16x4 cast_f32_to_f16(float4);
+
 // These tables integrate the gaussian function, and its inverse, covering a
 // spread of -FEATHER_TEXTURE_STDDEVS to +FEATHER_TEXTURE_STDDEVS.
 constexpr static uint32_t GAUSSIAN_TABLE_SIZE = 512;
 extern const uint16_t g_gaussianIntegralTableF16[GAUSSIAN_TABLE_SIZE];
 extern const uint16_t g_inverseGaussianIntegralTableF16[GAUSSIAN_TABLE_SIZE];
 
-float4 cast_f16_to_f32(uint16x4 x);
-uint16x4 cast_f32_to_f16(float4);
-
 // Code to generate g_gaussianIntegralTableF16 and
 // g_inverseGaussianIntegralTableF32. This is left in the codebase but #ifdef'd
 // out in case we ever want to change any parameters of the built-in tables.
diff --git a/renderer/include/rive/renderer/render_context.hpp b/renderer/include/rive/renderer/render_context.hpp
index 41c2c81..f6117ac 100644
--- a/renderer/include/rive/renderer/render_context.hpp
+++ b/renderer/include/rive/renderer/render_context.hpp
@@ -654,6 +654,7 @@
         // required, and if this is the path's first subpass.
         void pushMidpointFanDraw(
             const PathDraw*,
+            gpu::DrawType,
             uint32_t tessVertexCount,
             uint32_t tessLocation,
             gpu::ShaderMiscFlags = gpu::ShaderMiscFlags::none);
@@ -665,6 +666,7 @@
         // required, and if this is the path's first subpass.
         void pushOuterCubicsDraw(
             const PathDraw*,
+            gpu::DrawType,
             uint32_t tessVertexCount,
             uint32_t tessLocation,
             gpu::ShaderMiscFlags = gpu::ShaderMiscFlags::none);
diff --git a/renderer/premake5_pls_renderer.lua b/renderer/premake5_pls_renderer.lua
index cef4dbc..cc5b750 100644
--- a/renderer/premake5_pls_renderer.lua
+++ b/renderer/premake5_pls_renderer.lua
@@ -181,10 +181,9 @@
     filter({
         'system:windows',
         'options:toolset=msc',
-        'options:with-dawn or with-webgpu or with_vulkan',
     })
     do
-        -- Vulkan and WebGPU both make heavy use of designated initializers, which MSVC doesn't accept in C++17.
+        -- MSVC doesn't accept designated initializers in C++17.
         cppdialect('c++latest')
         defines({
             '_SILENCE_CXX20_IS_POD_DEPRECATION_WARNING',
diff --git a/renderer/src/d3d/render_context_d3d_impl.cpp b/renderer/src/d3d/render_context_d3d_impl.cpp
index 576ee7a..b7fe6fe 100644
--- a/renderer/src/d3d/render_context_d3d_impl.cpp
+++ b/renderer/src/d3d/render_context_d3d_impl.cpp
@@ -1296,7 +1296,14 @@
                 s << "#define " << GLSL_RESOLVE_PLS << '\n';
                 break;
             case DrawType::atomicInitialize:
-            case DrawType::stencilClipReset:
+            case DrawType::msaaStrokes:
+            case DrawType::msaaMidpointFanBorrowedCoverage:
+            case DrawType::msaaMidpointFans:
+            case DrawType::msaaMidpointFanStencilReset:
+            case DrawType::msaaMidpointFanPathsStencil:
+            case DrawType::msaaMidpointFanPathsCover:
+            case DrawType::msaaOuterCubics:
+            case DrawType::msaaStencilClipReset:
                 RIVE_UNREACHABLE();
         }
         s << glsl::constants << '\n';
@@ -1342,12 +1349,19 @@
                 }
                 break;
             case DrawType::atomicResolve:
-            case DrawType::stencilClipReset:
+            case DrawType::msaaStencilClipReset:
                 assert(interlockMode == gpu::InterlockMode::atomics);
                 s << gpu::glsl::draw_path_common << '\n';
                 s << gpu::glsl::atomic_draw << '\n';
                 break;
             case DrawType::atomicInitialize:
+            case DrawType::msaaStrokes:
+            case DrawType::msaaMidpointFanBorrowedCoverage:
+            case DrawType::msaaMidpointFans:
+            case DrawType::msaaMidpointFanStencilReset:
+            case DrawType::msaaMidpointFanPathsStencil:
+            case DrawType::msaaMidpointFanPathsCover:
+            case DrawType::msaaOuterCubics:
                 RIVE_UNREACHABLE();
         }
 
@@ -1425,7 +1439,14 @@
                     vertexAttribCount = 0;
                     break;
                 case DrawType::atomicInitialize:
-                case DrawType::stencilClipReset:
+                case DrawType::msaaStrokes:
+                case DrawType::msaaMidpointFanBorrowedCoverage:
+                case DrawType::msaaMidpointFans:
+                case DrawType::msaaMidpointFanStencilReset:
+                case DrawType::msaaMidpointFanPathsStencil:
+                case DrawType::msaaMidpointFanPathsCover:
+                case DrawType::msaaOuterCubics:
+                case DrawType::msaaStencilClipReset:
                     RIVE_UNREACHABLE();
             }
             VERIFY_OK(m_gpu->CreateInputLayout(layoutDesc,
@@ -2133,7 +2154,14 @@
                 m_gpuContext->Draw(4, 0);
                 break;
             case DrawType::atomicInitialize:
-            case DrawType::stencilClipReset:
+            case DrawType::msaaStrokes:
+            case DrawType::msaaMidpointFanBorrowedCoverage:
+            case DrawType::msaaMidpointFans:
+            case DrawType::msaaMidpointFanStencilReset:
+            case DrawType::msaaMidpointFanPathsStencil:
+            case DrawType::msaaMidpointFanPathsCover:
+            case DrawType::msaaOuterCubics:
+            case DrawType::msaaStencilClipReset:
                 RIVE_UNREACHABLE();
         }
     }
diff --git a/renderer/src/draw.cpp b/renderer/src/draw.cpp
index 5c87b2d..31ddb0a 100644
--- a/renderer/src/draw.cpp
+++ b/renderer/src/draw.cpp
@@ -1343,6 +1343,8 @@
 
 bool PathDraw::allocateResources(RenderContext::LogicalFlush* flush)
 {
+    const RenderContext::FrameDescriptor& frameDesc = flush->frameDescriptor();
+
     // Allocate a gradient if needed. Do this first since it's more expensive to
     // fail after setting up an atlas draw than a gradient draw.
     if (m_gradientRef != nullptr &&
@@ -1363,8 +1365,8 @@
         IAABB renderTargetBounds = {
             0,
             0,
-            static_cast<int32_t>(flush->frameDescriptor().renderTargetWidth),
-            static_cast<int32_t>(flush->frameDescriptor().renderTargetHeight),
+            static_cast<int32_t>(frameDesc.renderTargetWidth),
+            static_cast<int32_t>(frameDesc.renderTargetHeight),
         };
         IAABB visibleBounds = renderTargetBounds.intersect(m_pixelBounds);
 
@@ -1418,35 +1420,70 @@
     return true;
 }
 
-void PathDraw::determineSubpasses()
+void PathDraw::countSubpasses()
 {
-    // Interior triangulation has two subpasses: outer cubics and interior
-    // triangles.
-    m_subpassCount = m_triangulator != nullptr ? 2 : 1;
+    m_subpassCount = 1;
     m_prepassCount = 0;
 
-    if (m_coverageType == CoverageType::clockwiseAtomic && !isStroke())
+    switch (m_coverageType)
     {
-        // clockwiseAtomic fills need a prepass to render their borrowed
-        // coverage.
-        m_prepassCount = m_subpassCount;
-    }
-    else if (m_coverageType == CoverageType::msaa && isOpaque())
-    {
-        // In msaa mode we can draw opaque paths front-to-back by converting
-        // them to a prepass. This takes advantage of early Z culling.
-        //
-        // FIXME: To keep things simple initially, we don't reverse-sort draws
-        // that use clipping.
-        bool usesClipping = drawContents() & (gpu::DrawContents::activeClip |
-                                              gpu::DrawContents::clipUpdate);
-        if (!usesClipping)
+        case CoverageType::pixelLocalStorage:
+        case CoverageType::atlas:
+            m_subpassCount = 1;
+            break;
+
+        case CoverageType::clockwiseAtomic:
+            if (!isStroke())
+            {
+                m_prepassCount = 1; // Borrowed coverage.
+            }
+            m_subpassCount = 1;
+            break;
+
+        case CoverageType::msaa:
         {
-            // Render this path front-to-back instead of back-to-front.
-            assert(m_prepassCount == 0);
-            std::swap(m_subpassCount, m_prepassCount);
+            if (isStroke())
+            {
+                m_subpassCount = 1; // Strokes can be rendered in a single pass.
+            }
+            else if ((m_drawContents & gpu::kNestedClipUpdateMask) ==
+                     gpu::kNestedClipUpdateMask)
+            {
+                // Nested clip updates only have a stencil pass. (The reset is
+                // handled by a separate msaaStencilClipReset draw.)
+                m_subpassCount = 1;
+            }
+            else if (m_drawContents & gpu::DrawContents::evenOddFill)
+            {
+                m_subpassCount = 2; // MSAA "slow" path: stencil-then-cover.
+            }
+            else
+            {
+                // MSAA "fast" path: (effectively) single pass rendering.
+                m_subpassCount = 3;
+            }
+            if (isOpaque())
+            {
+                const bool usesClipping =
+                    m_drawContents & (gpu::DrawContents::activeClip |
+                                      gpu::DrawContents::clipUpdate);
+                if (!usesClipping)
+                {
+                    // Render this path front-to-back instead of back-to-front.
+                    assert(m_prepassCount == 0);
+                    m_prepassCount = m_subpassCount;
+                    m_subpassCount = 0;
+                }
+            }
         }
     }
+
+    if (m_triangulator != nullptr)
+    {
+        // Each tessellation draw has a corresponding interior triangles draw.
+        m_prepassCount *= 2;
+        m_subpassCount *= 2;
+    }
 }
 
 void PathDraw::pushToRenderContext(RenderContext::LogicalFlush* flush,
@@ -1457,8 +1494,12 @@
     assert(m_rawPathMutationID == m_pathRef->getRawPathMutationID());
     assert(!m_pathRef->getRawPath().empty());
 
-    if ((m_resourceCounts.outerCubicTessVertexCount |
-         m_resourceCounts.midpointFanTessVertexCount) == 0)
+    assert(m_resourceCounts.outerCubicTessVertexCount == 0 ||
+           m_resourceCounts.midpointFanTessVertexCount == 0);
+    uint32_t tessVertexCount = math::lossless_numeric_cast<uint32_t>(
+        m_resourceCounts.outerCubicTessVertexCount |
+        m_resourceCounts.midpointFanTessVertexCount);
+    if (tessVertexCount == 0)
     {
         return;
     }
@@ -1469,125 +1510,164 @@
         m_pathID = flush->pushPath(this);
     }
 
-    bool clockwiseAtomicFill =
-        !isStroke() && m_coverageType == CoverageType::clockwiseAtomic;
-
-    if (m_coverageType == CoverageType::atlas)
+    switch (m_coverageType)
     {
-        // Atlas draws only have one subpass -- the rectangular blit from the
-        // atlas to the screen. The step that renders coverage to the offscreen
-        // atlas is handled separately, outside the subpass system.
-        assert(!clockwiseAtomicFill);
-        assert(subpassIndex == 0);
-        flush->pushAtlasBlit(this, m_pathID);
-        return;
-    }
-
-    if (m_coverageType == CoverageType::msaa)
-    {
-        // MSAA draws only have one subpass for now.
-        assert(!clockwiseAtomicFill);
-        assert(m_prepassCount + subpassIndex == 0);
-        assert(!m_triangulator);
-        uint32_t tessVertexCount, tessLocation;
-        pushTessellationData(flush, &tessVertexCount, &tessLocation);
-        assert(tessVertexCount == m_resourceCounts.midpointFanTessVertexCount);
-        flush->pushMidpointFanDraw(this, tessVertexCount, tessLocation);
-        return;
-    }
-
-    switch (subpassIndex)
-    {
-        case -2: // Borrowed coverage prepass for interior triangulation.
+        case CoverageType::pixelLocalStorage:
         {
-            assert(clockwiseAtomicFill);
-            assert(m_triangulator != nullptr);
-            size_t actualCount RIVE_MAYBE_UNUSED =
-                flush->pushInteriorTriangulationDraw(
-                    this,
-                    m_pathID,
-                    gpu::WindingFaces::negative,
-                    gpu::ShaderMiscFlags::borrowedCoveragePrepass);
-            RIVE_DEBUG_CODE(m_numInteriorTriangleVerticesPushed += actualCount;)
-            assert(m_numInteriorTriangleVerticesPushed <=
-                   m_triangulator->maxVertexCount());
-            break;
-        }
-
-        case -1: // Borrowed coverage prepass for path tessellation.
-        {
-            assert(clockwiseAtomicFill);
-            // Allocate tessellation vertices and push a draw for the prepass.
-            // The tessellation vertices get filled in later by the main
-            // subpass.
-            if (m_triangulator != nullptr)
+            if (subpassIndex == 0)
             {
-                // Draw half the vertices now.
-                assert(m_resourceCounts.outerCubicTessVertexCount % 2 == 0);
-                auto prepassTessVertexCount =
-                    math::lossless_numeric_cast<uint32_t>(
-                        m_resourceCounts.outerCubicTessVertexCount / 2);
-                m_prepassTessLocation = flush->allocateOuterCubicTessVertices(
-                    prepassTessVertexCount);
-                flush->pushOuterCubicsDraw(
-                    this,
-                    prepassTessVertexCount,
-                    m_prepassTessLocation,
-                    gpu::ShaderMiscFlags::borrowedCoveragePrepass);
+                // Tessellation (midpoint fan or outer cubic).
+                uint32_t tessLocation =
+                    allocateTessellationVertices(flush, tessVertexCount);
+                pushTessellationData(flush, tessVertexCount, tessLocation);
+                pushTessellationDraw(flush, tessVertexCount, tessLocation);
             }
             else
             {
-                // Draw half the vertices now.
-                assert(m_resourceCounts.midpointFanTessVertexCount % 2 == 0);
-                auto prepassTessVertexCount =
-                    math::lossless_numeric_cast<uint32_t>(
-                        m_resourceCounts.midpointFanTessVertexCount / 2);
-                m_prepassTessLocation = flush->allocateMidpointFanTessVertices(
-                    prepassTessVertexCount);
-                flush->pushMidpointFanDraw(
-                    this,
-                    prepassTessVertexCount,
-                    m_prepassTessLocation,
-                    gpu::ShaderMiscFlags::borrowedCoveragePrepass);
+                // Interior triangles.
+                assert(m_triangulator != nullptr);
+                assert(subpassIndex == 1);
+                RIVE_DEBUG_CODE(m_numInteriorTriangleVerticesPushed +=)
+                flush->pushInteriorTriangulationDraw(this,
+                                                     m_pathID,
+                                                     gpu::WindingFaces::all);
+                assert(m_numInteriorTriangleVerticesPushed <=
+                       m_triangulator->maxVertexCount());
             }
             break;
         }
 
-        case 0: // Main subpass for path tessellation.
-        {
-            // Allocate tessellation vertices and push a path draw.
-            uint32_t tessVertexCount, tessLocation;
-            pushTessellationData(flush, &tessVertexCount, &tessLocation);
-            if (m_triangulator != nullptr)
+        case CoverageType::clockwiseAtomic:
+            if (!isStroke())
             {
-                flush->pushOuterCubicsDraw(this, tessVertexCount, tessLocation);
+                // The subpass and prepass each emit half the vertices.
+                assert(m_prepassCount == m_subpassCount);
+                assert(tessVertexCount % 2 == 0);
+                tessVertexCount /= 2;
             }
-            else
+            switch (subpassIndex)
             {
-                flush->pushMidpointFanDraw(this, tessVertexCount, tessLocation);
+                case -1: // Tessellation (borrowed, midpointFan or outerCubic).
+                    assert(!isStroke());
+                    m_prepassTessLocation =
+                        allocateTessellationVertices(flush, tessVertexCount);
+                    pushTessellationDraw(
+                        flush,
+                        tessVertexCount,
+                        m_prepassTessLocation,
+                        gpu::ShaderMiscFlags::borrowedCoveragePrepass);
+                    break;
+
+                case 0: // Tessellation (midpointFan or outerCubic).
+                {
+                    uint32_t tessLocation =
+                        allocateTessellationVertices(flush, tessVertexCount);
+                    pushTessellationData(flush, tessVertexCount, tessLocation);
+                    pushTessellationDraw(flush, tessVertexCount, tessLocation);
+                    break;
+                }
+
+                case -2: // Interior triangles (borrowed).
+                case 1:  // Interior triangles.
+                    assert(!isStroke());
+                    assert(m_triangulator != nullptr);
+                    RIVE_DEBUG_CODE(m_numInteriorTriangleVerticesPushed +=)
+                    flush->pushInteriorTriangulationDraw(
+                        this,
+                        m_pathID,
+                        subpassIndex < 0 ? gpu::WindingFaces::negative
+                                         : gpu::WindingFaces::positive,
+                        subpassIndex < 0
+                            ? gpu::ShaderMiscFlags::borrowedCoveragePrepass
+                            : gpu::ShaderMiscFlags::none);
+                    assert(m_numInteriorTriangleVerticesPushed <=
+                           m_triangulator->maxVertexCount());
+                    break;
+
+                default:
+                    RIVE_UNREACHABLE();
             }
             break;
-        }
 
-        case 1: // Main subpass for interior triangulation.
+        case CoverageType::msaa:
         {
-            assert(m_triangulator != nullptr);
-            size_t actualCount RIVE_MAYBE_UNUSED =
-                flush->pushInteriorTriangulationDraw(
-                    this,
-                    m_pathID,
-                    // In clockwiseAtomic mode, the negative triangles get
-                    // written out in a separate prepass.
-                    clockwiseAtomicFill ? gpu::WindingFaces::positive
-                                        : gpu::WindingFaces::all);
-            RIVE_DEBUG_CODE(m_numInteriorTriangleVerticesPushed += actualCount;)
-            assert(m_numInteriorTriangleVerticesPushed <=
-                   m_triangulator->maxVertexCount());
+            assert(m_prepassCount == 0 || m_subpassCount == 0);
+            int passCount = m_prepassCount | m_subpassCount;
+            int passIdx = subpassIndex + m_prepassCount;
+            if (passIdx == 0)
+            {
+                m_msaaTessLocation =
+                    allocateTessellationVertices(flush, tessVertexCount);
+                pushTessellationData(flush,
+                                     tessVertexCount,
+                                     m_msaaTessLocation);
+            }
+            constexpr static gpu::DrawType MSAA_FILL_TYPES[][3] = {
+                // Nested clip update (passCount == 1; the reset is handled by a
+                // separate msaaStencilClipReset draw.)
+                {
+                    gpu::DrawType::msaaMidpointFanPathsStencil,
+                },
+
+                // Slow path (passCount == 2): stencil-then-cover
+                {
+                    gpu::DrawType::msaaMidpointFanPathsStencil,
+                    gpu::DrawType::msaaMidpointFanPathsCover,
+                },
+
+                // Fast path (passCount == 3): (mostly) single pass rendering.
+                {
+                    gpu::DrawType::msaaMidpointFanBorrowedCoverage,
+                    gpu::DrawType::msaaMidpointFans,
+                    gpu::DrawType::msaaMidpointFanStencilReset,
+                },
+            };
+            assert(passCount <= 3);
+            assert(passIdx < passCount);
+            gpu::DrawType msaaDrawType =
+                isStroke() ? gpu::DrawType::msaaStrokes
+                           : MSAA_FILL_TYPES[passCount - 1][passIdx];
+            flush->pushMidpointFanDraw(this,
+                                       msaaDrawType,
+                                       tessVertexCount,
+                                       m_msaaTessLocation);
             break;
         }
 
-        default:
-            RIVE_UNREACHABLE();
+        case CoverageType::atlas:
+            // Atlas draws only have one subpass -- the rectangular blit from
+            // the atlas to the screen. The step that renders coverage to the
+            // offscreen atlas is handled separately, outside the subpass
+            // system.
+            assert(subpassIndex == 0);
+            flush->pushAtlasBlit(this, m_pathID);
+            break;
+    }
+}
+
+void PathDraw::pushTessellationDraw(RenderContext::LogicalFlush* flush,
+                                    uint32_t tessVertexCount,
+                                    uint32_t tessLocation,
+                                    gpu::ShaderMiscFlags shaderMiscFlags)
+{
+    if (m_triangulator != nullptr)
+    {
+        assert(!isStroke());
+        flush->pushOuterCubicsDraw(this,
+                                   gpu::DrawType::outerCurvePatches,
+                                   tessVertexCount,
+                                   tessLocation,
+                                   shaderMiscFlags);
+    }
+    else
+    {
+        flush->pushMidpointFanDraw(
+            this,
+            isFeatheredFill() ? gpu::DrawType::midpointFanCenterAAPatches
+                              : gpu::DrawType::midpointFanPatches,
+            tessVertexCount,
+            tessLocation,
+            shaderMiscFlags);
     }
 }
 
@@ -1596,50 +1676,27 @@
                                      uint32_t* tessBaseVertex)
 {
     assert(m_coverageType == CoverageType::atlas);
+    assert(m_resourceCounts.outerCubicTessVertexCount == 0 ||
+           m_resourceCounts.midpointFanTessVertexCount == 0);
 
-    if (m_pathID == 0)
+    *tessVertexCount = math::lossless_numeric_cast<uint32_t>(
+        m_resourceCounts.outerCubicTessVertexCount |
+        m_resourceCounts.midpointFanTessVertexCount);
+
+    if (*tessVertexCount == 0)
     {
-        assert(((m_resourceCounts.outerCubicTessVertexCount |
-                 m_resourceCounts.midpointFanTessVertexCount) == 0));
-        *tessVertexCount = 0;
+        assert(m_pathID == 0);
         return;
     }
 
-    pushTessellationData(flush, tessVertexCount, tessBaseVertex);
+    *tessBaseVertex = allocateTessellationVertices(flush, *tessVertexCount);
+    pushTessellationData(flush, *tessVertexCount, *tessBaseVertex);
 }
 
 void PathDraw::pushTessellationData(RenderContext::LogicalFlush* flush,
-                                    uint32_t* outTessVertexCount,
-                                    uint32_t* outTessLocation)
+                                    uint32_t tessVertexCount,
+                                    uint32_t tessLocation)
 {
-    uint32_t tessVertexCount = math::lossless_numeric_cast<uint32_t>(
-        m_triangulator != nullptr
-            ? m_resourceCounts.outerCubicTessVertexCount
-            : m_resourceCounts.midpointFanTessVertexCount);
-    assert(tessVertexCount > 0);
-
-    bool clockwiseAtomicFill =
-        !isStroke() && m_coverageType == CoverageType::clockwiseAtomic;
-
-    if (clockwiseAtomicFill)
-    {
-        // In clockwiseAtomic mode, we draw paths in two separate passes -- once
-        // backwards for borrowed coverage, and a separate forward pass.
-        assert(tessVertexCount % 2 == 0);
-        tessVertexCount /= 2;
-    }
-
-    // Allocate tessellation vertices and push a path draw.
-    uint32_t tessLocation;
-    if (m_triangulator != nullptr)
-    {
-        tessLocation = flush->allocateOuterCubicTessVertices(tessVertexCount);
-    }
-    else
-    {
-        tessLocation = flush->allocateMidpointFanTessVertices(tessVertexCount);
-    }
-
     // Determine where to fill in forward and mirrored tessellations.
     uint32_t forwardTessVertexCount, forwardTessLocation,
         mirroredTessVertexCount, mirroredTessLocation;
@@ -1656,11 +1713,14 @@
             mirroredTessLocation = tessLocation + tessVertexCount;
             break;
         case gpu::ContourDirections::reverseThenForward:
-            if (clockwiseAtomicFill)
+            if (m_coverageType == CoverageType::clockwiseAtomic && !isStroke())
             {
                 // The tessellation for borrowed coverage was allocated at a
                 // different location than the forward tessellation, both with
                 // "tessVertexCount" vertices.
+                assert(m_prepassTessLocation != 0); // With padding, this will
+                                                    // only be zero if it wasn't
+                                                    // initialized.
                 forwardTessVertexCount = mirroredTessVertexCount =
                     tessVertexCount;
                 forwardTessLocation = tessLocation;
@@ -1679,11 +1739,14 @@
             }
             break;
         case gpu::ContourDirections::forwardThenReverse:
-            if (clockwiseAtomicFill)
+            if (m_coverageType == CoverageType::clockwiseAtomic && !isStroke())
             {
                 // The tessellation for borrowed coverage was allocated at a
                 // different location than the forward tessellation, both with
                 // "tessVertexCount" vertices.
+                assert(m_prepassTessLocation != 0); // With padding, this will
+                                                    // only be zero if it wasn't
+                                                    // initialized.
                 forwardTessVertexCount = mirroredTessVertexCount =
                     tessVertexCount;
                 forwardTessLocation = m_prepassTessLocation;
@@ -1725,9 +1788,6 @@
     {
         pushMidpointFanTessellationData(&tessWriter);
     }
-
-    *outTessVertexCount = tessVertexCount;
-    *outTessLocation = tessLocation;
 }
 
 void PathDraw::pushMidpointFanTessellationData(
diff --git a/renderer/src/gl/gl_state.cpp b/renderer/src/gl/gl_state.cpp
index b6f8c7a..c04f68d 100644
--- a/renderer/src/gl/gl_state.cpp
+++ b/renderer/src/gl/gl_state.cpp
@@ -4,6 +4,7 @@
 
 #include "rive/renderer/gl/gl_state.hpp"
 
+#include "rive/renderer/gpu.hpp"
 #include "shaders/constants.glsl"
 
 namespace rive::gpu
@@ -64,6 +65,192 @@
     glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
 }
 
+static void gl_enable_disable(GLenum state, bool enabled)
+{
+    if (enabled)
+        glEnable(state);
+    else
+        glDisable(state);
+}
+
+void GLState::setDepthStencilEnabled(bool depthEnabled, bool stencilEnabled)
+{
+    if (!m_validState.depthStencilEnabled || m_depthTestEnabled != depthEnabled)
+    {
+        gl_enable_disable(GL_DEPTH_TEST, depthEnabled);
+        m_depthTestEnabled = depthEnabled;
+    }
+
+    if (!m_validState.depthStencilEnabled ||
+        m_stencilTestEnabled != stencilEnabled)
+    {
+        gl_enable_disable(GL_STENCIL_TEST, stencilEnabled);
+        m_stencilTestEnabled = stencilEnabled;
+    }
+
+    m_validState.depthStencilEnabled = true;
+}
+
+void GLState::setWriteMasks(bool colorWriteMask,
+                            bool depthWriteMask,
+                            uint8_t stencilWriteMask)
+{
+    if (!m_validState.writeMasks)
+    {
+        glColorMask(colorWriteMask,
+                    colorWriteMask,
+                    colorWriteMask,
+                    colorWriteMask);
+        glDepthMask(depthWriteMask);
+        glStencilMask(stencilWriteMask);
+        m_colorWriteMask = colorWriteMask;
+        m_depthWriteMask = depthWriteMask;
+        m_stencilWriteMask = stencilWriteMask;
+        m_validState.writeMasks = true;
+    }
+    else
+    {
+        if (colorWriteMask != m_colorWriteMask)
+        {
+            glColorMask(colorWriteMask,
+                        colorWriteMask,
+                        colorWriteMask,
+                        colorWriteMask);
+            m_colorWriteMask = colorWriteMask;
+        }
+        if (depthWriteMask != m_depthWriteMask)
+        {
+            glDepthMask(depthWriteMask);
+            m_depthWriteMask = depthWriteMask;
+        }
+        if (stencilWriteMask != m_stencilWriteMask)
+        {
+            glStencilMask(stencilWriteMask);
+            m_stencilWriteMask = stencilWriteMask;
+        }
+    }
+}
+
+void GLState::setCullFace(GLenum cullFace)
+{
+    if (!m_validState.cullFace || cullFace != m_cullFace)
+    {
+        if (cullFace == GL_NONE)
+        {
+            glDisable(GL_CULL_FACE);
+        }
+        else
+        {
+            if (!m_validState.cullFace || m_cullFace == GL_NONE)
+            {
+                glEnable(GL_CULL_FACE);
+            }
+            glCullFace(cullFace);
+        }
+        m_cullFace = cullFace;
+        m_validState.cullFace = true;
+    }
+}
+
+static GLenum gl_stencil_op(StencilOp op)
+{
+    switch (op)
+    {
+        case StencilOp::keep:
+            return GL_KEEP;
+        case StencilOp::replace:
+            return GL_REPLACE;
+        case StencilOp::zero:
+            return GL_ZERO;
+        case StencilOp::decrClamp:
+            return GL_DECR;
+        case StencilOp::incrWrap:
+            return GL_INCR_WRAP;
+        case StencilOp::decrWrap:
+            return GL_DECR_WRAP;
+    }
+    RIVE_UNREACHABLE();
+}
+
+static GLenum gl_stencil_func(gpu::StencilCompareOp compareOp)
+{
+    switch (compareOp)
+    {
+        case gpu::StencilCompareOp::less:
+            return GL_LESS;
+        case gpu::StencilCompareOp::equal:
+            return GL_EQUAL;
+        case gpu::StencilCompareOp::lessOrEqual:
+            return GL_LEQUAL;
+        case gpu::StencilCompareOp::notEqual:
+            return GL_NOTEQUAL;
+        case gpu::StencilCompareOp::always:
+            return GL_ALWAYS;
+    }
+    RIVE_UNREACHABLE();
+}
+
+static GLenum gl_cull_face(CullFace riveCullFace)
+{
+    switch (riveCullFace)
+    {
+        case CullFace::none:
+            return GL_NONE;
+        case CullFace::clockwise:
+            return GL_FRONT;
+        case CullFace::counterclockwise:
+            return GL_BACK;
+    }
+    RIVE_UNREACHABLE();
+}
+
+void GLState::setPipelineState(const gpu::PipelineState& pipelineState)
+{
+    setDepthStencilEnabled(pipelineState.depthTestEnabled,
+                           pipelineState.stencilTestEnabled);
+    setWriteMasks(pipelineState.colorWriteEnabled,
+                  pipelineState.depthWriteEnabled,
+                  pipelineState.stencilWriteMask);
+    if (pipelineState.stencilTestEnabled)
+    {
+        if (!pipelineState.stencilDoubleSided)
+        {
+            glStencilFunc(
+                gl_stencil_func(pipelineState.stencilFrontOps.compareOp),
+                pipelineState.stencilReference,
+                pipelineState.stencilCompareMask);
+            glStencilOp(
+                gl_stencil_op(pipelineState.stencilFrontOps.failOp),
+                gl_stencil_op(pipelineState.stencilFrontOps.depthFailOp),
+                gl_stencil_op(pipelineState.stencilFrontOps.passOp));
+        }
+        else
+        {
+            glStencilFuncSeparate(
+                GL_FRONT,
+                gl_stencil_func(pipelineState.stencilFrontOps.compareOp),
+                pipelineState.stencilReference,
+                pipelineState.stencilCompareMask);
+            glStencilOpSeparate(
+                GL_FRONT,
+                gl_stencil_op(pipelineState.stencilFrontOps.failOp),
+                gl_stencil_op(pipelineState.stencilFrontOps.depthFailOp),
+                gl_stencil_op(pipelineState.stencilFrontOps.passOp));
+            glStencilFuncSeparate(
+                GL_BACK,
+                gl_stencil_func(pipelineState.stencilBackOps.compareOp),
+                pipelineState.stencilReference,
+                pipelineState.stencilCompareMask);
+            glStencilOpSeparate(
+                GL_BACK,
+                gl_stencil_op(pipelineState.stencilBackOps.failOp),
+                gl_stencil_op(pipelineState.stencilBackOps.depthFailOp),
+                gl_stencil_op(pipelineState.stencilBackOps.passOp));
+        }
+    }
+    setCullFace(gl_cull_face(pipelineState.cullFace));
+}
+
 void GLState::setGLBlendMode(GLBlendMode blendMode)
 {
     if (m_validState.blendEquation && blendMode == m_blendMode)
@@ -141,67 +328,6 @@
     m_validState.blendEquation = true;
 }
 
-void GLState::setWriteMasks(bool colorWriteMask,
-                            bool depthWriteMask,
-                            GLuint stencilWriteMask)
-{
-    if (!m_validState.writeMasks)
-    {
-        glColorMask(colorWriteMask,
-                    colorWriteMask,
-                    colorWriteMask,
-                    colorWriteMask);
-        glDepthMask(depthWriteMask);
-        glStencilMask(stencilWriteMask);
-        m_colorWriteMask = colorWriteMask;
-        m_depthWriteMask = depthWriteMask;
-        m_stencilWriteMask = stencilWriteMask;
-        m_validState.writeMasks = true;
-    }
-    else
-    {
-        if (colorWriteMask != m_colorWriteMask)
-        {
-            glColorMask(colorWriteMask,
-                        colorWriteMask,
-                        colorWriteMask,
-                        colorWriteMask);
-            m_colorWriteMask = colorWriteMask;
-        }
-        if (depthWriteMask != m_depthWriteMask)
-        {
-            glDepthMask(depthWriteMask);
-            m_depthWriteMask = depthWriteMask;
-        }
-        if (stencilWriteMask != m_stencilWriteMask)
-        {
-            glStencilMask(stencilWriteMask);
-            m_stencilWriteMask = stencilWriteMask;
-        }
-    }
-}
-
-void GLState::setCullFace(GLenum cullFace)
-{
-    if (!m_validState.cullFace || cullFace != m_cullFace)
-    {
-        if (cullFace == GL_NONE)
-        {
-            glDisable(GL_CULL_FACE);
-        }
-        else
-        {
-            if (!m_validState.cullFace || m_cullFace == GL_NONE)
-            {
-                glEnable(GL_CULL_FACE);
-            }
-            glCullFace(cullFace);
-        }
-        m_cullFace = cullFace;
-        m_validState.cullFace = true;
-    }
-}
-
 void GLState::bindProgram(GLuint programID)
 {
     if (!m_validState.boundProgramID || programID != m_boundProgramID)
diff --git a/renderer/src/gl/render_context_gl_impl.cpp b/renderer/src/gl/render_context_gl_impl.cpp
index 2cd2e6f..7217ce9 100644
--- a/renderer/src/gl/render_context_gl_impl.cpp
+++ b/renderer/src/gl/render_context_gl_impl.cpp
@@ -40,6 +40,34 @@
 
 namespace rive::gpu
 {
+
+static bool is_tessellation_draw(gpu::DrawType drawType)
+{
+    switch (drawType)
+    {
+        case gpu::DrawType::midpointFanPatches:
+        case gpu::DrawType::midpointFanCenterAAPatches:
+        case gpu::DrawType::outerCurvePatches:
+        case gpu::DrawType::msaaStrokes:
+        case gpu::DrawType::msaaMidpointFanBorrowedCoverage:
+        case gpu::DrawType::msaaMidpointFans:
+        case gpu::DrawType::msaaMidpointFanStencilReset:
+        case gpu::DrawType::msaaMidpointFanPathsStencil:
+        case gpu::DrawType::msaaMidpointFanPathsCover:
+        case gpu::DrawType::msaaOuterCubics:
+            return true;
+        case gpu::DrawType::imageRect:
+        case gpu::DrawType::imageMesh:
+        case gpu::DrawType::interiorTriangulation:
+        case gpu::DrawType::atlasBlit:
+        case gpu::DrawType::atomicInitialize:
+        case gpu::DrawType::atomicResolve:
+        case gpu::DrawType::msaaStencilClipReset:
+            return false;
+    }
+    RIVE_UNREACHABLE();
+}
+
 RenderContextGLImpl::RenderContextGLImpl(
     const char* rendererString,
     GLCapabilities capabilities,
@@ -860,6 +888,13 @@
         case gpu::DrawType::midpointFanPatches:
         case gpu::DrawType::midpointFanCenterAAPatches:
         case gpu::DrawType::outerCurvePatches:
+        case gpu::DrawType::msaaStrokes:
+        case gpu::DrawType::msaaMidpointFanBorrowedCoverage:
+        case gpu::DrawType::msaaMidpointFans:
+        case gpu::DrawType::msaaMidpointFanStencilReset:
+        case gpu::DrawType::msaaMidpointFanPathsStencil:
+        case gpu::DrawType::msaaMidpointFanPathsCover:
+        case gpu::DrawType::msaaOuterCubics:
             if (shaderType == GL_VERTEX_SHADER)
             {
                 defines.push_back(GLSL_ENABLE_INSTANCE_INDEX);
@@ -870,7 +905,7 @@
                                   ? gpu::glsl::atomic_draw
                                   : gpu::glsl::draw_path);
             break;
-        case gpu::DrawType::stencilClipReset:
+        case gpu::DrawType::msaaStencilClipReset:
             assert(interlockMode == gpu::InterlockMode::msaa);
             sources.push_back(gpu::glsl::stencil_draw);
             break;
@@ -978,10 +1013,7 @@
                           FLUSH_UNIFORM_BUFFER_IDX);
 
     const bool isImageDraw = gpu::DrawTypeIsImageDraw(drawType);
-    const bool isTessellationDraw =
-        drawType == gpu::DrawType::midpointFanPatches ||
-        drawType == gpu::DrawType::midpointFanCenterAAPatches ||
-        drawType == gpu::DrawType::outerCurvePatches;
+    const bool isTessellationDraw = is_tessellation_draw(drawType);
     const bool isPaintDraw = isTessellationDraw ||
                              drawType == gpu::DrawType::interiorTriangulation ||
                              drawType == gpu::DrawType::atlasBlit;
@@ -1117,93 +1149,12 @@
     }
 }
 
-// Wraps calls to glDrawElementsInstanced*(), as appropriate for the current
-// platform. Also includes simple helpers for working with stencil.
-class DrawIndexedInstancedHelper
-{
-public:
-    DrawIndexedInstancedHelper(GLint baseInstanceUniformLocation,
-                               GLenum primitiveTopology,
-                               uint32_t instanceCount,
-                               uint32_t baseInstance) :
-        m_baseInstanceUniformLocation(baseInstanceUniformLocation),
-        m_primitiveTopology(primitiveTopology),
-        m_instanceCount(instanceCount),
-        m_baseInstance(baseInstance)
-    {}
-
-    void setIndexRange(uint32_t indexCount, uint32_t baseIndex)
-    {
-        m_indexCount = indexCount;
-        m_indexOffset =
-            reinterpret_cast<const void*>(baseIndex * sizeof(uint16_t));
-    }
-
-    void draw(const GLCapabilities& capabilities) const
-    {
-        assert(capabilities.ANGLE_base_vertex_base_instance_shader_builtin ==
-               (m_baseInstanceUniformLocation < 0));
-        for (uint32_t baseInstance = m_baseInstance,
-                      endInstance = m_baseInstance + m_instanceCount;
-             baseInstance < endInstance;)
-
-        {
-            uint32_t instanceCount =
-                std::min(endInstance - baseInstance,
-                         capabilities.maxSupportedInstancesPerDrawCommand);
-#ifndef RIVE_WEBGL
-            if (capabilities.ANGLE_base_vertex_base_instance_shader_builtin)
-            {
-                glDrawElementsInstancedBaseInstanceEXT(m_primitiveTopology,
-                                                       m_indexCount,
-                                                       GL_UNSIGNED_SHORT,
-                                                       m_indexOffset,
-                                                       instanceCount,
-                                                       baseInstance);
-            }
-            else
-#endif
-            {
-                glUniform1i(m_baseInstanceUniformLocation, baseInstance);
-                glDrawElementsInstanced(m_primitiveTopology,
-                                        m_indexCount,
-                                        GL_UNSIGNED_SHORT,
-                                        m_indexOffset,
-                                        instanceCount);
-            }
-            baseInstance += instanceCount;
-        }
-    }
-
-    void drawWithStencilSettings(GLenum comparator,
-                                 GLint ref,
-                                 GLuint mask,
-                                 GLenum stencilFailOp,
-                                 GLenum depthPassStencilFailOp,
-                                 GLenum depthPassStencilPassOp,
-                                 const GLCapabilities& capabilities) const
-    {
-        glStencilFunc(comparator, ref, mask);
-        glStencilOp(stencilFailOp,
-                    depthPassStencilFailOp,
-                    depthPassStencilPassOp);
-        draw(capabilities);
-    }
-
-private:
-    const GLint m_baseInstanceUniformLocation;
-    const GLenum m_primitiveTopology;
-    const uint32_t m_instanceCount;
-    const uint32_t m_baseInstance;
-    uint32_t m_indexCount = 0;
-    const void* m_indexOffset = nullptr;
-};
-
 void RenderContextGLImpl::flush(const FlushDescriptor& desc)
 {
     assert(desc.interlockMode != gpu::InterlockMode::clockwiseAtomic);
     auto renderTarget = static_cast<RenderTargetGL*>(desc.renderTarget);
 
+    m_state->setDepthStencilEnabled(false, false);
     m_state->setWriteMasks(true, true, 0xff);
     m_state->disableBlending();
 
@@ -1348,15 +1299,13 @@
                           fillBatch.scissor.top,
                           fillBatch.scissor.width(),
                           fillBatch.scissor.height());
-                DrawIndexedInstancedHelper drawHelper(
-                    m_atlasFillProgram.baseInstanceUniformLocation(),
+                drawIndexedInstancedNoInstancedAttribs(
                     GL_TRIANGLES,
-                    fillBatch.patchCount,
-                    fillBatch.basePatch);
-                drawHelper.setIndexRange(
                     gpu::kMidpointFanCenterAAPatchIndexCount,
-                    gpu::kMidpointFanCenterAAPatchBaseIndex);
-                drawHelper.draw(m_capabilities);
+                    gpu::kMidpointFanCenterAAPatchBaseIndex,
+                    fillBatch.patchCount,
+                    fillBatch.basePatch,
+                    m_atlasFillProgram.baseInstanceUniformLocation());
             }
         }
 
@@ -1372,14 +1321,13 @@
                           strokeBatch.scissor.top,
                           strokeBatch.scissor.width(),
                           strokeBatch.scissor.height());
-                DrawIndexedInstancedHelper drawHelper(
-                    m_atlasStrokeProgram.baseInstanceUniformLocation(),
+                drawIndexedInstancedNoInstancedAttribs(
                     GL_TRIANGLES,
+                    gpu::kMidpointFanPatchBorderIndexCount,
+                    gpu::kMidpointFanPatchBaseIndex,
                     strokeBatch.patchCount,
-                    strokeBatch.basePatch);
-                drawHelper.setIndexRange(gpu::kMidpointFanPatchBorderIndexCount,
-                                         gpu::kMidpointFanPatchBaseIndex);
-                drawHelper.draw(m_capabilities);
+                    strokeBatch.basePatch,
+                    m_atlasFillProgram.baseInstanceUniformLocation());
             }
         }
 
@@ -1450,9 +1398,6 @@
         }
         glClear(buffersToClear);
 
-        glEnable(GL_STENCIL_TEST);
-        glEnable(GL_DEPTH_TEST);
-
         if (desc.combinedShaderFeatures &
             gpu::ShaderFeatures::ENABLE_ADVANCED_BLEND)
         {
@@ -1480,6 +1425,7 @@
             continue;
         }
 
+        const gpu::DrawType drawType = batch.drawType;
         gpu::ShaderFeatures shaderFeatures =
             desc.interlockMode == gpu::InterlockMode::atomics
                 ? desc.combinedShaderFeatures
@@ -1487,21 +1433,21 @@
         gpu::ShaderMiscFlags shaderMiscFlags = batch.shaderMiscFlags;
         if (m_plsImpl != nullptr)
         {
-            shaderMiscFlags |= m_plsImpl->shaderMiscFlags(desc, batch.drawType);
+            shaderMiscFlags |= m_plsImpl->shaderMiscFlags(desc, drawType);
         }
         if (desc.interlockMode == gpu::InterlockMode::rasterOrdering &&
             (batch.drawContents & gpu::DrawContents::clockwiseFill))
         {
             shaderMiscFlags |= gpu::ShaderMiscFlags::clockwiseFill;
         }
-        uint32_t fragmentShaderKey = gpu::ShaderUniqueKey(batch.drawType,
+        uint32_t fragmentShaderKey = gpu::ShaderUniqueKey(drawType,
                                                           shaderFeatures,
                                                           desc.interlockMode,
                                                           shaderMiscFlags);
         const DrawProgram& drawProgram = m_drawPrograms
                                              .try_emplace(fragmentShaderKey,
                                                           this,
-                                                          batch.drawType,
+                                                          drawType,
                                                           shaderFeatures,
                                                           desc.interlockMode,
                                                           shaderMiscFlags)
@@ -1571,20 +1517,27 @@
             }
         }
 
-        switch (gpu::DrawType drawType = batch.drawType)
+        gpu::PipelineState pipelineState;
+        gpu::get_pipeline_state(drawType,
+                                desc.interlockMode,
+                                batch.drawContents,
+                                &pipelineState);
+        m_state->setPipelineState(pipelineState);
+
+        switch (drawType)
         {
             case DrawType::midpointFanPatches:
             case DrawType::midpointFanCenterAAPatches:
             case DrawType::outerCurvePatches:
+            case DrawType::msaaStrokes:
+            case DrawType::msaaMidpointFanBorrowedCoverage:
+            case DrawType::msaaMidpointFans:
+            case DrawType::msaaMidpointFanStencilReset:
+            case DrawType::msaaMidpointFanPathsStencil:
+            case DrawType::msaaMidpointFanPathsCover:
+            case DrawType::msaaOuterCubics:
             {
-                // Draw PLS patches that connect the tessellation vertices.
                 m_state->bindVAO(m_drawVAO);
-                DrawIndexedInstancedHelper drawHelper(
-                    drawProgram.baseInstanceUniformLocation(),
-                    GL_TRIANGLES,
-                    batch.elementCount,
-                    batch.baseElement);
-
                 if (desc.interlockMode != gpu::InterlockMode::msaa)
                 {
                     m_plsImpl->ensureRasterOrderingEnabled(
@@ -1592,216 +1545,44 @@
                         desc,
                         desc.interlockMode ==
                             gpu::InterlockMode::rasterOrdering);
-                    drawHelper.setIndexRange(gpu::PatchIndexCount(drawType),
-                                             gpu::PatchBaseIndex(drawType));
-                    m_state->setCullFace(GL_BACK);
-                    drawHelper.draw(m_capabilities);
-                    break;
                 }
-
-                // MSAA path draws require different stencil settings, depending
-                // on their drawContents.
-                bool hasActiveClip =
-                    ((batch.drawContents & gpu::DrawContents::activeClip));
-                bool isClipUpdate =
-                    ((batch.drawContents & gpu::DrawContents::clipUpdate));
-                bool isNestedClipUpdate =
-                    (batch.drawContents & gpu::kNestedClipUpdateMask) ==
-                    gpu::kNestedClipUpdateMask;
-                bool isEvenOddFill =
-                    (batch.drawContents & gpu::DrawContents::evenOddFill);
-                bool isStroke =
-                    (batch.drawContents & gpu::DrawContents::stroke);
-                if (isStroke)
-                {
-                    // MSAA strokes only use the "border" section of the patch.
-                    // (The depth test prevents double hits.)
-                    assert(drawType == gpu::DrawType::midpointFanPatches);
-                    drawHelper.setIndexRange(
-                        gpu::kMidpointFanPatchBorderIndexCount,
-                        gpu::kMidpointFanPatchBaseIndex);
-                    m_state->setWriteMasks(true, true, 0xff);
-                    m_state->setCullFace(GL_BACK);
-                    drawHelper.drawWithStencilSettings(
-                        hasActiveClip ? GL_EQUAL : GL_ALWAYS,
-                        0x80,
-                        0xff,
-                        GL_KEEP,
-                        GL_KEEP,
-                        GL_KEEP,
-                        m_capabilities);
-                    break;
-                }
-
-                // MSAA fills only use the "fan" section of the patch (they
-                // don't need AA borders).
-                drawHelper.setIndexRange(gpu::PatchFanIndexCount(drawType),
-                                         gpu::PatchFanBaseIndex(drawType));
-
-                // "nonZero" fill rules (that aren't nested clip updates) can be
-                // optimized to render directly instead of using a "stencil then
-                // cover" approach.
-                if (!isEvenOddFill && !isNestedClipUpdate)
-                {
-                    // Count backward triangle hits (negative coverage) in the
-                    // stencil buffer.
-                    m_state->setWriteMasks(false, false, 0x7f);
-                    m_state->setCullFace(GL_FRONT);
-                    drawHelper.drawWithStencilSettings(
-                        hasActiveClip ? GL_LEQUAL : GL_ALWAYS,
-                        0x80,
-                        0xff,
-                        GL_KEEP,
-                        GL_KEEP,
-                        GL_INCR_WRAP,
-                        m_capabilities);
-
-                    // (Configure both stencil faces before issuing the next
-                    // draws, so GL can give them the same internal pipeline
-                    // under the hood.)
-                    GLuint stencilReadMask = hasActiveClip ? 0xff : 0x7f;
-                    glStencilFuncSeparate(GL_FRONT,
-                                          GL_EQUAL,
-                                          0x80,
-                                          stencilReadMask);
-                    glStencilOpSeparate(
-                        GL_FRONT,
-                        GL_DECR, // Don't wrap; 0 must stay 0 outside the clip.
-                        GL_KEEP,
-                        isClipUpdate ? GL_REPLACE : GL_KEEP);
-                    glStencilFuncSeparate(GL_BACK,
-                                          GL_LESS,
-                                          0x80,
-                                          stencilReadMask);
-                    glStencilOpSeparate(GL_BACK,
-                                        GL_KEEP,
-                                        GL_KEEP,
-                                        isClipUpdate ? GL_REPLACE : GL_ZERO);
-                    m_state->setWriteMasks(!isClipUpdate,
-                                           !isClipUpdate,
-                                           isClipUpdate ? 0xff : 0x7f);
-
-                    // Draw forward triangles, clipped by the backward triangle
-                    // counts. (The depth test prevents double hits.)
-                    m_state->setCullFace(GL_BACK);
-                    drawHelper.draw(m_capabilities);
-
-                    // Clean up backward triangles in the stencil buffer, (also
-                    // filling negative winding numbers for nonZero fill).
-                    m_state->setCullFace(GL_FRONT);
-                    if (batch.drawContents & gpu::DrawContents::clockwiseFill)
-                    {
-                        // For clockwise fill, disable color & clip-bit writes
-                        // when cleaning up backward triangles. This mode only
-                        // fills in forward triangles.
-                        m_state->setWriteMasks(false, false, 0x7f);
-                    }
-                    drawHelper.draw(m_capabilities);
-                    break;
-                }
-
-                // Fall back on stencil-then-cover.
-                glStencilFunc(hasActiveClip ? GL_LEQUAL : GL_ALWAYS,
-                              0x80,
-                              0xff);
-                // Decrement front-facing triangles so the MSB is set when
-                // clockwise.
-                glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_DECR_WRAP);
-                glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_INCR_WRAP);
-                m_state->setWriteMasks(false,
-                                       false,
-                                       isEvenOddFill ? 0x1 : 0x7f);
-                m_state->setCullFace(GL_NONE);
-                drawHelper.draw(m_capabilities); // Stencil the path!
-
-                // Nested clip updates do the "cover" operation during
-                // DrawType::stencilClipReset.
-                if (!isNestedClipUpdate)
-                {
-                    assert(isEvenOddFill);
-                    m_state->setWriteMasks(!isClipUpdate,
-                                           !isClipUpdate,
-                                           isClipUpdate ? 0xff : 0x1);
-                    drawHelper.drawWithStencilSettings(
-                        GL_NOTEQUAL, // Cover the path!
-                        0x80,
-                        0x7f,
-                        GL_KEEP,
-                        GL_KEEP,
-                        isClipUpdate ? GL_REPLACE : GL_ZERO,
-                        m_capabilities);
-                }
+                drawIndexedInstancedNoInstancedAttribs(
+                    GL_TRIANGLES,
+                    gpu::PatchIndexCount(drawType),
+                    gpu::PatchBaseIndex(drawType),
+                    batch.elementCount,
+                    batch.baseElement,
+                    drawProgram.baseInstanceUniformLocation());
                 break;
             }
-            case gpu::DrawType::stencilClipReset:
+
+            case gpu::DrawType::msaaStencilClipReset:
             {
-                assert(desc.interlockMode == gpu::InterlockMode::msaa);
                 m_state->bindVAO(m_trianglesVAO);
-                bool isNestedClipUpdate =
-                    (batch.drawContents & gpu::kNestedClipUpdateMask) ==
-                    gpu::kNestedClipUpdateMask;
-                if (isNestedClipUpdate)
-                {
-                    // The nested clip just got stencilled and left in the
-                    // stencil buffer. Intersect it with the existing clip.
-                    // (Erasing regions of the existing clip that are outside
-                    // the nested clip.)
-                    glStencilFunc(
-                        GL_LESS,
-                        0x80,
-                        (batch.drawContents & gpu::DrawContents::clockwiseFill)
-                            // clockwise: (0x80 & 0xc0) < (stencilValue & 0xc0)
-                            //   => "If clipbit is set and winding is negative"
-                            //   => "If clipbit is set and winding is clockwise"
-                            //      (because clockwise decrements)
-                            //
-                            ? 0xc0
-                            // non-clockwise: 0x80 < stencilValue
-                            //   => "If clipbit is set and winding is nonzero"
-                            : 0xff);
-                    glStencilOp(GL_ZERO, GL_KEEP, GL_REPLACE);
-                }
-                else
-                {
-                    // Clear the entire previous clip.
-                    glStencilFunc(GL_NOTEQUAL, 0, 0xff);
-                    glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);
-                }
-                m_state->setWriteMasks(false, false, 0xff);
-                m_state->setCullFace(GL_BACK);
                 glDrawArrays(GL_TRIANGLES,
                              batch.baseElement,
                              batch.elementCount);
                 break;
             }
+
             case gpu::DrawType::interiorTriangulation:
             case gpu::DrawType::atlasBlit:
             {
-                if (desc.interlockMode == gpu::InterlockMode::msaa)
-                {
-                    bool hasActiveClip =
-                        ((batch.drawContents & gpu::DrawContents::activeClip));
-                    glStencilFunc(hasActiveClip ? GL_EQUAL : GL_ALWAYS,
-                                  0x80,
-                                  0xff);
-                    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
-                    m_state->setWriteMasks(true, false, 0x0);
-                }
-                else if (desc.interlockMode ==
-                         gpu::InterlockMode::rasterOrdering)
+                m_state->bindVAO(m_trianglesVAO);
+                if (desc.interlockMode != gpu::InterlockMode::msaa)
                 {
                     // Disable raster ordering if we're drawing true interior
                     // triangles (not atlas coverage). We know the triangulation
                     // is large enough that it's faster to issue a barrier than
                     // to force raster ordering in the fragment shader.
                     const bool needRasterOrdering =
+                        desc.interlockMode ==
+                            gpu::InterlockMode::rasterOrdering &&
                         drawType == gpu::DrawType::atlasBlit;
                     m_plsImpl->ensureRasterOrderingEnabled(this,
                                                            desc,
                                                            needRasterOrdering);
                 }
-                m_state->bindVAO(m_trianglesVAO);
-                m_state->setCullFace(GL_BACK);
                 glDrawArrays(GL_TRIANGLES,
                              batch.baseElement,
                              batch.elementCount);
@@ -1816,25 +1597,25 @@
                 }
                 break;
             }
+
             case gpu::DrawType::imageRect:
             {
-                assert(desc.interlockMode == gpu::InterlockMode::atomics);
                 // m_imageRectVAO should have gotten lazily allocated by now.
                 assert(m_imageRectVAO != 0);
-                m_plsImpl->ensureRasterOrderingEnabled(this, desc, false);
                 m_state->bindVAO(m_imageRectVAO);
+                m_plsImpl->ensureRasterOrderingEnabled(this, desc, false);
                 glBindBufferRange(GL_UNIFORM_BUFFER,
                                   IMAGE_DRAW_UNIFORM_BUFFER_IDX,
                                   gl_buffer_id(imageDrawUniformBufferRing()),
                                   batch.imageDrawDataOffset,
                                   sizeof(gpu::ImageDrawUniforms));
-                m_state->setCullFace(GL_NONE);
                 glDrawElements(GL_TRIANGLES,
                                std::size(gpu::kImageRectIndices),
                                GL_UNSIGNED_SHORT,
                                nullptr);
                 break;
             }
+
             case gpu::DrawType::imageMesh:
             {
                 LITE_RTTI_CAST_OR_BREAK(vertexBuffer,
@@ -1868,17 +1649,6 @@
                         desc,
                         m_platformFeatures.supportsRasterOrdering);
                 }
-                else
-                {
-                    bool hasActiveClip =
-                        ((batch.drawContents & gpu::DrawContents::activeClip));
-                    glStencilFunc(hasActiveClip ? GL_EQUAL : GL_ALWAYS,
-                                  0x80,
-                                  0xff);
-                    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
-                    m_state->setWriteMasks(true, false, 0x00);
-                }
-                m_state->setCullFace(GL_NONE);
                 glDrawElements(GL_TRIANGLES,
                                batch.elementCount,
                                GL_UNSIGNED_SHORT,
@@ -1886,18 +1656,18 @@
                                                              sizeof(uint16_t)));
                 break;
             }
+
             case gpu::DrawType::atomicResolve:
             {
-                assert(desc.interlockMode == gpu::InterlockMode::atomics);
-                m_plsImpl->ensureRasterOrderingEnabled(this, desc, false);
                 m_state->bindVAO(m_emptyVAO);
+                m_plsImpl->ensureRasterOrderingEnabled(this, desc, false);
                 m_plsImpl->setupAtomicResolve(this, desc);
                 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
                 break;
             }
+
             case gpu::DrawType::atomicInitialize:
             {
-                assert(desc.interlockMode == gpu::InterlockMode::atomics);
                 RIVE_UNREACHABLE();
             }
         }
@@ -1939,8 +1709,6 @@
         {
             glDisable(GL_BLEND_ADVANCED_COHERENT_KHR);
         }
-        glDisable(GL_DEPTH_TEST);
-        glDisable(GL_STENCIL_TEST);
         if (clipPlanesEnabled)
         {
             glDisable(GL_CLIP_DISTANCE0_EXT);
@@ -1965,6 +1733,49 @@
     }
 }
 
+void RenderContextGLImpl::drawIndexedInstancedNoInstancedAttribs(
+    GLenum primitiveTopology,
+    uint32_t indexCount,
+    uint32_t baseIndex,
+    uint32_t instanceCount,
+    uint32_t baseInstance,
+    GLint baseInstanceUniformLocation)
+{
+    assert(m_capabilities.ANGLE_base_vertex_base_instance_shader_builtin ==
+           (baseInstanceUniformLocation < 0));
+    const void* indexOffset =
+        reinterpret_cast<const void*>(baseIndex * sizeof(uint16_t));
+    for (uint32_t endInstance = baseInstance + instanceCount;
+         baseInstance < endInstance;)
+
+    {
+        uint32_t subInstanceCount =
+            std::min(endInstance - baseInstance,
+                     m_capabilities.maxSupportedInstancesPerDrawCommand);
+#ifndef RIVE_WEBGL
+        if (m_capabilities.ANGLE_base_vertex_base_instance_shader_builtin)
+        {
+            glDrawElementsInstancedBaseInstanceEXT(primitiveTopology,
+                                                   indexCount,
+                                                   GL_UNSIGNED_SHORT,
+                                                   indexOffset,
+                                                   subInstanceCount,
+                                                   baseInstance);
+        }
+        else
+#endif
+        {
+            glUniform1i(baseInstanceUniformLocation, baseInstance);
+            glDrawElementsInstanced(primitiveTopology,
+                                    indexCount,
+                                    GL_UNSIGNED_SHORT,
+                                    indexOffset,
+                                    subInstanceCount);
+        }
+        baseInstance += subInstanceCount;
+    }
+}
+
 void RenderContextGLImpl::blitTextureToFramebufferAsDraw(
     GLuint textureID,
     const IAABB& bounds,
@@ -2000,8 +1811,9 @@
 
     m_state->bindProgram(m_blitAsDrawProgram);
     m_state->bindVAO(m_emptyVAO);
-    m_state->setWriteMasks(true, true, 0xff);
     m_state->disableBlending();
+    m_state->setDepthStencilEnabled(false, false);
+    m_state->setWriteMasks(true, true, 0xff);
     m_state->setCullFace(GL_NONE);
     glActiveTexture(GL_TEXTURE0);
     glBindTexture(GL_TEXTURE_2D, textureID);
diff --git a/renderer/src/gpu.cpp b/renderer/src/gpu.cpp
index b1a9a7d..d394936 100644
--- a/renderer/src/gpu.cpp
+++ b/renderer/src/gpu.cpp
@@ -40,6 +40,13 @@
         case DrawType::midpointFanPatches:
         case DrawType::midpointFanCenterAAPatches:
         case DrawType::outerCurvePatches:
+        case DrawType::msaaStrokes:
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaMidpointFanStencilReset:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaMidpointFanPathsCover:
+        case DrawType::msaaOuterCubics:
             drawTypeKey = 0;
             break;
         case DrawType::interiorTriangulation:
@@ -62,7 +69,7 @@
             assert(interlockMode == gpu::InterlockMode::atomics);
             drawTypeKey = 6;
             break;
-        case DrawType::stencilClipReset:
+        case DrawType::msaaStencilClipReset:
             assert(interlockMode == gpu::InterlockMode::msaa);
             drawTypeKey = 7;
             break;
@@ -756,6 +763,385 @@
            .5f;
 }
 
+static bool get_color_mask(DrawType drawType, DrawContents drawContents)
+{
+    switch (drawType)
+    {
+        case DrawType::midpointFanPatches:
+        case DrawType::midpointFanCenterAAPatches:
+        case DrawType::outerCurvePatches:
+        case DrawType::interiorTriangulation:
+        case DrawType::atlasBlit:
+        case DrawType::imageRect:
+        case DrawType::imageMesh:
+        case DrawType::atomicInitialize:
+        case DrawType::atomicResolve:
+        case DrawType::msaaStrokes:
+        case DrawType::msaaOuterCubics:
+            return true;
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaStencilClipReset:
+            return false;
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaMidpointFanPathsCover:
+            return !(drawContents & gpu::DrawContents::clipUpdate);
+        case DrawType::msaaMidpointFanStencilReset:
+            // For clockwise fill, disable color writes when cleaning up
+            // backward triangles. Clockwise only fills in forward triangles.
+            return !(drawContents & (gpu::DrawContents::clockwiseFill |
+                                     gpu::DrawContents::clipUpdate));
+    }
+    return false;
+}
+
+static void get_depth_state(DrawType drawType,
+                            InterlockMode interlockMode,
+                            DrawContents drawContents,
+                            PipelineState* pipelineState)
+{
+    if (interlockMode != InterlockMode::msaa)
+    {
+        pipelineState->depthTestEnabled = false;
+        pipelineState->depthWriteEnabled = false;
+        return;
+    }
+
+    switch (drawType)
+    {
+        case DrawType::imageRect:
+        case DrawType::imageMesh:
+        case DrawType::atlasBlit:
+        case DrawType::outerCurvePatches:
+            pipelineState->depthTestEnabled = true;
+            pipelineState->depthWriteEnabled = false;
+            break;
+
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaStencilClipReset:
+            pipelineState->depthTestEnabled = true;
+            pipelineState->depthWriteEnabled = false;
+            break;
+
+        case DrawType::msaaStrokes:
+        case DrawType::msaaOuterCubics:
+            pipelineState->depthTestEnabled = true;
+            pipelineState->depthWriteEnabled = true;
+            break;
+
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaMidpointFanPathsCover:
+            pipelineState->depthTestEnabled = true;
+            pipelineState->depthWriteEnabled =
+                !(drawContents & gpu::DrawContents::clipUpdate);
+            break;
+
+        case DrawType::msaaMidpointFanStencilReset:
+            pipelineState->depthTestEnabled = true;
+            pipelineState->depthWriteEnabled =
+                !(drawContents & (gpu::DrawContents::clockwiseFill |
+                                  gpu::DrawContents::clipUpdate));
+            break;
+
+        case DrawType::interiorTriangulation:
+        case DrawType::atomicInitialize:
+        case DrawType::atomicResolve:
+        case DrawType::midpointFanPatches:
+        case DrawType::midpointFanCenterAAPatches:
+            RIVE_UNREACHABLE();
+    }
+}
+
+static void get_stencil_state(DrawType drawType,
+                              InterlockMode interlockMode,
+                              DrawContents drawContents,
+                              PipelineState* pipelineState)
+{
+    if (interlockMode != InterlockMode::msaa)
+    {
+        pipelineState->stencilTestEnabled = false;
+        // Use default values since stencil is disabled. This is (mildly)
+        // important because GL sets the stencilWriteMask regardless of whether
+        // stencil is enabled or not.
+        pipelineState->stencilCompareMask = 0xff;
+        pipelineState->stencilWriteMask = 0xff;
+        pipelineState->stencilReference = 0xff;
+        return;
+    }
+
+    const bool hasActiveClip = (drawContents & gpu::DrawContents::activeClip);
+    const bool isClipUpdate = (drawContents & gpu::DrawContents::clipUpdate);
+    switch (drawType)
+    {
+        case DrawType::imageRect:
+        case DrawType::imageMesh:
+        case DrawType::atlasBlit:
+        case DrawType::msaaStrokes:
+        case DrawType::msaaOuterCubics:
+            pipelineState->stencilTestEnabled = true;
+            pipelineState->stencilDoubleSided = false;
+            pipelineState->stencilCompareMask = 0xff;
+            pipelineState->stencilWriteMask = 0xff;
+            pipelineState->stencilReference = 0x80;
+            pipelineState->stencilFrontOps = {
+                .failOp = StencilOp::keep,
+                .passOp = StencilOp::keep,
+                .depthFailOp = StencilOp::keep,
+                .compareOp = hasActiveClip ? StencilCompareOp::equal
+                                           : StencilCompareOp::always,
+            };
+            break;
+
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+            // Count backward triangle hits (negative coverage) in the stencil
+            // buffer.
+            pipelineState->stencilTestEnabled = true;
+            pipelineState->stencilDoubleSided = false;
+            pipelineState->stencilCompareMask = 0xff;
+            pipelineState->stencilWriteMask = 0x7f;
+            pipelineState->stencilReference = 0x80;
+            pipelineState->stencilFrontOps = {
+                .failOp = StencilOp::keep,
+                .passOp = StencilOp::incrWrap,
+                .depthFailOp = StencilOp::keep,
+                .compareOp = hasActiveClip ? StencilCompareOp::lessOrEqual
+                                           : StencilCompareOp::always,
+            };
+            break;
+
+        case DrawType::msaaMidpointFans:
+            // Draw forward triangles, clipped by the backward triangle counts.
+            // (The depth test prevents double hits.)
+            pipelineState->stencilTestEnabled = true;
+            pipelineState->stencilDoubleSided = true;
+            pipelineState->stencilCompareMask = hasActiveClip ? 0xff : 0x7f;
+            pipelineState->stencilWriteMask = isClipUpdate ? 0xff : 0x7f;
+            pipelineState->stencilReference = 0x80;
+            pipelineState->stencilFrontOps = {
+                .failOp = StencilOp::decrClamp, // Don't wrap; 0 must stay 0
+                                                // outside the clip.
+                .passOp = isClipUpdate ? StencilOp::replace : StencilOp::keep,
+                .depthFailOp = StencilOp::keep,
+                .compareOp = StencilCompareOp::equal,
+            };
+            pipelineState->stencilBackOps = {
+                .failOp = StencilOp::keep,
+                .passOp = isClipUpdate ? StencilOp::replace : StencilOp::zero,
+                .depthFailOp = StencilOp::keep,
+                .compareOp = StencilCompareOp::less,
+            };
+            break;
+
+        case DrawType::msaaMidpointFanStencilReset:
+            // Clean up backward triangles in the stencil buffer, (also filling
+            // negative winding numbers for nonZero fill).
+            pipelineState->stencilTestEnabled = true;
+            pipelineState->stencilDoubleSided = true;
+            pipelineState->stencilCompareMask = hasActiveClip ? 0xff : 0x7f;
+            pipelineState->stencilWriteMask =
+                (drawContents & gpu::DrawContents::clockwiseFill)
+                    // For clockwise fill, disable clip-bit writes when cleaning
+                    // up backward triangles. Clockwise only fills in forward
+                    // triangles.
+                    ? 0x7f
+                    : (isClipUpdate ? 0xff : 0x7f);
+            pipelineState->stencilReference = 0x80;
+            pipelineState->stencilFrontOps = {
+                .failOp = StencilOp::decrClamp, // Don't wrap; 0 must stay 0
+                                                // outside the clip.
+                .passOp = isClipUpdate ? StencilOp::replace : StencilOp::keep,
+                .depthFailOp = StencilOp::keep,
+                .compareOp = StencilCompareOp::equal,
+            };
+            pipelineState->stencilBackOps = {
+                .failOp = StencilOp::keep,
+                .passOp = isClipUpdate ? StencilOp::replace : StencilOp::zero,
+                .depthFailOp = StencilOp::keep,
+                .compareOp = StencilCompareOp::less,
+            };
+            break;
+
+        case DrawType::msaaMidpointFanPathsStencil:
+            // Just stencil the path into the stencil buffer. This is used for
+            // nested clip updates and for evenOdd paths.
+            assert(drawContents & (gpu::DrawContents::evenOddFill) ||
+                   (drawContents & gpu::kNestedClipUpdateMask) ==
+                       gpu::kNestedClipUpdateMask);
+            pipelineState->stencilTestEnabled = true;
+            pipelineState->stencilDoubleSided = true;
+            pipelineState->stencilCompareMask = 0xff;
+            pipelineState->stencilWriteMask =
+                (drawContents & gpu::DrawContents::evenOddFill) ? 0x1 : 0x7f;
+            pipelineState->stencilReference = 0x80;
+            // Decrement front-facing triangles so the MSB is set when
+            // clockwise.
+            pipelineState->stencilFrontOps = {
+                .failOp = StencilOp::keep,
+                .passOp = StencilOp::decrWrap,
+                .depthFailOp = StencilOp::keep,
+                .compareOp = hasActiveClip ? StencilCompareOp::lessOrEqual
+                                           : StencilCompareOp::always,
+            };
+            pipelineState->stencilBackOps = {
+                .failOp = pipelineState->stencilFrontOps.failOp,
+                .passOp = StencilOp::incrWrap,
+                .depthFailOp = pipelineState->stencilFrontOps.depthFailOp,
+                .compareOp = pipelineState->stencilFrontOps.compareOp,
+            };
+            break;
+
+        case DrawType::msaaMidpointFanPathsCover:
+            // Draw & reset stencil winding numbers. This is only needed for
+            // evenOdd paths.
+            assert(drawContents & gpu::DrawContents::evenOddFill);
+            pipelineState->stencilTestEnabled = true;
+            pipelineState->stencilDoubleSided = false;
+            pipelineState->stencilCompareMask = 0x7f;
+            pipelineState->stencilWriteMask = isClipUpdate ? 0xff : 0x1;
+            pipelineState->stencilReference = 0x80;
+            pipelineState->stencilFrontOps = {
+                .failOp = StencilOp::keep,
+                .passOp = isClipUpdate ? StencilOp::replace : StencilOp::zero,
+                .depthFailOp = StencilOp::keep,
+                .compareOp = StencilCompareOp::notEqual,
+            };
+            break;
+
+        case DrawType::msaaStencilClipReset:
+            pipelineState->stencilTestEnabled = true;
+            pipelineState->stencilDoubleSided = false;
+            if ((drawContents & gpu::kNestedClipUpdateMask) ==
+                gpu::kNestedClipUpdateMask)
+            {
+                // The nested clip just got stencilled and left in the stencil
+                // buffer. Intersect it with the existing clip. (Erasing regions
+                // of the existing clip that are outside the nested clip.)
+                pipelineState->stencilCompareMask =
+                    (drawContents & gpu::DrawContents::clockwiseFill)
+                        // clockwise: (0x80 & 0xc0) < (stencilValue & 0xc0)
+                        //   => "If clipbit is set and winding is negative"
+                        //   => "If clipbit is set and winding is clockwise"
+                        //      (because clockwise decrements)
+                        //
+                        ? 0xc0
+                        // non-clockwise: 0x80 < stencilValue
+                        //   => "If clipbit is set and winding is nonzero"
+                        : 0xff;
+                pipelineState->stencilWriteMask = 0xff;
+                pipelineState->stencilReference = 0x80;
+                pipelineState->stencilFrontOps = {
+                    .failOp = StencilOp::zero,
+                    .passOp = StencilOp::replace,
+                    .depthFailOp = StencilOp::keep,
+                    .compareOp = StencilCompareOp::less,
+                };
+            }
+            else
+            {
+                // Clear the entire previous clip.
+                pipelineState->stencilCompareMask = 0xff;
+                pipelineState->stencilWriteMask = 0xff;
+                pipelineState->stencilReference = 0x00;
+                pipelineState->stencilFrontOps = {
+                    .failOp = StencilOp::keep,
+                    .passOp = StencilOp::zero,
+                    .depthFailOp = StencilOp::keep,
+                    .compareOp = StencilCompareOp::notEqual,
+                };
+            }
+            break;
+
+        case DrawType::interiorTriangulation:
+        case DrawType::atomicInitialize:
+        case DrawType::atomicResolve:
+        case DrawType::midpointFanPatches:
+        case DrawType::midpointFanCenterAAPatches:
+        case DrawType::outerCurvePatches:
+            RIVE_UNREACHABLE();
+    }
+}
+
+static CullFace get_cull_face(DrawType drawType)
+{
+    switch (drawType)
+    {
+        case DrawType::midpointFanPatches:
+        case DrawType::midpointFanCenterAAPatches:
+        case DrawType::outerCurvePatches:
+        case DrawType::interiorTriangulation:
+        case DrawType::atlasBlit:
+        case DrawType::msaaStrokes:
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaStencilClipReset:
+            return CullFace::counterclockwise;
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFanStencilReset:
+            // clockwise is always the front face in Rive, but for a couple
+            // draws we encode some stencil work in the counterclockwise face.
+            // It's done this way because the cull face is often supported as
+            // dynamic state, so we're keeping the option open to stuff two
+            // operations (clockwise and counterclockwise) into a single
+            // pipeline, and then select between them with the dynamic cull
+            // face.
+            return CullFace::clockwise;
+        case DrawType::imageRect:
+        case DrawType::imageMesh:
+        case DrawType::atomicResolve:
+        case DrawType::atomicInitialize:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaMidpointFanPathsCover:
+        case DrawType::msaaOuterCubics:
+            return CullFace::none;
+    }
+    RIVE_UNREACHABLE();
+}
+
+void get_pipeline_state(DrawType drawType,
+                        InterlockMode interlockMode,
+                        DrawContents drawContents,
+                        PipelineState* pipelineState)
+{
+#ifndef NDEBUG
+    // Ensure drawType is compatible with the interlock mode.
+    switch (drawType)
+    {
+        case DrawType::atlasBlit:
+        case DrawType::imageMesh:
+            break;
+
+        case DrawType::midpointFanPatches:
+        case DrawType::midpointFanCenterAAPatches:
+        case DrawType::outerCurvePatches:
+        case DrawType::interiorTriangulation:
+            assert(interlockMode != InterlockMode::msaa);
+            break;
+
+        case DrawType::imageRect:
+        case DrawType::atomicResolve:
+        case DrawType::atomicInitialize:
+            assert(interlockMode == InterlockMode::atomics);
+            break;
+
+        case DrawType::msaaStrokes:
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFanStencilReset:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaMidpointFanPathsCover:
+        case DrawType::msaaOuterCubics:
+        case DrawType::msaaStencilClipReset:
+            assert(interlockMode == InterlockMode::msaa);
+            break;
+    }
+#endif
+
+    pipelineState->colorWriteEnabled = get_color_mask(drawType, drawContents);
+    get_depth_state(drawType, interlockMode, drawContents, pipelineState);
+    get_stencil_state(drawType, interlockMode, drawContents, pipelineState);
+    pipelineState->cullFace = get_cull_face(drawType);
+}
+
 // Borrowed from:
 // https://stackoverflow.com/questions/1659440/32-bit-to-16-bit-floating-point-conversion
 //
diff --git a/renderer/src/metal/background_shader_compiler.mm b/renderer/src/metal/background_shader_compiler.mm
index e60e559..2cb9b02 100644
--- a/renderer/src/metal/background_shader_compiler.mm
+++ b/renderer/src/metal/background_shader_compiler.mm
@@ -230,7 +230,14 @@
                 [source appendFormat:@"%s\n", gpu::glsl::atomic_draw];
 #endif
                 break;
-            case DrawType::stencilClipReset:
+            case DrawType::msaaStrokes:
+            case DrawType::msaaMidpointFanBorrowedCoverage:
+            case DrawType::msaaMidpointFans:
+            case DrawType::msaaMidpointFanStencilReset:
+            case DrawType::msaaMidpointFanPathsStencil:
+            case DrawType::msaaMidpointFanPathsCover:
+            case DrawType::msaaOuterCubics:
+            case DrawType::msaaStencilClipReset:
                 RIVE_UNREACHABLE();
         }
 
diff --git a/renderer/src/metal/render_context_metal_impl.mm b/renderer/src/metal/render_context_metal_impl.mm
index 98ef812..02c72ba 100644
--- a/renderer/src/metal/render_context_metal_impl.mm
+++ b/renderer/src/metal/render_context_metal_impl.mm
@@ -195,7 +195,14 @@
                 break;
             case DrawType::atomicInitialize:
             case DrawType::atomicResolve:
-            case DrawType::stencilClipReset:
+            case DrawType::msaaStrokes:
+            case DrawType::msaaMidpointFanBorrowedCoverage:
+            case DrawType::msaaMidpointFans:
+            case DrawType::msaaMidpointFanStencilReset:
+            case DrawType::msaaMidpointFanPathsStencil:
+            case DrawType::msaaMidpointFanPathsCover:
+            case DrawType::msaaOuterCubics:
+            case DrawType::msaaStencilClipReset:
                 RIVE_UNREACHABLE();
         }
 
@@ -1572,7 +1579,14 @@
                             vertexCount:4];
                 break;
             }
-            case DrawType::stencilClipReset:
+            case DrawType::msaaStrokes:
+            case DrawType::msaaMidpointFanBorrowedCoverage:
+            case DrawType::msaaMidpointFans:
+            case DrawType::msaaMidpointFanStencilReset:
+            case DrawType::msaaMidpointFanPathsStencil:
+            case DrawType::msaaMidpointFanPathsCover:
+            case DrawType::msaaOuterCubics:
+            case DrawType::msaaStencilClipReset:
             {
                 RIVE_UNREACHABLE();
             }
diff --git a/renderer/src/render_context.cpp b/renderer/src/render_context.cpp
index 6d8f442..0c63cbc 100644
--- a/renderer/src/render_context.cpp
+++ b/renderer/src/render_context.cpp
@@ -389,7 +389,7 @@
     int passCountInBatch = 0;
     for (size_t i = 0; i < drawCount; ++i)
     {
-        draws[i]->determineSubpasses();
+        draws[i]->countSubpasses();
         assert(draws[i]->prepassCount() >= 0);
         assert(draws[i]->subpassCount() >= 0);
         assert(draws[i]->prepassCount() + draws[i]->subpassCount() >= 1);
@@ -1216,17 +1216,18 @@
         constexpr static int kDrawTypeShift = 45;
         constexpr static int64_t kDrawTypeMask RIVE_MAYBE_UNUSED =
             7llu << kDrawTypeShift;
-        constexpr static int kTextureHashShift = 30;
-        constexpr static int64_t kTextureHashMask = 0x7fffllu
+        constexpr static int kTextureHashShift = 31;
+        constexpr static int64_t kTextureHashMask = 0x3fffllu
                                                     << kTextureHashShift;
-        constexpr static int kBlendModeShift = 26;
+        constexpr static int kBlendModeShift = 27;
         constexpr static int kBlendModeMask = 0xf << kBlendModeShift;
-        constexpr static int kDrawContentsShift = 17;
+        constexpr static int kDrawContentsShift = 18;
         constexpr static int64_t kDrawContentsMask = 0x1ffllu
                                                      << kDrawContentsShift;
-        constexpr static int kDrawIndexShift = 1;
+        constexpr static int kDrawIndexShift = 2;
         constexpr static int64_t kDrawIndexMask = 0x7fff << kDrawIndexShift;
-        constexpr static int64_t kSubpassIndexMask = 0x1;
+        constexpr static int64_t kSubpassIndexMask = 0x3;
+
         for (size_t i = 0; i < m_draws.size(); ++i)
         {
             Draw* draw = m_draws[i].get();
@@ -1399,7 +1400,7 @@
         // condensed/batched list of low-level draws.
         int64_t priorSignedKey =
             !indirectDrawList.empty() ? indirectDrawList[0] : 0;
-        for (int64_t signedKey : indirectDrawList)
+        for (const int64_t signedKey : indirectDrawList)
         {
             assert(signedKey >= priorSignedKey);
             if ((priorSignedKey & needsBarrierMask) !=
@@ -2356,6 +2357,7 @@
 
 void RenderContext::LogicalFlush::pushMidpointFanDraw(
     const PathDraw* draw,
+    gpu::DrawType drawType,
     uint32_t tessVertexCount,
     uint32_t tessLocation,
     gpu::ShaderMiscFlags shaderMiscFlags)
@@ -2371,17 +2373,12 @@
     // flush() is responsible for alignment.
     assert(instanceCount * kMidpointFanPatchSegmentSpan == tessVertexCount);
 
-    pushPathDraw(draw,
-                 draw->isFeatheredFill()
-                     ? gpu::DrawType::midpointFanCenterAAPatches
-                     : gpu::DrawType::midpointFanPatches,
-                 shaderMiscFlags,
-                 instanceCount,
-                 baseInstance);
+    pushPathDraw(draw, drawType, shaderMiscFlags, instanceCount, baseInstance);
 }
 
 void RenderContext::LogicalFlush::pushOuterCubicsDraw(
     const PathDraw* draw,
+    gpu::DrawType drawType,
     uint32_t tessVertexCount,
     uint32_t tessLocation,
     gpu::ShaderMiscFlags shaderMiscFlags)
@@ -2397,11 +2394,7 @@
     // flush() is responsible for alignment.
     assert(instanceCount * kOuterCurvePatchSegmentSpan == tessVertexCount);
 
-    pushPathDraw(draw,
-                 gpu::DrawType::outerCurvePatches,
-                 shaderMiscFlags,
-                 instanceCount,
-                 baseInstance);
+    pushPathDraw(draw, drawType, shaderMiscFlags, instanceCount, baseInstance);
 }
 
 size_t RenderContext::LogicalFlush::pushInteriorTriangulationDraw(
@@ -2521,7 +2514,7 @@
     m_ctx->m_triangleVertexData.emplace_back(Vec2D{l, t}, 0, z);
     m_ctx->m_triangleVertexData.emplace_back(Vec2D{r, t}, 0, z);
     pushDraw(draw,
-             DrawType::stencilClipReset,
+             DrawType::msaaStencilClipReset,
              gpu::ShaderMiscFlags::none,
              PaintType::clipUpdate,
              6,
@@ -2643,7 +2636,14 @@
         case DrawType::outerCurvePatches:
         case DrawType::interiorTriangulation:
         case DrawType::atlasBlit:
-        case DrawType::stencilClipReset:
+        case DrawType::msaaStrokes:
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaMidpointFanStencilReset:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaMidpointFanPathsCover:
+        case DrawType::msaaOuterCubics:
+        case DrawType::msaaStencilClipReset:
             if (!m_drawList.empty())
             {
                 const DrawBatch& currentBatch = m_drawList.tail();
@@ -2656,6 +2656,18 @@
                                               draw) &&
                     can_combine_draw_images(currentBatch.imageTexture,
                                             draw->imageTexture());
+                if (canMergeWithPreviousBatch &&
+                    currentBatch.baseElement + currentBatch.elementCount !=
+                        baseElement)
+                {
+                    // In MSAA mode, multiple subpasses reference the same
+                    // tessellation data. Although rare, this breaks the
+                    // guarantee we have in other modes that mergeable batches
+                    // will always have contiguous patches.
+                    assert(m_ctx->frameInterlockMode() ==
+                           gpu::InterlockMode::msaa);
+                    canMergeWithPreviousBatch = false;
+                }
                 break;
             }
             [[fallthrough]];
@@ -2676,9 +2688,6 @@
         batch = &m_drawList.tail();
         assert(batch->drawType == drawType);
         assert(batch->shaderMiscFlags == shaderMiscFlags);
-        // Since draw data is always uploaded in order, we're guaranteed that
-        // the data for this draw is contiguous with the batch we're merging
-        // into.
         assert(batch->baseElement + batch->elementCount == baseElement);
         batch->elementCount += elementCount;
     }
diff --git a/renderer/src/rive_renderer.cpp b/renderer/src/rive_renderer.cpp
index f843784..1b0672b 100644
--- a/renderer/src/rive_renderer.cpp
+++ b/renderer/src/rive_renderer.cpp
@@ -392,7 +392,7 @@
     }
 
     clipAndPushDraw(gpu::DrawUniquePtr(
-        m_context->make<gpu::ImageMeshDraw>(gpu::Draw::kFullscreenPixelBounds,
+        m_context->make<gpu::ImageMeshDraw>(gpu::Draw::FULLSCREEN_PIXEL_BOUNDS,
                                             m_stack.back().matrix,
                                             blendMode,
                                             ref_rcp(texture),
diff --git a/renderer/src/shaders/draw_clockwise_image_mesh.glsl b/renderer/src/shaders/draw_clockwise_image_mesh.glsl
index cc12c5e..7a12caf 100644
--- a/renderer/src/shaders/draw_clockwise_image_mesh.glsl
+++ b/renderer/src/shaders/draw_clockwise_image_mesh.glsl
@@ -58,7 +58,8 @@
     VARYING_UNPACK(v_texCoord, float2);
 
     half4 meshColor = TEXTURE_SAMPLE(@imageTexture, imageSampler, v_texCoord);
-    meshColor.a *= imageDrawUniforms.opacity;
+    meshColor = make_half4(unmultiply_rgb(meshColor),
+                           meshColor.a * imageDrawUniforms.opacity);
 
     EMIT_FRAG_DATA(meshColor);
 }
diff --git a/renderer/src/shaders/draw_clockwise_path.glsl b/renderer/src/shaders/draw_clockwise_path.glsl
index cefa2db..62d0d80 100644
--- a/renderer/src/shaders/draw_clockwise_path.glsl
+++ b/renderer/src/shaders/draw_clockwise_path.glsl
@@ -354,7 +354,8 @@
                                             imageSampler,
                                             paintCoord,
                                             lod);
-            paintColor.a *= opacity;
+            paintColor =
+                make_half4(unmultiply_rgb(paintColor), paintColor.a * opacity);
         }
     }
 
diff --git a/renderer/src/vulkan/render_context_vulkan_impl.cpp b/renderer/src/vulkan/render_context_vulkan_impl.cpp
index ec1d41e..8af4baf 100644
--- a/renderer/src/vulkan/render_context_vulkan_impl.cpp
+++ b/renderer/src/vulkan/render_context_vulkan_impl.cpp
@@ -1619,7 +1619,14 @@
                 case DrawType::imageRect:
                 case DrawType::atomicResolve:
                 case DrawType::atomicInitialize:
-                case DrawType::stencilClipReset:
+                case DrawType::msaaStrokes:
+                case DrawType::msaaMidpointFanBorrowedCoverage:
+                case DrawType::msaaMidpointFans:
+                case DrawType::msaaMidpointFanStencilReset:
+                case DrawType::msaaMidpointFanPathsStencil:
+                case DrawType::msaaMidpointFanPathsCover:
+                case DrawType::msaaOuterCubics:
+                case DrawType::msaaStencilClipReset:
                     RIVE_UNREACHABLE();
             }
         }
@@ -1695,7 +1702,14 @@
                     break;
 
                 case DrawType::atomicInitialize:
-                case DrawType::stencilClipReset:
+                case DrawType::msaaStrokes:
+                case DrawType::msaaMidpointFanBorrowedCoverage:
+                case DrawType::msaaMidpointFans:
+                case DrawType::msaaMidpointFanStencilReset:
+                case DrawType::msaaMidpointFanPathsStencil:
+                case DrawType::msaaMidpointFanPathsCover:
+                case DrawType::msaaOuterCubics:
+                case DrawType::msaaStencilClipReset:
                     RIVE_UNREACHABLE();
             }
         }
@@ -1743,7 +1757,14 @@
                 case DrawType::imageRect:
                 case DrawType::atomicResolve:
                 case DrawType::atomicInitialize:
-                case DrawType::stencilClipReset:
+                case DrawType::msaaStrokes:
+                case DrawType::msaaMidpointFanBorrowedCoverage:
+                case DrawType::msaaMidpointFans:
+                case DrawType::msaaMidpointFanStencilReset:
+                case DrawType::msaaMidpointFanPathsStencil:
+                case DrawType::msaaMidpointFanPathsCover:
+                case DrawType::msaaOuterCubics:
+                case DrawType::msaaStencilClipReset:
                     RIVE_UNREACHABLE();
             }
         }
@@ -2020,7 +2041,14 @@
                 break;
 
             case DrawType::atomicInitialize:
-            case DrawType::stencilClipReset:
+            case DrawType::msaaStrokes:
+            case DrawType::msaaMidpointFans:
+            case DrawType::msaaMidpointFanBorrowedCoverage:
+            case DrawType::msaaMidpointFanStencilReset:
+            case DrawType::msaaMidpointFanPathsStencil:
+            case DrawType::msaaMidpointFanPathsCover:
+            case DrawType::msaaOuterCubics:
+            case DrawType::msaaStencilClipReset:
                 RIVE_UNREACHABLE();
         }
 
@@ -3998,7 +4026,14 @@
             }
 
             case DrawType::atomicInitialize:
-            case DrawType::stencilClipReset:
+            case DrawType::msaaStrokes:
+            case DrawType::msaaMidpointFanBorrowedCoverage:
+            case DrawType::msaaMidpointFans:
+            case DrawType::msaaMidpointFanStencilReset:
+            case DrawType::msaaMidpointFanPathsStencil:
+            case DrawType::msaaMidpointFanPathsCover:
+            case DrawType::msaaOuterCubics:
+            case DrawType::msaaStencilClipReset:
                 RIVE_UNREACHABLE();
         }
 
diff --git a/renderer/src/webgpu/render_context_webgpu_impl.cpp b/renderer/src/webgpu/render_context_webgpu_impl.cpp
index 6f9594a..3661bde 100644
--- a/renderer/src/webgpu/render_context_webgpu_impl.cpp
+++ b/renderer/src/webgpu/render_context_webgpu_impl.cpp
@@ -824,7 +824,14 @@
                     addDefine(GLSL_RESOLVE_PLS);
                     RIVE_UNREACHABLE();
                     break;
-                case DrawType::stencilClipReset:
+                case DrawType::msaaStrokes:
+                case DrawType::msaaMidpointFanBorrowedCoverage:
+                case DrawType::msaaMidpointFans:
+                case DrawType::msaaMidpointFanStencilReset:
+                case DrawType::msaaMidpointFanPathsStencil:
+                case DrawType::msaaMidpointFanPathsCover:
+                case DrawType::msaaOuterCubics:
+                case DrawType::msaaStencilClipReset:
                     RIVE_UNREACHABLE();
                     break;
             }
@@ -869,7 +876,14 @@
                 case DrawType::imageRect:
                 case DrawType::atomicInitialize:
                 case DrawType::atomicResolve:
-                case DrawType::stencilClipReset:
+                case DrawType::msaaStrokes:
+                case DrawType::msaaMidpointFanBorrowedCoverage:
+                case DrawType::msaaMidpointFans:
+                case DrawType::msaaMidpointFanStencilReset:
+                case DrawType::msaaMidpointFanPathsStencil:
+                case DrawType::msaaMidpointFanPathsCover:
+                case DrawType::msaaOuterCubics:
+                case DrawType::msaaStencilClipReset:
                     RIVE_UNREACHABLE();
                     break;
             }
@@ -952,7 +966,14 @@
                     break;
                 case DrawType::atomicInitialize:
                 case DrawType::atomicResolve:
-                case DrawType::stencilClipReset:
+                case DrawType::msaaStrokes:
+                case DrawType::msaaMidpointFanBorrowedCoverage:
+                case DrawType::msaaMidpointFans:
+                case DrawType::msaaMidpointFanStencilReset:
+                case DrawType::msaaMidpointFanPathsStencil:
+                case DrawType::msaaMidpointFanPathsCover:
+                case DrawType::msaaOuterCubics:
+                case DrawType::msaaStencilClipReset:
                     RIVE_UNREACHABLE();
             }
         }
@@ -2069,7 +2090,14 @@
             break;
         case DrawType::atomicInitialize:
         case DrawType::atomicResolve:
-        case DrawType::stencilClipReset:
+        case DrawType::msaaStrokes:
+        case DrawType::msaaMidpointFanBorrowedCoverage:
+        case DrawType::msaaMidpointFans:
+        case DrawType::msaaMidpointFanStencilReset:
+        case DrawType::msaaMidpointFanPathsStencil:
+        case DrawType::msaaMidpointFanPathsCover:
+        case DrawType::msaaOuterCubics:
+        case DrawType::msaaStencilClipReset:
             RIVE_UNREACHABLE();
     }
 
@@ -2710,7 +2738,14 @@
             }
             case DrawType::atomicInitialize:
             case DrawType::atomicResolve:
-            case DrawType::stencilClipReset:
+            case DrawType::msaaStrokes:
+            case DrawType::msaaMidpointFanBorrowedCoverage:
+            case DrawType::msaaMidpointFans:
+            case DrawType::msaaMidpointFanStencilReset:
+            case DrawType::msaaMidpointFanPathsStencil:
+            case DrawType::msaaMidpointFanPathsCover:
+            case DrawType::msaaOuterCubics:
+            case DrawType::msaaStencilClipReset:
                 RIVE_UNREACHABLE();
         }
     }
diff --git a/tests/gm/retrofittedcubictriangles.cpp b/tests/gm/retrofittedcubictriangles.cpp
index 167d00c..0598b77 100644
--- a/tests/gm/retrofittedcubictriangles.cpp
+++ b/tests/gm/retrofittedcubictriangles.cpp
@@ -35,7 +35,7 @@
 public:
     PushRetrofittedTrianglesGMDraw(RenderContext* context,
                                    const RiveRenderPaint* paint) :
-        PathDraw(kFullscreenPixelBounds,
+        PathDraw(FULLSCREEN_PIXEL_BOUNDS,
                  Mat2D(),
                  make_nonempty_placeholder_path(),
                  context->frameDescriptor().clockwiseFillOverride
@@ -63,10 +63,10 @@
             &context->perFrameAllocator());
     }
 
-    void determineSubpasses() override
+    void countSubpasses() override
     {
-        m_prepassCount = 0;
-        m_subpassCount = 1;
+        assert(m_prepassCount == 0);
+        assert(m_subpassCount == 1);
     }
 
     bool allocateResources(RenderContext::LogicalFlush* flush) override
@@ -95,6 +95,7 @@
 
             uint32_t tessLocation =
                 flush->allocateOuterCubicTessVertices(tessVertexCount);
+
             uint32_t forwardTessVertexCount, forwardTessLocation,
                 mirroredTessVertexCount, mirroredTessLocation;
             if (m_contourDirections ==
@@ -122,7 +123,6 @@
                 mirroredTessVertexCount,
                 mirroredTessLocation);
 
-            // PushRetrofittedTrianglesGMDraw specific push to render
             uint32_t contourID = tessWriter.pushContour(
                 {0, 0},
                 /*isStroke=*/false,
@@ -141,15 +141,18 @@
                                          RETROFITTED_TRIANGLE_CONTOUR_FLAG);
             }
 
-            auto shaderMiscFlags = gpu::ShaderMiscFlags::none;
             if (flush->frameDescriptor().clockwiseFillOverride)
             {
                 m_drawContents |= gpu::DrawContents::clockwiseFill;
             }
+
             flush->pushOuterCubicsDraw(this,
+                                       m_coverageType == CoverageType::msaa
+                                           ? gpu::DrawType::msaaOuterCubics
+                                           : gpu::DrawType::outerCurvePatches,
                                        tessVertexCount,
                                        tessLocation,
-                                       shaderMiscFlags);
+                                       gpu::ShaderMiscFlags::none);
         }
     }
 };