55
Spectacular Specular -LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Embed Size (px)

Citation preview

Page 1: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Spectacular Specular-LEAN and CLEAN specular highlights

Dan BakerFiraxis Games

Page 2: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

MotivationObservations from Movie folks

• “The most important aspect of rendering for a movie is anti-aliasing”

• “Games today still don’t look as good as animated movies 12 years ago.”

• “Why in video games does everything look so shiny?”

Page 3: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Reality Check• Have a habit of comparing ourselves against

other games• Therefore miss the obvious: Some of our

materials don’t look anything like they should• And they alias• And they sparkle

Page 4: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Shiny things• Water, Metal, commonly seen in games• Used for water in Civilization V, but metals

suffer from similar problems• Will tour two common lighting models, Phong

and Blinn Phong• Both have huge problems

Page 5: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Phong• Simple to implement• Since L is constant for environments, can turn

specular part into a preconvolved environment

Psp LRKNLKLRF )()(),(

Page 6: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Sometimes accurate

Good for perfect reflectors,like still water. But, perfect reflectors = high power

Page 7: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Problems with Phong• Aliases• Can’t get elongated reflections• Pretty inaccurate – very plastic look to it• No good way to add normals maps together• Anisotropic (grooved) materials will lose there

anisotropy at zoom• Can’t use high powers or else!

Page 8: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Problems with Phong

Classic Scene, sunset. Can’t get this with Phong lighting

Page 9: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Blinn Phong• Much more accurate if we have real lights in

our scene• Can get elongated shapes• Cheap to evaluate

psd HNKpLNK )()(

Page 10: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Blinn Phong problems• Aliases• Shading becomes very wrong with roughness• Highlights change based on pixel coverage, and become• Anisotropic (groved) materials will lose there

anisotropy at zoom• Can’t do environments easily• Can’t use high powers or else!

Page 11: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Spectacular Specular fail

A very rough wave filters to a perfectly flat standing water,An example

Down sampled of the final render

What Blinn-Phong gives us

Page 12: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Bump Filtering

Exactly the same substance, but one side is wrinkled. This completely changes the reflections. (Thanks to Chipotle for the foil)

Page 13: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Overviews problems• “The shimmies”, “The speckles” Lots and lots of talks about

this problem• The more substantial the normal map, the higher the power,

the more noise we get. Lots of artist tweaking, limits to our data

• Reflections are just plan wrong at distant scale, makes objects way over-shiny

• Can’t add normal maps together easily to get detail maps

Page 14: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Why this happens • The integral of a function over a range of

inputs isn’t the same as function with inputs integrated over a range

i iii

RR

IwFIFw

drRFdrRF

)()(

)()(

Where R is a region of a texture, F is our shader (in this case, a Phong or Blinn Phong shader) . The second version is a discrete version, where W is the sample weights from our hardware filtering.

Page 15: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

How do Movies solve this?• Typically use REYES, or more

advanced techniques• Roughly equivalent of shading

every relevant texel and averaging the results

• Very expensive, potentially thousands of shader evals per pixel

Page 16: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Dreaming the dream• Ideal lighting model:

– Can use any power we want– Will deal with zooming in and out correctly– Won’t alias– Easy to use: compatible with our current pipeline– Relatively inexpensive– Can add normals together– Can use all of our MIP hardware

Page 17: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Formal definition• What we really want is to build a replacement

for Blinn Phong that has this property (where F is basically our shader):

i i

ii IwFIFw )()(

Page 18: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

LEAN Mapping• Linear Efficient Antialiased Normal Mapping• Considered Fast Antialiased Reflectance Texture Mapping• Fast and flexible solution for bump filtering

– Shiny bumps won’t alias– Distant bumps will change surface shading– Directional bumps will become anisotropic highlight

• Allows blending layers of bumps• Works with existing Blinn-Phong pipeline

Page 19: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Scale problem solved

LEAN MapNormal Map

Page 20: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Prior Work• Posed by Kajiya 1985• Monte-Carlo

– Cabral et al. 1987, Westin et al. 1992, Becker & Max 1993• Multi-lobed distributions

– Fournier 1992, Han et al. 2007• Single Gaussian/Beckmann distribution

– Olano & North 1997, Schilling 1997, Toksvig 2005• Diffuse [Kilgard 2000]

Page 21: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Beckman Shading Model

2tan2

2

1 s

e

b

tb hh

e

1

2

1

2

1

The math is simpler then it looks. We are raising the power based on the distance of the half angle from the normal. This second formulation rolls the power into a covariance matrix, thereby giving us anisotropic power (e.g. two powers, one for X and one for Y).

Page 22: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Probability Distributions in Shading• Distribution of microfacet normals

– Perfectly reflective facets– Only facets oriented with reflect to – Look up probability of in distribution

• Beckmann distribution– Gaussian of facet tangents = projection

Page 23: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Filtering• Filter is linear combination over kernel• Linear representation → any linear filter

– Summed Area, EWA, …– MIP map, Hardware Anisotropic

• We need a BRDF that is linear

Page 24: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Filtering: Gaussians• Gaussian described by mean and variance

– –

• Mean combines linearly• Variance does not, but second moment does

Page 25: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Blinn-Phong ↔ Beckmann• Blinn-Phong approximates Gaussian [Lyon 1993]

• Better fit as increases• Variance , normalize

with

Page 26: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Blinn-Phong ↔ Beckmann

Blinn-Phong Beckmann

Page 27: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

LEAN mapping• Blinn-Phong ↔ Beckmann• Filtering Bumps• Sub-facet shading• Layers of bumps

Page 28: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Distributions & Bumps• If the normal is changing our surface

orientation, is there any way to add them together?

• Does that even • meaning?

Page 29: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

LEAN Mapping• Beckman distribution can be broken into pieces

that filter, but doesn’t deal with the normals.• Key insight: We think of the normal instead as a

shift of the distribution of microfacets

)()(2

1 1

2

1 nnt

nn bhbhe

Page 30: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Distributions

Beckman distribution works on a 2d plane. The blue discs represent the distribution of normals. Rather then change the orientation of the surface, we simply shift the center location of the distribution of normals by the x,y component of the normals. Thus, we interpret the normal as a shift in distribution, rather then a change in surface orientation

Page 31: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Filtering Bumps• Rather than bump-

local frame• Use surface

tangent frame– Bump normal =

mean of off-center distribution

Page 32: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Bumps vs. Surface Frame

Surface-frame BeckmannBump-frame Beckmann

Page 33: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

LEAN Data• Normal (for diffuse)

• Bump center in tangent frame

• Second moments

Page 34: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

LEAN Use• Pre-process

– Seed textures with , and – Build MIP chain

• Render-time– Look up with HW filtering– Reconstruct 2D covariance– Compute diffuse & specular per light

Page 35: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Sub-facet Shading• What about base specularity?

– Given base Blinn-Phong exponent,– Base Beckmann distribution

• One of these at each facet = convolution– Gaussians convolve by adding ’s– Fold into , or add when reconstructing

Page 36: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

LEAN Map features• Seamless replacement for Blinn-Phong• Specular bump antialiasing• Turns directional bumps into anisotropic

microfacets

Page 37: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Bump Layers• Uses

– Bump motion (ocean waves)– Detail texture– Decals

• Our approach– Conceptually a linear combination of heights– Equivalent to linear combination of

• Even from normal maps

Page 38: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Bump Layers: The Tricky Part• What about ?

– – – Expands out to , , and terms– terms are in , terms are in – terms are new:

– Total of four new cross terms

Page 39: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Layering Options1. Generate single combined LEAN map

– Mix actual heights, or use mixing equations– Time varying: need to generate per-frame– Decal or detail: need high-res LEAN map

2. Generate mixing texture– One per pair of layers– Decal or detail: need high-res LEAN mixture maps

3. Approximate cross terms– Use rather than a filtered mixing texture

Page 40: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Single LEAN MapMixture TextureApproximationSource 2Source 2Source 1

Layer Options

Source 1

MIP Biased

Mixed

Page 41: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

PerformanceSingle Layer Two Layers

Blinn-Phong LEAN Per-frame Mix texture Approx

ATI Radeon HD 5870

1570 FPS 1540 FPS 917 FPS 1450 FPS 1458 FPS

D3D Instructions 30 ALU1 TEX

42 ALU2 TEX

50 ALU3 TEX

54 ALU5 TEX

54 ALU4 TEX

1600 x 1200, single full screen object

Page 42: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Converting Blinn-Phong Data • So fast could be done at load time

float3 tn = tex2D(normalMap, coord);

float3 N = float3(2*tn.xy-1, tn.z);

float2 B = N.xy/(ScaleFactor&N.z);

float3 M = float3(B.x*B.x + 1/s, B.y*B.y + 1/s, B.x*B.y)

Output.lean1 = float4(tn, .5*M.z + .5)

Output.lean2 = float4(.5*B + .5, M.xy)

Page 43: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Texture Compression and Precision• Normal maps get big, painful to compress• Lean MAPs require 5 fields• x,y, x^2, xy, y^2• Caveat: The precision matters. Unlike other

techniques, we are using the normal filtering hardware

Page 44: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Obligatory Shader Codefloat4 f4BaseMeshColor = tex2D(BaseMeshColor, f2BaseTexCoord);

float4 f4BaseColor = tex2D(LeanTextureMap1, f2BaseTexCoord);

float Var = tex2D(LeanTextureMap2,f2BaseTexCoord).x;

float GradientScale = g_fLeanMapScale;

float VarianceScale = GradientScale*GradientScale;

float2 Gradient = float2(f4BaseColor.x*2-1,f4BaseColor.y*2-1) *GradientScale;

float3 Covar = float3(f4BaseColor.zw , Var*2 - 1) * VarianceScale;

// turn moments into elements of covariance matrix, matrix is

mat4(Covar.x,Covar.z,Covar,z,Covar.y)

Covar -= float3(Gradient.xy*Gradient.xy, Gradient.x*Gradient.y);

float3 Half = normalize(ViewDir + LightDir);

//Transform half angle back into tangent spaceHalf = mul(mTS, Half);

float2 HalfCenter = Half.xy/Half.z - Gradient.xy;

//Now calculate the specfloat Cxx = Covar.x + 1/g_fExp, Cyy = Covar.y + 1/g_fExp, Cxy = Covar.z;float Cdet = Cxx*Cyy - Cxy*Cxy;float e = (Cyy*HalfCenter.x*HalfCenter.x + (Cyy*HalfCenter.y - 2*Cxy*HalfCenter.x)*HalfCenter.y)*.5/Cdet;

fExp = (Cdet<=0 || e>=10 | Half.z < 0) ? 0 : exp(-e)/sqrt(Cdet);

Page 45: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Typical strategy• Remember that our is stored power is 1/s• Simple normalized texture, pow 32 = 4 bits

precision, pow 128 = 2 bits!• Can renormalize range, to capture some bits• If we want to use very high powers, e.g.

10,000+, really need 16 bits precision

Page 46: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Water For Civilization V• Lots of background, but why did we do this?• Needed to make water that worked at a

distance, not a smooth reflection• And, wanted a realistic wave combing effect• Does not use a reflection map, high powers let

us use an analytic model istead

Page 47: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Civ 5’s water• Linear combination of 4

moving bump maps• Allows us to accurate

wave directions

Page 48: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Can we make a cheaper version?• CLEAN Mapping

– An extension to LEAN mapping developed after paper published

– Common art problem: Went to 5 values, hard to drop into most pipelines, and need more precision

– Can we make it use less values• CLEAN mapping Cheap Linear Efficient Antialiased Normal

Mapping.

Page 49: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Dropping Anisotropy• Cool feature of LEAN maps, but efficiency might be

more important• Let’s examine the Beckmann distribution again

• Be really nice if we could make only 1 value instead of 3

)()(2

1 1nn

tnn bhbh

e

Page 50: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Dropping Terms• Can just approximate the covariance matrix

with a diagonal matrix

• Then store just X^2 + Y^2 in addition to X,Y

)var(0

0)var(22

22

YX

YX

)var(

)()(

2

122 YX

bhbh nnt

nn

e

Page 51: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

CLEAN Mapping• Now we have only 3 terms to store. X, Y, X^2 +

Y^2, can store in 3 values• Then, calculating the variance:

)( 22yxz MMMV

),,( zyx MMM

V

MHMH yyxx

e

22 )()(

2

1

Page 52: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Combining CLEAN Maps

2122)1(1

21

21

))1(2,2(2

))1(1,1(1

BBMttMM

BBM

BBM

tMtMB

tMtMB

zzz

yyy

xxx

yx

yx

Page 53: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Coming CLEAN• Most of the high level benefits of LEAN

mapping• About half the data costs• Does not support anisotropy

Page 54: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Conclusions• Normal map filtering = solved problem• Cheap, easy to make art for• Huge Visual Impact• NO EXCUSE to have messy specular!

Page 55: Spectacular Specular - LEAN and CLEAN specular highlights Dan Baker Firaxis Games

Thanks• Marc Olano – can find I3D paper on his

website• Firaxis Games