Skip to content

Commit

Permalink
greatly increased the speed of culling and buffer filling.
Browse files Browse the repository at this point in the history
updated viewports cache in debugdraw3d.
decreased delay before clearing temporary buffers.
  • Loading branch information
DmitriySalnikov committed Mar 28, 2024
1 parent 76bc4ae commit 03690de
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 163 deletions.
34 changes: 17 additions & 17 deletions examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ func _physics_process(delta: float) -> void:
physics_tick_processed = true
main_update(delta)

_update_timers(delta)


# Physics specific:
if not zylann_example:
DebugDraw3D.draw_line($"Lines/8".global_position, $Lines/Target.global_position, Color.YELLOW)
# Physics specific:
if not zylann_example:
DebugDraw3D.draw_line($"Lines/8".global_position, $Lines/Target.global_position, Color.YELLOW)

if more_test_cases:
_draw_rays_casts()

## Additional drawing in the Viewport
if true:
var _w1 = DebugDraw3D.new_scoped_config().set_viewport(%OtherWorldBox.get_viewport()).set_thickness(0.01).set_center_brightness(1)
DebugDraw3D.draw_box_xf(Transform3D(Basis()
.scaled(Vector3.ONE*0.3)
.rotated(Vector3(0,0,1), PI/4)
.rotated(Vector3(0,1,0), wrapf(Time.get_ticks_msec() / -1500.0, 0, TAU) - PI/4), %OtherWorldBox.global_transform.origin),
Color.BROWN, true, 0.4)

_draw_rays_casts()

## Additional drawing in the Viewport
if false:
var _w1 = DebugDraw3D.new_scoped_config().set_viewport(%OtherWorldBox.get_viewport()).set_thickness(0.01).set_center_brightness(1)
DebugDraw3D.draw_box_xf(Transform3D(Basis()
.scaled(Vector3.ONE*0.3)
.rotated(Vector3(0,0,1), PI/4)
.rotated(Vector3(0,1,0), wrapf(Time.get_ticks_msec() / -1500.0, 0, TAU) - PI/4), %OtherWorldBox.global_transform.origin),
Color.BROWN, true, 0.4)
_update_timers(delta)


func main_update(delta: float) -> void:
Expand Down Expand Up @@ -248,7 +248,7 @@ func main_update(delta: float) -> void:
DebugDraw3D.draw_box_xf(%OtherWorldBox.global_transform.rotated_local(Vector3(-1,1,-1).normalized(), wrapf(Time.get_ticks_msec() / -1000.0, 0, TAU) - PI/4), Color.SANDY_BROWN)

# Misc
if true:
if Engine.is_editor_hint():
#for i in 1000:
var _a11 = DebugDraw3D.new_scoped_config().set_thickness(0)
DebugDraw3D.draw_camera_frustum($Camera, Color.DARK_ORANGE)
Expand Down
8 changes: 4 additions & 4 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ std::shared_ptr<DebugGeometryContainer> DebugDraw3D::get_debug_container(Viewpor

if (const auto &cached_world = viewport_to_world_cache.find(p_vp);
cached_world != viewport_to_world_cache.end()) {
return debug_containers[cached_world->second];
return cached_world->second.dgc;
}

Ref<World3D> vp_world = p_vp->find_world_3d();
Expand All @@ -305,14 +305,14 @@ std::shared_ptr<DebugGeometryContainer> DebugDraw3D::get_debug_container(Viewpor
if (const auto &dgc = debug_containers.find(vp_world_id);
dgc != debug_containers.end()) {

viewport_to_world_cache[p_vp] = dgc->first;
viewport_to_world_cache[p_vp] = { dgc->first, dgc->second };
return dgc->second;
}

auto dgc = create_debug_container();
dgc->set_world(vp_world);
debug_containers[vp_world_id] = dgc;
viewport_to_world_cache[p_vp] = vp_world_id;
viewport_to_world_cache[p_vp] = { vp_world_id, dgc };

call_deferred(NAMEOF(_register_viewport_world_deferred), _get_root_world_viewport(p_vp), vp_world_id);

Expand Down Expand Up @@ -358,7 +358,7 @@ void DebugDraw3D::_remove_debug_container(const uint64_t &p_world_id) {
std::vector<const Viewport *> viewport_to_remove;

for (const auto &p : viewport_to_world_cache) {
if (p.second == p_world_id) {
if (p.second.world_id == p_world_id) {
viewport_to_remove.push_back(p.first);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/3d/debug_draw_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ class DebugDraw3D : public Object, public IScopeStorage<DebugDraw3DScopeConfig,
// Meshes
/// Store World3D id and debug container
std::unordered_map<uint64_t, std::shared_ptr<DebugGeometryContainer> > debug_containers;
std::unordered_map<const Viewport *, uint64_t> viewport_to_world_cache;
struct viewportToWorldCache {
uint64_t world_id = 0;
std::shared_ptr<DebugGeometryContainer> dgc;
};
std::unordered_map<const Viewport *, viewportToWorldCache> viewport_to_world_cache;

// Default materials and shaders
Ref<ShaderMaterial> shader_wireframe_mat;
Expand Down
19 changes: 11 additions & 8 deletions src/3d/debug_geometry_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
// cleanup and get available viewports
std::unordered_set<Viewport *> available_viewports = geometry_pool.get_and_validate_viewports();

// accumulate a time delta to delete objects in any case after their timers expire.
geometry_pool.update_expiration_delta(p_delta, ProcessType::PROCESS);

// Do not update geometry if frozen
if (owner->get_config()->is_freeze_3d_render())
return;
Expand Down Expand Up @@ -301,9 +304,9 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
InstanceType::SPHERE,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(Vector3_ONE * i.extent * 2), i.center),
Transform3D(Basis().scaled(Vector3(i.radius, i.radius, i.radius) * 2), i.center),
Colors::debug_bounds,
SphereBounds(i.center, i.extent.length_squared()),
SphereBounds(i.center, i.radius),
&Colors::empty_color);
}

Expand All @@ -317,9 +320,9 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
InstanceType::CUBE_CENTERED,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(o->bounds.extent * 2), o->bounds.center),
Transform3D(Basis().scaled(Vector3(o->bounds.radius, o->bounds.radius, o->bounds.radius) * 2), o->bounds.center),
Colors::debug_bounds,
SphereBounds(o->bounds.center, abs(o->bounds.extent.length())),
SphereBounds(o->bounds.center, o->bounds.radius),
&Colors::empty_color);
});
}
Expand All @@ -331,10 +334,9 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
}

geometry_pool.reset_visible_objects();
geometry_pool.fill_lines_data(immediate_mesh_storage.mesh, culling_data, p_delta);
geometry_pool.fill_instance_data(meshes, culling_data, p_delta);
geometry_pool.fill_mesh_data(meshes, immediate_mesh_storage.mesh, culling_data);

geometry_pool.update_expiration(p_delta, ProcessType::PROCESS);
geometry_pool.update_viewport_expiration(p_delta, ProcessType::PROCESS);
geometry_pool.reset_counter(p_delta, ProcessType::PROCESS);

is_frame_rendered = true;
Expand All @@ -348,7 +350,8 @@ void DebugGeometryContainer::update_geometry_physics_start(double p_delta) {
}

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

void DebugGeometryContainer::get_render_stats(Ref<DebugDraw3DStats> &p_stats) {
Expand Down
Loading

0 comments on commit 03690de

Please sign in to comment.