Skip to content

Commit

Permalink
Added Double Precision documentation.
Browse files Browse the repository at this point in the history
Fixed deleting Graphs if there is no other 2d graphics.
Significantly reduced the number of mesh position updates when `fix_precision_enabled` is active.
  • Loading branch information
DmitriySalnikov committed Nov 5, 2024
1 parent 39b8310 commit fa564b0
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 11 deletions.
1 change: 1 addition & 0 deletions Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ INPUT = docs/Introduction.md \
docs/Examples.md \
docs/Export.md \
docs/Build.md \
docs/DoublePrecision.md \
src/debug_draw_manager.h \
src/2d/debug_draw_2d.h \
src/2d/config_2d.h \
Expand Down
4 changes: 2 additions & 2 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def setup_options(env: SConsEnvironment, arguments):
opts.Add(BoolVariable("telemetry_enabled", "Enable the telemetry module", False))
opts.Add(BoolVariable("tracy_enabled", "Enable tracy profiler", False))
opts.Add(BoolVariable("force_enabled_dd3d", "Keep the rendering code in the release build", False))
opts.Add(BoolVariable("fix_precision_enabled", "Fix precision errors at greater distances, utilizing more CPU resources.", True))
opts.Add(BoolVariable("shader_world_coords_enabled", "Use world coordinates in shaders, if applicable.\nDisable it for stability at a great distance from the center of the world.", True))
opts.Add(BoolVariable("fix_precision_enabled", "Fix precision errors at greater distances, utilizing more CPU resources.\nApplies only in combination with 'precision=double'", True))
opts.Add(BoolVariable("shader_world_coords_enabled", "Use world coordinates in shaders, if applicable.\nExpandable meshes become more uniform.\nDisable it for stability at a great distance from the center of the world.", True))
opts.Add(BoolVariable("lto", "Link-time optimization", False))

opts.Update(env)
Expand Down
50 changes: 50 additions & 0 deletions docs/DoublePrecision.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Double Precision

[Godot Docs - Large world coordinates](https://docs.godotengine.org/en/latest/tutorials/physics/large_world_coordinates.html)

This library supports building with `precision=double`, but does not provide pre-compiled binaries, as does Godot itself.

Before building with double precision, it is recommended to read the [Build](Build.md) tips.

## Compilation flags

By default, you can simply run scons with the desired platform and the `precision=double` flag:

```python
scons platform=windows target=editor arch=x86_64 precision=double
```

But there are also a few additional flags that can affect the visual representation of meshes.

@note
The videos were captured at the position `Vector3(16_000_000, 0, 0)`.

### `fix_precision_enabled=yes|no` (default: yes)

This flag enables a hack that moves the centers of all multimesh to the camera position if the camera has moved too far away. If this flag is disabled, then all meshes will always be drawn relative to the center of the world.

Enabling or disabling the `fix_precision_enabled` flag only affects if `precision=double` is enabled.

![fix_precision_enabled](images/fix_precision_enabled.webp)

### `shader_world_coords_enabled=yes|no` (default: yes)

This flag changes how meshes using world coordinates will be displayed. This category includes all volumetric objects. Wireframe meshes are not affected by this flag.

`shader_world_coords_enabled` flag can be toggled independently of `precision=double`.

![shader_world_coords_enabled](images/shader_world_coords_enabled.webp)

But enabling world coordinates allows normalization of volumetric meshes within `float` precision.

![shader_world_coords_enabled_origin](images/shader_world_coords_enabled_origin.png)

## Examples

```python
# Disable world coordinates for shaders and enable position fix:
scons platform=windows target=editor arch=x86_64 precision=double shader_world_coords_enabled=no fix_precision_enabled=yes

# Leave the world coordinates enabled and disable the position fix::
scons platform=windows target=editor arch=x86_64 precision=double fix_precision_enabled=no
```
Binary file added docs/images/fix_precision_enabled.webp
Binary file not shown.
Binary file added docs/images/shader_world_coords_enabled.webp
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extends Node3D
@export var update_in_physics := false
@export var test_text := true
@export var test_graphs := false
@export var test_fps_graph := true
@export var more_test_cases := true
@export var draw_array_of_boxes := false
@export var draw_1m_boxes := false
Expand Down Expand Up @@ -540,6 +541,8 @@ func _get_sin_wave_for_graph() -> float:


func _remove_graphs():
if not test_fps_graph:
DebugDraw2D.remove_graph(&"FPS")
DebugDraw2D.remove_graph(&"randf")
DebugDraw2D.remove_graph(&"fps")
DebugDraw2D.remove_graph(&"fps2")
Expand Down
3 changes: 3 additions & 0 deletions examples_dd3d/DebugDrawDemoSceneCS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public partial class DebugDrawDemoSceneCS : Node3D
[Export] bool update_in_physics = false;
[Export] bool test_text = true;
[Export] bool test_graphs = false;
[Export] bool test_fps_graph = true;
[Export] bool more_test_cases = true;
[Export] bool draw_array_of_boxes = false;
[Export] bool draw_1m_boxes = false;
Expand Down Expand Up @@ -824,6 +825,8 @@ float _get_sin_wave_for_graph()

void _remove_graphs()
{
if (!test_fps_graph)
DebugDraw2D.RemoveGraph("FPS");
DebugDraw2D.RemoveGraph("randf");
DebugDraw2D.RemoveGraph("fps");
DebugDraw2D.RemoveGraph("fps2");
Expand Down
15 changes: 13 additions & 2 deletions src/2d/graphs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ DebugDraw2DGraph::DebugDraw2DGraph(DataGraphManager *_owner, StringName _title)

DebugDraw2DGraph::~DebugDraw2DGraph() {
custom_font.unref();
#ifndef DISABLE_DEBUG_RENDERING
if (owner) {
owner->mark_canvas_dirty();
}
#endif
}

void DebugDraw2DGraph::_init(DataGraphManager *_owner, StringName _title) {
Expand Down Expand Up @@ -487,7 +492,7 @@ DebugDraw2DGraph::graph_rects DebugDraw2DGraph::draw(CanvasItem *_ci, const Ref<
if (buffer_data->is_filled() || buffer_data->size() > 2) {
Vector2 base_pos = rects.base.position + Vector2(1, 1);
Vector2 size = rects.base.size - Vector2(1, 3);
double difference = max - min;
// double difference = max - min;
double size_multiplier_x = size.x / (get_buffer_size() - 2);
// double size_multiplier_y = difference != 0.0 ? size.y / Math::remap(buffer_data->get(0), min, max, graph_range.min, graph_range.max) : 0.0;
PackedVector2Array line_points;
Expand Down Expand Up @@ -675,6 +680,12 @@ DataGraphManager::~DataGraphManager() {
graphs.clear();
}

void DataGraphManager::mark_canvas_dirty() {
if (owner) {
owner->mark_canvas_dirty();
}
}

void DataGraphManager::draw(CanvasItem *_ci, Ref<Font> _font, Vector2 _vp_size, double _delta) const {
ZoneScoped;
LOCK_GUARD(datalock);
Expand Down Expand Up @@ -720,7 +731,7 @@ void DataGraphManager::draw(CanvasItem *_ci, Ref<Font> _font, Vector2 _vp_size,
// 'rect' is a parameter storing the rectangle of the parent node
// 'corner' is a parameter inherited from the root node
std::function<void(const GraphsNode *, const DebugDraw2DGraph::graph_rects &, const DebugDraw2DGraph::GraphPosition &, const bool &)> iterate_nodes;
iterate_nodes = [&, this](const GraphsNode *node, const DebugDraw2DGraph::graph_rects &rects, const DebugDraw2DGraph::GraphPosition &corner, const bool &is_root) {
iterate_nodes = [&](const GraphsNode *node, const DebugDraw2DGraph::graph_rects &rects, const DebugDraw2DGraph::GraphPosition &corner, const bool &is_root) {
DebugDraw2DGraph::graph_rects prev_rects = node->instance->draw(_ci, _font, rects, corner, is_root, _delta);

for (auto &g : node->children) {
Expand Down
1 change: 1 addition & 0 deletions src/2d/graphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ class DataGraphManager {
DataGraphManager(class DebugDraw2D *root);
~DataGraphManager();

void mark_canvas_dirty();
void draw(CanvasItem *_ci, Ref<Font> _font, Vector2 _vp_size, double _delta) const;
Ref<DebugDraw2DGraph> create_graph(const StringName &_title);
Ref<DebugDraw2DGraph> create_fps_graph(const StringName &_title);
Expand Down
14 changes: 8 additions & 6 deletions src/3d/debug_geometry_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ const Vector3 &DebugGeometryContainer::get_center_position() {
}

void DebugGeometryContainer::update_center_positions() {
if (center_position == prev_center_position)
if (center_position.distance_to(new_center_position) < 8192)
return;

Vector3 pos_diff = prev_center_position - center_position;
prev_center_position = center_position;
DEV_PRINT_STD(NAMEOF(DebugGeometryContainer) " Updated instance positions: %s, World3D (%d)\n", no_depth_test ? "NoDepth" : "Normal", viewport_world.is_valid() ? viewport_world->get_instance_id() : 0);

Vector3 pos_diff = center_position - new_center_position;
center_position = new_center_position;

geometry_pool.for_each_instance([&pos_diff](DelayedRendererInstance *i) {
i->data.origin_x += (float)pos_diff.x;
Expand Down Expand Up @@ -240,7 +242,7 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
frustum_arrays.push_back({ cam->get_frustum(), cam });

#ifdef FIX_DOUBLE_PRECISION_ERRORS
center_position = cam->get_global_position();
new_center_position = cam->get_global_position();
#endif
} else if (custom_editor_viewports.size() > 0) {
bool is_updated = false;
Expand All @@ -253,7 +255,7 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
if (!is_updated) {
is_updated = true;
#ifdef FIX_DOUBLE_PRECISION_ERRORS
center_position = vp_cam->get_global_position();
new_center_position = vp_cam->get_global_position();
#endif
}
}
Expand All @@ -267,7 +269,7 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
frustum_arrays.push_back({ vp_cam->get_frustum(), vp_cam });

#ifdef FIX_DOUBLE_PRECISION_ERRORS
center_position = vp_cam->get_global_position();
new_center_position = vp_cam->get_global_position();
#endif
}
#ifdef DEBUG_ENABLED
Expand Down
2 changes: 1 addition & 1 deletion src/3d/debug_geometry_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DebugGeometryContainer {
Ref<World3D> viewport_world;
#if defined(REAL_T_IS_DOUBLE) && defined(FIX_PRECISION_ENABLED)
Vector3 center_position;
Vector3 prev_center_position;
Vector3 new_center_position;
#endif

int32_t render_layers = 1;
Expand Down

0 comments on commit fa564b0

Please sign in to comment.