From 4867f6e79a0244df6c889606ef5ba423a66af491 Mon Sep 17 00:00:00 2001 From: Jamie <55252868+chitters2004@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:29:14 +0000 Subject: [PATCH] fix(agc): build static rasterizer discard pipelines without fragment state A draw that discards rasterization (SPI_SHADER_POS_FORMAT entry 0 of zero, or the tessellation capture pass of a tessellation plus geometry draw) baked rasterizerDiscardEnable into a pipeline that still carried the draw's fragment shader, colour and depth attachments and fragment dynamic states, and into pipeline libraries whose pre-rasterization part cannot take null set layouts under a static discard. Vulkan forbids a fragment stage when the pipeline has no fragment shader state (VUID-VkGraphicsPipelineCreateInfo-pStages-06894), the begun rendering had no attachments while the pipeline declared them (VUID-vkCmdDraw-colorAttachmentCount-06179), and the draw set stencil reference, depth bounds and colour write masks the pipeline did not make dynamic (VUID-vkCmdDraw-None-08608). A pipeline that bakes rasterizer discard is now created whole, without a fragment stage, rendering attachments or those dynamic states, and its draws no longer set them. It is no longer built from pipeline libraries. --- .../Graphics/include/Pipeline.hpp | 1 + .../libSceAgcDriver/Graphics/src/Pipeline.cpp | 16 +++- .../prx/libSceAgcDriver/tests/Graphics.cpp | 85 +++++++++++++++++++ .../agc_driver_rasterizer_discard_tests.cmake | 10 +++ .../tests/execution/DepthFastClearHarness.hpp | 2 + .../tests/execution/RasterizerDiscard.cpp | 46 ++++++++++ 6 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_rasterizer_discard_tests.cmake create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/RasterizerDiscard.cpp diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Pipeline.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Pipeline.hpp index 805108c6e774..fe0f03d90af6 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Pipeline.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Pipeline.hpp @@ -96,6 +96,7 @@ class Pipeline { bool depthBias = false; bool libraries = false; bool dynamicColorWriteMask = false; + bool staticDiscard = false; std::uint32_t dynamicSlots = 0; std::shared_ptr optimized; bool dynamicRendering = false; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp index 18c7928df16e..16019515ba69 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp @@ -395,7 +395,7 @@ void appendKey(std::vector& key, const TValue& value) { bool libraryKeys(PipelineLibraryKeys& keys, const Context& context, const State& state, const VertexInputLayout& input, const PipelineSetLayouts& resources, std::span shaders) { using Stage = ShaderRecompiler::ShaderStage; - if (!context.graphicsPipelineLibrary || context.pipelineExecutableInfo || (state.stages.tessellation && !context.tessellationPipelineCache) || state.depthBiasPerFace || state.rectList || BarycentricPatches(state, shaders) || SplitsAlternateAlpha(state) || std::ranges::any_of(shaders, [](const CompiledShader& shader) { return shader.program != nullptr && shader.program->functionLdsBuffer; }) || UsesMeshWindow(shaders) || FragmentInterlock(shaders)) return false; + if (!context.graphicsPipelineLibrary || state.rasterizerDiscard || context.pipelineExecutableInfo || (state.stages.tessellation && !context.tessellationPipelineCache) || state.depthBiasPerFace || state.rectList || BarycentricPatches(state, shaders) || SplitsAlternateAlpha(state) || std::ranges::any_of(shaders, [](const CompiledShader& shader) { return shader.program != nullptr && shader.program->functionLdsBuffer; }) || UsesMeshWindow(shaders) || FragmentInterlock(shaders)) return false; keys = {}; for (const auto& shader : shaders) { const auto identity = shader.program->Identity(); @@ -516,7 +516,7 @@ bool libraryKeys(PipelineLibraryKeys& keys, const Context& context, const State& Pipeline::Pipeline(const Context& context, const State& state, const VertexInputLayout& vertexInput, const ShaderResources& resources, std::span shaders, VkImageLayout attachmentLayout) : Pipeline(context, state, vertexInput, resources.SetLayouts(), shaders, attachmentLayout) {} -Pipeline::Pipeline(const Context& context, const State& state, const VertexInputLayout& vertexInput, const PipelineSetLayouts& resources, std::span shaders, VkImageLayout attachmentLayout) : context(context), _modules(shaders.size()), attachments(state.colors.size() + (state.depth ? 1u : 0u)), colorAttachments(state.colors.size()), depthBounds(state.depth.has_value() && state.depthBoundsTest), depthBias(state.depth.has_value() && state.depthBias), dynamicColorWriteMask(context.dynamicColorWriteMask && !FragmentInterlock(shaders)), dynamicRendering(context.graphicsPipelineLibrary) { +Pipeline::Pipeline(const Context& context, const State& state, const VertexInputLayout& vertexInput, const PipelineSetLayouts& resources, std::span shaders, VkImageLayout attachmentLayout) : context(context), _modules(shaders.size()), attachments(state.colors.size() + (state.depth ? 1u : 0u)), colorAttachments(state.colors.size()), depthBounds(state.depth.has_value() && state.depthBoundsTest && !state.rasterizerDiscard), depthBias(state.depth.has_value() && state.depthBias), dynamicColorWriteMask(context.dynamicColorWriteMask && !FragmentInterlock(shaders) && !state.rasterizerDiscard), staticDiscard(state.rasterizerDiscard), dynamicRendering(context.graphicsPipelineLibrary) { PerformanceTimer timing("Vulkan.GraphicsPipeline"); for (const auto& color : state.colors) boundAttachments.push_back(color.partialWriteMask == 0u); if (state.depth) boundAttachments.push_back(true); @@ -692,7 +692,7 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput PipelineLibraryKeys keys; libraries = libraryKeys(keys, context, state, vertexInput, resources, shaders); std::vector dynamicStates{VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR}; - if (state.stencilReplayReference) dynamicStates.push_back(VK_DYNAMIC_STATE_STENCIL_REFERENCE); + if (state.stencilReplayReference && !staticDiscard) dynamicStates.push_back(VK_DYNAMIC_STATE_STENCIL_REFERENCE); if (depthBounds) dynamicStates.push_back(VK_DYNAMIC_STATE_DEPTH_BOUNDS); if (depthBias) dynamicStates.push_back(VK_DYNAMIC_STATE_DEPTH_BIAS); if (libraries) dynamicStates.assign(PipelineLibraryDynamicStates().begin(), PipelineLibraryDynamicStates().end()); @@ -783,6 +783,7 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput std::copy(state.blendConstants.begin(), state.blendConstants.end(), blend.blendConstants); VkGraphicsPipelineCreateInfo pipelineInfo{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO}; pipelineInfo.flags = context.pipelineExecutableInfo ? VK_PIPELINE_CREATE_CAPTURE_STATISTICS_BIT_KHR : 0; + if (staticDiscard) std::erase_if(stages, [](const VkPipelineShaderStageCreateInfo& stage) { return stage.stage == VK_SHADER_STAGE_FRAGMENT_BIT; }); pipelineInfo.stageCount = static_cast(stages.size()); pipelineInfo.pStages = stages.data(); VkPipelineTessellationStateCreateInfo tessellation{VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO}; @@ -800,6 +801,13 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput pipelineInfo.pDynamicState = &dynamic; pipelineInfo.layout = layout; pipelineInfo.renderPass = renderPass; + if (staticDiscard) { + rendering.pNext = nullptr; + rendering.colorAttachmentCount = 0; + rendering.pColorAttachmentFormats = nullptr; + rendering.depthAttachmentFormat = VK_FORMAT_UNDEFINED; + rendering.stencilAttachmentFormat = VK_FORMAT_UNDEFINED; + } if (dynamicRendering) pipelineInfo.pNext = &rendering; timing.Mark("modules_and_state"); if (prebuilding && !libraries) { @@ -1018,7 +1026,7 @@ void Pipeline::Continue(CommandState& recording, const State& state, const State recording.BindPipeline(context, best != VK_NULL_HANDLE ? best : pipeline, dynamicSlots); setViewports(recording, state); setColorWriteMasks(recording, state, masked, false); - if (state.stencilReplayReference) recording.SetStencilReference(context, VK_STENCIL_FACE_FRONT_AND_BACK, *state.stencilReplayReference); + if (state.stencilReplayReference && !staticDiscard) recording.SetStencilReference(context, VK_STENCIL_FACE_FRONT_AND_BACK, *state.stencilReplayReference); if (libraries) { setLibraryState(recording, state); return; diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index 6f5634bdd241..a37b207abc4c 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -6128,6 +6128,13 @@ struct MockVulkan { std::map> pipelineLayoutPushRanges; std::map libraryLayouts; std::uint32_t pipelineCreateCount = 0; + struct DiscardPipeline { + std::size_t fragmentStages = 0; + std::uint32_t colorAttachments = ~0u; + VkFormat depthFormat = VK_FORMAT_MAX_ENUM; + std::vector dynamicStates; + }; + std::vector discardPipelines; std::vector> pipelineSpecializations; VkPipeline boundPipeline = VK_NULL_HANDLE; std::vector lastPushConstants; @@ -6382,6 +6389,24 @@ VKAPI_ATTR VkResult VKAPI_CALL mockCreateGraphicsPipelines(VkDevice, VkPipelineC } Require(count == 1, "mock expects exactly one graphics pipeline per call"); if ((infos[0].flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) { + MockVulkan::DiscardPipeline discarding; + bool fromLibraries = false; + for (auto* next = static_cast(infos[0].pNext); next != nullptr; next = next->pNext) { + if (next->sType == VK_STRUCTURE_TYPE_PIPELINE_LIBRARY_CREATE_INFO_KHR) fromLibraries = true; + if (next->sType != VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR) continue; + const auto* rendering = reinterpret_cast(next); + discarding.colorAttachments = rendering->colorAttachmentCount; + discarding.depthFormat = rendering->depthAttachmentFormat; + } + if (!fromLibraries && infos[0].pRasterizationState != nullptr && infos[0].pRasterizationState->rasterizerDiscardEnable == VK_TRUE) { + discarding.fragmentStages = static_cast(std::ranges::count_if(std::span(infos[0].pStages, infos[0].stageCount), [](const VkPipelineShaderStageCreateInfo& stage) { return stage.stage == VK_SHADER_STAGE_FRAGMENT_BIT; })); + if (infos[0].pDynamicState != nullptr) discarding.dynamicStates.assign(infos[0].pDynamicState->pDynamicStates, infos[0].pDynamicState->pDynamicStates + infos[0].pDynamicState->dynamicStateCount); + mock.discardPipelines.push_back(discarding); + *pipelines = makeHandle(); + ++mock.pipelineCreateCount; + ++mock.live; + return VK_SUCCESS; + } const auto* linked = static_cast(infos[0].pNext); Require(linked != nullptr && linked->sType == VK_STRUCTURE_TYPE_PIPELINE_LIBRARY_CREATE_INFO_KHR && linked->libraryCount != 0, "mock expects a graphics pipeline library or a pipeline linked from libraries"); *pipelines = makeHandle(); @@ -9951,6 +9976,65 @@ void pipelineLibrarySetTests() { } } +void staticDiscardPipelineTests() { + using namespace AgcDriver::Graphics; + mock = MockVulkan{}; + auto context = mockContext(); + context.graphicsPipelineLibrary = true; + context.pipelineLinkTimeOptimization = false; + context.dynamicColorWriteMask = true; + context.depthBounds = true; + const auto functions = CommandStateLog::stateFunctions(); + context.functions = &functions; + context.limits.maxColorAttachments = 1; + context.limits.framebufferNoAttachmentsSampleCounts = VK_SAMPLE_COUNT_1_BIT; + context.limits.framebufferDepthSampleCounts = VK_SAMPLE_COUNT_1_BIT; + context.limits.maxFramebufferWidth = 64; + context.limits.maxFramebufferHeight = 64; + context.limits.maxFramebufferLayers = 1; + ShaderRecompiler::RecompileResult vertex, fragment; + vertex.spirv = makeModule({}); + fragment.spirv = makeModule({.fragment = true}); + vertex.variantId = 341; + fragment.variantId = 342; + const std::array shaders{{{ShaderRecompiler::ShaderStage::Vertex, &vertex, 0}, {ShaderRecompiler::ShaderStage::Fragment, &fragment, PipelinePushSlotBytes}}}; + const PipelineSetLayouts resources{makeHandle(), VK_NULL_HANDLE, false, {}, {}}; + State state{}; + state.viewport = {0.0f, 0.0f, 64.0f, 64.0f, 0.0f, 1.0f}; + state.scissor = {{0, 0}, {64, 64}}; + state.color = {0, {64, 64}, VK_FORMAT_R8G8B8A8_UNORM, 64u * 64u * 4u, 0xe4u}; + state.colors = {state.color}; + state.hasColorTarget = true; + state.blend.colorWriteMask = 15; + state.blends = {state.blend}; + state.depth = DepthTarget{0, 0, {64, 64}, VK_FORMAT_D32_SFLOAT, 1.0f, 0}; + state.depthTest = true; + state.depthBoundsTest = true; + state.minDepthBounds = 0.25f; + state.maxDepthBounds = 0.75f; + state.stencilReplayReference = 7u; + state.rasterizerDiscard = true; + Require(!Pipeline::Prebuild(context, state, VertexInputLayout{}, resources, shaders), "a pipeline that bakes rasterizer discard was built from pipeline libraries"); + { + Pipeline pipeline(context, state, VertexInputLayout{}, resources, shaders, VK_IMAGE_LAYOUT_GENERAL); + Require(mock.discardPipelines.size() == 1, "a pipeline that bakes rasterizer discard was not created whole"); + const auto& created = mock.discardPipelines.front(); + Require(created.fragmentStages == 0, "a pipeline that bakes rasterizer discard kept its fragment shader stage"); + Require(created.colorAttachments == 0 && created.depthFormat == VK_FORMAT_UNDEFINED, "a pipeline that bakes rasterizer discard declared attachments its render pass does not begin with"); + Require(std::ranges::none_of(created.dynamicStates, [](VkDynamicState dynamicState) { return dynamicState == VK_DYNAMIC_STATE_STENCIL_REFERENCE || dynamicState == VK_DYNAMIC_STATE_DEPTH_BOUNDS || dynamicState == VK_DYNAMIC_STATE_BLEND_CONSTANTS || dynamicState == VK_DYNAMIC_STATE_COLOR_WRITE_MASK_EXT; }), "a pipeline that bakes rasterizer discard made fragment state dynamic"); + CommandState tracker; + tracker.Begin(makeHandle()); + CommandStateLog::stateLog.clear(); + pipeline.Continue(tracker, state); + for (const auto& line : CommandStateLog::stateLog) { + Require(line.rfind("colormask", 0) != 0 && line.rfind("bounds", 0) != 0 && line.rfind("reference", 0) != 0 && line.rfind("blendconstants", 0) != 0, "a draw with a pipeline that bakes rasterizer discard set fragment state: " + line); + } + CommandStateLog::stateLog.clear(); + } + ClearPipelineLibraries(context.device); + Require(mock.live == 0, "static discard pipeline tests leaked Vulkan objects"); +} + void pipelineLibraryClearWaitsForBuildTests() { using namespace AgcDriver::Graphics; mock = MockVulkan{}; @@ -15574,6 +15658,7 @@ int main(int argc, char** argv) { pipelineStoreLazyBufferPoolTests(); retainedPixelStageTests(); pipelineLibrarySetTests(); + staticDiscardPipelineTests(); pipelineLibraryClearWaitsForBuildTests(); tessellationPipelineLibraryCacheTests(); framebufferOwnerTests(); diff --git a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_rasterizer_discard_tests.cmake b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_rasterizer_discard_tests.cmake new file mode 100644 index 000000000000..435c4c661149 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_rasterizer_discard_tests.cmake @@ -0,0 +1,10 @@ +if(BUILD_TESTING) + add_test(NAME agc_driver_rasterizer_discard_render_pass COMMAND agc_driver_rasterizer_discard_tests) + set_tests_properties(agc_driver_rasterizer_discard_render_pass PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_GPL=1" SKIP_RETURN_CODE 77 TIMEOUT 60 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + add_test(NAME agc_driver_rasterizer_discard_libraries COMMAND agc_driver_rasterizer_discard_tests) + set_tests_properties(agc_driver_rasterizer_discard_libraries PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 TIMEOUT 60 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + if(WIN32) + set_tests_properties(agc_driver_rasterizer_discard_render_pass agc_driver_rasterizer_discard_libraries PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DepthFastClearHarness.hpp b/core/libs/prx/libSceAgcDriver/tests/execution/DepthFastClearHarness.hpp index e1d74d89e90e..61fc37a3dbb5 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DepthFastClearHarness.hpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DepthFastClearHarness.hpp @@ -52,6 +52,7 @@ struct DrawOptions { VkStencilOpState stencil{}; float minDepth = 0.0f; float maxDepth = 1.0f; + bool rasterizerDiscard = false; }; inline std::array BufferDescriptor(const void* data, std::uint32_t stride, std::uint32_t count) { @@ -129,6 +130,7 @@ inline std::uint32_t Draw(AgcDriver::VulkanDevice& device, const Shaders& compil state.stencilTest = options.stencilTest; state.stencilFront = options.stencil; state.stencilBack = options.stencil; + state.rasterizerDiscard = options.rasterizerDiscard; state.renderExtent = extent; state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; state.viewport = {0, static_cast(extent.height), static_cast(extent.width), -static_cast(extent.height), options.minDepth, options.maxDepth}; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/RasterizerDiscard.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/RasterizerDiscard.cpp new file mode 100644 index 000000000000..71221a38671b --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/RasterizerDiscard.cpp @@ -0,0 +1,46 @@ +#include "DepthFastClearHarness.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include + +namespace { + +using namespace AgcDriver::Graphics; +using DepthFastClearHarness::Covered; +using DepthFastClearHarness::Height; +using DepthFastClearHarness::Surface; +using DepthFastClearHarness::Width; + +alignas(4096) std::array Depth{}; +alignas(4096) std::array DepthWithStencil{}; +alignas(4096) std::array Stencil{}; + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + const auto compiled = DepthFastClearHarness::Compile(*device); + const std::array surfaces{{ + {reinterpret_cast(Depth.data()), 0, 0, false, VK_FORMAT_D32_SFLOAT}, + {reinterpret_cast(DepthWithStencil.data()), reinterpret_cast(Stencil.data()), 0, false, VK_FORMAT_D32_SFLOAT_S8_UINT}, + }}; + for (const auto& surface : surfaces) { + for (const bool discarded : {true, false, true, false}) { + const auto covered = DepthFastClearHarness::Draw(*device, compiled, surface, {.depthCompare = VK_COMPARE_OP_ALWAYS, .rasterizerDiscard = discarded}); + Require(covered == (discarded ? 0u : Covered), std::string("AGC graphics: a draw ") + (discarded ? "with" : "without") + " rasterizer discard covered " + std::to_string(covered) + " of " + std::to_string(Covered) + " pixels"); + } + const auto stenciled = DepthFastClearHarness::Draw(*device, compiled, surface, {.depthCompare = VK_COMPARE_OP_ALWAYS, .stencilTest = true, .stencil = {VK_STENCIL_OP_KEEP, VK_STENCIL_OP_REPLACE, VK_STENCIL_OP_KEEP, VK_COMPARE_OP_ALWAYS, 0xffu, 0xffu, 1u}, .rasterizerDiscard = true}); + Require(stenciled == 0, "AGC graphics: a discarded draw with a stencil test covered " + std::to_string(stenciled) + " pixels"); + } + std::puts("rasterizer discard tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +}