What is a stencil buffer? - Computer Graphics Stack Exchange

文章推薦指數: 80 %
投票人數:10人

The stencil test is simply a per-pixel operation in which the current pixel stencil value is tested against the content of the stencil ... 2022DeveloperSurveyisopen!Takesurvey. ComputerGraphicsStackExchangeisaquestionandanswersiteforcomputergraphicsresearchersandprogrammers.Itonlytakesaminutetosignup. Signuptojointhiscommunity Anybodycanaskaquestion Anybodycananswer Thebestanswersarevotedupandrisetothetop Home Public Questions Tags Users Companies Unanswered Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore Whatisastencilbuffer? AskQuestion Asked 6years,9monthsago Modified 4years,2monthsago Viewed 17ktimes 33 11 $\begingroup$ Wikipediastatesthatastencilbufferissomearbitrarybufferashadercanuse. However,ithintsthatit'susedforclipping,orotherwise"tightlybinding"thedepthandpixelbuffers,slightlycontradictingitself. Whatdoesthestencilbufferreallydo,andhowisitpracticallyusedinmodernapplications? stencil-buffer Share Improvethisquestion Follow askedAug4,2015at18:50 Qix-MONICAWASMISTREATEDQix-MONICAWASMISTREATED 57911goldbadge44silverbadges1414bronzebadges $\endgroup$ Addacomment  |  3Answers 3 Sortedby: Resettodefault Highestscore(default) Datemodified(newestfirst) Datecreated(oldestfirst) 38 $\begingroup$ ThestencilbufferdefinitionbyWikipediaisindeednotgreat,itfocusestoomuchonthedetailsofmodernimplementations(OpenGL).Ifindthedisambiguatedversioneasiertounderstand: Astencilisatemplateusedtodraworpaintidenticalletters,symbols,shapes,orpatternseverytimeitisused.Thedesignproducedbysuchatemplateisalsocalledastencil. That'swhatstencilmeantbeforeComputerGraphics.IfyoutypestencilonGoogleImages,thisisoneofthefirstresults: Asyoucansee,itissimplyamaskorpatternthatcanbeusedto"paint"thenegativeofthepatternontosomething. Thestencilbufferworksintheexactsameway.Onecanfillthestencilbufferwithaselectedpatternbydoingastencilrenderpass,thensettheappropriatestencilfunctionwhichwilldefinehowthepatternistobeinterpretedonsubsequentdrawings,thenrenderthefinalscene.Pixelsthatfallintotherejectedareasofthestencilmask,accordingtothecomparefunction,arenotdrawn. Whenitcomestoimplementingthestencilbuffer,sometimesitisindeedcoupledwiththedepthbuffer.Mostgraphicshardwareusesa1byte(8bits)stencilbuffer,whichisenoughformostapplications.Depthbuffersareusuallyimplementedusing3bytes(24bits),whichagainisnormallyenoughformostkindsof3Drendering.Soitisonlylogicaltopackthe8bitsofthestencilbufferwiththeother24ofthedepthbuffer,makingitpossibletostoreeachdepth+stencilpixelintoa32bitinteger.That'swhatWikipediameantby: ThedepthbufferandstencilbufferoftensharethesameareaintheRAMofthegraphicshardware. Oneapplicationinwhichthestencilbufferusedtobekingwasforshadowrendering,inatechniquecalledshadowvolumes,orsometimesalsoappropriatelycalledstencilshadows.Thiswasaverycleveruseofthebuffer,butnowadaysmostoftherenderingfieldseemstohaveshiftedtowardsdepth-basedshadowmaps. Share Improvethisanswer Follow editedAug9,2015at17:31 trichoplax 5,87222goldbadges2020silverbadges6666bronzebadges answeredAug4,2015at19:45 glampertglampert 1,3691111silverbadges2020bronzebadges $\endgroup$ 4 $\begingroup$ It'sonecharacter,butcleaver->clever.Ican'tedititD:Greatanswerbytheway,thismakesthemostsensesofar. $\endgroup$ – Qix-MONICAWASMISTREATED Aug4,2015at19:54 5 $\begingroup$ Ifyouhaveanexpensiveper-pixelshader,thestencilbuffercanalsobeusedtobatchsimilarthreads,reducingdivergenceandincreasingoccupancyasinthisOITpaper,sec2.6(disclaimer:I'manauthor). $\endgroup$ – jozxyqk Aug10,2015at5:45 $\begingroup$ @jozxyqk,that'ssomeoutsidetheboxthinkingrightthere!Verynice. $\endgroup$ – glampert Aug10,2015at19:05 $\begingroup$ MayIask,isthereasingleStencil&DepthBufferfortheentireGPU?OradifferentoneisassignedperobjectweDraw? $\endgroup$ – KeyC0de Nov7,2020at14:21 Addacomment  |  14 $\begingroup$ Thestencilbufferisanunsignedintegerbuffer,usually8-bitnowadays,whereyoucanfillper-pixelinfomationasyouwishbasedontheuseofvariousoperations(OpenGLOpshereforexample)followingastenciltest. Thestenciltestissimplyaper-pixeloperationinwhichthecurrentpixelstencilvalueistestedagainstthecontentofthestencilbuffer.Yousettheconditiontowhichthestenciltestispassed(e.g.ALWAYS,LESSetc.). Youcandecidewhathappenstothestencilvaluedependingontheoutcomeofthetest(inOpenGLusingtheoperationIlinkedyouabove) Thestencilbufferhasquiteafewuses,thefirstonesthatcometomindbeing: Itcanbeusedasamasktoselectivelyrendercertainpartoftheframebufferanddiscardtherest.This,forexample,isusefulforreflections,butalsolotsmore.Whendealingwithmultipleresolutionrenderingitcanbeusedtomarkupedgesthatneedtoberenderedatanhigherres.Thereareplentyofmaskingusagesreally. Averysimpleoverdrawfilterforexample.Youcansetthestenciltesttoalwayspassandalwaysincrementthevalue.Thevalueyouendupwithinthestencilbufferrepresentsyouroverdraw.[*] InthepastithasbeenusedalsoforshadowingtechniquessuchasShadowvolume. Thepoorstencilbufferisoftenquiteunderestimated,butcanbeagoodweaponinagraphicsprogrammer'sarmory. [*]Valuetobereadbackinapost-processusingtherightSRV! EDIT: Also,worthmentioningthatinShaderModel5.1(soD3D11.3andD3D12)youhaveaccesstothestencilreferencevalueviaSV_StencilRef Share Improvethisanswer Follow editedAug4,2015at20:21 answeredAug4,2015at19:44 cifzcifz 2,4681414silverbadges3131bronzebadges $\endgroup$ Addacomment  |  7 $\begingroup$ Astencilbuffercontainsper-pixelintegerdatawhichisusedtoadd morecontroloverwhichpixelsarerendered. Astencilbufferoperatessimilarlytoadepthbuffer.Sosimilarly, thatstencildataisstoredinadepthbuffer.Whiledepthdata determineswhichpixelisclosesttothecamera,stencildatacanbe usedasamoregeneralpurposeper-pixelmaskforsavingordiscarding pixels.Tocreatethemask,useastencilfunctiontocomparea referencestencilvalue--aglobalvalue--tothevalueinthe stencilbuffereachtimeapixelisrendered. Source:WhatIsaStencilBuffer?|MicrosoftDeveloperNetwork Thegeneralideaisthatyoucansetavalueforeachpixelandthensettheoperation(GREATER,SMALLER,EQUALetc),whenthisevaluatestotruethepixelisshadedandshown. Oneapplicationisrealtimereflection,whenrenderingthereflectiontexturethestencilbufferisusedtocreateamaskforwherethereflectionisapplied.Forexampleonlyapplythereflectiontotheglassmaterialshadedtriangles.Itcanalsobeusedtocreatearbitrarytwodimensionalmasksforthewholeframebuffer. Share Improvethisanswer Follow editedMar4,2018at19:56 user7220 answeredAug4,2015at19:18 MauriceMaurice 45133silverbadges88bronzebadges $\endgroup$ Addacomment  |  YourAnswer ThanksforcontributingananswertoComputerGraphicsStackExchange!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.UseMathJaxtoformatequations.MathJaxreference.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedstencil-bufferoraskyourownquestion. TheOverflowBlog Makeyouropen-sourceprojectpublicbeforeyou’reready(Ep.444) Thescienceofinterviewingdevelopers FeaturedonMeta AnnouncingthearrivalofValuedAssociate#1214:Dalmarus Improvementstositestatusandincidentcommunication Linked 2 Howarehardandsoftshadowscast? Related 3 Incorrectvalueswritteninstencilbuffer 6 UsesforStencilBuffer 2 Fastclippingwithoutclearingstencilbuffer 2 LayerrenderinginVulkan 1 ProblemusingDecalingwithStenciltoavoidZ-fighting HotNetworkQuestions SignalprocessinginPythonvsC++(Bandpassfilter) WhatdoesGandalftheWhitemeanbyhisstrangespeechaboutBoromir? Tryingtouseacaralternatorforapowersource(notforcar) ShouldIrevealmyslidesbit-by-bitwhengivingapresentation/lecture? WhatisRNAVtransitionandwhatisthedifferencebetweenRNAVtransitionandRNAVSTAR ESTAform:Mentionthecitydistrictinthecontactinformationsection? 'Mapping'thevaluesofalisttovariable Whatisthemostecologicallysustainablewaytohandleragscoveredinchaingrease? HowdoesHebrews11:27sayMoseswasnotafraid? IsitallowedtocopycodeundertheUnityReference-OnlyLicense? WhydoesmyMOSFETalwaysburn? Can'tmultiplywidthin\newcommandforincludegraphics HowManyDaysAfterSkimCoatCanIPaint? convertingairflowratebetweenl/mintokg/s SelectonlyonepointinpolygonusingQGIS DidSauroneverconsiderthattheValarand/orEruIlúvatarmaynotallowhimtoconquerMiddle-earth? Canarunascendinglistbemade? Isitillegaltorideadrunkhorse? Howtomodelasimpleshoe CovidTestRequirementforDubaiTransitFromIrelandtoIndonesia ExistenceProblemofpigeonhole,selectingamongasetof50numbers. StarWars:R2D2connectedtomainframe HowcanItellwhich3rdpartyflashesarecompatiblewithCanon'sSL3missingpinhotshoemount? Longestgeometricprogressionofprimes morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?