Skip to content
Open
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
2 changes: 1 addition & 1 deletion core/libs/prx/libSceAgcDriver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ foreach(agcPresentationStage IN ITEMS Vert Frag)
)
list(APPEND agcPresentationShaderHeaders ${agcPresentationShaderHeader})
endforeach()
foreach(agcSampleOrderVariant IN ITEMS "R8;comp;-DTEXEL_FORMAT=r8ui" "R16;comp;-DTEXEL_FORMAT=r16ui" "R32;comp;-DTEXEL_FORMAT=r32ui" "RG32;comp;-DTEXEL_FORMAT=rg32ui" "RGBA32;comp;-DTEXEL_FORMAT=rgba32ui" "Vertex;vert;-DPLAIN=1" "Depth;frag;-DPLAIN=1" "StencilExport;frag;-DSTENCIL_EXPORT=1" "StencilBits;frag;-DSTENCIL_BITS=1")
foreach(agcSampleOrderVariant IN ITEMS "R8;comp;-DTEXEL_FORMAT=r8ui" "R16;comp;-DTEXEL_FORMAT=r16ui" "R32;comp;-DTEXEL_FORMAT=r32ui" "RG32;comp;-DTEXEL_FORMAT=rg32ui" "RGBA32;comp;-DTEXEL_FORMAT=rgba32ui" "Vertex;vert;-DPLAIN=1" "Depth;frag;-DPLAIN=1" "StencilExport;frag;-DSTENCIL_EXPORT=1" "StencilBits;frag;-DSTENCIL_BITS=1" "DepthSingle;frag;-DDEPTH_SINGLE=1" "StencilExportSingle;frag;-DSTENCIL_EXPORT_SINGLE=1" "StencilBitsSingle;frag;-DSTENCIL_BITS_SINGLE=1")
list(GET agcSampleOrderVariant 0 agcSampleOrderName)
list(GET agcSampleOrderVariant 1 agcSampleOrderExtension)
list(GET agcSampleOrderVariant 2 agcSampleOrderDefine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,8 @@ void Driver::restartFinishedDraw(DrawTail& tail) {

void Driver::commitDraw(std::shared_ptr<VulkanDevice> localDevice, std::uint32_t queue, const Graphics::State& graphics, const Pm4::DrawParameters& drawParameters, std::span<const Graphics::CompiledShader> stages, std::span<const Graphics::GuestMemorySnapshot> snapshots, const std::shared_ptr<const DrawRecipe>& recipe, const std::vector<std::shared_ptr<DispatchVariant>>& recipeStages, std::uint64_t drawKey, const std::shared_ptr<DrawEntry>& recipeDonor, VulkanDevice::DrawTraceContext trace, std::uint64_t shape, const std::vector<std::uint32_t>* contentKey) {
const auto* tracePtr = trace.token != 0 ? &trace : nullptr;
if (localDevice != nullptr) localDevice->PrebuildGraphicsLibrariesOnFirstVisit(graphics.stages, stages);
const bool earlyTestsModule = localDevice != nullptr && graphics.pixelEarlyZ && Graphics::PartialSampleMask(graphics) && Graphics::DepthStencilTests(graphics) && Graphics::SampleMaskAfterEarlyTests(localDevice->graphicsContext(), graphics, stages);
if (localDevice != nullptr && !earlyTestsModule) localDevice->PrebuildGraphicsLibrariesOnFirstVisit(graphics.stages, stages);
GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::Draw);
std::lock_guard gpuLock(GuestMemory::GpuMutex());
if (auto current = device.Load(); current != nullptr && current != localDevice) localDevice = std::move(current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bool DepthTargetOverlaps(const DepthTarget& target, std::uint64_t address, std::
const char* DepthPlanesObligation(VkDevice device, std::uint64_t address, std::uint64_t bytes);
const char* DepthTargetObligation(const Context& context, const DepthTarget& target, bool depthWritten);
SampleOrderTarget DepthSurfaceOrderTarget(const Context& context, const DepthTarget& target);
SampleOrderTarget DepthSurfaceMaskedTarget(const Context& context, const DepthTarget& target);
DepthSampleLocations DepthSurfaceSampleLocations(const Context& context, const DepthTarget& target);
enum class DepthPlaneScope { Overlapped, Whole };
std::vector<std::pair<std::uint64_t, std::uint64_t>> FlushDepthPlanes(std::uint64_t address, std::size_t bytes, DepthPlaneScope scope = DepthPlaneScope::Overlapped);
Expand Down
2 changes: 2 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/include/Pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ bool ValidateSampleLocations(const Context& context, const State& state);
void ValidateShaderPair(const ShaderRecompiler::RecompileResult& vertex, const ShaderRecompiler::RecompileResult& fragment);
bool ShadersWriteMemory(std::span<const CompiledShader> shaders);
bool DeclaresEarlyFragmentTests(const ShaderRecompiler::RecompileResult& program);
bool SampleMaskAfterEarlyTests(const Context& context, const State& state, std::span<const CompiledShader> shaders);
std::vector<std::uint32_t> WithEarlyFragmentTests(std::span<const std::uint32_t> words);
void ValidateSampleMask(const Context& context, const State& state, std::span<const CompiledShader> shaders);
// Returns the color attachment locations the pixel shader writes.
std::set<std::uint32_t> ValidateShaders(std::span<const CompiledShader> shaders, const State& state, const VkPhysicalDeviceSubgroupProperties& subgroup, bool fragmentShaderBarycentric, bool descriptorIndexing = false, bool imageInt64Atomics = false, bool geometryShader = false, bool sampleRateShading = false, bool bufferInt64Atomics = false, bool shaderClipDistance = false, bool shaderCullDistance = false, bool viewportIndexLayer = false, bool runtimeDescriptorArray = false, bool demoteToHelperInvocation = false, bool shaderTerminateInvocation = false, bool meshShader = true, bool shaderResourceMinLod = false, bool fmaFloat16 = false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ SampleOrderPass SampleOrderPassFor(const Context& context, const State& state, c
void RecordSampleOrder(const Context& context, CommandState& recording, const SampleOrderPass& pass, std::span<const SampleOrderTarget> targets, bool toHost, std::vector<std::shared_ptr<void>>& kept);
void RecordDepthSampleLocations(const Context& context, VkCommandBuffer commands, const DepthSampleLocations& depth);
void RecordDepthSampleImport(const Context& context, CommandState& recording, const SampleOrderTarget& target, VkImageView samples, std::vector<std::shared_ptr<void>>& kept);
std::array<std::span<const std::uint32_t>, 9> SampleOrderShaderModules();
std::string MaskedDepthRestoreRefusal(const Context& context, const State& state);
std::shared_ptr<void> RecordMaskedDepthSave(const Context& context, CommandState& recording, const SampleOrderTarget& target, VkExtent2D extent);
void RecordMaskedDepthRestore(const Context& context, CommandState& recording, const SampleOrderTarget& target, VkExtent2D extent, std::uint8_t quadPixelMask, const std::shared_ptr<void>& saved, std::vector<std::shared_ptr<void>>& kept);
std::array<std::span<const std::uint32_t>, 12> SampleOrderShaderModules();
void ClearSampleOrders(VkDevice device);

}
Expand Down
20 changes: 20 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/shaders/SampleOrder.frag
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
#version 450
#extension GL_EXT_samplerless_texture_functions : require
#ifdef DEPTH_SINGLE
#define SINGLE_SAMPLE 1
#endif
#ifdef STENCIL_EXPORT_SINGLE
#define STENCIL_EXPORT 1
#define SINGLE_SAMPLE 1
#endif
#ifdef STENCIL_BITS_SINGLE
#define STENCIL_BITS 1
#define SINGLE_SAMPLE 1
#endif
#ifdef STENCIL_EXPORT
#extension GL_ARB_shader_stencil_export : require
#endif

#ifdef SINGLE_SAMPLE
layout(binding = 0) uniform texture2D depthSource;
layout(binding = 1) uniform utexture2D stencilSource;
#else
layout(binding = 0) uniform texture2DMS depthSource;
layout(binding = 1) uniform utexture2DMS stencilSource;
#endif

layout(push_constant) uniform Order {
uvec4 map;
Expand All @@ -19,7 +35,11 @@ void main() {
const uint bottom = right ? order.map.w : order.map.z;
const uint map = (at.y & 1) != 0 ? bottom : top;
if (map == 0x76543210u) discard;
#ifdef SINGLE_SAMPLE
const int source = 0;
#else
const int source = int((map >> (uint(gl_SampleID) * 4u)) & 7u);
#endif
#ifdef STENCIL_BITS
if (((texelFetch(stencilSource, at, source).r >> order.bit) & 1u) == 0u) discard;
#else
Expand Down
10 changes: 10 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4363,6 +4363,16 @@ SampleOrderTarget DepthSurfaceOrderTarget(const Context& context, const DepthTar
throw std::runtime_error("AGC graphics: the multisampled depth surface whose samples are put in the guest's order is not resident");
}

SampleOrderTarget DepthSurfaceMaskedTarget(const Context& context, const DepthTarget& target) {
std::lock_guard gpu(GuestMemory::GpuMutex());
std::unique_lock lock(surfacesMutex());
for (const auto& candidate : surfaces()) {
if (!candidate->active->load(std::memory_order_acquire) || candidate->context.device != context.device || !holdsTarget(candidate->target, target)) continue;
return {candidate->image, candidate->view, candidate->hostFormat, candidate->target.extent, target.samples, DepthSampleOrderMethod(context, target), candidate->owned};
}
throw std::runtime_error("AGC graphics: the depth surface whose masked pixels are put back after the draw is not resident");
}

DepthSampleLocations DepthSurfaceSampleLocations(const Context& context, const DepthTarget& target) {
std::lock_guard gpu(GuestMemory::GpuMutex());
std::unique_lock lock(surfacesMutex());
Expand Down
34 changes: 30 additions & 4 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,10 @@ DrawInputs prepareDrawInputs(const Context& context, const State& state, const P
const auto* recorder = Recorder::Active();
Require(recorder == nullptr || !recorder->CountingSamples(), "a partial write of a packed color target merges inside the pixel shader after early depth and stencil tests, which a shader that kills pixels under late tests cannot use while occlusion queries count samples: the early tests would count the pixels it kills");
}
if (SampleMaskAfterEarlyTests(context, state, shaders)) {
const auto* recorder = Recorder::Active();
Require(recorder == nullptr || !recorder->CountingSamples(), "a sample mask applied in the pixel shader, from a pixel program with early depth or stencil tests that writes memory, is unsupported while occlusion queries count samples: the early tests would count the masked pixels");
}
timer.phase(PhaseValidate);
timing.Mark("validate");
inputs.maxIndex = draw.indexed ? 0u : draw.firstVertex + draw.indexCount - 1u;
Expand Down Expand Up @@ -2887,6 +2891,12 @@ struct StagedWrite {
VkRect2D area{};
};

struct MaskedDepth {
SampleOrderTarget target;
VkExtent2D extent{};
std::uint8_t quadPixelMask = 0xfu;
};

struct BorrowedDraw {
BorrowedDraw(const std::shared_ptr<ShaderResources>& resources, const std::shared_ptr<Pipeline>& pipeline, const std::shared_ptr<Framebuffer>& framebuffer) : resources(resources), pipeline(pipeline), framebuffer(framebuffer) {}
BorrowedDraw(const BorrowedDraw&) = delete;
Expand Down Expand Up @@ -2916,6 +2926,7 @@ struct BorrowedDraw {
std::vector<SampleOrderTarget> reordered;
SampleOrderPass reorderPass;
std::optional<DepthSampleLocations> depthLocations;
std::optional<MaskedDepth> maskedDepth;
std::shared_ptr<std::shared_ptr<const void>>* spaceKeep = nullptr;
};

Expand Down Expand Up @@ -3166,7 +3177,7 @@ void recordDraw(const Context& context, const State& state, const Pm4::DrawParam
// within the open batch) and the extent. The previous draw's pass is continued only when this
// draw neither reads its attachments (a barrier would be owed, which no pass allows) nor
// records anything outside a pass (an indirect draw's argument barrier and scratch copies).
const auto passKey = DrawRenderPassKey(context, state, record.targetViews);
const auto passKey = record.maskedDepth ? (DrawRenderPassKey(context, state, record.targetViews) ^ (0x6d61736b65640000ull | record.maskedDepth->quadPixelMask)) * 1099511628211ull : DrawRenderPassKey(context, state, record.targetViews);
const bool readsTarget = std::any_of(record.targets.begin(), record.targets.end(), [&](const std::shared_ptr<StorageTexture>& target) { return resources.ReadsImage(target.get()); });
// A queued DCC key store over memory the draw writes or reads in place (unknown for an
// address-based build), or over its GPU-side records, lands before it, as before a dispatch
Expand Down Expand Up @@ -3294,6 +3305,7 @@ void recordDraw(const Context& context, const State& state, const Pm4::DrawParam
VkDeviceSize argumentOffset = 0;
bool rewritten = false;
if (!meshIndirect && !inputs.restartTable.empty()) meshArguments = directMeshArguments(context, draw, inputs.restartTable);
std::shared_ptr<void> savedDepth;
static const bool timeEachDraw = std::getenv("APS5_PROFILE_GPU_DRAWS") != nullptr;
auto eachDrawTiming = Recorder::NoTiming;
std::array<std::byte, PipelinePushConstantBytes> movedPush;
Expand Down Expand Up @@ -3346,6 +3358,10 @@ void recordDraw(const Context& context, const State& state, const Pm4::DrawParam
RecordSampleOrder(context, rec, record.reorderPass, record.reordered, true, kept);
for (auto& object : kept) recorder->Keep(std::move(object));
}
if (record.maskedDepth) {
savedDepth = RecordMaskedDepthSave(context, rec, record.maskedDepth->target, record.maskedDepth->extent);
recorder->Keep(savedDepth);
}
for (const auto& write : record.staged) RecordStagedClear(context, cb, *write.temporary);
APS5_LOG_OUT_DEBUG("Beginning pipeline renderExtent=%ux%u", state.renderExtent.width, state.renderExtent.height);
record.pipeline->Begin(rec, *record.framebuffer, state.renderExtent, state, record.masked, secondaryContents);
Expand Down Expand Up @@ -3430,9 +3446,14 @@ void recordDraw(const Context& context, const State& state, const Pm4::DrawParam
auto checkRecords = indirectRecordCheck(record.indirect);
APS5_LOG_CHARS_OUT_DEBUG("Draw recorded");
std::function<void(CommandState&)> storeProxies;
if (!continued && (!record.proxies.empty() || !record.reordered.empty() || record.depthLocations)) {
storeProxies = [proxies = record.proxies, reordered = record.reordered, reorderPass = record.reorderPass, depthLocations = record.depthLocations, context, recorder](CommandState& passRecording) {
if (!continued && (!record.proxies.empty() || !record.reordered.empty() || record.depthLocations || record.maskedDepth)) {
storeProxies = [proxies = record.proxies, reordered = record.reordered, reorderPass = record.reorderPass, depthLocations = record.depthLocations, maskedDepth = record.maskedDepth, savedDepth, context, recorder](CommandState& passRecording) {
const auto passCommands = passRecording.Commands();
if (maskedDepth) {
std::vector<std::shared_ptr<void>> kept;
RecordMaskedDepthRestore(context, passRecording, maskedDepth->target, maskedDepth->extent, maskedDepth->quadPixelMask, savedDepth, kept);
for (auto& object : kept) recorder->Keep(std::move(object));
}
for (const auto& proxy : proxies) {
proxy->RecordAttachmentProxyStore(passCommands, VK_IMAGE_LAYOUT_GENERAL);
Recorder::CountBarriers(Recorder::CommandClass::Draw, 2);
Expand Down Expand Up @@ -3587,6 +3608,7 @@ struct PreparedDraw {
std::vector<SampleOrderTarget> reordered;
SampleOrderPass reorderPass;
std::optional<DepthSampleLocations> depthLocations;
std::optional<MaskedDepth> maskedDepth;
std::shared_ptr<std::shared_ptr<const void>> spaceKeep;
};

Expand Down Expand Up @@ -4761,6 +4783,7 @@ bool prepareDraw(const Context& context, const State& state, const Pm4::DrawPara
}
}
prepared.depthLocations = passDepthLocations(context, pipelineState, *pipeline);
if (SampleMaskAfterEarlyTests(context, state, shaders) && DepthStencilWrites(state)) prepared.maskedDepth = MaskedDepth{DepthSurfaceMaskedTarget(context, *state.depth), state.renderExtent, QuadPixelMask(state)};
timer.phase(PhasePipeline);
APS5_LOG_CHARS_OUT_DEBUG("Pipeline created");
if (VerifyDrawPlan()) requireDrawPlanRepeats(context, state, draw, shaders, plan);
Expand Down Expand Up @@ -4841,6 +4864,7 @@ void recordPreparedDraw(const Context& context, const State& state, const Pm4::D
record.reordered = reordered;
record.reorderPass = reorderPass;
record.depthLocations = depthLocations;
record.maskedDepth = prepared.maskedDepth;
record.spaceKeep = &prepared.spaceKeep;
record.masked = prepared.masked.has_value() ? &*prepared.masked : nullptr;
recordDraw(context, state, draw, shaders, inputs, record, outcome, timer, ownWaitedMs, prepared.trace);
Expand All @@ -4849,7 +4873,7 @@ void recordPreparedDraw(const Context& context, const State& state, const Pm4::D
// only a template the resource cache serves under this content key (reusable: no lease,
// no copied writes, every direct region import- or mirror-served) of a direct draw, so a
// hit's proof is the template's ProveCurrent and nothing needs completion work.
if (recipeOut != nullptr && DrawRecipes() && cacheable && contentKey != nullptr && !outcome.waited && args == nullptr && resources->Reusable() && !resources->SpaceReuse() && !multisampled && !pipeline->ReordersSamples()) {
if (recipeOut != nullptr && DrawRecipes() && cacheable && contentKey != nullptr && !outcome.waited && args == nullptr && resources->Reusable() && !resources->SpaceReuse() && !multisampled && !pipeline->ReordersSamples() && !prepared.maskedDepth) {
auto recipe = std::make_shared<DrawRecipe>();
recipe->device = context.device;
recipe->templateRef = resources;
Expand Down Expand Up @@ -4992,6 +5016,7 @@ void recordPreparedDraw(const Context& context, const State& state, const Pm4::D
}
std::vector<std::shared_ptr<void>> reorderKept;
RecordSampleOrder(context, recording, reorderPass, reordered, true, reorderKept);
const auto savedDepth = prepared.maskedDepth ? RecordMaskedDepthSave(context, recording, prepared.maskedDepth->target, prepared.maskedDepth->extent) : nullptr;
APS5_LOG_OUT_DEBUG("Beginning pipeline renderExtent=%ux%u", state.renderExtent.width, state.renderExtent.height);
pipeline->Begin(recording, *framebuffer, state.renderExtent, state, masked.has_value() ? &*masked : nullptr);
APS5_LOG_CHARS_OUT_DEBUG("Pipeline Begin OK");
Expand Down Expand Up @@ -5028,6 +5053,7 @@ void recordPreparedDraw(const Context& context, const State& state, const Pm4::D
APS5_LOG_CHARS_OUT_DEBUG("Draw recorded");
if (recorded) recorder->EndPassSamples();
EndRenderPass(context, commands);
if (prepared.maskedDepth) RecordMaskedDepthRestore(context, recording, prepared.maskedDepth->target, prepared.maskedDepth->extent, prepared.maskedDepth->quadPixelMask, savedDepth, reorderKept);
RecordSampleOrder(context, recording, reorderPass, reordered, false, reorderKept);
if (depthLocations) {
RecordDepthSampleLocations(context, commands, *depthLocations);
Expand Down
Loading
Loading