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