Blender V5.3
eevee_sampling_lib.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
38
39#pragma once
40
45
46/* -------------------------------------------------------------------- */
53
54namespace eevee {
55
56struct Sampling {
57 [[storage(SAMPLING_BUF_SLOT, read)]] const SamplingData &sampling_buf;
58
59 float rng_1D_get(const eSamplingDimension dim) const
60 {
61 return sampling_buf.dimensions[dim];
62 }
63
64 float2 rng_2D_get(const eSamplingDimension dim) const
65 {
66 return float2(sampling_buf.dimensions[dim], sampling_buf.dimensions[dim + 1u]);
67 }
68
69 float3 rng_3D_get(const eSamplingDimension dim) const
70 {
71 return float3(sampling_buf.dimensions[dim],
72 sampling_buf.dimensions[dim + 1u],
73 sampling_buf.dimensions[dim + 2u]);
74 }
75};
76
77} // namespace eevee
78
80
81/* -------------------------------------------------------------------- */
84
85namespace random {
86
87namespace detail {
88
91{
92#if 0 /* Reference */
93 bits = (bits << 16u) | (bits >> 16u);
94 bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
95 bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
96 bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
97 bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
98#else
99 bits = bitfieldReverse(bits);
100#endif
101 /* Same as dividing by 0x100000000. */
102 return float(bits) * 2.3283064365386963e-10f;
103}
104
109float interleaved_gradient(float2 v, float offset = 0.0f)
110{
111 return fract(offset + 52.9829189f * fract(0.06711056f * v.x + 0.00583715f * v.y));
112}
113
118uint pcg(uint u)
119{
120 uint state = u * 747796405u + 2891336453u;
121 uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
122 return (word >> 22u) ^ word;
123}
124
125/* Reciprocal of unsigned integer maximum used in PCG functions below. */
126static constexpr float pcg_rcp = 1.0f / float(0xffffffffU);
127
128} // namespace detail
129
131float interleaved_gradient(float2 pixel, float seed, float offset)
132{
133 pixel += seed * (float2(47, 17) * 0.695f);
134 return detail::interleaved_gradient(pixel, offset);
135}
136
139{
140 return float2(interleaved_gradient(pixel, seed.x, offset.x),
141 interleaved_gradient(pixel, seed.y, offset.y));
142}
143
146{
147 return float3(interleaved_gradient(pixel, seed.x, offset.x),
148 interleaved_gradient(pixel, seed.y, offset.y),
149 interleaved_gradient(pixel, seed.z, offset.z));
150}
151
153float pcg(float v)
154{
156}
157
159float pcg(float2 v)
160{
161 uint2 u = floatBitsToUint(v);
162 return detail::pcg(detail::pcg(u.x) + u.y) * detail::pcg_rcp;
163}
164
166float pcg(float3 v)
167{
168 uint3 u = floatBitsToUint(v);
169 return detail::pcg(detail::pcg(detail::pcg(u.x) + u.y) + u.z) * detail::pcg_rcp;
170}
171
174{
175 uint3 u = floatBitsToUint(v);
176
177 u = u * 1664525u + 1013904223u;
178
179 u.x += u.y * u.z;
180 u.y += u.z * u.x;
181 u.z += u.x * u.y;
182
183 u ^= u >> 16u;
184
185 u.x += u.y * u.z;
186 u.y += u.z * u.x;
187 u.z += u.x * u.y;
188
189 return float3(u) * detail::pcg_rcp;
190}
191
194{
195 uint4 u = floatBitsToUint(v);
196
197 u = u * 1664525u + 1013904223u;
198
199 u.x += u.y * u.w;
200 u.y += u.z * u.x;
201 u.z += u.x * u.y;
202 u.w += u.y * u.z;
203
204 u ^= u >> 16u;
205
206 u.x += u.y * u.w;
207 u.y += u.z * u.x;
208 u.z += u.x * u.y;
209 u.w += u.y * u.z;
210
211 return float4(u) * detail::pcg_rcp;
212}
213
214/* 2D Noise function using the Hammersley point set, following [damm2012]. */
215template<typename T> float2 hammersley_2d(T i, T sample_count)
216{
217 float2 rand;
218 rand.x = float(i) / float(sample_count);
220 return rand;
221}
222template float2 hammersley_2d<float>(float i, float sample_count);
223template float2 hammersley_2d<uint>(uint i, uint sample_count);
224template float2 hammersley_2d<int>(int i, int sample_count);
225
226} // namespace random
227
228/* Not random but still useful. sample_count should be an even. */
229float2 regular_grid_2d(int i, int sample_count)
230{
231 int sample_per_dim = int(sqrt(float(sample_count)));
232 return (float2(i % sample_per_dim, i / sample_per_dim) + 0.5f) / float(sample_per_dim);
233}
234
236
237/* -------------------------------------------------------------------- */
242
243/* Given 1 random number in [0..1] range, return a random unit circle sample. */
245{
246 float phi = (rand - 0.5f) * M_TAU;
247 float cos_phi = cos(phi);
248 float sin_phi = sqrt(1.0f - square(cos_phi)) * sign(phi);
249 return float2(cos_phi, sin_phi);
250}
251
252/* Given 2 random number in [0..1] range, return a random unit disk sample. */
254{
255 return sample_circle(rand.y) * sqrt(rand.x);
256}
257
258/* This transform a 2d random sample (in [0..1] range) to a sample located on a cylinder of the
259 * same range. This is because the sampling functions expect such a random sample which is
260 * normally precomputed. */
262{
263 return float3(rand.x, sample_circle(rand.y));
264}
265
273{
274 float cos_theta = rand.x * 2.0f - 1.0f;
275 float sin_theta = safe_sqrt(1.0f - cos_theta * cos_theta);
276 return float3(sin_theta * sample_circle(rand.y), cos_theta);
277}
278
284{
285 /* Completely ad-hoc, but works well in practice and is fast. */
286 return sample_sphere(rand.xy) * sqrt(sqrt(rand.z));
287}
288
296{
297 float cos_theta = rand.x;
298 float sin_theta = safe_sqrt(1.0f - square(cos_theta));
299 return float3(sin_theta * sample_circle(rand.y), cos_theta);
300}
301
309float3 sample_uniform_cone(float2 rand, float cos_angle)
310{
311 float cos_theta = mix(cos_angle, 1.0f, rand.x);
312 float sin_theta = safe_sqrt(1.0f - square(cos_theta));
313 return float3(sin_theta * sample_circle(rand.y), cos_theta);
314}
315
322{
323 const float cos_theta = safe_sqrt(rand.x);
324 const float sin_theta = safe_sqrt(1.0f - rand.x);
325 return float3(sin_theta * sample_circle(rand.y), cos_theta);
326}
327
334{
335 const float cos_theta = safe_sqrt(rand_cylinder.x);
336 return float3(rand_cylinder.yz * sin_from_cos(cos_theta), cos_theta);
337}
338
#define M_TAU
ccl_device_inline float cos_theta(const float3 w)
ccl_device float sin_phi(const float3 w)
ccl_device_inline float sin_theta(const float3 w)
#define SAMPLING_BUF_SLOT
float3 sample_cos_hemisphere(const float2 rand)
float3 sample_cylinder(float2 rand)
float3 sample_ball(float3 rand)
float2 sample_disk(float2 rand)
float3 sample_sphere(float2 rand)
float3 sample_uniform_cone(float2 rand, float cos_angle)
float2 sample_circle(float rand)
float2 regular_grid_2d(int i, int sample_count)
float3 sample_hemisphere(float2 rand)
#define storage(slot, qualifiers)
T fract(T) RET
#define cos
constexpr T sign(T) RET
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
#define sqrt
VecBase< int, D > bitfieldReverse(VecOp< int, D >) RET
float square(float v)
float sin_from_cos(float c)
float safe_sqrt(float a)
const int state
#define T
float van_der_corput_radical_inverse(uint bits)
float interleaved_gradient(float2 v, float offset=0.0f)
static constexpr float pcg_rcp
float3 pcg_3d(float3 v)
float3 interleaved_gradient_3d(float2 pixel, float3 seed, float3 offset)
float4 pcg_4d(float4 v)
float2 interleaved_gradient_2d(float2 pixel, float2 seed, float2 offset)
float interleaved_gradient(float2 pixel, float seed, float offset)
float pcg(float v)
float2 hammersley_2d(T i, T sample_count)
#define mix
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float3 rng_3D_get(const eSamplingDimension dim) const
const SamplingData & sampling_buf
float2 rng_2D_get(const eSamplingDimension dim) const
float rng_1D_get(const eSamplingDimension dim) const
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
uint x
Definition types_uint2.h:13
uint y
Definition types_uint2.h:13
uint y
Definition types_uint3.h:13
uint z
Definition types_uint3.h:13
uint x
Definition types_uint3.h:13
uint x
Definition types_uint4.h:13
uint y
Definition types_uint4.h:13
uint z
Definition types_uint4.h:13
uint w
Definition types_uint4.h:13