mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #377 from MichonGoddijn231849/feat/color-target-slices
feat(agc): 4 KiB standard color targets and color views of one array slice
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_GRAPHICS_INCLUDE_COLORTARGETLAYOUT_HPP
|
||||
#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_GRAPHICS_INCLUDE_COLORTARGETLAYOUT_HPP
|
||||
|
||||
#include "prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
@@ -10,9 +11,14 @@ namespace AgcDriver::Graphics {
|
||||
|
||||
enum class ColorTileMode : std::uint32_t {
|
||||
Linear = 0,
|
||||
Standard4KB = 5,
|
||||
RenderTarget = 0x1b
|
||||
};
|
||||
|
||||
constexpr TextureTileMode ColorTextureTileMode(ColorTileMode mode) {
|
||||
return mode == ColorTileMode::Linear ? TextureTileMode::kLinear : mode == ColorTileMode::Standard4KB ? TextureTileMode::kStandard4KB : TextureTileMode::kR64KBX;
|
||||
}
|
||||
|
||||
ColorTileMode DecodeColorTileMode(std::uint32_t attrib3);
|
||||
|
||||
class ColorTargetLayout {
|
||||
@@ -20,7 +26,7 @@ public:
|
||||
ColorTargetLayout(std::uint32_t width, std::uint32_t height, ColorTileMode mode, std::uint32_t bytesPerElement = 4);
|
||||
std::size_t Bytes() const { return bytes; }
|
||||
std::size_t LinearBytes() const { return static_cast<std::size_t>(width) * height * elementBytes; }
|
||||
std::size_t Alignment() const { return mode == ColorTileMode::Linear ? 256u : 65536u; }
|
||||
std::size_t Alignment() const { return mode == ColorTileMode::Linear ? 256u : mode == ColorTileMode::Standard4KB ? 4096u : 65536u; }
|
||||
std::uint32_t BlocksPerRow() const { return pitch / blockWidth; }
|
||||
std::size_t Offset(std::uint32_t x, std::uint32_t y) const;
|
||||
void Detile(std::span<const std::byte> source, std::span<std::byte> destination) const;
|
||||
|
||||
@@ -17,6 +17,16 @@ std::uint32_t parity(std::uint32_t value) {
|
||||
return static_cast<std::uint32_t>(std::popcount(value)) & 1u;
|
||||
}
|
||||
|
||||
std::uint32_t standardOffset(std::uint32_t x, std::uint32_t y, std::uint32_t elementBytes) {
|
||||
switch (elementBytes) {
|
||||
case 1u: return ((y << 4) & 0x1f0u) ^ ((y << 5) & 0x400u) ^ (x & 0x00fu) ^ ((x << 5) & 0x200u) ^ ((x << 6) & 0x800u);
|
||||
case 2u: return ((y << 4) & 0x070u) ^ ((y << 5) & 0x100u) ^ ((y << 6) & 0x400u) ^ ((x << 1) & 0x00eu) ^ ((x << 4) & 0x080u) ^ ((x << 5) & 0x200u) ^ ((x << 6) & 0x800u);
|
||||
case 4u: return ((y << 4) & 0x070u) ^ ((y << 5) & 0x100u) ^ ((y << 6) & 0x400u) ^ ((x << 2) & 0x00cu) ^ ((x << 5) & 0x080u) ^ ((x << 6) & 0x200u) ^ ((x << 7) & 0x800u);
|
||||
case 8u: return ((y << 4) & 0x030u) ^ ((y << 6) & 0x100u) ^ ((y << 7) & 0x400u) ^ ((x << 3) & 0x008u) ^ ((x << 5) & 0x0c0u) ^ ((x << 6) & 0x200u) ^ ((x << 7) & 0x800u);
|
||||
default: return ((y << 4) & 0x030u) ^ ((y << 6) & 0x100u) ^ ((y << 7) & 0x400u) ^ ((x << 6) & 0x0c0u) ^ ((x << 7) & 0x200u) ^ ((x << 8) & 0x800u);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ColorTileMode DecodeColorTileMode(std::uint32_t attrib3) {
|
||||
@@ -24,7 +34,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 == 0x1b, "AGC graphics: unsupported color tile mode");
|
||||
require(mode == 0 || mode == 5 || mode == 0x1b, "AGC graphics: unsupported color tile mode");
|
||||
return static_cast<ColorTileMode>(mode);
|
||||
}
|
||||
|
||||
@@ -59,6 +69,20 @@ 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 auto index = static_cast<std::size_t>(std::countr_zero(bytesPerElement));
|
||||
std::call_once(once[index], [&] {
|
||||
auto& table = tables[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);
|
||||
});
|
||||
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) {
|
||||
@@ -83,6 +107,17 @@ ColorTargetLayout::ColorTargetLayout(std::uint32_t width, std::uint32_t height,
|
||||
yOffsets = tables.y.data();
|
||||
break;
|
||||
}
|
||||
case ColorTileMode::Standard4KB: {
|
||||
const auto log2Bytes = static_cast<std::uint32_t>(std::countr_zero(bytesPerElement));
|
||||
blockWidth = 1u << (6u - (log2Bytes + 1u) / 2u);
|
||||
blockHeight = 1u << (6u - log2Bytes / 2u);
|
||||
pitch = (width + blockWidth - 1u) / blockWidth * blockWidth;
|
||||
paddedHeight = (height + blockHeight - 1u) / blockHeight * blockHeight;
|
||||
const auto& tables = standardTables(bytesPerElement, blockWidth, blockHeight);
|
||||
xOffsets = tables.x.data();
|
||||
yOffsets = tables.y.data();
|
||||
break;
|
||||
}
|
||||
default: throw std::runtime_error("AGC graphics: unsupported color tile mode");
|
||||
}
|
||||
const auto size = static_cast<std::uint64_t>(pitch) * paddedHeight * bytesPerElement;
|
||||
@@ -93,7 +128,7 @@ ColorTargetLayout::ColorTargetLayout(std::uint32_t width, std::uint32_t height,
|
||||
std::size_t ColorTargetLayout::offset(std::uint32_t x, std::uint32_t y) const {
|
||||
if (mode == ColorTileMode::Linear) return (static_cast<std::size_t>(y) * pitch + x) * elementBytes;
|
||||
const auto block = static_cast<std::size_t>(y / blockHeight) * (pitch / blockWidth) + x / blockWidth;
|
||||
return block * 65536u + (xOffsets[x % blockWidth] ^ yOffsets[y % blockHeight]);
|
||||
return block * Alignment() + (xOffsets[x % blockWidth] ^ yOffsets[y % blockHeight]);
|
||||
}
|
||||
|
||||
std::size_t ColorTargetLayout::Offset(std::uint32_t x, std::uint32_t y) const {
|
||||
|
||||
@@ -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, "only 64 KiB tiled color targets are resident");
|
||||
Require(color.tileMode == ColorTileMode::RenderTarget || color.tileMode == ColorTileMode::Standard4KB, "only 4 KiB standard and 64 KiB tiled color targets are resident");
|
||||
const bool chain = color.mipCount > 1;
|
||||
GuestTextureResource surface{};
|
||||
surface.baseAddress = chain ? color.surfaceAddress : color.address;
|
||||
@@ -53,7 +53,7 @@ GuestTextureResource SurfaceForTarget(const ColorTarget& color) {
|
||||
surface.mipCount = color.mipCount;
|
||||
surface.baseLevel = 0;
|
||||
surface.lastLevel = color.mipCount - 1;
|
||||
surface.tileMode = TextureTileMode::kR64KBX;
|
||||
surface.tileMode = ColorTextureTileMode(color.tileMode);
|
||||
surface.dimension = TextureDimension::k2D;
|
||||
surface.format = GuestFormatFor(color.format, color.elementBytes);
|
||||
surface.dstSelX = 4;
|
||||
@@ -1508,7 +1508,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 && residentTargets) {
|
||||
if ((binding.gpuTiling || (color.tileMode == ColorTileMode::Standard4KB && 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
|
||||
@@ -2074,7 +2074,7 @@ void RunColorMetadataPass(const Context& context, const ColorMetadataPass& pass)
|
||||
Require(IsDccClear(keys), std::string("CB metadata pass over DCC keys that are ") + DccKeysName(keys) + " (per-block metadata is not modeled)");
|
||||
const auto texel = clearTexel(color, keys);
|
||||
std::shared_ptr<StorageTexture> resident;
|
||||
if (color.tileMode == ColorTileMode::RenderTarget && context.detiler != nullptr) {
|
||||
if ((color.tileMode == ColorTileMode::RenderTarget || color.tileMode == ColorTileMode::Standard4KB) && context.detiler != nullptr) {
|
||||
try {
|
||||
resident = CachedStorageSurface(context, SurfaceForTarget(color));
|
||||
} catch (const std::exception&) {
|
||||
|
||||
@@ -619,7 +619,9 @@ ColorTarget DecodeColorBuffer(const Registers& cx, std::uint32_t slot) {
|
||||
if ((info & ~(0x00039f7cu | 0x00040000u | 0x10000000u)) != 0) throw std::runtime_error("AGC graphics: color compression, DCC, endian conversion, nonstandard rounding or color optimization is unsupported (CB_COLOR_INFO 0x" + [&] { char text[16]; std::snprintf(text, sizeof(text), "%08x", info); return std::string(text); }() + ")");
|
||||
Require((info & 0x8000u) != 0 || number == 7 || number == 4 || number == 5, "unclamped normalized color is unsupported");
|
||||
const auto view = read(cx, 0x31b + stride);
|
||||
Require((view & ~0x3c000000u) == 0, "color array views are unsupported");
|
||||
Require((view & ~0x3fffffffu) == 0, "reserved CB_COLOR_VIEW bits are set");
|
||||
const auto slice = view & 0x1fffu;
|
||||
Require(slice == ((view >> 13u) & 0x1fffu), "color views of several array slices are unsupported");
|
||||
const auto viewMip = (view >> 26u) & 0xfu;
|
||||
zero(cx, 0x31d + stride, ~0u, "color samples, fragments or destination alpha override");
|
||||
const auto attrib2 = read(cx, 0x3b0 + slot);
|
||||
@@ -635,7 +637,7 @@ ColorTarget DecodeColorBuffer(const Registers& cx, std::uint32_t slot) {
|
||||
color.mip = viewMip;
|
||||
if (maxMip != 0) {
|
||||
// A mipmapped surface is addressed like a texture; the view renders into one mip of it.
|
||||
const auto mips = ComputeElementMipLayout(color.tileMode == ColorTileMode::Linear ? TextureTileMode::kLinear : TextureTileMode::kR64KBX, color.elementBytes, color.extent.width, color.extent.height, maxMip + 1u);
|
||||
const auto mips = ComputeElementMipLayout(ColorTextureTileMode(color.tileMode), color.elementBytes, color.extent.width, color.extent.height, maxMip + 1u);
|
||||
const auto& mip = mips.at(viewMip);
|
||||
mipOffset = mip.tiledOffset;
|
||||
color.mipTail = mip.tail;
|
||||
@@ -645,6 +647,7 @@ ColorTarget DecodeColorBuffer(const Registers& cx, std::uint32_t slot) {
|
||||
const auto high = read(cx, 0x390 + slot);
|
||||
Require((high & ~0xffu) == 0, "invalid color address extension");
|
||||
color.surfaceAddress = (static_cast<std::uint64_t>(high) << 40u) | (static_cast<std::uint64_t>(read(cx, 0x318 + stride)) << 8u);
|
||||
if (slice != 0) color.surfaceAddress += slice * ComputeSurfaceSize(ComputeElementMipLayout(ColorTextureTileMode(color.tileMode), color.elementBytes, color.surfaceExtent.width, color.surfaceExtent.height, color.mipCount), 1);
|
||||
color.address = color.surfaceAddress + mipOffset;
|
||||
color.bytes = colorLayout.Bytes();
|
||||
GuestMemory::CheckRange(reinterpret_cast<const void*>(color.address), color.bytes, colorLayout.Alignment(), true);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "BdaTests.hpp"
|
||||
#include "prx/libSceAgcDriver/Graphics/include/ColorTargetTransfer.hpp"
|
||||
#include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
@@ -26,7 +27,7 @@ void RunColorTargetLayoutTests() {
|
||||
reject([] { DecodeColorTileMode(0x4dc6c001); });
|
||||
reject([] { DecodeColorTileMode(0x4dc6e000); });
|
||||
reject([] { DecodeColorTileMode(0xcdc6c000); });
|
||||
reject([] { DecodeColorTileMode(0x09014000); });
|
||||
reject([] { DecodeColorTileMode(0x09004000); });
|
||||
reject([] { ColorTargetLayout(0, 1, ColorTileMode::RenderTarget); });
|
||||
const ColorTargetLayout padded(63, 2, ColorTileMode::Linear);
|
||||
Require(padded.Bytes() == 512 && padded.LinearBytes() == 504 && padded.Offset(0, 1) == 256, "linear rows are not padded to 256 bytes");
|
||||
@@ -37,6 +38,18 @@ void RunColorTargetLayoutTests() {
|
||||
Require(layout.Offset(128, 0) == 65536 && layout.Offset(0, 128) == 3 * 65536, "block raster order is incorrect");
|
||||
Require(layout.Offset(16, 0) == 0x2200 && layout.Offset(0, 8) == 0x1100, "render-target XOR addressing is incorrect");
|
||||
Require(layout.Offset(256, 0) == 2 * 65536, "third block address is incorrect");
|
||||
Require(DecodeColorTileMode(0x4dc14000) == ColorTileMode::Standard4KB, "4 KiB standard color descriptor was rejected");
|
||||
const ColorTargetLayout standard(256, 256, ColorTileMode::Standard4KB);
|
||||
Require(standard.Bytes() == ComputeSurfaceSize(ComputeElementMipLayout(TextureTileMode::kStandard4KB, 4, 256, 256, 1), 1) && standard.Alignment() == 4096, "4 KiB standard color layout differs from the texture layout");
|
||||
Require(standard.Offset(32, 0) == 4096 && standard.Offset(0, 32) == 8 * 4096 && standard.Offset(1, 0) == 4 && standard.Offset(0, 1) == 16, "4 KiB standard block or element addressing is incorrect");
|
||||
std::vector<bool> standardSeen(1024);
|
||||
for (std::uint32_t y = 0; y < 32; ++y) {
|
||||
for (std::uint32_t x = 0; x < 32; ++x) {
|
||||
const auto address = standard.Offset(x, y);
|
||||
Require(address < 4096 && address % 4 == 0 && !standardSeen[address / 4], "4 KiB standard block aliases or leaves its texels");
|
||||
standardSeen[address / 4] = true;
|
||||
}
|
||||
}
|
||||
reject([&] { layout.Offset(257, 0); });
|
||||
std::vector<std::byte> tiled(layout.Bytes(), std::byte{0x5a});
|
||||
std::vector<std::byte> linear(layout.LinearBytes());
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace {
|
||||
using AgcDriver::Graphics::Require;
|
||||
|
||||
alignas(256) std::array<std::byte, 1024> colorMemory{};
|
||||
alignas(256) std::array<std::byte, 2048> sliceMemory{};
|
||||
|
||||
AgcDriver::QueueState makeState() {
|
||||
AgcDriver::QueueState queue;
|
||||
@@ -126,7 +127,7 @@ void stateTests() {
|
||||
queue.context.erase(0x3b8);
|
||||
expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "missing register");
|
||||
queue = makeState();
|
||||
queue.context[0x3b8] |= 5u << 14u;
|
||||
queue.context[0x3b8] |= 1u << 14u;
|
||||
expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "unsupported color tile mode");
|
||||
queue = makeState();
|
||||
queue.context[0x3b0] = (62u << 14u) | 3u;
|
||||
@@ -482,7 +483,13 @@ void DepthStencilTests() {
|
||||
queue.context[0x31b] = 1u << 26u;
|
||||
expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "mip exceeds");
|
||||
queue.context[0x31b] = 1u << 13u;
|
||||
expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "array views");
|
||||
expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "several array slices");
|
||||
const auto sliced = reinterpret_cast<std::uintptr_t>(sliceMemory.data());
|
||||
queue.context[0x318] = static_cast<std::uint32_t>(sliced >> 8u);
|
||||
queue.context[0x390] = static_cast<std::uint32_t>(sliced >> 40u);
|
||||
queue.context[0x31b] = 1u | (1u << 13u);
|
||||
const auto slice = AgcDriver::Graphics::DecodeState(queue);
|
||||
Require(slice.color.address == sliced + 1024u && slice.color.bytes == 1024u, "a color view of one slice did not move the target by one slice");
|
||||
}
|
||||
|
||||
void DepthBoundsBiasTests() {
|
||||
|
||||
Reference in New Issue
Block a user