Skip to content

Commit d51615b

Browse files
committed
Change navigation map synchronization to an async process
Changes the navigation map synchronization to an async process to avoid stalling the main thread.
1 parent 0454122 commit d51615b

29 files changed

+1266
-481
lines changed

doc/classes/NavigationServer2D.xml

+15
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,13 @@
562562
Returns all navigation regions [RID]s that are currently assigned to the requested navigation [param map].
563563
</description>
564564
</method>
565+
<method name="map_get_use_async_iterations" qualifiers="const">
566+
<return type="bool" />
567+
<param index="0" name="map" type="RID" />
568+
<description>
569+
Returns [code]true[/code] if the [param map] synchronization uses an async process that runs on a background thread.
570+
</description>
571+
</method>
565572
<method name="map_get_use_edge_connections" qualifiers="const">
566573
<return type="bool" />
567574
<param index="0" name="map" type="RID" />
@@ -608,6 +615,14 @@
608615
Set the map's link connection radius used to connect links to navigation polygons.
609616
</description>
610617
</method>
618+
<method name="map_set_use_async_iterations">
619+
<return type="void" />
620+
<param index="0" name="map" type="RID" />
621+
<param index="1" name="enabled" type="bool" />
622+
<description>
623+
If [param enabled] is [code]true[/code] the [param map] synchronization uses an async process that runs on a background thread.
624+
</description>
625+
</method>
611626
<method name="map_set_use_edge_connections">
612627
<return type="void" />
613628
<param index="0" name="map" type="RID" />

doc/classes/NavigationServer3D.xml

+15
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,13 @@
641641
Returns the map's up direction.
642642
</description>
643643
</method>
644+
<method name="map_get_use_async_iterations" qualifiers="const">
645+
<return type="bool" />
646+
<param index="0" name="map" type="RID" />
647+
<description>
648+
Returns [code]true[/code] if the [param map] synchronization uses an async process that runs on a background thread.
649+
</description>
650+
</method>
644651
<method name="map_get_use_edge_connections" qualifiers="const">
645652
<return type="bool" />
646653
<param index="0" name="map" type="RID" />
@@ -711,6 +718,14 @@
711718
Sets the map up direction.
712719
</description>
713720
</method>
721+
<method name="map_set_use_async_iterations">
722+
<return type="void" />
723+
<param index="0" name="map" type="RID" />
724+
<param index="1" name="enabled" type="bool" />
725+
<description>
726+
If [param enabled] is [code]true[/code] the [param map] synchronization uses an async process that runs on a background thread.
727+
</description>
728+
</method>
714729
<method name="map_set_use_edge_connections">
715730
<return type="void" />
716731
<param index="0" name="map" type="RID" />

doc/classes/ProjectSettings.xml

+3
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,9 @@
21652165
<member name="navigation/pathfinding/max_threads" type="int" setter="" getter="" default="4">
21662166
Maximum number of threads that can run pathfinding queries simultaneously on the same pathfinding graph, for example the same navigation map. Additional threads increase memory consumption and synchronization time due to the need for extra data copies prepared for each thread. A value of [code]-1[/code] means unlimited and the maximum available OS processor count is used. Defaults to [code]1[/code] when the OS does not support threads.
21672167
</member>
2168+
<member name="navigation/world/map_use_async_iterations" type="bool" setter="" getter="" default="true">
2169+
If enabled, navigation map synchronization uses an async process that runs on a background thread. This avoids stalling the main thread but adds an additional delay to any navigation map change.
2170+
</member>
21682171
<member name="network/limits/debugger/max_chars_per_second" type="int" setter="" getter="" default="32768">
21692172
Maximum number of characters allowed to send as output from the debugger. Over this value, content is dropped. This helps not to stall the debugger connection.
21702173
</member>

modules/navigation/2d/godot_navigation_server_2d.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ uint32_t GodotNavigationServer2D::map_get_iteration_id(RID p_map) const {
259259
return NavigationServer3D::get_singleton()->map_get_iteration_id(p_map);
260260
}
261261

262+
void GodotNavigationServer2D::map_set_use_async_iterations(RID p_map, bool p_enabled) {
263+
return NavigationServer3D::get_singleton()->map_set_use_async_iterations(p_map, p_enabled);
264+
}
265+
266+
bool GodotNavigationServer2D::map_get_use_async_iterations(RID p_map) const {
267+
return NavigationServer3D::get_singleton()->map_get_use_async_iterations(p_map);
268+
}
269+
262270
void FORWARD_2(map_set_cell_size, RID, p_map, real_t, p_cell_size, rid_to_rid, real_to_real);
263271
real_t FORWARD_1_C(map_get_cell_size, RID, p_map, rid_to_rid);
264272

modules/navigation/2d/godot_navigation_server_2d.h

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class GodotNavigationServer2D : public NavigationServer2D {
7878
virtual void map_force_update(RID p_map) override;
7979
virtual Vector2 map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const override;
8080
virtual uint32_t map_get_iteration_id(RID p_map) const override;
81+
virtual void map_set_use_async_iterations(RID p_map, bool p_enabled) override;
82+
virtual bool map_get_use_async_iterations(RID p_map) const override;
8183

8284
virtual RID region_create() override;
8385
virtual void region_set_enabled(RID p_region, bool p_enabled) override;

modules/navigation/3d/godot_navigation_server_3d.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,19 @@ RID GodotNavigationServer3D::agent_get_map(RID p_agent) const {
364364
return RID();
365365
}
366366

367+
COMMAND_2(map_set_use_async_iterations, RID, p_map, bool, p_enabled) {
368+
NavMap *map = map_owner.get_or_null(p_map);
369+
ERR_FAIL_NULL(map);
370+
map->set_use_async_iterations(p_enabled);
371+
}
372+
373+
bool GodotNavigationServer3D::map_get_use_async_iterations(RID p_map) const {
374+
const NavMap *map = map_owner.get_or_null(p_map);
375+
ERR_FAIL_NULL_V(map, false);
376+
377+
return map->get_use_async_iterations();
378+
}
379+
367380
Vector3 GodotNavigationServer3D::map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const {
368381
const NavMap *map = map_owner.get_or_null(p_map);
369382
ERR_FAIL_NULL_V(map, Vector3());

modules/navigation/3d/godot_navigation_server_3d.h

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ class GodotNavigationServer3D : public NavigationServer3D {
147147
virtual void map_force_update(RID p_map) override;
148148
virtual uint32_t map_get_iteration_id(RID p_map) const override;
149149

150+
COMMAND_2(map_set_use_async_iterations, RID, p_map, bool, p_enabled);
151+
virtual bool map_get_use_async_iterations(RID p_map) const override;
152+
150153
virtual Vector3 map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const override;
151154

152155
virtual RID region_create() override;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**************************************************************************/
2+
/* nav_base_iteration_3d.h */
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 NAV_BASE_ITERATION_3D_H
32+
#define NAV_BASE_ITERATION_3D_H
33+
34+
#include "servers/navigation/navigation_utilities.h"
35+
36+
struct NavBaseIteration {
37+
uint32_t id = UINT32_MAX;
38+
bool enabled = true;
39+
uint32_t navigation_layers = 1;
40+
real_t enter_cost = 0.0;
41+
real_t travel_cost = 1.0;
42+
NavigationUtilities::PathSegmentType owner_type;
43+
ObjectID owner_object_id;
44+
RID owner_rid;
45+
bool owner_use_edge_connections = false;
46+
};
47+
48+
#endif // NAV_BASE_ITERATION_3D_H

0 commit comments

Comments
 (0)