Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/scene_tree_fti.cpp
20952 views
1
/**************************************************************************/
2
/* scene_tree_fti.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
#include "scene_tree_fti.h"
34
35
#include "core/config/engine.h"
36
#include "core/config/project_settings.h"
37
#include "core/math/transform_interpolator.h"
38
#include "core/os/os.h"
39
#include "scene/3d/visual_instance_3d.h"
40
41
#ifdef GODOT_SCENE_TREE_FTI_VERIFY
42
#include "scene_tree_fti_tests.h"
43
#endif
44
45
#ifdef DEV_ENABLED
46
47
// Uncomment this to enable some slow extra DEV_ENABLED
48
// checks to ensure there aren't more than one object added to the lists.
49
// #define GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
50
51
// Uncomment this to regularly print the tree that is being interpolated.
52
// #define GODOT_SCENE_TREE_FTI_PRINT_TREE
53
54
#endif
55
56
void SceneTreeFTI::_reset_node3d_flags(Node3D &r_node) {
57
r_node.data.fti_on_tick_xform_list = false;
58
r_node.data.fti_on_tick_property_list = false;
59
r_node.data.fti_on_frame_xform_list = false;
60
r_node.data.fti_on_frame_property_list = false;
61
r_node.data.fti_global_xform_interp_set = false;
62
r_node.data.fti_frame_xform_force_update = false;
63
r_node.data.fti_processed = false;
64
}
65
66
void SceneTreeFTI::_reset_flags(Node *p_node) {
67
Node3D *s = Object::cast_to<Node3D>(p_node);
68
69
if (s) {
70
_reset_node3d_flags(*s);
71
72
// In most cases the later NOTIFICATION_RESET_PHYSICS_INTERPOLATION
73
// will reset this, but this should help cover hidden nodes.
74
s->data.local_transform_prev = s->get_transform();
75
}
76
77
for (int n = 0; n < p_node->get_child_count(); n++) {
78
_reset_flags(p_node->get_child(n));
79
}
80
}
81
82
void SceneTreeFTI::set_enabled(Node *p_root, bool p_enabled) {
83
if (data.enabled == p_enabled) {
84
return;
85
}
86
MutexLock(data.mutex);
87
88
data.tick_xform_list[0].clear();
89
data.tick_xform_list[1].clear();
90
91
data.frame_xform_list.clear();
92
data.frame_xform_list_forced.clear();
93
94
data.tick_property_list[0].clear();
95
data.tick_property_list[1].clear();
96
97
data.frame_property_list.clear();
98
data.request_reset_list.clear();
99
100
_clear_depth_lists();
101
102
// Node3D flags must be reset.
103
if (p_root) {
104
_reset_flags(p_root);
105
}
106
107
data.enabled = p_enabled;
108
}
109
110
void SceneTreeFTI::tick_update() {
111
if (!data.enabled) {
112
return;
113
}
114
MutexLock(data.mutex);
115
116
_update_request_resets();
117
118
uint32_t curr_mirror = data.mirror;
119
uint32_t prev_mirror = curr_mirror ? 0 : 1;
120
121
LocalVector<Node3D *> &curr = data.tick_xform_list[curr_mirror];
122
LocalVector<Node3D *> &prev = data.tick_xform_list[prev_mirror];
123
124
// First detect on the previous list but not on this tick list.
125
for (uint32_t n = 0; n < prev.size(); n++) {
126
Node3D *s = prev[n];
127
if (!s->data.fti_on_tick_xform_list) {
128
// Needs a reset so jittering will stop.
129
s->fti_pump_xform();
130
131
// Optimization - detect whether we have rested at identity xform.
132
s->data.fti_is_identity_xform = s->data.local_transform == Transform3D();
133
134
// This may not get updated so set it to the same as global xform.
135
// TODO: double check this is the best value.
136
s->data.global_transform_interpolated = s->get_global_transform();
137
138
// Remove from interpolation list.
139
if (s->data.fti_on_frame_xform_list) {
140
_node_remove_from_frame_list(*s, false);
141
}
142
143
// Ensure that the node gets at least ONE further
144
// update in the resting position in the next frame update.
145
if (!s->data.fti_frame_xform_force_update) {
146
_node_add_to_frame_list(*s, true);
147
}
148
}
149
}
150
151
LocalVector<Node3D *> &curr_prop = data.tick_property_list[curr_mirror];
152
LocalVector<Node3D *> &prev_prop = data.tick_property_list[prev_mirror];
153
154
// Detect on the previous property list but not on this tick list.
155
for (uint32_t n = 0; n < prev_prop.size(); n++) {
156
Node3D *s = prev_prop[n];
157
158
if (!s->data.fti_on_tick_property_list) {
159
// Needs a reset so jittering will stop.
160
s->fti_pump_xform();
161
162
// Ensure the servers are up to date with the final resting value.
163
s->fti_update_servers_property();
164
165
// Remove from interpolation list.
166
if (s->data.fti_on_frame_property_list) {
167
s->data.fti_on_frame_property_list = false;
168
data.frame_property_list.erase_unordered(s);
169
170
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
171
DEV_CHECK_ONCE(data.frame_property_list.find(s) == -1);
172
#endif
173
}
174
}
175
}
176
177
// Pump all on the property list that are NOT on the tick list.
178
for (uint32_t n = 0; n < curr_prop.size(); n++) {
179
Node3D *s = curr_prop[n];
180
181
// Reset, needs to be marked each tick.
182
s->data.fti_on_tick_property_list = false;
183
s->fti_pump_property();
184
}
185
186
// Now pump all on the current list.
187
for (uint32_t n = 0; n < curr.size(); n++) {
188
Node3D *s = curr[n];
189
190
// Reset, needs to be marked each tick.
191
s->data.fti_on_tick_xform_list = false;
192
193
// Pump.
194
s->fti_pump_xform();
195
}
196
197
// Clear previous list and flip.
198
prev.clear();
199
prev_prop.clear();
200
data.mirror = prev_mirror;
201
}
202
203
void SceneTreeFTI::_update_request_resets() {
204
// For instance when first adding to the tree, when the previous transform is
205
// unset, to prevent streaking from the origin.
206
for (uint32_t n = 0; n < data.request_reset_list.size(); n++) {
207
Node3D *s = data.request_reset_list[n];
208
if (s->_is_physics_interpolation_reset_requested()) {
209
if (s->_is_vi_visible() && !s->_is_using_identity_transform()) {
210
s->notification(Node3D::NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
211
}
212
213
s->_set_physics_interpolation_reset_requested(false);
214
}
215
}
216
217
data.request_reset_list.clear();
218
}
219
220
void SceneTreeFTI::node_3d_request_reset(Node3D *p_node) {
221
DEV_CHECK_ONCE(data.enabled);
222
DEV_ASSERT(p_node);
223
224
MutexLock(data.mutex);
225
226
if (!p_node->_is_physics_interpolation_reset_requested()) {
227
p_node->_set_physics_interpolation_reset_requested(true);
228
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
229
DEV_CHECK_ONCE(data.request_reset_list.find(p_node) == -1);
230
#endif
231
data.request_reset_list.push_back(p_node);
232
}
233
}
234
235
void SceneTreeFTI::_node_3d_notify_set_property(Node3D &r_node) {
236
if (!r_node.is_physics_interpolated()) {
237
return;
238
}
239
240
DEV_CHECK_ONCE(data.enabled);
241
242
// Note that a Node3D can be on BOTH the transform list and the property list.
243
if (!r_node.data.fti_on_tick_property_list) {
244
r_node.data.fti_on_tick_property_list = true;
245
246
// Should only appear once in the property list.
247
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
248
DEV_CHECK_ONCE(data.tick_property_list[data.mirror].find(&r_node) == -1);
249
#endif
250
data.tick_property_list[data.mirror].push_back(&r_node);
251
}
252
253
if (!r_node.data.fti_on_frame_property_list) {
254
r_node.data.fti_on_frame_property_list = true;
255
256
// Should only appear once in the property frame list.
257
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
258
DEV_CHECK_ONCE(data.frame_property_list.find(&r_node) == -1);
259
#endif
260
data.frame_property_list.push_back(&r_node);
261
}
262
}
263
264
void SceneTreeFTI::_create_depth_lists() {
265
uint32_t first_list = data.frame_start ? 0 : 1;
266
267
for (uint32_t l = first_list; l < 2; l++) {
268
LocalVector<Node3D *> &source_list = l == 0 ? data.frame_xform_list : data.frame_xform_list_forced;
269
270
#ifdef DEBUG_ENABLED
271
bool log_nodes_moved_on_frame = (data.traversal_mode == TM_DEBUG) && !data.frame_start && data.periodic_debug_log;
272
if (log_nodes_moved_on_frame) {
273
if (source_list.size()) {
274
print_line(String("\n") + itos(source_list.size()) + " nodes moved during frame:");
275
} else {
276
print_line("0 nodes moved during frame.");
277
}
278
}
279
#endif
280
281
for (uint32_t n = 0; n < source_list.size(); n++) {
282
Node3D *s = source_list[n];
283
s->data.fti_processed = false;
284
285
int32_t depth = s->_get_scene_tree_depth();
286
287
// This shouldn't happen, but wouldn't be terrible if it did.
288
DEV_ASSERT(depth >= 0);
289
depth = MIN(depth, (int32_t)data.scene_tree_depth_limit - 1);
290
291
LocalVector<Node3D *> &dest_list = data.dirty_node_depth_lists[depth];
292
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
293
// Shouldn't really happen, but duplicates don't really matter that much.
294
if (dest_list.find(s) != -1) {
295
ERR_FAIL_COND(dest_list.find(s) != -1);
296
}
297
#endif
298
299
#ifdef DEBUG_ENABLED
300
if (log_nodes_moved_on_frame) {
301
print_line("\t" + s->get_name());
302
}
303
#endif
304
305
// Prevent being added to the dest_list twice when on
306
// the frame_xform_list AND the frame_xform_list_forced.
307
if ((l == 0) && s->data.fti_frame_xform_force_update) {
308
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
309
DEV_ASSERT(data.frame_xform_list_forced.find(s) != -1);
310
#endif
311
continue;
312
}
313
314
dest_list.push_back(s);
315
}
316
}
317
}
318
319
void SceneTreeFTI::_clear_depth_lists() {
320
for (uint32_t d = 0; d < data.scene_tree_depth_limit; d++) {
321
data.dirty_node_depth_lists[d].clear();
322
}
323
}
324
325
void SceneTreeFTI::_node_add_to_frame_list(Node3D &r_node, bool p_forced) {
326
if (p_forced) {
327
DEV_ASSERT(!r_node.data.fti_frame_xform_force_update);
328
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
329
int64_t found = data.frame_xform_list_forced.find(&r_node);
330
if (found != -1) {
331
ERR_FAIL_COND(found != -1);
332
}
333
#endif
334
data.frame_xform_list_forced.push_back(&r_node);
335
r_node.data.fti_frame_xform_force_update = true;
336
} else {
337
DEV_ASSERT(!r_node.data.fti_on_frame_xform_list);
338
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
339
int64_t found = data.frame_xform_list.find(&r_node);
340
if (found != -1) {
341
ERR_FAIL_COND(found != -1);
342
}
343
#endif
344
data.frame_xform_list.push_back(&r_node);
345
r_node.data.fti_on_frame_xform_list = true;
346
}
347
}
348
349
void SceneTreeFTI::_node_remove_from_frame_list(Node3D &r_node, bool p_forced) {
350
if (p_forced) {
351
DEV_ASSERT(r_node.data.fti_frame_xform_force_update);
352
data.frame_xform_list_forced.erase_unordered(&r_node);
353
r_node.data.fti_frame_xform_force_update = false;
354
} else {
355
DEV_ASSERT(r_node.data.fti_on_frame_xform_list);
356
data.frame_xform_list.erase_unordered(&r_node);
357
r_node.data.fti_on_frame_xform_list = false;
358
}
359
}
360
361
void SceneTreeFTI::_node_3d_notify_set_xform(Node3D &r_node) {
362
DEV_CHECK_ONCE(data.enabled);
363
364
if (!r_node.is_physics_interpolated()) {
365
// Force an update of non-interpolated to servers
366
// on the next traversal.
367
if (!r_node.data.fti_frame_xform_force_update) {
368
_node_add_to_frame_list(r_node, true);
369
}
370
371
// ToDo: Double check this is a win,
372
// non-interpolated nodes we always check for identity,
373
// *just in case*.
374
r_node.data.fti_is_identity_xform = r_node.get_transform() == Transform3D();
375
return;
376
}
377
378
r_node.data.fti_is_identity_xform = false;
379
380
if (!r_node.data.fti_on_tick_xform_list) {
381
r_node.data.fti_on_tick_xform_list = true;
382
383
// Should only appear once in the xform list.
384
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
385
DEV_CHECK_ONCE(data.tick_xform_list[data.mirror].find(&r_node) == -1);
386
#endif
387
data.tick_xform_list[data.mirror].push_back(&r_node);
388
389
// The following flag could have been previously set
390
// (for removal from the tick list).
391
// We no longer need this guarantee,
392
// however there is probably no downside to leaving it set
393
// as it will be cleared on the next frame anyway.
394
// This line is left for reference.
395
// r_node.data.fti_frame_xform_force_update = false;
396
}
397
398
if (!r_node.data.fti_on_frame_xform_list) {
399
_node_add_to_frame_list(r_node, false);
400
}
401
402
// If we are in the second half of a frame, always add to the force update list,
403
// because we ignore the tick update list during the second update.
404
if (data.in_frame) {
405
if (!r_node.data.fti_frame_xform_force_update) {
406
_node_add_to_frame_list(r_node, true);
407
}
408
}
409
}
410
411
void SceneTreeFTI::node_3d_notify_delete(Node3D *p_node) {
412
if (!data.enabled) {
413
return;
414
}
415
416
ERR_FAIL_NULL(p_node);
417
418
MutexLock(data.mutex);
419
420
// Remove from frame lists.
421
if (p_node->data.fti_on_frame_xform_list) {
422
_node_remove_from_frame_list(*p_node, false);
423
}
424
if (p_node->data.fti_frame_xform_force_update) {
425
_node_remove_from_frame_list(*p_node, true);
426
}
427
428
// Ensure this is kept in sync with the lists, in case a node
429
// is removed and re-added to the scene tree multiple times
430
// on the same frame / tick.
431
p_node->_set_physics_interpolation_reset_requested(false);
432
433
// Keep flags consistent for the same as a new node,
434
// because this node may re-enter the scene tree.
435
_reset_node3d_flags(*p_node);
436
437
// This can potentially be optimized for large scenes with large churn,
438
// as it will be doing a linear search through the lists.
439
data.tick_xform_list[0].erase_unordered(p_node);
440
data.tick_xform_list[1].erase_unordered(p_node);
441
442
data.tick_property_list[0].erase_unordered(p_node);
443
data.tick_property_list[1].erase_unordered(p_node);
444
445
data.frame_property_list.erase_unordered(p_node);
446
data.request_reset_list.erase_unordered(p_node);
447
448
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
449
// There should only be one occurrence on the lists.
450
// Check this in DEV_ENABLED builds.
451
DEV_CHECK_ONCE(data.tick_xform_list[0].find(p_node) == -1);
452
DEV_CHECK_ONCE(data.tick_xform_list[1].find(p_node) == -1);
453
454
DEV_CHECK_ONCE(data.tick_property_list[0].find(p_node) == -1);
455
DEV_CHECK_ONCE(data.tick_property_list[1].find(p_node) == -1);
456
457
DEV_CHECK_ONCE(data.frame_property_list.find(p_node) == -1);
458
DEV_CHECK_ONCE(data.request_reset_list.find(p_node) == -1);
459
460
DEV_CHECK_ONCE(data.frame_xform_list.find(p_node) == -1);
461
DEV_CHECK_ONCE(data.frame_xform_list_forced.find(p_node) == -1);
462
#endif
463
}
464
465
void SceneTreeFTI::_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) {
466
Node3D *s = Object::cast_to<Node3D>(p_node);
467
468
#ifdef DEBUG_ENABLED
469
data.debug_node_count++;
470
#endif
471
472
// Don't recurse into hidden branches.
473
if (s && !s->data.visible) {
474
// NOTE : If we change from recursing entire tree, we should do an is_visible_in_tree()
475
// check for the first of the branch.
476
return;
477
}
478
479
// Not a Node3D.
480
// Could be e.g. a viewport or something
481
// so we should still recurse to children.
482
if (!s) {
483
for (Node *node : p_node->iterate_children()) {
484
_update_dirty_nodes(node, p_current_half_frame, p_interpolation_fraction, p_active, nullptr, p_depth + 1);
485
}
486
return;
487
}
488
489
// We are going to be using data.global_transform, so
490
// we need to ensure data.global_transform is not dirty!
491
if (s->_test_dirty_bits(Node3D::DIRTY_GLOBAL_TRANSFORM)) {
492
_ALLOW_DISCARD_ s->get_global_transform();
493
}
494
495
// Start the active interpolation chain from here onwards
496
// as we recurse further into the SceneTree.
497
// Once we hit an active (interpolated) node, we have to fully
498
// process all ancestors because their xform will also change.
499
// Anything not moving (inactive) higher in the tree need not be processed.
500
if (!p_active) {
501
if (data.frame_start) {
502
// On the frame start, activate whenever we hit something that requests interpolation.
503
if (s->data.fti_on_frame_xform_list || s->data.fti_frame_xform_force_update) {
504
p_active = true;
505
}
506
} else {
507
// On the frame end, we want to re-interpolate *anything* that has moved
508
// since the frame start.
509
if (s->_test_dirty_bits(Node3D::DIRTY_GLOBAL_INTERPOLATED_TRANSFORM)) {
510
p_active = true;
511
512
#if 0
513
if (data.periodic_debug_log) {
514
print_line("activating on : " + s->get_name());
515
}
516
#endif
517
}
518
}
519
}
520
521
// ToDo : Check global_xform_interp is up to date for nodes
522
// that are not traversed by the depth lists.
523
if (data.frame_start) {
524
// Mark on the Node3D whether we have set global_transform_interp.
525
// This can later be used when calling `get_global_transform_interpolated()`
526
// to know which xform to return.
527
s->data.fti_global_xform_interp_set = p_active;
528
}
529
530
if (p_active) {
531
#ifdef GODOT_SCENE_TREE_FTI_PRINT_TREE
532
bool dirty = s->_test_dirty_bits(Node3D::DIRTY_GLOBAL_INTERPOLATED_TRANSFORM);
533
534
if (data.periodic_debug_log && !data.use_optimized_traversal_method && !data.frame_start) {
535
String sz;
536
for (int n = 0; n < p_depth; n++) {
537
sz += "\t";
538
}
539
print_line(sz + p_node->get_name() + (dirty ? " DIRTY" : "") + (s->get_transform() == Transform3D() ? "\t[IDENTITY]" : ""));
540
}
541
#endif
542
543
// First calculate our local xform.
544
// This will either use interpolation, or just use the current local if not interpolated.
545
Transform3D local_interp;
546
if (s->is_physics_interpolated()) {
547
// There may be no need to interpolate if the node has not been moved recently
548
// and is therefore not on the tick list...
549
if (s->data.fti_on_tick_xform_list) {
550
// Make sure to call `get_transform()` rather than using local_transform directly, because
551
// local_transform may be dirty and need updating from rotation / scale.
552
TransformInterpolator::interpolate_transform_3d(s->data.local_transform_prev, s->get_transform(), local_interp, p_interpolation_fraction);
553
} else {
554
local_interp = s->get_transform();
555
}
556
} else {
557
local_interp = s->get_transform();
558
}
559
560
// Concatenate parent xform.
561
if (!s->is_set_as_top_level()) {
562
if (p_parent_global_xform) {
563
s->data.global_transform_interpolated = s->data.fti_is_identity_xform ? *p_parent_global_xform : ((*p_parent_global_xform) * local_interp);
564
} else {
565
const Node3D *parent = s->get_parent_node_3d();
566
567
if (parent) {
568
const Transform3D &parent_glob = parent->data.fti_global_xform_interp_set ? parent->data.global_transform_interpolated : parent->get_global_transform();
569
s->data.global_transform_interpolated = s->data.fti_is_identity_xform ? parent_glob : parent_glob * local_interp;
570
} else {
571
s->data.global_transform_interpolated = local_interp;
572
}
573
}
574
} else {
575
s->data.global_transform_interpolated = local_interp;
576
}
577
578
// Watch for this, disable_scale can cause incredibly confusing bugs
579
// and must be checked for when calculating global xforms.
580
if (s->data.disable_scale) {
581
s->data.global_transform_interpolated.basis.orthonormalize();
582
}
583
584
// Upload to RenderingServer the interpolated global xform.
585
s->fti_update_servers_xform();
586
587
// Ensure branches are only processed once on each traversal.
588
s->data.fti_processed = true;
589
590
#ifdef DEBUG_ENABLED
591
data.debug_nodes_processed++;
592
#endif
593
} // if active.
594
595
// Remove the dirty interp flag from EVERYTHING as we go.
596
s->_clear_dirty_bits(Node3D::DIRTY_GLOBAL_INTERPOLATED_TRANSFORM);
597
598
// Recurse to children.
599
for (Node *node : p_node->iterate_children()) {
600
_update_dirty_nodes(node, 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);
601
}
602
}
603
604
void SceneTreeFTI::frame_update(Node *p_root, bool p_frame_start) {
605
if (!data.enabled || !p_root) {
606
return;
607
}
608
MutexLock(data.mutex);
609
610
data.frame_start = p_frame_start;
611
data.in_frame = true;
612
613
_update_request_resets();
614
615
float interpolation_fraction = Engine::get_singleton()->get_physics_interpolation_fraction();
616
uint32_t frame = Engine::get_singleton()->get_frames_drawn();
617
618
uint64_t before = 0;
619
#ifdef DEBUG_ENABLED
620
if (data.traversal_mode == TM_DEBUG) {
621
before = OS::get_singleton()->get_ticks_usec();
622
623
if (p_frame_start && ((frame % ((60 * 15) - 3)) == 0)) {
624
data.periodic_debug_log = true;
625
}
626
}
627
628
#ifdef GODOT_SCENE_TREE_FTI_PRINT_TREE
629
if (data.periodic_debug_log) {
630
print_line(String("\nScene: ") + (data.frame_start ? "start" : "end") + "\n");
631
}
632
#endif
633
#endif
634
635
data.debug_node_count = 0;
636
data.debug_nodes_processed = 0;
637
638
uint32_t half_frame = p_frame_start ? (frame * 2) : ((frame * 2) + 1);
639
640
bool print_debug_stats = false;
641
switch (data.traversal_mode) {
642
case TM_LEGACY: {
643
data.use_optimized_traversal_method = false;
644
} break;
645
case TM_DEBUG: {
646
// Switch on alternate frames between the two methods.
647
data.use_optimized_traversal_method = (frame % 2) == 1;
648
649
// Odd number ensures we debug stats for both methods.
650
print_debug_stats = (frame % ((60 * 8) - 1)) == 0;
651
} break;
652
default: {
653
data.use_optimized_traversal_method = true;
654
} break;
655
}
656
657
#ifdef GODOT_SCENE_TREE_FTI_VERIFY
658
_tests->frame_update(p_root, half_frame, interpolation_fraction);
659
#else
660
661
uint32_t skipped = 0;
662
663
if (!data.use_optimized_traversal_method) {
664
// Reference approach.
665
// Traverse the entire scene tree.
666
// Slow, but robust.
667
_update_dirty_nodes(p_root, half_frame, interpolation_fraction, false);
668
} else {
669
// Optimized approach.
670
// Traverse from depth lists.
671
// Be sure to check against the reference
672
// implementation when making changes.
673
_create_depth_lists();
674
675
for (uint32_t d = 0; d < data.scene_tree_depth_limit; d++) {
676
const LocalVector<Node3D *> &list = data.dirty_node_depth_lists[d];
677
678
#if 0
679
if (list.size() > 0) {
680
print_line("depth " + itos(d) + ", contains " + itos(list.size()));
681
}
682
#endif
683
684
for (uint32_t n = 0; n < list.size(); n++) {
685
// Already processed this frame?
686
Node3D *s = list[n];
687
688
if (s->data.fti_processed) {
689
#ifdef DEBUG_ENABLED
690
skipped++;
691
#endif
692
continue;
693
}
694
695
// The first node requires a recursive visibility check
696
// up the tree, because `is_visible()` only returns the node
697
// local flag.
698
if (Object::cast_to<VisualInstance3D>(s)) {
699
if (!s->_is_vi_visible()) {
700
#ifdef DEBUG_ENABLED
701
skipped++;
702
#endif
703
continue;
704
}
705
} else if (!s->is_visible_in_tree()) {
706
#ifdef DEBUG_ENABLED
707
skipped++;
708
#endif
709
continue;
710
}
711
712
_update_dirty_nodes(s, half_frame, interpolation_fraction, true);
713
}
714
}
715
716
_clear_depth_lists();
717
}
718
719
if (print_debug_stats) {
720
uint64_t after = OS::get_singleton()->get_ticks_usec();
721
print_line(String(data.use_optimized_traversal_method ? "FTI optimized" : "FTI reference") + " nodes traversed : " + itos(data.debug_node_count) + (skipped == 0 ? "" : ", skipped " + itos(skipped)) + ", processed : " + itos(data.debug_nodes_processed) + ", took " + itos(after - before) + " usec " + (data.frame_start ? "(start)" : "(end)"));
722
}
723
724
#endif // not GODOT_SCENE_TREE_FTI_VERIFY
725
726
// In theory we could clear the `force_update` flags from the nodes in the traversal.
727
// The problem is that hidden nodes are not recursed into, therefore the flags would
728
// never get cleared and could get out of sync with the forced list.
729
// So instead we are clearing them here manually.
730
// This is not ideal in terms of cache coherence so perhaps another method can be
731
// explored in future.
732
uint32_t forced_list_size = data.frame_xform_list_forced.size();
733
for (uint32_t n = 0; n < forced_list_size; n++) {
734
Node3D *s = data.frame_xform_list_forced[n];
735
s->data.fti_frame_xform_force_update = false;
736
}
737
data.frame_xform_list_forced.clear();
738
739
if (!p_frame_start && data.periodic_debug_log) {
740
data.periodic_debug_log = false;
741
}
742
743
// Update the properties once off at the end of the frame.
744
// No need for two passes for properties.
745
if (!p_frame_start) {
746
for (uint32_t n = 0; n < data.frame_property_list.size(); n++) {
747
Node3D *s = data.frame_property_list[n];
748
s->fti_update_servers_property();
749
}
750
}
751
752
// Marks the end of the frame.
753
// Enables us to recognize when change notifications
754
// come in _during_ a frame (they get treated differently).
755
if (!data.frame_start) {
756
data.in_frame = false;
757
}
758
}
759
760
SceneTreeFTI::SceneTreeFTI() {
761
#ifdef GODOT_SCENE_TREE_FTI_VERIFY
762
_tests = memnew(SceneTreeFTITests(*this));
763
#endif
764
765
Variant traversal_mode_string = GLOBAL_DEF("physics/3d/physics_interpolation/scene_traversal", "DEFAULT");
766
ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "physics/3d/physics_interpolation/scene_traversal", PROPERTY_HINT_ENUM, "DEFAULT,Legacy,Debug"));
767
768
data.traversal_mode = TM_DEFAULT;
769
770
if (traversal_mode_string == "Legacy") {
771
data.traversal_mode = TM_LEGACY;
772
} else if (traversal_mode_string == "Debug") {
773
// Don't allow debug mode in final exports,
774
// it will almost certainly be a mistake.
775
#ifdef DEBUG_ENABLED
776
data.traversal_mode = TM_DEBUG;
777
#else
778
data.traversal_mode = TM_DEFAULT;
779
#endif
780
}
781
782
switch (data.traversal_mode) {
783
default: {
784
print_verbose("SceneTreeFTI: traversal method DEFAULT");
785
} break;
786
case TM_LEGACY: {
787
print_verbose("SceneTreeFTI: traversal method Legacy");
788
} break;
789
case TM_DEBUG: {
790
print_verbose("SceneTreeFTI: traversal method Debug");
791
} break;
792
}
793
794
#ifdef GODOT_SCENE_TREE_FTI_EXTRA_CHECKS
795
print_line("SceneTreeFTI : GODOT_SCENE_TREE_FTI_EXTRA_CHECKS defined");
796
#endif
797
#ifdef GODOT_SCENE_TREE_FTI_PRINT_TREE
798
print_line("SceneTreeFTI : GODOT_SCENE_TREE_FTI_PRINT_TREE defined");
799
#endif
800
}
801
802
SceneTreeFTI::~SceneTreeFTI() {
803
#ifdef GODOT_SCENE_TREE_FTI_VERIFY
804
if (_tests) {
805
memfree(_tests);
806
_tests = nullptr;
807
}
808
#endif
809
}
810
811
#endif // ndef _3D_DISABLED
812
813