SlideShare a Scribd company logo
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 2015
Janie Clayton
 
Gpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and MetalGpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and Metal
Janie Clayton
 
Android native gl
Android native glAndroid native gl
Android native gl
Miguel Angel Alcalde Velado
 
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
LINE 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 2
matthewgalaviz
 
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
Pooya 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.pdf
SamiraKids
 
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
changehee lee
 
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.
Daroko blog(www.professionalbloggertricks.com)
 
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
philogb
 
SIGGRAPH Asia 2008 Modern OpenGL
SIGGRAPH Asia 2008 Modern OpenGLSIGGRAPH Asia 2008 Modern OpenGL
SIGGRAPH Asia 2008 Modern OpenGL
Mark Kilgard
 
Net core
Net coreNet core
Net core
Damir Dobric
 
Open frameworks 101_fitc
Open frameworks 101_fitcOpen frameworks 101_fitc
Open frameworks 101_fitc
benDesigning
 
Js lovers
Js loversJs lovers
Js lovers
Shivam Singh
 
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
Zoltán Váradi
 
Intro to WebGL and BabylonJS
Intro to WebGL and BabylonJSIntro to WebGL and BabylonJS
Intro to WebGL and BabylonJS
David 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 Barthold
Mark Kilgard
 
Advanced Graphics Workshop - GFX2011
Advanced Graphics Workshop - GFX2011Advanced Graphics Workshop - GFX2011
Advanced Graphics Workshop - GFX2011
Prabindh 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

如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
NishanthaBulumulla1
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
mz5nrf0n
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
Karya Keeper
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
GohKiangHock
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 

Recently uploaded (20)

如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 

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