yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
15fce7c8f
master
1// Hybrid Tausworthe PRNG 2// 3// Code adapted from: https://indico.cern.ch/event/93877/papers/2118070/files/4416-acat3.pdf (See document for license) 4// 5 6uniform float seed; 7RWStructuredBuffer<float> outputBuffer; 8 9uint seedPerThread(uint idx) 10{ 11 return ((uint)idx + (uint)(seed * 1000000)) * 1099087573UL; 12} 13 14uint tauStep(uint z, uint s1, uint s2, uint s3, uint M) 15{ 16 uint b = (((z << s1) ^ z) >> s2); 17 return (((z & M) << s3) ^ b); 18} 19 20[shader("compute")] 21[numthreads(64, 1, 1)] 22void computeMain(uint2 dispatchThreadId : SV_DispatchThreadID) 23{ 24 uint idx = dispatchThreadId.x; 25 uint val = ((uint)idx) * 1099087573UL + ((uint)seed) * 12003927; 26 27 uint z = tauStep(val, 13, 19, 12, 4294967294); 28 z = tauStep(z, 2, 25, 4, 4294967288); 29 z = tauStep(z, 3, 11, 17, 4294967280); 30 31 uint z1, z2, z3, z4; 32 uint r0, r1, r2, r3; 33 34 // STEP 1 35 uint _seed = seedPerThread(idx); 36 z1 = tauStep(_seed, 13, 19, 12, 429496729UL); 37 z2 = tauStep(_seed, 2, 25, 4, 4294967288UL); 38 z3 = tauStep(_seed, 3, 11, 17, 429496280UL); 39 z4 = (1664525 * _seed + 1013904223UL); 40 r0 = (z1 ^ z2 ^ z3 ^ z4); 41 // STEP 2 42 z1 = tauStep(r0, 13, 19, 12, 429496729UL); 43 z2 = tauStep(r0, 2, 25, 4, 4294967288UL); 44 z3 = tauStep(r0, 3, 11, 17, 429496280UL); 45 z4 = (1664525 * r0 + 1013904223UL); 46 r1 = (z1 ^ z2 ^ z3 ^ z4); 47 // STEP 3 48 z1 = tauStep(r1, 13, 19, 12, 429496729UL); 49 z2 = tauStep(r1, 2, 25, 4, 4294967288UL); 50 z3 = tauStep(r1, 3, 11, 17, 429496280UL); 51 z4 = (1664525 * r1 + 1013904223UL); 52 r2 = (z1 ^ z2 ^ z3 ^ z4); 53 // STEP 4 54 z1 = tauStep(r2, 13, 19, 12, 429496729UL); 55 z2 = tauStep(r2, 2, 25, 4, 4294967288UL); 56 z3 = tauStep(r2, 3, 11, 17, 429496280UL); 57 z4 = (1664525 * r2 + 1013904223UL); 58 r3 = (z1 ^ z2 ^ z3 ^ z4); 59 60 float u4 = r3 * 2.3283064365387e-10; 61 62 outputBuffer[idx] = u4; 63}