Files
NX2navi/tools/xlate/patches/0012-bitfield-fields-past-bit-31.patch
2026-11-07 05:04:38 -05:00

65 lines
3.4 KiB
Diff

--- a/src/Ryujinx.Graphics.Shader/Instructions/InstEmitBitfield.cs
+++ b/src/Ryujinx.Graphics.Shader/Instructions/InstEmitBitfield.cs
@@ -142,13 +142,46 @@ namespace Ryujinx.Graphics.Shader.Instructions
Operand size = context.BitfieldExtractU32(srcB, Const(8), Const(8));
+ // NX2NAVI: Maxwell's BFE (as PTX bfe) cuts a field that runs past bit 31 there, and a field that starts past it
+ // gives 0, or bit 31 repeated when signed and the size is not 0. SPIR-V leaves an offset or offset + count over
+ // 32 undefined, and AMD's bitfield instructions take offset and width modulo 32. NX2NAVI's Depths terrain vertex
+ // shaders take position and size from a register (BFE.R), so they are clamped into the defined range here;
+ // constant fields inside the word are emitted as before.
+ bool constantInRange = srcB.Type == OperandType.Constant && (srcB.Value & 0xff) + ((srcB.Value >> 8) & 0xff) <= 32;
+ Operand positionIn = position;
+ Operand sizeIn = size;
+
+ if (!constantInRange)
+ {
+ ClampBitfield(context, ref position, ref size);
+ }
+
Operand res = isSigned
? context.BitfieldExtractS32(srcA, position, size)
: context.BitfieldExtractU32(srcA, position, size);
+ if (!constantInRange && isSigned)
+ {
+ // The clamped extract gave 0 for a field that starts past bit 31; a signed one is bit 31 repeated.
+ Operand signBits = context.ConditionalSelect(context.ICompareEqual(sizeIn, Const(0)), Const(0), context.ShiftRightS32(srcA, Const(31)));
+
+ res = context.ConditionalSelect(context.ICompareGreaterOrEqualUnsigned(positionIn, Const(32)), signBits, res);
+ }
+
context.Copy(GetDest(rd), res);
- // TODO: CC, X, corner cases.
+ // TODO: CC, X.
+ }
+
+ // NX2NAVI: position and size of a bitfield cut to the 32-bit word: the size shrinks to what is left of the word
+ // after the position (0 for a position of 32 or more) and the position stays at most 31, so offset and
+ // offset + count stay inside the range SPIR-V defines.
+ private static void ClampBitfield(EmitterContext context, ref Operand position, ref Operand size)
+ {
+ Operand left = context.ISubtract(Const(32), context.IMinimumU32(position, Const(32)));
+
+ size = context.IMinimumU32(size, left);
+ position = context.IMinimumU32(position, Const(31));
}
private static void EmitBfi(EmitterContext context, Operand srcA, Operand srcB, Operand srcC, int rd)
@@ -157,6 +190,13 @@ namespace Ryujinx.Graphics.Shader.Instructions
Operand size = context.BitfieldExtractU32(srcB, Const(8), Const(8));
+ // NX2NAVI: as in EmitBfe, a field past bit 31 is cut there; one that starts past it inserts nothing (size 0
+ // after the clamp), so C comes back unchanged, as on Maxwell.
+ if (!(srcB.Type == OperandType.Constant && (srcB.Value & 0xff) + ((srcB.Value >> 8) & 0xff) <= 32))
+ {
+ ClampBitfield(context, ref position, ref size);
+ }
+
Operand res = context.BitfieldInsert(srcC, srcA, position, size);
context.Copy(GetDest(rd), res);