Last active
June 20, 2025 09:51
-
-
Save mattdesl/e72d39a1d80b3c1faca81d7425903715 to your computer and use it in GitHub Desktop.
Revisions
-
mattdesl revised this gist
Jul 1, 2024 . No changes.There are no files selected for viewing
-
mattdesl created this gist
Jul 1, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ // each pixel is assigned 4 random u32 values per frame @group(0) @binding(1) var<storage, read> rnd_state: array<u32>; // the current state within this pixel var<private> local_rnd_state:vec4u; fn random_u32(state:ptr<private,vec4u>) -> u32 { var st:vec4u = *state; /* Algorithm "xor128" from p. 5 of Marsaglia, "Xorshift RNGs" */ // Load the state from the storage buffer var t: u32 = st.w; var s: u32 = st.x; t ^= t << 11; t ^= t >> 8; var x:u32 = t ^ s ^ (s >> 19); *state = vec4u( x, s, st.y, st.z ); return x; } fn random() -> f32 { return f32(random_u32(&local_rnd_state)) / 0x100000000; } fn my_main () { var idx:u32 = /* index of this pixel into your rnd_state buffer */; // each pixel is assigned its own set of 4 random u32 for xorshift // then this is stored in a local private space, so that each time // random() is called, state will be shifted forward with xorshift local_rnd_state = vec4u( rnd_state[idx * 4 + 0], rnd_state[idx * 4 + 1], rnd_state[idx * 4 + 2], rnd_state[idx * 4 + 3] ); // now you are ready to call random() as many times as you like! out_color = vec4( random(), random(), random(), 1.0 ); }