Files
blender/intern/cycles/kernel/sample/pattern.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

163 lines
4.6 KiB
C
Raw Normal View History

/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#pragma once
#include "kernel/sample/jitter.h"
#include "util/hash.h"
CCL_NAMESPACE_BEGIN
/* Pseudo random numbers, uncomment this for debugging correlations. Only run
2018-01-19 15:34:54 +11:00
* this single threaded on a CPU for repeatable results. */
//#define __DEBUG_CORRELATION__
/* High Dimensional Sobol.
*
* Multidimensional sobol with generator matrices. Dimension 0 and 1 are equal
* to classic Van der Corput and Sobol sequences. */
#ifdef __SOBOL__
/* Skip initial numbers that for some dimensions have clear patterns that
* don't cover the entire sample space. Ideally we would have a better
* progressive pattern that doesn't suffer from this problem, because even
* with this offset some dimensions are quite poor.
*/
# define SOBOL_SKIP 64
ccl_device uint sobol_dimension(KernelGlobals kg, int index, int dimension)
{
uint result = 0;
uint i = index + SOBOL_SKIP;
2019-08-26 15:30:12 +02:00
for (int j = 0, x; (x = find_first_set(i)); i >>= x) {
j += x;
result ^= __float_as_uint(kernel_tex_fetch(__sample_pattern_lut, 32 * dimension + j - 1));
}
return result;
}
#endif /* __SOBOL__ */
ccl_device_forceinline float path_rng_1D(KernelGlobals kg,
uint rng_hash,
int sample,
int dimension)
{
#ifdef __DEBUG_CORRELATION__
return (float)drand48();
#endif
#ifdef __SOBOL__
if (kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_PMJ)
#endif
{
return pmj_sample_1D(kg, sample, rng_hash, dimension);
}
#ifdef __SOBOL__
/* Sobol sequence value using direction vectors. */
uint result = sobol_dimension(kg, sample, dimension);
float r = (float)result * (1.0f / (float)0xFFFFFFFF);
/* Cranly-Patterson rotation using rng seed */
float shift;
/* Hash rng with dimension to solve correlation issues.
* See T38710, T50116.
*/
uint tmp_rng = cmj_hash_simple(dimension, rng_hash);
shift = tmp_rng * (kernel_data.integrator.scrambling_distance / (float)0xFFFFFFFF);
return r + shift - floorf(r + shift);
#endif
}
ccl_device_forceinline void path_rng_2D(KernelGlobals kg,
Cycles: Kernel address space changes for MSL This is the first of a sequence of changes to support compiling Cycles kernels as MSL (Metal Shading Language) in preparation for a Metal GPU device implementation. MSL requires that all pointer types be declared with explicit address space attributes (device, thread, etc...). There is already precedent for this with Cycles' address space macros (ccl_global, ccl_private, etc...), therefore the first step of MSL-enablement is to apply these consistently. Line-for-line this represents the largest change required to enable MSL. Applying this change first will simplify future patches as well as offering the emergent benefit of enhanced descriptiveness. The vast majority of deltas in this patch fall into one of two cases: - Ensuring ccl_private is specified for thread-local pointer types - Ensuring ccl_global is specified for device-wide pointer types Additionally, the ccl_addr_space qualifier can be removed. Prior to Cycles X, ccl_addr_space was used as a context-dependent address space qualifier, but now it is either redundant (e.g. in struct typedefs), or can be replaced by ccl_global in the case of pointer types. Associated function variants (e.g. lcg_step_float_addrspace) are also redundant. In cases where address space qualifiers are chained with "const", this patch places the address space qualifier first. The rationale for this is that the choice of address space is likely to have the greater impact on runtime performance and overall architecture. The final part of this patch is the addition of a metal/compat.h header. This is partially complete and will be extended in future patches, paving the way for the full Metal implementation. Ref T92212 Reviewed By: brecht Maniphest Tasks: T92212 Differential Revision: https://developer.blender.org/D12864
2021-10-14 13:53:40 +01:00
uint rng_hash,
int sample,
int dimension,
ccl_private float *fx,
ccl_private float *fy)
{
#ifdef __DEBUG_CORRELATION__
*fx = (float)drand48();
*fy = (float)drand48();
return;
#endif
#ifdef __SOBOL__
if (kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_PMJ)
#endif
{
pmj_sample_2D(kg, sample, rng_hash, dimension, fx, fy);
return;
}
#ifdef __SOBOL__
/* Sobol. */
*fx = path_rng_1D(kg, rng_hash, sample, dimension);
*fy = path_rng_1D(kg, rng_hash, sample, dimension + 1);
#endif
}
/**
2021-09-22 14:48:01 +10:00
* 1D hash recommended from "Hash Functions for GPU Rendering" JCGT Vol. 9, No. 3, 2020
* See https://www.shadertoy.com/view/4tXyWN and https://www.shadertoy.com/view/XlGcRh
* http://www.jcgt.org/published/0009/03/02/paper.pdf
*/
ccl_device_inline uint hash_iqint1(uint n)
{
n = (n << 13U) ^ n;
n = n * (n * n * 15731U + 789221U) + 1376312589U;
return n;
}
/**
2021-09-22 14:48:01 +10:00
* 2D hash recommended from "Hash Functions for GPU Rendering" JCGT Vol. 9, No. 3, 2020
* See https://www.shadertoy.com/view/4tXyWN and https://www.shadertoy.com/view/XlGcRh
* http://www.jcgt.org/published/0009/03/02/paper.pdf
*/
ccl_device_inline uint hash_iqnt2d(const uint x, const uint y)
{
const uint qx = 1103515245U * ((x >> 1U) ^ (y));
const uint qy = 1103515245U * ((y >> 1U) ^ (x));
const uint n = 1103515245U * ((qx) ^ (qy >> 3U));
return n;
}
ccl_device_inline uint path_rng_hash_init(KernelGlobals kg,
const int sample,
const int x,
const int y)
{
const uint rng_hash = hash_iqnt2d(x, y) ^ kernel_data.integrator.seed;
#ifdef __DEBUG_CORRELATION__
srand48(rng_hash + sample);
#else
(void)sample;
#endif
return rng_hash;
}
ccl_device_inline bool sample_is_even(int pattern, int sample)
{
if (pattern == SAMPLING_PATTERN_PMJ) {
/* See Section 10.2.1, "Progressive Multi-Jittered Sample Sequences", Christensen et al.
* We can use this to get divide sample sequence into two classes for easier variance
* estimation. */
return popcount(uint(sample) & 0xaaaaaaaa) & 1;
}
else {
/* TODO(Stefan): Are there reliable ways of dividing CMJ and Sobol into two classes? */
return sample & 0x1;
}
}
CCL_NAMESPACE_END