From 6aa8e58de7b9dbabbb3bb9c24ed8d59c0cfa18dc Mon Sep 17 00:00:00 2001 From: Michon <231849@buas.nl> Date: Sun, 4 Oct 2026 18:27:40 +0200 Subject: [PATCH] feat(agc): 64 KiB standard color targets Color targets with the SW_64KB_S swizzle (CB_COLOR_ATTRIB3 tile mode 9) use the standard element order of the 4 KiB mode extended by the texture detiler's 64 KiB bits, and are resident like the other tiled targets. The standard blocks are now wider than tall for 2- and 8-byte elements, as the standard swizzle and the texture layout have them (they were transposed, aliasing texels). Co-Authored-By: Claude Opus 5.5 (1M context) --- .../Graphics/include/ColorTargetLayout.hpp | 3 +- .../Graphics/src/ColorTargetLayout.cpp | 39 ++++++++++++------- .../prx/libSceAgcDriver/Graphics/src/Draw.cpp | 6 +-- .../tests/ColorTargetLayout.cpp | 20 ++++++++++ 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp index 6431cd075..78112575a 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp @@ -12,11 +12,12 @@ namespace AgcDriver::Graphics { enum class ColorTileMode : std::uint32_t { Linear = 0, Standard4KB = 5, + Standard64KB = 9, RenderTarget = 0x1b }; constexpr TextureTileMode ColorTextureTileMode(ColorTileMode mode) { - return mode == ColorTileMode::Linear ? TextureTileMode::kLinear : mode == ColorTileMode::Standard4KB ? TextureTileMode::kStandard4KB : TextureTileMode::kR64KBX; + return mode == ColorTileMode::Linear ? TextureTileMode::kLinear : mode == ColorTileMode::Standard4KB ? TextureTileMode::kStandard4KB : mode == ColorTileMode::Standard64KB ? TextureTileMode::kStandard64KB : TextureTileMode::kR64KBX; } ColorTileMode DecodeColorTileMode(std::uint32_t attrib3); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp index 243402731..95cbf8470 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp @@ -27,6 +27,16 @@ std::uint32_t standardOffset(std::uint32_t x, std::uint32_t y, std::uint32_t ele } } +std::uint32_t standard64Extra(std::uint32_t x, std::uint32_t y, std::uint32_t elementBytes) { + switch (elementBytes) { + case 1u: return ((x << 7) & 0x2000u) ^ ((x << 8) & 0x8000u) ^ ((y << 6) & 0x1000u) ^ ((y << 7) & 0x4000u); + case 2u: return ((x << 7) & 0x2000u) ^ ((x << 8) & 0x8000u) ^ ((y << 7) & 0x1000u) ^ ((y << 8) & 0x4000u); + case 4u: return ((x << 8) & 0x2000u) ^ ((x << 9) & 0x8000u) ^ ((y << 7) & 0x1000u) ^ ((y << 8) & 0x4000u); + case 8u: return ((x << 8) & 0x2000u) ^ ((x << 9) & 0x8000u) ^ ((y << 8) & 0x1000u) ^ ((y << 9) & 0x4000u); + default: return ((x << 9) & 0x2000u) ^ ((x << 10) & 0x8000u) ^ ((y << 8) & 0x1000u) ^ ((y << 9) & 0x4000u); + } +} + } ColorTileMode DecodeColorTileMode(std::uint32_t attrib3) { @@ -35,7 +45,7 @@ ColorTileMode DecodeColorTileMode(std::uint32_t attrib3) { const auto mode = (attrib3 >> 14u) & 0x1fu; const auto fmaskMode = (attrib3 >> 19u) & 0x1fu; require(fmaskMode == 0 || fmaskMode == 0x18, "AGC graphics: unsupported color FMASK swizzle mode"); - require(mode == 0 || mode == 5 || mode == 0x1b, "AGC graphics: unsupported color tile mode"); + require(mode == 0 || mode == 5 || mode == 9 || mode == 0x1b, "AGC graphics: unsupported color tile mode"); return static_cast(mode); } @@ -70,18 +80,18 @@ const SwizzleTables& renderTargetTables(std::uint32_t bytesPerElement, std::uint return tables[index]; } -const SwizzleTables& standardTables(std::uint32_t bytesPerElement, std::uint32_t blockWidth, std::uint32_t blockHeight) { - static std::once_flag once[5]; - static SwizzleTables tables[5]; +const SwizzleTables& standardTables(std::uint32_t bytesPerElement, std::uint32_t blockWidth, std::uint32_t blockHeight, bool block64KB) { + static std::once_flag once[2][5]; + static SwizzleTables tables[2][5]; const auto index = static_cast(std::countr_zero(bytesPerElement)); - std::call_once(once[index], [&] { - auto& table = tables[index]; + std::call_once(once[block64KB][index], [&] { + auto& table = tables[block64KB][index]; table.x.resize(blockWidth); table.y.resize(blockHeight); - for (std::uint32_t x = 0; x < blockWidth; ++x) table.x[x] = standardOffset(x, 0, bytesPerElement); - for (std::uint32_t y = 0; y < blockHeight; ++y) table.y[y] = standardOffset(0, y, bytesPerElement); + for (std::uint32_t x = 0; x < blockWidth; ++x) table.x[x] = standardOffset(x, 0, bytesPerElement) ^ (block64KB ? standard64Extra(x, 0, bytesPerElement) : 0u); + for (std::uint32_t y = 0; y < blockHeight; ++y) table.y[y] = standardOffset(0, y, bytesPerElement) ^ (block64KB ? standard64Extra(0, y, bytesPerElement) : 0u); }); - return tables[index]; + return tables[block64KB][index]; } } @@ -108,13 +118,16 @@ ColorTargetLayout::ColorTargetLayout(std::uint32_t width, std::uint32_t height, yOffsets = tables.y.data(); break; } - case ColorTileMode::Standard4KB: { + case ColorTileMode::Standard4KB: + case ColorTileMode::Standard64KB: { const auto log2Bytes = static_cast(std::countr_zero(bytesPerElement)); - blockWidth = 1u << (6u - (log2Bytes + 1u) / 2u); - blockHeight = 1u << (6u - log2Bytes / 2u); + const bool block64KB = mode == ColorTileMode::Standard64KB; + const auto log2Side = block64KB ? 8u : 6u; + blockWidth = 1u << (log2Side - log2Bytes / 2u); + blockHeight = 1u << (log2Side - (log2Bytes + 1u) / 2u); pitch = (width + blockWidth - 1u) / blockWidth * blockWidth; paddedHeight = (height + blockHeight - 1u) / blockHeight * blockHeight; - const auto& tables = standardTables(bytesPerElement, blockWidth, blockHeight); + const auto& tables = standardTables(bytesPerElement, blockWidth, blockHeight, block64KB); xOffsets = tables.x.data(); yOffsets = tables.y.data(); break; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp index f9e21abd9..6a821c3d0 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp @@ -42,7 +42,7 @@ std::uint32_t GuestFormatFor(VkFormat format, std::uint32_t elementBytes) { // The color buffer as a single-mip 2D surface descriptor (tile mode SW_64KB_R_X). GuestTextureResource SurfaceForTarget(const ColorTarget& color) { - Require(color.tileMode == ColorTileMode::RenderTarget || color.tileMode == ColorTileMode::Standard4KB, "only 4 KiB standard and 64 KiB tiled color targets are resident"); + Require(color.tileMode != ColorTileMode::Linear, "linear color targets are not resident"); const bool chain = color.mipCount > 1; GuestTextureResource surface{}; surface.baseAddress = chain ? color.surfaceAddress : color.address; @@ -1546,7 +1546,7 @@ void Draw(const Context& context, const State& state, const Pm4::DrawParameters& timer.phase(PhaseSetup); // Debug aid: APS5_NO_RESIDENT_TARGETS=1 copies every target in and out again. static const bool residentTargets = std::getenv("APS5_NO_RESIDENT_TARGETS") == nullptr; - if ((binding.gpuTiling || (color.tileMode == ColorTileMode::Standard4KB && context.detiler != nullptr)) && residentTargets) { + if ((binding.gpuTiling || (color.tileMode != ColorTileMode::Linear && context.detiler != nullptr)) && residentTargets) { // The lookup refreshes the image on every draw (StorageTexture::Refresh: FlushPending, // CollectWrites over the target's pages, the DCC key scan of TextureClearKeys, then // UnchangedSince). The page walk is skipped while the worker's collect epoch lasts @@ -2130,7 +2130,7 @@ DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, con namespace { std::shared_ptr metadataPassResident(const Context& context, const ColorTarget& color) { - if ((color.tileMode != ColorTileMode::RenderTarget && color.tileMode != ColorTileMode::Standard4KB) || context.detiler == nullptr) return nullptr; + if (color.tileMode == ColorTileMode::Linear || context.detiler == nullptr) return nullptr; std::shared_ptr resident; try { resident = CachedStorageSurface(context, SurfaceForTarget(color)); diff --git a/core/libs/prx/libSceAgcDriver/tests/ColorTargetLayout.cpp b/core/libs/prx/libSceAgcDriver/tests/ColorTargetLayout.cpp index 972270466..40b69b7a4 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ColorTargetLayout.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ColorTargetLayout.cpp @@ -50,6 +50,26 @@ void RunColorTargetLayoutTests() { standardSeen[address / 4] = true; } } + for (const auto mode : {ColorTileMode::Standard4KB, ColorTileMode::Standard64KB}) { + for (const std::uint32_t bpe : {1u, 2u, 4u, 8u, 16u}) { + const auto block = mode == ColorTileMode::Standard64KB ? 65536u : 4096u; + const ColorTargetLayout sized(512, 512, mode, bpe); + Require(sized.Alignment() == block && sized.Bytes() == ComputeSurfaceSize(ComputeElementMipLayout(ColorTextureTileMode(mode), bpe, 512, 512, 1), 1), "standard color layout differs from the texture layout"); + std::vector seen(block / bpe); + std::uint32_t count = 0; + for (std::uint32_t y = 0; y < 512 && count < seen.size(); ++y) { + for (std::uint32_t x = 0; x < 512; ++x) { + const auto address = sized.Offset(x, y); + if (address >= block) continue; + Require(address % bpe == 0 && !seen[address / bpe], "standard block aliases its texels"); + seen[address / bpe] = true; + ++count; + } + } + Require(count == seen.size(), "standard block leaves texels unaddressed"); + } + } + Require(DecodeColorTileMode(0x4dc24000) == ColorTileMode::Standard64KB, "64 KiB standard color descriptor was rejected"); reject([&] { layout.Offset(257, 0); }); std::vector tiled(layout.Bytes(), std::byte{0x5a}); std::vector linear(layout.LinearBytes());