Sie sind auf Seite 1von 9

JUL22ND,2011

SpecularShowdownintheWildWest

Yousee,inthisworldtherestwokindsofpeople,myfriend:
Thosewithloadedgunsandthosewhodig.Youdig.Blondie

SaddleUp!
Inthispost,Illbereviewingsomeexistingmethodsforattainingwellbehavedspecularlighting.Illalsocoverasimple
twistonthesethatfitsbetterwithcurrentgamelightingapproachesandconsolememoryconstraints.
WhatdoImeanbywellbehaved?Imtalkingaboutavoidingspecularhighlightshimmeringonbumpysurfaces,aswell
asachievingtherightappearanceinthedistance:thecombinedeffectofthesebumpsasindividualwrinklesand
irregularitiesbecometoosmalltomakeout.Canwedoallofthisonabudget?Letshitthetrailandfindout!

TheGood,theBadandtheUgly
Somedaysitfeelslikethoseofusinvolvedinvideogamerenderingareabunchofcowboys:weplayfastandloosewith
thelawsofthelandaswerushtogetthingsuponscreenintime,wranglingpixelsalongthewaytoproducethelook
thatwe(andourartists)want.
Latelywevebeenrightingsomewrongsbyadoptinglinearlighting[1]andphysicallybasedshadingmodels[2]
somethingevenourmorecivilisedneighboursinfilmhaveonlyrecentlybeentransitioningto(seeotherpresentations
fromthesamecourse).Thatsallwellandgood,butbeforewegetaheadofourselvesandstartbelievingthattheWild
Westdaysareover,theresanothermajorareathatweneedtobetacklingbetter:aliasing.

AsDanBakerrightlyargues[3],aliasingisonekeydifferentiatorbetweenusrealtimefolkandtheofflineguys.Whilst
theycanaffordtothrowmoresamplesattheproblem,heavysupersamplingwouldbetooslowforus.(Admittedly,I
haventtried[4]inproduction,butImnotexpectingittoperformwelloncurrentconsoles.)MSAAisalsoineffective
asitonlyhandlesedgealiasing,soundersamplingartefactswithinshadingwillremainspecularshimmeringbeinga
primeexample.PostprocessAA(forwhichtherearenowmanypotentialoptions[5])isntreallyhelpfuleithersinceit
doesnothingtoaddresssharphighlightspoppinginandoutofexistenceasthecameraorobjectsmove.Finally,you
mightthinkthattemporalAAcouldbeasolution,butthatsreallyjustapoormanssupersamplingacrossframes,with
addedrelianceontemporalcoherency[6].
Insummary,thestandardAAtechniquesweuseingamesareprettyhopelessforcombatingshimmeringparticularly
forhigherspecularpowersandnoneofthemachievethedistancebehaviourthatwewanteither.Performanceaside,I
stronglysuspectthatevensupersamplingfallsshortinthatcaseunlessanobscenenumberofsamplesareusedtogether
withcustomtexturefiltering,otherwisebumpinformationwillbeaveragedaway.(Well,notentirelymoreonthislater.)
So,whatotheroptionsdowehave?Atanearlierconference[7],Dancoveredtwoworkaroundscommonlyemployedby
developers:scalingdownbumpinessorglossinessinthedistance.Thefirstisreallywrongthough,asitgivesusthe
oppositeofwhatwewant:ratherthanabumpysurfacelookingdullerwhenfurtherfromthecamera,flatteningthe
normalmapleadstoamoreglossyappearance!Instead,reducingthespecularpoweristoafirstapproximationthe
rightthingtodo.Althoughitssomethingthatneedstobetweakedonacasebycasebasisanddoesntworkcorrectly
fornormalmapswithbothbumpyandflatareas,itsstillbetterthansimplylivingwithaliasingoravoidinghighpowers
altogether.Ishouldaddthattexturespacelightingalsogetsamention,butitsanotherheavyweightalternativewithits
ownsetofproblems,soIwontdiscussitfurtherhere.(Theideaofmarryingthiswithadynamicvirtualtexturecache
bogglesmymindthough!)
Alltold,areweresignedtobeingransackedbybadlybehavingspecularanduglyshimmering?Maybenot,astheresa
newsheriffintown

CLEANinguptheStreets
LEANMapping[8]isarecentapproachforrobustfilteringofspecularhighlightsacrossallscales(withthepossible
exceptionofmagnificationmoreonthisinabit).Notonlydoesitmodelthemacroeffectofsurfaceroughnessinthe
distancereallywellevengeneratinganisotropichighlightsfromridgednormalmapsbutitalsosupportscombining
layersofdynamicbumpsatruntime(forafewdollarsmore,naturally).
Thereisntreallythespacetogointothegrittydetailsofhowitworksforthat,youcancheckthereferencesbutits
definitelyatthemorepracticalendofthesolutionspectrumcomparedtomanyprevioustechniques.Itshippedwith
CivilizationVafterall,andfrommyexperiencesofartheresultsareimpressive.
Thatsaid,therearesomesignificantroadblockspreventingimmediate,widespreadadoption:
Heavystoragedemands
Anisotropic,tangentspaceBeckmannformulation
Offthebat,memoryrequirementswillbealimitingfactorformanydevelopers.NotonlydoesLEANMappingneedtwo
texturesinplaceofastandardnormalmap(andmaybemoretocombinelayers),buttheoverheadiscompoundedby

peskyprecisionrequirements,forsimilarreasonsasVarianceShadowMaps[9].Firaxisdidmanagetosqueezethings
downto8bitperchannelstorageinsomecases,butthepaperadvises:Ingeneral,8bittexturesonlymakesenseif
absolutelyneededforspeedorspace.
Unsurprisingly,thisrulesoutDXT1orDXT5,whicharetwoofthemostcommoncrossplatformformatsfornormal
mapsoncurrentconsoles.Bycomparison,wecouldbefacingatleast8timesthestoragecost(possiblylessifthe
normalisrecoveredfromthecoreLEANtermsandotherdataispackedinitsplace).Yowzers!
Thingsgetevenrougherfordeferredrendering,whereGbufferspaceistypicallyascarcecommodityandtomake
mattersworse,thelightingformulationfurthercomplicatesthings.Evenassumingthateverythingcouldbemovedtoa
commonspace(suchasworldspace),wewouldstillneedtostoreanadditionaltangentvector!Theresalsomoremaths
involvedwithLEANMapping,sothatsanotherimportantconsiderationregardlessofhowwechoosetolightour
environments.
So,overall,itscertainlynotthedropinreplacementthatitmightfirstappear.Fortunatelythesheriffhasadeputywhos
quickeronthedraw:atthisyearsGDC,Danshowedoffacutdownversion,CLEAN(CheapLEAN)Mapping[3],that
sacrificesanisotropyforlowerstoragerequirementsroughlyhalfandslightlyhigherperformance.Thatsasignificant
improvementandlosinganisotropyeffectivelyresolvesthesecondissueaswell,butthefootprintoverDXTisstill
rathersteepformytaste.
WhatIdreallylikeissomethingthatcanbeappliedliberallywithouthavingtoseriouslyreevaluateartbudgets(and
bythat,Iofcoursemeanmakingcutselsewhere).Dontgetmewrong,Ithinkthat(C)LEANMappingisareally
excitingadvance,Ijustdontexpecttoseeitbeingusedwithwildabandononcurrentgenerationconsolesasthings
stand.Still,itsagreatoptiontokeepinmindforspecificsituations,withCiv5swaterservingasacaseinpoint.

WeNeedToGoCheaper
Isthereanythingouttherethatcouldgiveusmorebangforourbuck?Actually,waybackin2004,MichaelToksvig
presentedabeautifullysimpletechnique[10]thatmuchlike(C)LEANMappingtakesadvantageofMIPmapping
andhardwaretexturefiltering,butestimatesbumpvariancedirectlyfromthelengthsoftheaveragednormalvectors
storedinanexistingnormalmap.Sinceitdoesnthaveacatchynameassuch,IllrefertoitasToksvigAA.
Fromthepaper,theoriginalBlinnPhongformulationis:

1 + f t s Na . H
(
)
spec =
1+s
|N a |

ft s

,wheref t =

|N a |
|N a | + s(1 |N a |)

spec=1+fts1+s(Na.H|Na|)fts,whereft=|Na||Na|+s(1|Na|)

N a Naistheaveragednormalreadfromthetexture,fromwhichwecalculatethesocalledToksvigFactor,f t ft.Thisis
thenusedtomodulatethespecularexponent,s s,andtheoverallintensitydependingonthevariance(roughness).
Heresthesamethingincode,withsomeminortweaks:

1
2

float len = length(Na);


float ft = len/lerp(s, 1, len);

3
4

float scale = (1 + ft*s)/(1 + s);


float spec = scale*pow(saturate(dot(Na, H))/len, ft*s);

Intheoriginalversion,allofthisisstoredina2DLUTthatsindexedby dot(Na, H) and dot(Na, Na) ,inwhich


casethe CLAMP textureaddressmodewouldtakecareofthe saturate() forus.WhereasusingaLUTmademore
sensebackinthedaysofPixelShaderversion2.0andlower,wecanjustevaluatethingsdirectlyintheshader,which
removestheneedforaLUTperspecularexponentandthussidestepspotentialcacheandprecisionissues.
1+f s

Thefirstpartoftheequation, 1+st 1+fts1+s,alsolooksabitbogussinceitsonlyhandlingenergyconservationinone


directionandImprettysurethatthose1sshouldreallybe2s[2].TreatingBlinnPhongasaNormalDistribution
Function(NDF)[2],whatIsuspectyoureallywantis:

spec =

p+2
Na
(N . H) p ,whereN =
andp = f t s
8
|N a |
spec=p+28(N.H)p,whereN=Na|Na|andp=fts

ForthoseofyoualreadyusingenergyconservingBlinnPhong,thisshouldlookfamiliarandalsoquiteelegant:youre
justscalingthespecularexponentandtheneverythingjustworks.
Thismaybeobvious,butfordeferredrenderingyoullwanttodothisduringyourinitialscenepass,asthenyoure
scalingjusttheoncebeforepackingtheexponentintotheGbuffer,ratherthanforeverylight.Italsomeansthatyoure
stillfreetousecleverGbufferencodingsofnormals,suchasBFN[11]ortwocomponentsinviewspace[12].
Unfortunately,thisbringsusontoamajorproblemwithToksvigAA:youcantuseitwithtwo
componentinputnormals,suchas3Dcand(typically)DXT5.Thewholebasisofthetechniqueisthatlocalroughness
(divergentnormals)isapproximatelycapturedbythelengthofthefilterednormal,soitsnogoodtryingtodothiswith
encodingsthatreconstructaunitvector!Furthermore,evenifweuseDXT1exclusivelyfornormalmaps,compression
willstillmesswithvectorlength.Itseasytoforgetthissinceweoftengetacceptableresultsforlightingafter
renormalisation,butitcanreallyplayhavocwithToksvigAA.Givenallthat,itsnotsurprisingthatthetechniquehasnt
beenpickedupbydevelopers.
Wasthisyetanotherdeadend?Actually,no,asitsbroughtusabitclosertoasolution.

TheWildHunch
Heresanembarrassinglysimpleidea:ToksvigandLEANMappingexploittexturefiltering,sowhydontwedothis
offline?Letsseewhathappensifwetakeournormalmap(Figure1)andprecomputetheToksvigFactorsfroma
gaussianfilteredversionofeachMIPlevel.

Figure1:Normalmap
WhatweendupwithisshowninFigure2.Theresultsareimmediatelyintuitive:areasintheoriginalnormalmapthat
wereflatarewhite(glossy),whereasnoisy,bumpysectionsaredarker.

Figure2:Toksvigmap
Withtherightkernelsize,thisToksvigMapmatchesupreallywellagainsttheruntimeToksvigmethod,whichyoucan
seeforyourselfwiththedemoattheend.Itsevenbetterundermagnification(compareFigure3aand3b),asToksvig
AAshowsblockinessduetothediscontinuousnatureofbilinearfilteringcubicinterpolationwouldfixthis.(C)LEAN
mappingsuffersfromthesamekindofartefacts,butwedontgetthiswiththebakedversionbecauseweveprefiltered
withagaussian.

Figure3a:ToksvigAAundermagnification

Figure3b:Toksvigmapundermagnification
Whatweeffectivelyhavehereisanautogeneratedantialiasingglossmap,andunlikethe(C)LEANterms,it
compressesreallywell!Also,ifthismapisgeneratedaspartoftheartimportpipeline,thenwerefreetousehigh
precisionthreecomponentnormals,priortothecompressionmethodofourchoosing.So,withthisstraightforward
change,weveovercomethetwobigobstaclesofmemoryconsumptionandprecision.
Usingthismapistrivialasitsjustlikeanyotherglossmap:

1
2
3
4

float
float
float
float

ft = tex2D(gloss_map, uv).x;
p = ft*s;
scale = (p + 2)/8;
spec = scale*pow(saturate(dot(N, H)), p);

However,theobservantreaderwillnoticethattheToksvigMaphasbeengeneratedwithaparticularspecularpowerin
mind.Fortunately,youcanbakewithafixedpowerthatgivesreasonablecontrastaround100seemstoworkwelland
thenconvertlaterifyouneedthatflexibility(e.g.fortexturereuseormaterialpropertychanges):

ft /= lerp(s/fixed_s, 1, ft);

Anotheroptionistostoretheadjustedpower,f t s fts,astheexponent(range[0,1])ofamaximumspecularpower,as
suggestedbyNatyHoffman[2].Thislogspaceglossinesstermcouldbemoreintuitiveforartiststoworkwithandmay
removetheneedforaseparatematerialpropertyfortheexponent,plusitsalsoaconvenientformatfordirectGbuffer
storage[13].Athirdoptionistostorethevarianceorthelengthofthenormalinsteadanddotheremainingmathsat
runtime.
Thedataitselfcouldbepackedalongsideaspecularmaskorusedtomodulateanexistingglossmap.Icouldeven
imaginethemapbeinguseddirectlyasastartingpointforartiststopaintontopof,butinthatcasecarewillbeneeded
toavoidreintroducingaliasing!
Glossmapsarearguablymoreimportantthanspecularmasksinthecontextofphysicallybasedshading[2],butartists
aretypicallymorecomfortablewiththelatter.Additionally,withthehighintensityrangethatspossiblewithenergy
conservingspecular,antialiasingisevenmorecritical.Forthosereasons,Ifindthewholeideaofanautogenerated
texturetobereallyappealing,eventhoughIhaventtestedallofthisoutinproductionyet.Imabigfanofarttoolsthat
do8090%oftheworkautomatically,butstillprovideawaytogoinanddirectly,locallytweaktheoutput.Theresultis
lesshairpullingandmoretimededicatedtopolishing.Hopefullythisisanotherexampleofthat.

GunfightattheO.K.Corral
HowdoesToksvigMappingcomparetoLEANMapping?Well,itscertainlynotasgoodonaccountofthelackof
anisotropy,whichcanmeanoverbroadeningofthespecularhighlightinsomecasesandnotenough(leavingdampened
aliasing)inothers.However,itspossibletobakeCLEANMappinginstead,whichreducesremainingshimmeringatthe
costofalittlemorehighlightblooming,sinceittendstoconservativelyattenuate.
ThereasonwecandothisisthatthemainthingseparatingToksvigandCLEANinpracticeisthemeasureofvariance(
2 2):
2
toksvig

1| N a |
|N a |

2
clean

M z (M x2 + M y2 )

1
1+s 2

toksvig2=1|Na||Na|clean2=Mz(Mx2+My2)p=11+s2s
EverythingIalreadymentionedearlierwithrespecttoToksvigMapsbaking,evaluating,convertingbetweenpowers,
etc.appliesheretoo.IveevenhadsomesuccessbakingLEAN,butIllleavetalkingaboutthatforanothertime.
EventhoughLEANMappingisclosertothegroundtruth,bakedToksvig/CLEANMappingisstillahellofalotbetter
thandoingnothing,whichispreciselywhatmostofusaredoingatthemoment.

Rideem,Cowboy!

HeresasimpleWebGLdemothatallowsyoutotogglebetweenstandardBlinnPhong(default),ToksvigAAand
ToksvigMap.Youcanalsoedittheshadercodeonthefly!
Demoupdate:IvehadreportsofvisualissueswiththeToksvigMapoptionandNVIDIAGPUs.Ifitappearsasthough
youremissingMIPmaps,thenupgradingtothelatestdrivers(285.62+)shouldfixtheproblem.
BesidestheshimmeringwithBlinnPhong,noticehowtheteapotremainsshinywhenyouzoomout(mousewheel)as
bumpsdisappear.Incontrast,thematerialmaintainsitsappearancewithToksvig.
Ivealsocreatedanotherlittleexamplethatdemonstratesthefilteringprocess.

IntotheSunset
Phew,thatwasaratherlongpost!PerhapsIcouldhavejustsaid:StoretheToksvigFactorinatexture,butIenjoyed
thejourneyofgettingthereandIhopeyoufounditinterestingtoo!
IllfollowupwithmorethoughtsandanexpandeddemoatalaterdateonceIvehadtimetoinvestigatefurther
alternatives.Inthemeantime,Iminterestedtohearfromanyonewhosexploredthisarea.Besidesclearlybeinga
subjectthatkeepsDanBakerupatnight,IspottedthatJasonMitchellhadexperimentedwithSpecVarmaps[14]for
TeamFortress2[15]andNatyHoffmanalsosharedsomethoughtsonearlierworkhere[16].Beyondthatthough,I
haventseenmuchdiscussionoutsideofthereferencedliterature,soImallears!

Acknowledgements
Inadditiontothegreatworkofallthecitedauthors,Idalsoliketoacknowledgesourcesofinspirationandcodeforthe
demo.Theliveeditingenvironmentis(orwillbe)heavilyinspiredbyIigoQuilezsShaderToyandTom
BeddardsFractalLab.ItmakesuseofseveralopensourcecomponentssuchasAceandjQuery,plusvariousUI
widgets.SomeWebGLutilitycodewastakenfromMikeActons#AltDevBlogADaypost,whichbuildson/refactors
thisChromiumdemo.Illsavemoredetailsontheeditorforafuturepost.
Finally,thenormalmapisborrowedfromRenderMonkey.ItrustthatthisisokayasIcouldntfindalicenceanywhere,
butpleaseletmeknowifthatisntthecase.Thereisntawholelotoffreelyavailabledataoutthereandthatparticular
texture,withitsroughandsmoothareas,makesforagoodexample.

References
[1]dEon,E.,Gritz,L.,TheImportanceofBeingLinear,GPUGems3.
[2]Hoffman,N.,CraftingPhysicallyMotivatedShadingModelsforGameDevelopment,PhysicallyBasedShading
ModelsinFilmandGameProduction,SIGGRAPHCourse,2010.
[3]Baker,D.,SpectacularSpecularLEANandCLEANspecularhighlights,GDC2011.
[4]Persson,E.,Selectivesupersampling,2006.
[5]FilteringApproachesforRealTimeAntiAliasing,SIGGRAPHCourse,2011.
[6]Swoboda,M.,DeferredRenderinginFrameranger,2009.
[7]Baker,D.,ReflectanceRenderingwithPointLights,PhysicallyBasedReflectanceforGames,SIGGRAPH

Course,2006.
[8]Olano,M.,Baker,D.,LEANMapping,I3D2010.
[9]Donnelly,W.,Lauritzen,A.,VarianceShadowMaps,I3D2006.
[10]Toksvig,M.,MipmappingNormalMaps,2004.
[11]Kaplanyan,A.,CryENGINE3:ReachingtheSpeedofLight,AdvancesInRealTimeRendering,SIGGRAPH
Course,2010.
[12]Pranckeviius,A.,CompactNormalStorageforsmallGBuffers,2009.
[13]Coffin,C.SPUBasedDeferredShadinginBATTLEFIELD3forPlaystation3,GDC2011.
[14]Conran,P.,SpecVarMaps:BakingBumpMapsintoSpecularResponse,SIGGRAPHSketch,2005.
[15]Mitchell,J.,Francke,M.,Eng,D.,IllustrativerenderinginTeamFortress2,NPAR2007.
[16]Hoffman,N.,LightingPapers,2005.
[17]AkenineMller,T.,Haines,E.,Hoffman,N.,RealTimeRendering3rdEdition,A.K.Peters,Ltd.,2008.

Das könnte Ihnen auch gefallen