diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp index a17ca06c7824..4f8124279589 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp @@ -26,15 +26,16 @@ enum class ColorTileMode : std::uint32_t { GcnDisplay1D = 0x101, GcnDisplay2D = 0x102, GcnThin2D = 0x103, - GcnLinear = 0x104 + GcnLinear = 0x104, + GcnLinearGeneral = 0x109 }; 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::GcnLinearGeneral; } constexpr bool ColorTileModeIsLinear(ColorTileMode mode) { - return mode == ColorTileMode::Linear || mode == ColorTileMode::GcnLinear; + return mode == ColorTileMode::Linear || mode == ColorTileMode::GcnLinear || mode == ColorTileMode::GcnLinearGeneral; } constexpr bool ColorTileModeIsXor(ColorTileMode mode) { @@ -42,6 +43,7 @@ constexpr bool ColorTileModeIsXor(ColorTileMode mode) { } constexpr std::uint32_t ColorTileModeBlockBytes(ColorTileMode mode) { + if (mode == ColorTileMode::GcnLinearGeneral) return 1u; return ColorTileModeIsLinear(mode) ? 256u : mode == ColorTileMode::Standard4KB || mode == ColorTileMode::S4KBX || mode == ColorTileMode::D4KBX ? 4096u : 65536u; } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp index 83715e01bbe8..ed89756384c8 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp @@ -132,6 +132,9 @@ ColorTargetLayout::ColorTargetLayout(std::uint32_t width, std::uint32_t height, pitch = (width + pitchAlignment - 1u) / pitchAlignment * pitchAlignment; break; } + case ColorTileMode::GcnLinearGeneral: + pitch = width; + break; case ColorTileMode::S4KBX: case ColorTileMode::D4KBX: case ColorTileMode::Z64KBX: diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp index 70d9d9146ffa..a4892f2d4451 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp @@ -4840,7 +4840,7 @@ void recordPreparedDraw(const Context& context, const State& state, const Pm4::D if (checkRecords) checkRecords(); timer.phase(PhaseSync); APS5_LOG_CHARS_OUT_DEBUG("SubmitAndWait OK"); - for (const auto& binding : targets) GuestMemory::CheckRange(reinterpret_cast(binding.color.MemoryAddress()), binding.color.bytes, 256, true); + for (const auto& binding : targets) GuestMemory::CheckRange(reinterpret_cast(binding.color.MemoryAddress()), binding.color.bytes, std::min(256u, ColorTileModeBlockBytes(binding.color.tileMode)), true); for (auto& binding : targets) { if (!binding.dump) continue; std::lock_guard lock(dumpMutex); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp index 3849a708c1da..e4511b589d94 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp @@ -2454,10 +2454,13 @@ ColorTarget DecodeGcnColorBuffer(const Registers& cx, std::uint32_t slot) { Require(fragments <= color.coverageSamples, "GCN color targets with more fragments than samples are not implemented"); color.samples = fragments == 1u ? 1u : color.coverageSamples; if (fragments > 1u && fragments < color.coverageSamples) color.eqaaFragments = fragments; - Require((info & 0x3u) == 0 && (info & 0x80u) == 0, "endian-swapped or linear-general GCN color targets are not implemented"); + Require((info & 0x3u) == 0, "endian-swapped GCN color targets are not implemented"); + const bool linearGeneral = (info & 0x80u) != 0; + Require(!linearGeneral || color.coverageSamples == 1u, "multisampled linear-general GCN color targets are not implemented"); Require((info & 0x8000u) != 0 || number == 7 || number == 4 || number == 5, "unclamped normalized color is unsupported"); const auto view = read(cx, 0x31b + stride); - const auto slice = view & 0x7ffu; + Require(!linearGeneral || (view & 0x700u) == 0, "a linear-general GCN color view with SLICE_START bits above its low address byte is not implemented"); + const auto slice = linearGeneral ? 0u : view & 0x7ffu; const auto lastSlice = (view >> 13u) & 0x7ffu; Require(lastSlice >= slice, "the color view ends before its first slice"); const auto exports = find(cx, 0x207); @@ -2467,7 +2470,8 @@ ColorTarget DecodeGcnColorBuffer(const Registers& cx, std::uint32_t slot) { color.extent = {pitch, sliceElements / pitch}; color.surfaceExtent = color.extent; color.elementBytes = decoded.elementBytes; - switch (attrib & 0x1fu) { + if (linearGeneral) color.tileMode = ColorTileMode::GcnLinearGeneral; + else switch (attrib & 0x1fu) { case 8u: color.tileMode = ColorTileMode::GcnLinear; break; case 5u: case 13u: color.tileMode = ColorTileMode::GcnThin1D; break; case 9u: color.tileMode = ColorTileMode::GcnDisplay1D; break; @@ -2481,7 +2485,7 @@ ColorTarget DecodeGcnColorBuffer(const Registers& cx, std::uint32_t slot) { color.mipCount = 1; const ColorTargetLayout layout(color.extent.width, color.extent.height, color.tileMode, color.elementBytes, color.pipeBankXor); Require(layout.Bytes() == static_cast(pitch) * color.extent.height * color.elementBytes, "PS4 color target pitch or slice is not padded the way its tile mode pads them"); - color.surfaceAddress = (static_cast(read(cx, 0x318 + stride)) << 8u) + static_cast(slice) * layout.Bytes() * fragments; + color.surfaceAddress = (static_cast(read(cx, 0x318 + stride)) << 8u) + (linearGeneral ? view & 0xffu : static_cast(slice) * layout.Bytes() * fragments); color.address = color.surfaceAddress; color.bytes = layout.Bytes(); color.format = decoded.format; diff --git a/core/libs/prx/libSceAgcDriver/tests/GcnColorTarget.cpp b/core/libs/prx/libSceAgcDriver/tests/GcnColorTarget.cpp index d4a66faa60f9..82d975d44517 100644 --- a/core/libs/prx/libSceAgcDriver/tests/GcnColorTarget.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/GcnColorTarget.cpp @@ -46,6 +46,7 @@ constexpr std::uint32_t Format8888 = 10u; constexpr std::uint32_t BlendClamp = 1u << 15u; constexpr std::uint32_t FastClear = 1u << 13u; constexpr std::uint32_t Compression = 1u << 14u; +constexpr std::uint32_t LinearGeneral = 1u << 7u; constexpr std::uint32_t Thin1D = 13u; constexpr std::uint32_t Display2D = 10u; constexpr std::uint32_t Thin2D = 14u; @@ -229,6 +230,34 @@ void LaterSlicesDecode() { Check(Failure([base] { DecodeSlice(base, Thin2D, 1u, 4u); }).find("multisampled color views of a later slice of a 2D-tiled array") != std::string::npos, "slice 1 of a 2D-tiled 4x GCN array was accepted"); } +AgcDriver::Graphics::ColorTarget DecodeLinearGeneral(std::uint32_t format, std::uint32_t pitch, std::uint32_t height, std::uint32_t view, std::uint32_t attrib, std::uint32_t flags) { + AgcDriver::Registers cx; + cx[0x318] = static_cast(AddressOf(colorMemory) >> 8u); + cx[0x319] = pitch / 8u - 1u; + cx[0x31a] = pitch * height / 64u - 1u; + cx[0x31b] = view; + cx[0x31c] = (format << 2u) | BlendClamp | flags; + cx[0x31d] = attrib; + return AgcDriver::Graphics::DecodeColorBuffer(cx, 0); +} + +void LinearGeneralTargetsDecode() { + using AgcDriver::Graphics::ColorTileMode; + constexpr std::uint32_t Format8 = 1u; + constexpr std::uint32_t Pitch = 24u; + constexpr std::uint32_t Rows = 8u; + const auto base = AddressOf(colorMemory); + const auto offset = DecodeLinearGeneral(Format8888, Width, Height, 0x40u | (3u << 13u), Thin2D, LinearGeneral); + Check(offset.tileMode == ColorTileMode::GcnLinearGeneral && offset.address == base + 0x40u && offset.surfaceAddress == offset.address && offset.bytes == ColorBytes && offset.pipeBankXor == 0u, "a linear-general GCN target did not decode as a linear surface at CB_COLOR_BASE plus SLICE_START's low byte, whatever its tile mode index"); + const auto narrow = DecodeLinearGeneral(Format8, Pitch, Rows, 0x07u, Thin1D, LinearGeneral); + Check(narrow.tileMode == ColorTileMode::GcnLinearGeneral && narrow.extent.width == Pitch && narrow.extent.height == Rows && narrow.elementBytes == 1u && narrow.bytes == std::size_t{Pitch} * Rows && narrow.address == base + 7u, "an 8-bit linear-general GCN target with a 24-texel pitch did not decode unpadded at a byte offset"); + Check(Failure([] { DecodeLinearGeneral(Format8, Pitch, Rows, 0u, 8u, 0u); }).find("not padded the way its tile mode pads them") != std::string::npos, "an 8-bit linear-aligned GCN target with a 24-texel pitch was accepted"); + Check(Failure([] { DecodeLinearGeneral(Format8888, Width, Height, 0x100u, Thin1D, LinearGeneral); }).find("SLICE_START bits above its low address byte") != std::string::npos, "a linear-general GCN view with SLICE_START above 0xff was accepted"); + Check(Failure([] { DecodeLinearGeneral(Format8888, Width, Height, 0u, Thin1D | (2u << 12u) | (2u << 15u), LinearGeneral); }).find("multisampled linear-general") != std::string::npos, "a 4x linear-general GCN target was accepted"); + Check(Failure([] { DecodeLinearGeneral(Format8888, Width, Height, 0u, 31u, 0u); }).find("tile mode index 31") != std::string::npos, "GCN tile mode index 31 without CB_COLOR_INFO LINEAR_GENERAL was accepted"); + Check(Failure([] { DecodeLinearGeneral(Format8888, Width, Height, 0u, Thin1D, 1u); }).find("endian-swapped") != std::string::npos, "an endian-swapped GCN target was accepted"); +} + void PaddedTargetsAreCovered() { Check(FmaskDecompress(Height).empty(), "an FMASK decompress over a whole 4x GCN target was refused"); const auto padded = FmaskDecompress(Height - 4u); @@ -253,11 +282,12 @@ int main() { MultisampledSlicesDecode(); PaddedTargetsAreCovered(); LaterSlicesDecode(); + LinearGeneralTargetsDecode(); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } if (failures != 0) return 1; - std::puts("GCN multisampled color targets decode with their CMASK, EQAA and one-fragment targets with their FMASK base and size, and a metadata pass over the visible rows of a padded target is covered"); + std::puts("GCN multisampled color targets decode with their CMASK, EQAA and one-fragment targets with their FMASK base and size, a metadata pass over the visible rows of a padded target is covered, and linear-general targets decode unpadded at CB_COLOR_BASE plus SLICE_START's low byte"); return 0; } diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/GcnLinearGeneralColorTarget.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/GcnLinearGeneralColorTarget.cpp new file mode 100644 index 000000000000..3f1bdc7d8d3f --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/GcnLinearGeneralColorTarget.cpp @@ -0,0 +1,271 @@ +#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/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 + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; +using Triangle = std::array, 3>; + +constexpr std::uint32_t Width = 72; +constexpr std::uint32_t Height = 64; +constexpr std::uint32_t ElementBytes = 4; +constexpr std::uint32_t Format8888 = 10u; +constexpr std::uint32_t BlendClamp = 1u << 15u; +constexpr std::uint32_t LinearGeneral = 1u << 7u; +constexpr std::uint32_t Thin2D = 14u; +constexpr std::size_t SurfaceBytes = std::size_t{Width} * Height * ElementBytes; +constexpr std::size_t Guard = 256; +constexpr std::size_t Vertices = 0x20000; +constexpr std::size_t BlockBytes = 0x30000; +constexpr std::uint64_t GcnAddressLimit = std::uint64_t{1} << 40u; +constexpr std::array ByteOffsets{0x00u, 0x44u}; +constexpr std::uint8_t GuardByte = 0xa5u; +constexpr std::uint8_t DrawnBlue = 0x60u; + +alignas(256) constexpr std::array VertexCode{ + 0xe0382000u, 0x80000000u, 0xbf8c0f70u, 0xf80008cfu, 0x03020100u, 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}}}; + +alignas(256) std::array PixelCode{}; + +std::span Program() { + constexpr std::uint32_t inverse255 = 0x3b808081u; + PixelCode = { + 0x7e084900u, 0x7e0a4901u, + 0x100808ffu, inverse255, 0x100a0affu, inverse255, + 0x7e0c02ffu, std::bit_cast(static_cast(DrawnBlue) / 255.0f), + 0x7e0e02f2u, + 0xf800180fu, 0x07060504u, + 0xbf810000u, + }; + return PixelCode; +} + +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::array Background(std::uint32_t x, std::uint32_t y) { + return {static_cast(x), static_cast(y), 200u, 0u}; +} + +std::array Drawn(std::uint32_t x, std::uint32_t y) { + return {static_cast(x), static_cast(y), DrawnBlue, 255u}; +} + +AgcDriver::Graphics::ColorTarget DecodeTarget(std::uint64_t base, std::uint32_t byteOffset) { + AgcDriver::Registers cx; + cx[0x318] = static_cast(base >> 8u); + cx[0x319] = Width / 8u - 1u; + cx[0x31a] = Width * Height / 64u - 1u; + cx[0x31b] = byteOffset; + cx[0x31c] = (Format8888 << 2u) | BlendClamp | LinearGeneral; + cx[0x31d] = Thin2D; + const auto target = AgcDriver::Graphics::DecodeColorBuffer(cx, 0); + Require(target.format == VK_FORMAT_R8G8B8A8_UNORM && target.samples == 1u && target.tileMode == AgcDriver::Graphics::ColorTileMode::GcnLinearGeneral && target.address == base + byteOffset && target.bytes == SurfaceBytes && target.extent.width == Width && target.extent.height == Height, "the linear-general GCN target did not decode as an unpadded 72x64 RGBA8 surface at CB_COLOR_BASE plus SLICE_START's low byte"); + return target; +} + +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::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)}; + 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, ShaderRecompiler::ShaderVertexStageInfo{}, 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; + const AgcDriver::Pm4::DrawParameters draw{0, static_cast(Lower.size()), 0, 1, 0, false}; + device.Draw(state, draw, shaders); + device.WaitIdle(); +} + +std::string Describe(std::uint32_t byteOffset, std::uint32_t x, std::uint32_t y, const std::uint8_t* texel) { + char text[192]; + std::snprintf(text, sizeof(text), "byte offset 0x%02x: the texel at (%u, %u) of the unpadded rows holds %02x %02x %02x %02x", byteOffset, x, y, texel[0], texel[1], texel[2], texel[3]); + return text; +} + +std::size_t CheckTarget(AgcDriver::VulkanDevice& device, const LowBlock& block, std::size_t offset, std::uint32_t byteOffset) { + const auto base = block.At(offset); + std::vector guest(Guard + SurfaceBytes + Guard, GuardByte); + const auto texelAt = [&](std::uint32_t x, std::uint32_t y) { return guest.data() + Guard + byteOffset + (static_cast(y) * Width + x) * ElementBytes; }; + for (std::uint32_t y = 0; y < Height; ++y) { + for (std::uint32_t x = 0; x < Width; ++x) std::memcpy(texelAt(x, y), Background(x, y).data(), ElementBytes); + } + const auto window = base - Guard; + AgcDriver::GuestMemory::Write(window, std::as_bytes(std::span(guest)), 256); + Draw(device, block, DecodeTarget(base, byteOffset), Program()); + AgcDriver::Graphics::StorageTexture::FlushPending(window, guest.size(), nullptr, "test read-back"); + device.WaitIdle(); + AgcDriver::GuestMemory::Read(window, std::as_writable_bytes(std::span(guest)), 256); + for (std::size_t index = 0; index < Guard + byteOffset; ++index) Require(guest[index] == GuardByte, "byte offset " + std::to_string(byteOffset) + ": the draw wrote " + std::to_string(Guard + byteOffset - index) + " bytes before the target"); + for (std::size_t index = Guard + byteOffset + SurfaceBytes; index < guest.size(); ++index) Require(guest[index] == GuardByte, "byte offset " + std::to_string(byteOffset) + ": the draw wrote past the target's last unpadded row"); + 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 = texelAt(x, y); + const bool kept = std::memcmp(texel, Background(x, y).data(), ElementBytes) == 0; + const bool written = std::memcmp(texel, Drawn(x, y).data(), ElementBytes) == 0; + Require(kept || written, Describe(byteOffset, x, y, texel) + ", neither its background nor the color drawn for it"); + drawn += written ? 1u : 0u; + } + } + constexpr auto texels = std::size_t{Width} * Height; + Require(drawn > texels / 4u && drawn < texels * 3u / 4u, "byte offset " + std::to_string(byteOffset) + ": the triangle covers " + std::to_string(drawn) + " of " + std::to_string(texels) + " texels"); + return drawn; +} + +} + +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; + } + std::string counts; + for (std::size_t index = 0; index < ByteOffsets.size(); ++index) { + const auto drawn = CheckTarget(*device, block, Guard + index * 0x8000u, ByteOffsets[index]); + counts += (index == 0 ? "" : ", ") + std::to_string(drawn); + } + std::printf("GCN linear-general color target tests passed: a triangle drawn into an unpadded 72-texel-pitch RGBA8 target at CB_COLOR_BASE and at CB_COLOR_BASE + 0x44 lands in row-major texels (%s texels) and leaves every other texel and the bytes around the target unchanged\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-linear-general-color-targets.md b/docs/dev/debt/changes/claude-ps4-linear-general-color-targets.md new file mode 100644 index 000000000000..014fa38b444e --- /dev/null +++ b/docs/dev/debt/changes/claude-ps4-linear-general-color-targets.md @@ -0,0 +1,5 @@ +# libSceAgcDriver + +## add Unknown function info + +- [PS4 (GCN) linear-general color targets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver, PS4) - not checked on a console. A color target with `CB_COLOR_INFO.LINEAR_GENERAL` set ignores its tile mode index and is linear with the pitch `CB_COLOR_PITCH` gives, unpadded, at `CB_COLOR_BASE` plus `CB_COLOR_VIEW.SLICE_START[7:0]` bytes, with no slice offset, as [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/57e129ec548cfcf8572c0a26b1c9aaf7e95dc3b6/src/video_core/amdgpu/regs_color.h) reads these registers. `SLICE_START[10:8]` set, and multisampled linear-general targets, throw; endian-swapped targets still throw.