{
 "id": "masked-software-occlusion-culling",
 "title": "Masked software occlusion culling",
 "aliases": [
  "MOC",
  "masked occlusion culling"
 ],
 "domain": "performance",
 "subdomain": "occlusion culling",
 "stage": "performance",
 "realtime": "realtime",
 "platforms": [
  "cpu",
  "desktop-gpu"
 ],
 "year_introduced": 2016,
 "inputs": [
  "mesh",
  "depth"
 ],
 "outputs": [
  "visibility-mask"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "A CPU, SIMD-optimized rasterizer renders coarse occluders into a tiny hierarchical depth buffer built from binary coverage masks, then tests object bounds against it before they are submitted.",
 "how_it_works": "Instead of a full-precision depth buffer, each screen tile keeps two depth layers and a bit mask of which pixels belong to the nearer layer. Rasterizing an occluder triangle computes coverage with SIMD instructions across several scanlines at once and merges it into the mask by comparing against the layers' depth, updating a conservative min/max.\n\nOccluders are low-poly proxies of walls, terrain or large props, drawn front to back on worker threads at low resolution (e.g. 640x400). Objects then get an occludee test: their screen-space bounding box and nearest depth are checked against the buffer, and only objects not fully hidden are sent to the GPU.\n\nBecause everything lives on the CPU, results are available before draw submission, with no GPU readback latency.",
 "when_to_use": "Choose it on CPU-driven engines where draw submission or vertex work is costly and you can supply simple occluder proxies, especially when a GPU depth pyramid is not usable (older APIs, no compute). Prefer `hierarchical-z-occlusion-culling` in a `gpu-driven-rendering` pipeline where the GPU culls its own instances against the previous frame. Prefer `portal-pvs-culling` for enclosed indoor levels with authored cells, and `hardware-occlusion-queries` only when latency is acceptable.",
 "cost": {
  "class": "medium",
  "notes": "Spends a few CPU-core milliseconds per frame on SIMD rasterization; saves GPU and driver time. Qualitative."
 },
 "failure_modes": [
  "Over-aggressive occluder proxies larger than the real mesh cull visible objects, which pop in",
  "Low-resolution conservative buffer misses thin gaps, so occluded objects survive when it should have culled them",
  "Needs AVX2/SSE4.1 or equivalent SIMD; scalar fallback is far slower",
  "Rasterizing dynamic occluders every frame adds CPU latency in busy scenes",
  "The upstream repository is archived, so you maintain your own fork"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "gh:gametechdev/maskedocclusionculling",
   "type": "code",
   "title": "GameTechDev/MaskedOcclusionCulling",
   "description": "Example code for the research paper \"Masked Software Occlusion Culling\"; implements an efficient alternative to the hierarchical depth buffer algorithm.",
   "authors": [
    "GameTechDev"
   ],
   "year": 2016,
   "venue": "GitHub",
   "url": "https://github.com/GameTechDev/MaskedOcclusionCulling",
   "ids": {
    "repo": "GameTechDev/MaskedOcclusionCulling"
   },
   "license": "Apache-2.0",
   "extra": {
    "stars": 695,
    "stars_checked": "2026-11-29",
    "archived": true,
    "fork": false,
    "pushed_at": "2026-09-07T18:18:22Z",
    "language": "C++",
    "homepage": "https://software.intel.com/en-us/articles/masked-software-occlusion-culling",
    "license_name": "Apache License 2.0"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/GameTechDev/MaskedOcclusionCulling",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/GameTechDev/MaskedOcclusionCulling",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/GameTechDev/MaskedOcclusionCulling",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/GameTechDev/MaskedOcclusionCulling",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/GameTechDev/MaskedOcclusionCulling",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/GameTechDev/MaskedOcclusionCulling",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "gametechdev2016gametechdev"
  },
  {
   "role": "secondary",
   "key": "doi:10.1145/2816795.2818138",
   "type": "paper",
   "title": "Masked depth culling for graphics hardware",
   "authors": [
    "Magnus Andersson",
    "Jon Hasselgren",
    "Tomas Akenine-Möller"
   ],
   "year": 2015,
   "venue": "ACM Transactions on Graphics",
   "url": "https://doi.org/10.1145/2816795.2818138",
   "ids": {
    "doi": "10.1145/2816795.2818138"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2816795.2818138",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2816795.2818138",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2816795.2818138",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2816795.2818138",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2816795.2818138",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "andersson2015masked"
  }
 ],
 "presets": [
  {
   "id": "masked-software-occlusion-culling-live",
   "title": "Masked Software Occlusion Culling — 8x4 tile 2-layer HiZ & culled AABBs (left) vs lit scene (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_TILE_RES": 16,
    "P_OCCLUDER_W": 0.65,
    "P_CAM_SWEEP": 0.8,
    "P_SPLIT": 0.5
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_TILE_RES = 16.0; // P HiZ Tile Grid [8.0, 28.0, 2.0]\nconst float P_OCCLUDER_W = 0.65; // P Occluder Width [0.35, 1.0, 0.05]\nconst float P_CAM_SWEEP = 0.80; // P Camera Pan [0.0, 2.0, 0.05]\nconst float P_SPLIT = 0.50; // P HiZ Mask / Scene [0.0, 1.0, 0.02]\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// Foreground large wall occluders (matId=2) + background target occludees (matId=3)\nvec2 mapScene(vec3 p, float occW, bool includeOccludees) {\n  vec2 res = vec2(p.y + 0.72, 1.0); // ground plane\n\n  // Two large foreground occluder walls rasterized into the masked HiZ buffer\n  float dOcc1 = sdBox(p - vec3(-0.55, -0.10, 0.35), vec3(occW * 0.55, 0.62, 0.14));\n  if (dOcc1 < res.x) res = vec2(dOcc1, 2.0);\n  float dOcc2 = sdBox(p - vec3(0.62, -0.18, 0.15), vec3(occW * 0.48, 0.54, 0.14));\n  if (dOcc2 < res.x) res = vec2(dOcc2, 2.0);\n\n  if (includeOccludees) {\n    // 5 background objects sitting behind or peeking past the occluders\n    for (int i = 0; i < 5; i++) {\n      float fi = float(i);\n      vec3 c = vec3((fi - 2.0) * 0.56, -0.24 + 0.12 * mod(fi, 2.0), -0.95 - 0.25 * mod(fi, 3.0));\n      float dObj = sdBox(p - c, vec3(0.20, 0.46, 0.20)) - 0.02;\n      if (dObj < res.x) res = vec2(dObj, 3.0 + fi);\n    }\n  }\n  return res;\n}\n\nvec3 calcNormal(vec3 p, float occW) {\n  vec2 e = vec2(0.003, 0.0);\n  return normalize(vec3(\n    mapScene(p + e.xyy, occW, true).x - mapScene(p - e.xyy, occW, true).x,\n    mapScene(p + e.yxy, occW, true).x - mapScene(p - e.yxy, occW, true).x,\n    mapScene(p + e.yyx, occW, true).x - mapScene(p - e.yyx, occW, true).x\n  ));\n}\n\n// Trace occluder-only depth for the software HiZ tile buffer\nfloat traceOccluderDepth(vec3 ro, vec3 rd, float occW) {\n  float t = 0.0;\n  for (int i = 0; i < 36; i++) {\n    vec2 h = mapScene(ro + rd * t, occW, false);\n    if (h.x < 0.004) return t;\n    t += h.x;\n    if (t > 8.0) break;\n  }\n  return 8.0;\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 split = clamp(P_SPLIT, 0.0, 1.0);\n  bool isLit = screenX >= split;\n  float tileRes = clamp(floor(P_TILE_RES + 0.5), 8.0, 32.0);\n  float occW = clamp(P_OCCLUDER_W, 0.30, 1.05);\n  float pan = sin(iTime * max(P_CAM_SWEEP, 0.0) * 0.7) * 0.58;\n\n  vec3 ro = vec3(pan, 0.32, 2.38);\n  vec3 ta = vec3(0.0, -0.08, -0.60);\n  vec3 fwd = normalize(ta - ro);\n  vec3 right = normalize(cross(fwd, vec3(0.0, 1.0, 0.0)));\n  vec3 up = cross(right, fwd);\n  vec3 rd = normalize(fwd * 1.48 + right * uv.x + up * uv.y);\n\n  // Full scene trace\n  float t = 0.0;\n  float matId = 0.0;\n  bool hit = false;\n  for (int i = 0; i < 56; i++) {\n    vec2 h = mapScene(ro + rd * t, occW, true);\n    if (h.x < 0.0025) { hit = true; matId = h.y; break; }\n    t += h.x;\n    if (t > 8.5) break;\n  }\n\n  vec3 bg = mix(vec3(0.03, 0.045, 0.075), vec3(0.08, 0.11, 0.17), smoothstep(-0.45, 0.55, uv.y));\n  vec3 col = bg;\n\n  if (isLit) {\n    // Right: final rendered scene (only visible non-culled geometry drawn)\n    if (hit) {\n      vec3 p = ro + rd * t;\n      vec3 n = calcNormal(p, occW);\n      vec3 lDir = normalize(vec3(0.55, 0.80, 0.45));\n      float diff = max(dot(n, lDir), 0.0);\n      vec3 alb = vec3(0.24, 0.28, 0.35);\n      if (matId > 1.5 && matId < 2.5) alb = vec3(0.46, 0.52, 0.62);\n      if (matId > 2.5) alb = vec3(0.18, 0.82, 0.62);\n      col = alb * (0.26 + 0.84 * diff);\n    }\n  } else {\n    // Left: Masked 8x4 tile 2-layer HiZ buffer visualization + AABB cull status overlay\n    // Quantize screen UV to 8x4 tile grid (aspect 2:1 tiles)\n    vec2 tileScale = vec2(tileRes * 0.5, tileRes);\n    vec2 tileId = floor(uv * tileScale);\n    vec2 tileF = fract(uv * tileScale);\n    vec2 tileCenterUV = (tileId + 0.5) / tileScale;\n    vec3 rdTile = normalize(fwd * 1.48 + right * tileCenterUV.x + up * tileCenterUV.y);\n\n    // Evaluate tile's 2-layer conservative depth zMax0 (occluder depth) vs pixel exact depth\n    float zTileOcc = traceOccluderDepth(ro, rdTile, occW);\n    float zPixOcc = traceOccluderDepth(ro, rd, occW);\n    float invZTile = clamp(1.4 / max(zTileOcc, 0.8), 0.0, 1.0);\n\n    // Visualize 2-layer coverage mask: bright cyan where foreground occluder sets bitmask = 1\n    vec3 hizCol = mix(vec3(0.04, 0.07, 0.12), vec3(0.16, 0.52, 0.78), invZTile);\n    if (zPixOcc < 4.2) {\n      hizCol = mix(hizCol, vec3(0.26, 0.84, 0.96), 0.45);\n    }\n\n    // X-ray test all 5 background target AABBs against the tile HiZ depth:\n    // RED = fully occluded by HiZ (culled before vertex shading!); GREEN = passes HiZ test (drawn)\n    for (int i = 0; i < 5; i++) {\n      float fi = float(i);\n      vec3 c = vec3((fi - 2.0) * 0.56, -0.24 + 0.12 * mod(fi, 2.0), -0.95 - 0.25 * mod(fi, 3.0));\n      vec3 rel = c - ro;\n      float zObj = dot(rel, fwd);\n      if (zObj <= 0.2) continue;\n      vec2 proj = vec2(dot(rel, right), dot(rel, up)) * (1.48 / zObj);\n      vec2 halfExt = vec2(0.22, 0.48) * (1.48 / zObj);\n      vec2 dBox = abs(uv - proj) - halfExt;\n      float boxSdf = length(max(dBox, 0.0)) + min(max(dBox.x, dBox.y), 0.0);\n      if (boxSdf < 0.012) {\n        // Test AABB near-z against occluder HiZ depth at the AABB center & upper corner\n        vec3 rdC = normalize(fwd * 1.48 + right * proj.x + up * proj.y);\n        vec3 rdTop = normalize(fwd * 1.48 + right * proj.x + up * (proj.y + halfExt.y * 0.75));\n        float zHiZ = max(traceOccluderDepth(ro, rdC, occW), traceOccluderDepth(ro, rdTop, occW));\n        bool culled = (zObj - 0.22) > zHiZ;\n        vec3 aabbCol = culled ? vec3(0.98, 0.24, 0.22) : vec3(0.18, 0.96, 0.52);\n        float border = smoothstep(0.012, 0.002, abs(boxSdf));\n        float fill = step(boxSdf, 0.0) * (culled ? 0.22 : 0.14);\n        hizCol = mix(hizCol, aabbCol, max(border * 0.92, fill));\n      }\n    }\n\n    // Subtle 8x4 tile grid border\n    float grid = smoothstep(0.06, 0.015, min(min(tileF.x, 1.0 - tileF.x), min(tileF.y, 1.0 - tileF.y)));\n    col = mix(hizCol, vec3(0.02, 0.04, 0.07), grid * 0.55);\n  }\n\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": "masked-software-occlusion-culling",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:53:30.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.6365,
      "std": 0.1429,
      "min": 0.2419,
      "max": 0.9014
     },
     "image": "/specimens/masked-software-occlusion-culling-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "hierarchical-z-occlusion-culling",
  "gpu-driven-rendering",
  "portal-pvs-culling",
  "hardware-occlusion-queries"
 ],
 "extends": [
  "hierarchical-z-occlusion-culling"
 ],
 "supersedes": [],
 "combines_with": [
  {
   "id": "meshoptimizer",
   "why": "Simplified low-poly occluder meshes produced offline are what the rasterizer consumes."
  }
 ],
 "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/masked-software-occlusion-culling.json"
 },
 "solves": [
  "too many draw calls for hidden objects",
  "cpu side culling before submitting to the gpu",
  "occlusion culling without gpu readback latency",
  "buildings hide most of the scene but still render",
  "need occlusion culling on old graphics api"
 ],
 "authority": "GameTechDev, 2016",
 "url": "https://texelatlas.vercel.app/t/masked-software-occlusion-culling/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 42.36,
  "tier": "heavy",
  "p50_ms": 1.3132,
  "p95_ms": 1.4222,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 36,
  "preset_id": "masked-software-occlusion-culling-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 43.28,
   "tier": "heavy",
   "p50_ms": 0.6882,
   "p95_ms": 0.7032,
   "speedup_x": 1.91,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 25.85,
   "tier": "heavy",
   "p50_ms": 0.7446,
   "p95_ms": 0.7834,
   "speedup_x": 1.76,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}