這次的範例主要示範如何建構以及在 compute shader 中使用 acceleration structure來加速 ray 與幾何場景的碰撞偵測。

Acceleration Structure

在ray tracing中,一般會使用acceleration structure來加速ray與幾何場景的碰撞偵測。acceleration structure其實就是空間分割樹,藉由切割空間以及階層樹的幫助,來快速篩選出可能跟ray碰撞的幾何物件。這次的範例選用最常見的BVH來當作示範,關於BVH的說明可以參考Wiki

Bounding volume hierarchy - Wikipedia

Bounding Volume Hierarchy

這次的範例使用了MagicaVoxel ( http://ephtracy.github.io/ )的模型來測試,下圖的場景總共有21129個voxel,每個fragment使用三條ray的情況下(一條是成像,另外兩條是計算陰影),測試使用BVH以及不使用BVH的效能差異。可以看到使用BVH時可以達到約200fps,而完全不使用時則只有5fps。

With BVH

230 fps (3 Rays per fragment, 1 ray for rendering, 2 rays for shadowing, 21129 voxels)

230 fps (3 Rays per fragment, 1 ray for rendering, 2 rays for shadowing, 21129 voxels)

Without BVH

5 fps (3 Rays per fragment, 1 ray for rendering, 2 rays for shadowing, 21129 voxels)

5 fps (3 Rays per fragment, 1 ray for rendering, 2 rays for shadowing, 21129 voxels)

Github Page

fallingcat/ComputeRayTracingSamples