{
 "id": "sample-distribution-shadow-maps",
 "title": "Sample Distribution Shadow Maps (SDSM)",
 "aliases": [
  "SDSM"
 ],
 "domain": "visibility",
 "subdomain": "shadow map fitting",
 "stage": "visibility",
 "realtime": "realtime",
 "platforms": [
  "desktop-gpu",
  "webgpu",
  "unity",
  "unreal"
 ],
 "year_introduced": 2011,
 "inputs": [
  "depth",
  "mesh"
 ],
 "outputs": [
  "visibility-mask"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "Analyses the depth buffer each frame to fit cascade split positions and light-space bounds tightly around the pixels that will actually sample the shadow map.",
 "how_it_works": "Classic cascaded shadow maps place split planes with a fixed scheme and fit each cascade to the whole sub-frustum, wasting texels on empty space. SDSM first renders the scene depth and runs a GPU reduction to find the minimum and maximum visible view depth. It can then (a) set split planes from a logarithmic partition of that real range, or from a depth histogram, so cascades follow where pixels exist, and (b) for each cascade compute the tight light-space bounding box of just the visible samples, using min/max reductions of the projected positions. The resulting orthographic projections are read back or consumed by indirect dispatch, then used to render the maps. Snapping extents to texel increments keeps shadows stable under camera motion, and conservative expansion handles reduction latency.",
 "when_to_use": "Choose it when `cascaded-shadow-maps` waste resolution because the visible depth range varies a lot, as in outdoor scenes that alternate between looking at a wall and looking at the horizon. Prefer `virtual-shadow-maps` when geometry density and uniform quality matter more than fitting, and keep plain stabilised CSM for mobile where an extra depth reduction is too costly. Pair with normal-offset bias from `shadow-map-bias-slope-scale-normal-offset` per cascade.",
 "cost": {
  "class": "medium",
  "notes": "Adds a depth prepass and a compute reduction; the savings come from rendering smaller, tighter shadow maps. Cost is qualitative and scene dependent."
 },
 "failure_modes": [
  "Shadow shimmering when cascade bounds change every frame without stabilisation or hysteresis",
  "One-frame latency in bounds when reductions are read back, causing pop at fast camera turns",
  "Tight fit excludes off-screen casters unless the near plane is extended toward the light",
  "Depth outliers (sky, tiny particles) must be masked or the reduction loosens again"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/1944745.1944761",
   "type": "paper",
   "title": "Sample distribution shadow maps",
   "authors": [
    "Andrew Lauritzen",
    "Marco Salvi",
    "Aaron Lefohn"
   ],
   "year": 2011,
   "venue": "Symposium on Interactive 3D Graphics and Games",
   "url": "https://doi.org/10.1145/1944745.1944761",
   "ids": {
    "doi": "10.1145/1944745.1944761"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1944745.1944761",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1944745.1944761",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1944745.1944761",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1944745.1944761",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1944745.1944761",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "lauritzen2011sample"
  },
  {
   "role": "implementation",
   "key": "gh:therealmjp/shadows",
   "type": "code",
   "title": "TheRealMJP/Shadows",
   "description": "A sample app that demonstrates several techniques for rendering real-time shadow maps",
   "authors": [
    "TheRealMJP"
   ],
   "year": 2016,
   "venue": "GitHub",
   "url": "https://github.com/TheRealMJP/Shadows",
   "ids": {
    "repo": "TheRealMJP/Shadows"
   },
   "license": "MIT",
   "extra": {
    "stars": 1051,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-06-07T19:33:57Z",
    "language": "C++",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/TheRealMJP/Shadows",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/TheRealMJP/Shadows",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/TheRealMJP/Shadows",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/TheRealMJP/Shadows",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/TheRealMJP/Shadows",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/TheRealMJP/Shadows",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "therealmjp2016therealmjp"
  }
 ],
 "presets": [
  {
   "id": "sample-distribution-shadow-maps-live",
   "title": "Sample Distribution Shadow Maps (SDSM) — fixed CSM wasted cascades & aliasing (left) vs depth-fitted tight SDSM cascades (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_CASCADES": 4,
    "P_BAND_VIS": 0.55,
    "P_SUN_SWEEP": 0.5,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_CASCADES = 4.00; // P Cascade Count [2.0, 5.0, 1.0]\nconst float P_BAND_VIS = 0.55; // P Cascade Tint [0.0, 1.0, 0.05]\nconst float P_SUN_SWEEP = 0.50; // P Sun Sweep [0.0, 1.5, 0.05]\nconst float P_SPLIT = 0.48; // P Fixed CSM / SDSM [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// Long architectural vista with foreground + mid-ground + distant monoliths and empty depth gap in between\nvec2 mapVista(vec3 p) {\n  float dFloor = p.y + 0.55;\n  vec2 res = vec2(dFloor, 1.0);\n\n  // Repeating rows of tall monoliths along -Z\n  for (int i = 0; i < 4; i++) {\n    float z = -0.35 - float(i) * 1.35;\n    float dL = sdBox(p - vec3(-0.62, 0.05, z), vec3(0.16, 0.60, 0.16));\n    float dR = sdBox(p - vec3(0.62, 0.05, z), vec3(0.16, 0.60, 0.16));\n    float dTop = sdBox(p - vec3(0.0, 0.58, z), vec3(0.80, 0.09, 0.16));\n    float dArch = min(min(dL, dR), dTop);\n    if (dArch < res.x) res = vec2(dArch, 2.0);\n  }\n  return res;\n}\n\nvec3 calcNormal(vec3 p) {\n  vec2 e = vec2(0.003, 0.0);\n  return normalize(vec3(\n    mapVista(p + e.xyy).x - mapVista(p - e.xyy).x,\n    mapVista(p + e.yxy).x - mapVista(p - e.yxy).x,\n    mapVista(p + e.yyx).x - mapVista(p - e.yyx).x\n  ));\n}\n\n// Trace shadow ray from quantized shadow-map texel position to visualize real texel-resolution difference!\nfloat traceShadowAt(vec3 p, vec3 sunDir, float texelSize) {\n  // Snap point onto light-space shadow-map texel grid of size texelSize\n  vec3 lUp = vec3(0.0, 1.0, 0.0);\n  vec3 lRight = normalize(cross(lUp, sunDir));\n  vec3 lOrtho = cross(sunDir, lRight);\n  float u = floor(dot(p, lRight) / texelSize + 0.5) * texelSize;\n  float v = floor(dot(p, lOrtho) / texelSize + 0.5) * texelSize;\n  float w = dot(p, sunDir);\n  vec3 pSnap = lRight * u + lOrtho * v + sunDir * w;\n\n  float t = 0.05;\n  for (int i = 0; i < 20; i++) {\n    float h = mapVista(pSnap + sunDir * t).x;\n    if (h < 0.005) return 0.0;\n    t += max(h, 0.04);\n    if (t > 4.0) break;\n  }\n  return 1.0;\n}\n\nvec3 cascadeColor(float idx) {\n  if (idx < 0.5) return vec3(0.18, 0.92, 0.56);\n  if (idx < 1.5) return vec3(0.20, 0.72, 1.0);\n  if (idx < 2.5) return vec3(0.98, 0.78, 0.22);\n  if (idx < 3.5) return vec3(0.98, 0.36, 0.32);\n  return vec3(0.84, 0.38, 0.98);\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 numCasc = clamp(floor(P_CASCADES + 0.5), 2.0, 5.0);\n  float bandVis = clamp(P_BAND_VIS, 0.0, 1.0);\n  float t = iTime * max(P_SUN_SWEEP, 0.0);\n\n  vec3 ro = vec3(0.16 * sin(t * 0.4), 0.36, 2.20);\n  vec3 rd = normalize(vec3(uv.x, uv.y - 0.10, -1.45));\n  vec3 sunDir = normalize(vec3(0.68 * cos(t * 0.35 + 0.4), 0.62, 0.38));\n  vec3 sky = mix(vec3(0.72, 0.80, 0.90), vec3(0.18, 0.42, 0.76), smoothstep(-0.08, 0.55, rd.y));\n  vec3 col = sky;\n\n  float dist = 0.05;\n  vec2 hit = vec2(12.0, 0.0);\n  for (int i = 0; i < 64; i++) {\n    vec2 h = mapVista(ro + rd * dist);\n    if (h.x < 0.003) { hit = vec2(dist, h.y); break; }\n    dist += h.x;\n    if (dist > 11.0) break;\n  }\n\n  if (hit.y > 0.5) {\n    vec3 p = ro + rd * hit.x;\n    vec3 n = calcNormal(p);\n    float viewZ = hit.x;\n\n    float cascIdx = 0.0;\n    float texelSz = 0.02;\n    if (screenX < split) {\n      // Left: Fixed naive CSM splits spanning [0.1 .. 24.0] -> wastes cascade 0 in empty air near camera,\n      // forcing visible geometry into coarse cascades with blocky aliased shadows!\n      float zFrac = clamp(log(max(viewZ, 0.1) / 0.1) / log(24.0 / 0.1), 0.0, 0.999);\n      cascIdx = floor(zFrac * numCasc);\n      texelSz = 0.032 * pow(1.75, cascIdx);\n    } else {\n      // Right: SDSM reduces actual visible depth bounds [zMin=1.6 .. zMax=8.2] each frame ->\n      // tightly fits all cascades around real scene samples for 3-4x crisper shadow texels!\n      float zMin = 1.55;\n      float zMax = 8.40;\n      float zFrac = clamp(log(max(viewZ, zMin) / zMin) / log(zMax / zMin), 0.0, 0.999);\n      cascIdx = floor(zFrac * numCasc);\n      texelSz = 0.008 * pow(1.42, cascIdx);\n    }\n\n    float sh = traceShadowAt(p + n * 0.01, sunDir, texelSz);\n    vec3 alb = (hit.y < 1.5) ? vec3(0.74, 0.72, 0.68) : vec3(0.86, 0.82, 0.76);\n    float ndl = max(dot(n, sunDir), 0.0);\n    vec3 lit = alb * (vec3(0.20, 0.28, 0.42) + vec3(1.32, 1.18, 0.96) * ndl * sh);\n\n    // Subtle cascade split band tint & boundary line\n    vec3 cTint = cascadeColor(cascIdx);\n    col = mix(lit, lit * (0.55 + 0.75 * cTint), bandVis * 0.48);\n    col = mix(sky, col, exp(-viewZ * 0.06));\n  }\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": "sample-distribution-shadow-maps",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T21:07:23.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.8363,
      "std": 0.0556,
      "min": 0.6708,
      "max": 0.914
     },
     "image": "/specimens/sample-distribution-shadow-maps-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "cascaded-shadow-maps",
  "virtual-shadow-maps",
  "shadow-mapping",
  "percentage-closer-filtering"
 ],
 "extends": [
  "cascaded-shadow-maps"
 ],
 "supersedes": [],
 "combines_with": [
  {
   "id": "hierarchical-z-occlusion-culling",
   "why": "The depth pyramid built for culling can provide the min/max depth the SDSM reduction needs."
  }
 ],
 "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/sample-distribution-shadow-maps.json"
 },
 "solves": [
  "cascaded shadows waste resolution when looking at a wall",
  "shadow quality changes a lot depending on view direction",
  "cascade splits do not match the visible depth range",
  "want tighter shadow map fitting around visible pixels",
  "distant sun shadows blurry although camera sees little depth"
 ],
 "authority": "Lauritzen et al., 2011",
 "url": "https://texelatlas.vercel.app/t/sample-distribution-shadow-maps/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 48.99,
  "tier": "heavy",
  "p50_ms": 1.6119,
  "p95_ms": 1.6824,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "sample-distribution-shadow-maps-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 55.66,
   "tier": "heavy",
   "p50_ms": 0.885,
   "p95_ms": 0.8938,
   "speedup_x": 1.82,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 32.78,
   "tier": "heavy",
   "p50_ms": 0.9441,
   "p95_ms": 0.9618,
   "speedup_x": 1.71,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}