Home  Writing  Games  Music  Dev  About 

Deferred shading

is a screen-space shading technique performed on a second rendering pass, after the vertex and pixel shaders are rendered. On first pass of deferred shader, only data required for shading computation is gathered. Positions, normals, and materals for each surface are rendered into geometry buffer using render to texture. After this, a pixel shader computes the direct and indirect lighting at each pixel using the information of the texture buffers in screen space.

Advantages

Primary advantage is the decoupling of scene geometry from lighting. Only one geometry pass is requried, and each light is only computed for those pixels that it actually affects. This gives the ability to render many lights in a scene without a significant performance hit. There are some other advantages claimed for the approach. These advantages may include simpler management of complex lighting resources, ease of managing other complex shader resources, and the simplification of the software rendering pipeline.

Disadvantages

Deferred lighting

Deferred lighting (also known as Light Pre-Pass) is a modification of the Deferred Shading. This technique uses three passes, instead of two in deferred shading. On first pass over the scene geometry, only normals and specular spread factor are written to the color buffer. The screen-space, “deferred” pass then accumulates diffuse and specular lighting data separately, so a last pass must be made over the scene geometry to output final image with per-pixel shading. The apparent advantage of deferred lighting is a dramatic reduction in the size of the G-Buffer. The obvious cost is the need to render the scene geometry twice instead of once. An additional cost is that the deferred pass in deferred lighting must output diffuse and specular irradiance separately, whereas the deferred pass in deferred shading need only output a single combined radiance value.

Due to reduction of the size of the G-buffer this technique can partially overcome one serious disadvantage of the deferred shading - multiple materials. Another problem that can be solved is MSAA. Deferred lighting can be used with MSAA on DirectX 9 hardware.

Drawbacks

Myths from incorrect practices

When all the calculations are performed at pixel level (this is the only possible way to go with deferred shading), performance will be similar, because lighting pixel shaders for deferred renderers are not that much more complicated than those for forward renderers. The only added work is G-buffer sampling and, possibly, unpacking. But your application is much less likely to become bottlenecked by the CPU or the vertex pipe.

Optmisiations

Given the structure of a deferred renderer, the obvious places that are likely to be performance bottlenecks are deferring, lighting, and post-processing.

For S.T.A.L.K.E.R., we used a hierarchical occlusion-culling system that utilized both the CPU and the GPU. Our coarsest test was sector-portal culling followed by CPU-based occlusion culling (similar to the hierarchical z-buffer [Greene et al. 1993]). In a typical closed-space frame, this reduced the number of lights by up to 30 to 50 percent. Then we used DirectX 9's occlusion query to eliminate the completely occluded lights. Finally, we used a stencil mask to tag affected pixels for each light. All together, these culling optimizations resulted in a twofold or even larger performance increase.

Even with perfect culling, though, lighting is still the most expensive aspect of a deferred renderer. Therefore, it is important to make sure that the shaders and artistic properties of the light sources are optimized as much as possible.

Does the light need to cast shadows?
    
    – Many lights are needed only to mimic a global illumination-style look, and shadows just ruin the illusion.
    
    Does the light need a projective image?
    
    – If so, maybe the projection is the same as the shadow-map projection (which saves a few dp4s)?
    
    Does the light need to cast shadows from translucent surfaces? See Figure 9-3.
    09_deferred_shading_03.jpg
    
    Figure 9-3 Cast Shadows from Translucent Objects
    
    – If so, are there any translucent surfaces that exist in the light's frustum?
    
    – If both projective image and translucent color-modifier are required, maybe we can combine them into one image?
    
    Does the light need to contribute to glossy specular reflection?
    
    – Many lights in S.T.A.L.K.E.R. were added to mimic global illumination. For these lights, we used very simple diffuse-only lighting.
    
    Does the light move?
    
    – If it doesn't, we precomputed shadow-caster visibility to make shadow generation more efficient, by incrementally testing visibility for primitives that are found to be static to each other in a particular light configuration, thus forming a conservative potentially visible set (PVS) with a set of rules that can invalidate parts of it.

References

https://developer.nvidia.com/gpugems/gpugems2/part-ii-shading-lighting-and-shadows/chapter-9-deferred-shading-stalker https://leifnode.com/2015/05/tiled-deferred-shading/