Metal for Ray Tracing Acceleration

WWDC 2018

Posted by Den on August 14, 2018 · 11 mins read
Metal for Ray Tracing Acceleration

Metal for Ray Tracing Acceleration

WWDC 2018

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

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

Soft shadows

  • Can be computed directly with ray tracing
  • Realistic transition from hard to soft shadows

Global illumination

  • Naturally modeled with ray tracing

Rendering with Ray Tracing

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

Accelerating Ray/Scene Intersection

Ray Tracing with Metal Performance Shaders

Primary Rays and Shading

Creating an Intersector

Create an MPSRayIntersector with a Metal device

Creating an Acceleration Structure

Create an MPSTriangleAccelerationStructure with a Metal devcie
Assign vertex buffer
Build acceleration structure

Generating Primary Rays

Finding Intersections with the Scene

Encode intersection test into a command buffer

Shading

Shadow Rays

Shadow Rays vs Primary Rays

  • Maximum intersection distance
  • Don’t need triangle index or barycentric coordinates
  • Propagate color from shading kernel to final kernel

Customizing the Ray Struct

  • Ray type is configurable
  • Choose what data is provided to the intersector
  • Append app — specific data
Configure MPSRayIntersector for new Ray struct type

Finding Shadow Ray Intersections

Modiry call to MPSRayIntersector

Adding Ray Color to the Image

Secondary Rays

Updating Secondary Rays

Demo

Multi-GPU Ray Tracing

  • Split work across GPUs
  • Copy data between GPUs
  • Synchronize execution

Splitting Work Across GPUs

Copying Scene Data

Creating a Shared CPU Allocation

Synchronizing with Metal Events

Load Balancing 👍

  • GPUs can have different performance
  • Some regions are more complex to render
  • Adjust region sizes to keep GPUs fully utilized

Timing GPU Work