0

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);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
javirk
  • 23
  • 5
  • There [doesn't appear to be one](https://www.w3.org/TR/WGSL/) – Nicol Bolas Dec 04 '22 at 18:06
  • That's what I thought, and I can't find any workaround. I asked about how to get the minimum float value on the WebGPU repo (https://github.com/gpuweb/gpuweb/issues/3656). I will update this thread if there's an answer. – javirk Dec 04 '22 at 21:23

1 Answers1

0

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.

javirk
  • 23
  • 5