diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp index 8367436d9..7d3ab0af8 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp @@ -41,6 +41,8 @@ struct ColorTarget { // DCC metadata of a compressed target (CB_COLOR_INFO DCC_ENABLE), or 0 (see DccMetadata.hpp). std::uint64_t dccAddress = 0; bool dccAlphaOnMsb = false; + std::uint64_t cmaskAddress = 0; + std::size_t cmaskBytes = 0; std::uint64_t surfaceAddress = 0; VkExtent2D surfaceExtent{}; std::uint32_t mipCount = 1; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp index e2c33ddc9..29a48ebe9 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp @@ -90,15 +90,18 @@ bool clearToTexel(StorageTexture& image, const std::array& texel, return image.FillClear(std::span(pattern), StorageTexture::WholeImage, refusal); } +void writeTexels(const ColorTarget& color, const std::array& texel) { + const auto elementBytes = static_cast(color.elementBytes); + std::vector texels(color.bytes); + for (std::size_t offset = 0; offset + elementBytes <= texels.size(); offset += elementBytes) std::memcpy(texels.data() + offset, texel.data(), elementBytes); + GuestMemory::Write(color.address, texels); +} + void storeClearTexels(const Context& context, const ColorTarget& color, const std::array& texel) { StorageTexture::FlushPending(color.address, color.bytes, nullptr, "fast-clear materialization"); const auto keys = ReadDccKeys(color.dccAddress, color.bytes); if (!IsDccClear(keys)) return; - const auto current = keys == DccKeys::ClearRegister ? texel : clearTexel(color, keys); - const auto elementBytes = static_cast(color.elementBytes); - std::vector texels(color.bytes); - for (std::size_t offset = 0; offset + elementBytes <= texels.size(); offset += elementBytes) std::memcpy(texels.data() + offset, current.data(), elementBytes); - GuestMemory::Write(color.address, texels); + writeTexels(color, keys == DccKeys::ClearRegister ? texel : clearTexel(color, keys)); MarkDccUncompressed(context, color.dccAddress, color.bytes); } @@ -125,6 +128,24 @@ void materializeRegisterClear(const Context& context, const ColorTarget& color, resident.Refresh(); } +bool materializeCmaskClear(const Context& context, const ColorTarget& color, StorageTexture* resident) { + if (color.cmaskAddress == 0) return false; + const auto metadataBytes = static_cast(color.cmaskBytes) * 256u; + const auto state = CurrentDccKeys(color.cmaskAddress, metadataBytes); + if (state == DccKeys::Uncompressed) return false; + Require(color.dccAddress == 0, std::string("CMASK of a DCC color target that is not all expanded is not modeled (") + DccKeysName(state) + ")"); + Require(state == DccKeys::Clear0000, std::string("CMASK whose tiles are not all fast-cleared or all expanded is not modeled (") + DccKeysName(state) + ")"); + const auto texel = clearTexel(color, DccKeys::ClearRegister); + const char* refusal = nullptr; + if (resident == nullptr || !clearToTexel(*resident, texel, color.elementBytes, refusal)) { + StorageTexture::FlushPending(color.address, color.bytes, nullptr, "fast-clear materialization"); + writeTexels(color, texel); + if (resident != nullptr) resident->Refresh(); + } + MarkDccUncompressed(context, color.cmaskAddress, metadataBytes); + return true; +} + void imageBarrier(const Context& context, VkCommandBuffer commands, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout, VkPipelineStageFlags sourceStage, VkPipelineStageFlags destinationStage, VkAccessFlags sourceAccess, VkAccessFlags destinationAccess) { VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; barrier.srcAccessMask = sourceAccess; @@ -895,6 +916,7 @@ std::shared_ptr refreshResidentTarget(const Context& context, co std::lock_guard lock(reportMutex); if (reported.insert(color.address).second) std::fprintf(stderr, "[gpu] color target 0x%llx stays non-resident: %s\n", static_cast(color.address), error.what()); } + if (resident != nullptr) materializeCmaskClear(context, color, resident.get()); if (profile) { const auto lookupUs = std::chrono::duration(std::chrono::steady_clock::now() - lookupStart).count(); ++outcome.targetLookups; @@ -1543,6 +1565,7 @@ void Draw(const Context& context, const State& state, const Pm4::DrawParameters& targetViews.push_back(binding.proxied ? binding.resident->AttachmentProxyView() : binding.resident->AttachmentView(color.format, color.mip, color.depthSlice)); continue; } + materializeCmaskClear(context, color, nullptr); Require(!color.mipTail, "rendering into a packed mip tail needs the resident image of its surface"); Require(color.depth == 1, "rendering into a 3D color target needs the resident image of its surface"); if (binding.gpuTiling) { @@ -2103,22 +2126,33 @@ DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, con return result; } +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; + std::shared_ptr resident; + try { + resident = CachedStorageSurface(context, SurfaceForTarget(color)); + } catch (const std::exception&) { + return nullptr; + } + return resident != nullptr && resident->GuestBytes() == color.bytes ? resident : nullptr; +} + +} + void RunColorMetadataPass(const Context& context, const ColorMetadataPass& pass) { for (const auto& color : pass.targets) { + if (color.cmaskAddress != 0) { + const auto resident = metadataPassResident(context, color); + if (materializeCmaskClear(context, color, resident.get()) && resident != nullptr) resident->MarkDirty(); + } if (color.dccAddress == 0) continue; auto keys = CurrentDccKeys(color.dccAddress, color.bytes); if (keys == DccKeys::Uncompressed) continue; 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 resident; - if ((color.tileMode == ColorTileMode::RenderTarget || color.tileMode == ColorTileMode::Standard4KB) && context.detiler != nullptr) { - try { - resident = CachedStorageSurface(context, SurfaceForTarget(color)); - } catch (const std::exception&) { - resident = nullptr; - } - if (resident != nullptr && resident->GuestBytes() != color.bytes) resident = nullptr; - } + const auto resident = metadataPassResident(context, color); if (resident != nullptr) { const char* refusal = nullptr; const bool current = keys == DccKeys::ClearRegister ? clearToTexel(*resident, texel, color.elementBytes, refusal) : StorageTexture::FindPending(color.address, color.bytes) == resident || resident->UploadedKeys() == keys; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp index 02b9bc1b5..f16df7221 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp @@ -642,7 +642,7 @@ ColorTarget DecodeColorBuffer(const Registers& cx, std::uint32_t slot) { const auto format = (info >> 2u) & 0x1fu; const auto decoded = DecodeColorFormat(format, number, swap); // ROUND_MODE (bit 18) only affects unorm rounding. With DCC_ENABLE (bit 28) the target is written - 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); }() + ")"); + if ((info & ~(0x00039f7cu | 0x00040000u | 0x10000000u | 0x00002000u)) != 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 & ~0x3fffffffu) == 0, "reserved CB_COLOR_VIEW bits are set"); @@ -690,6 +690,13 @@ ColorTarget DecodeColorBuffer(const Registers& cx, std::uint32_t slot) { const auto clear = find(cx, 0x323 + word + stride); color.clearWords[word] = clear == cx.end() ? 0u : clear->second; } + if ((info & 0x2000u) != 0) { + Require(maxMip == 0 && !volume && slice == 0, "CMASK fast clears of a mipmapped, 3D or array color target are unsupported"); + const auto cmaskHigh = find(cx, 0x398 + slot); + color.cmaskAddress = ((cmaskHigh == cx.end() ? 0ull : static_cast(cmaskHigh->second & 0xffu)) << 40u) | (static_cast(read(cx, 0x31f + stride)) << 8u); + Require(color.cmaskAddress != 0, "CMASK fast clears without a CMASK address are unsupported"); + color.cmaskBytes = (static_cast((color.extent.width + 7u) / 8u) * ((color.extent.height + 7u) / 8u) + 1u) / 2u; + } if ((info & 0x10000000u) != 0) { if (maxMip == 0) { const auto dccHigh = find(cx, 0x3a8 + slot); diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index d691d7c56..976b8d1d3 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -819,6 +819,70 @@ void metadataPassTests() { } } +alignas(256) std::array cmaskMemory{}; + +void cmaskTests() { + using AgcDriver::Graphics::DecodeColorBuffer; + auto queue = makeState(); + queue.context[0x31c] |= 0x2000; + const auto cmaskAddress = reinterpret_cast(cmaskMemory.data()); + queue.context[0x31f] = static_cast(cmaskAddress >> 8u); + queue.context[0x398] = static_cast(cmaskAddress >> 40u); + queue.context[0x323] = 0x11223344; + queue.context[0x324] = 0; + const auto target = DecodeColorBuffer(queue.context, 0); + Require(target.cmaskAddress == cmaskAddress && target.cmaskBytes == cmaskMemory.size(), "the fast-clear target lost its CMASK"); + Require(DecodeColorBuffer(makeState().context, 0).cmaskAddress == 0, "a target without FAST_CLEAR got a CMASK"); + auto mipmapped = queue; + mipmapped.context[0x3b0] |= 1u << 28u; + expectFailure([&] { DecodeColorBuffer(mipmapped.context, 0); }, "mipmapped, 3D or array"); + auto unaddressed = queue; + unaddressed.context[0x31f] = 0; + unaddressed.context[0x398] = 0; + expectFailure([&] { DecodeColorBuffer(unaddressed.context, 0); }, "without a CMASK address"); + + queue.context[0x0] = 0; + queue.context[0x202] = 0xcc0020; + auto pass = AgcDriver::Graphics::DecodeColorMetadataPass(queue); + Require(pass.has_value() && pass->targets.size() == 1 && pass->targets[0].cmaskAddress == cmaskAddress, "the metadata pass lost the target's CMASK"); + const AgcDriver::Graphics::Context context{}; + const auto texels = [&](std::uint32_t value) { + for (std::size_t offset = 0; offset < colorMemory.size(); offset += 4) { + std::uint32_t texel = 0; + std::memcpy(&texel, colorMemory.data() + offset, 4); + if (texel != value) return false; + } + return true; + }; + const auto cmaskIs = [&](std::uint8_t value) { return std::all_of(cmaskMemory.begin(), cmaskMemory.end(), [&](std::uint8_t byte) { return byte == value; }); }; + std::memset(colorMemory.data(), 0x5a, colorMemory.size()); + cmaskMemory.fill(0); + AgcDriver::Graphics::RunColorMetadataPass(context, *pass); + Require(texels(0x11223344) && cmaskIs(0xff), "a CMASK fast clear was not eliminated into the texels"); + std::memset(colorMemory.data(), 0x5a, colorMemory.size()); + AgcDriver::Graphics::RunColorMetadataPass(context, *pass); + Require(texels(0x5a5a5a5au) && cmaskIs(0xff), "a pass over an expanded CMASK changed the texels"); + cmaskMemory = {0x00, 0xff, 0x00, 0x00}; + expectFailure([&] { AgcDriver::Graphics::RunColorMetadataPass(context, *pass); }, "not all fast-cleared or all expanded"); + Require(texels(0x5a5a5a5au), "a refused pass changed the texels"); + + queue.context[0x31c] |= 0x10000000; + const auto keysAddress = reinterpret_cast(dccKeys.data()); + queue.context[0x325] = static_cast(keysAddress >> 8u); + queue.context[0x3a8] = static_cast(keysAddress >> 40u); + pass = AgcDriver::Graphics::DecodeColorMetadataPass(queue); + Require(pass.has_value() && pass->targets.size() == 1 && pass->targets[0].cmaskAddress == cmaskAddress && pass->targets[0].dccAddress == keysAddress, "a DCC fast-clear target lost its CMASK or DCC keys"); + std::memset(colorMemory.data(), 0x5a, colorMemory.size()); + cmaskMemory.fill(0xff); + dccKeys.fill(0x20); + AgcDriver::Graphics::RunColorMetadataPass(context, *pass); + Require(texels(0x11223344) && cmaskIs(0xff) && std::ranges::all_of(dccKeys, [](std::uint8_t key) { return key == 0xff; }), "a DCC fast clear under an expanded CMASK was not eliminated through its keys"); + std::memset(colorMemory.data(), 0x5a, colorMemory.size()); + cmaskMemory.fill(0); + expectFailure([&] { AgcDriver::Graphics::RunColorMetadataPass(context, *pass); }, "DCC color target that is not all expanded"); + Require(texels(0x5a5a5a5au), "a refused pass changed the texels of a DCC target"); +} + void DepthClipTests() { auto queue = makeState(); const auto direct = AgcDriver::Graphics::DecodeState(queue); @@ -2204,6 +2268,7 @@ int main() { DisabledColorTests(); CompactedExportTests(); metadataPassTests(); + cmaskTests(); ShaderStageTests(); PixelInputLayoutTests(); InitialContextTests(); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 7b9455c33..f9917fa2c 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -273,6 +273,7 @@ Throughout the project, every function at every stage either **does exactly what - The Linux placement of the `--to-intel` stubs in the [ELF patcher](../../core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp) is covered only by a synthetic test. - The libc [SSE4a trap emulation](../../core/libs/prx/libc/src/specifics/windows/Sse4aEmulation.hpp) on Windows is superseded by `--to-intel` and remains only until the relinked title has been verified without it. - [Descriptor pool chain](../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - each pool holds two storage buffer descriptors more than a set can take, as a workaround: RADV (Mesa 26.0 and later) lets a `FREE_DESCRIPTOR_SET` pool place a set up to 32 bytes past the pool's buffer ([`util_vma_heap_init`](https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/src/amd/vulkan/radv_descriptor_pool.c) is given `bo_size + RADV_POOL_HEAP_OFFSET` as a size), and with a page-multiple pool size those bytes are an unmapped page +- [CMASK fast clears](../../core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp) (libSceAgcDriver) - only a CMASK whose nibbles are all fast-cleared (0) or all expanded (0xF) is modeled, checked over one nibble per 8x8 tile of the target from its start; the console's CMASK layout (pipe alignment, padding) is not decoded. The first draw or fast-clear eliminate over a fast-cleared CMASK fills the whole target with `CB_COLOR_CLEAR_WORD` and stores those bytes as expanded, so a draw that covers only part of the target reads the clear value in the rest as the eliminate would leave it. On a DCC target the clear is in the DCC keys, so its CMASK must be all expanded. A mixed CMASK throws, as does a DCC target's CMASK that is not all expanded and a fast clear of a mipmapped, 3D or array color target - [DCC display buffers](../../core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp) are presented only from uniform uncompressed or fast-clear keys; other keys, and register-clear keys over a pending image, throw. Keys are read as one byte per 256 bytes (the driver's model), not in the console's layout. - [8_SRGB color targets](../../core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp) (libSceAgcDriver) on a device that cannot attach and blend R8_SRGB render into an RGBA8_SRGB copy of the target, so the sRGB encode, decode and blend rounding are the device's; the console's are not measured. - [8_SRGB color targets](../../core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp) (libSceAgcDriver) on such a device throw unless they are single-mip 2D surfaces with a resident image; linear targets have none.