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) <noreply@anthropic.com>
This commit is contained in:
Michon
2026-10-01 23:21:17 +02:00
parent 709d7fe0fd
commit f9162abb84
8 changed files with 44 additions and 26 deletions
@@ -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;
@@ -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<Texture> DepthSurfaceTexture(const Context& context, std::span<const std::uint32_t> words, const GuestTextureResource& resource, VkComponentMapping components);
@@ -47,6 +47,7 @@ struct ColorTarget {
std::uint32_t mip = 0;
bool mipTail = false;
std::array<std::uint32_t, 2> 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<ColorTarget> colors;
std::vector<VkPipelineColorBlendAttachmentState> blends;
@@ -141,6 +141,14 @@ std::vector<std::unique_ptr<DepthSurface>>& 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<std::uint64_t>((extent.width + blockWidth - 1) / blockWidth * blockWidth);
const auto height = static_cast<std::uint64_t>((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()) {
@@ -451,7 +451,7 @@ bool ValidationKey(const Context& context, std::span<const CompiledShader> 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<std::uint64_t>(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<StorageTexture>& 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<RenderTarget>(context, color, state.blends[index].blendEnable != 0);
binding.target = std::make_unique<RenderTarget>(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));
@@ -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<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout")(context.device, &layoutInfo, nullptr, &layout), "vkCreatePipelineLayout graphics");
std::vector<VkAttachmentDescription> colors;
std::vector<VkAttachmentReference> references;
std::vector<VkAttachmentReference> 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<std::byte> 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);
@@ -514,9 +514,7 @@ std::set<std::uint32_t> ValidateShaders(std::span<const CompiledShader> 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<std::size_t>(state.colors.size(), 1u);
const auto attachments = std::max<std::size_t>(state.blends.size(), 1u);
std::set<std::uint32_t> locations;
for (const auto& [location, signature] : previous.outputs) {
if (location >= attachments) continue;
@@ -1,5 +1,6 @@
#include <cstdio>
#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<std::uint64_t>(slice) * DepthSliceBytes(depth.extent, zFormat == 1 ? 2u : 4u);
if (depth.stencilAddress != 0) depth.stencilAddress += static_cast<std::uint64_t>(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<std::uint8_t>(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<unsigned long long>(color.address), color.extent.width, color.extent.height, static_cast<unsigned long long>(color.bytes), static_cast<unsigned>(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<unsigned>(result.topology));
return result;
}
@@ -573,13 +574,16 @@ State DecodeState(const QueueState& queue) {
std::array<std::uint8_t, 8> ExportMappings(const State& state) {
std::array<std::uint8_t, 8> 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;