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