diff --git a/examples_dd3d/DebugDrawDemoScene.gd b/examples_dd3d/DebugDrawDemoScene.gd index 63c306c9..6b3eaef0 100644 --- a/examples_dd3d/DebugDrawDemoScene.gd +++ b/examples_dd3d/DebugDrawDemoScene.gd @@ -67,6 +67,20 @@ func _update_keys_just_press(): func _process(delta: float) -> void: + DebugDraw3D.scoped_config().set_thickness(10) + print("def ", DebugDraw3D.scoped_config().get_thickness()) + var s11 = DebugDraw3D.new_scoped_config() + print("over ", s11.get_thickness()) + s11.set_thickness(1) + print("over changed ", s11.get_thickness()) + print("def recheck ", DebugDraw3D.scoped_config().get_thickness()) + if true: + var s13 = DebugDraw3D.new_scoped_config() + s13.set_thickness(2) + print("scope ", s13.get_thickness()) + print("def recheck 2 ", DebugDraw3D.scoped_config().get_thickness()) + print("over recheck ", s11.get_thickness()) + _update_keys_just_press() if _is_key_just_pressed(KEY_F1): diff --git a/src/2d/config_2d.h b/src/2d/config_2d.h index 78f630b5..2a4f0415 100644 --- a/src/2d/config_2d.h +++ b/src/2d/config_2d.h @@ -7,7 +7,6 @@ GODOT_WARNING_DISABLE() #include #include GODOT_WARNING_RESTORE() - using namespace godot; class DebugDrawConfig2D : public RefCounted { diff --git a/src/2d/debug_draw_2d.cpp b/src/2d/debug_draw_2d.cpp index f0fe5ae6..55dbf213 100644 --- a/src/2d/debug_draw_2d.cpp +++ b/src/2d/debug_draw_2d.cpp @@ -7,11 +7,11 @@ #include "stats_2d.h" #include "utils/utils.h" +#include + GODOT_WARNING_DISABLE() GODOT_WARNING_RESTORE() -#include - #define NEED_LEAVE (!_is_enabled_override()) DebugDraw2D *DebugDraw2D::singleton = nullptr; diff --git a/src/2d/debug_draw_2d.h b/src/2d/debug_draw_2d.h index 0d4add68..756375f6 100644 --- a/src/2d/debug_draw_2d.h +++ b/src/2d/debug_draw_2d.h @@ -3,16 +3,15 @@ #include "common/colors.h" #include "utils/compiler.h" +#include + GODOT_WARNING_DISABLE() -#include #include #include +#include #include #include GODOT_WARNING_RESTORE() - -#include - using namespace godot; class DataGraphManager; diff --git a/src/2d/graphs.h b/src/2d/graphs.h index a388c02b..9c617371 100644 --- a/src/2d/graphs.h +++ b/src/2d/graphs.h @@ -4,16 +4,15 @@ #include "common/colors.h" #include "utils/compiler.h" +#include +#include +#include + GODOT_WARNING_DISABLE() #include #include #include GODOT_WARNING_RESTORE() - -#include -#include -#include - using namespace godot; class DataGraphManager; diff --git a/src/2d/grouped_text.h b/src/2d/grouped_text.h index 0085c2a7..3a505e74 100644 --- a/src/2d/grouped_text.h +++ b/src/2d/grouped_text.h @@ -3,17 +3,16 @@ #include "utils/compiler.h" -GODOT_WARNING_DISABLE() -#include -#include -#include -GODOT_WARNING_RESTORE() - #include #include #include #include +GODOT_WARNING_DISABLE() +#include +#include +#include +GODOT_WARNING_RESTORE() using namespace godot; class TextGroupItem { diff --git a/src/2d/stats_2d.h b/src/2d/stats_2d.h index 7dbdcedc..6a5b5bbf 100644 --- a/src/2d/stats_2d.h +++ b/src/2d/stats_2d.h @@ -5,7 +5,6 @@ GODOT_WARNING_DISABLE() #include GODOT_WARNING_RESTORE() - using namespace godot; class DebugDrawStats2D : public RefCounted { diff --git a/src/3d/config_3d.h b/src/3d/config_3d.h index fbff3204..4b664994 100644 --- a/src/3d/config_3d.h +++ b/src/3d/config_3d.h @@ -6,7 +6,6 @@ GODOT_WARNING_DISABLE() #include GODOT_WARNING_RESTORE() - using namespace godot; class DebugDrawConfig3D : public RefCounted { diff --git a/src/3d/config_scoped_3d.cpp b/src/3d/config_scoped_3d.cpp new file mode 100644 index 00000000..c108b6ce --- /dev/null +++ b/src/3d/config_scoped_3d.cpp @@ -0,0 +1,42 @@ +#include "config_scoped_3d.h" + +#include "utils/utils.h" + +void DDScopedConfig3D::_bind_methods() { +#define REG_CLASS_NAME DDScopedConfig3D + REG_METHOD(set_thickness, "value"); + REG_METHOD(get_thickness); +#undef REG_CLASS_NAME +} + +Ref DDScopedConfig3D::set_thickness(real_t value) { + thickness = Math::clamp(value, (real_t)0, (real_t)100); + return Ref(this); +} + +real_t DDScopedConfig3D::get_thickness() { + return thickness; +} + +DDScopedConfig3D::DDScopedConfig3D() { + unregister_action = nullptr; + thread_id = 0; + guard_id = 0; + + thickness = 0; +} + +DDScopedConfig3D::DDScopedConfig3D(const uint64_t &p_thread_id, const uint64_t &p_guard_id, const DDScopedConfig3D *parent, const unregister_func p_unreg) { + unregister_action = p_unreg; + + thread_id = p_thread_id; + guard_id = p_guard_id; + + thickness = parent->thickness; +} + +DDScopedConfig3D::~DDScopedConfig3D() { + if (unregister_action) { + unregister_action(thread_id, guard_id); + } +} diff --git a/src/3d/config_scoped_3d.h b/src/3d/config_scoped_3d.h new file mode 100644 index 00000000..5a5fe05d --- /dev/null +++ b/src/3d/config_scoped_3d.h @@ -0,0 +1,31 @@ +#pragma once + +#include "utils/compiler.h" + +#include + +GODOT_WARNING_DISABLE() +#include +GODOT_WARNING_RESTORE() +using namespace godot; + +class DDScopedConfig3D : public RefCounted { + GDCLASS(DDScopedConfig3D, RefCounted) + +protected: + static void _bind_methods(); + uint64_t thread_id; + uint64_t guard_id; + + typedef std::function unregister_func; + unregister_func unregister_action; + real_t thickness; + +public: + Ref set_thickness(real_t value); + real_t get_thickness(); + + DDScopedConfig3D(); + DDScopedConfig3D(const uint64_t &p_thread_id, const uint64_t &p_guard_id, const DDScopedConfig3D *parent, const unregister_func p_unreg); + ~DDScopedConfig3D(); +}; diff --git a/src/3d/debug_draw_3d.cpp b/src/3d/debug_draw_3d.cpp index b5ed8058..de2c9ffb 100644 --- a/src/3d/debug_draw_3d.cpp +++ b/src/3d/debug_draw_3d.cpp @@ -8,35 +8,18 @@ #include "stats_3d.h" #include "utils/utils.h" +#include + GODOT_WARNING_DISABLE() #include #include +#include GODOT_WARNING_RESTORE() -#include - #define NEED_LEAVE (!_is_enabled_override()) DebugDraw3D *DebugDraw3D::singleton = nullptr; -void DDScopedConfig3D::_bind_methods() { -#define REG_CLASS_NAME DDScopedConfig3D - REG_PROP(empty_color, Variant::COLOR); -#undef REG_CLASS_NAME -} - -DDScopedConfig3D::DDScopedConfig3D() { - // TODO check for the first create() for properties defaults - // TODO add this check for DD3D and DD2D to avoid warnings - DEV_PRINT_STD_ERR(NAMEOF(DDScopedConfig3D) " must be called with arguments!\n"); -} - -DDScopedConfig3D::DDScopedConfig3D(const uint64_t &p_thread_id, const uint64_t &p_guard_id, const DDScopedConfig3D *parent) { -} - -DDScopedConfig3D::~DDScopedConfig3D() { -} - void DebugDraw3D::_bind_methods() { #define REG_CLASS_NAME DebugDraw3D @@ -51,7 +34,6 @@ void DebugDraw3D::_bind_methods() { REG_PROP(custom_viewport, Variant::OBJECT, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE); #pragma endregion -#undef REG_CLASS_NAME #pragma region Draw Functions ClassDB::bind_method(D_METHOD(NAMEOF(clear_all)), &DebugDraw3D::clear_all); @@ -100,7 +82,11 @@ void DebugDraw3D::_bind_methods() { #pragma endregion // Draw Functions - ClassDB::bind_method(D_METHOD(NAMEOF(get_render_stats)), &DebugDraw3D::get_render_stats); + REG_METHOD(get_render_stats); + REG_METHOD(new_scoped_config); + REG_METHOD(scoped_config); + +#undef REG_CLASS_NAME } DebugDraw3D::DebugDraw3D() { @@ -110,6 +96,7 @@ DebugDraw3D::DebugDraw3D() { void DebugDraw3D::init(DebugDrawManager *root) { root_node = root; set_config(nullptr); + default_scoped_config.instantiate(); _load_materials(); @@ -155,27 +142,88 @@ void DebugDraw3D::process(double delta) { // Update 3D debug dgc->update_geometry(delta); + #endif + + // TODO avoid in release + _clear_scoped_configs(); } Ref DebugDraw3D::new_scoped_config() { + LOCK_GUARD(scoped_datalock); static std::atomic create_counter = 0; create_counter++; - return Ref(); + uint64_t thread = OS::get_singleton()->get_thread_caller_id(); + Ref res = memnew( + DDScopedConfig3D( + thread, + create_counter, + scoped_config_for_current_thread(), + std::bind(&DebugDraw3D::_unregister_scoped_config, this, std::placeholders::_1, std::placeholders::_2))); + + _register_scoped_config(thread, create_counter, res.ptr()); + return res; } -DDScopedConfig3D *DebugDraw3D::get_transform_for_current_thread() { - return nullptr; +DDScopedConfig3D *DebugDraw3D::scoped_config_for_current_thread() { + LOCK_GUARD(scoped_datalock); + uint64_t thread = OS::get_singleton()->get_thread_caller_id(); + + if (cached_scoped_configs.find(thread) != cached_scoped_configs.end()) { + return cached_scoped_configs[thread]; + } + + if (scoped_configs.find(thread) != scoped_configs.end()) { + const auto &cfgs = scoped_configs[thread]; + if (!cfgs.empty()) { + DDScopedConfig3D *tmp = cfgs.back().second; + cached_scoped_configs[thread] = tmp; + return tmp; + } + } + + cached_scoped_configs[thread] = default_scoped_config.ptr(); + return default_scoped_config.ptr(); } -void DebugDraw3D::register_scoped_config(uint64_t guard_id, uint64_t thread_id, DDScopedConfig3D *cfg) { +Ref DebugDraw3D::scoped_config() { + return default_scoped_config; } -void DebugDraw3D::unregister_scoped_config(uint64_t guard_id, uint64_t thread_id) { +void DebugDraw3D::_register_scoped_config(uint64_t thread_id, uint64_t guard_id, DDScopedConfig3D *cfg) { + LOCK_GUARD(scoped_datalock); + + uint64_t thread = OS::get_singleton()->get_thread_caller_id(); + scoped_configs[thread_id].push_back(ScopedPairIdConfig(guard_id, cfg)); + + // Update cached value + cached_scoped_configs[thread] = cfg; } -void DebugDraw3D::clear_scoped_configs() { +void DebugDraw3D::_unregister_scoped_config(uint64_t thread_id, uint64_t guard_id) { + LOCK_GUARD(scoped_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; }); + + if (res != cfgs.rend()) { + cfgs.erase(--res.base()); + + // Update cached value + if (!cfgs.empty()) { + cached_scoped_configs[thread_id] = cfgs.back().second; + } else { + cached_scoped_configs[thread_id] = default_scoped_config.ptr(); + } + } +} + +void DebugDraw3D::_clear_scoped_configs() { + LOCK_GUARD(scoped_datalock); + + cached_scoped_configs.clear(); + scoped_configs.clear(); } void DebugDraw3D::_load_materials() { diff --git a/src/3d/debug_draw_3d.h b/src/3d/debug_draw_3d.h index 68fca309..6494cd48 100644 --- a/src/3d/debug_draw_3d.h +++ b/src/3d/debug_draw_3d.h @@ -1,17 +1,20 @@ #pragma once #include "common/colors.h" +#include "common/i_scoped_storage.h" +#include "config_scoped_3d.h" + +#include +#include +#include GODOT_WARNING_DISABLE() #include #include #include #include +#include GODOT_WARNING_RESTORE() - -#include -#include - using namespace godot; class DataGraphManager; @@ -20,34 +23,7 @@ class DebugDrawConfig3D; class DebugDrawStats3D; class DebugGeometryContainer; -class DDScopedConfig3D : public RefCounted { - GDCLASS(DDScopedConfig3D, RefCounted) - -protected: - static void _bind_methods(); - uint64_t thread_id = 0; - uint64_t guard_id = 0; - void set_empty_color(const Color &_col){}; - Color get_empty_color() const { return Color(); }; - -public: - DDScopedConfig3D(); - DDScopedConfig3D(const uint64_t &p_thread_id, const uint64_t &p_guard_id, const DDScopedConfig3D *parent); - ~DDScopedConfig3D(); -}; - -template -class ScopedStorage { -private: - virtual TCfgStorage *get_transform_for_current_thread() = 0; - -public: - virtual void register_scoped_config(uint64_t guard_id, uint64_t thread_id, TCfgStorage *cfg) = 0; - virtual void unregister_scoped_config(uint64_t guard_id, uint64_t thread_id) = 0; - virtual void clear_scoped_configs() = 0; -}; - -class DebugDraw3D : public Object, public ScopedStorage { +class DebugDraw3D : public Object, public IScopedStorage { GDCLASS(DebugDraw3D, Object) friend DebugDrawManager; @@ -59,8 +35,17 @@ class DebugDraw3D : public Object, public ScopedStorage { std::vector custom_editor_viewports; DebugDrawManager *root_node = nullptr; - // Inherited via ScopedStorage - DDScopedConfig3D *get_transform_for_current_thread() override; + Ref default_scoped_config; + typedef std::pair ScopedPairIdConfig; + // stores thread id and array of id's with ptrs + // TODO in release use only default_scoped_config + std::map > scoped_configs; + // stores thread id and most recent config + std::map cached_scoped_configs; + std::recursive_mutex scoped_datalock; + + // Inherited via IScopedStorage + DDScopedConfig3D *scoped_config_for_current_thread() override; #ifndef DISABLE_DEBUG_RENDERING #ifdef DEV_ENABLED @@ -114,10 +99,11 @@ class DebugDraw3D : public Object, public ScopedStorage { Ref new_scoped_config(); - // Inherited via ScopedStorage - void register_scoped_config(uint64_t guard_id, uint64_t thread_id, DDScopedConfig3D *cfg) override; - void unregister_scoped_config(uint64_t guard_id, uint64_t thread_id) override; - void clear_scoped_configs() override; + // Inherited via IScopedStorage + Ref scoped_config() override; + void _register_scoped_config(uint64_t thread_id, uint64_t guard_id, DDScopedConfig3D *cfg) override; + void _unregister_scoped_config(uint64_t thread_id, uint64_t guard_id) override; + void _clear_scoped_configs() override; Node *get_root_node(); void set_custom_editor_viewport(std::vector _viewports); diff --git a/src/3d/debug_geometry_container.cpp b/src/3d/debug_geometry_container.cpp index 65a7016f..df291b14 100644 --- a/src/3d/debug_geometry_container.cpp +++ b/src/3d/debug_geometry_container.cpp @@ -5,12 +5,11 @@ #include "debug_draw_3d.h" #include "stats_3d.h" +#include + GODOT_WARNING_DISABLE() #include GODOT_WARNING_RESTORE() - -#include - using namespace godot; DebugGeometryContainer::DebugGeometryContainer(class DebugDraw3D *root) { diff --git a/src/3d/debug_geometry_container.h b/src/3d/debug_geometry_container.h index dc65892e..da1620b8 100644 --- a/src/3d/debug_geometry_container.h +++ b/src/3d/debug_geometry_container.h @@ -7,6 +7,8 @@ #include "utils/math_utils.h" #include "utils/utils.h" +#include + GODOT_WARNING_DISABLE() #include #include @@ -21,9 +23,6 @@ GODOT_WARNING_DISABLE() #include #include GODOT_WARNING_RESTORE() - -#include - using namespace godot; class DebugDrawStats3D; diff --git a/src/3d/geometry_generators.h b/src/3d/geometry_generators.h index 4297d8e1..0544ce20 100644 --- a/src/3d/geometry_generators.h +++ b/src/3d/geometry_generators.h @@ -3,15 +3,14 @@ #include "utils/compiler.h" #include "utils/utils.h" +#include +#include + GODOT_WARNING_DISABLE() #include #include #include GODOT_WARNING_RESTORE() - -#include -#include - using namespace godot; class GeometryGenerator { diff --git a/src/3d/render_instances.h b/src/3d/render_instances.h index de1aef75..dcc64e9d 100644 --- a/src/3d/render_instances.h +++ b/src/3d/render_instances.h @@ -5,20 +5,19 @@ #include "utils/math_utils.h" #include "utils/utils.h" +#include +#include + GODOT_WARNING_DISABLE() #include GODOT_WARNING_RESTORE() - -#include -#include +using namespace godot; namespace godot { class MultiMesh; } class DebugDrawStats3D; -using namespace godot; - enum InstanceType : char { CUBES, CUBES_CENTERED, diff --git a/src/3d/stats_3d.h b/src/3d/stats_3d.h index a3d786aa..7693e534 100644 --- a/src/3d/stats_3d.h +++ b/src/3d/stats_3d.h @@ -5,7 +5,6 @@ GODOT_WARNING_DISABLE() #include GODOT_WARNING_RESTORE() - using namespace godot; class DebugDrawStats3D : public RefCounted { diff --git a/src/common/i_scoped_storage.h b/src/common/i_scoped_storage.h new file mode 100644 index 00000000..7eed00de --- /dev/null +++ b/src/common/i_scoped_storage.h @@ -0,0 +1,25 @@ +#pragma once + +#include "utils/compiler.h" + +#include + +GODOT_WARNING_DISABLE() +#include +#include +GODOT_WARNING_RESTORE() +using namespace godot; + +template +class IScopedStorage { +private: + virtual TCfgStorage *scoped_config_for_current_thread() = 0; + +public: + typedef std::function unregister_func; + + virtual Ref scoped_config() = 0; + virtual void _register_scoped_config(uint64_t thread_id, uint64_t guard_id, TCfgStorage *cfg) = 0; + virtual void _unregister_scoped_config(uint64_t thread_id, uint64_t guard_id) = 0; + virtual void _clear_scoped_configs() = 0; +}; diff --git a/src/debug_draw_manager.h b/src/debug_draw_manager.h index 13d2c1fc..3b7b9dab 100644 --- a/src/debug_draw_manager.h +++ b/src/debug_draw_manager.h @@ -2,12 +2,11 @@ #include "utils/compiler.h" +#include + GODOT_WARNING_DISABLE() #include GODOT_WARNING_RESTORE() - -#include - using namespace godot; class DebugDrawManager : public CanvasLayer { diff --git a/src/default_sources.json b/src/default_sources.json index e779545d..456aaa57 100644 --- a/src/default_sources.json +++ b/src/default_sources.json @@ -5,6 +5,7 @@ "2d/grouped_text.cpp", "2d/stats_2d.cpp", "3d/config_3d.cpp", + "3d/config_scoped_3d.cpp", "3d/debug_draw_3d.cpp", "3d/debug_geometry_container.cpp", "3d/geometry_generators.cpp", diff --git a/src/dev_debug_draw_3d_Library.vcxproj b/src/dev_debug_draw_3d_Library.vcxproj index 5ecb39bf..becb124a 100644 --- a/src/dev_debug_draw_3d_Library.vcxproj +++ b/src/dev_debug_draw_3d_Library.vcxproj @@ -190,6 +190,9 @@ false + + false + false @@ -243,6 +246,9 @@ false + + false + false @@ -267,6 +273,9 @@ false + + false + false diff --git a/src/dev_debug_draw_3d_Library.vcxproj.filters b/src/dev_debug_draw_3d_Library.vcxproj.filters index a2bf4dce..56d774be 100644 --- a/src/dev_debug_draw_3d_Library.vcxproj.filters +++ b/src/dev_debug_draw_3d_Library.vcxproj.filters @@ -60,6 +60,9 @@ common + + 3d + @@ -127,6 +130,12 @@ common + + common + + + 3d + diff --git a/src/editor/asset_library_update_checker.h b/src/editor/asset_library_update_checker.h index a4325e3f..657d4708 100644 --- a/src/editor/asset_library_update_checker.h +++ b/src/editor/asset_library_update_checker.h @@ -11,7 +11,6 @@ GODOT_WARNING_DISABLE() #include #include GODOT_WARNING_RESTORE() - using namespace godot; class AssetLibraryUpdateChecker : public RefCounted { diff --git a/src/editor/editor_menu_extensions.h b/src/editor/editor_menu_extensions.h index 973bd751..274ea9e3 100644 --- a/src/editor/editor_menu_extensions.h +++ b/src/editor/editor_menu_extensions.h @@ -6,7 +6,6 @@ GODOT_WARNING_DISABLE() #include GODOT_WARNING_RESTORE() - using namespace godot; class DebugDrawMenuExtensionPlugin : public EditorPlugin { diff --git a/src/editor/generate_csharp_bindings.h b/src/editor/generate_csharp_bindings.h index f63cb1e3..7e286777 100644 --- a/src/editor/generate_csharp_bindings.h +++ b/src/editor/generate_csharp_bindings.h @@ -11,7 +11,6 @@ GODOT_WARNING_DISABLE() #include #include GODOT_WARNING_RESTORE() - using namespace godot; class GenerateCSharpBindingsPlugin { diff --git a/src/register_types.cpp b/src/register_types.cpp index 515d8d07..583a2389 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -5,11 +5,11 @@ #include "2d/graphs.h" #include "2d/stats_2d.h" #include "3d/config_3d.h" +#include "3d/config_scoped_3d.h" #include "3d/debug_draw_3d.h" #include "3d/stats_3d.h" #include "debug_draw_manager.h" #include "utils/utils.h" -using namespace godot; DebugDrawManager *debug_draw_manager = nullptr; @@ -26,6 +26,7 @@ GODOT_WARNING_RESTORE() Ref upd_checker; #endif #endif +using namespace godot; /** GDExtension Initialize **/ void initialize_debug_draw_3d_module(ModuleInitializationLevel p_level) { @@ -55,6 +56,7 @@ void initialize_debug_draw_3d_module(ModuleInitializationLevel p_level) { ClassDB::register_class(); EditorPlugins::add_by_type(); + // TODO register as unexposed ClassDB::register_class(); upd_checker.instantiate(); } diff --git a/src/utils/math_utils.h b/src/utils/math_utils.h index 04bf229a..758d7d87 100644 --- a/src/utils/math_utils.h +++ b/src/utils/math_utils.h @@ -2,13 +2,12 @@ #include "compiler.h" +#include + GODOT_WARNING_DISABLE() #include #include GODOT_WARNING_RESTORE() - -#include - using namespace godot; class MathUtils {