Blender V5.3
scene.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 <cstdlib>
6
7#include "bvh/bvh.h"
8
9#include "device/device.h"
10
11#include "scene/background.h"
12#include "scene/bake.h"
13#include "scene/camera.h"
14#include "scene/curves.h"
15#include "scene/devicescene.h"
16#include "scene/film.h"
17#include "scene/hair.h"
18#include "scene/integrator.h"
19#include "scene/light.h"
20#include "scene/mesh.h"
21#include "scene/object.h"
22#include "scene/osl.h"
23#include "scene/particles.h"
24#include "scene/pointcloud.h"
25#include "scene/procedural.h"
26#include "scene/scene.h"
28#include "scene/shader.h"
29#include "scene/svm.h"
30#include "scene/tables.h"
31#include "scene/volume.h"
32
33#include "session/session.h"
34
36#include "util/log.h"
37#include "util/progress.h"
38
40
41Scene ::Scene(const SceneParams &params_, Device *device)
42 : name("Scene"),
43 default_surface(nullptr),
44 default_volume(nullptr),
45 default_light(nullptr),
46 default_background(nullptr),
47 default_empty(nullptr),
50 params(params_),
51 update_stats(nullptr),
52 kernels_loaded(false),
53 /* TODO(sergey): Check if it's indeed optimal value for the split kernel.
54 */
56{
57 memset((void *)&dscene.data, 0, sizeof(dscene.data));
58
59 osl_manager = make_unique<OSLManager>(device);
62
63 light_manager = make_unique<LightManager>();
64 geometry_manager = make_unique<GeometryManager>();
65 object_manager = make_unique<ObjectManager>();
66 image_manager = make_unique<ImageManager>(device->info, params);
67 particle_system_manager = make_unique<ParticleSystemManager>();
68 bake_manager = make_unique<BakeManager>();
69 procedural_manager = make_unique<ProceduralManager>();
70 volume_manager = make_unique<VolumeManager>();
71
72 /* Create nodes after managers, since create_node() can tag the managers. */
75 lookup_tables = make_unique<LookupTables>();
80
81 ccl::Film::add_default(this);
82 ccl::ShaderManager::add_default(this);
83}
84
86{
87 free_memory(true);
88}
89
90void Scene::free_memory(bool final)
91{
92 bvh.reset();
93
94 /* The order of deletion is important to make sure data is freed based on
95 * possible dependencies as the Nodes' reference counts are decremented in the
96 * destructors:
97 *
98 * - Procedurals can create and hold pointers to any other types.
99 * - Objects can hold pointers to Geometries and ParticleSystems
100 * - Lights and Geometries can hold pointers to Shaders.
101 *
102 * Similarly, we first delete all nodes and their associated device data, and
103 * then the managers and their associated device data.
104 */
106 objects.clear();
107 geometry.clear();
109 passes.clear();
110
111 if (device) {
113 film->device_free(device, &dscene, this);
117 }
118
119 if (final) {
120 cameras.clear();
123 films.clear();
125
126 camera = nullptr;
127 dicing_camera = nullptr;
128 integrator = nullptr;
129 scene_attribute = nullptr;
130 film = nullptr;
131 background = nullptr;
132 }
133
134 /* Delete Shaders after every other nodes to ensure that we do not try to
135 * decrement the reference count on some dangling pointer. */
136 shaders.clear();
137
138 /* Now that all nodes have been deleted, we can safely delete managers and
139 * device data. */
140 if (device) {
141 object_manager->device_free(device, &dscene, true);
142 geometry_manager->device_free(device, &dscene, true);
143 shader_manager->device_free(device, &dscene, this);
144 osl_manager->device_free(device, &dscene, this);
145 light_manager->device_free(device, &dscene);
146 particle_system_manager->device_free(device, &dscene);
147 bake_manager->device_free(device, &dscene);
148 volume_manager->device_free(&dscene);
149
150 if (final) {
151 image_manager->device_free(this);
152 }
153 else {
154 image_manager->device_free_builtin(this);
155 }
156
157 lookup_tables->device_free(device, &dscene);
158 }
159
160 if (final) {
161 lookup_tables.reset();
162 object_manager.reset();
163 geometry_manager.reset();
164 shader_manager.reset();
165 osl_manager.reset();
166 light_manager.reset();
168 image_manager.reset();
169 bake_manager.reset();
170 update_stats.reset();
171 procedural_manager.reset();
172 volume_manager.reset();
173 }
174}
175
176void Scene::device_update(Device *device_, Progress &progress)
177{
178 if (!device) {
179 device = device_;
180 }
181
182 const bool print_stats = need_data_update();
183 bool kernels_reloaded = false;
184
185 while (true) {
186 if (update_stats) {
187 update_stats->clear();
188 }
189
190 const scoped_callback_timer timer([this, print_stats](double time) {
191 if (update_stats) {
192 update_stats->scene.times.add_entry({"device_update", time});
193
194 if (print_stats) {
195 printf("Update statistics:\n%s\n", update_stats->full_report().c_str());
196 }
197 }
198 });
199
200 /* The order of updates is important, because there's dependencies between
201 * the different managers, using data computed by previous managers. */
202
203 if (film->update_lightgroups(this)) {
204 light_manager->tag_update(this, ccl::LightManager::LIGHT_MODIFIED);
205 object_manager->tag_update(this, ccl::ObjectManager::OBJECT_MODIFIED);
207 }
208 if (film->exposure_is_modified()) {
210 }
211
212 /* Compile shaders and get information about features they used. */
213 progress.set_status("Updating Shaders");
214 osl_manager->device_update_pre(device, this);
215 shader_manager->device_update_pre(device, &dscene, this, progress);
216
217 if (progress.get_cancel() || device->have_error()) {
218 return;
219 }
220
221 /* Passes. After shader manager as this depends on the shaders. */
222 film->update_passes(this);
223
224 /* Update kernel features. After shaders and passes since those affect features. */
226
227 /* Load render kernels, before uploading most data to the GPU, and before displacement and
228 * background light need to run kernels.
229 *
230 * Do it outside of the scene mutex since the heavy part of the loading (i.e. kernel
231 * compilation) does not depend on the scene and some other functionality (like display
232 * driver) might be waiting on the scene mutex to synchronize display pass.
233 *
234 * This does mean the scene might have gotten updated in the meantime, in which case
235 * we have to redo the first part of the scene update. */
236 const uint64_t kernel_features = dscene.data.kernel_features;
238 if (!kernels_loaded || loaded_kernel_features != kernel_features) {
239 mutex.unlock();
240 kernels_reloaded |= load_kernels(progress);
241 mutex.lock();
242 }
243
244 if (progress.get_cancel() || device->have_error()) {
245 return;
246 }
247
249 break;
250 }
251 }
252
253 /* Upload shaders to GPU and compile OSL kernels, after kernels have been loaded. */
254 shader_manager->device_update_post(device, &dscene, this, progress);
255 osl_manager->device_update_post(device, this, progress, kernels_reloaded);
256
257 if (progress.get_cancel() || device->have_error()) {
258 return;
259 }
260
261 procedural_manager->update(this, progress);
262
263 if (progress.get_cancel()) {
264 return;
265 }
266
267 progress.set_status("Updating Background");
269
270 if (progress.get_cancel() || device->have_error()) {
271 return;
272 }
273
274 progress.set_status("Updating Scene Attribute");
276
277 if (progress.get_cancel() || device->have_error()) {
278 return;
279 }
280
281 /* Camera will be used by adaptive subdivision, so do early. */
282 progress.set_status("Updating Camera");
284
285 if (progress.get_cancel() || device->have_error()) {
286 return;
287 }
288
289 geometry_manager->device_update_preprocess(device, this, progress);
290 if (progress.get_cancel() || device->have_error()) {
291 return;
292 }
293
294 /* Update objects after geometry preprocessing. */
295 progress.set_status("Updating Objects");
296 object_manager->device_update(device, &dscene, this, progress);
297
298 if (progress.get_cancel() || device->have_error()) {
299 return;
300 }
301
302 progress.set_status("Updating Particle Systems");
303 particle_system_manager->device_update(device, &dscene, this, progress);
304
305 if (progress.get_cancel() || device->have_error()) {
306 return;
307 }
308
309 /* Camera and shaders must be ready here for adaptive subdivision and displacement. */
310 progress.set_status("Updating Meshes");
311 geometry_manager->device_update(device, &dscene, this, progress);
312
313 if (progress.get_cancel() || device->have_error()) {
314 return;
315 }
316
317 /* Update object flags with final geometry. */
318 progress.set_status("Updating Objects Flags");
319 object_manager->device_update_flags(device, &dscene, this, progress);
320
321 if (progress.get_cancel() || device->have_error()) {
322 return;
323 }
324
325 /* Update BVH primitive objects with final geometry. */
326 progress.set_status("Updating Primitive Offsets");
327 object_manager->device_update_prim_offsets(device, &dscene, this);
328
329 if (progress.get_cancel() || device->have_error()) {
330 return;
331 }
332
333 /* Images last, as they should be more likely to use host memory fallback than geometry.
334 * Some images may have been uploaded early for displacement already at this point. */
335 progress.set_status("Updating Images");
336 image_manager->device_update(device, this, progress);
337
338 if (progress.get_cancel() || device->have_error()) {
339 return;
340 }
341
342 /* Evaluate volume shader to build volume octrees. */
343 progress.set_status("Updating Volume");
344 volume_manager->device_update(device, &dscene, this, progress);
345
346 if (progress.get_cancel() || device->have_error()) {
347 return;
348 }
349
350 progress.set_status("Updating Camera Volume");
352
353 if (progress.get_cancel() || device->have_error()) {
354 return;
355 }
356
357 progress.set_status("Updating Lookup Tables");
358 lookup_tables->device_update(device, &dscene, this);
359
360 if (progress.get_cancel() || device->have_error()) {
361 return;
362 }
363
364 /* Light manager needs shaders and final meshes for triangles in light tree. */
365 progress.set_status("Updating Lights");
366 light_manager->device_update(device, &dscene, this, progress);
367
368 if (progress.get_cancel() || device->have_error()) {
369 return;
370 }
371
372 progress.set_status("Updating Integrator");
374
375 if (progress.get_cancel() || device->have_error()) {
376 return;
377 }
378
379 progress.set_status("Updating Film");
381
382 if (progress.get_cancel() || device->have_error()) {
383 return;
384 }
385
386 /* Update lookup tables a second time for film tables. */
387 progress.set_status("Updating Lookup Tables");
388 lookup_tables->device_update(device, &dscene, this);
389
390 if (progress.get_cancel() || device->have_error()) {
391 return;
392 }
393
394 progress.set_status("Updating Baking");
395 bake_manager->device_update(device, &dscene, this, progress);
396
397 if (progress.get_cancel() || device->have_error()) {
398 return;
399 }
400
401 if (device->have_error() == false) {
402 dscene.data.volume_stack_size = get_volume_stack_size();
403
404 progress.set_status("Updating Device", "Writing constant memory");
405 device->const_copy_to("data", &dscene.data, sizeof(dscene.data));
406 }
407
409
411 /* Swap current camera/object/vertex positions to previous positions for next frame. */
413 object_manager->update_interactive_motion(this);
414 geometry_manager->update_interactive_motion(this);
415 }
416
417 if (print_stats) {
418 const size_t mem_used = util_guarded_get_mem_used();
419 const size_t mem_peak = util_guarded_get_mem_peak();
420
421 LOG_INFO << "System memory statistics after full device sync:\n"
422 << " Usage: " << string_human_readable_number(mem_used) << " ("
423 << string_human_readable_size(mem_used) << ")\n"
424 << " Peak: " << string_human_readable_number(mem_peak) << " ("
425 << string_human_readable_size(mem_peak) << ")";
426 }
427}
428
430{
431 if (integrator->get_motion_blur()) {
432 return MOTION_BLUR;
433 }
434 const DenoiserPassMask denoiser_motion_passes = DENOISER_PASS_BACKWARD_MOTION |
436 const bool denoiser_motion = (integrator->get_use_denoise()) &&
437 (integrator->get_denoiser_passes() & denoiser_motion_passes) != 0;
438 if (denoiser_motion || (Pass::contains(passes, PASS_MOTION) ||
441 {
443 }
444 return MOTION_NONE;
445}
446
448{
450 return 2.0f;
451 }
452 return camera->get_shuttertime();
453}
454
456{
457 if (std == ATTR_STD_UV) {
459 }
462 {
463 return need_motion() != MOTION_NONE;
464 }
465
466 return false;
467}
468
470{
471 for (int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++) {
473 attributes.add((AttributeStandard)std);
474 }
475 }
476
477 for (const Shader *shader : shaders) {
478 attributes.add(shader->global_attributes);
479 }
480}
481
483{
484 return (need_reset() || film->is_modified());
485}
486
488{
489 return (
490 background->is_modified() || image_manager->need_update() || object_manager->need_update() ||
491 geometry_manager->need_update() || light_manager->need_update() ||
492 lookup_tables->need_update() || integrator->is_modified() || shader_manager->need_update() ||
493 particle_system_manager->need_update() || bake_manager->need_update() ||
495}
496
497bool Scene::need_reset(const bool check_camera)
498{
499 return need_data_update() || (check_camera && camera->is_modified());
500}
501
503{
504 osl_manager->reset(this);
506
507 /* ensure all objects are updated */
512
513 background->tag_update(this);
516 object_manager->tag_update(this, ObjectManager::UPDATE_ALL);
518 light_manager->tag_update(this, LightManager::UPDATE_ALL);
519 particle_system_manager->tag_update(this);
520 procedural_manager->tag_update();
521}
522
524{
525 free_memory(false);
526}
527
529{
530 geometry_manager->collect_statistics(this, stats);
531 image_manager->collect_statistics(stats, this);
532}
533
535{
536 if (!update_stats) {
537 update_stats = make_unique<SceneUpdateStats>();
538 }
539}
540
542{
543 if (!need_update()) {
544 return;
545 }
546
547 /* These features are not being tweaked as often as shaders,
548 * so could be done selective magic for the viewport as well. */
549 uint64_t kernel_features = shader_manager->get_kernel_features(this);
550
551 const bool use_motion = need_motion() == Scene::MotionType::MOTION_BLUR;
552 kernel_features |= KERNEL_FEATURE_PATH_TRACING;
553
554 /* Track the max prim count in case the backend needs to rebuild BVHs or
555 * kernels to support different limits. */
556 size_t kernel_max_prim_count = 0;
557
558 /* Figure out whether the scene will use shader ray-trace we need at least
559 * one caustic light, one caustic caster and one caustic receiver to use
560 * and enable the MNEE code path. */
561 bool has_caustics_receiver = false;
562 bool has_caustics_caster = false;
563 bool has_caustics_light = false;
564
565 for (Object *object : objects) {
566 if (object->get_is_caustics_caster()) {
567 has_caustics_caster = true;
568 }
569 else if (object->get_is_caustics_receiver()) {
570 has_caustics_receiver = true;
571 }
572 Geometry *geom = object->get_geometry();
573 if (use_motion) {
574 if (object->use_motion() || geom->get_use_motion_blur()) {
575 kernel_features |= KERNEL_FEATURE_OBJECT_MOTION;
576 }
577 }
578 if (object->get_is_shadow_catcher() && !geom->is_light()) {
579 kernel_features |= KERNEL_FEATURE_SHADOW_CATCHER;
580 }
581 if (geom->is_hair()) {
582 const Hair *hair = static_cast<const Hair *>(geom);
583 kernel_features |= (hair->curve_shape == CURVE_RIBBON) ? KERNEL_FEATURE_HAIR_RIBBON :
585 kernel_max_prim_count = max(kernel_max_prim_count, hair->num_segments());
586 }
587 else if (geom->is_pointcloud()) {
588 const PointCloud *pointcloud = static_cast<const PointCloud *>(geom);
589 kernel_features |= KERNEL_FEATURE_POINTCLOUD;
590 if (pointcloud->primitive_type() & PRIMITIVE_GSPLAT) {
591 kernel_features |= KERNEL_FEATURE_GSPLATS;
592 }
593 kernel_max_prim_count = max(kernel_max_prim_count, pointcloud->num_points());
594 }
595 else if (geom->is_mesh()) {
596 kernel_max_prim_count = max(kernel_max_prim_count,
597 static_cast<Mesh *>(geom)->num_triangles());
598 }
599 else if (geom->is_light()) {
600 const Light *light = static_cast<const Light *>(object->get_geometry());
601 if (light->get_use_caustics()) {
602 has_caustics_light = true;
603 }
604 }
605 if (object->has_light_linking()) {
606 kernel_features |= KERNEL_FEATURE_LIGHT_LINKING;
607 }
608 if (object->has_shadow_linking()) {
609 kernel_features |= KERNEL_FEATURE_SHADOW_LINKING;
610 }
611 }
612
613 dscene.data.integrator.use_caustics = false;
614 if (device->info.has_mnee() && has_caustics_caster && has_caustics_receiver &&
615 has_caustics_light)
616 {
617 dscene.data.integrator.use_caustics = true;
618 kernel_features |= KERNEL_FEATURE_MNEE;
619 }
620
622 kernel_features |= KERNEL_FEATURE_PATH_GUIDING;
623 }
624
625 if (bake_manager->get_baking()) {
626 kernel_features |= KERNEL_FEATURE_BAKING;
627 }
628
629 kernel_features |= film->get_kernel_features(this);
630 kernel_features |= integrator->get_kernel_features();
631 kernel_features |= camera->get_kernel_features();
632
633 dscene.data.kernel_features = kernel_features;
634
635 /* Currently viewport render is faster with higher max_closures, needs
636 * investigating. */
637 const uint max_closures = (params.background) ? get_max_closure_count() : MAX_CLOSURE;
638 dscene.data.max_closures = max_closures;
639 dscene.data.max_shaders = shaders.size();
640
641 /* Inform the device of the BVH limits. If this returns true, all BVHs
642 * and kernels need to be rebuilt. */
643 if (device->set_bvh_limits(objects.size(), kernel_max_prim_count)) {
644 kernels_loaded = false;
645 for (Geometry *geom : geometry) {
646 geom->need_update_rebuild = true;
647 geom->tag_modified();
648 }
649 }
650}
651
652bool Scene::update(Progress &progress)
653{
654 if (!need_update()) {
655 return false;
656 }
657
658 /* Upload scene data to the GPU. */
659 progress.set_status("Updating Scene");
660 MEM_GUARDED_CALL(&progress, device_update, device, progress);
661
662 return true;
663}
664
665bool Scene::update_camera_resolution(Progress &progress, int width, int height)
666{
667 bool update_data = false;
668
669 if (camera->set_screen_size(width, height)) {
671 update_data = true;
672 }
673
674 if (integrator->get_use_pixel_jitter()) {
675 integrator->tag_use_pixel_jitter_modified();
676
678 update_data = true;
679 }
680
681 if (update_data) {
682 progress.set_status("Updating Device", "Writing constant memory");
683 device->const_copy_to("data", &dscene.data, sizeof(dscene.data));
684 }
685 return update_data;
686}
687
688static void log_kernel_features(const uint64_t features)
689{
690 LOG_INFO << "Requested features:";
691 LOG_INFO << "Use BSDF " << string_from_bool(features & KERNEL_FEATURE_NODE_BSDF);
692 LOG_INFO << "Use Emission " << string_from_bool(features & KERNEL_FEATURE_NODE_EMISSION);
693 LOG_INFO << "Use Volume " << string_from_bool(features & KERNEL_FEATURE_NODE_VOLUME);
694 LOG_INFO << "Use Bump " << string_from_bool(features & KERNEL_FEATURE_NODE_BUMP);
695 LOG_INFO << "Use Voronoi " << string_from_bool(features & KERNEL_FEATURE_NODE_VORONOI_EXTRA);
696 LOG_INFO << "Use Shader Raytrace " << string_from_bool(features & KERNEL_FEATURE_NODE_RAYTRACE);
697 LOG_INFO << "Use MNEE " << string_from_bool(features & KERNEL_FEATURE_MNEE);
698 LOG_INFO << "Use Transparent " << string_from_bool(features & KERNEL_FEATURE_TRANSPARENT);
699 LOG_INFO << "Use Denoising " << string_from_bool(features & KERNEL_FEATURE_DENOISING);
700 LOG_INFO << "Use Path Tracing " << string_from_bool(features & KERNEL_FEATURE_PATH_TRACING);
701 LOG_INFO << "Use Hair " << string_from_bool(features & KERNEL_FEATURE_HAIR);
702 LOG_INFO << "Use Pointclouds " << string_from_bool(features & KERNEL_FEATURE_POINTCLOUD);
703 LOG_INFO << "Use Object Motion " << string_from_bool(features & KERNEL_FEATURE_OBJECT_MOTION);
704 LOG_INFO << "Use Baking " << string_from_bool(features & KERNEL_FEATURE_BAKING);
705 LOG_INFO << "Use Subsurface " << string_from_bool(features & KERNEL_FEATURE_SUBSURFACE);
706 LOG_INFO << "Use Volume " << string_from_bool(features & KERNEL_FEATURE_VOLUME);
707 LOG_INFO << "Use Shadow Catcher " << string_from_bool(features & KERNEL_FEATURE_SHADOW_CATCHER);
708 LOG_INFO << "Use Portal Node " << string_from_bool(features & KERNEL_FEATURE_NODE_PORTAL);
709 LOG_INFO << "Use Light Linking " << string_from_bool(features & KERNEL_FEATURE_LIGHT_LINKING);
710 LOG_INFO << "Use Shadow Linking " << string_from_bool(features & KERNEL_FEATURE_SHADOW_LINKING);
711 LOG_INFO << "Use Gaussian Splats " << string_from_bool(features & KERNEL_FEATURE_GSPLATS);
712}
713
715{
716 progress.set_status("Loading render kernels (may take a few minutes the first time)");
717
718 const scoped_timer timer;
719
720 const uint64_t kernel_features = dscene.data.kernel_features;
721 log_kernel_features(kernel_features);
722 if (!device->load_kernels(kernel_features)) {
723 string message = device->error_message();
724 if (message.empty()) {
725 message = "Failed loading render kernel, see console for errors";
726 }
727
728 progress.set_error(message);
729 progress.set_status(message);
730 progress.set_update();
731 return false;
732 }
733
734 kernels_loaded = true;
735 loaded_kernel_features = kernel_features;
736 return true;
737}
738
740{
741 if (shader_manager->use_osl()) {
742 /* OSL always needs the maximum as we can't predict the
743 * number of closures a shader might generate. */
744 return MAX_CLOSURE;
745 }
746
747 int max_closures = 0;
748 for (int i = 0; i < shaders.size(); i++) {
749 Shader *shader = shaders[i];
750 if (shader->reference_count()) {
751 const int num_closures = shader->graph->get_num_closures();
752 max_closures = max(max_closures, num_closures);
753 }
754 }
756
758 /* This is usually harmless as more complex shader tend to get many
759 * closures discarded due to mixing or low weights. We need to limit
760 * to MAX_CLOSURE as this is hardcoded in CPU/mega kernels, and it
761 * avoids excessive memory usage for split kernels. */
762 LOG_WARNING << "Maximum number of closures exceeded: " << max_closure_global << " > "
763 << MAX_CLOSURE;
764
766 }
767
768 return max_closure_global;
769}
770
772{
773 int volume_stack_size = 0;
774
775 /* Space for background volume and terminator.
776 * Don't do optional here because camera ray initialization expects that there
777 * is space for at least those elements (avoiding extra condition to check if
778 * there is actual volume or not).
779 */
780 volume_stack_size += 2;
781
782 /* Quick non-expensive check. Can over-estimate maximum possible nested level,
783 * but does not require expensive calculation during pre-processing. */
784 bool has_volume_object = false;
785 for (const Object *object : objects) {
786 if (!object->get_geometry()->has_volume) {
787 continue;
788 }
789
790 if (object->intersects_volume) {
791 /* Object intersects another volume, assume it's possible to go deeper in
792 * the stack. */
793 /* TODO(sergey): This might count nesting twice (A intersects B and B
794 * intersects A), but can't think of a computationally cheap algorithm.
795 * Dividing my 2 doesn't work because of Venn diagram example with 3
796 * circles. */
797 ++volume_stack_size;
798 }
799 else if (!has_volume_object) {
800 /* Allocate space for at least one volume object. */
801 ++volume_stack_size;
802 }
803
804 has_volume_object = true;
805
806 if (volume_stack_size == MAX_VOLUME_STACK_SIZE) {
807 break;
808 }
809 }
810
811 volume_stack_size = min(volume_stack_size, MAX_VOLUME_STACK_SIZE);
812
813 LOG_DEBUG << "Detected required volume stack size " << volume_stack_size;
814
815 return volume_stack_size;
816}
817
819{
821 has_shadow_catcher_ = false;
822 for (Object *object : objects) {
823 /* Shadow catcher flags on lights only controls effect on other objects, it's
824 * not catching shadows itself. This is on by default, so ignore to avoid
825 * performance impact when there is no actual shadow catcher. */
826 if (object->get_is_shadow_catcher() && !object->get_geometry()->is_light()) {
827 has_shadow_catcher_ = true;
828 break;
829 }
830 }
831
833 }
834
835 return has_shadow_catcher_;
836}
837
842
844{
845 has_volume_modified_ = false;
846 return dscene.data.integrator.use_volumes;
847}
848
850{
852}
853
858
860{
861 for (const Object *object : objects) {
862 if (!object->get_geometry()->is_light()) {
863 continue;
864 }
865
866 const Light *light = static_cast<const Light *>(object->get_geometry());
867 if (light->get_is_enabled() && light->get_use_mis() && light->is_traceable()) {
868 return true;
869 }
870 }
871
872 return false;
873}
874
875template<class T> T *Scene::create_light_node()
876{
877 unique_ptr<T> node = make_unique<T>();
878 T *node_ptr = node.get();
879 node->set_owner(this);
880 geometry.push_back(std::move(node));
881 light_manager->tag_update(this, LightManager::LIGHT_ADDED);
882 return node_ptr;
883}
884
889
894
899
904
909
911{
912 unique_ptr<Mesh> node = make_unique<Mesh>();
913 Mesh *node_ptr = node.get();
914 node->set_owner(this);
915 geometry.push_back(std::move(node));
917 return node_ptr;
918}
919
921{
922 unique_ptr<Hair> node = make_unique<Hair>();
923 Hair *node_ptr = node.get();
924 node->set_owner(this);
925 geometry.push_back(std::move(node));
927 return node_ptr;
928}
929
931{
932 unique_ptr<Volume> node = make_unique<Volume>();
933 Volume *node_ptr = node.get();
934 node->set_owner(this);
935 geometry.push_back(std::move(node));
937 return node_ptr;
938}
939
941{
942 unique_ptr<PointCloud> node = make_unique<PointCloud>();
943 PointCloud *node_ptr = node.get();
944 node->set_owner(this);
945 geometry.push_back(std::move(node));
947 return node_ptr;
948}
949
951{
952 unique_ptr<Object> node = make_unique<Object>();
953 Object *node_ptr = node.get();
954 node->set_owner(this);
955 objects.push_back(std::move(node));
957 return node_ptr;
958}
959
961{
962 unique_ptr<ParticleSystem> node = make_unique<ParticleSystem>();
963 ParticleSystem *node_ptr = node.get();
964 node->set_owner(this);
965 particle_systems.push_back(std::move(node));
966 particle_system_manager->tag_update(this);
967 return node_ptr;
968}
969
971{
972 unique_ptr<Shader> node = make_unique<Shader>();
973 Shader *node_ptr = node.get();
974 node->set_owner(this);
975 shaders.push_back(std::move(node));
977 return node_ptr;
978}
979
981{
982 unique_ptr<Pass> node = make_unique<Pass>();
983 Pass *node_ptr = node.get();
984 node->set_owner(this);
985 passes.push_back(std::move(node));
987 return node_ptr;
988}
989
991{
992 unique_ptr<Camera> node = make_unique<Camera>();
993 Camera *node_ptr = node.get();
994 node->set_owner(this);
995 cameras.push_back(std::move(node));
996 return node_ptr;
997}
998
1000{
1001 unique_ptr<Integrator> node = make_unique<Integrator>();
1002 Integrator *node_ptr = node.get();
1003 node->set_owner(this);
1004 integrators.push_back(std::move(node));
1005 return node_ptr;
1006}
1007
1009{
1010 unique_ptr<SceneAttributes> node = make_unique<SceneAttributes>();
1011 SceneAttributes *node_ptr = node.get();
1012 node->set_owner(this);
1013 scene_attributes.push_back(std::move(node));
1014 return node_ptr;
1015}
1016
1018{
1019 unique_ptr<Background> node = make_unique<Background>();
1020 Background *node_ptr = node.get();
1021 node->set_owner(this);
1022 backgrounds.push_back(std::move(node));
1023 return node_ptr;
1024}
1025
1027{
1028 unique_ptr<Film> node = make_unique<Film>();
1029 Film *node_ptr = node.get();
1030 node->set_owner(this);
1031 films.push_back(std::move(node));
1032 return node_ptr;
1033}
1034
1036{
1037 assert(node->get_owner() == this);
1039 light_manager->tag_update(this, LightManager::LIGHT_REMOVED);
1040}
1041
1043{
1044 assert(node->get_owner() == this);
1047}
1048
1050{
1051 assert(node->get_owner() == this);
1054}
1055
1057{
1058 assert(node->get_owner() == this);
1061}
1062
1064{
1065 assert(node->get_owner() == this);
1068}
1069
1071{
1072 assert(node->get_owner() == this);
1073
1074 uint flag;
1075 if (node->is_hair()) {
1077 }
1078 else {
1080 if (node->has_volume) {
1081 volume_manager->tag_update({node});
1082 }
1083 }
1084
1086 geometry_manager->tag_update(this, flag);
1087}
1088
1090{
1091 assert(node->get_owner() == this);
1092
1094 if (node->get_geometry()->has_volume) {
1095 volume_manager->tag_update({node}, flag);
1096 }
1097
1099 object_manager->tag_update(this, flag);
1100}
1101
1103{
1104 assert(node->get_owner() == this);
1106 particle_system_manager->tag_update(this);
1107}
1108
1110{
1111 assert(node->get_owner() == this);
1112 /* don't delete unused shaders, not supported */
1113 node->clear_reference_count();
1114}
1115
1117{
1118 assert(node->get_owner() == this);
1120 procedural_manager->tag_update();
1121}
1122
1124{
1125 assert(node->get_owner() == this);
1127 film->tag_modified();
1128}
1129
1130template<typename T> static void assert_same_owner(const set<T *> &nodes, const NodeOwner *owner)
1131{
1132#ifdef NDEBUG
1133 (void)nodes;
1134 (void)owner;
1135#else
1136 for (const T *node : nodes) {
1137 assert(node->get_owner() == owner);
1138 }
1139#endif
1140}
1141
1142template<> void Scene::delete_nodes(const set<Geometry *> &nodes, const NodeOwner *owner)
1143{
1144 assert_same_owner(nodes, owner);
1145 volume_manager->tag_update(nodes);
1146 geometry.erase_in_set(nodes);
1148 light_manager->tag_update(this, LightManager::LIGHT_REMOVED);
1149}
1150
1151template<> void Scene::delete_nodes(const set<Object *> &nodes, const NodeOwner *owner)
1152{
1153 assert_same_owner(nodes, owner);
1155 objects.erase_in_set(nodes);
1157}
1158
1159template<> void Scene::delete_nodes(const set<ParticleSystem *> &nodes, const NodeOwner *owner)
1160{
1161 assert_same_owner(nodes, owner);
1163 particle_system_manager->tag_update(this);
1164}
1165
1166template<> void Scene::delete_nodes(const set<Shader *> &nodes, const NodeOwner * /*owner*/)
1167{
1168 /* don't delete unused shaders, not supported */
1169 for (Shader *shader : nodes) {
1170 shader->clear_reference_count();
1171 }
1172}
1173
1174template<> void Scene::delete_nodes(const set<Procedural *> &nodes, const NodeOwner *owner)
1175{
1176 assert_same_owner(nodes, owner);
1178 procedural_manager->tag_update();
1179}
1180
1181template<> void Scene::delete_nodes(const set<Pass *> &nodes, const NodeOwner *owner)
1182{
1183 assert_same_owner(nodes, owner);
1184 passes.erase_in_set(nodes);
1185 film->tag_modified();
1186}
1187
1188/* Template instantiations so we don't have to inline functions. */
1194
unsigned long long int uint64_t
btScalar max
void add(ustring name)
void device_update(Device *device, DeviceScene *dscene, Scene *scene)
void device_free(Device *device, DeviceScene *dscene)
void tag_update(Scene *scene)
void update_interactive_motion()
void device_update_volume(Device *device, DeviceScene *dscene, Scene *scene)
bool set_screen_size(int width, int height)
void device_free(Device *device, DeviceScene *dscene, Scene *scene)
void device_update(Device *device, DeviceScene *dscene, Scene *scene)
uint64_t get_kernel_features() const
bool has_mnee() const
KernelData data
virtual void optimize_for_scene(Scene *)
virtual const string & error_message()
virtual void const_copy_to(const char *name, void *host, const size_t size)=0
DeviceInfo info
virtual bool set_bvh_limits(size_t, size_t)
virtual bool load_kernels(const uint64_t)
bool have_error()
Definition film.h:29
uint64_t get_kernel_features(const Scene *scene) const
Definition film.cpp:811
void update_passes(Scene *scene)
Definition film.cpp:539
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
bool update_lightgroups(Scene *scene)
Definition film.cpp:519
bool is_light() const
bool is_pointcloud() const
bool is_hair() const
bool is_mesh() const
Definition hair.h:13
size_t num_segments() const
Definition hair.h:114
CurveShapeType curve_shape
Definition hair.h:77
void tag_update(Scene *scene, const uint32_t flag)
void device_update(Device *device, DeviceScene *dscene, Scene *scene)
uint64_t get_kernel_features() const
void device_free(Device *device, DeviceScene *dscene, bool force_free=false)
GuidingParams get_guiding_params(const Device *device) const
bool is_modified() const
virtual bool is_traceable() const =0
Definition pass.h:50
static bool contains(const unique_ptr_vector< Pass > &passes, PassType type)
Definition pass.cpp:404
size_t num_points() const
PrimitiveType primitive_type() const override
bool get_cancel() const
Definition progress.h:77
void set_status(const string &status_, const string &substatus_="")
Definition progress.h:248
void set_update()
Definition progress.h:306
void set_error(const string &error_message_)
Definition progress.h:98
void device_free(Device *device, DeviceScene *dscene, bool force_free=false)
bool is_modified() const
void device_update(Device *device, DeviceScene *dscene, Scene *scene)
void tag_update(Scene *scene, const uint32_t flag)
bool background
Definition scene.h:91
ShadingSystem shadingsystem
Definition scene.h:65
bool has_volume_modified() const
Definition scene.cpp:849
bool kernels_loaded
Definition scene.h:270
unique_ptr_vector< Procedural > procedurals
Definition scene.h:163
Film * film
Definition scene.h:147
string name
Definition scene.h:136
unique_ptr< ObjectManager > object_manager
Definition scene.h:171
bool need_global_attribute(AttributeStandard std) const
Definition scene.cpp:455
MotionType need_motion() const
Definition scene.cpp:429
void need_global_attributes(AttributeRequestSet &attributes)
Definition scene.cpp:469
bool update(Progress &progress)
Definition scene.cpp:652
unique_ptr< LightManager > light_manager
Definition scene.h:167
T * create_light_node()
Definition scene.cpp:875
Device * device
Definition scene.h:186
unique_ptr< ParticleSystemManager > particle_system_manager
Definition scene.h:172
int get_max_closure_count()
Definition scene.cpp:739
bool scene_updated_while_loading_kernels
Definition scene.h:194
void device_free()
Definition scene.cpp:523
bool has_shadow_catcher_
Definition scene.h:275
Shader * default_volume
Definition scene.h:179
Camera * dicing_camera
Definition scene.h:146
SceneParams params
Definition scene.h:190
unique_ptr_vector< Geometry > geometry
Definition scene.h:161
Shader * default_surface
Definition scene.h:178
unique_ptr< SceneUpdateStats > update_stats
Definition scene.h:197
bool has_volume_modified_
Definition scene.h:277
void tag_shadow_catcher_modified()
Definition scene.cpp:838
void free_memory(bool final)
Definition scene.cpp:90
unique_ptr_vector< Camera > cameras
Definition scene.h:157
Shader * default_empty
Definition scene.h:182
unique_ptr_vector< SceneAttributes > scene_attributes
Definition scene.h:156
unique_ptr_vector< Background > backgrounds
Definition scene.h:153
void enable_update_stats()
Definition scene.cpp:534
Shader * default_background
Definition scene.h:181
T * create_node(Args &&...)=delete
unique_ptr< BakeManager > bake_manager
Definition scene.h:173
bool shadow_catcher_modified_
Definition scene.h:276
Background * background
Definition scene.h:148
void tag_has_volume_modified()
Definition scene.cpp:854
unique_ptr_vector< Shader > shaders
Definition scene.h:158
PointLight * create_node()
Definition scene.cpp:885
MotionType
Definition scene.h:207
@ MOTION_PASS
Definition scene.h:207
@ MOTION_NONE
Definition scene.h:207
@ MOTION_BLUR
Definition scene.h:207
@ MOTION_PASS_INTERACTIVE
Definition scene.h:207
bool has_volume()
Definition scene.cpp:843
void update_kernel_features()
Definition scene.cpp:541
unique_ptr< ShaderManager > shader_manager
Definition scene.h:169
void delete_nodes(const set< T * > &nodes)
Definition scene.h:251
Camera * camera
Definition scene.h:145
unique_ptr_vector< ParticleSystem > particle_systems
Definition scene.h:160
bool has_shadow_catcher()
Definition scene.cpp:818
unique_ptr< GeometryManager > geometry_manager
Definition scene.h:170
void delete_node(T *node)=delete
~Scene() override
Definition scene.cpp:85
bool use_light_mis() const
Definition scene.cpp:859
unique_ptr< OSLManager > osl_manager
Definition scene.h:168
unique_ptr_vector< Integrator > integrators
Definition scene.h:155
bool load_kernels(Progress &progress)
Definition scene.cpp:714
int max_closure_global
Definition scene.h:280
unique_ptr_vector< Object > objects
Definition scene.h:162
Integrator * integrator
Definition scene.h:149
uint64_t loaded_kernel_features
Definition scene.h:271
thread_mutex mutex
Definition scene.h:193
bool need_data_update()
Definition scene.cpp:487
int get_volume_stack_size() const
Definition scene.cpp:771
unique_ptr< ImageManager > image_manager
Definition scene.h:166
unique_ptr< ProceduralManager > procedural_manager
Definition scene.h:174
bool need_reset(const bool check_camera=true)
Definition scene.cpp:497
SceneAttributes * scene_attribute
Definition scene.h:150
bool need_update()
Definition scene.cpp:482
Shader * default_light
Definition scene.h:180
unique_ptr< BVH > bvh
Definition scene.h:142
bool update_camera_resolution(Progress &progress, int width, int height)
Definition scene.cpp:665
void reset()
Definition scene.cpp:502
void collect_statistics(RenderStats *stats)
Definition scene.cpp:528
unique_ptr_vector< Pass > passes
Definition scene.h:159
unique_ptr_vector< Film > films
Definition scene.h:154
DeviceScene dscene
Definition scene.h:187
float motion_shutter_time()
Definition scene.cpp:447
unique_ptr< VolumeManager > volume_manager
Definition scene.h:175
unique_ptr< LookupTables > lookup_tables
Definition scene.h:143
void device_update(Device *device, Progress &progress)
Definition scene.cpp:176
static unique_ptr< ShaderManager > create(const int shadingsystem)
static void add_default(Scene *scene)
NODE_DECLARE unique_ptr< ShaderGraph > graph
void push_back(unique_ptr< T > &&value)
void erase_by_swap(const size_t index)
size_t size() const
void erase_in_set(const set< T * > &values)
#define printf
int DenoiserPassMask
Definition denoise.h:37
@ DENOISER_PASS_BACKWARD_MOTION
Definition denoise.h:33
@ DENOISER_PASS_SPECULAR_MOTION
Definition denoise.h:34
#define KERNEL_FEATURE_VOLUME
#define KERNEL_FEATURE_HAIR_RIBBON
#define KERNEL_FEATURE_NODE_PORTAL
#define MAX_VOLUME_STACK_SIZE
#define KERNEL_FEATURE_SHADOW_CATCHER
#define KERNEL_FEATURE_OBJECT_MOTION
#define KERNEL_FEATURE_PATH_GUIDING
#define KERNEL_FEATURE_TRANSPARENT
#define KERNEL_FEATURE_SUBSURFACE
#define KERNEL_FEATURE_NODE_BSDF
#define KERNEL_FEATURE_HAIR_THICK
#define KERNEL_FEATURE_NODE_VORONOI_EXTRA
#define KERNEL_FEATURE_NODE_VOLUME
#define KERNEL_FEATURE_PATH_TRACING
#define KERNEL_FEATURE_GSPLATS
#define MAX_CLOSURE
#define KERNEL_FEATURE_SHADOW_LINKING
#define KERNEL_FEATURE_DENOISING
#define KERNEL_FEATURE_LIGHT_LINKING
#define KERNEL_FEATURE_HAIR
#define KERNEL_FEATURE_NODE_RAYTRACE
#define KERNEL_FEATURE_BAKING
#define KERNEL_FEATURE_MNEE
#define KERNEL_FEATURE_POINTCLOUD
#define KERNEL_FEATURE_NODE_EMISSION
#define KERNEL_FEATURE_NODE_BUMP
#define CCL_NAMESPACE_END
#define assert(assertion)
size_t util_guarded_get_mem_used()
size_t util_guarded_get_mem_peak()
@ PRIMITIVE_GSPLAT
@ CURVE_RIBBON
@ PASS_UV
@ PASS_MOTION
@ PASS_DENOISING_BACKWARD_MOTION
@ PASS_DENOISING_SPECULAR_MOTION
AttributeStandard
@ ATTR_STD_UV
@ ATTR_STD_VOLUME_VELOCITY_Y
@ ATTR_STD_NONE
@ ATTR_STD_VOLUME_VELOCITY_Z
@ ATTR_STD_NUM
@ ATTR_STD_VOLUME_VELOCITY
@ ATTR_STD_VOLUME_VELOCITY_X
#define LOG_DEBUG
Definition log.h:107
#define LOG_WARNING
Definition log.h:103
#define LOG_INFO
Definition log.h:106
#define T
#define MEM_GUARDED_CALL(progress, func,...)
@ SHADINGSYSTEM_SVM
static void assert_same_owner(const set< T * > &nodes, const NodeOwner *owner)
Definition scene.cpp:1130
static void log_kernel_features(const uint64_t features)
Definition scene.cpp:688
#define min(a, b)
Definition sort.cc:38
string string_from_bool(bool var)
Definition string.cpp:185
string string_human_readable_size(size_t size)
Definition string.cpp:333
string string_human_readable_number(size_t num)
Definition string.cpp:352
int reference_count() const
Definition graph/node.h:184
void tag_modified()
bool is_modified() const