From 25063d71f6897d5d73d23ee1dccc2e2f2848509c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 08:19:40 +0400 Subject: [PATCH 1/2] feat(agc): render PS4 color views of several slices through the vertex render target index A single-sample CB_COLOR_VIEW of several slices with PA_CL_VS_OUT_CNTL USE_VTX_RENDER_TARGET_INDX now decodes as a layered view of the array at CB_COLOR_BASE and draws through its resident array image, each layer placed with its own GCN bank rotation. Multisampled views still throw. Co-Authored-By: Claude Opus 5.5 --- .../prx/libSceAgcDriver/Graphics/src/Draw.cpp | 2 +- .../libSceAgcDriver/Graphics/src/State.cpp | 10 +- .../prx/libSceAgcDriver/tests/Graphics.cpp | 6 +- .../tests/execution/GcnLayeredColorTarget.cpp | 326 ++++++++++++++++++ .../changes/claude-ps4-gcn-layered-color.md | 5 + 5 files changed, 346 insertions(+), 3 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/GcnLayeredColorTarget.cpp create mode 100644 docs/dev/debt/changes/claude-ps4-gcn-layered-color.md diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp index 70d9d9146ffa..5ed627e9470f 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp @@ -4075,7 +4075,7 @@ bool prepareDraw(const Context& context, const State& state, const Pm4::DrawPara Require(color.dccAddress == 0 && color.cmaskAddress == 0 && AttachmentProxyFormat(context, color.format) == VK_FORMAT_UNDEFINED, "a slice of a 2D array color target with DCC, CMASK or an attachment proxy has no resident array image"); const auto array = ArraySurfaceForTarget(color); // DecodeColorBuffer offsets slice s by s times this size; every slice the image stores must agree. - const auto sliceBytes = ComputeSurfaceSize(ComputeElementMipLayout(ColorTextureTileMode(color.tileMode), color.elementBytes, color.surfaceExtent.width, color.surfaceExtent.height, color.mipCount), 1); + const auto sliceBytes = ColorTileModeIsGcn(color.tileMode) ? static_cast(color.bytes) : ComputeSurfaceSize(ComputeElementMipLayout(ColorTextureTileMode(color.tileMode), color.elementBytes, color.surfaceExtent.width, color.surfaceExtent.height, color.mipCount), 1); const auto geometry = DescribeSurface(array); for (std::uint32_t slice = 0; slice <= color.arrayLastSlice; ++slice) Require(geometry.GuestLayerOffset(slice) == slice * sliceBytes, "the array image's layer pitch differs from the color view's slice offset"); Require(color.surfaceAddress == color.arrayAddress + color.arraySlice * sliceBytes, "the array image's layer pitch differs from the color view's slice offset"); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp index 3849a708c1da..a4d33c4a7fea 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp @@ -2461,7 +2461,8 @@ ColorTarget DecodeGcnColorBuffer(const Registers& cx, std::uint32_t slot) { const auto lastSlice = (view >> 13u) & 0x7ffu; Require(lastSlice >= slice, "the color view ends before its first slice"); const auto exports = find(cx, 0x207); - Require(lastSlice == slice || exports == cx.end() || (exports->second & (1u << 18u)) == 0, "GCN layered rendering into several array slices through the vertex render target index is not implemented"); + const bool layered = lastSlice != slice && exports != cx.end() && (exports->second & (1u << 18u)) != 0; + Require(!layered || color.coverageSamples == 1u, "GCN layered rendering into several slices of a multisampled color array through the vertex render target index is not implemented"); const auto pitch = ((read(cx, 0x319 + stride) & 0x7ffu) + 1u) * 8u; const auto sliceElements = ((read(cx, 0x31a + stride) & 0x3fffffu) + 1u) * 64u; color.extent = {pitch, sliceElements / pitch}; @@ -2484,6 +2485,12 @@ ColorTarget DecodeGcnColorBuffer(const Registers& cx, std::uint32_t slot) { color.surfaceAddress = (static_cast(read(cx, 0x318 + stride)) << 8u) + static_cast(slice) * layout.Bytes() * fragments; color.address = color.surfaceAddress; color.bytes = layout.Bytes(); + if (layered) { + color.arrayAddress = static_cast(read(cx, 0x318 + stride)) << 8u; + color.arraySlice = slice; + color.arrayLastSlice = lastSlice; + color.layers = lastSlice + 1u - slice; + } color.format = decoded.format; color.componentMapping = decoded.componentMapping; color.packing = decoded.packing; @@ -2519,6 +2526,7 @@ GuestTextureResource ArraySurfaceForTarget(const ColorTarget& color) { surface.height = color.surfaceExtent.height; surface.depthOrLastArray = color.arrayLastSlice; surface.dimension = TextureDimension::k2DArray; + if (ColorTileModeIsGcn(color.tileMode)) surface.pipeBankXor = 0; return surface; } diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index 3fe29cdc5044..38e1878ed01b 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -4011,7 +4011,11 @@ void gcnColorSliceTests() { queue.context[0x31b] = 1u | (2u << 13u); Require(DecodeColorBuffer(queue.context, 0).address == base + GcnSliceBytes, "a GCN color view of several slices decoded without PA_CL_VS_OUT_CNTL does not draw into its first slice"); queue.context[0x207] = 1u << 18u; - expectFailure([&] { static_cast(DecodeColorBuffer(queue.context, 0)); }, "GCN layered rendering into several array slices through the vertex render target index is not implemented"); + const auto layered = DecodeColorBuffer(queue.context, 0); + Require(layered.address == base + GcnSliceBytes && layered.bytes == GcnSliceBytes && layered.layers == 2u && layered.arrayAddress == base && layered.arraySlice == 1u && layered.arrayLastSlice == 2u, "a GCN color view of slices 1 to 2 with a vertex render target index does not decode as two layers of the array at CB_COLOR_BASE"); + queue.context[0x31d] = 5u | (1u << 12u) | (1u << 15u); + expectFailure([&] { static_cast(DecodeColorBuffer(queue.context, 0)); }, "GCN layered rendering into several slices of a multisampled color array through the vertex render target index is not implemented"); + queue.context[0x31d] = 5u; queue.context[0x31b] = 1u | (1u << 13u); Require(DecodeColorBuffer(queue.context, 0).address == base + GcnSliceBytes, "a GCN color view of one slice is refused for a vertex render target index"); queue.context[0x207] = 0; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/GcnLayeredColorTarget.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/GcnLayeredColorTarget.cpp new file mode 100644 index 000000000000..ccb96baf9a76 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/GcnLayeredColorTarget.cpp @@ -0,0 +1,326 @@ +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "prx/libSceAgcDriver/Graphics/include/State.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Texture.hpp" +#include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" +#include "prx/libc/include/GuestAllocations.hpp" +#include "prx/libc/include/GuestArena.hpp" +#include "prx/libc/include/GuestWriteWatch.hpp" +#include "Recompiler.hpp" +#include "RdnaDecoder/RdnaInstructionDecoder.hpp" +#include "VulkanTestDevice.hpp" +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; +using Triangle = std::array, 3>; + +constexpr std::uint32_t Width = 128; +constexpr std::uint32_t Height = 64; +constexpr std::uint32_t Slices = 4; +constexpr std::uint32_t FirstSlice = 1; +constexpr std::uint32_t LastSlice = 3; +constexpr std::uint32_t ElementBytes = 4; +constexpr std::uint32_t Format8888 = 10u; +constexpr std::uint32_t BlendClamp = 1u << 15u; +constexpr std::uint32_t LayerExports = (1u << 18u) | (1u << 21u); +constexpr std::size_t SliceBytes = std::size_t{Width} * Height * ElementBytes; +constexpr std::size_t ArrayBytes = SliceBytes * Slices; +constexpr std::size_t ArrayStride = 0x20000; +constexpr std::size_t Modes = 2; +constexpr std::size_t Vertices = Modes * ArrayStride; +constexpr std::size_t BlockBytes = Vertices + 0x10000; +constexpr std::uint64_t GcnAddressLimit = std::uint64_t{1} << 40u; +constexpr std::array DrawnLayers{0u, 2u}; + +alignas(256) constexpr std::array VertexCode{ + 0xe0382000u, 0x80000000u, 0xbf8c0f70u, 0xf80000cfu, 0x03020100u, + 0x7e080204u, 0x7e0a0280u, 0xf80008dfu, 0x05040505u, 0xbf810000u, +}; + +constexpr Triangle Lower{{{-1.0f, -1.0f, 0.5f, 1.0f}, {0.9f, -1.0f, 0.5f, 1.0f}, {-1.0f, 0.7f, 0.5f, 1.0f}}}; + +std::uint8_t DrawnBlue(std::uint32_t slice) { + return static_cast(16u + 16u * slice); +} + +alignas(256) std::array, DrawnLayers.size()> Programs{}; + +std::span Program(std::size_t index, std::uint32_t slice) { + constexpr std::uint32_t inverse255 = 0x3b808081u; + auto& code = Programs.at(index); + code = { + 0x7e084900u, 0x7e0a4901u, + 0x100808ffu, inverse255, 0x100a0affu, inverse255, + 0x7e0c02ffu, std::bit_cast(static_cast(DrawnBlue(slice)) / 255.0f), + 0x7e0e02f2u, + 0xf800180fu, 0x07060504u, + 0xbf810000u, + }; + return std::span(code.data(), 12); +} + +class LowBlock { +public: + LowBlock() { +#if defined(_WIN32) || defined(__APPLE__) + data = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(BlockBytes, 65536)); +#ifdef _WIN32 + if (data != nullptr) GuestArena::GuestArenaCommit_nid_postfix(data, BlockBytes, PAGE_READWRITE, BlockBytes); +#else + constexpr std::uint32_t ReadWriteProtection = 0x04; + if (data != nullptr) GuestArena::GuestArenaCommit_nid_postfix(data, BlockBytes, ReadWriteProtection, BlockBytes); +#endif +#else +#ifdef ANYPS5_ENABLE_ASAN + constexpr std::uintptr_t FirstHint = 0x60000000; +#else + constexpr std::uintptr_t FirstHint = std::uintptr_t{1} << 32u; +#endif + for (std::uintptr_t hint = FirstHint; data == nullptr && hint < GcnAddressLimit; hint += std::uintptr_t{1} << 32u) { + void* mapped = mmap(reinterpret_cast(hint), BlockBytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0); + if (mapped != MAP_FAILED) data = static_cast(mapped); + } + if (data != nullptr && AgcDriver::GuestMemory::WriteWatched()) GuestWriteWatch::GuestWriteWatchRegister_nid_postfix(data, BlockBytes); +#endif + if (data == nullptr) return; + std::memset(data, 0, BlockBytes); + GuestAllocations::Mutation().Add(data, BlockBytes, true, true, true); + AgcDriver::GuestMemory::CollectWritesUncached(Address(), BlockBytes); + } + + ~LowBlock() { + if (data == nullptr) return; + AgcDriver::Graphics::StorageTexture::FlushPending(Address(), BlockBytes, nullptr, "test release"); + GuestAllocations::Mutation().Remove(data); +#if defined(_WIN32) || defined(__APPLE__) + GuestArena::GuestArenaReset_nid_postfix(data, BlockBytes); + GuestArena::GuestArenaRelease_nid_postfix(data, BlockBytes); +#else + munmap(data, BlockBytes); + if (AgcDriver::GuestMemory::WriteWatched()) GuestWriteWatch::GuestWriteWatchUnregister_nid_postfix(data, BlockBytes); +#endif + } + + LowBlock(const LowBlock&) = delete; + LowBlock& operator=(const LowBlock&) = delete; + + bool Usable() const { return data != nullptr && Address() + BlockBytes <= GcnAddressLimit; } + std::uint64_t Address() const { return static_cast(reinterpret_cast(data)); } + std::uint64_t At(std::size_t offset) const { return Address() + offset; } + std::uint8_t* Data(std::size_t offset) const { return data + offset; } + +private: + std::uint8_t* data = nullptr; +}; + +struct ArrayLayout { + AgcDriver::Graphics::GcnTileGeometry geometry; + AgcDriver::Graphics::SurfaceGeometry surface; +}; + +ArrayLayout TexturePlacement(AgcDriver::Graphics::TextureTileMode mode, std::uint64_t base) { + AgcDriver::Graphics::GuestTextureResource array{}; + array.baseAddress = base; + array.width = Width; + array.height = Height; + array.depthOrLastArray = Slices - 1u; + array.mipCount = 1u; + array.allocatedMipCount = 1u; + array.tileMode = mode; + array.dimension = AgcDriver::Graphics::TextureDimension::k2DArray; + array.format = 56u; + array.gcnPitch = Width; + ArrayLayout layout{AgcDriver::Graphics::GcnTiling(mode, ElementBytes), AgcDriver::Graphics::DescribeSurface(array)}; + Require(layout.surface.layers == Slices && layout.surface.guestBytes == ArrayBytes, "the texture path does not see the GCN array as four whole slices"); + return layout; +} + +std::size_t TextureOffset(const ArrayLayout& layout, std::uint32_t slice, std::uint32_t x, std::uint32_t y) { + const auto& geometry = layout.geometry; + const auto& mip = layout.surface.mips[0]; + std::uint32_t inBlock = 0; + for (std::uint32_t bit = 0; bit < 16u; ++bit) inBlock |= static_cast(std::popcount((x & geometry.equation[bit] & 0xfffu) ^ ((y << 12u) & geometry.equation[bit] & 0xfff000u)) & 1) << bit; + const auto block = static_cast(y / geometry.blockHeight) * mip.blocksPerRow + x / geometry.blockWidth; + return static_cast(layout.surface.GuestOffset(slice, mip) + block * geometry.blockBytes + (inBlock ^ AgcDriver::Graphics::GcnLayerBankXor(geometry, slice))); +} + +ShaderRecompiler::ShaderPixelStageInfo PixelInfo() { + ShaderRecompiler::ShaderPixelStageInfo pixel{}; + pixel.inputAddr = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionX) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionY); + pixel.posX = true; + pixel.posY = true; + pixel.targetOutputMode[0] = 9; + pixel.targetExportMapping.fill(0xe4u); + return pixel; +} + +void Draw(AgcDriver::VulkanDevice& device, const LowBlock& block, const AgcDriver::Graphics::ColorTarget& color, std::uint32_t layer, std::span pixelCode) { + const auto target = device.Target(); + std::memcpy(block.Data(Vertices), Lower.data(), sizeof(Lower)); + const auto address = block.At(Vertices); + const std::vector vertexUserData{static_cast(address), static_cast((address >> 32u) & 0xffffu) | (16u << 16u), static_cast(sizeof(Lower)), 0xfacu | (7u << 12u) | (14u << 15u), layer}; + ShaderRecompiler::ShaderVertexStageInfo vertexInfo{}; + vertexInfo.paClVsOutCntl = LayerExports; + const std::array vertexMemory{{{reinterpret_cast(VertexCode.data()), std::as_bytes(std::span(VertexCode))}}}; + ShaderRecompiler::RecompileRequest vertex{ + {ShaderStage::Vertex, reinterpret_cast(VertexCode.data()), VertexCode, 0, {}}, + {64u, 0, vertexUserData, std::nullopt, std::nullopt, vertexInfo, vertexMemory}, + target, + {0, 0, 0, 64} + }; + vertex.useCache = false; + const auto vertexResult = ShaderRecompiler::Recompile(vertex); + const auto vertexPush = static_cast(vertexResult.pushConstants.size()); + const std::vector pixelUserData(8, 0u); + const std::array pixelMemory{{{reinterpret_cast(pixelCode.data()), std::as_bytes(pixelCode)}}}; + ShaderRecompiler::RecompileRequest fragment{ + {ShaderStage::Fragment, reinterpret_cast(pixelCode.data()), pixelCode, 0, {}}, + {64u, 0, pixelUserData, std::nullopt, PixelInfo(), std::nullopt, pixelMemory}, + target, + PixelPushLayout(vertexPush, target) + }; + fragment.useCache = false; + const auto pixelResult = ShaderRecompiler::Recompile(fragment); + const std::array shaders{{ + {ShaderStage::Vertex, &vertexResult, 0}, + {ShaderStage::Fragment, &pixelResult, PixelPushOffset(vertexPush, target)} + }}; + AgcDriver::Graphics::State state{}; + state.stages = {AgcDriver::Graphics::ShaderPath::Vertex, 0u, 64u, 64u, std::nullopt, std::nullopt}; + state.colors = {color}; + state.color = color; + state.hasColorTarget = true; + state.renderExtent = {Width, Height}; + state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + state.viewport = {0, static_cast(Height), static_cast(Width), -static_cast(Height), 0, 1}; + state.negativeOneToOne = false; + state.scissor = {{0, 0}, {Width, Height}}; + state.cullMode = VK_CULL_MODE_NONE; + state.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; + VkPipelineColorBlendAttachmentState blend{}; + blend.colorWriteMask = 0xf; + state.blends = {blend}; + state.blend = blend; + state.blendConstants = {}; + state.samples = 1u; + state.renderLayers = color.layers; + const AgcDriver::Pm4::DrawParameters draw{0, static_cast(Lower.size()), 0, 1, 0, false}; + device.Draw(state, draw, shaders); + device.WaitIdle(); +} + +std::array Background(std::uint32_t slice, std::uint32_t x, std::uint32_t y) { + return {static_cast(x), static_cast(y), static_cast(200u + slice), 0u}; +} + +std::array Drawn(std::uint32_t slice, std::uint32_t x, std::uint32_t y) { + return {static_cast(x), static_cast(y), DrawnBlue(slice), 255u}; +} + +AgcDriver::Graphics::ColorTarget DecodeLayeredView(std::uint64_t base, std::uint32_t tileIndex) { + AgcDriver::Registers cx; + cx[0x207] = LayerExports; + cx[0x318] = static_cast(base >> 8u); + cx[0x319] = Width / 8u - 1u; + cx[0x31a] = Width * Height / 64u - 1u; + cx[0x31b] = FirstSlice | (LastSlice << 13u); + cx[0x31c] = (Format8888 << 2u) | BlendClamp; + cx[0x31d] = tileIndex; + const auto target = AgcDriver::Graphics::DecodeColorBuffer(cx, 0); + Require(target.format == VK_FORMAT_R8G8B8A8_UNORM && target.samples == 1u && target.layers == LastSlice + 1u - FirstSlice && target.arraySlice == FirstSlice && target.arrayLastSlice == LastSlice && target.arrayAddress == base && target.address == base + FirstSlice * SliceBytes && target.bytes == SliceBytes, "the GCN view of slices 1 to 3 with a vertex render target index did not decode as a three-layer view of the array at its base"); + return target; +} + +std::string Describe(const char* mode, std::uint32_t slice, std::uint32_t x, std::uint32_t y, const std::uint8_t* texel) { + char text[192]; + std::snprintf(text, sizeof(text), "%s slice %u: the texel the texture path places at (%u, %u) holds %02x %02x %02x %02x", mode, slice, x, y, texel[0], texel[1], texel[2], texel[3]); + return text; +} + +std::size_t CheckArray(AgcDriver::VulkanDevice& device, const LowBlock& block, std::size_t offset, std::uint32_t tileIndex, AgcDriver::Graphics::TextureTileMode mode, const char* name) { + const auto base = block.At(offset); + const auto layout = TexturePlacement(mode, base); + std::vector guest(ArrayBytes); + for (std::uint32_t slice = 0; slice < Slices; ++slice) { + for (std::uint32_t y = 0; y < Height; ++y) { + for (std::uint32_t x = 0; x < Width; ++x) std::memcpy(guest.data() + TextureOffset(layout, slice, x, y), Background(slice, x, y).data(), ElementBytes); + } + } + AgcDriver::GuestMemory::Write(base, std::as_bytes(std::span(guest)), 256); + const auto target = DecodeLayeredView(base, tileIndex); + for (std::size_t index = 0; index < DrawnLayers.size(); ++index) Draw(device, block, target, DrawnLayers[index], Program(index, FirstSlice + DrawnLayers[index])); + AgcDriver::Graphics::StorageTexture::FlushPending(base, ArrayBytes, nullptr, "test read-back"); + device.WaitIdle(); + AgcDriver::GuestMemory::Read(base, std::as_writable_bytes(std::span(guest)), 256); + std::size_t drawnTotal = 0; + for (std::uint32_t slice = 0; slice < Slices; ++slice) { + const bool drawnSlice = slice >= FirstSlice && std::ranges::find(DrawnLayers, slice - FirstSlice) != DrawnLayers.end(); + std::size_t drawn = 0; + for (std::uint32_t y = 0; y < Height; ++y) { + for (std::uint32_t x = 0; x < Width; ++x) { + const auto* texel = guest.data() + TextureOffset(layout, slice, x, y); + const bool kept = std::memcmp(texel, Background(slice, x, y).data(), ElementBytes) == 0; + const bool written = drawnSlice && std::memcmp(texel, Drawn(slice, x, y).data(), ElementBytes) == 0; + Require(kept || written, Describe(name, slice, x, y, texel) + (drawnSlice ? ", neither its background nor the color drawn for it" : ", which no layer index chose, but not its background")); + drawn += written ? 1u : 0u; + } + } + const auto texels = std::size_t{Width} * Height; + Require(!drawnSlice || (drawn > texels / 4u && drawn < texels * 3u / 4u), std::string(name) + " slice " + std::to_string(slice) + ": the triangle covers " + std::to_string(drawn) + " of " + std::to_string(texels) + " texels"); + drawnTotal += drawn; + } + return drawnTotal; +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + ShaderRecompiler::SetGcnGuestShaders(true); + const LowBlock block; + if (!block.Usable()) { + std::printf("skipped, no guest block below the 40-bit GCN color base range (0x%llx)\n", static_cast(block.Address())); + return VulkanTestSkipped; + } + struct Mode { std::uint32_t index; AgcDriver::Graphics::TextureTileMode mode; const char* name; }; + constexpr std::array modes{{ + {13u, AgcDriver::Graphics::TextureTileMode::kGcnThin1D, "Thin1D"}, + {14u, AgcDriver::Graphics::TextureTileMode::kGcnThin2D, "Thin2D"}, + }}; + std::string counts; + for (std::size_t index = 0; index < modes.size(); ++index) { + const auto drawn = CheckArray(*device, block, index * ArrayStride, modes[index].index, modes[index].mode, modes[index].name); + counts += (index == 0 ? "" : ", ") + std::to_string(drawn) + " " + modes[index].name; + } + std::printf("GCN layered color target tests passed: in a view of slices 1 to 3 of four-slice thin 1D and 2D arrays, draws exporting render target index 0 and 2 land in slices 1 and 3 where the texture path places those layers (%s texels), and slices 0 and 2 keep their background\n", counts.c_str()); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/docs/dev/debt/changes/claude-ps4-gcn-layered-color.md b/docs/dev/debt/changes/claude-ps4-gcn-layered-color.md new file mode 100644 index 000000000000..e14bb8104630 --- /dev/null +++ b/docs/dev/debt/changes/claude-ps4-gcn-layered-color.md @@ -0,0 +1,5 @@ +# libSceAgcDriver + +## change [PS4 color array slices] + +- [PS4 color array slices](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver, PS4) - not checked on a console. A `CB_COLOR_VIEW` of one slice renders into that slice, at the surface address plus the slice index times the slice size. A view of several slices renders into its first slice when `PA_CL_VS_OUT_CNTL` bit 18 (USE_VTX_RENDER_TARGET_INDX) is clear; with it set, a single-sample view renders into the slice the vertex render target index selects, counted from the slice start, through the array's resident image (see Layered color targets), and a multisampled one throws, as does a slice max below the slice start. Bits [12:11] and [31:24] are ignored: shadPS4 defines no fields there. Later slices of `GcnDisplay2D` and `GcnThin2D` take the bank rotation the texture path gives that layer (see PS4 (GCN) array layer banks), and such a single-slice view of a multisampled target throws. From 45304973fcc0fda0d1734f4bdbf9084a1a8cf30c Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 01:18:20 +0200 Subject: [PATCH 2/2] feat(agc): draw PS4 thick 1D color targets as 3D volumes Co-Authored-By: Claude Opus 5.5 --- .../Graphics/include/ColorTargetLayout.hpp | 7 +- .../Graphics/src/ColorTargetLayout.cpp | 11 + .../libSceAgcDriver/Graphics/src/State.cpp | 13 +- .../tests/execution/GcnThickColorTarget.cpp | 341 ++++++++++++++++++ .../feat-agc-ps4-thick-color-target.md | 5 + 5 files changed, 374 insertions(+), 3 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/GcnThickColorTarget.cpp create mode 100644 docs/dev/debt/changes/feat-agc-ps4-thick-color-target.md diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp index a17ca06c7824..36133b0bf674 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp @@ -26,11 +26,12 @@ enum class ColorTileMode : std::uint32_t { GcnDisplay1D = 0x101, GcnDisplay2D = 0x102, GcnThin2D = 0x103, - GcnLinear = 0x104 + GcnLinear = 0x104, + GcnThick1D = 0x10a }; constexpr bool ColorTileModeIsGcn(ColorTileMode mode) { - return mode == ColorTileMode::GcnThin1D || mode == ColorTileMode::GcnDisplay1D || mode == ColorTileMode::GcnDisplay2D || mode == ColorTileMode::GcnThin2D || mode == ColorTileMode::GcnLinear; + return mode == ColorTileMode::GcnThin1D || mode == ColorTileMode::GcnDisplay1D || mode == ColorTileMode::GcnDisplay2D || mode == ColorTileMode::GcnThin2D || mode == ColorTileMode::GcnLinear || mode == ColorTileMode::GcnThick1D; } constexpr bool ColorTileModeIsLinear(ColorTileMode mode) { @@ -50,6 +51,7 @@ constexpr TextureTileMode ColorTextureTileMode(ColorTileMode mode) { if (mode == ColorTileMode::GcnDisplay1D) return TextureTileMode::kGcnDisplay1D; if (mode == ColorTileMode::GcnDisplay2D) return TextureTileMode::kGcnDisplay2D; if (mode == ColorTileMode::GcnThin2D) return TextureTileMode::kGcnThin2D; + if (mode == ColorTileMode::GcnThick1D) return TextureTileMode::kGcnThick1D; return ColorTileModeIsLinear(mode) ? TextureTileMode::kLinear : mode == ColorTileMode::Standard4KB ? TextureTileMode::kStandard4KB : mode == ColorTileMode::Standard64KB ? TextureTileMode::kStandard64KB : mode == ColorTileMode::S4KBX ? TextureTileMode::kS4KBX : mode == ColorTileMode::D4KBX ? TextureTileMode::kD4KBX : mode == ColorTileMode::D64KBX ? TextureTileMode::kD64KBX : mode == ColorTileMode::Z64KBX ? TextureTileMode::kZ64KBX : TextureTileMode::kR64KBX; } @@ -80,6 +82,7 @@ class ColorTargetLayout { std::size_t blockBytes = 0; std::uint32_t pipeBankXor = 0; bool gcn = false; + bool thick = false; std::array gcnEquation{}; // The XOR swizzle is linear over GF(2), so a block offset is xOffsets[x] ^ yOffsets[y]. const std::uint32_t* xOffsets = nullptr; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp index 83715e01bbe8..f8f4fdea78a2 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp @@ -176,6 +176,16 @@ ColorTargetLayout::ColorTargetLayout(std::uint32_t width, std::uint32_t height, gcnEquation = geometry.equation; break; } + case ColorTileMode::GcnThick1D: { + require(pipeBankXor == 0, "AGC graphics: a PS4 thick color target has no bank XOR"); + blockWidth = 8u; + blockHeight = 8u; + pitch = (width + blockWidth - 1u) / blockWidth * blockWidth; + paddedHeight = (height + blockHeight - 1u) / blockHeight * blockHeight; + gcn = true; + thick = true; + break; + } default: throw std::runtime_error("AGC graphics: unsupported color tile mode"); } blockBytes = gcn ? static_cast(blockWidth) * blockHeight * bytesPerElement : ColorTileModeBlockBytes(mode); @@ -185,6 +195,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 { + require(!thick, "AGC graphics: a PS4 thick color target interleaves four slices per micro tile and is addressed through its 3D surface, not a 2D layout"); if (ColorTileModeIsLinear(mode)) return (static_cast(y) * pitch + x) * elementBytes; const auto block = static_cast(y / blockHeight) * (pitch / blockWidth) + x / blockWidth; if (gcn) { diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp index b5a993750a86..ab754edd5ca7 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp @@ -2551,8 +2551,13 @@ ColorTarget DecodeGcnColorBuffer(const Registers& cx, std::uint32_t slot) { case 9u: color.tileMode = ColorTileMode::GcnDisplay1D; break; case 10u: color.tileMode = ColorTileMode::GcnDisplay2D; break; case 14u: color.tileMode = ColorTileMode::GcnThin2D; break; + case 19u: color.tileMode = ColorTileMode::GcnThick1D; break; default: throw std::runtime_error("AGC graphics: PS4 color target tile mode index " + std::to_string(attrib & 0x1fu) + " is not implemented"); } + const bool thick = color.tileMode == ColorTileMode::GcnThick1D; + Require(!thick || (color.coverageSamples == 1u && fragments == 1u), "a multisampled PS4 thick color target is not implemented"); + Require(!thick || slice == 0u, "a PS4 thick color view that starts past slice 0 is not implemented"); + Require(!thick || (info & 0x6000u) == 0u, "a fast-cleared or compressed PS4 thick color target (CMASK or FMASK) is not implemented"); const bool macroTiled = color.tileMode == ColorTileMode::GcnDisplay2D || color.tileMode == ColorTileMode::GcnThin2D; Require(slice == 0 || !macroTiled || color.samples == 1u, "GCN multisampled color views of a later slice of a 2D-tiled array are not implemented"); if (macroTiled) color.pipeBankXor = GcnLayerBankXor(GcnTiling(ColorTextureTileMode(color.tileMode), color.elementBytes), slice); @@ -2562,7 +2567,7 @@ ColorTarget DecodeGcnColorBuffer(const Registers& cx, std::uint32_t slot) { color.surfaceAddress = (static_cast(read(cx, 0x318 + stride)) << 8u) + static_cast(slice) * layout.Bytes() * fragments; color.address = color.surfaceAddress; color.bytes = layout.Bytes(); - if (layered) { + if (layered && !thick) { color.arrayAddress = static_cast(read(cx, 0x318 + stride)) << 8u; color.arraySlice = slice; color.arrayLastSlice = lastSlice; @@ -2571,6 +2576,12 @@ ColorTarget DecodeGcnColorBuffer(const Registers& cx, std::uint32_t slot) { color.format = decoded.format; color.componentMapping = decoded.componentMapping; color.packing = decoded.packing; + if (thick) { + color.volume = true; + color.depth = lastSlice + 1u; + color.layers = layered ? color.depth : 1u; + color.bytes = SurfaceGuestBytes(SurfaceForTarget(color)); + } for (std::uint32_t word = 0; word < 2; ++word) { const auto clear = find(cx, 0x323 + word + stride); color.clearWords[word] = clear == cx.end() ? 0u : clear->second; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/GcnThickColorTarget.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/GcnThickColorTarget.cpp new file mode 100644 index 000000000000..6868353ce919 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/GcnThickColorTarget.cpp @@ -0,0 +1,341 @@ +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "prx/libSceAgcDriver/Graphics/include/State.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Texture.hpp" +#include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" +#include "prx/libc/include/GuestAllocations.hpp" +#include "prx/libc/include/GuestArena.hpp" +#include "prx/libc/include/GuestWriteWatch.hpp" +#include "Recompiler.hpp" +#include "RdnaDecoder/RdnaInstructionDecoder.hpp" +#include "VulkanTestDevice.hpp" +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; +using Triangle = std::array, 3>; + +constexpr std::uint32_t Width = 32; +constexpr std::uint32_t Height = 32; +constexpr std::uint32_t Depth = 8; +constexpr std::uint32_t ElementBytes = 4; +constexpr std::uint32_t Format8888 = 10u; +constexpr std::uint32_t BlendClamp = 1u << 15u; +constexpr std::uint32_t ThickTileIndex = 19u; +constexpr std::uint32_t LayerExports = (1u << 18u) | (1u << 21u); +constexpr std::size_t SliceBytes = std::size_t{Width} * Height * ElementBytes; +constexpr std::size_t VolumeBytes = SliceBytes * Depth; +constexpr std::size_t Vertices = 0x10000; +constexpr std::size_t BlockBytes = Vertices + 0x10000; +constexpr std::uint64_t GcnAddressLimit = std::uint64_t{1} << 40u; +constexpr std::array DrawnSlices{1u, 2u, 6u}; + +alignas(256) constexpr std::array VertexCode{ + 0xe0382000u, 0x80000000u, 0xbf8c0f70u, 0xf80000cfu, 0x03020100u, + 0x7e080204u, 0x7e0a0280u, 0xf80008dfu, 0x05040505u, 0xbf810000u, +}; + +constexpr Triangle Lower{{{-1.0f, -1.0f, 0.5f, 1.0f}, {0.9f, -1.0f, 0.5f, 1.0f}, {-1.0f, 0.7f, 0.5f, 1.0f}}}; + +std::uint8_t DrawnBlue(std::uint32_t slice) { + return static_cast(16u + 16u * slice); +} + +alignas(256) std::array, DrawnSlices.size()> Programs{}; + +std::span Program(std::size_t index, std::uint32_t slice) { + constexpr std::uint32_t inverse255 = 0x3b808081u; + auto& code = Programs.at(index); + code = { + 0x7e084900u, 0x7e0a4901u, + 0x100808ffu, inverse255, 0x100a0affu, inverse255, + 0x7e0c02ffu, std::bit_cast(static_cast(DrawnBlue(slice)) / 255.0f), + 0x7e0e02f2u, + 0xf800180fu, 0x07060504u, + 0xbf810000u, + }; + return std::span(code.data(), 12); +} + +class LowBlock { +public: + LowBlock() { +#if defined(_WIN32) || defined(__APPLE__) + data = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(BlockBytes, 65536)); +#ifdef _WIN32 + if (data != nullptr) GuestArena::GuestArenaCommit_nid_postfix(data, BlockBytes, PAGE_READWRITE, BlockBytes); +#else + constexpr std::uint32_t ReadWriteProtection = 0x04; + if (data != nullptr) GuestArena::GuestArenaCommit_nid_postfix(data, BlockBytes, ReadWriteProtection, BlockBytes); +#endif +#else +#ifdef ANYPS5_ENABLE_ASAN + constexpr std::uintptr_t FirstHint = 0x60000000; +#else + constexpr std::uintptr_t FirstHint = std::uintptr_t{1} << 32u; +#endif + for (std::uintptr_t hint = FirstHint; data == nullptr && hint < GcnAddressLimit; hint += std::uintptr_t{1} << 32u) { + void* mapped = mmap(reinterpret_cast(hint), BlockBytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0); + if (mapped != MAP_FAILED) data = static_cast(mapped); + } + if (data != nullptr && AgcDriver::GuestMemory::WriteWatched()) GuestWriteWatch::GuestWriteWatchRegister_nid_postfix(data, BlockBytes); +#endif + if (data == nullptr) return; + std::memset(data, 0, BlockBytes); + GuestAllocations::Mutation().Add(data, BlockBytes, true, true, true); + AgcDriver::GuestMemory::CollectWritesUncached(Address(), BlockBytes); + } + + ~LowBlock() { + if (data == nullptr) return; + AgcDriver::Graphics::StorageTexture::FlushPending(Address(), BlockBytes, nullptr, "test release"); + GuestAllocations::Mutation().Remove(data); +#if defined(_WIN32) || defined(__APPLE__) + GuestArena::GuestArenaReset_nid_postfix(data, BlockBytes); + GuestArena::GuestArenaRelease_nid_postfix(data, BlockBytes); +#else + munmap(data, BlockBytes); + if (AgcDriver::GuestMemory::WriteWatched()) GuestWriteWatch::GuestWriteWatchUnregister_nid_postfix(data, BlockBytes); +#endif + } + + LowBlock(const LowBlock&) = delete; + LowBlock& operator=(const LowBlock&) = delete; + + bool Usable() const { return data != nullptr && Address() + BlockBytes <= GcnAddressLimit; } + std::uint64_t Address() const { return static_cast(reinterpret_cast(data)); } + std::uint64_t At(std::size_t offset) const { return Address() + offset; } + std::uint8_t* Data(std::size_t offset) const { return data + offset; } + +private: + std::uint8_t* data = nullptr; +}; + +std::size_t ThickOffset(std::uint32_t x, std::uint32_t y, std::uint32_t z) { + const auto bit = [](std::uint32_t value, std::uint32_t index) { return (value >> index) & 1u; }; + const std::array element{bit(x, 0), bit(y, 0), bit(x, 1), bit(z, 0), bit(y, 1), bit(z, 1), bit(x, 2), bit(y, 2)}; + std::uint32_t index = 0; + for (std::uint32_t position = 0; position < element.size(); ++position) index |= element[position] << position; + constexpr std::size_t microTileBytes = 8u * 8u * 4u * ElementBytes; + const std::size_t slab = static_cast(z / 4u) * SliceBytes * 4u; + const std::size_t microTile = (static_cast(y / 8u) * (Width / 8u) + x / 8u) * microTileBytes; + return slab + microTile + static_cast(index) * ElementBytes; +} + +ShaderRecompiler::ShaderPixelStageInfo PixelInfo() { + ShaderRecompiler::ShaderPixelStageInfo pixel{}; + pixel.inputAddr = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionX) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionY); + pixel.posX = true; + pixel.posY = true; + pixel.targetOutputMode[0] = 9; + pixel.targetExportMapping.fill(0xe4u); + return pixel; +} + +void Draw(AgcDriver::VulkanDevice& device, const LowBlock& block, const AgcDriver::Graphics::ColorTarget& color, std::uint32_t layer, std::span pixelCode) { + const auto target = device.Target(); + std::memcpy(block.Data(Vertices), Lower.data(), sizeof(Lower)); + const auto address = block.At(Vertices); + const std::vector vertexUserData{static_cast(address), static_cast((address >> 32u) & 0xffffu) | (16u << 16u), static_cast(sizeof(Lower)), 0xfacu | (7u << 12u) | (14u << 15u), layer}; + ShaderRecompiler::ShaderVertexStageInfo vertexInfo{}; + vertexInfo.paClVsOutCntl = LayerExports; + const std::array vertexMemory{{{reinterpret_cast(VertexCode.data()), std::as_bytes(std::span(VertexCode))}}}; + ShaderRecompiler::RecompileRequest vertex{ + {ShaderStage::Vertex, reinterpret_cast(VertexCode.data()), VertexCode, 0, {}}, + {64u, 0, vertexUserData, std::nullopt, std::nullopt, vertexInfo, vertexMemory}, + target, + {0, 0, 0, 64} + }; + vertex.useCache = false; + const auto vertexResult = ShaderRecompiler::Recompile(vertex); + const auto vertexPush = static_cast(vertexResult.pushConstants.size()); + const std::vector pixelUserData(8, 0u); + const std::array pixelMemory{{{reinterpret_cast(pixelCode.data()), std::as_bytes(pixelCode)}}}; + ShaderRecompiler::RecompileRequest fragment{ + {ShaderStage::Fragment, reinterpret_cast(pixelCode.data()), pixelCode, 0, {}}, + {64u, 0, pixelUserData, std::nullopt, PixelInfo(), std::nullopt, pixelMemory}, + target, + PixelPushLayout(vertexPush, target) + }; + fragment.useCache = false; + const auto pixelResult = ShaderRecompiler::Recompile(fragment); + const std::array shaders{{ + {ShaderStage::Vertex, &vertexResult, 0}, + {ShaderStage::Fragment, &pixelResult, PixelPushOffset(vertexPush, target)} + }}; + AgcDriver::Graphics::State state{}; + state.stages = {AgcDriver::Graphics::ShaderPath::Vertex, 0u, 64u, 64u, std::nullopt, std::nullopt}; + state.colors = {color}; + state.color = color; + state.hasColorTarget = true; + state.renderExtent = {Width, Height}; + state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + state.viewport = {0, static_cast(Height), static_cast(Width), -static_cast(Height), 0, 1}; + state.negativeOneToOne = false; + state.scissor = {{0, 0}, {Width, Height}}; + state.cullMode = VK_CULL_MODE_NONE; + state.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; + VkPipelineColorBlendAttachmentState blend{}; + blend.colorWriteMask = 0xf; + state.blends = {blend}; + state.blend = blend; + state.blendConstants = {}; + state.samples = 1u; + state.renderLayers = color.layers; + const AgcDriver::Pm4::DrawParameters draw{0, static_cast(Lower.size()), 0, 1, 0, false}; + device.Draw(state, draw, shaders); + device.WaitIdle(); +} + +std::array Background(std::uint32_t slice, std::uint32_t x, std::uint32_t y) { + return {static_cast(x), static_cast(y), static_cast(200u + slice), 0u}; +} + +std::array Drawn(std::uint32_t slice, std::uint32_t x, std::uint32_t y) { + return {static_cast(x), static_cast(y), DrawnBlue(slice), 255u}; +} + +AgcDriver::Registers VolumeRegisters(std::uint64_t base) { + AgcDriver::Registers cx; + cx[0x207] = LayerExports; + cx[0x318] = static_cast(base >> 8u); + cx[0x319] = Width / 8u - 1u; + cx[0x31a] = Width * Height / 64u - 1u; + cx[0x31b] = (Depth - 1u) << 13u; + cx[0x31c] = (Format8888 << 2u) | BlendClamp; + cx[0x31d] = ThickTileIndex | (ThickTileIndex << 5u); + return cx; +} + +void ExpectFailure(const std::function& action, const std::string& expected) { + try { + action(); + } catch (const std::exception& error) { + Require(std::string(error.what()).find(expected) != std::string::npos, "expected \"" + expected + "\", got \"" + error.what() + "\""); + return; + } + throw std::runtime_error("expected a failure containing \"" + expected + "\""); +} + +AgcDriver::Graphics::ColorTarget DecodeVolume(std::uint64_t base) { + const auto target = AgcDriver::Graphics::DecodeColorBuffer(VolumeRegisters(base), 0); + Require(target.volume && target.depth == Depth && target.layers == Depth && target.depthSlice == 0u && target.arrayLastSlice == 0u && target.address == base && target.bytes == VolumeBytes && target.extent.width == Width && target.extent.height == Height && target.tileMode == AgcDriver::Graphics::ColorTileMode::GcnThick1D, "a thick 1D GCN view of slices 0 to 7 with a vertex render target index did not decode as an eight-slice 3D target of the whole volume"); + return target; +} + +void RequireDecodeLimits(std::uint64_t base) { + auto unlayered = VolumeRegisters(base); + unlayered[0x207] = 0u; + const auto single = AgcDriver::Graphics::DecodeColorBuffer(unlayered, 0); + Require(single.volume && single.depth == Depth && single.layers == 1u && single.depthSlice == 0u, "a thick 1D GCN view without a vertex render target index did not draw into its first slice alone"); + auto later = VolumeRegisters(base); + later[0x31b] |= 2u; + ExpectFailure([&] { static_cast(AgcDriver::Graphics::DecodeColorBuffer(later, 0)); }, "a PS4 thick color view that starts past slice 0 is not implemented"); + auto multisampled = VolumeRegisters(base); + multisampled[0x207] = 0u; + multisampled[0x31d] |= (1u << 12u) | (1u << 15u); + ExpectFailure([&] { static_cast(AgcDriver::Graphics::DecodeColorBuffer(multisampled, 0)); }, "multisampled PS4 thick color target"); + auto cleared = VolumeRegisters(base); + cleared[0x31c] |= 1u << 13u; + ExpectFailure([&] { static_cast(AgcDriver::Graphics::DecodeColorBuffer(cleared, 0)); }, "fast-cleared or compressed PS4 thick color target"); + const AgcDriver::Graphics::ColorTargetLayout slice(Width, Height, AgcDriver::Graphics::ColorTileMode::GcnThick1D, ElementBytes); + Require(slice.Bytes() == SliceBytes, "the 2D layout of a thick 1D color target does not span one slice"); + ExpectFailure([&] { static_cast(slice.Offset(0, 0)); }, "addressed through its 3D surface"); +} + +std::size_t CheckVolume(AgcDriver::VulkanDevice& device, const LowBlock& block) { + const auto base = block.At(0); + AgcDriver::Graphics::GuestTextureResource volume{}; + volume.baseAddress = base; + volume.width = Width; + volume.height = Height; + volume.depthOrLastArray = Depth - 1u; + volume.mipCount = 1u; + volume.allocatedMipCount = 1u; + volume.tileMode = AgcDriver::Graphics::TextureTileMode::kGcnThick1D; + volume.dimension = AgcDriver::Graphics::TextureDimension::k3D; + volume.format = 56u; + volume.gcnPitch = Width; + const auto surface = AgcDriver::Graphics::DescribeSurface(volume); + Require(surface.guestBytes == VolumeBytes, "the texture path does not see the thick 1D volume as eight whole slices"); + std::vector guest(VolumeBytes); + for (std::uint32_t z = 0; z < Depth; ++z) { + for (std::uint32_t y = 0; y < Height; ++y) { + for (std::uint32_t x = 0; x < Width; ++x) std::memcpy(guest.data() + ThickOffset(x, y, z), Background(z, x, y).data(), ElementBytes); + } + } + AgcDriver::GuestMemory::Write(base, std::as_bytes(std::span(guest)), 256); + const auto target = DecodeVolume(base); + for (std::size_t index = 0; index < DrawnSlices.size(); ++index) Draw(device, block, target, DrawnSlices[index], Program(index, DrawnSlices[index])); + AgcDriver::Graphics::StorageTexture::FlushPending(base, VolumeBytes, nullptr, "test read-back"); + device.WaitIdle(); + AgcDriver::GuestMemory::Read(base, std::as_writable_bytes(std::span(guest)), 256); + std::size_t drawnTotal = 0; + for (std::uint32_t z = 0; z < Depth; ++z) { + const bool drawnSlice = std::ranges::find(DrawnSlices, z) != DrawnSlices.end(); + std::size_t drawn = 0; + for (std::uint32_t y = 0; y < Height; ++y) { + for (std::uint32_t x = 0; x < Width; ++x) { + const auto* texel = guest.data() + ThickOffset(x, y, z); + const bool kept = std::memcmp(texel, Background(z, x, y).data(), ElementBytes) == 0; + const bool written = drawnSlice && std::memcmp(texel, Drawn(z, x, y).data(), ElementBytes) == 0; + if (!kept && !written) { + char text[192]; + std::snprintf(text, sizeof(text), "slice %u: the texel the thick 1D layout places at (%u, %u) holds %02x %02x %02x %02x, %s", z, x, y, texel[0], texel[1], texel[2], texel[3], drawnSlice ? "neither its background nor the color drawn for it" : "which no render target index chose, but not its background"); + throw std::runtime_error(text); + } + drawn += written ? 1u : 0u; + } + } + const auto texels = std::size_t{Width} * Height; + Require(!drawnSlice || (drawn > texels / 4u && drawn < texels * 3u / 4u), "slice " + std::to_string(z) + ": the triangle covers " + std::to_string(drawn) + " of " + std::to_string(texels) + " texels"); + drawnTotal += drawn; + } + return drawnTotal; +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + ShaderRecompiler::SetGcnGuestShaders(true); + const LowBlock block; + if (!block.Usable()) { + std::printf("skipped, no guest block below the 40-bit GCN color base range (0x%llx)\n", static_cast(block.Address())); + return VulkanTestSkipped; + } + RequireDecodeLimits(block.At(0)); + const auto drawn = CheckVolume(*device, block); + std::printf("GCN thick color target tests passed: draws exporting render target index 1, 2 and 6 into a 32x32x8 thick 1D volume land in those depth slices where the texture path places them (%zu texels), and the other five slices keep their background\n", drawn); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/docs/dev/debt/changes/feat-agc-ps4-thick-color-target.md b/docs/dev/debt/changes/feat-agc-ps4-thick-color-target.md new file mode 100644 index 000000000000..f55a27c966cc --- /dev/null +++ b/docs/dev/debt/changes/feat-agc-ps4-thick-color-target.md @@ -0,0 +1,5 @@ +# libSceAgcDriver + +## add Functional + +- [PS4 thick color targets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver, PS4) - not measured on a console. `CB_COLOR_ATTRIB` tile mode index 19 (1D thick) is drawn as a 3D target through the resident image of its volume. The CB registers carry no volume depth, so the volume is taken as `CB_COLOR_VIEW` slice max + 1 slices; a view shorter than the volume gets a resident image of the view's depth, kept coherent with a deeper 3D texture of the same memory only through guest memory. With `PA_CL_VS_OUT_CNTL` USE_VTX_RENDER_TARGET_INDX the vertex render target index selects the depth slice; without it the draw lands in slice 0. These throw: views starting past slice 0, multisampled targets, CMASK fast clears and FMASK compression, and the other thick and PRT tile modes.