Blender  V3.3
integrator.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #include "device/device.h"
5 
6 #include "scene/background.h"
7 #include "scene/bake.h"
8 #include "scene/camera.h"
9 #include "scene/film.h"
10 #include "scene/integrator.h"
11 #include "scene/jitter.h"
12 #include "scene/light.h"
13 #include "scene/object.h"
14 #include "scene/scene.h"
15 #include "scene/shader.h"
16 #include "scene/sobol.h"
17 #include "scene/stats.h"
18 
19 #include "kernel/types.h"
20 
21 #include "util/foreach.h"
22 #include "util/hash.h"
23 #include "util/log.h"
24 #include "util/task.h"
25 #include "util/time.h"
26 
28 
30 {
31  NodeType *type = NodeType::add("integrator", create);
32 
33  SOCKET_INT(min_bounce, "Min Bounce", 0);
34  SOCKET_INT(max_bounce, "Max Bounce", 7);
35 
36  SOCKET_INT(max_diffuse_bounce, "Max Diffuse Bounce", 7);
37  SOCKET_INT(max_glossy_bounce, "Max Glossy Bounce", 7);
38  SOCKET_INT(max_transmission_bounce, "Max Transmission Bounce", 7);
39  SOCKET_INT(max_volume_bounce, "Max Volume Bounce", 7);
40 
41  SOCKET_INT(transparent_min_bounce, "Transparent Min Bounce", 0);
42  SOCKET_INT(transparent_max_bounce, "Transparent Max Bounce", 7);
43 
44 #ifdef WITH_CYCLES_DEBUG
45  static NodeEnum direct_light_sampling_type_enum;
46  direct_light_sampling_type_enum.insert("multiple_importance_sampling",
48  direct_light_sampling_type_enum.insert("forward_path_tracing", DIRECT_LIGHT_SAMPLING_FORWARD);
49  direct_light_sampling_type_enum.insert("next_event_estimation", DIRECT_LIGHT_SAMPLING_NEE);
50  SOCKET_ENUM(direct_light_sampling_type,
51  "Direct Light Sampling Type",
52  direct_light_sampling_type_enum,
54 #endif
55 
56  SOCKET_INT(ao_bounces, "AO Bounces", 0);
57  SOCKET_FLOAT(ao_factor, "AO Factor", 0.0f);
58  SOCKET_FLOAT(ao_distance, "AO Distance", FLT_MAX);
59  SOCKET_FLOAT(ao_additive_factor, "AO Additive Factor", 0.0f);
60 
61  SOCKET_INT(volume_max_steps, "Volume Max Steps", 1024);
62  SOCKET_FLOAT(volume_step_rate, "Volume Step Rate", 1.0f);
63 
64  SOCKET_BOOLEAN(caustics_reflective, "Reflective Caustics", true);
65  SOCKET_BOOLEAN(caustics_refractive, "Refractive Caustics", true);
66  SOCKET_FLOAT(filter_glossy, "Filter Glossy", 0.0f);
67 
68  SOCKET_BOOLEAN(use_direct_light, "Use Direct Light", true);
69  SOCKET_BOOLEAN(use_indirect_light, "Use Indirect Light", true);
70  SOCKET_BOOLEAN(use_diffuse, "Use Diffuse", true);
71  SOCKET_BOOLEAN(use_glossy, "Use Glossy", true);
72  SOCKET_BOOLEAN(use_transmission, "Use Transmission", true);
73  SOCKET_BOOLEAN(use_emission, "Use Emission", true);
74 
75  SOCKET_INT(seed, "Seed", 0);
76  SOCKET_FLOAT(sample_clamp_direct, "Sample Clamp Direct", 0.0f);
77  SOCKET_FLOAT(sample_clamp_indirect, "Sample Clamp Indirect", 10.0f);
78  SOCKET_BOOLEAN(motion_blur, "Motion Blur", false);
79 
80  SOCKET_INT(aa_samples, "AA Samples", 0);
81  SOCKET_INT(start_sample, "Start Sample", 0);
82 
83  SOCKET_BOOLEAN(use_adaptive_sampling, "Use Adaptive Sampling", true);
84  SOCKET_FLOAT(adaptive_threshold, "Adaptive Threshold", 0.01f);
85  SOCKET_INT(adaptive_min_samples, "Adaptive Min Samples", 0);
86 
87  SOCKET_FLOAT(light_sampling_threshold, "Light Sampling Threshold", 0.01f);
88 
89  static NodeEnum sampling_pattern_enum;
90  sampling_pattern_enum.insert("sobol", SAMPLING_PATTERN_SOBOL);
91  sampling_pattern_enum.insert("pmj", SAMPLING_PATTERN_PMJ);
92  SOCKET_ENUM(sampling_pattern, "Sampling Pattern", sampling_pattern_enum, SAMPLING_PATTERN_SOBOL);
93  SOCKET_FLOAT(scrambling_distance, "Scrambling Distance", 1.0f);
94 
95  static NodeEnum denoiser_type_enum;
96  denoiser_type_enum.insert("optix", DENOISER_OPTIX);
97  denoiser_type_enum.insert("openimagedenoise", DENOISER_OPENIMAGEDENOISE);
98 
99  static NodeEnum denoiser_prefilter_enum;
100  denoiser_prefilter_enum.insert("none", DENOISER_PREFILTER_NONE);
101  denoiser_prefilter_enum.insert("fast", DENOISER_PREFILTER_FAST);
102  denoiser_prefilter_enum.insert("accurate", DENOISER_PREFILTER_ACCURATE);
103 
104  /* Default to accurate denoising with OpenImageDenoise. For interactive viewport
105  * it's best use OptiX and disable the normal pass since it does not always have
106  * the desired effect for that denoiser. */
107  SOCKET_BOOLEAN(use_denoise, "Use Denoiser", false);
108  SOCKET_ENUM(denoiser_type, "Denoiser Type", denoiser_type_enum, DENOISER_OPENIMAGEDENOISE);
109  SOCKET_INT(denoise_start_sample, "Start Sample to Denoise", 0);
110  SOCKET_BOOLEAN(use_denoise_pass_albedo, "Use Albedo Pass for Denoiser", true);
111  SOCKET_BOOLEAN(use_denoise_pass_normal, "Use Normal Pass for Denoiser", true);
112  SOCKET_ENUM(denoiser_prefilter,
113  "Denoiser Prefilter",
114  denoiser_prefilter_enum,
116 
117  return type;
118 }
119 
120 Integrator::Integrator() : Node(get_node_type())
121 {
122 }
123 
125 {
126 }
127 
129 {
130  if (!is_modified())
131  return;
132 
133  scoped_callback_timer timer([scene](double time) {
134  if (scene->update_stats) {
135  scene->update_stats->integrator.times.add_entry({"device_update", time});
136  }
137  });
138 
139  KernelIntegrator *kintegrator = &dscene->data.integrator;
140 
141  /* Adaptive sampling requires PMJ samples.
142  *
143  * This also makes detection of sampling pattern a bit more involved: can not rely on the changed
144  * state of socket, since its value might be different from the effective value used here. So
145  * instead compare with previous value in the KernelIntegrator. Only do it if the device was
146  * updated once (in which case the `sample_pattern_lut` will be allocated to a non-zero size). */
147  const SamplingPattern new_sampling_pattern = (use_adaptive_sampling) ? SAMPLING_PATTERN_PMJ :
148  sampling_pattern;
149 
150  const bool need_update_lut = max_bounce_is_modified() || max_transmission_bounce_is_modified() ||
151  dscene->sample_pattern_lut.size() == 0 ||
152  kintegrator->sampling_pattern != new_sampling_pattern;
153 
154  if (need_update_lut) {
155  dscene->sample_pattern_lut.tag_realloc();
156  }
157 
158  device_free(device, dscene);
159 
160  /* integrator parameters */
161  kintegrator->min_bounce = min_bounce + 1;
162  kintegrator->max_bounce = max_bounce + 1;
163 
164  kintegrator->max_diffuse_bounce = max_diffuse_bounce + 1;
165  kintegrator->max_glossy_bounce = max_glossy_bounce + 1;
166  kintegrator->max_transmission_bounce = max_transmission_bounce + 1;
167  kintegrator->max_volume_bounce = max_volume_bounce + 1;
168 
169  kintegrator->transparent_min_bounce = transparent_min_bounce + 1;
170  kintegrator->transparent_max_bounce = transparent_max_bounce + 1;
171 
172  kintegrator->ao_bounces = (ao_factor != 0.0f) ? ao_bounces : 0;
173  kintegrator->ao_bounces_distance = ao_distance;
174  kintegrator->ao_bounces_factor = ao_factor;
175  kintegrator->ao_additive_factor = ao_additive_factor;
176 
177 #ifdef WITH_CYCLES_DEBUG
178  kintegrator->direct_light_sampling_type = direct_light_sampling_type;
179 #else
180  kintegrator->direct_light_sampling_type = DIRECT_LIGHT_SAMPLING_MIS;
181 #endif
182 
183  /* Transparent Shadows
184  * We only need to enable transparent shadows, if we actually have
185  * transparent shaders in the scene. Otherwise we can disable it
186  * to improve performance a bit. */
187  kintegrator->transparent_shadows = false;
188  foreach (Shader *shader, scene->shaders) {
189  /* keep this in sync with SD_HAS_TRANSPARENT_SHADOW in shader.cpp */
190  if ((shader->has_surface_transparent && shader->get_use_transparent_shadow()) ||
191  shader->has_volume) {
192  kintegrator->transparent_shadows = true;
193  break;
194  }
195  }
196 
197  kintegrator->volume_max_steps = volume_max_steps;
198  kintegrator->volume_step_rate = volume_step_rate;
199 
200  kintegrator->caustics_reflective = caustics_reflective;
201  kintegrator->caustics_refractive = caustics_refractive;
202  kintegrator->filter_glossy = (filter_glossy == 0.0f) ? FLT_MAX : 1.0f / filter_glossy;
203 
204  kintegrator->filter_closures = 0;
205  if (!use_direct_light) {
206  kintegrator->filter_closures |= FILTER_CLOSURE_DIRECT_LIGHT;
207  }
208  if (!use_indirect_light) {
209  kintegrator->min_bounce = 1;
210  kintegrator->max_bounce = 1;
211  }
212  if (!use_diffuse) {
213  kintegrator->filter_closures |= FILTER_CLOSURE_DIFFUSE;
214  }
215  if (!use_glossy) {
216  kintegrator->filter_closures |= FILTER_CLOSURE_GLOSSY;
217  }
218  if (!use_transmission) {
219  kintegrator->filter_closures |= FILTER_CLOSURE_TRANSMISSION;
220  }
221  if (!use_emission) {
222  kintegrator->filter_closures |= FILTER_CLOSURE_EMISSION;
223  }
224  if (scene->bake_manager->get_baking()) {
225  /* Baking does not need to trace through transparency, we only want to bake
226  * the object itself. */
227  kintegrator->filter_closures |= FILTER_CLOSURE_TRANSPARENT;
228  }
229 
230  kintegrator->seed = seed;
231 
232  kintegrator->sample_clamp_direct = (sample_clamp_direct == 0.0f) ? FLT_MAX :
233  sample_clamp_direct * 3.0f;
234  kintegrator->sample_clamp_indirect = (sample_clamp_indirect == 0.0f) ?
235  FLT_MAX :
236  sample_clamp_indirect * 3.0f;
237 
238  kintegrator->sampling_pattern = new_sampling_pattern;
239  kintegrator->scrambling_distance = scrambling_distance;
240 
241  if (light_sampling_threshold > 0.0f) {
242  kintegrator->light_inv_rr_threshold = 1.0f / light_sampling_threshold;
243  }
244  else {
245  kintegrator->light_inv_rr_threshold = 0.0f;
246  }
247 
248  /* sobol directions table */
249  int max_samples = max_bounce + transparent_max_bounce + 3 + VOLUME_BOUNDS_MAX +
251 
252  int dimensions = PRNG_BASE_NUM + max_samples * PRNG_BOUNCE_NUM;
253  dimensions = min(dimensions, SOBOL_MAX_DIMENSIONS);
254 
255  if (need_update_lut) {
256  if (kintegrator->sampling_pattern == SAMPLING_PATTERN_SOBOL) {
257  uint *directions = (uint *)dscene->sample_pattern_lut.alloc(SOBOL_BITS * dimensions);
258 
259  sobol_generate_direction_vectors((uint(*)[SOBOL_BITS])directions, dimensions);
260 
261  dscene->sample_pattern_lut.copy_to_device();
262  }
263  else {
264  constexpr int sequence_size = NUM_PMJ_SAMPLES;
265  constexpr int num_sequences = NUM_PMJ_PATTERNS;
266  float2 *directions = (float2 *)dscene->sample_pattern_lut.alloc(sequence_size *
267  num_sequences * 2);
268  TaskPool pool;
269  for (int j = 0; j < num_sequences; ++j) {
270  float2 *sequence = directions + j * sequence_size;
271  pool.push(
272  function_bind(&progressive_multi_jitter_02_generate_2D, sequence, sequence_size, j));
273  }
274  pool.wait_work();
275 
276  dscene->sample_pattern_lut.copy_to_device();
277  }
278  }
279 
280  kintegrator->has_shadow_catcher = scene->has_shadow_catcher();
281 
282  dscene->sample_pattern_lut.clear_modified();
283  clear_modified();
284 }
285 
286 void Integrator::device_free(Device *, DeviceScene *dscene, bool force_free)
287 {
288  dscene->sample_pattern_lut.free_if_need_realloc(force_free);
289 }
290 
292 {
293  if (flag & UPDATE_ALL) {
294  tag_modified();
295  }
296 
297  if (flag & AO_PASS_MODIFIED) {
298  /* tag only the ao_bounces socket as modified so we avoid updating sample_pattern_lut
299  * unnecessarily */
300  tag_ao_bounces_modified();
301  }
302 
303  if (filter_glossy_is_modified()) {
304  foreach (Shader *shader, scene->shaders) {
305  if (shader->has_integrator_dependency) {
307  break;
308  }
309  }
310  }
311 
312  if (motion_blur_is_modified()) {
315  }
316 }
317 
319 {
320  uint kernel_features = 0;
321 
322  if (ao_additive_factor != 0.0f) {
323  kernel_features |= KERNEL_FEATURE_AO_ADDITIVE;
324  }
325 
326  return kernel_features;
327 }
328 
330 {
331  AdaptiveSampling adaptive_sampling;
332 
333  adaptive_sampling.use = use_adaptive_sampling;
334 
335  if (!adaptive_sampling.use) {
336  return adaptive_sampling;
337  }
338 
339  if (aa_samples > 0 && adaptive_threshold == 0.0f) {
340  adaptive_sampling.threshold = max(0.001f, 1.0f / (float)aa_samples);
341  VLOG_INFO << "Cycles adaptive sampling: automatic threshold = " << adaptive_sampling.threshold;
342  }
343  else {
344  adaptive_sampling.threshold = adaptive_threshold;
345  }
346 
347  if (adaptive_sampling.threshold > 0 && adaptive_min_samples == 0) {
348  /* Threshold 0.1 -> 32, 0.01 -> 64, 0.001 -> 128.
349  * This is highly scene dependent, we make a guess that seemed to work well
350  * in various test scenes. */
351  const int min_samples = (int)ceilf(16.0f / powf(adaptive_sampling.threshold, 0.3f));
352  adaptive_sampling.min_samples = max(4, min_samples);
353  VLOG_INFO << "Cycles adaptive sampling: automatic min samples = "
354  << adaptive_sampling.min_samples;
355  }
356  else {
357  adaptive_sampling.min_samples = max(4, adaptive_min_samples);
358  }
359 
360  /* Arbitrary factor that makes the threshold more similar to what is was before,
361  * and gives arguably more intuitive values. */
362  adaptive_sampling.threshold *= 5.0f;
363 
364  adaptive_sampling.adaptive_step = 16;
365 
366  DCHECK(is_power_of_two(adaptive_sampling.adaptive_step))
367  << "Adaptive step must be a power of two for bitwise operations to work";
368 
369  return adaptive_sampling;
370 }
371 
373 {
374  DenoiseParams denoise_params;
375 
376  denoise_params.use = use_denoise;
377 
378  denoise_params.type = denoiser_type;
379 
380  denoise_params.start_sample = denoise_start_sample;
381 
382  denoise_params.use_pass_albedo = use_denoise_pass_albedo;
383  denoise_params.use_pass_normal = use_denoise_pass_normal;
384 
385  denoise_params.prefilter = denoiser_prefilter;
386 
387  return denoise_params;
388 }
389 
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
static unsigned long seed
Definition: btSoftBody.h:39
bool get_baking() const
Definition: bake.cpp:25
DenoiserType type
Definition: denoise.h:53
int start_sample
Definition: denoise.h:56
DenoiserPrefilter prefilter
Definition: denoise.h:65
NODE_DECLARE bool use
Definition: denoise.h:50
bool use_pass_normal
Definition: denoise.h:60
bool use_pass_albedo
Definition: denoise.h:59
device_vector< float > sample_pattern_lut
Definition: scene.h:125
void tag_update(Scene *scene, uint32_t flag)
Definition: integrator.cpp:291
void device_update(Device *device, DeviceScene *dscene, Scene *scene)
Definition: integrator.cpp:128
DenoiseParams get_denoise_params() const
Definition: integrator.cpp:372
@ AO_PASS_MODIFIED
Definition: integrator.h:87
AdaptiveSampling get_adaptive_sampling() const
Definition: integrator.cpp:329
void device_free(Device *device, DeviceScene *dscene, bool force_free=false)
Definition: integrator.cpp:286
uint get_kernel_features() const
Definition: integrator.cpp:318
void tag_update(Scene *scene, uint32_t flag)
void tag_update(Scene *scene, uint32_t flag)
bool has_volume
Definition: scene/shader.h:107
bool has_integrator_dependency
Definition: scene/shader.h:115
bool has_surface_transparent
Definition: scene/shader.h:105
void free_if_need_realloc(bool force_free)
#define powf(x, y)
Definition: cuda/compat.h:103
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
double time
Scene scene
@ DENOISER_PREFILTER_FAST
Definition: denoise.h:33
@ DENOISER_PREFILTER_NONE
Definition: denoise.h:29
@ DENOISER_PREFILTER_ACCURATE
Definition: denoise.h:37
@ DENOISER_OPTIX
Definition: denoise.h:13
@ DENOISER_OPENIMAGEDENOISE
Definition: denoise.h:14
#define function_bind
CCL_NAMESPACE_BEGIN NODE_DEFINE(Integrator)
Definition: integrator.cpp:29
void progressive_multi_jitter_02_generate_2D(float2 points[], int size, int rng_seed)
Definition: jitter.cpp:262
@ FILTER_CLOSURE_EMISSION
Definition: kernel/types.h:424
@ FILTER_CLOSURE_GLOSSY
Definition: kernel/types.h:426
@ FILTER_CLOSURE_DIFFUSE
Definition: kernel/types.h:425
@ FILTER_CLOSURE_TRANSPARENT
Definition: kernel/types.h:428
@ FILTER_CLOSURE_DIRECT_LIGHT
Definition: kernel/types.h:429
@ FILTER_CLOSURE_TRANSMISSION
Definition: kernel/types.h:427
#define NUM_PMJ_SAMPLES
#define VOLUME_BOUNDS_MAX
Definition: kernel/types.h:35
@ DIRECT_LIGHT_SAMPLING_MIS
Definition: kernel/types.h:495
@ DIRECT_LIGHT_SAMPLING_NEE
Definition: kernel/types.h:497
@ DIRECT_LIGHT_SAMPLING_FORWARD
Definition: kernel/types.h:496
@ PRNG_BOUNCE_NUM
Definition: kernel/types.h:172
@ PRNG_BASE_NUM
Definition: kernel/types.h:162
@ KERNEL_FEATURE_AO_ADDITIVE
#define BSSRDF_MAX_BOUNCES
Definition: kernel/types.h:32
SamplingPattern
Definition: kernel/types.h:178
@ SAMPLING_PATTERN_PMJ
Definition: kernel/types.h:180
@ SAMPLING_PATTERN_SOBOL
Definition: kernel/types.h:179
#define NUM_PMJ_PATTERNS
#define BSSRDF_MAX_HITS
Definition: kernel/types.h:31
#define VLOG_INFO
Definition: log.h:77
#define DCHECK(expression)
Definition: log.h:55
#define ceilf(x)
Definition: metal/compat.h:225
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRefNull prop_name, int32_t value)
Allocate a new IDProperty of type IDP_INT, set its name and value.
#define SOCKET_FLOAT(name, ui_name, default_value,...)
Definition: node_type.h:191
#define SOCKET_INT(name, ui_name, default_value,...)
Definition: node_type.h:187
#define SOCKET_BOOLEAN(name, ui_name, default_value,...)
Definition: node_type.h:185
#define SOCKET_ENUM(name, ui_name, values, default_value,...)
Definition: node_type.h:207
CCL_NAMESPACE_BEGIN void sobol_generate_direction_vectors(uint vectors[][SOBOL_BITS], int dimensions)
Definition: sobol.cpp:31
#define SOBOL_BITS
Definition: sobol.h:11
#define SOBOL_MAX_DIMENSIONS
Definition: sobol.h:12
#define min(a, b)
Definition: sort.c:35
unsigned int uint32_t
Definition: stdint.h:80
void insert(const char *x, int y)
Definition: node_enum.h:20
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=NULL)
void tag_modified()
Definition: graph/node.cpp:809
bool is_modified() const
Definition: graph/node.cpp:804
BakeManager * bake_manager
Definition: scene.h:228
vector< Shader * > shaders
Definition: scene.h:215
ObjectManager * object_manager
Definition: scene.h:226
bool has_shadow_catcher()
Definition: scene.cpp:737
ShaderManager * shader_manager
Definition: scene.h:224
struct Object * camera
SceneUpdateStats * update_stats
Definition: scene.h:249
void push(TaskRunFunction &&task)
Definition: task.cpp:23
void wait_work(Summary *stats=NULL)
Definition: task.cpp:29
float max
ccl_device_inline bool is_power_of_two(size_t x)
Definition: util/types.h:66