diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp index 900ca544a794..195c20ddcbf1 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp @@ -1033,8 +1033,12 @@ bool UintPacked(ShaderRecompiler::ColorExportPacking packing) { return packing == ShaderRecompiler::ColorExportPacking::Uint || packing == ShaderRecompiler::ColorExportPacking::UintClamp8 || packing == ShaderRecompiler::ColorExportPacking::UintClamp16 || packing == ShaderRecompiler::ColorExportPacking::UintMask8 || packing == ShaderRecompiler::ColorExportPacking::UintMask16; } +bool NibblePacked(ShaderRecompiler::ColorExportPacking packing) { + return packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4 || packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4Truncated; +} + const char* PackedColorTargetName(ShaderRecompiler::ColorExportPacking packing) { - if (packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4) return "4_4_4_4 unorm"; + if (NibblePacked(packing)) return "4_4_4_4 unorm"; if (UintPacked(packing)) return "unsigned integer"; return packing == ShaderRecompiler::ColorExportPacking::Float11_11_10 ? "11_11_10 float" : "10_11_11 unorm"; } @@ -1046,7 +1050,7 @@ ShaderRecompiler::ColorExportPacking NarrowUintPacking(VkFormat format, ShaderRe } bool WordPacked(ShaderRecompiler::ColorExportPacking packing) { - return packing == ShaderRecompiler::ColorExportPacking::Unorm10_11_11 || packing == ShaderRecompiler::ColorExportPacking::Float11_11_10 || packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4; + return packing == ShaderRecompiler::ColorExportPacking::Unorm10_11_11 || packing == ShaderRecompiler::ColorExportPacking::Unorm10_11_11Truncated || packing == ShaderRecompiler::ColorExportPacking::Float11_11_10 || NibblePacked(packing); } // CB_COLOR_INFO FORMAT / NUMBER_TYPE / COMP_SWAP to a Vulkan attachment format. AMD formats list @@ -2111,7 +2115,7 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) { state.blendEnable = VK_FALSE; } Require(!state.blendEnable, "blending into " + packed + " color target is unsupported"); - const bool nibbles = color.packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4; + const bool nibbles = NibblePacked(color.packing); const std::uint32_t packedMask = nibbles ? 0xfu : 7u; Require(nibbles || floating || (exportedMask & packedMask) == packedMask, "partial writes of " + packed + " color target are unsupported"); if ((exportedMask & packedMask) != packedMask || color.mergeBlend != 0u) { @@ -2208,8 +2212,8 @@ std::uint32_t PackedWriteMask(std::uint8_t componentMapping, std::uint32_t expor std::uint32_t PackedWriteMask(ShaderRecompiler::ColorExportPacking packing, std::uint8_t componentMapping, std::uint32_t exportedMask) { constexpr std::array nibbles{0x000fu, 0x00f0u, 0x0f00u, 0xf000u}; constexpr std::array floats{0x000003ffu, 0x001ffc00u, 0xffe00000u, 0u}; - Require(packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4 || packing == ShaderRecompiler::ColorExportPacking::Float11_11_10, "a write mask of a packed color target that is neither 4_4_4_4 unorm nor 11_11_10 float"); - const auto& fields = packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4 ? nibbles : floats; + Require(NibblePacked(packing) || packing == ShaderRecompiler::ColorExportPacking::Float11_11_10, "a write mask of a packed color target that is neither 4_4_4_4 unorm nor 11_11_10 float"); + const auto& fields = NibblePacked(packing) ? nibbles : floats; std::uint32_t mask = 0; for (std::uint32_t component = 0; component < 4; ++component) { if (((exportedMask >> ((componentMapping >> (2u * component)) & 3u)) & 1u) != 0) mask |= fields[component]; @@ -2621,8 +2625,8 @@ ColorTarget DecodeColorBuffer(const Registers& cx, std::uint32_t slot, bool allo color.componentMapping = decoded.componentMapping; color.packing = decoded.packing; if (WordPacked(color.packing) && (info & 0x10000000u) != 0) throw std::runtime_error("AGC graphics: DCC-compressed " + std::string(PackedColorTargetName(color.packing)) + " color targets are unsupported"); - Require(color.packing != ShaderRecompiler::ColorExportPacking::Unorm10_11_11 || (info & 0x00040000u) == 0, "truncating (ROUND_MODE) 10_11_11 unorm color targets are unsupported"); - Require(color.packing != ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4 || (info & 0x00040000u) == 0, "truncating (ROUND_MODE) 4_4_4_4 unorm color targets are unsupported"); + if (color.packing == ShaderRecompiler::ColorExportPacking::Unorm10_11_11 && (info & 0x00040000u) != 0) color.packing = ShaderRecompiler::ColorExportPacking::Unorm10_11_11Truncated; + if (color.packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4 && (info & 0x00040000u) != 0) color.packing = ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4Truncated; 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/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index c4b2bfd55132..781b363550bd 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -12169,7 +12169,8 @@ void unorm10_11_11TargetTests() { expectFailure([&] { AgcDriver::Graphics::DecodeState(changed); }, "DCC-compressed 10_11_11 unorm color targets"); changed = queue; changed.context[0x31c] |= 0x40000u; - expectFailure([&] { AgcDriver::Graphics::DecodeState(changed); }, "truncating (ROUND_MODE) 10_11_11 unorm color targets"); + const auto truncating = AgcDriver::Graphics::DecodeState(changed); + Require(truncating.color.format == state.color.format && truncating.color.componentMapping == state.color.componentMapping && truncating.color.packing == ColorExportPacking::Unorm10_11_11Truncated, "a 10_11_11 unorm target with ROUND_MODE set did not decode as a truncating packed target"); constexpr std::array code{0xf800180fu, 0x07060504u, 0xbf810000u}; const auto request = packedExportRequest(queue, state, code); @@ -12526,7 +12527,8 @@ void unorm4_4_4_4TargetTests() { expectFailure([&] { AgcDriver::Graphics::DecodeState(changed); }, "DCC-compressed 4_4_4_4 unorm color targets"); changed = queue; changed.context[0x31c] |= 0x40000u; - expectFailure([&] { AgcDriver::Graphics::DecodeState(changed); }, "truncating (ROUND_MODE) 4_4_4_4 unorm color targets"); + const auto truncating = AgcDriver::Graphics::DecodeState(changed); + Require(truncating.color.format == state.color.format && truncating.color.componentMapping == state.color.componentMapping && truncating.color.packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4Truncated, "a 4_4_4_4 unorm target with ROUND_MODE set did not decode as a truncating packed target"); constexpr std::array code{0xf800180fu, 0x07060504u, 0xbf810000u}; const auto request = packedExportRequest(queue, state, code); @@ -12678,7 +12680,7 @@ void float11_11_10TargetTests() { const std::array shaders{{{ShaderRecompiler::ShaderStage::Vertex, &vertex, 0}, {ShaderRecompiler::ShaderStage::Fragment, &packed, 0}}}; Require(AgcDriver::Graphics::ValidateShaders(shaders, state, subgroup, false) == std::set{0u}, "the packed float export did not reach attachment 0"); auto invalid = request; - invalid.context.pixel->targetExportPacking[0] = static_cast(9); + invalid.context.pixel->targetExportPacking[0] = static_cast(static_cast(ColorExportPacking::Unorm10_11_11Truncated) + 1u); expectFailure([&] { static_cast(ShaderRecompiler::Recompile(invalid)); }, "invalid prepared fragment export packing"); } diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnorm4444ColorTarget.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnorm4444ColorTarget.cpp index 85326136cf85..370419067f7d 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnorm4444ColorTarget.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnorm4444ColorTarget.cpp @@ -44,6 +44,7 @@ constexpr std::uint32_t Height = 128; constexpr std::uint32_t ScissorWidth = 128; constexpr std::size_t BlockBytes = 65536; constexpr std::uint32_t Unorm4_4_4_4Info = 0x8000u | (19u << 2u) | (2u << 11u); +constexpr std::uint32_t RoundModeBit = 0x40000u; constexpr std::uint32_t LinearAttrib3 = 0x09000000u; constexpr std::uint32_t Float32Export = 9; constexpr std::uint32_t Float16Export = 4; @@ -56,12 +57,13 @@ constexpr std::array, 3> Triangle{{ {-1.0f, -1.0f, 0.5f, 1.0f}, {3.0f, -1.0f, 0.5f, 1.0f}, {-1.0f, 3.0f, 0.5f, 1.0f} }}; -alignas(256) std::array, 32> Programs{}; +alignas(256) std::array, 64> Programs{}; std::size_t programCount = 0; struct Golden { std::array rgba; std::uint16_t word; + std::uint16_t truncatedWord; }; std::uint32_t Half(float value) { @@ -152,17 +154,17 @@ struct Block { std::uint8_t* data = nullptr; }; -AgcDriver::Graphics::ColorTarget DecodeTarget(const Block& block, std::uint32_t attrib3) { +AgcDriver::Graphics::ColorTarget DecodeTarget(const Block& block, std::uint32_t attrib3, bool truncate) { AgcDriver::Registers cx; cx[0x318] = static_cast(block.Address() >> 8u); cx[0x31b] = 0; - cx[0x31c] = Unorm4_4_4_4Info; + cx[0x31c] = Unorm4_4_4_4Info | (truncate ? RoundModeBit : 0u); cx[0x31d] = 0; cx[0x3b0] = ((Width - 1u) << 14u) | (Height - 1u); cx[0x3b8] = attrib3; cx[0x390] = static_cast(block.Address() >> 40u); auto color = AgcDriver::Graphics::DecodeColorBuffer(cx, 0); - Require(color.format == VK_FORMAT_R16_UINT && color.elementBytes == 2 && color.componentMapping == 0x1bu && color.packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4 && color.bytes == block.bytes, "the 4_4_4_4 unorm target did not decode as a packed R16_UINT target"); + Require(color.format == VK_FORMAT_R16_UINT && color.elementBytes == 2 && color.componentMapping == 0x1bu && color.packing == (truncate ? ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4Truncated : ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4) && color.bytes == block.bytes, "the 4_4_4_4 unorm target did not decode as a packed R16_UINT target"); return color; } @@ -272,43 +274,44 @@ std::string Describe(std::array rgba) { return text; } -void TargetTests(AgcDriver::VulkanDevice& device, Block& block, std::uint32_t attrib3, ColorTileMode mode, const char* kind) { +void TargetTests(AgcDriver::VulkanDevice& device, Block& block, std::uint32_t attrib3, ColorTileMode mode, const char* kind, bool truncate) { const ColorTargetLayout layout(Width, Height, mode, 2); Require(layout.Bytes() == block.bytes, "the test surface size disagrees with its layout"); - const auto color = DecodeTarget(block, attrib3); + const auto color = DecodeTarget(block, attrib3, truncate); auto before = Fill(block); const float nan = std::numeric_limits::quiet_NaN(); const float infinity = std::numeric_limits::infinity(); - const std::array float32Cases{{ - {{1.0f, 0.0f, 0.0f, 0.0f}, 0xf000u}, - {{0.0f, 1.0f, 0.0f, 0.0f}, 0x0f00u}, - {{0.0f, 0.0f, 1.0f, 0.0f}, 0x00f0u}, - {{0.0f, 0.0f, 0.0f, 1.0f}, 0x000fu}, - {{1.0f, 1.0f, 1.0f, 1.0f}, 0xffffu}, - {{1.0f, 0.5f, 0.0f, 0.25f}, 0xf804u}, - {{0.0f, 1.0f, 0.5f, 0.25f}, 0x0f84u}, - {{nan, -1.0f, 2.0f, infinity}, 0x00ffu}, - {{1.0f / 15.0f, 2.0f / 15.0f, 7.0f / 15.0f, 14.0f / 15.0f}, 0x127eu}, - {{0.1f, 0.7f, 0.9f, 0.3f}, 0x2ad5u}, - {{-infinity, std::numeric_limits::min(), 0.999999940f, std::numeric_limits::denorm_min()}, 0x00f0u}, + const std::array float32Cases{{ + {{1.0f, 0.0f, 0.0f, 0.0f}, 0xf000u, 0xf000u}, + {{0.0f, 1.0f, 0.0f, 0.0f}, 0x0f00u, 0x0f00u}, + {{0.0f, 0.0f, 1.0f, 0.0f}, 0x00f0u, 0x00f0u}, + {{0.0f, 0.0f, 0.0f, 1.0f}, 0x000fu, 0x000fu}, + {{1.0f, 1.0f, 1.0f, 1.0f}, 0xffffu, 0xffffu}, + {{1.0f, 0.5f, 0.0f, 0.25f}, 0xf804u, 0xf703u}, + {{0.0f, 1.0f, 0.5f, 0.25f}, 0x0f84u, 0x0f73u}, + {{nan, -1.0f, 2.0f, infinity}, 0x00ffu, 0x00ffu}, + {{1.0f / 15.0f, 2.0f / 15.0f, 7.0f / 15.0f, 14.0f / 15.0f}, 0x127eu, 0x127eu}, + {{0.1f, 0.7f, 0.9f, 0.3f}, 0x2ad5u, 0x1ad4u}, + {{-infinity, std::numeric_limits::min(), 0.999999940f, std::numeric_limits::denorm_min()}, 0x00f0u, 0x00e0u}, + {{std::bit_cast(0x3d888888u), std::bit_cast(0x3d888889u), std::bit_cast(0x3f088888u), std::bit_cast(0x3f088889u)}, 0x1188u, 0x0178u}, }}; for (const auto& golden : float32Cases) { Draw(device, color, Float32Export, Float32Program(golden.rgba)); - ExpectSurface(device, block, layout, before, golden.word, std::string("a 32_ABGR export of ") + Describe(golden.rgba) + " into a " + kind + " 4_4_4_4 unorm target"); + ExpectSurface(device, block, layout, before, truncate ? golden.truncatedWord : golden.word, std::string("a 32_ABGR export of ") + Describe(golden.rgba) + " into a " + kind + " 4_4_4_4 unorm target"); before = ReadBack(device, block); } const std::array float16Cases{{ - {{1.0f, 0.0f, 0.0f, 0.0f}, 0xf000u}, - {{0.0f, 1.0f, 0.0f, 0.0f}, 0x0f00u}, - {{0.0f, 0.0f, 1.0f, 0.0f}, 0x00f0u}, - {{0.0f, 0.0f, 0.0f, 1.0f}, 0x000fu}, - {{1.0f, 1.0f, 1.0f, 1.0f}, 0xffffu}, - {{1.0f, 0.5f, 0.0f, 0.25f}, 0xf804u}, - {{0.5f, 0.75f, 0.125f, 0.25f}, 0x8b24u}, + {{1.0f, 0.0f, 0.0f, 0.0f}, 0xf000u, 0xf000u}, + {{0.0f, 1.0f, 0.0f, 0.0f}, 0x0f00u, 0x0f00u}, + {{0.0f, 0.0f, 1.0f, 0.0f}, 0x00f0u, 0x00f0u}, + {{0.0f, 0.0f, 0.0f, 1.0f}, 0x000fu, 0x000fu}, + {{1.0f, 1.0f, 1.0f, 1.0f}, 0xffffu, 0xffffu}, + {{1.0f, 0.5f, 0.0f, 0.25f}, 0xf804u, 0xf703u}, + {{0.5f, 0.75f, 0.125f, 0.25f}, 0x8b24u, 0x7b13u}, }}; for (const auto& golden : float16Cases) { Draw(device, color, Float16Export, Float16Program(golden.rgba)); - ExpectSurface(device, block, layout, before, golden.word, std::string("an FP16_ABGR export of ") + Describe(golden.rgba) + " into a " + kind + " 4_4_4_4 unorm target"); + ExpectSurface(device, block, layout, before, truncate ? golden.truncatedWord : golden.word, std::string("an FP16_ABGR export of ") + Describe(golden.rgba) + " into a " + kind + " 4_4_4_4 unorm target"); before = ReadBack(device, block); } } @@ -320,7 +323,8 @@ int main() { const auto device = OpenVulkanTestDevice(); if (!device) return VulkanTestSkipped; Block linear(ColorTargetLayout(Width, Height, ColorTileMode::Linear, 2).Bytes()); - TargetTests(*device, linear, LinearAttrib3, ColorTileMode::Linear, "linear"); + TargetTests(*device, linear, LinearAttrib3, ColorTileMode::Linear, "linear", false); + TargetTests(*device, linear, LinearAttrib3, ColorTileMode::Linear, "linear ROUND_MODE 1", true); std::puts("4_4_4_4 unorm color target tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnorm4444PartialWrite.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnorm4444PartialWrite.cpp index 09cb138925ed..7cbe3251dac7 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnorm4444PartialWrite.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnorm4444PartialWrite.cpp @@ -47,6 +47,7 @@ constexpr std::uint32_t Height = 128; constexpr std::uint32_t ScissorWidth = 128; constexpr std::size_t BlockBytes = 65536; constexpr std::uint32_t Unorm4_4_4_4Info = 0x8000u | (19u << 2u) | (2u << 11u); +constexpr std::uint32_t RoundModeBit = 0x40000u; constexpr std::uint32_t TiledAttrib3 = 0x4dc6c000u; constexpr std::uint32_t Float32Export = 9; constexpr std::uint32_t Float16Export = 4; @@ -205,17 +206,17 @@ struct Block { std::uint8_t* data = nullptr; }; -AgcDriver::Graphics::ColorTarget DecodeTarget(const Block& block) { +AgcDriver::Graphics::ColorTarget DecodeTarget(const Block& block, bool truncate) { AgcDriver::Registers cx; cx[0x318] = static_cast(block.Address() >> 8u); cx[0x31b] = 0; - cx[0x31c] = Unorm4_4_4_4Info; + cx[0x31c] = Unorm4_4_4_4Info | (truncate ? RoundModeBit : 0u); cx[0x31d] = 0; cx[0x3b0] = ((Width - 1u) << 14u) | (Height - 1u); cx[0x3b8] = TiledAttrib3; cx[0x390] = static_cast(block.Address() >> 40u); auto color = AgcDriver::Graphics::DecodeColorBuffer(cx, 0); - Require(color.format == VK_FORMAT_R16_UINT && color.componentMapping == 0x1bu && color.packing == ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4 && color.tileMode == ColorTileMode::RenderTarget && color.bytes == block.bytes, "the tiled 4_4_4_4 unorm target did not decode as a packed R16_UINT target"); + Require(color.format == VK_FORMAT_R16_UINT && color.componentMapping == 0x1bu && color.packing == (truncate ? ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4Truncated : ShaderRecompiler::ColorExportPacking::Unorm4_4_4_4) && color.tileMode == ColorTileMode::RenderTarget && color.bytes == block.bytes, "the tiled 4_4_4_4 unorm target did not decode as a packed R16_UINT target"); return color; } @@ -481,7 +482,7 @@ int main() { } const ColorTargetLayout layout(Width, Height, ColorTileMode::RenderTarget, 2); Block block(layout.Bytes()); - const auto color = DecodeTarget(block); + const auto color = DecodeTarget(block, false); Require(AgcDriver::Graphics::PackedWriteMask(color.componentMapping, 1u) == 0xf000u && AgcDriver::Graphics::PackedWriteMask(color.componentMapping, 8u) == 0x000fu && AgcDriver::Graphics::PackedWriteMask(color.componentMapping, 5u) == 0xf0f0u, "the export components did not map to the guest nibbles (X in 15..12, W in 3..0)"); const std::array cases{{ {0x1u, 0xf000u, {1.0f, 0.0f, 0.0f, 0.0f}}, @@ -502,6 +503,14 @@ int main() { ExpectSurface(*device, block, layout, before, goldens[index], wordMask, "a 32_ABGR " + Describe(mask, rgba) + " into a tiled 4_4_4_4 unorm target"); before = ReadBack(*device, block); } + const auto truncating = DecodeTarget(block, true); + const std::array truncatedGoldens{0xf000u, 0x7fffu, 0x369cu, 0x02ffu, 0xfffbu, 0x0000u, 0x0f73u, 0x1ad4u, 0x0000u}; + for (std::size_t index = 0; index < cases.size(); ++index) { + const auto& [mask, wordMask, rgba] = cases[index]; + DrawCase(*device, truncating, mask, wordMask, Float32Export, Float32Program(rgba)); + ExpectSurface(*device, block, layout, before, truncatedGoldens[index], wordMask, "a 32_ABGR " + Describe(mask, rgba) + " into a tiled ROUND_MODE 1 4_4_4_4 unorm target"); + before = ReadBack(*device, block); + } const std::array b{2.0f / 15.0f, 1.0f, 1.0f, 1.0f}; const std::array c{0.6f, 0.2f, 0.8f, 0.4f}; const std::array d{1.0f, 0.0f, 9.0f / 15.0f, 0.0f}; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnormColorTarget.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnormColorTarget.cpp index 31bf88c82d87..3569192bed07 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnormColorTarget.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PackedUnormColorTarget.cpp @@ -45,6 +45,7 @@ constexpr std::uint32_t Height = 128; constexpr std::uint32_t ScissorWidth = 128; constexpr std::size_t BlockBytes = 65536; constexpr std::uint32_t Unorm10_11_11Info = 0x8018u; +constexpr std::uint32_t RoundModeBit = 0x40000u; constexpr std::uint32_t TiledAttrib3 = 0x4dc6c000u; constexpr std::uint32_t LinearAttrib3 = 0x09000000u; constexpr std::uint32_t Float32Export = 9; @@ -61,15 +62,16 @@ constexpr std::array, 3> Triangle{{ alignas(256) std::array, 32> Programs{}; std::size_t programCount = 0; -std::uint32_t Unorm(float value, std::uint32_t bits) { +std::uint32_t Unorm(float value, std::uint32_t bits, bool truncate) { const auto maximum = (1u << bits) - 1u; if (!(value > 0.0f)) return 0; if (value >= 1.0f) return maximum; - return static_cast(std::nearbyint(static_cast(value) * maximum)); + const double scaled = static_cast(value) * maximum; + return static_cast(truncate ? std::floor(scaled) : std::nearbyint(scaled)); } -std::uint32_t Packed(std::array rgb) { - return Unorm(rgb[0], 11) | (Unorm(rgb[1], 11) << 11u) | (Unorm(rgb[2], 10) << 22u); +std::uint32_t Packed(std::array rgb, bool truncate) { + return Unorm(rgb[0], 11, truncate) | (Unorm(rgb[1], 11, truncate) << 11u) | (Unorm(rgb[2], 10, truncate) << 22u); } std::uint32_t Half(float value) { @@ -160,17 +162,17 @@ struct Block { std::uint8_t* data = nullptr; }; -AgcDriver::Graphics::ColorTarget DecodeTarget(const Block& block, std::uint32_t attrib3) { +AgcDriver::Graphics::ColorTarget DecodeTarget(const Block& block, std::uint32_t attrib3, bool truncate) { AgcDriver::Registers cx; cx[0x318] = static_cast(block.Address() >> 8u); cx[0x31b] = 0; - cx[0x31c] = Unorm10_11_11Info; + cx[0x31c] = Unorm10_11_11Info | (truncate ? RoundModeBit : 0u); cx[0x31d] = 0; cx[0x3b0] = ((Width - 1u) << 14u) | (Height - 1u); cx[0x3b8] = attrib3; cx[0x390] = static_cast(block.Address() >> 40u); auto color = AgcDriver::Graphics::DecodeColorBuffer(cx, 0); - Require(color.format == VK_FORMAT_R32_UINT && color.elementBytes == 4 && color.packing == ShaderRecompiler::ColorExportPacking::Unorm10_11_11 && color.bytes == block.bytes, "the 10_11_11 unorm target did not decode as a packed R32_UINT surface"); + Require(color.format == VK_FORMAT_R32_UINT && color.elementBytes == 4 && color.packing == (truncate ? ShaderRecompiler::ColorExportPacking::Unorm10_11_11Truncated : ShaderRecompiler::ColorExportPacking::Unorm10_11_11) && color.bytes == block.bytes, "the 10_11_11 unorm target did not decode as a packed R32_UINT surface"); return color; } @@ -280,10 +282,10 @@ std::string Describe(std::array rgb) { return text; } -void TargetTests(AgcDriver::VulkanDevice& device, Block& block, std::uint32_t attrib3, ColorTileMode mode, const char* kind) { +void TargetTests(AgcDriver::VulkanDevice& device, Block& block, std::uint32_t attrib3, ColorTileMode mode, const char* kind, bool truncate) { const ColorTargetLayout layout(Width, Height, mode, 4); Require(layout.Bytes() == block.bytes, "the test surface size disagrees with its layout"); - const auto color = DecodeTarget(block, attrib3); + const auto color = DecodeTarget(block, attrib3, truncate); auto before = Fill(block); const float nan = std::numeric_limits::quiet_NaN(); const float infinity = std::numeric_limits::infinity(); @@ -299,12 +301,12 @@ void TargetTests(AgcDriver::VulkanDevice& device, Block& block, std::uint32_t at }}; for (const auto& rgb : cases) { Draw(device, color, Float32Export, Float32Program(rgb)); - ExpectSurface(device, block, layout, before, Packed(rgb), std::string("a 32_ABGR export of ") + Describe(rgb) + " into a " + kind + " 10_11_11 unorm target"); + ExpectSurface(device, block, layout, before, Packed(rgb, truncate), std::string("a 32_ABGR export of ") + Describe(rgb) + " into a " + kind + " 10_11_11 unorm target"); before = ReadBack(device, block); } const std::array halves{0.5f, 0.75f, 0.125f}; Draw(device, color, Float16Export, Float16Program(halves)); - ExpectSurface(device, block, layout, before, Packed(halves), std::string("an FP16_ABGR export of ") + Describe(halves) + " into a " + kind + " 10_11_11 unorm target"); + ExpectSurface(device, block, layout, before, Packed(halves, truncate), std::string("an FP16_ABGR export of ") + Describe(halves) + " into a " + kind + " 10_11_11 unorm target"); } } @@ -315,8 +317,9 @@ int main() { if (!device) return VulkanTestSkipped; Block tiled(ColorTargetLayout(Width, Height, ColorTileMode::RenderTarget, 4).Bytes()); Block linear(ColorTargetLayout(Width, Height, ColorTileMode::Linear, 4).Bytes()); - TargetTests(*device, tiled, TiledAttrib3, ColorTileMode::RenderTarget, "tiled"); - TargetTests(*device, linear, LinearAttrib3, ColorTileMode::Linear, "linear"); + TargetTests(*device, tiled, TiledAttrib3, ColorTileMode::RenderTarget, "tiled", false); + TargetTests(*device, linear, LinearAttrib3, ColorTileMode::Linear, "linear", false); + TargetTests(*device, tiled, TiledAttrib3, ColorTileMode::RenderTarget, "tiled ROUND_MODE 1", true); std::puts("10_11_11 unorm color target tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp b/core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp index 63ac303e6691..3a76da4511fd 100644 --- a/core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp +++ b/core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp @@ -245,7 +245,7 @@ ConservativeZExport readConservativeZExport(Reader& reader) { ColorExportPacking readColorExportPacking(Reader& reader) { const auto value = reader.ReadU8(); - if (value > static_cast(ColorExportPacking::UintMask16)) { + if (value > static_cast(ColorExportPacking::Unorm10_11_11Truncated)) { throw std::runtime_error("RequestSerializer: invalid ColorExportPacking value"); } return static_cast(value); diff --git a/core/shader/recompiler/FragmentOutputSpecialization.hpp b/core/shader/recompiler/FragmentOutputSpecialization.hpp index e36c093baff9..eba84ee4f4bc 100644 --- a/core/shader/recompiler/FragmentOutputSpecialization.hpp +++ b/core/shader/recompiler/FragmentOutputSpecialization.hpp @@ -32,7 +32,7 @@ inline std::vector SpecializeFragmentOutputs(std::vector= PipelineSpecialization::ExportPackingBase + 8u) continue; - if (constant.value > static_cast(ColorExportPacking::UintMask16)) throw std::runtime_error("invalid prepared fragment export packing"); + if (constant.value > static_cast(ColorExportPacking::Unorm10_11_11Truncated)) throw std::runtime_error("invalid prepared fragment export packing"); if (constant.value != static_cast(ColorExportPacking::None)) packed.insert(constant.id - PipelineSpecialization::ExportPackingBase); } if (packed.empty() && !dualSource && reversed == 0u) return words; diff --git a/core/shader/recompiler/Recompiler.hpp b/core/shader/recompiler/Recompiler.hpp index c46af2af968d..f81f31aee43c 100644 --- a/core/shader/recompiler/Recompiler.hpp +++ b/core/shader/recompiler/Recompiler.hpp @@ -138,7 +138,9 @@ enum class ColorExportPacking : std::uint8_t { UintClamp8, UintClamp16, UintMask8, - UintMask16 + UintMask16, + Unorm4_4_4_4Truncated, + Unorm10_11_11Truncated }; enum class RasterPrimitive : std::uint8_t { diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hpp index 7bb4dd6e9e7f..1c24963fd4c7 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterHelpers.hpp @@ -144,6 +144,7 @@ std::uint32_t EmitUFloatToF32Bits(SpirvEmitterState& state, std::uint32_t raw, s std::uint32_t NormalizeFormatComponent(SpirvEmitterState& state, const SpirvBufferFormatInfo& info, std::uint32_t component, std::uint32_t raw); std::uint32_t EmitD16FormatComponent(SpirvEmitterState& state, const SpirvBufferFormatInfo& info, std::uint32_t component, std::uint32_t raw); std::uint32_t EmitFormatStoreComponent(SpirvEmitterState& state, const SpirvBufferFormatInfo& info, std::uint32_t component, std::uint32_t data); +std::uint32_t EmitTruncatedUnormStoreComponent(SpirvEmitterState& state, std::uint32_t data, std::uint32_t maximum); std::uint32_t EmitD16StoreComponent(SpirvEmitterState& state, const SpirvBufferFormatInfo& info, std::uint32_t component, std::uint32_t half); void EmitDeviceAtomicMemoryBarrier(SpirvEmitterState& state); void EmitDeviceAtomicMemoryBarrier(SpirvEmitterState& state, std::uint32_t memorySemantics); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp index ac1baf43a6b7..9b831ecf46da 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp @@ -201,7 +201,7 @@ std::uint32_t F32BitsToSmallFloatRtz(SpirvEmitterState& state, std::uint32_t bit return EmitOrU32(state, sign, magnitude); } -std::uint32_t NormStoreBits(SpirvEmitterState& state, std::uint32_t bits, std::uint32_t maximum, bool signedFormat) { +std::uint32_t NormStoreBits(SpirvEmitterState& state, std::uint32_t bits, std::uint32_t maximum, bool signedFormat, bool truncate) { const auto value = EmitBitcastU32ToF32(state, bits); const auto magnitudeBits = EmitAndConstant(state, bits, 0x7fffffffu); const auto magnitude = EmitBitcastU32ToF32(state, magnitudeBits); @@ -227,7 +227,7 @@ std::uint32_t NormStoreBits(SpirvEmitterState& state, std::uint32_t bits, std::u const auto odd = EmitCompareU32Constant(state, spv::OpINotEqual, EmitAndConstant(state, quotient, 1u), 0u); const auto tie = EmitLogicalAndBool(state, Binary(state, spv::OpIEqual, TypeBool(state), remainder, half), EmitLogicalOrBool(state, sticky, odd)); const auto up = EmitLogicalOrBool(state, UnsignedGreater(state, remainder, half), tie); - const auto rounded = EmitAddU32(state, quotient, EmitSelectValueU32(state, up, ConstantU32(state, 1u), ConstantU32(state, 0u))); + const auto rounded = truncate ? quotient : EmitAddU32(state, quotient, EmitSelectValueU32(state, up, ConstantU32(state, 1u), ConstantU32(state, 0u))); auto result = EmitSelectValueU32(state, UnsignedGreater(state, extra, ConstantU32(state, 16u)), ConstantU32(state, 0u), rounded); const auto full = Binary(state, spv::OpFOrdGreaterThanEqual, TypeBool(state), magnitude, ConstantF32Value(state, 1.0f)); result = EmitSelectValueU32(state, full, ConstantU32(state, maximum), result); @@ -279,6 +279,10 @@ std::uint32_t EmitD16FormatComponent(SpirvEmitterState& state, const SpirvBuffer } } +std::uint32_t EmitTruncatedUnormStoreComponent(SpirvEmitterState& state, std::uint32_t data, std::uint32_t maximum) { + return NormStoreBits(state, data, maximum, false, true); +} + std::uint32_t EmitFormatStoreComponent(SpirvEmitterState& state, const SpirvBufferFormatInfo& info, std::uint32_t component, std::uint32_t data) { const auto bits = info.componentBits[component]; const std::uint32_t mask = bits >= 32u ? 0xffffffffu : (1u << bits) - 1u; @@ -294,10 +298,10 @@ std::uint32_t EmitFormatStoreComponent(SpirvEmitterState& state, const SpirvBuff } case SpirvFormatComponentType::Unorm: case SpirvFormatComponentType::Uscaled: - return NormStoreBits(state, data, mask, false); + return NormStoreBits(state, data, mask, false, false); case SpirvFormatComponentType::Snorm: case SpirvFormatComponentType::Sscaled: - return EmitAndConstant(state, NormStoreBits(state, data, mask >> 1u, true), mask); + return EmitAndConstant(state, NormStoreBits(state, data, mask >> 1u, true, false), mask); case SpirvFormatComponentType::Float: if (bits == 32u) return data; if (bits == 16u) return F32BitsToSmallFloatRtz(state, data, 10u, true); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp index e7452aa37552..77a4850e6480 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp @@ -444,7 +444,7 @@ std::uint32_t MrtOutputMode(const SpirvEmitterState& state, const ExportInfo& ex return state.inputInfo.pixel->targetOutputMode[exp.index]; } -std::uint32_t PackExport(SpirvEmitterState& state, IrBufferFormat format, std::uint32_t color) { +std::uint32_t PackExport(SpirvEmitterState& state, IrBufferFormat format, std::uint32_t color, bool truncate) { const auto info = GetFormatInfo(format); auto packed = ConstantU32(state, 0u); for (std::uint32_t component = 0; component < info.componentCount; component++) { @@ -452,7 +452,7 @@ std::uint32_t PackExport(SpirvEmitterState& state, IrBufferFormat format, std::u state.module.AddFunction(spv::OpCompositeExtract, TypeF32(state), value, color, component); const auto bits = state.module.AllocateId(); state.module.AddFunction(spv::OpBitcast, TypeU32(state), bits, value); - auto field = EmitFormatStoreComponent(state, info, component, bits); + auto field = truncate ? EmitTruncatedUnormStoreComponent(state, bits, (1u << info.componentBits[component]) - 1u) : EmitFormatStoreComponent(state, info, component, bits); if (format == IrBufferFormat::Format10_11_11Float) { const auto mantissaBits = info.componentBits[component] - 5u; const auto nan = EmitCompareU32Constant(state, spv::OpUGreaterThan, EmitAndConstant(state, bits, 0x7fffffffu), 0x7f800000u); @@ -465,11 +465,16 @@ std::uint32_t PackExport(SpirvEmitterState& state, IrBufferFormat format, std::u std::uint32_t SelectExportPacking(SpirvEmitterState& state, std::uint32_t target, std::uint32_t color, std::uint32_t mode) { const auto packing = state.module.SpecializationConstant(TypeU32(state), PipelineSpecialization::ExportPackingBase + target, static_cast(ColorExportPacking::None)); - const auto unorm = EmitCompareU32Constant(state, spv::OpIEqual, packing, static_cast(ColorExportPacking::Unorm10_11_11)); + const auto unormTruncated = EmitCompareU32Constant(state, spv::OpIEqual, packing, static_cast(ColorExportPacking::Unorm10_11_11Truncated)); + const auto unorm = Binary(state, spv::OpLogicalOr, TypeBool(state), EmitCompareU32Constant(state, spv::OpIEqual, packing, static_cast(ColorExportPacking::Unorm10_11_11)), unormTruncated); const auto floating = EmitCompareU32Constant(state, spv::OpIEqual, packing, static_cast(ColorExportPacking::Float11_11_10)); - const auto small = EmitCompareU32Constant(state, spv::OpIEqual, packing, static_cast(ColorExportPacking::Unorm4_4_4_4)); - const auto wide = Select(state, TypeU32(state), floating, PackExport(state, IrBufferFormat::Format10_11_11Float, color), PackExport(state, IrBufferFormat::Format11_11_10UNorm, color)); - const auto word = Select(state, TypeU32(state), small, PackExport(state, IrBufferFormat::Format4_4_4_4UNorm, color), wide); + const auto rounded = EmitCompareU32Constant(state, spv::OpIEqual, packing, static_cast(ColorExportPacking::Unorm4_4_4_4)); + const auto truncated = EmitCompareU32Constant(state, spv::OpIEqual, packing, static_cast(ColorExportPacking::Unorm4_4_4_4Truncated)); + const auto small = Binary(state, spv::OpLogicalOr, TypeBool(state), rounded, truncated); + const auto unormWord = Select(state, TypeU32(state), unormTruncated, PackExport(state, IrBufferFormat::Format11_11_10UNorm, color, true), PackExport(state, IrBufferFormat::Format11_11_10UNorm, color, false)); + const auto wide = Select(state, TypeU32(state), floating, PackExport(state, IrBufferFormat::Format10_11_11Float, color, false), unormWord); + const auto nibbles = Select(state, TypeU32(state), truncated, PackExport(state, IrBufferFormat::Format4_4_4_4UNorm, color, true), PackExport(state, IrBufferFormat::Format4_4_4_4UNorm, color, false)); + const auto word = Select(state, TypeU32(state), small, nibbles, wide); const auto packed = Binary(state, spv::OpLogicalOr, TypeBool(state), Binary(state, spv::OpLogicalOr, TypeBool(state), unorm, floating), small); const bool clamp = mode == 2u || mode == 4u || mode == 9u; const bool mask = mode == 1u; @@ -674,10 +679,11 @@ void MergeColorTarget(SpirvEmitterState& state, std::size_t entry, std::uint32_t const auto stored = state.module.AllocateId(); state.module.AddFunction(spv::OpCompositeExtract, TypeU32(state), stored, EmitStorageImageRead(state, resource, coord, sample), 0u); const bool floating = merge.packing == ColorExportPacking::Float11_11_10; - if (!floating && merge.packing != ColorExportPacking::Unorm4_4_4_4) FailEmit("a color target merge into an unsupported packing"); + const bool truncated = merge.packing == ColorExportPacking::Unorm4_4_4_4Truncated; + if (!floating && !truncated && merge.packing != ColorExportPacking::Unorm4_4_4_4) FailEmit("a color target merge into an unsupported packing"); if (merge.blendControl != 0u && !floating) FailEmit("a blending color target merge into a target other than 11_11_10 float"); const auto format = floating ? IrBufferFormat::Format10_11_11Float : IrBufferFormat::Format4_4_4_4UNorm; - const auto word = PackExport(state, format, merge.blendControl != 0u ? BlendPackedColor(state, format, color, stored, merge.blendControl) : color); + const auto word = PackExport(state, format, merge.blendControl != 0u ? BlendPackedColor(state, format, color, stored, merge.blendControl) : color, truncated); const auto kept = EmitBinaryU32(state, spv::OpBitwiseAnd, stored, ConstantU32(state, ~merge.mask)); const auto merged = EmitOrU32(state, kept, EmitBinaryU32(state, spv::OpBitwiseAnd, word, ConstantU32(state, merge.mask))); state.module.AddFunction(spv::OpStore, state.mergeValueVariables[entry], merged); diff --git a/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp b/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp index 19414421c6d9..e8493ed02c02 100644 --- a/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp +++ b/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp @@ -173,7 +173,7 @@ ShaderStageInputInfo BuildShaderStageInputInfo(ShaderStageKind stage, const Gues for (const auto& merge : pixel.targetMerges) { const auto& first = pixel.targetMerges.front(); if (merge.mask == 0u || merge.target >= 8u || std::ranges::count(pixel.targetMerges, merge.target, &TargetMerge::target) != 1 || (merge.samples != 1u && merge.samples != 2u && merge.samples != 4u && merge.samples != 8u) || merge.samples != first.samples || merge.depthStencilWrites != first.depthStencilWrites || merge.depthStencilTests != first.depthStencilTests) throw std::runtime_error("ShaderInputInfoBuilder: invalid color target merge"); - if (merge.blend.enabled ? merge.packing != ColorExportPacking::None : merge.packing != ColorExportPacking::Unorm4_4_4_4 && merge.packing != ColorExportPacking::Float11_11_10) throw std::runtime_error("ShaderInputInfoBuilder: a color target merge into an unsupported packing"); + if (merge.blend.enabled ? merge.packing != ColorExportPacking::None : merge.packing != ColorExportPacking::Unorm4_4_4_4 && merge.packing != ColorExportPacking::Unorm4_4_4_4Truncated && merge.packing != ColorExportPacking::Float11_11_10) throw std::runtime_error("ShaderInputInfoBuilder: a color target merge into an unsupported packing"); if (merge.blendControl != 0u && merge.packing != ColorExportPacking::Float11_11_10) throw std::runtime_error("ShaderInputInfoBuilder: a blending color target merge into a target other than 11_11_10 float"); if ((merge.layered && (merge.samples != 1u || merge.lastSlice < merge.firstSlice)) || (!merge.layered && (merge.firstSlice != 0u || merge.lastSlice != 0u))) throw std::runtime_error("ShaderInputInfoBuilder: a color target merge into a multisampled array, a slice range that ends before it starts, or slices of an unlayered target"); const auto& blend = merge.blend; diff --git a/docs/dev/debt/changes/agc-10-11-11-truncation.md b/docs/dev/debt/changes/agc-10-11-11-truncation.md new file mode 100644 index 000000000000..371bac46b6c0 --- /dev/null +++ b/docs/dev/debt/changes/agc-10-11-11-truncation.md @@ -0,0 +1,5 @@ +# libSceAgcDriver + +## change [10_11_11 unorm color targets] + +- [10_11_11 unorm color targets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - the CB conversion is measured on RDNA2 (gfx1036 under RADV, COLOR_10_11_11 with the UNORM number type, RB+ off, 32_ABGR exports), not on the console's gfx1013: with `CB_COLOR_INFO.ROUND_MODE` clear an export rounds to nearest (a 32-bit input never falls on a tie), and with it set truncates to `floor(clamp(v, 0, 1) * max)`, 2047 for the 11-bit fields and 1023 for the 10-bit one; NaN gives 0. The pixel shader packs each export that way into an R32_UINT attachment. PS4 (GCN) color targets do not read `ROUND_MODE` and always round. diff --git a/docs/dev/debt/changes/agc-4-4-4-4-truncation.md b/docs/dev/debt/changes/agc-4-4-4-4-truncation.md new file mode 100644 index 000000000000..01268a4925e6 --- /dev/null +++ b/docs/dev/debt/changes/agc-4-4-4-4-truncation.md @@ -0,0 +1,5 @@ +# libSceAgcDriver + +## add Functional + +- [4_4_4_4 unorm ROUND_MODE](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - measured on RDNA2 (gfx1036 under RADV, RB+ off, 32_ABGR exports), not on the console's gfx1013: with `CB_COLOR_INFO.ROUND_MODE` set the CB truncates each export to `floor(clamp(v, 0, 1) * 15)` (NaN to 0), and with it clear rounds to nearest. The pixel shader packs a 4_4_4_4 target that way. PS4 (GCN) color targets do not read `ROUND_MODE` and always round.