Specular Color in Light Pre-pass Renderer

Light pre-pass and inferred lighting have some nice properties that make them popular alternatives to traditional deferred rendering. Like a deferred renderer, a light pre-pass renderer generates a G-buffer and then accumulates the lighting using screen space operations. A light pre-pass renderer differs from a deferred renderer by only computing part of the lighting equation in the passes. The full equation is then computed in a final pass.

Here is the basic equation for computing the lighting for a surface using the Blinn-Phong reflection model:

D \sum C_i (N \cdot L_i) + S \sum C_i (N \cdot H_i)^G

D is the diffuse reflectance of the surface, S is the specular reflectance, G is the glossiness and N is the surface normal. These are all properties of the material and are constant over all of the light passes. The light-dependent parameters are L (the direction to the light source), C (the color of the light source) and H (the half vector).

With a light pre-pass renderer, the calculations are performed as they are organized in this equation. First the renderer loops over all of the lights computing the summed terms. Then a final pass is performed in which the lighting results are combined with the material parameters D and S.

By doing the lighting this way you can reduce the width of the G-buffer, since you don’t need to store the diffuse or specular components (we do however need the glossiness). In the case of inferred lighting, you can also compute the lighting at a lower resolution than the final image while still having full resolution textures.

A light pre-pass renderer is typically implemented using a RGBA render target and each pass accumulates the diffuse part of the equation in the RGB channels and the specular part in the A channel. While we would ideally like to store the diffuse and specular components in 3 channels each, this requires writing into multiple render targets. Currently support for alpha blending into multiple render targets is uncommon and the result would require more bandwidth and memory. Since the specular component in our original equation is modulated by the light color we lose information when we store it in a single channel of the render target.

With this adjustment to the equation, here’s what we’re computing in the lighting passes:

\begin{tabular}{ l c l } \({target}_{RGB}\) & \(=\) & \(\sum C_i (N \cdot L_i)\) \\ \({target}_A\) & \(=\) & \(\sum {intensity}(C_i) (N \cdot H_i)^G\) \end{tabular}

One addition is the intensity function. This is used to convert the light color into a monochrome value so that dim lights will have less pronounced specular highlights. A photometric function could be used, or we could treat the color as a vector and compute the length; we’ll return to this later.

One way to reconstruct the lighting in the final pass is to scale the diffuse component by the material’s diffuse reflectance and the specular component by the specular reflectance and add them together:

D * {target}_{RGB} + S * {target}_A

Because the specular lighting component is monochrome, we’re computing the lighting as if the light sources were all white; the highlight will pickup the color of the material, but it won’t be affected by the original color of the light. If all of your lights are white — like in an office building perhaps — this is fine. However, when you have intense colored lights the results can look a little strange:

Fortunately, there’s a simple approximation we can apply to get much better results. Since the diffuse lighting includes the color of the light source encoded in it, with a little manipulation we can extract it and apply it to the specular component.

In the case of a single light source, the RGB portion of the render target simply contains the light color multiplied by diffuse attenuation term. If we treat the color as a vector, we can decompose it into a normal vector and a magnitude:

\begin{tabular}{ l c l } \({target}_{RGB}\) & \(=\) & \(C_0 (N \cdot L_0)\) \\ & \(=\) & \(\hat{C_0}\left \| C_0 \right \| (N \cdot L_0)\)  \\ \(\hat{C_0}\) & \(=\) & \({normalize}({target}_{RGB}), \mbox{for} \; (N \cdot L_0) \neq 0\) \end{tabular}

The final equation shows the normalized light color extracted from the render target. Note that the required condition will always be true where we want the specular highlight to appear. If we use the length of the light color as our intensity function during the lighting passes, then multiplying by the normalized color in the final pass gives us the correct result in the case of a single light source.

D * {target}_{RGB} + S * {normalize}({target}_{RGB}) * {target}_A

Here’s what the result looks like when we extract the light color from the diffuse and apply it to the specular:

As mentioned, this only holds for a single light source. When there are multiple light sources our calculations become an approximation. Thankfully, the results look quite good in real world scenarios with this method and there’s very little cost to including it in a light pre-pass or inferred renderer. Comparing with a reference image, it’s easy to see the discrepancies caused by this approximation, but when viewed on its own the artifacts are not very noticeable.

6 Responses to “Specular Color in Light Pre-pass Renderer”

  1. trinity.ns said:

    May 21, 10 at 4:17 pm

    Wow, that’s a great post. Where do you get your knowledge from max? Just curious, my lectures don’t teach that kind of stuff.

  2. Light Pre-Pass vs Deferred Renderer – Part 1 | Gamedev Coder Diary said:

    Apr 11, 11 at 4:04 am

    [...] only works correctly for a single light but it’s definitely better than nothing – see Max’s Programming Blog for a nice explanation of [...]

  3. Angie said:

    May 02, 13 at 10:04 pm

    This is a really good tip especially to those new to the blogosphere.
    Short but very precise information… Thanks for sharing this one.
    A must read post!

  4. ask a girl out advice said:

    Jul 19, 13 at 3:01 pm

    You don’t ask your friend to hang out that way do you. By making it an activity that you’ll attend whether or not
    she can go makes it easier for her to say yes. Making it sound casual
    will make her less paranoid herself.

  5. mobile games said:

    Apr 14, 14 at 1:02 am

    Wow, that’s what I was seeking for, what a data!

    existing here at this weblog, thanks admin of this web page.

  6. Edna said:

    Sep 06, 14 at 1:35 pm

    If some one wishes to be updated with latest technologjes therefore hee must be
    visit this web site and be up to date daily.

Leave a Reply