2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
2013-06-07 16:06:22 +00:00
|
|
|
#include "kernel_jitter.h"
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
#ifdef __SOBOL__
|
|
|
|
|
2011-11-12 14:29:52 +00:00
|
|
|
/* skip initial numbers that are not as well distributed, especially the
|
2012-06-09 17:22:52 +00:00
|
|
|
* first sequence is just 0 everywhere, which can be problematic for e.g.
|
|
|
|
* path termination */
|
2011-11-12 14:29:52 +00:00
|
|
|
#define SOBOL_SKIP 64
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* High Dimensional Sobol */
|
|
|
|
|
|
|
|
/* van der corput radical inverse */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device uint van_der_corput(uint bits)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
bits = (bits << 16) | (bits >> 16);
|
|
|
|
bits = ((bits & 0x00ff00ff) << 8) | ((bits & 0xff00ff00) >> 8);
|
|
|
|
bits = ((bits & 0x0f0f0f0f) << 4) | ((bits & 0xf0f0f0f0) >> 4);
|
|
|
|
bits = ((bits & 0x33333333) << 2) | ((bits & 0xcccccccc) >> 2);
|
|
|
|
bits = ((bits & 0x55555555) << 1) | ((bits & 0xaaaaaaaa) >> 1);
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sobol radical inverse */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device uint sobol(uint i)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
uint r = 0;
|
|
|
|
|
|
|
|
for(uint v = 1U << 31; i; i >>= 1, v ^= v >> 1)
|
|
|
|
if(i & 1)
|
|
|
|
r ^= v;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* inverse of sobol radical inverse */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device uint sobol_inverse(uint i)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
const uint msb = 1U << 31;
|
|
|
|
uint r = 0;
|
|
|
|
|
|
|
|
for(uint v = 1; i; i <<= 1, v ^= v << 1)
|
|
|
|
if(i & msb)
|
|
|
|
r ^= v;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* multidimensional sobol with generator matrices
|
2012-06-09 17:22:52 +00:00
|
|
|
* dimension 0 and 1 are equal to van_der_corput() and sobol() respectively */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device uint sobol_dimension(KernelGlobals *kg, int index, int dimension)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
uint result = 0;
|
|
|
|
uint i = index;
|
|
|
|
|
|
|
|
for(uint j = 0; i; i >>= 1, j++)
|
|
|
|
if(i & 1)
|
|
|
|
result ^= kernel_tex_fetch(__sobol_directions, 32*dimension + j);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lookup index and x/y coordinate, assumes m is a power of two */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device uint sobol_lookup(const uint m, const uint frame, const uint ex, const uint ey, uint *x, uint *y)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* shift is constant per frame */
|
|
|
|
const uint shift = frame << (m << 1);
|
|
|
|
const uint sobol_shift = sobol(shift);
|
|
|
|
/* van der Corput is its own inverse */
|
|
|
|
const uint lower = van_der_corput(ex << (32 - m));
|
|
|
|
/* need to compensate for ey difference and shift */
|
|
|
|
const uint sobol_lower = sobol(lower);
|
|
|
|
const uint mask = ~-(1 << m) << (32 - m); /* only m upper bits */
|
|
|
|
const uint delta = ((ey << (32 - m)) ^ sobol_lower ^ sobol_shift) & mask;
|
|
|
|
/* only use m upper bits for the index (m is a power of two) */
|
|
|
|
const uint sobol_result = delta | (delta >> m);
|
|
|
|
const uint upper = sobol_inverse(sobol_result);
|
|
|
|
const uint index = shift | upper | lower;
|
|
|
|
*x = van_der_corput(index);
|
|
|
|
*y = sobol_shift ^ sobol_result ^ sobol_lower;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline float path_rng_1D(KernelGlobals *kg, RNG *rng, int sample, int num_samples, int dimension)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2013-06-11 21:58:48 +00:00
|
|
|
#ifdef __CMJ__
|
|
|
|
if(kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_CMJ) {
|
|
|
|
/* correlated multi-jittered */
|
|
|
|
int p = *rng + dimension;
|
|
|
|
return cmj_sample_1D(sample, num_samples, p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
#ifdef __SOBOL_FULL_SCREEN__
|
2013-06-09 16:37:04 +00:00
|
|
|
uint result = sobol_dimension(kg, *rng, dimension);
|
2011-04-27 11:58:34 +00:00
|
|
|
float r = (float)result * (1.0f/(float)0xFFFFFFFF);
|
|
|
|
return r;
|
|
|
|
#else
|
|
|
|
/* compute sobol sequence value using direction vectors */
|
2011-11-12 14:29:52 +00:00
|
|
|
uint result = sobol_dimension(kg, sample + SOBOL_SKIP, dimension);
|
2011-04-27 11:58:34 +00:00
|
|
|
float r = (float)result * (1.0f/(float)0xFFFFFFFF);
|
|
|
|
|
|
|
|
/* Cranly-Patterson rotation using rng seed */
|
|
|
|
float shift;
|
|
|
|
|
2014-02-26 17:47:50 +00:00
|
|
|
/* using the same *rng value to offset seems to give correlation issues,
|
|
|
|
* we could hash it with the dimension but this has a performance impact,
|
|
|
|
* we need to find a solution for this */
|
2011-04-27 11:58:34 +00:00
|
|
|
if(dimension & 1)
|
2013-06-21 12:34:34 +00:00
|
|
|
shift = (*rng >> 16) * (1.0f/(float)0xFFFF);
|
2011-04-27 11:58:34 +00:00
|
|
|
else
|
2013-06-21 12:34:34 +00:00
|
|
|
shift = (*rng & 0xFFFF) * (1.0f/(float)0xFFFF);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-06-09 17:45:22 +00:00
|
|
|
return r + shift - floorf(r + shift);
|
2011-04-27 11:58:34 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline void path_rng_2D(KernelGlobals *kg, RNG *rng, int sample, int num_samples, int dimension, float *fx, float *fy)
|
2013-06-07 16:06:22 +00:00
|
|
|
{
|
|
|
|
#ifdef __CMJ__
|
|
|
|
if(kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_CMJ) {
|
|
|
|
/* correlated multi-jittered */
|
2013-06-09 16:37:04 +00:00
|
|
|
int p = *rng + dimension;
|
2013-06-11 21:58:48 +00:00
|
|
|
cmj_sample_2D(sample, num_samples, p, fx, fy);
|
2013-06-07 16:06:22 +00:00
|
|
|
}
|
2013-07-23 16:23:30 +00:00
|
|
|
else
|
2013-06-07 16:06:22 +00:00
|
|
|
#endif
|
2013-07-23 16:23:30 +00:00
|
|
|
{
|
|
|
|
/* sobol */
|
|
|
|
*fx = path_rng_1D(kg, rng, sample, num_samples, dimension);
|
|
|
|
*fy = path_rng_1D(kg, rng, sample, num_samples, dimension + 1);
|
|
|
|
}
|
2013-06-07 16:06:22 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline void path_rng_init(KernelGlobals *kg, ccl_global uint *rng_state, int sample, int num_samples, RNG *rng, int x, int y, float *fx, float *fy)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
#ifdef __SOBOL_FULL_SCREEN__
|
|
|
|
uint px, py;
|
|
|
|
uint bits = 16; /* limits us to 65536x65536 and 65536 samples */
|
|
|
|
uint size = 1 << bits;
|
2011-09-16 13:14:02 +00:00
|
|
|
uint frame = sample;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
*rng = sobol_lookup(bits, frame, x, y, &px, &py);
|
|
|
|
|
2011-10-29 14:27:24 +00:00
|
|
|
*rng ^= kernel_data.integrator.seed;
|
|
|
|
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
if(sample == 0) {
|
|
|
|
*fx = 0.5f;
|
|
|
|
*fy = 0.5f;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*fx = size * (float)px * (1.0f/(float)0xFFFFFFFF) - x;
|
|
|
|
*fy = size * (float)py * (1.0f/(float)0xFFFFFFFF) - y;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
#else
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
*rng = *rng_state;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-10-29 14:27:24 +00:00
|
|
|
*rng ^= kernel_data.integrator.seed;
|
|
|
|
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
if(sample == 0) {
|
|
|
|
*fx = 0.5f;
|
|
|
|
*fy = 0.5f;
|
|
|
|
}
|
|
|
|
else {
|
2013-06-11 21:58:48 +00:00
|
|
|
path_rng_2D(kg, rng, sample, num_samples, PRNG_FILTER_U, fx, fy);
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device void path_rng_end(KernelGlobals *kg, ccl_global uint *rng_state, RNG rng)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* nothing to do */
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* Linear Congruential Generator */
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline float path_rng_1D(KernelGlobals *kg, RNG& rng, int sample, int num_samples, int dimension)
|
2013-06-07 16:06:22 +00:00
|
|
|
{
|
2013-06-11 21:58:48 +00:00
|
|
|
/* implicit mod 2^32 */
|
|
|
|
rng = (1103515245*(rng) + 12345);
|
|
|
|
return (float)rng * (1.0f/(float)0xFFFFFFFF);
|
2013-06-07 16:06:22 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline void path_rng_2D(KernelGlobals *kg, RNG& rng, int sample, int num_samples, int dimension, float *fx, float *fy)
|
2013-06-07 16:06:22 +00:00
|
|
|
{
|
2013-06-11 21:58:48 +00:00
|
|
|
*fx = path_rng_1D(kg, rng, sample, num_samples, dimension);
|
|
|
|
*fy = path_rng_1D(kg, rng, sample, num_samples, dimension + 1);
|
2013-06-07 16:06:22 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device void path_rng_init(KernelGlobals *kg, ccl_global uint *rng_state, int sample, int num_samples, RNG *rng, int x, int y, float *fx, float *fy)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* load state */
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
*rng = *rng_state;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-10-29 14:27:24 +00:00
|
|
|
*rng ^= kernel_data.integrator.seed;
|
|
|
|
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
if(sample == 0) {
|
|
|
|
*fx = 0.5f;
|
|
|
|
*fy = 0.5f;
|
|
|
|
}
|
|
|
|
else {
|
2013-06-11 21:58:48 +00:00
|
|
|
path_rng_2D(kg, rng, sample, num_samples, PRNG_FILTER_U, fx, fy);
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device void path_rng_end(KernelGlobals *kg, ccl_global uint *rng_state, RNG rng)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-09-16 13:14:02 +00:00
|
|
|
/* store state for next sample */
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
*rng_state = rng;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-01-03 01:48:48 +00:00
|
|
|
/* Linear Congruential Generator */
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device uint lcg_step_uint(uint *rng)
|
2013-08-18 14:15:57 +00:00
|
|
|
{
|
|
|
|
/* implicit mod 2^32 */
|
|
|
|
*rng = (1103515245*(*rng) + 12345);
|
|
|
|
return *rng;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float lcg_step_float(uint *rng)
|
2013-04-01 20:26:52 +00:00
|
|
|
{
|
|
|
|
/* implicit mod 2^32 */
|
|
|
|
*rng = (1103515245*(*rng) + 12345);
|
|
|
|
return (float)*rng * (1.0f/(float)0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device uint lcg_init(uint seed)
|
2013-04-01 20:26:52 +00:00
|
|
|
{
|
2013-04-17 14:48:01 +00:00
|
|
|
uint rng = seed;
|
2013-08-18 14:15:57 +00:00
|
|
|
lcg_step_uint(&rng);
|
2013-04-01 20:26:52 +00:00
|
|
|
return rng;
|
|
|
|
}
|
|
|
|
|
2014-01-03 01:48:48 +00:00
|
|
|
/* Path Tracing Utility Functions
|
|
|
|
*
|
|
|
|
* For each random number in each step of the path we must have a unique
|
|
|
|
* dimension to avoid using the same sequence twice.
|
|
|
|
*
|
|
|
|
* For branches in the path we must be careful not to reuse the same number
|
|
|
|
* in a sequence and offset accordingly. */
|
|
|
|
|
|
|
|
ccl_device_inline float path_state_rng_1D(KernelGlobals *kg, RNG *rng, PathState *state, int dimension)
|
|
|
|
{
|
|
|
|
return path_rng_1D(kg, rng, state->sample, state->num_samples, state->rng_offset + dimension);
|
|
|
|
}
|
|
|
|
|
|
|
|
ccl_device_inline void path_state_rng_2D(KernelGlobals *kg, RNG *rng, PathState *state, int dimension, float *fx, float *fy)
|
|
|
|
{
|
|
|
|
path_rng_2D(kg, rng, state->sample, state->num_samples, state->rng_offset + dimension, fx, fy);
|
|
|
|
}
|
|
|
|
|
|
|
|
ccl_device_inline float path_branched_rng_1D(KernelGlobals *kg, RNG *rng, PathState *state, int branch, int num_branches, int dimension)
|
|
|
|
{
|
|
|
|
return path_rng_1D(kg, rng, state->sample*num_branches + branch, state->num_samples*num_branches, state->rng_offset + dimension);
|
|
|
|
}
|
|
|
|
|
|
|
|
ccl_device_inline void path_branched_rng_2D(KernelGlobals *kg, RNG *rng, PathState *state, int branch, int num_branches, int dimension, float *fx, float *fy)
|
|
|
|
{
|
|
|
|
path_rng_2D(kg, rng, state->sample*num_branches + branch, state->num_samples*num_branches, state->rng_offset + dimension, fx, fy);
|
|
|
|
}
|
|
|
|
|
|
|
|
ccl_device_inline void path_state_branch(PathState *state, int branch, int num_branches)
|
|
|
|
{
|
|
|
|
/* path is splitting into a branch, adjust so that each branch
|
|
|
|
* still gets a unique sample from the same sequence */
|
|
|
|
state->rng_offset += PRNG_BOUNCE_NUM;
|
|
|
|
state->sample = state->sample*num_branches + branch;
|
|
|
|
state->num_samples = state->num_samples*num_branches;
|
|
|
|
}
|
|
|
|
|
2014-01-03 18:24:55 +00:00
|
|
|
ccl_device_inline uint lcg_state_init(RNG *rng, PathState *state, uint scramble)
|
2014-01-03 01:48:48 +00:00
|
|
|
{
|
|
|
|
return lcg_init(*rng + state->rng_offset + state->sample*scramble);
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|