/**************************************************************************/1/* scene_tree_fti.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/os/mutex.h"33#include "core/templates/local_vector.h"3435class Node3D;36class Node;37struct Transform3D;38class SceneTreeFTITests;3940#ifdef DEV_ENABLED41// Uncomment this to verify traversal method results.42// #define GODOT_SCENE_TREE_FTI_VERIFY43#endif4445#ifdef _3D_DISABLED46// Stubs47class SceneTreeFTI {48public:49void frame_update(Node *p_root, bool p_frame_start) {}50void tick_update() {}51void set_enabled(Node *p_root, bool p_enabled) {}52bool is_enabled() const { return false; }5354void node_3d_notify_changed(Node3D &r_node, bool p_transform_changed) {}55void node_3d_notify_delete(Node3D *p_node) {}56void node_3d_request_reset(Node3D *p_node) {}57};58#else5960// Important.61// This class uses raw pointers, so it is essential that on deletion, this class is notified62// so that any references can be cleared up to prevent dangling pointer access.6364// This class can be used from a custom SceneTree.6566// Note we could potentially make SceneTreeFTI static / global to avoid the lookup through scene tree,67// but this covers the custom case of multiple scene trees.6869class SceneTreeFTI {70friend class SceneTreeFTITests;7172enum TraversalMode : unsigned {73TM_DEFAULT,74TM_LEGACY,75TM_DEBUG,76};7778struct Data {79static const uint32_t scene_tree_depth_limit = 32;8081// Prev / Curr lists of Node3Ds having local xforms pumped.82LocalVector<Node3D *> tick_xform_list[2];8384// The frame lists are changed nodes that need to start traversal,85// either longterm (on the tick list) or single frame forced.86LocalVector<Node3D *> frame_xform_list;87LocalVector<Node3D *> frame_xform_list_forced;8889// Prev / Curr lists of Node3Ds having actively interpolated properties.90LocalVector<Node3D *> tick_property_list[2];9192LocalVector<Node3D *> frame_property_list;93LocalVector<Node3D *> request_reset_list;94LocalVector<Node3D *> dirty_node_depth_lists[scene_tree_depth_limit];9596// When we are using two alternating lists,97// which one is current.98uint32_t mirror = 0;99100// Global on / off switch for SceneTreeFTI.101bool enabled = false;102103// Whether we are in physics ticks, or in a frame.104bool in_frame = false;105106// Updating at the start of the frame, or the end on second pass.107bool frame_start = true;108109Mutex mutex;110111TraversalMode traversal_mode = TM_DEFAULT;112bool use_optimized_traversal_method = true;113114// DEBUGGING115bool periodic_debug_log = false;116uint32_t debug_node_count = 0;117uint32_t debug_nodes_processed = 0;118119} data;120121#ifdef GODOT_SCENE_TREE_FTI_VERIFY122SceneTreeFTITests *_tests = nullptr;123#endif124125void _update_dirty_nodes(Node *p_node, uint32_t p_current_half_frame, float p_interpolation_fraction, bool p_active, const Transform3D *p_parent_global_xform = nullptr, int p_depth = 0);126void _update_request_resets();127128void _reset_flags(Node *p_node);129void _reset_node3d_flags(Node3D &r_node);130void _node_3d_notify_set_xform(Node3D &r_node);131void _node_3d_notify_set_property(Node3D &r_node);132133void _node_add_to_frame_list(Node3D &r_node, bool p_forced);134void _node_remove_from_frame_list(Node3D &r_node, bool p_forced);135136void _create_depth_lists();137void _clear_depth_lists();138139public:140// Hottest function, allow inlining the data.enabled check.141void node_3d_notify_changed(Node3D &r_node, bool p_transform_changed) {142if (!data.enabled) {143return;144}145MutexLock(data.mutex);146147if (p_transform_changed) {148_node_3d_notify_set_xform(r_node);149} else {150_node_3d_notify_set_property(r_node);151}152}153154void node_3d_request_reset(Node3D *p_node);155void node_3d_notify_delete(Node3D *p_node);156157// Calculate interpolated xforms, send to visual server.158void frame_update(Node *p_root, bool p_frame_start);159160// Update local xform pumps.161void tick_update();162163void set_enabled(Node *p_root, bool p_enabled);164bool is_enabled() const { return data.enabled; }165166void set_debug_next_frame() { data.periodic_debug_log = true; }167168SceneTreeFTI();169~SceneTreeFTI();170};171172#endif // ndef _3D_DISABLED173174175