SlideShare a Scribd company logo
Lets have a look at Apple's Metal Framework
DEVELOPER
MEETUP
LINE
#31
Frederik Vogel
LINE Fukuoka 株式会社
iOS Engineer
Freddy
Why this topic?
• I am curious about Metal
• What can I do with Metal?
• Is is difficult to use?
• Can i integrated it in my Project?
• I want to use Swift!!
• Not many resources
• Hope to meet other people with same interest ?
Motivation
Let's have a look at Apple's Metal Framework
• History
• Metal concepts
• Render pipeline
• Compute pipeline
• Shader
• Demo
• Other possibilities
Agenda
History
Lets have a look at Apple's Metal Framework
Lets have a look at Apple's Metal Framework
https://en.wikipedia.org/wiki/Nintendo_64
1996
https://nintendoeverything.com/donkey-kong-country-came-with-a-risky-investment-for-rare/
1994
https://steemit.com/gaming/@jcdetona/lhuyh-review-fire-emblem-awakening-for-3ds
🧐
https://steemit.com/gaming/@jcdetona/lhuyh-review-fire-emblem-awakening-for-3ds
😳
OpenGL
Where does it come from?
• 1981 Silicon Graphics (SGI) was founded
History
OpenGL
Where does it come from?
• 1981 Silicon Graphics (SGI) was founded
• 1992 opened and renamed IRIS GL -> OpenGL
History
OpenGL
Where does it come from?
• 1981 Silicon Graphics (SGI) was founded
• 1992 opened and renamed IRIS GL API -> OpenGL
• 1996 Quake used OpenGL
History
John Carmack, id Software, now Oculus VR
https://www.neogaf.com/threads/techspot-the-history-of-the-modern-graphics-processor.531219/
1996, Quake
http://www.vgamuseum.info/index.php/news/item/1-3dfx-voodoo-1
OpenGL
Where does it come from?
• 1981 Silicon Graphics (SGI) was founded
• 1992 opened and renamed IRIS GL API -> OpenGL
• 1996 Quake used OpenGL
• 3D Graphic cards became popular
History
OpenGL
Where does it come from?
• 1981 Silicon Graphics (SGI) was founded
• 1992 opened and renamed IRIS GL API -> OpenGL
• 1996 Quake used OpenGL
• 3D Graphic cards became popular
• 2003 OpenGL ES was released, subset of OpenGL
History
OpenGL
Where does it come from?
• 1981 Silicon Graphics (SGI) was founded
• 1992 opened and renamed IRIS GL API -> OpenGL
• 1996 Quake used OpenGL
• 3D Graphic cards became popular
• 2003 OpenGL ES was released, subset of OpenGL
• 2007 iPhone released, supported OpenGL ES
History
OpenGL
Where does it come from?
• 1981 Silicon Graphics (SGI) was founded
• 1992 opened and renamed IRIS GL API -> OpenGL
• 1996 Quake used OpenGL
• 3D Graphic cards became popular
• 2003 OpenGL ES was released, subset of OpenGL
• 2007 iPhone released, supported OpenGL ES
• 2010 iPhone supports OpenGL ES 2.0 + Shader
History
OpenGL
Where does it come from?
• 1981 Silicon Graphics (SGI) was founded
• 1992 opened and renamed IRIS GL API -> OpenGL
• 1996 Quake used OpenGL
• 3D Graphic cards became popular
• 2003 OpenGL ES was released, subset of OpenGL
• 2007 iPhone released, supported OpenGL ES
• 2010 iPhone supports OpenGL ES 2.0 + Shader
• 2014 Metal was released
History
OpenGL
Where does it come from?
• 1981 Silicon Graphics (SGI) was founded
• 1992 opened and renamed IRIS GL API -> OpenGL
• 1996 Quake used OpenGL
• 3D Graphic cards became popular
• 2003 OpenGL ES was released, subset of OpenGL
• 2007 iPhone released, supported OpenGL ES
• 2010 iPhone supports OpenGL ES 2.0 + Shader
• 2014 Metal was released
• 2017 Metal 2 was released
History
What is Metal?
• Provides access to the GPU
• Low-level access
• Low-overhead access
• Accelerated 3D graphics rendering
• Data-parallel computation
• Full control of GPU computation power
Metal
🤘
Why not OpenGL ES?
• For graphics programming
• Not for general-purpose programming
• Need to copy memory between CPU and GPU
• Very old, has its beginning in the 80’s
• Does not fit for Apple’s plan
OpenGL ES
Why Metal?
• For graphics programming
• For general-purpose programming
• CPU and GPU are on the same chip
• No Need to copy memory between CPU and GPU
• Optimized for the chip
• Developer has full control of buffer
• Queue work in preferred order
• Can delay buffer execution
• More control over the performance
Metal
?
Render Pipeline
Render pipeline
Vertex Function
• Runs once on every vertex
• Calculates position for every vertex
• Assemble position and other data
Vertex Shader
struct RasterizerData
{
float4 position [[position]];
float4 color;
};
vertex RasterizerData myVertexFunction( uint vid [[ vertex_id ]],
constant packed_float4* position [[ buffer(0) ]],
constant packed_float4* color [[ buffer(1) ]])
{
RasterizerData outVertex;
outVertex.position = position[vid];
outVertex.color = color[vid];
return outVertex;
};
Assemble vertices to primitive
• Groups vertices into primitive types
• Primitive Types: point, line, lineStrip, triangle, triangleStrip
• Needed for rasterization
Rasterization
• Rasterize primitive types
• Interpolates Position
• Interpolates other Data
Fragment Function
• Runs for every pixel on the primitive
• Calculates color for each pixel
Fragment Function
struct RasterizerData
{
float4 position [[position]];
float4 color;
};
fragment half4 myFragmentFunction(RasterizerData inFrag [[stage_in]])
{
return half4(inFrag.color);
};
Render pipeline
What belongs to Metal?
• Metal framework
• MetalKit framework
• Metal Performance Shaders framework (MPS)
• Metal shading language (MSL)
• Metal standard library
Metal World
Let’s build our render pipeline!
Concept
MTLDevice
• Represent the actual GPU
• Create command Queue
• Creates pipeline
• Creates Data Buffer
• Access to custom function library
What is it for?
MTLRenderPipelineState
• Represents rendering pipeline
• Access to custom function library
• Creates compute or render pipeline
What is it for?
MTLCommandQueue
• Created by the MetalDevice
• Creates CommandBuffers
• Queues and schedules CommandBuffers
What is it for?
MTLCommandBuffer
• Created every frame
• Creates CommandEncoders
• RenderCommandEncoder
• ParallelRenderCommandEncoder
• ComputeCommandEncoder
• BlitCommandEncoder
• Encodes to the specific GPU hardware
What is it for?
MTLCommandEncoder
• Encode commands to the specific GPU hardware
What is it for?
🤯
A lot of information!
Setup
Every frame
Demo
Let’s see the code!
MetalKit
MetalKit framework?
• Reduces the boilerplate code
• No need to setup RenderPassDescriptor
• No need to setup MetalLayer
• No need to setup Displaylink
• Provides MTKView
• Provides MTKTextureLoader
• Model Handling
Metal
UIKit + Metal = MetalKit
Demo
Let’s see the code again!
Compute Pipeline
Compute Pipeline
• Only one stage
• shader is marked with kernel keyword
• kernel reads and write directly from/to resource
• Must specify “thread” size
• Fine control performance with threadGroups
Metal
Demo
More code!
Resume
Resume
• A lot of information
• But not too difficult
• An own world
• Compute Pipeline very powerful
• Particles
• Apple says its good for AR, VR, Cryptography, Machine
learning, Physics and Finance
!
Intelligent Scissors
Finance, Cryptography…
Machine learning!?!
Bitcoin Trading Bot!! 😃
Further Sources
・developer.apple.com/documentation/metal
Apple Documentation
・flexmonkey.blogspot.co.uk
・github.com/FlexMonkey/ParticleLab
FlexMonkey Creative Coding in Swift
・by Janie Clayton
Metal Programming Guide: Tutorial and Reference via Swift

More Related Content

Lets have a look at Apple's Metal Framework