4

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.

emh
  • 199
  • 10
  • did you make any progress with this? Helpful links? Things you tried that didn't work? Or anything else that would help someone who is about to attempt to tackle this problem? – Ryder Brooks Mar 26 '18 at 21:52
  • 1
    @RyderBrooks I went with a much simpler grid based collision approach, since my units are basically staying mostly on a 2D surface. The code can be found at https://github.com/emnh/rts/blob/master/src.client/game/client/engine2_physics.cljs . I used https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch29.html for inspiration. – emh Mar 28 '18 at 04:45
  • Are you planing that game to explicitly 2D or will be in 3D? Like depth will be part of gameplay? – Ilian Zapryanov Apr 08 '20 at 18:03
  • @IlianZapryanov Well it was 3D but on a surface (terrain) which is a 2D plane with heightmap embedded in 3D. – emh Apr 09 '20 at 19:29

0 Answers0