{
 "id": "hierarchical-z-occlusion-culling",
 "title": "Hierarchical-Z occlusion culling",
 "aliases": [
  "Hi-Z culling",
  "HZB culling"
 ],
 "domain": "performance",
 "subdomain": "occlusion culling",
 "stage": "performance",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "unity",
  "unreal"
 ],
 "year_introduced": 1993,
 "inputs": [
  "depth",
  "mesh"
 ],
 "outputs": [
  "visibility-mask"
 ],
 "style": "both",
 "dynamic": "both",
 "summary": "Build a mip chain of the depth buffer keeping the farthest depth per texel, then reject any object whose bounds lie entirely behind that conservative depth.",
 "how_it_works": "After rendering occluders, downsample the depth buffer into a pyramid where each texel stores the maximum (farthest) depth of the texels beneath it. To test an object, project its bounding box to screen, pick the mip level at which the box covers about one texel or a few, sample depth there, and compare the box's nearest depth against it. If the nearest point is behind everything, the object is hidden.\n\nGPU-driven engines typically reuse the previous frame's depth, reprojected, then test and draw in a second pass to fix false rejections.",
 "when_to_use": "Choose it for dense indoor or urban scenes where large occluders hide most geometry. Prefer software occlusion rasterization on CPU for non-GPU-driven pipelines, or portal and PVS systems for static levels. Prefer plain frustum culling in open, flat scenes where occlusion rarely helps.",
 "cost": {
  "class": "cheap",
  "notes": "Pyramid build is a few small passes; tests are a handful of texture fetches per object."
 },
 "failure_modes": [
  "Using last frame's depth causes objects to be culled wrongly after fast camera or object motion",
  "Conservative tests miss thin occluders, leaving culling inefficient",
  "Mip selection errors give over-culling and visible holes",
  "Needs depth readable as a texture, which complicates MSAA"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/166117.166147",
   "type": "paper",
   "title": "Hierarchical Z-buffer visibility",
   "authors": [
    "Ned Greene",
    "Michael Kass",
    "Gavin Miller"
   ],
   "year": 1993,
   "venue": "Proceedings of the 20th annual conference on Computer graphics and interactive techniques",
   "url": "https://doi.org/10.1145/166117.166147",
   "ids": {
    "doi": "10.1145/166117.166147"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/166117.166147",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/166117.166147",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/166117.166147",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/166117.166147",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/166117.166147",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "greene1993hierarchical"
  },
  {
   "role": "implementation",
   "key": "gh:zeux/niagara",
   "type": "code",
   "title": "zeux/niagara",
   "description": "A Vulkan renderer written from scratch on stream",
   "authors": [
    "zeux"
   ],
   "year": 2018,
   "venue": "GitHub",
   "url": "https://github.com/zeux/niagara",
   "ids": {
    "repo": "zeux/niagara"
   },
   "license": "MIT",
   "extra": {
    "stars": 1797,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-10-09T02:20:14Z",
    "language": "C++",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/niagara",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/niagara",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/niagara",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/niagara",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/niagara",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/niagara",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "zeux2018zeux"
  }
 ],
 "presets": [
  {
   "id": "hierarchical-z-occlusion-culling-live",
   "title": "Hierarchical-Z occlusion culling — Hi-Z max-depth mip pyramid vs conservative box culling",
   "runtime": "glsl-fragment",
   "params": {
    "P_MIP_LEVEL": 3,
    "P_OCCLUDER_H": 0.72,
    "P_SPLIT": 0.44,
    "P_SWEEP_SPEED": 1
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_MIP_LEVEL = 3.0; // P Hi-Z Mip Level [1.0, 5.0, 1.0]\nconst float P_OCCLUDER_H = 0.72; // P Occluder Height [0.25, 1.2, 0.03]\nconst float P_SPLIT = 0.44; // P Hi-Z / Cull Split [0.0, 1.0, 0.02]\nconst float P_SWEEP_SPEED = 1.0; // P Camera Sway [0.0, 2.5, 0.05]\n\nfloat sdBox(vec3 p, vec3 b) {\n  vec3 q = abs(p) - b;\n  return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);\n}\n\n// Primary foreground occluder walls & piers\nfloat mapOccluders(vec3 p, float occH) {\n  float d = p.y + 0.85;\n  float w1 = sdBox(p - vec3(-0.72, -0.85 + occH, -0.8), vec3(0.58, occH, 0.14));\n  float w2 = sdBox(p - vec3(0.68, -0.85 + occH * 0.88, -1.15), vec3(0.52, occH * 0.88, 0.14));\n  float w3 = sdBox(p - vec3(0.02, -0.85 + occH * 0.62, -2.05), vec3(0.46, occH * 0.62, 0.14));\n  return min(d, min(w1, min(w2, w3)));\n}\n\n// Evaluate depth of foreground occluders along a view ray\nfloat traceOccluderDepth(vec3 ro, vec3 rd, float occH) {\n  float t = 0.05;\n  for (int i = 0; i < 40; i++) {\n    float d = mapOccluders(ro + rd * t, occH);\n    if (d < 0.003) return t;\n    t += d;\n    if (t > 12.0) break;\n  }\n  return 12.0;\n}\n\nvec3 calcOccNormal(vec3 p, float occH) {\n  vec2 e = vec2(0.003, 0.0);\n  return normalize(vec3(\n    mapOccluders(p + e.xyy, occH) - mapOccluders(p - e.xyy, occH),\n    mapOccluders(p + e.yxy, occH) - mapOccluders(p - e.yxy, occH),\n    mapOccluders(p + e.yyx, occH) - mapOccluders(p - e.yyx, occH)\n  ));\n}\n\n// Project a 3D world point into camera screen space (uv, depth)\nvec3 projectPt(vec3 p, vec3 ro, vec3 fw, vec3 rt, vec3 up) {\n  vec3 v = p - ro;\n  float z = dot(v, fw);\n  float x = dot(v, rt) / max(z, 0.05) * 1.45;\n  float y = dot(v, up) / max(z, 0.05) * 1.45;\n  return vec3(x, y, z);\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 screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float tAnim = iTime * max(P_SWEEP_SPEED, 0.0);\n  float occH = clamp(P_OCCLUDER_H, 0.2, 1.3);\n  float mipLvl = floor(clamp(P_MIP_LEVEL, 1.0, 5.0));\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n\n  vec3 ro = vec3(0.65 * sin(tAnim * 0.55), 0.35 + 0.12 * cos(tAnim * 0.4), 2.35);\n  vec3 ta = vec3(0.0, -0.15, -1.4);\n  vec3 fw = normalize(ta - ro);\n  vec3 rt = normalize(cross(fw, vec3(0.0, 1.0, 0.0)));\n  vec3 up = cross(rt, fw);\n  vec3 rd = normalize(fw * 1.45 + rt * uv.x + up * uv.y);\n\n  // Trace foreground occluders\n  float tOcc = traceOccluderDepth(ro, rd, occH);\n\n  // Conservative Hi-Z max-depth mip cell for current pixel\n  float cellRes = pow(2.0, 6.0 - mipLvl);\n  vec2 cellSize = vec2(1.0 / cellRes);\n  vec2 cellCenter = (floor(uv * cellRes) + 0.5) * cellSize;\n  float hiZMax = 0.0;\n  for (int j = 0; j < 2; j++) {\n    for (int i = 0; i < 2; i++) {\n      vec2 off = (vec2(float(i), float(j)) - 0.5) * cellSize * 0.72;\n      vec2 suv = cellCenter + off;\n      vec3 srd = normalize(fw * 1.45 + rt * suv.x + up * suv.y);\n      hiZMax = max(hiZMax, traceOccluderDepth(ro, srd, occH));\n    }\n  }\n\n  // Base shaded scene (sky + occluder walls)\n  vec3 sky = mix(vec3(0.05, 0.07, 0.11), vec3(0.12, 0.18, 0.28), clamp(uv.y + 0.5, 0.0, 1.0));\n  vec3 sceneCol = sky;\n  if (tOcc < 11.5) {\n    vec3 p = ro + rd * tOcc;\n    vec3 n = calcOccNormal(p, occH);\n    vec3 lDir = normalize(vec3(0.55, 0.85, 0.45));\n    float diff = max(dot(n, lDir), 0.0);\n    float isFloor = step(0.85, n.y);\n    vec2 grid = abs(fract(p.xz * 1.5 - 0.5) - 0.5);\n    float gLine = smoothstep(0.035, 0.008, min(grid.x, grid.y));\n    vec3 alb = mix(vec3(0.18, 0.22, 0.29), vec3(0.12, 0.14, 0.18) + gLine * vec3(0.07, 0.11, 0.16), isFloor);\n    float rim = pow(1.0 - max(dot(n, -rd), 0.0), 3.0);\n    sceneCol = alb * (0.28 + 0.85 * diff) + vec3(0.18, 0.34, 0.52) * rim * (1.0 - isFloor) * 0.45;\n  }\n\n  // Evaluate 9 candidate bounding boxes behind the occluders against the Hi-Z pyramid\n  vec3 boxOverlay = vec3(0.0);\n  float boxAlpha = 0.0;\n  for (int k = 0; k < 9; k++) {\n    float fk = float(k);\n    float row = floor(fk / 3.0);\n    float colIdx = mod(fk, 3.0) - 1.0;\n    vec3 bPos = vec3(\n      colIdx * 0.92 + 0.25 * sin(tAnim * 0.7 + fk * 1.4),\n      -0.42 + 0.32 * sin(tAnim * 0.9 + fk * 2.1) + row * 0.18,\n      -1.55 - row * 0.95\n    );\n    vec3 bHalf = vec3(0.20, 0.24, 0.20);\n\n    // Project candidate box center & evaluate conservative Hi-Z occlusion test\n    vec3 proj = projectPt(bPos, ro, fw, rt, up);\n    float boxMinZ = proj.z - 0.24;\n    vec2 bCell = (floor(proj.xy * cellRes) + 0.5) * cellSize;\n    float localHiZ = 0.0;\n    for (int sy = 0; sy < 2; sy++) {\n      for (int sx = 0; sx < 2; sx++) {\n        vec2 suv = bCell + (vec2(float(sx), float(sy)) - 0.5) * cellSize * 0.65;\n        vec3 srd = normalize(fw * 1.45 + rt * suv.x + up * suv.y);\n        localHiZ = max(localHiZ, traceOccluderDepth(ro, srd, occH));\n      }\n    }\n    bool isCulled = boxMinZ > localHiZ;\n\n    // Ray-box analytic intersection for rendering the candidate box\n    vec3 invRd = 1.0 / (rd + sign(rd + 1e-6) * 1e-5);\n    vec3 t0 = (bPos - bHalf - ro) * invRd;\n    vec3 t1 = (bPos + bHalf - ro) * invRd;\n    vec3 tMinV = min(t0, t1);\n    vec3 tMaxV = max(t0, t1);\n    float tNear = max(max(tMinV.x, tMinV.y), tMinV.z);\n    float tFar = min(min(tMaxV.x, tMaxV.y), tMaxV.z);\n    if (tFar > max(tNear, 0.05)) {\n      vec3 hitP = ro + rd * tNear;\n      vec3 lp = abs((hitP - bPos) / bHalf);\n      float edge = smoothstep(0.84, 0.96, max(max(min(lp.x, lp.y), min(lp.y, lp.z)), min(lp.x, lp.z)));\n      if (!isCulled && tNear < tOcc) {\n        vec3 bn = normalize(sign(hitP - bPos) * step(vec3(0.98), lp));\n        float bDiff = 0.35 + 0.75 * max(dot(bn, normalize(vec3(0.5, 0.9, 0.4))), 0.0);\n        vec3 visCol = mix(vec3(0.14, 0.84, 0.58), vec3(0.96, 0.98, 0.85), edge);\n        sceneCol = visCol * bDiff;\n        tOcc = tNear;\n      } else if (isCulled) {\n        // X-ray magenta wireframe showing Hi-Z culled geometry saved from rasterization\n        vec3 cullCol = vec3(1.0, 0.22, 0.55) * (0.28 + 1.35 * edge);\n        boxOverlay = max(boxOverlay, cullCol);\n        boxAlpha = max(boxAlpha, 0.24 + 0.58 * edge);\n      }\n    }\n\n    // Screen-space projected AABB indicator on Hi-Z side\n    float halfW = 0.24 / max(proj.z, 0.3) * 1.45;\n    vec2 boxD = abs(uv - proj.xy) - vec2(halfW);\n    float rectEdge = smoothstep(0.012, 0.002, abs(max(boxD.x, boxD.y)));\n    if (screenX < split) {\n      vec3 tagCol = isCulled ? vec3(1.0, 0.24, 0.52) : vec3(0.18, 0.96, 0.62);\n      boxOverlay = max(boxOverlay, tagCol * rectEdge * 1.4);\n      boxAlpha = max(boxAlpha, rectEdge * 0.85);\n    }\n  }\n\n  // Left view: Hi-Z Depth Mip Pyramid visualization\n  vec3 hiZCol;\n  float zNorm = clamp(1.0 - (hiZMax - 1.2) / 5.5, 0.0, 1.0);\n  hiZCol = mix(vec3(0.04, 0.08, 0.22), vec3(0.18, 0.68, 0.94), smoothstep(0.0, 0.65, zNorm));\n  hiZCol = mix(hiZCol, vec3(0.96, 0.92, 0.52), smoothstep(0.65, 1.0, zNorm));\n  vec2 cellF = abs(fract(uv * cellRes) - 0.5);\n  float cellGrid = smoothstep(0.46, 0.50, max(cellF.x, cellF.y));\n  hiZCol = mix(hiZCol, vec3(0.05, 0.09, 0.14), cellGrid * 0.55);\n\n  vec3 col = (screenX < split) ? mix(hiZCol, boxOverlay, boxAlpha) : mix(sceneCol, boxOverlay, boxAlpha * 0.68);\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\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": "hierarchical-z-occlusion-culling",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:04:30.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5654,
      "std": 0.1638,
      "min": 0.2898,
      "max": 0.9014
     },
     "image": "/specimens/hierarchical-z-occlusion-culling-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "gpu-driven-rendering",
  "meshlets"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "meshlets",
   "why": "Meshlet bounding spheres project to small boxes that the pyramid can reject cheaply."
  }
 ],
 "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/hierarchical-z-occlusion-culling.json"
 },
 "solves": [
  "objects hidden behind walls still get drawn",
  "dense city or indoor scene overdraws badly",
  "need to skip hidden geometry using the depth buffer",
  "too many draws for things you can't see",
  "gpu culling against last frame's depth"
 ],
 "authority": "Greene et al., 1993",
 "url": "https://texelatlas.vercel.app/t/hierarchical-z-occlusion-culling/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 345.82,
  "tier": "extreme",
  "p50_ms": 10.8589,
  "p95_ms": 10.9263,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "hierarchical-z-occlusion-culling-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 379.3,
   "tier": "extreme",
   "p50_ms": 5.993,
   "p95_ms": 6.0546,
   "speedup_x": 1.81,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 231.36,
   "tier": "extreme",
   "p50_ms": 6.6632,
   "p95_ms": 6.6933,
   "speedup_x": 1.63,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}