---
id: "sah-bvh-construction"
title: "SAH BVH construction"
aliases: [SAH BVH, surface area heuristic]
domain: "performance"
stage: "performance"
realtime: "offline"
platforms: [cpu, cuda, desktop-gpu, webgpu]
year_introduced: 1990
inputs: [mesh]
outputs: [graph]
style: "both"
dynamic: "static"
cost: "offline"
authority: "MacDonald & Booth, 1990"
url: "https://texelatlas.vercel.app/t/sah-bvh-construction/"
sources_verified_at: "2026-11-29"
---
# SAH BVH construction (MacDonald & Booth, 1990)

> Build a bounding volume hierarchy by choosing each split to minimise the expected ray traversal cost, estimated from child surface areas and primitive counts.

## 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

## 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 use it

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.

## Cost

offline — Build cost is higher than spatial-median builders; traversal gets cheaper.
Measured GPU Weight: 13.32× baseline (moderate) — 0.4181 ms median / 0.434 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 12.54× baseline (moderate) — 0.1981 ms median (2.11× faster on L4).
Ampere A100 scaling: 8.66× baseline (moderate) — 0.2495 ms median (1.68× faster on A100).

## Failure modes

- 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

## Engine support

- blender: builtin — `Cycles CPU/GPU acceleration structure builder uses Surface Area Heuristic (SAH) splitting in intern/cycles/bvh/build.cpp` (evidence: https://github.com/blender/blender/blob/main/intern/cycles/bvh/build.cpp, checked 2026-12-01)
- godot: builtin — `Internal dynamic BVH spatial acceleration structure (core/math/bvh_tree.h)` (evidence: https://github.com/godotengine/godot/blob/4.7.2-stable/core/math/bvh_tree.h, checked 2026-12-01)
- metal: builtin — `MTLPrimitiveAccelerationStructureDescriptor + MTLAccelerationStructureCommandEncoder.build(...)` (evidence: https://developer.apple.com/tutorials/data/documentation/metal/mtlprimitiveaccelerationstructuredescriptor.json, checked 2026-12-01)
- threejs: plugin — `npm i three-mesh-bvh; new MeshBVH(geometry, { strategy: SAH })` (evidence: https://github.com/gkjohnson/three-mesh-bvh/blob/master/API.md, checked 2026-12-01)
- unreal: builtin — `Project Settings > Rendering > Hardware Ray Tracing (builds GPU TLAS/BLAS acceleration structures for Lumen, Reflections, and Path Tracer)` (evidence: https://dev.epicgames.com/documentation/en-us/unreal-engine/hardware-ray-tracing-in-unreal-engine, checked 2026-12-01)

## Dataflow

- Inputs: mesh
- Outputs: graph
- Platforms: cpu, cuda, desktop-gpu, webgpu

## Presets

### SAH BVH construction — tight surface-area-heuristic bounding hierarchy & ray traversal heatmap (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_BVH_DEPTH": 3,
  "P_HEATMAP_MIX": 0.65,
  "P_RAY_SWEEP": 1,
  "P_SHOW_BOXES": 1
}
```

```glsl
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).

## Combines with

- [three-mesh-bvh](https://texelatlas.vercel.app/t/three-mesh-bvh.md): The library builds SAH or center trees and exposes raycasts and spatial queries.

## Sources

1. [primary] J. David MacDonald, Kellogg S. Booth (1990) Heuristics for ray tracing using space subdivision. The Visual Computer. https://doi.org/10.1007/bf01911006 (verified via crossref on 2026-11-29)
2. [implementation] gkjohnson (2018) gkjohnson/three-mesh-bvh. https://github.com/gkjohnson/three-mesh-bvh License: MIT. (verified via github on 2026-11-29)

## Cite this entry

Texel. "SAH BVH construction." https://texelatlas.vercel.app/t/sah-bvh-construction/ (accessed 2026-12-01).
