Skip to content

Commit

Permalink
added conditional compilation and runtime checks to disable draw call…
Browse files Browse the repository at this point in the history
…s in C#

added 2d stats
  • Loading branch information
DmitriySalnikov committed Sep 16, 2023
1 parent c1cbc2f commit 3da6b63
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 116 deletions.
2 changes: 1 addition & 1 deletion addons/debug_draw_3d/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Most likely, when exporting a release version of a game, you don't want to expor

![export_features](/images/export_features.png)

In C#, these tags are not taken into account at the moment. So you will have to manually specify `FORCED_DD3D`.
In C#, these tags are not taken into account at compile time, so the Release build will use Runtime checks to disable draw calls. If you want to avoid this, you can manually specify the `FORCED_DD3D` symbol.

![csharp_compilation_symbols](/images/csharp_compilation_symbols.png)

Expand Down
28 changes: 23 additions & 5 deletions examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ extends Node3D

@export_group("Text groups", "text_groups")
@export var text_groups_show_hints := true
@export var text_groups_show_stats := true
@export var text_groups_show_stats := false
@export var text_groups_show_stats_2d := false
@export var text_groups_position := DebugDrawConfig2D.POSITION_LEFT_TOP
@export var text_groups_offset := Vector2i(8, 8)
@export var text_groups_padding := Vector2i(3, 1)
@export_range(1, 100) var text_groups_default_font_size := 12
@export_range(1, 100) var text_groups_title_font_size := 14
@export_range(1, 100) var text_groups_text_font_size := 12

@export_category("Graphs")
@export_group("Graphs", "graph")
@export var graph_offset := Vector2i(8, 8)
@export var graph_size := Vector2i(200, 80)
@export_range(1, 100) var graph_title_font_size := 14
Expand Down Expand Up @@ -222,7 +223,7 @@ func _process(delta: float) -> void:
DebugDraw2D.get_graph(&"FPS").offset = Vector2i(0, 64)

# Adding more graphs
if test_graphs:
if test_graphs and DebugDraw2D.debug_enabled:
_graph_test()
else:
_remove_graphs()
Expand Down Expand Up @@ -260,11 +261,11 @@ func _text_tests():
DebugDraw2D.set_text("Rendered frames", Engine.get_frames_drawn())
DebugDraw2D.end_text_group()

if text_groups_show_stats:
if text_groups_show_stats or text_groups_show_stats_2d:
DebugDraw2D.begin_text_group("-- Stats --", 3, Color.WHEAT)

var render_stats := DebugDraw3D.get_render_stats()
if render_stats:
if render_stats && text_groups_show_stats:
DebugDraw2D.set_text("Total", render_stats.total_geometry)
DebugDraw2D.set_text("Instances", render_stats.instances, 1)
DebugDraw2D.set_text("Lines", render_stats.lines, 2)
Expand All @@ -279,6 +280,16 @@ func _text_tests():
DebugDraw2D.set_text("Filling lines buffer", "%.2f ms" % (render_stats.time_filling_buffers_lines_usec / 1000.0), 9)
DebugDraw2D.set_text("Filling time", "%.2f ms" % (render_stats.total_time_filling_buffers_usec / 1000.0), 10)
DebugDraw2D.set_text("Total time", "%.2f ms" % (render_stats.total_time_spent_usec / 1000.0), 11)

if text_groups_show_stats && text_groups_show_stats_2d:
DebugDraw2D.set_text("----", null, 19)

var render_stats_2d := DebugDraw2D.get_render_stats()
if render_stats_2d && text_groups_show_stats_2d:
DebugDraw2D.set_text("Text groups", render_stats_2d.overlay_text_groups, 20)
DebugDraw2D.set_text("Text lines", render_stats_2d.overlay_text_lines, 21)
DebugDraw2D.set_text("Graphs total", render_stats_2d.overlay_graphs_total, 22)
DebugDraw2D.set_text("Graphs enabled", render_stats_2d.overlay_graphs_enabled, 23)

DebugDraw2D.end_text_group()

Expand Down Expand Up @@ -340,6 +351,12 @@ func _graph_test():
_create_graph(&"fps10", true, false, DebugDrawGraph.TEXT_ALL, &"fps8", DebugDrawGraph.SIDE_TOP)
# warning-ignore:return_value_discarded
_create_graph(&"fps11", true, true, DebugDrawGraph.TEXT_ALL, &"fps9", DebugDrawGraph.SIDE_RIGHT)
# warning-ignore:return_value_discarded
_create_graph(&"fps13", true, true, DebugDrawGraph.TEXT_ALL, &"", DebugDrawGraph.SIDE_RIGHT)
if not DebugDraw2D.get_graph(&"fps13"):
return

DebugDraw2D.get_graph(&"fps13").enabled = false

# If graphs exists, then more tests are done
DebugDraw2D.get_graph(&"Sin Wave!").data_getter = Callable(self, &"_get_sin_wave_for_graph")
Expand Down Expand Up @@ -394,6 +411,7 @@ func _remove_graphs():
DebugDraw2D.remove_graph(&"fps10")
DebugDraw2D.remove_graph(&"fps11")
DebugDraw2D.remove_graph(&"fps12")
DebugDraw2D.remove_graph(&"fps13")


func _create_graph(title, is_fps, show_title, flags, parent := &"", parent_side := DebugDrawGraph.SIDE_BOTTOM, pos = DebugDrawGraph.POSITION_LEFT_BOTTOM, size := Vector2i(256, 60), font = null) -> DebugDrawGraph:
Expand Down
4 changes: 3 additions & 1 deletion examples_dd3d/DebugDrawDemoScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ process_priority = 1
script = ExtResource("1")
custom_font = ExtResource("2_aedbq")
start_culling_distance = 55.0
text_groups_show_stats = true
text_groups_show_stats_2d = true
text_groups_position = 2
text_groups_default_font_size = 15
text_groups_title_font_size = 20
Expand Down Expand Up @@ -253,7 +255,7 @@ stretch = true
[node name="Viewport" type="SubViewport" parent="Panel/ViewportContainer"]
handle_input_locally = false
size = Vector2i(2, 2)
size = Vector2i(300, 300)
render_target_update_mode = 0
[node name="CameraLayer2_5" type="Camera3D" parent="Panel/ViewportContainer/Viewport"]
Expand Down
32 changes: 27 additions & 5 deletions src/2d/debug_draw_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,20 @@ Control *DebugDraw2D::get_custom_canvas() const {
#pragma region Draw Functions

Ref<DebugDrawStats2D> DebugDraw2D::get_render_stats() {
// TODO: add 2d version
#ifndef DISABLE_DEBUG_RENDERING
Ref<DebugDrawStats2D> stats;
stats.instantiate();
stats->setup(
grouped_text->get_text_group_count(),
grouped_text->get_text_line_total_count(),

data_graphs->get_graphs_enabled(),
data_graphs->get_graphs_total());

return stats;
#else
return Ref<DebugDrawStats2D>();
#endif
}

void DebugDraw2D::clear_2d_objects() {
Expand All @@ -235,12 +247,22 @@ void DebugDraw2D::clear_2d_objects() {
if (!obj || NEED_LEAVE) return; \
obj->func(__VA_ARGS__)

#define FORCE_CALL_TO_2D(obj, func, ...) \
if (!obj) return; \
obj->func(__VA_ARGS__)

#define CALL_TO_2D_RET(obj, func, def, ...) \
if (!obj || NEED_LEAVE) return def; \
return obj->func(__VA_ARGS__)

#define FORCE_CALL_TO_2D_RET(obj, func, def, ...) \
if (!obj) return def; \
return obj->func(__VA_ARGS__)
#else
#define CALL_TO_2D(obj, func, ...) return
#define FORCE_CALL_TO_2D(obj, func, ...) return
#define CALL_TO_2D_RET(obj, func, def, ...) return def
#define FORCE_CALL_TO_2D_RET(obj, func, def, ...) return def
#endif

void DebugDraw2D::begin_text_group(String group_title, int group_priority, Color group_color, bool show_title, int title_size, int text_size) {
Expand Down Expand Up @@ -271,19 +293,19 @@ void DebugDraw2D::graph_update_data(const StringName &title, real_t data) {
}

void DebugDraw2D::remove_graph(const StringName &title) {
CALL_TO_2D(data_graphs, remove_graph, title);
FORCE_CALL_TO_2D(data_graphs, remove_graph, title);
}

void DebugDraw2D::clear_graphs() {
CALL_TO_2D(data_graphs, clear_graphs);
FORCE_CALL_TO_2D(data_graphs, clear_graphs);
}

Ref<DebugDrawGraph> DebugDraw2D::get_graph(const StringName &title) {
CALL_TO_2D_RET(data_graphs, get_graph, Ref<DebugDrawGraph>(), title);
FORCE_CALL_TO_2D_RET(data_graphs, get_graph, Ref<DebugDrawGraph>(), title);
}

PackedStringArray DebugDraw2D::get_graph_names() {
CALL_TO_2D_RET(data_graphs, get_graph_names, PackedStringArray());
FORCE_CALL_TO_2D_RET(data_graphs, get_graph_names, PackedStringArray());
}

#pragma endregion // Graphs
Expand Down
17 changes: 16 additions & 1 deletion src/2d/graphs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ DebugDrawGraph::graph_rects DebugDrawGraph::draw(CanvasItem *_ci, const Ref<Font
if (get_show_text_flags() & DebugDrawGraph::TextFlags::TEXT_MIN) {
_ci->draw_string(draw_font,
(pos + Vector2(4, graph_size.y - 3)).floor(),
FMT_STR("min: {0} {1}",format_float(min, precision), get_text_suffix()),
FMT_STR("min: {0} {1}", format_float(min, precision), get_text_suffix()),
godot::HORIZONTAL_ALIGNMENT_LEFT, -1, get_text_size(), get_text_color());
}

Expand Down Expand Up @@ -826,4 +826,19 @@ PackedStringArray DataGraphManager::get_graph_names() const {
return res;
}

size_t DataGraphManager::get_graphs_enabled() const {
LOCK_GUARD(datalock);
size_t total = 0;
for (auto &g : graphs) {
if (g->is_enabled())
total++;
}
return total;
}

size_t DataGraphManager::get_graphs_total() const {
LOCK_GUARD(datalock);
return graphs.size();
}

#endif
3 changes: 2 additions & 1 deletion src/2d/graphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class DebugDrawGraph : public RefCounted {
};

struct graph_interpolated_values_range {
public:
double shrink_weight_max, shrink_weight_min;
double upd_timer_max, upd_timer_min, max_timer_delay;
double min, max, avg;
Expand Down Expand Up @@ -251,6 +250,8 @@ class DataGraphManager {
void clear_graphs();
Ref<DebugDrawGraph> get_graph(const StringName &_title) const;
PackedStringArray get_graph_names() const;
size_t get_graphs_enabled() const;
size_t get_graphs_total() const;
};

#endif
17 changes: 17 additions & 0 deletions src/2d/grouped_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,21 @@ void GroupedText::draw(CanvasItem *_ci, const Ref<Font> &_font, const Vector2 &_
}
}

size_t GroupedText::get_text_group_count() {
LOCK_GUARD(datalock);
return _text_groups.size();
}

size_t GroupedText::get_text_line_total_count() {
LOCK_GUARD(datalock);
size_t total = 0;
for (const auto &g : _text_groups) {
total += g->Texts.size();
if (g->is_show_title()) {
total++;
}
}
return total;
}

#endif
3 changes: 3 additions & 0 deletions src/2d/grouped_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class GroupedText {
void end_text_group();
void set_text(const String &_key, const Variant &_value, const int &_priority, const Color &_color_of_value, const double &_duration);
void draw(CanvasItem *_ci, const Ref<Font> &_font, const Vector2 &_vp_size);

size_t get_text_group_count();
size_t get_text_line_total_count();
};

#endif
18 changes: 5 additions & 13 deletions src/2d/stats_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,11 @@ void DebugDrawStats2D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(type, #name), NAMEOF(set_##name), NAMEOF(get_##name));
#pragma region Parameters

REG_PROPERTY_NO_SET(instances, Variant::INT);
REG_PROPERTY_NO_SET(lines, Variant::INT);
REG_PROPERTY_NO_SET(total_geometry, Variant::INT);
REG_PROPERTY_NO_SET(visible_instances, Variant::INT);
REG_PROPERTY_NO_SET(visible_lines, Variant::INT);
REG_PROPERTY_NO_SET(total_visible, Variant::INT);
REG_PROPERTY_NO_SET(time_filling_buffers_instances_usec, Variant::INT);
REG_PROPERTY_NO_SET(time_filling_buffers_lines_usec, Variant::INT);
REG_PROPERTY_NO_SET(total_time_filling_buffers_usec, Variant::INT);
REG_PROPERTY_NO_SET(time_culling_instant_usec, Variant::INT);
REG_PROPERTY_NO_SET(time_culling_delayed_usec, Variant::INT);
REG_PROPERTY_NO_SET(total_time_culling_usec, Variant::INT);
REG_PROPERTY_NO_SET(total_time_spent_usec, Variant::INT);
REG_PROPERTY_NO_SET(overlay_text_groups, Variant::INT);
REG_PROPERTY_NO_SET(overlay_text_lines, Variant::INT);

REG_PROPERTY_NO_SET(overlay_graphs_enabled, Variant::INT);
REG_PROPERTY_NO_SET(overlay_graphs_total, Variant::INT);

#undef REG_PROPERTY_NO_SET
#pragma endregion
Expand Down
52 changes: 12 additions & 40 deletions src/2d/stats_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,26 @@ public: \
type get_##name() const { return name; } \
void set_##name(int64_t val) {}

DEFINE_DEFAULT_PROP(int64_t, instances, 0);
DEFINE_DEFAULT_PROP(int64_t, lines, 0);
DEFINE_DEFAULT_PROP(int64_t, total_geometry, 0);
DEFINE_DEFAULT_PROP(int64_t, overlay_text_groups, 0);
DEFINE_DEFAULT_PROP(int64_t, overlay_text_lines, 0);

DEFINE_DEFAULT_PROP(int64_t, visible_instances, 0);
DEFINE_DEFAULT_PROP(int64_t, visible_lines, 0);
DEFINE_DEFAULT_PROP(int64_t, total_visible, 0);

DEFINE_DEFAULT_PROP(int64_t, time_filling_buffers_instances_usec, 0);
DEFINE_DEFAULT_PROP(int64_t, time_filling_buffers_lines_usec, 0);
DEFINE_DEFAULT_PROP(int64_t, total_time_filling_buffers_usec, 0);

DEFINE_DEFAULT_PROP(int64_t, time_culling_instant_usec, 0);
DEFINE_DEFAULT_PROP(int64_t, time_culling_delayed_usec, 0);
DEFINE_DEFAULT_PROP(int64_t, total_time_culling_usec, 0);

DEFINE_DEFAULT_PROP(int64_t, total_time_spent_usec, 0);
DEFINE_DEFAULT_PROP(int64_t, overlay_graphs_enabled, 0);
DEFINE_DEFAULT_PROP(int64_t, overlay_graphs_total, 0);

#undef DEFINE_DEFAULT_PROP

DebugDrawStats2D(){};

void setup(
const int64_t &t_instances,
const int64_t &t_lines,
const int64_t &t_visible_instances,
const int64_t &t_visible_lines,
const int64_t &t_time_filling_buffers_instances_usec,
const int64_t &t_time_filling_buffers_lines_usec,
const int64_t &t_time_culling_instant_usec,
const int64_t &t_time_culling_delayed_usec) {

instances = t_instances;
lines = t_lines;
total_geometry = t_instances + t_lines;

visible_instances = t_visible_instances;
visible_lines = t_visible_lines;
total_visible = t_visible_instances + t_visible_lines;

time_filling_buffers_instances_usec = t_time_filling_buffers_instances_usec;
time_filling_buffers_lines_usec = t_time_filling_buffers_lines_usec;
total_time_filling_buffers_usec = t_time_filling_buffers_instances_usec + t_time_filling_buffers_lines_usec;
const int64_t &t_overlay_text_groups,
const int64_t &t_overlay_text_lines,
const int64_t &t_overlay_graphs_enabled,
const int64_t &t_overlay_graphs_total) {

time_culling_instant_usec = t_time_culling_instant_usec;
time_culling_delayed_usec = t_time_culling_delayed_usec;
total_time_culling_usec = t_time_culling_instant_usec + t_time_culling_delayed_usec;
overlay_text_groups = t_overlay_text_groups;
overlay_text_lines = t_overlay_text_lines;

total_time_spent_usec = total_time_filling_buffers_usec + total_time_culling_usec;
overlay_graphs_enabled = t_overlay_graphs_enabled;
overlay_graphs_total = t_overlay_graphs_total;
};
};
1 change: 1 addition & 0 deletions src/3d/render_instances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/time.hpp>
GODOT_WARNING_RESTORE()

// TODO too big buffer constantly shrinking
template <class T, size_t _TStepSize>
struct TempBigBuffer {
private:
Expand Down
2 changes: 1 addition & 1 deletion src/debug_draw_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#endif

GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/file_access.hpp>
#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/file_access.hpp>
#include <godot_cpp/classes/standard_material3d.hpp>
GODOT_WARNING_RESTORE()

Expand Down
Loading

0 comments on commit 3da6b63

Please sign in to comment.