Move PRNG to Random.hh
This commit is contained in:
parent
18f8c141af
commit
cd9bf6c1a9
3 changed files with 48 additions and 32 deletions
|
@ -51,6 +51,7 @@
|
||||||
C018AD3F21978E690094BE3C /* Queue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Queue.swift; sourceTree = "<group>"; };
|
C018AD3F21978E690094BE3C /* Queue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Queue.swift; sourceTree = "<group>"; };
|
||||||
C018AD412197907B0094BE3C /* QueueTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueTests.swift; sourceTree = "<group>"; };
|
C018AD412197907B0094BE3C /* QueueTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueTests.swift; sourceTree = "<group>"; };
|
||||||
C028A6A121A46796005DE718 /* Math.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Math.swift; sourceTree = "<group>"; };
|
C028A6A121A46796005DE718 /* Math.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Math.swift; sourceTree = "<group>"; };
|
||||||
|
C04A27A421A6FB4B00FCABFB /* Random.hh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Random.hh; sourceTree = "<group>"; };
|
||||||
C08C589F218F46F000EAFC2D /* Algorithms.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Algorithms.swift; sourceTree = "<group>"; };
|
C08C589F218F46F000EAFC2D /* Algorithms.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Algorithms.swift; sourceTree = "<group>"; };
|
||||||
C08C58A1218F474E00EAFC2D /* TerrainAlgorithms.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = TerrainAlgorithms.metal; sourceTree = "<group>"; };
|
C08C58A1218F474E00EAFC2D /* TerrainAlgorithms.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = TerrainAlgorithms.metal; sourceTree = "<group>"; };
|
||||||
C0C15A8A218DDD85007494E2 /* Terrain.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Terrain.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
C0C15A8A218DDD85007494E2 /* Terrain.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Terrain.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||||
|
@ -121,6 +122,7 @@
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
C0C15ABA218E2A90007494E2 /* ShaderTypes.h */,
|
C0C15ABA218E2A90007494E2 /* ShaderTypes.h */,
|
||||||
|
C04A27A421A6FB4B00FCABFB /* Random.hh */,
|
||||||
C0C15AB8218E2A90007494E2 /* Shaders.metal */,
|
C0C15AB8218E2A90007494E2 /* Shaders.metal */,
|
||||||
C08C58A1218F474E00EAFC2D /* TerrainAlgorithms.metal */,
|
C08C58A1218F474E00EAFC2D /* TerrainAlgorithms.metal */,
|
||||||
);
|
);
|
||||||
|
|
44
Terrain2/Shaders/Random.hh
Normal file
44
Terrain2/Shaders/Random.hh
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
//
|
||||||
|
// Random.hh
|
||||||
|
// Terrain
|
||||||
|
//
|
||||||
|
// Created by Eryn Wells on 11/22/18.
|
||||||
|
// Copyright © 2018 Eryn Wells. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef Random_hh
|
||||||
|
#define Random_hh
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* Random_hh */
|
|
@ -9,40 +9,10 @@
|
||||||
#include <metal_stdlib>
|
#include <metal_stdlib>
|
||||||
#include <metal_types>
|
#include <metal_types>
|
||||||
#include "ShaderTypes.h"
|
#include "ShaderTypes.h"
|
||||||
|
#include "Random.hh"
|
||||||
|
|
||||||
using namespace metal;
|
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)]],
|
kernel void updateGeometryHeights(texture2d<float> texture [[texture(GeneratorTextureIndexIn)]],
|
||||||
constant float2 *texCoords [[buffer(GeneratorBufferIndexTexCoords)]],
|
constant float2 *texCoords [[buffer(GeneratorBufferIndexTexCoords)]],
|
||||||
constant Uniforms &uniforms [[buffer(GeneratorBufferIndexUniforms)]],
|
constant Uniforms &uniforms [[buffer(GeneratorBufferIndexUniforms)]],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue