{
 "id": "irradiance-caching",
 "title": "Irradiance Caching",
 "aliases": [
  "Ward irradiance cache",
  "irradiance gradients"
 ],
 "domain": "gi",
 "subdomain": "sparse interpolation",
 "stage": "lighting",
 "realtime": "offline",
 "platforms": [
  "cpu"
 ],
 "year_introduced": 1988,
 "inputs": [
  "mesh",
  "material-params"
 ],
 "outputs": [
  "irradiance-probes"
 ],
 "style": "photoreal",
 "dynamic": "static",
 "summary": "Computes expensive diffuse indirect irradiance only at sparse surface points and interpolates between them, placing samples more densely near geometry where lighting changes quickly.",
 "how_it_works": "Diffuse indirect light varies slowly over open surfaces and quickly near corners and occluders. Irradiance caching exploits this by computing the hemispherical integral (with hundreds of rays) only at selected points and reusing it for neighbours.\n\nWhen a shading point `x` needs indirect light, the renderer searches an **octree** of earlier cache records. A record at `p` with normal `n_p` and **validity radius** `R_p` (tied to the harmonic mean distance to surfaces seen from `p`) contributes a weight `w = 1 / (|x - p| / R_p + sqrt(1 - n . n_p))`. If the summed weights exceed a threshold, the result is the weighted average of the contributing records. Otherwise a new record is computed and inserted.\n\nBecause the validity radius is proportional to the distance of nearby geometry, cache records are dense in corners and sparse on flat walls. **Irradiance gradients** (translational and rotational) extrapolate each record's value linearly, which allows larger spacing and smoother results.",
 "when_to_use": "Choose it for offline diffuse GI where final gathering at every pixel is too expensive, such as in a classic ray-tracing renderer or baker with photon maps.\n\nPrefer [path-tracing](path-tracing) when you want unbiased, simpler code and cost is acceptable. Prefer [lightmap-baking](lightmap-baking) when the output should live in texture space for real-time use. Prefer [surfel-gi](surfel-gi) when you want the same idea (sparse cached surface irradiance) at runtime and dynamically.",
 "cost": {
  "class": "offline",
  "notes": "Reduces indirect rays per pixel by one to two orders of magnitude on smooth scenes; cost depends on the error threshold."
 },
 "failure_modes": [
  "Splotchy low-frequency artifacts where record spacing is coarse and interpolation is visible",
  "Light leaks at thin geometry because records near a wall can be applied to the other side",
  "Not view-independent in practice: sampling order shows as temporal inconsistency in animation, so caches must be shared across frames",
  "Diffuse only; poor for glossy indirect lighting"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/54852.378490",
   "type": "paper",
   "title": "A ray tracing solution for diffuse interreflection",
   "authors": [
    "Gregory J. Ward",
    "Francis M. Rubinstein",
    "Robert D. Clear"
   ],
   "year": 1988,
   "venue": "Proceedings of the 15th annual conference on Computer graphics and interactive techniques",
   "url": "https://doi.org/10.1145/54852.378490",
   "ids": {
    "doi": "10.1145/54852.378490"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/54852.378490",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/54852.378490",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/54852.378490",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/54852.378490",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/54852.378490",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "ward1988tracing"
  }
 ],
 "presets": [
  {
   "id": "irradiance-caching-live",
   "title": "Irradiance Caching — sparse Ward cache records & validity radii (left) vs smooth gradient GI (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_ERROR_TOL": 0.55,
    "P_CACHE_VIS": 1,
    "P_GI_GAIN": 1.25,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_ERROR_TOL = 0.55; // P Error Tolerance a [0.2, 1.2, 0.03]\nconst float P_CACHE_VIS = 1.0; // P Cache Overlay [0.0, 1.5, 0.05]\nconst float P_GI_GAIN = 1.25; // P Indirect Gain [0.3, 2.2, 0.05]\nconst float P_SPLIT = 0.48; // P Records / Smooth Split [0.0, 1.0, 0.02]\n\nfloat hash31(vec3 p) {\n  p = fract(p * 0.1031);\n  p += dot(p, p.yzx + 33.33);\n  return fract((p.x + p.y) * p.z);\n}\n\nvec3 hash33(vec3 p) {\n  p = fract(p * vec3(0.1031, 0.1030, 0.0973));\n  p += dot(p, p.yxz + 33.33);\n  return fract((p.xxy + p.yxx) * p.zyx);\n}\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\nvec2 mapScene(vec3 p) {\n  vec2 res = vec2(p.y + 0.85, 1.0);\n  float dC = 1.05 - p.y; if (dC < res.x) res = vec2(dC, 1.0);\n  float dB = p.z + 1.38; if (dB < res.x) res = vec2(dB, 1.0);\n  float dL = p.x + 1.28; if (dL < res.x) res = vec2(dL, 2.0);\n  float dR = 1.28 - p.x; if (dR < res.x) res = vec2(dR, 3.0);\n\n  float dArch = sdBox(p - vec3(-0.35, -0.35, -0.35), vec3(0.28, 0.50, 0.28)) - 0.02;\n  if (dArch < res.x) res = vec2(dArch, 4.0);\n  float dSph = length(p - vec3(0.42, -0.49, -0.10)) - 0.36;\n  if (dSph < res.x) res = vec2(dSph, 4.0);\n  return res;\n}\n\nvec3 calcNormal(vec3 p) {\n  vec2 e = vec2(0.0022, 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// Harmonic-mean distance R_i proxy: shrinks tightly in creases/corners, grows wide on open walls\nfloat harmonicRadius(vec3 p, vec3 n) {\n  float h1 = max(mapScene(p + n * 0.16).x, 0.02);\n  float h2 = max(mapScene(p + n * 0.42).x, 0.04);\n  float crease = clamp((h1 / 0.16) * 0.55 + (h2 / 0.42) * 0.45, 0.18, 1.0);\n  return 0.10 + 0.26 * crease;\n}\n\nvec3 evalIndirect(vec3 p, vec3 n) {\n  float wL = clamp(1.0 - (p.x + 1.28) * 0.52, 0.0, 1.0);\n  float wR = clamp(1.0 - (1.28 - p.x) * 0.52, 0.0, 1.0);\n  vec3 warm = vec3(0.95, 0.36, 0.14) * wL * (0.45 + 0.55 * max(dot(n, vec3(1.0, 0.0, 0.0)), 0.0));\n  vec3 cool = vec3(0.16, 0.74, 0.88) * wR * (0.45 + 0.55 * max(dot(n, vec3(-1.0, 0.0, 0.0)), 0.0));\n  vec3 sky = vec3(0.32, 0.34, 0.38) * (0.45 + 0.55 * max(n.y, 0.0));\n  float ao = clamp(0.38 + 0.62 * mapScene(p + n * 0.25).x / 0.25, 0.18, 1.0);\n  return (warm * 0.58 + cool * 0.58 + sky * 0.48) * ao;\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 isSmooth = screenX >= split;\n  float errTol = clamp(P_ERROR_TOL, 0.18, 1.25);\n  float cacheVis = clamp(P_CACHE_VIS, 0.0, 1.5);\n  float giGain = clamp(P_GI_GAIN, 0.2, 2.4);\n\n  vec3 ro = vec3(0.0, 0.02, 2.34);\n  vec3 rd = normalize(vec3(uv, -1.50));\n\n  vec3 col = vec3(0.02);\n  float t = 0.0;\n  float matId = 0.0;\n  bool hit = false;\n  for (int i = 0; i < 64; i++) {\n    vec2 h = mapScene(ro + rd * t);\n    if (h.x < 0.002) { hit = true; matId = h.y; break; }\n    t += h.x;\n    if (t > 9.0) break;\n  }\n\n  if (hit) {\n    vec3 p = ro + rd * t;\n    vec3 n = calcNormal(p);\n\n    vec3 alb = vec3(0.78, 0.78, 0.80);\n    if (matId > 1.5 && matId < 2.5) alb = vec3(0.92, 0.34, 0.16);\n    if (matId > 2.5 && matId < 3.5) alb = vec3(0.14, 0.72, 0.84);\n\n    vec3 lPos = vec3(0.18 * sin(iTime * 0.65), 0.94, -0.25);\n    vec3 lDir = normalize(lPos - p);\n    float direct = max(dot(n, lDir), 0.0) * 0.58;\n    vec3 smoothGI = evalIndirect(p, n) * giGain;\n\n    // Adaptive spatial hash placing Ward irradiance cache samples denser near creases (small R_i)\n    float rHarm = harmonicRadius(p, n) * (0.55 + 0.85 * errTol);\n    float cellSz = max(rHarm * 0.92, 0.08);\n    vec3 cid = floor(p / cellSz);\n\n    float minDist = 1e5;\n    float ringVis = 0.0;\n    vec3 nearestIrr = smoothGI;\n    float nearestRad = rHarm;\n\n    // Search 3x3x3 neighborhood of 3D cache cells to find nearest surface records & Ward weights\n    vec3 wIrr = vec3(0.0);\n    float wSum = 0.0;\n    for (int dz = -1; dz <= 1; dz++) {\n      for (int dy = -1; dy <= 1; dy++) {\n        for (int dx = -1; dx <= 1; dx++) {\n          vec3 nb = cid + vec3(float(dx), float(dy), float(dz));\n          vec3 rnd = hash33(nb);\n          vec3 cPos = (nb + 0.15 + 0.70 * rnd) * cellSz;\n          // Project record onto tangent plane of p so it sits on the surface\n          vec3 cSurf = cPos - n * dot(cPos - p, n);\n          float d = length(p - cSurf);\n          float rValid = cellSz * 0.72;\n          if (d < minDist) {\n            minDist = d;\n            nearestIrr = evalIndirect(cSurf, n) * giGain;\n            nearestRad = rValid;\n          }\n          // Ward weighting function w_i(p) = 1 / (d / R_i + sqrt(1 - n.n_i)) - 1/a\n          float w = max(1.0 / (d / max(rValid, 0.04) + 0.15) - 1.0 / (1.0 + errTol * 2.2), 0.0);\n          wIrr += evalIndirect(cSurf, n) * giGain * w;\n          wSum += w;\n          ringVis = max(ringVis, smoothstep(0.012, 0.003, abs(d - rValid * 0.65)));\n        }\n      }\n    }\n\n    vec3 interpGI = (wSum > 1e-4) ? (wIrr / wSum) : smoothGI;\n    vec3 finalGI = mix(nearestIrr, interpGI, isSmooth ? 0.92 : 0.35);\n\n    col = alb * (direct + finalGI);\n\n    if (!isSmooth) {\n      // Left: visualize cache sample centers (bright gold dots) & validity circles (cyan)\n      float dotMask = smoothstep(0.024, 0.011, minDist);\n      col = mix(col, vec3(0.22, 0.92, 1.0), ringVis * 0.42 * min(cacheVis, 1.2));\n      col = mix(col, vec3(1.0, 0.88, 0.32), dotMask * 0.92 * min(cacheVis, 1.2));\n    }\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": "irradiance-caching",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:53:28.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.7136,
      "std": 0.1353,
      "min": 0.1255,
      "max": 0.9014
     },
     "image": "/specimens/irradiance-caching-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "photon-mapping",
  "lightmap-baking",
  "surfel-gi",
  "irradiance-volumes"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "photon-mapping",
   "why": "Final gathering at cache points uses photon-map irradiance rather than tracing every ray."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/gi/irradiance-caching.json"
 },
 "solves": [
  "path tracing diffuse bounce is slow and noisy",
  "indirect lighting computed too often in smooth areas",
  "speed up offline gi for diffuse interiors",
  "reuse indirect light between nearby surface points"
 ],
 "authority": "Ward et al., 1988",
 "url": "https://texelatlas.vercel.app/t/irradiance-caching/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 89.1,
  "tier": "extreme",
  "p50_ms": 2.7621,
  "p95_ms": 2.8965,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 36,
  "preset_id": "irradiance-caching-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 80.71,
   "tier": "extreme",
   "p50_ms": 1.2833,
   "p95_ms": 1.2851,
   "speedup_x": 2.15,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 61.3,
   "tier": "extreme",
   "p50_ms": 1.7654,
   "p95_ms": 1.7669,
   "speedup_x": 1.56,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}