From f9162abb840cde12e137363c0ff9cca07f3719c4 Mon Sep 17 00:00:00 2001 From: Michon <231849@buas.nl> Date: Thu, 1 Oct 2026 23:21:17 +0200 Subject: [PATCH] fix(agc): color targets with gaps between written MRT slots, depth array slices An MRT slot below the highest written one that the draw does not write is a VK_ATTACHMENT_UNUSED reference with a disabled blend; color targets keep their slot for export mappings, pipeline and pass keys. DB_DEPTH_VIEW SLICE_START moves the depth and stencil planes by whole slices, so each slice of a depth array is its own depth surface. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/Driver/Draw/DrawRegisterKey.cpp | 2 +- .../Graphics/include/DepthSurface.hpp | 1 + .../Graphics/include/State.hpp | 2 +- .../Graphics/src/DepthSurface.cpp | 8 +++++ .../prx/libSceAgcDriver/Graphics/src/Draw.cpp | 8 +++-- .../libSceAgcDriver/Graphics/src/Pipeline.cpp | 11 +++--- .../Graphics/src/ShaderValidation.cpp | 4 +-- .../libSceAgcDriver/Graphics/src/State.cpp | 34 +++++++++++-------- 8 files changed, 44 insertions(+), 26 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRegisterKey.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRegisterKey.cpp index 4366fcf02..f4184ee0f 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRegisterKey.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRegisterKey.cpp @@ -54,7 +54,7 @@ bool Driver::sameDecode(const DrawDecode& a, const DrawDecode& b) { const auto& s = a.state; const auto& t = b.state; const auto sameColor = [](const Graphics::ColorTarget& x, const Graphics::ColorTarget& y) { - return x.address == y.address && x.extent.width == y.extent.width && x.extent.height == y.extent.height && x.format == y.format && x.bytes == y.bytes && x.componentMapping == y.componentMapping && x.tileMode == y.tileMode && x.elementBytes == y.elementBytes && x.dccAddress == y.dccAddress && x.dccAlphaOnMsb == y.dccAlphaOnMsb; + return x.address == y.address && x.extent.width == y.extent.width && x.extent.height == y.extent.height && x.format == y.format && x.bytes == y.bytes && x.componentMapping == y.componentMapping && x.tileMode == y.tileMode && x.elementBytes == y.elementBytes && x.dccAddress == y.dccAddress && x.dccAlphaOnMsb == y.dccAlphaOnMsb && x.slot == y.slot; }; const auto sameBlend = [](const VkPipelineColorBlendAttachmentState& x, const VkPipelineColorBlendAttachmentState& y) { return x.blendEnable == y.blendEnable && x.srcColorBlendFactor == y.srcColorBlendFactor && x.dstColorBlendFactor == y.dstColorBlendFactor && x.colorBlendOp == y.colorBlendOp && x.srcAlphaBlendFactor == y.srcAlphaBlendFactor && x.dstAlphaBlendFactor == y.dstAlphaBlendFactor && x.alphaBlendOp == y.alphaBlendOp && x.colorWriteMask == y.colorWriteMask; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp index 7dbb70b89..2e8ed728c 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp @@ -12,6 +12,7 @@ namespace AgcDriver::Graphics { class Texture; VkImageView DepthSurfaceView(const Context& context, const DepthTarget& target); +std::uint64_t DepthSliceBytes(VkExtent2D extent, std::uint32_t bytesPerTexel); void ClearDepthSurfaces(VkDevice device); bool DepthSurfaceAt(std::uint64_t address); std::shared_ptr DepthSurfaceTexture(const Context& context, std::span words, const GuestTextureResource& resource, VkComponentMapping components); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp index 08fdf00a8..326a2098b 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp @@ -47,6 +47,7 @@ struct ColorTarget { std::uint32_t mip = 0; bool mipTail = false; std::array clearWords{}; + std::uint32_t slot = 0; }; struct DepthTarget { @@ -67,7 +68,6 @@ struct State { bool stencilTest = false; VkStencilOpState stencilFront{}; VkStencilOpState stencilBack{}; - // MRT slot 0; `colors`/`blends` hold every written slot, attachment i being slot i. ColorTarget color; std::vector colors; std::vector blends; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp index 7f3a0cdcc..33194fdb2 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp @@ -141,6 +141,14 @@ std::vector>& surfaces() { } +std::uint64_t DepthSliceBytes(VkExtent2D extent, std::uint32_t bytesPerTexel) { + const std::uint32_t blockWidth = bytesPerTexel == 4 ? 128u : 256u; + const std::uint32_t blockHeight = bytesPerTexel == 1 ? 256u : 128u; + const auto width = static_cast((extent.width + blockWidth - 1) / blockWidth * blockWidth); + const auto height = static_cast((extent.height + blockHeight - 1) / blockHeight * blockHeight); + return width * height * bytesPerTexel; +} + VkImageView DepthSurfaceView(const Context& context, const DepthTarget& target) { std::lock_guard lock(surfacesMutex()); for (const auto& surface : surfaces()) { diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp index 9aca4f99a..a20c246df 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp @@ -451,7 +451,7 @@ bool ValidationKey(const Context& context, std::span shade add(state.rectList); add(state.topology); add(state.cullMode); - add(state.colors.size()); + add(state.blends.size()); add(context.subgroup.subgroupSize); add(context.subgroup.supportedStages); add(context.subgroup.supportedOperations); @@ -1193,6 +1193,10 @@ void recordDraw(const Context& context, const State& state, const Pm4::DrawParam passKey *= 1099511628211ull; }; for (const auto view : record.targetViews) mix(reinterpret_cast(view)); + if (state.blends.size() != state.colors.size()) { + mix(state.blends.size()); + for (const auto& color : state.colors) mix(color.slot); + } mix(state.renderExtent.width); mix(state.renderExtent.height); const bool readsTarget = std::any_of(record.targets.begin(), record.targets.end(), [&](const std::shared_ptr& target) { return resources.ReadsImage(target.get()); }); @@ -1428,7 +1432,7 @@ void Draw(const Context& context, const State& state, const Pm4::DrawParameters& } } timer.phase(PhaseReadTarget); - binding.target = std::make_unique(context, color, state.blends[index].blendEnable != 0); + binding.target = std::make_unique(context, color, state.blends.at(color.slot).blendEnable != 0); targetViews.push_back(binding.target->View()); } if (state.depth) targetViews.push_back(DepthSurfaceView(context, *state.depth)); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp index 36772cf21..6fe6836ea 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp @@ -45,8 +45,8 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput // A cached pipeline may outlive its device's teardown (see ClearCachedPipelines); it must not keep // the buffer pool, which is reset with the device, alive past it. this->context.bufferPool.reset(); - Require(state.blends.size() == state.colors.size(), "blend states do not match decoded color state"); - Require(state.colors.size() <= context.limits.maxColorAttachments, "color targets exceed device attachment limits"); + Require(state.blends.size() == (state.colors.empty() ? 0u : state.colors.back().slot + 1u) && state.colors.size() <= state.blends.size(), "blend states do not match decoded color state"); + Require(state.blends.size() <= context.limits.maxColorAttachments, "color targets exceed device attachment limits"); Require(state.hasColorTarget || (context.limits.framebufferNoAttachmentsSampleCounts & VK_SAMPLE_COUNT_1_BIT) != 0, "device does not support single-sample rendering without attachments"); Require(!state.negativeOneToOne || context.depthClipControl, "negative-one-to-one depth clipping requires VK_EXT_depth_clip_control with depthClipControl enabled"); if (state.rectList) Require(context.tessellationShader && context.limits.maxTessellationPatchSize >= 4, "rect-list requires tessellation with four output control points"); @@ -88,7 +88,7 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput layoutInfo.pPushConstantRanges = pushStages != 0 ? &push : nullptr; Check(context.Function("vkCreatePipelineLayout")(context.device, &layoutInfo, nullptr, &layout), "vkCreatePipelineLayout graphics"); std::vector colors; - std::vector references; + std::vector references(state.blends.size(), VkAttachmentReference{VK_ATTACHMENT_UNUSED, attachmentLayout}); for (std::uint32_t index = 0; index < state.colors.size(); ++index) { VkAttachmentDescription color{}; color.format = state.colors[index].format; @@ -100,7 +100,7 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput color.initialLayout = attachmentLayout; color.finalLayout = attachmentLayout; colors.push_back(color); - references.push_back({index, attachmentLayout}); + references.at(state.colors[index].slot) = {index, attachmentLayout}; } VkSubpassDescription subpass{}; subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; @@ -341,6 +341,9 @@ std::vector pipelineKey(const Context& context, const State& state, c for (const auto value : state.blendConstants) append(key, value); append(key, state.colors.size()); for (const auto& color : state.colors) append(key, color.format); + if (state.blends.size() != state.colors.size()) { + for (const auto& color : state.colors) append(key, color.slot); + } append(key, state.depth.has_value()); if (state.depth) { append(key, state.depth->format); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp index bf9bf9795..fb29126f5 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp @@ -514,9 +514,7 @@ std::set ValidateShaders(std::span shaders, } previous = current; } - // One float4 color per MRT attachment. A pixel shader may also export no color at all when it - // writes storage images or buffers instead; its attachments are then left untouched. - const auto attachments = std::max(state.colors.size(), 1u); + const auto attachments = std::max(state.blends.size(), 1u); std::set locations; for (const auto& [location, signature] : previous.outputs) { if (location >= attachments) continue; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp index cdeb09c36..6cb429af8 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp @@ -1,5 +1,6 @@ #include #include "prx/libSceAgcDriver/Graphics/include/State.hpp" +#include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp" #include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" #include "prx/libSceAgcDriver/Graphics/include/DccMetadata.hpp" #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" @@ -137,7 +138,7 @@ VkStencilOpState stencilFace(std::uint32_t compare, std::uint32_t ops, std::uint void decodeDepth(const Registers& cx, std::uint32_t depthControl, State& result) { zero(cx, 0x000, 0x00001f9fu, "depth/stencil clear, copy, resummarize or decompress draws (DB_RENDER_CONTROL)"); const auto view = read(cx, 0x002); - zero(cx, 0x002, ~0x03000000u, "depth array slices or mips (DB_DEPTH_VIEW)"); + zero(cx, 0x002, 0x3c000000u, "depth mips (DB_DEPTH_VIEW MIP_LEVEL)"); zero(cx, 0x010, 0x000f100cu, "multisampled, partially resident or mipmapped depth (DB_Z_INFO)"); zero(cx, 0x011, 0x00001000u, "partially resident stencil (DB_STENCIL_INFO)"); const auto zFormat = read(cx, 0x010) & 3u; @@ -158,6 +159,10 @@ void decodeDepth(const Registers& cx, std::uint32_t depthControl, State& result) Require(!stencil || stencilReadOnly || base(0x015, 0x01d) == depth.stencilAddress, "stencil read and written at different addresses is unsupported"); const auto size = read(cx, 0x007); depth.extent = {(size & 0x3fffu) + 1u, ((size >> 16u) & 0x3fffu) + 1u}; + if (const auto slice = view & 0x1fffu; slice != 0) { + if (depth.address != 0) depth.address += static_cast(slice) * DepthSliceBytes(depth.extent, zFormat == 1 ? 2u : 4u); + if (depth.stencilAddress != 0) depth.stencilAddress += static_cast(slice) * DepthSliceBytes(depth.extent, 1u); + } depth.format = zFormat == 1 ? (stencil ? VK_FORMAT_D16_UNORM_S8_UINT : VK_FORMAT_D16_UNORM) : (stencil ? VK_FORMAT_D32_SFLOAT_S8_UINT : VK_FORMAT_D32_SFLOAT); depth.clearDepth = readFloat(cx, 0x00b); depth.clearStencil = static_cast(read(cx, 0x00a) & 0xffu); @@ -465,18 +470,11 @@ State DecodeState(const QueueState& queue) { // matters where the shader exports. const auto targetMask = read(cx, 0x8e) & shaderMask; APS5_LOG_OUT_DEBUG("CB_TARGET_MASK=0x%x CB_SHADER_MASK=0x%x", targetMask, shaderMask); - // MRT slots 0..n-1 become attachments 0..n-1; a slot between written slots must be written too. std::uint32_t slotCount = 0; for (std::uint32_t slot = 0; slot < 8; ++slot) { if (((targetMask >> (4u * slot)) & 0xfu) != 0) slotCount = slot + 1; } - for (std::uint32_t slot = 0; slot < slotCount; ++slot) { - if (((targetMask >> (4u * slot)) & 0xfu) == 0) { - std::ostringstream message; - message << "AGC graphics: color targets with gaps are unsupported: CB_TARGET_MASK=0x" << std::hex << targetMask << ", CB_SHADER_MASK=0x" << shaderMask; - throw std::runtime_error(message.str()); - } - } + const auto written = [&](std::uint32_t slot) { return ((targetMask >> (4u * slot)) & 0xfu) != 0; }; result.hasColorTarget = slotCount != 0; APS5_LOG_OUT_DEBUG("hasColorTarget=%u slots=%u", result.hasColorTarget ? 1u : 0u, slotCount); @@ -489,12 +487,13 @@ State DecodeState(const QueueState& queue) { // exports that PA_CL_VS_OUT_CNTL validation above already limits to ignored layer/viewport data. Require((read(cx, 0x1c3) & 0xfu) == 4, "additional position exports are unsupported"); for (std::uint32_t slot = 0; slot < slotCount; ++slot) { + if (!written(slot)) continue; // Export formats only matter for the targets the draw writes. const auto slotExport = (exportFormat >> (4u * slot)) & 0xfu; if (slotExport == 0 || slotExport == 7 || slotExport == 8 || slotExport > 9) throw std::runtime_error("AGC graphics: color export format " + std::to_string(slotExport) + " is unsupported"); const auto color = DecodeColorBuffer(cx, slot); APS5_LOG_OUT_DEBUG("Color %u address=0x%llx extent=%ux%u bytes=%llu VkFormat=%u", slot, static_cast(color.address), color.extent.width, color.extent.height, static_cast(color.bytes), static_cast(color.format)); - if (slot == 0) { + if (result.colors.empty()) { result.renderExtent = color.extent; } else { result.renderExtent = {std::min(result.renderExtent.width, color.extent.width), std::min(result.renderExtent.height, color.extent.height)}; @@ -536,12 +535,14 @@ State DecodeState(const QueueState& queue) { intersect(result.scissor, cx, 0x90, false); if ((read(cx, 0x292) & 2u) != 0) intersect(result.scissor, cx, 0x94, false); APS5_LOG_OUT_DEBUG("Scissor offset=(%d,%d) extent=%ux%u", result.scissor.offset.x, result.scissor.offset.y, result.scissor.extent.width, result.scissor.extent.height); - for (std::uint32_t slot = 0; slot < result.colors.size(); ++slot) { + result.blends.assign(slotCount, VkPipelineColorBlendAttachmentState{}); + for (const auto& color : result.colors) { + const auto slot = color.slot; const auto blend = read(cx, 0x1e0 + slot); APS5_LOG_OUT_DEBUG("Blend %u control=0x%x", slot, blend); Require((blend & 0x0000e000u) == 0, "reserved blend control bits"); VkPipelineColorBlendAttachmentState state{}; - const auto mapping = result.colors[slot].componentMapping; + const auto mapping = color.componentMapping; const auto exportedMask = (targetMask >> (4u * slot)) & 0xfu; for (std::uint32_t component = 0; component < 4; ++component) { if (((exportedMask >> ((mapping >> (2u * component)) & 3u)) & 1u) != 0) state.colorWriteMask |= 1u << component; @@ -563,9 +564,9 @@ State DecodeState(const QueueState& queue) { } for (std::uint32_t i = 0; i < 4; ++i) result.blendConstants[i] = readFloat(cx, 0x105 + i); } - result.blends.push_back(state); + result.blends[slot] = state; } - if (!result.blends.empty()) result.blend = result.blends.front(); + if (!result.colors.empty()) result.blend = result.blends[result.colors.front().slot]; APS5_LOG_OUT_DEBUG("DecodeState done colorTarget=%u render=%ux%u topology=%u", result.hasColorTarget ? 1u : 0u, result.renderExtent.width, result.renderExtent.height, static_cast(result.topology)); return result; } @@ -573,13 +574,16 @@ State DecodeState(const QueueState& queue) { std::array ExportMappings(const State& state) { std::array mappings{}; mappings.fill(0xe4u); - for (std::size_t slot = 0; slot < state.colors.size() && slot < mappings.size(); ++slot) mappings[slot] = state.colors[slot].componentMapping; + for (const auto& color : state.colors) { + if (color.slot < mappings.size()) mappings[color.slot] = color.componentMapping; + } return mappings; } ColorTarget DecodeColorBuffer(const Registers& cx, std::uint32_t slot) { const auto stride = slot * 0xfu; ColorTarget color{}; + color.slot = slot; const auto info = read(cx, 0x31c + stride); const auto number = (info >> 8u) & 7u; const auto swap = (info >> 11u) & 3u;