{
 "id": "clustered-forward-shading",
 "title": "Clustered forward shading",
 "aliases": [
  "clustered shading",
  "clustered forward"
 ],
 "domain": "performance",
 "subdomain": "light culling",
 "stage": "performance",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu",
  "unity"
 ],
 "year_introduced": 2012,
 "inputs": [
  "depth",
  "mesh"
 ],
 "outputs": [
  "radiance"
 ],
 "style": "both",
 "dynamic": "both",
 "summary": "Divide the view frustum into a 3D grid of clusters, assign each light to the clusters it overlaps, and let each shaded fragment loop only over its cluster's lights.",
 "how_it_works": "The frustum is sliced into screen tiles and depth slices, usually exponential in depth. A compute pass tests each light's bounding volume against cluster bounds and writes light indices into per-cluster lists, optionally restricted to clusters containing visible geometry.\n\nIn the fragment shader, the pixel's position and depth give its cluster id; the shader reads that list and accumulates lighting. Unlike 2D tiling, depth splits mean far-apart surfaces at the same pixel do not share a long list, which handles depth discontinuities and transparent objects.",
 "when_to_use": "Choose it for many dynamic lights with forward rendering, MSAA or transparencies. Prefer `tiled-deferred-shading` when deferred is already in place and depth ranges per tile are tight. Prefer a handful of per-object lights when light count stays small.",
 "cost": {
  "class": "medium",
  "notes": "Cost depends on lights per cluster and cluster count; the build pass is cheap relative to shading."
 },
 "failure_modes": [
  "Large lights fall in many clusters, bloating lists and overflowing fixed list capacity",
  "Lights near cluster boundaries cause popping if bounds are too tight",
  "Slice distribution mismatched to scene depth leaves clusters under- or over-populated",
  "Per-fragment light loops diverge across a wavefront"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/2343045.2343095",
   "type": "paper",
   "title": "Tiled and clustered forward shading",
   "authors": [
    "Ola Olsson",
    "Markus Billeter",
    "Ulf Assarsson"
   ],
   "year": 2012,
   "venue": "ACM SIGGRAPH 2012 Posters on - SIGGRAPH '12",
   "url": "https://doi.org/10.1145/2343045.2343095",
   "ids": {
    "doi": "10.1145/2343045.2343095"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2343045.2343095",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2343045.2343095",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2343045.2343095",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2343045.2343095",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2343045.2343095",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "olsson2012tiled"
  },
  {
   "role": "implementation",
   "key": "gh:pezcode/cluster",
   "type": "code",
   "title": "pezcode/Cluster",
   "description": "Clustered shading implementation with bgfx",
   "authors": [
    "pezcode"
   ],
   "year": 2019,
   "venue": "GitHub",
   "url": "https://github.com/pezcode/Cluster",
   "ids": {
    "repo": "pezcode/Cluster"
   },
   "license": "MIT",
   "extra": {
    "stars": 473,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2022-06-25T17:17:17Z",
    "language": "C++",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pezcode/Cluster",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pezcode/Cluster",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pezcode/Cluster",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pezcode/Cluster",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pezcode/Cluster",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pezcode/Cluster",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "pezcode2019pezcode"
  }
 ],
 "presets": [
  {
   "id": "clustered-forward-shading-specimen",
   "title": "Toy top-down frustum clusters with per-cluster light counts",
   "runtime": "glsl-fragment",
   "params": {
    "LIGHTS": 14,
    "SLICES": 8,
    "BINS": 8,
    "HALF_FOV_TAN": 0.8
   },
   "code": "// Toy top-down view of a frustum cut into clusters (angular bins x exponential depth slices); colour = lights overlapping each cluster.\nconst int LIGHTS = 14;\nconst int SLICES = 8;\nconst int BINS = 8;\nconst float NEAR = 0.1, FAR = 10.0;\nconst float HALF_FOV_TAN = 0.8;\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n    vec2 uv = fragCoord / iResolution.xy;\n    vec2 v = vec2((uv.x - 0.5) * 2.0 * 3.2, 0.0);       // view-space x\n    float z = 0.02 + uv.y * 0.0;                          // placeholder, set below\n    // map screen y to depth with exponential spacing so slice cells look equal height\n    float sl = uv.y * float(SLICES);\n    z = NEAR * pow(FAR / NEAR, sl / float(SLICES));\n    v.x = (uv.x - 0.5) * 2.0 * z * HALF_FOV_TAN;\n    float bin = floor(uv.x * float(BINS));\n    float s = floor(sl);\n    float z0 = NEAR * pow(FAR / NEAR, s / float(SLICES)), z1 = NEAR * pow(FAR / NEAR, (s + 1.0) / float(SLICES));\n    float t0 = (bin / float(BINS) - 0.5) * 2.0 * HALF_FOV_TAN, t1 = ((bin + 1.0) / float(BINS) - 0.5) * 2.0 * HALF_FOV_TAN;\n    vec2 lo = vec2(min(min(t0 * z0, t0 * z1), min(t1 * z0, t1 * z1)), z0);\n    vec2 hi = vec2(max(max(t0 * z0, t0 * z1), max(t1 * z0, t1 * z1)), z1);\n    int cnt = 0; float lit = 0.0; float dm = 1e9;\n    for (int i = 0; i < LIGHTS; i++) {\n        float f = float(i);\n        float lz = NEAR * pow(FAR / NEAR, fract(f * 0.381 + 0.1 * sin(iTime * 0.5 + f)) * 0.95 + 0.03);\n        float lx = lz * HALF_FOV_TAN * (2.0 * fract(f * 0.618 + 0.2) - 1.0) * 0.9;\n        float r = 0.35 * lz + 0.1;\n        vec2 c = vec2(lx, lz);\n        if (distance(clamp(c, lo, hi), c) < r) cnt++;\n        lit += smoothstep(r, 0.0, distance(vec2(v.x, z), c));\n        dm = min(dm, abs(distance(vec2(v.x, z), c) - r) / z);\n    }\n    float x = float(cnt) / 5.0;\n    vec3 heat = clamp(vec3(1.5 * x, 1.5 - abs(3.0 * x - 1.5), 1.5 - 1.5 * x), 0.0, 1.0);\n    vec3 col = mix(vec3(0.04, 0.05, 0.08), heat * 0.6, cnt > 0 ? 1.0 : 0.0);\n    col += vec3(1.0, 0.9, 0.7) * lit * 0.15;\n    vec2 g = fract(vec2(uv.x * float(BINS), sl));\n    col = mix(col, vec3(0.0), (g.x < 0.03 || g.y < 0.03) ? 0.6 : 0.0);\n    col = mix(col, vec3(1.0), smoothstep(0.012, 0.004, dm) * 0.8);\n    fragColor = vec4(pow(col, vec3(0.8)), 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "clustered-forward-shading",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.4691,
      "std": 0.2229,
      "unique_colors": 529
     },
     "image": "/specimens/clustered-forward-shading-specimen.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "tiled-deferred-shading"
 ],
 "extends": [
  "tiled-deferred-shading"
 ],
 "supersedes": [],
 "combines_with": [
  {
   "id": "hierarchical-z-occlusion-culling",
   "why": "A depth pre-pass used for cluster culling also feeds the pyramid used for object culling."
  }
 ],
 "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/clustered-forward-shading.json"
 },
 "solves": [
  "too many dynamic lights tank the frame rate",
  "forward renderer needs hundreds of point lights",
  "need transparent objects lit by many lights",
  "deferred g-buffer is too heavy for msaa or vr"
 ],
 "authority": "Olsson et al., 2012",
 "url": "https://texelatlas.vercel.app/t/clustered-forward-shading/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 9.75,
  "tier": "moderate",
  "p50_ms": 0.3208,
  "p95_ms": 0.3384,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "clustered-forward-shading-specimen",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 9.94,
   "tier": "moderate",
   "p50_ms": 0.1581,
   "p95_ms": 0.1582,
   "speedup_x": 2.03,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 6.44,
   "tier": "moderate",
   "p50_ms": 0.1855,
   "p95_ms": 0.1855,
   "speedup_x": 1.73,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}