Skip to content

Instantly share code, notes, and snippets.

@septagon
Last active August 20, 2022 12:27
Show Gist options
  • Save septagon/e04204d4cdd59a6c028c70de59bd62b7 to your computer and use it in GitHub Desktop.
Save septagon/e04204d4cdd59a6c028c70de59bd62b7 to your computer and use it in GitHub Desktop.
RWTexture2D<float3> tex : register(u0);
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs", copied in from Wikipedia
uint Xorshift(uint seed)
{
uint x = seed;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
}
static const float primes[13] =
{
6143,
6229,
6311,
6373,
6481,
6577,
6679,
6763,
6841,
6947,
7001,
7109,
7211
};
uint ComputeSeed(uint3 DTid)
{
return
DTid.x * primes[DTid.x % 13] +
DTid.y * primes[12 - DTid.y % 13];
}
[numthreads(1, 1, 1)]
void main(uint3 DTid : SV_DispatchThreadID)
{
uint r = Xorshift(ComputeSeed(DTid));
uint g = Xorshift(r);
uint b = Xorshift(g);
tex[DTid.xy] = float3(r, g, b) / 0xFFFFFFFF;
}
@septagon
Copy link
Author

Now reimplemented for WebGL, too:

https://cyos.babylonjs.com/#DJDWJP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment