Search code examples
glslshaderwgsl

What's the equivalent of GLSL's uintBitsToFloat in WGSL?


I am converting a random number generator from GLSL to WGSL, and it uses the function uintBitsToFloat. What's the equivalent in WGSL? Example use, from https://www.shadertoy.com/view/Mt3cRX:

uint Hash_Wang(uint key) {
    key = (key ^ 61u) ^ (key >> 16u);
    key = key + (key << 3u);
    key = key ^ (key >> 4u);
    key = key * 0x27D4EB2Du;
    key = key ^ (key >> 15u);
    return key;
}

float UniformUintToFloat(uint u) {
    // IEEE-754: 2^-32 = 0x2F800000
    return float(u) * uintBitsToFloat(0x2F800000u);
}

Solution

  • Solution

    The equivalent in WGSL is bitcast<T>. For this specific case where I want to convert to f32:

    bitcast<f32>(0x2F800000u)
    

    Apart from this, as explained here, WGSL supports hexadecimal floats, so 0x1p-126f should do the job as the smallest non-subnormal number.