Skip to content

Commit

Permalink
GeometryPool's are separated to individually handle instances created…
Browse files Browse the repository at this point in the history
… in `_physics_process` and in `_process`
  • Loading branch information
DmitriySalnikov committed Dec 9, 2023
1 parent 054f2dc commit fc7d546
Show file tree
Hide file tree
Showing 14 changed files with 394 additions and 202 deletions.
30 changes: 26 additions & 4 deletions examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ extends Node3D

@export var custom_font : Font
@export var zylann_example := false
@export var update_in_physics := false
@export var test_text := true
@export var test_graphs := false
@export var more_test_cases := true
@export var draw_array_of_boxes := false
@export_range(0, 5, 0.001) var debug_thickness := 0.05
@export_range(0, 1, 0.001) var debug_center_brightness := 0.8
@export_range(0, 1024) var start_culling_distance := 55.0

@export_group("Text groups", "text_groups")
Expand Down Expand Up @@ -67,11 +69,24 @@ func _update_keys_just_press():
button_presses[KEY_3] = set_key.call(KEY_3)


func _process(delta: float) -> void:
func _process(delta):
if !update_in_physics:
main_update(delta)


func _physics_process(delta: float) -> void:
if update_in_physics:
main_update(delta)

if not zylann_example:
DebugDraw3D.draw_line($"Lines/8".global_transform.origin, $Lines/Target.global_transform.origin, Color.YELLOW)


func main_update(delta: float) -> void:
if Input.is_action_just_pressed("ui_end"):
DebugDraw3D.regenerate_geometry_meshes()

DebugDraw3D.scoped_config().set_thickness(debug_thickness)
DebugDraw3D.scoped_config().set_thickness(debug_thickness).set_center_brightness(debug_center_brightness)
if false: #test
var _s11 = DebugDraw3D.new_scoped_config().set_thickness(1)
if true:
Expand Down Expand Up @@ -204,7 +219,10 @@ func _process(delta: float) -> void:
var points_below2: PackedVector3Array = []
var points_below3: PackedVector3Array = []
var lines_above: PackedVector3Array = []

for c in $LinePath.get_children():
if not c is Node3D:
break
points.append(c.global_transform.origin)
points_below.append(c.global_transform.origin + Vector3.DOWN)
points_below2.append(c.global_transform.origin + Vector3.DOWN * 2)
Expand All @@ -226,15 +244,19 @@ func _process(delta: float) -> void:
var _a11 = DebugDraw3D.new_scoped_config().set_thickness(0)
DebugDraw3D.draw_camera_frustum($Camera, Color.DARK_ORANGE)

DebugDraw3D.draw_arrow($Misc/Arrow.global_transform, Color.YELLOW_GREEN)
if true:
var _s123 = DebugDraw3D.new_scoped_config().set_center_brightness(0.1)
DebugDraw3D.draw_arrow($Misc/Arrow.global_transform, Color.YELLOW_GREEN)

DebugDraw3D.draw_square($Misc/Billboard.global_transform.origin, 0.5, Color.GREEN)

DebugDraw3D.draw_position($Misc/Position.global_transform, Color.BROWN)

DebugDraw3D.draw_gizmo($Misc/GizmoTransform.global_transform, DebugDraw3D.empty_color, true)
DebugDraw3D.draw_gizmo($Misc/GizmoNormal.global_transform.orthonormalized(), DebugDraw3D.empty_color, false)
DebugDraw3D.draw_gizmo($Misc/GizmoOneColor.global_transform, Color.BROWN, true)
if true:
var _s123 = DebugDraw3D.new_scoped_config().set_center_brightness(0.5)
DebugDraw3D.draw_gizmo($Misc/GizmoNormal.global_transform.orthonormalized(), DebugDraw3D.empty_color, false)

var tg : Transform3D = $Misc/Grids/Grid.global_transform
var tn : Vector3 = $Misc/Grids/Grid/Subdivision.transform.origin
Expand Down
22 changes: 14 additions & 8 deletions examples_dd3d/DebugDrawDemoScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ func _on_Button_pressed() -> void:
process_priority = 1
script = ExtResource("1")
custom_font = ExtResource("2_aedbq")
update_in_physics = true
draw_array_of_boxes = true
text_groups_show_stats = true
text_groups_position = 2
[node name="Camera" type="Camera3D" parent="."]
Expand Down Expand Up @@ -430,13 +433,6 @@ transform = Transform3D(9.78549, 0, 0, 0, 4.20302, 0, 0, 0, 5.62455, -23.6827, -
[node name="Lines" type="Node3D" parent="Zones"]
transform = Transform3D(10.7186, 0, 0, 0, 3.9777, 0, 0, 0, 7.05487, 10.6302, 1.91174, -7.11416)
[node name="LinePathAnim" type="AnimationPlayer" parent="."]
root_node = NodePath("../LinePath")
libraries = {
"": SubResource("AnimationLibrary_nj4nv")
}
autoplay = "New Anim"
[node name="Label3D" type="Label3D" parent="."]
transform = Transform3D(1, -1.93359e-07, -8.48396e-08, -1.17881e-07, 1, 5.96046e-08, 6.22839e-09, 0, 1, 0, 0, 0)
visible = false
Expand All @@ -449,6 +445,13 @@ text = "TestTestTestTest
TestTestTest"
font_size = 80
[node name="LinesAnim" type="AnimationPlayer" parent="."]
root_node = NodePath("../LinePath")
libraries = {
"": SubResource("AnimationLibrary_nj4nv")
}
autoplay = "New Anim"
[node name="LinePath" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.67362, -8)
Expand Down Expand Up @@ -624,11 +627,14 @@ transform = Transform3D(1, 6.61592e-09, 2.23038e-08, 4.9239e-07, 1, 0, -3.40677e
transform = Transform3D(-0.998871, -0.0207882, -0.0355643, 0.0855375, -0.5714, -2.68836, 0.0136011, -0.249864, 0.572532, 1.43126, 0.26242, 1.92347)
[node name="6" type="Node3D" parent="Lines"]
transform = Transform3D(-0.998872, -0.0207882, -0.0355643, 0.0855371, -0.5714, -2.68836, 0.0136012, -0.249864, 0.572533, 1.43441, 1.50606, 1.20028)
transform = Transform3D(-0.998872, -0.0207882, -0.0355643, 0.085537, -0.5714, -2.68836, 0.0136012, -0.249864, 0.572533, 1.43441, 1.50606, 1.20028)
[node name="7" type="Node3D" parent="Lines"]
transform = Transform3D(-0.998873, -0.0207882, -0.0355641, 0.0855357, -0.5714, -2.68836, 0.0136014, -0.249864, 0.572533, 0.0511096, -1.3236, 1.06745)
[node name="8" type="Node3D" parent="Lines"]
transform = Transform3D(-0.998873, -0.0207882, -0.0355641, 0.0855353, -0.5714, -2.68836, 0.0136016, -0.249864, 0.572533, -1.01372, -3.80486, 1.25019)
[node name="Target" type="Node3D" parent="Lines"]
transform = Transform3D(1, -2.7352e-06, 2.60722e-07, 3.7807e-06, 1, 0, -3.59182e-07, -1.49012e-08, 1, -0.69134, 0.176476, 1.30597)
Expand Down
15 changes: 15 additions & 0 deletions src/2d/debug_draw_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ void DebugDraw2D::process(double delta) {
#endif
}

void DebugDraw2D::physics_process_start(double delta) {
ZoneScoped;
#ifndef DISABLE_DEBUG_RENDERING
FrameMarkStart("2D Physics Step");
#endif
}

void DebugDraw2D::physics_process_end(double delta) {
ZoneScoped;
#ifndef DISABLE_DEBUG_RENDERING
// TODO implement
FrameMarkEnd("2D Physics Step");
#endif
}

#ifndef DISABLE_DEBUG_RENDERING
void DebugDraw2D::_finish_frame_and_update() {
ZoneScoped;
Expand Down
2 changes: 2 additions & 0 deletions src/2d/debug_draw_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class DebugDraw2D : public Object {
inline bool _is_enabled_override() const;

void process(double delta);
void physics_process_start(double delta);
void physics_process_end(double delta);

#pragma region Exposed Parameter Values

Expand Down
33 changes: 30 additions & 3 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,31 @@ DebugDraw3D::~DebugDraw3D() {

void DebugDraw3D::process(double delta) {
ZoneScoped;
FrameMarkStart("3D Setup");
#ifndef DISABLE_DEBUG_RENDERING
FrameMarkStart("3D Update");

// Update 3D debug
dgc->update_geometry(delta);

_clear_scoped_configs();
FrameMarkEnd("3D Update");
#endif
}

void DebugDraw3D::physics_process_start(double delta) {
ZoneScoped;
#ifndef DISABLE_DEBUG_RENDERING
FrameMarkStart("3D Physics Step");
dgc->update_geometry_physics_start(delta);
#endif
}

void DebugDraw3D::physics_process_end(double delta) {
ZoneScoped;
#ifndef DISABLE_DEBUG_RENDERING
dgc->update_geometry_physics_end(delta);
FrameMarkEnd("3D Physics Step");
#endif
FrameMarkEnd("3D Setup");
}

#ifndef DISABLE_DEBUG_RENDERING
Expand Down Expand Up @@ -459,6 +475,7 @@ void DebugDraw3D::clear_all() {

#ifndef DISABLE_DEBUG_RENDERING
#define IS_DEFAULT_COLOR(name) (name == Colors::empty_color)
#define GET_PROC_TYPE() (root_node->is_physics_processing() ? ProcessType::PHYSICS_PROCESS : ProcessType::PROCESS)
#define CHECK_BEFORE_CALL() \
if (!dgc || NEED_LEAVE || config->is_freeze_3d_render()) return;
#else
Expand All @@ -479,7 +496,7 @@ void DebugDraw3D::add_or_update_line_with_thickness(real_t _exp_time, const std:
LOCK_GUARD(datalock);

if (!scfg->get_thickness()) {
dgc->geometry_pool.add_or_update_line(_exp_time, _lines, _col);
dgc->geometry_pool.add_or_update_line(_exp_time, GET_PROC_TYPE(), _lines, _col);
} else {
for (int i = 0; i < _lines.size(); i += 2) {
ZoneScoped;
Expand All @@ -490,6 +507,7 @@ void DebugDraw3D::add_or_update_line_with_thickness(real_t _exp_time, const std:
dgc->geometry_pool.add_or_update_instance(
InstanceType::LINE_VOLUMETRIC,
_exp_time,
GET_PROC_TYPE(),
Transform3D(Basis().looking_at(center, get_up_vector(center)).scaled(Vector3(len, len, len)), a), // slow
_col,
_scoped_config_to_custom(scfg.ptr()),
Expand Down Expand Up @@ -520,6 +538,7 @@ void DebugDraw3D::draw_sphere_xf_base(const Transform3D &transform, const Color
dgc->geometry_pool.add_or_update_instance(
_scoped_config_type_convert(hd ? ConvertableInstanceType::SPHERE_HD : ConvertableInstanceType::SPHERE, scfg.ptr()),
duration,
GET_PROC_TYPE(),
transform,
IS_DEFAULT_COLOR(color) ? Colors::chartreuse : color,
_scoped_config_to_custom(scfg.ptr()),
Expand Down Expand Up @@ -563,6 +582,7 @@ void DebugDraw3D::draw_cylinder(const Transform3D &transform, const Color &color
dgc->geometry_pool.add_or_update_instance(
_scoped_config_type_convert(ConvertableInstanceType::CYLINDER, scfg.ptr()),
duration,
GET_PROC_TYPE(),
transform,
IS_DEFAULT_COLOR(color) ? Colors::forest_green : color,
_scoped_config_to_custom(scfg.ptr()),
Expand Down Expand Up @@ -637,6 +657,7 @@ void DebugDraw3D::draw_box_xf(const Transform3D &transform, const Color &color,
dgc->geometry_pool.add_or_update_instance(
_scoped_config_type_convert(is_box_centered ? ConvertableInstanceType::CUBE_CENTERED : ConvertableInstanceType::CUBE, scfg.ptr()),
duration,
GET_PROC_TYPE(),
transform,
IS_DEFAULT_COLOR(color) ? Colors::forest_green : color,
_scoped_config_to_custom(scfg.ptr()),
Expand Down Expand Up @@ -678,6 +699,7 @@ void DebugDraw3D::draw_line_hit(const Vector3 &start, const Vector3 &end, const
dgc->geometry_pool.add_or_update_instance(
InstanceType::BILLBOARD_SQUARE,
duration,
GET_PROC_TYPE(),
Transform3D(Basis().scaled(Vector3_ONE * hit_size), hit),
IS_DEFAULT_COLOR(hit_color) ? config->get_line_hit_color() : hit_color,
Color(), // Just a plane, no need to store custom data
Expand Down Expand Up @@ -779,6 +801,7 @@ void DebugDraw3D::create_arrow(const Vector3 &a, const Vector3 &b, const Color &
dgc->geometry_pool.add_or_update_instance(
_scoped_config_type_convert(ConvertableInstanceType::ARROWHEAD, scfg.ptr()),
duration,
GET_PROC_TYPE(),
t,
IS_DEFAULT_COLOR(color) ? Colors::light_green : color,
_scoped_config_to_custom(scfg.ptr()),
Expand All @@ -795,6 +818,7 @@ void DebugDraw3D::draw_arrow(const Transform3D &transform, const Color &color, c
dgc->geometry_pool.add_or_update_instance(
_scoped_config_type_convert(ConvertableInstanceType::ARROWHEAD, scfg.ptr()),
duration,
GET_PROC_TYPE(),
transform,
IS_DEFAULT_COLOR(color) ? Colors::light_green : color,
_scoped_config_to_custom(scfg.ptr()),
Expand Down Expand Up @@ -861,6 +885,7 @@ void DebugDraw3D::draw_square(const Vector3 &position, const real_t &size, const
dgc->geometry_pool.add_or_update_instance(
InstanceType::BILLBOARD_SQUARE,
duration,
GET_PROC_TYPE(),
t,
IS_DEFAULT_COLOR(color) ? Colors::red : color,
_scoped_config_to_custom(scfg.ptr()),
Expand All @@ -887,6 +912,7 @@ void DebugDraw3D::draw_position(const Transform3D &transform, const Color &color
dgc->geometry_pool.add_or_update_instance(
_scoped_config_type_convert(ConvertableInstanceType::POSITION, scfg.ptr()),
duration,
GET_PROC_TYPE(),
transform,
IS_DEFAULT_COLOR(color) ? Colors::crimson : color,
_scoped_config_to_custom(scfg.ptr()),
Expand Down Expand Up @@ -1001,4 +1027,5 @@ void DebugDraw3D::draw_camera_frustum_planes(const Array &camera_frustum, const
#endif

#undef IS_DEFAULT_COLOR
#undef GET_PROC_TYPE
#undef NEED_LEAVE
2 changes: 2 additions & 0 deletions src/3d/debug_draw_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class DebugDraw3D : public Object, public IScopedStorage<DDScopedConfig3D> {
inline bool _is_enabled_override() const;

void process(double delta);
void physics_process_start(double delta);
void physics_process_end(double delta);

#pragma region Exposed Parameter Values

Expand Down
23 changes: 19 additions & 4 deletions src/3d/debug_geometry_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ void DebugGeometryContainer::update_geometry(double delta) {
geometry_pool.add_or_update_instance(
InstanceType::SPHERE,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(Vector3_ONE * i.second * 2), i.first),
Colors::debug_bounds,
Color(),
Expand All @@ -286,6 +287,7 @@ void DebugGeometryContainer::update_geometry(double delta) {
geometry_pool.add_or_update_instance(
InstanceType::CUBE,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(diag), bottom),
Colors::debug_bounds,
Color(),
Expand All @@ -298,16 +300,29 @@ void DebugGeometryContainer::update_geometry(double delta) {
geometry_pool.fill_lines_data(immediate_mesh_storage.mesh);

// Update MultiMeshInstances
std::array<Ref<MultiMesh> *, (int)InstanceType::ALL> meshes;
for (int i = 0; i < (int)InstanceType::ALL; i++) {
static std::array<Ref<MultiMesh> *, (int)InstanceType::MAX> meshes;
for (int i = 0; i < (int)InstanceType::MAX; i++) {
meshes[i] = &multi_mesh_storage[i].mesh;
}

geometry_pool.fill_instance_data(meshes);

geometry_pool.scan_visible_instances();
geometry_pool.update_expiration(delta);
geometry_pool.reset_counter(delta);
geometry_pool.update_expiration(delta, ProcessType::PROCESS);
geometry_pool.reset_counter(delta, ProcessType::PROCESS);

is_frame_rendered = true;
}

void DebugGeometryContainer::update_geometry_physics_start(double delta) {
if (is_frame_rendered) {
geometry_pool.reset_counter(delta, ProcessType::PHYSICS_PROCESS);
is_frame_rendered = false;
}
}

void DebugGeometryContainer::update_geometry_physics_end(double delta) {
geometry_pool.update_expiration(delta, ProcessType::PHYSICS_PROCESS);
}

void DebugGeometryContainer::get_render_stats(Ref<DebugDrawStats3D> &stats) {
Expand Down
5 changes: 4 additions & 1 deletion src/3d/debug_geometry_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DebugGeometryContainer {
mesh.unref();
}
};
MultiMeshStorage multi_mesh_storage[(int)InstanceType::ALL] = {};
MultiMeshStorage multi_mesh_storage[(int)InstanceType::MAX] = {};

struct ImmediateMeshStorage {
RID instance;
Expand All @@ -50,6 +50,7 @@ class DebugGeometryContainer {
GeometryPool geometry_pool;
Node *scene_world_node = nullptr;
int32_t render_layers = 1;
bool is_frame_rendered = false;

void CreateMMI(InstanceType type, UsingShaderType shader, const String &name, Ref<ArrayMesh> mesh);

Expand All @@ -61,6 +62,8 @@ class DebugGeometryContainer {
Node *get_world();

void update_geometry(double delta);
void update_geometry_physics_start(double delta);
void update_geometry_physics_end(double delta);

void set_render_layer_mask(int32_t layers);
int32_t get_render_layer_mask() const;
Expand Down
Loading

0 comments on commit fc7d546

Please sign in to comment.