Deferred Shading
Task
Implement deferred shading with given G-buffer textures (iRTBuffer0, iRTBuffer1, iRTBuffer2) and an array of lights c_Lights. Use the Blinn-Phong ambient, diffuse and specular lighting model with halfway vector and attenuation with given constants (kc, kl, kq).
Theory
Deferred shading is an advanced rendering technique used in computer graphics to efficiently handle complex lighting calculations. Unlike traditional forward rendering, where lighting calculations are performed for each object as it is rendered, deferred shading separates the rendering process into two distinct passes: the geometry pass and the lighting pass. This separation allows for more efficient and flexible lighting calculations, especially in scenes with many light sources.
1. Geometry Pass
In the geometry pass, the scene's geometry is rendered, and various attributes of each fragment are stored in multiple render targets (MRT), collectively known as the G-buffer. The G-buffer typically includes color, normal, position and depth textures.
To eliminate the need for a separate position texture, you can reconstruct the fragment position from it's depth value.
2. Lighting Pass
In the lighting pass, the G-buffer textures are used to perform lighting calculations. Since all the necessary information (color, normals, positions, depth) is already stored in the G-buffer, the lighting calculations can be performed in screen space.
Advantages
1. Efficient Handling of Multiple Lights. Deferred shading decouples geometry rendering from lighting calculations, allowing for efficient handling of scenes with many light sources. Each light is only computed for the pixels it affects
2. Reduced Overdraw. Since lighting is calculated in screen space, deferred shading avoids the overdraw problem common in forward rendering, where multiple objects might cover the same pixel.
3. Flexibility in Lighting Models. Deferred shading allows for complex lighting models and effects, as all necessary information (color, normals, positions) is available in the G-buffer.
Disadvantages
1. Transparency Handling. Deferred shading struggles with transparency, as it requires additional passes or techniques like depth peeling to handle transparent objects.
2. High Memory Bandwidth. The G-buffer can be large, consuming significant memory bandwidth and storage.
3. Anti-Aliasing Challenges. Hardware anti-aliasing (MSAA) does not work well with deferred shading, requiring post-processing techniques like FXAA or MLAA to achieve smooth edges.
4. Multiple Materials. Handling multiple materials can be challenging, as it requires more data to be stored in the G-buffer.