D3DBook:(Lighting) Summary

From GDWiki
Jump to: navigation, search

Contents

Comparison and Summary

All the content for lighting has been covered by the previous nine chapters. Because of the significant number of methods and algorithms discussed an additional reference chapter is included. It is this final chapter that covers a comparison and summary of the key points of interest with each approach discussed so far. For further information on the points summarised here, refer back to previous chapters.

Global Versus Local Illumination

In simple terms there are two phases to lighting – transport and interaction. The complexities of both phases can be varied. Some key points:

Global Local
Based on physical understanding of light energy’s interaction with the entire environment Only considers relationship between source and destination.
Tend to be very expensive in terms of performance Can be easily implemented for real-time performance
Generates much higher quality images Generates acceptable quality images
Shadowing is implicitly included Requires an explicit shadowing algorithm to be implemented

There is a lot of active research into global illumination and over the previous few years a lot of advances in performance have been made. Techniques such as pre-computed radiance transfer and spherical harmonics are popular when it comes to bridging the real-time gap. It would not be surprising if global illumination becomes a realistic option for general lighting in real-time applications in the future.

Light Sources and the Lighting Environment

  • The type of source affects the distribution of light energy within the environment and consequently affects the appearance of the receiving surface.
    • Light sources can be either direct or indirect, where indirect sources are related to global illumination approaches.
    • Directional lights are good for modelling light sources a great distance away from the scene that cover a large area – e.g. sunlight.
      • Directional lights only have a direction and no position or attenuation
    • Point lights are robust, general-purpose sources. They emit light energy in equal amounts across all directions and are good at modelling sources that have obvious sources – such as light bulbs.
      • Point lights have position and attenuation but no direction.
    • Spot lights are special cases of point lights where surrounding geometry blocks emission of light except for one particular direction. They are good at representing projected light such as torches
      • Spot lights have position, direction and attenuation
    • Area lights are very important when representing real-world lighting and fall into the indirect category. Use of many point lights can be a sufficient approximation as a true implementation is still difficult with current real-time hardware.
      • Area lights share the properties of a point light but for obvious reasons don’t have a single point of origin.
  • Different sources have different performance profiles:
    • Directional light (fastest)
    • Point light
    • Spot light
    • Area light (slowest)
  • The number of light sources in a scene is also an important consideration
    • Effective placement and use of light sources is crucial
  • Light type, quantity and placement are all good candidates for Level Of Detail (LOD) algorithms.

Architecture

The use of lighting models should be an integral part of any graphics architecture – it is possible to shoe-horn lighting models into existing code, but consideration in the design phase is always better. Consider the following:

  • Single or multiple passes can be used.
    • Each light source in its own pass…
      • … is easiest to implement
      • … is more flexible
      • … is slower due to overhead of repeated computation
    • Multiple lights per pass…
      • … is not substantially more difficult to implement
      • … single light per pass can just be a special case of the same multiple lights per pass shader code.
      • … gains flexibility and scalability across different types of hardware
      • … is usually faster, but longer shader programs with loops may not suit all hardware. Using conditional compilation to generate many permutations during the build-process and then selecting the appropriate permutation at runtime is a common choice.
  • Where large numbers of lights with complex models are to be used then deferred shading should be considered.
  • Level-Of-Detail algorithms should be standard functionality. Scale according to significance and contribution to the final image where possible.
    • Mix-and-match terms can be employed to provide greater control over performance and quality.

Lighting Resolution

The resolution of lighting calculations has a big impact on the quality of the final image. Refer to the following images from the third chapter:

image:Image_10.1.png
Image 10.1

From left to right image 10.1 shows per-triangle, per-vertex and per-pixel. Left-most is cheapest whereas right-most is most expensive. Both per-face and per-vertex derive their performance from the complexity of the geometry being rendered; per-pixel also varies according to the output resolution.

There are four types of per-pixel lighting to consider. In order of performance (fastest to slowest) and quality (lowest to highest):

  • Phong shading. This is a step-up from per-vertex lighting and the Gouraud shading used. Instead of the final colour evaluated at each vertex being interpolated the inputs from each vertex are evaluated and the lighting model is evaluated for each pixel. It improves resolution but does cannot represent per-pixel variations of inputs. The sample code for the fourth through to ninth chapter is implemented in this style.
  • Normal mapping. The normal vector used in evaluating the lighting model is read from a texture mapped across the surface of the geometry. This allows for a fine degree of control over the inputs but can suffer due to the lack of parallax making the surface appear flat.
  • Parallax mapping with offset limiting. An extension of normal mapping that adds the important parallax effect to improve perceived realism. An additional ‘height map’ texture is required to add this.
  • Ray-traced. This blurs the boundaries between traditional polygonal and ray-traced computer graphics approaches. Generates a high quality image with correct self-occlusion and parallax whilst also offering the potential for self-shadowing. Has very high texture sampling requirements (up to 150 samples for every pixel could be necessary) and relies upon branching and looping constructs which are not always optimal on all types of hardware.

There are several storage-reduction methods available for per-pixel lighting techniques:

  • Tangent-space transformations require additional per-vertex attributes. The storage format can be varied to reduce the overhead for these. Direct3D 10’s new geometry shader can be used to eliminate these additional per-vertex attributes.
  • The format used to store normal maps and height maps can be varied, including block-compressed formats.
  • Normal maps can be made redundant by generating normal vectors directly from the height map.

Types of Materials

If realism is the priority then understanding the material being modelled is essential. The human brain is very good at picking up mismatches or incorrect results for materials it is familiar with in the real-world.

Real-world values for energy response, colour and other inputs can be hard to find but do exist. Matching values between rendered images and real-world measurements can make a noticeable difference.

Lighting Models

Six main lighting models were introduced in this section of the book. These should cover most uses but are by no means the only lighting models available. For more specific requirements it is worth researching other available models – searching online for other work by the authors of the papers referenced in this section is a good starting point.

Some basic highlights of the models are as follows:

Phong and Blinn-Phong

  • Standard model used in many forms over the last 30 years
  • Simple mathematical derivation makes it both flexible and non-representative
  • Good for plastics

Cook-Torrance

  • Good for metals
  • Introduces the micro-facet model
  • Spectral reflectance based on the Fresnel term

Oren-Nayar

  • Non-lambertian
  • Diffuse-only model
  • Good for rough surfaces

Strauss

  • Does not introduce anything fundamentally new
  • Primary aim is to be flexible with well defined inputs – puts usability first
  • Two main inputs – smoothness and metalness
    • Allows for a wide range of materials

Ward

  • Empirical model based on observed data
  • Isotropic and anisotropic forms
  • Isotropic is a simplified special case of anisotropic

Ashikhmin-Shirley

  • Anisotropic model
  • Good for metal and plastic materials
  • Has a non-Lambertian diffuse term to aid conservation of energy

It is worth noting that individual models may perform well for particular classes of materials but it rarely means that these are the only classes of materials they support. Appropriate use of controlling parameters allows most models to be general purpose.

Performance

Drawing general conclusions about performance for a massively parallel system such as found with CPU(s) and GPU(s) is very difficult. However the assembly output from the HLSL compiler can be a reasonable indicator as to which approaches are likely to be the most expensive to execute:

Light source Approximate number of instructions
Directional 19
Point 38
Spot 44
Area 46-548

The per-pixel techniques all rely on supporting data – a normal map, height map or both. The following table includes the storage assuming 512x512 textures stored at 8 bits per channel:

Per-pixel technique Approximate number of instructions Storage required (megabytes)
Normal mapping 9 1.33
Parallax with offset limiting 15 1.58
Parallax with offset limiting (simple normal generation) 28 0.25
Parallax with offset limiting (Sobel filter) 53 0.25
Ray-traced 68 1.58
Ray-traced (simple normal generation) 81 0.25
Ray-traced (Sobel filter) 105 0.25
Ray-traced with shadows 114 1.58
Ray-traced with shadows (simple normal generation) 126 0.25
Ray-traced with shadows (Sobel filter) 151 0.25

The above table lists the number of instructions for the pixel shader component (obviously the most significant for per-pixel techniques!) but they all rely on a 36 instruction vertex shader to perform the tangent-space transformations.

With a framework of light source and per-pixel lighting technique it is necessary to include a lighting model to generate the final colour. The following table summarises the performance characteristics for all of the methods shown in previous chapters – they utilize Phong shading as their framework.

Lighting model Approximate number of instructions
Blinn-Phong 23
Phong 26
Ward (Isotropic with look-up) 33
Oren-Nayar (simplified evaluation with look-up) 38
Cook-Torrance (look-up roughness, simple Fresnel) 44
Ward (Isotropic) 44
Ward (Anisotropic) 48
Cook-Torrance (Beckmann roughness, approximated Fresnel) 52
Cook-Torrance (look-up roughness, simple Fresnel) 54
Cook-Torrance (Gaussian roughness, approximated Fresnel) 54
Strauss 60
Oren-Nayar (simplified evaluation) 62
Cook-Torrance (Beckmann roughness, simple Fresnel) 62
Cook-Torrance (Gaussian roughness, simple Fresnel) 65
Ashikhmin-Shirley 71
Oren-Nayar (full evaluation) 77

With three fundamental choices when implementing a lighting model (light source, per-pixel technique and lighting model) it is difficult to represent all performance combinations in a manageable form. However, using the previous three tables it is possible to put some rough estimates together for how a particular method is likely to perform.

For example, combining a spot-light with ray-traced per-pixel lighting and Ashikhmin-Shirley’s model is going to produce a very complex shader whilst a point light, parallax mapping and Phong combination produces a much shorter option.

It is important to realise that the implementations of algorithms provided in this section are not exhaustively optimized – they are not intentionally sub-optimal, but clarity and simplicity was chosen over complex but potentially faster methods.

Several chapters show how look-up textures can be used to balance between storage space and instruction counts. This basic principle can be employed in many other areas and is an important optimization opportunity.

The advanced programming constructs that come as standard in shader model 4 – such as looping and branching – also offer numerous optimization opportunities. Expensive evaluations can be dynamically eliminated by branching on an important input; for example only executing the lighting calculation for surfaces oriented towards the light requires a simple if( dot( normal, light ) >= 0.0f ) { /* code */ } construct.

Whilst the software side of Direct3D 10 can efficiently express these constructs it is worth noting that not all hardware will do such a good job. In previous generations different GPU’s could have wildly different performance characteristics for dynamic flow control. This made it difficult to write one shader that would get the best performance from all hardware and it is quite possible that the same will happen with Direct3D 10 hardware. The exact details of this are beyond the scope of this book and referring to IHV provided information is the best approach.

Navigate to other chapters in this section:

Foundation & Theory Direct Light Sources Techniques For Dynamic Per-Pixel Lighting Phong and Blinn-Phong Cook-Torrance Oren-Nayar Strauss Ward Ashikhmin-Shirley Comparison and Summary
Personal tools