Metal for Ray Tracing Acceleration
Metal for Ray Tracing Acceleration
WWDC 2018
Metal performance Shaders
GPU — accelerated primitives, optimized for iOS and macOS
- Image processing
- Linear algebra
- Machine learning- inference and training (new)
- Ray tracing (new)
Ray Tracing
Tracing a ray’s path as it interacts with a scene
- Rendering
- Audio and physics simulation
- Collision detection
- AI and pathfinding
- Ambient occlusion, refraction, area lights, depth of field, motion blur
- Method of choice for photorealistic, offline rendering
- Significantly more computationally expensive — doing more work to simulate physical effects
![](https://cdn-images-1.medium.com/max/800/1*4U3Ok6ZRrmZHIpT5uOCnyQ.gif)
Rasterization
- Projects triangles onto the screen one at a time
- Fast-method of choice for games and real-time applications
- Difficult to model behavior of light
Reflections
- Can be computed accurately with ray tracing
![](https://cdn-images-1.medium.com/max/800/1*CTv5Ce4HtkDVb4FNkMoWzA.png)
Soft shadows
- Can be computed directly with ray tracing
- Realistic transition from hard to soft shadows
![](https://cdn-images-1.medium.com/max/800/1*BJkmOv4910-yybj5QhW79A.png)
Global illumination
- Naturally modeled with ray tracing
![](https://cdn-images-1.medium.com/max/800/1*gJw4CZur4qa7HPvYrj1Wfw.png)
Rendering with Ray Tracing
![](https://cdn-images-1.medium.com/max/800/1*VrxqCcFX1WNTIh-d0DtfeA.gif)
![](https://cdn-images-1.medium.com/max/800/1*rTTOoL5m8Er_R0AlpAhWtg.gif)
MPSRayIntersector
- Ray intersector accelerates ray/triangle intersection tests on the GPU
- Accepts batches of rays in a Metal buffer
- Returns one intersection per ray
- Encodes into a Metal command buffer
![](https://cdn-images-1.medium.com/max/800/1*z_DDUBSvx31Sc1JOPXvGvw.png)
![](https://cdn-images-1.medium.com/max/800/1*PntlqlRU0dPyh4_rX0qnNg.gif)
Accelerating Ray/Scene Intersection
![](https://cdn-images-1.medium.com/max/800/1*fhAEwiN0-3Ypo9vggg4FBQ.gif)
Ray Tracing with Metal Performance Shaders
![](https://cdn-images-1.medium.com/max/800/1*a2KnvNE7luDje6El-XTO8A.png)
Primary Rays and Shading
![](https://cdn-images-1.medium.com/max/800/1*2mTNIiiBesnuutpVwTEqsg.gif)
Creating an Intersector
![](https://cdn-images-1.medium.com/max/800/1*9lEtzFoktXddeSpdw-jooQ.png)
Creating an Acceleration Structure
![](https://cdn-images-1.medium.com/max/800/1*JcvxTdLQZllphrGMMtoAcg.png)
![](https://cdn-images-1.medium.com/max/800/1*8qYIJi8G6mkedHYL_uXmSw.png)
![](https://cdn-images-1.medium.com/max/800/1*d06cCWspA7EZ394MBhBlyg.png)
Generating Primary Rays
![](https://cdn-images-1.medium.com/max/800/1*thfuQKsGMUfO2ZiF8o2EZg.gif)
Finding Intersections with the Scene
![](https://cdn-images-1.medium.com/max/800/1*83quOpyjCmB70nVikx3phQ.png)
Shading
![](https://cdn-images-1.medium.com/max/800/1*aZ1O0UVGkwcj1qPH0HfByg.png)
![](https://cdn-images-1.medium.com/max/800/1*gEamK5dnWtLV2UfIiKMO9Q.gif)
Shadow Rays
![](https://cdn-images-1.medium.com/max/800/1*x3gSzqPr2KFS8qNAvwfcVA.png)
![](https://cdn-images-1.medium.com/max/800/1*eDiUKuEoRpW0XtQ00uvB_g.gif)
![](https://cdn-images-1.medium.com/max/800/1*MUWCryHBrw40VcWe9bIOig.gif)
Shadow Rays vs Primary Rays
- Maximum intersection distance
- Don’t need triangle index or barycentric coordinates
- Propagate color from shading kernel to final kernel
![](https://cdn-images-1.medium.com/max/800/1*OkWXz0bUeidld-6KZH-pvw.png)
Customizing the Ray Struct
- Ray type is configurable
- Choose what data is provided to the intersector
- Append app — specific data
![](https://cdn-images-1.medium.com/max/800/1*3J79J8b5GmrEBQrXS5PVeg.png)
![](https://cdn-images-1.medium.com/max/800/1*18XRRfYH2v-hX2b1lq77Xw.png)
Finding Shadow Ray Intersections
![](https://cdn-images-1.medium.com/max/800/1*iWxJzvKphM1v7X6blg8eYw.png)
Adding Ray Color to the Image
![](https://cdn-images-1.medium.com/max/800/1*nBXVjthtxoNL7rrK4DgxRQ.png)
Secondary Rays
![](https://cdn-images-1.medium.com/max/800/1*75-qrAgT4H1vcc-uwZ_gpQ.png)
![](https://cdn-images-1.medium.com/max/800/1*_aIRqnvhY-NaI7jaalHDjQ.gif)
![](https://cdn-images-1.medium.com/max/800/1*lmsqqCuIu_FT4c2wGEqF7Q.gif)
Updating Secondary Rays
![](https://cdn-images-1.medium.com/max/800/1*XgeVsVcMX8irQD1TtGQ5KQ.gif)
Demo
Multi-GPU Ray Tracing
- Split work across GPUs
- Copy data between GPUs
- Synchronize execution
Splitting Work Across GPUs
![](https://cdn-images-1.medium.com/max/800/1*nIYB85ZzxnciwVQnyBW3uA.png)
Copying Scene Data
![](https://cdn-images-1.medium.com/max/800/1*WuD16gYlze0eC5QxiiASlg.gif)
Creating a Shared CPU Allocation
![](https://cdn-images-1.medium.com/max/800/1*u8WA2ZrfKCCxLlniIA6I0Q.gif)
![](https://cdn-images-1.medium.com/max/800/1*0mqBsAt-h6gLj48BIYibcQ.png)
Synchronizing with Metal Events
![](https://cdn-images-1.medium.com/max/800/1*YAvvfgZvJMhABq9wRJ1MWQ.gif)
Load Balancing 👍
- GPUs can have different performance
- Some regions are more complex to render
- Adjust region sizes to keep GPUs fully utilized
![](https://cdn-images-1.medium.com/max/800/1*D6_i6DNK386fEZi3FFZcyg.png)
![](https://cdn-images-1.medium.com/max/800/1*qqihUPwhiVyPm-8CbmBaLQ.gif)
![](https://cdn-images-1.medium.com/max/800/1*dM4VF_qFSnb13eef7Jmw-A.gif)
Timing GPU Work
![](https://cdn-images-1.medium.com/max/800/1*3jEG2eBdT1KNAZtqm9AQ6w.png)