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 0000000..ce52f92 Binary files /dev/null and b/chapter18.playground/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/chapter18.playground/timeline.xctimeline b/chapter18.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/chapter18.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + +