diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/Vop3CubeClamp.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/Vop3CubeClamp.cpp new file mode 100644 index 000000000..07e25ea2b --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/Vop3CubeClamp.cpp @@ -0,0 +1,207 @@ +#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 +#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, 0xd544000a, 0x041a0b04, 0xd545000b, + 0x041a0b04, 0xd546000c, 0x041a0b04, 0xd547000d, 0x041a0b04, 0xd544800e, 0x041a0b04, 0xd545800f, + 0x041a0b04, 0xd5468010, 0x041a0b04, 0xd5478011, 0x041a0b04, 0xd5440212, 0x241a0b04, 0xd5450113, + 0xc41a0b04, 0xd5460414, 0x641a0b04, 0xd5470115, 0x841a0b04, 0xd5448016, 0xe41a0b04, 0xd5458417, + 0x441a0b04, 0xd5468118, 0x841a0b04, 0xd5478219, 0x241a0b04, 0xe0781000, 0x80010a03, 0xe0781010, + 0x80010e03, 0xe0781020, 0x80011203, 0xe0781030, 0x80011603, 0xbf810000, +}; + +constexpr std::uint32_t Rows[32][4] = { + {0x80000000u, 0x80000000u, 0x80000000u, 0x00000000u}, + {0x3f800000u, 0xbf800000u, 0x3f800000u, 0x00000000u}, + {0x7f800001u, 0x3f000000u, 0x3f000000u, 0x00000000u}, + {0x40000000u, 0x3f000000u, 0xff812345u, 0x00000000u}, + {0x80000007u, 0x7fc00000u, 0x7fc00000u, 0x00000000u}, + {0x00000007u, 0xffc00000u, 0x7f800001u, 0x00000000u}, + {0xff800000u, 0x80000007u, 0x00000007u, 0x00000000u}, + {0x7fc00000u, 0x00000007u, 0xff800000u, 0x00000000u}, + {0xbf000000u, 0x00000000u, 0x80000007u, 0x00000000u}, + {0x00000007u, 0x00000007u, 0x7f800001u, 0x00000000u}, + {0x00000007u, 0x7f800000u, 0x80000007u, 0x00000000u}, + {0x00000007u, 0x00000007u, 0x80000007u, 0x00000000u}, + {0xff800000u, 0xff800000u, 0x40000000u, 0x00000000u}, + {0x40000000u, 0xc0200000u, 0xc0200000u, 0x00000000u}, + {0x7fc00000u, 0xff800000u, 0x80000000u, 0x00000000u}, + {0xc0200000u, 0x7f800000u, 0x7f800000u, 0x00000000u}, + {0xbf000000u, 0xbf000000u, 0x3f000000u, 0x00000000u}, + {0x7fc00000u, 0xff800000u, 0xc0200000u, 0x00000000u}, + {0x00000000u, 0xff800000u, 0x00000000u, 0x00000000u}, + {0x00000000u, 0xff800000u, 0xbf000000u, 0x00000000u}, + {0xff800000u, 0x00000000u, 0xff800000u, 0x00000000u}, + {0xff800000u, 0x7f800000u, 0x00000000u, 0x00000000u}, + {0xff800000u, 0x7f800000u, 0xbf000000u, 0x00000000u}, + {0xff800000u, 0xff800000u, 0xff800000u, 0x00000000u}, + {0xff800000u, 0xff800000u, 0xbf000000u, 0x00000000u}, + {0xff800000u, 0xbf000000u, 0xbf000000u, 0x00000000u}, + {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u}, + {0x00000000u, 0x00000000u, 0x7fc00000u, 0x00000000u}, + {0x00000000u, 0x00000000u, 0xffc00000u, 0x00000000u}, + {0x00000000u, 0x00000000u, 0x7f800001u, 0x00000000u}, + {0x00000000u, 0x00000000u, 0xff812345u, 0x00000000u} +}; +constexpr std::uint32_t Expected[32][16] = { + {0x40800000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x40800000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x40800000u, 0x3f800000u, 0x3f800000u, 0x40000000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x40800000u, 0xbf800000u, 0xbf800000u, 0xc0000000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x3f800000u}, + {0x00000000u, 0xbf000000u, 0xbf000000u, 0x7f800001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3f000000u, 0x3f000000u, 0x7f800001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x00000000u, 0x7f812345u, 0xbf000000u, 0x40800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x3f800000u, 0xff812345u, 0x3f000000u, 0x40800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x00000000u, 0xffc00000u, 0xffc00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x7fc00000u, 0x7fc00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x00000000u, 0xff800001u, 0x7fc00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x7f800001u, 0xffc00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x3f800000u, 0x00000007u, 0x00000007u, 0xff800000u, 0x3f800000u, 0x00000007u, 0x00000007u, 0x00000000u, 0x00000000u, 0x00000007u, 0x80000007u, 0x7f800000u, 0x00000000u, 0x00000007u, 0x00000007u, 0x3f800000u}, + {0x00000000u, 0x7f800000u, 0x80000007u, 0x7fc00000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xff800000u, 0x00000007u, 0x7fc00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x3f800000u, 0x80000007u, 0x80000000u, 0xbf800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000007u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000007u, 0x00000000u, 0x3f800000u}, + {0x40000000u, 0x00000007u, 0x7f800001u, 0x00000000u, 0x3f800000u, 0x00000007u, 0x00000000u, 0x00000000u, 0x40000000u, 0x00000007u, 0x7f800001u, 0x00000000u, 0x3f800000u, 0x00000007u, 0x00000000u, 0x00000000u}, + {0x40000000u, 0x00000007u, 0x80000007u, 0x7f800000u, 0x3f800000u, 0x00000007u, 0x00000000u, 0x3f800000u, 0x40000000u, 0x00000007u, 0x80000007u, 0x7f800000u, 0x3f800000u, 0x00000007u, 0x00000007u, 0x3f800000u}, + {0x40800000u, 0x00000007u, 0x80000007u, 0x00000000u, 0x3f800000u, 0x00000007u, 0x00000000u, 0x00000000u, 0x40800000u, 0x00000007u, 0x00000007u, 0x00000000u, 0x3f800000u, 0x00000007u, 0x00000000u, 0x00000000u}, + {0x40400000u, 0xff800000u, 0xc0000000u, 0xff800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x40000000u, 0x7f800000u, 0x40000000u, 0xff800000u, 0x3f800000u, 0x00000000u, 0x3f800000u, 0x3f800000u}, + {0x40a00000u, 0xc0000000u, 0x40200000u, 0xc0a00000u, 0x3f800000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x40a00000u, 0x40000000u, 0xc0200000u, 0x40a00000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x00000000u}, + {0x00000000u, 0x00000000u, 0x7f800000u, 0x7fc00000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x80000000u, 0xff800000u, 0x7fc00000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u}, + {0x40800000u, 0xc0200000u, 0xff800000u, 0x7f800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x40800000u, 0xc0200000u, 0x7f800000u, 0xff800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x3f800000u}, + {0x40800000u, 0xbf000000u, 0x3f000000u, 0x3f800000u, 0x3f800000u, 0x00000000u, 0x3f000000u, 0x3f800000u, 0x40800000u, 0xbf000000u, 0xbf000000u, 0xbf800000u, 0x3f800000u, 0x00000000u, 0x3f000000u, 0x3f800000u}, + {0x00000000u, 0x40200000u, 0x7f800000u, 0x7fc00000u, 0x00000000u, 0x3f800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0xc0200000u, 0xff800000u, 0x7fc00000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u}, + {0x40400000u, 0x00000000u, 0x80000000u, 0xff800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x40000000u, 0x00000000u, 0x00000000u, 0xff800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x3f800000u}, + {0x40400000u, 0x00000000u, 0x3f000000u, 0xff800000u, 0x3f800000u, 0x00000000u, 0x3f000000u, 0x00000000u, 0x40000000u, 0x00000000u, 0x3f000000u, 0xff800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x3f800000u}, + {0x40a00000u, 0x7f800000u, 0x80000000u, 0xff800000u, 0x3f800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x40a00000u, 0x7f800000u, 0x00000000u, 0x7f800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x40000000u, 0xff800000u, 0x00000000u, 0x7f800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x40000000u, 0x7f800000u, 0x80000000u, 0x7f800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x3f800000u}, + {0x40000000u, 0xff800000u, 0xbf000000u, 0x7f800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x40000000u, 0x7f800000u, 0xbf000000u, 0x7f800000u, 0x3f800000u, 0x00000000u, 0x3f000000u, 0x3f800000u}, + {0x40a00000u, 0x7f800000u, 0x7f800000u, 0xff800000u, 0x3f800000u, 0x3f800000u, 0x3f800000u, 0x00000000u, 0x40a00000u, 0x7f800000u, 0xff800000u, 0x7f800000u, 0x3f800000u, 0x00000000u, 0x3f800000u, 0x00000000u}, + {0x40400000u, 0xff800000u, 0x3f000000u, 0xff800000u, 0x3f800000u, 0x00000000u, 0x3f000000u, 0x00000000u, 0x40000000u, 0x7f800000u, 0x3f000000u, 0xff800000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x3f800000u}, + {0x3f800000u, 0xbf000000u, 0x3f000000u, 0xff800000u, 0x3f800000u, 0x00000000u, 0x3f000000u, 0x00000000u, 0x00000000u, 0xbf000000u, 0xbf000000u, 0x7f800000u, 0x00000000u, 0x3f000000u, 0x3f000000u, 0x3f800000u}, + {0x40800000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x40800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x40800000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x40800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x40000000u, 0x00000000u, 0x7fc00000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x40000000u, 0x00000000u, 0x7fc00000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x40000000u, 0x00000000u, 0xffc00000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x40000000u, 0x00000000u, 0x7fc00000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x40000000u, 0x00000000u, 0x7f800001u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x40000000u, 0x00000000u, 0x7f800001u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u}, + {0x40000000u, 0x00000000u, 0xff812345u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x40000000u, 0x00000000u, 0x7f812345u, 0x00000000u, 0x3f800000u, 0x00000000u, 0x00000000u, 0x00000000u} +}; +constexpr const char* Names[16] = { + "v_cubeid_f32 v10, v4, v5, v6", + "v_cubesc_f32 v11, v4, v5, v6", + "v_cubetc_f32 v12, v4, v5, v6", + "v_cubema_f32 v13, v4, v5, v6", + "v_cubeid_f32 v14, v4, v5, v6 clamp", + "v_cubesc_f32 v15, v4, v5, v6 clamp", + "v_cubetc_f32 v16, v4, v5, v6 clamp", + "v_cubema_f32 v17, v4, v5, v6 clamp", + "v_cubeid_f32 v18, -v4, |v5|, v6", + "v_cubesc_f32 v19, |v4|, -v5, -v6", + "v_cubetc_f32 v20, -v4, -v5, |v6|", + "v_cubema_f32 v21, |v4|, v5, -v6", + "v_cubeid_f32 v22, -v4, -v5, -v6 clamp", + "v_cubesc_f32 v23, v4, -v5, |v6| clamp", + "v_cubetc_f32 v24, |v4|, v5, -v6 clamp", + "v_cubema_f32 v25, -v4, |v5|, v6 clamp", +}; + +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 cube clamp: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected)); +} + +ShaderRecompiler::RecompileResult Compile(AgcDriver::VulkanDevice& device, std::span code) { + 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::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; + return ShaderRecompiler::Recompile(request); +} + +void Run(AgcDriver::VulkanDevice& device) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]); + Output.fill(0xdeadbeefu); + const std::span code(Code); + const auto result = Compile(device, code); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void CheckOmodRefused(AgcDriver::VulkanDevice& device) { + for (const std::uint32_t opcode : {0x144u, 0x145u, 0x146u, 0x147u}) { + alignas(256) const std::array code{0xd400000au | (opcode << 16u), 0x0c1a0b04u, 0xbf810000u}; + std::string refusal; + try { + static_cast(Compile(device, code)); + } catch (const std::exception& error) { + refusal = error.what(); + } + Require(refusal.find("VOP3 source modifiers are not implemented") != std::string::npos, "VOP3 opcode " + Hex(opcode) + " with omod was not refused"); + } +} + +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 < 16; ++i) { + Expect(tid, out[i], Expected[tid][i], Names[i]); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + CheckOmodRefused(*device); + std::puts("vop3 cube clamp tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp index f91cfbcd5..9d4658bee 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp @@ -757,6 +757,11 @@ bool usesSignedSaturateClamp(RdnaOpcode opcode) { return opcode == RdnaOpcode::VAddNcI32 || opcode == RdnaOpcode::VSubNcI32; } +bool isCubeOpcode(RdnaOpcode opcode) { + return opcode == RdnaOpcode::VCubeidF32 || opcode == RdnaOpcode::VCubescF32 || opcode == RdnaOpcode::VCubetcF32 || + opcode == RdnaOpcode::VCubemaF32; +} + bool isNativeVop3B16BinaryOpcode(RdnaOpcode opcode) { switch (opcode) { case RdnaOpcode::VAddNcU16: @@ -1932,7 +1937,7 @@ bool isF32DivisionStepOpcode(RdnaOpcode opcode) { bool supportsNativeVop3Clamp(RdnaOpcode opcode) { return supportsNativeVop3ResultModifiers(opcode) || usesInexactClampControl(opcode) || usesSignedSaturateClamp(opcode) || - isF32DivisionStepOpcode(opcode); + isF32DivisionStepOpcode(opcode) || isCubeOpcode(opcode); } void checkNativeVop3Modifiers(RdnaOpcode opcode, bool permlane, bool carryInOut, bool scalarDst, std::uint32_t abs, std::uint32_t opSel, std::uint32_t clamp, std::uint32_t omod, std::uint32_t neg) { diff --git a/core/shader/recompiler/Translation/src/FloatInstructions.cpp b/core/shader/recompiler/Translation/src/FloatInstructions.cpp index e0c73c88d..32b749646 100644 --- a/core/shader/recompiler/Translation/src/FloatInstructions.cpp +++ b/core/shader/recompiler/Translation/src/FloatInstructions.cpp @@ -568,22 +568,34 @@ bool TranslationContext::vCubemaF32(const RdnaInstruction& inst) { } bool TranslationContext::floatCube(const RdnaInstruction& inst, std::uint32_t resultKind) { - const IrF32 x = readMixF32(sourceAt(inst, 0u)); - const IrF32 y = readMixF32(sourceAt(inst, 1u)); - const IrF32 z = readMixF32(sourceAt(inst, 2u)); - const IrF32 nx(ir.Emit(IrOpcode::FPNeg32, IrType::F32, {&x.Value()})); - const IrF32 ny(ir.Emit(IrOpcode::FPNeg32, IrType::F32, {&y.Value()})); - const IrF32 nz(ir.Emit(IrOpcode::FPNeg32, IrType::F32, {&z.Value()})); - const IrF32 ax(ir.Emit(IrOpcode::FPAbs32, IrType::F32, {&x.Value()})); - const IrF32 ay(ir.Emit(IrOpcode::FPAbs32, IrType::F32, {&y.Value()})); - const IrF32 az(ir.Emit(IrOpcode::FPAbs32, IrType::F32, {&z.Value()})); + const IrF32 x(ir.BitCastF32(readU32(sourceAt(inst, 0u)).Value())); + const IrF32 y(ir.BitCastF32(readU32(sourceAt(inst, 1u)).Value())); + const IrF32 z(ir.BitCastF32(readU32(sourceAt(inst, 2u)).Value())); + const IrF32 nx(ir.BitCastF32(ir.BitwiseXor(ir.BitCastU32(x.Value()), ir.Constant(0x80000000u)))); + const IrF32 ny(ir.BitCastF32(ir.BitwiseXor(ir.BitCastU32(y.Value()), ir.Constant(0x80000000u)))); + const IrF32 nz(ir.BitCastF32(ir.BitwiseXor(ir.BitCastU32(z.Value()), ir.Constant(0x80000000u)))); + const auto exponentZero = [&](IrF32 value) { + return IrU1(ir.IEqual(ir.BitwiseAnd(ir.BitCastU32(value.Value()), ir.Constant(0x7f800000u)), ir.Constant(0u))); + }; + const auto flushed = [&](IrF32 value) { + return IrF32(ir.BitCastF32(ir.Select(exponentZero(value).Value(), ir.Constant(0u), ir.BitCastU32(value.Value())))); + }; + const auto magnitude = [&](IrF32 value) { + return IrF32(ir.BitCastF32(ir.BitwiseAnd(ir.BitCastU32(value.Value()), ir.Constant(0x7fffffffu)))); + }; + const IrF32 fx = flushed(x); + const IrF32 fy = flushed(y); + const IrF32 fz = flushed(z); + const IrF32 ax = magnitude(fx); + const IrF32 ay = magnitude(fy); + const IrF32 az = magnitude(fz); const IrU1 zDominatesX(ir.Emit(IrOpcode::FPOrdGreaterThanEqual32, IrType::U1, {&az.Value(), &ax.Value()})); const IrU1 zDominatesY(ir.Emit(IrOpcode::FPOrdGreaterThanEqual32, IrType::U1, {&az.Value(), &ay.Value()})); const IrU1 zFace(ir.LogicalAnd(zDominatesX.Value(), zDominatesY.Value())); const IrU1 yFace(ir.Emit(IrOpcode::FPOrdGreaterThanEqual32, IrType::U1, {&ay.Value(), &ax.Value()})); - const IrU1 xNegative(ir.Emit(IrOpcode::FPOrdLessThan32, IrType::U1, {&x.Value(), &ir.ConstantF32(0.0f)})); - const IrU1 yNegative(ir.Emit(IrOpcode::FPOrdLessThan32, IrType::U1, {&y.Value(), &ir.ConstantF32(0.0f)})); - const IrU1 zNegative(ir.Emit(IrOpcode::FPOrdLessThan32, IrType::U1, {&z.Value(), &ir.ConstantF32(0.0f)})); + const IrU1 xNegative(ir.Emit(IrOpcode::FPOrdLessThan32, IrType::U1, {&fx.Value(), &ir.ConstantF32(0.0f)})); + const IrU1 yNegative(ir.Emit(IrOpcode::FPOrdLessThan32, IrType::U1, {&fy.Value(), &ir.ConstantF32(0.0f)})); + const IrU1 zNegative(ir.Emit(IrOpcode::FPOrdLessThan32, IrType::U1, {&fz.Value(), &ir.ConstantF32(0.0f)})); const auto selectFace = [&](IrF32 xValue, IrF32 yValue, IrF32 zValue) { return selectF32(zFace, zValue, selectF32(yFace, yValue, xValue)); }; @@ -606,11 +618,10 @@ bool TranslationContext::floatCube(const RdnaInstruction& inst, std::uint32_t re result = selectFace(ny, selectF32(yNegative, nz, z), ny); break; case 3u: { - const IrF32 two(ir.ConstantF32(2.0f)); - const IrF32 xTwo(ir.Emit(IrOpcode::FPMul32, IrType::F32, {&x.Value(), &two.Value()})); - const IrF32 yTwo(ir.Emit(IrOpcode::FPMul32, IrType::F32, {&y.Value(), &two.Value()})); - const IrF32 zTwo(ir.Emit(IrOpcode::FPMul32, IrType::F32, {&z.Value(), &two.Value()})); - result = selectFace(xTwo, yTwo, zTwo); + const IrF32 major = selectFace(x, y, z); + const IrF32 twice(ir.Emit(IrOpcode::FPMul32, IrType::F32, {&major.Value(), &ir.ConstantF32(2.0f)})); + const IrU1 nan(ir.UGreaterThan(ir.BitCastU32(magnitude(major).Value()), ir.Constant(0x7f800000u))); + result = selectF32(nan, major, selectF32(exponentZero(major), IrF32(ir.ConstantF32(0.0f)), twice)); break; } default: