Shading in Valve’s Source Engine
SIGGRAPH 2006

Jason Mitchell

ValveIntroduction¾ World Lighting• Radiosity Normal Mapping• Specular Lighting¾ Model lighting• Irradiance Volume• Half-Lambert• Phong terms¾ High Dynamic Range Rendering• Tone mapping• Autoexposure¾ Color Correction

Timeline¾ Half-Life 2 and Counter-Strike: Source (November 2004)• Baseline shading model we’ll present today¾ Lost Coast (October 2005)• High Dynamic Range Rendering¾ Day of Defeat: Source (September 2005)• Color Correction and HDR¾ Half-Life 2: Episode One (June 2006)• Phong shadingEpisodic Content Î Incremental Technology¾ The extensions to the baseline Half-Life 2 technology
have been shipped in subsequent products in theyear and a half since HL2 shipped.
• More efficient way to develop technology• Delivers value to customers and licensees¾ Shorten the delay between hardware availability andgame support

Marketplace Context

See full survey at http://www.steampowered.com/status/survey.html

World Lighting with Radiosity¾ Realism¾ More forgiving than direct lighting only

• Avoids harsh lighting situations• Less micro-management of light sources for content

production• Can’t tune lights shot-by-shot like movies. Don’tknow what the shots are, and don’t want to take theproduction time to do thisDirect lighting onlyRadiosity lighting

Normal Mapping¾ Reusable high-frequency detail¾ Higher detail than we can currently get fromtriangles¾ Works well with both diffuse and specular
lighting models¾ Can now be made to integrate with radiosity

Radiosity Normal Mapping¾ We created this technique to efficiently address thestrengths of both radiosity and normal mapping¾ In other engines, per-pixel lighting terms are often
accumulated one light at a time
• Multiple lights handled by summing multiple light contributions
within or between passes¾ We express the full lighting environment in our novel

basis in order to effectively perform diffuse bumpmapping with respect to an arbitrary number of lights¾ This is the key to shading in Valve’s Source engine

Basis for Radiosity Normal Mapping 1 1 1 
,
,
−

6
2
3


1
1 
 1
,−
,

−
6
2
3







3
1 
,0 ,

2
3 

Computing Light map Values¾ Traditionally, when computing light map values using aradiosity preprocessor, a single color value is calculated¾ In Radiosity Normal Mapping, compute light values foreach vector in our basis• This triples light map storage, but we are willing to bear this costfor the added quality and flexibilityAt the pixel level. . .
¾ Sample three light map colors, and blend between thembased on the transformed vector

float3 dp;
dp.x = saturate( dot( normal, bumpBasis[0] ) );dp.y = saturate( dot( normal, bumpBasis[1] ) );dp.z = saturate( dot( normal, bumpBasis[2] ) );dp *= dp;
diffuseLighting = dp.x * lightmapColor1 +dp.y * lightmapColor2 +dp.z * lightmapColor3;

Light Maps Only

Variable Luxel Density
Coarser light maps usedin low frequency areas

High density typically
4”×4” luxels

Final Rendered Scene

World Specular Lighting¾ We use cube maps for specular lighting the world¾ Designers place the sample locations for specular lighting¾ Cube maps are pre-computed in-engine from the level
data using rendering• Especially interesting for HDR, which we’ll discuss later¾ World surfaces pick up the “best” cube map, or cube
maps can be manually assigned to surfaces to fix
boundary problems

Environment probes
placed in Level Editor

Specular on World GeometryDiffuse Lighting Only

Specular from cube maps

Other World Lighting¾ There are a few other special cases• Reflective and refractive water• Special effects such as the Citadel Core in Episode One• Some simple geometry which is just vertex lit

Model Lighting¾ Prior to Episode One, models (objects which aren’t

part of the immovable world geometry) usedprimarily view-independent lighting terms:• Two local diffuse lights with Half-Lambert• Directional ambient term (ambient cube)¾ Also cubic environment mapping terms¾ These terms maxed out ps_2_0, which was our highend when we shipped Half-Life 2 in November 2004

Half Lambert¾ Typically clamp

N·L to zero at theterminator¾ Half Lambertscales the -1 to 1
cosine term (redcurve) by ½,
biases by ½ andsquares to pull thelight all the way
around (bluecurve)¾ Another example of choosing aforgiving lighting model¾ Similar to Exaggerated Shading
apaper by Rusinkiewicz et al

Lambertian Term

Half LambertIndirect Illumination in Games¾ Quake III used a filtered sample

from a 3D array of ambient terms toprovide a constant ambient for agiven character [Hook99]¾ MotoGP used a hemisphere lighting
model where bounced light from theground varied spatially throughout
the race track [Hargreaves03]¾ Max Payne 2 stored linear 4-term
spherical harmonic coefficients foruse in lighting characters and othermoving geometry [Lehtinen06]

From [Hargreaves03]

Ambient Cube
¾ Pre-compute irradiance samples

throughout the environment¾ 4’×4’×8’ density¾ The set of samples is referred to in
the literature as an irradiancevolume [Greger98] and eachsample defines an irradianceenvironment map [Ramamoorthi01]

Directional Ambient

Constant Ambient¾ Directional ambient term which includes indirect light¾ Lights beyond the first two can be added to the ambient cube

Ambient Cube Basis+y¾ Six RGB lobes stored in shader constants¾ More concise basis than first two orders of sphericalharmonics (nine RGB colors)¾ Pixel shader constant store is currently a bottleneck,though this eases quite a bit on ps_3_0¾ Developed in parallel with other techniques such as[Ramamoorthi01]

-x
-z
+x

float3 AmbientLight( const float3 worldNormal ){float3 nSquared = worldNormal * worldNormal;
int3 isNegative = ( worldNormal < 0.0 );float3 linearColor;linearColor = nSquared.x * cAmbientCube[isNegative.x]+
nSquared.y * cAmbientCube[isNegative.y+2] +nSquared.z * cAmbientCube[isNegative.z+4];
return linearColor;
}

+z
-y

Comparison of Ambient Cube to Spherical Harmonics

Original Light Probes

Irradiance Environment
Maps from[Ramamoorthi01]

Valve Ambient
Cubes

Benefits of Half Lambert and Ambient Cube+

Lambert 1+

Lambert 2+

Half Lambert 1=Constant Ambient+

Half Lambert 2

Most Games=

Ambient Cube

Source Models

Benefits of Half Lambert and Ambient Cube

Lambertian terms + constant ambient

Half Lambert + ambient cube

Extensions to Earlier Model Lighting¾ When we shipped Half-Life 2, ps_2_0 was the mostadvanced pixel shader model available¾ In the spring of 2006, 40% of our users have hardware
which supports ps_2_b or higher• Of the people who have played Episode One in the twomonths since it has shipped, 62% have such hardware¾ For Episode One and a recent update to Day of Defeat:Source, we used these longer ps_2_b shaders to add

Phong terms to model lightingps_2_b¾ Shader model on ATI RADEON Xx00 parts¾ Same programming model as ps_2_0 but with longer
shaders• ps_2_0 – 64 alu and 32 texture• ps_2_b – 512 total…any mix of alu and texture¾ ATI RADEON Xx00 & X1x00 series

NVIDIA GeForce 6x00 & 7x000 series

Phong Terms¾ Compute Phong terms

for up to two local lights¾ Can specify specularexponent variations
across a model• Currently, map blends
between specularexponent of 1 and 150¾ Modulate with scalarmask channel andtweakable Fresnel term

Specular Exponent

Specular Mask

Episodic Alyx Shade Tree*

Diffuse
Diffuse 11

AlbedoAlbedo

+

Final
Final
Diffuse
Diffuse+
Diffuse
Diffuse 22

Total
Total
Diffuse
DiffusePhong
Phong Mask
Mask
Light
Light
sourcesourceparametersparameters

Final Lighting*

Ambient
Ambient
Cube
Cube

Raw
Raw Phong
Phong

Phong
Phong
Exponent
Exponent

FresnelFresnelSpecularSpecular
Term
Term

High Dynamic Range
Rendering in the
Source Engine

High Dynamic Range¾ A High Dynamic Range image is an image that has agreater contrast range than can be shown on astandard display device, or that can be captured witha standard camera with just a single exposure¾ High Dynamic Range rendering performs
intermediate operations such as shading in highdynamic range space¾ The rendered HDR results are then mapped to alimited-range display

Real-World Sky at Multiple ExposuresScene from Lost Coast at Multiple Exposures

HDR Skybox¾ Authored by painting multiple exposures of the sky toallow for real-time exposure adjustment

Authored Sources of HDR Illumination¾ HDR sky box authored by painting multiple exposuresof the sky• Start with seamless LDR sky texture and paint HDR

information from there• We use a combination of Photoshop and HDRShop to makeedits / touchups at different exposure levels• The industry has not addressed this issue adequately at thispoint and there is an opportunity for a tool vendor to step up
and fix this

¾ Local lights with HDR values placed in levels

Generated Sources of HDR Illumination¾ HDR light maps are generated offline using ourradiosity solver and stored in RGBE format. At run
time, these are stored in a 16bit per channel format¾ HDR Cube maps are generated by the engine using
the HDR skybox as well as the HDR light sources andthe HDR light maps
• The result is that an object which reflecting the sun or someother bright part of the scene, you will see this in the full
effect of the brightness in the reflection, which is one of the
key advantages of HDR renderingHDR water reflection and refraction

LDR

HDR

Tone Mapping¾ Very careful with linear versus gamma color spaces¾ All shaders in our DirectX 9 code path are single-pass¾ As a result, tone mapping can be performed during
normal rendering• Simple subroutine added to the end of all pixel shaders¾ This allows us to use 32-bit RGBA render targets• No increase in render target memory footprint• MSAA supported• 60% of our total users see HDR rendering• 82% of those who have played Episode One so far!

Auto Exposure¾ Running luminance histogram of post-tonemapped frame• Update one bucket per frame• More histogram buckets for low luminance values¾ Each frame, we determine which output pixels fall within agiven range, tagging such pixels in the stencil buffer¾ Use an asynchronous occlusion query to count pixels in range¾ Full running histogram (not just a single average luminance) isavailable to the CPU with no stalls¾ Time averaging is used smooth out auto exposure adjustments¾ Designers can also modify auto exposure and bloom acrossdifferent areas of a level or in connection with game events

A single bucket from the running histogram (shown in red)Another bucket from the running histogram (shown in red)Implemented Multiple HDR Paths¾ Floating point• HDR textures and render targets are fp16
• Shipped this path, but it is not the one typically used
¾ Integer• Render to 32 bit RGBA integer frame buffer• Store HDR textures in fp16 (linear color space) when filteredfp16 textures are supported.
• Store HDR textures in 4.12 linear color space otherwise.
• Had to implement this version along with floating point
version to support more hardware

Float vs. Integer HDR Tradeoffs¾ Floating Point+ Pre-tone-mapping data available for blooming+ General refraction mapping techniques preserve HDR information- Requires floating-point alpha blending- Hardware lacks precision to perform fixed function pixel fog¾ Integer+ Compatible with Multisample Antialiasing (MSAA)+ Works on all DirectX 9 hardware (runs well even on ATI RADEON 9600)+ Small performance hit relative to LDR

Half Life 2: Episode 1

Half Life 2: Episode 1

Half Life 2: Episode 1

Team Fortress 2: NPR + HDR!

Team Fortress 2: NPR + HDR!

Team Fortress 2: NPR + HDR!

Lost Coast
DemoColor Correction¾ Color has been used in visual arts to evoke particular emotional

responses for millennia, and we can do the same in video games¾ It is very natural to apply color correction to rendered images as apost process, since we’re doing lots of image processing already• Simply define a mapping from a one set of RGB triples to another• We do this with a 323 volume texture lookup (sometimes several)¾ Decouples color correction from the art or lighting of a given setting¾ Useful for a variety of purposes• Stylization• Day for night• Gameplay

Color Correction in Source¾ Color correction in games can go beyond what is

possible in film
¾ The dynamic nature of games is both a curse and ablessing• More difficult to tweak to specific scenes• Far more potential to exploit the dynamic nature of games• Strong feedback loops with the player• Additional sideband communication with the player¾ Color correction is a powerful tool for art direction• Mod authors and licensees can differentiate their titles

Desaturation
Original Image

Desaturated

Future Directions¾ Improvements toirradiance volume
sampling¾ Shadow Mapping¾ Foliage Rendering¾ Soft Particles¾ Non-Photorealistic
rendering in Team

Fortress 2

Conclusion¾ World Lighting• Radiosity Normal Mapping• Specular Lighting¾ Model lighting• Irradiance Volume• Half-Lambert• Phong terms¾ High Dynamic Range Rendering• Tone mapping• Autoexposure¾ Color Correction

SDK & Academic Licensing¾ Publicly available SDK¾ Academic licenses provide• Access to Valve games• Source code
• HLSL shaders, Radiosity and visibility calculations• AI system, path finding• Animation system, acting system, inverse kinematics• Production quality art and sound assets• Useful level and modeling tools• Hammer level editor, Faceposer, Model viewing utilities

academiclicensing@valvesoftware.com

Acknowledgements¾ Many thanks to Gary McTaggart, Chris Green,

James Grieve, Brian Jacobson, Ken Birdwell,
Bay Raitt and the rest of the folks at Valve who
contributed to this material and theengineering behind it¾ Thanks also to Paul Debevec for the lightprobes used in the comparison withRamamoorthi’s Irradiance Environment Maps

References¾ [Debevec00] Paul Debevec, Personal Communication, 2000.
¾ [Diefenbach97] Paul J. Diefenbach, “Multi-pass Pipeline Rendering: Realism For Dynamic
¾
¾
¾
¾
¾
¾
¾
¾
¾

Environments,” Proceedings, 1997 Symposium on Interactive 3D Graphics, 1997.
[Franke06] Scott Franke, Personal Communication, 2006.
[Green06] Chris Green and Gary McTaggart, “High Performance HDR Rendering on DX9Class Hardware,” Poster presented at the ACM Symposium on Interactive 3D Graphics
and Games, March, 2006.
[Greger98] Gene Greger, Peter Shirley, Philip M. Hubbard and Donald P. Greenberg,“The Irradiance Volume,” IEEE Computer Graphics & Applications, 18(2):32-43, 1998.
[Hargreaves03] Shawn Hargreaves, “Hemisphere Lighting with Radiosity Maps”,
Gamasutra, August 2003.
[Hook99] Brian Hook, “The Quake 3 Arena Rendering Architecture,” Game Developer’s
Conference 1999.
[Lehtinen06] Jaakko Lehtinen, Personal Communication, 2006.
[McTaggart04] Gary McTaggart, “Half-Life 2 Shading,” GDC Direct3D Tutorial 2004.
[Ramamoorthi01] Ravi Ramamoorthi and Pat Hanrahan, “An Efficient Representation for
Irradiance Environment Maps,” SIGGRAPH 2001, pages 497-500.
[Rusinkiewicz06] Szymon Rusinkiewicz, Michael Burns, and Doug DeCarlo. “Exaggerated
Shading for Depicting Shape and Detail,” ACM Transactions on Graphics (Proc.
SIGGRAPH). 25(3) July 2006.

Questions?

Valve is Hiring!

jobs@valvesoftware.com