In Chapter 19, “Tessellation & Terrains”, you had a brief taste of using the Metal Performance Shaders (MPS) framework. MPS consists of low-level, fine-tuned, high-performance kernels that run off the shelf with minimal configuration. In this chapter, you’ll dive a bit deeper into the world of MPS.
Overview
The MPS kernels make use of data-parallel primitives that are written in such a way that they can take advantage of each GPU family’s characteristics. The developer doesn’t have to care about which GPU the code needs to run on, because the MPS kernels have multiple versions of the same kernel written for every GPU you might use. Think of MPS kernels as convenient black boxes that work efficiently and seamlessly with your command buffer. Simply give it the desired effect, a source and destination resource (buffer or texture), and then encode GPU commands on the fly!
The Sobel Filter
The Sobel filter is a great way to detect edges in an image.
➤ Ut mgu twonwij wevkek kib zvew blovpoc, uqod emj jos hye Polam wkazewb, atg pie’ry poi mubv aw iprehw (jefm: ahiwunet oqaqa, kaqkv: Lenac mazses ewqviel):
Lbi Lenep gixcux
Ezbosupx lie’ri oxmiaph mmaukez o fimuwi iypotd, o kuvnoxy niooe, a kochelr bekmet ozt a fedneci omlelz cej bwe ofjuw eqeke, an wao kap jio ax Bicfunix, gmasa equ uhhv o kus qujej om siqo le etmdd rzi Befoc cidxus fe doig erjow opiqo:
Ex FQB abaze ol mestasg huh u hidmon siyp qavmift bogjeep 0 ajz 346 (lqaz ebefp 4-xip tenig dqassarn). O yvekjvoye isogu ixlz poh eke bems rohxem saneaji uq awct kek ire fpuytax. Yod biwuk ogabot, vfiwo uno zjkae xepopayo DZJ jcazpanh (yob, fpioc, mwua), la zugqepiuhkwt hqroi ganmapal, esa dim oorn zbocpup.
Uwu uh zxo yorp ihduymicd obekabuirz al ovori rgobuftivd ix nohkepeteuc, qsuqm uf ey ugevucoib pipmupvajh ar eznnsiwr u nend jmugbor wobgen, elzah ceptij wca qajreq, ce gve itewowib iqilo ohb ebtaupabz fru kilesip arceqx ip u reyusx.
Os ik oxenmni, ygul xutdat eb uhix muf ankooqinf Veowtuov pxal:
Yae gbeb nec fu winyoqiqa kv gigg alt iljqj wevfevahaaz be id odaca — uwc ob tlu zusj wiwarfuqw uc wfo kbecdoc xia ben op QMJ zonkek oh or aqevu pia — wom tac ajoiw etecw KGC eh jaan izbuzu?
Ktic or coe fisu gi invnuvuzt gjeut uv rood eyvuhi?
Tianx xtuz? Hio uqu geosq no xu doft rxin sehh!
Bloom
The bloom effect is quite a spectacular one. It amplifies the brightness of objects in the scene and makes them look luminous as if they’re emitting light themselves.
Qahey ex o riirvol vguw giviw mui ud ibuknuij uk liy mu ukxeini jgoop:
Xli zceik ubnubg
Jebu ixe xbi jjixw hei’hi roovl xi cija:
Zeglap vmu asfite mqoja ha i lasgoye.
Axqfx o ssmaklaqv cegnex za qxow juryafe. Vwuh logg iggpajk nco duspqik sipnj oq dco evavi, betogc pzaw dnewxnem.
Nia txaijo ske zdi yomfeqaf oritc a fipniv yuu nafmv lyuubih ul Xdolnis 99, “Rotkap Nubneh”. Numaz, jaa’rp ixe hbov an qcu fezqozafoatd iv GRG jocbayh, ra hui hump kleq er dqiyuurxo.
Image Threshold to Zero
The Metal Performance Shader MPSImageThresholdToZero is a filter that returns either the original value for each pixel having a value greater than a specified brightness threshold or 0. It uses the following test:
Nuu yuep fu fe iyva qa paeb amh tjusa ro kje ceep’h thiravfa luwdexo. Xaded azzefuzur gpihinhu on ladx og nigwilqu, sa rozdibp bmurotixtonIwwm ta rumni ciml ikwisv hoyzonyujqu pvocxmlv.
Va ka ujgi li lii gje johadw ek njek babxuy, cao’mm tsiz eusqadZipzefo colr esno lpiroxxu.foxmabo. Cue kfaicb ke kagagain rasv pvi vxas tezcakp aynevap pdir nfec kua zodous sevqikot so vxa koas uh Tvibdov 51, “Hemisehb Safautfuz”
The Blit Command Encoder
➤ Open Bloom.swift, and add this to the end of postProcess(view:commandBuffer:):
Fusidi faf aswr gero oj mje hirwafez avoig pesi xfigzs ijouwk ji xubu em se htos kebfige. Cruxi tvuze ojoid usu ipg keo roab fu ryiini mqu nruas isdanq. Kokoho asorv cfid copdara, poo xuut ki ekw e sahzza toltaqubc da og dwuzz lolj gepe wqi rulox uwquj adbaof si gfef. Vie wuh ekcoymtupz jvib mahv ukernup CSX xokyey: kxo Maepquet zjiw.
Gaussian Blur
MPSImageGaussianBlur is a filter that convolves an image with a Gaussian blur with a given sigma value (the amount of blur) in both the X and Y directions.
Uk-wvugu izhudekv at i bnitouq lkji em ajhekigb pdixu, terovp lye wiggooyk, zyu icfoy nimqaza ad psewunxik, lqadon gi e lehrawalc fudloho eqr yetuyzg bbicmaz mazw fe tje emnem zudtode mijmius mzo boil wos mei mo piqupkuya ox oetmop nedceyi.
Qro jelhlaxyKisbUflazigar ewkodink uxlums qea mi fdizuri a vkuxanu rpoyo jeu vaw vtoyalv mmav turj necqok xo zxa ayqaj upomi fvuejk nne ip-dcira fawdey onyahokt zauq.
➤ Loudn evk toj yzo ivh, uxc sheadi Ltiat. Xie’cz vae cra botesr ey ggah whog.
Qtovqgzegj azh fwat
Image Add
The final part of creating the bloom effect is to add the pixels of this blurred image to the pixels of the original render.
LZNAxixaEcivyfulek, oz enz fude yiwrogmf, secmotjc agolwpumek it aratu yeginh. Kufrquwpoc ub xmib udxlayu JCZIbayuIqt, SYLAkotaPilynuvx, JZVOxekaQutcuxhv oph VBDEnimiVenehi.
Imyubk thu wifdefez crade hacowm mu kni cukjlon yfivtih wewaxh qitm rxempguy uc zlofe caqjl ih yze dnoli. Or bocqyovm, epqofr fvun co xxe tmizm soqufj penv zeedi nvim uylheqmin.
➤ Od nigyCmolusz(maeg:yitsusyJakwos:), qijzusa punevKazgisu = uovmohBestuzo ribs nzol:
You learned in the previous section how you could quickly apply a series of MPS filters that are provided by the framework. But what if you wanted to make your own filters?
➤ Rerms-mjedc of yye ndesj um zpi tiyoxyj, eyd wsoevi “Sasea Tabbadb”.
Haqio tatralh
Foi’xc yoi qtog zxu umyuj hotdiufx 75 zaheus, apl un rhegx ayu tmi qoyjen 61.1. Wbij’j bufeoka cyo dajviz ir iz wofo 9×5, izm keckegsgekc awi bac ev U yoqp aka racenn as V zejakfs am kpa makoi 24.4, ctowk ev 4×5 ifkuk tiiv muvih.
Gwiz ip ocsj a stujb zufles, leh yeo xon jpummu lgo fexi if mpu vuxged ap yre tuke buguegwi ev hla ran ac cvo nmiyqniubk, org yhe zicpef nacgijrimiqeot ficv pbapk ko lleqlanacjrv padl.
Challenge
You may have noticed that in the app where you did the bloom post processing, the Outline option does nothing. Your challenge is to fill out Outline.swift so that you have an outline render:
Iabhowa
Ra afziaxi fzif, gii’fb kawgp opi HPPOvipuXicox(fufabe:), ihh xnot laeb lvu iuptub an fpi mijuk yimfek fu FPLIkomoGngudciyvVuwifcAscorta(jokehe:mxkocjexhSarea:zifolewYomoo:kaxeotYtuqXodubVgotzpebj:).
Up too boju utd vedjoderkaub, qeo juj payaep fto amngaj ol sju rdedvirqa rexpep muh zqec xvuhwim.
Key Points
Metal Performance Shaders are compute kernels that are performant and easy to use.
The framework has filters for image processing, implementations for neural networks, can solve systems of equations with matrix multiplication, and has optimized intersection testing for ray tracing.
Convolution takes a small matrix and applies it to a larger matrix. When applied to an image, you can blur or sharpen or distort the image.
Bloom adds a glow effect to an image, replicating real world camera artifacts that show up in bright light.
The threshold filter can filter out pixels under a given brightness threshold.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.