Move PRNG to Random.hh

This commit is contained in:
Eryn Wells 2018-11-22 07:58:36 -07:00
parent 18f8c141af
commit cd9bf6c1a9
3 changed files with 48 additions and 32 deletions

View file

@ -9,40 +9,10 @@
#include <metal_stdlib>
#include <metal_types>
#include "ShaderTypes.h"
#include "Random.hh"
using namespace metal;
/// A pseudo-random number generator providing several algorithms.
/// - http://reedbeta.com/blog/quick-and-easy-gpu-random-numbers-in-d3d11/
struct PRNG {
PRNG(uint seed) : mSeed(wangHash(seed)) { }
/// Generate a random unsigned integer using a linear congruential generator.
uint lcg() {
mSeed = 1664525 * mSeed + 1013904223;
return mSeed;
}
/// Generate a random unsigned integer using the Xorshift algorithm from George Marsaglia's paper.
uint xorShift() {
mSeed ^= (mSeed << 13);
mSeed ^= (mSeed >> 17);
mSeed ^= (mSeed << 5);
return mSeed;
}
uint wangHash(uint seed) {
seed = (seed ^ 61) ^ (seed >> 16);
seed *= 9;
seed = seed ^ (seed >> 4);
seed *= 0x27d4eb2d;
seed = seed ^ (seed >> 15);
return seed;
}
private:
uint mSeed;
};
kernel void updateGeometryHeights(texture2d<float> texture [[texture(GeneratorTextureIndexIn)]],
constant float2 *texCoords [[buffer(GeneratorBufferIndexTexCoords)]],
constant Uniforms &uniforms [[buffer(GeneratorBufferIndexUniforms)]],