perf(renderer): Always enable primitive restart (#15361)

Rive does not use primitive restart, but enable it anyway for triangle
strips and lists (where supported) for two reasons:
 1) Consistency. Metal and WebGL always enable it for strips and lists, and
    WebGPU for strips, so match their behavior.
 2) Performance on Adreno. Enabling it for lists bizarrely makes Adreno
    inkbleed over 2x faster.

Rive-Mono-Commit: 7599990f2f2e6dec7c3328f3f16f360afb5ea72c
diff --git a/renderer/include/rive/renderer/vulkan/vulkan_context.hpp b/renderer/include/rive/renderer/vulkan/vulkan_context.hpp
index 98fc37c..2502a4d 100644
--- a/renderer/include/rive/renderer/vulkan/vulkan_context.hpp
+++ b/renderer/include/rive/renderer/vulkan/vulkan_context.hpp
@@ -35,6 +35,9 @@
   // VK_EXT_color_write_enable.
   bool colorWriteEnable = false;
 
+  // VK_EXT_primitive_topology_list_restart.
+  bool primitiveTopologyListRestart = false;
+
   // Indicates a nonconformant driver, like MoltenVK.
   bool VK_KHR_portability_subset = false;
 
diff --git a/renderer/rive_vk_bootstrap/include/rive_vk_bootstrap/vulkan_device.hpp b/renderer/rive_vk_bootstrap/include/rive_vk_bootstrap/vulkan_device.hpp
index 808ad0c..08e14c5 100644
--- a/renderer/rive_vk_bootstrap/include/rive_vk_bootstrap/vulkan_device.hpp
+++ b/renderer/rive_vk_bootstrap/include/rive_vk_bootstrap/vulkan_device.hpp
@@ -119,6 +119,12 @@
       const std::vector<VkExtensionProperties>& supportedExtensions,
       std::vector<const char*>& extensions);
 
+  std::optional<VkPhysicalDevicePrimitiveTopologyListRestartFeaturesEXT>
+  tryEnableListRestartFeatures(
+      VulkanInstance&,
+      const std::vector<VkExtensionProperties>& supportedExtensions,
+      std::vector<const char*>& extensions);
+
   bool addExtensionIfSupported(
       const char* name,
       const std::vector<VkExtensionProperties>& supportedExtensions,
diff --git a/renderer/rive_vk_bootstrap/src/vulkan_device.cpp b/renderer/rive_vk_bootstrap/src/vulkan_device.cpp
index 038e54b..2785b9e 100644
--- a/renderer/rive_vk_bootstrap/src/vulkan_device.cpp
+++ b/renderer/rive_vk_bootstrap/src/vulkan_device.cpp
@@ -236,6 +236,8 @@
       interlockFeatures;
   std::optional<VkPhysicalDeviceColorWriteEnableFeaturesEXT>
       colorWriteEnableFeatures;
+  std::optional<VkPhysicalDevicePrimitiveTopologyListRestartFeaturesEXT>
+      listRestartFeatures;
 
   if (!opts.coreFeaturesOnly)
   {
@@ -251,6 +253,10 @@
         instance,
         supportedExtensions,
         addedExtensions);
+    listRestartFeatures = tryEnableListRestartFeatures(
+        instance,
+        supportedExtensions,
+        addedExtensions);
   }
 
   // Get our list of queue family properties.
@@ -319,6 +325,12 @@
         deviceCreateInfo.pNext);
     deviceCreateInfo.pNext = &colorWriteEnableFeatures.value();
   }
+  if (listRestartFeatures.has_value())
+  {
+    listRestartFeatures.value().pNext = const_cast<void*>(
+        deviceCreateInfo.pNext);
+    deviceCreateInfo.pNext = &listRestartFeatures.value();
+  }
 
   DEFINE_AND_LOAD_INSTANCE_FUNC_OR_RETURN(vkCreateDevice, instance);
   VK_CONFIRM_OR_RETURN_MSG(
@@ -406,6 +418,8 @@
       printf("%sfragmentShaderPixelInterlock", *commaSeparator);
     if (m_riveVulkanFeatures.colorWriteEnable)
       printf("%scolorWriteEnable", *commaSeparator);
+    if (m_riveVulkanFeatures.primitiveTopologyListRestart)
+      printf("%sprimitiveTopologyListRestart", *commaSeparator);
     if (m_riveVulkanFeatures.VK_KHR_portability_subset)
       printf("%sVK_KHR_portability_subset", *commaSeparator);
     printf(" ] ====\n");
@@ -807,6 +821,43 @@
   return std::nullopt;
 }
 
+std::optional<VkPhysicalDevicePrimitiveTopologyListRestartFeaturesEXT>
+VulkanDevice::tryEnableListRestartFeatures(
+    VulkanInstance& instance,
+    const std::vector<VkExtensionProperties>& supportedExtensions,
+    std::vector<const char*>& extensions)
+{
+  if (addExtensionIfSupported(
+          VK_EXT_PRIMITIVE_TOPOLOGY_LIST_RESTART_EXTENSION_NAME,
+          supportedExtensions,
+          extensions))
+  {
+    constexpr static VkPhysicalDevicePrimitiveTopologyListRestartFeaturesEXT
+        requestedListRestartFeatures = {
+            .sType =
+                VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIMITIVE_TOPOLOGY_LIST_RESTART_FEATURES_EXT,
+            .primitiveTopologyListRestart = VK_TRUE,
+        };
+
+    auto testedListRestartFeatures = requestedListRestartFeatures;
+
+    // Test to see if this is supported
+    VkPhysicalDeviceFeatures2 features = {
+        .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
+        .pNext = &testedListRestartFeatures,
+    };
+
+    if (instance.tryGetPhysicalDeviceFeatures2(m_physicalDevice, &features) &&
+        testedListRestartFeatures.primitiveTopologyListRestart)
+    {
+      m_riveVulkanFeatures.primitiveTopologyListRestart = true;
+      return requestedListRestartFeatures;
+    }
+  }
+
+  return std::nullopt;
+}
+
 bool VulkanDevice::addExtensionIfSupported(
     const char* name,
     const std::vector<VkExtensionProperties>& supportedExtensions,
diff --git a/renderer/src/d3d12/d3d12_pipeline_manager.cpp b/renderer/src/d3d12/d3d12_pipeline_manager.cpp
index c6e7891..62ce080 100644
--- a/renderer/src/d3d12/d3d12_pipeline_manager.cpp
+++ b/renderer/src/d3d12/d3d12_pipeline_manager.cpp
@@ -46,6 +46,14 @@
 
 namespace rive::gpu
 {
+// Rive does not use primitive restart, but enables it anyway for triangle
+// strips and lists (where supported) for two reasons:
+//  1) Consistency. Metal and WebGL always enable it for strips and lists, and
+//     WebGPU for strips, so match their behavior.
+//  2) Performance on Adreno. Enabling it for lists bizarrely makes Adreno
+//     inkbleed over 2x faster.
+constexpr D3D12_INDEX_BUFFER_STRIP_CUT_VALUE
+    RivePrimitiveRestartSentinel = D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF;
 
 D3D12PipelineManager::D3D12PipelineManager(
     ComPtr<ID3D12Device> device,
@@ -429,6 +437,7 @@
   psoDesc.DepthStencilState = depthStencilDesc;
   psoDesc.SampleMask = UINT_MAX;
   psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
+  psoDesc.IBStripCutValue = RivePrimitiveRestartSentinel;
   psoDesc.NumRenderTargets = 1;
   psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
   psoDesc.DSVFormat = isDepthStencil ? DXGI_FORMAT_D24_UNORM_S8_UINT
@@ -521,6 +530,7 @@
   psoDesc.DepthStencilState.StencilEnable = FALSE;
   psoDesc.SampleMask = UINT_MAX;
   psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
+  psoDesc.IBStripCutValue = RivePrimitiveRestartSentinel;
   psoDesc.NumRenderTargets = 1;
   psoDesc.RTVFormats[0] = DXGI_FORMAT_R32G32B32A32_UINT;
   psoDesc.SampleDesc.Count = 1;
@@ -578,6 +588,7 @@
   psoDesc.DepthStencilState.StencilEnable = FALSE;
   psoDesc.SampleMask = UINT_MAX;
   psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
+  psoDesc.IBStripCutValue = RivePrimitiveRestartSentinel;
   psoDesc.NumRenderTargets = 1;
   psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
   psoDesc.SampleDesc.Count = 1;
@@ -644,6 +655,7 @@
   psoDesc.DepthStencilState.StencilEnable = FALSE;
   psoDesc.SampleMask = UINT_MAX;
   psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
+  psoDesc.IBStripCutValue = RivePrimitiveRestartSentinel;
   psoDesc.NumRenderTargets = 1;
   psoDesc.RTVFormats[0] = DXGI_FORMAT_R16_FLOAT;
   psoDesc.SampleDesc.Count = 1;
diff --git a/renderer/src/gl/gl_state.cpp b/renderer/src/gl/gl_state.cpp
index e682467..43614e0 100644
--- a/renderer/src/gl/gl_state.cpp
+++ b/renderer/src/gl/gl_state.cpp
@@ -26,10 +26,14 @@
   // Low-effort attempt to reset core state we don't use to default values.
   glDisable(GL_POLYGON_OFFSET_FILL);
 #ifndef RIVE_WEBGL
+  // Rive does not use primitive restart, but enables it anyway for triangle
+  // strips and lists (where supported) for two reasons:
+  //  1) Consistency. Metal and WebGL always enable it for strips and lists, and
+  //     WebGPU for strips, so match their behavior.
+  //  2) Performance on Adreno. Enabling it for lists bizarrely makes Adreno
+  //     inkbleed over 2x faster.
   // https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.18
-  // WebGL 2.0 behaves as though PRIMITIVE_RESTART_FIXED_INDEX were always
-  // enabled.
-  glDisable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
+  glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
 #endif
   glDisable(GL_RASTERIZER_DISCARD);
   glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
diff --git a/renderer/src/gpu.cpp b/renderer/src/gpu.cpp
index 0be3ee1..fdb9b52 100644
--- a/renderer/src/gpu.cpp
+++ b/renderer/src/gpu.cpp
@@ -918,8 +918,10 @@
   {
     const uint32_t patchBaseVertex = patch << patchStrideLog2;
     const auto emitPatchVertex = [&](uint32_t patchVertexID) {
-      indices[indexCount++] = math::lossless_numeric_cast<uint16_t>(
+      auto idx = math::lossless_numeric_cast<uint16_t>(
           patchBaseVertex + patchVertexID);
+      assert(idx != 0xffff); // 0xffff is reserved for primitive restart.
+      indices[indexCount++] = idx;
     };
     // Middle-out fan topology, matching the fan half of
     // generate_buffer_data_for_patch_type().
@@ -985,6 +987,7 @@
         const uint32_t v3 = v1 + 1;
         for (uint32_t v : {v0, v1, v2, v2, v1, v3})
         {
+          assert(v != 0xffff); // 0xffff is reserved for primitive restart.
           indices[indexCount++] = math::lossless_numeric_cast<uint16_t>(v);
         }
       }
@@ -999,6 +1002,8 @@
     PatchVertex vertices[kPatchVertexBufferCount],
     uint16_t indices[kPatchIndexBufferCount])
 {
+  // 0xffff is reserved for primitive restart.
+  static_assert(kPatchVertexBufferCount < 0xffff);
   generate_buffer_data_for_patch_type(
       PatchType::midpointFan,
       vertices,
diff --git a/renderer/src/vulkan/common_layouts.hpp b/renderer/src/vulkan/common_layouts.hpp
index 8955168..ee9b246 100644
--- a/renderer/src/vulkan/common_layouts.hpp
+++ b/renderer/src/vulkan/common_layouts.hpp
@@ -237,12 +237,32 @@
         .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
 };
 
-constexpr VkPipelineInputAssemblyStateCreateInfo INPUT_ASSEMBLY_TRIANGLE_LIST =
-    {
+constexpr VkPipelineInputAssemblyStateCreateInfo InputAssemblyTriangleList = {
+    .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
+    .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
+};
+
+// Requires VK_EXT_primitive_topology_list_restart.
+constexpr VkPipelineInputAssemblyStateCreateInfo
+    InputAssemblyTriangleListRestart = {
         .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
         .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
+        .primitiveRestartEnable = VK_TRUE,
 };
 
+// Rive does not use primitive restart, but enables it anyway for triangle
+// strips and lists (where supported) for two reasons:
+//  1) Consistency. Metal and WebGL always enable it for strips and lists, and
+//     WebGPU for strips, so match their behavior.
+//  2) Performance on Adreno. Enabling it for lists bizarrely makes Adreno
+//     inkbleed over 2x faster.
+constexpr const VkPipelineInputAssemblyStateCreateInfo*
+inputAssemblyTriangleList(bool primitiveTopologyListRestart)
+{
+  return primitiveTopologyListRestart ? &InputAssemblyTriangleListRestart
+                                      : &InputAssemblyTriangleList;
+}
+
 constexpr VkPipelineViewportStateCreateInfo SINGLE_VIEWPORT = {
     .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
     .viewportCount = 1,
diff --git a/renderer/src/vulkan/draw_pipeline_vulkan.cpp b/renderer/src/vulkan/draw_pipeline_vulkan.cpp
index d780a6d..c7d03d7 100644
--- a/renderer/src/vulkan/draw_pipeline_vulkan.cpp
+++ b/renderer/src/vulkan/draw_pipeline_vulkan.cpp
@@ -591,7 +591,8 @@
     case DrawType::outerCurvePatches:
       pipelineCreateInfo.pVertexInputState = &layout::PATH_VERTEX_INPUT_STATE;
       pipelineCreateInfo
-          .pInputAssemblyState = &layout::INPUT_ASSEMBLY_TRIANGLE_LIST;
+          .pInputAssemblyState = layout::inputAssemblyTriangleList(
+          m_vk->features.primitiveTopologyListRestart);
       break;
 
     case DrawType::stencilOuterCubicBorrowedCoverage:
@@ -613,7 +614,8 @@
       // everything from the vertex index.
       pipelineCreateInfo.pVertexInputState = &layout::EMPTY_VERTEX_INPUT_STATE;
       pipelineCreateInfo
-          .pInputAssemblyState = &layout::INPUT_ASSEMBLY_TRIANGLE_LIST;
+          .pInputAssemblyState = layout::inputAssemblyTriangleList(
+          m_vk->features.primitiveTopologyListRestart);
       break;
 
     case DrawType::clipReset:
@@ -622,21 +624,24 @@
       pipelineCreateInfo
           .pVertexInputState = &layout::INTERIOR_TRI_VERTEX_INPUT_STATE;
       pipelineCreateInfo
-          .pInputAssemblyState = &layout::INPUT_ASSEMBLY_TRIANGLE_LIST;
+          .pInputAssemblyState = layout::inputAssemblyTriangleList(
+          m_vk->features.primitiveTopologyListRestart);
       break;
 
     case DrawType::imageRect:
       pipelineCreateInfo
           .pVertexInputState = &layout::IMAGE_RECT_VERTEX_INPUT_STATE;
       pipelineCreateInfo
-          .pInputAssemblyState = &layout::INPUT_ASSEMBLY_TRIANGLE_LIST;
+          .pInputAssemblyState = layout::inputAssemblyTriangleList(
+          m_vk->features.primitiveTopologyListRestart);
       break;
 
     case DrawType::imageMesh:
       pipelineCreateInfo
           .pVertexInputState = &layout::IMAGE_MESH_VERTEX_INPUT_STATE;
       pipelineCreateInfo
-          .pInputAssemblyState = &layout::INPUT_ASSEMBLY_TRIANGLE_LIST;
+          .pInputAssemblyState = layout::inputAssemblyTriangleList(
+          m_vk->features.primitiveTopologyListRestart);
       break;
 
     case DrawType::renderPassResolve:
diff --git a/renderer/src/vulkan/render_context_vulkan_impl.cpp b/renderer/src/vulkan/render_context_vulkan_impl.cpp
index 648f0d1..d430983 100644
--- a/renderer/src/vulkan/render_context_vulkan_impl.cpp
+++ b/renderer/src/vulkan/render_context_vulkan_impl.cpp
@@ -926,7 +926,8 @@
         .stageCount = 2,
         .pStages = stages,
         .pVertexInputState = &pipelineVertexInputStateCreateInfo,
-        .pInputAssemblyState = &layout::INPUT_ASSEMBLY_TRIANGLE_LIST,
+        .pInputAssemblyState = layout::inputAssemblyTriangleList(
+            m_vk->features.primitiveTopologyListRestart),
         .pViewportState = &layout::SINGLE_VIEWPORT,
         .pRasterizationState = &layout::RASTER_STATE_CULL_BACK_CCW,
         .pMultisampleState = &layout::MSAA_DISABLED,
@@ -1095,7 +1096,8 @@
         .stageCount = std::size(stages),
         .pStages = stages,
         .pVertexInputState = &layout::PATH_VERTEX_INPUT_STATE,
-        .pInputAssemblyState = &layout::INPUT_ASSEMBLY_TRIANGLE_LIST,
+        .pInputAssemblyState = layout::inputAssemblyTriangleList(
+            m_vk->features.primitiveTopologyListRestart),
         .pViewportState = &layout::SINGLE_VIEWPORT,
         .pRasterizationState = &layout::RASTER_STATE_CULL_NONE_CW,
         .pMultisampleState = &layout::MSAA_DISABLED,