Blender V5.3
film.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/film.h"
6#include "device/device.h"
7#include "scene/background.h"
8#include "scene/bake.h"
9#include "scene/camera.h"
10#include "scene/integrator.h"
11#include "scene/mesh.h"
12#include "scene/object.h"
13#include "scene/scene.h"
14#include "scene/stats.h"
15#include "scene/tables.h"
16
17#include "util/log.h"
18#include "util/math.h"
19#include "util/math_cdf.h"
20#include "util/time.h"
21
23
24/* Pixel Filter */
25
26static float filter_func_box(float /*v*/, float /*width*/)
27{
28 return 1.0f;
29}
30
31static float filter_func_gaussian(float v, const float width)
32{
33 v *= 6.0f / width;
34 return expf(-2.0f * v * v);
35}
36
37static float filter_func_blackman_harris(float v, const float width)
38{
39 v = M_2PI_F * (v / width + 0.5f);
40 return 0.35875f - 0.48829f * cosf(v) + 0.14128f * cosf(2.0f * v) - 0.01168f * cosf(3.0f * v);
41}
42
43static vector<float> filter_table(FilterType type, float width)
44{
46 float (*filter_func)(float, float) = nullptr;
47
48 switch (type) {
49 case FILTER_BOX:
50 filter_func = filter_func_box;
51 break;
52 case FILTER_GAUSSIAN:
53 filter_func = filter_func_gaussian;
54 width *= 3.0f;
55 break;
57 filter_func = filter_func_blackman_harris;
58 width *= 2.0f;
59 break;
60 default:
61 assert(0);
62 }
63
64 /* Create importance sampling table. */
65
66 /* TODO(sergey): With the even filter table size resolution we can not
67 * really make it nice symmetric importance map without sampling full range
68 * (meaning, we would need to sample full filter range and not use the
69 * make_symmetric argument).
70 *
71 * Current code matches exactly initial filter table code, but we should
72 * consider either making FILTER_TABLE_SIZE odd value or sample full filter.
73 */
74
77 0.0f,
78 width * 0.5f,
79 [filter_func, width](const float x) { return filter_func(x, width); },
80 true,
82
83 return filter_table;
84}
85
86/* Film */
87
89{
90 NodeType *type = NodeType::add("film", create);
91
92 SOCKET_FLOAT(exposure, "Exposure", 1.0f);
93 SOCKET_FLOAT(pass_alpha_threshold, "Pass Alpha Threshold", 0.5f);
94
95 static NodeEnum filter_enum;
96 filter_enum.insert("box", FILTER_BOX);
97 filter_enum.insert("gaussian", FILTER_GAUSSIAN);
98 filter_enum.insert("blackman_harris", FILTER_BLACKMAN_HARRIS);
99
100 SOCKET_ENUM(filter_type, "Filter Type", filter_enum, FILTER_BOX);
101 SOCKET_FLOAT(filter_width, "Filter Width", 1.0f);
102
103 SOCKET_FLOAT(mist_start, "Mist Start", 0.0f);
104 SOCKET_FLOAT(mist_depth, "Mist Depth", 100.0f);
105 SOCKET_FLOAT(mist_falloff, "Mist Falloff", 1.0f);
106
107 const NodeEnum *pass_type_enum = Pass::get_type_enum();
108 SOCKET_ENUM(display_pass, "Display Pass", *pass_type_enum, PASS_COMBINED);
109
110 SOCKET_BOOLEAN(show_active_pixels, "Show Active Pixels", false);
111
112 static NodeEnum cryptomatte_passes_enum;
113 cryptomatte_passes_enum.insert("none", CRYPT_NONE);
114 cryptomatte_passes_enum.insert("object", CRYPT_OBJECT);
115 cryptomatte_passes_enum.insert("material", CRYPT_MATERIAL);
116 cryptomatte_passes_enum.insert("asset", CRYPT_ASSET);
117 cryptomatte_passes_enum.insert("accurate", CRYPT_ACCURATE);
118 SOCKET_ENUM(cryptomatte_passes, "Cryptomatte Passes", cryptomatte_passes_enum, CRYPT_NONE);
119
120 SOCKET_INT(cryptomatte_depth, "Cryptomatte Depth", 0);
121
122 SOCKET_BOOLEAN(use_approximate_shadow_catcher, "Use Approximate Shadow Catcher", false);
123
124 SOCKET_BOOLEAN(use_sample_count, "Use Sample Count Pass", false);
125
126 SOCKET_BOOLEAN(denoising_pass_follow_reflections, "Denoising Pass Reflections", true);
127 SOCKET_BOOLEAN(denoising_pass_use_albedo_roughness_weighting,
128 "Denoising Pass Albedo Roughness Weighting",
129 true);
130
131 return type;
132}
133
134Film::Film() : Node(get_node_type()), filter_table_offset_(TABLE_OFFSET_INVALID) {}
135
136Film::~Film() = default;
137
139{
140 Pass *pass = scene->create_node<Pass>();
141 pass->set_type(PASS_COMBINED);
142}
143
144void Film::device_update(Device *device, DeviceScene *dscene, Scene *scene)
145{
146 if (!is_modified()) {
147 return;
148 }
149
150 const scoped_callback_timer timer([scene](double time) {
151 if (scene->update_stats) {
152 scene->update_stats->film.times.add_entry({"update", time});
153 }
154 });
155
156 device_free(device, dscene, scene);
157
158 KernelFilm *kfilm = &dscene->data.film;
159
160 /* update data */
161 kfilm->exposure = exposure;
162 kfilm->pass_alpha_threshold = pass_alpha_threshold;
163 kfilm->pass_flag = 0;
164 kfilm->denoising_pass_flag = 0;
165
166 kfilm->use_approximate_shadow_catcher = get_use_approximate_shadow_catcher();
167
168 kfilm->light_pass_flag = 0;
169 kfilm->pass_stride = 0;
170
171 /* Mark with PASS_UNUSED to avoid mask test in the kernel. */
172 kfilm->pass_combined = PASS_UNUSED;
173 kfilm->pass_depth = PASS_UNUSED;
174 kfilm->pass_position = PASS_UNUSED;
175 kfilm->pass_normal = PASS_UNUSED;
176 kfilm->pass_roughness = PASS_UNUSED;
177 kfilm->pass_motion = PASS_UNUSED;
178 kfilm->pass_motion_weight = PASS_UNUSED;
179 kfilm->pass_uv = PASS_UNUSED;
180 kfilm->pass_object_id = PASS_UNUSED;
181 kfilm->pass_material_id = PASS_UNUSED;
182 kfilm->pass_diffuse_color = PASS_UNUSED;
183 kfilm->pass_glossy_color = PASS_UNUSED;
184 kfilm->pass_transmission_color = PASS_UNUSED;
185 kfilm->pass_background = PASS_UNUSED;
186 kfilm->pass_emission = PASS_UNUSED;
187 kfilm->pass_ao = PASS_UNUSED;
188 kfilm->pass_diffuse_direct = PASS_UNUSED;
189 kfilm->pass_diffuse_indirect = PASS_UNUSED;
190 kfilm->pass_glossy_direct = PASS_UNUSED;
191 kfilm->pass_glossy_indirect = PASS_UNUSED;
192 kfilm->pass_transmission_direct = PASS_UNUSED;
193 kfilm->pass_transmission_indirect = PASS_UNUSED;
194 kfilm->pass_volume_direct = PASS_UNUSED;
195 kfilm->pass_volume_indirect = PASS_UNUSED;
196 kfilm->pass_volume_scatter = PASS_UNUSED;
197 kfilm->pass_volume_transmit = PASS_UNUSED;
198 kfilm->pass_volume_scatter_denoised = PASS_UNUSED;
199 kfilm->pass_volume_transmit_denoised = PASS_UNUSED;
200 kfilm->pass_volume_majorant = PASS_UNUSED;
201 kfilm->pass_lightgroup = PASS_UNUSED;
202
203 /* Mark passes as unused so that the kernel knows the pass is inaccessible. */
204 kfilm->pass_denoising_albedo = PASS_UNUSED;
205 kfilm->pass_denoising_specular_albedo = PASS_UNUSED;
206 kfilm->pass_denoising_normal = PASS_UNUSED;
207 kfilm->pass_denoising_roughness = PASS_UNUSED;
208 kfilm->pass_denoising_depth = PASS_UNUSED;
209 kfilm->pass_denoising_backward_motion = PASS_UNUSED;
210 kfilm->pass_denoising_specular_motion = PASS_UNUSED;
211 kfilm->pass_sample_count = PASS_UNUSED;
212 kfilm->pass_render_time = PASS_UNUSED;
213 kfilm->pass_adaptive_aux_buffer = PASS_UNUSED;
214 kfilm->pass_shadow_catcher = PASS_UNUSED;
215 kfilm->pass_shadow_catcher_sample_count = PASS_UNUSED;
216 kfilm->pass_shadow_catcher_matte = PASS_UNUSED;
217
218 kfilm->pass_guiding_color = PASS_UNUSED;
219 kfilm->pass_guiding_probability = PASS_UNUSED;
220 kfilm->pass_guiding_avg_roughness = PASS_UNUSED;
221
222 bool have_cryptomatte = false;
223 bool have_aov_color = false;
224 bool have_aov_value = false;
225 bool have_lightgroup = false;
226
227 for (size_t i = 0; i < scene->passes.size(); i++) {
228 const Pass *pass = scene->passes[i];
229
230 if (pass->get_type() == PASS_NONE || !pass->is_written()) {
231 continue;
232 }
233
234 if (pass->get_mode() == PassMode::DENOISED) {
235 /* Generally we only storing offsets of the noisy passes. The display pass is an exception
236 * since it is a read operation and not a write. */
237 if (pass->get_type() == PASS_VOLUME_TRANSMIT) {
238 kfilm->pass_volume_transmit_denoised = kfilm->pass_stride;
239 }
240 else if (pass->get_type() == PASS_VOLUME_SCATTER) {
241 kfilm->pass_volume_scatter_denoised = kfilm->pass_stride;
242 }
243 kfilm->pass_stride += pass->get_info().num_components;
244 continue;
245 }
246
247 /* Can't do motion pass if no motion vectors are available. */
248 if (pass->get_type() == PASS_MOTION || pass->get_type() == PASS_MOTION_WEIGHT ||
249 pass->get_type() == PASS_DENOISING_BACKWARD_MOTION ||
250 pass->get_type() == PASS_DENOISING_SPECULAR_MOTION)
251 {
252 const Scene::MotionType need_motion = scene->need_motion();
253 if (need_motion != Scene::MOTION_PASS && need_motion != Scene::MOTION_PASS_INTERACTIVE) {
254 kfilm->pass_stride += pass->get_info().num_components;
255 continue;
256 }
257 }
258
259 const int pass_flag = (1 << (pass->get_type() % 32));
260 if (pass->get_type() <= PASS_CATEGORY_LIGHT_END) {
261 kfilm->light_pass_flag |= pass_flag;
262 }
263 else if (pass->get_type() <= PASS_CATEGORY_DATA_END) {
264 kfilm->pass_flag |= pass_flag;
265 }
266 else if (pass->get_type() <= PASS_CATEGORY_DENOISING_END) {
267 kfilm->denoising_pass_flag |= pass_flag;
268 }
269 else {
270 assert(pass->get_type() <= PASS_CATEGORY_BAKE_END);
271 }
272
273 if (!pass->get_lightgroup().empty()) {
274 if (!have_lightgroup) {
275 kfilm->pass_lightgroup = kfilm->pass_stride;
276 have_lightgroup = true;
277 }
278 kfilm->pass_stride += pass->get_info().num_components;
279 continue;
280 }
281
282 switch (pass->get_type()) {
283 case PASS_COMBINED:
284 kfilm->pass_combined = kfilm->pass_stride;
285 break;
286 case PASS_DEPTH:
287 kfilm->pass_depth = kfilm->pass_stride;
288 break;
289 case PASS_NORMAL:
290 kfilm->pass_normal = kfilm->pass_stride;
291 break;
292 case PASS_POSITION:
293 kfilm->pass_position = kfilm->pass_stride;
294 break;
295 case PASS_ROUGHNESS:
296 kfilm->pass_roughness = kfilm->pass_stride;
297 break;
298 case PASS_UV:
299 kfilm->pass_uv = kfilm->pass_stride;
300 break;
301 case PASS_MOTION:
302 kfilm->pass_motion = kfilm->pass_stride;
303 break;
305 kfilm->pass_motion_weight = kfilm->pass_stride;
306 break;
307 case PASS_OBJECT_ID:
308 kfilm->pass_object_id = kfilm->pass_stride;
309 break;
310 case PASS_MATERIAL_ID:
311 kfilm->pass_material_id = kfilm->pass_stride;
312 break;
313
314 case PASS_MIST:
315 kfilm->pass_mist = kfilm->pass_stride;
316 break;
317 case PASS_EMISSION:
318 kfilm->pass_emission = kfilm->pass_stride;
319 break;
320 case PASS_BACKGROUND:
321 kfilm->pass_background = kfilm->pass_stride;
322 break;
323 case PASS_AO:
324 kfilm->pass_ao = kfilm->pass_stride;
325 break;
326
328 kfilm->pass_diffuse_color = kfilm->pass_stride;
329 break;
331 kfilm->pass_glossy_color = kfilm->pass_stride;
332 break;
334 kfilm->pass_transmission_color = kfilm->pass_stride;
335 break;
337 kfilm->pass_diffuse_indirect = kfilm->pass_stride;
338 break;
340 kfilm->pass_glossy_indirect = kfilm->pass_stride;
341 break;
343 kfilm->pass_transmission_indirect = kfilm->pass_stride;
344 break;
346 kfilm->pass_volume_indirect = kfilm->pass_stride;
347 break;
349 kfilm->pass_diffuse_direct = kfilm->pass_stride;
350 break;
352 kfilm->pass_glossy_direct = kfilm->pass_stride;
353 break;
355 kfilm->pass_transmission_direct = kfilm->pass_stride;
356 break;
358 kfilm->pass_volume_direct = kfilm->pass_stride;
359 break;
361 kfilm->pass_volume_scatter = kfilm->pass_stride;
362 break;
364 kfilm->pass_volume_transmit = kfilm->pass_stride;
365 break;
367 kfilm->pass_volume_majorant = kfilm->pass_stride;
368 break;
370 kfilm->pass_volume_majorant_sample_count = kfilm->pass_stride;
371 break;
372
374 kfilm->pass_bake_primitive = kfilm->pass_stride;
375 break;
376 case PASS_BAKE_SEED:
377 kfilm->pass_bake_seed = kfilm->pass_stride;
378 break;
380 kfilm->pass_bake_differential = kfilm->pass_stride;
381 break;
382
383 case PASS_CRYPTOMATTE:
384 kfilm->pass_cryptomatte = have_cryptomatte ?
385 min(kfilm->pass_cryptomatte, kfilm->pass_stride) :
386 kfilm->pass_stride;
387 have_cryptomatte = true;
388 break;
389
391 kfilm->pass_denoising_albedo = kfilm->pass_stride;
392 break;
394 kfilm->pass_denoising_specular_albedo = kfilm->pass_stride;
395 break;
397 kfilm->pass_denoising_normal = kfilm->pass_stride;
398 break;
400 kfilm->pass_denoising_roughness = kfilm->pass_stride;
401 break;
403 kfilm->pass_denoising_depth = kfilm->pass_stride;
404 break;
406 kfilm->pass_denoising_backward_motion = kfilm->pass_stride;
407 break;
409 kfilm->pass_denoising_specular_motion = kfilm->pass_stride;
410 break;
411
413 kfilm->pass_shadow_catcher = kfilm->pass_stride;
414 break;
416 kfilm->pass_shadow_catcher_sample_count = kfilm->pass_stride;
417 break;
419 kfilm->pass_shadow_catcher_matte = kfilm->pass_stride;
420 break;
421
423 kfilm->pass_adaptive_aux_buffer = kfilm->pass_stride;
424 break;
426 kfilm->pass_sample_count = kfilm->pass_stride;
427 break;
428 case PASS_RENDER_TIME:
429 kfilm->pass_render_time = kfilm->pass_stride;
430 break;
431
432 case PASS_AOV_COLOR:
433 if (!have_aov_color) {
434 kfilm->pass_aov_color = kfilm->pass_stride;
435 have_aov_color = true;
436 }
437 break;
438 case PASS_AOV_VALUE:
439 if (!have_aov_value) {
440 kfilm->pass_aov_value = kfilm->pass_stride;
441 have_aov_value = true;
442 }
443 break;
445 kfilm->pass_guiding_color = kfilm->pass_stride;
446 break;
448 kfilm->pass_guiding_probability = kfilm->pass_stride;
449 break;
451 kfilm->pass_guiding_avg_roughness = kfilm->pass_stride;
452 break;
453 default:
454 assert(false);
455 break;
456 }
457
458 kfilm->pass_stride += pass->get_info().num_components;
459 }
460
461 /* update filter table */
462 vector<float> table = filter_table(filter_type, filter_width);
463 scene->lookup_tables->remove_table(&filter_table_offset_);
464 filter_table_offset_ = scene->lookup_tables->add_table(dscene, table);
465 dscene->data.tables.filter_table_offset = (int)filter_table_offset_;
466
467 /* mist pass parameters */
468 kfilm->mist_start = mist_start;
469 kfilm->mist_inv_depth = (mist_depth > 0.0f) ? 1.0f / mist_depth : 0.0f;
470 kfilm->mist_falloff = mist_falloff;
471
472 kfilm->cryptomatte_passes = cryptomatte_passes;
473 kfilm->cryptomatte_depth = cryptomatte_depth;
474
475 /* denoiser pass parameters */
476 kfilm->denoising_pass_options_flag = 0;
477 if (denoising_pass_follow_reflections) {
478 kfilm->denoising_pass_options_flag |= DENOISING_PASS_FOLLOW_REFLECTIONS;
479 }
480 if (denoising_pass_use_albedo_roughness_weighting) {
481 kfilm->denoising_pass_options_flag |= DENOISING_PASS_USE_ALBEDO_ROUGHNESS_WEIGHTING;
482 }
483
485}
486
487void Film::device_free(Device * /*device*/, DeviceScene * /*dscene*/, Scene *scene)
488{
489 scene->lookup_tables->remove_table(&filter_table_offset_);
490}
491
492int Film::get_aov_offset(Scene *scene, string name, bool &is_color)
493{
494 int offset_color = 0;
495 int offset_value = 0;
496 for (const Pass *pass : scene->passes) {
497 if (pass->get_name() == name) {
498 if (pass->get_type() == PASS_AOV_VALUE) {
499 is_color = false;
500 return offset_value;
501 }
502 if (pass->get_type() == PASS_AOV_COLOR) {
503 is_color = true;
504 return offset_color;
505 }
506 }
507
508 if (pass->get_type() == PASS_AOV_VALUE) {
509 offset_value += pass->get_info().num_components;
510 }
511 else if (pass->get_type() == PASS_AOV_COLOR) {
512 offset_color += pass->get_info().num_components;
513 }
514 }
515
516 return -1;
517}
518
520{
521 map<ustring, int> lightgroups;
522 int i = 0;
523 for (const Pass *pass : scene->passes) {
524 const ustring lightgroup = pass->get_lightgroup();
525 if (!lightgroup.empty()) {
526 if (!lightgroups.contains(lightgroup)) {
527 lightgroups[lightgroup] = i++;
528 }
529 }
530 }
531 if (scene->lightgroups != lightgroups) {
532 scene->lightgroups = lightgroups;
533 return true;
534 }
535
536 return false;
537}
538
540{
541 const Background *background = scene->background;
542 const BakeManager *bake_manager = scene->bake_manager.get();
543 const ObjectManager *object_manager = scene->object_manager.get();
544 Integrator *integrator = scene->integrator;
545
546 if (!is_modified() && !object_manager->need_update() && !integrator->is_modified() &&
547 !background->is_modified() && !scene->has_volume_modified())
548 {
549 return;
550 }
551
552 /* Remove auto generated passes and recreate them. */
553 remove_auto_passes(scene);
554
555 /* Display pass for viewport. */
556 const PassType display_pass = get_display_pass();
557 add_auto_pass(scene, display_pass);
558
559 /* Assumption is that a combined pass always exists for now, for example
560 * adaptive sampling is always based on a combined pass. But we should
561 * try to lift this limitation in the future for faster rendering of
562 * individual passes. */
563 if (display_pass != PASS_COMBINED) {
564 add_auto_pass(scene, PASS_COMBINED);
565 }
566
567 /* Create passes needed for adaptive sampling. */
568 const AdaptiveSampling adaptive_sampling = integrator->get_adaptive_sampling();
569 if (adaptive_sampling.use) {
570 add_auto_pass(scene, PASS_SAMPLE_COUNT);
571 add_auto_pass(scene, PASS_ADAPTIVE_AUX_BUFFER);
572 }
573
574 /* Create passes needed for denoising. */
575 const bool use_denoise = integrator->get_use_denoise();
576 if (use_denoise) {
577 const DenoiserPassMask denoiser_passes = integrator->get_denoiser_passes();
578 if (denoiser_passes & DENOISER_PASS_ALBEDO) {
579 add_auto_pass(scene, PASS_DENOISING_ALBEDO);
580 }
581 if (denoiser_passes & DENOISER_PASS_SPECULAR_ALBEDO) {
582 add_auto_pass(scene, PASS_DENOISING_SPECULAR_ALBEDO);
583 }
584 if (denoiser_passes & DENOISER_PASS_NORMAL) {
585 add_auto_pass(scene, PASS_DENOISING_NORMAL);
586 }
587 if (denoiser_passes & DENOISER_PASS_ROUGHNESS) {
588 add_auto_pass(scene, PASS_DENOISING_ROUGHNESS);
589 }
590 if (denoiser_passes & DENOISER_PASS_DEPTH) {
591 add_auto_pass(scene, PASS_DENOISING_DEPTH);
592 }
593 if (denoiser_passes & DENOISER_PASS_BACKWARD_MOTION) {
594 add_auto_pass(scene, PASS_DENOISING_BACKWARD_MOTION);
595 }
596 if (denoiser_passes & DENOISER_PASS_SPECULAR_MOTION) {
597 add_auto_pass(scene, PASS_DENOISING_SPECULAR_MOTION);
598 }
599 }
600
601 /* Create passes for shadow catcher. */
602 if (scene->has_shadow_catcher()) {
603 const bool need_background = get_use_approximate_shadow_catcher() &&
604 !background->get_transparent();
605
606 add_auto_pass(scene, PASS_SHADOW_CATCHER);
607 add_auto_pass(scene, PASS_SHADOW_CATCHER_SAMPLE_COUNT);
608 add_auto_pass(scene, PASS_SHADOW_CATCHER_MATTE);
609
610 if (need_background) {
611 add_auto_pass(scene, PASS_BACKGROUND);
612 }
613 }
614 else if (Pass::contains(scene->passes, PASS_SHADOW_CATCHER)) {
615 add_auto_pass(scene, PASS_SHADOW_CATCHER);
616 add_auto_pass(scene, PASS_SHADOW_CATCHER_SAMPLE_COUNT);
617 }
618
619 const vector<Pass *> passes_immutable = scene->passes;
620 for (const Pass *pass : passes_immutable) {
621 const PassInfo info = pass->get_info();
622 /* Add utility passes needed to generate some light passes. */
623 if (info.divide_type != PASS_NONE) {
624 add_auto_pass(scene, info.divide_type);
625 }
626 if (info.direct_type != PASS_NONE) {
627 add_auto_pass(scene, info.direct_type);
628 }
629 if (info.indirect_type != PASS_NONE) {
630 add_auto_pass(scene, info.indirect_type);
631 }
632
633 /* NOTE: Enable all denoised passes when storage is requested.
634 * This way it is possible to tweak denoiser parameters later on. */
635 if (info.support_denoise && use_denoise) {
636 add_auto_pass(scene, pass->get_type(), PassMode::DENOISED);
637 }
638 }
639
640 if (bake_manager->get_baking()) {
641 add_auto_pass(scene, PASS_BAKE_PRIMITIVE, "BakePrimitive");
642 add_auto_pass(scene, PASS_BAKE_DIFFERENTIAL, "BakeDifferential");
643 if (bake_manager->get_use_seed()) {
644 add_auto_pass(scene, PASS_BAKE_SEED, "BakeSeed");
645 }
646 }
647
648 /* Add sample count pass for tiled rendering. */
649 if (use_sample_count) {
651 add_auto_pass(scene, PASS_SAMPLE_COUNT);
652 }
653 }
654
655 if (scene->has_volume()) {
656 add_auto_pass(scene, PASS_VOLUME_SCATTER);
657 add_auto_pass(scene, PASS_VOLUME_SCATTER, PassMode::DENOISED, "Volume Scatter");
658 add_auto_pass(scene, PASS_VOLUME_TRANSMIT);
659 add_auto_pass(scene, PASS_VOLUME_TRANSMIT, PassMode::DENOISED, "Volume Transmit");
661 add_auto_pass(scene, PASS_SAMPLE_COUNT);
662 }
664 add_auto_pass(scene, PASS_VOLUME_MAJORANT, "Volume Majorant");
665 }
666 add_auto_pass(scene, PASS_VOLUME_MAJORANT_SAMPLE_COUNT);
667 }
668
669 /* Remove duplicates and initialize internal pass info. */
670 finalize_passes(scene, use_denoise);
671
672 /* Flush scene updates. */
673 const bool have_uv_pass = Pass::contains(scene->passes, PASS_UV);
674 const bool have_motion_pass = Pass::contains(scene->passes, PASS_MOTION) ||
677 const bool have_ao_pass = Pass::contains(scene->passes, PASS_AO);
678
679 if (have_uv_pass != prev_have_uv_pass) {
680 scene->geometry_manager->tag_update(scene, GeometryManager::UV_PASS_NEEDED);
681 for (Shader *shader : scene->shaders) {
682 shader->need_update_uvs = true;
683 }
684 }
685 if (have_motion_pass != prev_have_motion_pass) {
686 scene->geometry_manager->tag_update(scene, GeometryManager::MOTION_PASS_NEEDED);
687 }
688 if (have_ao_pass != prev_have_ao_pass) {
690 }
691
692 prev_have_uv_pass = have_uv_pass;
693 prev_have_motion_pass = have_motion_pass;
694 prev_have_ao_pass = have_ao_pass;
695
696 tag_modified();
697
698 /* Debug logging. */
700 LOG_INFO << "Effective scene passes:";
701 for (const Pass *pass : scene->passes) {
702 LOG_INFO << "- " << *pass;
703 }
704 }
705}
706
707void Film::add_auto_pass(Scene *scene, PassType type, const char *name)
708{
709 add_auto_pass(scene, type, PassMode::NOISY, name);
710}
711
712void Film::add_auto_pass(Scene *scene, PassType type, PassMode mode, const char *name)
713{
714 unique_ptr<Pass> pass = make_unique<Pass>();
715 pass->set_type(type);
716 pass->set_mode(mode);
717 pass->set_name(ustring((name) ? name : ""));
718 pass->is_auto_ = true;
719
720 pass->set_owner(scene);
721 scene->passes.push_back(std::move(pass));
722}
723
724void Film::remove_auto_passes(Scene *scene)
725{
726 /* Remove all passes which were automatically created. */
727 unique_ptr_vector<Pass> new_passes;
728
729 for (size_t i = 0; i < scene->passes.size(); i++) {
730 unique_ptr<Pass> pass = scene->passes.steal(i);
731 if (!pass->is_auto_) {
732 new_passes.push_back(std::move(pass));
733 }
734 }
735
736 scene->passes = std::move(new_passes);
737}
738
739static bool compare_pass_order(const Pass *a, const Pass *b)
740{
741 /* On the highest level, sort by number of components.
742 * Within passes of the same component count, sort so that all non-lightgroup passes come first.
743 * Within that group, sort by type. */
744 const int num_components_a = a->get_info().num_components;
745 const int num_components_b = b->get_info().num_components;
746
747 if (num_components_a == num_components_b) {
748 const int is_lightgroup_a = !a->get_lightgroup().empty();
749 const int is_lightgroup_b = !b->get_lightgroup().empty();
750 if (is_lightgroup_a == is_lightgroup_b) {
751 return (a->get_type() < b->get_type());
752 }
753 return is_lightgroup_b;
754 }
755
756 return num_components_a > num_components_b;
757}
758
759void Film::finalize_passes(Scene *scene, const bool use_denoise)
760{
761 /* Remove duplicate passes. */
762 unique_ptr_vector<Pass> new_passes;
763
764 for (size_t i = 0; i < scene->passes.size(); i++) {
765 unique_ptr<Pass> pass = scene->passes.steal(i);
766
767 /* Disable denoising on passes if denoising is disabled, or if the
768 * pass does not support it. */
769 const bool need_denoise = pass->get_info().support_denoise &&
770 (use_denoise || is_volume_guiding_pass(pass->get_type()));
771 pass->set_mode(need_denoise ? pass->get_mode() : PassMode::NOISY);
772
773 /* Merge duplicate passes. */
774 bool duplicate_found = false;
775 for (Pass *new_pass : new_passes) {
776 /* If different type or denoising, don't merge. */
777 if (new_pass->get_type() != pass->get_type() || new_pass->get_mode() != pass->get_mode()) {
778 continue;
779 }
780
781 /* If both passes have a name and the names are different, don't merge.
782 * If either pass has a name, we'll use that name. */
783 if (!pass->get_name().empty() && !new_pass->get_name().empty() &&
784 pass->get_name() != new_pass->get_name())
785 {
786 continue;
787 }
788
789 if (!pass->get_name().empty() && new_pass->get_name().empty()) {
790 new_pass->set_name(pass->get_name());
791 }
792
793 new_pass->is_auto_ &= pass->is_auto_;
794 duplicate_found = true;
795 break;
796 }
797
798 if (!duplicate_found) {
799 new_passes.push_back(std::move(pass));
800 }
801 }
802
803 /* Order from by components and type, This is required for AOVs and cryptomatte passes,
804 * which the kernel assumes to be in order. Note this must use stable sort so cryptomatte
805 * passes remain in the right order. */
807
808 scene->passes = std::move(new_passes);
809}
810
812{
813 uint64_t kernel_features = 0;
814
815 for (const Pass *pass : scene->passes) {
816 if (!pass->is_written()) {
817 continue;
818 }
819
820 const PassType pass_type = pass->get_type();
821 const PassMode pass_mode = pass->get_mode();
822
823 const bool has_denoise_pass = (pass_mode == PassMode::DENOISED) &&
824 !is_volume_guiding_pass(pass_type);
825
826 if (has_denoise_pass ||
827 (pass_type >= PASS_DENOISING_ALBEDO && pass_type <= PASS_DENOISING_SPECULAR_MOTION))
828 {
829 kernel_features |= KERNEL_FEATURE_DENOISING;
830 }
831
832 if (pass_type >= PASS_DIFFUSE && pass_type <= PASS_VOLUME_TRANSMIT) {
833 kernel_features |= KERNEL_FEATURE_LIGHT_PASSES;
834 }
835
836 if (pass_type == PASS_AO) {
837 kernel_features |= KERNEL_FEATURE_AO_PASS;
838 }
839 }
840
841 return kernel_features;
842}
843
unsigned long long int uint64_t
bool get_use_seed() const
Definition bake.cpp:48
bool get_baking() const
Definition bake.cpp:15
KernelData data
Definition film.h:29
uint64_t get_kernel_features(const Scene *scene) const
Definition film.cpp:811
int get_aov_offset(Scene *scene, string name, bool &is_color)
Definition film.cpp:492
void update_passes(Scene *scene)
Definition film.cpp:539
static void add_default(Scene *scene)
Definition film.cpp:138
void device_free(Device *device, DeviceScene *dscene, Scene *scene)
Definition film.cpp:487
void device_update(Device *device, DeviceScene *dscene, Scene *scene)
Definition film.cpp:144
Film()
Definition film.cpp:134
~Film() override
bool update_lightgroups(Scene *scene)
Definition film.cpp:519
void tag_update(Scene *scene, const uint32_t flag)
AdaptiveSampling get_adaptive_sampling() const
bool is_modified() const
bool need_update() const
Definition pass.h:50
PassInfo get_info() const
Definition pass.cpp:150
static bool contains(const unique_ptr_vector< Pass > &passes, PassType type)
Definition pass.cpp:404
static const NodeEnum * get_type_enum()
Definition pass.cpp:45
bool is_written() const
Definition pass.cpp:155
Definition scene.h:133
bool has_volume_modified() const
Definition scene.cpp:849
unique_ptr< ObjectManager > object_manager
Definition scene.h:171
MotionType need_motion() const
Definition scene.cpp:429
unique_ptr< SceneUpdateStats > update_stats
Definition scene.h:197
T * create_node(Args &&...)=delete
unique_ptr< BakeManager > bake_manager
Definition scene.h:173
Background * background
Definition scene.h:148
unique_ptr_vector< Shader > shaders
Definition scene.h:158
MotionType
Definition scene.h:207
@ MOTION_PASS
Definition scene.h:207
@ MOTION_PASS_INTERACTIVE
Definition scene.h:207
bool has_volume()
Definition scene.cpp:843
bool has_shadow_catcher()
Definition scene.cpp:818
unique_ptr< GeometryManager > geometry_manager
Definition scene.h:170
Integrator * integrator
Definition scene.h:149
map< ustring, int > lightgroups
Definition scene.h:139
unique_ptr_vector< Pass > passes
Definition scene.h:159
unique_ptr< LookupTables > lookup_tables
Definition scene.h:143
bool need_update_uvs
void push_back(unique_ptr< T > &&value)
unique_ptr< T > steal(const size_t i)
size_t size() const
void stable_sort(Compare compare)
int DenoiserPassMask
Definition denoise.h:37
@ DENOISER_PASS_DEPTH
Definition denoise.h:32
@ DENOISER_PASS_ROUGHNESS
Definition denoise.h:31
@ DENOISER_PASS_NORMAL
Definition denoise.h:30
@ DENOISER_PASS_ALBEDO
Definition denoise.h:28
@ DENOISER_PASS_SPECULAR_ALBEDO
Definition denoise.h:29
@ DENOISER_PASS_BACKWARD_MOTION
Definition denoise.h:33
@ DENOISER_PASS_SPECULAR_MOTION
Definition denoise.h:34
#define PASS_UNUSED
#define FILTER_TABLE_SIZE
#define KERNEL_FEATURE_LIGHT_PASSES
#define KERNEL_FEATURE_AO_PASS
#define KERNEL_FEATURE_DENOISING
#define expf(x)
#define CCL_NAMESPACE_END
static float filter_func_gaussian(float v, const float width)
Definition film.cpp:31
static bool compare_pass_order(const Pass *a, const Pass *b)
Definition film.cpp:739
static float filter_func_blackman_harris(float v, const float width)
Definition film.cpp:37
static CCL_NAMESPACE_BEGIN float filter_func_box(float, float)
Definition film.cpp:26
static vector< float > filter_table(FilterType type, float width)
Definition film.cpp:43
FilterType
Definition film.h:21
@ FILTER_BOX
Definition film.h:22
@ FILTER_BLACKMAN_HARRIS
Definition film.h:24
@ FILTER_GAUSSIAN
Definition film.h:23
#define assert(assertion)
@ CRYPT_ASSET
@ CRYPT_NONE
@ CRYPT_ACCURATE
@ CRYPT_OBJECT
@ CRYPT_MATERIAL
PassType
@ PASS_SHADOW_CATCHER_SAMPLE_COUNT
@ PASS_EMISSION
@ PASS_POSITION
@ PASS_BACKGROUND
@ PASS_TRANSMISSION_DIRECT
@ PASS_VOLUME_DIRECT
@ PASS_UV
@ PASS_TRANSMISSION_COLOR
@ PASS_SHADOW_CATCHER_MATTE
@ PASS_GUIDING_COLOR
@ PASS_DEPTH
@ PASS_MIST
@ PASS_VOLUME_SCATTER
@ PASS_TRANSMISSION_INDIRECT
@ PASS_BAKE_SEED
@ PASS_SHADOW_CATCHER
@ PASS_VOLUME_MAJORANT
@ PASS_DENOISING_NORMAL
@ PASS_ROUGHNESS
@ PASS_DIFFUSE_DIRECT
@ PASS_MOTION
@ PASS_CATEGORY_BAKE_END
@ PASS_MATERIAL_ID
@ PASS_CATEGORY_DENOISING_END
@ PASS_VOLUME_MAJORANT_SAMPLE_COUNT
@ PASS_AO
@ PASS_COMBINED
@ PASS_DIFFUSE
@ PASS_DENOISING_BACKWARD_MOTION
@ PASS_DIFFUSE_INDIRECT
@ PASS_RENDER_TIME
@ PASS_GUIDING_PROBABILITY
@ PASS_CATEGORY_DATA_END
@ PASS_ADAPTIVE_AUX_BUFFER
@ PASS_OBJECT_ID
@ PASS_DENOISING_SPECULAR_MOTION
@ PASS_DENOISING_ROUGHNESS
@ PASS_AOV_COLOR
@ PASS_NONE
@ PASS_DENOISING_SPECULAR_ALBEDO
@ PASS_VOLUME_INDIRECT
@ PASS_NORMAL
@ PASS_CRYPTOMATTE
@ PASS_DIFFUSE_COLOR
@ PASS_SAMPLE_COUNT
@ PASS_GLOSSY_DIRECT
@ PASS_MOTION_WEIGHT
@ PASS_DENOISING_ALBEDO
@ PASS_AOV_VALUE
@ PASS_GUIDING_AVG_ROUGHNESS
@ PASS_GLOSSY_COLOR
@ PASS_GLOSSY_INDIRECT
@ PASS_BAKE_DIFFERENTIAL
@ PASS_VOLUME_TRANSMIT
@ PASS_DENOISING_DEPTH
@ PASS_BAKE_PRIMITIVE
@ PASS_CATEGORY_LIGHT_END
@ DENOISING_PASS_FOLLOW_REFLECTIONS
@ DENOISING_PASS_USE_ALBEDO_ROUGHNESS_WEIGHTING
#define LOG_IS_ON(level)
Definition log.h:113
@ LOG_LEVEL_INFO
Definition log.h:25
#define LOG_INFO
Definition log.h:106
void util_cdf_inverted(const int resolution, const float from, const float to, Functor functor, const bool make_symmetric, vector< float > &inv_cdf)
Definition math_cdf.h:45
#define SOCKET_FLOAT(name, ui_name, default_value,...)
Definition node_type.h:219
#define SOCKET_INT(name, ui_name, default_value,...)
Definition node_type.h:213
#define NODE_DEFINE(structname)
Definition node_type.h:157
#define SOCKET_BOOLEAN(name, ui_name, default_value,...)
Definition node_type.h:211
#define SOCKET_ENUM(name, ui_name, values, default_value,...)
Definition node_type.h:235
bool is_volume_guiding_pass(const PassType pass_type)
Definition pass.cpp:478
PassMode
Definition pass.h:20
@ DENOISED
Definition pass.h:22
@ NOISY
Definition pass.h:21
#define M_2PI_F
#define cosf
@ TABLE_OFFSET_INVALID
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
void insert(const char *x, const int y)
Definition node_enum.h:21
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=nullptr)
const NodeType * type
Definition graph/node.h:179
ustring name
Definition graph/node.h:178
void clear_modified()
void tag_modified()
bool is_modified() const
Node(const NodeType *type, ustring name=ustring())
PassType direct_type
Definition pass.h:34
int num_components
Definition pass.h:28
bool support_denoise
Definition pass.h:47
PassType divide_type
Definition pass.h:33
PassType indirect_type
Definition pass.h:35