SAH BVH construction
Build a bounding volume hierarchy by choosing each split to minimise the expected ray traversal cost, estimated from child surface areas and primitive counts.
Also known as SAH BVH, surface area heuristic
In the family Performance, LOD & compression, acceleration structures
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | Plugin | npm i three-mesh-bvh; new MeshBVH(geometry, { strategy: SAH }) docs |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | Built in | Internal dynamic BVH spatial acceleration structure (core/math/bvh_tree.h) docs |
| Unity | None found | |
| Unreal 5.7 | Built in | Project Settings > Rendering > Hardware Ray Tracing (builds GPU TLAS/BLAS acceleration structures for Lumen, Reflections, and Path Tracer) docs |
| Blender 5.2 | Built in | Cycles CPU/GPU acceleration structure builder uses Surface Area Heuristic (SAH) splitting in intern/cycles/bvh/build.cpp docs |
| WebGPU W3C | None found | |
| Metal | Built in | MTLPrimitiveAccelerationStructureDescriptor + MTLAccelerationStructureCommandEncoder.build(...) docs |
How it works
A ray hits a box with probability proportional to its surface area. For a candidate split, cost is traversal overhead plus sum over children of area times primitive count, normalised by parent area. The builder tests candidate planes, often binned along each axis, and picks the lowest cost, or makes a leaf if splitting is worse.
Binned SAH avoids sorting by using fixed buckets; sweeps give exact splits. The tree gives faster rays than median or object splits, at higher build cost.
When to choose it
Reach for it when
- ray tracing is slow because of poor acceleration structure
- need fastest traversal for a static scene
- bvh quality is bad from simple splits
- reduce ray-box tests per ray
- build a high-quality tree once for a fixed model
Choose it for static scenes where tracing speed matters. Prefer LBVH or Morton-code builders for fast per-frame rebuilds of dynamic scenes, then refit. Prefer refit alone for small deformations.
Field marks
- Slow builds for millions of primitives
- Overlapping boxes from large triangles make SAH ineffective without splitting
- Cost constants assumed by SAH rarely match the real hardware
- Refit degrades quality as geometry deforms
Presets
SAH BVH construction — tight surface-area-heuristic bounding hierarchy & ray traversal heatmap
{
"P_BVH_DEPTH": 3,
"P_HEATMAP_MIX": 0.65,
"P_RAY_SWEEP": 1,
"P_SHOW_BOXES": 1
}Show GLSL (193 lines)
const float P_BVH_DEPTH = 3.0; // P BVH Depth [1.0, 4.0, 1.0]
const float P_HEATMAP_MIX = 0.65; // P Step Heatmap [0.0, 1.2, 0.04]
const float P_RAY_SWEEP = 1.0; // P Ray Sweep [0.0, 2.5, 0.05]
const float P_SHOW_BOXES = 1.0; // P AABB Boxes [0.0, 1.5, 0.05]
float sdBox2(vec2 p, vec2 bMin, vec2 bMax) {
vec2 c = 0.5 * (bMin + bMax);
vec2 h = 0.5 * (bMax - bMin);
vec2 q = abs(p - c) - h;
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
}
// 2D ray-AABB slab intersection returning vec2(tNear, tFar)
vec2 intersectBox2(vec2 ro, vec2 rd, vec2 bMin, vec2 bMax) {
vec2 invD = 1.0 / (rd + sign(rd + 1e-6) * 1e-5);
vec2 t0 = (bMin - ro) * invD;
vec2 t1 = (bMax - ro) * invD;
vec2 tmin = min(t0, t1);
vec2 tmax = max(t0, t1);
return vec2(max(tmin.x, tmin.y), min(tmax.x, tmax.y));
}
float sdSegment(vec2 p, vec2 a, vec2 b) {
vec2 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-5), 0.0, 1.0);
return length(pa - ba * h);
}
vec3 heatPalette(float x) {
x = clamp(x, 0.0, 1.0);
vec3 c0 = vec3(0.04, 0.07, 0.16);
vec3 c1 = vec3(0.08, 0.44, 0.68);
vec3 c2 = vec3(0.95, 0.62, 0.14);
vec3 c3 = vec3(1.00, 0.96, 0.72);
if (x < 0.35) return mix(c0, c1, x / 0.35);
if (x < 0.72) return mix(c1, c2, (x - 0.35) / 0.37);
return mix(c2, c3, (x - 0.72) / 0.28);
}
vec3 aces(vec3 x) {
x = max(x, 0.0);
return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
float px = 1.0 / max(iResolution.y, 1.0);
float tAnim = iTime * max(P_RAY_SWEEP, 0.0);
float maxDepth = floor(clamp(P_BVH_DEPTH, 1.0, 4.0));
float heatMix = clamp(P_HEATMAP_MIX, 0.0, 1.3);
float boxVis = clamp(P_SHOW_BOXES, 0.0, 1.6);
// Define 8 clustered scene discs grouped into 4 tight SAH leaf bounds, 2 mid bounds, 1 root bound
vec2 cPos[8];
float cRad[8];
cPos[0] = vec2(-0.56, 0.22); cRad[0] = 0.055;
cPos[1] = vec2(-0.42, 0.16); cRad[1] = 0.048;
cPos[2] = vec2(-0.46, -0.22); cRad[2] = 0.060;
cPos[3] = vec2(-0.26, -0.28); cRad[3] = 0.046;
cPos[4] = vec2(0.24, 0.28); cRad[4] = 0.052;
cPos[5] = vec2(0.40, 0.18); cRad[5] = 0.058;
cPos[6] = vec2(0.32, -0.14); cRad[6] = 0.050;
cPos[7] = vec2(0.56, -0.26); cRad[7] = 0.062;
// Tight SAH-partitioned AABBs (minimizing perimeter*count = Surface Area Heuristic!)
vec2 leafMin[4]; vec2 leafMax[4];
for (int k = 0; k < 4; k++) {
vec2 a = cPos[k * 2]; float ra = cRad[k * 2];
vec2 b = cPos[k * 2 + 1]; float rb = cRad[k * 2 + 1];
leafMin[k] = min(a - vec2(ra), b - vec2(rb)) - vec2(0.015);
leafMax[k] = max(a + vec2(ra), b + vec2(rb)) + vec2(0.015);
}
vec2 midMin[2]; vec2 midMax[2];
midMin[0] = min(leafMin[0], leafMin[1]) - vec2(0.018);
midMax[0] = max(leafMax[0], leafMax[1]) + vec2(0.018);
midMin[1] = min(leafMin[2], leafMin[3]) - vec2(0.018);
midMax[1] = max(leafMax[2], leafMax[3]) + vec2(0.018);
vec2 rootMin = min(midMin[0], midMin[1]) - vec2(0.022);
vec2 rootMax = max(midMax[0], midMax[1]) + vec2(0.022);
// Moving probe ray origin sweeping across the BVH from the left edge
vec2 rayOrig = vec2(-0.82, 0.28 * sin(tAnim * 0.55));
float sweepAng = 0.36 * sin(tAnim * 0.85);
vec2 rayDir = vec2(cos(sweepAng), sin(sweepAng));
// Evaluate pixel-space BVH traversal cost heatmap (number of AABBs containing or tested near pixel)
float cost = 0.0;
if (sdBox2(uv, rootMin, rootMax) < 0.0) cost += 1.0;
if (maxDepth >= 2.0) {
for (int m = 0; m < 2; m++) {
if (sdBox2(uv, midMin[m], midMax[m]) < 0.0) cost += 1.5;
}
}
if (maxDepth >= 3.0) {
for (int l = 0; l < 4; l++) {
if (sdBox2(uv, leafMin[l], leafMax[l]) < 0.0) cost += 2.2;
}
}
if (maxDepth >= 4.0) {
for (int i = 0; i < 8; i++) {
if (sdBox2(uv, cPos[i] - vec2(cRad[i]), cPos[i] + vec2(cRad[i])) < 0.0) cost += 2.6;
}
}
// Dark blueprint backdrop + traversal heatmap tint
vec3 bg = vec3(0.032, 0.044, 0.068);
vec2 grid = abs(fract(uv * 12.0 - 0.5) - 0.5);
bg += vec3(0.02, 0.035, 0.055) * smoothstep(0.04, 0.005, min(grid.x, grid.y));
vec3 col = mix(bg, heatPalette(cost / 8.0), 0.58 * heatMix);
// Test sweeping ray against BVH nodes to highlight traversed vs pruned subtrees
vec2 hRoot = intersectBox2(rayOrig, rayDir, rootMin, rootMax);
bool hitRoot = hRoot.y > max(hRoot.x, 0.0);
// Draw Root AABB (Depth 1)
if (boxVis > 0.01) {
float dR = abs(sdBox2(uv, rootMin, rootMax));
vec3 rCol = hitRoot ? vec3(0.45, 0.72, 1.0) : vec3(0.24, 0.30, 0.42);
col = mix(col, rCol, smoothstep(px * 3.0, px * 0.8, dR) * 0.85 * min(boxVis, 1.2));
}
// Depth 2: Mid-level SAH split boxes
bool hitMid[2];
for (int m = 0; m < 2; m++) {
vec2 hm = intersectBox2(rayOrig, rayDir, midMin[m], midMax[m]);
hitMid[m] = hitRoot && hm.y > max(hm.x, 0.0);
if (maxDepth >= 2.0 && boxVis > 0.01) {
float dm = abs(sdBox2(uv, midMin[m], midMax[m]));
vec3 mCol = hitMid[m] ? vec3(0.22, 0.94, 0.74) : vec3(0.22, 0.34, 0.45);
col = mix(col, mCol, smoothstep(px * 2.8, px * 0.7, dm) * (hitMid[m] ? 0.95 : 0.45) * min(boxVis, 1.2));
}
}
// Depth 3: Tight SAH Leaf boxes
bool hitLeaf[4];
for (int l = 0; l < 4; l++) {
int parent = l / 2;
vec2 hl = intersectBox2(rayOrig, rayDir, leafMin[l], leafMax[l]);
hitLeaf[l] = hitMid[parent] && hl.y > max(hl.x, 0.0);
if (maxDepth >= 3.0 && boxVis > 0.01) {
float dl = abs(sdBox2(uv, leafMin[l], leafMax[l]));
vec3 lCol = hitLeaf[l] ? vec3(1.0, 0.78, 0.22) : vec3(0.26, 0.32, 0.40);
col = mix(col, lCol, smoothstep(px * 2.6, px * 0.6, dl) * (hitLeaf[l] ? 0.98 : 0.42) * min(boxVis, 1.2));
}
}
// Ray-disc closest hit along the active sweeping ray
float tClosest = 2.2;
int hitPrim = -1;
for (int i = 0; i < 8; i++) {
int leaf = i / 2;
if (!hitLeaf[leaf]) continue;
vec2 oc = rayOrig - cPos[i];
float b = dot(oc, rayDir);
float c = dot(oc, oc) - cRad[i] * cRad[i];
float disc = b * b - c;
if (disc > 0.0) {
float th = -b - sqrt(disc);
if (th > 0.0 && th < tClosest) {
tClosest = th;
hitPrim = i;
}
}
}
// Draw primitives (highlighted if tested/hit, muted if pruned by BVH)
for (int i = 0; i < 8; i++) {
int leaf = i / 2;
float dC = length(uv - cPos[i]) - cRad[i];
if (maxDepth >= 4.0 && boxVis > 0.01) {
float dpb = abs(sdBox2(uv, cPos[i] - vec2(cRad[i]), cPos[i] + vec2(cRad[i])));
col = mix(col, vec3(0.95, 0.42, 0.32), smoothstep(px * 2.0, px * 0.5, dpb) * (hitLeaf[leaf] ? 0.85 : 0.25) * min(boxVis, 1.2));
}
vec3 pCol = (i == hitPrim)
? vec3(1.0, 0.94, 0.52)
: (hitLeaf[leaf] ? vec3(0.92, 0.52, 0.22) : vec3(0.28, 0.38, 0.52));
float fill = smoothstep(px * 1.5, -px * 1.5, dC);
float rim = smoothstep(px * 3.0, px * 0.5, abs(dC));
col = mix(col, pCol * (0.75 + 0.35 * smoothstep(cRad[i], -cRad[i], uv.y - cPos[i].y)), fill);
col = mix(col, pCol * 1.35, rim * 0.8);
}
// Draw sweeping probe ray & intersection spark
vec2 rayEnd = rayOrig + rayDir * tClosest;
float dRay = sdSegment(uv, rayOrig, rayEnd);
col += vec3(0.25, 0.92, 1.0) * smoothstep(px * 5.0, px * 0.7, dRay) * 0.85;
col += vec3(1.0, 0.98, 0.85) * smoothstep(0.022, 0.004, length(uv - rayOrig));
if (hitPrim >= 0) {
col += vec3(1.0, 0.88, 0.38) * exp(-length(uv - rayEnd) * 32.0) * 1.4;
}
fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| sah-bvh-construction-live | Tesla T4 1920x1080 | 13.3× baseline (moderate) · 0.418 ms / 0.434 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| sah-bvh-construction-live | NVIDIA L4 1920x1080 | 12.5× baseline (moderate) · 0.198 ms / 0.205 ms · 2.1× faster on L4 | opengl-timer-query · Cross-architecture validation |
| sah-bvh-construction-live | NVIDIA A100-SXM4-40GB 1920x1080 | 8.66× baseline (moderate) · 0.249 ms / 0.264 ms · 1.7× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
From the citation graph (Crossref reference lists): cited by GPU hardware occlusion queries, Multiple-Scattering Approximation by Octaves.
Crosses
- three-mesh-bvh: The library builds SAH or center trees and exposes raycasts and spatial queries.
Sources
- primaryJ. David MacDonald, Kellogg S. Booth (1990). Heuristics for ray tracing using space subdivision. The Visual Computer.Verified via crossref · 2026-11-29doi:10.1007/bf01911006
- implementationgkjohnson (2018). gkjohnson/three-mesh-bvh. A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.GitHub API · license MIT · 2026-11-29