diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/Vop3PackLdexp.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/Vop3PackLdexp.cpp new file mode 100644 index 000000000..baa0da0a1 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/Vop3PackLdexp.cpp @@ -0,0 +1,169 @@ +#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, 0xe0381000, 0x80000401, 0xbf8c3f70, 0xd768000a, 0x02020b04, 0xd768020b, + 0x22020b04, 0xd769000c, 0x02020b04, 0xd769010d, 0x62020b04, 0xd762000e, 0x02020d04, 0xd762010f, + 0x22020d04, 0xd7620010, 0x02020f05, 0xd54d0011, 0x041a0b04, 0xd7110012, 0x02020b04, 0xd7110213, + 0x22020b04, 0xd7111814, 0x02020b04, 0xd7111115, 0x22020b04, 0xd56b0016, 0x02020b04, 0xd56b0017, + 0x02020f06, 0xe0781000, 0x80010a03, 0xe0781010, 0x80010e03, 0xe0781020, 0x80011203, 0xe0781030, + 0x80011603, 0xbf810000, +}; + +constexpr std::uint32_t Rows[32][4] = { + {0x3fc00000u, 0x00800000u, 0x00000080u, 0xffffffffu}, + {0xbf800001u, 0x80ffffffu, 0xffffff81u, 0x00000000u}, + {0x3f800000u, 0x7f000000u, 0x0000007fu, 0x00000000u}, + {0x40000000u, 0x00800001u, 0xffffff82u, 0xfffffffeu}, + {0x3f7fff80u, 0x3effff00u, 0x00000000u, 0x00000001u}, + {0xbf000000u, 0x3f000000u, 0x00000115u, 0x348abf59u}, + {0x382000a0u, 0xffc00000u, 0xffffff00u, 0x0000007fu}, + {0x7fa12345u, 0xb8400180u, 0x367c8d22u, 0xffff0000u}, + {0x37800101u, 0x3840017fu, 0x80000001u, 0x000000fdu}, + {0x00000000u, 0x00000001u, 0xffffff69u, 0xffffff00u}, + {0xd62c214cu, 0x7f800001u, 0x000000feu, 0xffffff6bu}, + {0xffc00000u, 0x3ac900c9u, 0xffffffffu, 0x00000116u}, + {0x38a00141u, 0xff800000u, 0x00000000u, 0x00000098u}, + {0x628aa774u, 0x807fffffu, 0xffffff69u, 0xffffff81u}, + {0x7f800000u, 0x3b490191u, 0x0fdc7365u, 0x00000080u}, + {0xff800000u, 0x7f800000u, 0x00000000u, 0x00000116u}, + {0x807fffffu, 0x80000000u, 0x00000096u, 0x00000080u}, + {0x7f800001u, 0x3a800000u, 0x00001000u, 0x00000082u}, + {0x37c000c0u, 0x7fa12345u, 0xffffffffu, 0x7f703ee0u}, + {0x00000001u, 0xab16df9cu, 0xa053bb90u, 0x55e635d7u}, + {0x8dc822deu, 0x00000000u, 0x7fffffffu, 0x002baa81u}, + {0xffc00000u, 0xffbfffffu, 0x00000097u, 0x00000082u}, + {0x80000000u, 0x2d6e2b74u, 0xffffff6bu, 0x9fb7084au}, + {0xf2d7d40fu, 0x7f800001u, 0x00000100u, 0x7ffffffeu}, + {0x3c000000u, 0x80000000u, 0xfffffe00u, 0x1b1c3f27u}, + {0x3efffe01u, 0x3f000001u, 0xffffff6bu, 0xffffffffu}, + {0x3f000000u, 0xb8a0013fu, 0x00000096u, 0x5cdff86du}, + {0x3b490192u, 0x4801920eu, 0x00000115u, 0x7fffffffu}, + {0x3ac900cau, 0x37c000c1u, 0x00000081u, 0x00000082u}, + {0xbf7ffcffu, 0x32135b34u, 0x00000017u, 0x00000002u}, + {0x38a00141u, 0x3b490193u, 0xffffff82u, 0x8acac024u}, + {0x3820009fu, 0x3f7fffc0u, 0x0000007eu, 0x80000000u} +}; +constexpr std::uint32_t Expected[32][14] = { + {0x00007fffu, 0x00008001u, 0x0000ffffu, 0x00000000u, 0x7f800000u, 0xff800000u, 0x00000000u, 0x1fa00000u, 0x00000000u, 0x00008000u, 0x00803fc0u, 0x00808000u, 0x00000000u, 0xffffff80u}, + {0x00008001u, 0x00007fffu, 0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x80ffffffu, 0xa0c08080u, 0xffff0001u, 0x7fff8001u, 0x80ffbf80u, 0x80ff8001u, 0xc17fffffu, 0x00000000u}, + {0x7fff7fffu, 0x7fff8001u, 0xffffffffu, 0x00000000u, 0x7f000000u, 0xff000000u, 0x7f000000u, 0x5f400000u, 0x00000000u, 0x00008000u, 0x7f003f80u, 0x7f008000u, 0x00000000u, 0x00000000u}, + {0x00007fffu, 0x00008001u, 0x0000ffffu, 0x00000000u, 0x01000000u, 0x81000000u, 0x00000000u, 0x20400000u, 0x00010000u, 0x00018000u, 0x00804000u, 0x00808000u, 0x40000000u, 0x000000fcu}, + {0x3fff7fffu, 0x3fff8001u, 0x7fffffffu, 0x00000000u, 0x3f7fff80u, 0xbf7fff80u, 0x3f7fff00u, 0x3ebfff40u, 0xff00ff80u, 0x7f007f80u, 0x3eff3f7fu, 0x3effff80u, 0x00008000u, 0x00000000u}, + {0x4000c000u, 0x40004000u, 0x80000000u, 0x00000000u, 0xff800000u, 0xff800000u, 0x7f800000u, 0x7f000000u, 0x00000000u, 0x00008000u, 0x3f00bf00u, 0x3f008000u, 0x00000000u, 0xda210b4du}, + {0x00000001u, 0x0000ffffu, 0x00000002u, 0x00000000u, 0x00000000u, 0x80000000u, 0xffc00000u, 0x9c700050u, 0x000000a0u, 0x000080a0u, 0xffc03820u, 0xffc080a0u, 0xd8000000u, 0xffff8100u}, + {0xffff0000u, 0x00010000u, 0x00000000u, 0x00030000u, 0x7fa12345u, 0xffa12345u, 0x80000000u, 0x9b701262u, 0x01802345u, 0x0180a345u, 0xb8407fa1u, 0xb840a345u, 0xdaf4e780u, 0x72de0000u}, + {0x00010001u, 0x0001ffffu, 0x00030001u, 0x00000000u, 0x00000000u, 0x80000000u, 0x7f800000u, 0x37600140u, 0x017f0101u, 0x017f8101u, 0x38403780u, 0x38408101u, 0x80c1807fu, 0x800000fdu}, + {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000001u, 0x00010000u, 0x00018000u, 0x00000000u, 0x00008000u, 0x00000000u, 0x00009700u}, + {0x00008001u, 0x00007fffu, 0x00000000u, 0x00000000u, 0xff800000u, 0xff800000u, 0x7f800001u, 0xaa561026u, 0x0001214cu, 0x0001a14cu, 0x7f80d62cu, 0x7f80a14cu, 0x302c214cu, 0xffff6c2au}, + {0x00320000u, 0x00320000u, 0x00640000u, 0x00000000u, 0xffc00000u, 0xffc00000u, 0x7f800000u, 0x9dc50065u, 0x00c90000u, 0x00c98000u, 0x3ac9ffc0u, 0x3ac98000u, 0xcdc00000u, 0xfffffeeau}, + {0x80010003u, 0x7ffffffdu, 0x00000005u, 0xffff0000u, 0x38a00141u, 0xb8a00141u, 0xff800000u, 0x9b900020u, 0x00000141u, 0x00008141u, 0xff8038a0u, 0xff808141u, 0x5f800000u, 0x00000000u}, + {0x00007fffu, 0x00008001u, 0x0000ffffu, 0x00000000u, 0x170aa774u, 0x970aa774u, 0x80000000u, 0x7185d3bau, 0xffffa774u, 0x7fff2774u, 0x807f628au, 0x807fa774u, 0x5775588cu, 0x00004ae9u}, + {0x00647fffu, 0x00648001u, 0x00c9ffffu, 0x00000000u, 0x7f800000u, 0xff800000u, 0x7b490191u, 0x5d640149u, 0x01910000u, 0x01918000u, 0x3b497f80u, 0x3b498000u, 0xb7800000u, 0xee39b280u}, + {0x7fff8001u, 0x7fff7fffu, 0xffff0000u, 0x00000000u, 0xff800000u, 0xff800000u, 0x7f800000u, 0xbf800000u, 0x00000000u, 0x00008000u, 0x7f80ff80u, 0x7f808000u, 0x00000000u, 0x00000000u}, + {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x80000000u, 0x803f7f7fu, 0x0000ffffu, 0x00007fffu, 0x8000807fu, 0x8000ffffu, 0x80000000u, 0x00004b00u}, + {0x00200000u, 0x00200000u, 0x00400000u, 0x00000000u, 0x7f800001u, 0xff800001u, 0x7b800000u, 0x5c800000u, 0x00000001u, 0x00008001u, 0x3a807f80u, 0x3a808001u, 0x3a800000u, 0x00082000u}, + {0x00000001u, 0x0000ffffu, 0x00000001u, 0x00000000u, 0x374000c0u, 0xb74000c0u, 0x7fa12345u, 0x5bb11283u, 0x234500c0u, 0x234580c0u, 0x7fa137c0u, 0x7fa180c0u, 0xff9a73c0u, 0x808fc120u}, + {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0xff800000u, 0x550b704eu, 0xdf9c0001u, 0x5f9c8001u, 0xab160000u, 0xab168001u, 0xab16df9cu, 0x4f8755f0u}, + {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xff800000u, 0xff800000u, 0x00000000u, 0x4764116fu, 0x000022deu, 0x0000a2deu, 0x00008dc8u, 0x0000a2deu, 0x00000000u, 0x7fd4557fu}, + {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xffc00000u, 0xffc00000u, 0xffbfffffu, 0xffbf7f80u, 0xffff0000u, 0x7fff8000u, 0xffbfffc0u, 0xffbf8000u, 0x00400000u, 0x00004caeu}, + {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x5737163au, 0x2b740000u, 0x2b748000u, 0x2d6e8000u, 0x2d6e8000u, 0x00000000u, 0x0a782ceeu}, + {0x00008001u, 0x00007fffu, 0x00000000u, 0x00000000u, 0xff800000u, 0xff800000u, 0x7f800001u, 0xb8ab6a08u, 0x0001d40fu, 0x0001540fu, 0x7f80f2d7u, 0x7f80d40fu, 0x6b57d40fu, 0xfffffe00u}, + {0x00000100u, 0x0000ff00u, 0x00000200u, 0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x5e000000u, 0x00000000u, 0x00008000u, 0x80003c00u, 0x80008000u, 0x00000000u, 0xc781b200u}, + {0x40003fffu, 0x4000c001u, 0x80007fffu, 0x00000000u, 0x00000000u, 0x80000000u, 0x3e800001u, 0x3f807f01u, 0x0001fe01u, 0x00017e01u, 0x3f003effu, 0x3f00fe01u, 0x7dfffe01u, 0x00000095u}, + {0xfffe4000u, 0x0002c000u, 0x00008000u, 0x00050000u, 0x7f800000u, 0xff800000u, 0xff800000u, 0x7b50001fu, 0x013f0000u, 0x013f8000u, 0xb8a03f00u, 0xb8a08000u, 0x81000000u, 0x6b3b8fdeu}, + {0x7fff0064u, 0x7fffff9cu, 0xffff00c9u, 0x00000000u, 0x7f800000u, 0xff800000u, 0x7f800000u, 0x41254a50u, 0x920e0192u, 0x120e8192u, 0x48013b49u, 0x48018192u, 0xf27559fcu, 0x7ffffeebu}, + {0x00010032u, 0x0001ffceu, 0x00020065u, 0x00000000u, 0x7b4900cau, 0xfb4900cau, 0x78c000c1u, 0x38c400c6u, 0x00c100cau, 0x00c180cau, 0x37c03ac9u, 0x37c080cau, 0x4f09984au, 0x00004182u}, + {0x00008003u, 0x00007ffdu, 0x00000000u, 0x00000000u, 0xcafffcffu, 0xcafffcffu, 0x33135b34u, 0x7849ab9au, 0x5b34fcffu, 0x5b347cffu, 0x3213bf7fu, 0x3213fcffu, 0xf9db08ccu, 0x0000002eu}, + {0x00650003u, 0x0065fffdu, 0x00c90005u, 0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x3a75016au, 0x01930141u, 0x01938141u, 0x3b4938a0u, 0x3b498141u, 0x7a6af953u, 0xb0356e48u}, + {0x7fff0001u, 0x7fffffffu, 0xffff0002u, 0x00000000u, 0x7720009fu, 0xf720009fu, 0x00000000u, 0x3b4f7fafu, 0xffc0009fu, 0x7fc0809fu, 0x3f7f3820u, 0x3f7f809fu, 0x687fd840u, 0x00000000u} +}; +constexpr const char* Names[14] = {"cvt_pknorm_i16_f320", "cvt_pknorm_i16_f321", "cvt_pknorm_u16_f322", "cvt_pknorm_u16_f323", "ldexp_f324", "ldexp_f325", "ldexp_f326", "lerp_u87", "pack_b32_f168", "pack_b32_f169", "pack_b32_f1610", "pack_b32_f1611", "mul_lo_i32_0", "mul_lo_i32_1"}; + +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("vop3 pack ldexp: 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 i = 0; i < 14; ++i) Expect(tid, out[i], Expected[tid][i], Names[i]); + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + std::puts("vop3 pack ldexp 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/IrOpcode.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp index 8bd56b1f4..e627f6929 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp @@ -139,8 +139,6 @@ enum class IrOpcode : std::uint16_t { CompositeExtractU32x3, CompositeExtractU32x4, PackHalf2x16, - PackSnorm2x16, - PackUnorm2x16, PackFloat2x16Rtz, FPAbs32, FPNeg32, @@ -239,7 +237,6 @@ enum class IrOpcode : std::uint16_t { FPCos, FPExp2, FPLog2, - FPLdexp, FPRoundEven32, FPFloor32, FPCeil32, diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp index 158279a36..6d496354c 100644 --- a/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp @@ -180,8 +180,6 @@ constexpr std::array(IrOpcode::Count)> Meta makeMeta("CompositeExtractU32x3", U32, U32x3, U32), makeMeta("CompositeExtractU32x4", U32, U32x4, U32), makeMeta("PackHalf2x16", U32, F32x2), - makeMeta("PackSnorm2x16", U32, F32x2), - makeMeta("PackUnorm2x16", U32, F32x2), makeMeta("PackFloat2x16Rtz", U32, F32, F32), makeMeta("FPAbs32", F32, F32), makeMeta("FPNeg32", F32, F32), @@ -280,7 +278,6 @@ constexpr std::array(IrOpcode::Count)> Meta makeMeta("FPCos", F32, F32), makeMeta("FPExp2", F32, F32), makeMeta("FPLog2", F32, F32), - makeMeta("FPLdexp", F32, F32, U32), makeMeta("FPRoundEven32", F32, F32), makeMeta("FPFloor32", F32, F32), makeMeta("FPCeil32", F32, F32), diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp index 9926fdf1d..097f31c48 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp @@ -55,7 +55,6 @@ std::uint32_t EmitFPRecipSqrt32(SpirvEmitterState& state, std::uint32_t arg0); std::uint32_t EmitFPSqrt(SpirvEmitterState& state, std::uint32_t arg0); std::uint32_t EmitFPExp2(SpirvEmitterState& state, std::uint32_t arg0); std::uint32_t EmitFPLog2(SpirvEmitterState& state, std::uint32_t arg0); -std::uint32_t EmitFPLdexp(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1); std::uint32_t EmitFPSin(SpirvEmitterState& state, std::uint32_t arg0); std::uint32_t EmitFPCos(SpirvEmitterState& state, std::uint32_t arg0); std::uint32_t EmitIdentity(SpirvValueEmitContext& ctx, std::uint32_t value); @@ -170,8 +169,6 @@ std::uint32_t EmitFNegateValue(SpirvEmitterState& state, std::uint32_t arg0); std::uint32_t EmitFAbsValue(SpirvEmitterState& state, std::uint32_t arg0); std::uint32_t EmitPackHalf2x16(SpirvEmitterState& state, std::uint32_t arg0); std::uint32_t EmitF32ToF16BitsRte(SpirvEmitterState& state, std::uint32_t value); -std::uint32_t EmitPackSnorm2x16(SpirvEmitterState& state, std::uint32_t arg0); -std::uint32_t EmitPackUnorm2x16(SpirvEmitterState& state, std::uint32_t arg0); std::uint32_t EmitFPFma32(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1, std::uint32_t arg2); std::uint32_t EmitFPMad32(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1, std::uint32_t arg2); std::uint32_t EmitFPRoundEven32(SpirvEmitterState& state, std::uint32_t arg0); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp index 8c3ca95fc..e9202a8fa 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp @@ -465,11 +465,6 @@ std::uint32_t EmitFPLog2(SpirvEmitterState& state, std::uint32_t arg0) { return EmitExt(state, TypeF32(state), GLSLstd450Log2, {EmitFlushF32DenormToSignedZero(state, arg0)}); } -std::uint32_t EmitFPLdexp(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1) { - const auto exponent = Unary(state, spv::OpBitcast, TypeI32(state), arg1); - return EmitExt(state, TypeF32(state), GLSLstd450Ldexp, {arg0, exponent}); -} - std::uint32_t EmitFPSin(SpirvEmitterState& state, std::uint32_t arg0) { const auto cycle = EmitTrigCycleF32(state, arg0, true); const auto source = Binary(state, spv::OpFMul, TypeF32(state), cycle, ConstantF32(state, 0x40c90fdbu)); @@ -956,14 +951,6 @@ std::uint32_t EmitPackHalf2x16(SpirvEmitterState& state, std::uint32_t arg0) { return Binary(state, spv::OpBitwiseOr, TypeU32(state), component(0u), high); } -std::uint32_t EmitPackSnorm2x16(SpirvEmitterState& state, std::uint32_t arg0) { - return EmitGlsl(state, arg0); -} - -std::uint32_t EmitPackUnorm2x16(SpirvEmitterState& state, std::uint32_t arg0) { - return EmitGlsl(state, arg0); -} - std::uint32_t EmitFPFma32(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1, std::uint32_t arg2) { return Exact(state, EmitGlsl(state, arg0, arg1, arg2)); } diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp index 28f7bd86b..4d84894de 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp @@ -313,8 +313,6 @@ void EmitDirectInstruction(SpirvValueEmitContext& ctx, const IrValue& inst) { case IrOpcode::CompositeExtractU32x3: return Invoke(EmitCompositeExtractU32x3, ctx, inst); case IrOpcode::CompositeExtractU32x4: return Invoke(EmitCompositeExtractU32x4, ctx, inst); case IrOpcode::PackHalf2x16: return Invoke(EmitPackHalf2x16, ctx, inst); - case IrOpcode::PackSnorm2x16: return Invoke(EmitPackSnorm2x16, ctx, inst); - case IrOpcode::PackUnorm2x16: return Invoke(EmitPackUnorm2x16, ctx, inst); case IrOpcode::PackFloat2x16Rtz: return Invoke(EmitPackFloat2x16Rtz, ctx, inst); case IrOpcode::FPAbs32: return Invoke(EmitFPAbs32, ctx, inst); case IrOpcode::FPNeg32: return Invoke(EmitFPNeg32, ctx, inst); @@ -413,7 +411,6 @@ void EmitDirectInstruction(SpirvValueEmitContext& ctx, const IrValue& inst) { case IrOpcode::FPCos: return Invoke(EmitFPCos, ctx, inst); case IrOpcode::FPExp2: return Invoke(EmitFPExp2, ctx, inst); case IrOpcode::FPLog2: return Invoke(EmitFPLog2, ctx, inst); - case IrOpcode::FPLdexp: return Invoke(EmitFPLdexp, ctx, inst); case IrOpcode::FPRoundEven32: return Invoke(EmitFPRoundEven32, ctx, inst); case IrOpcode::FPFloor32: return Invoke(EmitFPFloor32, ctx, inst); case IrOpcode::FPCeil32: return Invoke(EmitFPCeil32, ctx, inst); diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 60181644a..44b4d9d4e 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -147,7 +147,7 @@ private: void vFrexpExpI32F32(const RdnaInstruction& inst); void vCvtOffF32I4(const RdnaInstruction& inst); void vCvtPkrtzF16F32(const RdnaInstruction& inst); - void vCvtPknormF32(const RdnaInstruction& inst, IrOpcode opcode); + void vCvtPknormF32(const RdnaInstruction& inst, bool signedValue); void vCvtPkU8F32(const RdnaInstruction& inst); void vPackB32F16(const RdnaInstruction& inst); bool packedFloat16(const RdnaInstruction& inst, IrOpcode opcode, bool accumulator, bool quietSnan); @@ -164,8 +164,10 @@ private: bool vDivFixupF32(const RdnaInstruction& inst); bool vFrexpMantF32(const RdnaInstruction& inst); bool vFmaLegacyF32(const RdnaInstruction& inst); + bool vLdexpF32(const RdnaInstruction& inst); IrU32 readF16Bits(const RdnaOperand& operand); IrU32 normF16(IrU32 bits, bool signedValue); + IrU32 normF32(IrU32 bits, bool signedValue); bool vLdexpF16(const RdnaInstruction& inst); bool vFrexpF16(const RdnaInstruction& inst, bool exponent); bool vCvtNormF16(const RdnaInstruction& inst, bool signedValue); diff --git a/core/shader/recompiler/Translation/src/ConvertInstructions.cpp b/core/shader/recompiler/Translation/src/ConvertInstructions.cpp index 00842b3b2..c42b4fe2b 100644 --- a/core/shader/recompiler/Translation/src/ConvertInstructions.cpp +++ b/core/shader/recompiler/Translation/src/ConvertInstructions.cpp @@ -151,11 +151,10 @@ void TranslationContext::vCvtPkrtzF16F32(const RdnaInstruction& inst) { writeOperand(inst.destination, &result.Value()); } -void TranslationContext::vCvtPknormF32(const RdnaInstruction& inst, IrOpcode opcode) { - IrValue* lhs = readOperand(sourceAt(inst, 0u), IrType::F32); - IrValue* rhs = readOperand(sourceAt(inst, 1u), IrType::F32); - IrValue& pair = ir.Emit(IrOpcode::CompositeConstructF32x2, IrType::F32x2, {lhs, rhs}); - const IrU32 result(ir.Emit(opcode, IrType::U32, {&pair})); +void TranslationContext::vCvtPknormF32(const RdnaInstruction& inst, bool signedValue) { + const IrU32 low = normF32(readU32(sourceAt(inst, 0u)), signedValue); + const IrU32 high = normF32(readU32(sourceAt(inst, 1u)), signedValue); + const IrU32 result(ir.BitwiseOr(low.Value(), ir.ShiftLeftLogical(high.Value(), ir.Constant(16u)))); writeOperand(inst.destination, &result.Value()); } @@ -171,8 +170,8 @@ void TranslationContext::vCvtPkU8F32(const RdnaInstruction& inst) { } void TranslationContext::vPackB32F16(const RdnaInstruction& inst) { - const IrU32 low = readF16LaneBits(sourceAt(inst, 0u), false); - const IrU32 high(ir.ShiftLeftLogical(readF16LaneBits(sourceAt(inst, 1u), false).Value(), ir.Constant(16u))); + const IrU32 low = readF16Bits(sourceAt(inst, 0u)); + const IrU32 high(ir.ShiftLeftLogical(readF16Bits(sourceAt(inst, 1u)).Value(), ir.Constant(16u))); const IrU32 result(ir.BitwiseOr(low.Value(), high.Value())); writeOperand(inst.destination, &result.Value()); } diff --git a/core/shader/recompiler/Translation/src/FloatInstructions.cpp b/core/shader/recompiler/Translation/src/FloatInstructions.cpp index 6d7f3d8ea..cbe1314ac 100644 --- a/core/shader/recompiler/Translation/src/FloatInstructions.cpp +++ b/core/shader/recompiler/Translation/src/FloatInstructions.cpp @@ -216,6 +216,31 @@ IrU32 TranslationContext::normF16(IrU32 bits, bool signedValue) { return IrU32(ir.BitwiseAnd(ir.Select(nan.Value(), ir.Constant(0u), value.Value()), ir.Constant(0xffffu))); } +IrU32 TranslationContext::normF32(IrU32 bits, bool signedValue) { + const IrU32 magnitude(ir.BitwiseAnd(bits.Value(), ir.Constant(0x7fffffffu))); + const IrU1 negative(ir.INotEqual(ir.BitwiseAnd(bits.Value(), ir.Constant(0x80000000u)), ir.Constant(0u))); + const IrU1 nan(ir.UGreaterThan(magnitude.Value(), ir.Constant(0x7f800000u))); + const IrU1 saturated(ir.Emit(IrOpcode::UGreaterThanEqual32, IrType::U1, {&magnitude.Value(), &ir.Constant(0x3f800000u)})); + const std::uint32_t scale = signedValue ? 32767u : 65535u; + const IrU32 exponent(ir.ShiftRightLogical(magnitude.Value(), ir.Constant(23u))); + const IrU32 mantissa(ir.BitwiseOr(ir.BitwiseAnd(magnitude.Value(), ir.Constant(0x7fffffu)), ir.Constant(0x800000u))); + const IrU32 shift(ir.Emit(IrOpcode::UMin32, IrType::U32, {&ir.ISub(ir.Constant(149u), exponent.Value()), &ir.Constant(63u)})); + const IrU64 product(ir.Emit(IrOpcode::IMul64, IrType::U64, {&ir.ConstructU64(mantissa.Value(), ir.Constant(0u)), &ir.ConstantU64(scale)})); + const IrU64 halves(ir.Emit(IrOpcode::ShiftRightLogical64, IrType::U64, {&product.Value(), &shift.Value()})); + const IrU1 sticky(ir.Emit(IrOpcode::INotEqual64, IrType::U1, {&ir.Emit(IrOpcode::ShiftLeftLogical64, IrType::U64, {&halves.Value(), &shift.Value()}), &product.Value()})); + const IrU32 doubled = extractU64(halves)[0]; + const IrU32 truncated(ir.ShiftRightLogical(doubled.Value(), ir.Constant(1u))); + const IrU1 roundUp(ir.LogicalAnd(ir.INotEqual(ir.BitwiseAnd(doubled.Value(), ir.Constant(1u)), ir.Constant(0u)), + ir.LogicalOr(sticky.Value(), ir.INotEqual(ir.BitwiseAnd(truncated.Value(), ir.Constant(1u)), ir.Constant(0u))))); + IrU32 value(ir.Select(saturated.Value(), ir.Constant(scale), ir.IAdd(truncated.Value(), ir.Select(roundUp.Value(), ir.Constant(1u), ir.Constant(0u))))); + if (signedValue) { + value = IrU32(ir.Select(negative.Value(), ir.ISub(ir.Constant(0u), value.Value()), value.Value())); + } else { + value = IrU32(ir.Select(negative.Value(), ir.Constant(0u), value.Value())); + } + return IrU32(ir.BitwiseAnd(ir.Select(nan.Value(), ir.Constant(0u), value.Value()), ir.Constant(0xffffu))); +} + bool TranslationContext::vLdexpF16(const RdnaInstruction& inst) { const IrU32 bits = readF16Bits(sourceAt(inst, 0u)); const IrF16 half(ir.Emit(IrOpcode::BitCastF16U16, IrType::F16, {&ir.Emit(IrOpcode::ConvertU16U32, IrType::U16, {&bits.Value()})})); @@ -480,6 +505,23 @@ bool TranslationContext::vFrexpMantF32(const RdnaInstruction& inst) { return true; } +bool TranslationContext::vLdexpF32(const RdnaInstruction& inst) { + const IrU32 bits = readU32(sourceAt(inst, 0u)); + const IrU32 offset = readU32(sourceAt(inst, 1u)); + const IrU32 sign(ir.BitwiseAnd(bits.Value(), ir.Constant(0x80000000u))); + const IrU32 exponent(ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&bits.Value(), &ir.Constant(23u), &ir.Constant(8u)})); + const IrU32 clamped(ir.Emit(IrOpcode::SMax32, IrType::U32, {&ir.Emit(IrOpcode::SMin32, IrType::U32, {&offset.Value(), &ir.Constant(512u)}), &ir.Constant(static_cast(-512))})); + const IrU32 scaled(ir.IAdd(exponent.Value(), clamped.Value())); + const IrU32 normal(ir.BitwiseOr(ir.BitwiseAnd(bits.Value(), ir.Constant(0x807fffffu)), ir.ShiftLeftLogical(scaled.Value(), ir.Constant(23u)))); + const IrU32 overflow(ir.BitwiseOr(sign.Value(), ir.Constant(0x7f800000u))); + IrU32 result(ir.Select(ir.Emit(IrOpcode::SGreaterThanEqual32, IrType::U1, {&scaled.Value(), &ir.Constant(255u)}), overflow.Value(), normal.Value())); + result = IrU32(ir.Select(ir.Emit(IrOpcode::SLessThanEqual32, IrType::U1, {&scaled.Value(), &ir.Constant(0u)}), sign.Value(), result.Value())); + result = IrU32(ir.Select(ir.IEqual(exponent.Value(), ir.Constant(0xffu)), bits.Value(), result.Value())); + result = IrU32(ir.Select(ir.IEqual(exponent.Value(), ir.Constant(0u)), sign.Value(), result.Value())); + writeOperand(inst.destination, &ir.BitCastF32(result.Value())); + return true; +} + bool TranslationContext::vDot2cF32F16(const RdnaInstruction& inst) { RdnaOperand a = sourceAt(inst, 0u); a.opSel = false; diff --git a/core/shader/recompiler/Translation/src/VectorInstructions.cpp b/core/shader/recompiler/Translation/src/VectorInstructions.cpp index 1ff70c7f9..2ad16ee6d 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -708,10 +708,10 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) { vCvtPkrtzF16F32(inst); return true; case RdnaOpcode::VCvtPknormI16F32: - vCvtPknormF32(inst, IrOpcode::PackSnorm2x16); + vCvtPknormF32(inst, true); return true; case RdnaOpcode::VCvtPknormU16F32: - vCvtPknormF32(inst, IrOpcode::PackUnorm2x16); + vCvtPknormF32(inst, false); return true; case RdnaOpcode::VCvtPkU8F32: vCvtPkU8F32(inst); @@ -947,7 +947,7 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) { case RdnaOpcode::VDivFixupF64: return vDivFixupF64(inst); case RdnaOpcode::VLdexpF32: - return floatBinary(inst, IrOpcode::FPLdexp, false); + return vLdexpF32(inst); case RdnaOpcode::VMacF32: return floatTernary(inst, roundsProductSeparately(inst) ? IrOpcode::FPMad32 : IrOpcode::FPFma32, true, true); case RdnaOpcode::VMadmkF32: