Skip to content

Commit 4647502

Browse files
committed
SDSL: handle OpBitcast in OpSpecConstantOp constant folding
TryGetConstantValue switch fell through to NotImplementedException when a SpecConstant chain reinterpret-cast bits between scalar types (e.g. int↔uint, float↔int via asfloat/asuint pattern in a constant context). Reinterpret via BitConverter on the underlying 32-bit pattern and dispatch on the result type's scalar.
1 parent 15df70b commit 4647502

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Context.Constants.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ public bool TryGetConstantValue(OpDataIndex i, [MaybeNullWhen(false)] out object
104104
_ => throw new NotSupportedException($"Unsupported conversion op: {op}"),
105105
};
106106
break;
107+
// Bitcast: reinterpret the operand's bits as the target scalar type.
108+
case Specification.Op.OpBitcast:
109+
if (!TryGetConstantValue(i.Data.Memory.Span[4], out var bitcastOperand, out _))
110+
return false;
111+
if (ReverseTypes[resultType] is not ScalarType { Type: var bitcastTargetScalar })
112+
throw new NotSupportedException($"OpBitcast result type {ReverseTypes[resultType]} is not a scalar");
113+
var bitcastBits = bitcastOperand switch
114+
{
115+
int v => (uint)v,
116+
uint v => v,
117+
float v => BitConverter.SingleToUInt32Bits(v),
118+
_ => throw new NotSupportedException($"OpBitcast operand type {bitcastOperand.GetType()} is not supported"),
119+
};
120+
value = bitcastTargetScalar switch
121+
{
122+
Scalar.Int => (object)(int)bitcastBits,
123+
Scalar.UInt => bitcastBits,
124+
Scalar.Float => BitConverter.UInt32BitsToSingle(bitcastBits),
125+
_ => throw new NotSupportedException($"OpBitcast target scalar {bitcastTargetScalar} is not supported"),
126+
};
127+
break;
107128
// Unary operations
108129
case Specification.Op.OpSNegate:
109130
case Specification.Op.OpFNegate:

0 commit comments

Comments
 (0)