Multiple Render Targets - Advanced Rendering - Shader Learning

Advanced Rendering

Multiple Render Targets

Task

Modify the rendering pipeline to store the scene's color, normal, world position, and depth data in separate dedicated textures (render targets), ensuring that each type of data is written without overlap.


Control which render targets are active by editing the .properties configuration file. Based on the selected targets, the post-processing shader receives the corresponding samplers: iRTBuffer0, iRTBuffer1, iRTBuffer2. The depth buffer iDepthBuffer is available by default.


Split the screen into four viewports and render each of the four render target textures into its own quadrant in the post-process.glsl file.

Theory

Multiple Render Targets (MRT) is a feature that allows a fragment shader to output to more than one render target simultaneously. This means that you can write different data to multiple textures in a single rendering pass. MRT is particularly useful for advanced rendering techniques such as deferred shading, where various types of information (e.g., color, normals, depth) need to be captured in separate textures for later processing.


How to Use MRT

  1. Create a framebuffer object.
  2. Create multiple textures and attach them to the framebuffer as color attachments.
  3. In the fragment shader, output to multiple targets using the layout qualifier.
layout(location = 0) out vec4 colorBuffer;
layout(location = 1) out vec4 normalBuffer;
layout(location = 2) out vec4 posBuffer;

location qualifier specify the render target index that a particular output writes to. By default the location is 0, so if we use one render target, we may not specify a layout for it:

out vec4 FragColor;

Blending Issues


When using MRT, it is often necessary to disable blending. Each render target is intended to store different types of information, such as color, normals, or depth. Blending operations, however, are typically designed to combine the output of the fragment shader with the existing content of the framebuffer. If blending is enabled, it can inadvertently mix the outputs leading to incorrect results.