Top Banner
#WWDC18 © 2018 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. Dan Omachi, Metal Ecosystem Engineer Sukanya Sudugu, GPU Software Engineer Metal for OpenGL Developers
347

Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Oct 03, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

#WWDC18

© 2018 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Dan Omachi, Metal Ecosystem Engineer Sukanya Sudugu, GPU Software Engineer

•Metal for OpenGL Developers

Page 2: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

•These legacy APIs are deprecated •Still available in iOS 12,macOS 10.14 and tvOS 12

•Begin transitioning to Metal

Page 3: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Choosing an Approach

High-level Apple frameworks • SpriteKit, SceneKit, and Core Image

Page 4: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Choosing an Approach

High-level Apple frameworks • SpriteKit, SceneKit, and Core Image

Third-party engines • Unity, Unreal, Lumberyard, etc. • Update to latest version

Page 5: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Choosing an Approach

High-level Apple frameworks • SpriteKit, SceneKit, and Core Image

Third-party engines • Unity, Unreal, Lumberyard, etc. • Update to latest version

Focus today—incrementally porting to Metal

Page 6: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

•Metal Concepts

Page 7: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Challenges with OpenGL

OpenGL designed more than 25 years ago • Core architecture reflects the origins of 3D graphics • Extensions retrofitted some GPU features

Page 8: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Challenges with OpenGL

OpenGL designed more than 25 years ago • Core architecture reflects the origins of 3D graphics • Extensions retrofitted some GPU features

Page 9: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Challenges with OpenGL

OpenGL designed more than 25 years ago • Core architecture reflects the origins of 3D graphics • Extensions retrofitted some GPU features

Fundamental design choices based on past principles • GPU pipeline has changed • Multithreaded operation not considered • Asynchronous processing, not core

Page 10: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Challenges with OpenGL

OpenGL designed more than 25 years ago • Core architecture reflects the origins of 3D graphics • Extensions retrofitted some GPU features

Fundamental design choices based on past principles • GPU pipeline has changed • Multithreaded operation not considered • Asynchronous processing, not core

Page 11: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Challenges with OpenGL

OpenGL designed more than 25 years ago • Core architecture reflects the origins of 3D graphics • Extensions retrofitted some GPU features

Fundamental design choices based on past principles • GPU pipeline has changed • Multithreaded operation not considered • Asynchronous processing, not core

Page 12: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Design Goals for Metal

Efficient GPU interaction

Page 13: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Efficient GPU interaction • Low CPU overhead

Design Goals for Metal

Page 14: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Efficient GPU interaction • Low CPU overhead • Multithreaded execution

Design Goals for Metal

Page 15: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Efficient GPU interaction • Low CPU overhead • Multithreaded execution • Predictable operation

Design Goals for Metal

Page 16: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Efficient GPU interaction • Low CPU overhead • Multithreaded execution • Predictable operation • Resource and synchronization control

Design Goals for Metal

Page 17: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Efficient GPU interaction • Low CPU overhead • Multithreaded execution • Predictable operation • Resource and synchronization control

Approachable to OpenGL developers

Design Goals for Metal

Page 18: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Efficient GPU interaction • Low CPU overhead • Multithreaded execution • Predictable operation • Resource and synchronization control

Approachable to OpenGL developers

Built for modern and Apple-design GPUs

Design Goals for Metal

Page 19: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Expensive CPU operations performed less often

Key Conceptual Differences Expensive operations less frequent

Page 20: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Expensive CPU operations performed less often

More GPU command generation during object creation

Key Conceptual Differences Expensive operations less frequent

Page 21: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Expensive CPU operations performed less often

More GPU command generation during object creation

Less needed when rendering

Key Conceptual Differences Expensive operations less frequent

Page 22: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Reflects the modern GPU architectures

Key Conceptual Differences Modern GPU pipeline

This way there's much less cost to use them later on when actually rendering

Page 23: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Reflects the modern GPU architectures

Closer match yields less costly translation to GPU commands

Key Conceptual Differences Modern GPU pipeline

This way there's much less cost to use them later on when actually rendering

Page 24: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Reflects the modern GPU architectures

Closer match yields less costly translation to GPU commands

State grouped more efficiently

Key Conceptual Differences Modern GPU pipeline

This way there's much less cost to use them later on when actually rendering

Page 25: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Designed for multithreaded execution

Key Conceptual Differences Multithreaded execution

Page 26: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Designed for multithreaded execution

Clear rules for multithreaded usage

Key Conceptual Differences Multithreaded execution

Page 27: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Key Conceptual Differences Multithreaded execution

Designed for multithreaded execution

Clear rules for multithreaded usage

Cross thread object usability

Page 28: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

True interaction between software and GPU

Key Conceptual Differences Execution model

Page 29: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

True interaction between software and GPU

Predictable operation allows efficient designs

Key Conceptual Differences Execution model

Page 30: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

True interaction between software and GPU

Predictable operation allows efficient designs

Thinner stack between application and GPU

Key Conceptual Differences Execution model

Page 31: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

OpenGL Context

Application Renderer

OpenGL API

Page 32: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Device

Page 33: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Device

Page 34: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Device

Page 35: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Device

Page 36: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

PipelinesBuffersTexturesDevice

Metal API

Application Renderer

GPU

Page 37: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Pipelines

Buffers

Textures

Device

Metal API

Application Renderer

Render Objects

GPU

Page 38: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command QueueDevice

Textures

Buffers

Render Objects

Pipelines

Page 39: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command QueueDevice

Textures

Buffers

Render Objects

Pipelines

Page 40: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device GPUDevice

Textures

Buffers

Render Objects

Pipelines

Page 41: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device GPUDevice

Textures

Buffers

Render Objects

Pipelines

Page 42: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Textures

Buffers

Render Objects

Pipelines

Page 43: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Page 44: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Page 45: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Page 46: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 47: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 48: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 49: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 50: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 51: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 52: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 53: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 54: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 55: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 56: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 57: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 58: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 59: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 60: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 61: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 62: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 63: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 64: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 65: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffers

Command Encoders

Page 66: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

Command Queue Device

Page 67: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

Command Queue Device

Page 68: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 69: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 70: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 71: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 72: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 73: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 74: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 75: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 76: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 77: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 78: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

GPU

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue

Page 79: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue GPU

Page 80: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue GPU

Page 81: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue GPU

Page 82: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Command Buffers

Command Encoders

DeviceCommand Queue GPU

Page 83: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Command Buffers

Command Encoders

Device GPUCommand Queue

Page 84: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Command Buffers

Command Encoders

Device GPUCommand Queue

Page 85: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Command Buffers

Command Encoders

Device GPUCommand Queue

Page 86: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Command Buffers

Command Encoders

Device GPUCommand Queue

Page 87: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal API

Application Renderer

Command Buffers

Device GPUCommand Queue

Command Encoders

Page 88: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Encoders

Render Command Encoder

Page 89: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Encoders

Render Command Encoder

Blit Command Encoder

Page 90: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Encoders

Render Command Encoder

Blit Command Encoder

Compute Command Encoder

Page 91: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Encodes a series of render commands

Render Command Encoders Commands for a render pass

Page 92: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Encodes a series of render commands

Also called a Render Pass

Render Command Encoders Commands for a render pass

Page 93: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Encodes a series of render commands

Also called a Render Pass

Set render object for the graphics pipeline • Buffer, textures, shaders

Render Command Encoders Commands for a render pass

Page 94: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Encodes a series of render commands

Also called a Render Pass

Set render object for the graphics pipeline • Buffer, textures, shaders

Issue draw commands • Draw primitives, draw index primitives, instanced draws

Render Command Encoders Commands for a render pass

Page 95: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Associated with a set of render targets

Render Command Encoders Render targets

Page 96: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Associated with a set of render targets • Textures for rendering

Render Command Encoders Render targets

Page 97: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Associated with a set of render targets • Textures for rendering

Specify a set of render targets upon creation

Render Command Encoders Render targets

Page 98: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Associated with a set of render targets • Textures for rendering

Specify a set of render targets upon creation

All draw commands directed to these for lifetime of encoder

Render Command Encoders Render targets

Page 99: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Associated with a set of render targets • Textures for rendering

Specify a set of render targets upon creation

All draw commands directed to these for lifetime of encoder

New render targets need a new encoder

Render Command Encoders Render targets

Page 100: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Associated with a set of render targets • Textures for rendering

Specify a set of render targets upon creation

All draw commands directed to these for lifetime of encoder

New render targets need a new encoder

Clear delineation between sets of render targets

Render Command Encoders Render targets

Page 101: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Objects

Textures

Page 102: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Textures

Buffers

Render Objects

Page 103: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Textures

Buffers

Samplers

Render Objects

Page 104: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Textures

Buffers

Samplers

Render pipeline states

Render Objects

Page 105: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Textures

Buffers

Samplers

Render pipeline states

Depth stencil states

Render Objects

Page 106: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Created from a device • Usable only on that device

Render Objects Object creation

Page 107: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Created from a device • Usable only on that device

Objects state set at creation • Descriptor object specifies properties for render object

Render Objects Object creation

Page 108: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Created from a device • Usable only on that device

Objects state set at creation • Descriptor object specifies properties for render object

State set at creation fixed for the lifetime of the object • Image data of textures and values in buffers can change

Render Objects Object creation

Page 109: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Objects Object creation

Metal compiles objects into GPU state once • Never needs to check for changes and recompile

Page 110: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Objects Object creation

Metal compiles objects into GPU state once • Never needs to check for changes and recompile

Multithreaded usage more efficient • Metal does not need to protect state from changes on other threads

Page 111: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

•Metal Porting

Page 112: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Build RenderInitialize

Page 113: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Build RenderInitialize

Page 114: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Build RenderInitialize

Page 115: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Build RenderInitialize

Page 116: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 117: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 118: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 119: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal Shading Language

Based on C++ • Classes, templates, structs, enums, namespaces

Page 120: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal Shading Language

Based on C++ • Classes, templates, structs, enums, namespaces

Built-in types for vectors and matrices

Page 121: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal Shading Language

Based on C++ • Classes, templates, structs, enums, namespaces

Built-in types for vectors and matrices

Built-in functions and operators

Page 122: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal Shading Language

Based on C++ • Classes, templates, structs, enums, namespaces

Built-in types for vectors and matrices

Built-in functions and operators

Built-in classes for textures and samplers

Page 123: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 124: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 125: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 126: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 127: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 128: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 129: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 130: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 131: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 132: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

float2 texCoord;};

Page 133: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;

struct VertexOutput{ float4 clipPos [[position]]; float2 texCoord;};

struct Vertex{ float4 modelPos; float2 texCoord;};

Page 134: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

struct VertexOutput{ float4 clipPos [[position]]; float2 texCoord;};

struct Vertex{ float4 modelPos; float2 texCoord;};

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;

Page 135: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

struct VertexOutput{ float4 clipPos [[position]]; float2 texCoord;};

struct Vertex{ float4 modelPos; float2 texCoord;};

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;

Page 136: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

struct VertexOutput{ float4 clipPos [[position]]; float2 texCoord;};

struct Vertex{ float4 modelPos; float2 texCoord;};

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;

Page 137: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

struct VertexOutput{ float4 clipPos [[position]]; float2 texCoord;};

struct Vertex{ float4 modelPos; float2 texCoord;};

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;

Page 138: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

struct VertexOutput{ float4 clipPos [[position]]; float2 texCoord;};

struct Vertex{ float4 modelPos; float2 texCoord;};

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;

Page 139: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

struct VertexOutput{ float4 clipPos [[position]]; float2 texCoord;};

struct Vertex{ float4 modelPos; float2 texCoord;};

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;

Page 140: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

struct VertexOutput{ float4 clipPos [[position]]; float2 texCoord;};

struct Vertex{ float4 modelPos; float2 texCoord;};

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;

Page 141: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

struct VertexOutput{ float4 clipPos [[position]]; float2 texCoord;};

struct Vertex{ float4 modelPos; float2 texCoord;};

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;

Page 142: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

float2 texCoord;};

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 143: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 144: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

vertex VertexOutput myVertexShader(uint vid [[ vertex_id ]], device Vertex * vertices [[ buffer(0) ]], constant Uniforms & uniforms [[ buffer(1) ]]){ VertexOutput out; out.clipPos = vertices[vid].modelPos * uniforms.mvp; out.texCoord = vertices[vid].texCoord; return out;}

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 145: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 146: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

[renderEncoder setFragmentBuffer:myUniformBuffer offset:0 atIndex:3];

[renderEncoder setFragmentTexture:myColorTexture atIndex:0];

[renderEncoder setFragmentSampler:mySampler atIndex:1];

Page 147: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

[renderEncoder setFragmentBuffer:myUniformBuffer offset:0 atIndex:3];

[renderEncoder setFragmentTexture:myColorTexture atIndex:0];

[renderEncoder setFragmentSampler:mySampler atIndex:1];

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 148: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

[renderEncoder setFragmentBuffer:myUniformBuffer offset:0 atIndex:3];

[renderEncoder setFragmentTexture:myColorTexture atIndex:0];

[renderEncoder setFragmentSampler:mySampler atIndex:1];

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 149: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

[renderEncoder setFragmentBuffer:myUniformBuffer offset:0 atIndex:3];

[renderEncoder setFragmentTexture:myColorTexture atIndex:0];

[renderEncoder setFragmentSampler:mySampler atIndex:1];

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 150: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

[renderEncoder setFragmentBuffer:myUniformBuffer offset:0 atIndex:3];

[renderEncoder setFragmentTexture:myColorTexture atIndex:0];

[renderEncoder setFragmentSampler:mySampler atIndex:1];

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 151: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

[renderEncoder setFragmentBuffer:myUniformBuffer offset:0 atIndex:3];

[renderEncoder setFragmentTexture:myColorTexture atIndex:0];

[renderEncoder setFragmentSampler:mySampler atIndex:1];

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 152: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

[renderEncoder setFragmentBuffer:myUniformBuffer offset:0 atIndex:3];

[renderEncoder setFragmentTexture:myColorTexture atIndex:0];

[renderEncoder setFragmentSampler:mySampler atIndex:1];

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 153: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

[renderEncoder setFragmentBuffer:myUniformBuffer offset:0 atIndex:3];

[renderEncoder setFragmentTexture:myColorTexture atIndex:0];

[renderEncoder setFragmentSampler:mySampler atIndex:1];

fragment float4 myFragmentShader(VertexOutput in [[ stage_in ]], constant Uniforms & uniforms [[ buffer(3) ]], texture2d<float> colorTex [[ texture(0) ]], sampler texSampler [[ sampler(1) ]]){ return colorTex.sample(texSampler, in.texCoord * uniforms.coordScale);}

Page 154: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

SIMD Type Library Types for shader development

Vector and matrix types

Page 155: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Vector and matrix types

Usable with Metal shading language and application code

SIMD Type Library Types for shader development

Page 156: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Vector and matrix types

Usable with Metal shading language and application code

SIMD Type Library Types for shader development

struct MyUniforms { matrix_float4x4 modelViewProjection; vector_float4 sunPosition; };

Page 157: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Vector and matrix types

Usable with Metal shading language and application code

SIMD Type Library Types for shader development

struct MyUniforms {c matrix_float4x4 modelViewProjection; vector_float4 sunPosition; };

MyShaderTypes.h

Page 158: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

SIMD Type Library Types for shader development

MyRenderer.m

#include "MyShaderTypes.h"

struct MyUniforms {c matrix_float4x4 modelViewProjection; vector_float4 sunPosition; };

MyShaderTypes.h

MyShaders.metal

#include "MyShaderTypes.h"

Page 159: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Xcode compiles shaders into a Metal library (.metallib)

Shader Compilation Building with Xcode

Page 160: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Xcode compiles shaders into a Metal library (.metallib)

Front-end compilation to binary intermediate representation

Shader Compilation Building with Xcode

Page 161: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Xcode compiles shaders into a Metal library (.metallib)

Front-end compilation to binary intermediate representation

Avoids parsing time on customer systems

Shader Compilation Building with Xcode

Page 162: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Xcode compiles shaders into a Metal library (.metallib)

Front-end compilation to binary intermediate representation

Avoids parsing time on customer systems

By default, all shaders built into default.metallib • Placed in app bundle for run time retrieval

Shader Compilation Building with Xcode

Page 163: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Runtime Shader Compilation

Also can build shaders from source at runtime

Page 164: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Runtime Shader Compilation

Also can build shaders from source at runtime

Significant disadvantages • Full shader compilation occurs at runtime • Compilation errors less obvious • No header sharing between application and runtime built shaders

Page 165: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Runtime Shader Compilation

Also can build shaders from source at runtime

Significant disadvantages • Full shader compilation occurs at runtime • Compilation errors less obvious • No header sharing between application and runtime built shaders

Build time compilation recommended

Page 166: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 167: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 168: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Display

Textures

Buffers

Render Objects

Pipelines

Command Buffer

Command Encoder

GPU

Metal API

Application Renderer

Command Queue Device

Page 169: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Display

GPU

Command Buffer

Command Encoder

Metal API

Application Renderer

Textures

Buffers

Render Objects

PipelinesCommand Queue Device

Page 170: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Devices

A device represents one GPU

Page 171: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Devices

A device represents one GPU

Creates render objects • Textures, buffers, pipelines

Page 172: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Devices

A device represents one GPU

Creates render objects • Textures, buffers, pipelines

macOS multiple devices may be avaliable

Page 173: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Devices

A device represents one GPU

Creates render objects • Textures, buffers, pipelines

macOS multiple devices may be avaliable

Default device suitable for most applications

// Getting a device id<MTLDevice> device = MTLCreateSystemDefaultDevice();

Page 174: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Queues

Queue created from a device

Page 175: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Queues

Queue created from a device

Queues execute command buffers in order • Create queue at initialization

Page 176: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Queues

Queue created from a device

Queues execute command buffers in order • Create queue at initialization

Typically one queue sufficient

Page 177: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Queue created from a device

Queues execute command buffers in order • Create queue at initialization

Typically one queue sufficient

Command Queues

// Getting a device id<MTLCommandQueue> commandQueue = [device newCommandQueue];

Page 178: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 179: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 180: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Display

Textures

Buffers

Render Objects

Pipelines

Command Buffer

Command Encoder

GPU

Metal API

Application Renderer

Command Queue Device

Page 181: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Display

Command Buffer

Command Encoder

GPU

Metal API

Application Renderer

Command Queue Device

Textures

Buffers

Render Objects

Pipelines

Page 182: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Textures

Buffers

Pipelines

Page 183: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Textures

Buffers

Pipelines

Page 184: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Device

Page 185: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Descriptor

Device

Page 186: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Texture descriptor

Device

Page 187: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Texture descriptor

Device

2D 512 512 1 RGBA8

Texture Type Width

Height Num Mipmaps

Pixel Format

Page 188: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Texture descriptor

Device

2D 512 512 1 RGBA8

Texture Type Width

Height Num Mipmaps

Pixel Format

Page 189: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Texture descriptor

Device

2D 512 512 1 RGBA8

Texture Type Width

Height Num Mipmaps

Pixel Format

Texture Object

Page 190: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Texture descriptor

Memory

Device

2D 512 512 1 RGBA8

Texture Type Width

Height Num Mipmaps

Pixel Format

Texture Object

Page 191: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Texture descriptor

Memory

Device

2D 512 512 1 RGBA8

Texture Type Width

Height Num Mipmaps

Pixel Format

Texture Object

Page 192: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Memory

Device

Texture Object

Page 193: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Memory

Device

Texture Object

Page 194: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Memory

Device

Texture Object

Page 195: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Memory

Device

Texture Object

Page 196: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Memory

Device

Texture Object

Page 197: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes

Memory

Page 198: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes Shared storage

Memory

CPUGPU

Page 199: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes Shared storage

Memory

CPUGPU

Page 200: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes Private storage

Memory

CPUGPU

Page 201: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes Private storage

Memory

CPUGPU

Page 202: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

System Memory

Storage Modes Private storage

Video Memory

CPUGPU

Page 203: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes Managed storage

Video Memory

CPUGPU

System Memory

Page 204: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes Managed storage

Video Memory

CPUGPU

System Memory

Page 205: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes Managed storage

Video Memory

CPUGPU

System Memory

Page 206: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes Managed storage

Video Memory

CPUGPU

System Memory

Page 207: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Textures

MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor new]; textureDescriptor.pixelFormat = MTLPixelFormatBGRA8Unorm; textureDescriptor.width = 512; textureDescriptor.height = 512; textureDescriptor.storageMode = MTLStorageModeShared;

id<MTLTexture> texture = [device newTextureWithDescriptor:textureDescriptor];

Page 208: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Textures

MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor new]; textureDescriptor.pixelFormat = MTLPixelFormatBGRA8Unorm; textureDescriptor.width = 512; textureDescriptor.height = 512; textureDescriptor.storageMode = MTLStorageModeShared;;

id<MTLTexture> texture = [device newTextureWithDescriptor:textureDescriptor];

Page 209: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Textures

MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor new]; textureDescriptor.pixelFormat = MTLPixelFormatBGRA8Unorm; textureDescriptor.width = 512; textureDescriptor.height = 512; textureDescriptor.storageMode = MTLStorageModeShared;

id<MTLTexture> texture = [device newTextureWithDescriptor:textureDescriptor];

Page 210: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Loading Image Data

NSUInteger bytesPerRow = 4 * image.width;

MTLRegion region ={ { 0, 0, 0 }, // Origin { 512, 512, 1 } // Size};

[texture replaceRegion:region mipmapLevel:0 withBytes:imageData bytesPerRow:bytesPerRow];

Page 211: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Loading Image Data

NSUInteger bytesPerRow = 4 * image.width;

MTLRegion region ={ { 0, 0, 0 }, // Origin { 512, 512, 1 } // Size};

[texture replaceRegion:region mipmapLevel:0 withBytes:imageData bytesPerRow:bytesPerRow];

Page 212: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Loading Image Data

NSUInteger bytesPerRow = 4 * image.width;

MTLRegion region ={ { 0, 0, 0 }, // Origin { 512, 512, 1 } // Size};

[texture replaceRegion:region mipmapLevel:0 withBytes:imageData bytesPerRow:bytesPerRow];

Page 213: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Texture Differences

Sampler state never part of texture • Wrap modes, filtering, min/max LOD

Page 214: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Texture Differences

Sampler state never part of texture • Wrap modes, filtering, min/max LOD

Texture image data not flipped • OpenGL uses bottom-left origin, Metal uses top-left origin

Texture Differences

Page 215: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Texture Differences

Sampler state never part of texture • Wrap modes, filtering, min/max LOD

Texture image data not flipped • OpenGL uses bottom-left origin, Metal uses top-left origin

Metal does not perform format conversion

Page 216: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Textures

Buffers

Shaders

Page 217: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Textures

Buffers

Shaders

Page 218: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Metal uses buffers for vertices, indices, and all uniform data

OpenGL's vertex, element, and uniform buffers are similar • Easier to port apps that have adopted these

Buffers

Page 219: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Buffers

id<MTLBuffer> buffer = [device newBufferWithLength:bufferDataByteSize options:MTLResourceStorageModeShared];

struct MyUniforms *uniforms = (struct MyUniforms*) buffer.contents;

uniforms->modelViewProjection = modelViewProjection; uniforms->sunPosition = sunPosition;

Page 220: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Buffers

id<MTLBuffer> buffer = [device newBufferWithLength:bufferDataByteSize options:MTLResourceStorageModeShared];

struct MyUniforms *uniforms = (struct MyUniforms*) buffer.contents;

uniforms->modelViewProjection = modelViewProjection; uniforms->sunPosition = sunPosition;

Page 221: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Buffers

id<MTLBuffer> buffer = [device newBufferWithLength:bufferDataByteSize

options:MTLResourceStorageModeShared];

struct MyUniforms *uniforms = (struct MyUniforms*) buffer.contents;

uniforms->modelViewProjection = modelViewProjection; uniforms->sunPosition = sunPosition;

Page 222: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Notes About Buffer Data Pay attention to alignment

Type Alignment

float3, int3, uint3 16 bytes

float3x3, float4x3 16 bytes

half3, short3, ushore3, 8 bytes

half3x3, half4x3 8 bytes

structures 4 bytes

Page 223: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Notes About Buffer Data Pay attention to alignment

Type Alignment

float3, int3, uint3 16 bytes

float3x3, float4x3 16 bytes

half3, short3, ushore3, 8 bytes

half3x3, half4x3 8 bytes

structures 4 bytes

Page 224: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Notes About Buffer Data Pay attention to alignment

Type Alignment

float3, int3, uint3 16 bytes

float3x3, float4x3 16 bytes

half3, short3, ushore3, 8 bytes

half3x3, half4x3 8 bytes

structures 4 bytes

Page 225: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Notes About Buffer Data SIMD and packed types

SIMD libraries vector and matrix types follow same rules as Metal shaders

Special packed vector types available to shaders • packed_float3 consumes 12 bytes • packed_half3 consumes 6 bytes

Cannot directly operate on packed types • Cast to non-packed type required

Page 226: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes for Porting

Use most convient storage modes • Easier access to data

Page 227: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes for Porting

Use most convient storage modes • Easier access to data

On iOS • Create all textures and buffers with MTLStorageModeShared

Page 228: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Storage Modes for Porting

Use most convient storage modes • Easier access to data

On iOS • Create all textures and buffers with MTLStorageModeShared

On macOS • Create all textures with MTLStorageModeManaged • Make judicious use of MTLStorageModeShared for buffers - Separate GPU only data from CPU accessible data

Page 229: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Texture Loading • Textures from KTX, PVR, JPG, PNG, TIFF, etc.

Model Loading • Vertex buffers from USD, OBJ, Alembic, etc.

MetalKit Texture and buffer utilities

Page 230: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Textures

Buffers

Pipelines

Page 231: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Textures

Buffers

Pipelines

Page 232: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Device

Render Pipeline Descriptor

Page 233: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Device

Render Pipeline Descriptor

Vertex Shader

Fragment Shader

Page 234: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Device

Render Pipeline Descriptor

Vertex Layout

Vertex Shader

Fragment Shader

Page 235: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Device

Render Pipeline Descriptor

Vertex Layout

Render Target Pixel Formats

Blend State

Vertex Shader

Fragment Shader

Page 236: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Device

Render Pipeline Descriptor

Vertex Layout

Render Target Pixel Formats

Blend State

Vertex Shader

Fragment Shader

Page 237: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Device

Render StatePipeline Object

Render Pipeline Descriptor

Vertex Layout

Render Target Pixel Formats

Blend State

Vertex Shader

Fragment Shader

Page 238: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Device

Render StatePipeline Object

Page 239: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Render Pipeline Objects

id<MTLLibrary> defaultLibrary = [device newDefaultLibrary];

id<MTLFunction> vertexFunction = [defaultLibrary newFunctionWithName:@"vertexShader"]; id<MTLFunction> fragmentFunction = [defaultLibrary newFunctionWithName:@"fragmentShader"];

MTLRenderPipelineDescriptor *pipelineStateDescriptor = [MTLRenderPipelineDescriptor new]; pipelineStateDescriptor.vertexFunction = vertexFunction; pipelineStateDescriptor.fragmentFunction = fragmentFunction; pipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm;

id<MTLRenderPipelineState> pipelineState; pipelineState = [device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:nil];

Page 240: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Render Pipeline Objects

id<MTLLibrary> defaultLibrary = [device newDefaultLibrary];

id<MTLFunction> vertexFunction = [defaultLibrary newFunctionWithName:@"vertexShader"]; id<MTLFunction> fragmentFunction = [defaultLibrary newFunctionWithName:@"fragmentShader"];

MTLRenderPipelineDescriptor *pipelineStateDescriptor = [MTLRenderPipelineDescriptor new]; pipelineStateDescriptor.vertexFunction = vertexFunction; pipelineStateDescriptor.fragmentFunction = fragmentFunction; pipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm;

id<MTLRenderPipelineState> pipelineState; pipelineState = [device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:nil];

Page 241: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Render Pipeline Objects

id<MTLLibrary> defaultLibrary = [device newDefaultLibrary];

id<MTLFunction> vertexFunction = [defaultLibrary newFunctionWithName:@"vertexShader"]; id<MTLFunction> fragmentFunction = [defaultLibrary newFunctionWithName:@"fragmentShader"];

MTLRenderPipelineDescriptor *pipelineStateDescriptor = [MTLRenderPipelineDescriptor new]; pipelineStateDescriptor.vertexFunction = vertexFunction; pipelineStateDescriptor.fragmentFunction = fragmentFunction; pipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm;

id<MTLRenderPipelineState> pipelineState; pipelineState = [device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:nil];

Page 242: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Render Pipeline Objects

id<MTLLibrary> defaultLibrary = [device newDefaultLibrary];

id<MTLFunction> vertexFunction = [defaultLibrary newFunctionWithName:@"vertexShader"]; id<MTLFunction> fragmentFunction = [defaultLibrary newFunctionWithName:@"fragmentShader"];

MTLRenderPipelineDescriptor *pipelineStateDescriptor = [MTLRenderPipelineDescriptor new]; pipelineStateDescriptor.vertexFunction = vertexFunction; pipelineStateDescriptor.fragmentFunction = fragmentFunction; pipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm;

id<MTLRenderPipelineState> pipelineState; pipelineState = [device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:nil];

Page 243: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Render Pipeline Objects

id<MTLLibrary> defaultLibrary = [device newDefaultLibrary];

id<MTLFunction> vertexFunction = [defaultLibrary newFunctionWithName:@"vertexShader"]; id<MTLFunction> fragmentFunction = [defaultLibrary newFunctionWithName:@"fragmentShader"];

MTLRenderPipelineDescriptor *pipelineStateDescriptor = [MTLRenderPipelineDescriptor new]; pipelineStateDescriptor.vertexFunction = vertexFunction; pipelineStateDescriptor.fragmentFunction = fragmentFunction; pipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm;

id<MTLRenderPipelineState> pipelineState; pipelineState = [device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:nil];

Page 244: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Creating Render Pipeline Objects

id<MTLLibrary> defaultLibrary = [device newDefaultLibrary];

id<MTLFunction> vertexFunction = [defaultLibrary newFunctionWithName:@"vertexShader"]; id<MTLFunction> fragmentFunction = [defaultLibrary newFunctionWithName:@"fragmentShader"];

MTLRenderPipelineDescriptor *pipelineStateDescriptor = [MTLRenderPipelineDescriptor new]; pipelineStateDescriptor.vertexFunction = vertexFunction; pipelineStateDescriptor.fragmentFunction = fragmentFunction; pipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm;

id<MTLRenderPipelineState> pipelineState; pipelineState = [device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:nil];

Page 245: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Vertex Shader

Fragment Shader

Vertex Shader

Blend State

Fragment Shader

Vertex Layout

Render Target Pixel Formats

Pipeline Differences OpenGL Program Objects

OpenGL

Metal

Page 246: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Pipeline Building Create at intitlization

Full compilation key advantage of state grouping

Choose a canonical vertex layout for meshes

Use a limited set of render target formats

Page 247: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

!

Store pipeline state objects in a dictionary using descriptor as key

Construct descriptor at draw time with current state

Retrieve existing pipeline from dictionary OR build new pipeline

Pipeline Building Lazy creation at draw time

Page 248: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Create Render Objects at Initialization

Object creation expensive

Page 249: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Create Render Objects at Initialization

Object creation expensive • Pipelines require backend compilation

Page 250: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Create Render Objects at Initialization

Object creation expensive • Pipelines require backend compilation • Buffers and textures need allocations

Page 251: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Create Render Objects at Initialization

Object creation expensive • Pipelines require backend compilation • Buffers and textures need allocations

Once created, much faster usage during rendering

Page 252: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Sukanya Sudugu, GPU Software Engineer

•Porting the Render Loop

Page 253: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 254: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 255: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 256: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 257: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 258: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Display

Textures

Buffers

Render Objects

Pipelines

Command Buffer

Command Encoder

GPU

Metal API

Application Renderer

Command Queue Device

Page 259: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Display

Textures

Buffers

Render Objects

Pipelines

Command Encoder

GPU

Metal API

Application Renderer

Command Queue Device

Command Buffer

Page 260: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Explicit control over command buffer submission

Start with one command buffer per frame

Optionally split a frame into multiple command buffers to • Submit early and get the GPU started • Build commands on multiple threads

Page 261: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Explicit control over command buffer submission

Start with one command buffer per frame

Optionally split a frame into multiple command buffers to • Submit early and get the GPU started • Build commands on multiple threads

Completion handler invoked when execution is finished

Page 262: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Render Loop

// Obtaining a command buffer at the beginning of each frame id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];

// Encoding commands ...

// Commit the command buffer to the GPU for execution [commandBuffer commit];

Page 263: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Render Loop

// Obtaining a command buffer at the beginning of each frame id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];

// Encoding commands ...

// Commit the command buffer to the GPU for execution [commandBuffer commit];

Page 264: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Render Loop

// Obtaining a command buffer at the beginning of each frame id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];

// Encoding commands ...

// Commit the command buffer to the GPU for execution [commandBuffer commit];

Page 265: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Render Loop

// Obtaining a command buffer at the beginning of each frame id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];

// Encoding commands ...

// Commit the command buffer to the GPU for execution [commandBuffer commit];

Page 266: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Render Loop

// Obtaining a command buffer at the beginning of each frame id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];

// Encoding commands ...

// Commit the command buffer to the GPU for execution [commandBuffer commit];

// Wait until the GPU has finished execution [commandBuffer waitUntilCompleted];

Page 267: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Render Loop

// Obtaining a command buffer at the beginning of each frame id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];

// Encoding commands ...

// Commit the command buffer to the GPU for execution [commandBuffer commit];

Page 268: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Render Loop

// Obtaining a command buffer at the beginning of each frame id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];

// Encoding commands ...

// Commit the command buffer to the GPU for execution [commandBuffer commit];

// Add a completion hander to tell me when the GPU is done [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer) { // GPU is done with my buffer! ... }];

Page 269: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 270: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Resource Updates

Resources are explicitly managed in Metal

No implicit synchronization like OpenGL

Allows for fine gained synchronization

Application has complete control

Best model dependent on usage • Triple buffering recommended

Shared Resources

Dynamic Vertices

Dynamic UniformsGPU

ReadsCPU

Writes

Page 271: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Resource Updates Without synchronization

Time

CPU

Buffer

GPU

Read

Write

Page 272: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Resource Updates Without synchronization

Time

CPU

Buffer

GPU

Read

Write

Frame 1 Data

Frame 1

Page 273: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Resource Updates Without synchronization

Time

CPU

Buffer

GPU

Read

Write

Frame 1

Frame 1 Data

Frame 1

Page 274: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Resource Updates Without synchronization

Time

CPU

Buffer

GPU

Read

Write

Frame 2 DataFrame 1 Data

Frame 1 Wait

Frame 1

Frame 2

Page 275: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Resource Updates Without synchronization

Time

CPU

Buffer

GPU

Read

Write

Frame 2 DataFrame 1 Data

Frame 1 Wait

Frame 1

Frame 2

Page 276: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Temporary Solution Synchronous wait after every frame

Time

CPU

Buffer

GPU

Read

Write

Frame 2 Data

Frame 1

Frame 1

Wait Frame 2

Idle

Page 277: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Temporary Solution Synchronous wait after every frame

Time

CPU

Buffer

GPU

Read

Write

Frame 2 Data

Frame 1

Frame 1

Wait Frame 2

Idle

Page 278: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Triple Buffering Shared buffer pool

Time

Buffer

Read

Write

Frame 1 Data

GPU Frame 1

CPU Frame 1

Page 279: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Triple Buffering Shared buffer pool

Time

Buffer

Read

Write

Frame 1 Data

Frame 3 DataFrame 2 Data

GPU Frame 1

Frame 2 Frame 3CPU Frame 1

Page 280: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Triple Buffering Shared buffer pool

Time

Buffer

Read

Write

Frame 1 Data

Frame 3 DataFrame 2 Data

GPU Frame 1

Frame 2 Frame 3CPU Frame 1

Page 281: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Triple Buffering Shared buffer pool

Time

Buffer

Read

Write

Frame 1 Data

Frame 3 DataFrame 2 Data

GPU Frame 1

Frame 2 Frame 3CPU Frame 1

Page 282: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Triple Buffering Shared buffer pool

Time

Buffer

Read

Write

GPU

Frame 2 Frame 3CPU Frame 1

Frame 1 Data

Frame 3 DataFrame 2 Data

Frame 1

Page 283: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Triple Buffering Shared buffer pool

Time

Buffer

Read

Write

GPU

Frame 2 Frame 3CPU Frame 1

Frame 1 Data

Frame 3 DataFrame 2 Data

Completion Handler

Frame 1

Page 284: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Triple Buffering Shared buffer pool

Time

Buffer

Read

Write

GPU

Frame 2 Frame 3CPU Frame 1

Frame 4 Data

Frame 3 DataFrame 2 Data

Frame 4

Completion Handler

Frame 1

Page 285: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Triple Buffering Shared buffer pool

Time

Buffer

Read

Write

GPU

Frame 2 Frame 3CPU Frame 1

Frame 4 Data

Frame 3 DataFrame 2 Data

Frame 4

Completion Handler

Frame 1 Frame 2

Page 286: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Triple Buffering Shared buffer pool

Time

Buffer

Read

Write

GPU

Frame 2 Frame 3CPU Frame 1

Frame 4 DataFrame 4 Data

Frame 3 DataFrame 5 Data

Frame 4

Completion Handler

Frame 1

Frame 5

Frame 2 Frame 3

Page 287: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Triple Buffering Implementation

// Create FIFO queue of three dynamic data uniform buffers id <MTLBuffer> myUniformBuffers[3];

// Create a semaphore that gets signaled at each frame boundary. // The GPU signals the semaphore once it completes a frame’s work, // allowing CPU To work on a new frame dispatch_semaphore_t frameBoundarySemaphore = dispatch_semaphore_create(3);

// Current frame Index NSUInteger currentUniformIndex = 0;

Page 288: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Triple Buffering Implementation

// Create FIFO queue of three dynamic data uniform buffers id <MTLBuffer> myUniformBuffers[3];

// Create a semaphore that gets signaled at each frame boundary. // The GPU signals the semaphore once it completes a frame’s work, // allowing CPU To work on a new frame dispatch_semaphore_t frameBoundarySemaphore = dispatch_semaphore_create(3);

// Current frame Index NSUInteger currentUniformIndex = 0;

Page 289: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Triple Buffering Implementation

// Create FIFO queue of three dynamic data uniform buffers id <MTLBuffer> myUniformBuffers[3];

// Create a semaphore that gets signaled at each frame boundary. // The GPU signals the semaphore once it completes a frame’s work, // allowing CPU To work on a new frame dispatch_semaphore_t frameBoundarySemaphore = dispatch_semaphore_create(3);

// Current frame Index NSUInteger currentUniformIndex = 0;

Page 290: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Wait until inflight frame is completed dispatch_semaphore_wait(frameBoundarySemaphore, DISPATCH_TIME_FOREVER);

// Grab current frame and update its buffer currentUniformIndex = (currentUniformIndex + 1) % 3; [self updateUniformResource: myUniformBuffers[currentUniformIndex]];

// Encode commands and bind uniform buffer for GPU access

// Schedule frame completion handler [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer) { // GPU work is complete. Signal the Semaphore to start CPU work dispatch_semaphore_signal(frameBoundarySemaphore); }];

// Finalize and commit frame to GPU [commandBuffer commit];

Page 291: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Wait until inflight frame is completed dispatch_semaphore_wait(frameBoundarySemaphore, DISPATCH_TIME_FOREVER);

// Grab current frame and update its buffer currentUniformIndex = (currentUniformIndex + 1) % 3; [self updateUniformResource: myUniformBuffers[currentUniformIndex]];

// Encode commands and bind uniform buffer for GPU access

// Schedule frame completion handler [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer) { // GPU work is complete. Signal the Semaphore to start CPU work dispatch_semaphore_signal(frameBoundarySemaphore); }];

// Finalize and commit frame to GPU [commandBuffer commit];

Page 292: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Wait until inflight frame is completed dispatch_semaphore_wait(frameBoundarySemaphore, DISPATCH_TIME_FOREVER);

// Grab current frame and update its buffer currentUniformIndex = (currentUniformIndex + 1) % 3; [self updateUniformResource: myUniformBuffers[currentUniformIndex]];

// Encode commands and bind uniform buffer for GPU access

// Schedule frame completion handler [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer) { // GPU work is complete. Signal the Semaphore to start CPU work dispatch_semaphore_signal(frameBoundarySemaphore); }];

// Finalize and commit frame to GPU [commandBuffer commit];

Page 293: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Wait until inflight frame is completed dispatch_semaphore_wait(frameBoundarySemaphore, DISPATCH_TIME_FOREVER);

// Grab current frame and update its buffer currentUniformIndex = (currentUniformIndex + 1) % 3; [self updateUniformResource: myUniformBuffers[currentUniformIndex]];

// Encode commands and bind uniform buffer for GPU access

// Schedule frame completion handler [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer) { // GPU work is complete. Signal the Semaphore to start CPU work dispatch_semaphore_signal(frameBoundarySemaphore); }];

// Finalize and commit frame to GPU [commandBuffer commit];

Page 294: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Wait until inflight frame is completed dispatch_semaphore_wait(frameBoundarySemaphore, DISPATCH_TIME_FOREVER);

// Grab current frame and update its buffer currentUniformIndex = (currentUniformIndex + 1) % 3; [self updateUniformResource: myUniformBuffers[currentUniformIndex]];

// Encode commands and bind uniform buffer for GPU access

// Schedule frame completion handler [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer) { // GPU work is complete. Signal the Semaphore to start CPU work dispatch_semaphore_signal(frameBoundarySemaphore); }];

// Finalize and commit frame to GPU [commandBuffer commit];

Page 295: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Wait until inflight frame is completed dispatch_semaphore_wait(frameBoundarySemaphore, DISPATCH_TIME_FOREVER);

// Grab current frame and update its buffer currentUniformIndex = (currentUniformIndex + 1) % 3; [self updateUniformResource: myUniformBuffers[currentUniformIndex]];

// Encode commands and bind uniform buffer for GPU access

// Schedule frame completion handler [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer) { // GPU work is complete. Signal the Semaphore to start CPU work dispatch_semaphore_signal(frameBoundarySemaphore); }];

// Finalize and commit frame to GPU [commandBuffer commit];

Page 296: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 297: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Display

Textures

Buffers

Render Objects

Pipelines

Command Buffer

Command Encoder

GPU

Metal API

Application Renderer

Command Queue Device

Page 298: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Display

Textures

Buffers

Render Objects

Pipelines

Command Buffer

Command Encoder

GPU

Metal API

Application Renderer

Command Queue Device

Page 299: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Pass Descriptor

Render Pass Descriptor

Color

Command BufferDepth

Stencil

Page 300: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Pass Descriptor

Render Pass Descriptor

Color

Command Buffer

Render CommandEncoder

Depth

Stencil

Page 301: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Metal Render Pass descriptor MTLRenderPassDescriptor * desc = [MTLRenderPassDescriptor new]; desc.colorAttachment[0].texture = myColorTexture; desc.depthAttachment.texture = myDepthTexture; id <MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor: desc];

Render Pass Setup

Page 302: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Metal Render Pass descriptor MTLRenderPassDescriptor * desc = [MTLRenderPassDescriptor new]; desc.colorAttachment[0].texture = myColorTexture; desc.depthAttachment.texture = myDepthTexture; id <MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor: desc];

Render Pass Setup

Page 303: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Pass Load and Store Actions

Render Pass Descriptor

Color

Command Buffer

Render CommandEncoder

Depth

Stencil

Load Action

Store Action

Page 304: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Pass Load and Store Actions

Color

Depth

Store ActionLoad Action Draw

Page 305: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Pass Load and Store Actions

Color

Depth

Clear

Clear

Store ActionLoad Action Draw

Page 306: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Pass Load and Store Actions

Color

Depth

Clear

Clear

Store ActionLoad Action Draw

Page 307: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Pass Load and Store Actions

Store Action

Store

Don’tcare

Color

Depth

Load Action Draw

Clear

Clear

Page 308: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Pass Load and Store Actions

// Color attachment Load and Store Actions MTLRenderPassDescriptor * desc = [MTLRenderPassDescriptor new]; desc.colorAttachment[0].texture = myColorTexture; desc.colorAttachment[0].loadAction = MTLLoadActionClear; desc.colorAttachment[0].clearColor = MTLClearColorMake(0.39f, 0.34f, 0.53f, 1.0f); desc.colorAttachment[0].storeAction = MTLStoreActionStore; id <MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor: desc];

Page 309: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Pass Load and Store Actions

// Color attachment Load and Store Actions MTLRenderPassDescriptor * desc = [MTLRenderPassDescriptor new]; desc.colorAttachment[0].texture = myColorTexture; desc.colorAttachment[0].loadAction = MTLLoadActionClear; desc.colorAttachment[0].clearColor = MTLClearColorMake(0.39f, 0.34f, 0.53f, 1.0f); desc.colorAttachment[0].storeAction = MTLStoreActionStore; id <MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor: desc];

Page 310: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Render Pass Setup

// Color attachment Load and Store Actions MTLRenderPassDescriptor * desc = [MTLRenderPassDescriptor new]; desc.colorAttachment[0].texture = myColorTexture; desc.colorAttachment[0].loadAction = MTLLoadActionClear; desc.colorAttachment[0].clearColor = MTLClearColorMake(0.39f, 0.34f, 0.53f, 1.0f); desc.colorAttachment[0].storeAction = MTLStoreActionStore; id <MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor: desc];

Page 311: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with OpenGL

glBindFramebuffer(GL_FRAMEBUFFER, myFramebuffer);

glUseProgram(myProgram);

glBindBuffer(GL_ARRAY_BUFFER, myVertexBuffer);

glBindBuffer(GL_UNIFORM_BUFFER, myUniforms);

glBindTexture(GL_TEXTURE_2D, myColorTexture);

glDrawArrays(GL_TRIANGLES, 0, numVertices);

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Page 312: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with OpenGL

glBindFramebuffer(GL_FRAMEBUFFER, myFramebuffer);

glUseProgram(myProgram);

glBindBuffer(GL_ARRAY_BUFFER, myVertexBuffer);

glBindBuffer(GL_UNIFORM_BUFFER, myUniforms);

glBindTexture(GL_TEXTURE_2D, myColorTexture);

glDrawArrays(GL_TRIANGLES, 0, numVertices);

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Page 313: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with OpenGL

glBindFramebuffer(GL_FRAMEBUFFER, myFramebuffer);

glUseProgram(myProgram);

glBindBuffer(GL_ARRAY_BUFFER, myVertexBuffer);

glBindBuffer(GL_UNIFORM_BUFFER, myUniforms);

glBindTexture(GL_TEXTURE_2D, myColorTexture);

glDrawArrays(GL_TRIANGLES, 0, numVertices);

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Page 314: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with OpenGL

glBindFramebuffer(GL_FRAMEBUFFER, myFramebuffer);

glUseProgram(myProgram);

glBindBuffer(GL_ARRAY_BUFFER, myVertexBuffer);

glBindBuffer(GL_UNIFORM_BUFFER, myUniforms);

glBindTexture(GL_TEXTURE_2D, myColorTexture);

glDrawArrays(GL_TRIANGLES, 0, numVertices);

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Page 315: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with OpenGL

glBindFramebuffer(GL_FRAMEBUFFER, myFramebuffer);

glUseProgram(myProgram);

glBindBuffer(GL_ARRAY_BUFFER, myVertexBuffer);

glBindBuffer(GL_UNIFORM_BUFFER, myUniforms);

glBindTexture(GL_TEXTURE_2D, myColorTexture);

glDrawArrays(GL_TRIANGLES, 0, numVertices);

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Page 316: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with OpenGL

glBindFramebuffer(GL_FRAMEBUFFER, myFramebuffer);

glUseProgram(myProgram);

glBindBuffer(GL_ARRAY_BUFFER, myVertexBuffer);

glBindBuffer(GL_UNIFORM_BUFFER, myUniforms);

glBindTexture(GL_TEXTURE_2D, myColorTexture);

glDrawArrays(GL_TRIANGLES, 0, numVertices);

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Page 317: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with OpenGL

glBindFramebuffer(GL_FRAMEBUFFER, myFramebuffer);

glUseProgram(myProgram);

glBindBuffer(GL_ARRAY_BUFFER, myVertexBuffer);

glBindBuffer(GL_UNIFORM_BUFFER, myUniforms);

glBindTexture(GL_TEXTURE_2D, myColorTexture);

glDrawArrays(GL_TRIANGLES, 0, numVertices);

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Page 318: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with Metal

encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];

[encoder setPipelineState:myPipeline];

[encoder setVertexBuffer:myVertexData offset:0 atIndex:0];

[encoder setVertexBuffer:myUniforms offset:0 atIndex:1]; [encoder setFragmentBuffer:myUniforms offset:0 atIndex:1];

[encoder setFragmentTexture:myColorTexture atIndex:0];

[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:numVertices];

[encoder endEncoding];

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Page 319: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with Metal

encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];

[encoder setPipelineState:myPipeline];

[encoder setVertexBuffer:myVertexData offset:0 atIndex:0];

[encoder setVertexBuffer:myUniforms offset:0 atIndex:1]; [encoder setFragmentBuffer:myUniforms offset:0 atIndex:1];

[encoder setFragmentTexture:myColorTexture atIndex:0];

[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:numVertices];

[encoder endEncoding];

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Page 320: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Rendering with Metal

encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];

[encoder setPipelineState:myPipeline];

[encoder setVertexBuffer:myVertexData offset:0 atIndex:0];

[encoder setVertexBuffer:myUniforms offset:0 atIndex:1]; [encoder setFragmentBuffer:myUniforms offset:0 atIndex:1];

[encoder setFragmentTexture:myColorTexture atIndex:0];

[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:numVertices];

[encoder endEncoding];

Page 321: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with Metal

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];

[encoder setPipelineState:myPipeline];

[encoder setVertexBuffer:myVertexData offset:0 atIndex:0];

[encoder setVertexBuffer:myUniforms offset:0 atIndex:1]; [encoder setFragmentBuffer:myUniforms offset:0 atIndex:1];

[encoder setFragmentTexture:myColorTexture atIndex:0];

[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:numVertices];

[encoder endEncoding];

Page 322: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Rendering with Metal

encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];

[encoder setPipelineState:myPipeline];

[encoder setVertexBuffer:myVertexData offset:0 atIndex:0];

[encoder setVertexBuffer:myUniforms offset:0 atIndex:1]; [encoder setFragmentBuffer:myUniforms offset:0 atIndex:1];

[encoder setFragmentTexture:myColorTexture atIndex:0];

[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:numVertices];

[encoder endEncoding];

Page 323: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Rendering with Metal

encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];

[encoder setPipelineState:myPipeline];

[encoder setVertexBuffer:myVertexData offset:0 atIndex:0];

[encoder setVertexBuffer:myUniforms offset:0 atIndex:1]; [encoder setFragmentBuffer:myUniforms offset:0 atIndex:1];

[encoder setFragmentTexture:myColorTexture atIndex:0];

[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:numVertices];

[encoder endEncoding];

Page 324: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Rendering with Metal

encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];

[encoder setPipelineState:myPipeline];

[encoder setVertexBuffer:myVertexData offset:0 atIndex:0];

[encoder setVertexBuffer:myUniforms offset:0 atIndex:1]; [encoder setFragmentBuffer:myUniforms offset:0 atIndex:1];

[encoder setFragmentTexture:myColorTexture atIndex:0];

[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:numVertices];

[encoder endEncoding];

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Page 325: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Shaders

Vertex Buffers

Textures

Render Targets

Draws

Uniforms

Rendering with Metal

encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];

[encoder setPipelineState:myPipeline];

[encoder setVertexBuffer:myVertexData offset:0 atIndex:0];

[encoder setVertexBuffer:myUniforms offset:0 atIndex:1]; [encoder setFragmentBuffer:myUniforms offset:0 atIndex:1];

[encoder setFragmentTexture:myColorTexture atIndex:0];

[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:numVertices];

[encoder endEncoding];

Page 326: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 327: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Display

Textures

Buffers

Render Objects

Pipelines

Command Buffer

Command Encoder

GPU

Metal API

Application Renderer

Command Queue Device

Page 328: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Display

Textures

Buffers

Render Objects

Pipelines

Command Buffer

Command Encoder

GPU

Metal API

Application Renderer

Command Queue Device

Page 329: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Drawables—Textures for on screen display

Each frame MTKView provides • Drawable texture • Render pass descriptor setup with the drawable

Render to drawables like any other texture

Present drawable when done rendering

Display Drawables and presentation

Page 330: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Render offscreen passes ...

// Acquire a render pass descriptor generated from the drawable’s texture MTLRenderPassDescriptor* renderPassDescriptor = view.currentRenderPassDescriptor;

// encode your on-screen render passes id <MTLRenderCommandEncoder> renderCommandEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; // Encode render commands ...

[renderCommandEncoder endEncoding];

// Register the drawable presentation [commandBuffer presentDrawable:view.currentDrawable]; [commandBuffer commit];

Page 331: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Render offscreen passes ...

// Acquire a render pass descriptor generated from the drawable’s texture MTLRenderPassDescriptor* renderPassDescriptor = view.currentRenderPassDescriptor;

// encode your on-screen render passes id <MTLRenderCommandEncoder> renderCommandEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; // Encode render commands ...

[renderCommandEncoder endEncoding];

// Register the drawable presentation [commandBuffer presentDrawable:view.currentDrawable]; [commandBuffer commit];

Page 332: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

// Render offscreen passes ...

// Acquire a render pass descriptor generated from the drawable’s texture MTLRenderPassDescriptor* renderPassDescriptor = view.currentRenderPassDescriptor;

// encode your on-screen render passes id <MTLRenderCommandEncoder> renderCommandEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; // Encode render commands ...

[renderCommandEncoder endEncoding];

// Register the drawable presentation [commandBuffer presentDrawable:view.currentDrawable]; [commandBuffer commit];

Page 333: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Command Buffers

Resource Updates

Render Encoders

Display

Build RenderInitialize

Devices and Queues

Render Objects

Shaders

Page 334: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Incrementally Porting

Create shared Metal/OpenGL textures using IOSurface or CVPixelBuffer • Render to texture on one API and read in the other

Can enable mixed Metal/OpenGL applications

Sample code available

Page 335: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Going Further Multithreading

Metal is designed to facilitate Multithreading • Consider multithreading if application is CPU bound • Encode multiple command buffers simultaneously • Split single render pass using MTLParalllelCommandEncoder

Page 336: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Going Further Staying on the GPU

Metal natively supports compute

Performance benefits • Reduces CPU utilization • Reduces GPU-CPU synchronization points • Free’s data bandwidth to the GPU

New algorithms possible • Particle systems, physics, object culling

Page 337: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

More Metal Features

Page 338: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Argument Buffers

Multi Viewport Rendering

More Metal Features

Resource Heaps

Framebuffer Fetch

Tile shaders

Layered Rendering

Memoryless Render Targets

Metal Performance Shaders

Indirect Command Buffers Programmable Sample Positions

Events

Raster Order Groups

Image Blocks

Tessellation

Indirect Dispatch

Typed Buffers

Resource Views

SIMD Group operators

Sharable Textures

Function Specialization Texture Arrays

Array of Samplers Wide Color

Compute

Page 339: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Developer Tools Debug and optimize your applications

Xcode contains an advanced set of GPU tools

Enable Metal's API validation layer • On by default when target run from Xcode

Page 340: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update
Page 341: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update
Page 342: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update
Page 343: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update
Page 344: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update
Page 345: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

Summary

OpenGL and OpenCL are deprecated

Now it’s time to adopt Metal

Incremental port is possible

Full suite of developer tools available

Page 346: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update

More Informationhttps://developer.apple.com/wwdc18/604

OpenGL to Metal Porting Lab Technology Lab 5 Wednesday 12:00PM

Metal Shader Debugging and Profiling Hall 3 Thursday 3:00PM

Metal Game Performance Optimization Hall 1 Friday 10:00AM

Page 347: Metal for OpenGL Developers - Apple Inc.€¦ · High-level Apple frameworks • SpriteKit, SceneKit, and Core Image Third-party engines • Unity, Unreal, Lumberyard, etc. • Update