madmann91 / bvh

A modern C++ BVH construction and traversal library
MIT License
864 stars 83 forks source link

Ability to re-use BVH allocations? #75

Open heyx3 opened 3 months ago

heyx3 commented 3 months ago

Currently, if I'm reading the builders correctly, they always allocate a new bvh instance when building. It would be nice to have the option of re-using allocations.

madmann91 commented 3 months ago

It's unclear how to pass existing allocations/BVHs/buffers to the BVH builders. For instance, the mini tree builder needs to create mini-trees that require their own allocations of impredictable size, and other builders over-allocate in order to avoid allocating often when building the tree (they over-allocate, then shrink the resulting buffers). This design is very efficient and simple and I would like to keep it as it is.

While it is theoretically possible to pass an additional buffer for builders other than the mini-tree one, there's a good chance that it won't be large enough anyway (due to the over-allocation+shrinking design). That said, if your only concern is performance, you can just drop your old BVH right before building a new one with the same size, and the OS will likely re-use the blocks that were just freed.

If your concern is running this code in an embedded scenario, or some other application where allocations should be tightly controlled, the real solution here would be to use custom STL allocators (that would allow the builders to use a pool-based allocator, for instance). I am open to a PR tackling that problem, if you have the time.