From df8c0f1a3acff13b384fe5e001a91e619f761180 Mon Sep 17 00:00:00 2001 From: Michon <231849@buas.nl> Date: Fri, 2 Oct 2026 21:27:43 +0200 Subject: [PATCH] perf(agc): build the color target swizzle tables once per element size ColorTargetLayout built its SW_64KB_R_X offset tables in every constructor, and every draw constructs a layout per color target while decoding and again while setting the target up. The tables depend only on the element size; they are now built once per size under std::call_once and the layout points into them. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../Graphics/include/ColorTargetLayout.hpp | 4 +- .../Graphics/src/ColorTargetLayout.cpp | 51 ++++++++++++++----- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp index 860584f7b..90e71db95 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp @@ -37,8 +37,8 @@ private: std::uint32_t blockWidth = 1; std::uint32_t blockHeight = 1; // The XOR swizzle is linear over GF(2), so a block offset is xOffsets[x] ^ yOffsets[y]. - std::vector xOffsets; - std::vector yOffsets; + const std::uint32_t* xOffsets = nullptr; + const std::uint32_t* yOffsets = nullptr; }; } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp index 4c74d01bd..b25def3e6 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include namespace AgcDriver::Graphics { @@ -27,6 +28,39 @@ ColorTileMode DecodeColorTileMode(std::uint32_t attrib3) { return static_cast(mode); } +namespace { + +struct SwizzleTables { + std::vector x; + std::vector y; +}; + +const SwizzleTables& renderTargetTables(std::uint32_t bytesPerElement, std::uint32_t blockWidth, std::uint32_t blockHeight) { + static std::once_flag once[5]; + static SwizzleTables tables[5]; + const auto index = static_cast(std::countr_zero(bytesPerElement)); + std::call_once(once[index], [&] { + const auto* equation = FindTextureSwizzleEquation(27u, bytesPerElement); + require(equation != nullptr, "AGC graphics: no SW_64KB_R_X equation for the color element size"); + auto& table = tables[index]; + table.x.resize(blockWidth); + table.y.resize(blockHeight); + for (std::uint32_t x = 0; x < blockWidth; ++x) { + std::uint32_t offset = 0; + for (std::uint32_t bit = 0; bit < 16u; ++bit) offset |= parity(x & equation->bits[bit] & 0xfffu) << bit; + table.x[x] = offset; + } + for (std::uint32_t y = 0; y < blockHeight; ++y) { + std::uint32_t offset = 0; + for (std::uint32_t bit = 0; bit < 16u; ++bit) offset |= parity((y << 12u) & equation->bits[bit] & 0xfff000u) << bit; + table.y[y] = offset; + } + }); + return tables[index]; +} + +} + ColorTargetLayout::ColorTargetLayout(std::uint32_t width, std::uint32_t height, ColorTileMode mode, std::uint32_t bytesPerElement) : width(width), height(height), pitch(width), mode(mode), bytes(0), elementBytes(bytesPerElement) { require(width != 0 && height != 0 && width <= 16384 && height <= 16384, "AGC graphics: invalid color surface extent"); require(std::has_single_bit(bytesPerElement) && bytesPerElement <= 16u, "AGC graphics: unsupported color element size"); @@ -44,20 +78,9 @@ ColorTargetLayout::ColorTargetLayout(std::uint32_t width, std::uint32_t height, blockHeight = 1u << (log2Elements / 2u); pitch = (width + blockWidth - 1u) / blockWidth * blockWidth; paddedHeight = (height + blockHeight - 1u) / blockHeight * blockHeight; - const auto* equation = FindTextureSwizzleEquation(27u, bytesPerElement); - require(equation != nullptr, "AGC graphics: no SW_64KB_R_X equation for the color element size"); - xOffsets.resize(blockWidth); - yOffsets.resize(blockHeight); - for (std::uint32_t x = 0; x < blockWidth; ++x) { - std::uint32_t offset = 0; - for (std::uint32_t bit = 0; bit < 16u; ++bit) offset |= parity(x & equation->bits[bit] & 0xfffu) << bit; - xOffsets[x] = offset; - } - for (std::uint32_t y = 0; y < blockHeight; ++y) { - std::uint32_t offset = 0; - for (std::uint32_t bit = 0; bit < 16u; ++bit) offset |= parity((y << 12u) & equation->bits[bit] & 0xfff000u) << bit; - yOffsets[y] = offset; - } + const auto& tables = renderTargetTables(bytesPerElement, blockWidth, blockHeight); + xOffsets = tables.x.data(); + yOffsets = tables.y.data(); break; } default: throw std::runtime_error("AGC graphics: unsupported color tile mode");