Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<OptimizedPipeline> optimized;
bool dynamicRendering = false;
Expand Down
16 changes: 12 additions & 4 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const CompiledShader> 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();
Expand Down Expand Up @@ -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<const CompiledShader> 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<const CompiledShader> 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<const CompiledShader> 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);
Expand Down Expand Up @@ -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<VkDynamicState> 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);
Expand Down Expand Up @@ -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<std::uint32_t>(stages.size());
pipelineInfo.pStages = stages.data();
VkPipelineTessellationStateCreateInfo tessellation{VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO};
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
85 changes: 85 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6287,6 +6287,13 @@ struct MockVulkan {
std::vector<bool> fragmentLibraryMultisample;
std::vector<bool> 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<VkDynamicState> dynamicStates;
};
std::vector<DiscardPipeline> discardPipelines;
std::vector<std::array<std::uint32_t, 3>> pipelineSpecializations;
VkPipeline boundPipeline = VK_NULL_HANDLE;
std::vector<std::byte> lastPushConstants;
Expand Down Expand Up @@ -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<const VkBaseInStructure*>(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<const VkPipelineRenderingCreateInfoKHR*>(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::size_t>(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<VkPipeline>();
++mock.pipelineCreateCount;
++mock.live;
return VK_SUCCESS;
}
const auto* linked = static_cast<const VkPipelineLibraryCreateInfoKHR*>(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<VkPipeline>();
Expand Down Expand Up @@ -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<CompiledShader, 2> shaders{{{ShaderRecompiler::ShaderStage::Vertex, &vertex, 0}, {ShaderRecompiler::ShaderStage::Fragment, &fragment, PipelinePushSlotBytes}}};
const PipelineSetLayouts resources{makeHandle<VkDescriptorSetLayout>(), 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<VkCommandBuffer>());
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}) {
Expand Down Expand Up @@ -16190,6 +16274,7 @@ int main(int argc, char** argv) {
pipelineLibrarySetTests();
pipelineLibraryNarrowKeyTests();
pipelineLibraryDynamicDepthBiasTests();
staticDiscardPipelineTests();
pipelineLibraryClearWaitsForBuildTests();
tessellationPipelineLibraryCacheTests();
framebufferOwnerTests();
Expand Down
Original file line number Diff line number Diff line change
@@ -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:$<TARGET_FILE_DIR:libc>")
endif()
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ struct DrawOptions {
float maxDepth = 1.0f;
bool depthBias = false;
float depthBiasConstant = 0.0f;
bool rasterizerDiscard = false;
};

inline std::array<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t stride, std::uint32_t count) {
Expand Down Expand Up @@ -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<float>(extent.height), static_cast<float>(extent.width), -static_cast<float>(extent.height), options.minDepth, options.maxDepth};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "DepthFastClearHarness.hpp"
#include "VulkanTestDevice.hpp"
#include <array>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <string>

namespace {

using namespace AgcDriver::Graphics;
using DepthFastClearHarness::Covered;
using DepthFastClearHarness::Height;
using DepthFastClearHarness::Surface;
using DepthFastClearHarness::Width;

alignas(4096) std::array<float, Width * Height> Depth{};
alignas(4096) std::array<float, Width * Height> DepthWithStencil{};
alignas(4096) std::array<std::uint8_t, Width * Height> Stencil{};

}

int main() {
try {
const auto device = OpenVulkanTestDevice();
if (!device) return VulkanTestSkipped;
const auto compiled = DepthFastClearHarness::Compile(*device);
const std::array<Surface, 2> surfaces{{
{reinterpret_cast<std::uintptr_t>(Depth.data()), 0, 0, false, VK_FORMAT_D32_SFLOAT},
{reinterpret_cast<std::uintptr_t>(DepthWithStencil.data()), reinterpret_cast<std::uintptr_t>(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;
}
}
Loading