#2013·warp

Grouped CUDA LBVH can exceed the query stack capacity with 33 AABBs

Author: shi-ericCreated Sep 30, 2026Updated Sep 30, 2026
Labelsbugspatial acceleration

Bug Description

A grouped BVH built on CUDA with constructor="lbvh" and leaf_size=1 can exceed the query stack's capacity with just 33 identical AABBs, each assigned to a different group.

The example below builds a tree with reachable depth 32. A host simulation of a query covering all bounds reaches 33 pending stack entries, exceeding the 32-entry query stack. The 32-AABB control reaches depth 31 and needs 32 entries.

Expected: A full-range AABB query can return all 33 primitives exactly once within the supported stack capacity.

Observed: The constructed tree requires 33 stack entries for that traversal. The reproducer copies the device tree to the host and measures its depth and traversal stack usage; it does not launch a query on the affected tree.

python
"""Inspect grouped CUDA LBVH depth and stack usage without querying the tree."""

import ctypes

import numpy as np
import warp as wp


class Header(ctypes.Structure):
    # Prefix of the native BVH descriptor, through its root pointer.
    _fields_ = [
        ("node_lowers", ctypes.c_void_p),
        ("node_uppers", ctypes.c_void_p),
        ("node_parents", ctypes.c_void_p),
        ("node_counts", ctypes.c_void_p),
        ("primitive_indices", ctypes.c_void_p),
        ("max_depth", ctypes.c_int),
        ("max_nodes", ctypes.c_int),
        ("num_nodes", ctypes.c_int),
        ("num_leaf_nodes", ctypes.c_int),
        ("root", ctypes.c_void_p),
    ]


def read_device(ptr, count, dtype):
    return wp.array(ptr=int(ptr), shape=count, dtype=dtype, device="cuda:0", copy=False).numpy()


def inspect_tree(bvh):
    raw = read_device(bvh.id, ctypes.sizeof(Header), wp.uint8)
    header = Header.from_buffer_copy(raw.tobytes())
    root = int(read_device(header.root, 1, wp.int32)[0])
    lowers = read_device(header.node_lowers, header.max_nodes * 4, wp.uint32).reshape(-1, 4)[:, 3]
    uppers = read_device(header.node_uppers, header.max_nodes * 4, wp.uint32).reshape(-1, 4)[:, 3]

    pending = [(root, 0)]
    seen = set()
    max_depth = 0
    stack_peak = 1
    while pending:
        node, depth = pending.pop()
        assert 0 <= node < header.max_nodes and node not in seen
        seen.add(node)
        if int(lowers[node]) >> 31:
            max_depth = max(max_depth, depth)
        else:
            # Simulate an all-overlap traversal, pushing left and then right.
            pending.append((int(lowers[node]) & 0x7FFFFFFF, depth + 1))
            pending.append((int(uppers[node]) & 0x7FFFFFFF, depth + 1))
            stack_peak = max(stack_peak, len(pending))
    return max_depth, stack_peak


for count in (32, 33):
    keys = np.array([0] + [1 << i for i in range(count - 1)], dtype=np.uint32)
    groups = np.bitwise_not(keys).view(np.int32)
    bvh = wp.Bvh(
        wp.zeros(count, dtype=wp.vec3, device="cuda:0"),
        wp.array(np.ones((count, 3), dtype=np.float32), dtype=wp.vec3, device="cuda:0"),
        groups=wp.array(groups, dtype=wp.int32, device="cuda:0"),
        constructor="lbvh",
        leaf_size=1,
    )
    depth, peak = inspect_tree(bvh)
    print(f"{count} AABBs: reachable depth={depth}, simulated stack peak={peak}")

Output:

32 AABBs: reachable depth=31, simulated stack peak=32
33 AABBs: reachable depth=32, simulated stack peak=33

Reproduction is confirmed at 44bd2379d. For historical context, 7c6aa975ea addressed overly deep BVHs in March 2025, and 0e08171333 added grouped BVH support in November 2025. The earliest affected commit has not been established.

System Information

  • Warp: 1.19.0.dev0, standard native build with CUDA enabled
  • OS: Ubuntu 22.04.5 LTS
  • Python: 3.12.13
  • NumPy: 2.5.3
  • GPU: NVIDIA RTX PRO 6000 Blackwell Server Edition, MIG 1g.24gb, sm_120
  • NVIDIA driver: 595.58.03
  • CUDA Toolkit: 12.9, NVCC 12.9.86