{
 "id": "vndf-sampling",
 "title": "Visible normal distribution sampling (GGX VNDF)",
 "aliases": [],
 "domain": "materials",
 "subdomain": "microfacet BRDF",
 "stage": "material",
 "realtime": "realtime",
 "platforms": [
  "webgpu",
  "desktop-gpu",
  "cuda",
  "cpu",
  "blender"
 ],
 "year_introduced": 2018,
 "inputs": [
  "material-params",
  "normals"
 ],
 "outputs": [
  "radiance"
 ],
 "style": "photoreal",
 "dynamic": "both",
 "summary": "Sample only the microfacet normals visible from the viewer, rather than the whole distribution, for lower variance and no wasted samples below the horizon. Heitz's ellipsoid-stretch derivation gives a compact exact sampler for GGX.",
 "how_it_works": "Naively importance-sampling D(h)*(n.h) produces half-vectors that may be backfacing or masked, wasting samples and creating variance at grazing angles. The visible normal distribution D_v(h) = G1(v) max(0, v.h) D(h) / (n.v) is the distribution of normals a viewer actually sees. Heitz shows that for GGX it is the projection of a hemisphere of an ellipsoid, so one can transform the view vector by the roughness, sample a disk-based hemisphere in the stretched space, and transform back, avoiding any rejection. The resulting reflected direction's pdf is D_v(h)/(4 v.h), which cancels the Fresnel-masking factors and leaves weights near F*G2/G1. Later variants (spherical caps, bounded VNDF) reduce cost further and avoid below-horizon directions.",
 "when_to_use": "Choose it for Monte Carlo evaluation of GGX in path tracers, ReSTIR-style integrators, and pre-filtering of specular IBL on the GPU. Prefer plain D(h) sampling only when matching legacy implementations. Pair with `smith-masking-shadowing` and `ggx-trowbridge-reitz` since the sampler assumes Smith masking.",
 "cost": {
  "class": "cheap",
  "notes": "A handful of arithmetic ops, one sqrt and one trigonometric pair per sample."
 },
 "failure_modes": [
  "Assumes Smith masking, so any other G invalidates the pdf",
  "Numerical precision near zero roughness causes degenerate half-vectors; clamp alpha",
  "Backfacing shading normals need the unbiased variant",
  "Samples reflected below the horizon still occur, so a visibility check is needed"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://jcgt.org/published/0007/04/01",
   "type": "web",
   "title": "Sampling the GGX Distribution of Visible Normals",
   "authors": [
    "Eric Heitz"
   ],
   "year": null,
   "venue": "JCGT 2018",
   "url": "https://jcgt.org/published/0007/04/01",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261117034942/https://jcgt.org/published/0007/04/01/",
     "timestamp": "20261117034942"
    },
    "page_title": null,
    "host": "jcgt.org",
    "title_confirmed_by_page": false,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://jcgt.org/published/0007/04/01 → 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": null,
   "__bib": "heitzsampling"
  }
 ],
 "presets": [
  {
   "id": "vndf-sampling-live",
   "title": "GGX VNDF Sampling — standard NDF high-variance grazing noise (left) vs low-variance visible-normal sampling (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_ROUGHNESS": 0.42,
    "P_NOISE_SCALE": 1,
    "P_LIGHT_SPIN": 0.55,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_ROUGHNESS = 0.42; // P GGX Roughness [0.12, 0.85, 0.02]\nconst float P_NOISE_SCALE = 1.0; // P MC Variance Vis [0.2, 1.8, 0.05]\nconst float P_LIGHT_SPIN = 0.55; // P Env Spin [0.0, 2.2, 0.05]\nconst float P_SPLIT = 0.48; // P NDF / VNDF 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\n// High-contrast studio environment for Monte Carlo GGX reflection comparison\nvec3 evalEnv(vec3 d, float spin) {\n  float ang = atan(d.z, d.x) + spin * 0.45;\n  vec3 sky = mix(vec3(0.04, 0.06, 0.11), vec3(0.18, 0.28, 0.45), smoothstep(-0.2, 0.7, d.y));\n  // Bright overhead softboxes & studio horizon strips\n  float box1 = smoothstep(0.26, 0.16, abs(sin(ang * 2.0))) * smoothstep(0.18, 0.52, d.y);\n  float sun = pow(max(dot(d, normalize(vec3(cos(spin * 0.45), 0.62, sin(spin * 0.45)))), 0.0), 32.0);\n  float rimStrip = exp(-abs(d.y - 0.08) * 18.0) * (0.4 + 0.6 * cos(ang * 3.0));\n  return sky + vec3(2.4, 2.2, 1.9) * box1 + vec3(3.2, 2.6, 1.8) * sun + vec3(0.4, 0.85, 1.0) * rimStrip * 0.7;\n}\n\n// Orthonormal tangent basis from surface normal n\nvoid buildBasis(vec3 n, out vec3 t, out vec3 b) {\n  vec3 up = (abs(n.y) < 0.99) ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0);\n  t = normalize(cross(up, n));\n  b = cross(n, t);\n}\n\n// Heitz 2018 exact GGX VNDF microfacet normal sampler in local tangent space (n = +Z)\nvec3 sampleGGX_VNDF(vec3 Ve, float alpha, vec2 u) {\n  vec3 Vh = normalize(vec3(alpha * Ve.x, alpha * Ve.y, max(Ve.z, 1e-4)));\n  float lensq = Vh.x * Vh.x + Vh.y * Vh.y;\n  vec3 T1 = (lensq > 1e-6) ? vec3(-Vh.y, Vh.x, 0.0) / sqrt(lensq) : vec3(1.0, 0.0, 0.0);\n  vec3 T2 = cross(Vh, T1);\n  float r = sqrt(clamp(u.x, 0.0, 0.9999));\n  float phi = 6.2831853 * u.y;\n  float t1 = r * cos(phi);\n  float t2 = r * sin(phi);\n  float s = 0.5 * (1.0 + Vh.z);\n  t2 = (1.0 - s) * sqrt(max(1.0 - t1 * t1, 0.0)) + s * t2;\n  vec3 Nh = t1 * T1 + t2 * T2 + sqrt(max(0.0, 1.0 - t1 * t1 - t2 * t2)) * Vh;\n  return normalize(vec3(alpha * Nh.x, alpha * Nh.y, max(0.0, Nh.z)));\n}\n\n// Standard GGX NDF sampler (ignores view direction -> samples backfacing/masked microfacets at grazing angles!)\nvec3 sampleGGX_NDF(float alpha, vec2 u) {\n  float phi = 6.2831853 * u.y;\n  float cosT = sqrt(max((1.0 - u.x) / max(1.0 + (alpha * alpha - 1.0) * u.x, 1e-5), 0.0));\n  float sinT = sqrt(max(1.0 - cosT * cosT, 0.0));\n  return vec3(sinT * cos(phi), sinT * sin(phi), cosT);\n}\n\nfloat smithG1(float ndx, float alpha) {\n  float a2 = alpha * alpha;\n  return 2.0 * ndx / max(ndx + sqrt(a2 + (1.0 - a2) * ndx * ndx), 1e-4);\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 isVNDF = screenX >= split;\n  float rough = clamp(P_ROUGHNESS, 0.10, 0.90);\n  float alpha = rough * rough;\n  float nGain = clamp(P_NOISE_SCALE, 0.1, 2.0);\n  float spin = iTime * max(P_LIGHT_SPIN, 0.0);\n\n  vec3 ro = vec3(0.0, 0.42, 2.25);\n  vec3 rd = normalize(vec3(uv - vec2(0.0, 0.08), -1.48));\n  vec3 col = evalEnv(rd, spin) * 0.35;\n\n  // Intersect matte-gold metallic sphere (R=0.58) and grazing metallic ground plane (y=-0.58)\n  float b = dot(ro, rd);\n  float c = dot(ro, ro) - 0.58 * 0.58;\n  float disc = b * b - c;\n  float tSph = (disc > 0.0 && (-b - sqrt(disc)) > 0.05) ? (-b - sqrt(disc)) : 1e5;\n  float tPl = (rd.y < -0.01) ? ((-0.58 - ro.y) / rd.y) : 1e5;\n  if (tPl < 0.05 || tPl > 14.0) tPl = 1e5;\n\n  float tHit = min(tSph, tPl);\n  if (tHit < 1e4) {\n    vec3 p = ro + rd * tHit;\n    vec3 n = (tSph < tPl) ? normalize(p) : vec3(0.0, 1.0, 0.0);\n    vec3 v = -rd;\n    vec3 T, B;\n    buildBasis(n, T, B);\n    vec3 Ve = normalize(vec3(dot(v, T), dot(v, B), max(dot(v, n), 0.005)));\n\n    // 4-sample Monte Carlo estimator per pixel: NDF suffers massive grazing fireflies/rejections; VNDF stays crisp!\n    vec3 acc = vec3(0.0);\n    vec3 f0 = (tSph < tPl) ? vec3(0.95, 0.76, 0.42) : vec3(0.55, 0.64, 0.78);\n    vec2 pixSeed = floor(fragCoord * mix(1.0, 0.65, clamp(nGain - 0.5, 0.0, 0.7)));\n    float frameJit = floor(iTime * 12.0) * 0.037;\n\n    for (int s = 0; s < 4; s++) {\n      float fs = float(s);\n      vec2 u = vec2(\n        hash21(pixSeed + vec2(fs * 17.3, frameJit * 11.1)),\n        hash21(pixSeed * 1.37 + vec2(frameJit * 7.3, fs * 29.7))\n      );\n      u = clamp(u, 0.002, 0.998);\n\n      vec3 hLocal = isVNDF ? sampleGGX_VNDF(Ve, alpha, u) : sampleGGX_NDF(alpha, u);\n      vec3 hWorld = normalize(T * hLocal.x + B * hLocal.y + n * hLocal.z);\n      vec3 lWorld = reflect(-v, hWorld);\n      float ndl = dot(n, lWorld);\n      float ndv = max(dot(n, v), 0.005);\n      float ndh = max(dot(n, hWorld), 0.005);\n      float vdh = max(dot(v, hWorld), 0.005);\n\n      if (ndl > 0.002) {\n        vec3 F = f0 + (1.0 - f0) * pow(1.0 - vdh, 5.0);\n        vec3 env = evalEnv(lWorld, spin);\n        if (isVNDF) {\n          // VNDF weight reduces cleanly to G1(L) * F -- bounded in [0, 1], zero grazing weight explosion!\n          float w = smithG1(ndl, alpha);\n          acc += env * F * w;\n        } else {\n          // Classic NDF weight G2 * (V.H) / (N.V * N.H) explodes at grazing N.V -> 0!\n          float G2 = smithG1(ndl, alpha) * smithG1(ndv, alpha);\n          float w = clamp((G2 * vdh) / max(ndv * ndh, 0.015), 0.0, 4.5 * nGain);\n          acc += env * F * w;\n        }\n      }\n    }\n    col = acc * 0.25;\n    if (tPl < tSph) col = mix(col, evalEnv(rd, spin) * 0.35, 1.0 - exp(-tPl * 0.14));\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": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "vndf-sampling",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T20:35:58.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.3585,
      "std": 0.2231,
      "min": 0,
      "max": 0.9837
     },
     "image": "/specimens/vndf-sampling-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "ggx-trowbridge-reitz",
  "smith-masking-shadowing",
  "kulla-conty-multiscatter"
 ],
 "extends": [
  "smith-masking-shadowing"
 ],
 "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/vndf-sampling.json"
 },
 "solves": [
  "noise at grazing angles in glossy sampling",
  "wasted samples on backfacing microfacets",
  "importance sampling ggx has high variance",
  "monte carlo specular rendering converges slowly"
 ],
 "authority": "Heitz",
 "url": "https://texelatlas.vercel.app/t/vndf-sampling/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 12.81,
  "tier": "moderate",
  "p50_ms": 0.4213,
  "p95_ms": 0.4407,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "vndf-sampling-live",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 10.26,
   "tier": "moderate",
   "p50_ms": 0.1631,
   "p95_ms": 0.1647,
   "speedup_x": 2.58,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 7.88,
   "tier": "moderate",
   "p50_ms": 0.227,
   "p95_ms": 0.2274,
   "speedup_x": 1.86,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}