This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* replace stratified sampling with halton sampling * experiment with random texture * remove stratified sampling files * remove texture experiments * divide instead of multiply to fix precision bug * combine stratified and halton sampling * blue noise (wip) * use white noise for preview * white noise from xorshift instead of texture * optimize setStrataCount * comments and cleanup * include noise texture * remove unneeded line * remove halton sequence code * remove unnecessary shuffle * remove stratifiedRandom array property * rename to stratifiedSampler * rename sceneSampler; add comments
- Loading branch information
Lucas
committed
Sep 11, 2019
1 parent
16d1312
commit 94d4fb9
Showing
17 changed files
with
365 additions
and
300 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,61 @@ | ||
// Random number generation as described by | ||
// http://www.reedbeta.com/blog/quick-and-easy-gpu-random-numbers-in-d3d11/ | ||
|
||
export default function(params) { | ||
return ` | ||
|
||
// higher quality but slower hashing function | ||
uint wangHash(uint x) { | ||
x = (x ^ 61u) ^ (x >> 16u); | ||
x *= 9u; | ||
x = x ^ (x >> 4u); | ||
x *= 0x27d4eb2du; | ||
x = x ^ (x >> 15u); | ||
return x; | ||
} | ||
// Noise texture used to generate a different random number for each pixel. | ||
// We use blue noise in particular, but any type of noise will work. | ||
uniform sampler2D noise; | ||
|
||
uniform float stratifiedSamples[SAMPLING_DIMENSIONS]; | ||
uniform float strataSize; | ||
uniform float useStratifiedSampling; | ||
|
||
// Every time we call randomSample() in the shader, and for every call to render, | ||
// we want that specific bit of the shader to fetch a sample from the same position in stratifiedSamples | ||
// This allows us to use stratified sampling for each random variable in our path tracing | ||
int sampleIndex = 0; | ||
|
||
const highp float maxUint = 1.0 / 4294967295.0; | ||
|
||
float pixelSeed; | ||
highp uint randState; | ||
|
||
// lower quality but faster hashing function | ||
// simple integer hashing function | ||
// https://en.wikipedia.org/wiki/Xorshift | ||
uint xorshift(uint x) { | ||
x ^= x << 13u; | ||
x ^= x >> 17u; | ||
x ^= x << 5u; | ||
return x; | ||
} | ||
|
||
uniform float seed; // Random number [0, 1) | ||
uniform float strataStart[STRATA_DIMENSIONS]; | ||
uniform float strataSize; | ||
void initRandom() { | ||
vec2 noiseSize = vec2(textureSize(noise, 0)); | ||
|
||
const highp float maxUint = 1.0 / 4294967295.0; | ||
highp uint randState; | ||
int strataDimension; | ||
// tile the small noise texture across the entire screen | ||
pixelSeed = texture(noise, vCoord / (pixelSize * noiseSize)).r; | ||
|
||
// init state with high quality hashing function to avoid patterns across the 2d image | ||
void initRandom() { | ||
randState = wangHash(floatBitsToUint(seed)); | ||
randState *= wangHash(floatBitsToUint(vCoord.x)); | ||
randState *= wangHash(floatBitsToUint(vCoord.y)); | ||
randState = wangHash(randState); | ||
strataDimension = 0; | ||
// white noise used if stratified sampling is disabled | ||
// produces more balanced path tracing for 1 sample-per-pixel renders | ||
randState = xorshift(xorshift(floatBitsToUint(vCoord.x)) * xorshift(floatBitsToUint(vCoord.y))); | ||
} | ||
|
||
float random() { | ||
float randomSample() { | ||
randState = xorshift(randState); | ||
float f = float(randState) * maxUint; | ||
|
||
// transform random number between [0, 1] to (0, 1) | ||
return EPS + (1.0 - 2.0 * EPS) * f; | ||
} | ||
float stratifiedSample = stratifiedSamples[sampleIndex++]; | ||
|
||
vec2 randomVec2() { | ||
return vec2(random(), random()); | ||
} | ||
float random = mix( | ||
float(randState) * maxUint, // white noise | ||
fract((stratifiedSample + pixelSeed) * strataSize), // blue noise + stratified samples | ||
useStratifiedSampling | ||
); | ||
|
||
float randomStrata() { | ||
return strataStart[strataDimension++] + strataSize * random(); | ||
// transform random number between [0, 1] to (0, 1) | ||
return EPS + (1.0 - 2.0 * EPS) * random; | ||
} | ||
|
||
vec2 randomStrataVec2() { | ||
return vec2(randomStrata(), randomStrata()); | ||
vec2 randomSampleVec2() { | ||
return vec2(randomSample(), randomSample()); | ||
} | ||
`; | ||
}; |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Oops, something went wrong.