Stencil Buffer Trick for Volume Deferred Shading - Daqi's Blog
文章推薦指數: 80 %
Here is a nice stencil buffer trick introduced by Yuriy O'Donnell (2009). What it do is basically using the stencil buffer as a counter to ... Togglenavigation Daqi'sBlog Home Publications Music Photo About Deferredshadinghavebeenknowntolargelyboosttheefficiencyofrenderingwhentreatingalargeamountoflights.Themechanismisverysimple:separatethegeometrycomplexityfromlighting.Todothis,G-bufferswhichformanarrayoftextures,oftenincludingposition,materialcolor,normalofthepointstoshade,arerenderedinthefirstpassfromthepointofview.Then,anorthographiccameraareusedtorenderaquadthatcoversthewholescreen.Thenormalized(0-1)screencoordinatesarethenusedtoretrievedthegeometry/materialdataatthepointofthescreen,whichisfedintothelightingfunction.Insuchaway,weavoidproducingtonsoffragmentsfromtheprojectedscenegeometry,instead,onlyrenderthosewhicharevisible. However,imaginewehavealargegroupoflights.We’llstillhavetogothroughthewholelistoflightsforeachscreenpixel.Withamorephysicallybasedlightingmodel,inwhicheachlighthasainfluentialradius(resultedfromthephysicalfactthathasanidealpointlightsourcehasinversesquareddrop-off),fragmentsthatareoutsideacertainlight’sinfluentialradiuswouldwastetimeonwaitingotherfragmentsinthesamebatchtogotoadifferentbranch.WeknowthatbranchingisbadforGPU.Thisleadstoaseveretimepenalty.Manytechniqueshavebeenproposedtoalleviatethisproblem.Tileddeferredshadingisaverypopularmethod,probablymostofyouhaveheardof.Itpartitionthescreenintotilesandcreatea“lightlist”foreachtileusingonlythelightsthatintersectwiththetiles.Thisisofcourseanelegantmethod.However,wewillalwaysneedtodosomepreprocessingbeforegeneratinganewgroupoflights(ifthereisaneedto). Asimplersolutionisvolumedeferredshading.Wejustneedtorender“lightvolumes”foreachlight,which,asyoumightguess,canbeaspherewitharadiusequaltothelight’sinfluentialradius.Forexample,inOpenGL,wejustneedtocreatealistofvertices/indicesofasphereandprepareamodelmatrixforeachlight(whichissimplyscalingandtranslation).Whilerendering,weperformthedrawcallntimes,wherenisthenumberoflights.Onesuchlightvolumewillproducefragmentsthatcoversthespecificregiononthescreenwherethefragmentsarepossibletobeshadedbythelight.Ofcourse,bydoingthiswearelosingthedepthdimension.Wehavetoexplicitlytestthefragmentsandmakesuretheyareataboutthesamedepthregionofthelight(whichisonlyanecessarycondition).Ofcourse,tilerenderingalsorequiresuchtesting,butifwehavelightsscatteringeverywhereinavery“deep”scene,thelightstobetestedaresignificantlylesser.However,becausenopreprocessingarerequired,volumedeferredshadinghavequitecompetitiveperformanceinmostcases. Wait!Weshouldnotrendernpasses!Instead,abetterwayistousetheinstancingfunctionwhichissupportedoneverymoderngraphicscardtoavoidthelatencycausedbylotsofcommunicationbetweenCPUandGPU.Alsoanotherimportantthing,depthwriteshouldbedisabledandadditiveblendingshouldbeenabled.Thereasonthatdepthwriteshouldbedisabledisthatlightvolumesarenotrealgeometry.Whiletwolightvolumesareclosetoeachotherandareilluminatingthesameregionofthescene,wedon’twantthemtooccludeeachothersuchthatsomepartareonlyshadedbyoneofthelightvolumes. Ifyoudothevolumedeferredshadingdescribedabovedirectly,wewillimmediatelydiscoverthatsomethinggoeswrong.Whenyouapproachesalight’slocation(withamovingcamera),atsomepointthescreenwillsuddenlybedarkened.Thisisbecausenomatteryouturnbackfacecullingon/off,youwillfallinadilemmathatyoueitherrenderthepixelstwiceasbrightwhenyouareoutofthelightvolume,ornotrenderanythingwhenyouareinsidethelightvolume. Itturnsoutthatthissituationcanbeeasilysolvedbyswitchingthecullingmodetofront-faceculling.However,thisisnotgoodenough.WecanactuallykeeptheZbuffercreatedbyG-buffersrenderingandusethisinformationperformsomerejectionoffragmentsthatarenotintersectedbylightvolumes.HereisanicestencilbuffertrickintroducedbyYuriyO’Donnell(2009).Whatitdoisbasicallyusingthestencilbufferasacountertorecordwhetherthefrontfaceofalightvolumeisbehindthevisiblescenegeometry(sothatithasnochancetoshadethepixel).Thisisachievedbyonlyrenderingfrontfaces(withcolorbufferwritedisabled)inthefirstpassandadd1tothestenciloftheZ-failedpixels.Anothersituationisthatthebackfaceofalightvolumeisbeforethevisiblescenegeometry,whichissolvedbythesecondpass-renderingonlybackfacesanduseaGreater/Equalz-testtocontinuefilterthefinalpixelsfromthepixelswithazerostencilvalue(alreadypassthefirsttest).Sothatwecankeeponlythelightvolumepixelsthat“fail”thez-test(theoriginal“LESS”one),whichintuitivelycorrespondstothescenegeometry-generatedpixelsthatintersectswiththelightvolume.Noticethistrickalsoworkswhenyouareinsidealightvolume,inwhichcasefrontfaceswon’tgetrendered(itisillogicalthatthefactthatweareinsidealightvolumeandthatthefrontfaceisbehindthevisiblegeometryholdatthesametime!),leavingazerostencilvaluethatallowustouseGreater/Equaldepthtestonlytofilterthepixelstobeshaded.Ofcourse,ineitherpassweneedtodisableZwrite.Certainlywedon’twantthelightvolumesbumpingintoeachother. TheoriginaldiagramusedbyYuriyO'Donnell. Whilethistrickdefinitelyincreasestheefficiencyespeciallywhenthelightingsituationisverycomplex,wecandosomethingbetter.Oftenmodelingadetailedsphereaspolygoncreateslargenumberofverticesthatcramupthepipeline.Instead,wecanuseaverycoarsepolygon-sphere(e.g.,with4vertical/horizontalslices)withaslightlylargerradiustoensurethatthelightvolumeisboundedcorrectly.Wecanevenusejustaboundingcubeofthelightvolume!Ofcourse,theleastthingwecandoisjustuseaquad.However,thatgivesupsomeaforementioneddepthtestbenefitsanditalsoinvolvessomecomplexprojectivegeometry.Justforfun,Ialsopreparedaderivationoftheaxis-alignedscreenspaceboundingquadoftheprojectedsphere. ←PreviousPost NextPost→
延伸文章資訊
- 1How to Create Masks With the Custom Stencil Buffer - Epic ...
How to Create Masks With the Custom Stencil Buffer | Tips and Tricks | Unreal Engine ... This epi...
- 2YouTube | Custom stencils, Create mask, Unreal engine
How to Create Masks With the Custom Stencil Buffer | Tips & Tricks | Unreal Engine. Masking enabl...
- 3Using the Stencil Buffer for Sprite Occlusion - prime31 blog
- 4Stencil testing - LearnOpenGL
The stencil test is based on the content of yet another buffer called the stencil ... Nice tutori...
- 5Improving Shadows and Reflections via the Stencil Buffer
Yes, you can use the stencil buffer simply to reject fragments outside some ... shadow algorithm ...