madmann91 / bvh

A modern C++ BVH construction and traversal library
MIT License
936 stars 88 forks source link

Cancel build +progress monitor #67

Open Silverlan opened 1 year ago

Silverlan commented 1 year ago

Embree has a progress monitor for build operations, that also allows you to cancel the build operation: https://github.com/embree/embree/blob/master/README.md#description-description-21

It would be great to have something like this for this library as well. The ability to cancel in particular can be very handy for large scenes where building the bvh can take a very long time.

madmann91 commented 12 months ago

It should be possible to implement it, and by the way feel free to submit a PR for review. The issue though is that this requires some sort of knowledge of the build algorithm, in the sense that the notion of "progress" is really not going to be super accurate (it's -- for the most part -- a recursive process and you may not know the cost of building the subtree until you actually do it).

That said, a reasonable assumption that the cost of a subtree of size N is somewhat proportional to N log(N) might make sense. There's still the problem that the mini-tree builder runs in parallel so you have to take care of sending the progress information only once (or at least send it in an ordered fashion somehow). Maybe an atomic number is enough, so that you can start building the BVH, then check the progress at regular intervals by looking at the value held by that atomic.

So all in all it's a pretty complicated implementation for a small feature that, although nice to have, is not strictly necessary. I'll keep that in mind when doing further development work on the library though, if I can find the time for it.

Silverlan commented 12 months ago

Thanks for the response! Personally I'd be more interested in the ability to cancel anyway, the progress callback would just be nice-to-have. In my case I have scenes where building the BVH can take several minutes and if the user closes the program during that, they currently have to wait until the BVH building is complete.

I haven't looked too closely at the source code for bvh, but I'll try and see if I can whip up a PR for that in the near future.

madmann91 commented 11 months ago

For this specific use case, I don't think you need the ability to cancel the build per se. I would imagine that through OS services you can send a kill signal to the process that builds the BVH, without having to wait for the threads to join. If you want to kill your own process on Linux, use kill(getpid(), sig) or raise(sig) (where sig is the signal to send, SIGTERM or SIGKILL).

madmann91 commented 11 months ago

If you don't want to kill the current process but want to continue executing the program, you could use a subprocess to build the BVH as a work around. This would allow you to kill it before it terminates. The downside is that you'll have to communicate via files or pipes. The serialization API allows you to do that, by sending a stream of bytes over, instead of writing to a file on disk.