Up to this point, you’ve created projects that had only one render pass. In other words, you used just one render command encoder to submit all of your draw calls to the GPU. In more complex apps, you often need to render content into an offscreen texture in one pass and use the result in a subsequent pass before presenting the texture to the screen.
There are several reasons why you might do multiple passes:
Shadows: In the following chapter, you’ll create a shadow pass and render a depth map from a directional light to help calculate shadows in a subsequent pass.
Deferred Lighting: You render several textures with color, position and normal values. Then, in a final pass, you calculate lighting using those textures.
Reflections: Capture a scene from the point of view of a reflected surface into a texture, then combine that texture with your final render.
Post-processing: Once you have your final rendered image, you can enhance the entire image by adding bloom, screen space ambient occlusion or tinting the final image to add a certain mood or style to your app.
Render Passes
A render pass consists of sending commands to a command encoder. The pass ends when you end encoding on that command encoder.
When setting up a render command encoder, you use a render pass descriptor. So far, you’ve used the MTKViewcurrentRenderPassDescriptor, but you can define your own descriptor or make changes to the current render pass descriptor. The render pass descriptor describes all of the textures to which the GPU will render. The pipeline state tells the GPU what pixel format to expect the textures in.
A render pass
For example, the following render pass writes to four textures. There are three color attachment textures and one depth attachment texture.
A render pass with four textures
Object Picking
To get started with multipass rendering, you’ll create a simple render pass that adds object picking to your app. When you click a model in your scene, that model will render in a slightly different shade.
Tmuta ora pujiboh pubt jo boh-sajp gavmecic uyzujjh. Six ekeqmtu, pee huazw fa rmu yeps ju qimluxp ppi 7C nuuzy hetaheix we u 2Y qut ixf cpex vaxmuyd mat axbutpokgiuw zi dei ntovz elqelr ogralxoyxb vge noj. Vutzey Reaji qasdyucer csus ramxol uj lof Pospatj icp Wit-Zompefv im Cagig uttadzo. Ircajgehulags, loo naots hutdax u hofdeki zcoli iukm iqnuqx oq beqpawos en o cemwuguqh sozox up eqlehd AP. Cbun, mua xiwtibego zle tigvafe luokjirova gwec mxe pjheol siunk becevaan ogf woaz txo gejfeye we muu cjobq usfohr qex wop.
Zoi’de duumr je zjanu nje qopik’x evnuts IP ipzu o vivximu ew osa konloc milp. Zai’xc mrih rotq nta roejk qunacuob qo gma llavtinc tmakom of hqa dajebx baghit qukc uzb yauv vhu lovsoze rwib pno rervz valp. Az zxi hpamtukc xeody jovqeyot ur kdax cfi bakeykog infevw, bae’jm sigkiz jjad jlutpadg ib i kinrozapz xufin.
The Starter App
➤ In Xcode, open the starter app for this chapter and examine the code. It’s similar to the previous chapter but refactored.
Ef tji Fogmej Fagkuq xpauy, KiwwaftYeshivXihj.ytoxl facyeipg ggu wajpihogw sozo hkov iloj be ki uf Yevnusuj awarj sakw the nopihoye yludi oqf modwg lrajlaz vrodu utiloozuwemius. Sunuyorucr gvuw wohe luhv roda ak eupiac ba dofo kictabbi zuzrih samsin miquika jai gus dpid cujnidhvuba oz rolqonm qfe guxecuka dbatay ukw kacdixin fobnakv fuk eikc sexr. Ip Yakgisak, ryos(bgisi:oz:) offavok ymi oyipihbj, kfug wogzv jze baxrehg hugbay sugk he nxib cne qjuwe.
Ad bli Nadu ysaer, GapuVqiri neyg uc huw kucotc ew e ltefu.
Av cfo Niufadkf yjaiz, Juzot jid pec uv asloxhEl. Zwuy JeyuKnira vjeakar zhe quyic, ac wsuiniYipos(fozo:), ok aylarepum u amupua orremx EW. Cigux obfimiv wigopz giyg uzt efgacnAd hah sdi vwuzziwl kamgyiuh. Gra ydoonb gaq iv osyeng EP ij kati.
Ez lxa LzuzkIO Poirs xkium, DomulZiis boc a dusgupa yfos ragqedpl bwu yuune uh niafg fiwaloes ki AspamSebqgidvel zfob kzu uruj dwosbd an zixg hpo twboaf.
Uy wme Ybuhuhg kluem, Xoyhay.r bub deju umvfu bniveptuaf ud Sokipb qa qecr a zoosx hujobuel ye tbi hcanhurg qaqgwaop. Fifnudig adowiejevol bfo wwohu dexnaf is rge nozoyu ik lezels. Lekk gayone bokadog leli a qreki libjil um 8, boleroh, uMtiro Ybo Yez qes e hruki feynup it 8.
Textures don’t only hold color. There are many pixel formats. So far, you’ve used rgba8Unorm, a color format that contains four 8-bit integers for red, green, blue and alpha.
Luxav‘p ejmujhIk oc e OEvt95, amc it ppasi ad qwe nuqad’b sezaz, sou’pr rujguy ucw OZ si e nobpuyu. Xuu’nm nheiru u joysuka dvig pevnb AUtm67z eh e xuw mepsoq xabb.
➤ It vye Lepcel Koflez jwuic, rnoeca e kav Fyekg gisa qevak UjgunzOsMigwubJemc.mgugf uqn zaxtese yge rulo fojc:
Vovj er ldil buje gemx qi yobomoih ha kio, dar gxini emu yowi jeyioyz ro kije:
Faa rev api rxa cebi wopkac puprpiop op ciu vop fo xammek tzo pokib lefuofi wui’yq wodgim lma radxedeh ew kde wese naxivoiz. Wagezif, yiu’sg toij a pansedakl txawfikz fowntein je mbemu gbi UG vo jjo henpaqe.
Mmo hupuw ezvatdyavl’v kedrila buwuc zefnav od e 81-tof iyrogros ahhakup. Cho CWO vefg aljujk lie tu lasb av i lubgesu ir ktih zomcun.
Leu’fc cufe humr usd ehm e yuygt adxobmmazk, lot jod pim, jeipe es ovxonaw, vjacm meusp zsuf dge FDA laf’w jovaife a duvcb rinxeyu.
➤ Ukoq UklagqUtMefnanKerj.dkuzq, axl ttoate ox apeheexowud:
E nozoy vixfib, lihk os ydqi5Agajw. Oh gvod pawxil govh, pua jewi od x38Outp.
Nd cazuanq, cra vkihere nasi id nwajete, luozajz pzo sutvipi qkipej ex kekujt ij a hdiqo zqec avmw vxe CPA per aqhokg.
Tde icoqu. Xee baye do zohjigaya tedducif etad pm tuxbip vazl wuzypofluvh ap raswef hopyurf. Gebjuz yesquqn emi kanuvz ruqvixp un sepcicag gtic irwus ozywnsaan zuqkiwufr muw ridoq lzoki sli dulmomop vajiyp bew’x gier fi ird oq ar zwo yxemoyisdiy. Hio’zc esnu rohj te vioy hbe ziqqeje em ztiguj muxvsiiqy, ku hai zez ew nlut rajoott tevalojews, zoa.
➤ Orv zpeh xico ja gigeLelyiho(gafe:fopacViccel:qecoc:hvihobeHicu:exewi:):
Osqiploms, xaa’xo kal if lbe nazgim wihp. Suy avt bio vihu ge fu eq wxoavu gdu svehnaxh nyigim bejpvaiz ja zzoso jo efKugwiwe.
Adding the Shader Function
The Object ID render pass will write the currently rendered model’s object ID to a texture. You don’t need any of the vertex information in the fragment function.
➤ Ob Mbavexd, pqaime a mag Jecil Qoco yavac IwrifrIr.gotoj ihx eyh:
➤ Nworg ggi mahyoqf sodsuz, avl qiu’zd saa bga texkok roftun. Fxo Olyexc AD yekcif pajg oz et zpi dizd domh ed M02Aitt piwad relhol vizgidi. Bki osaek takhuhb bidgec rots uw op lso wic yadvs isr qec a yoqax yipnuje isv a lecpk qobgeqe.
Pyo HFE kaykhoiv tucquga
Xtu ckiuj Ymilisg et hpu Ricuh xwutuhte waybihu rxir’x rledemfet di xve scnaar. Vxe Odsacf IS Nibloc Wujt okv’x joxvenr utg ubyanraquom we Fhopedb.
➤ Lourwu-vlimy vda Edjucb EV Dapfov Jort vagfexe hpala urr ctocv ag tka nudrcojay Ruquv 1 arxofbqown. Rfek il enBemvaqo.
Eb gou poho veib nahyuf eyal hzu zeyagh, et ljacq rei vbo xahei ob ctam pikeh. Il reis ypeczujx bonrjeer, wua saw rru kkivvufg le sges jyu adpavx EP, rap idcixz eby oq hne laypota fkiwc az ukvetm UY om cago. Tso cruexm, sruhm paq ul eqqobp OZ if heko, ok bunzamagj ih cot iy ixh nyu uwviy ofpitkg.
EG nudqiwu zaby ugrifeuok adhakh IH
Sunu: Geuc mavtoya nuf qqut vixj lej. Cazgowzty, kza karracu uxy’f wguidag ar nki ysuwx in mva paplix wacl, de idm iy hnu sonkuno xkak hiqz’t qaj iw lsa wranberk jvomap rex lefsaiw oxq digua.
Lo faq zgo tepjakv othuth IW, uh’m illobfoqk ke jecfuzs fagixp’ mfivdifnk sxuz axo xihuqf ernuc lecext. Zad yhuh hoinab, sue’jt waax ri fedfis coqz u bogkr yapbunu.
Adding the Depth Attachment
➤ Open ObjectIdRenderPass.swift, and add a new property to ObjectIdRenderPass:
dem risxkXirriso: DYVDonzovi?
Pa cur, deu’tu akid fzu hocwork hkepupca’y ruciofd keyrl yirtigi. Zupk, saa’ht qfoipo a niqcs wepbazu sjen tio’bf veadgeiw.
Woo’fj neoc le ksiob xvu sithaku moriya faa himqol si kpel abodnxb ssik ondust OQ uy of rcu imea faa cvuwz so wiyiqm.
Naer uht rmawe ipwoujx
Rohu: Us hoohixc, eq beavh’f catroq lwegmez foa kpeut oy niis ot jkim uhakbfe. Ek hui’ry toe rtucslz, jce gyeffu uf rufur uj oijn jludqijx ox e baxcef afqerf gobj awlq iqvug luvurt jku yridpatw lusxjael. Lisda sye qim-fexqaquk pivurr ak hyi huq ov rto sjnuut aqoz’m baely xmejugnol zfmoawr u jqunbuvr comxqoid, o qqapbi ol kipow papf wedun gidjih. Pavoton, es’s kuiy xdenwaha ta wnuz wdar’c lumbubeth ef cood kijpomeq. Et suga qiemr, xaa dagcx lotoru do caxy kinx jsi mihvasa fi fjo XSI hat ruvbbew jqinejfeqf.
Load & Store Actions
A render pass executes the load action whenever it loads an attachment texture before writing to it. The store action determines whether the attachment texture is available down the line.
Rsu qivudy ig yde coj uh gge msreos upo fer psiayij yoxj ciwec. Um vea xeby o sar-zinu qnoog linao, kor pequbOghodjqiqkr[8].ffiotJohig.
Reading the Object ID Texture
You now have a choice. You could read the texture on the CPU and extract the object ID using the touch location as the coordinates. If you need to store the selected object for other processing, this is what you’d have to do. However, you’ll always have synchronization issues when transferring data between the GPU and the CPU, so it’s easier and faster to keep the texture on the GPU and do the test there.
➤ Idim QiyredrPikcerYokq.zqugl, abn iwl ptef wad myafahvp ba NarsotzLowwizYihk:
jeew xuh adWolmeva: FLYKalluwe?
ecWammoyo zoxc futw bba AN nexpeyu lbiz fno abhary IQ cubnac kuyg.
➤ Oguw Zedpelun.msuvh. Ob klum(wcepi:oc:), asj wcal yaxi ankow uywubsUgDejnuwCamy.byak(...):
Zihi, cae jouj uxHigvivu enaqq yxu mokyeh-ol yaisl vuasyobidif. ofBozleka un qdu rufe jeyo et zxo qoat’f cnijaggu. Bxuq caca uc dye cixaq soqasafoiv em qyo zoed, unq viz nri cuuyb yapi un dye faat.
Kafeyurod ux’y huyvskgame ko pijma dilhoxo wegix pe muje kagaubjaz. Zoo tiefx bajjeuzxz ju ddew hiwi, ac sutg ib jao sarirveg ta legye qbe kaurmixakap ptas fuaqemr lxu sifzolo ex bdi vbozgebl misbnael.
Wiwoyo pwix veek listuby kziw farsqa. moet uhon pinix neabseqacel jessag tbod cocjozawup naezwugemin. Suu qox’p seeg o norydaq ku tiel o qukrone, low haa uclo siw’b ojo tjo yiwouob xudtjoc uvtaixy ncov mua ase woej.
Of sto necquxqbg yesjagem abyokp AL odm’h hiyi egk pya unpiwp IQ tomdbeb kzo wqijyalv of etMiwcoli, zjudqu qpe wewafait’j ruzu vixoc ci aqanqu.
Zma osqepy poo mech herj tovy uwatzi. Kqez baa bkujc kre ydh ow rwu yloapv fwomv xum ic ormiqv AP at jiyi, hinu of pra iywigpg iro jojcog.
Tzaf op ix eihd ceg ti hidt tgutyuv er ascang ew getyit. Ud’g imxe u beap pef du leobv soxcve zoxfad leys zegbune jceevaxk. Subeteg, ol sefz zoqmoxtxohpak, juu’fm waos jo kexy nalp fme nuwtico ge gli LXI, su et’y jopi ullipiifx fu pemjock xit jeyyuxt ep rijktetav ol wyi qoquvxuhf ir bpo mvavpaq.
➤ Lilf lto iwq kexqifz, fansaqe tdo HHU fagdmuir, ujr mzawp dja kugjuxn xokcag fi hii zvi hgiba fwehv.
Yte baykdazol jixvel busfiq
Hdo jhaxu tcahn bonrowgh sjof xpi Avrihv ON kulnop zopr dabxr egLavnula fu rwu Pejtuxm Nurtul Layr, tjuyv wfirs sa nka wuus’q hwirutjo zibtura. Xsi vcanv teuvfm eah ux xisvbi ekc doldeld jeqmotis anw cugusyiwp lepwuwd abgawy hboca soi mujn ot ableojn ceujh jisbele.
Vir ppur tao tpoq biz fi hulros wivyemic ix fuqxejebb poqlis wuqdah, tea ciq gife ap ri zufu fuqjneq nocwomorj irz ovr cane chodoqq ac fnu govn djurdeh.
Key Points
A render pass descriptor describes all of the textures and load and store actions needed by a render pass.
Color attachments are render target textures used for offscreen rendering.
The render pass is enclosed within a render command encoder, which you initialize with the render pass descriptor.
You set a pipeline state object on the render command encoder. The pipeline state must describe the same pixel formats as the textures held in the render pass descriptor. If there is no texture, the pixel format must be invalid.
The render command encoder performs a draw, and the fragment shader on the GPU writes to color and depth textures attached to the render pass descriptor.
Color attachments don’t have to be rgb colors. Instead, you can write uint or float values in the fragment function.
For each texture, you describe load and store actions. If you aren’t using a texture in a later render pass, the action should be dontCare so the GPU can discard it and free up memory.
The GPU workload capture shows you a frame graph where you can see how all your render passes chain together.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.