-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
debug_draw_manager.cpp
429 lines (352 loc) · 14.8 KB
/
debug_draw_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "debug_draw_manager.h"
#include "2d/debug_draw_2d.h"
#include "2d/grouped_text.h"
#include "3d/debug_draw_3d.h"
#include "utils/utils.h"
#include "version.h"
#ifdef TOOLS_ENABLED
#include "editor/generate_csharp_bindings.h"
#endif
GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/file_access.hpp>
GODOT_WARNING_RESTORE()
using namespace godot;
#ifdef DEV_ENABLED
Object *DebugDrawManager::default_arg_obj = nullptr;
#endif
#ifndef DISABLE_DEBUG_RENDERING
void _DD3D_PhysicsWatcher::init(DebugDrawManager *p_root) {
root_node = p_root;
set_physics_process_priority(INT32_MIN);
}
void _DD3D_PhysicsWatcher::_physics_process(double p_delta) {
root_node->_physics_process_start(p_delta);
}
#endif
DebugDrawManager *DebugDrawManager::singleton = nullptr;
const char *DebugDrawManager::s_initial_state = "initial_debug_state";
const char *DebugDrawManager::s_manager_aliases = NAMEOF(DebugDrawManager) "_singleton_aliases ";
const char *DebugDrawManager::s_dd2d_aliases = NAMEOF(DebugDraw2D) "_singleton_aliases";
const char *DebugDrawManager::s_dd3d_aliases = NAMEOF(DebugDraw3D) "_singleton_aliases";
const char *DebugDrawManager::s_extension_unloading = "extension_unloading";
void DebugDrawManager::_bind_methods() {
#ifdef DEV_ENABLED
ClassDB::bind_method(D_METHOD(NAMEOF(api_test1)), &DebugDrawManager::api_test1);
ClassDB::bind_method(D_METHOD(NAMEOF(api_test2)), &DebugDrawManager::api_test2);
ClassDB::bind_method(D_METHOD(NAMEOF(api_test3)), &DebugDrawManager::api_test3);
ClassDB::bind_method(D_METHOD(NAMEOF(api_test4)), &DebugDrawManager::api_test4);
ClassDB::bind_method(D_METHOD(NAMEOF(api_test5)), &DebugDrawManager::api_test5);
default_arg_obj = memnew(Object);
ClassDB::bind_method(D_METHOD(NAMEOF(api_test6)), &DebugDrawManager::api_test6, default_arg_obj, 1, Variant(), true, 2, 4, 3.5f, "String", "StringName", "Node/Path");
ClassDB::bind_method(D_METHOD(NAMEOF(api_test7)), &DebugDrawManager::api_test7, Color(1, 2, 3, 4), Vector2(1, 2), Vector2i(3, 4), Vector3(1, 2, 3), Vector3i(4, 5, 6), Vector4(1, 2, 3, 4), Vector4i(5, 6, 7, 8), Rect2(Vector2(1, 2), Vector2(3, 4)), Rect2(Vector2(5, 6), Vector2(7, 8)));
ClassDB::bind_method(D_METHOD(NAMEOF(api_test8)), &DebugDrawManager::api_test8, Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(5, 6)), Transform3D(Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9), Vector3(10, 11, 12)), Plane(1, 2, 3, 4), Quaternion(1, 2, 3, 4), AABB(Vector3(1, 2, 3), Vector3(4, 5, 6)), Basis(Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9)), Projection(Vector4(1, 2, 3, 4), Vector4(5, 6, 7, 8), Vector4(9, 10, 11, 12), Vector4(13, 14, 15, 16)));
ClassDB::bind_method(D_METHOD(NAMEOF(api_test9)), &DebugDrawManager::api_test9, RID(), Callable(), Signal(), Utils::make_dict("test", 2, Vector2(2, 1), Plane(4, 3, 2, 1), "ArrayKey_1", Array::make(2, "test_dict")), Array::make(1, "test", SECOND_VALUE));
ClassDB::bind_method(D_METHOD(NAMEOF(api_test10)), &DebugDrawManager::api_test10, PackedByteArray(TypedArray<uint8_t>::make(1, 2, 3, 4)), PackedInt32Array(), PackedInt64Array(TypedArray<int64_t>::make(-1, -2, -3, -4)), PackedFloat32Array(TypedArray<float>::make(0.1f, 0.2f, 0.3f, 0.4f)), PackedFloat64Array(TypedArray<double>::make(10.5, 20.5, 30.5, 40.5)), PackedStringArray(TypedArray<String>::make("1", "2", "3", "4")), PackedVector2Array(TypedArray<String>::make(Vector2(8, 9), Vector2(28, 39))), PackedVector3Array(TypedArray<String>::make(Vector3(7, 8, 9), Vector3(9, 2, 3))), PackedColorArray(TypedArray<String>::make(Color(1, 1, 0, 0.5), Color(1, 0, 1))));
BIND_ENUM_CONSTANT(FIRST_VALUE);
BIND_ENUM_CONSTANT(SECOND_VALUE);
#endif
#define REG_CLASS_NAME DebugDrawManager
#ifdef TOOLS_ENABLED
#ifdef TELEMETRY_ENABLED
ClassDB::bind_method(D_METHOD(NAMEOF(_on_telemetry_sending_completed)), &DebugDrawManager::_on_telemetry_sending_completed);
#endif
#endif
// TODO use CALLABLE_MP in 4.2!
ClassDB::bind_method(D_METHOD(NAMEOF(_integrate_into_engine)), &DebugDrawManager::_integrate_into_engine);
ClassDB::bind_method(D_METHOD(NAMEOF(_on_scene_changed)), &DebugDrawManager::_on_scene_changed);
ClassDB::bind_method(D_METHOD(NAMEOF(clear_all)), &DebugDrawManager::clear_all);
REG_PROP_BOOL(debug_enabled);
ADD_SIGNAL(MethodInfo(s_extension_unloading));
#undef REG_CLASS_NAME
}
Node *DebugDrawManager::_get_current_scene() {
#ifndef DISABLE_DEBUG_RENDERING
auto ST = SCENE_TREE();
if (IS_EDITOR_HINT()) {
return ST->get_edited_scene_root();
}
return ST->get_current_scene();
#else
return nullptr;
#endif
}
void DebugDrawManager::_connect_scene_changed() {
ZoneScoped;
#ifndef DISABLE_DEBUG_RENDERING
// Skip when exiting the tree and finish this loop
if (is_closing) {
return;
}
Node *scene = _get_current_scene();
if (scene && UtilityFunctions::is_instance_valid(scene)) {
scene->connect(StringName("tree_exiting"), Callable(this, NAMEOF(_on_scene_changed)).bindv(Array::make(false)), CONNECT_ONE_SHOT | CONNECT_DEFERRED);
return;
}
SCENE_TREE()->connect(StringName("tree_changed"), Callable(this, NAMEOF(_on_scene_changed)).bindv(Array::make(true)), CONNECT_ONE_SHOT | CONNECT_DEFERRED);
#endif
}
void DebugDrawManager::_on_scene_changed(bool p_is_scene_null) {
ZoneScoped;
#ifndef DISABLE_DEBUG_RENDERING
if (!is_current_scene_is_null || is_current_scene_is_null != p_is_scene_null) {
DEV_PRINT("Scene changed! clear_all()");
debug_draw_3d_singleton->clear_all();
// call this version of `clear_all` to avoid casts on nullptr
debug_draw_2d_singleton->_clear_all_internal(true);
}
is_current_scene_is_null = p_is_scene_null;
_connect_scene_changed();
#endif
}
DebugDrawManager::DebugDrawManager() {
}
DebugDrawManager::~DebugDrawManager() {
UNASSIGN_SINGLETON(DebugDrawManager);
deinit();
#ifdef DEV_ENABLED
memdelete(default_arg_obj);
#endif
}
void DebugDrawManager::clear_all() {
ZoneScoped;
if (debug_draw_2d_singleton)
debug_draw_2d_singleton->clear_all();
if (debug_draw_3d_singleton)
debug_draw_3d_singleton->clear_all();
}
void DebugDrawManager::set_debug_enabled(bool value) {
debug_enabled = value;
if (!value) {
clear_all();
}
}
bool DebugDrawManager::is_debug_enabled() const {
return debug_enabled;
}
void DebugDrawManager::init() {
ZoneScoped;
ASSIGN_SINGLETON(DebugDrawManager);
DEFINE_SETTING_AND_GET(bool initial_debug_state, String(Utils::root_settings_section) + s_initial_state, true, Variant::BOOL);
set_debug_enabled(initial_debug_state);
// manager_aliases.push_back(StringName("Dmgr"));
// dd2d_aliases.push_back(StringName("Dbg2"));
// dd3d_aliases.push_back(StringName("Dbg3"));
root_settings_section = String(Utils::root_settings_section) + "common/";
DEFINE_SETTING_AND_GET_HINT(Variant mgr_a, root_settings_section + s_manager_aliases, manager_aliases, Variant::ARRAY, PropertyHint::PROPERTY_HINT_TYPE_STRING, FMT_STR("{0}:", Variant::STRING_NAME));
DEFINE_SETTING_AND_GET_HINT(Variant dd2d_a, root_settings_section + s_dd2d_aliases, dd2d_aliases, Variant::ARRAY, PropertyHint::PROPERTY_HINT_TYPE_STRING, FMT_STR("{0}:", Variant::STRING_NAME));
DEFINE_SETTING_AND_GET_HINT(Variant dd3d_a, root_settings_section + s_dd3d_aliases, dd3d_aliases, Variant::ARRAY, PropertyHint::PROPERTY_HINT_TYPE_STRING, FMT_STR("{0}:", Variant::STRING_NAME));
manager_aliases = TypedArray<StringName>(mgr_a);
dd2d_aliases = TypedArray<StringName>(dd2d_a);
dd3d_aliases = TypedArray<StringName>(dd3d_a);
Engine::get_singleton()->register_singleton(NAMEOF(DebugDrawManager), this);
_register_singleton_aliases(manager_aliases, this);
debug_draw_2d_singleton = memnew(DebugDraw2D);
Engine::get_singleton()->register_singleton(NAMEOF(DebugDraw2D), debug_draw_2d_singleton);
_register_singleton_aliases(dd2d_aliases, debug_draw_2d_singleton);
debug_draw_3d_singleton = memnew(DebugDraw3D);
Engine::get_singleton()->register_singleton(NAMEOF(DebugDraw3D), debug_draw_3d_singleton);
_register_singleton_aliases(dd3d_aliases, debug_draw_3d_singleton);
debug_draw_2d_singleton->init(this);
debug_draw_3d_singleton->init(this);
call_deferred(NAMEOF(_integrate_into_engine));
}
void DebugDrawManager::deinit() {
ZoneScoped;
is_closing = true;
if (Engine::get_singleton()->has_singleton(NAMEOF(DebugDrawManager))) {
Engine::get_singleton()->unregister_singleton(NAMEOF(DebugDrawManager));
_unregister_singleton_aliases(manager_aliases);
}
if (debug_draw_2d_singleton) {
Engine::get_singleton()->unregister_singleton(NAMEOF(DebugDraw2D));
_unregister_singleton_aliases(dd2d_aliases);
memdelete(debug_draw_2d_singleton);
debug_draw_2d_singleton = nullptr;
}
if (debug_draw_3d_singleton) {
Engine::get_singleton()->unregister_singleton(NAMEOF(DebugDraw3D));
_unregister_singleton_aliases(dd3d_aliases);
memdelete(debug_draw_3d_singleton);
debug_draw_3d_singleton = nullptr;
}
emit_signal(s_extension_unloading);
}
void DebugDrawManager::_register_singleton_aliases(const TypedArray<StringName> &p_names, Object *p_instance) {
for (int i = 0; i < p_names.size(); i++) {
if (!p_names[i].operator godot::StringName().is_empty())
Engine::get_singleton()->register_singleton(p_names[i], p_instance);
}
}
void DebugDrawManager::_unregister_singleton_aliases(const TypedArray<StringName> &p_names) {
for (int i = 0; i < p_names.size(); i++) {
if (!p_names[i].operator godot::StringName().is_empty())
Engine::get_singleton()->unregister_singleton(p_names[i]);
}
}
void DebugDrawManager::_integrate_into_engine() {
ZoneScoped;
// disable by default for headless
{
if (Engine::get_singleton()->has_singleton("DisplayServer")) {
Object *display_server = Engine::get_singleton()->get_singleton("DisplayServer");
if (display_server) {
bool is_enabled = display_server->call("window_can_draw", 0);
set_debug_enabled(is_enabled);
#if defined(TOOLS_ENABLED) && defined(TELEMETRY_ENABLED)
if (IS_EDITOR_HINT() && is_enabled) {
time_usage_reporter = std::make_unique<UsageTimeReporter>(DD3D_VERSION_STR, Utils::root_settings_section, [&]() { call_deferred(NAMEOF(_on_telemetry_sending_completed)); });
}
#endif
}
} else {
set_debug_enabled(false);
}
}
// Draw everything after calls from scripts to avoid lagging
set_process_priority(INT32_MAX);
set_physics_process_priority(INT32_MAX);
SCENE_ROOT()->add_child(this);
SCENE_ROOT()->move_child(this, 0);
#ifndef DISABLE_DEBUG_RENDERING
{
// will be auto deleted as child
_DD3D_PhysicsWatcher *physics_watcher = memnew(_DD3D_PhysicsWatcher);
add_child(physics_watcher);
physics_watcher->init(this);
}
// Useful nodes and names:
// CanvasItemEditor - probably 2D editor viewport
// Node3DEditorViewportContainer - base 3D viewport
// Node3DEditorViewportContainer/Node3DEditorViewport - base of 1 of 4 viewports
// Node3DEditorViewportContainer/Node3DEditorViewport/SubViewportContainer/SubViewport/Camera3D
// Funny hack to get default font
{
Control *c = memnew(Control);
SCENE_ROOT()->add_child(c);
debug_draw_2d_singleton->_font = c->get_theme_default_font();
memdelete(c);
}
// Setup default text group
debug_draw_2d_singleton->grouped_text->end_text_group();
if (IS_EDITOR_HINT()) {
String editor3d = "Node3DEditorViewportContainer";
String subviewport = SubViewport::get_class_static();
Node *res = Utils::find_node_by_class(SCENE_ROOT(), editor3d);
Node *n = res->get_child(0)->get_child(0);
n->set_meta("UseParentSize", true);
debug_draw_2d_singleton->default_canvas = (Control *)n;
// actual tree for godot 4.0 beta 14
//
// 0. VSplitContainer
// 0. Node3DEditorViewportContainer >>> res
// 0. Node3DEditorViewport
// 0. SubViewportContainer >>> res->get_child(0)->get_child(0)
// 0. SubViewport
// 0. Camera3D
// 1. ...
// 1. Node3DEditorViewport
// 0. SubViewportContainer
// 0. SubViewport
// ......
std::vector<SubViewport *> editor_viewports;
TypedArray<Node> viewports_root = res->get_children();
for (int i = 0; i < viewports_root.size(); i++) {
Node *node = cast_to<Node>(viewports_root[i]);
if (node) {
SubViewport *sub_view = cast_to<SubViewport>(Utils::find_node_by_class(node, subviewport));
if (sub_view) {
editor_viewports.push_back(sub_view);
}
}
}
debug_draw_3d_singleton->set_custom_editor_viewport(editor_viewports);
if (editor_viewports.size()) {
debug_draw_3d_singleton->scoped_config()->set_viewport(editor_viewports[0]);
}
/*
// Used to explore the editor tree.
auto f = FileAccess::open("user://tree.txt", FileAccess::WRITE);
f->store_string(Utils::get_scene_tree_as_string(res->get_parent()->get_parent()));
*/
} else {
set_layer(64);
auto default_canvas = memnew(Control);
default_canvas->set_name("DebugDrawDefaultCanvas");
((Control *)default_canvas)->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
((Control *)default_canvas)->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
debug_draw_2d_singleton->default_canvas = default_canvas;
// will be auto deleted as child
add_child(default_canvas);
debug_draw_3d_singleton->scoped_config()->set_viewport(SCENE_ROOT());
}
debug_draw_2d_singleton->set_custom_canvas(debug_draw_2d_singleton->custom_canvas);
_connect_scene_changed();
#endif
#ifdef TOOLS_ENABLED
if (IS_EDITOR_HINT()) {
_try_to_update_cs_bindings();
}
#endif
}
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
// Replace _changes_changed's body with:
// __declspec(noinline) void RenderingServerDefault::_changes_changed() {
// #if 0
// auto *asd = memnew(Object);
// memdelete(asd);
// #endif
// }
#ifndef DISABLE_DEBUG_RENDERING
if (debug_enabled) {
DebugDraw3D::get_singleton()->process(p_delta);
DebugDraw2D::get_singleton()->process(p_delta);
if (!DebugDraw2D::get_singleton()->is_drawing_frame()) {
FrameMark;
}
}
#endif
log_flush_time += p_delta;
if (log_flush_time > 0.25f) {
log_flush_time -= 0.25f;
Utils::print_logs();
}
#ifdef TRACY_ENABLE
if (!debug_enabled || !DebugDraw2D::get_singleton()->is_debug_enabled()) {
FrameMark;
}
#endif
}
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);
}
}
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);
}
#endif
}
#ifdef TOOLS_ENABLED
void DebugDrawManager::_try_to_update_cs_bindings() {
ZoneScoped;
auto g = GenerateCSharpBindingsPlugin();
if (g.is_need_to_update()) {
PRINT_WARNING("C# bindings for 'Debug Draw' were not found or are outdated!");
g.generate();
}
}
#ifdef TELEMETRY_ENABLED
void DebugDrawManager::_on_telemetry_sending_completed() {
if (time_usage_reporter) {
time_usage_reporter->stop_thread();
}
}
#endif
#endif