iOSDC 2017
Shuichi Tsutsumi
@shu223
WWDC 2014
Swift Metal !
WWDC2014 Metal
WWDC2014 Swift
!
!
Metal
"
"
Metal
※
✋
Metal
GPU
• Core ML
• Vision
• Core Image
• SceneKit
• SpriteKit
• MapKit
Metal
GPU
• Core ML
• Vision
• Core Image
• SceneKit
• SpriteKit
• MapKit
• Core Animation
• UIKit
143,000
Metal API
143,000
17,000,000
Metal API
Metal API
Metal
✓Metal
✓Metal
✓Metal
imageView.image = image
















CPU GPU
CPU
•
•
GPU
•
• CPU
• CPU
- 100%
• GPU GPU
-
-
-
-


1 60
• CPU
•
OK
1


1 60
• CPU
•
OK
GPU
1
: GPU GPU
GPU
GPU
GPU
Your app
???
OpenGL
OpenGL
• API
•
• Apple
Metal
• Apple
• Apple
• OpenGL 10
• OpenGL Metal
imageView.image = image
Metal API
func draw(in view: MTKView) {
guard let drawable = view.currentDrawable else {return}
guard let commandBuffer = commandQueue.makeCommandBuffer() else {fatalError()}


guard let blitEncoder = commandBuffer.makeBlitCommandEncoder() else {fatalError()}
let targetW = min(texture.width, drawable.texture.width)
let targetH = min(texture.height, drawable.texture.height)
blitEncoder.copy(from: texture,
sourceSlice: 0,
sourceLevel: 0,
sourceOrigin: MTLOrigin(x: 0, y: 0, z: 0),
sourceSize: MTLSizeMake(targetW, targetH, texture.depth),
to: drawable.texture,
destinationSlice: 0,
destinationLevel: 0,
destinationOrigin: MTLOrigin(x: 0, y: 0, z: 0))
blitEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
}
private let device = MTLCreateSystemDefaultDevice()!


private func setup() {
commandQueue = device.makeCommandQueue()
let textureLoader = MTKTextureLoader(device: device)
texture = try! textureLoader.newTexture(name: "highsierra", scaleFactor: view.contentScaleFactor, bundle: nil)
mtkView.device = device
mtkView.delegate = self
mtkView.colorPixelFormat = texture.pixelFormat
}
• MTLDevice
• MTLCommandQueue
• MTLCommandBuffer
• MTLCommandEncoder
• MTLTexture
• MTKView
• MTKTextureLoader
• etc…
$








MTLCommandBuffer
MTLTexture
MTLDevice
MTLCommandBuffer
MTLTexture
MTLDevice
MTLCommandEncoder
MTLCommandBuffer
MTLTexture
MTLDevice
MTLCommandQueue
MTLCommandEncoder
MTLCommandBuffer
MTLTexture
MTLDevice
MTLCommandQueue
MTKTextureLoader
MTLCommandEncoder
MTLDevice
let device = MTLCreateSystemDefaultDevice()!
※
MTLTexture, MTKTextureLoader
//
loader = MTKTextureLoader(device: device)
//
texture = try! loader.newTexture(

name: “hoge", 

scaleFactor: view.contentScaleFactor, 

bundle: nil)
※
MTLCommandQueue, MTLCommandBuffer
//
let commandBuffer = commandQueue.makeCommandBuffer()!
// …
//
commandBuffer.commit()
※
//
commandQueue = device.makeCommandQueue()
MTLCommandEncoder
•
MTLRenderCommandEncoder
MTLComputeCommandEncoder
MTLBlitCommandEncode
MTLCommandEncoder (MTLBlitCommandEncoder)
let blitEncoder = commandBuffer.makeBlitCommandEncoder()!
blitEncoder.copy(from: texture,
sourceSlice: 0,
sourceLevel: 0,
sourceOrigin: srcOrigin,
sourceSize: srcSize,
to: drawable.texture,
destinationSlice: 0,
destinationLevel: 0,
destinationOrigin:dstOrigin)
blitEncoder.endEncoding()
※
MTLCommandEncoder (MTLBlitCommandEncoder)
let blitEncoder = commandBuffer.makeBlitCommandEncoder()!
blitEncoder.copy(from: texture,
sourceSlice: 0,
sourceLevel: 0,
sourceOrigin: srcOrigin,
sourceSize: srcSize,
to: drawable.texture,
destinationSlice: 0,
destinationLevel: 0,
destinationOrigin:dstOrigin)
blitEncoder.endEncoding()
※
//
let commandBuffer = commandQueue.makeCommandBuffer()!
//
let blitEncoder = commandBuffer.makeBlitCommandEncoder()!
blitEncoder.copy(from: texture,
sourceSlice: 0,
sourceLevel: 0,
sourceOrigin: srcOrigin,
sourceSize: srcSize,
to: drawable.texture,
destinationSlice: 0,
destinationLevel: 0,
destinationOrigin:dstOrigin)
//
blitEncoder.endEncoding()
//
commandBuffer.commit()
※
private let device = MTLCreateSystemDefaultDevice()!
private func setup() {
commandQueue = device.makeCommandQueue()
let textureLoader = MTKTextureLoader(device: device)
texture = try! textureLoader.newTexture(name: "highsierra", scaleFactor: view.contentScaleFactor, bundle: nil)
mtkView.device = device
mtkView.delegate = self
mtkView.colorPixelFormat = texture.pixelFormat
}
func draw(in view: MTKView) {
guard let drawable = view.currentDrawable else {return}
guard let commandBuffer = commandQueue.makeCommandBuffer() else {fatalError()}


guard let blitEncoder = commandBuffer.makeBlitCommandEncoder() else {fatalError()}
let targetW = min(texture.width, drawable.texture.width)
let targetH = min(texture.height, drawable.texture.height)
blitEncoder.copy(from: texture,
sourceSlice: 0,
sourceLevel: 0,
sourceOrigin: MTLOrigin(x: 0, y: 0, z: 0),
sourceSize: MTLSizeMake(targetW, targetH, texture.depth),
to: drawable.texture,
destinationSlice: 0,
destinationLevel: 0,
destinationOrigin: MTLOrigin(x: 0, y: 0, z: 0))
blitEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
}
private let device = MTLCreateSystemDefaultDevice()!
private func setup() {
commandQueue = device.makeCommandQueue()
let textureLoader = MTKTextureLoader(device: device)
texture = try! textureLoader.newTexture(name: "highsierra", scaleFactor: view.contentScaleFactor, bundle: nil)
mtkView.device = device
mtkView.delegate = self
mtkView.colorPixelFormat = texture.pixelFormat
}
→
→ GPU
→ GPU
→
func draw(in view: MTKView) {
guard let drawable = view.currentDrawable else {return}
guard let commandBuffer = commandQueue.makeCommandBuffer() else {fatalError()}


guard let blitEncoder = commandBuffer.makeBlitCommandEncoder() else {fatalError()}
let targetW = min(texture.width, drawable.texture.width)
let targetH = min(texture.height, drawable.texture.height)
blitEncoder.copy(from: texture,
sourceSlice: 0,
sourceLevel: 0,
sourceOrigin: MTLOrigin(x: 0, y: 0, z: 0),
sourceSize: MTLSizeMake(targetW, targetH, texture.depth),
to: drawable.texture,
destinationSlice: 0,
destinationLevel: 0,
destinationOrigin: MTLOrigin(x: 0, y: 0, z: 0))
blitEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
}
%
Powered by
GPU
GPU
Your app
???
Metal
• ARKit
• MPS CNN / RNN
• Core Image
GPU Metal
• Metal - UIKit
• GPU GPU
• Metal - Metal / OpenGL
• Metal -
https://github.com/shu223

飛び道具ではないMetal #iOSDC