kpreid / all-is-cubes

Yet another block/voxel game; in this one the blocks are made out of blocks. Runs in browsers on WebGL+WebAssembly.
https://kpreid.dreamwidth.org/tag/all+is+cubes
Apache License 2.0
164 stars 8 forks source link

Instanced block rendering #458

Open kpreid opened 8 months ago

kpreid commented 8 months ago

As of b4d42702f9fa7ed8d22d7ae3b1efc75388cca7e5, there is just-barely-working support for rendering Space contents using instanced block meshes instead of chunk meshes. This already provides great performance improvement for rendering — at least on desktop — but there's a lot of pieces left to do.

kpreid commented 8 months ago

In the wgpu renderer, we're not picking the size of the instance buffer to correctly account for all these block instances. It's working anyway. Why? There must be some overallocation.

This turned out to be overallocation because we allocate for all chunks and only push culled chunks, so it could fail given enough block instances. Fixed in 51d9197916c0546bb050e6303e7b1c953b5ab2e5.

kpreid commented 8 months ago

a0c547497ec9417ef161208a7c8c17c1041e9f53 fixes some of the mesh API and some of the inefficiency; it is now up to the user of ChunkedSpaceMesh to gather instances (so they can do it as part of the iteration they already must do), and none of the internal hash tables are exposed.

kpreid commented 8 months ago

Add tests in the mesh crate for instance behavior.

There are some tests now, added along with specific improvements. I'll call this done-enough-not-to-track even though there's more work ahead.

When a change in the world occurs, consider just adding/replacing one instance instead of remeshing the containing chunk.

This is partly supported via 7a16b293f89a8d577e12562abf3eb0b847b0b723 — instances are never used for blocks that wouldn't be instanced, but instance-only changes should skip the mesh rebuild. However, it doesn't work 100% of the time, for reasons I have not yet diagnosed.

Also of note for performance: 7627d9e630c3bd054ead324c7195999269528a3c added InstanceMap specifically so that removals were possible, but the strategy in 7a16b293f89a8d577e12562abf3eb0b847b0b723 is to sweep the entire chunk, so that we don't have to keep or update a complex todo data structure. So, one or the other should change.

kpreid commented 7 months ago

434fc90b9dfee95afd042af97c71b01cc4124acb makes instance-only updates work consistently.