From 1c1ad17b4361f0ec0beaae58e6a8c865dfaf9eca Mon Sep 17 00:00:00 2001 From: PouleyKetchoupp Date: Wed, 20 Jan 2021 09:44:21 -0700 Subject: [PATCH] Update broadphase performance test in physics tests Start logging physics tick one frame earlier for each operation Disable debug collision to avoid rendering bottleneck Fixes in adding/removing bodies to avoid bottlenecks outside of physics 2D: Increase message queue size to allow adding more objects at once 3D: Remove camera to disable rendering altogether 3D: Fix error with create_rigidbody_box missing default value --- 2d/physics_tests/project.godot | 4 +++ 2d/physics_tests/test.gd | 7 ++++ .../tests/performance/test_perf_broadphase.gd | 32 +++++++++++-------- .../performance/test_perf_broadphase.tscn | 3 ++ 3d/physics_tests/main.tscn | 2 +- 3d/physics_tests/test.gd | 9 +++++- .../tests/performance/test_perf_broadphase.gd | 32 +++++++++++-------- .../performance/test_perf_broadphase.tscn | 11 +++---- 8 files changed, 66 insertions(+), 34 deletions(-) diff --git a/2d/physics_tests/project.godot b/2d/physics_tests/project.godot index 408a95dd..2e09f2e3 100644 --- a/2d/physics_tests/project.godot +++ b/2d/physics_tests/project.godot @@ -73,6 +73,10 @@ toggle_pause={ ] } +[memory] + +limits/message_queue/max_size_kb=10240 + [rendering] quality/driver/driver_name="GLES2" diff --git a/2d/physics_tests/test.gd b/2d/physics_tests/test.gd index 1dfc8c17..1c312703 100644 --- a/2d/physics_tests/test.gd +++ b/2d/physics_tests/test.gd @@ -4,6 +4,8 @@ extends Node2D signal wait_done() +export var _enable_debug_collision = true + var _timer var _timer_started = false @@ -21,6 +23,11 @@ class Circle2D: var _drawn_nodes = [] +func _enter_tree(): + if not _enable_debug_collision: + get_tree().debug_collisions_hint = false + + func _physics_process(_delta): if _wait_physics_ticks_counter > 0: _wait_physics_ticks_counter -= 1 diff --git a/2d/physics_tests/tests/performance/test_perf_broadphase.gd b/2d/physics_tests/tests/performance/test_perf_broadphase.gd index 21d5edaa..f2b0a9ca 100644 --- a/2d/physics_tests/tests/performance/test_perf_broadphase.gd +++ b/2d/physics_tests/tests/performance/test_perf_broadphase.gd @@ -15,9 +15,14 @@ var _log_physics_time_start = 0 func _ready(): - _create_objects() + yield(start_timer(1.0), "timeout") + if is_timer_canceled(): + return _log_physics_start() + + _create_objects() + yield(wait_for_physics_ticks(5), "wait_done") _log_physics_stop() @@ -25,9 +30,10 @@ func _ready(): if is_timer_canceled(): return + _log_physics_start() + _add_objects() - _log_physics_start() yield(wait_for_physics_ticks(5), "wait_done") _log_physics_stop() @@ -35,9 +41,10 @@ func _ready(): if is_timer_canceled(): return + _log_physics_start() + _move_objects() - _log_physics_start() yield(wait_for_physics_ticks(5), "wait_done") _log_physics_stop() @@ -45,9 +52,10 @@ func _ready(): if is_timer_canceled(): return + _log_physics_start() + _remove_objects() - _log_physics_start() yield(wait_for_physics_ticks(5), "wait_done") _log_physics_stop() @@ -85,9 +93,6 @@ func _log_physics_stop(): func _create_objects(): _objects.clear() - var template_body = create_rigidbody_box(BOX_SIZE) - template_body.gravity_scale = 0.0 - Log.print_log("* Creating objects...") var timer = OS.get_ticks_usec() @@ -97,9 +102,10 @@ func _create_objects(): var pos_y = -0.5 * (column_size - 1) * BOX_SPACE.y for column in column_size: - var box = template_body.duplicate() + # Create a new object and shape every time to avoid the overhead of connecting many bodies to the same shape. + var box = create_rigidbody_box(BOX_SIZE) + box.gravity_scale = 0.0 box.position = Vector2(pos_x, pos_y) - box.name = "Box%03d" % (row * column + 1) _objects.push_back(box) pos_y += BOX_SPACE.y @@ -109,8 +115,6 @@ func _create_objects(): timer = OS.get_ticks_usec() - timer Log.print_log(" Create Time: %.3f ms" % (0.001 * timer)) - template_body.queue_free() - func _add_objects(): var root_node = $Objects @@ -142,8 +146,10 @@ func _remove_objects(): Log.print_log("* Removing objects...") var timer = OS.get_ticks_usec() - for object in _objects: - root_node.remove_child(object) + # Remove objects in reversed order to avoid the overhead of changing children index in parent. + var object_count = _objects.size() + for object_index in object_count: + root_node.remove_child(_objects[object_count - object_index - 1]) timer = OS.get_ticks_usec() - timer Log.print_log(" Remove Time: %.3f ms" % (0.001 * timer)) diff --git a/2d/physics_tests/tests/performance/test_perf_broadphase.tscn b/2d/physics_tests/tests/performance/test_perf_broadphase.tscn index 0cc10fa1..75ea05fe 100644 --- a/2d/physics_tests/tests/performance/test_perf_broadphase.tscn +++ b/2d/physics_tests/tests/performance/test_perf_broadphase.tscn @@ -4,6 +4,9 @@ [node name="Test" type="Node2D"] script = ExtResource( 1 ) +_enable_debug_collision = false +row_size = 300 +column_size = 300 [node name="Objects" type="Node2D" parent="."] position = Vector2( 512, 300 ) diff --git a/3d/physics_tests/main.tscn b/3d/physics_tests/main.tscn index 3fbc6c72..8af1bc56 100644 --- a/3d/physics_tests/main.tscn +++ b/3d/physics_tests/main.tscn @@ -41,7 +41,7 @@ margin_left = 157.0 margin_top = 13.0 margin_right = 375.0 margin_bottom = 27.0 -text = "R - RESTART / D - TOGGLE COLLISION / F - TOGGLE FULL SCREEN / ESC - QUIT" +text = "P - TOGGLE PAUSE / R - RESTART / D - TOGGLE COLLISION / F - TOGGLE FULL SCREEN / ESC - QUIT" __meta__ = { "_edit_use_anchors_": false } diff --git a/3d/physics_tests/test.gd b/3d/physics_tests/test.gd index 6149522f..384d3522 100644 --- a/3d/physics_tests/test.gd +++ b/3d/physics_tests/test.gd @@ -4,6 +4,8 @@ extends Node signal wait_done() +export var _enable_debug_collision = true + var _timer var _timer_started = false @@ -12,6 +14,11 @@ var _wait_physics_ticks_counter = 0 var _drawn_nodes = [] +func _enter_tree(): + if not _enable_debug_collision: + get_tree().debug_collisions_hint = false + + func _physics_process(_delta): if _wait_physics_ticks_counter > 0: _wait_physics_ticks_counter -= 1 @@ -60,7 +67,7 @@ func clear_drawn_nodes(): _drawn_nodes.clear() -func create_rigidbody_box(size, pickable): +func create_rigidbody_box(size, pickable = false): var shape = BoxShape.new() shape.extents = 0.5 * size diff --git a/3d/physics_tests/tests/performance/test_perf_broadphase.gd b/3d/physics_tests/tests/performance/test_perf_broadphase.gd index 0673d0e8..328c6652 100644 --- a/3d/physics_tests/tests/performance/test_perf_broadphase.gd +++ b/3d/physics_tests/tests/performance/test_perf_broadphase.gd @@ -16,9 +16,14 @@ var _log_physics_time_start = 0 func _ready(): - _create_objects() + yield(start_timer(1.0), "timeout") + if is_timer_canceled(): + return _log_physics_start() + + _create_objects() + yield(wait_for_physics_ticks(5), "wait_done") _log_physics_stop() @@ -26,9 +31,10 @@ func _ready(): if is_timer_canceled(): return + _log_physics_start() + _add_objects() - _log_physics_start() yield(wait_for_physics_ticks(5), "wait_done") _log_physics_stop() @@ -36,9 +42,10 @@ func _ready(): if is_timer_canceled(): return + _log_physics_start() + _move_objects() - _log_physics_start() yield(wait_for_physics_ticks(5), "wait_done") _log_physics_stop() @@ -46,9 +53,10 @@ func _ready(): if is_timer_canceled(): return + _log_physics_start() + _remove_objects() - _log_physics_start() yield(wait_for_physics_ticks(5), "wait_done") _log_physics_stop() @@ -86,9 +94,6 @@ func _log_physics_stop(): func _create_objects(): _objects.clear() - var template_body = create_rigidbody_box(BOX_SIZE) - template_body.gravity_scale = 0.0 - Log.print_log("* Creating objects...") var timer = OS.get_ticks_usec() @@ -101,9 +106,10 @@ func _create_objects(): var pos_z = -0.5 * (depth_size - 1) * BOX_SPACE.z for depth in depth_size: - var box = template_body.duplicate() + # Create a new object and shape every time to avoid the overhead of connecting many bodies to the same shape. + var box = create_rigidbody_box(BOX_SIZE) + box.gravity_scale = 0.0 box.transform.origin = Vector3(pos_x, pos_y, pos_z) - box.name = "Box%03d" % (row * column + 1) _objects.push_back(box) pos_z += BOX_SPACE.z @@ -115,8 +121,6 @@ func _create_objects(): timer = OS.get_ticks_usec() - timer Log.print_log(" Create Time: %.3f ms" % (0.001 * timer)) - template_body.queue_free() - func _add_objects(): var root_node = $Objects @@ -148,8 +152,10 @@ func _remove_objects(): Log.print_log("* Removing objects...") var timer = OS.get_ticks_usec() - for object in _objects: - root_node.remove_child(object) + # Remove objects in reversed order to avoid the overhead of changing children index in parent. + var object_count = _objects.size() + for object_index in object_count: + root_node.remove_child(_objects[object_count - object_index - 1]) timer = OS.get_ticks_usec() - timer Log.print_log(" Remove Time: %.3f ms" % (0.001 * timer)) diff --git a/3d/physics_tests/tests/performance/test_perf_broadphase.tscn b/3d/physics_tests/tests/performance/test_perf_broadphase.tscn index b9ca979c..46a6db66 100644 --- a/3d/physics_tests/tests/performance/test_perf_broadphase.tscn +++ b/3d/physics_tests/tests/performance/test_perf_broadphase.tscn @@ -1,13 +1,12 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=2 format=2] [ext_resource path="res://tests/performance/test_perf_broadphase.gd" type="Script" id=1] -[ext_resource path="res://utils/camera_orbit.gd" type="Script" id=5] [node name="Test" type="Spatial"] script = ExtResource( 1 ) +_enable_debug_collision = false +row_size = 50 +column_size = 50 +depth_size = 50 [node name="Objects" type="Spatial" parent="."] - -[node name="Camera" type="Camera" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 29.8407 ) -script = ExtResource( 5 )