diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Pipeline.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Pipeline.hpp index 15be976c7c8b..651c824bc8a5 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Pipeline.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Pipeline.hpp @@ -111,6 +111,7 @@ class Pipeline { bool narrowLibraryKeys = false; bool dynamicDepthBiasEnable = 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 1060dc7a0954..d3d172a6af87 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp @@ -720,7 +720,7 @@ bool shadesPerSample(const ShaderRecompiler::RecompileResult& program) { bool libraryKeys(PipelineLibraryKeys& keys, const Context& context, const State& state, const VertexInputLayout& input, const PipelineSetLayouts& resources, std::span shaders, bool narrow) { 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) || AvoidsInterlockEpilog(context, 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) || AvoidsInterlockEpilog(context, shaders)) return false; keys = {}; for (const auto& shader : shaders) { const auto identity = shader.program->Identity(); @@ -844,7 +844,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 && !AvoidsInterlockEpilog(context, 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 && !AvoidsInterlockEpilog(context, 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); @@ -1023,7 +1023,7 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput narrowLibraryKeys = narrowLibraryKeysNow(); libraries = libraryKeys(keys, context, state, vertexInput, resources, shaders, narrowLibraryKeys); 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 (state.stencilReplayReference && state.topology == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP && state.cullMode != VK_CULL_MODE_NONE && context.graphicsPipelineLibrary) dynamicStates.push_back(VK_DYNAMIC_STATE_FRONT_FACE_EXT); if (depthBounds) dynamicStates.push_back(VK_DYNAMIC_STATE_DEPTH_BOUNDS); if (depthBias) dynamicStates.push_back(VK_DYNAMIC_STATE_DEPTH_BIAS); @@ -1117,6 +1117,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}; @@ -1134,6 +1135,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) { @@ -1356,7 +1364,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 595995318a2e..94765b4c1b3a 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -6287,6 +6287,13 @@ struct MockVulkan { std::vector fragmentLibraryMultisample; std::vector preRasterizationBiasEnableDynamic; 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; @@ -6541,6 +6548,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(); @@ -10153,6 +10178,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 pipelineLibraryNarrowKeyTests() { using namespace AgcDriver::Graphics; for (const bool wide : {false, true}) { @@ -16190,6 +16274,7 @@ int main(int argc, char** argv) { pipelineLibrarySetTests(); pipelineLibraryNarrowKeyTests(); pipelineLibraryDynamicDepthBiasTests(); + 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 eaf8e6f86462..e49dfe18e332 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DepthFastClearHarness.hpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DepthFastClearHarness.hpp @@ -54,6 +54,7 @@ struct DrawOptions { float maxDepth = 1.0f; bool depthBias = false; float depthBiasConstant = 0.0f; + bool rasterizerDiscard = false; }; inline std::array BufferDescriptor(const void* data, std::uint32_t stride, std::uint32_t count) { @@ -133,6 +134,7 @@ inline std::uint32_t Draw(AgcDriver::VulkanDevice& device, const Shaders& compil state.stencilBack = options.stencil; state.depthBias = options.depthBias; state.depthBiasConstant = options.depthBiasConstant; + 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; + } +}