updated to Swift 3
This commit is contained in:
parent
14db3095d0
commit
2fa2654ec8
|
|
@ -14,13 +14,13 @@ public class MetalView: NSObject, MTKViewDelegate {
|
||||||
|
|
||||||
func registerShaders() {
|
func registerShaders() {
|
||||||
device = MTLCreateSystemDefaultDevice()!
|
device = MTLCreateSystemDefaultDevice()!
|
||||||
queue = device.newCommandQueue()
|
queue = device.makeCommandQueue()
|
||||||
let path = Bundle.main.pathForResource("Shaders", ofType: "metal")
|
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
|
||||||
do {
|
do {
|
||||||
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
|
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
|
||||||
let library = try device.newLibrary(withSource: input, options: nil)
|
let library = try device.makeLibrary(source: input, options: nil)
|
||||||
let kernel = library.newFunction(withName: "compute")!
|
let kernel = library.makeFunction(name: "compute")!
|
||||||
cps = try device.newComputePipelineState(with: kernel)
|
cps = try device.makeComputePipelineState(function: kernel)
|
||||||
} catch let e {
|
} catch let e {
|
||||||
Swift.print("\(e)")
|
Swift.print("\(e)")
|
||||||
}
|
}
|
||||||
|
|
@ -30,8 +30,8 @@ public class MetalView: NSObject, MTKViewDelegate {
|
||||||
|
|
||||||
public func draw(in view: MTKView) {
|
public func draw(in view: MTKView) {
|
||||||
if let drawable = view.currentDrawable {
|
if let drawable = view.currentDrawable {
|
||||||
let commandBuffer = queue.commandBuffer()
|
let commandBuffer = queue.makeCommandBuffer()
|
||||||
let commandEncoder = commandBuffer.computeCommandEncoder()
|
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
|
||||||
commandEncoder.setComputePipelineState(cps)
|
commandEncoder.setComputePipelineState(cps)
|
||||||
commandEncoder.setTexture(drawable.texture, at: 0)
|
commandEncoder.setTexture(drawable.texture, at: 0)
|
||||||
let threadGroupCount = MTLSizeMake(8, 8, 1)
|
let threadGroupCount = MTLSizeMake(8, 8, 1)
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -3,43 +3,43 @@ import MetalKit
|
||||||
|
|
||||||
public class MetalView: NSObject, MTKViewDelegate {
|
public class MetalView: NSObject, MTKViewDelegate {
|
||||||
|
|
||||||
public var device: MTLDevice!
|
public var device: MTLDevice!
|
||||||
var queue: MTLCommandQueue!
|
var queue: MTLCommandQueue!
|
||||||
var cps: MTLComputePipelineState!
|
var cps: MTLComputePipelineState!
|
||||||
|
|
||||||
override public init() {
|
override public init() {
|
||||||
super.init()
|
super.init()
|
||||||
registerShaders()
|
registerShaders()
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerShaders() {
|
||||||
|
device = MTLCreateSystemDefaultDevice()!
|
||||||
|
queue = device.makeCommandQueue()
|
||||||
|
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
|
||||||
|
do {
|
||||||
|
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
|
||||||
|
let library = try device.makeLibrary(source: input, options: nil)
|
||||||
|
let kernel = library.makeFunction(name: "compute")!
|
||||||
|
cps = try device.makeComputePipelineState(function: kernel)
|
||||||
|
} catch let e {
|
||||||
|
Swift.print("\(e)")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func registerShaders() {
|
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
|
||||||
device = MTLCreateSystemDefaultDevice()!
|
|
||||||
queue = device.newCommandQueue()
|
public func draw(in view: MTKView) {
|
||||||
let path = Bundle.main.pathForResource("Shaders", ofType: "metal")
|
if let drawable = view.currentDrawable {
|
||||||
do {
|
let commandBuffer = queue.makeCommandBuffer()
|
||||||
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
|
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
|
||||||
let library = try device.newLibrary(withSource: input, options: nil)
|
commandEncoder.setComputePipelineState(cps)
|
||||||
let kernel = library.newFunction(withName: "compute")!
|
commandEncoder.setTexture(drawable.texture, at: 0)
|
||||||
cps = try device.newComputePipelineState(with: kernel)
|
let threadGroupCount = MTLSizeMake(8, 8, 1)
|
||||||
} catch let e {
|
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)
|
||||||
Swift.print("\(e)")
|
commandEncoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount)
|
||||||
}
|
commandEncoder.endEncoding()
|
||||||
}
|
commandBuffer.present(drawable)
|
||||||
|
commandBuffer.commit()
|
||||||
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
|
|
||||||
|
|
||||||
public func draw(in view: MTKView) {
|
|
||||||
if let drawable = view.currentDrawable {
|
|
||||||
let commandBuffer = queue.commandBuffer()
|
|
||||||
let commandEncoder = commandBuffer.computeCommandEncoder()
|
|
||||||
commandEncoder.setComputePipelineState(cps)
|
|
||||||
commandEncoder.setTexture(drawable.texture, at: 0)
|
|
||||||
let threadGroupCount = MTLSizeMake(8, 8, 1)
|
|
||||||
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)
|
|
||||||
commandEncoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount)
|
|
||||||
commandEncoder.endEncoding()
|
|
||||||
commandBuffer.present(drawable)
|
|
||||||
commandBuffer.commit()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Reference in New Issue