{
 "id": "deferred-shading",
 "title": "Deferred shading (G-buffer)",
 "aliases": [
  "deferred rendering",
  "G-buffer rendering"
 ],
 "domain": "performance",
 "subdomain": "shading architecture",
 "stage": "performance",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu",
  "unity",
  "unreal",
  "godot"
 ],
 "year_introduced": 1988,
 "inputs": [
  "mesh",
  "material-params"
 ],
 "outputs": [
  "gbuffer",
  "radiance"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "Rasterize geometry once into several screen-sized attribute targets, then run lighting as a separate screen-space pass so shading cost scales with pixels and lights, not with overdraw.",
 "how_it_works": "Pass one renders every opaque mesh into a set of render targets, the G-buffer, storing per-pixel what lighting later needs: depth, a packed normal, albedo, roughness, metalness, sometimes emissive and a material id. No lighting is evaluated here, so overdraw only costs attribute writes.\n\nPass two reads those targets and evaluates lighting per pixel. Each light is drawn as a proxy (full-screen quad, sphere, cone) or batched by tiles, and its contribution is accumulated additively into an HDR target, with position reconstructed from depth. Because each visible pixel is shaded once per light, hundreds of small lights become affordable.\n\nTransparent surfaces, which have no single attribute set per pixel, are handled in a separate forward pass afterwards.",
 "when_to_use": "Choose it when a scene has many dynamic lights and mostly opaque surfaces, and you want screen-space effects (SSAO, SSR, decals) that need normals and depth anyway. Prefer `clustered-forward-shading` when you need MSAA, many transparents, or tight bandwidth on mobile. Prefer `tiled-deferred-shading` as the scaling step once the light count makes per-light proxies expensive. Prefer `visibility-buffer` when G-buffer bandwidth or material-count fragmentation is the bottleneck.",
 "cost": {
  "class": "medium",
  "notes": "Dominated by G-buffer bandwidth (4-6 targets at full resolution) rather than ALU; qualitative, no source-specific numbers."
 },
 "failure_modes": [
  "Hardware MSAA is awkward: multisampled G-buffers multiply bandwidth, so engines fall back to post-process AA and get shimmering sub-pixel edges",
  "Transparency, hair and foliage with alpha need a second forward path with different lighting, so the two look slightly different",
  "Packing normals and material data into few channels causes banding in smooth gradients and limits material variety (one BRDF per pass)",
  "Bandwidth blows up on tile-based mobile GPUs unless the G-buffer stays in on-chip memory",
  "Light proxy overdraw becomes the new bottleneck with large overlapping lights"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/97879.97901",
   "type": "paper",
   "title": "Comprehensible rendering of 3-D shapes",
   "authors": [
    "Takafumi Saito",
    "Tokiichiro Takahashi"
   ],
   "year": 1990,
   "venue": "Proceedings of the 17th annual conference on Computer graphics and interactive techniques",
   "url": "https://doi.org/10.1145/97879.97901",
   "ids": {
    "doi": "10.1145/97879.97901"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/97879.97901",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/97879.97901",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/97879.97901",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/97879.97901",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/97879.97901",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "saito1990comprehensible"
  },
  {
   "role": "primary",
   "key": "doi:10.1145/54852.378468",
   "type": "paper",
   "title": "The triangle processor and normal vector shader",
   "authors": [
    "Michael Deering",
    "Stephanie Winner",
    "Bic Schediwy",
    "Chris Duffy",
    "Neil Hunt"
   ],
   "year": 1988,
   "venue": "Proceedings of the 15th annual conference on Computer graphics and interactive techniques",
   "url": "https://doi.org/10.1145/54852.378468",
   "ids": {
    "doi": "10.1145/54852.378468"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/54852.378468",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/54852.378468",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/54852.378468",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/54852.378468",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/54852.378468",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "deering1988triangle"
  },
  {
   "role": "talk",
   "key": "url:https://www.nvidia.com/docs/IO/8228/GDC2004_DeferredShading.pdf",
   "type": "talk",
   "title": "Deferred Shading (GDC 2004)",
   "authors": [
    "Shawn Hargreaves",
    "Mark Harris"
   ],
   "year": null,
   "venue": "GDC 2004",
   "url": "https://www.nvidia.com/en-us/page-not-found",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": null,
    "page_title": "Page Not Found | NVIDIA",
    "host": "nvidia.com",
    "title_confirmed_by_page": false,
    "title_unconfirmed": true
   },
   "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://www.nvidia.com/docs/IO/8228/GDC2004_DeferredShading.pdf → 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": 0,
   "__bib": "hargreavesdeferred"
  }
 ],
 "presets": [
  {
   "id": "deferred-shading-live",
   "title": "Deferred shading — G-buffer (albedo, normals, depth) split vs multi-light pass",
   "runtime": "glsl-fragment",
   "params": {
    "P_LIGHT_COUNT": 12,
    "P_LIGHT_RADIUS": 1.4,
    "P_SPECULAR": 0.8,
    "P_GBUF_SPLIT": 0.44
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_LIGHT_COUNT = 12.0; // P Point Lights [2.0, 16.0, 1.0]\nconst float P_LIGHT_RADIUS = 1.4; // P Light Radius [0.4, 2.8, 0.05]\nconst float P_SPECULAR = 0.8; // P Specular Gain [0.0, 2.0, 0.05]\nconst float P_GBUF_SPLIT = 0.44; // P G-Buffer Split [0.0, 1.0, 0.02]\n\nmat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }\n\nfloat sdTorus(vec3 p, vec2 t) {\n  vec2 q = vec2(length(p.xz) - t.x, p.y);\n  return length(q) - t.y;\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\n// Returns vec2(distance, materialId)\nvec2 mapScene(vec3 p) {\n  vec2 res = vec2(p.y + 0.85, 1.0); // Floor\n\n  // Central sculpted torus\n  vec3 q = p - vec3(0.0, 0.05, 0.0);\n  q.xy = rot2(iTime * 0.35) * q.xy;\n  q.yz = rot2(0.55) * q.yz;\n  float dT = sdTorus(q, vec2(0.68, 0.24));\n  if (dT < res.x) res = vec2(dT, 2.0);\n\n  // Orbiting chrome spheres\n  for (int i = 0; i < 4; i++) {\n    float a = float(i) * 1.5708 + iTime * 0.4;\n    vec3 sp = vec3(cos(a) * 1.45, -0.45 + 0.18 * sin(iTime * 1.2 + float(i)), sin(a) * 1.45);\n    float dS = length(p - sp) - 0.32;\n    if (dS < res.x) res = vec2(dS, 3.0);\n  }\n\n  // Architectural rear pillars\n  vec3 rp = p - vec3(0.0, 0.0, -2.2);\n  rp.x = mod(rp.x + 1.2, 2.4) - 1.2;\n  float dP = sdBox(rp, vec3(0.28, 2.0, 0.28)) - 0.04;\n  if (dP < res.x) res = vec2(dP, 4.0);\n\n  return res;\n}\n\nvec3 calcNormal(vec3 p) {\n  vec2 e = vec2(0.002, 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\nvec3 hsv2rgb(float h, float s, float v) {\n  vec3 p = abs(fract(vec3(h) + vec3(0.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0);\n  return v * mix(vec3(1.0), clamp(p - 1.0, 0.0, 1.0), s);\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  vec2 screenUV = fragCoord / iResolution.xy;\n\n  vec3 ro = vec3(0.0, 0.85, 3.6);\n  vec3 ta = vec3(0.0, -0.1, 0.0);\n  vec3 ww = normalize(ta - ro);\n  vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));\n  vec3 vv = cross(uu, ww);\n  vec3 rd = normalize(uv.x * uu + uv.y * vv + 1.45 * ww);\n\n  // Geometry pass: populate G-buffer (albedo, world normal, linear depth, roughness)\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) { matId = h.y; hit = true; break; }\n    t += h.x;\n    if (t > 16.0) break;\n  }\n\n  vec3 gAlbedo = vec3(0.03, 0.04, 0.06);\n  vec3 gNormal = vec3(0.0, 0.0, 1.0);\n  float gRough = 0.5;\n  float gDepth = clamp(t / 12.0, 0.0, 1.0);\n  vec3 lit = vec3(0.02, 0.025, 0.04);\n\n  if (hit) {\n    vec3 p = ro + rd * t;\n    gNormal = calcNormal(p);\n    if (matId < 1.5) {\n      vec2 chk = floor(p.xz * 1.4);\n      float m = mod(chk.x + chk.y, 2.0);\n      gAlbedo = mix(vec3(0.18, 0.19, 0.22), vec3(0.46, 0.48, 0.52), m);\n      gRough = mix(0.28, 0.52, m);\n    } else if (matId < 2.5) {\n      gAlbedo = vec3(0.84, 0.78, 0.72);\n      gRough = 0.22;\n    } else if (matId < 3.5) {\n      gAlbedo = vec3(0.75, 0.82, 0.88);\n      gRough = 0.14;\n    } else {\n      gAlbedo = vec3(0.48, 0.50, 0.56);\n      gRough = 0.58;\n    }\n\n    // Lighting pass: accumulate N deferred point lights solely from G-buffer attributes\n    lit = gAlbedo * vec3(0.06, 0.07, 0.11);\n    int nLights = int(clamp(P_LIGHT_COUNT, 1.0, 16.0));\n    float lRad = max(P_LIGHT_RADIUS, 0.25);\n    float specGain = max(P_SPECULAR, 0.0);\n    vec3 v = -rd;\n\n    for (int i = 0; i < 16; i++) {\n      if (i >= nLights) break;\n      float fi = float(i);\n      float ang = fi * 0.3927 * 1.6 + iTime * (0.45 + 0.05 * mod(fi, 3.0));\n      float rOrbit = 0.85 + 0.95 * sin(fi * 1.9 + iTime * 0.3);\n      vec3 lPos = vec3(cos(ang) * rOrbit, -0.35 + 0.75 * abs(sin(fi * 1.3 + iTime * 0.7)), sin(ang) * rOrbit);\n      vec3 lCol = hsv2rgb(fract(fi * 0.13 + 0.05), 0.82, 1.0);\n\n      vec3 toL = lPos - p;\n      float dist = length(toL) + 1e-4;\n      vec3 l = toL / dist;\n      float att = pow(clamp(1.0 - dist / (lRad * 2.2), 0.0, 1.0), 2.0) / (0.18 + dist * dist);\n\n      float ndl = max(dot(gNormal, l), 0.0);\n      vec3 h = normalize(l + v);\n      float shininess = mix(96.0, 10.0, gRough);\n      float spec = pow(max(dot(gNormal, h), 0.0), shininess) * specGain;\n      lit += lCol * att * (gAlbedo * ndl * 1.5 + vec3(spec) * 1.2);\n    }\n  }\n\n  // Visualize light volumes as subtle glowing sprites in the deferred composite\n  int nLightsVis = int(clamp(P_LIGHT_COUNT, 1.0, 16.0));\n  for (int i = 0; i < 16; i++) {\n    if (i >= nLightsVis) break;\n    float fi = float(i);\n    float ang = fi * 0.3927 * 1.6 + iTime * (0.45 + 0.05 * mod(fi, 3.0));\n    float rOrbit = 0.85 + 0.95 * sin(fi * 1.9 + iTime * 0.3);\n    vec3 lPos = vec3(cos(ang) * rOrbit, -0.35 + 0.75 * abs(sin(fi * 1.3 + iTime * 0.7)), sin(ang) * rOrbit);\n    vec3 toL = lPos - ro;\n    float proj = dot(toL, rd);\n    if (proj > 0.0 && (!hit || proj < t)) {\n      float perp = length(toL - rd * proj);\n      vec3 lCol = hsv2rgb(fract(fi * 0.13 + 0.05), 0.78, 1.0);\n      lit += lCol * exp(-perp * 26.0) * 1.1;\n    }\n  }\n\n  // Split G-buffer diagnostic panel on the left: top = World Normals, middle = Albedo, bottom = Depth\n  float splitX = clamp(P_GBUF_SPLIT, 0.0, 1.0);\n  vec3 col = aces(lit);\n  if (screenUV.x < splitX) {\n    if (screenUV.y > 0.666) {\n      col = hit ? (gNormal * 0.5 + 0.5) : vec3(0.08, 0.09, 0.14);\n    } else if (screenUV.y > 0.333) {\n      col = hit ? gAlbedo : vec3(0.06, 0.07, 0.09);\n    } else {\n      float dVis = hit ? pow(1.0 - gDepth, 1.8) : 0.0;\n      col = vec3(dVis * 0.9, dVis * 0.95, dVis);\n    }\n    // Thin horizontal dividers between G-buffer rows\n    if (abs(screenUV.y - 0.666) < 0.004 || abs(screenUV.y - 0.333) < 0.004) col = vec3(0.95);\n  }\n  if (splitX > 0.01 && splitX < 0.99 && abs(screenUV.x - splitX) < 0.004) col = vec3(0.98, 0.86, 0.45);\n\n  fragColor = vec4(pow(clamp(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": "deferred-shading",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T16:50:13.852Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.6445,
      "std": 0.2759,
      "unique_colors": 865
     },
     "image": "/specimens/deferred-shading-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "tiled-deferred-shading",
  "clustered-forward-shading",
  "visibility-buffer",
  "ssao",
  "screen-space-reflections"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "tiled-deferred-shading",
   "why": "Deferred's G-buffer is exactly what the tile light-culling pass reads depth from before accumulating lights."
  },
  {
   "id": "ssao",
   "why": "The depth and normal targets written by the geometry pass feed screen-space AO with no extra rendering."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/performance/deferred-shading.json"
 },
 "solves": [
  "many dynamic lights make forward rendering too slow",
  "pixel shader cost explodes with overdraw and lights",
  "need normals and depth for screen-space effects for free",
  "thousands of small point lights in a scene",
  "lighting cost should scale with pixels not geometry"
 ],
 "authority": "Saito & Takahashi, 1990",
 "url": "https://texelatlas.vercel.app/t/deferred-shading/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 66.6,
  "tier": "extreme",
  "p50_ms": 2.1913,
  "p95_ms": 2.284,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "deferred-shading-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 61.6,
   "tier": "extreme",
   "p50_ms": 0.9795,
   "p95_ms": 0.9923,
   "speedup_x": 2.24,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 42,
   "tier": "heavy",
   "p50_ms": 1.2096,
   "p95_ms": 1.2247,
   "speedup_x": 1.81,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}