Blender V4.5
eevee_sampling.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "BKE_colortools.hh"
12#include "BKE_scene.hh"
13
14#include "BLI_rand.h"
15
16#include "BLI_math_base.hh"
17#include "BLI_math_base_safe.h"
18
19#include "eevee_instance.hh"
20#include "eevee_sampling.hh"
21
22namespace blender::eevee {
23
24/* -------------------------------------------------------------------- */
27
28void Sampling::init(const Scene *scene)
29{
30 sample_count_ = inst_.is_viewport() ? scene->eevee.taa_samples : scene->eevee.taa_render_samples;
31
32 if (inst_.is_image_render) {
33 sample_count_ = math::max(uint64_t(1), sample_count_);
34 }
35
36 if (sample_count_ == 0) {
37 BLI_assert(inst_.is_viewport());
38 sample_count_ = infinite_sample_count_;
39 }
40
41 if (inst_.is_viewport()) {
42 /* We can't rely on the film module as it is initialized later. */
43 int pixel_size = BKE_render_preview_pixel_size(&inst_.scene->r);
44 if (pixel_size > 1) {
45 /* Enforce to render at least all the film pixel once. */
46 sample_count_ = max_ii(sample_count_, square_i(pixel_size));
47 }
48 }
49
50 motion_blur_steps_ = !inst_.is_viewport() && ((scene->r.mode & R_MBLUR) != 0) ?
52 1;
53 sample_count_ = divide_ceil_u(sample_count_, motion_blur_steps_);
54
55 if (scene->eevee.flag & SCE_EEVEE_DOF_JITTER) {
56 if (sample_count_ == infinite_sample_count_) {
57 /* Special case for viewport continuous rendering. We clamp to a max sample
58 * to avoid the jittered dof never converging. */
59 dof_ring_count_ = 6;
60 }
61 else {
62 dof_ring_count_ = sampling_web_ring_count_get(dof_web_density_, sample_count_);
63 }
64 dof_sample_count_ = sampling_web_sample_count_get(dof_web_density_, dof_ring_count_);
65 /* Change total sample count to fill the web pattern entirely. */
66 sample_count_ = divide_ceil_u(sample_count_, dof_sample_count_) * dof_sample_count_;
67 }
68 else {
69 dof_ring_count_ = 0;
70 dof_sample_count_ = 1;
71 }
72
73 /* Only multiply after to have full the full DoF web pattern for each time steps. */
74 sample_count_ *= motion_blur_steps_;
75
76 auto clamp_value_load = [](float value) { return (value > 0.0) ? value : 1e20; };
77
78 clamp_data_.sun_threshold = clamp_value_load(inst_.world.sun_threshold());
79 clamp_data_.surface_direct = clamp_value_load(scene->eevee.clamp_surface_direct);
80 clamp_data_.surface_indirect = clamp_value_load(scene->eevee.clamp_surface_indirect);
81 clamp_data_.volume_direct = clamp_value_load(scene->eevee.clamp_volume_direct);
82 clamp_data_.volume_indirect = clamp_value_load(scene->eevee.clamp_volume_indirect);
83}
84
85void Sampling::init(const Object &probe_object)
86{
87 BLI_assert(inst_.is_baking());
88 const ::LightProbe &lightprobe = DRW_object_get_data_for_drawing<::LightProbe>(probe_object);
89
90 sample_count_ = max_ii(1, lightprobe.grid_bake_samples);
91 sample_ = 0;
92}
93
95{
96 if (reset_) {
97 viewport_sample_ = 0;
98 }
99
100 if (inst_.is_viewport()) {
101
102 interactive_mode_ = viewport_sample_ < interactive_mode_threshold;
103
104 bool interactive_mode_disabled = (inst_.scene->eevee.flag & SCE_EEVEE_TAA_REPROJECTION) == 0 ||
105 inst_.is_viewport_image_render;
106 if (interactive_mode_disabled) {
107 interactive_mode_ = false;
108 sample_ = viewport_sample_;
109 }
110 else if (interactive_mode_) {
111 int interactive_sample_count = interactive_sample_max_;
112
113 if (viewport_sample_ < interactive_sample_count) {
114 /* Loop over the same starting samples. */
115 sample_ = sample_ % interactive_sample_count;
116 }
117 else {
118 /* Break out of the loop and resume normal pattern. */
119 sample_ = interactive_sample_count;
120 }
121 }
122 }
123}
124
126{
127 {
128 /* Repeat the sequence for all pixels that are being up-scaled. */
129 uint64_t sample_filter = sample_ / square_i(inst_.film.scaling_factor_get());
130 if (interactive_mode()) {
131 sample_filter = sample_filter % interactive_sample_aa_;
132 }
133 /* TODO(fclem) we could use some persistent states to speedup the computation. */
134 double2 r, offset = {0, 0};
135 /* Using 2,3 primes as per UE4 Temporal AA presentation.
136 * http://advances.realtimerendering.com/s2014/epic/TemporalAA.pptx (slide 14) */
137 uint2 primes = {2, 3};
138 BLI_halton_2d(primes, offset, sample_filter + 1, r);
139 /* WORKAROUND: We offset the distribution to make the first sample (0,0). This way, we are
140 * assured that at least one of the samples inside the TAA rotation will match the one from the
141 * draw manager. This makes sure overlays are correctly composited in static scene. */
142 data_.dimensions[SAMPLING_FILTER_U] = fractf(r[0] + (1.0 / 2.0));
143 data_.dimensions[SAMPLING_FILTER_V] = fractf(r[1] + (2.0 / 3.0));
144 /* TODO de-correlate. */
145 data_.dimensions[SAMPLING_TIME] = r[0];
146 data_.dimensions[SAMPLING_CLOSURE] = r[1];
147 data_.dimensions[SAMPLING_RAYTRACE_X] = r[0];
148 }
149 {
150 double3 r, offset = {0, 0, 0};
151 uint3 primes = {5, 7, 3};
152 BLI_halton_3d(primes, offset, sample_ + 1, r);
153 data_.dimensions[SAMPLING_LENS_U] = r[0];
154 data_.dimensions[SAMPLING_LENS_V] = r[1];
155 /* TODO de-correlate. */
156 data_.dimensions[SAMPLING_LIGHTPROBE] = r[0];
157 data_.dimensions[SAMPLING_TRANSPARENCY] = r[1];
158 /* TODO de-correlate. */
159 data_.dimensions[SAMPLING_AO_U] = r[0];
160 data_.dimensions[SAMPLING_AO_V] = r[1];
161 data_.dimensions[SAMPLING_AO_W] = r[2];
162 /* TODO de-correlate. */
163 data_.dimensions[SAMPLING_CURVES_U] = r[0];
164 }
165 {
166 uint64_t sample_raytrace = sample_;
167 if (interactive_mode()) {
168 sample_raytrace = sample_raytrace % interactive_sample_raytrace_;
169 }
170 /* Using leaped Halton sequence so we can reused the same primes as lens. */
171 double3 r, offset = {0, 0, 0};
172 uint64_t leap = 13;
173 uint3 primes = {5, 7, 11};
174 BLI_halton_3d(primes, offset, sample_raytrace * leap + 1, r);
175 data_.dimensions[SAMPLING_SHADOW_U] = r[0];
176 data_.dimensions[SAMPLING_SHADOW_V] = r[1];
177 data_.dimensions[SAMPLING_SHADOW_W] = r[2];
178 /* TODO de-correlate. */
179 data_.dimensions[SAMPLING_RAYTRACE_U] = r[0];
180 data_.dimensions[SAMPLING_RAYTRACE_V] = r[1];
181 data_.dimensions[SAMPLING_RAYTRACE_W] = r[2];
182 }
183 {
184 double3 r, offset = {0, 0, 0};
185 uint3 primes = {2, 3, 5};
186 BLI_halton_3d(primes, offset, sample_ + 1, r);
187 /* WORKAROUND: We offset the distribution to make the first sample (0,0,0). */
188 /* TODO de-correlate. */
189 data_.dimensions[SAMPLING_SHADOW_I] = fractf(r[0] + (1.0 / 2.0));
190 data_.dimensions[SAMPLING_SHADOW_J] = fractf(r[1] + (2.0 / 3.0));
191 data_.dimensions[SAMPLING_SHADOW_K] = fractf(r[2] + (4.0 / 5.0));
192 }
193 {
194 uint64_t sample_volume = sample_;
195 if (interactive_mode()) {
196 sample_volume = sample_volume % interactive_sample_volume_;
197 }
198 double3 r, offset = {0, 0, 0};
199 uint3 primes = {2, 3, 5};
200 BLI_halton_3d(primes, offset, sample_volume + 1, r);
201 /* WORKAROUND: We offset the distribution to make the first sample (0,0,0). */
202 data_.dimensions[SAMPLING_VOLUME_U] = fractf(r[0] + (1.0 / 2.0));
203 data_.dimensions[SAMPLING_VOLUME_V] = fractf(r[1] + (2.0 / 3.0));
204 data_.dimensions[SAMPLING_VOLUME_W] = fractf(r[2] + (4.0 / 5.0));
205 }
206 {
207 /* Using leaped Halton sequence so we can reused the same primes. */
208 double2 r, offset = {0, 0};
209 uint64_t leap = 5;
210 uint2 primes = {2, 3};
211 BLI_halton_2d(primes, offset, sample_ * leap + 1, r);
212 data_.dimensions[SAMPLING_SHADOW_X] = r[0];
213 data_.dimensions[SAMPLING_SHADOW_Y] = r[1];
214 /* TODO de-correlate. */
215 data_.dimensions[SAMPLING_SSS_U] = r[0];
216 data_.dimensions[SAMPLING_SSS_V] = r[1];
217 }
218 {
219 /* Don't leave unused data undefined. */
220 data_.dimensions[SAMPLING_UNUSED_0] = 0.0f;
221 data_.dimensions[SAMPLING_UNUSED_1] = 0.0f;
222 data_.dimensions[SAMPLING_UNUSED_2] = 0.0f;
223 }
224
226 /* These numbers are often fed to `sqrt`. Make sure their values are in the expected range. */
227 BLI_assert(data_.dimensions[i] >= 0.0f);
228 BLI_assert(data_.dimensions[i] < 1.0f);
230 }
231
232 data_.push_update();
233
234 viewport_sample_++;
235 sample_++;
236
237 reset_ = false;
238}
239
241{
242 BLI_assert(inst_.is_viewport());
243 reset_ = true;
244}
245
247{
248 BLI_assert(inst_.is_viewport());
249 return reset_;
250}
251
253
254/* -------------------------------------------------------------------- */
257
259{
261 sample.z = rand.x * 2.0f - 1.0f; /* cos theta */
262
263 float r = sqrtf(fmaxf(0.0f, 1.0f - square_f(sample.z))); /* sin theta */
264
265 float omega = rand.y * 2.0f * M_PI;
266 sample.x = r * cosf(omega);
267 sample.y = r * sinf(omega);
268
269 sample *= sqrtf(sqrtf(rand.z));
270 return sample;
271}
272
274{
275 float omega = rand.y * 2.0f * M_PI;
276 return sqrtf(rand.x) * float2(cosf(omega), sinf(omega));
277}
278
280{
281 const float omega = rand.y * 2.0f * M_PI;
282 const float cos_theta = rand.x;
283 const float sin_theta = safe_sqrtf(1.0f - square_f(cos_theta));
284 return float3(sin_theta * float2(cosf(omega), sinf(omega)), cos_theta);
285}
286
288{
289 const float omega = rand.y * 2.0f * M_PI;
290 const float cos_theta = rand.x * 2.0f - 1.0f;
291 const float sin_theta = safe_sqrtf(1.0f - square_f(cos_theta));
292 return float3(sin_theta * float2(cosf(omega), sinf(omega)), cos_theta);
293}
294
296{
297 /* Fibonacci spiral. */
298 float omega = 4.0f * M_PI * (1.0f + sqrtf(5.0f)) * rand.x;
299 float r = sqrtf(rand.x);
300 /* Random rotation. */
301 omega += rand.y * 2.0f * M_PI;
302 return r * float2(cosf(omega), sinf(omega));
303}
304
305void Sampling::dof_disk_sample_get(float *r_radius, float *r_theta) const
306{
307 if (dof_ring_count_ == 0) {
308 *r_radius = *r_theta = 0.0f;
309 return;
310 }
311
312 int s = sample_ - 1;
313 int ring = 0;
314 int ring_sample_count = 1;
315 int ring_sample = 1;
316
317 s = s * (dof_web_density_ - 1);
318 s = s % dof_sample_count_;
319
320 /* Choosing sample to we get faster convergence.
321 * The issue here is that we cannot map a low discrepancy sequence to this sampling pattern
322 * because the same sample could be chosen twice in relatively short intervals. */
323 /* For now just use an ascending sequence with an offset. This gives us relatively quick
324 * initial coverage and relatively high distance between samples. */
325 /* TODO(@fclem) We can try to order samples based on a LDS into a table to avoid duplicates.
326 * The drawback would be some memory consumption and initialize time. */
327 int samples_passed = 1;
328 while (s >= samples_passed) {
329 ring++;
330 ring_sample_count = ring * dof_web_density_;
331 ring_sample = s - samples_passed;
332 ring_sample = (ring_sample + 1) % ring_sample_count;
333 samples_passed += ring_sample_count;
334 }
335
336 *r_radius = ring / float(dof_ring_count_);
337 *r_theta = 2.0f * M_PI * ring_sample / float(ring_sample_count);
338}
339
341
342/* -------------------------------------------------------------------- */
345
347{
348 BLI_assert(cdf.size() > 1);
349 cdf[0] = 0.0f;
350 /* Actual CDF evaluation. */
351 for (int u : IndexRange(cdf.size() - 1)) {
352 float x = float(u + 1) / float(cdf.size() - 1);
353 cdf[u + 1] = cdf[u] + BKE_curvemapping_evaluateF(&curve, 0, x);
354 }
355 /* Normalize the CDF. */
356 for (int u : cdf.index_range()) {
357 cdf[u] /= cdf.last();
358 }
359 /* Just to make sure. */
360 cdf.last() = 1.0f;
361}
362
364{
365 BLI_assert(cdf.first() == 0.0f && cdf.last() == 1.0f);
366 for (int u : inverted_cdf.index_range()) {
367 float x = clamp_f(u / float(inverted_cdf.size() - 1), 1e-5f, 1.0f - 1e-5f);
368 for (int i : cdf.index_range().drop_front(1)) {
369 if (cdf[i] >= x) {
370 float t = (x - cdf[i]) / (cdf[i] - cdf[i - 1]);
371 inverted_cdf[u] = (float(i) + t) / float(cdf.size() - 1);
372 break;
373 }
374 }
375 }
376}
377
379
380} // namespace blender::eevee
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
int BKE_render_preview_pixel_size(const RenderData *r)
Definition scene.cc:2942
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE uint divide_ceil_u(uint a, uint b)
MINLINE float clamp_f(float value, float min, float max)
MINLINE int square_i(int a)
MINLINE int max_ii(int a, int b)
MINLINE float square_f(float a)
MINLINE float safe_sqrtf(float a)
#define M_PI
Random number functions.
void BLI_halton_3d(const unsigned int prime[3], double offset[3], int n, double *r)
Definition rand.cc:250
void BLI_halton_2d(const unsigned int prime[2], double offset[2], int n, double *r)
Definition rand.cc:237
#define UNUSED_VARS_NDEBUG(...)
@ SCE_EEVEE_TAA_REPROJECTION
@ SCE_EEVEE_DOF_JITTER
@ R_MBLUR
ccl_device_inline float cos_theta(const float3 w)
ccl_device_inline float sin_theta(const float3 w)
unsigned long long int uint64_t
constexpr IndexRange drop_front(int64_t n) const
int64_t size() const
const T & last(const int64_t n=0) const
IndexRange index_range() const
const T & first() const
static float3 sample_sphere(const float2 &rand)
static void cdf_invert(Vector< float > &cdf, Vector< float > &inverted_cdf)
static float2 sample_disk(const float2 &rand)
void dof_disk_sample_get(float *r_radius, float *r_theta) const
void init(const Scene *scene)
static float2 sample_spiral(const float2 &rand)
static void cdf_from_curvemapping(const CurveMapping &curve, Vector< float > &cdf)
static float3 sample_hemisphere(const float2 &rand)
static float3 sample_ball(const float3 &rand)
#define sinf(x)
#define cosf(x)
#define fmaxf(x, y)
#define sqrtf(x)
Mesh & DRW_object_get_data_for_drawing(const Object &object)
#define SAMPLING_DIMENSION_COUNT
MINLINE float fractf(float a)
static int sampling_web_ring_count_get(int web_density, int sample_count)
static int sampling_web_sample_count_get(int web_density, int in_ring_count)
T max(const T &a, const T &b)
VecBase< uint32_t, 2 > uint2
VecBase< double, 2 > double2
VecBase< uint32_t, 3 > uint3
VecBase< float, 2 > float2
VecBase< double, 3 > double3
VecBase< float, 3 > float3
float clamp_surface_direct
float clamp_volume_indirect
float clamp_surface_indirect
float clamp_volume_direct
struct RenderData r
struct SceneEEVEE eevee
i
Definition text_draw.cc:230