{
 "id": "shadow-volumes",
 "title": "Shadow Volumes",
 "aliases": [],
 "domain": "visibility",
 "subdomain": "geometric shadows",
 "stage": "visibility",
 "realtime": "realtime",
 "platforms": [
  "desktop-gpu",
  "webgl2",
  "unity",
  "unreal",
  "godot"
 ],
 "year_introduced": 1977,
 "inputs": [
  "mesh"
 ],
 "outputs": [
  "visibility-mask"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "Extrudes occluder silhouettes away from the light into volumes, then uses a stencil counter to decide whether each pixel lies inside any of them.",
 "how_it_works": "For each point light, find silhouette edges of casters (edges shared by a front-facing and a back-facing triangle) and extrude them to infinity, forming closed volumes. Render the scene depth first, then draw volume faces into the stencil buffer: increment on back faces and decrement on front faces that pass depth (z-fail, Carmack's reverse) or the opposite for z-pass. Pixels with a non-zero count lie in shadow and are excluded from the lighting pass. The result has exact, pixel-accurate hard edges independent of any shadow-map resolution, but requires watertight, manifold geometry, silhouette computation and heavy fill rate.",
 "when_to_use": "Choose it for razor-sharp hard shadows on simple, closed meshes where shadow-map aliasing is unacceptable. Prefer `shadow-mapping` for any content with alpha tests, foliage, skinned or non-manifold meshes, `percentage-closer-filtering` for soft edges, and `ray-traced-shadows` for exact results on modern hardware.",
 "cost": {
  "class": "medium",
  "notes": "Fill-rate bound: large extruded quads cover many pixels, so cost scales with overdraw of volume faces."
 },
 "failure_modes": [
  "Requires closed, manifold meshes or shadows fail",
  "Cap and far-plane handling needed for z-fail correctness",
  "Hard edges only; softening needs extra tricks",
  "Very high fill-rate cost with many lights"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/563858.563901",
   "type": "paper",
   "title": "Shadow algorithms for computer graphics",
   "authors": [
    "Franklin C. Crow"
   ],
   "year": 1977,
   "venue": "Proceedings of the 4th annual conference on Computer graphics and interactive techniques",
   "url": "https://doi.org/10.1145/563858.563901",
   "ids": {
    "doi": "10.1145/563858.563901"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/563858.563901",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/563858.563901",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/563858.563901",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/563858.563901",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/563858.563901",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "crow1977shadow"
  },
  {
   "role": "tutorial",
   "key": "arxiv:cs/0301002",
   "type": "paper",
   "title": "Practical and Robust Stenciled Shadow Volumes for Hardware-Accelerated Rendering",
   "authors": [
    "Cass Everitt",
    "Mark J. Kilgard"
   ],
   "year": 2003,
   "venue": "arXiv preprint (cs.GR)",
   "url": "https://arxiv.org/abs/cs/0301002",
   "ids": {
    "arxiv": "cs/0301002"
   },
   "license": null,
   "extra": {
    "arxiv_version": "1",
    "category": "cs.GR"
   },
   "provenance": {
    "title": {
     "authority": "arxiv",
     "endpoint": "https://export.arxiv.org/api/query?id_list=cs/0301002",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "arxiv",
     "endpoint": "https://export.arxiv.org/api/query?id_list=cs/0301002",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "arxiv",
     "endpoint": "https://export.arxiv.org/api/query?id_list=cs/0301002",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "arxiv",
     "endpoint": "https://export.arxiv.org/api/query?id_list=cs/0301002",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "arxiv",
     "endpoint": "https://export.arxiv.org/api/query?id_list=cs/0301002",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "everitt2003practical"
  }
 ],
 "presets": [
  {
   "id": "shadow-volumes-live",
   "title": "Shadow Volumes (Carmack Reverse) — extruded silhouette shadow wedges & stencil count (left) vs razor-sharp stencil-masked shadows (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_VOL_ALPHA": 0.6,
    "P_LIGHT_SWEEP": 0.55,
    "P_AMBIENT": 0.28,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_VOL_ALPHA = 0.60; // P Volume Extrusion Vis [0.1, 1.0, 0.05]\nconst float P_LIGHT_SWEEP = 0.55; // P Light Motion [0.0, 1.6, 0.05]\nconst float P_AMBIENT = 0.28; // P Ambient Level [0.05, 0.6, 0.05]\nconst float P_SPLIT = 0.48; // P Volumes / Stencil [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// Sharp polygonal obelisks & floating prism casting extruded geometric shadow volumes\nvec2 mapCasters(vec3 p) {\n  float d1 = sdBox(p - vec3(-0.36, -0.10, -0.15), vec3(0.14, 0.42, 0.14));\n  float d2 = sdBox(p - vec3(0.34, 0.06, -0.05), vec3(0.16, 0.16, 0.16));\n  vec2 res = (d1 < d2) ? vec2(d1, 2.0) : vec2(d2, 3.0);\n  return res;\n}\n\nvec2 mapScene(vec3 p) {\n  vec2 res = vec2(p.y + 0.52, 1.0);\n  vec2 c = mapCasters(p);\n  if (c.x < res.x) res = c;\n  return res;\n}\n\nvec3 calcNormal(vec3 p) {\n  vec2 e = vec2(0.002, 0.0);\n  return normalize(vec3(\n    mapScene(p + e.xyy).x - mapScene(p - e.xyy).x,\n    mapScene(p + e.yxy).x - mapScene(p - e.yxy).x,\n    mapScene(p + e.yyx).x - mapScene(p - e.yyx).x\n  ));\n}\n\n// Exact point-in-extruded-shadow-volume test (projects ray backward toward lightPos to see if a caster blocks it)\nbool inShadowVolume(vec3 p, vec3 lPos) {\n  vec3 toL = lPos - p;\n  float distL = length(toL);\n  vec3 dir = toL / max(distL, 1e-4);\n  float t = 0.025;\n  for (int i = 0; i < 24; i++) {\n    float h = mapCasters(p + dir * t).x;\n    if (h < 0.0025) return true;\n    t += max(h, 0.025);\n    if (t > distL - 0.04) break;\n  }\n  return false;\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  float volVis = clamp(P_VOL_ALPHA, 0.05, 1.0);\n  float amb = clamp(P_AMBIENT, 0.05, 0.7);\n  float t = iTime * max(P_LIGHT_SWEEP, 0.0);\n\n  vec3 lPos = vec3(0.95 * cos(t * 0.65), 0.78, 0.75 * sin(t * 0.65) + 0.15);\n  vec3 ro = vec3(0.0, 0.42, 2.25);\n  vec3 rd = normalize(vec3(uv.x, uv.y - 0.15, -1.48));\n  vec3 col = mix(vec3(0.04, 0.05, 0.09), vec3(0.11, 0.15, 0.24), smoothstep(-0.3, 0.6, uv.y));\n\n  float dist = 0.05;\n  vec2 hit = vec2(10.0, 0.0);\n  for (int i = 0; i < 64; i++) {\n    vec2 h = mapScene(ro + rd * dist);\n    if (h.x < 0.002) { hit = vec2(dist, h.y); break; }\n    dist += h.x;\n    if (dist > 9.0) break;\n  }\n\n  float maxMarch = min(hit.x, 5.5);\n\n  if (hit.y > 0.5) {\n    vec3 p = ro + rd * hit.x;\n    vec3 n = calcNormal(p);\n    vec3 lDir = normalize(lPos - p);\n    float lDist = length(lPos - p);\n    float atten = 2.6 / (0.4 + lDist * lDist);\n\n    // Exact binary stencil shadow test (pixel-sharp, zero shadow-map texel aliasing!)\n    float lit = inShadowVolume(p + n * 0.008, lPos) ? 0.0 : 1.0;\n\n    vec3 alb = vec3(0.68, 0.66, 0.62);\n    if (hit.y < 1.5) {\n      float chk = mod(floor(p.x * 3.0) + floor(p.z * 3.0), 2.0);\n      alb = mix(vec3(0.50, 0.52, 0.56), vec3(0.76, 0.75, 0.72), chk);\n    } else if (hit.y < 2.5) {\n      alb = vec3(0.86, 0.54, 0.26);\n    } else {\n      alb = vec3(0.26, 0.72, 0.88);\n    }\n\n    float ndl = max(dot(n, lDir), 0.0);\n    col = alb * (vec3(0.16, 0.22, 0.34) * amb * 2.2 + vec3(1.40, 1.24, 1.02) * ndl * lit * atten);\n    if (hit.y < 1.5) col = mix(vec3(0.04, 0.05, 0.09), col, exp(-hit.x * 0.11));\n  }\n\n  // Left: Visualize the translucent extruded silhouette shadow-volume wedges & entry/exit stencil faces!\n  if (screenX < split) {\n    float volHits = 0.0;\n    float edgeHits = 0.0;\n    bool prevIn = false;\n    for (int s = 1; s <= 22; s++) {\n      float ts = (float(s) / 22.0) * maxMarch;\n      vec3 sp = ro + rd * ts;\n      bool curIn = inShadowVolume(sp, lPos);\n      if (curIn) volHits += 1.0;\n      if (curIn != prevIn && s > 1) edgeHits += 1.0;\n      prevIn = curIn;\n    }\n    vec3 wedgeCol = mix(vec3(0.16, 0.78, 1.0), vec3(1.0, 0.28, 0.54), clamp(edgeHits * 0.5, 0.0, 1.0));\n    col += wedgeCol * (volHits / 22.0) * volVis * 0.85;\n    col += vec3(0.28, 0.96, 1.0) * clamp(edgeHits, 0.0, 2.0) * volVis * 0.24;\n  }\n\n  // Glowing point light indicator\n  float glowD = length(cross(lPos - ro, rd));\n  col += vec3(1.4, 1.15, 0.75) * exp(-glowD * 12.0) * 0.55;\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.98, 1.0), sep);\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": "shadow-volumes",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T21:07:24.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5465,
      "std": 0.1726,
      "min": 0.2556,
      "max": 0.9528
     },
     "image": "/specimens/shadow-volumes-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "shadow-mapping",
  "ray-traced-shadows"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/visibility/shadow-volumes.json"
 },
 "solves": [
  "shadow map edges are jaggy or aliased",
  "need razor sharp pixel-accurate hard shadows",
  "shadow acne and peter-panning in simple scenes",
  "stencil-based shadows for closed meshes"
 ],
 "authority": "Crow, 1977",
 "url": "https://texelatlas.vercel.app/t/shadow-volumes/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 51.13,
  "tier": "heavy",
  "p50_ms": 1.6822,
  "p95_ms": 1.7911,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "shadow-volumes-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 51.33,
   "tier": "heavy",
   "p50_ms": 0.8161,
   "p95_ms": 0.8248,
   "speedup_x": 2.06,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 33.22,
   "tier": "heavy",
   "p50_ms": 0.9567,
   "p95_ms": 0.9762,
   "speedup_x": 1.76,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}