Is it feasible to adapt the approach in this paper from CUDA to WebGL 2 shaders and still be efficient?
- (1) Assign a Morton code for each primitive according to its centroid.
- Should be easy, as bitwise operations (AND, OR, etc) are available in WebGL 2 shaders.
- (2) Sort the Morton codes.
- Do you think a bucket / radix sort based on Histopyramids would do the trick, most significant bit first? This would replace the parallel radix sort from the paper. Are there are any other applicable sorting methods for WebGL 2? Bitonic merge sort (it seems more complicated to implement)?
- (3) Construct a binary radix tree.
- Would the Histopyramid bucket / radix sort in step 2 be able to also implicitly construct the tree? Or is another step required? E.g. for traversal pointers (children? parent? hit and miss pointers?).
- (4) Assign a bounding box for each internal node.
- Haven't thought too much about this one yet.. I hope doing it bottom-up with one shader invocation per level is fast enough.
Has it been done before? I could not find it. Any relevant links are appreciated. The closest I've found is Space Partitioning - Kd-tree - Using WebGL. It constructs the tree in JavaScript, but it does show how to do stackless tree traversal in a WebGL Shader. If it turns out to be too difficult to construct a BVH using shaders I might get away with constructing it in JavaScript, as the KD-tree demo uses "only" 2ms on 4096 elements on my machine. It would be cool to scale higher though.
My goal is to do collision detection on the GPU for an RTS game, scaling to as many units and projectiles as possible. All elements are assumed to be spherical. The BVH will be reconstructed real-time each frame (which leaves 16ms at 60FPS, not accounting for other tasks).There are three tasks I can think of currently:
- Unit overlap dispersion.
- Target selection (nearest neighbour, as units with long attack range might have too many matches).
- Projectile damage application.
Perhaps there are more clever tricks or simplifications than using BVH. Suggestions welcome.