{
 "id": "next-event-estimation",
 "title": "Next-event estimation (direct light sampling)",
 "aliases": [
  "NEE",
  "direct lighting sampling",
  "explicit light sampling",
  "shadow rays"
 ],
 "domain": "gi",
 "subdomain": "Monte Carlo sampling",
 "stage": "lighting",
 "realtime": "interactive",
 "platforms": [
  "cuda",
  "desktop-gpu",
  "cpu",
  "blender",
  "webgpu",
  "python"
 ],
 "year_introduced": 1996,
 "inputs": [
  "mesh",
  "material-params"
 ],
 "outputs": [
  "radiance"
 ],
 "style": "photoreal",
 "dynamic": "both",
 "summary": "At every path vertex, trace a shadow ray toward a sampled point on a light instead of hoping a random bounce hits it. Direct illumination becomes low-variance and small lights stop producing fireflies.",
 "how_it_works": "A naive path tracer finds light only when a random bounce happens to hit an emitter, so small bright lights give extremely noisy images. Next-event estimation splits the rendering equation at each vertex: the direct term is estimated by choosing a point on a light, evaluating the BSDF and geometry for that direction, and casting a shadow ray to check visibility.\n\nThe indirect term continues with the usual BSDF-sampled bounce. To avoid counting emission twice, the continued path must not add emitted light from a surface it hits directly after an NEE-sampled vertex, or the two strategies must be combined with [multiple-importance-sampling](multiple-importance-sampling) weights. The light is chosen by area, power or, with many lights, by a hierarchy ([light-bvh-many-lights](light-bvh-many-lights)); the sample pdf converts from area to solid angle with the usual `distance^2 / cos` factor.\n\nSpecular chains are the exception: a delta BSDF cannot be sampled by NEE, so those vertices still rely on BSDF sampling and emission hits.",
 "when_to_use": "Choose it in any path tracer that renders small or bright lights, area lights or HDRI environments (with importance sampling); it is the first change that makes a path tracer converge usably.\n\nPrefer [multiple-importance-sampling](multiple-importance-sampling) on top of it when the BSDF is glossy and lights are large, so neither strategy fails alone. Prefer [restir-di](restir-di) when there are thousands of lights and one sample per pixel. Prefer [bidirectional-path-tracing](bidirectional-path-tracing) or [metropolis-light-transport](metropolis-light-transport) when the shadow ray is blocked by glass and caustics matter. Plain [path-tracing](path-tracing) without NEE is acceptable only for large, uniformly bright environments.",
 "cost": {
  "class": "medium",
  "notes": "One extra shadow ray and BSDF evaluation per path vertex; usually far cheaper than the variance it removes."
 },
 "failure_modes": [
  "Double-counted emission when the BSDF-sampled bounce is allowed to add light already estimated by NEE",
  "Black shadows behind glass or water because shadow rays cannot pass through refractive materials",
  "Wrong pdf conversion between area and solid angle gives lights that are too bright or too dim by a distance-squared factor",
  "Poor light selection with many lights makes noise worse than uniform sampling in large scenes",
  "Light samples that cross the horizon of a shading normal produce negative or NaN contributions"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://pbr-book.org/4ed/Light_Transport_I_Surface_Reflection/A_Better_Path_Tracer",
   "type": "web",
   "title": "A Better Path Tracer",
   "authors": [
    "Matt Pharr",
    "Wenzel Jakob",
    "Greg Humphreys"
   ],
   "year": null,
   "venue": "Physically Based Rendering 4th ed., 13.4",
   "url": "https://pbr-book.org/4ed/Light_Transport_I_Surface_Reflection/A_Better_Path_Tracer",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20260917015157/https://pbr-book.org/4ed/Light_Transport_I_Surface_Reflection/A_Better_Path_Tracer",
     "timestamp": "20260917015157"
    },
    "page_title": "A Better Path Tracer",
    "host": "pbr-book.org",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://pbr-book.org/4ed/Light_Transport_I_Surface_Reflection/A_Better_Path_Tracer → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://pbr-book.org/4ed/Light_Transport_I_Surface_Reflection/A_Better_Path_Tracer → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "pharrbetter"
  },
  {
   "role": "implementation",
   "key": "gh:mmp/pbrt-v4",
   "type": "code",
   "title": "mmp/pbrt-v4",
   "description": "Source code to pbrt, the ray tracer described in the forthcoming 4th edition of the \"Physically Based Rendering: From Theory to Implementation\" book.",
   "authors": [
    "mmp"
   ],
   "year": 2017,
   "venue": "GitHub",
   "url": "https://github.com/mmp/pbrt-v4",
   "ids": {
    "repo": "mmp/pbrt-v4"
   },
   "license": "Apache-2.0",
   "extra": {
    "stars": 3761,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-15T22:47:49Z",
    "language": "C++",
    "homepage": "https://pbrt.org",
    "license_name": "Apache License 2.0"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mmp/pbrt-v4",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mmp/pbrt-v4",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mmp/pbrt-v4",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mmp/pbrt-v4",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mmp/pbrt-v4",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mmp/pbrt-v4",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "mmp2017pbrt"
  },
  {
   "role": "implementation",
   "key": "gh:nvidiagameworks/falcor",
   "type": "code",
   "title": "NVIDIAGameWorks/Falcor",
   "description": "Real-Time Rendering Framework",
   "authors": [
    "NVIDIAGameWorks"
   ],
   "year": 2017,
   "venue": "GitHub",
   "url": "https://github.com/NVIDIAGameWorks/Falcor",
   "ids": {
    "repo": "NVIDIAGameWorks/Falcor"
   },
   "license": "NOASSERTION",
   "extra": {
    "stars": 3285,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-09-21T15:12:00Z",
    "language": "C++",
    "homepage": "https://developer.nvidia.com/falcor",
    "license_name": "Other"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/NVIDIAGameWorks/Falcor",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/NVIDIAGameWorks/Falcor",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/NVIDIAGameWorks/Falcor",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/NVIDIAGameWorks/Falcor",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/NVIDIAGameWorks/Falcor",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/NVIDIAGameWorks/Falcor",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "nvidiagameworks2017nvidiagameworks"
  }
 ],
 "presets": [
  {
   "id": "next-event-estimation-live",
   "title": "Next-Event Estimation — blind BSDF light hits (left) vs explicit direct-light shadow rays (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_LIGHT_SIZE": 0.22,
    "P_NOISE_GAIN": 1,
    "P_BOUNCE_INT": 1.1,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_LIGHT_SIZE = 0.22; // P Emitter Radius [0.08, 0.55, 0.02]\nconst float P_NOISE_GAIN = 1.0; // P BSDF Variance [0.2, 2.0, 0.05]\nconst float P_BOUNCE_INT = 1.1; // P Light Power [0.4, 2.2, 0.05]\nconst float P_SPLIT = 0.48; // P Blind / NEE Split [0.0, 1.0, 0.02]\n\nfloat hash21(vec2 p) {\n  p = fract(p * vec2(127.1, 311.7));\n  p += dot(p, p + 34.5);\n  return fract(p.x * p.y);\n}\n\nvec2 hash22(vec2 p) {\n  vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));\n  p3 += dot(p3, p3.yzx + 33.33);\n  return fract((p3.xx + p3.yz) * p3.zy);\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.0 - p.y; if (dC < res.x) res = vec2(dC, 1.0);\n  float dB = p.z + 1.45; if (dB < res.x) res = vec2(dB, 1.0);\n  float dL = p.x + 1.35; if (dL < res.x) res = vec2(dL, 1.0);\n  float dR = 1.35 - p.x; if (dR < res.x) res = vec2(dR, 1.0);\n\n  // Sculpted sphere + pedestal casting crisp penumbra from the small area light\n  float dSph = length(p - vec3(0.0, -0.35, -0.22)) - 0.36;\n  if (dSph < res.x) res = vec2(dSph, 2.0);\n  float dCol = sdBox(p - vec3(-0.62, -0.45, -0.58), vec3(0.22, 0.40, 0.22)) - 0.02;\n  if (dCol < res.x) res = vec2(dCol, 3.0);\n  float dCol2 = sdBox(p - vec3(0.66, -0.55, -0.45), vec3(0.24, 0.30, 0.24)) - 0.02;\n  if (dCol2 < res.x) res = vec2(dCol2, 3.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\nfloat softShadow(vec3 ro, vec3 rd, float maxT, float k) {\n  float res = 1.0;\n  float t = 0.03;\n  for (int i = 0; i < 24; i++) {\n    float h = mapScene(ro + rd * t).x;\n    res = min(res, k * h / max(t, 1e-4));\n    t += clamp(h, 0.02, 0.18);\n    if (res < 0.01 || t > maxT) break;\n  }\n  return clamp(res, 0.0, 1.0);\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 isNEE = screenX >= split;\n  float lRad = clamp(P_LIGHT_SIZE, 0.07, 0.58);\n  float noiseGain = clamp(P_NOISE_GAIN, 0.15, 2.2);\n  float power = clamp(P_BOUNCE_INT, 0.3, 2.4);\n\n  vec3 lPos = vec3(0.32 * sin(iTime * 0.7), 0.96, -0.25);\n\n  vec3 ro = vec3(0.0, 0.04, 2.38);\n  vec3 rd = normalize(vec3(uv, -1.52));\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    if (p.y > 0.98 && length(p.xz - lPos.xz) < lRad) {\n      col = vec3(3.8, 3.3, 2.6) * power;\n    } else {\n      vec3 alb = vec3(0.68, 0.70, 0.74);\n      if (matId > 1.5 && matId < 2.5) alb = vec3(0.86, 0.56, 0.24);\n      if (matId > 2.5) alb = vec3(0.30, 0.62, 0.76);\n\n      vec3 lVec = lPos - p;\n      float lDist = length(lVec);\n      vec3 lDir = lVec / max(lDist, 1e-4);\n      float ndl = max(dot(n, lDir), 0.0);\n      float cosL = max(dot(vec3(0.0, -1.0, 0.0), -lDir), 0.0);\n\n      // Solid angle subtended by the small area light omega = A * cosL / d^2\n      float area = 3.1415926 * lRad * lRad;\n      float solidAngle = clamp(area * cosL / (lDist * lDist + 0.08), 0.004, 1.2);\n      float sh = softShadow(p + n * 0.012, lDir, lDist - 0.05, 2.2 / max(lRad, 0.08));\n\n      // Ground-truth direct illumination (constant total radiant power regardless of light radius)\n      vec3 directClean = alb * vec3(2.8, 2.45, 1.95) * power * ndl * cosL * sh / (lDist * lDist + 0.45);\n      vec3 amb = alb * vec3(0.08, 0.10, 0.13) * clamp(0.45 + 0.55 * mapScene(p + n * 0.24).x / 0.24, 0.2, 1.0);\n\n      if (!isNEE) {\n        // Left: Naive unguided BSDF sampling. Hit probability scales with solidAngle / pi!\n        // Smaller light => tiny hit chance => huge weight (1 / pHit) fireflies & mostly black pixels!\n        float pHit = clamp(solidAngle * 1.45 / max(noiseGain, 0.2), 0.04, 0.85);\n        vec2 rnd = hash22(floor(fragCoord * 0.8) + fract(iTime * 6.7) * 23.1);\n        float didHit = step(1.0 - pHit, rnd.x);\n        float estimator = didHit / max(pHit, 0.06);\n        float spike = pow(rnd.y, 4.0) * 1.8 * didHit / max(lRad * 3.0, 0.25);\n        col = amb + directClean * (estimator * 0.72 + spike * 0.35);\n      } else {\n        // Right: Explicit Next-Event Estimation shadow ray samples the emitter directly with low variance\n        float jitter = (hash21(floor(fragCoord) + fract(iTime * 3.1) * 17.0) - 0.5) * 0.06 * lRad;\n        col = amb + directClean * (1.0 + jitter);\n      }\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": "reduced-sandbox",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "next-event-estimation",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:53:32.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.3662,
      "std": 0.1854,
      "min": 0.0844,
      "max": 0.9916
     },
     "image": "/specimens/next-event-estimation-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "path-tracing",
  "multiple-importance-sampling",
  "restir-di",
  "light-bvh-many-lights",
  "ray-traced-shadows",
  "bidirectional-path-tracing"
 ],
 "extends": [
  "path-tracing"
 ],
 "supersedes": [],
 "combines_with": [
  {
   "id": "multiple-importance-sampling",
   "why": "NEE and BSDF sampling each estimate the direct term; MIS weights blend them so whichever strategy has the better pdf dominates."
  },
  {
   "id": "light-bvh-many-lights",
   "why": "Supplies the light-selection pdf so NEE scales to scenes with many emitters."
  },
  {
   "id": "owen-scrambled-sobol-sampling",
   "why": "Low-discrepancy points choose the light and surface sample per vertex, reducing the remaining variance."
  }
 ],
 "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/next-event-estimation.json"
 },
 "solves": [
  "path tracer is extremely noisy with small lights",
  "fireflies from tiny bright emitters",
  "direct lighting converges too slowly",
  "light only found when random bounces hit it"
 ],
 "authority": "Pharr et al.",
 "url": "https://texelatlas.vercel.app/t/next-event-estimation/",
 "execution_fidelity": "reduced-sandbox",
 "fidelity_label": "Analytic Sandbox",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 35.89,
  "tier": "heavy",
  "p50_ms": 1.1127,
  "p95_ms": 1.1983,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 36,
  "preset_id": "next-event-estimation-live",
  "execution_fidelity": "reduced-sandbox",
  "fidelity_label": "Analytic Sandbox",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 37.13,
   "tier": "heavy",
   "p50_ms": 0.5903,
   "p95_ms": 0.6018,
   "speedup_x": 1.88,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 22.41,
   "tier": "heavy",
   "p50_ms": 0.6455,
   "p95_ms": 0.6475,
   "speedup_x": 1.72,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}