{
 "id": "joint-non-local-means-denoising",
 "title": "Joint Non-Local Means (JNLM) Path-Tracing Denoiser",
 "aliases": [
  "JNLM",
  "non-local means Monte Carlo denoising",
  "feature-guided NLM denoising"
 ],
 "domain": "post",
 "subdomain": "denoising",
 "stage": "post",
 "realtime": "interactive",
 "platforms": [
  "webgpu",
  "webgl2",
  "desktop-gpu",
  "cuda",
  "cpu"
 ],
 "year_introduced": 2012,
 "inputs": [
  "hdr-image",
  "gbuffer",
  "normals",
  "depth"
 ],
 "outputs": [
  "hdr-image"
 ],
 "style": "photoreal",
 "dynamic": "both",
 "summary": "Denoises Monte Carlo path-traced images by averaging pixels whose surrounding radiance patches and auxiliary G-buffer features (albedo, normal, depth) match under variance-normalized squared distance, requiring zero trained weights.",
 "how_it_works": "Standard bilateral filters compare single pixels, which breaks down under heavy Monte Carlo noise because individual noisy samples cannot reliably distinguish edges from variance. Joint Non-Local Means (JNLM) instead compares small **spatial patches** (for example 3×3 or 7×7 neighborhoods) around a center pixel `p` and each candidate neighbor `q` inside a larger search window.\n\nThe squared patch distance in color space is normalized by the **estimated per-pixel sample variance** (tracked across progressive path-tracing iterations or dual half-buffers) so high-variance regions filter more aggressively while converged pixels stay sharp. To prevent blurring across geometric discontinuities and fine textures, JNLM combines the color patch distance with **joint feature weights** computed from noise-free auxiliary AOVs—surface albedo, shading normal, and world depth—taking the strictest similarity criterion across channels.\n\nEach pixel's denoised radiance is the weighted average of its search window. Because the entire algorithm is analytic and needs no neural network weights, it runs cleanly on both WebGPU and WebGL2 with predictable memory use and optional adaptive sampling feedback.",
 "when_to_use": "Choose it when building a lightweight or browser-based progressive path tracer that needs clean, edge-preserving denoising on both WebGPU and WebGL2 without bundling tens of megabytes of neural network weights.\n\nPrefer [open-image-denoise](open-image-denoise) when you can afford neural weights and need crisp reconstruction at 1–4 samples per pixel during interactive camera motion. Prefer [svgf](svgf) or [nvidia-nrd-relax-reblur](nvidia-nrd-relax-reblur) for real-time 60 fps games with motion vectors and temporal reprojection. Prefer [atrous-wavelet-filter](atrous-wavelet-filter) when a tiny fixed-cost spatial pass is required.",
 "cost": {
  "class": "heavy",
  "notes": "Search-window times patch-size comparisons scale quadratically in window radius; interactive at moderate radii on GPU via separable patch-distance passes, though heavier than à-trous wavelet filters."
 },
 "failure_modes": [
  "At very low sample counts (1–4 spp), noisy radiance patches can still smear glossy reflections, caustics, or soft shadow penumbrae",
  "Extreme fireflies bias both the sample variance estimate and patch distances unless clamped or filtered beforehand",
  "Over-smoothing of fine bump-mapped or procedural micro-details that are absent from the smooth shading normal and albedo AOVs",
  "Frame-to-frame temporal flickering on animated sequences because the filter is purely spatial without temporal reprojection"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/2366145.2366214",
   "type": "paper",
   "title": "Adaptive rendering with non-local means filtering",
   "authors": [
    "Fabrice Rousselle",
    "Claude Knaus",
    "Matthias Zwicker"
   ],
   "year": 2012,
   "venue": "ACM Transactions on Graphics",
   "url": "https://doi.org/10.1145/2366145.2366214",
   "ids": {
    "doi": "10.1145/2366145.2366214"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2366145.2366214",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2366145.2366214",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2366145.2366214",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2366145.2366214",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2366145.2366214",
     "checked_at": "2026-11-30T17:59:51.239Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-30T17:59:51.239Z",
   "title_sim": 1,
   "__bib": "rousselle2012adaptive"
  },
  {
   "role": "implementation",
   "key": "gh:pissang/mutracer",
   "type": "code",
   "title": "pissang/mutracer",
   "description": "WebGPU physically based path tracer",
   "authors": [
    "pissang"
   ],
   "year": 2026,
   "venue": "GitHub",
   "url": "https://github.com/pissang/mutracer",
   "ids": {
    "repo": "pissang/mutracer"
   },
   "license": "MIT",
   "extra": {
    "stars": 204,
    "stars_checked": "2026-11-30",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-30T01:28:58Z",
    "language": "TypeScript",
    "homepage": "https://mutracer.meimu-studio.com/",
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pissang/mutracer",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pissang/mutracer",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pissang/mutracer",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pissang/mutracer",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pissang/mutracer",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/pissang/mutracer",
     "checked_at": "2026-11-30T17:59:51.239Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-30T17:59:51.239Z",
   "title_sim": null,
   "__bib": "pissang2026pissang"
  }
 ],
 "presets": [
  {
   "id": "joint-non-local-means-denoising-live",
   "title": "Joint Non-Local Means (JNLM) — 1-spp noisy path-traced render (left) vs G-buffer patch-matched denoising (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_NOISE": 0.75,
    "P_PATCH_SIGMA": 0.45,
    "P_GBUF_GUIDE": 1,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_NOISE = 0.75; // P MC Noise Level [0.15, 1.4, 0.05]\nconst float P_PATCH_SIGMA = 0.45; // P Patch Bandwidth [0.15, 1.2, 0.05]\nconst float P_GBUF_GUIDE = 1.00; // P Aux G-Buffer Weight [0.0, 1.5, 0.05]\nconst float P_SPLIT = 0.48; // P Noisy / JNLM [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\n// Analytical Cornell-like studio G-buffer + clean radiance\nvoid evalGBuffer(vec2 uv, float t, out vec3 rad, out vec3 alb, out vec3 nrm, out float dep) {\n  vec3 ro = vec3(0.0, 0.25, 2.25);\n  vec3 rd = normalize(vec3(uv.x, uv.y - 0.06, -1.5));\n  vec3 lPos = vec3(0.75 * cos(t * 0.55), 1.15, 0.55 * sin(t * 0.55) + 0.25);\n\n  dep = 10.0;\n  nrm = vec3(0.0, 0.0, 1.0);\n  alb = vec3(0.18, 0.20, 0.24);\n  int id = 0;\n  vec3 hp = vec3(0.0);\n\n  // Back wall (z = -1.2)\n  float tb = (-1.2 - ro.z) / min(rd.z, -1e-4);\n  if (tb > 0.1 && tb < dep) {\n    dep = tb; id = 1; hp = ro + rd * tb; nrm = vec3(0.0, 0.0, 1.0);\n    float wallSplit = step(0.0, hp.x);\n    alb = mix(vec3(0.84, 0.26, 0.20), vec3(0.20, 0.64, 0.74), wallSplit);\n  }\n  // Floor (y = -0.55)\n  if (rd.y < -0.005) {\n    float tf = (-0.55 - ro.y) / rd.y;\n    if (tf > 0.1 && tf < dep) {\n      dep = tf; id = 2; hp = ro + rd * tf; nrm = vec3(0.0, 1.0, 0.0);\n      float chk = mod(floor(hp.x * 3.5) + floor(hp.z * 3.5), 2.0);\n      alb = mix(vec3(0.56, 0.54, 0.50), vec3(0.78, 0.76, 0.72), chk);\n    }\n  }\n  // Left matte-gold sphere\n  vec3 c1 = vec3(-0.34, -0.18, -0.10);\n  vec3 o1 = ro - c1;\n  float b1 = dot(o1, rd), d1 = b1 * b1 - (dot(o1, o1) - 0.37 * 0.37);\n  if (d1 > 0.0) {\n    float t1 = -b1 - sqrt(d1);\n    if (t1 > 0.1 && t1 < dep) {\n      dep = t1; id = 3; hp = ro + rd * t1; nrm = normalize(hp - c1);\n      alb = vec3(0.92, 0.72, 0.34);\n    }\n  }\n  // Right ceramic sphere\n  vec3 c2 = vec3(0.38, -0.25, 0.16);\n  vec3 o2 = ro - c2;\n  float b2 = dot(o2, rd), d2 = b2 * b2 - (dot(o2, o2) - 0.30 * 0.30);\n  if (d2 > 0.0) {\n    float t2 = -b2 - sqrt(d2);\n    if (t2 > 0.1 && t2 < dep) {\n      dep = t2; id = 4; hp = ro + rd * t2; nrm = normalize(hp - c2);\n      alb = vec3(0.88, 0.89, 0.92);\n    }\n  }\n\n  vec3 lDir = normalize(lPos - hp);\n  float lDist = length(lPos - hp);\n  // Soft analytical sphere shadows on floor & wall\n  float sh = 1.0;\n  if (id == 1 || id == 2) {\n    vec3 toC1 = c1 - hp;\n    float p1 = max(dot(toC1, lDir), 0.0);\n    float dist1 = length(toC1 - lDir * p1);\n    sh *= smoothstep(0.30, 0.44, dist1);\n    vec3 toC2 = c2 - hp;\n    float p2 = max(dot(toC2, lDir), 0.0);\n    float dist2 = length(toC2 - lDir * p2);\n    sh *= smoothstep(0.24, 0.38, dist2);\n  }\n  float ndl = max(dot(nrm, lDir), 0.0);\n  vec3 bounce = vec3(0.36, 0.16, 0.14) * max(-nrm.x, 0.0) + vec3(0.14, 0.30, 0.36) * max(nrm.x, 0.0) + vec3(0.16, 0.18, 0.22);\n  rad = alb * (vec3(1.45, 1.32, 1.14) * ndl * sh / (0.4 + 0.22 * lDist * lDist) + bounce);\n}\n\nvec3 noisyRadiance(vec2 fc, float noiseAmp, float t, out vec3 alb, out vec3 nrm, out float dep) {\n  vec2 uv = (fc - 0.5 * iResolution.xy) / iResolution.y;\n  vec3 rad;\n  evalGBuffer(uv, t, rad, alb, nrm, dep);\n  vec2 p = floor(fc);\n  float fr = floor(t * 10.0) * 0.13;\n  // Variance-scaled zero-mean Monte Carlo radiance noise (with occasional fireflies)\n  vec3 n = vec3(\n    hash21(p + vec2(11.3, fr * 31.7)),\n    hash21(p + vec2(47.9, fr * 19.3)),\n    hash21(p + vec2(83.1, fr * 53.1))\n  ) - 0.5;\n  float spike = step(0.96, hash21(p * 1.31 + vec2(fr * 17.0, 5.1))) * 1.8;\n  return max(rad + n * noiseAmp * (0.55 + 0.7 * rad) + vec3(spike) * noiseAmp * 0.35 * alb, 0.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  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  float noiseAmp = clamp(P_NOISE, 0.05, 1.6);\n  float sigma = clamp(P_PATCH_SIGMA, 0.10, 1.5);\n  float gGuide = clamp(P_GBUF_GUIDE, 0.0, 2.0);\n\n  vec3 cAlb, cNrm;\n  float cDep;\n  vec3 centerNoisy = noisyRadiance(fragCoord, noiseAmp, iTime, cAlb, cNrm, cDep);\n  vec3 col = centerNoisy;\n\n  if (screenX >= split) {\n    // Right: Joint Non-Local Means -- compare 3-tap micro-patches + G-buffer (albedo, normal, depth) across search window\n    vec3 acc = vec3(0.0);\n    float wSum = 0.0;\n    float stepPx = 2.0;\n\n    // Precompute tiny 3-point signature around center p\n    vec3 a0, n0; float d0;\n    vec3 cpL = noisyRadiance(fragCoord + vec2(-1.5, 0.0), noiseAmp, iTime, a0, n0, d0);\n    vec3 cpU = noisyRadiance(fragCoord + vec2(0.0, 1.5), noiseAmp, iTime, a0, n0, d0);\n\n    for (int iy = -2; iy <= 2; iy++) {\n      for (int ix = -2; ix <= 2; ix++) {\n        vec2 q = fragCoord + vec2(float(ix), float(iy)) * stepPx;\n        vec3 qAlb, qNrm;\n        float qDep;\n        vec3 qRad = noisyRadiance(q, noiseAmp, iTime, qAlb, qNrm, qDep);\n\n        // Radiance patch distance (3-tap proxy for 3x3 patch)\n        vec3 qpL = noisyRadiance(q + vec2(-1.5, 0.0), noiseAmp, iTime, a0, n0, d0);\n        vec3 qpU = noisyRadiance(q + vec2(0.0, 1.5), noiseAmp, iTime, a0, n0, d0);\n        float dPatch = dot(centerNoisy - qRad, centerNoisy - qRad)\n                     + dot(cpL - qpL, cpL - qpL)\n                     + dot(cpU - qpU, cpU - qpU);\n        float dColorNorm = dPatch / max(3.0 * sigma * sigma * (0.25 + noiseAmp * noiseAmp), 0.01);\n\n        // Joint G-buffer feature distance (normal + albedo + depth)\n        float dNrm = 1.0 - max(dot(cNrm, qNrm), 0.0);\n        float dAlb = dot(cAlb - qAlb, cAlb - qAlb);\n        float dDep = abs(cDep - qDep) / max(cDep, 0.25);\n        float dAux = (dNrm * 18.0 + dAlb * 14.0 + dDep * 24.0) * gGuide;\n\n        float w = exp(-dColorNorm - dAux);\n        acc += qRad * w;\n        wSum += w;\n      }\n    }\n    col = acc / max(wSum, 1e-4);\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": "joint-non-local-means-denoising",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T21:07:13.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.7193,
      "std": 0.1832,
      "min": 0,
      "max": 0.9734
     },
     "image": "/specimens/joint-non-local-means-denoising-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "open-image-denoise",
  "svgf",
  "atrous-wavelet-filter",
  "nvidia-nrd-relax-reblur",
  "path-tracing"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "path-tracing",
   "why": "Consumes progressive HDR radiance, sample variance, albedo, and normal AOVs to produce converged-looking renders at far fewer samples."
  },
  {
   "id": "owen-scrambled-sobol-sampling",
   "why": "Well-stratified low-discrepancy samples yield lower-noise variance estimates that improve NLM patch-similarity accuracy."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-30T17:59:51.239Z",
  "draft": "data/drafts/post/joint-non-local-means-denoising.json"
 },
 "solves": [
  "progressive path tracer stays grainy without an ai denoiser dependency",
  "simple bilateral blur smears fine textures and sharp caustic highlights",
  "denoise monte carlo renders using auxiliary normal albedo and depth guides",
  "need a pure compute shader denoiser that runs in webgpu with no ml weights",
  "remove high-variance path tracing noise while preserving repeating surface detail"
 ],
 "authority": "Rousselle et al., 2012",
 "url": "https://texelatlas.vercel.app/t/joint-non-local-means-denoising/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 152.02,
  "tier": "extreme",
  "p50_ms": 5.0016,
  "p95_ms": 5.2803,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "joint-non-local-means-denoising-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 143.49,
   "tier": "extreme",
   "p50_ms": 2.2815,
   "p95_ms": 2.3402,
   "speedup_x": 2.19,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 106.06,
   "tier": "extreme",
   "p50_ms": 3.0546,
   "p95_ms": 3.0669,
   "speedup_x": 1.64,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}