In deferred shading, what is a G-buffer, what does it typically store per pixel, and why is it filled before any lighting runs?
answer
- several render targets, one pass
- surface inputs, not lit colour
- colour, normal, roughness, depth
- position comes back from depth
- only the visible surface gets lit
basics
~20 sA G-buffer is a set of screen-sized render targets that a deferred renderer's geometry pass fills with each visible pixel's surface attributes: depth, base colour, normal and material parameters. Lighting then reads them, so lights touch only visible surfaces.
solid answer
~40 sThe *G-buffer* (geometry buffer) is the output of deferred shading's first pass. Every opaque mesh is drawn once into several render targets at the same time, writing surface attributes rather than lit colour: base colour, a world- or view-space normal, material parameters such as roughness and metalness, often ambient occlusion and motion vectors, plus the depth buffer. Position is usually not stored; it is rebuilt from depth and the pixel's screen coordinates. Because the depth test has already kept only the nearest opaque surface per pixel when lighting starts, the lighting passes evaluate each light against one surface per pixel, and fragments hidden by overdraw never pay for lighting. Many layouts pack this into three to five 32-bit targets plus depth.
go deeper
Name the attributes a G-buffer holds, say they are written in one geometry pass into several render targets, and explain that lighting reads them afterwards.
Explain why position is rebuilt from depth, how a normal fits into two channels, and why overdraw now costs attribute writes but not lighting.
Reason in bytes per pixel: every channel you add is written and read for every pixel at full resolution, so justify each one by the effects that need it.
Treat the layout as a contract between materials, lighting and screen-space effects; changing it later touches every shader, so settle the material menu early.
## What a G-buffer is **Deferred shading** splits a frame into two stages. First a **geometry pass** draws every opaque mesh once and, instead of computing lit colour, writes the surface's properties into several screen-sized render targets at the same time (multiple render targets). Together those targets are the **G-buffer**, short for geometry buffer. Then one or more **lighting passes** read the G-buffer and compute how each light affects each pixel, writing the result into a separate lit colour target, usually a high-dynamic-range one. The word *deferred* means exactly this: lighting is postponed until the frame knows which surface is visible at every pixel. ## What it typically stores There is no standard layout. Each renderer packs what its materials and screen-space effects need into as few bytes as it can. A representative layout: | Target | Example format | Contents | |---|---|---| | Depth | 32-bit depth | distance used by the depth test, later used to rebuild position | | Target 0 | `RGBA8` | base colour (albedo) in RGB, ambient occlusion in A | | Target 1 | `RGB10A2` or `RG16` | surface normal, often packed into two channels | | Target 2 | `RGBA8` | roughness, metalness, a material or shading-model ID, a spare channel | | Target 3 | `RG16F` | screen-space motion vector for temporal effects | That is 4 + 4 + 4 + 4 + 4 = **20 bytes per pixel**. Common variations: - **Emissive** colour is either stored in its own target or written straight into the lit target during the geometry pass. - **Specular colour or reflectance** replaces metalness in renderers that use a specular workflow. - **Normals** are stored in world space or view space; packing them into two channels (for example with an octahedral mapping) saves a channel without losing much precision. - **Position is rarely stored.** It is rebuilt per pixel from the depth value, the pixel's screen coordinates and the inverse of the camera's projection, which costs a few instructions instead of 12 or more bytes of memory traffic per pixel. ## Why it is filled before any lighting The order is the whole point. A deferred frame runs: 1. **Geometry pass.** Draw opaque meshes with depth testing. Nearer surfaces overwrite farther ones, so when the pass ends each pixel holds exactly one surface: the visible one. 2. **Lighting pass or passes.** For each light, or for each pixel with a list of nearby lights, read the stored attributes, evaluate the material's lighting model and add the result to the lit target. 3. **Later passes.** Transparent surfaces, post-processing and other screen-space effects run on top. Because visibility is resolved first, lighting is evaluated only for surfaces that are actually seen. Where several meshes overlap a pixel, the hidden fragments still pay to write their attributes, but they never pay for lights. And because lighting reads a screen-space image rather than drawing meshes, its cost depends on how many pixels each light covers rather than on how many objects are in the scene. The same attributes feed other screen-space work too: ambient occlusion and reflections read depth and normals, and decals can modify the stored base colour or normal before lighting runs, so the decal is lit exactly like the surface under it. ## What it cannot hold The G-buffer keeps **one surface per pixel**, and that is its main limitation: - A glass pane and the wall behind it cannot both be stored, so transparent surfaces are drawn later in a separate forward pass. - Every material must express itself through the same few channels. A material that needs extra parameters, such as skin, hair or cloth, needs a shading-model ID and spare channels, and the lighting pass branches on that ID. - Each channel costs bandwidth at full resolution: every byte is written once and read at least once for every pixel on screen, every frame. ## Common mistakes - Describing the G-buffer as storing lit colour. It stores the inputs to lighting; the lit result goes to a different target. - Assuming one G-buffer layer per light. There is one set of targets for the frame, and every light reads it. - Storing world position as three floats out of habit, which spends bandwidth on data the depth buffer already implies. - Claiming deferred shading removes the cost of overdraw. It removes lighting from hidden fragments, not their vertex work or their attribute writes.
- Why do most deferred renderers not store world position in the G-buffer?Position can be rebuilt per pixel from the depth value, the pixel's screen coordinates and the inverse of the camera's projection, so storing it spends 12 or more bytes per pixel on data the depth buffer already implies. Lighting passes are usually limited by memory bandwidth rather than arithmetic, so a few instructions to reconstruct position cost less than reading a three-float target for every pixel.
- What happens when a material needs a parameter the G-buffer has no room for, such as subsurface scattering for skin?It needs a shading-model ID plus somewhere for its extra data, often by reinterpreting spare channels for that model only. The lighting pass then branches on the ID. Every new shading model either competes for the same few bytes or widens the G-buffer for every pixel on screen, which is why deferred renderers offer a short, fixed menu of material models.
- The G-buffer stores the final lit colour of each pixel.
- Each light writes its own separate G-buffer layer.
- A G-buffer must store world position as three floats per pixel.
- The G-buffer holds every surface along the view ray, transparent ones included.
- Deferring lighting also removes the cost of overdraw from the geometry pass.