From a93e6772d62acd2080bc8b17e8568fedc5e33a83 Mon Sep 17 00:00:00 2001 From: DmitriySalnikov Date: Mon, 4 Nov 2024 19:51:58 +0300 Subject: [PATCH] `precision=double` fixes Added the ability to disable world coordinates for shaders. Added the ability to fix the position of instances. `ScopedPairIdConfig` is now a structure. Added a class `ViewportToDebugContainerItem` for storing containers of the world and viewports. Replaced `typedef` with `using`. Fixed warnings about converting `double` to `float`. --- SConstruct | 11 ++ .../debug_draw_3d/debug_draw_3d.gdextension | 50 +++++++ dev_debug_draw_3d_Library.sln | 3 + examples_dd3d/DebugDrawDemoScene.gd | 2 +- examples_dd3d/DebugDrawDemoSceneCS.cs | 2 +- src/2d/debug_draw_2d.cpp | 5 +- src/2d/debug_draw_2d.h | 3 +- src/2d/grouped_text.cpp | 2 +- src/2d/grouped_text.h | 4 +- src/3d/config_scope_3d.h | 2 +- src/3d/debug_draw_3d.cpp | 140 +++++++++++------- src/3d/debug_draw_3d.h | 33 +++-- src/3d/debug_geometry_container.cpp | 78 ++++++++-- src/3d/debug_geometry_container.h | 12 +- src/3d/geometry_generators.cpp | 68 ++++----- src/3d/geometry_generators.h | 12 +- src/3d/render_instances.cpp | 2 +- src/3d/render_instances.h | 6 +- src/common/i_scope_storage.h | 2 +- src/debug_draw_manager.cpp | 28 +++- src/debug_draw_manager.h | 3 + src/dev_debug_draw_3d_Library.vcxproj | 52 +++++++ src/editor/generate_csharp_bindings.h | 4 +- src/resources/extendable_meshes.gdshader | 13 +- src/utils/compiler.h | 4 +- src/utils/utils.h | 4 +- 26 files changed, 402 insertions(+), 143 deletions(-) diff --git a/SConstruct b/SConstruct index c7e1f8e5..fd6638e3 100644 --- a/SConstruct +++ b/SConstruct @@ -48,6 +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("lto", "Link-time optimization", False)) opts.Update(env) @@ -84,6 +86,12 @@ def setup_defines_and_flags(env: SConsEnvironment, src_out): env.Append(CPPDEFINES=["TRACY_ENABLE", "TRACY_ON_DEMAND", "TRACY_DELAYED_INIT", "TRACY_MANUAL_LIFETIME"]) src_out.append("thirdparty/tracy/public/TracyClient.cpp") + if env["fix_precision_enabled"]: + env.Append(CPPDEFINES=["FIX_PRECISION_ENABLED"]) + + if not env["shader_world_coords_enabled"]: + env.Append(CPPDEFINES=["DISABLE_SHADER_WORLD_COORDS"]) + if env.get("is_msvc", False): env.Append(LINKFLAGS=["/WX:NO"]) @@ -162,6 +170,9 @@ extra_tags = "" if "release" in env["target"] and env["force_enabled_dd3d"]: extra_tags += ".enabled" +if env.get("precision", "single") == "double": + extra_tags += ".double" + lib_utils.get_library_object( env, project_name, lib_name, extra_tags, env["addon_output_dir"], src_folder, additional_src ) diff --git a/addons/debug_draw_3d/debug_draw_3d.gdextension b/addons/debug_draw_3d/debug_draw_3d.gdextension index 523bd4a0..9017632c 100644 --- a/addons/debug_draw_3d/debug_draw_3d.gdextension +++ b/addons/debug_draw_3d/debug_draw_3d.gdextension @@ -101,3 +101,53 @@ web.template_release.wasm32.nothreads.forced_dd3d = "libs/libdd3d.web.template_r web.template_release.wasm32.forced_dd3d = "libs/libdd3d.web.template_release.wasm32.threads.enabled.wasm" ios.template_release.forced_dd3d = "libs/libdd3d.ios.template_release.universal.enabled.dylib" + +; ------------------------------------- +; DOUBLE PRECISION +; ------------------------------------- + +; ------------------------------------- +; debug + +macos.double = "libs/libdd3d.macos.editor.universal.double.framework" +windows.x86_64.double = "libs/libdd3d.windows.editor.x86_64.double.dll" +linux.x86_64.double = "libs/libdd3d.linux.editor.x86_64.double.so" + +web.wasm32.nothreads.double = "libs/libdd3d.web.template_debug.wasm32.double.wasm" +web.wasm32.double = "libs/libdd3d.web.template_debug.wasm32.threads.double.wasm" + +android.arm32.double = "libs/libdd3d.android.template_debug.arm32.double.so" +android.arm64.double = "libs/libdd3d.android.template_debug.arm64.double.so" +android.x86_32.double = "libs/libdd3d.android.template_debug.x86_32.double.so" +android.x86_64.double = "libs/libdd3d.android.template_debug.x86_64.double.so" + +ios.double = "libs/libdd3d.ios.template_debug.universal.dylib" + +; ------------------------------------- +; release no debug draw + +macos.template_release.double = "libs/libdd3d.macos.template_release.universal.double.framework" +windows.template_release.x86_64.double = "libs/libdd3d.windows.template_release.x86_64.double.dll" +linux.template_release.x86_64.double = "libs/libdd3d.linux.template_release.x86_64.double.so" + +web.template_release.wasm32.nothreads.double = "libs/libdd3d.web.template_release.wasm32.double.wasm" +web.template_release.wasm32.double = "libs/libdd3d.web.template_release.wasm32.threads.double.wasm" + +android.template_release.arm32.double = "libs/libdd3d.android.template_release.arm32.double.so" +android.template_release.arm64.double = "libs/libdd3d.android.template_release.arm64.double.so" +android.template_release.x86_32.double = "libs/libdd3d.android.template_release.x86_32.double.so" +android.template_release.x86_64.double = "libs/libdd3d.android.template_release.x86_64.double.so" + +ios.template_release.double = "libs/libdd3d.ios.template_release.universal.double.dylib" + +; ------------------------------------- +; release forced debug draw + +macos.template_release.forced_dd3d.double = "libs/libdd3d.macos.template_release.universal.enabled.double.framework" +windows.template_release.x86_64.forced_dd3d.double = "libs/libdd3d.windows.template_release.x86_64.enabled.double.dll" +linux.template_release.x86_64.forced_dd3d.double = "libs/libdd3d.linux.template_release.x86_64.enabled.double.so" + +web.template_release.wasm32.nothreads.forced_dd3d.double = "libs/libdd3d.web.template_release.wasm32.enabled.double.wasm" +web.template_release.wasm32.forced_dd3d.double = "libs/libdd3d.web.template_release.wasm32.threads.enabled.double.wasm" + +ios.template_release.forced_dd3d.double = "libs/libdd3d.ios.template_release.universal.enabled.double.dylib" diff --git a/dev_debug_draw_3d_Library.sln b/dev_debug_draw_3d_Library.sln index 7551ee1e..06bd9c60 100644 --- a/dev_debug_draw_3d_Library.sln +++ b/dev_debug_draw_3d_Library.sln @@ -7,12 +7,15 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "debug_draw_3d_Library", "sr EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + editor_dev_double|x64 = editor_dev_double|x64 editor_dev|x64 = editor_dev|x64 editor|x64 = editor|x64 release_forced|x64 = release_forced|x64 template_release|x64 = template_release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A265372D-FCD7-4FBD-9F8C-0D31F6D0F557}.editor_dev_double|x64.ActiveCfg = editor_dev_double|x64 + {A265372D-FCD7-4FBD-9F8C-0D31F6D0F557}.editor_dev_double|x64.Build.0 = editor_dev_double|x64 {A265372D-FCD7-4FBD-9F8C-0D31F6D0F557}.editor_dev|x64.ActiveCfg = editor_dev|x64 {A265372D-FCD7-4FBD-9F8C-0D31F6D0F557}.editor_dev|x64.Build.0 = editor_dev|x64 {A265372D-FCD7-4FBD-9F8C-0D31F6D0F557}.editor|x64.ActiveCfg = editor|x64 diff --git a/examples_dd3d/DebugDrawDemoScene.gd b/examples_dd3d/DebugDrawDemoScene.gd index 6d560aec..196395e9 100644 --- a/examples_dd3d/DebugDrawDemoScene.gd +++ b/examples_dd3d/DebugDrawDemoScene.gd @@ -460,7 +460,7 @@ func _draw_array_of_boxes(): var size = Vector3.ONE cfg.set_thickness(randf_range(0, 0.1)) #var size = Vector3(randf_range(0.1, 100),randf_range(0.1, 100),randf_range(0.1, 100)) - DebugDraw3D.draw_box(Vector3(x * mul, (-4-z) * mul, y * mul), Quaternion.IDENTITY, size, DebugDraw3D.empty_color, false, cubes_max_time) + DebugDraw3D.draw_box(Vector3(x * mul, (-4-z) * mul, y * mul) + global_position, Quaternion.IDENTITY, size, DebugDraw3D.empty_color, false, cubes_max_time) #print("Draw Cubes: %fms" % ((Time.get_ticks_usec() - _start_time) / 1000.0)) timer_cubes = cubes_max_time diff --git a/examples_dd3d/DebugDrawDemoSceneCS.cs b/examples_dd3d/DebugDrawDemoSceneCS.cs index d45c4eb2..523f143d 100644 --- a/examples_dd3d/DebugDrawDemoSceneCS.cs +++ b/examples_dd3d/DebugDrawDemoSceneCS.cs @@ -743,7 +743,7 @@ void _draw_array_of_boxes() var size = Vector3.One; cfg.SetThickness(Random.Shared.NextSingle() * 0.1f); //size = new Vector3(Random.Shared.NextSingle() * 100 + 0.1f, Random.Shared.NextSingle() * 100 + 0.1f, Random.Shared.NextSingle() * 100 + 0.1f); - DebugDraw3D.DrawBox(new Vector3(x * mul, (-4 - z) * mul, y * mul), Quaternion.Identity, size, null, false, cubes_max_time); + DebugDraw3D.DrawBox(new Vector3(x * mul, (-4 - z) * mul, y * mul) + GlobalPosition, Quaternion.Identity, size, null, false, cubes_max_time); } } } diff --git a/src/2d/debug_draw_2d.cpp b/src/2d/debug_draw_2d.cpp index 15ced9b0..2161937e 100644 --- a/src/2d/debug_draw_2d.cpp +++ b/src/2d/debug_draw_2d.cpp @@ -109,7 +109,10 @@ DebugDraw2D::~DebugDraw2D() { static bool DebugDraw2D_frame_mark_2d_started = false; #endif -void DebugDraw2D::process(double delta) { +void DebugDraw2D::process_start(double delta) { +} + +void DebugDraw2D::process_end(double delta) { ZoneScoped; #ifdef TRACY_ENABLE diff --git a/src/2d/debug_draw_2d.h b/src/2d/debug_draw_2d.h index bab49486..d9c87906 100644 --- a/src/2d/debug_draw_2d.h +++ b/src/2d/debug_draw_2d.h @@ -58,7 +58,8 @@ class DebugDraw2D : public Object { void _on_canvas_item_draw(Control *ci); inline bool _is_enabled_override() const; - void process(double delta); + void process_start(double delta); + void process_end(double delta); void physics_process_start(double delta); void physics_process_end(double delta); diff --git a/src/2d/grouped_text.cpp b/src/2d/grouped_text.cpp index e4a570cf..58657436 100644 --- a/src/2d/grouped_text.cpp +++ b/src/2d/grouped_text.cpp @@ -296,7 +296,7 @@ void GroupedText::draw(CanvasItem *p_ci, const Ref &p_font, const Vector2 const Vector2 text_padding = owner->get_config()->get_text_padding(); const Vector2 font_offset = Vector2(0, (real_t)draw_font->get_ascent(font_size)) + text_padding; - float size_right_revert = (size.x + text_padding.x * 2) * right_side_multiplier; + real_t size_right_revert = (size.x + text_padding.x * 2) * right_side_multiplier; backgrounds.push_back(DrawRectInstance( Rect2(Vector2(pos.x + size_right_revert, pos.y).floor(), Vector2(size.x + text_padding.x * 2, size.y + text_padding.y * 2).floor()), owner->get_config()->get_text_background_color())); diff --git a/src/2d/grouped_text.h b/src/2d/grouped_text.h index 40dd8501..b4635b79 100644 --- a/src/2d/grouped_text.h +++ b/src/2d/grouped_text.h @@ -35,7 +35,7 @@ class TextGroupItem { bool is_expired(); }; -typedef std::shared_ptr TextGroupItem_ptr; +using TextGroupItem_ptr = std::shared_ptr; class TextGroup { private: @@ -74,7 +74,7 @@ class TextGroup { void cleanup_texts(const std::function &p_update, const double &p_delta); }; -typedef std::shared_ptr TextGroup_ptr; +using TextGroup_ptr = std::shared_ptr; class GroupedText { struct DrawRectInstance { diff --git a/src/3d/config_scope_3d.h b/src/3d/config_scope_3d.h index 7e80ed73..e126e48d 100644 --- a/src/3d/config_scope_3d.h +++ b/src/3d/config_scope_3d.h @@ -66,7 +66,7 @@ class DebugDraw3DScopeConfig : public RefCounted { uint64_t thread_id; uint64_t guard_id; - typedef std::function unregister_func; + using unregister_func = std::function; unregister_func unregister_action; public: diff --git a/src/3d/debug_draw_3d.cpp b/src/3d/debug_draw_3d.cpp index 08e47830..c2c2a455 100644 --- a/src/3d/debug_draw_3d.cpp +++ b/src/3d/debug_draw_3d.cpp @@ -13,10 +13,15 @@ GODOT_WARNING_DISABLE() #include #include +#ifndef DISABLE_DEBUG_RENDERING +#include + // save meshes #if !defined(DISABLE_DEBUG_RENDERING) && defined(DEV_ENABLED) #include #include +#endif + #endif GODOT_WARNING_RESTORE() @@ -48,6 +53,22 @@ _DD3D_WorldWatcher::_DD3D_WorldWatcher(DebugDraw3D *p_root, uint64_t p_world_id) m_owner = p_root; m_world_id = p_world_id; } + +DebugDraw3D::ViewportToDebugContainerItem::ViewportToDebugContainerItem() : + world_id(0), dgcs() { +} + +DebugDraw3D::ViewportToDebugContainerItem::ViewportToDebugContainerItem(ViewportToDebugContainerItem &&other) noexcept + : world_id(std::exchange(other.world_id, 0)) { + for (size_t i = 0; i < (int)MeshMaterialVariant::MAX; i++) { + dgcs[i] = std::move(other.dgcs[i]); + } +} + +DebugDraw3D::ViewportToDebugContainerItem::~ViewportToDebugContainerItem() { + for (auto &i : dgcs) + i = nullptr; +} #endif DebugDraw3D *DebugDraw3D::singleton = nullptr; @@ -182,8 +203,6 @@ void DebugDraw3D::init(DebugDrawManager *p_root) { default_scoped_config->set_plane_size(def_plane_size == 0 ? INFINITY : def_plane_size); _load_materials(); - - // Need to call_deferred `regenerate_geometry_meshes` outside this methods in Manager } DebugDraw3D::~DebugDraw3D() { @@ -193,14 +212,20 @@ DebugDraw3D::~DebugDraw3D() { root_node = nullptr; } -void DebugDraw3D::process(double p_delta) { +void DebugDraw3D::process_start(double delta) { +#ifndef DISABLE_DEBUG_RENDERING + +#endif +} + +void DebugDraw3D::process_end(double p_delta) { ZoneScoped; #ifndef DISABLE_DEBUG_RENDERING FrameMarkStart("3D Update"); // Update 3D debug for (const auto &p : debug_containers) { - for (const auto &dgc : p.second) { + for (const auto &dgc : p.second.dgcs) { if (dgc) { dgc->update_geometry(p_delta); } @@ -220,7 +245,7 @@ void DebugDraw3D::physics_process_start(double p_delta) { FrameMarkStart("3D Physics Step"); for (const auto &p : debug_containers) { - for (const auto &dgc : p.second) { + for (const auto &dgc : p.second.dgcs) { if (dgc) { dgc->update_geometry_physics_start(p_delta); } @@ -233,7 +258,7 @@ void DebugDraw3D::physics_process_end(double p_delta) { ZoneScoped; #ifndef DISABLE_DEBUG_RENDERING for (const auto &p : debug_containers) { - for (const auto &dgc : p.second) { + for (const auto &dgc : p.second.dgcs) { if (dgc) { dgc->update_geometry_physics_end(p_delta); } @@ -259,7 +284,7 @@ const std::shared_ptr DebugDraw3D::scoped_config_f if (it_v != scoped_configs.cend()) { const auto &cfgs = it_v->second; if (!cfgs.empty()) { - DebugDraw3DScopeConfig *tmp = cfgs.back().second; + DebugDraw3DScopeConfig *tmp = cfgs.back().scfg; cached_scoped_configs[thread] = tmp->data; return tmp->data; } @@ -285,14 +310,14 @@ void DebugDraw3D::_unregister_scoped_config(uint64_t thread_id, uint64_t guard_i LOCK_GUARD(datalock); auto &cfgs = scoped_configs[thread_id]; - auto res = std::find_if(cfgs.rbegin(), cfgs.rend(), [&guard_id](const ScopedPairIdConfig &i) { return i.first == guard_id; }); + auto res = std::find_if(cfgs.rbegin(), cfgs.rend(), [&guard_id](const ScopedPairIdConfig &i) { return i.id == guard_id; }); if (res != cfgs.rend()) { cfgs.erase(--res.base()); // Update cached value if (!cfgs.empty()) { - cached_scoped_configs[thread_id] = cfgs.back().second->data; + cached_scoped_configs[thread_id] = cfgs.back().scfg->data; } else { cached_scoped_configs[thread_id] = default_scoped_config.ptr()->data; } @@ -332,12 +357,12 @@ std::array, 2> *DebugDraw3D::get_shared_meshes() { bool p_use_icosphere_hd = PS()->get_setting(root_settings_section + s_use_icosphere_hd); shared_generated_meshes.resize((int)InstanceType::MAX); - for (int i = 0; i < 2; i++) { + for (int i = 0; i < (int)MeshMaterialVariant::MAX; i++) { MeshMaterialType mat_type = MeshMaterialType::Wireframe; #define GEN_MESH(_type, _gen) \ shared_generated_meshes[(int)_type][i] = _gen; \ - shared_generated_meshes[(int)_type][i]->surface_set_material(0, get_material_variant(mat_type, i == 0 ? MeshMaterialVariant::Normal : MeshMaterialVariant::NoDepth)); + shared_generated_meshes[(int)_type][i]->surface_set_material(0, get_material_variant(mat_type, (MeshMaterialVariant)i)); // WIREFRAME @@ -378,11 +403,7 @@ std::array, 2> *DebugDraw3D::get_shared_meshes() { return shared_generated_meshes.data(); } -std::shared_ptr DebugDraw3D::create_debug_container(bool p_no_depth_test) { - return std::make_shared(this, p_no_depth_test); -} - -std::shared_ptr DebugDraw3D::get_debug_container(const DebugDraw3DScopeConfig::DebugContainerDependent &p_dgcd, const bool p_generate_new_container) { +DebugGeometryContainer *DebugDraw3D::get_debug_container(const DebugDraw3DScopeConfig::DebugContainerDependent &p_dgcd, const bool p_generate_new_container) { ZoneScoped; LOCK_GUARD(datalock); @@ -393,37 +414,35 @@ std::shared_ptr DebugDraw3D::get_debug_container(const D int dgc_depth = !!p_dgcd.no_depth_test; if (const auto &cached_world = viewport_to_world_cache.find(p_dgcd.viewport); - cached_world != viewport_to_world_cache.end() && cached_world->second.dgcs[dgc_depth]) { - return cached_world->second.dgcs[dgc_depth]; + cached_world != viewport_to_world_cache.end() && cached_world->second->dgcs[dgc_depth]) { + return cached_world->second->dgcs[dgc_depth].get(); } Ref vp_world = p_dgcd.viewport->find_world_3d(); uint64_t vp_world_id = vp_world->get_instance_id(); if (const auto &dgc_pair = debug_containers.find(vp_world_id); - dgc_pair != debug_containers.end() && dgc_pair->second[dgc_depth]) { + dgc_pair != debug_containers.end() && dgc_pair->second.dgcs[dgc_depth]) { - auto &cache = viewport_to_world_cache[p_dgcd.viewport]; - cache.world_id = dgc_pair->first; - cache.dgcs[dgc_depth] = dgc_pair->second[dgc_depth]; - return dgc_pair->second[dgc_depth]; + viewport_to_world_cache[p_dgcd.viewport] = &dgc_pair->second; + return dgc_pair->second.dgcs[dgc_depth].get(); } if (!p_generate_new_container) { return nullptr; } - auto dgc = create_debug_container(p_dgcd.no_depth_test); - dgc->set_world(vp_world); - debug_containers[vp_world_id][dgc_depth] = dgc; + // create or update container storage + auto &c = debug_containers[vp_world_id]; + c.world_id = vp_world_id; + c.dgcs[dgc_depth] = std::make_unique(this, p_dgcd.no_depth_test); + c.dgcs[dgc_depth]->set_world(vp_world); - auto &cache = viewport_to_world_cache[p_dgcd.viewport]; - cache.world_id = vp_world_id; - cache.dgcs[dgc_depth] = dgc; + viewport_to_world_cache[p_dgcd.viewport] = &debug_containers[vp_world_id]; call_deferred(NAMEOF(_register_viewport_world_deferred), _get_root_world_viewport(p_dgcd.viewport)->get_instance_id(), vp_world_id); - return dgc; + return c.dgcs[dgc_depth].get(); } void DebugDraw3D::_register_viewport_world_deferred(uint64_t /*Viewport * */ vp_id, const uint64_t p_world_id) { @@ -465,7 +484,7 @@ void DebugDraw3D::_remove_debug_container(const uint64_t &p_world_id) { std::vector viewport_to_remove; for (const auto &p : viewport_to_world_cache) { - if (p.second.world_id == p_world_id) { + if (p.second->world_id == p_world_id) { viewport_to_remove.push_back(p.first); } } @@ -547,6 +566,10 @@ void DebugDraw3D::_load_materials() { } } +#ifdef DISABLE_SHADER_WORLD_COORDS + prefix += "#define NO_WORLD_COORD\n"; +#endif + LOAD_SHADER(mesh_shaders[(int)MeshMaterialType::Wireframe][variant], prefix + DD3DResources::src_resources_wireframe_unshaded_gdshader); LOAD_SHADER(mesh_shaders[(int)MeshMaterialType::Billboard][variant], prefix + DD3DResources::src_resources_billboard_unshaded_gdshader); LOAD_SHADER(mesh_shaders[(int)MeshMaterialType::Plane][variant], prefix + DD3DResources::src_resources_plane_unshaded_gdshader); @@ -619,7 +642,7 @@ Ref DebugDraw3D::get_render_stats() { stats_3d.instantiate(); for (const auto &p : debug_containers) { - for (const auto &dgc : p.second) { + for (const auto &dgc : p.second.dgcs) { if (dgc) { dgc->get_render_stats(stats_3d); res->combine_with(stats_3d); @@ -661,12 +684,13 @@ void DebugDraw3D::regenerate_geometry_meshes() { for (auto &p : debug_containers) { for (int i = 0; i < 2; i++) { - if (p.second[i]) { - Ref old_world = p.second[i]->get_world(); - bool no_depth = p.second[i]->is_no_depth_test(); + auto &dgc = p.second.dgcs[i]; + if (dgc) { + Ref old_world = dgc->get_world(); + bool is_no_depth = dgc->is_no_depth_test(); - p.second[i] = create_debug_container(no_depth); - p.second[i]->set_world(old_world); + dgc = std::make_unique(this, is_no_depth); + dgc->set_world(old_world); } } } @@ -677,7 +701,7 @@ void DebugDraw3D::clear_all() { ZoneScoped; #ifndef DISABLE_DEBUG_RENDERING for (auto &p : debug_containers) { - for (const auto &dgc : p.second) { + for (const auto &dgc : p.second.dgcs) { if (dgc) { dgc->clear_3d_objects(); } @@ -693,14 +717,19 @@ void DebugDraw3D::clear_all() { #define GET_PROC_TYPE() (Engine::get_singleton()->is_in_physics_frame() ? ProcessType::PHYSICS_PROCESS : ProcessType::PROCESS) #define CHECK_BEFORE_CALL() \ if (NEED_LEAVE || config->is_freeze_3d_render()) return; -#endif #define GET_SCOPED_CFG_AND_DGC() \ auto scfg = scoped_config_for_current_thread(); \ auto dgc = get_debug_container(scfg->dcd, true); \ if (!dgc) return -#ifndef DISABLE_DEBUG_RENDERING +#if defined(REAL_T_IS_DOUBLE) && defined(FIX_PRECISION_ENABLED) +#define FIX_PRECISION_TRANSFORM(xf) Transform3D(xf.basis, xf.origin - dgc->get_center_position()) +#define FIX_PRECISION_POSITION(pos) (pos - dgc->get_center_position()) +#else +#define FIX_PRECISION_TRANSFORM(xf) (xf) +#define FIX_PRECISION_POSITION(pos) (pos) +#endif #ifdef DEV_ENABLED void DebugDraw3D::_save_generated_meshes() { @@ -739,6 +768,11 @@ void DebugDraw3D::add_or_update_line_with_thickness(real_t p_exp_time, std::uniq GET_SCOPED_CFG_AND_DGC(); if (!scfg->thickness) { +#if defined(REAL_T_IS_DOUBLE) && defined(FIX_PRECISION_ENABLED) + for (size_t l = 0; l < p_line_count; l++) { + p_lines[l] -= dgc->get_center_position(); + } +#endif dgc->geometry_pool.add_or_update_line( scfg, p_exp_time, @@ -758,7 +792,7 @@ void DebugDraw3D::add_or_update_line_with_thickness(real_t p_exp_time, std::uniq InstanceType::LINE_VOLUMETRIC, p_exp_time, GET_PROC_TYPE(), - Transform3D(Basis().looking_at(center, get_up_vector(center)).scaled(VEC3_ONE(len)), a), // slow + Transform3D(Basis().looking_at(center, get_up_vector(center)).scaled(VEC3_ONE(len)), FIX_PRECISION_POSITION(a)), // slow p_col, SphereBounds(a + center, len * .5f)); } @@ -779,7 +813,7 @@ void DebugDraw3D::draw_sphere_base(const Transform3D &transform, const Color &co ConvertableInstanceType::SPHERE, duration, GET_PROC_TYPE(), - transform, + FIX_PRECISION_TRANSFORM(transform), IS_DEFAULT_COLOR(color) ? Colors::chartreuse : color, SphereBounds(transform.origin, MathUtils::get_max_basis_length(transform.basis) * 0.5f)); } @@ -812,7 +846,7 @@ void DebugDraw3D::draw_cylinder(const Transform3D &transform, const Color &color ConvertableInstanceType::CYLINDER, duration, GET_PROC_TYPE(), - transform, + FIX_PRECISION_TRANSFORM(transform), IS_DEFAULT_COLOR(color) ? Colors::forest_green : color, SphereBounds(transform.origin, MathUtils::get_max_basis_length(transform.basis) * MathUtils::CylinderRadiusForSphere)); } @@ -836,7 +870,7 @@ void DebugDraw3D::draw_cylinder_ab(const Vector3 &a, const Vector3 &b, const rea ConvertableInstanceType::CYLINDER_AB, duration, GET_PROC_TYPE(), - t, + FIX_PRECISION_TRANSFORM(t), IS_DEFAULT_COLOR(color) ? Colors::forest_green : color, SphereBounds(t.origin, MathUtils::get_max_basis_length(t.basis) * MathUtils::CylinderRadiusForSphere)); } @@ -878,7 +912,7 @@ void DebugDraw3D::draw_box_ab(const Vector3 &a, const Vector3 &b, const Vector3 ConvertableInstanceType::CUBE, duration, GET_PROC_TYPE(), - t, + FIX_PRECISION_TRANSFORM(t), IS_DEFAULT_COLOR(color) ? Colors::forest_green : color, sb); } else { @@ -910,7 +944,7 @@ void DebugDraw3D::draw_box_xf(const Transform3D &transform, const Color &color, is_box_centered ? ConvertableInstanceType::CUBE_CENTERED : ConvertableInstanceType::CUBE, duration, GET_PROC_TYPE(), - transform, + FIX_PRECISION_TRANSFORM(transform), IS_DEFAULT_COLOR(color) ? Colors::forest_green : color, sb); } @@ -951,7 +985,7 @@ void DebugDraw3D::draw_line_hit(const Vector3 &start, const Vector3 &end, const InstanceType::BILLBOARD_SQUARE, duration, GET_PROC_TYPE(), - Transform3D(Basis().scaled(VEC3_ONE(hit_size)), hit), + Transform3D(Basis().scaled(VEC3_ONE(hit_size)), FIX_PRECISION_POSITION(hit)), IS_DEFAULT_COLOR(hit_color) ? config->get_line_hit_color() : hit_color, SphereBounds(hit, MathUtils::CubeRadiusForSphere * hit_size), &Colors::empty_color); @@ -1058,7 +1092,7 @@ void DebugDraw3D::create_arrow(const Vector3 &p_a, const Vector3 &p_b, const Col ConvertableInstanceType::ARROWHEAD, p_duration, GET_PROC_TYPE(), - t, + FIX_PRECISION_TRANSFORM(t), IS_DEFAULT_COLOR(p_color) ? Colors::light_green : p_color, SphereBounds(t.origin + t.basis.get_column(2) * 0.5f, MathUtils::ArrowRadiusForSphere * size)); } @@ -1075,7 +1109,7 @@ void DebugDraw3D::draw_arrowhead(const Transform3D &transform, const Color &colo ConvertableInstanceType::ARROWHEAD, duration, GET_PROC_TYPE(), - transform, + FIX_PRECISION_TRANSFORM(transform), IS_DEFAULT_COLOR(color) ? Colors::light_green : color, SphereBounds(transform.origin + transform.basis.get_column(2) * 0.5f, MathUtils::ArrowRadiusForSphere * MathUtils::get_max_basis_length(transform.basis))); } @@ -1106,7 +1140,7 @@ void DebugDraw3D::draw_arrow_path(const PackedVector3Array &path, const Color &c LOCK_GUARD(datalock); add_or_update_line_with_thickness(duration, std::move(l), s, IS_DEFAULT_COLOR(color) ? Colors::light_green : color); - for (int i = 0; i < path.size() - 1; i++) { + for (int64_t i = 0; i < path.size() - 1; i++) { create_arrow(path[i], path[i + 1], color, arrow_size, is_absolute_size, duration); } } @@ -1142,7 +1176,7 @@ void DebugDraw3D::draw_square(const Vector3 &position, const real_t &size, const InstanceType::BILLBOARD_SQUARE, duration, GET_PROC_TYPE(), - t, + FIX_PRECISION_TRANSFORM(t), IS_DEFAULT_COLOR(color) ? Colors::red : color, SphereBounds(position, MathUtils::CubeRadiusForSphere * size), &Colors::empty_color); @@ -1170,7 +1204,7 @@ void DebugDraw3D::draw_plane(const Plane &plane, const Color &color, const Vecto InstanceType::PLANE, duration, GET_PROC_TYPE(), - t, + FIX_PRECISION_TRANSFORM(t), front_color, SphereBounds(center_pos, MathUtils::CubeRadiusForSphere * plane_size), &custom_col); @@ -1205,7 +1239,7 @@ void DebugDraw3D::draw_position(const Transform3D &transform, const Color &color ConvertableInstanceType::POSITION, duration, GET_PROC_TYPE(), - transform, + FIX_PRECISION_TRANSFORM(transform), IS_DEFAULT_COLOR(color) ? Colors::crimson : color, SphereBounds(transform.origin, MathUtils::get_max_basis_length(transform.basis) * MathUtils::AxisRadiusForSphere)); } diff --git a/src/3d/debug_draw_3d.h b/src/3d/debug_draw_3d.h index 986e3331..23836d07 100644 --- a/src/3d/debug_draw_3d.h +++ b/src/3d/debug_draw_3d.h @@ -158,7 +158,12 @@ class DebugDraw3D : public Object, public IScopeStorage ScopedPairIdConfig; + struct ScopedPairIdConfig { + uint64_t id; + DebugDraw3DScopeConfig *scfg; + ScopedPairIdConfig(uint64_t id, DebugDraw3DScopeConfig *scfg) : + id(id), scfg(scfg) {} + }; // stores thread id and array of id's with ptrs std::unordered_map > scoped_configs; // stores thread id and most recent config @@ -174,14 +179,20 @@ class DebugDraw3D : public Object, public IScopeStorage, 2> > shared_generated_meshes; + std::vector, (int)MeshMaterialVariant::MAX> > shared_generated_meshes; + /// Store World3D id and debug container - std::unordered_map[2]> debug_containers; - struct viewportToWorldCache { - uint64_t world_id = 0; - std::shared_ptr dgcs[2]; + struct ViewportToDebugContainerItem { + uint64_t world_id; + std::unique_ptr dgcs[(int)MeshMaterialVariant::MAX]; + + ViewportToDebugContainerItem(); + ViewportToDebugContainerItem(ViewportToDebugContainerItem &&other) noexcept; + ~ViewportToDebugContainerItem(); }; - std::unordered_map viewport_to_world_cache; + + std::unordered_map debug_containers; + std::unordered_map viewport_to_world_cache; // Default materials and shaders Ref mesh_shaders[(int)MeshMaterialType::MAX][(int)MeshMaterialVariant::MAX]; @@ -191,9 +202,8 @@ class DebugDraw3D : public Object, public IScopeStorage, 2> *get_shared_meshes(); - std::shared_ptr create_debug_container(bool p_no_depth_test); - std::shared_ptr get_debug_container(const DebugDraw3DScopeConfig::DebugContainerDependent &p_dgcd, const bool p_generate_new_container); + std::array, (int)MeshMaterialVariant::MAX> *get_shared_meshes(); + DebugGeometryContainer *get_debug_container(const DebugDraw3DScopeConfig::DebugContainerDependent &p_dgcd, const bool p_generate_new_container); void _register_viewport_world_deferred(uint64_t /*Viewport * */ p_vp, const uint64_t p_world_id); Viewport *_get_root_world_viewport(Viewport *p_vp); void _remove_debug_container(const uint64_t &p_world_id); @@ -220,7 +230,8 @@ class DebugDraw3D : public Object, public IScopeStorageget_instance_id() : 0); + DEV_PRINT_STD(NAMEOF(DebugGeometryContainer) " destroyed: %s, World3D (%d)\n", no_depth_test ? "NoDepth" : "Normal", viewport_world.is_valid() ? viewport_world->get_instance_id() : 0); LOCK_GUARD(owner->datalock); geometry_pool.clear_pool(); @@ -100,7 +100,6 @@ void DebugGeometryContainer::CreateMMI(InstanceType p_type, Ref p_mes new_mm.instantiate(); new_mm->set_name(String::num_int64((int)p_type)); - new_mm->set_transform_format(MultiMesh::TransformFormat::TRANSFORM_3D); new_mm->set_use_colors(true); new_mm->set_transform_format(MultiMesh::TRANSFORM_3D); new_mm->set_use_custom_data(true); @@ -118,23 +117,59 @@ void DebugGeometryContainer::CreateMMI(InstanceType p_type, Ref p_mes void DebugGeometryContainer::set_world(Ref p_new_world) { ZoneScoped; - if (p_new_world == base_world_viewport) { + if (p_new_world == viewport_world) { return; } - base_world_viewport = p_new_world; + viewport_world = p_new_world; + RenderingServer *rs = RenderingServer::get_singleton(); - RID scenario = base_world_viewport.is_valid() ? base_world_viewport->get_scenario() : RID(); + RID scenario = viewport_world.is_valid() ? viewport_world->get_scenario() : RID(); for (auto &s : multi_mesh_storage) { rs->instance_set_scenario(s.instance, scenario); } + rs->instance_set_scenario(immediate_mesh_storage.instance, scenario); } Ref DebugGeometryContainer::get_world() { - return base_world_viewport; + return viewport_world; +} + +#if defined(REAL_T_IS_DOUBLE) && defined(FIX_PRECISION_ENABLED) +const Vector3 &DebugGeometryContainer::get_center_position() { + return center_position; +} + +void DebugGeometryContainer::update_center_positions() { + if (center_position == prev_center_position) + return; + + Vector3 pos_diff = prev_center_position - center_position; + prev_center_position = center_position; + + geometry_pool.for_each_instance([&pos_diff](DelayedRendererInstance *i) { + i->data.origin_x += (float)pos_diff.x; + i->data.origin_y += (float)pos_diff.y; + i->data.origin_z += (float)pos_diff.z; + }); + + geometry_pool.for_each_line([&pos_diff](DelayedRendererLine *i) { + for (size_t l = 0; l < i->lines_count; l++) { + i->lines[l] += pos_diff; + } + }); + + RenderingServer *rs = RenderingServer::get_singleton(); + Transform3D xf = Transform3D(Basis(), center_position); + for (auto &s : multi_mesh_storage) { + rs->instance_set_transform(s.instance, xf); + } + + rs->instance_set_transform(immediate_mesh_storage.instance, xf); } +#endif void DebugGeometryContainer::update_geometry(double p_delta) { ZoneScoped; @@ -172,6 +207,10 @@ void DebugGeometryContainer::update_geometry(double p_delta) { set_render_layer_mask(owner->get_config()->get_geometry_render_layers()); } +#if defined(REAL_T_IS_DOUBLE) && defined(FIX_PRECISION_ENABLED) +#define FIX_DOUBLE_PRECISION_ERRORS +#endif + std::unordered_map > culling_data; { ZoneScopedN("Get frustums"); @@ -199,12 +238,24 @@ void DebugGeometryContainer::update_geometry(double p_delta) { if (owner->config->is_force_use_camera_from_scene() && cam) { frustum_arrays.push_back({ cam->get_frustum(), cam }); + +#ifdef FIX_DOUBLE_PRECISION_ERRORS + center_position = cam->get_global_position(); +#endif } else if (custom_editor_viewports.size() > 0) { + bool is_updated = false; for (const auto &evp : custom_editor_viewports) { if (evp->get_update_mode() == SubViewport::UpdateMode::UPDATE_ALWAYS) { - Camera3D *cam = evp->get_camera_3d(); - if (cam) { - frustum_arrays.push_back({ cam->get_frustum(), cam }); + Camera3D *vp_cam = evp->get_camera_3d(); + if (vp_cam) { + frustum_arrays.push_back({ vp_cam->get_frustum(), vp_cam }); + + if (!is_updated) { + is_updated = true; +#ifdef FIX_DOUBLE_PRECISION_ERRORS + center_position = vp_cam->get_global_position(); +#endif + } } } } @@ -214,6 +265,10 @@ void DebugGeometryContainer::update_geometry(double p_delta) { Camera3D *vp_cam = vp_p->get_camera_3d(); if (vp_cam) { frustum_arrays.push_back({ vp_cam->get_frustum(), vp_cam }); + +#ifdef FIX_DOUBLE_PRECISION_ERRORS + center_position = vp_cam->get_global_position(); +#endif } #ifdef DEBUG_ENABLED } @@ -254,6 +309,11 @@ void DebugGeometryContainer::update_geometry(double p_delta) { } } +#ifdef FIX_DOUBLE_PRECISION_ERRORS + update_center_positions(); +#endif +#undef FIX_DOUBLE_PRECISION_ERRORS + // Debug bounds of instances and lines if (owner->get_config()->is_visible_instance_bounds()) { ZoneScopedN("Debug bounds"); diff --git a/src/3d/debug_geometry_container.h b/src/3d/debug_geometry_container.h index aa8cdd9b..5839028a 100644 --- a/src/3d/debug_geometry_container.h +++ b/src/3d/debug_geometry_container.h @@ -43,7 +43,12 @@ class DebugGeometryContainer { ImmediateMeshStorage immediate_mesh_storage; GeometryPool geometry_pool; - Ref base_world_viewport; + Ref viewport_world; +#if defined(REAL_T_IS_DOUBLE) && defined(FIX_PRECISION_ENABLED) + Vector3 center_position; + Vector3 prev_center_position; +#endif + int32_t render_layers = 1; bool is_frame_rendered = false; bool no_depth_test = false; @@ -59,6 +64,11 @@ class DebugGeometryContainer { void set_world(Ref p_new_world); Ref get_world(); +#if defined(REAL_T_IS_DOUBLE) && defined(FIX_PRECISION_ENABLED) + const Vector3 &get_center_position(); + void update_center_positions(); +#endif + void update_geometry(double p_delta); void update_geometry_physics_start(double p_delta); void update_geometry_physics_end(double p_delta); diff --git a/src/3d/geometry_generators.cpp b/src/3d/geometry_generators.cpp index d8817685..0c09161a 100644 --- a/src/3d/geometry_generators.cpp +++ b/src/3d/geometry_generators.cpp @@ -183,7 +183,7 @@ Ref GeometryGenerator::CreateMesh(Mesh::PrimitiveType type, const Pac return mesh; } -Ref GeometryGenerator::RotatedMesh(const Ref mesh, const Vector3 &axis, const real_t &angle) { +Ref GeometryGenerator::RotatedMesh(const Ref mesh, const Vector3 &axis, const float &angle) { Array arrs = mesh->surface_get_arrays(0); Mesh::PrimitiveType ptype = mesh->surface_get_primitive_type(0); ERR_FAIL_COND_V(arrs.size() == 0, mesh); @@ -244,7 +244,7 @@ Ref GeometryGenerator::ConvertWireframeToVolumetric(Ref me PackedVector2Array res_uv; if (has_indexes) { - for (int i = 0; i < indexes.size(); i += 2) { + for (int64_t i = 0; i < indexes.size(); i += 2) { Vector3 normal_a = has_normals ? normals[indexes[i]] : Vector3(0, 1, 0.0001f); if (add_bevel) @@ -253,7 +253,7 @@ Ref GeometryGenerator::ConvertWireframeToVolumetric(Ref me GenerateVolumetricSegment(vertexes[indexes[i]], vertexes[indexes[i + 1]], normal_a, res_vertexes, res_custom0, res_indexes, res_uv, add_caps); } } else { - for (int i = 0; i < vertexes.size(); i += 2) { + for (int64_t i = 0; i < vertexes.size(); i += 2) { Vector3 normal_a = has_normals ? normals[i] : Vector3(0, 1, 0.0001f); if (add_bevel) @@ -451,14 +451,14 @@ void GeometryGenerator::GenerateVolumetricSegmentBevel(const Vector3 &a, const V } } -Ref GeometryGenerator::CreateVolumetricArrowHead(const real_t &radius, const real_t &length, const real_t &offset_mult, const bool &add_bevel) { +Ref GeometryGenerator::CreateVolumetricArrowHead(const float &radius, const float &length, const float &offset_mult, const bool &add_bevel) { PackedVector3Array vertexes; PackedVector2Array uv; PackedVector3Array custom0; PackedInt32Array indexes; - real_t front_offset = add_bevel ? .5f : 0; - real_t square_diag_mult = MathUtils::Sqrt2; + float front_offset = add_bevel ? .5f : 0; + float square_diag_mult = MathUtils::Sqrt2; vertexes.push_back(Vector3(0, 0, 0)); // 0 @@ -591,49 +591,49 @@ void GeometryGenerator::CreateLinesFromPathWireframe(const PackedVector3Array &p ZoneScoped; vertexes.resize(((size_t)path.size() - 1) * 2); - for (size_t i = 0; i < (size_t)path.size() - 1; i++) { - vertexes[i * 2 + 0] = path[(int)i]; - vertexes[i * 2 + 1] = path[(int)i + 1]; + for (int64_t i = 0; i < path.size() - 1; i++) { + vertexes[i * 2 + 0] = path[i]; + vertexes[i * 2 + 1] = path[i + 1]; } } void GeometryGenerator::CreateLinesFromPathWireframe(const PackedVector3Array &path, Vector3 *vertexes) { ZoneScoped; - for (size_t i = 0; i < (size_t)path.size() - 1; i++) { - vertexes[i * 2 + 0] = path[(int)i]; - vertexes[i * 2 + 1] = path[(int)i + 1]; + for (int64_t i = 0; i < path.size() - 1; i++) { + vertexes[i * 2 + 0] = path[i]; + vertexes[i * 2 + 1] = path[i + 1]; } } void GeometryGenerator::ConvertTriIndexesToWireframe(const PackedInt32Array &tri_indexes, std::vector &indexes) { ZoneScoped; - indexes.resize((size_t)tri_indexes.size() * 2); - - for (size_t i = 0; i < (size_t)tri_indexes.size() / 3; i++) { - indexes[i * 6 + 0] = tri_indexes[(int)i * 3 + 0]; - indexes[i * 6 + 1] = tri_indexes[(int)i * 3 + 1]; - indexes[i * 6 + 2] = tri_indexes[(int)i * 3 + 1]; - indexes[i * 6 + 3] = tri_indexes[(int)i * 3 + 2]; - indexes[i * 6 + 4] = tri_indexes[(int)i * 3 + 2]; - indexes[i * 6 + 5] = tri_indexes[(int)i * 3 + 0]; + indexes.resize(tri_indexes.size() * 2); + + for (int64_t i = 0; i < tri_indexes.size() / 3; i++) { + indexes[i * 6 + 0] = tri_indexes[i * 3 + 0]; + indexes[i * 6 + 1] = tri_indexes[i * 3 + 1]; + indexes[i * 6 + 2] = tri_indexes[i * 3 + 1]; + indexes[i * 6 + 3] = tri_indexes[i * 3 + 2]; + indexes[i * 6 + 4] = tri_indexes[i * 3 + 2]; + indexes[i * 6 + 5] = tri_indexes[i * 3 + 0]; } } void GeometryGenerator::ConvertTriIndexesToWireframe(const PackedInt32Array &tri_indexes, int *indexes) { ZoneScoped; - for (size_t i = 0; i < (size_t)tri_indexes.size() / 3; i++) { - indexes[i * 6 + 0] = tri_indexes[(int)i * 3 + 0]; - indexes[i * 6 + 1] = tri_indexes[(int)i * 3 + 1]; - indexes[i * 6 + 2] = tri_indexes[(int)i * 3 + 1]; - indexes[i * 6 + 3] = tri_indexes[(int)i * 3 + 2]; - indexes[i * 6 + 4] = tri_indexes[(int)i * 3 + 2]; - indexes[i * 6 + 5] = tri_indexes[(int)i * 3 + 0]; + for (int64_t i = 0; i < tri_indexes.size() / 3; i++) { + indexes[i * 6 + 0] = tri_indexes[i * 3 + 0]; + indexes[i * 6 + 1] = tri_indexes[i * 3 + 1]; + indexes[i * 6 + 2] = tri_indexes[i * 3 + 1]; + indexes[i * 6 + 3] = tri_indexes[i * 3 + 2]; + indexes[i * 6 + 4] = tri_indexes[i * 3 + 2]; + indexes[i * 6 + 5] = tri_indexes[i * 3 + 0]; } } -GeometryGenerator::IcosphereTriMesh GeometryGenerator::MakeIcosphereTriMesh(const real_t &radius, const int &resolution) { +GeometryGenerator::IcosphereTriMesh GeometryGenerator::MakeIcosphereTriMesh(const float &radius, const int &resolution) { // https://winter.dev/projects/mesh/icosphere const float Z = (1.0f + Math::sqrt(5.0f)) / 2.0f; // Golden ratio @@ -703,7 +703,7 @@ GeometryGenerator::IcosphereTriMesh GeometryGenerator::MakeIcosphereTriMesh(cons std::unordered_map triangleFromEdge; int indexCount = currentIndexCount; - for (int t = 0; t < indexCount; t += 3) { + for (int64_t t = 0; t < indexCount; t += 3) { int midpoints[3] = {}; for (int e = 0; e < 3; e++) { @@ -772,7 +772,7 @@ GeometryGenerator::IcosphereTriMesh GeometryGenerator::MakeIcosphereTriMesh(cons return sphere; } -Ref GeometryGenerator::CreateIcosphereLines(const real_t &radius, const int &depth) { +Ref GeometryGenerator::CreateIcosphereLines(const float &radius, const int &depth) { auto res = MakeIcosphereTriMesh(radius, depth); // PRINT("{0} vertexes, {1} indexes", res.vertexes.size(), res.indexes.size()); @@ -789,7 +789,7 @@ Ref GeometryGenerator::CreateIcosphereLines(const real_t &radius, con res.normals); } -Ref GeometryGenerator::CreateSphereLines(const int &_lats, const int &_lons, const real_t &radius, const int &subdivide) { +Ref GeometryGenerator::CreateSphereLines(const int &_lats, const int &_lons, const float &radius, const int &subdivide) { ZoneScoped; int lats = _lats * subdivide; int lons = _lons * subdivide; @@ -856,9 +856,9 @@ Ref GeometryGenerator::CreateSphereLines(const int &_lats, const int normals); } -Ref GeometryGenerator::CreateCylinderLines(const int &edges, const real_t &radius, const real_t &height, const int &subdivide) { +Ref GeometryGenerator::CreateCylinderLines(const int &edges, const float &radius, const float &height, const int &subdivide) { ZoneScoped; - real_t angle = 360.f / edges; + float angle = 360.f / edges; PackedVector3Array vertexes; PackedVector3Array normals; diff --git a/src/3d/geometry_generators.h b/src/3d/geometry_generators.h index 2e06cb0e..e75f3b97 100644 --- a/src/3d/geometry_generators.h +++ b/src/3d/geometry_generators.h @@ -20,7 +20,7 @@ class GeometryGenerator { static void GenerateVolumetricSegment(const Vector3 &a, const Vector3 &b, const Vector3 &normal, PackedVector3Array &vertexes, PackedVector3Array &custom0, PackedInt32Array &indexes, PackedVector2Array &uv, const bool &add_caps = true); static void GenerateVolumetricSegmentBevel(const Vector3 &a, const Vector3 &b, const Vector3 &normal, PackedVector3Array &vertexes, PackedVector3Array &custom0, PackedInt32Array &indexes, PackedVector2Array &uv, const bool &add_caps = true); - static IcosphereTriMesh MakeIcosphereTriMesh(const real_t &radius, const int &resolution); + static IcosphereTriMesh MakeIcosphereTriMesh(const float &radius, const int &resolution); public: #pragma region Predefined Geometry Parts @@ -58,10 +58,10 @@ class GeometryGenerator { } static Ref CreateMesh(Mesh::PrimitiveType type, const PackedVector3Array &vertexes, const PackedInt32Array &indexes = {}, const PackedColorArray &colors = {}, const PackedVector3Array &normals = {}, const PackedVector2Array &uv = {}, const PackedFloat32Array &custom0 = {}, BitField flags = 0); - static Ref RotatedMesh(const Ref mesh, const Vector3 &axis, const real_t &angle); + static Ref RotatedMesh(const Ref mesh, const Vector3 &axis, const float &angle); static Ref ConvertWireframeToVolumetric(Ref mesh, const bool &add_bevel, const bool &add_caps = false); - static Ref CreateVolumetricArrowHead(const real_t &radius, const real_t &length, const real_t &offset_mult, const bool &add_bevel); + static Ref CreateVolumetricArrowHead(const float &radius, const float &length, const float &offset_mult, const bool &add_bevel); static Ref CreateCameraFrustumLines(const std::array &frustum); static void CreateCameraFrustumLinesWireframe(const std::array &frustum, std::vector &vertexes); @@ -73,7 +73,7 @@ class GeometryGenerator { static void ConvertTriIndexesToWireframe(const PackedInt32Array &tri_indexes, std::vector &indexes); static void ConvertTriIndexesToWireframe(const PackedInt32Array &tri_indexes, int *indexes); - static Ref CreateIcosphereLines(const real_t &radius, const int &depth); - static Ref CreateSphereLines(const int &_lats, const int &_lons, const real_t &radius, const int &subdivide = 1); - static Ref CreateCylinderLines(const int &edges, const real_t &radius, const real_t &height, const int &subdivide = 1); + static Ref CreateIcosphereLines(const float &radius, const int &depth); + static Ref CreateSphereLines(const int &_lats, const int &_lons, const float &radius, const int &subdivide = 1); + static Ref CreateCylinderLines(const int &edges, const float &radius, const float &height, const int &subdivide = 1); }; diff --git a/src/3d/render_instances.cpp b/src/3d/render_instances.cpp index 0159c003..c0cfe090 100644 --- a/src/3d/render_instances.cpp +++ b/src/3d/render_instances.cpp @@ -512,7 +512,7 @@ GeometryType GeometryPool::_scoped_config_get_geometry_type(const std::shared_pt Color GeometryPool::_scoped_config_to_custom(const std::shared_ptr &p_cfg) { // ZoneScoped; if (_scoped_config_get_geometry_type(p_cfg) == GeometryType::Volumetric) - return Color(p_cfg->thickness, p_cfg->center_brightness, 0, 0); + return Color((float)p_cfg->thickness, (float)p_cfg->center_brightness, (float)0, (float)0); return Color(); } diff --git a/src/3d/render_instances.h b/src/3d/render_instances.h index bd896d15..c4d902b5 100644 --- a/src/3d/render_instances.h +++ b/src/3d/render_instances.h @@ -54,11 +54,11 @@ struct GeometryPoolData3DInstance { GeometryPoolData3DInstance(const Transform3D &p_xf, const Color &p_color, const Color &p_custom) : basis_x(p_xf.basis[0]), - origin_x(p_xf.origin.x), + origin_x((float)p_xf.origin.x), basis_y(p_xf.basis[1]), - origin_y(p_xf.origin.y), + origin_y((float)p_xf.origin.y), basis_z(p_xf.basis[2]), - origin_z(p_xf.origin.z), + origin_z((float)p_xf.origin.z), color(p_color), custom(p_custom) {} }; diff --git a/src/common/i_scope_storage.h b/src/common/i_scope_storage.h index a59f88c0..b7f9ffa4 100644 --- a/src/common/i_scope_storage.h +++ b/src/common/i_scope_storage.h @@ -22,7 +22,7 @@ class IScopeStorage { #endif public: - typedef std::function unregister_func; + using unregister_func = std::function; virtual Ref scoped_config() = 0; }; diff --git a/src/debug_draw_manager.cpp b/src/debug_draw_manager.cpp index e6b2ab0f..33d515ae 100644 --- a/src/debug_draw_manager.cpp +++ b/src/debug_draw_manager.cpp @@ -22,9 +22,14 @@ Object *DebugDrawManager::default_arg_obj = nullptr; #ifndef DISABLE_DEBUG_RENDERING void _DD3D_PhysicsWatcher::init(DebugDrawManager *p_root) { root_node = p_root; + set_process_priority(INT32_MIN); set_physics_process_priority(INT32_MIN); } +void _DD3D_PhysicsWatcher::_process(double p_delta) { + root_node->_process_start(p_delta); +} + void _DD3D_PhysicsWatcher::_physics_process(double p_delta) { root_node->_physics_process_start(p_delta); } @@ -344,6 +349,13 @@ void DebugDrawManager::_integrate_into_engine() { #endif } +void DebugDrawManager::_process_start(double p_delta) { + if (debug_enabled) { + debug_draw_3d_singleton->process_start(p_delta); + debug_draw_2d_singleton->process_start(p_delta); + } +} + void DebugDrawManager::_process(double p_delta) { // To discover what causes the constant look here: // https://github.com/godotengine/godot/blob/baf6b4634d08bc3e193a38b86e96945052002f64/servers/rendering/rendering_server_default.h#L104 @@ -356,10 +368,10 @@ void DebugDrawManager::_process(double p_delta) { // } #ifndef DISABLE_DEBUG_RENDERING if (debug_enabled) { - DebugDraw3D::get_singleton()->process(p_delta); - DebugDraw2D::get_singleton()->process(p_delta); + debug_draw_3d_singleton->process_end(p_delta); + debug_draw_2d_singleton->process_end(p_delta); - if (!DebugDraw2D::get_singleton()->is_drawing_frame()) { + if (!debug_draw_2d_singleton->is_drawing_frame()) { FrameMark; } } @@ -372,7 +384,7 @@ void DebugDrawManager::_process(double p_delta) { } #ifdef TRACY_ENABLE - if (!debug_enabled || !DebugDraw2D::get_singleton()->is_debug_enabled()) { + if (!debug_enabled || !debug_draw_2d_singleton->is_debug_enabled()) { FrameMark; } #endif @@ -380,16 +392,16 @@ void DebugDrawManager::_process(double p_delta) { void DebugDrawManager::_physics_process_start(double p_delta) { if (debug_enabled) { - DebugDraw3D::get_singleton()->physics_process_start(p_delta); - DebugDraw2D::get_singleton()->physics_process_start(p_delta); + debug_draw_3d_singleton->physics_process_start(p_delta); + debug_draw_2d_singleton->physics_process_start(p_delta); } } void DebugDrawManager::_physics_process(double p_delta) { #ifndef DISABLE_DEBUG_RENDERING if (debug_enabled) { - DebugDraw3D::get_singleton()->physics_process_end(p_delta); - DebugDraw2D::get_singleton()->physics_process_end(p_delta); + debug_draw_3d_singleton->physics_process_end(p_delta); + debug_draw_2d_singleton->physics_process_end(p_delta); } #endif } diff --git a/src/debug_draw_manager.h b/src/debug_draw_manager.h index e981d66e..e598f43e 100644 --- a/src/debug_draw_manager.h +++ b/src/debug_draw_manager.h @@ -22,6 +22,7 @@ class _DD3D_PhysicsWatcher : public Node { public: void init(DebugDrawManager *p_root); + virtual void _process(double p_delta) override; virtual void _physics_process(double p_delta) override; }; #endif @@ -155,6 +156,8 @@ class DebugDrawManager : public CanvasLayer { /// @private void deinit(); + /// @private + void _process_start(double p_delta); /// @private virtual void _process(double p_delta) override; /// @private diff --git a/src/dev_debug_draw_3d_Library.vcxproj b/src/dev_debug_draw_3d_Library.vcxproj index 9754feef..44fe27e9 100644 --- a/src/dev_debug_draw_3d_Library.vcxproj +++ b/src/dev_debug_draw_3d_Library.vcxproj @@ -5,6 +5,10 @@ editor_analyze x64 + + editor_dev_double + x64 + editor_dev x64 @@ -53,6 +57,13 @@ Unicode true + + DynamicLibrary + true + v143 + Unicode + true + DynamicLibrary false @@ -83,6 +94,9 @@ + + + @@ -120,6 +134,16 @@ true clang-analyzer-* + + $(ProjectDir);$(ProjectDir)..\godot-cpp\include;$(ProjectDir)..\godot-cpp\gdextension;$(ProjectDir)..\godot-cpp\gen\include;$(IncludePath) + $(ProjectDir)..\godot-cpp\bin;$(LibraryPath) + $(ProjectDir)..\addons\debug_draw_3d\libs\ + libdd3d.windows.editor.$(LlvmPlatformName).double + $(SolutionDir)obj\$(PlatformTarget)\ + true + true + clang-analyzer-* + false $(ProjectDir);$(ProjectDir)..\godot-cpp\include;$(ProjectDir)..\godot-cpp\gdextension;$(ProjectDir)..\godot-cpp\gen\include;$(IncludePath) @@ -229,6 +253,34 @@ + + + Level3 + false + true + WIN64;NOMINMAX;TYPED_METHOD_BIND;HOT_RELOAD_ENABLED;DEBUG_ENABLED;GDEXTENSION;TOOLS_ENABLED;DEV_ENABLED;REAL_T_IS_DOUBLE;DISABLE_SHADER_WORLD_COORDS;FIX_PRECISION_ENABLED;%(PreprocessorDefinitions) + true + Sync + + + EditAndContinue + stdcpp17 + true + + + MultiThreaded + + + NotSet + DebugFull + libgodot-cpp.windows.editor.dev.double.$(LlvmPlatformName).lib + $(OutDir)$(TargetName)$(TargetExt) + Default + true + $(OutDir)$(TargetName)_vs.pdb + + + Level3 diff --git a/src/editor/generate_csharp_bindings.h b/src/editor/generate_csharp_bindings.h index be835cef..07f76951 100644 --- a/src/editor/generate_csharp_bindings.h +++ b/src/editor/generate_csharp_bindings.h @@ -91,14 +91,14 @@ class GenerateCSharpBindingsPlugin { TypedArray generate_for_classes; TypedArray avoid_caching_for_classes; - typedef std::map > extend_class_strings; + using extend_class_strings = std::map >; extend_class_strings additional_statics_for_classes; extend_class_strings override_disposable_for_classes; PackedStringArray singletons; bool is_shift_pressed = false; bool is_generate_unload_event = false; - typedef std::map remap_data; + using remap_data = std::map; std::map types_map = { { Variant::NIL, "NILL" }, diff --git a/src/resources/extendable_meshes.gdshader b/src/resources/extendable_meshes.gdshader index 08386c35..b998cb05 100644 --- a/src/resources/extendable_meshes.gdshader +++ b/src/resources/extendable_meshes.gdshader @@ -1,8 +1,12 @@ //#define NO_DEPTH +//#define NO_WORLD_COORD //#define FORCED_TRANSPARENT shader_type spatial; -render_mode cull_disabled, shadows_disabled, unshaded, world_vertex_coords +render_mode cull_disabled, shadows_disabled, unshaded +#if !defined(NO_WORLD_COORD) +, world_vertex_coords +#endif #if defined(FOG_DISABLED) , fog_disabled #endif @@ -23,7 +27,12 @@ mat3 orthonormalize(mat3 m) { void vertex() { brightness_of_center = INSTANCE_CUSTOM.y; - VERTEX = VERTEX + (CUSTOM0.xyz * INSTANCE_CUSTOM.x) * orthonormalize(inverse(mat3(normalize(MODEL_MATRIX[0].xyz), normalize(MODEL_MATRIX[1].xyz), normalize(MODEL_MATRIX[2].xyz)))); + VERTEX = VERTEX + (CUSTOM0.xyz * INSTANCE_CUSTOM.x) +#if !defined(NO_WORLD_COORD) + * orthonormalize(inverse(mat3(normalize(MODEL_MATRIX[0].xyz), normalize(MODEL_MATRIX[1].xyz), normalize(MODEL_MATRIX[2].xyz)))); +#else + ; +#endif } vec3 toLinearFast(vec3 col) { diff --git a/src/utils/compiler.h b/src/utils/compiler.h index 13a732dd..ef035a46 100644 --- a/src/utils/compiler.h +++ b/src/utils/compiler.h @@ -20,9 +20,9 @@ #if _MSC_VER #define GODOT_WARNING_DISABLE() \ - __pragma(warning(disable : 4244 26451 26495)) + __pragma(warning(disable : 4309 4244 26451 26495)) #define GODOT_WARNING_RESTORE() \ - __pragma(warning(default : 4244 26451 26495)) + __pragma(warning(default : 4309 4244 26451 26495)) #define MSVC_WARNING_DISABLE(n) __pragma(warning(disable \ : n)) diff --git a/src/utils/utils.h b/src/utils/utils.h index 68b30cc2..6b21eb5f 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -191,7 +191,7 @@ const extern godot::Vector3 Vector3_FORWARD; const extern godot::Quaternion Quaternion_IDENTITY; -#if REAL_T_IS_DOUBLE +#ifdef REAL_T_IS_DOUBLE struct Vector3Float { float x, y, z; Vector3Float() : @@ -202,7 +202,7 @@ struct Vector3Float { x((float)v.x), y((float)v.y), z((float)v.z) {} }; #else -typedef godot::Vector3 Vector3Float; +using Vector3Float = godot::Vector3; #endif class Utils {