Blender V4.3
pattern.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
9#include "util/hash.h"
10
12
13/* Pseudo random numbers, uncomment this for debugging correlations. Only run
14 * this single threaded on a CPU for repeatable results. */
15// #define __DEBUG_CORRELATION__
16
17/*
18 * The `path_rng_*()` functions below use a shuffled scrambled Sobol
19 * sequence to generate their samples. Sobol samplers have a property
20 * that is worth being aware of when choosing how to use the sample
21 * dimensions:
22 *
23 * 1. In general, earlier sets of dimensions are better stratified. So
24 * prefer e.g. x,y over y,z over z,w for the things that are most
25 * important to sample well.
26 * 2. As a rule of thumb, dimensions that are closer to each other are
27 * better stratified than dimensions that are far. So prefer e.g.
28 * x,y over x,z.
29 */
30
32{
33 if (kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_SOBOL_BURLEY) {
34 /* One sequence per pixel, using the length mask optimization. */
35 return make_uint3(sample, pixel_index, kernel_data.integrator.sobol_index_mask);
36 }
37 else if (kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_BLUE_NOISE_PURE) {
38 /* For blue-noise samples, we use a single sequence (seed 0) with each pixel receiving
39 * a section of it.
40 * The total length is expected to get very large (effectively pixel count times sample count),
41 * so we don't use the length mask optimization here. */
42 pixel_index *= kernel_data.integrator.blue_noise_sequence_length;
43 return make_uint3(sample + pixel_index, 0, 0xffffffff);
44 }
45 else if (kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_BLUE_NOISE_FIRST) {
46 /* The "first" pattern uses a 1SPP blue-noise sequence for the first sample, and a separate
47 * N-1 SPP sequence for the remaining pixels. The purpose of this is to get blue-noise
48 * properties during viewport navigation, which will generally use 1 SPP.
49 * Unfortunately using just the first sample of a full blue-noise sequence doesn't give
50 * its benefits, so we combine the two as a tradeoff between quality at 1 SPP and full SPP. */
51 if (sample == 0) {
52 return make_uint3(pixel_index, 0x0cd0519f, 0xffffffff);
53 }
54 else {
55 pixel_index *= kernel_data.integrator.blue_noise_sequence_length;
56 return make_uint3((sample - 1) + pixel_index, 0, 0xffffffff);
57 }
58 }
59 else {
60 kernel_assert(false);
61 return make_uint3(0, 0, 0);
62 }
63}
64
66 uint rng_pixel,
68 int dimension)
69{
70#ifdef __DEBUG_CORRELATION__
71 return (float)drand48();
72#endif
73
74 if (kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_TABULATED_SOBOL) {
75 return tabulated_sobol_sample_1D(kg, sample, rng_pixel, dimension);
76 }
77
78 uint3 index = blue_noise_indexing(kg, rng_pixel, sample);
79 return sobol_burley_sample_1D(index.x, dimension, index.y, index.z);
80}
81
83 uint rng_pixel,
84 int sample,
85 int dimension)
86{
87#ifdef __DEBUG_CORRELATION__
88 return make_float2((float)drand48(), (float)drand48());
89#endif
90
91 if (kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_TABULATED_SOBOL) {
92 return tabulated_sobol_sample_2D(kg, sample, rng_pixel, dimension);
93 }
94
95 uint3 index = blue_noise_indexing(kg, rng_pixel, sample);
96 return sobol_burley_sample_2D(index.x, dimension, index.y, index.z);
97}
98
100 uint rng_pixel,
101 int sample,
102 int dimension)
103{
104#ifdef __DEBUG_CORRELATION__
105 return make_float3((float)drand48(), (float)drand48(), (float)drand48());
106#endif
107
108 if (kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_TABULATED_SOBOL) {
109 return tabulated_sobol_sample_3D(kg, sample, rng_pixel, dimension);
110 }
111
112 uint3 index = blue_noise_indexing(kg, rng_pixel, sample);
113 return sobol_burley_sample_3D(index.x, dimension, index.y, index.z);
114}
115
117 uint rng_pixel,
118 int sample,
119 int dimension)
120{
121#ifdef __DEBUG_CORRELATION__
122 return make_float4((float)drand48(), (float)drand48(), (float)drand48(), (float)drand48());
123#endif
124
125 if (kernel_data.integrator.sampling_pattern == SAMPLING_PATTERN_TABULATED_SOBOL) {
126 return tabulated_sobol_sample_4D(kg, sample, rng_pixel, dimension);
127 }
128
129 uint3 index = blue_noise_indexing(kg, rng_pixel, sample);
130 return sobol_burley_sample_4D(index.x, dimension, index.y, index.z);
131}
132
134 const int sample,
135 const int x,
136 const int y)
137{
138 const uint pattern = kernel_data.integrator.sampling_pattern;
140#ifdef __DEBUG_CORRELATION__
141 return srand48(rng_pixel + sample);
142#else
143 (void)sample;
144#endif
145
146 /* The white-noise samplers use a random per-pixel hash to generate independent sequences. */
147 return hash_iqnt2d(x, y) ^ kernel_data.integrator.seed;
148 }
149 else {
150 /* The blue-noise samplers use a single sequence for all pixels, but offset the index within
151 * the sequence for each pixel. We use a hierarchically shuffled 2D morton curve to determine
152 * each pixel's offset along the sequence.
153 *
154 * Based on:
155 * https://psychopath.io/post/2022_07_24_owen_scrambling_based_dithered_blue_noise_sampling.
156 *
157 * TODO(lukas): Use a precomputed Hilbert curve to avoid directionality bias in the noise
158 * distribution. We can just precompute a small-ish tile and repeat it in morton code order.
159 */
160 return nested_uniform_scramble_base4(morton2d(x, y), kernel_data.integrator.seed);
161 }
162}
163
169{
170#if 0
171 if (!(pattern == SAMPLING_PATTERN_TABULATED_SOBOL || pattern == SAMPLING_PATTERN_SOBOL_BURLEY)) {
172 /* Fallback: assign samples randomly.
173 * This is guaranteed to work "okay" for any sampler, but isn't good.
174 * (NOTE: the seed constant is just a random number to guard against
175 * possible interactions with other uses of the hash. There's nothing
176 * special about it.)
177 */
178 return hash_hp_seeded_uint(sample, 0xa771f873) & 1;
179 }
180#else
181 (void)pattern;
182#endif
183
184 /* This follows the approach from section 10.2.1 of "Progressive
185 * Multi-Jittered Sample Sequences" by Christensen et al., but
186 * implemented with efficient bit-fiddling.
187 *
188 * This approach also turns out to work equally well with Owen
189 * scrambled and shuffled Sobol (see https://developer.blender.org/D15746#429471).
190 */
191 return popcount(uint(sample) & 0xaaaaaaaa) & 1;
192}
unsigned int uint
ccl_device_inline uint nested_uniform_scramble_base4(uint i, uint seed)
ccl_device_inline uint morton2d(uint x, uint y)
#define kernel_assert(cond)
#define kernel_data
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define ccl_device_forceinline
#define ccl_device_inline
#define CCL_NAMESPACE_END
ccl_device_forceinline float4 make_float4(const float x, const float y, const float z, const float w)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline uint3 make_uint3(const uint x, const uint y, const uint z)
ccl_device_forceinline float2 make_float2(const float x, const float y)
ccl_device_inline uint hash_iqnt2d(const uint x, const uint y)
Definition hash.h:520
ccl_device_inline uint hash_hp_seeded_uint(uint i, uint seed)
Definition hash.h:421
ccl_device float3 tabulated_sobol_sample_3D(KernelGlobals kg, uint sample, const uint rng_hash, const uint dimension)
ccl_device float2 tabulated_sobol_sample_2D(KernelGlobals kg, uint sample, const uint rng_hash, const uint dimension)
ccl_device float4 tabulated_sobol_sample_4D(KernelGlobals kg, uint sample, const uint rng_hash, const uint dimension)
ccl_device float tabulated_sobol_sample_1D(KernelGlobals kg, uint sample, const uint rng_hash, const uint dimension)
@ SAMPLING_PATTERN_BLUE_NOISE_FIRST
@ SAMPLING_PATTERN_TABULATED_SOBOL
@ SAMPLING_PATTERN_BLUE_NOISE_PURE
@ SAMPLING_PATTERN_SOBOL_BURLEY
ccl_device_inline uint path_rng_pixel_init(KernelGlobals kg, const int sample, const int x, const int y)
Definition pattern.h:133
ccl_device_forceinline float2 path_rng_2D(KernelGlobals kg, uint rng_pixel, int sample, int dimension)
Definition pattern.h:82
ccl_device_forceinline float path_rng_1D(KernelGlobals kg, uint rng_pixel, uint sample, int dimension)
Definition pattern.h:65
ccl_device_forceinline float3 path_rng_3D(KernelGlobals kg, uint rng_pixel, int sample, int dimension)
Definition pattern.h:99
ccl_device_forceinline float4 path_rng_4D(KernelGlobals kg, uint rng_pixel, int sample, int dimension)
Definition pattern.h:116
CCL_NAMESPACE_BEGIN ccl_device_forceinline uint3 blue_noise_indexing(KernelGlobals kg, uint pixel_index, uint sample)
Definition pattern.h:31
ccl_device_inline bool sample_is_class_A(int pattern, int sample)
Definition pattern.h:168
ccl_device float sobol_burley_sample_1D(uint index, uint const dimension, uint seed, uint shuffled_index_mask)
ccl_device float2 sobol_burley_sample_2D(uint index, const uint dimension_set, uint seed, uint shuffled_index_mask)
ccl_device float4 sobol_burley_sample_4D(uint index, const uint dimension_set, uint seed, uint shuffled_index_mask)
ccl_device float3 sobol_burley_sample_3D(uint index, const uint dimension_set, uint seed, uint shuffled_index_mask)
ccl_device_inline uint popcount(uint x)
Definition util/math.h:855