On this page
Forward vs Deferred
The pipeline tells you where lighting happens, in the fragment stage. Forward and deferred are two answers to the question that comes next: for a scene full of objects and full of lights, how do we combine them without doing the work twice?
// The one decision that shapes the cost of a lit scene:
// how do objects and lights get combined?
The question underneath both¶
A real scene has many objects and many lights, and a correct image needs each object to be lit by every light that actually reaches it. If you did that naively, the work would multiply: more objects times more lights. Forward and deferred differ in when and where they do that work, and that single choice is what separates a scene that scales from one that collapses as you add lights.
Forward¶
In forward rendering, the geometry and its lighting are computed together, in the fragment stage, as each object is drawn. For every object you visit, your shader loops over the lights that affect it and adds their contribution. It is the simplest model to reason about, it plays nicely with transparency, and it works on virtually every GPU. The price is that the lighting cost scales with objects times lights. Add a fifth light to a room of fifty objects and you have just done fifty times more fragment work, because every object re-ran the lighting loop.
This is the default in most real-time engines, and it is the default in the Universal Render Pipeline that this library targets. Every lit shader in the Lighting section is a forward shader, and that is why they call a lighting helper and let it loop the lights for them.
Deferred¶
In deferred rendering, the scene is drawn in two separate phases. First, a geometry pass renders every object and writes its material properties, albedo, normal, and whatever else your lighting model needs, into a set of full-screen textures called the G-buffer. At this point nothing is lit; you have only stored what each pixel is made of. Then a single full-screen lighting pass reads the G-buffer and applies all the lights to every pixel at once.
The payoff is that object count disappears from the lighting cost. The geometry pass costs objects with no lights, and the lighting pass costs pixels times lights, independent of how many objects there are. Fifty objects and five lights, or five objects and five lights, the lighting pass does exactly the same work. That is why deferred shines in dense, multi-light scenes. The price is real, though: you can only light what you were able to store in the G-buffer, transparency becomes awkward because a pixel can only hold one material, and the setup needs multiple render targets and more memory bandwidth.
Choosing, and what it means for you¶
In practice the choice is usually a project or engine setting, not something you pick per shader. Forward wins on mobile and on scenes with heavy transparency, because it is simple and forgiving. Deferred wins on high-end targets with many dynamic lights, because it keeps the lighting cost flat as the object count grows. A lot of production engines ship forward by default and offer deferred, or a hybrid, for specific scenes.
What this means for you as a shader author is mostly about reading the lighting API and anticipating cost. If your lighting function loops lights, you are thinking forward, and you should watch how many lights a material can take. If your engine hands you a G-buffer, you are thinking deferred, and the question becomes what you can afford to store per pixel. Understanding the two paths stops the lighting code from being a black box and turns it into a decision you can defend.
The shape of each, in code¶
Forward lights each object as it is drawn; the loop runs once per object per light. Deferred writes a G-buffer, then runs the same lighting math once per pixel for every light. The lighting is nearly identical; only the data it reads, and where it runs, changes.
// ---- Forward: run while drawing THIS object, over the lights that reach it.
// Cost scales with objects x lights.
half3 LightForward(half3 albedo, half3 normalWS)
{
half3 color = albedo * _Ambient;
uint count = GetNumLights();
for (uint i = 0; i < count; i++)
{
Light light = GetLight(i);
color += albedo * light.color * saturate(dot(normalWS, light.direction));
}
return color;
}
// ---- Deferred: the geometry pass stores this, per pixel, with no lighting.
struct GBuffer
{
half3 albedo;
half3 normalWS;
half3 extra; // whatever your lighting model reads (emissive, spec, ...)
};
// Then one full-screen pass lights every pixel at once.
// Cost scales with pixels x lights, independent of object count.
half3 LightDeferred(GBuffer g)
{
half3 color = g.albedo * _Ambient;
uint count = GetNumLights();
for (uint i = 0; i < count; i++)
{
Light light = GetLight(i);
color += g.albedo * light.color * saturate(dot(g.normalWS, light.direction));
}
return color;
}
Notice the two lighting bodies are almost the same. The entire difference between the two render paths is that forward feeds them live per-object data, while deferred feeds them the stored G-buffer. When you read any engine’s lighting source, that is the seam to look for.
🔗 Related Chapters¶
The Render Pipeline • Lambertian • Ambient & Hemi
Related functions: Dot • Saturate
❤️ Support Shader Story¶
If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.
Your support helps keep this library open and free for everyone.