| 1 | /**************************************************************************/ |
| 2 | /* scene_tree_fti_tests.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifndef _3D_DISABLED |
| 32 | |
| 33 | #ifdef GODOT_SCENE_TREE_FTI_VERIFY |
| 34 | #include "scene_tree_fti_tests.h" |
| 35 | |
| 36 | #include "scene/3d/node_3d.h" |
| 37 | #include "scene/3d/visual_instance_3d.h" |
| 38 | #include "scene/main/scene_tree_fti.h" |
| 39 | |
| 40 | void SceneTreeFTITests::debug_verify_failed(const Node3D *p_node_3d, const Transform3D &p_test) { |
| 41 | print_line("VERIFY FAILED\n"); |
| 42 | print_line("test xform : " + String(Variant(p_test))); |
| 43 | |
| 44 | bool first = true; |
| 45 | |
| 46 | while (p_node_3d) { |
| 47 | int32_t depth = MAX(p_node_3d->_get_scene_tree_depth(), 0); |
| 48 | String tabs; |
| 49 | for (int32_t n = 0; n < depth; n++) { |
| 50 | tabs += "\t"; |
| 51 | } |
| 52 | |
| 53 | bool interp_equal = p_node_3d->_get_cached_global_transform_interpolated() == p_test; |
| 54 | bool glob_equal = p_node_3d->get_global_transform() == p_test; |
| 55 | |
| 56 | String sz = tabs + p_node_3d->get_name() + " [ " + p_node_3d->get_class_name() + " ]\n"; |
| 57 | |
| 58 | if (first) { |
| 59 | sz += tabs + "... " + String(Variant(p_test)) + "\n"; |
| 60 | } |
| 61 | |
| 62 | sz += tabs + (p_node_3d->data.fti_global_xform_interp_set ? "[I] " : "[i] ") + String(Variant(p_node_3d->_get_cached_global_transform_interpolated())) + (interp_equal ? " ***" : "") + "\n"; |
| 63 | sz += tabs + "[g] " + String(Variant(p_node_3d->get_global_transform())) + (glob_equal ? " ***" : ""); |
| 64 | |
| 65 | print_line(sz); |
| 66 | |
| 67 | p_node_3d = p_node_3d->get_parent_node_3d(); |
| 68 | first = false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void SceneTreeFTITests::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, int p_depth) { |
| 73 | SceneTreeFTI::Data &data = _fti.data; |
| 74 | |
| 75 | // There are two runs going on here. |
| 76 | // FIRST the naive entire scene tree (reference), where we are |
| 77 | // setting state (i.e. writing out xforms, and other state) |
| 78 | // SECOND the optimized run, where we are NOT |
| 79 | // writing state, but only verifying that the xforms calculated |
| 80 | // match those from the reference approach. |
| 81 | bool should_verify = (data.traversal_mode == SceneTreeFTI::TM_DEBUG) && data.use_optimized_traversal_method; |
| 82 | bool set_state = !should_verify; |
| 83 | |
| 84 | Node3D *s = Object::cast_to<Node3D>(p_node); |
| 85 | |
| 86 | if (s && !s->is_visible()) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | if (!s) { |
| 91 | for (int n = 0; n < p_node->get_child_count(); n++) { |
| 92 | update_dirty_nodes(p_node->get_child(n), p_current_half_frame, p_interpolation_fraction, p_active, nullptr, p_depth + 1); |
| 93 | } |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (s->_test_dirty_bits(Node3D::DIRTY_GLOBAL_TRANSFORM)) { |
| 98 | _ALLOW_DISCARD_ s->get_global_transform(); |
| 99 | } |
| 100 | |
| 101 | if (!p_active) { |
| 102 | if (data.frame_start) { |
| 103 | if (s->data.fti_on_frame_xform_list || s->data.fti_frame_xform_force_update) { |
| 104 | p_active = true; |
| 105 | } |
| 106 | } else { |
| 107 | if (s->_test_dirty_bits(Node3D::DIRTY_GLOBAL_INTERPOLATED_TRANSFORM)) { |
| 108 | p_active = true; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (data.frame_start) { |
| 114 | s->data.fti_global_xform_interp_set = p_active; |
| 115 | } |
| 116 | |
| 117 | if (p_active) { |
| 118 | Transform3D local_interp; |
| 119 | if (s->is_physics_interpolated()) { |
| 120 | if (s->data.fti_on_tick_xform_list) { |
| 121 | TransformInterpolator::interpolate_transform_3d(s->data.local_transform_prev, s->get_transform(), local_interp, p_interpolation_fraction); |
| 122 | } else { |
| 123 | local_interp = s->get_transform(); |
| 124 | } |
| 125 | } else { |
| 126 | local_interp = s->get_transform(); |
| 127 | } |
| 128 | |
| 129 | if (!s->is_set_as_top_level()) { |
| 130 | if (p_parent_global_xform) { |
| 131 | if (should_verify) { |
| 132 | Transform3D test = (*p_parent_global_xform) * local_interp; |
| 133 | if (s->data.disable_scale) { |
| 134 | test.basis.orthonormalize(); |
| 135 | } |
| 136 | if (s->data.global_transform_interpolated != test) { |
| 137 | debug_verify_failed(s, test); |
| 138 | DEV_ASSERT(s->data.global_transform_interpolated == test); |
| 139 | } |
| 140 | } else { |
| 141 | s->data.global_transform_interpolated = s->data.fti_is_identity_xform ? (*p_parent_global_xform) : (*p_parent_global_xform) * local_interp; |
| 142 | } |
| 143 | } else { |
| 144 | const Node3D *parent = s->get_parent_node_3d(); |
| 145 | |
| 146 | if (parent) { |
| 147 | const Transform3D &parent_glob = parent->data.fti_global_xform_interp_set ? parent->data.global_transform_interpolated : parent->get_global_transform(); |
| 148 | |
| 149 | if (should_verify) { |
| 150 | Transform3D test = parent_glob * local_interp; |
| 151 | if (s->data.disable_scale) { |
| 152 | test.basis.orthonormalize(); |
| 153 | } |
| 154 | if (s->data.global_transform_interpolated != test) { |
| 155 | debug_verify_failed(s, test); |
| 156 | DEV_ASSERT(s->data.global_transform_interpolated == test); |
| 157 | } |
| 158 | |
| 159 | } else { |
| 160 | s->data.global_transform_interpolated = s->data.fti_is_identity_xform ? parent_glob : parent_glob * local_interp; |
| 161 | } |
| 162 | } else { |
| 163 | if (set_state) { |
| 164 | s->data.global_transform_interpolated = local_interp; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } else { |
| 169 | if (set_state) { |
| 170 | s->data.global_transform_interpolated = local_interp; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (set_state) { |
| 175 | if (s->data.disable_scale) { |
| 176 | s->data.global_transform_interpolated.basis.orthonormalize(); |
| 177 | } |
| 178 | |
| 179 | s->fti_update_servers_xform(); |
| 180 | |
| 181 | s->data.fti_frame_xform_force_update = false; |
| 182 | } |
| 183 | |
| 184 | s->data.fti_processed = true; |
| 185 | } // if active. |
| 186 | |
| 187 | if (set_state) { |
| 188 | s->_clear_dirty_bits(Node3D::DIRTY_GLOBAL_INTERPOLATED_TRANSFORM); |
| 189 | } |
| 190 | |
| 191 | for (int n = 0; n < p_node->get_child_count(); n++) { |
| 192 | update_dirty_nodes(p_node->get_child(n), p_current_half_frame, p_interpolation_fraction, p_active, s->data.fti_global_xform_interp_set ? &s->data.global_transform_interpolated : &s->data.global_transform, p_depth + 1); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void SceneTreeFTITests::frame_update(Node *p_root, uint32_t p_half_frame, float p_interpolation_fraction) { |
| 197 | SceneTreeFTI::Data &data = _fti.data; |
| 198 | |
| 199 | // For testing, use both methods. |
| 200 | // FIRST the entire tree, writing out state. |
| 201 | { |
| 202 | data.use_optimized_traversal_method = false; |
| 203 | update_dirty_nodes(p_root, p_half_frame, p_interpolation_fraction, false); |
| 204 | } |
| 205 | |
| 206 | // SECOND the optimized depth lists only, |
| 207 | // no writing of state, and verifying results. |
| 208 | { |
| 209 | data.use_optimized_traversal_method = true; |
| 210 | |
| 211 | _fti._create_depth_lists(); |
| 212 | |
| 213 | for (uint32_t d = 0; d < data.scene_tree_depth_limit; d++) { |
| 214 | const LocalVector<Node3D *> &list = data.dirty_node_depth_lists[d]; |
| 215 | |
| 216 | for (uint32_t n = 0; n < list.size(); n++) { |
| 217 | Node3D *s = list[n]; |
| 218 | |
| 219 | if (s->data.fti_processed) { |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | if (Object::cast_to<VisualInstance3D>(s)) { |
| 224 | if (!s->_is_vi_visible()) { |
| 225 | continue; |
| 226 | } |
| 227 | } else if (!s->is_visible_in_tree()) { |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | update_dirty_nodes(s, p_half_frame, p_interpolation_fraction, true); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | _fti._clear_depth_lists(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | SceneTreeFTITests::SceneTreeFTITests(SceneTreeFTI &p_fti) : |
| 240 | _fti(p_fti) { |
| 241 | print_line("SceneTreeFTI : GODOT_SCENE_TREE_FTI_VERIFY defined"); |
| 242 | } |
| 243 | |
| 244 | #endif // def GODOT_SCENE_TREE_FTI_VERIFY |
| 245 | |
| 246 | #endif // ndef _3D_DISABLED |