therealnv6 / voxel-impl

A simple engine for our voxel games
1 stars 0 forks source link

optimize chunk loading #4

Open therealnv6 opened 1 year ago

therealnv6 commented 1 year ago

fps literally drops to 0 for a whole second when it's loading new chunks

therealnv6 commented 1 year ago

Okay, so there are a few things I just thought about.

  1. Caching meshes. Once a chunk is meshed, store the mesh inside of a chunk cache. We could do this within the chunk itself, shouldn't be too much of a problem.
  2. Asynchronous meshing. Since meshing using our engine does not depend on the Surface, we can send the (non-initialized) Mesh to the render thread, which allows us to create it in a different thread.
  3. Maybe don't re-render the already rendered chunks? I don't think it should make too much of a difference, but still should reduce the amount of triangles since it's not rendering them layered.
therealnv6 commented 1 year ago

I optimized a bunch in the most recent commit, https://github.com/polykami/voxel-impl/commit/e9a63fd0b9825ff8980552375e4edeae62dfec38.

Here are the changes I made:

  1. Caching meshes. Meshes are now cached inside of the parent Chunk object.
  2. Keeping track of rendered chunks. If chunks are already rendered (and aren't dirty), we won't re-render them. This costs a draw call, and avoids duplicate triangles.
  3. Started working on system for asynchronous chunk generation. We now have this handy ChunkRenderQueue, which keeps tracks of all chunks that should be rendered within the next frame. This can be accessed from other threads. https://github.com/polykami/voxel-impl/blob/e9a63fd0b9825ff8980552375e4edeae62dfec38/src/chunk/container.rs#L145
therealnv6 commented 1 year ago

Newest commit adds multi-threading to the chunk generation, https://github.com/polykami/voxel-impl/commit/36947dc40c1a5556dbfc41c26562a98773b88a42.

Performance still seems to be rather lacking, will have to do more investigation and optimization.

therealnv6 commented 1 year ago

Optimized a lot more in commit https://github.com/polykami/voxel-impl/commit/ddf68ab882f48dd0262678e6fbe36364bc00c3b3. Here are some of the changes:

  1. Chunk unloading. I previously added chunk unloading, however this did not seem to work properly. Chunks are "hidden" (their visibility are set to invisible), rather than despawning the entire chunk entity. This allows for us to re-use the same entity once it has to get rendered again.
  2. Revamp of the chunk update method. We're not using the chunks resource within the thread anymore, instead we're now saving the updates as '(i32, [u8; ChunkShape::SIZE as usize])' (ref: https://github.com/polykami/voxel-impl/blob/ddf68ab882f48dd0262678e6fbe36364bc00c3b3/src/chunk/container/queue.rs#L7)

I did realize some unexpected behavior regarding to chunk loading, as it seems like tons of chunks are being loaded for no apparent reason. I think there is some miscalculation, or a domain/linearity issue somewhere. View video for reference: https://www.youtube.com/watch?v=2IunTrzmROs