Blender V5.3
blenkernel/intern/compositor.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <limits>
10#include <string>
11
12#include <fmt/format.h>
13
14#include "BLI_enum_flags.hh"
15#include "BLI_index_range.hh"
16#include "BLI_listbase.hh"
17#include "BLI_math_base.hh"
18#include "BLI_set.hh"
19#include "BLI_string_ref.hh"
20#include "BLI_string_utf8.hh"
21#include "BLI_string_utils.hh"
22
23#include "BLT_translation.hh"
24
25#include "DNA_layer_types.h"
26#include "DNA_node_types.h"
27#include "DNA_object_enums.h"
28#include "DNA_scene_types.h"
29#include "DNA_space_types.h"
30#include "DNA_view3d_types.h"
32
33#include "RNA_access.hh"
34#include "RNA_path.hh"
35#include "RNA_prototypes.hh"
36
37#include "BLO_read_write.hh"
38
39#include "BKE_anim_data.hh"
40#include "BKE_animsys.hh"
41#include "BKE_compositor.hh"
44#include "BKE_context.hh"
45#include "BKE_cryptomatte.hh"
46#include "BKE_global.hh"
47#include "BKE_idprop.hh"
48#include "BKE_lib_id.hh"
49#include "BKE_lib_query.hh"
50#include "BKE_node.hh"
52#include "BKE_node_runtime.hh"
54
55#include "DEG_depsgraph.hh"
58
59#include "WM_api.hh"
60
61#include "IMB_imbuf.hh"
62
63#include "NOD_dependencies.hh"
64
66
67/* --------------------------------------------------------------------
68 * Cache.
69 */
70
72{
73 this->clear_frames();
74}
75
76ImBuf *Cache::get_frame(const int frame_number, const int view_identifier)
77{
78 std::scoped_lock lock{frames_mutex_};
79 const FrameKey key = FrameKey(frame_number, view_identifier);
80 ImBuf *cached_frame = this->frames_.lookup_try(key).value_or(nullptr);
81 if (!cached_frame) {
82 return nullptr;
83 }
84
85 IMB_refImBuf(cached_frame);
86 return cached_frame;
87}
88
89void Cache::add_frame(const int frame_number, const int view_identifier, ImBuf *image_buffer)
90{
91 std::scoped_lock lock{frames_mutex_};
92 /* First evict frames if needed to maintain the memory cache limit. In almost all cases, the
93 * while loop will run exactly once, since the images in the cache will almost always have the
94 * same size, so one goes out, one goes in. So we needn't worry about performance. */
95 const int64_t cache_limit = size_t(U.memcachelimit) * 1024 * 1024;
96 const int64_t image_size = IMB_get_size_in_memory(image_buffer);
97 while (!this->frames_.is_empty() && this->size() + image_size > cache_limit) {
98 this->evict_frame(frame_number);
99 }
100
101 this->frames_.add_new(FrameKey(frame_number, view_identifier), image_buffer);
102}
103
105{
106 std::scoped_lock lock{frames_mutex_};
107 for (ImBuf *image_buffer : this->frames_.values()) {
108 IMB_freeImBuf(image_buffer);
109 }
110 this->frames_.clear();
111}
112
114{
115 /* Compute a sorted vector of all cached frames. */
116 VectorSet<int> frame_numbers_set;
117 {
118 std::scoped_lock lock{frames_mutex_};
119 frame_numbers_set.reserve(this->frames_.size());
120 for (const FrameKey &key : this->frames_.keys()) {
121 frame_numbers_set.add(key.frame_number);
122 }
123 }
124 Vector<int> frame_numbers = frame_numbers_set.extract_vector();
125 std::ranges::sort(frame_numbers);
126
127 Vector<FrameRange> frame_ranges;
128 for (const int frame : frame_numbers) {
129 /* We start a new range by appending a singleton range of the current frame, either because
130 * this is the first range or because the last range will not be contiguous with the current
131 * frame. */
132 if (frame_ranges.is_empty() || frame - frame_ranges.last().end > 1) {
133 frame_ranges.append(FrameRange(frame, frame));
134 }
135 else {
136 /* Otherwise, the frame is contiguous with the last range, so we just grow its size by 1. */
137 frame_ranges.last().end++;
138 }
139 }
140
141 return frame_ranges;
142}
143
144void Cache::evict_frame(const int current_frame_number)
145{
146 if (this->frames_.is_empty()) {
147 return;
148 }
149
150 /* Find the keys with the maximum and minimum frame numbers. */
151 FrameKey minimum_key = FrameKey(std::numeric_limits<int>::max());
152 FrameKey maximum_key = FrameKey(std::numeric_limits<int>::lowest());
153 for (const FrameKey &key : this->frames_.keys()) {
154 if (key.frame_number < minimum_key.frame_number) {
155 minimum_key = key;
156 }
157 if (key.frame_number > maximum_key.frame_number) {
158 maximum_key = key;
159 }
160 }
161
162 /* Prioritize evicting frames that are behind the current frame and are furthest from it. */
163 if (minimum_key.frame_number < current_frame_number) {
164 IMB_freeImBuf(this->frames_.pop(minimum_key));
165 return;
166 }
167
168 /* Otherwise, evict the frame that is after the current frame and is furthest from it. */
169 IMB_freeImBuf(this->frames_.pop(maximum_key));
170}
171
172int64_t Cache::size()
173{
174 int64_t size = 0;
175 for (ImBuf *image_buffer : this->frames_.values()) {
176 size += IMB_get_size_in_memory(image_buffer);
177 }
178 return size;
179}
180
181/* --------------------------------------------------------------------
182 * Scene Compositor Effects.
183 */
184
185bool is_enabled(const Scene &scene, const ExecutionMode mode)
186{
187 if (mode == ExecutionMode::Render && !(scene.r.scemode & R_DOCOMP)) {
188 return false;
189 }
190
191 for (SceneCompositorEffect &effect : scene.compositor_effects) {
192 if (is_effect_enabled(effect, mode)) {
193 return true;
194 }
195 }
196
197 return false;
198}
199
201{
202 return static_cast<SceneCompositorEffect *>(BLI_findstring(
203 &(scene.compositor_effects), name.data(), offsetof(SceneCompositorEffect, name)));
204}
205
207{
208 for (SceneCompositorEffect &effect : scene.compositor_effects) {
209 if (flag_is_set(effect.flags, SceneCompositorEffectFlags::IsActive)) {
210 return &effect;
211 }
212 }
213
214 return nullptr;
215}
216
218{
219 const bNodeTree *original_node_group = DEG_get_original(effect.node_group);
220 if (!original_node_group || ID_MISSING(original_node_group)) {
221 return false;
222 }
223
224 switch (mode) {
226 return flag_is_set(effect.flags, SceneCompositorEffectFlags::EnableForRender);
228 return flag_is_set(effect.flags, SceneCompositorEffectFlags::EnableForPreview);
229 }
230
232 return false;
233}
234
236{
237 for (SceneCompositorEffect &other_effect : scene.compositor_effects) {
238 other_effect.flags &= ~SceneCompositorEffectFlags::IsActive;
239 }
240
241 /* Activate the active state of the effect. */
243}
244
246 SceneCompositorEffect &effect,
247 StringRef new_name,
248 const bool update_animation_data)
249{
250 std::string old_name = effect.name;
251 new_name.copy_utf8_truncated(effect.name);
253 &effect,
254 CTX_DATA_(BLT_I18NCONTEXT_ID_SCENE, "Scene Effect"),
255 '.',
257 sizeof(effect.name));
258
259 if (!update_animation_data) {
260 return;
261 }
262
263 /* Fix all the animation data which may link to this. */
265 "compositor_effects",
266 RNA_path_name_to_infix(old_name.c_str()),
268 true,
269 *G_MAIN);
270}
271
273{
274 SceneCompositorEffect &effect = *MEM_new<SceneCompositorEffect>("Scene Compositor Effect");
275 rename_effect(scene, effect, name, false);
276 BLI_addtail(&scene.compositor_effects, &effect);
277 set_active_effect(scene, effect);
278 return effect;
279}
280
282{
283 SceneCompositorEffect &new_effect = *MEM_dupalloc(&source_effect);
284 if (source_effect.node_group) {
286 }
287 if (source_effect.system_properties) {
289 }
291 rename_effect(scene, new_effect, source_effect.name, false);
293 return new_effect;
294}
295
296static void free_effect(SceneCompositorEffect &effect, const bool decrement_user_count)
297{
298 if (decrement_user_count && effect.node_group) {
299 id_us_min(&effect.node_group->id);
300 }
301
302 if (effect.system_properties) {
303 IDP_FreeProperty_ex(effect.system_properties, decrement_user_count);
304 }
305
306 MEM_delete(&effect);
307}
308
310{
311 BLI_remlink(&scene.compositor_effects, &effect);
312
313 const bool was_active = flag_is_set(effect.flags, SceneCompositorEffectFlags::IsActive);
314 if (was_active && !scene.compositor_effects.is_empty()) {
315 set_active_effect(scene, *scene.compositor_effects.begin());
316 }
317
318 free_effect(effect, true);
319}
320
321void copy_effects(Scene &target_scene, const Scene &source_scene, const int flags)
322{
323 target_scene.compositor_effects.clear_no_delete();
324 for (const SceneCompositorEffect &source_effect : source_scene.compositor_effects) {
325 SceneCompositorEffect &new_effect = *MEM_dupalloc(&source_effect);
327 if (source_effect.system_properties) {
329 }
330 }
331}
332
333void free_effects(Scene &scene)
334{
335 for (SceneCompositorEffect &effect : scene.compositor_effects.items_mutable()) {
336 free_effect(effect, false);
337 }
338 scene.compositor_effects.clear_no_delete();
339}
340
342{
343 for (SceneCompositorEffect &effect : scene.compositor_effects.items_reversed_mutable()) {
344 remove_effect(scene, effect);
345 }
346}
347
349{
350 for (SceneCompositorEffect &effect : scene.compositor_effects) {
352 if (effect.system_properties) {
354 &data,
356 effect.system_properties, IDP_TYPE_FILTER_ID, [&](IDProperty *property) {
357 BKE_lib_query_idpropertiesForeachIDLink_callback(property, &data);
358 }));
359 }
360 }
361}
362
363void write_effects(const Scene &scene, BlendWriter &writer)
364{
366 for (const SceneCompositorEffect &effect : scene.compositor_effects) {
367 if (effect.system_properties) {
368 IDP_BlendWrite(&writer, effect.system_properties);
369 }
370 }
371}
372
373void read_effects(Scene &scene, BlendDataReader &reader)
374{
376 for (SceneCompositorEffect &effect : scene.compositor_effects) {
378 IDP_BlendDataRead(&reader, &effect.system_properties);
379 }
380}
381
383{
384 const std::optional<AncestorPointerRNA> effect_ptr = RNA_struct_search_closest_ancestor_by_type(
385 &property_ptr, RNA_SceneCompositorEffect);
386 if (effect_ptr.has_value()) {
387 return static_cast<const SceneCompositorEffect *>(effect_ptr->data);
388 }
389
390 const Scene *scene = id_cast<const Scene *>(property_ptr.owner_id);
391 for (SceneCompositorEffect &effect : scene->compositor_effects) {
392 bool found = false;
393 IDP_foreach_property(effect.system_properties, 0, [&](IDProperty *id_property) {
394 if (id_property == property_ptr.data) {
395 found = true;
396 }
397 });
398 if (found) {
399 return &effect;
400 }
401 }
402 return nullptr;
403}
404
406{
407 if (!effect.system_properties) {
408 effect.system_properties =
409 bke::idprop::create_group("SceneCompositorEffectProperties").release();
410 }
411
412 /* In case the node group is missing, do not update the properties to avoid the values reverting
413 * to their default value if the node group later becomes available. */
414 if (!effect.node_group || ID_MISSING(effect.node_group)) {
415 return;
416 }
417
419 &scene.id, RNA_SceneCompositorEffectProperties, &effect);
421
424}
425
426/* --------------------------------------------------------------------
427 * Query.
428 */
429
430/* Adds the pass names of the passes used by the given Render Layer node to the given used passes.
431 * This essentially adds the pass names of the outputs that are logically linked. */
433{
434 for (const bNodeSocket *output : node->output_sockets()) {
435 if (output->is_logically_linked()) {
436 /* The combined pass is aliased as Image and Alpha is generated by the node based on the
437 * combined pass. */
438 if (output->identifier == StringRef("Image") || output->identifier == StringRef("Alpha")) {
439 used_passes.add(RE_PASSNAME_COMBINED);
440 }
441 else {
442 used_passes.add(output->identifier);
443 }
444 }
445 }
446}
447
448/* Adds the pass names of the passes used by the given Group Input node to the given used passes.
449 * The Group Input node only uses the combined pass for the first input, while the rest are
450 * ignored. */
452{
453 /* Only the virtual socket exists, so no pass is used. */
454 if (node->output_sockets().size() == 1) {
455 return;
456 }
457
458 if (!node->output_sockets()[0]->is_logically_linked()) {
459 return;
460 }
461
462 used_passes.add(RE_PASSNAME_COMBINED);
463}
464
465/* Adds the pass names of all Cryptomatte layers needed by the given node to the given used passes.
466 * Only passes in the given viewer layers are added. */
468 const ViewLayer *view_layer,
469 Set<std::string> &used_passes)
470{
472 return;
473 }
474
475 Scene *scene = reinterpret_cast<Scene *>(node->id);
476 if (!scene) {
477 return;
478 }
479
481 BKE_cryptomatte_init_from_scene(scene, false));
482
484 if (layer_names.is_empty()) {
485 return;
486 }
487
488 /* If the stored layer name doesn't corresponds to an existing Cryptomatte layer, fall back to
489 * the name of the first layer. */
490 const NodeCryptomatte *data = static_cast<NodeCryptomatte *>(node->storage);
491 const std::string layer_name = layer_names.contains(data->layer_name) ? data->layer_name :
492 layer_names[0];
493
494 /* Does not use passes from the given view layer, so no need to add anything. */
495 if (!StringRef(layer_name).startswith(view_layer->name)) {
496 return;
497 }
498
499 /* Find out which type of Cryptomatte layers the node needs. Also ensure the type is enabled in
500 * the view layer, because the node can use one of the types as a placeholder. */
501 const char *cryptomatte_type_name = nullptr;
502 if (StringRef(layer_name).endswith(RE_PASSNAME_CRYPTOMATTE_OBJECT)) {
504 cryptomatte_type_name = RE_PASSNAME_CRYPTOMATTE_OBJECT;
505 }
506 }
507 else if (StringRef(layer_name).endswith(RE_PASSNAME_CRYPTOMATTE_ASSET)) {
509 cryptomatte_type_name = RE_PASSNAME_CRYPTOMATTE_ASSET;
510 }
511 }
512 else if (StringRef(layer_name).endswith(RE_PASSNAME_CRYPTOMATTE_MATERIAL)) {
514 cryptomatte_type_name = RE_PASSNAME_CRYPTOMATTE_MATERIAL;
515 }
516 }
517
518 if (!cryptomatte_type_name) {
519 return;
520 }
521
522 /* Each layer stores two ranks/levels, so do ceiling division by two. */
523 const int cryptomatte_layers_count = int(math::ceil(view_layer->cryptomatte_levels / 2.0f));
524 for (const int i : IndexRange(cryptomatte_layers_count)) {
525 used_passes.add(fmt::format("{}{:02}", cryptomatte_type_name, i));
526 }
527}
528
529/* Adds the pass names of the passes used by the given compositor node tree to the given used
530 * passes. This is called recursively for node groups. */
531static void add_used_passes_recursive(const bNodeTree *node_tree,
532 const ViewLayer *view_layer,
533 const bool is_root_tree_of_first_effect,
534 Set<const bNodeTree *> &node_trees_already_searched,
535 Set<std::string> &used_passes)
536{
537 if (node_tree == nullptr) {
538 return;
539 }
540
541 node_tree->ensure_topology_cache();
542 for (const bNode *node : node_tree->all_nodes()) {
543 if (node->is_muted()) {
544 continue;
545 }
546
547 switch (node->type_legacy) {
548 case NODE_GROUP:
549 case NODE_CUSTOM_GROUP: {
550 const bNodeTree *node_group_tree = reinterpret_cast<const bNodeTree *>(node->id);
551 if (node_trees_already_searched.add(node_group_tree)) {
553 node_group_tree, view_layer, false, node_trees_already_searched, used_passes);
554 }
555 break;
556 }
559 break;
560 case NODE_GROUP_INPUT:
561 if (is_root_tree_of_first_effect) {
563 }
564 break;
566 add_passes_used_by_cryptomatte_node(node, view_layer, used_passes);
567 break;
568 default:
569 break;
570 }
571 }
572}
573
575 const ViewLayer *view_layer,
576 const ExecutionMode mode)
577{
578 Set<std::string> used_passes;
579 bool is_first_effect = true;
580 Set<const bNodeTree *> node_trees_already_searched;
581 for (const SceneCompositorEffect &effect : scene.compositor_effects) {
582 if (!is_effect_enabled(effect, mode)) {
583 continue;
584 }
586 effect.node_group, view_layer, is_first_effect, node_trees_already_searched, used_passes);
587 is_first_effect = false;
588 }
589 return used_passes;
590}
591
592bool is_viewport_compositor_enabled(const View3D &view_3d, const RegionView3D &region_view_3d)
593{
595 return false;
596 }
597
598 if (!ELEM(view_3d.shading.type, OB_MATERIAL, OB_TEXTURE, OB_RENDER)) {
599 return false;
600 }
601
603 region_view_3d.persp != RV3D_CAMOB)
604 {
605 return false;
606 }
607
608 return true;
609}
610
612{
613 const Scene *scene = CTX_data_scene(&context);
614 if (!is_enabled(*scene, ExecutionMode::Preview)) {
615 return false;
616 }
617
618 wmWindowManager *window_manager = CTX_wm_manager(&context);
619 for (const wmWindow &window : window_manager->windows) {
620 const bScreen *screen = WM_window_get_active_screen(&window);
621 for (const ScrArea &area : screen->areabase) {
622 const SpaceLink &space = *area.spacedata.first();
623 if (space.spacetype == SPACE_VIEW3D) {
624 const View3D &view_3d = reinterpret_cast<const View3D &>(space);
625 for (ARegion &region : area.regionbase) {
626 if (region.regiontype == RGN_TYPE_WINDOW) {
627 const RegionView3D &region_view_3d = *static_cast<RegionView3D *>(region.regiondata);
628 if (is_viewport_compositor_enabled(view_3d, region_view_3d)) {
629 return true;
630 }
631 }
632 }
633 }
634 }
635 }
636
637 return false;
638}
639
640/* --------------------------------------------------------------------
641 * Depsgraph.
642 */
643
645 const SceneCompositorEffect &effect,
646 DepsNodeHandle *compositor_output_depsgraph_node)
647{
649 *effect.node_group);
650
652 if (ID *id = IDP_ID_get(property)) {
653 evaluation_dependencies.add_generic_id_full(id);
654 }
655 });
656
657 for (ID *id : evaluation_dependencies.ids.values()) {
658 switch (ID_Type(GS(id->name))) {
659 case ID_OB: {
660 Object *object = reinterpret_cast<Object *>(id);
662 evaluation_dependencies.objects_info.lookup_default(object->id.session_uid, {});
663 if (info.transform) {
664 DEG_add_object_relation(compositor_output_depsgraph_node,
665 object,
667 "Object Transform -> Compositor");
668 }
669 if (object->type == OB_CAMERA && info.camera_parameters) {
670 DEG_add_object_relation(compositor_output_depsgraph_node,
671 object,
673 "Camera Parameters -> Compositor");
674 }
675 if (object->type == OB_ARMATURE && info.pose) {
676 DEG_add_object_relation(compositor_output_depsgraph_node,
677 object,
679 "Armature Pose -> Compositor");
680 }
681 break;
682 }
683 case ID_IM:
684 DEG_add_generic_id_relation(compositor_output_depsgraph_node, id, "Image -> Compositor");
685 break;
686 case ID_TE:
687 DEG_add_generic_id_relation(compositor_output_depsgraph_node, id, "Texture -> Compositor");
688 break;
689 case ID_VF:
691 compositor_output_depsgraph_node, reinterpret_cast<VFont *>(id), "Font -> Compositor");
692 break;
693 default:
694 break;
695 }
696 }
697
698 if (evaluation_dependencies.needs_active_camera) {
699 DEG_add_scene_camera_relation(compositor_output_depsgraph_node,
700 &scene,
702 "Active Camera Transforms -> Compositor");
703 }
704
705 /* Active camera is a scene parameter that can change, so we need a relation for that, too. */
706 if (evaluation_dependencies.needs_active_camera ||
707 evaluation_dependencies.needs_scene_render_params)
708 {
709 DEG_add_scene_relation(compositor_output_depsgraph_node,
710 &scene,
712 "Active Camera Parameters -> Compositor");
713 }
714
715 if (evaluation_dependencies.time_dependent) {
716 DEG_add_time_source_relation(compositor_output_depsgraph_node, "Time Source -> Compositor");
717 }
718}
719
720/* --------------------------------------------------------------------
721 * Compute Contexts.
722 */
723
725 const bNode &node,
726 const bke::bNodeTreeZone *zone,
727 const ComputeContext &compute_context,
728 bke::ComputeContextCache &compute_context_cache)
729{
730 const ComputeContext *current_context = &compute_context;
731 const bke::bNodeTreeZones *zones = node.owner_tree().zones();
732 if (!zones) {
733 return current_context;
734 }
735 const bke::bNodeTreeZone *node_zone = zones->get_zone_by_node(node.identifier);
736 Vector<const bke::bNodeTreeZone *> zone_stack = zones->get_zones_to_enter(zone, node_zone);
737
738 for (const bke::bNodeTreeZone *current_zone : zone_stack) {
739 const bNode &output_node = *current_zone->output_node();
740 if (output_node.is_type("GeometryNodeRepeatOutput"_ustr)) {
741 const auto *repeat_storage = static_cast<NodeGeometryRepeatOutput *>(output_node.storage);
742 const int inspection_index = repeat_storage->inspection_index;
743 current_context = &compute_context_cache.for_repeat_zone(
744 current_context, *current_zone->output_node(), inspection_index);
745 }
746 else if (output_node.is_type("NodeClosureOutput"_ustr)) {
747 /* Viewers inside closures are not supported and are ignored. */
748 return nullptr;
749 }
750 else {
752 return nullptr;
753 }
754 }
755
756 return current_context;
757}
758
759/* Recursively search node groups to find the node group whose instance key matches the given
760 * active node group instance key, and return the compute context of the viewer node that lies
761 * inside it. Returns a nullptr if no active viewer node exists inside the node group. */
763 const bNodeTree &node_group,
764 const ComputeContext &compute_context,
765 const bNodeInstanceKey instance_key,
766 const bNodeInstanceKey active_node_group_instance_key,
767 bke::ComputeContextCache &compute_context_cache)
768{
769 node_group.ensure_topology_cache();
770
771 /* If this is the active node group, returns the hash of the compute context of the active viewer
772 * node that inside it. */
773 if (active_node_group_instance_key == instance_key) {
774 for (const bNode *node : node_group.nodes_by_type("CompositorNodeViewer"_ustr)) {
775 if (node->flag & NODE_DO_OUTPUT && !node->is_muted()) {
776 const ComputeContext *zone_viewer_compute_context = get_zone_viewer_compute_context(
777 *node, nullptr, compute_context, compute_context_cache);
778 return zone_viewer_compute_context;
779 }
780 }
781 return nullptr;
782 }
783
784 /* Otherwise, we have to check node groups recursively. */
785 for (const bNode *group_node : node_group.group_nodes()) {
786 const ID *original_node_group = DEG_get_original(group_node->id);
787 if (group_node->is_muted() || !original_node_group || ID_MISSING(original_node_group)) {
788 continue;
789 }
790
791 const ComputeContext *zone_compute_context = get_zone_viewer_compute_context(
792 *group_node, nullptr, compute_context, compute_context_cache);
793 if (!zone_compute_context) {
794 continue;
795 }
796
797 const bNodeTree &child_node_group = *id_cast<const bNodeTree *>(group_node->id);
798 const bNodeInstanceKey child_instance_key = bke::node_instance_key(
799 instance_key, &node_group, group_node);
800 const bke::GroupNodeComputeContext &child_compute_context =
801 compute_context_cache.for_group_node(
802 zone_compute_context, group_node->identifier, &group_node->owner_tree());
803 const ComputeContext *viewer_compute_context = compute_viewer_compute_context_recursive(
804 child_node_group,
805 child_compute_context,
806 child_instance_key,
807 active_node_group_instance_key,
808 compute_context_cache);
809 if (viewer_compute_context) {
810 return viewer_compute_context;
811 }
812 }
813
814 return nullptr;
815}
816
818 const Scene &scene, bke::ComputeContextCache &compute_context_cache)
819{
820 const bke::DataBlockComputeContext &scene_compute_context = compute_context_cache.for_data_block(
821 nullptr, scene.id);
822 const SceneCompositorEffect *active_effect = get_active_effect(scene);
823 if (!active_effect) {
824 return nullptr;
825 }
826
827 if (!is_effect_enabled(*active_effect, ExecutionMode::Preview)) {
828 return nullptr;
829 }
830
831 const bke::SceneCompositorEffectComputeContext &effect_compute_context =
832 compute_context_cache.for_scene_compositor_effect(&scene_compute_context, *active_effect);
833
834 const bNodeTree *original_node_group = DEG_get_original(active_effect->node_group);
835 if (!original_node_group || ID_MISSING(original_node_group)) {
836 return nullptr;
837 }
838
840 effect_compute_context,
842 active_effect->node_group->active_viewer_key,
843 compute_context_cache);
844}
845
847 const Scene &scene,
848 const bNodeTree &root_node_group,
849 bke::ComputeContextCache &compute_context_cache)
850{
851 const bke::DataBlockComputeContext &scene_compute_context = compute_context_cache.for_data_block(
852 nullptr, scene.id);
853 return compute_viewer_compute_context_recursive(root_node_group,
854 scene_compute_context,
856 root_node_group.active_viewer_key,
857 compute_context_cache);
858}
859
860} // namespace blender::bke::compositor
#define G_MAIN
#define IDP_BlendDataRead(reader, prop)
#define BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data_, func_call_)
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
#define NODE_CUSTOM_GROUP
Definition BKE_node.hh:870
#define NODE_GROUP
Definition BKE_node.hh:865
#define NODE_GROUP_INPUT
Definition BKE_node.hh:868
#define CMP_NODE_CRYPTOMATTE
#define CMP_NODE_R_LAYERS
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define CTX_DATA_(context, msgid)
#define BLT_I18NCONTEXT_ID_SCENE
#define ID_MISSING(_id)
Definition DNA_ID.h:695
#define RE_PASSNAME_COMBINED
#define RE_PASSNAME_CRYPTOMATTE_MATERIAL
#define RE_PASSNAME_CRYPTOMATTE_ASSET
#define RE_PASSNAME_CRYPTOMATTE_OBJECT
#define ND_COMPO_RESULT
Definition WM_types.hh:466
#define NC_SCENE
Definition WM_types.hh:391
volatile int lock
#define BLI_assert_unreachable()
#define ELEM(...)
#define output
constexpr IndexRange()=default
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr StringRef()
bool add(const Key &key)
void reserve(const int64_t n)
bool contains(const T &value) const
void append(const T &value)
const T & last(const int64_t n=0) const
bool is_empty() const
const GroupNodeComputeContext & for_group_node(const ComputeContext *parent, int32_t node_id, const bNodeTree *tree=nullptr)
const RepeatZoneComputeContext & for_repeat_zone(const ComputeContext *parent, int32_t output_node_id, int iteration)
const DataBlockComputeContext & for_data_block(const ComputeContext *parent, const uint32_t orig_session_uid, const ID *id=nullptr)
const SceneCompositorEffectComputeContext & for_scene_compositor_effect(const ComputeContext *parent, const SceneCompositorEffect &effect)
Vector< const bNodeTreeZone * > get_zones_to_enter(const bNodeTreeZone *outer_zone, const bNodeTreeZone *inner_zone) const
const bNodeTreeZone * get_zone_by_node(const int32_t node_id) const
constexpr const char * data() const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
#define offsetof(t, d)
#define main()
static void add_passes_used_by_group_input_node(const bNode *node, Set< std::string > &used_passes)
void free_effect(Scene &scene, SceneCompositorEffect &effect)
static void add_passes_used_by_cryptomatte_node(const bNode *node, const ViewLayer *view_layer, Set< std::string > &used_passes)
SceneCompositorEffect * get_active_effect(const Scene &scene)
void remove_effect(Scene &scene, SceneCompositorEffect &effect)
const SceneCompositorEffect * get_effect_from_property(const PointerRNA &property_ptr)
SceneCompositorEffect * get_effect(const Scene &scene, StringRef name)
SceneCompositorEffect & new_effect(Scene &scene, StringRef name)
void copy_effects(Scene &target_scene, const Scene &source_scene, const int flags)
bool is_viewport_compositor_enabled(const View3D &view_3d, const RegionView3D &region_view_3d)
void add_depsgraph_relations(Scene &scene, const SceneCompositorEffect &effect, DepsNodeHandle *compositor_output_depsgraph_node)
bool is_enabled(const Scene &scene, ExecutionMode mode)
void write_effects(const Scene &scene, BlendWriter &writer)
bool is_viewport_compositor_used(const bContext &context)
void set_active_effect(const Scene &scene, SceneCompositorEffect &effect)
SceneCompositorEffect & duplicate_effect(Scene &scene, SceneCompositorEffect &source_effect)
static const ComputeContext * compute_viewer_compute_context_recursive(const bNodeTree &node_group, const ComputeContext &compute_context, const bNodeInstanceKey instance_key, const bNodeInstanceKey active_node_group_instance_key, bke::ComputeContextCache &compute_context_cache)
const ComputeContext * compute_viewer_compute_context(const Scene &scene, bke::ComputeContextCache &compute_context_cache)
static void add_used_passes_recursive(const bNodeTree *node_tree, const ViewLayer *view_layer, const bool is_root_tree_of_first_effect, Set< const bNodeTree * > &node_trees_already_searched, Set< std::string > &used_passes)
void rename_effect(Scene &scene, SceneCompositorEffect &effect, StringRef new_name, bool update_animation_data=true)
void for_each_id_in_effects(const Scene &scene, LibraryForeachIDData &data)
static void add_passes_used_by_render_layer_node(const bNode *node, Set< std::string > &used_passes)
Set< std::string > get_used_passes(const Scene &scene, const ViewLayer *view_layer, ExecutionMode mode)
void update_effect_node_group_interface(Main &main, Scene &scene, SceneCompositorEffect &effect)
void read_effects(Scene &scene, BlendDataReader &reader)
const ComputeContext * get_zone_viewer_compute_context(const bNode &node, const bke::bNodeTreeZone *zone, const ComputeContext &compute_context, bke::ComputeContextCache &compute_context_cache)
bool is_effect_enabled(const SceneCompositorEffect &effect, ExecutionMode mode)
std::unique_ptr< CryptomatteSession, CryptomatteSessionDeleter > CryptomatteSessionPtr
const Vector< std::string > & BKE_cryptomatte_layer_names_get(const CryptomatteSession &session)
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
const bNodeInstanceKey NODE_INSTANCE_KEY_BASE
Definition node.cc:5600
bNodeInstanceKey node_instance_key(bNodeInstanceKey parent_key, const bNodeTree *ntree, const bNode *node)
Definition node.cc:5621
T ceil(const T &a)
EvalDependencies gather_eval_dependencies_recursive(const bNodeTree &ntree)
void WM_main_add_notifier(uint type, void *reference)
UserDef U
Definition blender.cc:58
__int64 int64_t
Definition BKE_text.h:90
void IDP_FreeProperty_ex(IDProperty *prop, bool do_id_user)
Definition idprop.cc:1398
void DEG_add_generic_id_relation(DepsNodeHandle *node_handle, ID *id, const char *description)
@ V3D_SHADING_USE_COMPOSITOR_CAMERA
@ V3D_SHADING_USE_COMPOSITOR_DISABLED
void id_us_plus(ID *id)
Definition lib_id.cc:433
struct CryptomatteSession * BKE_cryptomatte_init_from_scene(const struct Scene *scene, bool build_meta_data)
void IDP_BlendWrite(BlendWriter *writer, const IDProperty *prop)
Definition idprop.cc:1696
std::string RNA_path_name_to_infix(const StringRefNull string)
Definition rna_path.cc:1419
void IDP_foreach_property(IDProperty *id_property_root, int type_filter, FunctionRef< void(IDProperty *id_property)> callback)
std::optional< AncestorPointerRNA > RNA_struct_search_closest_ancestor_by_type(const PointerRNA *ptr, const StructRNA *srna)
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
void IMB_refImBuf(ImBuf *ibuf)
void DEG_add_object_relation(DepsNodeHandle *node_handle, Object *object, eDepsObjectComponentType component, const char *description)
size_t IMB_get_size_in_memory(const ImBuf *ibuf)
void BKE_animdata_fix_paths(ID &id, StringRef prefix, StringRef old_infix, StringRef new_infix, bool verify_paths, const DriverMap &driver_map)
void BLI_uniquename(const ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_maxncpy) ATTR_NONNULL(1
void IMB_freeImBuf(ImBuf *ibuf)
void DEG_add_time_source_relation(DepsNodeHandle *node_handle, const char *description)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
wmWindowManager * CTX_wm_manager(const bContext *C)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
T * DEG_get_original(T *id)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void RNA_ensure_and_sync_system_properties(Main &bmain, PointerRNA &ptr, IDProperty &idprops)
@ ID_RECALC_COMPOSITOR
Definition DNA_ID.h:1161
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ VIEW_LAYER_CRYPTOMATTE_ASSET
@ VIEW_LAYER_CRYPTOMATTE_MATERIAL
@ VIEW_LAYER_CRYPTOMATTE_OBJECT
Scene * CTX_data_scene(const bContext *C)
void DEG_add_scene_camera_relation(DepsNodeHandle *node_handle, Scene *scene, eDepsObjectComponentType component, const char *description)
void DEG_add_scene_relation(DepsNodeHandle *node_handle, Scene *scene, eDepsSceneComponentType component, const char *description)
IDProperty * IDP_CopyProperty_ex(const IDProperty *prop, int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:968
@ CMP_NODE_CRYPTOMATTE_SOURCE_RENDER
bScreen * WM_window_get_active_screen(const wmWindow *win)
void id_us_min(ID *id)
Definition lib_id.cc:441
void DEG_add_vfont_relation(DepsNodeHandle *handle, VFont *vfont, const char *description)
@ IDP_TYPE_FILTER_ID
eRegion_Type regiontype
void write_struct_list(const ListBaseT< T > *list, BlendStructWriterTypedFn< T > fn=nullptr)
eRegionView3D_Persp persp
struct IDProperty * system_properties
SceneCompositorEffectFlags flags
ListBaseT< SceneCompositorEffect > compositor_effects
struct RenderData r
ListBaseT< ARegion > regionbase
ListBaseT< SpaceLink > spacedata
View3DShadingUseCompositor use_compositor
View3DShading shading
eViewLayerCryptomatteFlags cryptomatte_flag
bNodeInstanceKey active_viewer_key
ListBaseT< struct ScrArea > areabase
void add_frame(int frame_number, int view_identifier, ImBuf *image_buffer)
ImBuf * get_frame(int frame_number, int view_identifier)