Graphics Reference
In-Depth Information
void main ()
{ vec3 worldNormal = vertexWorldNormals ;
vec2 texCoord = gl_FragCoord . xy ￿ pixelSize ;
vec3 diffuse = texture2D ( diffuseTex , diffuseTexCoord ). rgb ;
vec4 lightIndices = texture2D ( lightIndexTex , texCoord );
// Do a lighting calculation for each channel in the texture(s)
// Not checking if a light index is valid means calculating some
// useless lighting calculations
vec3 lightIntensity = doLightCalc ( lightIndices . r , worldNormal );
lightIntensity += doLightCalc ( lightIndices . g , worldNormal );
lightIntensity += doLightCalc ( lightIndices . b , worldNormal );
lightIntensity += doLightCalc ( lightIndices . a , worldNormal );
gl_FragColor = vec4 ( diffuse ￿ lightIntensity ,0.0);
}
Listing 2.6. Scene rendering in light indexed deferred rendering.
The lighting is calculated for each channel in the current texture. With some
GPU architectures it is often more optimal to do the lighting calculation for each
channel even if it is not used. This is because branches in the shader will cause
stalls in the pipeline. An example of calculating the lighting for a given pixel is
shown in Listing 2.6.
Sorting lights. Light indexed rendering has a maximum number of lights per pixel
that can be shown. If the number of lights per pixel exceeds this then the applica-
tion needs to decide what to do. The application can have a maximum of as many
lights per pixel as we can render to multiple textures. However, if this maximum
is reached during execution, then lights will need to be discarded, throwing away
the light influence of one or more lights from one frame to the next. In order
to make this as un-noticeable as possible, the lights should be sorted so that the
lights that take up the least screen space or are smallest in contribution to the
final image are discarded first. There is also a possibility of joining multiple small
lights into one larger one to reduce the number of lights per pixel.
This section has described how to use light indexed rendering in OpenGL ES
2.0 and 3.0; the next section describes how to optimize each of the techniques
using OpenGL ES extensions.
2.4 OpenGL ES Extensions
This chapter has covered various deferred rendering techniques and how to im-
plement them using OpenGL ES 2.0 and 3.0. Each of these APIs has various
OpenGL ES extensions that may be utilized to optimize the techniques. This
Search WWH ::




Custom Search