diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SdwaIntegerClamp.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SdwaIntegerClamp.cpp new file mode 100644 index 000000000..3174ec093 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SdwaIntegerClamp.cpp @@ -0,0 +1,188 @@ +#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, 0x4a140af9, 0x06012604, 0x7e160307, + 0x4c160af9, 0x030c3504, 0x4e180af9, 0x0a062804, 0x7d8a0c80, 0x501a0af9, 0x05062404, 0xd501000e, + 0x01a90280, 0x7e1e0307, 0x7d8a0c80, 0x521e0af9, 0x06023304, 0xd5010010, 0x01a90280, 0x7d8a0c80, + 0x54220af9, 0x04082604, 0xd5010012, 0x01a90280, 0x12260af9, 0x0d062d04, 0x16280af9, 0x06062604, + 0x142a0af9, 0x06062604, 0x262c0af9, 0x01062604, 0x7e2e0307, 0x302e0af9, 0x06003404, 0x3c300af9, + 0x06062604, 0xe0781000, 0x80010a03, 0xe0781010, 0x80010e03, 0xe0781020, 0x80011203, 0xe0781030, + 0x80011603, 0xbf810000, +}; + +constexpr std::uint32_t Rows[32][4] = { + {0x00000000u, 0x00000000u, 0x00010000u, 0x51bb9a4eu}, + {0x007fffffu, 0xfd503aeeu, 0x00000000u, 0x00008000u}, + {0x00000001u, 0x219db754u, 0x00000000u, 0xfdf13c3fu}, + {0x00008000u, 0xfffffffeu, 0x000000ffu, 0x7f7f7f7fu}, + {0x00010000u, 0x80000001u, 0x00007fffu, 0x00008000u}, + {0xfffffffeu, 0x00000000u, 0x00008000u, 0x7fffffffu}, + {0x00000000u, 0x00000002u, 0x00000000u, 0x00008000u}, + {0x00000000u, 0x00010000u, 0x00000000u, 0xe5224648u}, + {0x00010000u, 0x00000000u, 0x00007fffu, 0x80000000u}, + {0x0013fd86u, 0x08ce4bd3u, 0xa671bed2u, 0xffff0000u}, + {0x00ffffffu, 0x00000002u, 0x00000000u, 0xa8d118f0u}, + {0xffff0000u, 0xffff0000u, 0x007fffffu, 0x8bc63fd0u}, + {0xfffffffeu, 0x00ff00ffu, 0x00000000u, 0x00000002u}, + {0x00000000u, 0x00000000u, 0x7f7f7f7fu, 0x00ff00ffu}, + {0x00000000u, 0x00000000u, 0x93552cc2u, 0xffffffffu}, + {0x00000000u, 0x00000000u, 0xccaf1cddu, 0xf37566c1u}, + {0x00000000u, 0x00000000u, 0xffffffffu, 0x0000ffffu}, + {0x00000000u, 0x00000000u, 0xffffffffu, 0x12345678u}, + {0x00000000u, 0x00000001u, 0x00ffffffu, 0x33869405u}, + {0x00000000u, 0x00000001u, 0xff00ff00u, 0x202be01bu}, + {0x00000000u, 0x00000002u, 0x4c31a5c3u, 0x5534d12cu}, + {0x00000000u, 0x00000002u, 0x7ba28771u, 0x007fffffu}, + {0x00000000u, 0x00000002u, 0x7fffffffu, 0xb8cf0c88u}, + {0x00000000u, 0x00000002u, 0xf6340ef5u, 0xcf296973u}, + {0x00000000u, 0x00000002u, 0xffff0000u, 0xffffffffu}, + {0x00000000u, 0x0000007fu, 0x0000ffffu, 0x8d2cc93bu}, + {0x00000000u, 0x0000007fu, 0x121b4baau, 0x00000002u}, + {0x00000000u, 0x0000007fu, 0x12345678u, 0x00ff00ffu}, + {0x00000000u, 0x0000007fu, 0x7f7f7f7fu, 0x000000ffu}, + {0x00000000u, 0x00000080u, 0x00000001u, 0x00010000u}, + {0x00000000u, 0x00000080u, 0x0000ffffu, 0x00ff00ffu}, + {0x00000000u, 0x00000080u, 0x0000ffffu, 0x80000000u} +}; +constexpr std::uint32_t Expected[32][15] = { + {0x00000000u, 0x00009a4eu, 0x00000000u, 0x00000001u, 0x00000000u, 0x00bb9a4eu, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x51bb0000u, 0xffffffffu}, + {0xfd503bedu, 0xff028000u, 0x00000000u, 0x0000fd4fu, 0x00000000u, 0x00008000u, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0xffffffffu, 0x0000281du, 0x0000003au, 0x0000ffffu, 0x02d03aeeu}, + {0x219db754u, 0x00003c3fu, 0xffffff9cu, 0x0000219eu, 0x00000000u, 0x00f13c3fu, 0x00000001u, 0x0000b753u, 0x00000000u, 0x219d0000u, 0x009db754u, 0xffffffffu, 0x00000001u, 0xfdf1dbaau, 0xde6248aau}, + {0xffffffffu, 0x7f017f7fu, 0xffffffffu, 0x00008000u, 0x00000000u, 0x007f7f7fu, 0x00000001u, 0x0000fffdu, 0x00000000u, 0x80000000u, 0xffffffffu, 0xffffffffu, 0x000000ffu, 0x7f7ffffeu, 0x00008001u}, + {0x80000001u, 0x00008000u, 0x00000000u, 0x00008001u, 0x00000000u, 0x00008000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00010000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x7ffefffeu}, + {0x000000ffu, 0xfffeffffu, 0x00000000u, 0x0000ffffu, 0x00000000u, 0xfeffffffu, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x7fff0000u, 0x00000001u}, + {0x00000002u, 0x00008000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00008000u, 0x00000001u, 0x00000002u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000002u, 0xfffffffdu}, + {0x00010000u, 0x00004648u, 0x00000001u, 0x00000001u, 0x00000000u, 0x00224648u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xe5220000u, 0xfffeffffu}, + {0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0xfffeffffu}, + {0x08ce4cd0u, 0xfd7e0000u, 0x00000048u, 0x00000655u, 0x00000000u, 0x00ff0000u, 0x00000001u, 0x00000000u, 0x00000001u, 0xffff0000u, 0xffffffffu, 0xfffffc1eu, 0x0000004bu, 0xffff392fu, 0xf72249aau}, + {0x00000101u, 0xffff18f0u, 0x00000000u, 0x0000ffffu, 0x00000000u, 0xfdd118f0u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x01fffffeu, 0xffffffffu, 0x00000000u, 0xa8d10000u, 0xff000002u}, + {0xffff0000u, 0x00003fd0u, 0xffffffffu, 0x0000ffffu, 0x00000001u, 0x00c63fd0u, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0xffffffffu, 0x00000001u, 0x00000000u, 0x8bc60000u, 0xffffffffu}, + {0x00ff01feu, 0xfffe0002u, 0x00000001u, 0x0000ffffu, 0x00000001u, 0x00000002u, 0x00000001u, 0x00000000u, 0x00000001u, 0xfe020000u, 0xffffffffu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00ff00feu}, + {0x00000000u, 0x000000ffu, 0x00000000u, 0x00000001u, 0x00000000u, 0x00ff00ffu, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00ff0000u, 0xffffffffu}, + {0x00000000u, 0x0000ffffu, 0x00000000u, 0x00000001u, 0x00000000u, 0x00ffffffu, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xffff0000u, 0xffffffffu}, + {0x00000000u, 0x000066c1u, 0x00000000u, 0x00000001u, 0x00000000u, 0x007566c1u, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xf3750000u, 0xffffffffu}, + {0x00000000u, 0x0000ffffu, 0x00000000u, 0x00000001u, 0x00000000u, 0x0000ffffu, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xffffffffu}, + {0x00000000u, 0x00005678u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00345678u, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x12340000u, 0xffffffffu}, + {0x00000001u, 0x00009405u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00869405u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x33860001u, 0xfffffffeu}, + {0x00000001u, 0x0000e01bu, 0x00000000u, 0x00000001u, 0x00000000u, 0x002be01bu, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x202b0001u, 0xfffffffeu}, + {0x00000002u, 0x0000d12cu, 0x00000000u, 0x00000001u, 0x00000000u, 0x0034d12cu, 0x00000001u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x55340002u, 0xfffffffdu}, + {0x00000002u, 0x0000ffffu, 0x00000000u, 0x00000001u, 0x00000000u, 0x007fffffu, 0x00000001u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x007f0002u, 0xfffffffdu}, + {0x00000002u, 0x00000c88u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00cf0c88u, 0x00000001u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xb8cf0002u, 0xfffffffdu}, + {0x00000002u, 0x00006973u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00296973u, 0x00000001u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xcf290002u, 0xfffffffdu}, + {0x00000002u, 0x0000ffffu, 0x00000000u, 0x00000001u, 0x00000000u, 0x00ffffffu, 0x00000001u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xffff0002u, 0xfffffffdu}, + {0x0000007fu, 0x0000c93bu, 0x00000000u, 0x00000001u, 0x00000000u, 0x002cc93bu, 0x00000001u, 0x0000007eu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x8d2c007fu, 0xffffff80u}, + {0x0000007fu, 0x00000002u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000002u, 0x00000001u, 0x0000007eu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x0000007fu, 0xffffff80u}, + {0x0000007fu, 0x000000ffu, 0x00000000u, 0x00000001u, 0x00000000u, 0x00ff00ffu, 0x00000001u, 0x0000007eu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00ff007fu, 0xffffff80u}, + {0x0000007fu, 0x000000ffu, 0x00000000u, 0x00000001u, 0x00000000u, 0x000000ffu, 0x00000001u, 0x0000007eu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x0000007fu, 0xffffff80u}, + {0x00000080u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00010000u, 0x00000001u, 0x0000007fu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00010080u, 0xffffff7fu}, + {0x00000080u, 0x000000ffu, 0x00000000u, 0x00000001u, 0x00000000u, 0x00ff00ffu, 0x00000001u, 0x0000007fu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00ff0080u, 0xffffff7fu}, + {0x00000080u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000001u, 0x0000007fu, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000080u, 0xffffff7fu} +}; +constexpr const char* Names[15] = { + "add_nc_u32", + "sub_nc_u32", + "subrev_nc_u32", + "add_co_ci_u32", + "add_co_ci_u32 carry", + "sub_co_ci_u32", + "sub_co_ci_u32 carry", + "subrev_co_ci_u32", + "subrev_co_ci_u32 carry", + "mul_i32_i24", + "mul_u32_u24", + "mul_hi_i32_i24", + "min_u32", + "ashrrev_i32", + "xnor_b32", +}; + +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("sdwa integer clamp: 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 < 15; ++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("sdwa integer 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 e8aa3dcbe..f91cfbcd5 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp @@ -1316,6 +1316,11 @@ const Vop2SdwaRule* findVop2SdwaRule(std::uint32_t encoding) { return &vop2SdwaRules[static_cast(info->sdwaProfile)]; } +bool supportsVop2SdwaIntegerClamp(RdnaOpcode op, std::uint32_t encoding) { + const auto* info = findVectorOpcodeEntry(vop2Opcodes, encoding); + return info != nullptr && info->sdwaProfile == Vop2SdwaProfile::Integer && op != RdnaOpcode::VCndmaskB32; +} + bool isVop2SdwaDestinationSupported(const Vop2SdwaRule& rule, const Vop2SdwaFields& fields) { if (!hasSdwaSelector(rule.dstSelectors, fields.dstSel)) { return false; @@ -1334,7 +1339,8 @@ void validateVop2Sdwa(const RdnaInstruction& instruction, std::uint32_t opcode, if (fields.src0Sel > 6u || fields.src1Sel > 6u || fields.dstSel > 6u) { throw std::invalid_argument("VOP2 SDWA selector is invalid"); } - if ((fields.clamp != 0u || fields.omod != 0u) && !isVop2FloatOpcode(instruction.op)) { + const bool clampSupported = isVop2FloatOpcode(instruction.op) || supportsVop2SdwaIntegerClamp(instruction.op, opcode); + if ((fields.clamp != 0u && !clampSupported) || (fields.omod != 0u && !isVop2FloatOpcode(instruction.op))) { throw std::invalid_argument("VOP2 SDWA output modifiers are not supported"); } if (isFullWidthVop2Sdwa(fields)) { diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 69eefe8ea..5c0f4b7f4 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -251,6 +251,7 @@ private: void addU32(const RdnaInstruction& inst, bool vector, bool useCarryIn); void subU32(const RdnaInstruction& inst, bool vector, bool reverse); void subbU32(const RdnaInstruction& inst, bool vector, bool reverse); + bool vAddSubNcU32(const RdnaInstruction& inst, bool subtract, bool reverse); void sAbsdiffI32(const RdnaInstruction& inst); void sAddSubI32(const RdnaInstruction& inst, bool subtract); void sLshlAddU32(const RdnaInstruction& inst, std::uint32_t shiftAmount); diff --git a/core/shader/recompiler/Translation/src/ControlFlowInstructions.cpp b/core/shader/recompiler/Translation/src/ControlFlowInstructions.cpp index 45edc6d6d..fea539944 100644 --- a/core/shader/recompiler/Translation/src/ControlFlowInstructions.cpp +++ b/core/shader/recompiler/Translation/src/ControlFlowInstructions.cpp @@ -72,8 +72,8 @@ void TranslationContext::addU32(const RdnaInstruction& inst, bool vector, bool u const IrU32 sum(ir.Emit(IrOpcode::CompositeExtractU32x2, IrType::U32, {&firstAdd, &ir.Constant(0u)})); const IrU32 firstCarry(ir.Emit(IrOpcode::CompositeExtractU32x2, IrType::U32, {&firstAdd, &ir.Constant(1u)})); if (!useCarryIn) { - writeRawU32(inst.destination, sum); const IrU1 carryOut(ir.INotEqual(firstCarry.Value(), ir.Constant(0u))); + writeRawU32(inst.destination, inst.destination.clamp ? IrU32(ir.Select(carryOut.Value(), ir.Constant(0xffffffffu), sum.Value())) : sum); if (vector) { writeMask(inst.destination2, carryOut); return; @@ -87,7 +87,7 @@ void TranslationContext::addU32(const RdnaInstruction& inst, bool vector, bool u const IrU32 result(ir.Emit(IrOpcode::CompositeExtractU32x2, IrType::U32, {&secondAdd, &ir.Constant(0u)})); const IrU32 secondCarry(ir.Emit(IrOpcode::CompositeExtractU32x2, IrType::U32, {&secondAdd, &ir.Constant(1u)})); const IrU1 carryOut(ir.LogicalOr(ir.INotEqual(firstCarry.Value(), ir.Constant(0u)), ir.INotEqual(secondCarry.Value(), ir.Constant(0u)))); - writeRawU32(inst.destination, result); + writeRawU32(inst.destination, inst.destination.clamp ? IrU32(ir.Select(carryOut.Value(), ir.Constant(0xffffffffu), result.Value())) : result); if (vector) { writeMask(inst.destination2, carryOut); return; @@ -122,7 +122,7 @@ void TranslationContext::subbU32(const RdnaInstruction& inst, bool vector, bool const IrU32 result(ir.ISub(partial.Value(), borrowInU32.Value())); const IrU1 secondBorrow(ir.ULessThan(partial.Value(), borrowInU32.Value())); const IrU1 borrowOut(ir.LogicalOr(firstBorrow.Value(), secondBorrow.Value())); - writeRawU32(inst.destination, result); + writeRawU32(inst.destination, inst.destination.clamp ? IrU32(ir.Select(borrowOut.Value(), ir.Constant(0u), result.Value())) : result); if (vector) { writeMask(inst.destination2, borrowOut); return; @@ -130,6 +130,20 @@ void TranslationContext::subbU32(const RdnaInstruction& inst, bool vector, bool ir.SetScc(borrowOut.Value()); } +bool TranslationContext::vAddSubNcU32(const RdnaInstruction& inst, bool subtract, bool reverse) { + const IrU32 first = readU32(sourceAt(inst, 0u)); + const IrU32 second = readU32(sourceAt(inst, 1u)); + const IrU32& lhs = reverse ? second : first; + const IrU32& rhs = reverse ? first : second; + IrU32 result(subtract ? ir.ISub(lhs.Value(), rhs.Value()) : ir.IAdd(lhs.Value(), rhs.Value())); + if (inst.destination.clamp) { + const IrU1 carry(subtract ? ir.ULessThan(lhs.Value(), rhs.Value()) : ir.ULessThan(result.Value(), lhs.Value())); + result = IrU32(ir.Select(carry.Value(), ir.Constant(subtract ? 0u : 0xffffffffu), result.Value())); + } + writeOperand(inst.destination, &result.Value()); + return true; +} + void TranslationContext::sAbsdiffI32(const RdnaInstruction& inst) { const IrU32 lhs = readU32(sourceAt(inst, 0u)); const IrU32 rhs = readU32(sourceAt(inst, 1u)); diff --git a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp index ea5b647a1..ec9d70af4 100644 --- a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp +++ b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp @@ -316,6 +316,12 @@ bool TranslationContext::integer24(const RdnaInstruction& inst, bool sign, bool const IrU32 rhs(ir.Emit(extractOpcode, IrType::U32, {&rhsSource.Value(), &ir.Constant(0u), &ir.Constant(24u)})); const IrOpcode multiplyOpcode = high ? (sign ? IrOpcode::SMulHi : IrOpcode::UMulHi) : IrOpcode::IMul32; IrU32 result(ir.Emit(multiplyOpcode, IrType::U32, {&lhs.Value(), &rhs.Value()})); + if (inst.destination.clamp && !high && !addend) { + const IrU32 top(ir.Emit(sign ? IrOpcode::SMulHi : IrOpcode::UMulHi, IrType::U32, {&lhs.Value(), &rhs.Value()})); + IrValue& expected = sign ? ir.ShiftRightArithmetic(result.Value(), ir.Constant(31u)) : ir.Constant(0u); + IrValue& saturated = sign ? ir.BitwiseXor(ir.ShiftRightArithmetic(top.Value(), ir.Constant(31u)), ir.Constant(0x7fffffffu)) : ir.Constant(0xffffffffu); + result = IrU32(ir.Select(ir.INotEqual(top.Value(), expected), saturated, result.Value())); + } if (addend) { result = IrU32(ir.IAdd(result.Value(), readU32(sourceAt(inst, 2u)).Value())); } diff --git a/core/shader/recompiler/Translation/src/VectorInstructions.cpp b/core/shader/recompiler/Translation/src/VectorInstructions.cpp index 2ad16ee6d..0b93cffa2 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -997,11 +997,11 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) { case RdnaOpcode::VMulHiI32: return simpleInteger(inst, IrOpcode::SMulHi, IrType::U32, false, false, false); case RdnaOpcode::VAddNcU32: - return simpleInteger(inst, IrOpcode::IAdd32, IrType::U32, false, false, false); + return vAddSubNcU32(inst, false, false); case RdnaOpcode::VSubNcU32: - return simpleInteger(inst, IrOpcode::ISub32, IrType::U32, false, false, false); + return vAddSubNcU32(inst, true, false); case RdnaOpcode::VSubrevNcU32: - return simpleInteger(inst, IrOpcode::ISub32, IrType::U32, true, false, false); + return vAddSubNcU32(inst, true, true); case RdnaOpcode::VMinI32: return simpleInteger(inst, IrOpcode::SMin32, IrType::U32, false, false, false); case RdnaOpcode::VMaxI32: