Symbols in this topic
@vgpu/wgsl-std/hash
Pure WGSL hash utilities for deterministic shader randomness. Import them for integer hashing, multi-output PCG-style lattice hashes, and stable conversion from u32 hashes to unit floats.
Import
WGSL
1
import { hash1, hash2, hash3, hashU32, pcg2d, pcg3d, unitFloat } from "@vgpu/wgsl-std/hash";Signature
WGSL
1
2
3
4
5
6
7
export fn hashU32(value: u32) -> u32;
export fn pcg2d(value: vec2u) -> vec2u;
export fn pcg3d(value: vec3u) -> vec3u;
export fn unitFloat(hash: u32) -> f32;
export fn hash1(seed: f32) -> f32;
export fn hash2(seed: vec2f) -> vec2f;
export fn hash3(seed: vec3f) -> vec3f;Parameters
| Param | Type | Required | Default | Notes |
|---|---|---|---|---|
| value | u32 | ✔ | — | Input integer for hashU32, implemented with Wellons lowbias32 constants. |
| value | vec2u | ✔ | — | Two-dimensional unsigned seed for pcg2d; returns two decorrelated unsigned outputs. |
| value | vec3u | ✔ | — | Three-dimensional unsigned seed for pcg3d; returns three decorrelated unsigned outputs. |
| hash | u32 | ✔ | — | Hash bits passed to unitFloat. The low 8 bits are discarded, then the top 24 bits map to [0.0, 1.0). |
| seed | f32 | ✔ | — | Float seed for hash1; bitcast to u32 before hashing. -0.0 and 0.0 hash differently. |
| seed | vec2f | ✔ | — | Float vector seed for hash2; bitcast to vec2u, hashed with pcg2d, converted with unitFloat. |
| seed | vec3f | ✔ | — | Float vector seed for hash3; bitcast to vec3u, hashed with pcg3d, converted with unitFloat. |
Returns: hashU32 returns u32; pcg2d/pcg3d return unsigned vectors; unitFloat and hash1 return f32 in [0.0, 1.0); hash2/hash3 return float vectors with each component in [0.0, 1.0).
Throws: These WGSL declarations do not throw. resolveShader() can still throw VGPU-WGSL-SYM-NOEXPORT for misspelled imports, VGPU-WGSL-PKG-NOTFOUND if the package import cannot be resolved, or validation errors such as VGPU-WGSL-NAGA-UNKNOWN if caller WGSL is invalid.
Examples
TypeScript
1
2
3
4
5
6
7
8
9
const hashWgsl = `
import { pcg3d, unitFloat } from "@vgpu/wgsl-std/hash";
fn cellRandom(cell: vec3i) -> f32 {
return unitFloat(pcg3d(bitcast<vec3u>(cell)).x);
}
`;
console.log(hashWgsl.includes("cellRandom"));TypeScript
1
2
3
4
5
6
7
8
9
const jitterWgsl = `
import { hash2 } from "@vgpu/wgsl-std/hash";
fn jitter(pixel: vec2f) -> vec2f {
return hash2(pixel) - vec2f(0.5);
}
`;
console.log(jitterWgsl.length > 0);Notes
- This module is pure WGSL: it declares no
@group, no@binding, no overrides, no hidden state, and no entry points. hashU32is Wellons lowbias32, not PCG.pcg2dandpcg3dare multi-output PCG-style vector hashes for lattice coordinates.unitFloat(hash)computesf32(hash >> 8u) * (1.0 / 16777216.0), so it never returns1.0.- Float wrappers hash raw IEEE bit patterns. Normalize NaNs and signed zero yourself if those values should collide.
- For lattice noise, prefer integer cell coordinates (
vec*ibitcast tovec*u) over sine/fract-style float hashes. - See also:
@vgpu/wgsl-std/noise,@vgpu/wgsl-std/color,resolveShader.