fix(Vulkan): Skip warmup pipelines without matching subpasses (#15295) Fix invalid Vulkan shader warmup permutations that can crash inside vkCreateGraphicsPipelines when precompilation is enabled. Import the shared renderer fix from rive-app/rive-runtime#131 and add core regression coverage. --------- Co-authored-by: Charlie Lopez <charliesbot@google.com> Rive-Mono-Commit: 0e94da8717ed4de35fd66d8fd696134bb63216e9
diff --git a/renderer/src/vulkan/draw_shader_vulkan.cpp b/renderer/src/vulkan/draw_shader_vulkan.cpp index 5b8c862..fa3b8fa 100644 --- a/renderer/src/vulkan/draw_shader_vulkan.cpp +++ b/renderer/src/vulkan/draw_shader_vulkan.cpp
@@ -29,12 +29,16 @@ // fixedFunctionColorOutput does no dstRead, by definition. assert(!msaaDstRead || !fixedFunctionColorOutput); + // Exclude clipReset from the color-output assertion because its shader + // only updates stencil and requires neither fixed-function color output + // nor advanced blending. if (type == Type::fragment && interlockMode == InterlockMode::depthStencil && drawType != DrawType::renderPassInitialize && - drawType != DrawType::renderPassResolve) + drawType != DrawType::renderPassResolve && + drawType != DrawType::clipReset) { // Fixed function color output and advanced blend are mutually exclusive - // and one of them should always be set in depthStencil mode. + // and one must be set for ordinary depthStencil color draws. assert( fixedFunctionColorOutput != bool(shaderFeatures & ShaderFeatures::ENABLE_ADVANCED_BLEND));
diff --git a/renderer/src/vulkan/pipeline_manager_vulkan.cpp b/renderer/src/vulkan/pipeline_manager_vulkan.cpp index 035228c..2822322 100644 --- a/renderer/src/vulkan/pipeline_manager_vulkan.cpp +++ b/renderer/src/vulkan/pipeline_manager_vulkan.cpp
@@ -474,6 +474,17 @@ [&](DrawType drawType, ShaderFeatures shaderFeatures, ShaderMiscFlags shaderMiscFlags) { + if (interlockMode == InterlockMode::depthStencil && + drawType == DrawType::renderPassResolve && + enums::is_flag_set( + shaderMiscFlags, + ShaderMiscFlags::fixedFunctionColorOutput)) + { + // Manual resolve reads the framebuffer. Its shader cannot use the + // write-only color path, even when the render pass omits that flag. + return true; + } + PipelineProps props{ .drawType = drawType, .shaderFeatures = shaderFeatures, @@ -579,6 +590,17 @@ continue; } + if (interlockMode == InterlockMode::depthStencil && + (drawType == DrawType::renderPassInitialize || + drawType == DrawType::renderPassResolve) && + !enums::is_flag_set(drawContents, DrawContents::opaquePaint)) + { + // LogicalFlush marks color seed and manual resolve draws opaque + // to disable blending. They copy color, including alpha, rather + // than composite it with the destination. + continue; + } + props.drawContents = drawContents; for (auto variableRenderPassOptions : math::iterate_bit_combinations_in_mask(validPassOptions)) @@ -598,6 +620,31 @@ continue; } + // Only atomic and manually resolved render passes have a resolve + // subpass. Skip resolve draws for other passes because they have no + // matching subpass. + if (drawType == DrawType::renderPassResolve && + interlockMode != InterlockMode::atomics && + !enums::is_flag_set( + props.renderPassOptions, + RenderPassOptionsVulkan::manuallyResolved)) + { + continue; + } + + // In depthStencil mode, only MSAA passes that preserve the render + // target have a color-seeding subpass. Skip initialization draws + // for other passes because they have no matching subpass. + if (drawType == DrawType::renderPassInitialize && + interlockMode == InterlockMode::depthStencil && + (!enums::is_flag_set( + props.renderPassOptions, + RenderPassOptionsVulkan::msaa) || + colorLoadAction != LoadAction::preserveRenderTarget)) + { + continue; + } + if (interlockMode == InterlockMode::depthStencil) { if (enums::is_flag_set(
diff --git a/renderer/src/vulkan/pipeline_manager_vulkan.hpp b/renderer/src/vulkan/pipeline_manager_vulkan.hpp index 9795e82..5bd6071 100644 --- a/renderer/src/vulkan/pipeline_manager_vulkan.hpp +++ b/renderer/src/vulkan/pipeline_manager_vulkan.hpp
@@ -74,13 +74,15 @@ return PLSBackingType::inputAttachment; } - void forEachUbershaderPermutation( - InterlockMode, + // Enumerate the ubershader pipeline permutations for the given render + // properties and call func for each one. Stop if func returns false. + static void forEachUbershaderPermutation( + InterlockMode interlockMode, VkFormat renderTargetFormat, VkImageUsageFlags renderTargetUsage, - LoadAction, + LoadAction colorLoadAction, DrawPipelineVulkan::Options drawPipelineOptions, - const PlatformFeatures&, + const PlatformFeatures& platformFeatures, const std::function<bool(const PipelineProps&)>& func); #if !defined(NDEBUG)
diff --git a/tests/unit_tests/renderer/gpu_namespace_test.cpp b/tests/unit_tests/renderer/gpu_namespace_test.cpp index ad6c81d..b97538e 100644 --- a/tests/unit_tests/renderer/gpu_namespace_test.cpp +++ b/tests/unit_tests/renderer/gpu_namespace_test.cpp
@@ -9,6 +9,9 @@ #include "rive/renderer/rive_render_paint.hpp" #include "rive_render_path.hpp" #include "shaders/constants.glsl" +#ifdef RIVE_VULKAN +#include "vulkan/pipeline_manager_vulkan.hpp" +#endif #include <catch.hpp> #include <cstring> #include <map> @@ -214,6 +217,149 @@ } } +#ifdef RIVE_VULKAN +// Exercise the backend's enumeration, including render-pass options. The +// generic shader enumerator cannot tell whether a Vulkan subpass exists. +TEST_CASE( + "Vulkan warmup pipeline states and resolve subpasses", + "[gpu][vulkan_warmup]") +{ + using namespace gpu; + PlatformFeatures features; + features.msaaColorPreserveNeedsDraw = true; + features.supportsPipelineDynamicState = true; + for (auto mode : + {InterlockMode::rasterOrdering, + InterlockMode::atomics, + InterlockMode::depthStencil}) + { + for (auto load : + {LoadAction::clear, + LoadAction::preserveRenderTarget, + LoadAction::dontCare}) + { + for (VkImageUsageFlags usage : + {VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT), + VkImageUsageFlags( + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)}) + { + CAPTURE(mode, load, usage); + std::set<bool> resolveSampleCounts; + size_t ordinaryDraws = 0; + PipelineManagerVulkan::forEachUbershaderPermutation( + mode, + VK_FORMAT_R8G8B8A8_UNORM, + usage, + load, + DrawPipelineVulkan::Options::none, + features, + [&](const DrawPipelineVulkan::PipelineProps& props) { + CAPTURE( + props.drawType, + props.drawContents, + props.shaderMiscFlags, + props.renderPassOptions); + // Exercise the same state construction as DrawPipelineVulkan, + // including its invariants, without needing a Vulkan device. + const auto state = get_pipeline_state( + props.drawType, + props.interlockMode, + props.shaderMiscFlags, + props.drawContents, + enums::is_flag_set( + props.renderPassOptions, + RenderPassOptionsVulkan::fixedFunctionColorOutput), + props.blendMode, + features); + if (mode == InterlockMode::depthStencil && + (props.drawType == DrawType::renderPassInitialize || + props.drawType == DrawType::renderPassResolve)) + { + // These fullscreen copies replace color, including alpha. + // Blending would incorrectly combine it with the destination. + CHECK(enums::is_flag_set( + props.drawContents, + DrawContents::opaquePaint)); + CHECK(state.blendEquation == BlendEquation::none); + } + if (props.drawType == DrawType::renderPassResolve) + { + // RenderPassVulkan appends a resolve subpass only for these + // cases. Fixed-function MSAA resolve uses the main subpass. + CHECK( + (mode == InterlockMode::atomics || + enums::is_flag_set( + props.renderPassOptions, + RenderPassOptionsVulkan::manuallyResolved))); + resolveSampleCounts.insert(enums::is_flag_set( + props.renderPassOptions, + RenderPassOptionsVulkan::msaa)); + } + else if (props.drawType != DrawType::renderPassInitialize) + { + ++ordinaryDraws; + } + return true; + }); + CHECK(ordinaryDraws > 0); + // Prevent a blanket resolve skip from satisfying the negative check. + CHECK(resolveSampleCounts.count(false) == 1); + CHECK( + resolveSampleCounts.count(true) == + (mode == InterlockMode::depthStencil ? 1 : 0)); + } + } + } +} + +TEST_CASE("Vulkan warmup color seed subpasses", "[gpu][vulkan_warmup]") +{ + using namespace gpu; + PlatformFeatures features; + features.msaaColorPreserveNeedsDraw = true; + features.supportsPipelineDynamicState = true; + for (auto load : + {LoadAction::clear, + LoadAction::preserveRenderTarget, + LoadAction::dontCare}) + { + CAPTURE(load); + std::set<bool> seedSources; + PipelineManagerVulkan::forEachUbershaderPermutation( + InterlockMode::depthStencil, + VK_FORMAT_R8G8B8A8_UNORM, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT, + load, + DrawPipelineVulkan::Options::none, + features, + [&](const DrawPipelineVulkan::PipelineProps& props) { + if (props.drawType == DrawType::renderPassInitialize) + { + CHECK(load == LoadAction::preserveRenderTarget); + CHECK(enums::is_flag_set( + props.renderPassOptions, + RenderPassOptionsVulkan::msaa)); + seedSources.insert(enums::is_flag_set( + props.renderPassOptions, + RenderPassOptionsVulkan::msaaSeedFromOffscreenTexture)); + } + return true; + }); + if (load == LoadAction::preserveRenderTarget) + { + // Both the destination image and an offscreen image can seed MSAA. + CHECK(seedSources == std::set<bool>{false, true}); + } + else + { + CHECK(seedSources.empty()); + } + } +} +#endif + // ShaderUniqueKey() packs shaderMiscFlags densely, using only the bits that are // relevant to the interlockMode. Draw types that share a drawTypeKey therefore // have to agree on that layout -- otherwise the same packed value means
diff --git a/tests/unit_tests/renderer/rendering_tests.cpp b/tests/unit_tests/renderer/rendering_tests.cpp index 7e71d3b..f1f1f5f 100644 --- a/tests/unit_tests/renderer/rendering_tests.cpp +++ b/tests/unit_tests/renderer/rendering_tests.cpp
@@ -6,7 +6,13 @@ #include "rive/renderer.hpp" #include "rive/factory.hpp" +#ifdef RIVE_VULKAN +#include "rive/renderer/render_context.hpp" +#include "rive/renderer/vulkan/render_context_vulkan_impl.hpp" +#endif + #include <catch.hpp> +#include <set> using namespace rive; using namespace rive::gpu; @@ -104,6 +110,48 @@ #endif }; +#if defined(RIVE_VULKAN) && !defined(RIVE_NO_VK_BOOTSTRAP) +// Exercise background warmup on a real Vulkan device using its default +// rendering mode and depthStencil mode. +// "Vulkan warmup shader stages" separately tests all compiled-in modes +// with a fake driver, without validating real GPU pipeline creation. +TEST_CASE("Vulkan background pipeline warmup", "[rendering][vulkan_warmup]") +{ + auto window = testingWindowFactories[0].function(); + REQUIRE(window != nullptr); + auto* context = window->renderContext(); + REQUIRE(context != nullptr); + auto* impl = context->static_impl_cast<RenderContextVulkanImpl>(); + const auto defaultMode = context->selectInterlockMode({}); + const std::set<InterlockMode> modes{ + defaultMode, + InterlockMode::depthStencil, + }; + for (auto mode : modes) + { + for (auto load : {LoadAction::clear, LoadAction::preserveRenderTarget}) + { + for (VkImageUsageFlags usage : + {VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT), + VkImageUsageFlags( + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)}) + { + CAPTURE(mode, load, usage); + impl->startAsyncPipelineCreation( + mode, + VK_FORMAT_R8G8B8A8_UNORM, + usage, + load); + // Drain the compile queue so failures occur within this test, before + // the render context and its device are destroyed. + impl->waitForAsyncPipelineCreation(); + } + } + } +} +#endif + // Ensure that rendering still succeeds when compilations fail (e.g., by falling // back on an uber shader or at least not crashing). Valid compilations may fail // in the real world if the device is pressed for resources or in a bad state.
diff --git a/tests/unit_tests/renderer/vulkan_allocation_failure_test.cpp b/tests/unit_tests/renderer/vulkan_allocation_failure_test.cpp index 62911ae..79e966e 100644 --- a/tests/unit_tests/renderer/vulkan_allocation_failure_test.cpp +++ b/tests/unit_tests/renderer/vulkan_allocation_failure_test.cpp
@@ -11,6 +11,8 @@ #ifdef RIVE_VULKAN #include "rive/renderer/vulkan/vulkan_context.hpp" +#include "vulkan/draw_shader_vulkan.hpp" +#include "vulkan/pipeline_manager_vulkan.hpp" #include <catch.hpp> #include <string.h> @@ -24,6 +26,7 @@ { VkResult createRenderPass = VK_SUCCESS; VkResult createSampler = VK_SUCCESS; + VkResult createShaderModule = VK_SUCCESS; VkResult createBuffer = VK_SUCCESS; VkResult mapMemory = VK_SUCCESS; } g_failures; @@ -71,6 +74,11 @@ createRenderPass) DEFINE_FAKE_CREATE(CreateSampler, VkSamplerCreateInfo, VkSampler, createSampler) DEFINE_FAKE_CREATE(CreateBuffer, VkBufferCreateInfo, VkBuffer, createBuffer) +DEFINE_FAKE_CREATE( + CreateShaderModule, + VkShaderModuleCreateInfo, + VkShaderModule, + createShaderModule) #undef DEFINE_FAKE_CREATE VKAPI_ATTR void VKAPI_CALL fake_vkGetPhysicalDeviceProperties( @@ -229,6 +237,8 @@ ENTRY("vkCreateRenderPass", fake_vkCreateRenderPass) ENTRY("vkCreateSampler", fake_vkCreateSampler) + ENTRY("vkCreateShaderModule", fake_vkCreateShaderModule) + ENTRY("vkDestroyShaderModule", fakeIgnore) ENTRY("vkCreateBuffer", fake_vkCreateBuffer) ENTRY("vkCreateImage", fake_vkCreateImage) ENTRY("vkGetPhysicalDeviceProperties", fake_vkGetPhysicalDeviceProperties) @@ -357,6 +367,122 @@ } } // namespace +// Use the stand-in driver to exercise the real shader constructor without a +// GPU. clipReset only updates stencil, so warmup can omit both color modes. +TEST_CASE("Vulkan colorless clip reset shaders", "[vulkan][vulkan_warmup]") +{ + FakeDriverReset resetDriver; + FakeContext vk; + for (auto flags : + {ShaderMiscFlags::none, + ShaderMiscFlags::msaaDstRead, + ShaderMiscFlags::fixedFunctionColorOutput}) + { + for (auto type : + {DrawShaderVulkan::Type::vertex, DrawShaderVulkan::Type::fragment}) + { + CAPTURE(flags, type); + DrawShaderVulkan shader( + type, + vk, + DrawType::clipReset, + ShaderFeatures::ENABLE_DITHER, + InterlockMode::depthStencil, + flags); + CHECK(shader.module() == liveHandle<VkShaderModule>()); + } + } +} + +// Run real shader selection and pipeline-state construction against the fake +// driver. A live-device warmup aborts at its first assertion, hiding later +// invalid permutations, and cannot cover modes unavailable on that device. +TEST_CASE("Vulkan warmup shader stages", "[vulkan][vulkan_warmup]") +{ + FakeDriverReset resetDriver; + FakeContext vk; + for (bool clipPlanes : {false, true}) + { + for (bool dynamicState : {false, true}) + { + PlatformFeatures features; + features.msaaColorPreserveNeedsDraw = true; + features.clockwiseAtomicBorrowedCoverageBarrierNeedsRenderPassInit = true; + features.supportsClipPlanes = clipPlanes; + features.supportsPipelineDynamicState = dynamicState; + for (auto mode : + {InterlockMode::rasterOrdering, +#ifndef RIVE_ANDROID + InterlockMode::clockwise, +#endif +#ifdef WITH_VULKAN_ATOMICS + InterlockMode::atomics, + InterlockMode::clockwiseAtomic, +#endif + InterlockMode::depthStencil}) + { + for (auto load : + {LoadAction::clear, + LoadAction::preserveRenderTarget, + LoadAction::dontCare}) + { + for (VkImageUsageFlags usage : + {VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT), + VkImageUsageFlags( + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)}) + { + CAPTURE(clipPlanes, dynamicState, mode, load, usage); + size_t permutationCount = 0; + PipelineManagerVulkan::forEachUbershaderPermutation( + mode, + VK_FORMAT_R8G8B8A8_UNORM, + usage, + load, + DrawPipelineVulkan::Options::none, + features, + [&](const DrawPipelineVulkan::PipelineProps& props) { + CAPTURE( + props.drawType, + props.drawContents, + props.shaderFeatures, + props.shaderMiscFlags, + props.renderPassOptions); + (void)get_pipeline_state( + props.drawType, + props.interlockMode, + props.shaderMiscFlags, + props.drawContents, + enums::is_flag_set( + props.renderPassOptions, + RenderPassOptionsVulkan::fixedFunctionColorOutput), + props.blendMode, + features); + for (auto type : + {DrawShaderVulkan::Type::vertex, + DrawShaderVulkan::Type::fragment}) + { + CAPTURE(type); + DrawShaderVulkan shader( + type, + vk, + props.drawType, + props.shaderFeatures, + props.interlockMode, + props.shaderMiscFlags); + CHECK(shader.module() == liveHandle<VkShaderModule>()); + } + ++permutationCount; + return true; + }); + CHECK(permutationCount > 0); + } + } + } + } + } +} + TEST_CASE( "VK_CREATE_HANDLE publishes the driver's handle on success", "[vulkan]")