Blender V5.3
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 "RNA_access.hh"
12
13#include "BKE_colortools.hh"
14#include "BKE_scene.hh"
15
16#include "BLI_rand.hh"
17#include "BLI_rand_c.hh"
18#include "BLI_time.hh"
19
20#include "BLI_math_base.hh"
21#include "BLI_math_base_safe.hh"
22
23#include "eevee_instance.hh"
24#include "eevee_sampling.hh"
25
26namespace blender::eevee {
27
28/* -------------------------------------------------------------------- */
31
32void Sampling::init(const Scene *scene)
33{
34 /* Note: Cycles have different option for view layers sample overrides. The current behavior
35 * matches the default `Use`, which simply override if non-zero. */
36 uint64_t render_sample_count = (inst_.view_layer->samples > 0) ? inst_.view_layer->samples :
38
39 sample_count_ = inst_.is_viewport() ? scene->eevee.taa_samples : render_sample_count;
40 time_limit_ = scene->eevee.time_limit;
41
42 if (inst_.is_image_render) {
43 sample_count_ = math::max(uint64_t(1), sample_count_);
44 }
45
46 if (sample_count_ == 0) {
47 BLI_assert(inst_.is_viewport());
48 sample_count_ = infinite_sample_count_;
49 }
50
51 if (inst_.is_viewport()) {
52 /* We can't rely on the film module as it is initialized later. */
53 int pixel_size = BKE_render_preview_pixel_size(&inst_.scene->r);
54 if (pixel_size > 1) {
55 /* Enforce to render at least all the film pixel once. */
56 sample_count_ = max_ii(sample_count_, square_i(pixel_size));
57 }
58 }
59
60 motion_blur_steps_ = !inst_.is_viewport() && ((scene->r.mode & R_MBLUR) != 0) ?
62 1;
63 sample_count_ = divide_ceil_u(sample_count_, motion_blur_steps_);
64
65 if (scene->eevee.flag & SCE_EEVEE_DOF_JITTER) {
66 if (sample_count_ == infinite_sample_count_) {
67 /* Special case for viewport continuous rendering. We clamp to a max sample
68 * to avoid the jittered dof never converging. */
69 dof_ring_count_ = 6;
70 }
71 else {
72 dof_ring_count_ = sampling_web_ring_count_get(dof_web_density_, sample_count_);
73 }
74 dof_sample_count_ = sampling_web_sample_count_get(dof_web_density_, dof_ring_count_);
75 /* Change total sample count to fill the web pattern entirely. */
76 sample_count_ = divide_ceil_u(sample_count_, dof_sample_count_) * dof_sample_count_;
77 }
78 else {
79 dof_ring_count_ = 0;
80 dof_sample_count_ = 1;
81 }
82
83 /* Only multiply after to have full the full DoF web pattern for each time steps. */
84 sample_count_ *= motion_blur_steps_;
85
86 auto clamp_value_load = [](float value) { return (value > 0.0) ? value : unclamped_max; };
87
88 clamp_data_.sun_threshold = clamp_value_load(inst_.world.sun_threshold());
89 clamp_data_.surface_direct = clamp_value_load(scene->eevee.clamp_surface_direct);
90 clamp_data_.surface_indirect = clamp_value_load(scene->eevee.clamp_surface_indirect);
91 clamp_data_.volume_direct = clamp_value_load(scene->eevee.clamp_volume_direct);
92 clamp_data_.volume_indirect = clamp_value_load(scene->eevee.clamp_volume_indirect);
93
94 clamp_data_.direct_scale = scene->eevee.direct_light_intensity;
95 clamp_data_.indirect_scale = scene->eevee.indirect_light_intensity;
96
97 /* Options for overwriting pixel jitter sample position. */
98 PointerRNA prop_scene = RNA_id_pointer_create(const_cast<ID *>(&scene->id));
99 blender::PropertyRNA *override_pixel_jitter_sample_prop = RNA_struct_find_property(
100 &prop_scene, "[\"override_pixel_jitter_sample\"]");
101 use_custom_pixel_jitter_sample_ = false;
102 if (override_pixel_jitter_sample_prop) {
103 const int array_length = RNA_property_array_length(&prop_scene,
104 override_pixel_jitter_sample_prop);
105 if (array_length == 2) {
107 &prop_scene, override_pixel_jitter_sample_prop, &custom_pixel_jitter_sample_[0]);
108 use_custom_pixel_jitter_sample_ = true;
109 }
110 else if (array_length != 0) {
111 printf("%s: scene.custom_pixel_jitter_sample length is not 0 or 2.\n", __func__);
112 }
113 }
114
115 if (!inst_.is_viewport()) {
116 start_render_time_ = BLI_time_now_seconds();
117 }
118}
119
120void Sampling::init(const Object &probe_object)
121{
122 BLI_assert(inst_.is_baking());
124 probe_object);
125
126 sample_count_ = max_ii(1, lightprobe.grid_bake_samples);
127 sample_ = 0;
128 start_render_time_ = BLI_time_now_seconds();
129}
130
132{
133 if (reset_) {
134 viewport_sample_ = 0;
135 start_render_time_ = BLI_time_now_seconds();
136 }
137
138 if (inst_.is_viewport()) {
139
140 interactive_mode_ = viewport_sample_ < interactive_mode_threshold;
141
142 bool interactive_mode_disabled = (inst_.scene->eevee.flag & SCE_EEVEE_TAA_REPROJECTION) == 0 ||
143 inst_.is_viewport_image_render;
144 if (interactive_mode_disabled) {
145 interactive_mode_ = false;
146 sample_ = viewport_sample_;
147 }
148 else if (interactive_mode_) {
149 int interactive_sample_count = interactive_sample_max_;
150
151 if (viewport_sample_ < interactive_sample_count) {
152 /* Loop over the same starting samples. */
153 sample_ = sample_ % interactive_sample_count;
154 }
155 else {
156 /* Break out of the loop and resume normal pattern. */
157 sample_ = interactive_sample_count;
158 }
159 }
160 }
161}
162
164{
165 if (time_limit_ > 0.0f) {
166 current_time_ = BLI_time_now_seconds();
167 }
168}
169
170bool Sampling::check_time_limit_reached() const
171{
172 if (time_limit_ > 0.0f && sample_ > 0 && viewport_sample_ > 0) {
173 if (current_time_ - start_render_time_ >= time_limit_) {
174 return true;
175 }
176 }
177 return false;
178}
179
181{
182 {
183 /* Repeat the sequence for all pixels that are being up-scaled. */
184 uint64_t sample_filter = sample_ / square_i(inst_.film.scaling_factor_get());
185 if (interactive_mode()) {
186 sample_filter = sample_filter % interactive_sample_aa_;
187 }
188 /* TODO(fclem) we could use some persistent states to speedup the computation. */
189 double2 r, offset = {0, 0};
190 if (use_custom_pixel_jitter_sample_) {
191 r[0] = custom_pixel_jitter_sample_[0];
192 r[1] = custom_pixel_jitter_sample_[1];
193 data_.dimensions[SAMPLING_FILTER_U] = fractf(r[0] + 0.5f);
194 data_.dimensions[SAMPLING_FILTER_V] = fractf(r[1] + 0.5f);
195 }
196 else {
197 /* Using 2,3 primes as per UE4 Temporal AA presentation.
198 * http://advances.realtimerendering.com/s2014/epic/TemporalAA.pptx (slide 14) */
199 uint2 primes = {2, 3};
200 BLI_halton_2d(primes, offset, sample_filter + 1, r);
201 /* WORKAROUND: We offset the distribution to make the first sample (0,0). This way, we are
202 * assured that at least one of the samples inside the TAA rotation will match the one from
203 * the draw manager. This makes sure overlays are correctly composited in static scene. */
204 data_.dimensions[SAMPLING_FILTER_U] = fractf(r[0] + (1.0f / 2.0f));
205 data_.dimensions[SAMPLING_FILTER_V] = fractf(r[1] + (2.0f / 3.0f));
206 }
207 /* TODO de-correlate. */
208 data_.dimensions[SAMPLING_TIME] = r[0];
209 data_.dimensions[SAMPLING_CLOSURE] = r[1];
210 data_.dimensions[SAMPLING_RAYTRACE_X] = r[0];
211 }
212 {
213 double3 r, offset = {0, 0, 0};
214 uint3 primes = {5, 7, 3};
215 BLI_halton_3d(primes, offset, sample_ + 1, r);
216 data_.dimensions[SAMPLING_LENS_U] = r[0];
217 data_.dimensions[SAMPLING_LENS_V] = r[1];
218 /* TODO de-correlate. */
219 data_.dimensions[SAMPLING_LIGHTPROBE] = r[0];
220 data_.dimensions[SAMPLING_TRANSPARENCY] = r[1];
221 /* TODO de-correlate. */
222 data_.dimensions[SAMPLING_AO_U] = r[0];
223 data_.dimensions[SAMPLING_AO_V] = r[1];
224 data_.dimensions[SAMPLING_AO_W] = r[2];
225 /* TODO de-correlate. */
226 data_.dimensions[SAMPLING_CURVES_U] = r[0];
227 }
228 {
229 uint64_t sample_raytrace = sample_;
230 if (interactive_mode()) {
231 sample_raytrace = sample_raytrace % interactive_sample_raytrace_;
232 }
233 /* Using leaped Halton sequence so we can reused the same primes as lens. */
234 double3 r, offset = {0, 0, 0};
235 uint64_t leap = 13;
236 uint3 primes = {5, 7, 11};
237 BLI_halton_3d(primes, offset, sample_raytrace * leap + 1, r);
238 data_.dimensions[SAMPLING_SHADOW_U] = r[0];
239 data_.dimensions[SAMPLING_SHADOW_V] = r[1];
240 data_.dimensions[SAMPLING_SHADOW_W] = r[2];
241 /* TODO de-correlate. */
242 data_.dimensions[SAMPLING_RAYTRACE_U] = r[0];
243 data_.dimensions[SAMPLING_RAYTRACE_V] = r[1];
244 data_.dimensions[SAMPLING_RAYTRACE_W] = r[2];
245 }
246 {
247 double3 r, offset = {0, 0, 0};
248 uint3 primes = {2, 3, 5};
249 BLI_halton_3d(primes, offset, sample_ + 1, r);
250 /* WORKAROUND: We offset the distribution to make the first sample (0,0,0). */
251 /* TODO de-correlate. */
252 data_.dimensions[SAMPLING_SHADOW_I] = fractf(r[0] + (1.0 / 2.0));
253 data_.dimensions[SAMPLING_SHADOW_J] = fractf(r[1] + (2.0 / 3.0));
254 data_.dimensions[SAMPLING_SHADOW_K] = fractf(r[2] + (4.0 / 5.0));
255 }
256 {
257 uint64_t sample_volume = sample_;
258 if (interactive_mode()) {
259 sample_volume = sample_volume % interactive_sample_volume_;
260 }
261 double3 r, offset = {0, 0, 0};
262 uint3 primes = {2, 3, 5};
263 BLI_halton_3d(primes, offset, sample_volume + 1, r);
264 /* WORKAROUND: We offset the distribution to make the first sample (0,0,0). */
265 data_.dimensions[SAMPLING_VOLUME_U] = fractf(r[0] + (1.0 / 2.0));
266 data_.dimensions[SAMPLING_VOLUME_V] = fractf(r[1] + (2.0 / 3.0));
267 data_.dimensions[SAMPLING_VOLUME_W] = fractf(r[2] + (4.0 / 5.0));
268 }
269 {
270 /* Using leaped Halton sequence so we can reused the same primes. */
271 double2 r, offset = {0, 0};
272 uint64_t leap = 5;
273 uint2 primes = {2, 3};
274 BLI_halton_2d(primes, offset, sample_ * leap + 1, r);
275 data_.dimensions[SAMPLING_SHADOW_X] = r[0];
276 data_.dimensions[SAMPLING_SHADOW_Y] = r[1];
277 /* TODO de-correlate. */
278 data_.dimensions[SAMPLING_SSS_U] = r[0];
279 data_.dimensions[SAMPLING_SSS_V] = r[1];
280 }
281 {
282 /* Separate sequence for G-buffer quantization dithering. */
283 double3 r, offset = {0, 0, 0};
284 uint3 primes = {11, 13, 17};
285 BLI_halton_3d(primes, offset, sample_ + 1, r);
286 data_.dimensions[SAMPLING_GBUFFER_U] = r[0];
287 data_.dimensions[SAMPLING_GBUFFER_V] = r[1];
288 data_.dimensions[SAMPLING_GBUFFER_W] = r[2];
289 }
290 {
291 /* Separate sequence for film accumulation buffer quantization dithering (see #129533). */
292 double x, offset = 0;
293 BLI_halton_1d(19, offset, sample_ + 1, &x);
294 data_.dimensions[SAMPLING_FILM_U] = float(x);
295 }
296 {
297 /* Separate sequence for panoramic film sample jittering. */
298 double2 r, offset = {0, 0};
299 uint64_t leap = 4;
300 uint2 primes = {5, 7};
301 BLI_halton_2d(primes, offset, sample_ * leap + 1, r);
302 data_.dimensions[SAMPLING_PANORAMIC_U] = r[0];
303 data_.dimensions[SAMPLING_PANORAMIC_V] = r[1];
304 }
305
306 for (int i : IndexRange(SAMPLING_DIMENSION_COUNT)) {
307 /* These numbers are often fed to `sqrt`. Make sure their values are in the expected range. */
308 BLI_assert(data_.dimensions[i] >= 0.0f);
309 BLI_assert(data_.dimensions[i] < 1.0f);
311 }
312
313 data_.push_update();
314
315 viewport_sample_++;
316 sample_++;
317
318 reset_ = false;
319}
320
322{
323 BLI_assert(inst_.is_viewport());
324 reset_ = true;
325}
326
328{
329 BLI_assert(inst_.is_viewport());
330 return reset_;
331}
332
334
335/* -------------------------------------------------------------------- */
338
340{
342 sample.z = rand.x * 2.0f - 1.0f; /* cos theta */
343
344 float r = sqrtf(fmaxf(0.0f, 1.0f - square_f(sample.z))); /* sin theta */
345
346 float omega = rand.y * 2.0f * M_PI;
347 sample.x = r * cosf(omega);
348 sample.y = r * sinf(omega);
349
350 sample *= sqrtf(sqrtf(rand.z));
351 return sample;
352}
353
355{
356 float omega = rand.y * 2.0f * M_PI;
357 return sqrtf(rand.x) * float2(cosf(omega), sinf(omega));
358}
359
361{
362 const float omega = rand.y * 2.0f * M_PI;
363 const float cos_theta = rand.x;
364 const float sin_theta = safe_sqrtf(1.0f - square_f(cos_theta));
365 return float3(sin_theta * float2(cosf(omega), sinf(omega)), cos_theta);
366}
367
369{
370 const float omega = rand.y * 2.0f * M_PI;
371 const float cos_theta = rand.x * 2.0f - 1.0f;
372 const float sin_theta = safe_sqrtf(1.0f - square_f(cos_theta));
373 return float3(sin_theta * float2(cosf(omega), sinf(omega)), cos_theta);
374}
375
377{
378 /* Fibonacci spiral. */
379 float omega = 4.0f * M_PI * (1.0f + sqrtf(5.0f)) * rand.x;
380 float r = sqrtf(rand.x);
381 /* Random rotation. */
382 omega += rand.y * 2.0f * M_PI;
383 return r * float2(cosf(omega), sinf(omega));
384}
385
386void Sampling::dof_disk_sample_get(float *r_radius, float *r_theta) const
387{
388 if (dof_ring_count_ == 0) {
389 *r_radius = *r_theta = 0.0f;
390 return;
391 }
392
393 int s = sample_ - 1;
394 int ring = 0;
395 int ring_sample_count = 1;
396 int ring_sample = 1;
397
398 s = s * (dof_web_density_ - 1);
399 s = s % dof_sample_count_;
400
401 /* Choosing sample to we get faster convergence.
402 * The issue here is that we cannot map a low discrepancy sequence to this sampling pattern
403 * because the same sample could be chosen twice in relatively short intervals. */
404 /* For now just use an ascending sequence with an offset. This gives us relatively quick
405 * initial coverage and relatively high distance between samples. */
406 /* TODO(@fclem) We can try to order samples based on a LDS into a table to avoid duplicates.
407 * The drawback would be some memory consumption and initialize time. */
408 int samples_passed = 1;
409 while (s >= samples_passed) {
410 ring++;
411 ring_sample_count = ring * dof_web_density_;
412 ring_sample = s - samples_passed;
413 ring_sample = (ring_sample + 1) % ring_sample_count;
414 samples_passed += ring_sample_count;
415 }
416
417 *r_radius = ring / float(dof_ring_count_);
418 *r_theta = 2.0f * M_PI * ring_sample / float(ring_sample_count);
419}
420
422
423/* -------------------------------------------------------------------- */
426
428{
429 BLI_assert(cdf.size() > 1);
430 cdf[0] = 0.0f;
431 /* Actual CDF evaluation. */
432 for (int u : IndexRange(cdf.size() - 1)) {
433 float x = float(u + 1) / float(cdf.size() - 1);
434 cdf[u + 1] = cdf[u] + BKE_curvemapping_evaluateF(&curve, 0, x);
435 }
436 /* Normalize the CDF. */
437 for (int u : cdf.index_range()) {
438 cdf[u] /= cdf.last();
439 }
440 /* Just to make sure. */
441 cdf.last() = 1.0f;
442}
443
445{
446 BLI_assert(cdf.first() == 0.0f && cdf.last() == 1.0f);
447 for (int u : inverted_cdf.index_range()) {
448 float x = clamp_f(u / float(inverted_cdf.size() - 1), 1e-5f, 1.0f - 1e-5f);
449 for (int i : cdf.index_range().drop_front(1)) {
450 if (cdf[i] >= x) {
451 float t = (x - cdf[i]) / (cdf[i] - cdf[i - 1]);
452 inverted_cdf[u] = (float(i) + t) / float(cdf.size() - 1);
453 break;
454 }
455 }
456 }
457}
458
460
461} // namespace blender::eevee
#define M_PI
Random number functions.
Platform independent time functions.
#define UNUSED_VARS_NDEBUG(...)
ccl_device_inline float cos_theta(const float3 w)
ccl_device_inline float sin_theta(const float3 w)
constexpr IndexRange()=default
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)
T max(const T &a, const T &b)
VecBase< uint32_t, 2 > uint2
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int BKE_render_preview_pixel_size(const RenderData *r)
Definition scene.cc:3183
unsigned __int64 uint64_t
Definition BKE_text.h:91
void BLI_halton_2d(const unsigned int prime[2], double offset[2], int n, double *r)
Definition rand.cc:239
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
MINLINE float safe_sqrtf(float a)
MINLINE int square_i(int a)
PointerRNA RNA_id_pointer_create(ID *id)
VecBase< double, 2 > double2
MINLINE float fractf(float a)
MINLINE int max_ii(int a, int b)
VecBase< uint32_t, 3 > uint3
@ SCE_EEVEE_TAA_REPROJECTION
void BLI_halton_1d(unsigned int prime, double offset, int n, double *r)
Definition rand.cc:228
VecBase< float, 2 > float2
T & DRW_object_get_data_for_drawing(const Object &object)
void BLI_halton_3d(const unsigned int prime[3], double offset[3], int n, double *r)
Definition rand.cc:252
VecBase< double, 3 > double3
MINLINE uint divide_ceil_u(uint a, uint b)
MINLINE float square_f(float a)
double BLI_time_now_seconds()
Definition time.cc:30
VecBase< float, 3 > float3
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
MINLINE float clamp_f(float value, float min, float max)
#define sqrtf
#define fmaxf
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eSceneEEVEE_Flag flag
struct RenderData r
struct SceneEEVEE eevee