---
id: "masked-software-occlusion-culling"
title: "Masked software occlusion culling"
aliases: [MOC, masked occlusion culling]
domain: "performance"
stage: "performance"
realtime: "realtime"
platforms: [cpu, desktop-gpu]
year_introduced: 2016
inputs: [mesh, depth]
outputs: [visibility-mask]
style: "both"
dynamic: "dynamic"
cost: "medium"
authority: "GameTechDev, 2016"
url: "https://texelatlas.vercel.app/t/masked-software-occlusion-culling/"
sources_verified_at: "2026-11-29"
---
# Masked software occlusion culling (GameTechDev, 2016)

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

## Reach for it when

- 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

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

Occluders 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.

Because everything lives on the CPU, results are available before draw submission, with no GPU readback latency.

## When to use it

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](https://texelatlas.vercel.app/t/hierarchical-z-occlusion-culling.md) in a [GPU-driven rendering](https://texelatlas.vercel.app/t/gpu-driven-rendering.md) pipeline where the GPU culls its own instances against the previous frame. Prefer [Portal and PVS visibility culling](https://texelatlas.vercel.app/t/portal-pvs-culling.md) for enclosed indoor levels with authored cells, and [GPU hardware occlusion queries](https://texelatlas.vercel.app/t/hardware-occlusion-queries.md) only when latency is acceptable.

## Cost

medium — Spends a few CPU-core milliseconds per frame on SIMD rasterization; saves GPU and driver time. Qualitative.
Measured GPU Weight: 42.36× baseline (heavy) — 1.3132 ms median / 1.4222 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 43.28× baseline (heavy) — 0.6882 ms median (1.91× faster on L4).
Ampere A100 scaling: 25.85× baseline (heavy) — 0.7446 ms median (1.76× faster on A100).

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

## Dataflow

- Inputs: mesh, depth
- Outputs: visibility-mask
- Platforms: cpu, desktop-gpu

## Presets

### Masked Software Occlusion Culling — 8x4 tile 2-layer HiZ & culled AABBs (left) vs lit scene (right) (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_TILE_RES": 16,
  "P_OCCLUDER_W": 0.65,
  "P_CAM_SWEEP": 0.8,
  "P_SPLIT": 0.5
}
```

```glsl
const float P_TILE_RES = 16.0; // P HiZ Tile Grid [8.0, 28.0, 2.0]
const float P_OCCLUDER_W = 0.65; // P Occluder Width [0.35, 1.0, 0.05]
const float P_CAM_SWEEP = 0.80; // P Camera Pan [0.0, 2.0, 0.05]
const float P_SPLIT = 0.50; // P HiZ Mask / Scene [0.0, 1.0, 0.02]

float sdBox(vec3 p, vec3 b) {
  vec3 q = abs(p) - b;
  return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

// Foreground large wall occluders (matId=2) + background target occludees (matId=3)
vec2 mapScene(vec3 p, float occW, bool includeOccludees) {
  vec2 res = vec2(p.y + 0.72, 1.0); // ground plane

  // Two large foreground occluder walls rasterized into the masked HiZ buffer
  float dOcc1 = sdBox(p - vec3(-0.55, -0.10, 0.35), vec3(occW * 0.55, 0.62, 0.14));
  if (dOcc1 < res.x) res = vec2(dOcc1, 2.0);
  float dOcc2 = sdBox(p - vec3(0.62, -0.18, 0.15), vec3(occW * 0.48, 0.54, 0.14));
  if (dOcc2 < res.x) res = vec2(dOcc2, 2.0);

  if (includeOccludees) {
    // 5 background objects sitting behind or peeking past the occluders
    for (int i = 0; i < 5; i++) {
      float fi = float(i);
      vec3 c = vec3((fi - 2.0) * 0.56, -0.24 + 0.12 * mod(fi, 2.0), -0.95 - 0.25 * mod(fi, 3.0));
      float dObj = sdBox(p - c, vec3(0.20, 0.46, 0.20)) - 0.02;
      if (dObj < res.x) res = vec2(dObj, 3.0 + fi);
    }
  }
  return res;
}

vec3 calcNormal(vec3 p, float occW) {
  vec2 e = vec2(0.003, 0.0);
  return normalize(vec3(
    mapScene(p + e.xyy, occW, true).x - mapScene(p - e.xyy, occW, true).x,
    mapScene(p + e.yxy, occW, true).x - mapScene(p - e.yxy, occW, true).x,
    mapScene(p + e.yyx, occW, true).x - mapScene(p - e.yyx, occW, true).x
  ));
}

// Trace occluder-only depth for the software HiZ tile buffer
float traceOccluderDepth(vec3 ro, vec3 rd, float occW) {
  float t = 0.0;
  for (int i = 0; i < 36; i++) {
    vec2 h = mapScene(ro + rd * t, occW, false);
    if (h.x < 0.004) return t;
    t += h.x;
    if (t > 8.0) break;
  }
  return 8.0;
}

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 screenX = fragCoord.x / max(iResolution.x, 1.0);
  float split = clamp(P_SPLIT, 0.0, 1.0);
  bool isLit = screenX >= split;
  float tileRes = clamp(floor(P_TILE_RES + 0.5), 8.0, 32.0);
  float occW = clamp(P_OCCLUDER_W, 0.30, 1.05);
  float pan = sin(iTime * max(P_CAM_SWEEP, 0.0) * 0.7) * 0.58;

  vec3 ro = vec3(pan, 0.32, 2.38);
  vec3 ta = vec3(0.0, -0.08, -0.60);
  vec3 fwd = normalize(ta - ro);
  vec3 right = normalize(cross(fwd, vec3(0.0, 1.0, 0.0)));
  vec3 up = cross(right, fwd);
  vec3 rd = normalize(fwd * 1.48 + right * uv.x + up * uv.y);

  // Full scene trace
  float t = 0.0;
  float matId = 0.0;
  bool hit = false;
  for (int i = 0; i < 56; i++) {
    vec2 h = mapScene(ro + rd * t, occW, true);
    if (h.x < 0.0025) { hit = true; matId = h.y; break; }
    t += h.x;
    if (t > 8.5) break;
  }

  vec3 bg = mix(vec3(0.03, 0.045, 0.075), vec3(0.08, 0.11, 0.17), smoothstep(-0.45, 0.55, uv.y));
  vec3 col = bg;

  if (isLit) {
    // Right: final rendered scene (only visible non-culled geometry drawn)
    if (hit) {
      vec3 p = ro + rd * t;
      vec3 n = calcNormal(p, occW);
      vec3 lDir = normalize(vec3(0.55, 0.80, 0.45));
      float diff = max(dot(n, lDir), 0.0);
      vec3 alb = vec3(0.24, 0.28, 0.35);
      if (matId > 1.5 && matId < 2.5) alb = vec3(0.46, 0.52, 0.62);
      if (matId > 2.5) alb = vec3(0.18, 0.82, 0.62);
      col = alb * (0.26 + 0.84 * diff);
    }
  } else {
    // Left: Masked 8x4 tile 2-layer HiZ buffer visualization + AABB cull status overlay
    // Quantize screen UV to 8x4 tile grid (aspect 2:1 tiles)
    vec2 tileScale = vec2(tileRes * 0.5, tileRes);
    vec2 tileId = floor(uv * tileScale);
    vec2 tileF = fract(uv * tileScale);
    vec2 tileCenterUV = (tileId + 0.5) / tileScale;
    vec3 rdTile = normalize(fwd * 1.48 + right * tileCenterUV.x + up * tileCenterUV.y);

    // Evaluate tile's 2-layer conservative depth zMax0 (occluder depth) vs pixel exact depth
    float zTileOcc = traceOccluderDepth(ro, rdTile, occW);
    float zPixOcc = traceOccluderDepth(ro, rd, occW);
    float invZTile = clamp(1.4 / max(zTileOcc, 0.8), 0.0, 1.0);

    // Visualize 2-layer coverage mask: bright cyan where foreground occluder sets bitmask = 1
    vec3 hizCol = mix(vec3(0.04, 0.07, 0.12), vec3(0.16, 0.52, 0.78), invZTile);
    if (zPixOcc < 4.2) {
      hizCol = mix(hizCol, vec3(0.26, 0.84, 0.96), 0.45);
    }

    // X-ray test all 5 background target AABBs against the tile HiZ depth:
    // RED = fully occluded by HiZ (culled before vertex shading!); GREEN = passes HiZ test (drawn)
    for (int i = 0; i < 5; i++) {
      float fi = float(i);
      vec3 c = vec3((fi - 2.0) * 0.56, -0.24 + 0.12 * mod(fi, 2.0), -0.95 - 0.25 * mod(fi, 3.0));
      vec3 rel = c - ro;
      float zObj = dot(rel, fwd);
      if (zObj <= 0.2) continue;
      vec2 proj = vec2(dot(rel, right), dot(rel, up)) * (1.48 / zObj);
      vec2 halfExt = vec2(0.22, 0.48) * (1.48 / zObj);
      vec2 dBox = abs(uv - proj) - halfExt;
      float boxSdf = length(max(dBox, 0.0)) + min(max(dBox.x, dBox.y), 0.0);
      if (boxSdf < 0.012) {
        // Test AABB near-z against occluder HiZ depth at the AABB center & upper corner
        vec3 rdC = normalize(fwd * 1.48 + right * proj.x + up * proj.y);
        vec3 rdTop = normalize(fwd * 1.48 + right * proj.x + up * (proj.y + halfExt.y * 0.75));
        float zHiZ = max(traceOccluderDepth(ro, rdC, occW), traceOccluderDepth(ro, rdTop, occW));
        bool culled = (zObj - 0.22) > zHiZ;
        vec3 aabbCol = culled ? vec3(0.98, 0.24, 0.22) : vec3(0.18, 0.96, 0.52);
        float border = smoothstep(0.012, 0.002, abs(boxSdf));
        float fill = step(boxSdf, 0.0) * (culled ? 0.22 : 0.14);
        hizCol = mix(hizCol, aabbCol, max(border * 0.92, fill));
      }
    }

    // Subtle 8x4 tile grid border
    float grid = smoothstep(0.06, 0.015, min(min(tileF.x, 1.0 - tileF.x), min(tileF.y, 1.0 - tileF.y)));
    col = mix(hizCol, vec3(0.02, 0.04, 0.07), grid * 0.55);
  }

  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));
  col = mix(col, vec3(0.95, 0.97, 1.0), sep);

  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: MIT — written for this index.

## Lineage

- extends [Hierarchical-Z occlusion culling](https://texelatlas.vercel.app/t/hierarchical-z-occlusion-culling.md)

## Combines with

- [meshoptimizer mesh optimization](https://texelatlas.vercel.app/t/meshoptimizer.md): Simplified low-poly occluder meshes produced offline are what the rasterizer consumes.

## Sources

1. [primary] GameTechDev (2016) GameTechDev/MaskedOcclusionCulling. https://github.com/GameTechDev/MaskedOcclusionCulling License: Apache-2.0. (verified via github on 2026-11-29)
2. [secondary] Magnus Andersson, Jon Hasselgren, Tomas Akenine-Möller (2015) Masked depth culling for graphics hardware. ACM Transactions on Graphics. https://doi.org/10.1145/2816795.2818138 (verified via crossref on 2026-11-29)

## Cite this entry

Texel. "Masked software occlusion culling." https://texelatlas.vercel.app/t/masked-software-occlusion-culling/ (accessed 2026-12-01).
