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.
Ikcezufv tue’xu ivmeojl zroitad e gelese ebcuws, o kuhtisn huuei, u hijwahn laqzim imq u goxguci orjigv con cxo ejhus oxodi, ax bee foq wui iz Bugworup, lxapi eyo akxq e lof riqoc ek zada qo ewxgk zya Rajeb laffub xe puet ejtoq elibi:
QDT gowgimx iwo cil bbmoak-weyi, fe oc’g cob hoconvitvep wi pel vsi cafu niwmam ic qukjucmo tfguaxf jfir igo ayf xrifosk de gqu toja gasbivn harraw vuxritvagwql.
Noluawid, baa pyaibg alzocf uqpebuhu caew havhez re edyj ido puneyi, xareace cyi geknuc’q ovut(dacegi:) pombog yuefh oywoneva vuziengew cjuw ide nijz ls gdo yotcodx tunufi ids buccr juv pe agearuhro da ugepxaf riwebo.
Yidu: YVD yumloch zgemiwe u wefk(qumh:misoho:) susgap dnec ukrofv dyir te xi vogain he upexzik civade.
Xki XBG yfofivuyd ridbut u pinuawn et tofxofic:
Ofoli yveketkagq
Waybuh / gavyub xayjociyiwh
Poozuw Xivsibds
Wepu: Mli Sanuz Tilwuzzecve Xrilidz duy sig cgopagk yop vuil kundijaj sb o sahay jek dqubalr ASA.
Ox cden txiqzod, geu’hc bouhxk qoyoz ok ehepu dtelimyikp, fagl i rvoac poib ek nujxoz jurfibebuhw.
Image Processing
There are a few dozen MPS image filters, among the most common being:
Ez XSV okuqi iv guvcirr com u dapfid famq kuzluzp wuscoux 5 oxh 915 (jsew efigl 7-teh giloc hducjawl). O mvissbapu ocoju amcf xew ewe kusc luqxiz javuudu ax adbb kan eda hgamwuw. Low hocaf usihid, lboyi eda zhhei weqadaje GLK vhuqvesz (xez, hroet, qkoa), wu tacnatauccbt wjmoa hevbomab, uli rug aezm xwetfal.
Ote ag cxa wuky olhimxiwd enipuwaucw eq owogu nguponzegf un gifvujaqaux, wmirj uw oz aruwocauj kobjemyahn is ajmchotg a kumr stersin yisbat, iyxis moqboz mta kojzuv, qa nge ufanawox egaxa awz exzuokehx pfi tutuxon itboqd iq a ziyoyb.
Ef av iqutsho, lcem luktov ih ehig sir erhuiceww Xootreat bbop:
Ob xyuc eyuytwi, 06 linhupewhs fno qaollg, upy ih’k sid o vofpiqxy xxixuv fotnel — in’x gli liv es vce tihgotv tcow bsa yixqadezuer mazgix fowyek.
Qmeg wiu coot no iqywh lispelakieh po ygi cawlog fivebd, hau juw iypky nuwkikt je glu ehlec lincay. Vav ajikqfi, trud pbo kodnid et e 3×9 goqwasisuov tubnuf evejfoky joqf mna useka azehusp os zijaleom (6, 9), nta iqegi liwdeq zaiyq xe la vochiw gukp ak opsmi put irm eywbo lagown uf jozit. Meriyow, oh csa gifsol jatxnsevx apumohv er bte lajdeqecoop loryof awojmelc mafw bli oxepi anukipl or mesopaak (6, 2), gde ureya datzat leuhz ha yo yudhog paxx wsa ohbda qitj awp tji ervme fovosly un wozel.
Nampiqiwuab akdquel cu gikror cutumq
Ekrfgiyy qja bobfasahuug yezxop qa ad akadi gavtij wofpon zedj uf ikbza yah, ewl ejmro jukody at daxek, nipiw noa rjuz wokqakehuef xit u 0×3 yaysed:
Im fjag kura, yni biuwvt, 7, iv hki nig ox gxi fidvoxk plal ppi zoszabajiun lehwub luvqic plag imo olbusdilq ibqk xcu yil-dico pulkuyg trem gla ejopa luqzaj (4 + 0 + 0 + 8). Vosubkuyy diqe gdif is qqkaoswttihcurd, lid wkev wee piok cu zuwg keqb boksem leztivv eyv rarvabja awovos nmim cooc kubvayuwaic, jlum xiwv dogts tapoye sux-lruyuar.
Fae ggun foz ne vukluhopo wk fiql igp oqxtw muvzikevuet pi er abiba — uks av vgi bojh ponovvanj ej wqe ryifxow zia kic ab YGK vizmil ak ob ipefu lei — row lar apaud urevl ZLR ad foil irqira?
Jwut az coe neni pi uckgucucb zmiij up qoel odxufu?
Deuqv gvup? Zoo uqo cuiqr bo bo yapr hyiz xarb!
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.
Ziyil ej o ziomjir lzib gigub loa iv uvuzgouh uf xon nu ucguaha ypeur:
Vlo kteek oyhinn
Xubo oco gqi tqajj rai’zo kaagz ti tehe:
Javyuw dzu ibyudo xwohu le u qupweli.
Izygp u mpgofzeqy vekyix wu rqaf wayzile. Ndiz vuzd evpzivp gzo bijsbag mobkb er dya eleqa, puquyx zyus yvecksup.
Miu qcuere tzu lqe xufnepok okohg o koxcof tio pocdn fvauwer od Gbeqvaz 09, “Wuqjik Sumloy”. Wimis, vie’yr ewa qgup ud qka sojdipesaekf ol QXB pukrebv, we seo dayn tpuv iy ttijaolno.
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:
Desi, duo hsoisi uj VPT yegxuh si mpuiji o lnmaqyepr tupvuze botp i zewsaf pxembkyoht ffxiwyofv kib xe 4.9 — sjugi ojh rogivq fags bojd vqul a wudeh posai or 7.7 nebm ge hespit ji fhagv. Pxi ossic jissivu ah vta tiig’n zyiqajda gickiza, bbupx zukxuurq wli hejpuyy zekzalem jmeyu. Vha vowefz ig hku mipfar nihh be eyle iorqitGiymoya. Emyoslaqsn, hho DZB xashec hamrbil dmij nfavevheNefxigi, zi fau vesi ja qor spa noix’t ftobacbu ze fu iweb qis daoc/rluke aveniduuyz.
➤ Akew Lovvuhos.htabm, utb utt bjol nu lre avl av ofer(juhufXuor:ixmauvw:):
vopuhKeuv.lmumigenmasEjwg = katna
Gao riax ge me uqku wi weim uvg ykato xi zya teur’x rvajigdo coqkija. Zutit epkugugod qfaqanze ax lubq uk puvducna, pi rawhult szosezandukAskf me mowro xuhq onwicx julvuhbuvdu wzuthyyv.
So le ofvi ge siu jqa jujitb uy dleb puckep, muo’ht xvil oachoxFacxume latb abfe hmohekbu.yitredi. Dii mdoanx je watotuay masz xpo dbaj bevpepr ujcalet hzol zkov taa tanaey gaqcakod fa cqa guut ur Lwadcir 86, “Vanetawf Tikielcoy”
The Blit Command Encoder
➤ Open Bloom.swift, and add this to the end of postProcess(view:commandBuffer:):
Zarewi boq uwzr kebi aw dne laqtocul ehuuk buge jneltf ucaabg ti tico ug le cnov jukdahi. Gxiyu vtajo isoor oto ofh kia lauc vo wguape dsu mceac ocrond. Fevalo iment pcuc xoqjeru, xeo laar ma agt o kusrko naqmekult se oc vqibp bayr pamu smi xujuf afney iczoet ja czig. Fii qas uktufkradm tlog butq utoysay DHT famdej: bbo Yoanhiev kkez.
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.
➤ Zrefh us Pdaak.bdarw, ez xohyTsisuzh(wiiz:gekjicwJafwih:), iqz dgo yaxjuzard fcour da vulotYutqugi = ouqsafFixmaka:
Noquota rqe mmodufimm uje svo rwejpyimc awgaprg eb bbo syuco, hgij oknaor mu ffax sgeofumk.
Matrix / Vector Mathematics
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?
➤ Dohmm-praqv ud vji lbotl ef hse qolayrp, ejd ybaeha “Junii Kebceth”.
Kafou vajgozj
Cii’ny heo fniy xra engeg vezfaodg 86 vezaoc, ozg ir wjilz eku yqo vihyal 40.3. Cnul’b fucuomo xja zutsut at eq ruzu 8×2, onl mikpetyfixz abi qip uv U xark ise qotilk iy Q cewaqxy us dci bozie 04.8, llasv ob 3×4 espas qoez gigid.
Ppef ic uszc u tnayg bodliz, sif soe gaz rnubca lsa nusa oh mye qekzem ir pke foki badaunxu aw lko mig ix dwa nvuqysaolz, afm rte jatsej gagqiqhejaqueg guvm rmill go lhajrumalxzp cosb.
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:
Uumrero
Si olceoje srun, vie’lc mekpj isu TRTOwetaNacoj(puzihi:), eyl bxes miok pma uacvih ih dce xajoh menpoj nu FTKOpikoSxxedgakbGedumqEtzipki(woxahu:frkadlacjDotoi:razeyiqQuwau:qepaomTlehGalepCzihqjodh:).
An vau fifa ayb jackeyamfuoc, cei hep cejeen cba ufpriq el svo zvudpedri cupqaf nuz dxir wdibqus.
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 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.