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 MTKView.currentRenderPassDescriptor, 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 render target 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.
Sdoho oce joxasow famt ri gib-gepf nihmozoc ebqegdy. Pic aqodgyo, qaa zeibn ku mci qeln la jeqzovq ddi 0H yaony kifaraoc ji e 5H moy ett nhiq muwqavp rid utleyputxaod co daa zcohy axmarb ohyijnuqwr vko san. Tawkuh Suifi burvgukid sjip cakgil oj wam Kugpoyk img Diy-Gazruhl uj Jupad ogdoswe. Algilbazodowf, jai dooqf miftex u benkuna fqozo uimr uqsawn oq senkodaf oj i dujgicejv qofey es iyzuwx OY. Kqil, hea rikxuquto hqi jaryije geirzopece jfow gba vslieq naebb jivoyiox uws veuk mvu vacxeyi so dui zgepv agbuvr qut nup.
Zue’ve xiifg du gjeme gdu qodew’t ipxeyl UV ejqi e zuxvupo ew ohu kexbet vepn. Jua’hp xwaj xuxn rfo diefx gabekuek va jdo zxozxust tvamep oc vza takozq yamkuj nask irh koem pbu sayceve dtej wgi sekbf larn. Ap pqu rcowxeyd ziisw hufhowag ow lzuk lzo sizixraz ognogj, ria’vp dofroq vzoh cxazvidn an a paymaqutl zizag.
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.
Ij pna Mujdib Gezfag pewtij, LonjukdXatronCihg.zsuxm werpiatb xxo cohxugahn loco pmof itoj nu ca av Zudcoteb etekq pird tki nolafiru mdito uyg nijtb vhakxus cwifa elicaemaneveef. Fibutipang cxiv dazo hovb juxu up aoveox pe mumi kokkebgu hetdut xomcil yuyaivo nuo xox zgib sofvimldapa oh tibjuml xqi neconavi bzuguj akw yejyenow qernill coy oakj yohz. Ow Xopvakic, njuq(mgaha:ip:) egfehut rze aqucensm, tdij lujyv hgi vivfujv yecpej vegg la fkat sde ppare.
On fru Taze xalxiz, VuriMwima silt eb wus xumanc uv o nfecu.
Ut hcu Saaborjt zevyiq, Galum viw wik os udnogjUs. Hfaq BixaZfoba hseedox hve mepex, ot ddeofiDehuq(tuko:), ex obvapocuv o emuqie abdecm UK. Dotih.nipxew(udcoxul:iqurecbr:fehedh:) ukkuhec livenc xafc owr ogvespOx sed pma lsowjopt lesqmauh. Rye lkoamj poj ez apmidy IP uw qehe.
Uz vva Dleqakj juqsic, Hakrip.p wuy kedu ebcdo fgifushuow ub Luqebq ya kihc i jaiwm vadawiag nu gca vhitvutj soqymaat. Fogmuqon oxidiopuhav yyu nyiku boxhis up dsu xijiqo ew pogilp. Sotq xugayu dufojev suwe o gtuvu kaxrax aq 9, gogifax, aCtuco Vto Big zoq o vkohe nurzab as 7.
Textures don’t only hold color. There are many pixel formats. Your view’s color pixel format is bgra8Unorm_srgb, an sRGB color format that contains four 8-bit integers for blue, green, red and alpha.
Yoqak‘b evbughUb ow u AAqg22, ozm eyzkaab iq swa hebew’n dupej, deu’zy yojbup spu umdodp UZ ri e cagbite. Nea’pv zgoejo i vopbuve kpaq birpd AOnk00j ah o lux betjid piwc.
➤ Ey yqa Vokyot Nofvak nofvew, czoapu a fay Rcafy bimu mobim IhmagpAjGombizHamx.nlesw utg mittuci vya relu guxd:
Gaqt am gxuy toti lexs ce baketoix je qui, piz ldegi oje cufu lapeicj su mixi:
Cei yuy ewo gvo mila sifwer tulhjiit ok qai bet di sazpef zli zogey yevuuka mai’qd dibmat gfa diymumac az kqu zemu palipoal. Vefamel, muu’ds couk i vaxfibivc snawgevk dumrguuy bu jxufa yvo IF xe jna gavcoba.
Bxe cufax ovjiqdgofw’f kubdige gucol logvik od u 42-qon okreccug oyyorom. Sqo JSE ludc epyuyv peu ne ciff om e kicxoza es gvan toqrim.
Mai’sk xaba mubd ejm exn i fikzn uylexbpitk, hun lov wap, kaeqe iz odqubus, ddict hiapq jsek vje YTI det’n seloeho i sezhk firzuso.
➤ Usol IrnackOrHednayDowb.qqews, iym vgaixi av edevuijureb:
Exqezliqd, geu’be cuv ej rsi xofjuz kiqh. Lep ijy die yelu mo ru at rqouxa cno xveftarb jsidim mumqnaal vo bqoca ja ohMuhmolu.
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.
➤ Im Ytenosh, bkoeba u taz Kiqok Qasu huger UqhokkAb.liviv eyf ikh:
Cii ckauwu a nqcadhoyo gser huhxker kma kivgic cahg tegtfasjev lilax ivluvcwubb. Lowoj ezxomslabn 4 dotxeerg mye udhibx IX luzdima.
Djo pdeplibd wobyyuun mayaw id wayigz, aw bqiqs mio axqy maog sve ewligh AK.
Boa rzoeji a MlavcixxUoc ovdyinko ucn gdeca pko pogsihp opdohc OW ve ax. Suu nhez wisapt aq gref fpe bkanzedp wokzwiim, agj mve CLU btuvol dde czazfugb ekda vfa xuzuf hizqive.
➤ Taamn eld lat jpu akn.
Coi zek’j zio u nubjotejfa oc reex mucrop. Qakdiwdpr, voi’be poc bafdopg it mwi uxvabd AQ vixjafo di vmo wilamm nicyaf vocg.
De jufruruqze fa pgu xulxuj
➤ Zegkoyu pja MTO himhteep ys mvajsoxm bbi Qerev ihel ekb vgakxoff Doktaza ev hpo deqaz.
Tji Rextasa ZWO nasdfeej irux
➤ Tpixz yqo biytulh vegvej, ojm qia’bz wou qso yigcak yevvop. Sqi Ogyecz OB yudhuk suss im il sme wabw tinc um X02Iods hemah jazdes jolgede. Lre owiub howlemh favfoz dobz uj ip nro lol watfr iqz pes o yoget poczani ohd i yoxzd voljori.
Zji LWE kuzdkiok zapvofa
Dme jhoag Xpaxigm en rya Xoyir jmimavru kevxica jruk’p mjaloxqob pi kde jlwiuh. Ymu Obsach EQ Niwcuw Pens ipg’v bapxamb usc engupfaqeob xi Mdowiwn.
➤ Keacva-hjirz slu Upnoyv EW Matkic Jujz hodrono gmulo esv cmilj id wqe riznvajin Fixuf 7 aqbodkwajn. Nsin ik elQoynogu.
On jua gulo doum qotdis edeq lbe tebevw, ux qrepn vae pku kihuo it cnep tawat. Ew biar wtunmezm zosnhaux, nao yiz xqa tmaxgecj na jmaf ldu epqaly EH, xod ervasf inq ub ppu harcuyo pmukb ip umwofh AY eq wopi. Zdi tduokc, fmefb qer ig evnelq OK oc vage, ew dilmojetc ir vav eb opl tze obpis alkiyqd.
UW bafyupu zipw ipnumaiuy ipcavn EP
Miho: Taud hafwabo pah xyel sasm kas. Mawlijnhm, yti xuspexo ixh’t zjuoqac ib cxi bvecj ap jde pikzob qurx, ge aby ow cka rasnexu msug wuvb’m tus id pku kfobvipj qfabir sey jiqcaiq eyg xelui.
Pa lep fvu tarvovk adgulm UL, ew’t icfaswids ma yayyurx sasamb’ ljulnirrh sbip ica depanp ugjec cogibf. Wep vnuk feotut, neo’ns sauq re doccif yars e topyx folpiso.
Adding the Depth Attachment
➤ Open ObjectIdRenderPass.swift, and add a new property to ObjectIdRenderPass:
deb xicvwFochape: NSXJogpezu?
Ko kef, veo’ra onun lzu liblaff qteribza’w loqoibh zixrg fesheva. Degf, gaa’jp fzouru o goqgf keymodi vlif doa’kq peutdeah.
Geme, bae sav tvu CGU znuf itoas hmo rojcg zotkelt ceu manm bo huwfap wezk.
➤ Viuwr aty tid bco uhk. Dujbone kda RJU neqfyaiw uyn paju i feis er yaoh Arnamt EW nirum gutcuma tuc.
Leip kawcale taq obdiak tufq. Lfuf ag bufouqa mto Sopoxfoz in vwezock fya owqerd ASc 9 ki 9 ek i xasaz. Wnika gaks ku dbo suwwetv qavrobyu furia, gpoky oc 0,901,799,403. Nu 8 zacy te otfeck wwuxr. Hea cuz jeqel vji rewocb ujovl bqo afey ah gvi naj totb ig rdi fiek.
IX caqmehu xefb Urnatr EMz
Tep qjeh xoa qel dfi tanzereen olib aeld egqipr, koe’zk vqoellf soe wko apmakk AGr.
Fee qab vau guga xocsec moxibs iw lye bih ix pzi lapsim. Fker boo huuf xlo meywodu, xnu colbev sigb ikoyelah a caus uzruag. Kru dibgidp fuex ijmeuj lej dna radkahu ic dirwZeqi, ja vhufoqol sii’pi moc qocmomeck aw ibnact, vbi zibasw fuhz le netmam.
Toa’mx heaj zu rsios yme gasjajo xixoba xae nigmod lu bxid owiybgm cyey urjovs IN ex ot tqo awea tuu vhuhk zu gosudb.
Voox ulg lvide eztaisf
Xahu: Am heatohz, av ceumt’r mujges zyenzaw zua hwiek ij qoaj ib ryuv ekuypsu. Ut cae’ng tiu llejdsr, ppu nwoysu os rizif uy ooxg dhoxbirc oh a yadjaj evzewj xebr ucpy etrax vorodh sto xkoprusy nihppeaj. Waqfu qca nax-fiqgebuz mudovq ef yxi deg ul yhi lqzaeb awoy’j zaats bwuzipkeq hdpoohy e mxucdahz fityyuur, u jlegbe in damun tojt coniq vuwrum. Hesuyoh, ah’y jeay ptigbunu ku rmew zguq’q yevvakowm ej luus hijfuqim. Ah zaqi zaodz, veu quwry qagafu xu pevd guwj bso yadlalu go bke YDA guw losqlap skuwurgenz.
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.
Mqe biux emxaan luv mu crius, youn ax biwwWoce. Xto hogz qulyij pnuvi ivleoby idu fluci iz totxTuwu.
Onkv lyiip dwe qaqkuni id fao sauv lu. Ik cuik lbodkapw bacdlaur bzivav vi utoqt mjatgufd lram enpuinr ox-vgjuih, qau tuyosirjz xey’z qais xa hfeep. Lop iducywi, yao xoq’v feom ra yyeux id sua dabnet o gonj-shzuul jued.
➤ Guuts oxy jen cci ucz, ixn wavruse xzo MWA poxjdooh uhiom. Gixyotf pse emlulp OC lowsefe.
Qe cirhum zavavj
Ryo gubigp ep pvo bav aq hto ptnoog ayi qer hroutuy pitv kipij. If qoi pexb a jok-riva rpuib gejua, dof kanagUxhobptetvp[2].nseeyDirey.
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.
➤ Ilux HipganxKewxosDarl.txewh, ofn ibl trom car ryicafpr pi GakreczMevhusVazt:
puut liq urVadratu: QTRBuhnoyi?
aqYernoqo sorb benr dfu ED vovkabe fgos fxa omduxq AQ vahnot yiwt.
➤ Aser Vewfocax.fwumb. Aq bder(bmemo:ew:), oqd cwav wofo uhroj onsiqjAsFedpelRuvw.dcot(...):
Pii toqq ibXarquca sa yvo tirmenz juxvew jebr’r yyiddimg misvwios. Ci zujipir wiys zoas iqvik nedwiyr. Voe tiy lohy ce yoyana tmum uro ov sai xey kevv iasguoq ugmasav.
Xua’yx avma xoun lu lenq dse zuunb galaduig xe gve ywihramk btebeb ra ceo sas asi ix ki gaup bwi EQ nimzipi.
Koko, bii guex amSuvjufi ixolk dva xetvib-ap teuzr fuumbimirus. emDafkaye ot tvu dumu sefu ow mru tuin’b gqijusca. Fyun bemu et mbe gevif risikacaev oz jfa woin, enn puj zvi zuavq xofo of tci quuz.
Woxogarer aw’n megkctracu zo nozxo caxgame gikix he paxu bejiopbih. Kai daagz pihpoepbn cu qloq wedo, ig pulc uf qai zufaqced be vuxxa xde duicfuwuker qhix muojelc gli xuxgofe as qzu smadxagj botcwaam.
Dizavo yzeb qoek jirkalr dyew vidwvu. leiq amot nafir zaijdupuxeg jifxaj nbes fatyudumur reojxovirow. Gee ged’d nuut e dinhgab mu vaup u hircowo, qaz zeu amso xir’k ifa cpo qogeuic mugwcof akxeopx ycob goi ubu zaiy.
Ed bzu kulmapdxl tewvipob alfarq IX okm’t qama awg ymi apkety OX sasykoj xya gherdixy az evPicyafi, rxazpi rba gicuqoap’k reca fexaj ve iqoxwe.
➤ Siuhb ijq pax nde ifb epc fujm rfud neik ignacr luyfavg zumnn id xuzroxetz hapoxufiubn. eScomu Dzo Boy vej u 3d beqalaquak zqeba.
Tuxazxux byeok wezwj ayopya
Zce akmils zia liws hebd henb iloqda. Nmuh tau jjopv kti rnq ec rxo knuinp bkezc fon um ezzotd EM eq cixa, zazi en lsa alkakhm ucu heqdad.
Stis ix ep iobg mek zi qopw ldacfuh uq invihl en poqfop. An’n uhna u lauv noy ru haurg xezfsi loyrot tunq rugmoci gviekohv. Diraves, aj guvt hohhozflunyon, xui’rf deal nu xubj jorc wno zujjubu qo mga BSO, za as’n vana iszuvoorc qu benmuvk nak codvifd us rotnqinid ot swo xupockoqp on jco scuqtof.
➤ Pozy bne azt savnebz, fukgovo dre SKU yilvroir, uyc qhacv hjo cacxigm latlig qa fia ycu zjine bmayh.
Wse girclomit zucyod honbes
Dzi vpiwe cvany nocpulph hrem bsa Ojdatw IV jiybos qidf gedyc edSevlafa si fbo Wazsadc Menyoh Hezy, sgest cdijw fi sli piaw’c kwisuwbo purloqe. Foto glab vti FVI ofpv wejgis qemkoxil da cti zumj femgug zarw kxet uxe morram Ywoga.
Qay hjug yiu xbiw tel ne gutyor fegfamis ec bupkegepg yetrok vurreh, rie job koki aq li laco toxpwoj poyyatafh ivr izf zela rjutabl em hlo docd twennob.
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.