Deferred shading (G-buffer)
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.
Also known as deferred rendering, G-buffer rendering
In the family Performance, LOD & compression, shading architecture
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | Forward renderer; G-buffer needs custom MRT passes. |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | Renderers are Forward+, Mobile and Compatibility; no deferred G-buffer path. |
| Unity | Built in | HDRP Asset > Lit Shader Mode = Deferred (or Both); URP Renderer > Rendering Path = Deferred docs |
| Unreal 5.7 | Built in | Default renderer is deferred; optional Forward Shading Renderer in Project Settings > Rendering docs |
| Blender 5.2 | Built in | EEVEE Next default opaque lighting runs through DeferredPipeline with multi-layer G-buffer closures docs |
| WebGPU W3C | Built in | GPURenderPassDescriptor.colorAttachments (up to maxColorAttachments=8 MRT targets in one pass) docs Hardware MRT G-buffer output is native; lighting pass is authored in WGSL. |
| Metal | None found |
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.
Pass 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.
Transparent surfaces, which have no single attribute set per pixel, are handled in a separate forward pass afterwards.
When to choose it
Reach for it when
- 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
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 rendering when G-buffer bandwidth or material-count fragmentation is the bottleneck.
Field marks
- 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
Presets
Deferred shading — G-buffer (albedo, normals, depth) split vs multi-light pass
{
"P_LIGHT_COUNT": 12,
"P_LIGHT_RADIUS": 1.4,
"P_SPECULAR": 0.8,
"P_GBUF_SPLIT": 0.44
}Show GLSL (175 lines)
const float P_LIGHT_COUNT = 12.0; // P Point Lights [2.0, 16.0, 1.0]
const float P_LIGHT_RADIUS = 1.4; // P Light Radius [0.4, 2.8, 0.05]
const float P_SPECULAR = 0.8; // P Specular Gain [0.0, 2.0, 0.05]
const float P_GBUF_SPLIT = 0.44; // P G-Buffer Split [0.0, 1.0, 0.02]
mat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
float sdTorus(vec3 p, vec2 t) {
vec2 q = vec2(length(p.xz) - t.x, p.y);
return length(q) - t.y;
}
float sdBox(vec3 p, vec3 b) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}
// Returns vec2(distance, materialId)
vec2 mapScene(vec3 p) {
vec2 res = vec2(p.y + 0.85, 1.0); // Floor
// Central sculpted torus
vec3 q = p - vec3(0.0, 0.05, 0.0);
q.xy = rot2(iTime * 0.35) * q.xy;
q.yz = rot2(0.55) * q.yz;
float dT = sdTorus(q, vec2(0.68, 0.24));
if (dT < res.x) res = vec2(dT, 2.0);
// Orbiting chrome spheres
for (int i = 0; i < 4; i++) {
float a = float(i) * 1.5708 + iTime * 0.4;
vec3 sp = vec3(cos(a) * 1.45, -0.45 + 0.18 * sin(iTime * 1.2 + float(i)), sin(a) * 1.45);
float dS = length(p - sp) - 0.32;
if (dS < res.x) res = vec2(dS, 3.0);
}
// Architectural rear pillars
vec3 rp = p - vec3(0.0, 0.0, -2.2);
rp.x = mod(rp.x + 1.2, 2.4) - 1.2;
float dP = sdBox(rp, vec3(0.28, 2.0, 0.28)) - 0.04;
if (dP < res.x) res = vec2(dP, 4.0);
return res;
}
vec3 calcNormal(vec3 p) {
vec2 e = vec2(0.002, 0.0);
return normalize(vec3(
mapScene(p + e.xyy).x - mapScene(p - e.xyy).x,
mapScene(p + e.yxy).x - mapScene(p - e.yxy).x,
mapScene(p + e.yyx).x - mapScene(p - e.yyx).x
));
}
vec3 hsv2rgb(float h, float s, float v) {
vec3 p = abs(fract(vec3(h) + vec3(0.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0);
return v * mix(vec3(1.0), clamp(p - 1.0, 0.0, 1.0), s);
}
vec3 aces(vec3 x) {
x = max(x, 0.0);
return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
vec2 screenUV = fragCoord / iResolution.xy;
vec3 ro = vec3(0.0, 0.85, 3.6);
vec3 ta = vec3(0.0, -0.1, 0.0);
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + 1.45 * ww);
// Geometry pass: populate G-buffer (albedo, world normal, linear depth, roughness)
float t = 0.0;
float matId = 0.0;
bool hit = false;
for (int i = 0; i < 64; i++) {
vec2 h = mapScene(ro + rd * t);
if (h.x < 0.002) { matId = h.y; hit = true; break; }
t += h.x;
if (t > 16.0) break;
}
vec3 gAlbedo = vec3(0.03, 0.04, 0.06);
vec3 gNormal = vec3(0.0, 0.0, 1.0);
float gRough = 0.5;
float gDepth = clamp(t / 12.0, 0.0, 1.0);
vec3 lit = vec3(0.02, 0.025, 0.04);
if (hit) {
vec3 p = ro + rd * t;
gNormal = calcNormal(p);
if (matId < 1.5) {
vec2 chk = floor(p.xz * 1.4);
float m = mod(chk.x + chk.y, 2.0);
gAlbedo = mix(vec3(0.18, 0.19, 0.22), vec3(0.46, 0.48, 0.52), m);
gRough = mix(0.28, 0.52, m);
} else if (matId < 2.5) {
gAlbedo = vec3(0.84, 0.78, 0.72);
gRough = 0.22;
} else if (matId < 3.5) {
gAlbedo = vec3(0.75, 0.82, 0.88);
gRough = 0.14;
} else {
gAlbedo = vec3(0.48, 0.50, 0.56);
gRough = 0.58;
}
// Lighting pass: accumulate N deferred point lights solely from G-buffer attributes
lit = gAlbedo * vec3(0.06, 0.07, 0.11);
int nLights = int(clamp(P_LIGHT_COUNT, 1.0, 16.0));
float lRad = max(P_LIGHT_RADIUS, 0.25);
float specGain = max(P_SPECULAR, 0.0);
vec3 v = -rd;
for (int i = 0; i < 16; i++) {
if (i >= nLights) break;
float fi = float(i);
float ang = fi * 0.3927 * 1.6 + iTime * (0.45 + 0.05 * mod(fi, 3.0));
float rOrbit = 0.85 + 0.95 * sin(fi * 1.9 + iTime * 0.3);
vec3 lPos = vec3(cos(ang) * rOrbit, -0.35 + 0.75 * abs(sin(fi * 1.3 + iTime * 0.7)), sin(ang) * rOrbit);
vec3 lCol = hsv2rgb(fract(fi * 0.13 + 0.05), 0.82, 1.0);
vec3 toL = lPos - p;
float dist = length(toL) + 1e-4;
vec3 l = toL / dist;
float att = pow(clamp(1.0 - dist / (lRad * 2.2), 0.0, 1.0), 2.0) / (0.18 + dist * dist);
float ndl = max(dot(gNormal, l), 0.0);
vec3 h = normalize(l + v);
float shininess = mix(96.0, 10.0, gRough);
float spec = pow(max(dot(gNormal, h), 0.0), shininess) * specGain;
lit += lCol * att * (gAlbedo * ndl * 1.5 + vec3(spec) * 1.2);
}
}
// Visualize light volumes as subtle glowing sprites in the deferred composite
int nLightsVis = int(clamp(P_LIGHT_COUNT, 1.0, 16.0));
for (int i = 0; i < 16; i++) {
if (i >= nLightsVis) break;
float fi = float(i);
float ang = fi * 0.3927 * 1.6 + iTime * (0.45 + 0.05 * mod(fi, 3.0));
float rOrbit = 0.85 + 0.95 * sin(fi * 1.9 + iTime * 0.3);
vec3 lPos = vec3(cos(ang) * rOrbit, -0.35 + 0.75 * abs(sin(fi * 1.3 + iTime * 0.7)), sin(ang) * rOrbit);
vec3 toL = lPos - ro;
float proj = dot(toL, rd);
if (proj > 0.0 && (!hit || proj < t)) {
float perp = length(toL - rd * proj);
vec3 lCol = hsv2rgb(fract(fi * 0.13 + 0.05), 0.78, 1.0);
lit += lCol * exp(-perp * 26.0) * 1.1;
}
}
// Split G-buffer diagnostic panel on the left: top = World Normals, middle = Albedo, bottom = Depth
float splitX = clamp(P_GBUF_SPLIT, 0.0, 1.0);
vec3 col = aces(lit);
if (screenUV.x < splitX) {
if (screenUV.y > 0.666) {
col = hit ? (gNormal * 0.5 + 0.5) : vec3(0.08, 0.09, 0.14);
} else if (screenUV.y > 0.333) {
col = hit ? gAlbedo : vec3(0.06, 0.07, 0.09);
} else {
float dVis = hit ? pow(1.0 - gDepth, 1.8) : 0.0;
col = vec3(dVis * 0.9, dVis * 0.95, dVis);
}
// Thin horizontal dividers between G-buffer rows
if (abs(screenUV.y - 0.666) < 0.004 || abs(screenUV.y - 0.333) < 0.004) col = vec3(0.95);
}
if (splitX > 0.01 && splitX < 0.99 && abs(screenUV.x - splitX) < 0.004) col = vec3(0.98, 0.86, 0.45);
fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| deferred-shading-live | Tesla T4 1920x1080 | 66.6× baseline (extreme) · 2.19 ms / 2.28 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| deferred-shading-live | NVIDIA L4 1920x1080 | 61.6× baseline (extreme) · 0.980 ms / 0.992 ms · 2.2× faster on L4 | opengl-timer-query · Cross-architecture validation |
| deferred-shading-live | NVIDIA A100-SXM4-40GB 1920x1080 | 42.0× baseline (heavy) · 1.21 ms / 1.22 ms · 1.8× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
From the citation graph (Crossref reference lists): its primary source cites Marching Cubes; cited by Fine tone control in hardware hatching, Hashed alpha testing / alpha-to-coverage for foliage, Inverted-hull outlines, Procedural cross-hatching shader, Suggestive contours, Real-time hatching with tonal art maps.
Crosses
- Tiled deferred shading: Deferred's G-buffer is exactly what the tile light-culling pass reads depth from before accumulating lights.
- Screen-Space Ambient Occlusion: The depth and normal targets written by the geometry pass feed screen-space AO with no extra rendering.
Sources
- primaryTakafumi Saito, Tokiichiro Takahashi (1990). Comprehensible rendering of 3-D shapes. Proceedings of the 17th annual conference on Computer graphics and interactive techniques.Verified via crossref · 2026-11-29doi:10.1145/97879.97901
- primaryMichael Deering, Stephanie Winner, Bic Schediwy, Chris Duffy, Neil Hunt (1988). The triangle processor and normal vector shader. Proceedings of the 15th annual conference on Computer graphics and interactive techniques.Verified via crossref · 2026-11-29doi:10.1145/54852.378468
- talkShawn Hargreaves, Mark Harris. Deferred Shading (GDC 2004). GDC 2004.Verified via live page · 2026-11-29