Skip to content

BVH 加速结构

Bounding Volume Hierarchy (BVH) 是光线追踪中最重要的加速结构之一。

概述

BVH 通过将场景中的物体组织成层次包围盒树,将光线求交的时间复杂度从 O(N) 降低到 O(log N)。

SAH 分割策略

Surface Area Heuristic (SAH) 是选择最优分割位置的代价函数:

$$C = C_{trav} + \frac{SA(L)}{SA(N)} \cdot |L| \cdot C_{isect} + \frac{SA(R)}{SA(N)} \cdot |R| \cdot C_{isect}$$

其中:

  • $SA$ 表示包围盒表面积
  • $|L|$, $|R|$ 分别是左右子树的图元数量
  • $C_{trav}$ 是遍历节点代价
  • $C_{isect}$ 是求交测试代价

构建算法

BVH 构建 (SAH)

输入: 图元集合 P 输出: BVH 根节点

  1. if |P| ≤ 叶子阈值 then
  2. return 创建叶子节点(P)
  3. end if
  4. 选择分割轴 (x/y/z) 使 SAH 代价最小
  5. 将 P 分割为 P_left, P_right
  6. node.left ← BVH-Build(P_left)
  7. node.right ← BVH-Build(P_right)
  8. return node

遍历算法

BVH 遍历 (栈式)

输入: 光线 R, BVH 根节点 root 输出: 最近相交点 hit

  1. stack.push(root)
  2. while stack 非空 do
  3. node ← stack.pop()
  4. if R 与 node.aabb 不相交 then continue
  5. if node 是叶子 then
  6. 测试 R 与 node 中所有图元
    
  7. else
  8. stack.push(node.left)
    
  9. stack.push(node.right)
    
  10. end if
  11. end while

性能分析

场景球体数暴力求交BVH 求交加速比
Demo101042.5×
Random100100714×
Complex1000100010100×

代码实现

cpp
// BVH 节点结构
struct BVHNode {
    AABB bounds;
    int left;    // 左子节点索引(负数表示叶子)
    int right;   // 右子节点索引
    int start;   // 叶子节点:图元起始索引
    int count;   // 叶子节点:图元数量
};

// BVH 遍历 kernel
__device__ bool traverse_bvh(
    const Ray& ray,
    const BVHNode* nodes,
    const Sphere* spheres,
    HitRecord& closest_hit
) {
    int stack[64];
    int stack_ptr = 0;
    stack[stack_ptr++] = 0;  // 根节点

    while (stack_ptr > 0) {
        int node_idx = stack[--stack_ptr];
        const BVHNode& node = nodes[node_idx];

        if (!intersect_aabb(ray, node.bounds)) continue;

        if (node.left < 0) {  // 叶子节点
            for (int i = 0; i < node.count; ++i) {
                // 测试图元求交
            }
        } else {
            stack[stack_ptr++] = node.left;
            stack[stack_ptr++] = node.right;
        }
    }
    return closest_hit.valid;
}

参考资料

  • [Wald 2007] "On fast Construction of SAH-based BVH"
  • [Aila & Karras 2010] "Understanding Ray Traversal Efficiency on GPUs"

Technical Whitepaper · Built with VitePress