Skip to content

Commit

Permalink
Scoped config works, but not integrated
Browse files Browse the repository at this point in the history
Reordered includes
  • Loading branch information
DmitriySalnikov committed Nov 24, 2023
1 parent 3958415 commit e76c99e
Show file tree
Hide file tree
Showing 27 changed files with 263 additions and 112 deletions.
14 changes: 14 additions & 0 deletions examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion src/2d/config_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/font.hpp>
#include <godot_cpp/classes/ref_counted.hpp>
GODOT_WARNING_RESTORE()

using namespace godot;

class DebugDrawConfig2D : public RefCounted {
Expand Down
4 changes: 2 additions & 2 deletions src/2d/debug_draw_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include "stats_2d.h"
#include "utils/utils.h"

#include <limits.h>

GODOT_WARNING_DISABLE()
GODOT_WARNING_RESTORE()

#include <limits.h>

#define NEED_LEAVE (!_is_enabled_override())

DebugDraw2D *DebugDraw2D::singleton = nullptr;
Expand Down
7 changes: 3 additions & 4 deletions src/2d/debug_draw_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
#include "common/colors.h"
#include "utils/compiler.h"

#include <memory>

GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/canvas_item.hpp>
#include <godot_cpp/classes/canvas_layer.hpp>
#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/font.hpp>
#include <godot_cpp/classes/ref_counted.hpp>
GODOT_WARNING_RESTORE()

#include <memory>

using namespace godot;

class DataGraphManager;
Expand Down
9 changes: 4 additions & 5 deletions src/2d/graphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
#include "common/colors.h"
#include "utils/compiler.h"

#include <map>
#include <mutex>
#include <vector>

GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/canvas_item.hpp>
#include <godot_cpp/classes/font.hpp>
#include <godot_cpp/classes/ref_counted.hpp>
GODOT_WARNING_RESTORE()

#include <map>
#include <mutex>
#include <vector>

using namespace godot;

class DataGraphManager;
Expand Down
11 changes: 5 additions & 6 deletions src/2d/grouped_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@

#include "utils/compiler.h"

GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/canvas_item.hpp>
#include <godot_cpp/classes/font.hpp>
#include <godot_cpp/variant/builtin_types.hpp>
GODOT_WARNING_RESTORE()

#include <functional>
#include <memory>
#include <mutex>
#include <unordered_set>

GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/canvas_item.hpp>
#include <godot_cpp/classes/font.hpp>
#include <godot_cpp/variant/builtin_types.hpp>
GODOT_WARNING_RESTORE()
using namespace godot;

class TextGroupItem {
Expand Down
1 change: 0 additions & 1 deletion src/2d/stats_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/ref_counted.hpp>
GODOT_WARNING_RESTORE()

using namespace godot;

class DebugDrawStats2D : public RefCounted {
Expand Down
1 change: 0 additions & 1 deletion src/3d/config_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/ref_counted.hpp>
GODOT_WARNING_RESTORE()

using namespace godot;

class DebugDrawConfig3D : public RefCounted {
Expand Down
42 changes: 42 additions & 0 deletions src/3d/config_scoped_3d.cpp
Original file line number Diff line number Diff line change
@@ -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> DDScopedConfig3D::set_thickness(real_t value) {
thickness = Math::clamp(value, (real_t)0, (real_t)100);
return Ref<DDScopedConfig3D>(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);
}
}
31 changes: 31 additions & 0 deletions src/3d/config_scoped_3d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "utils/compiler.h"

#include <functional>

GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/ref_counted.hpp>
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<void(uint64_t, uint64_t)> unregister_func;
unregister_func unregister_action;
real_t thickness;

public:
Ref<DDScopedConfig3D> 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();
};
104 changes: 76 additions & 28 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,18 @@
#include "stats_3d.h"
#include "utils/utils.h"

#include <limits.h>

GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/camera3d.hpp>
#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/os.hpp>
GODOT_WARNING_RESTORE()

#include <limits.h>

#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

Expand All @@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -110,6 +96,7 @@ DebugDraw3D::DebugDraw3D() {
void DebugDraw3D::init(DebugDrawManager *root) {
root_node = root;
set_config(nullptr);
default_scoped_config.instantiate();

_load_materials();

Expand Down Expand Up @@ -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<DDScopedConfig3D> DebugDraw3D::new_scoped_config() {
LOCK_GUARD(scoped_datalock);
static std::atomic<uint64_t> create_counter = 0;
create_counter++;

return Ref<DDScopedConfig3D>();
uint64_t thread = OS::get_singleton()->get_thread_caller_id();
Ref<DDScopedConfig3D> 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<DDScopedConfig3D> 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() {
Expand Down
Loading

0 comments on commit e76c99e

Please sign in to comment.