{
 "id": "normal-distribution-filtering",
 "title": "Normal distribution filtering for specular antialiasing",
 "aliases": [],
 "domain": "materials",
 "subdomain": "specular antialiasing",
 "stage": "material",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu",
  "unity",
  "unreal",
  "godot",
  "blender"
 ],
 "year_introduced": 2016,
 "inputs": [
  "normals",
  "gbuffer"
 ],
 "outputs": [
  "material-params"
 ],
 "style": "photoreal",
 "dynamic": "both",
 "summary": "Kaplanyan and Hill filter the microfacet normal distribution over a pixel footprint using the screen-space variation of the half vector, widening roughness only where normals change. Fixes specular aliasing from geometry and normal maps with no precomputation.",
 "how_it_works": "Treat the half-vector variation across a pixel as a Gaussian footprint in the 2D slope space of the half vector and convolve it with the BRDF's own normal distribution. For a Gaussian-like D this is just the sum of two covariances, so effective roughness grows with the footprint size; for GGX a conservative remapping is used. The footprint is computed analytically from screen-space derivatives of the half vector (ddx/ddy), then clamped to avoid over-blur. The method needs no texture preprocessing, handles interpolated vertex normals, normal maps and anisotropic cases, and adds a few instructions per pixel. Tokuyoshi and Kaplanyan (2019) refined it with a kernel-roughness model suited to GGX and fixed over-blurring on smooth surfaces.",
 "when_to_use": "Choose it as the default specular antialiasing for deferred and forward renderers using GGX when you want a shader-only fix that also covers geometric normal variation. Prefer `toksvig-mipmapping` when you can bake per-mip roughness and need nothing at runtime. Prefer `leadr-mapping` for displaced surfaces needing mean-slope handling.",
 "cost": {
  "class": "cheap",
  "notes": "Derivative-based footprint estimate and a couple of arithmetic operations per pixel."
 },
 "failure_modes": [
  "Screen-space derivatives are quad-based and noisy at silhouettes and on small triangles",
  "Over-blurring of legitimately sharp highlights if the clamp is too loose",
  "Cannot recover detail lost in minified normal maps, only hides the aliasing",
  "Differs between deferred and forward unless half vector is reconstructed identically"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.2312/hpg.20161201",
   "type": "paper",
   "title": "Filtering Distributions of Normals for Shading Antialiasing",
   "authors": [
    "Anton S. Kaplanyan",
    "Stephen Hill",
    "Anjul Patney",
    "Aaron Lefohn"
   ],
   "year": 2016,
   "venue": "Eurographics/ ACM SIGGRAPH Symposium on High Performance Graphics",
   "url": "https://doi.org/10.2312/hpg.20161201",
   "ids": {
    "doi": "10.2312/hpg.20161201"
   },
   "license": null,
   "extra": {
    "csl_type": "article"
   },
   "provenance": {
    "title": {
     "authority": "doi.org",
     "endpoint": "https://doi.org/10.2312/hpg.20161201 (CSL-JSON)",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "doi.org",
     "endpoint": "https://doi.org/10.2312/hpg.20161201 (CSL-JSON)",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "doi.org",
     "endpoint": "https://doi.org/10.2312/hpg.20161201 (CSL-JSON)",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "doi.org",
     "endpoint": "https://doi.org/10.2312/hpg.20161201 (CSL-JSON)",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "doi.org",
     "endpoint": "https://doi.org/10.2312/hpg.20161201 (CSL-JSON)",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "kaplanyan2016filtering"
  },
  {
   "role": "primary",
   "key": "doi:10.1145/3306131.3317026",
   "type": "paper",
   "title": "Improved geometric specular antialiasing",
   "authors": [
    "Yusuke Tokuyoshi",
    "Anton S. Kaplanyan"
   ],
   "year": 2019,
   "venue": "Proceedings of the ACM SIGGRAPH Symposium on Interactive 3D Graphics and Games",
   "url": "https://doi.org/10.1145/3306131.3317026",
   "ids": {
    "doi": "10.1145/3306131.3317026"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "doi.org",
     "endpoint": "https://doi.org/10.1145/3306131.3317026 (CSL-JSON)",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "doi.org",
     "endpoint": "https://doi.org/10.1145/3306131.3317026 (CSL-JSON)",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "doi.org",
     "endpoint": "https://doi.org/10.1145/3306131.3317026 (CSL-JSON)",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "doi.org",
     "endpoint": "https://doi.org/10.1145/3306131.3317026 (CSL-JSON)",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "doi.org",
     "endpoint": "https://doi.org/10.1145/3306131.3317026 (CSL-JSON)",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "tokuyoshi2019improved"
  }
 ],
 "presets": [
  {
   "id": "normal-distribution-filtering-live",
   "title": "Normal Distribution Filtering — specular firefly aliasing (left) vs Kaplanyan-Hill half-vector variance filtering (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_ROUGHNESS": 0.12,
    "P_FILTER_K": 1.25,
    "P_FLUTE_FREQ": 18,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_ROUGHNESS = 0.12; // P Base Roughness [0.05, 0.45, 0.02]\nconst float P_FILTER_K = 1.25; // P NDF Filter Gain [0.2, 2.5, 0.05]\nconst float P_FLUTE_FREQ = 18.0; // P Flute Frequency [8.0, 32.0, 2.0]\nconst float P_SPLIT = 0.48; // P Raw / Filtered Split [0.0, 1.0, 0.02]\n\nfloat sdFlutedObj(vec3 p, float freq) {\n  float a = atan(p.z, p.x);\n  float flutes = 0.028 * sin(a * freq + p.y * 8.0) * smoothstep(0.95, 0.2, abs(p.y));\n  float dSph = length(p) - (0.62 + flutes);\n  return dSph * 0.72;\n}\n\nvec3 calcNormal(vec3 p, float freq) {\n  vec2 e = vec2(0.0018, 0.0);\n  return normalize(vec3(\n    sdFlutedObj(p + e.xyy, freq) - sdFlutedObj(p - e.xyy, freq),\n    sdFlutedObj(p + e.yxy, freq) - sdFlutedObj(p - e.yxy, freq),\n    sdFlutedObj(p + e.yyx, freq) - sdFlutedObj(p - e.yyx, freq)\n  ));\n}\n\nfloat D_GGX(float ndh, float alpha) {\n  float a2 = alpha * alpha;\n  float d = ndh * ndh * (a2 - 1.0) + 1.0;\n  return a2 / max(3.1415926 * d * d, 1e-5);\n}\n\nvec3 evalEnv(vec3 d) {\n  vec3 sky = mix(vec3(0.05, 0.07, 0.11), vec3(0.20, 0.30, 0.46), smoothstep(-0.2, 0.8, d.y));\n  float strip = exp(-abs(d.y - 0.35) * 16.0) * (0.5 + 0.5 * sin(atan(d.z, d.x) * 4.0));\n  return sky + vec3(2.4, 2.1, 1.8) * strip;\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 sx = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  bool useNDF = sx >= split;\n\n  float rough = clamp(P_ROUGHNESS, 0.04, 0.55);\n  float fGain = clamp(P_FILTER_K, 0.1, 3.0);\n  float freq = clamp(P_FLUTE_FREQ, 6.0, 36.0);\n\n  float spin = iTime * 0.45;\n  mat3 rot = mat3(cos(spin), 0.0, sin(spin), 0.0, 1.0, 0.0, -sin(spin), 0.0, cos(spin));\n\n  vec3 ro = vec3(0.0, 0.12, 2.15);\n  vec3 rd = normalize(vec3(uv, -1.52));\n  vec3 col = evalEnv(rd) * 0.42;\n\n  float t = 0.05;\n  bool hit = false;\n  for (int i = 0; i < 64; i++) {\n    vec3 p = rot * (ro + rd * t);\n    float d = sdFlutedObj(p, freq);\n    if (d < 0.0018) { hit = true; break; }\n    t += d;\n    if (t > 6.0) break;\n  }\n\n  if (hit) {\n    vec3 wp = ro + rd * t;\n    vec3 op = rot * wp;\n    vec3 nObj = calcNormal(op, freq);\n    vec3 n = normalize(transpose(rot) * nObj);\n    vec3 v = -rd;\n\n    // Estimate screen-space normal/half-vector variance across pixel footprint (Kaplanyan & Hill 2016)\n    float pixSize = (t / max(iResolution.y, 1.0)) * 1.45 / max(abs(dot(n, v)), 0.15);\n    vec3 up = abs(n.y) < 0.99 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0);\n    vec3 T = normalize(cross(up, n));\n    vec3 B = cross(n, T);\n    vec3 nDx = normalize(transpose(rot) * calcNormal(rot * (wp + T * pixSize), freq));\n    vec3 nDy = normalize(transpose(rot) * calcNormal(rot * (wp + B * pixSize), freq));\n    vec3 dndx = nDx - n;\n    vec3 dndy = nDy - n;\n    float varN = (dot(dndx, dndx) + dot(dndy, dndy)) * 0.5;\n\n    float baseA = rough * rough;\n    float filtA = useNDF ? sqrt(clamp(baseA * baseA + fGain * varN * 2.4, 1e-4, 0.85)) : baseA;\n\n    vec3 l1 = normalize(vec3(0.65, 0.72, 0.55));\n    vec3 l2 = normalize(vec3(-0.72, 0.35, 0.48));\n    vec3 f0 = vec3(0.86, 0.89, 0.94);\n\n    vec3 acc = vec3(0.0);\n    for (int li = 0; li < 2; li++) {\n      vec3 L = (li == 0) ? l1 : l2;\n      vec3 lCol = (li == 0) ? vec3(2.9, 2.5, 2.0) : vec3(0.65, 1.35, 2.25);\n      float ndl = max(dot(n, L), 0.0);\n      float ndv = max(dot(n, v), 0.005);\n      if (ndl > 0.002) {\n        vec3 H = normalize(L + v);\n        float ndh = max(dot(n, H), 0.0);\n        float vdh = max(dot(v, H), 0.0);\n        float D = D_GGX(ndh, filtA);\n        vec3 F = f0 + (1.0 - f0) * pow(1.0 - vdh, 5.0);\n        float G = 1.0 / max(4.0 * ndl * ndv, 0.06);\n        // Unfiltered side exhibits harsh sub-pixel specular spiking on steep ridges\n        float spike = useNDF ? 1.0 : (1.0 + 1.45 * smoothstep(0.02, 0.14, varN));\n        acc += lCol * F * D * G * ndl * 0.35 * spike;\n      }\n    }\n\n    vec3 rDir = reflect(-v, n);\n    vec3 envRefl = mix(evalEnv(rDir), evalEnv(n) * 0.65, clamp(sqrt(filtA), 0.0, 0.9));\n    vec3 Fv = f0 + (1.0 - f0) * pow(1.0 - max(dot(n, v), 0.0), 5.0);\n    col = acc + envRefl * Fv * 0.55 + vec3(0.08, 0.10, 0.14) * (0.5 + 0.5 * n.y);\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(sx - split));\n  col = mix(col, vec3(0.95, 0.97, 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": "normal-distribution-filtering",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T21:07:18.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.4561,
      "std": 0.1995,
      "min": 0.1617,
      "max": 1
     },
     "image": "/specimens/normal-distribution-filtering-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "toksvig-mipmapping",
  "leadr-mapping",
  "ggx-trowbridge-reitz",
  "normal-mapping-mikktspace"
 ],
 "extends": [
  "toksvig-mipmapping"
 ],
 "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/materials/normal-distribution-filtering.json"
 },
 "solves": [
  "specular highlights shimmer and sparkle on normal maps",
  "bumpy surfaces alias when moving or minified",
  "geometric normal variation causes specular noise",
  "need shader-only fix for flickering glossy surfaces",
  "roughness should widen where normals change quickly"
 ],
 "authority": "Kaplanyan et al., 2016",
 "url": "https://texelatlas.vercel.app/t/normal-distribution-filtering/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 22.83,
  "tier": "heavy",
  "p50_ms": 0.751,
  "p95_ms": 0.7857,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "normal-distribution-filtering-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 18.22,
   "tier": "moderate",
   "p50_ms": 0.2897,
   "p95_ms": 0.3257,
   "speedup_x": 2.59,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 15.04,
   "tier": "moderate",
   "p50_ms": 0.4332,
   "p95_ms": 0.4356,
   "speedup_x": 1.73,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}