Hello @JohnK ,
Wow that Morton Code BVH in .js is impressive! The BVH time was definitely faster than the Octree time in the Helmet demo. And the Sphere demo was really fast. Good work!
As to if you should implement this as the general Babylon.js ray tracing structure, or use something like I have, let me start off by admitting that of all the dozens of components necessary to implement ray tracing, the acceleration structure is my weakest area of knowledge. I have read (but mostly not understood) several research papers, some by companies like Nvidia, most from academia, about comparing different acceleration structure choices, or how they did their unique spin on the classic structures, like your Morton code example.
The good news for us that are using BVHs, is that most scholars and pro programmers alike seem to generally agree that the BVH comes out on top, over regular grids, over Octrees, and slightly over KD trees for ray tracing. This is evident in your Helmet example. Also, Nvidia’s RTX cards use only BVHs for their newest cutting edge demos. I’ve heard them mention it in a couple of technical presentations. In fact, if I’m not mistaken, they even have a dedicated processing unit on the cards that does nothing but BVH handling and traversal. So if they’re doing it, we can’t be too far off the mark! Ha
Unfortunately when trying to implement a BVH in the Nvidia manner, that structure building and traversal is proprietary as their hardware sales and ray tracing software rely on it. Therefore, when I have read an Nvidia paper, I feel like I’m not getting the whole picture, or the whole source code, in order for me to implement it myself.
Contrast that with a paper from academia (often as part of a thesis or dissertation) and you may get more source code, or Github link if you’re lucky, but I personally find it hard to wade through the formal, sometimes math-heavy text. Since it comes from an old tradition, I feel that the authors are conforming to a more formal academic research paper dynamic. Which doesn’t bode well for me, a non-CS, non-math degreed hobbyist coder!
Please take a look at my BVH js builder. I credit the original author at the top of the file. His Github BVH C++ code inspired me and kind of directed me in how to approach this complex topic. As for the GPU storage and traversal of this structure, I must confess that I had to reverse engineer some minimized fragment shader code inside the Antimatter WebGL path tracer upon inspection in the browser developer toolbar. This is because it was not open source or on Github anywhere. As ‘penance’ for borrowing like that, I added spaces and sensible variable and function names to the minified code manually (working with lines like int g = c(h, j); ) and worked through it line by line. Since then I have made it my own and optimized it and made it work for WebGL 2, so I don’t feel so bad for using copied code, ha.
So if you feel that the Morton codes would speed up building, by all means go with it! I must admit I really don’t understand Morton codes yet and how to implement something like that inside the BVH building. Having said that though, I feel like my current traditional BVH runs pretty fast on the GPU in the browser for simple scenes.
This reply is already too long, but in a later post I will give an overview of how the BVH building is done, how it is stored on the GPU, and how it is traversed on my current renderer. That way, anyone who is interested in diving deeper into that world can hopefully benefit from our discussion and comments. Who knows, maybe someone will help me improve it, or help me get it to work with large models on mobile (which is still an outstanding issue that I don’t fully know how to deal with)!
Thanks for contributing to this thread!