From 5c8c5d25783d790a2d2b5db1642b32b25f9b74e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Fri, 2 Oct 2026 08:23:01 +0200 Subject: [PATCH] feat(shader): implement the D16 format buffer loads buffer_load_format_d16_{x,xy,xyz,xyzw}, buffer_load_format_d16_hi_x and tbuffer_load_format_d16_{x,xy,xyz,xyzw} convert each component to 16 bits and pack two per VGPR. The conversions follow the hardware: unorm/snorm round the exact value to nearest even, scaled integers round to nearest even and saturate at 65504, 32-bit floats truncate toward zero (saturating at 65504), and 32-bit integers saturate to 16 bits. All of it is done with integer ops so it does not depend on the driver's f16 rounding. _x and _hi_x keep the other half, xyz keeps the upper half of its second register, and a descriptor constant one is f16 1.0 for non-integer formats. Co-Authored-By: Claude --- .../tests/execution/FormatD16.cpp | 176 ++++++++++++++++++ .../IrMetadata/ResourceInfo.hpp | 1 + .../include/RdnaDecoder/RdnaOpcode.hpp | 9 + .../RdnaDecoder/src/RdnaMemoryOpDecoder.cpp | 16 +- .../recompiler/RdnaDecoder/src/RdnaOpcode.cpp | 9 + .../SpirvBackend/SpirvEmitterHelpers.hpp | 1 + .../src/SpirvMemory/SpirvFormatConvert.cpp | 123 ++++++++++++ .../SpirvMemory/SpirvMemoryInstructions.cpp | 5 +- .../Translation/TranslationContext.hpp | 1 + .../MemoryInstructions/BufferInstructions.cpp | 35 ++++ .../MemoryInstructionDispatch.cpp | 10 + 11 files changed, 383 insertions(+), 3 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/FormatD16.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/FormatD16.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/FormatD16.cpp new file mode 100644 index 000000000..54a17ee50 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/FormatD16.cpp @@ -0,0 +1,176 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Inputs = 4; +constexpr std::uint32_t Results = 16; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array Code{ + 0x34020084, 0x34060086, 0xbe900300, 0xbe910301, 0xbe920302, 0xbe9303ff, 0x01001244, 0x7e1402ff, + 0xdead0000, 0x7e1602ff, 0xdead0000, 0x7e1802ff, 0xdead0000, 0x7e1a02ff, 0xdead0000, 0x7e1c02ff, + 0xdead0000, 0x7e1e02ff, 0xdead0000, 0x7e2002ff, 0xdead0000, 0x7e2202ff, 0xdead0000, 0x7e2402ff, + 0xdead0000, 0x7e2602ff, 0xdead0000, 0x7e2802ff, 0xbeefbeef, 0x7e2a02ff, 0xbeefbeef, 0x7e2c02ff, + 0xbeefbeef, 0x7e2e02ff, 0xbeefbeef, 0x7e3002ff, 0xbeefbeef, 0x7e3202ff, 0xbeefbeef, 0xe8081000, + 0x80200a01, 0xe8101000, 0x80200b01, 0xe8381008, 0x80200c01, 0xe8401008, 0x80200d01, 0xe8481008, + 0x80200e01, 0xe8501008, 0x80200f01, 0xe8301000, 0x80201001, 0xe8b01004, 0x80201101, 0xe8a01004, + 0x80201201, 0xe8a81004, 0x80201301, 0xe8711000, 0x80201401, 0xe9e2100c, 0x80201501, 0xe0981004, + 0x80001701, 0xe20c100c, 0x80041801, 0xbf8c3f70, 0xe0701000, 0x80010a03, 0xe0701004, 0x80010b03, + 0xe0701008, 0x80010c03, 0xe070100c, 0x80010d03, 0xe0701010, 0x80010e03, 0xe0701014, 0x80010f03, + 0xe0701018, 0x80011003, 0xe070101c, 0x80011103, 0xe0701020, 0x80011203, 0xe0701024, 0x80011303, + 0xe0701028, 0x80011403, 0xe070102c, 0x80011503, 0xe0701030, 0x80011603, 0xe0701034, 0x80011703, + 0xe0701038, 0x80011803, 0xe070103c, 0x80011903, 0xbf810000, +}; + +constexpr std::uint32_t Rows[32][4] = { + {0x000000ffu, 0x7f800000u, 0x0000ffffu, 0x73cf256du}, + {0x00000080u, 0xff800000u, 0x00008000u, 0xdda1494cu}, + {0x0000007fu, 0x7fc00000u, 0x00007fffu, 0x8f4d3e27u}, + {0x00000000u, 0x7f800001u, 0x0000ffafu, 0xdb5b5fabu}, + {0x81ff7f01u, 0xffc12345u, 0x00007ff7u, 0xec99108du}, + {0xc7fde805u, 0x7fa00000u, 0x00008009u, 0x7734d7c1u}, + {0x73ab4876u, 0x00000001u, 0x0000fffdu, 0x8201e2bdu}, + {0xdae44550u, 0x33000001u, 0x00000001u, 0x965eda32u}, + {0x309d6b79u, 0x387fc000u, 0x2f45e678u, 0xcdcc6929u}, + {0x830c71c2u, 0x477ff000u, 0x79cb9e86u, 0xa13ffe79u}, + {0x9d2c67edu, 0x47800000u, 0xcb008853u, 0x2fa91425u}, + {0x18187993u, 0xc77fefffu, 0x7253edc6u, 0x4dabb481u}, + {0x244caf9cu, 0x3f800000u, 0x17362f25u, 0x89e7d15fu}, + {0xcf44dd3fu, 0xbf800000u, 0xe3eff9c0u, 0xb1852f27u}, + {0xa26b7f62u, 0x80000000u, 0x0ab8ab67u, 0x986e86cbu}, + {0xfb710734u, 0x3fc00000u, 0x656abd72u, 0xf6fa5db8u}, + {0x73f778aau, 0xa7677796u, 0xbd299753u, 0x9d95847eu}, + {0xa66b0d38u, 0x28518867u, 0x9f8558a6u, 0x03d71684u}, + {0xd4ea65d0u, 0x8743feb6u, 0x102b938bu, 0x0f3ebdd3u}, + {0x09208a65u, 0x30b17d0bu, 0xe12b2b8fu, 0x3deffa38u}, + {0x99809225u, 0x07b37e14u, 0xc7321cc0u, 0x76c468aeu}, + {0x5387f613u, 0x70c6a5b8u, 0x97491e23u, 0xd7a94dedu}, + {0x320094eau, 0x84e55160u, 0x3bd03346u, 0xa3ea284du}, + {0x4b4d8474u, 0x7ff12229u, 0x012d0ea6u, 0xa9964aefu}, + {0x15c1d2dfu, 0x75139237u, 0xa7a11490u, 0x4735af1cu}, + {0x6822a6b2u, 0xff666589u, 0x8d1fe1dau, 0xfee5a5b2u}, + {0xee82ec3fu, 0xd7185ddau, 0x154cd2aau, 0xb53302fcu}, + {0x4105cca7u, 0x50b601fcu, 0xc20ba2c2u, 0x3acb6266u}, + {0x834c687au, 0x49fe85b0u, 0x079dd25au, 0x11fa2ac0u}, + {0x902a174fu, 0xc42b7170u, 0x1ba1192eu, 0x66809a11u}, + {0x1b98fbe4u, 0xd8b9b45cu, 0x4a789cb3u, 0x62f28d1au}, + {0x111b8aaau, 0xf542441du, 0x0452ef05u, 0xd8e94b15u}, +}; +constexpr std::uint32_t Expected[32][16] = { + {0xdead3c00u, 0xdeada008u, 0xdead3c00u, 0xdead8200u, 0xdead7bffu, 0xdeadbc00u, 0xdeadffffu, 0xdead7c00u, 0xdeadffffu, 0xdead7fffu, 0x00003c00u, 0x0025006du, 0xbeef00cfu, 0x7c00beefu, 0x000036d7u, 0x3c003c00u}, + {0xdead3804u, 0xdeadbc00u, 0xdead3800u, 0xdeadbc00u, 0xdead7800u, 0xdeadf800u, 0xdeadff80u, 0xdeadfc00u, 0xdeadffffu, 0xdead8000u, 0x00003804u, 0x0049004cu, 0xbeef00a1u, 0xfc00beefu, 0x000034c5u, 0x3c003c00u}, + {0xdead37f8u, 0xdead3c00u, 0xdead3800u, 0xdead3c00u, 0xdead7800u, 0xdead7800u, 0xdead007fu, 0xdead7e00u, 0xdeadffffu, 0xdead7fffu, 0x000037f8u, 0x003e0027u, 0xbeef004du, 0x7e00beefu, 0x000030e5u, 0x3c003c00u}, + {0xdead0000u, 0xdead0000u, 0xdead3bfdu, 0xdead9910u, 0xdead7bfdu, 0xdeadd510u, 0xdead0000u, 0xdead7c01u, 0xdeadffffu, 0xdead7fffu, 0x00000000u, 0x005f00abu, 0xbeef005bu, 0x7c01beefu, 0x0000395du, 0x3c003c00u}, + {0xdead1c04u, 0xdead2008u, 0xdead37ffu, 0xdead3bffu, 0xdead77ffu, 0xdead77ffu, 0xdead0001u, 0xdeadfe09u, 0xdeadffffu, 0xdead8000u, 0x37f81c04u, 0x0010008du, 0xbeef0099u, 0xfe09beefu, 0x0000386cu, 0x3c003c00u}, + {0xdead2505u, 0xdead290au, 0xdead3800u, 0xdeadbbffu, 0xdead7800u, 0xdeadf7ffu, 0xdead0005u, 0xdead7d00u, 0xdeadffffu, 0xdead7fffu, 0x3b472505u, 0x00d700c1u, 0xbeef0034u, 0x7d00beefu, 0x00003a0eu, 0x3c003c00u}, + {0xdead3767u, 0xdead3b6fu, 0xdead3c00u, 0xdead8600u, 0xdead7bffu, 0xdeadc200u, 0xdead0076u, 0xdead0000u, 0xdead0001u, 0xdead0001u, 0x34853767u, 0x00e200bdu, 0xbeef0001u, 0x0000beefu, 0x000039eeu, 0x3c003c00u}, + {0xdead3505u, 0xdead390au, 0xdead0100u, 0xdead0200u, 0xdead3c00u, 0xdead3c00u, 0xdead0050u, 0xdead0000u, 0xdeadffffu, 0xdead7fffu, 0x34543505u, 0x00da0032u, 0xbeef005eu, 0x0000beefu, 0x00003246u, 0x3c003c00u}, + {0xdead3798u, 0xdead3b9fu, 0xdead3b34u, 0xdeadb262u, 0xdead7b34u, 0xdeadee62u, 0xdead0079u, 0xdead03ffu, 0xdeadffffu, 0xdead7fffu, 0x36b73798u, 0x00690029u, 0xbeef00ccu, 0x03ffbeefu, 0x00003125u, 0x3c003c00u}, + {0xdead3a16u, 0xdeadb7d0u, 0xdead38f4u, 0xdeadba18u, 0xdead78f4u, 0xdeadf618u, 0xdeadffc2u, 0xdead7bffu, 0xdeadffffu, 0xdead7fffu, 0x37173a16u, 0x00fe0079u, 0xbeef003fu, 0x7bffbeefu, 0x00003798u, 0x3c003c00u}, + {0xdead3b6fu, 0xdeadb0cau, 0xdead3843u, 0xdeadbb7bu, 0xdead7843u, 0xdeadf77bu, 0xdeadffedu, 0xdead7bffu, 0xdeadffffu, 0xdead7fffu, 0x36763b6fu, 0x00140025u, 0xbeef00a9u, 0x7bffbeefu, 0x000030a5u, 0x3c003c00u}, + {0xdead389du, 0xdeadbadeu, 0xdead3b6eu, 0xdeadb08fu, 0xdead7b6eu, 0xdeadec8eu, 0xdeadff93u, 0xdeadfbffu, 0xdeadffffu, 0xdead8000u, 0x3798389du, 0x00b40081u, 0xbeef00abu, 0xfbffbeefu, 0x0000380cu, 0x3c003c00u}, + {0xdead38e5u, 0xdeadba4du, 0xdead31e5u, 0xdead35e5u, 0xdead71e5u, 0xdead71e5u, 0xdeadff9cu, 0xdead3c00u, 0xdeadffffu, 0xdead7fffu, 0x397d38e5u, 0x00d1005fu, 0xbeef00e7u, 0x3c00beefu, 0x000035f6u, 0x3c003c00u}, + {0xdead33e8u, 0xdead37f0u, 0xdead3bceu, 0xdeadaa40u, 0xdead7bceu, 0xdeade640u, 0xdead003fu, 0xdeadbc00u, 0xdeadffffu, 0xdead8000u, 0x3aef33e8u, 0x002f0027u, 0xbeef0085u, 0xbc00beefu, 0x000030e5u, 0x3c003c00u}, + {0xdead3626u, 0xdead3a2cu, 0xdead395bu, 0xdeadb94au, 0xdead795bu, 0xdeadf54au, 0xdead0062u, 0xdead8000u, 0xdeadffffu, 0xdead8000u, 0x37f83626u, 0x008600cbu, 0xbeef006eu, 0x8000beefu, 0x00003a5eu, 0x3c003c00u}, + {0xdead3287u, 0xdead368du, 0xdead39ecu, 0xdeadb829u, 0xdead79ecu, 0xdeadf429u, 0xdead0034u, 0xdead3e00u, 0xdeadffffu, 0xdead7fffu, 0x27073287u, 0x005d00b8u, 0xbeef00fau, 0x3e00beefu, 0x000039c6u, 0x3c003c00u}, + {0xdead3955u, 0xdeadb96bu, 0xdead38bbu, 0xdeadba8bu, 0xdead78bbu, 0xdeadf68bu, 0xdeadffaau, 0xdead8000u, 0xdeadffffu, 0xdead8000u, 0x37883955u, 0x0084007eu, 0xbeef0095u, 0x8000beefu, 0x000037e8u, 0x3c003c00u}, + {0xdead3307u, 0xdead370eu, 0xdead358au, 0xdead398au, 0xdead758au, 0xdead758au, 0xdead0038u, 0xdead0000u, 0xdeadffffu, 0xdead7fffu, 0x2a873307u, 0x00160084u, 0xbeef00d7u, 0x0000beefu, 0x00003824u, 0x3c003c00u}, + {0xdead3a87u, 0xdeadb60cu, 0xdead389cu, 0xdeadbac7u, 0xdead789cu, 0xdeadf6c7u, 0xdeadffd0u, 0xdead8000u, 0xdeadffffu, 0xdead8000u, 0x36563a87u, 0x00bd00d3u, 0xbeef003eu, 0x8000beefu, 0x00003a9fu, 0x3c003c00u}, + {0xdead3656u, 0xdead3a5du, 0xdead3172u, 0xdead3572u, 0xdead7172u, 0xdead7172u, 0xdead0065u, 0xdead0000u, 0xdeadffffu, 0xdead7fffu, 0x38543656u, 0x00fa0038u, 0xbeef00efu, 0x0000beefu, 0x00003307u, 0x3c003c00u}, + {0xdead30a5u, 0xdead34a9u, 0xdead2f30u, 0xdead3330u, 0xdead6f30u, 0xdead6f30u, 0xdead0025u, 0xdead0000u, 0xdeadffffu, 0xdead7fffu, 0x389530a5u, 0x006800aeu, 0xbeef00c4u, 0x0000beefu, 0x00003975u, 0x3c003c00u}, + {0xdead2cc5u, 0xdead30cau, 0xdead2f89u, 0xdead3389u, 0xdead6f89u, 0xdead6f89u, 0xdead0013u, 0xdead7bffu, 0xdeadffffu, 0xdead7fffu, 0x3bb82cc5u, 0x004d00edu, 0xbeef00a9u, 0x7bffbeefu, 0x00003b6fu, 0x3c003c00u}, + {0xdead3b57u, 0xdeadb18bu, 0xdead3269u, 0xdead3669u, 0xdead7269u, 0xdead7269u, 0xdeadffeau, 0xdead8000u, 0xdeadffffu, 0xdead8000u, 0x38a53b57u, 0x0028004du, 0xbeef00eau, 0x8000beefu, 0x000034d5u, 0x3c003c00u}, + {0xdead3747u, 0xdead3b4fu, 0xdead2b53u, 0xdead2f53u, 0xdead6b53u, 0xdead6b53u, 0xdead0074u, 0xdead7f89u, 0xdeadffffu, 0xdead7fffu, 0x38243747u, 0x004a00efu, 0xbeef0096u, 0x7f89beefu, 0x00003b7fu, 0x3c003c00u}, + {0xdead3affu, 0xdeadb428u, 0xdead2d24u, 0xdead3124u, 0xdead6d24u, 0xdead6d24u, 0xdeadffdfu, 0xdead7bffu, 0xdeadffffu, 0xdead7fffu, 0x3a973affu, 0x00af001cu, 0xbeef0035u, 0x7bffbeefu, 0x00002f07u, 0x3c003c00u}, + {0xdead3996u, 0xdeadb8eau, 0xdead3b0fu, 0xdeadb38au, 0xdead7b0fu, 0xdeadef8au, 0xdeadffb2u, 0xdeadfbffu, 0xdeadffffu, 0xdead8000u, 0x39353996u, 0x00a500b2u, 0xbeef00e5u, 0xfbffbeefu, 0x00003996u, 0x3c003c00u}, + {0xdead33e8u, 0xdead37f0u, 0xdead3a95u, 0xdeadb5abu, 0xdead7a95u, 0xdeadf1abu, 0xdead003fu, 0xdeadfbffu, 0xdeadffffu, 0xdead8000u, 0x3b6733e8u, 0x000200fcu, 0xbeef0033u, 0xfbffbeefu, 0x00003be8u, 0x3c003c00u}, + {0xdead393du, 0xdeadb99bu, 0xdead3916u, 0xdeadb9d4u, 0xdead7916u, 0xdeadf5d4u, 0xdeadffa7u, 0xdead7bffu, 0xdeadffffu, 0xdead7fffu, 0x3a66393du, 0x00620066u, 0xbeef00cbu, 0x7bffbeefu, 0x00003666u, 0x3c003c00u}, + {0xdead37a8u, 0xdead3bafu, 0xdead3a93u, 0xdeadb5b5u, 0xdead7a93u, 0xdeadf1b5u, 0xdead007au, 0xdead7bffu, 0xdeadffffu, 0xdead7fffu, 0x368737a8u, 0x002a00c0u, 0xbeef00fau, 0x7bffbeefu, 0x00003a06u, 0x3c003c00u}, + {0xdead34f5u, 0xdead38fau, 0xdead2e4cu, 0xdead324cu, 0xdead6e4cu, 0xdead6e4cu, 0xdead004fu, 0xdeade15bu, 0xdeadffffu, 0xdead8000u, 0x2dc634f5u, 0x009a0011u, 0xbeef0080u, 0xe15bbeefu, 0x00002c44u, 0x3c003c00u}, + {0xdead3b27u, 0xdeadb30eu, 0xdead38e6u, 0xdeadba35u, 0xdead78e6u, 0xdeadf635u, 0xdeadffe4u, 0xdeadfbffu, 0xdeadffffu, 0xdead8000u, 0x3be03b27u, 0x008d001au, 0xbeef00f2u, 0xfbffbeefu, 0x00002e87u, 0x3c003c00u}, + {0xdead3955u, 0xdeadb96bu, 0xdead3b78u, 0xdeadb03fu, 0xdead7b78u, 0xdeadec3fu, 0xdeadffaau, 0xdeadfbffu, 0xdeadffffu, 0xdead8000u, 0x38543955u, 0x004b0015u, 0xbeef00e9u, 0xfbffbeefu, 0x00002d45u, 0x3c003c00u}, +}; +constexpr const char* Names[16] = {"8unorm", "8snorm", "16unorm", "16snorm", "16uscaled", "16sscaled", "8sint", "32float", "32uint", "32sint", "xy_8_8unorm", "xyz_lo", "xyz_hi", "hi_x_32float", "xyzw_lo", "xyzw_hi"}; + +void Fill(std::uint32_t tid, std::uint32_t* words) { + std::copy(std::begin(Rows[tid]), std::end(Rows[tid]), words); +} + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x01016facu}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +void Expect(std::uint32_t tid, std::uint32_t actual, std::uint32_t expected, const char* name) { + Require(actual == expected, std::string("format d16: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected)); +} + +void Run(AgcDriver::VulkanDevice& device) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]); + Output.fill(0xdeadbeefu); + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::span code(Code); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + const auto result = ShaderRecompiler::Recompile(request); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void Check() { + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const std::uint32_t* in = &Input[tid * Inputs]; + const std::uint32_t* out = &Output[tid * Results]; + for (std::uint32_t index = 0; index < 16; ++index) Expect(tid, out[index], Expected[tid][index], Names[index]); + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + std::puts("format d16 tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp index f5020e2a8..996f4e0a0 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp @@ -40,6 +40,7 @@ struct MemoryInfo { bool dataSigned = false; bool typed = false; bool formatted = false; + bool d16 = false; bool imageHasMip = false; bool imageR128 = false; bool idxen = false; diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index bb74fb55c..7ae7a6437 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -652,6 +652,15 @@ enum class RdnaOpcode : std::uint16_t { TbufferLoadFormatXy, TbufferLoadFormatXyz, TbufferLoadFormatXyzw, + TbufferLoadFormatD16X, + TbufferLoadFormatD16Xy, + TbufferLoadFormatD16Xyz, + TbufferLoadFormatD16Xyzw, + BufferLoadFormatD16X, + BufferLoadFormatD16Xy, + BufferLoadFormatD16Xyz, + BufferLoadFormatD16Xyzw, + BufferLoadFormatD16HiX, TbufferStoreFormatX, TbufferStoreFormatXy, TbufferStoreFormatXyz, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp index da5b0ab52..67ce0bf3e 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp @@ -18,10 +18,13 @@ RdnaOperand d16Half(RdnaOperand operand, RdnaOpcode opcode) { case RdnaOpcode::FlatLoadShortD16: case RdnaOpcode::DsReadU8D16: case RdnaOpcode::DsReadI8D16: - case RdnaOpcode::DsReadU16D16: operand.sdwaSel = 4u; break; + case RdnaOpcode::DsReadU16D16: + case RdnaOpcode::BufferLoadFormatD16X: + case RdnaOpcode::TbufferLoadFormatD16X: operand.sdwaSel = 4u; break; case RdnaOpcode::BufferLoadUbyteD16Hi: case RdnaOpcode::BufferLoadSbyteD16Hi: case RdnaOpcode::BufferLoadShortD16Hi: + case RdnaOpcode::BufferLoadFormatD16HiX: case RdnaOpcode::BufferStoreByteD16Hi: case RdnaOpcode::BufferStoreShortD16Hi: case RdnaOpcode::FlatLoadUbyteD16Hi: @@ -91,6 +94,7 @@ constexpr MemoryOpcodeInfo mubufOpcodes[] = { {0x23u, RdnaOpcode::BufferLoadSbyteD16Hi, 1, 8, true, false, false}, {0x24u, RdnaOpcode::BufferLoadShortD16, 1, 16, false, false, false}, {0x25u, RdnaOpcode::BufferLoadShortD16Hi, 1, 16, false, false, false}, + {0x26u, RdnaOpcode::BufferLoadFormatD16HiX, 1, 32, false, false, true}, {0x19u, RdnaOpcode::BufferStoreByteD16Hi, 1, 8, false, false, false}, {0x1bu, RdnaOpcode::BufferStoreShortD16Hi, 1, 16, false, false, false}, {0x30u, RdnaOpcode::BufferAtomicSwap, 1, 32, false, false, false}, @@ -108,6 +112,10 @@ constexpr MemoryOpcodeInfo mubufOpcodes[] = { {0x40u, RdnaOpcode::BufferAtomicFmax, 1, 32, false, false, false}, {0x50u, RdnaOpcode::BufferAtomicSwapX2, 2, 32, false, false, false}, {0x5au, RdnaOpcode::BufferAtomicOrX2, 2, 32, false, false, false}, + {0x80u, RdnaOpcode::BufferLoadFormatD16X, 1, 32, false, false, true}, + {0x81u, RdnaOpcode::BufferLoadFormatD16Xy, 2, 32, false, false, true}, + {0x82u, RdnaOpcode::BufferLoadFormatD16Xyz, 3, 32, false, false, true}, + {0x83u, RdnaOpcode::BufferLoadFormatD16Xyzw, 4, 32, false, false, true}, }; constexpr MemoryOpcodeInfo mtbufOpcodes[] = { @@ -119,6 +127,10 @@ constexpr MemoryOpcodeInfo mtbufOpcodes[] = { {0x05u, RdnaOpcode::TbufferStoreFormatXy, 2, 32, false, true, true}, {0x06u, RdnaOpcode::TbufferStoreFormatXyz, 3, 32, false, true, true}, {0x07u, RdnaOpcode::TbufferStoreFormatXyzw, 4, 32, false, true, true}, + {0x08u, RdnaOpcode::TbufferLoadFormatD16X, 1, 32, false, true, true}, + {0x09u, RdnaOpcode::TbufferLoadFormatD16Xy, 2, 32, false, true, true}, + {0x0au, RdnaOpcode::TbufferLoadFormatD16Xyz, 3, 32, false, true, true}, + {0x0bu, RdnaOpcode::TbufferLoadFormatD16Xyzw, 4, 32, false, true, true}, }; constexpr MemoryOpcodeInfo flatOpcodes[] = { @@ -598,7 +610,7 @@ RdnaInstruction DecodeRdnaMtbuf(std::uint32_t programCounter, std::span #include +#include #include #include #include @@ -102,6 +103,128 @@ std::uint32_t NormalizeFormatComponent(SpirvEmitterState& state, const SpirvBuff } } +namespace { + +std::uint32_t Ext(SpirvEmitterState& state, std::uint32_t type, std::uint32_t op, std::uint32_t lhs, std::uint32_t rhs) { + const auto result = state.module.AllocateId(); + state.module.AddFunction(spv::OpExtInst, type, result, GlslStd450(state), op, lhs, rhs); + return result; +} + +std::uint32_t FindMsb(SpirvEmitterState& state, std::uint32_t value) { + const auto result = state.module.AllocateId(); + state.module.AddFunction(spv::OpExtInst, TypeU32(state), result, GlslStd450(state), GLSLstd450FindUMsb, value); + return result; +} + +std::uint32_t UnsignedGreater(SpirvEmitterState& state, std::uint32_t lhs, std::uint32_t rhs) { + return Binary(state, spv::OpUGreaterThan, TypeBool(state), lhs, rhs); +} + +std::uint32_t SplitSign(SpirvEmitterState& state, std::uint32_t raw, std::uint32_t& magnitude) { + const auto negative = Binary(state, spv::OpSLessThan, TypeBool(state), EmitTBufferBitcastU32ToI32(state, raw), EmitTBufferBitcastU32ToI32(state, ConstantU32(state, 0u))); + magnitude = EmitSelectValueU32(state, negative, Unary(state, spv::OpSNegate, TypeU32(state), raw), raw); + return EmitSelectValueU32(state, negative, ConstantU32(state, 0x8000u), ConstantU32(state, 0u)); +} + +std::uint32_t NormToF16Bits(SpirvEmitterState& state, std::uint32_t value, std::uint32_t maximum) { + const std::uint32_t width = 32u - static_cast(std::countl_zero(maximum)); + const auto zero = EmitCompareU32Constant(state, spv::OpIEqual, value, 0u); + const auto safe = EmitSelectValueU32(state, zero, ConstantU32(state, 1u), value); + const auto first = EmitBinaryU32(state, spv::OpISub, ConstantU32(state, 9u + width), FindMsb(state, safe)); + const auto low = Binary(state, spv::OpULessThan, TypeBool(state), EmitBinaryU32(state, spv::OpShiftLeftLogical, safe, first), ConstantU32(state, 1024u * maximum)); + const auto adjusted = EmitSelectValueU32(state, low, EmitAddU32(state, first, ConstantU32(state, 1u)), first); + const auto shift = Ext(state, TypeU32(state), GLSLstd450UMin, adjusted, ConstantU32(state, 24u)); + const auto scaled = EmitBinaryU32(state, spv::OpShiftLeftLogical, safe, shift); + const auto quotient = EmitBinaryU32(state, spv::OpUDiv, scaled, ConstantU32(state, maximum)); + const auto remainder = EmitBinaryU32(state, spv::OpUMod, scaled, ConstantU32(state, maximum)); + const auto up = UnsignedGreater(state, remainder, ConstantU32(state, maximum >> 1u)); + const auto rounded = EmitAddU32(state, quotient, EmitSelectValueU32(state, up, ConstantU32(state, 1u), ConstantU32(state, 0u))); + const auto exponent = EmitBinaryU32(state, spv::OpShiftLeftLogical, EmitBinaryU32(state, spv::OpISub, ConstantU32(state, 25u), shift), ConstantU32(state, 10u)); + const auto bits = EmitBinaryU32(state, spv::OpISub, EmitAddU32(state, exponent, rounded), ConstantU32(state, 1024u)); + return EmitSelectValueU32(state, zero, ConstantU32(state, 0u), bits); +} + +std::uint32_t IntegerToF16Bits(SpirvEmitterState& state, std::uint32_t value) { + const auto zero = EmitCompareU32Constant(state, spv::OpIEqual, value, 0u); + const auto msb = FindMsb(state, EmitSelectValueU32(state, zero, ConstantU32(state, 1u), value)); + const auto wide = UnsignedGreater(state, msb, ConstantU32(state, 10u)); + const auto left = EmitSelectValueU32(state, wide, ConstantU32(state, 0u), EmitBinaryU32(state, spv::OpISub, ConstantU32(state, 10u), msb)); + const auto right = EmitSelectValueU32(state, wide, EmitBinaryU32(state, spv::OpISub, msb, ConstantU32(state, 10u)), ConstantU32(state, 0u)); + const auto quotient = EmitBinaryU32(state, spv::OpShiftRightLogical, EmitBinaryU32(state, spv::OpShiftLeftLogical, value, left), right); + const auto unit = EmitBinaryU32(state, spv::OpShiftLeftLogical, ConstantU32(state, 1u), right); + const auto remainder = EmitBinaryU32(state, spv::OpBitwiseAnd, value, EmitBinaryU32(state, spv::OpISub, unit, ConstantU32(state, 1u))); + const auto half = EmitBinaryU32(state, spv::OpShiftRightLogical, unit, ConstantU32(state, 1u)); + 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), odd); + const auto up = EmitLogicalAndBool(state, wide, EmitLogicalOrBool(state, UnsignedGreater(state, remainder, half), tie)); + const auto rounded = EmitAddU32(state, quotient, EmitSelectValueU32(state, up, ConstantU32(state, 1u), ConstantU32(state, 0u))); + const auto exponent = EmitBinaryU32(state, spv::OpShiftLeftLogical, EmitAddU32(state, msb, ConstantU32(state, 15u)), ConstantU32(state, 10u)); + const auto bits = EmitBinaryU32(state, spv::OpISub, EmitAddU32(state, exponent, rounded), ConstantU32(state, 1024u)); + return EmitSelectValueU32(state, zero, ConstantU32(state, 0u), Ext(state, TypeU32(state), GLSLstd450UMin, bits, ConstantU32(state, 0x7bffu))); +} + +std::uint32_t F32BitsToF16BitsRtz(SpirvEmitterState& state, std::uint32_t bits) { + const auto sign = EmitAndConstant(state, EmitBinaryU32(state, spv::OpShiftRightLogical, bits, ConstantU32(state, 16u)), 0x8000u); + const auto exponent = EmitAndConstant(state, EmitBinaryU32(state, spv::OpShiftRightLogical, bits, ConstantU32(state, 23u)), 0xffu); + const auto mantissa = EmitAndConstant(state, bits, 0x7fffffu); + const auto top = EmitBinaryU32(state, spv::OpShiftRightLogical, mantissa, ConstantU32(state, 13u)); + const auto lostPayload = EmitLogicalAndBool(state, EmitCompareU32Constant(state, spv::OpIEqual, top, 0u), EmitCompareU32Constant(state, spv::OpINotEqual, mantissa, 0u)); + const auto special = EmitOrU32(state, ConstantU32(state, 0x7c00u), EmitSelectValueU32(state, lostPayload, ConstantU32(state, 1u), top)); + const auto biased = Binary(state, spv::OpISub, TypeI32(state), EmitTBufferBitcastU32ToI32(state, exponent), EmitTBufferBitcastU32ToI32(state, ConstantU32(state, 112u))); + const auto atLeast = [&](std::uint32_t limit) { + return Binary(state, spv::OpSGreaterThanEqual, TypeBool(state), biased, EmitTBufferBitcastU32ToI32(state, ConstantU32(state, limit))); + }; + const auto biasedU = Unary(state, spv::OpBitcast, TypeU32(state), biased); + const auto normal = EmitOrU32(state, EmitBinaryU32(state, spv::OpShiftLeftLogical, biasedU, ConstantU32(state, 10u)), top); + const auto subnormalShift = EmitBinaryU32(state, spv::OpISub, ConstantU32(state, 14u), biasedU); + const auto subnormal = EmitBinaryU32(state, spv::OpShiftRightLogical, EmitOrU32(state, mantissa, ConstantU32(state, 0x800000u)), Ext(state, TypeU32(state), GLSLstd450UMin, subnormalShift, ConstantU32(state, 31u))); + auto magnitude = EmitSelectValueU32(state, atLeast(static_cast(-10)), subnormal, ConstantU32(state, 0u)); + magnitude = EmitSelectValueU32(state, atLeast(1u), normal, magnitude); + magnitude = EmitSelectValueU32(state, atLeast(31u), ConstantU32(state, 0x7bffu), magnitude); + magnitude = EmitSelectValueU32(state, EmitCompareU32Constant(state, spv::OpIEqual, exponent, 0xffu), special, magnitude); + return EmitOrU32(state, sign, magnitude); +} + +} + +std::uint32_t EmitD16FormatComponent(SpirvEmitterState& state, const SpirvBufferFormatInfo& info, std::uint32_t component, std::uint32_t raw) { + const auto bits = info.componentBits[component]; + if (info.type != SpirvFormatComponentType::Float && bits > 16u) { + if (info.type == SpirvFormatComponentType::Uint) return Ext(state, TypeU32(state), GLSLstd450UMin, raw, ConstantU32(state, 0xffffu)); + if (info.type == SpirvFormatComponentType::Sint) { + const auto clamped = Ext(state, TypeI32(state), GLSLstd450SMax, EmitTBufferBitcastU32ToI32(state, raw), EmitTBufferBitcastU32ToI32(state, ConstantU32(state, 0xffff8000u))); + const auto limited = Ext(state, TypeI32(state), GLSLstd450SMin, clamped, EmitTBufferBitcastU32ToI32(state, ConstantU32(state, 0x7fffu))); + return EmitAndConstant(state, Unary(state, spv::OpBitcast, TypeU32(state), limited), 0xffffu); + } + FailEmit("D16 conversion of a component wider than 16 bits is not supported"); + } + std::uint32_t magnitude = raw; + switch (info.type) { + case SpirvFormatComponentType::Uint: + case SpirvFormatComponentType::Sint: + return EmitAndConstant(state, raw, 0xffffu); + case SpirvFormatComponentType::Unorm: + return NormToF16Bits(state, raw, (1u << bits) - 1u); + case SpirvFormatComponentType::Snorm: { + const auto sign = SplitSign(state, raw, magnitude); + const std::uint32_t maximum = (1u << (bits - 1u)) - 1u; + return EmitOrU32(state, sign, NormToF16Bits(state, Ext(state, TypeU32(state), GLSLstd450UMin, magnitude, ConstantU32(state, maximum)), maximum)); + } + case SpirvFormatComponentType::Uscaled: + return IntegerToF16Bits(state, raw); + case SpirvFormatComponentType::Sscaled: { + const auto sign = SplitSign(state, raw, magnitude); + return EmitOrU32(state, sign, IntegerToF16Bits(state, magnitude)); + } + case SpirvFormatComponentType::Float: + if (bits == 16u) return EmitAndConstant(state, raw, 0xffffu); + return F32BitsToF16BitsRtz(state, NormalizeFormatComponent(state, info, component, raw)); + default: + FailEmit("buffer format component type is not supported"); + } +} + void EmitDeviceAtomicMemoryBarrier(SpirvEmitterState& state) { // GCN atomics imply no fence; this device-scope barrier is the acquire side of lock/publish // patterns (atomic, then loads of data other waves wrote). It also makes every non-returning diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp index 329d9ea00..f9e2031ae 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp @@ -326,6 +326,8 @@ std::uint32_t LoadFormattedComponent(SpirvValueEmitContext& ctx, const MemoryInf auto& state = ctx.state; const auto source = ResolveOutputSource(ctx, mem, info, outputComponent); if (source.kind != SpirvFormattedSourceKind::Memory) { + const bool integer = info.type == SpirvFormatComponentType::Uint || info.type == SpirvFormatComponentType::Sint; + if (mem.d16 && source.kind == SpirvFormattedSourceKind::One && !integer) return ConstantU32(state, 0x3c00u); return FormattedConstant(ctx, info, source.kind); } const auto component = source.component; @@ -344,12 +346,13 @@ std::uint32_t LoadFormattedComponent(SpirvValueEmitContext& ctx, const MemoryInf } else { raw = loadSubword(component, bits, signedType); } - return NormalizeFormatComponent(state, info, component, raw); + return mem.d16 ? EmitD16FormatComponent(state, info, component, raw) : NormalizeFormatComponent(state, info, component, raw); } std::uint32_t FormattedLoadPrepared(SpirvValueEmitContext& ctx, const IrValue& inst, const MemoryInfo& mem, std::uint32_t outputComponent, const MemoryResourceAccess& resource) { const auto info = MemoryFormatInfo(ctx.state, mem); if (info.type == SpirvFormatComponentType::Unknown) { + if (mem.d16) ctx.Fail(inst, "is a D16 format load with an unknown buffer format"); return LoadWordPrepared(ctx, inst, RebaseRawComponent(mem, outputComponent), resource); } return LoadFormattedComponent(ctx, mem, info, outputComponent, [&](std::uint32_t component) { diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 876ab3162..79b518971 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -84,6 +84,7 @@ private: IrValue* narrowSubdword(IrU32 value, std::uint32_t bits); bool sLoad(const RdnaInstruction& inst, bool raw); bool bufferLoad(const RdnaInstruction& inst); + bool bufferLoadFormatD16(const RdnaInstruction& inst); bool bufferStore(const RdnaInstruction& inst); bool bufferAtomic(const RdnaInstruction& inst, IrOpcode opcode); bool imageAtomic(const RdnaInstruction& inst, IrOpcode opcode); diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/BufferInstructions.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/BufferInstructions.cpp index fa9006ed7..b51e0c8c9 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/BufferInstructions.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/BufferInstructions.cpp @@ -1,5 +1,6 @@ #include "Translation/MemoryInstructions.hpp" #include "Translation/TranslationContext.hpp" +#include #include namespace ShaderRecompiler { @@ -80,6 +81,40 @@ bool TranslationContext::bufferLoad(const RdnaInstruction& inst) { return true; } +bool TranslationContext::bufferLoadFormatD16(const RdnaInstruction& inst) { + MemoryInfo memory = bufferMemoryInfoFromInstruction(inst); + memory.coherent = inst.glc || inst.dlc; + memory.d16 = true; + static constexpr std::array opcodes{IrOpcode::LoadBufferU32, IrOpcode::LoadBufferU32x2, IrOpcode::LoadBufferU32x3, IrOpcode::LoadBufferU32x4}; + const std::uint32_t count = memory.dataDwords; + if (count == 0u || count > opcodes.size()) { + return false; + } + const IrOpcode opcode = opcodes[count - 1u]; + IrValue* resource = getBufferResource(memory); + const BufferAddress address = readBufferAddress(inst); + IrValue& exec = ir.GetExec(); + IrValue& loaded = ir.Emit(opcode, IrOpcodeType(opcode), {resource, &address.index.Value(), &address.offset.Value(), &address.soffset.Value(), &exec}, addMemoryInfo(memory, inst.programCounter)); + if (count == 1u) { + writeRawU32(inst.destination, IrU32(loaded)); + return true; + } + std::array halves{}; + for (std::uint32_t component = 0u; component < count; ++component) { + halves[component] = &ir.CompositeExtract(loaded, component); + } + for (std::uint32_t component = 0u; component < count; component += 2u) { + RdnaOperand target = offsetOperand(inst.destination, component / 2u); + if (component + 1u == count) { + target.sdwaSel = 4u; + writeRawU32(target, IrU32(*halves[component])); + } else { + writeRawU32(target, IrU32(ir.BitwiseOr(*halves[component], ir.ShiftLeftLogical(*halves[component + 1u], ir.Constant(16u))))); + } + } + return true; +} + bool TranslationContext::bufferStore(const RdnaInstruction& inst) { MemoryInfo memory = bufferMemoryInfoFromInstruction(inst); memory.coherent = inst.glc || inst.dlc; diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp index c5faca3d5..70e24ba22 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp @@ -68,6 +68,16 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) { case RdnaOpcode::TbufferLoadFormatXyz: case RdnaOpcode::TbufferLoadFormatXyzw: return bufferLoad(inst); + case RdnaOpcode::BufferLoadFormatD16X: + case RdnaOpcode::BufferLoadFormatD16Xy: + case RdnaOpcode::BufferLoadFormatD16Xyz: + case RdnaOpcode::BufferLoadFormatD16Xyzw: + case RdnaOpcode::BufferLoadFormatD16HiX: + case RdnaOpcode::TbufferLoadFormatD16X: + case RdnaOpcode::TbufferLoadFormatD16Xy: + case RdnaOpcode::TbufferLoadFormatD16Xyz: + case RdnaOpcode::TbufferLoadFormatD16Xyzw: + return bufferLoadFormatD16(inst); case RdnaOpcode::BufferStoreFormatX: case RdnaOpcode::BufferStoreFormatXy: