Zylann / godot_voxel

Voxel module for Godot Engine
MIT License
2.72k stars 251 forks source link

Create collision shapes from the meshing thread #124

Open Zylann opened 4 years ago

Zylann commented 4 years ago

As seen in https://github.com/Zylann/godot_voxel/issues/54#issuecomment-524662585, creating collision shapes is slow, about 10 times slower than creating Mesh objects. image

However, they are currently created on the main thread, and queued if the time spent doing it exceeds 8 ms, which slows down updates a lot, and the game with it. That means collision shapes are first responsible for collidable terrain update snappiness.

Originally, this happened on the main thread because Mesh objects could not be safely created from a thread without performance degradation, due to how Godot's renderer was implemented (OpenGL), and that Mesh used to be required before creating a collision shape.

However, since the current code for creating them no longer uses Mesh, it could be easily moved to the thread instead of being done on the main thread. Unfortunately, contrary to other servers, Godot doesn't implement any thread-safety on PhysicServer. Which prevents us from doing that. I just spent an hour modifying Voxel Tools code to do this, only to find it would be unsafe :(

This issue must be addressed as soon as Godot implements this. For now, I pushed my WIP in the physics_multithread branch.

Made a proposal: https://github.com/godotengine/godot-proposals/issues/483

snouk-z commented 3 years ago

Hi, I came across the same problem on my custom terrain generator.

I actually had the same idea of withdrawing my PhysicsServer calls from my meshing thread to enqueue them in the main thread. In order to avoid big framerate drops I tried to make an aynschronous (not threaded) worker to dequeue and generate the collisions, it doesn't work well, I still get crashes (asynchronism doesn't seem to be an option either).

I'm looking at the plugin sources right now to figure out which way you choose to handle the collision rendering in your main thead. But if you can share some hints on this, it would save me some time :p Thanks

Zylann commented 3 years ago

I did two things: