SlideShare a Scribd company logo
1 of 21
Georgi Nikolov 11.05.2023
Drawing with Metal
Insights after years of web development with WebGL
• From Sofia, Bulgaria
• Living in Berlin for about 5 years now
• Working as a freelance developer
• Background is traditional web development
• True passion is computer graphics, animation and experimental UIs
What is Metal?
• Low level access to the GPU on Apple Platforms
• Graphics rendering
• Parallel computing
• Underlying framework for all of Apple graphics and game APIs
• SpriteKit
• CoreGraphics
• CoreAnimation
• More and more games for Apple platforms support it
What is Metal?
• Part of the modern pipeline based rendering APIs (in fact the first one)
• Metal
• Vulkan
• DirectX 12
• WebGPU
• As low level as you can get on Apple hardware, extremely efficient
• Still relatively easy to pick up and be productive with
• Easily integrable into SpriteKit, SceneKit, UIKit, SwiftUI
What is Metal?
• Easily integrable with Model I/O
• Importing and exporting 3D assets
• Loading model animations, textures, etc
• Has C++ interface metal-cpp
• Different Apple GPU families have different capabilities
• Need to check at runtime if a given shiny feature is present
• If not, build a workaround for it
Anatomy of a Metal app
1. Create a CAMetalLayer to render its pixel contents programmatically via Metal
2. Get a reference to a MTLDevice that is a direct link to the device GPU
3. Create a vertex buffer holding the data you want to render
struct Vertex {
float2 position;
float3 colorRGB;
};
let vertices: [Vertex] = [
Vertex(position: float2(-0.5, 0.5), colorRGB: float3(1, 0, 0)),
Vertex(position: float2(0.5, 0.5), colorRGB: float3(0, 1, 0)),
Vertex(position: float2(0, -0.5), colorRGB: float3(0, 0, 1)),
]
Anatomy of a Metal app
4. Write your shaders to be ran on the GPU
1. Vertex shader to process each vertex in parallel
2. Fragment shader to process each pixel in parallel
3. Written as small programs in Metal Shading Language
1. Based on C++14
5. Compile your shaders and rendering settings into MTLRenderPipelineState
6. Set your viewport size
7. Set your newly created MTLRenderPipelineState as active
Anatomy of a Metal app
8. Send argument data to your vertex shader on the GPU
9. Submit a drawing command to the GPU
1. Primitive mode - point, lines, triangles
2. How many vertices to render
Differences with WebGL
1. Significantly faster
1. Made for Apple hardware
1. Offline resource creation and compilation
2. System memory shared between CPU and GPU
3. Faster resource loading
2. Lower level
3. Compute / tessellation shaders, ray tracing, MetalFX upscaling
4. Incredibly better tooling and debugging experience in Xcode
Similarities with WebGL
1. The maths
2. GPUs excel at 3 things, regardless of API used:
1. Floating point math
2. Primitive rasterisation
3. Textures blending
3. Apple devices use Metal behind the scenes to render WebGL
App architecture
1. Very thin SwiftUI layer
1. Compromised mostly of text and buttons
2. Metal view
1. Drawing the demo graphics and running the animations
2. Capturing user input such as taps and dragging
These two layers stay synced using published properties via Combine
ObservableObject which acts as aa central dispatcher
App architecture
class Options: ObservableObject {
@Published var activeProjectName: String = WelcomeScreen.SCREEN_NAME
}
Point light shadows demo
Cube shadows with depth cubemaps in a single draw via Metal layer selection
https://learnopengl.com/
Point light shadows demo
Different spheres, same underlying shaders: Metal function constants
constant bool is_sphere_back_side [[function_constant(0)]];
constant bool is_shaded_and_shadowed [[function_constant(1)]];
constant bool is_cut_off_alpha [[function_constant(2)]];
fragment float4 fragment_main() {
// ...
if (is_cut_off_alpha) {
float a = computeOpacity(in.uv);
if (a < 0.5) {
discard_fragment();
}
}
}
Xcode tooling & debugging
1. Incredibly nice Metal Debugger
1. Geometry viewer
2. Shader debugger
3. Render frame capture
4. GPU timeline
2. Texture creation
1. Different compression formats just work
2. Offline mips generation
Swift vs JS
1. More expressive syntax with more constructs
2. Statically typed
3. Proper OOP as opposed to prototypical inheritance
4. Protocols and extensions
References and readings
georgi-nikolov.com
metal-sketch-dojo.pptx

More Related Content

Similar to metal-sketch-dojo.pptx

GPU Programming: Chicago CocoaConf 2015
GPU Programming: Chicago CocoaConf 2015GPU Programming: Chicago CocoaConf 2015
GPU Programming: Chicago CocoaConf 2015Janie Clayton
 
Gpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and MetalGpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and MetalJanie Clayton
 
Lets have a look at Apple's Metal Framework
Lets have a look at Apple's Metal FrameworkLets have a look at Apple's Metal Framework
Lets have a look at Apple's Metal FrameworkLINE Corporation
 
Getting started with open gl es 2
Getting started with open gl es 2Getting started with open gl es 2
Getting started with open gl es 2matthewgalaviz
 
The next generation of GPU APIs for Game Engines
The next generation of GPU APIs for Game EnginesThe next generation of GPU APIs for Game Engines
The next generation of GPU APIs for Game EnginesPooya Eimandar
 
JIT Spraying Never Dies - Bypass CFG By Leveraging WARP Shader JIT Spraying.pdf
JIT Spraying Never Dies - Bypass CFG By Leveraging WARP Shader JIT Spraying.pdfJIT Spraying Never Dies - Bypass CFG By Leveraging WARP Shader JIT Spraying.pdf
JIT Spraying Never Dies - Bypass CFG By Leveraging WARP Shader JIT Spraying.pdfSamiraKids
 
Gdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_glGdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_glchangehee lee
 
Leaving Flatland: Getting Started with WebGL- SXSW 2012
Leaving Flatland: Getting Started with WebGL- SXSW 2012Leaving Flatland: Getting Started with WebGL- SXSW 2012
Leaving Flatland: Getting Started with WebGL- SXSW 2012philogb
 
SIGGRAPH Asia 2008 Modern OpenGL
SIGGRAPH Asia 2008 Modern OpenGLSIGGRAPH Asia 2008 Modern OpenGL
SIGGRAPH Asia 2008 Modern OpenGLMark Kilgard
 
Open frameworks 101_fitc
Open frameworks 101_fitcOpen frameworks 101_fitc
Open frameworks 101_fitcbenDesigning
 
Tools and practices for rapid application development
Tools and practices for rapid application developmentTools and practices for rapid application development
Tools and practices for rapid application developmentZoltán Váradi
 
Intro to WebGL and BabylonJS
Intro to WebGL and BabylonJSIntro to WebGL and BabylonJS
Intro to WebGL and BabylonJSDavid Voyles
 
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft..."Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...Dataconomy Media
 
Power of WebGL (FSTO 2014)
Power of WebGL (FSTO 2014)Power of WebGL (FSTO 2014)
Power of WebGL (FSTO 2014)Verold
 
GTC 2009 OpenGL Barthold
GTC 2009 OpenGL BartholdGTC 2009 OpenGL Barthold
GTC 2009 OpenGL BartholdMark Kilgard
 
Advanced Graphics Workshop - GFX2011
Advanced Graphics Workshop - GFX2011Advanced Graphics Workshop - GFX2011
Advanced Graphics Workshop - GFX2011Prabindh Sundareson
 

Similar to metal-sketch-dojo.pptx (20)

GPU Programming: Chicago CocoaConf 2015
GPU Programming: Chicago CocoaConf 2015GPU Programming: Chicago CocoaConf 2015
GPU Programming: Chicago CocoaConf 2015
 
Gpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and MetalGpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and Metal
 
Android native gl
Android native glAndroid native gl
Android native gl
 
Lets have a look at Apple's Metal Framework
Lets have a look at Apple's Metal FrameworkLets have a look at Apple's Metal Framework
Lets have a look at Apple's Metal Framework
 
Getting started with open gl es 2
Getting started with open gl es 2Getting started with open gl es 2
Getting started with open gl es 2
 
The next generation of GPU APIs for Game Engines
The next generation of GPU APIs for Game EnginesThe next generation of GPU APIs for Game Engines
The next generation of GPU APIs for Game Engines
 
JIT Spraying Never Dies - Bypass CFG By Leveraging WARP Shader JIT Spraying.pdf
JIT Spraying Never Dies - Bypass CFG By Leveraging WARP Shader JIT Spraying.pdfJIT Spraying Never Dies - Bypass CFG By Leveraging WARP Shader JIT Spraying.pdf
JIT Spraying Never Dies - Bypass CFG By Leveraging WARP Shader JIT Spraying.pdf
 
Gdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_glGdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_gl
 
Data structures graphics library in computer graphics.
Data structures  graphics library in computer graphics.Data structures  graphics library in computer graphics.
Data structures graphics library in computer graphics.
 
Leaving Flatland: Getting Started with WebGL- SXSW 2012
Leaving Flatland: Getting Started with WebGL- SXSW 2012Leaving Flatland: Getting Started with WebGL- SXSW 2012
Leaving Flatland: Getting Started with WebGL- SXSW 2012
 
SIGGRAPH Asia 2008 Modern OpenGL
SIGGRAPH Asia 2008 Modern OpenGLSIGGRAPH Asia 2008 Modern OpenGL
SIGGRAPH Asia 2008 Modern OpenGL
 
Net core
Net coreNet core
Net core
 
Open frameworks 101_fitc
Open frameworks 101_fitcOpen frameworks 101_fitc
Open frameworks 101_fitc
 
Js lovers
Js loversJs lovers
Js lovers
 
Tools and practices for rapid application development
Tools and practices for rapid application developmentTools and practices for rapid application development
Tools and practices for rapid application development
 
Intro to WebGL and BabylonJS
Intro to WebGL and BabylonJSIntro to WebGL and BabylonJS
Intro to WebGL and BabylonJS
 
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft..."Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...
 
Power of WebGL (FSTO 2014)
Power of WebGL (FSTO 2014)Power of WebGL (FSTO 2014)
Power of WebGL (FSTO 2014)
 
GTC 2009 OpenGL Barthold
GTC 2009 OpenGL BartholdGTC 2009 OpenGL Barthold
GTC 2009 OpenGL Barthold
 
Advanced Graphics Workshop - GFX2011
Advanced Graphics Workshop - GFX2011Advanced Graphics Workshop - GFX2011
Advanced Graphics Workshop - GFX2011
 

Recently uploaded

Rapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and InsightsRapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and Insightsrapidoform
 
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxFrom Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxNeo4j
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Maxim Salnikov
 
Novo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNovo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNeo4j
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Henry Schreiner
 
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckMarc Lester
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConNatan Silnitsky
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationElement34
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio, Inc.
 
Test Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfTest Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfkalichargn70th171
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMarkus Moeller
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfSrushith Repakula
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Clinic
 
Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksJinanKordab
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaNeo4j
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Andreas Granig
 
Your Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | EvmuxYour Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | Evmuxevmux96
 
Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14VMware Tanzu
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)Roberto Bettazzoni
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...naitiksharma1124
 

Recently uploaded (20)

Rapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and InsightsRapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and Insights
 
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxFrom Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Novo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNovo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMs
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined Deck
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test Automation
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
Test Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfTest Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdf
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with Links
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
 
Your Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | EvmuxYour Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | Evmux
 
Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 

metal-sketch-dojo.pptx

  • 1. Georgi Nikolov 11.05.2023 Drawing with Metal Insights after years of web development with WebGL
  • 2. • From Sofia, Bulgaria • Living in Berlin for about 5 years now • Working as a freelance developer • Background is traditional web development • True passion is computer graphics, animation and experimental UIs
  • 3.
  • 4. What is Metal? • Low level access to the GPU on Apple Platforms • Graphics rendering • Parallel computing • Underlying framework for all of Apple graphics and game APIs • SpriteKit • CoreGraphics • CoreAnimation • More and more games for Apple platforms support it
  • 5. What is Metal? • Part of the modern pipeline based rendering APIs (in fact the first one) • Metal • Vulkan • DirectX 12 • WebGPU • As low level as you can get on Apple hardware, extremely efficient • Still relatively easy to pick up and be productive with • Easily integrable into SpriteKit, SceneKit, UIKit, SwiftUI
  • 6. What is Metal? • Easily integrable with Model I/O • Importing and exporting 3D assets • Loading model animations, textures, etc • Has C++ interface metal-cpp • Different Apple GPU families have different capabilities • Need to check at runtime if a given shiny feature is present • If not, build a workaround for it
  • 7. Anatomy of a Metal app 1. Create a CAMetalLayer to render its pixel contents programmatically via Metal 2. Get a reference to a MTLDevice that is a direct link to the device GPU 3. Create a vertex buffer holding the data you want to render struct Vertex { float2 position; float3 colorRGB; }; let vertices: [Vertex] = [ Vertex(position: float2(-0.5, 0.5), colorRGB: float3(1, 0, 0)), Vertex(position: float2(0.5, 0.5), colorRGB: float3(0, 1, 0)), Vertex(position: float2(0, -0.5), colorRGB: float3(0, 0, 1)), ]
  • 8. Anatomy of a Metal app 4. Write your shaders to be ran on the GPU 1. Vertex shader to process each vertex in parallel 2. Fragment shader to process each pixel in parallel 3. Written as small programs in Metal Shading Language 1. Based on C++14 5. Compile your shaders and rendering settings into MTLRenderPipelineState 6. Set your viewport size 7. Set your newly created MTLRenderPipelineState as active
  • 9. Anatomy of a Metal app 8. Send argument data to your vertex shader on the GPU 9. Submit a drawing command to the GPU 1. Primitive mode - point, lines, triangles 2. How many vertices to render
  • 10. Differences with WebGL 1. Significantly faster 1. Made for Apple hardware 1. Offline resource creation and compilation 2. System memory shared between CPU and GPU 3. Faster resource loading 2. Lower level 3. Compute / tessellation shaders, ray tracing, MetalFX upscaling 4. Incredibly better tooling and debugging experience in Xcode
  • 11. Similarities with WebGL 1. The maths 2. GPUs excel at 3 things, regardless of API used: 1. Floating point math 2. Primitive rasterisation 3. Textures blending 3. Apple devices use Metal behind the scenes to render WebGL
  • 12.
  • 13. App architecture 1. Very thin SwiftUI layer 1. Compromised mostly of text and buttons 2. Metal view 1. Drawing the demo graphics and running the animations 2. Capturing user input such as taps and dragging These two layers stay synced using published properties via Combine ObservableObject which acts as aa central dispatcher
  • 14. App architecture class Options: ObservableObject { @Published var activeProjectName: String = WelcomeScreen.SCREEN_NAME }
  • 15. Point light shadows demo Cube shadows with depth cubemaps in a single draw via Metal layer selection https://learnopengl.com/
  • 16. Point light shadows demo Different spheres, same underlying shaders: Metal function constants constant bool is_sphere_back_side [[function_constant(0)]]; constant bool is_shaded_and_shadowed [[function_constant(1)]]; constant bool is_cut_off_alpha [[function_constant(2)]]; fragment float4 fragment_main() { // ... if (is_cut_off_alpha) { float a = computeOpacity(in.uv); if (a < 0.5) { discard_fragment(); } } }
  • 17. Xcode tooling & debugging 1. Incredibly nice Metal Debugger 1. Geometry viewer 2. Shader debugger 3. Render frame capture 4. GPU timeline 2. Texture creation 1. Different compression formats just work 2. Offline mips generation
  • 18. Swift vs JS 1. More expressive syntax with more constructs 2. Statically typed 3. Proper OOP as opposed to prototypical inheritance 4. Protocols and extensions