From 4ea1209d0409c9b323716fe4d947ea4a946dddb8 Mon Sep 17 00:00:00 2001 From: Michon <231849@buas.nl> Date: Tue, 29 Sep 2026 22:58:57 +0200 Subject: [PATCH] fix(recompiler): read f16 inline constants as half values and honour VOP3 f16 op_sel A float inline constant (4.0, 2.0, -4.0...) read by an f16 operation is the half value in the low 16 bits (LLVM getInlineEncodingV216); it was the low half of the f32 bits, 0. op_sel[0..2] now selects the high half of v_fma/min3/max3/med3_f16 sources. GTA V's CAS sharpen computed rcp(1 + 4w) as rcp(0) and stored black. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../Translation/TranslationContext.hpp | 1 + .../src/TranslationContext/OperandAccess.cpp | 2 +- .../src/TranslationContext/SubwordPacking.cpp | 28 +++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index eb9c775ac..f9d46dde6 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -50,6 +50,7 @@ private: IrU64 readU64(const RdnaOperand& operand); IrF32 readF16LaneAsF32(const RdnaOperand& operand, bool highLane, bool packed = false); IrF32 readF16AsF32(const RdnaOperand& operand); + IrU32 readF16SourceBits(const RdnaOperand& operand); IrF32 readMixF32(const RdnaOperand& operand); IrU32 readU16LaneRaw(const RdnaOperand& operand, bool highLane); IrU32 readU16LaneAsU32(const RdnaOperand& operand, bool highLane, bool signExtend); diff --git a/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp b/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp index bd1d89b8c..6615b4de3 100644 --- a/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp +++ b/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp @@ -113,7 +113,7 @@ IrValue* TranslationContext::readOperand(const RdnaOperand& operand, IrType type return &ir.Emit(IrOpcode::ConvertU16U32, IrType::U16, {&applyBitSourceModifiers(operand, readRawU32(operand)).Value()}); } if (type == IrType::F16) { - const IrU16 bits(ir.Emit(IrOpcode::ConvertU16U32, IrType::U16, {&applyBitSourceModifiers(operand, readRawU32(operand)).Value()})); + const IrU16 bits(ir.Emit(IrOpcode::ConvertU16U32, IrType::U16, {&readF16SourceBits(operand).Value()})); return &ir.Emit(IrOpcode::BitCastF16U16, IrType::F16, {&bits.Value()}); } if (type == IrType::U1) { diff --git a/core/shader/recompiler/Translation/src/TranslationContext/SubwordPacking.cpp b/core/shader/recompiler/Translation/src/TranslationContext/SubwordPacking.cpp index f16d28d82..db0a90be1 100644 --- a/core/shader/recompiler/Translation/src/TranslationContext/SubwordPacking.cpp +++ b/core/shader/recompiler/Translation/src/TranslationContext/SubwordPacking.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include namespace ShaderRecompiler { @@ -37,7 +39,8 @@ void TranslationContext::writeU32Pair(const RdnaOperand& operand, const std::arr IrF32 TranslationContext::readF16LaneAsF32(const RdnaOperand& operand, bool highLane, bool packed) { const bool selectHigh = packed ? (highLane ? operand.opSelHi : operand.opSel) : highLane; - const IrU32 raw = readU16LaneRaw(operand, selectHigh); + const IrU32 source = readF16SourceBits(operand); + const IrU32 raw(ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&source.Value(), &ir.Constant(selectHigh ? 16u : 0u), &ir.Constant(16u)})); const IrU16 bits(ir.Emit(IrOpcode::ConvertU16U32, IrType::U16, {&raw.Value()})); const IrF16 half(ir.Emit(IrOpcode::BitCastF16U16, IrType::F16, {&bits.Value()})); IrF32 value(ir.Emit(IrOpcode::ConvertF32F16, IrType::F32, {&half.Value()})); @@ -51,8 +54,29 @@ IrF32 TranslationContext::readF16LaneAsF32(const RdnaOperand& operand, bool high return value; } +IrU32 TranslationContext::readF16SourceBits(const RdnaOperand& operand) { + if (operand.kind != RdnaOperandKind::FloatInlineConstant) { + return applyBitSourceModifiers(operand, readRawU32(operand)); + } + std::uint32_t half = 0u; + switch (operand.value) { + case 0x3f000000u: half = 0x3800u; break; + case 0xbf000000u: half = 0xb800u; break; + case 0x3f800000u: half = 0x3c00u; break; + case 0xbf800000u: half = 0xbc00u; break; + case 0x40000000u: half = 0x4000u; break; + case 0xc0000000u: half = 0xc000u; break; + case 0x40800000u: half = 0x4400u; break; + case 0xc0800000u: half = 0xc400u; break; + case 0x3e22f983u: half = 0x3118u; break; + default: throw std::runtime_error("TranslationContext::readF16SourceBits unknown float inline constant bits " + std::to_string(operand.value)); + } + return applyBitSourceModifiers(operand, IrU32(ir.Constant(half))); +} + IrF32 TranslationContext::readF16AsF32(const RdnaOperand& operand) { - const IrU32 raw = applyBitSourceModifiers(operand, readRawU32(operand)); + const IrU32 source = readF16SourceBits(operand); + const IrU32 raw = operand.opSel ? IrU32(ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&source.Value(), &ir.Constant(16u), &ir.Constant(16u)})) : source; const IrU16 bits(ir.Emit(IrOpcode::ConvertU16U32, IrType::U16, {&raw.Value()})); const IrF16 half(ir.Emit(IrOpcode::BitCastF16U16, IrType::F16, {&bits.Value()})); IrF32 value(ir.Emit(IrOpcode::ConvertF32F16, IrType::F32, {&half.Value()}));