updated to Swift 3

This commit is contained in:
Marius Horga 2016-11-29 23:05:57 +02:00
parent 9ee29ce749
commit dfc0532882
5 changed files with 13 additions and 33 deletions

View File

@ -33,8 +33,7 @@ float fbm(float2 uv)
} }
kernel void compute(texture2d<float, access::write> output [[texture(0)]], kernel void compute(texture2d<float, access::write> output [[texture(0)]],
constant float2 &mouse [[buffer(0)]], constant float &timer [[buffer(0)]],
constant float &timer [[buffer(1)]],
uint2 gid [[thread_position_in_grid]]) uint2 gid [[thread_position_in_grid]])
{ {
int width = output.get_width(); int width = output.get_width();

View File

@ -7,15 +7,6 @@ public class MetalView: MTKView, NSWindowDelegate {
var cps: MTLComputePipelineState! = nil var cps: MTLComputePipelineState! = nil
var timer: Float = 0 var timer: Float = 0
var timerBuffer: MTLBuffer! var timerBuffer: MTLBuffer!
var mouseBuffer: MTLBuffer!
var pos: NSPoint!
override public func mouseDown(_ event: NSEvent) {
pos = convertToLayer(convert(event.locationInWindow, from: nil))
let scale = layer!.contentsScale
pos.x *= scale
pos.y *= scale
}
required public init(coder: NSCoder) { required public init(coder: NSCoder) {
super.init(coder: coder) super.init(coder: coder)
@ -28,12 +19,11 @@ public class MetalView: MTKView, NSWindowDelegate {
override public func draw(_ dirtyRect: NSRect) { override public func draw(_ dirtyRect: NSRect) {
if let drawable = currentDrawable { if let drawable = 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)
commandEncoder.setBuffer(mouseBuffer, offset: 0, at: 2) commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0)
commandEncoder.setBuffer(timerBuffer, offset: 0, at: 1)
update() update()
let threadGroupCount = MTLSizeMake(8, 8, 1) let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)
@ -47,24 +37,21 @@ public class MetalView: MTKView, NSWindowDelegate {
func update() { func update() {
timer += 0.01 timer += 0.01
var bufferPointer = timerBuffer.contents() let bufferPointer = timerBuffer.contents()
memcpy(bufferPointer, &timer, sizeof(Float.self)) memcpy(bufferPointer, &timer, MemoryLayout<Float>.size)
bufferPointer = mouseBuffer.contents()
memcpy(bufferPointer, &pos, sizeof(NSPoint.self))
} }
func registerShaders() { func registerShaders() {
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)")
} }
timerBuffer = device!.newBuffer(withLength: sizeof(Float.self), options: []) timerBuffer = device!.makeBuffer(length: MemoryLayout<Float>.size, options: [])
mouseBuffer = device!.newBuffer(withLength: sizeof(NSPoint.self), options: [])
} }
} }

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='osx'> <playground version='5.0' target-platform='osx' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/> <timeline fileName='timeline.xctimeline'/>
</playground> </playground>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>