From d8a5656df8123e4f09295440429bc73abcbcdab5 Mon Sep 17 00:00:00 2001 From: Marius Horga Date: Sat, 1 Oct 2016 13:00:16 +0300 Subject: [PATCH] added chapter 18 --- README.md | 1 + chapter18.playground/Contents.swift | 45 ++++++++++++++++++ chapter18.playground/Sources/MetalView.swift | 40 ++++++++++++++++ chapter18.playground/contents.xcplayground | 4 ++ .../contents.xcworkspacedata | 7 +++ .../UserInterfaceState.xcuserstate | Bin 0 -> 11344 bytes chapter18.playground/timeline.xctimeline | 6 +++ 7 files changed, 103 insertions(+) create mode 100644 chapter18.playground/Contents.swift create mode 100644 chapter18.playground/Sources/MetalView.swift create mode 100644 chapter18.playground/contents.xcplayground create mode 100644 chapter18.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 chapter18.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate create mode 100644 chapter18.playground/timeline.xctimeline diff --git a/README.md b/README.md index 664abc4..e021d1a 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,4 @@ Repository to accompany the following blog posts: - [Using MetalKit part 15](http://mhorga.org/2016/06/23/using-metalkit-part-15.html) - [Using MetalKit part 16](http://mhorga.org/2016/07/06/using-metalkit-part-16.html) - [Using MetalKit part 17](http://mhorga.org/2016/09/24/using-metalkit-part-17.html) +- [Using MetalKit part 18](http://mhorga.org/2016/09/30/using-metalkit-part-2-3-2.html) diff --git a/chapter18.playground/Contents.swift b/chapter18.playground/Contents.swift new file mode 100644 index 0000000..0027557 --- /dev/null +++ b/chapter18.playground/Contents.swift @@ -0,0 +1,45 @@ + + +let shader = +"#include \n" + + "using namespace metal;" + + "kernel void k(texture2d o[[texture(0)]]," + + " uint2 gid[[thread_position_in_grid]]) {" + + " int width = o.get_width();" + + " int height = o.get_height();" + + " float2 uv = float2(gid) / float2(width, height);" + + " float3 color = mix(float3(1.0, 0.6, 0.1), float3(0.5, 0.8, 1.0), sqrt(1 - uv.y));" + + " float2 q = uv - float2(0.67, 0.25);" + + " float r = 0.2 + 0.1 * cos(atan2(q.x, q.y) * 9.0 + 20.0 * q.x);" + + " color *= smoothstep(r, r + 0.01, length(q));" + + " r = 0.03 + 0.002 * cos(120.0 * q.y) + exp(-50.0 * (1.0 - uv.y));" + + " color *= 1.0 - (1.0 - smoothstep(r, r + 0.002, abs(q.x - 0.25 * sin(2.0 * q.y)))) * smoothstep(0.0, 0.1, q.y);" + + " o.write(float4(color, 1.0), gid);" + +"}" + +//"#include \n" + +// "using namespace metal;" + +// "kernel void k(texture2d o[[texture(0)]]," + +// " uint2 gid[[thread_position_in_grid]]) {" + +// " int width = o.get_width();" + +// " int height = o.get_height();" + +// " float2 uv = float2(gid) / float2(width, height);" + +// " float2 q = uv - float2(0.5);" + +// " float a = atan2(q.y, q.x) + 0.25;" + +// " float s = 0.5 + 0.5 * sin(3.0 * a);" + +// " float t = 0.15 + 0.5 * pow(s, 0.3) + 0.1 * pow(0.5 + 0.5 * cos(6.0 * a), 0.5);" + +// " float h = sqrt(dot(q, q)) / t;" + +// " float f = 0.0;" + +// " if(h < 0.4) f = 1.0;" + +// " float3 color = mix(float3(0.9), float3(0.5 * h, 0.5 + 0.5 * h, 0.0), f);" + +// " o.write(float4(color, 1.0), gid);" + +//"}" + +import MetalKit +import PlaygroundSupport + +let frame = CGRect(x: 0, y: 0, width: 300, height: 300) +let view = MTKView(frame: frame) +let delegate = MetalView(mtkView: view, shader: shader) +view.delegate = delegate +PlaygroundPage.current.liveView = view diff --git a/chapter18.playground/Sources/MetalView.swift b/chapter18.playground/Sources/MetalView.swift new file mode 100644 index 0000000..b8c4802 --- /dev/null +++ b/chapter18.playground/Sources/MetalView.swift @@ -0,0 +1,40 @@ + +import MetalKit + +public class MetalView: NSObject, MTKViewDelegate { + weak var view: MTKView! + let commandQueue: MTLCommandQueue + let device: MTLDevice + let cps: MTLComputePipelineState + + public init?(mtkView: MTKView, shader: String) { + view = mtkView + view.clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1) + view.colorPixelFormat = .bgra8Unorm + device = MTLCreateSystemDefaultDevice()! + commandQueue = device.makeCommandQueue() + let library = try! device.makeLibrary(source: shader, options: nil) + let function = library.makeFunction(name:"k")! + cps = try! device.makeComputePipelineState(function: function) + + super.init() + view.delegate = self + view.device = device + } + + public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} + + public func draw(in view: MTKView) { + let drawable = view.currentDrawable! + let commandBuffer = commandQueue.makeCommandBuffer() + let commandEncoder = commandBuffer.makeComputeCommandEncoder() + commandEncoder.setComputePipelineState(cps) + commandEncoder.setTexture(drawable.texture, at: 0) + let groups = MTLSize(width: Int(view.frame.width)/4, height: Int(view.frame.height)/4, depth: 1) + let threads = MTLSize(width: 8, height: 8,depth: 1) + commandEncoder.dispatchThreadgroups(groups,threadsPerThreadgroup: threads) + commandEncoder.endEncoding() + commandBuffer.present(drawable) + commandBuffer.commit() + } +} diff --git a/chapter18.playground/contents.xcplayground b/chapter18.playground/contents.xcplayground new file mode 100644 index 0000000..a93d484 --- /dev/null +++ b/chapter18.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/chapter18.playground/playground.xcworkspace/contents.xcworkspacedata b/chapter18.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/chapter18.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/chapter18.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate b/chapter18.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000000000000000000000000000000000000..ce52f92bfc3b8951779165b837675e3697634090 GIT binary patch literal 11344 zcmd6N33yZ0*7hFKW}cI#nTJy*p%auAkfEhzrln9Q6oinrhZ0DWlB7Uo+HtywqKF`f z2yL14qIex}1euXR^fHJHqNoUn10bV-`t6f6X@mG&|Nnpf@B11aa&q?GYwf+pcdc{E zYdr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