[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ] [ Search: ]

4.9.13.2 Using the Deferred Shading Render Manager

Selecting the Deferred Render Manager

The simplest method to select the deferred render manager is to use the following configuration option:

 
Engine.RenderManager.Default = crystalspace.rendermanager.deferred

Alternately, the deferred render manager can also be loaded manually as follows:

 
rm = csLoadPlugin<iRenderManager> (objectRegistry,
  "crystalspace.rendermanager.deferred");

Note: When loading the deferred render manager manually the 'CS/data/config/engine.cfg' configuration file will not, by default, be present in the configuration manager. This file specifies configuration options used by the deferred render manager. Be sure to define these configurations option prior to loading the deferred render manager.

After being loaded, the engine must be instructed to use the deferred render manager as follows:

 
engine->SetRenderManager (rm);

Selecting the Deferred Rendering Technique

To select whether to use deferred shading or deferred lighting, you have to specify an according renderlayer configuration.

To use the default deferred lighting configuration use the following configuration option:

 
RenderManager.Deferred.Layers = /data/renderlayers/lighting_default_deferred.xml

To use the default deferred shading configuration use the following configuration options instead:

 
RenderManager.Deferred.Layers = /data/renderlayers/lighting_default_deferred_full.xml

Using Light Clipping Volumes

In addition to the usual settings a light may have, the deferred render manager supports an additional one: light clipping volumes.

These volumes allow you to further specify the influence via an arbitrary triangle mesh. To use them simply specify a trimesh (in light space) in the light's object model with id ‘clip’.

A simple example may look like this:

 
<light name='Test'>
  <center y='5.00' x='0' z='0'/>
  <color blue='0.0' green='0.0' red='0.5'/>
  <radius>10</radius>
  <type>directional</type>
  <move>
    <matrix>
    <roty>0</roty>
    <rotz>0</rotz>
    <rotx>-1.57</rotx>
    </matrix>
  </move>
  <trimesh>
    <id>clip</id>
    <box>
      <min x="-10" y="0" z="0" />
      <max x="10" y="10" z="10" />
    </box>
  </trimesh>
</light>

Filling the GBuffer

Before lighting can be computed the deferred render manager will fill the GBuffer with the current sectors geometry information using the ‘deferred full’ shader type for deferred shading or the ‘deferred fill’ shader type for deferred lighting. Each material can specify its own shader that will be used by the deferred render manager. Additionally materials may specify a ‘deferred use’ shader if deferred lighting is used that'll be used during the accumulation pass.

If no such shader is specified then the default shader for that type will be used:

deferred full

‘CS/data/shader/deferred/full/fill_buffer.xml’

deferred fill

‘CS/data/shader/deferred/lighting/fill_buffer.xml’

deferred use

‘CS/data/shader/deferred/lighting/use_buffer.xml’

Using the GBuffer

After the GBuffer is filled with the relevant information the deferred render manager will compute lighting information. To do this the render manager will iterate through all visible lights in the sector and draws proxy geometry that approximates the size and shape of the light source. Each light proxy is drawn using a shader that reads data from the GBuffer, computes the lighting equation, and adds it to the accumulation buffer.

The following types of light sources are supported:

point light

Represented as a sphere. The detail of the sphere can be controlled using the ‘RenderManager.Deferred.SphereDetail’ configuration option (defaults to 16). The proxy geometry is drawn using the ‘CS/data/shader/deferred/full/point_light.xml’ or ‘CS/data/shader/deferred/lighting/point_light.xml’ shader respectively.

spot light

Represented as a cone. The detail of the sphere can be controlled using the ‘RenderManager.Deferred.ConeDetail’ configuration option (defaults to 16). The proxy geometry is drawn using the ‘CS/data/shader/deferred/full/spot_light.xml’ or ‘CS/data/shader/deferred/lighting/spot_light.xml’ shader respectively.

directional light

Represented as a cube. The proxy geometry is drawn using the ‘CS/data/shader/deferred/full/directional_light.xml’ or ‘CS/data/shader/deferred/lighting/directional_light.xml’ shader respectively.

The deferred render manager will also output ambient light for the entire sector during the final accumulation pass using ‘CS/data/shader/deferred/full/use_buffer.xml’ for deferred shading and the specified shader for ‘deferred use’ for deferred lighting (see above).

Note: The shader include file ‘CS/data/shader/deferred/light_common.cginc’ and respectively ‘CS/data/shader/deferred/light_common.glsli’ contains a number of useful functions for use with light proxy shaders.

Default GBuffer Layout

The default GBuffer consists of 5 16-bit floating-point RGBA color buffers along with one 32-bit depth and stencil buffer for deferred shading and one 16-bit floating-point RGBA buffer with one 32-bit depth and stencil buffer for deferred lighting.

Note: Depending on hardware support the depth buffer might not contain any stencil bits.

The default data layout for deferred lighting and deferred shading is presented in the following table:

 
               +-----+-------+---------+-----------+
               | Red | Green | Blue    |  Alpha    |
+--------------+-----+-------+---------+-----------+
| buffer 0     | normal (XYZ)          | shininess |
+--------------+-----------------------+-----------+
| depth buffer |      depth and stencil value      |
+--------------+-----------------------------------+

               +-----+-------+---------+-----------+
               | Red | Green | Blue    |  Alpha    |
+--------------+-----+-------+---------+-----------+
| buffer 0     | normal (XYZ)          | shininess |
+--------------+-----------------------+-----------+
| buffer 1     | diffuse (RGBA)                    |
+--------------+-----------------------+-----------+
| buffer 2     | specular (RGB)        | glow (R)  |
+--------------+-----------------------+-----------+
| buffer 3     | ambient (RGB)         | glow (G)  |
+--------------+-----------------------+-----------+
| buffer 4     | static specular (RGB) | glow (B)  |
+--------------+-----------------------+-----------+
| depth buffer |      depth and stencil value      |
+--------------+-----------------------------------+

The data stored in the GBuffer is described below:

diffuse (RGBA)

The red, green, blue and alpha components of the geometries diffuse color.

specular (RGB)

The red, green, and blue components of the geometries specular color.

ambient (RGB)

The red, green, and blue components of the geometries ambient color.

normal (XYZ)

The xyz components of the geometries normal vector in view space. Normalized and mapped into the range [0,1].

depth and stencil value

The nonlinear post projection depth value and an optional stencil value.

Changing the GBuffer Layout

The layout of the GBuffer can be changed using the following configuration options:

RenderManager.Deferred.GBuffer.BufferCount

Controls the number of color buffers present in the GBuffer (defaults to 5 for deferred shading and 1 for deferred lighting). The maximum number of color buffers can be queried from the iGraphics3D device as follows:

 
const csGraphics3DCaps *caps = G3D->GetCaps ();
int maxColorBuffers = caps->MaxRTColorAttachments;
RenderManager.Deferred.GBuffer.BufferFormat

Controls the format of the color buffers (defaults to rbga16_f).

Note: Using 8-bits per color channel to store the view space normals can cause noticeable banding.

Note: It is the responsibility of the light proxy, ‘deferred full’, and ‘deferred fill’ shaders to account for changes in the GBuffer layout.

Light Proxy Shader Variables

The deferred render manager provides additional shader variables that are only available to the light proxy shaders:

tex gbuffer N

Specifies the Nth color buffer where N is between 0 and RenderManager.Deferred.GBuffer.BufferCount - 1.

tex gbuffer depth

Specifies the GBuffers depth buffer.

light position view

The position of the light in view space. Only available for point lights and spot lights.

light direction view

The direction of the light in view space. Only available for spot lights and directional lights.

The Accumulation Buffer

The accumulation buffer used by the deferred render manager to store lighting results is fixed to be the same size as the GBuffer. However, the format of the accumulation buffer can be controlled through the ‘RenderManager.Deferred.AccumBufferFormat’ configuration options (defaults to rgba16_f for deferred shading and rgb16_f for deferred lighting). Using a more compact format (rgb8 for instance) may be useful if high precision is not needed or memory is an issue.

Forward Shading Fallback

Due to the limitations of the deferred shading technique it is necessary for the deferred render manager to allow for some objects to be drawn using forward shading. The deferred render manager uses a meshes render priority (see section Render Priorities and Objects in Sectors) to determine if the mesh should be drawn using forward shading. The render priorities that the deferred render manager considers forward shading priorities can be controlled through the ‘RenderManager.Deferred.ForwardPriorities’ configuration option. This configuration option contains a comma separated list of render priority names (defaults to ‘alpha,transp,portal’). Any mesh belonging to one of these render priorities will be drawn using forward shading.

Note: Heavy portals must belong to a render priority that will be drawn using forward shading. Any heavy portal that is not drawn using forward shading will appear as solid black polygon.

Render Layers

In general the use of render layers is unnecessary for deferred shading because there is only one well defined layer. However, the deferred render manager must also support forward shading and, as a result, more complex render layers are needed. The render layers can be controlled through the ‘RenderManager.Deferred.Layers’ configuration option (defaults to ‘/data/renderlayers/lighting_default_deferred.xml’). The deferred render manager expects there to be either one render layer that specifies the ‘deferred full’ shader type or two render layers that specify the ‘deferred fill’ and ‘deferred use’ shader types respectively. Those layers are reserved for deferred rendering and will not be used during forward shading. Additionally you may specify a layer using the ‘z_only’ shader type which will be used by both forward and deferred rendering. All other render layers will be used for forward shading only.

Note: If multiple layers with the special shader types outlined above are specified only the first will be treated as explained above so you should not use those types in multiple layers.

Note: If layers with ‘deferred fill’ and/or ‘deferred use’ are specified in addition to an ‘deferred full’ layer are used they'll won't be treated specially (i.e. they'll be used for forward shading only) which is most certainly not what you want.

Note: If no render layer is specified with a ‘deferred full’ shader type and not both render layers specifying ‘deferred fill’ and ‘deferred use’ could be located then a default layer using ‘deferred full’ will be added that uses the ‘CS/data/shader/deferred/full/fill_buffer.xml’ shader.

Dynamic Shadows

The deferred render manager supports dynamic shadows using Cascaded Shadow Maps. To enable them simple specify the following configuration option:

 
RenderManager.Deferred.Shadows.Enabled = yes

To control the splitting distanced of the cascaded shadow maps you can use the ‘RenderManager.Deferred.NumSplits’ configuration option to set the number of splits that will be produced for each projector and ‘RenderManager.Deferred.FarZ’ to specify the maximum distance from the camera for which shadow maps will be produced.

To control the resolution of the produced shadow maps you can use the control option ‘RenderManager.Deferred.ShadowMapResolution’ (point and spot lights default to half this resolution) or for a finer grain of control you can also set the resolutions for specific light types individually using the following control options:

 
RenderManager.Deferred.ShadowMapResolution.Directional
RenderManager.Deferred.ShadowMapResolution.Spot
RenderManager.Deferred.ShadowMapResolution.Point

Debug Commands

The deferred render manager implements the iDebugHelper interface and responds to the following debug commands:

toggle_visualize_gbuffer

Toggles a display of the GBuffer at the bottom of the screen. The GBuffers color buffers will be displayed in order from left to right followed by the GBuffers depth buffer and then the accumulation buffers. The debug command can be activated through BugPlug (see section Using the BugPlug Debugging Plugin) using the command ctrl+d, ctrl+shift+alt+g.

toggle_visualize_lightvolumes

Toggles a display of the light proxy geometry drawn for each light. The debug command can be activated through BugPlug using the command ctrl+d, ctrl+shift+alt+l.

toggle_debug_flag draw.pssm.split.frustum

Toggles a display of all shadow maps produced during this frame. The debug command can be activated through BugPlug using the command ctrl+d, ctrl+shift+alt+f.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated using texi2html 1.76.