Skip to content

Commit

Permalink
added chapter 18
Browse files Browse the repository at this point in the history
  • Loading branch information
mhorga committed Oct 1, 2016
1 parent d870b9d commit d8a5656
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
45 changes: 45 additions & 0 deletions chapter18.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


let shader =
"#include <metal_stdlib>\n" +
"using namespace metal;" +
"kernel void k(texture2d<float,access::write> 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 <metal_stdlib>\n" +
// "using namespace metal;" +
// "kernel void k(texture2d<float,access::write> 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
40 changes: 40 additions & 0 deletions chapter18.playground/Sources/MetalView.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
4 changes: 4 additions & 0 deletions chapter18.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
6 changes: 6 additions & 0 deletions chapter18.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>

0 comments on commit d8a5656

Please sign in to comment.