{
 "id": "sah-bvh-construction",
 "title": "SAH BVH construction",
 "aliases": [
  "SAH BVH",
  "surface area heuristic"
 ],
 "domain": "performance",
 "subdomain": "acceleration structures",
 "stage": "performance",
 "realtime": "offline",
 "platforms": [
  "cpu",
  "cuda",
  "desktop-gpu",
  "webgpu"
 ],
 "year_introduced": 1990,
 "inputs": [
  "mesh"
 ],
 "outputs": [
  "graph"
 ],
 "style": "both",
 "dynamic": "static",
 "summary": "Build a bounding volume hierarchy by choosing each split to minimise the expected ray traversal cost, estimated from child surface areas and primitive counts.",
 "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.\n\nBinned 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": "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": {
  "class": "offline",
  "notes": "Build cost is higher than spatial-median builders; traversal gets cheaper."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1007/bf01911006",
   "type": "paper",
   "title": "Heuristics for ray tracing using space subdivision",
   "authors": [
    "J. David MacDonald",
    "Kellogg S. Booth"
   ],
   "year": 1990,
   "venue": "The Visual Computer",
   "url": "https://doi.org/10.1007/bf01911006",
   "ids": {
    "doi": "10.1007/bf01911006"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/bf01911006",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/bf01911006",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/bf01911006",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/bf01911006",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/bf01911006",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "macdonald1990heuristics"
  },
  {
   "role": "implementation",
   "key": "gh:gkjohnson/three-mesh-bvh",
   "type": "code",
   "title": "gkjohnson/three-mesh-bvh",
   "description": "A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.",
   "authors": [
    "gkjohnson"
   ],
   "year": 2018,
   "venue": "GitHub",
   "url": "https://github.com/gkjohnson/three-mesh-bvh",
   "ids": {
    "repo": "gkjohnson/three-mesh-bvh"
   },
   "license": "MIT",
   "extra": {
    "stars": 3601,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-28T18:11:52Z",
    "language": "JavaScript",
    "homepage": "https://gkjohnson.github.io/three-mesh-bvh/raycast.html",
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "gkjohnson2018gkjohnson"
  }
 ],
 "presets": [
  {
   "id": "sah-bvh-construction-live",
   "title": "SAH BVH construction — tight surface-area-heuristic bounding hierarchy & ray traversal heatmap",
   "runtime": "glsl-fragment",
   "params": {
    "P_BVH_DEPTH": 3,
    "P_HEATMAP_MIX": 0.65,
    "P_RAY_SWEEP": 1,
    "P_SHOW_BOXES": 1
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_BVH_DEPTH = 3.0; // P BVH Depth [1.0, 4.0, 1.0]\nconst float P_HEATMAP_MIX = 0.65; // P Step Heatmap [0.0, 1.2, 0.04]\nconst float P_RAY_SWEEP = 1.0; // P Ray Sweep [0.0, 2.5, 0.05]\nconst float P_SHOW_BOXES = 1.0; // P AABB Boxes [0.0, 1.5, 0.05]\n\nfloat sdBox2(vec2 p, vec2 bMin, vec2 bMax) {\n  vec2 c = 0.5 * (bMin + bMax);\n  vec2 h = 0.5 * (bMax - bMin);\n  vec2 q = abs(p - c) - h;\n  return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);\n}\n\n// 2D ray-AABB slab intersection returning vec2(tNear, tFar)\nvec2 intersectBox2(vec2 ro, vec2 rd, vec2 bMin, vec2 bMax) {\n  vec2 invD = 1.0 / (rd + sign(rd + 1e-6) * 1e-5);\n  vec2 t0 = (bMin - ro) * invD;\n  vec2 t1 = (bMax - ro) * invD;\n  vec2 tmin = min(t0, t1);\n  vec2 tmax = max(t0, t1);\n  return vec2(max(tmin.x, tmin.y), min(tmax.x, tmax.y));\n}\n\nfloat sdSegment(vec2 p, vec2 a, vec2 b) {\n  vec2 pa = p - a, ba = b - a;\n  float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-5), 0.0, 1.0);\n  return length(pa - ba * h);\n}\n\nvec3 heatPalette(float x) {\n  x = clamp(x, 0.0, 1.0);\n  vec3 c0 = vec3(0.04, 0.07, 0.16);\n  vec3 c1 = vec3(0.08, 0.44, 0.68);\n  vec3 c2 = vec3(0.95, 0.62, 0.14);\n  vec3 c3 = vec3(1.00, 0.96, 0.72);\n  if (x < 0.35) return mix(c0, c1, x / 0.35);\n  if (x < 0.72) return mix(c1, c2, (x - 0.35) / 0.37);\n  return mix(c2, c3, (x - 0.72) / 0.28);\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float px = 1.0 / max(iResolution.y, 1.0);\n  float tAnim = iTime * max(P_RAY_SWEEP, 0.0);\n  float maxDepth = floor(clamp(P_BVH_DEPTH, 1.0, 4.0));\n  float heatMix = clamp(P_HEATMAP_MIX, 0.0, 1.3);\n  float boxVis = clamp(P_SHOW_BOXES, 0.0, 1.6);\n\n  // Define 8 clustered scene discs grouped into 4 tight SAH leaf bounds, 2 mid bounds, 1 root bound\n  vec2 cPos[8];\n  float cRad[8];\n  cPos[0] = vec2(-0.56, 0.22); cRad[0] = 0.055;\n  cPos[1] = vec2(-0.42, 0.16); cRad[1] = 0.048;\n  cPos[2] = vec2(-0.46, -0.22); cRad[2] = 0.060;\n  cPos[3] = vec2(-0.26, -0.28); cRad[3] = 0.046;\n  cPos[4] = vec2(0.24, 0.28); cRad[4] = 0.052;\n  cPos[5] = vec2(0.40, 0.18); cRad[5] = 0.058;\n  cPos[6] = vec2(0.32, -0.14); cRad[6] = 0.050;\n  cPos[7] = vec2(0.56, -0.26); cRad[7] = 0.062;\n\n  // Tight SAH-partitioned AABBs (minimizing perimeter*count = Surface Area Heuristic!)\n  vec2 leafMin[4]; vec2 leafMax[4];\n  for (int k = 0; k < 4; k++) {\n    vec2 a = cPos[k * 2]; float ra = cRad[k * 2];\n    vec2 b = cPos[k * 2 + 1]; float rb = cRad[k * 2 + 1];\n    leafMin[k] = min(a - vec2(ra), b - vec2(rb)) - vec2(0.015);\n    leafMax[k] = max(a + vec2(ra), b + vec2(rb)) + vec2(0.015);\n  }\n  vec2 midMin[2]; vec2 midMax[2];\n  midMin[0] = min(leafMin[0], leafMin[1]) - vec2(0.018);\n  midMax[0] = max(leafMax[0], leafMax[1]) + vec2(0.018);\n  midMin[1] = min(leafMin[2], leafMin[3]) - vec2(0.018);\n  midMax[1] = max(leafMax[2], leafMax[3]) + vec2(0.018);\n  vec2 rootMin = min(midMin[0], midMin[1]) - vec2(0.022);\n  vec2 rootMax = max(midMax[0], midMax[1]) + vec2(0.022);\n\n  // Moving probe ray origin sweeping across the BVH from the left edge\n  vec2 rayOrig = vec2(-0.82, 0.28 * sin(tAnim * 0.55));\n  float sweepAng = 0.36 * sin(tAnim * 0.85);\n  vec2 rayDir = vec2(cos(sweepAng), sin(sweepAng));\n\n  // Evaluate pixel-space BVH traversal cost heatmap (number of AABBs containing or tested near pixel)\n  float cost = 0.0;\n  if (sdBox2(uv, rootMin, rootMax) < 0.0) cost += 1.0;\n  if (maxDepth >= 2.0) {\n    for (int m = 0; m < 2; m++) {\n      if (sdBox2(uv, midMin[m], midMax[m]) < 0.0) cost += 1.5;\n    }\n  }\n  if (maxDepth >= 3.0) {\n    for (int l = 0; l < 4; l++) {\n      if (sdBox2(uv, leafMin[l], leafMax[l]) < 0.0) cost += 2.2;\n    }\n  }\n  if (maxDepth >= 4.0) {\n    for (int i = 0; i < 8; i++) {\n      if (sdBox2(uv, cPos[i] - vec2(cRad[i]), cPos[i] + vec2(cRad[i])) < 0.0) cost += 2.6;\n    }\n  }\n\n  // Dark blueprint backdrop + traversal heatmap tint\n  vec3 bg = vec3(0.032, 0.044, 0.068);\n  vec2 grid = abs(fract(uv * 12.0 - 0.5) - 0.5);\n  bg += vec3(0.02, 0.035, 0.055) * smoothstep(0.04, 0.005, min(grid.x, grid.y));\n  vec3 col = mix(bg, heatPalette(cost / 8.0), 0.58 * heatMix);\n\n  // Test sweeping ray against BVH nodes to highlight traversed vs pruned subtrees\n  vec2 hRoot = intersectBox2(rayOrig, rayDir, rootMin, rootMax);\n  bool hitRoot = hRoot.y > max(hRoot.x, 0.0);\n\n  // Draw Root AABB (Depth 1)\n  if (boxVis > 0.01) {\n    float dR = abs(sdBox2(uv, rootMin, rootMax));\n    vec3 rCol = hitRoot ? vec3(0.45, 0.72, 1.0) : vec3(0.24, 0.30, 0.42);\n    col = mix(col, rCol, smoothstep(px * 3.0, px * 0.8, dR) * 0.85 * min(boxVis, 1.2));\n  }\n\n  // Depth 2: Mid-level SAH split boxes\n  bool hitMid[2];\n  for (int m = 0; m < 2; m++) {\n    vec2 hm = intersectBox2(rayOrig, rayDir, midMin[m], midMax[m]);\n    hitMid[m] = hitRoot && hm.y > max(hm.x, 0.0);\n    if (maxDepth >= 2.0 && boxVis > 0.01) {\n      float dm = abs(sdBox2(uv, midMin[m], midMax[m]));\n      vec3 mCol = hitMid[m] ? vec3(0.22, 0.94, 0.74) : vec3(0.22, 0.34, 0.45);\n      col = mix(col, mCol, smoothstep(px * 2.8, px * 0.7, dm) * (hitMid[m] ? 0.95 : 0.45) * min(boxVis, 1.2));\n    }\n  }\n\n  // Depth 3: Tight SAH Leaf boxes\n  bool hitLeaf[4];\n  for (int l = 0; l < 4; l++) {\n    int parent = l / 2;\n    vec2 hl = intersectBox2(rayOrig, rayDir, leafMin[l], leafMax[l]);\n    hitLeaf[l] = hitMid[parent] && hl.y > max(hl.x, 0.0);\n    if (maxDepth >= 3.0 && boxVis > 0.01) {\n      float dl = abs(sdBox2(uv, leafMin[l], leafMax[l]));\n      vec3 lCol = hitLeaf[l] ? vec3(1.0, 0.78, 0.22) : vec3(0.26, 0.32, 0.40);\n      col = mix(col, lCol, smoothstep(px * 2.6, px * 0.6, dl) * (hitLeaf[l] ? 0.98 : 0.42) * min(boxVis, 1.2));\n    }\n  }\n\n  // Ray-disc closest hit along the active sweeping ray\n  float tClosest = 2.2;\n  int hitPrim = -1;\n  for (int i = 0; i < 8; i++) {\n    int leaf = i / 2;\n    if (!hitLeaf[leaf]) continue;\n    vec2 oc = rayOrig - cPos[i];\n    float b = dot(oc, rayDir);\n    float c = dot(oc, oc) - cRad[i] * cRad[i];\n    float disc = b * b - c;\n    if (disc > 0.0) {\n      float th = -b - sqrt(disc);\n      if (th > 0.0 && th < tClosest) {\n        tClosest = th;\n        hitPrim = i;\n      }\n    }\n  }\n\n  // Draw primitives (highlighted if tested/hit, muted if pruned by BVH)\n  for (int i = 0; i < 8; i++) {\n    int leaf = i / 2;\n    float dC = length(uv - cPos[i]) - cRad[i];\n    if (maxDepth >= 4.0 && boxVis > 0.01) {\n      float dpb = abs(sdBox2(uv, cPos[i] - vec2(cRad[i]), cPos[i] + vec2(cRad[i])));\n      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));\n    }\n    vec3 pCol = (i == hitPrim)\n      ? vec3(1.0, 0.94, 0.52)\n      : (hitLeaf[leaf] ? vec3(0.92, 0.52, 0.22) : vec3(0.28, 0.38, 0.52));\n    float fill = smoothstep(px * 1.5, -px * 1.5, dC);\n    float rim = smoothstep(px * 3.0, px * 0.5, abs(dC));\n    col = mix(col, pCol * (0.75 + 0.35 * smoothstep(cRad[i], -cRad[i], uv.y - cPos[i].y)), fill);\n    col = mix(col, pCol * 1.35, rim * 0.8);\n  }\n\n  // Draw sweeping probe ray & intersection spark\n  vec2 rayEnd = rayOrig + rayDir * tClosest;\n  float dRay = sdSegment(uv, rayOrig, rayEnd);\n  col += vec3(0.25, 0.92, 1.0) * smoothstep(px * 5.0, px * 0.7, dRay) * 0.85;\n  col += vec3(1.0, 0.98, 0.85) * smoothstep(0.022, 0.004, length(uv - rayOrig));\n  if (hitPrim >= 0) {\n    col += vec3(1.0, 0.88, 0.38) * exp(-length(uv - rayEnd) * 32.0) * 1.4;\n  }\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "sah-bvh-construction",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:04:37.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.458,
      "std": 0.1588,
      "min": 0.2509,
      "max": 0.9167
     },
     "image": "/specimens/sah-bvh-construction-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "three-mesh-bvh"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "three-mesh-bvh",
   "why": "The library builds SAH or center trees and exposes raycasts and spatial queries."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/performance/sah-bvh-construction.json"
 },
 "solves": [
  "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"
 ],
 "authority": "MacDonald & Booth, 1990",
 "url": "https://texelatlas.vercel.app/t/sah-bvh-construction/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 13.32,
  "tier": "moderate",
  "p50_ms": 0.4181,
  "p95_ms": 0.434,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "sah-bvh-construction-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 12.54,
   "tier": "moderate",
   "p50_ms": 0.1981,
   "p95_ms": 0.2049,
   "speedup_x": 2.11,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 8.66,
   "tier": "moderate",
   "p50_ms": 0.2495,
   "p95_ms": 0.2639,
   "speedup_x": 1.68,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}