Skip to content

Commit

Permalink
WIP: Update playground
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Nov 5, 2023
1 parent e42077a commit c4fc6f7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 30 deletions.
1 change: 1 addition & 0 deletions playground/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xcuserdata/
74 changes: 44 additions & 30 deletions playground/Metal.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,23 @@ let source = """
}
"""

@available(macOS 11.0, *)
public class MetalViewRenderer: NSObject, MTKViewDelegate {
weak var view: MTKView!
let commandQueue: MTLCommandQueue!
let device: MTLDevice!
let cps: MTLComputePipelineState!
var cps: MTLComputePipelineState?
private var startDate: Date = Date()
private var color: vector_float3 = vector_float3(0.3, 0.2, 1.0) // rgb
public init?(mtkView: MTKView) {
view = mtkView
device = MTLCreateSystemDefaultDevice()!
commandQueue = device.makeCommandQueue()
let library = try! device.makeLibrary(source: source, options: nil)
let function = library.makeFunction(name: "effect")!
cps = try! device.makeComputePipelineState(function: function)
if let library = try? device.makeLibrary(source: source, options: nil) {
if let function = library.makeFunction(name: "effect") {
cps = try? device.makeComputePipelineState(function: function)
}
}

super.init()
view.delegate = self
Expand All @@ -74,7 +77,8 @@ public class MetalViewRenderer: NSObject, MTKViewDelegate {
startDate = Date()
}

if let drawable = view.currentDrawable,
if let cps = cps,
let drawable = view.currentDrawable,
let commandBuffer = commandQueue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
{
Expand Down Expand Up @@ -136,31 +140,41 @@ struct ContentView: View {
VStack {
Text("Choose effect color")
HStack {
Button(action: {
delegate?.setColor(vector_float3(1.0, 0.3, 0.2))
}) {
Text("Red")
}
Button(action: {
delegate?.setColor(vector_float3(0.2, 1.0, 0.3))
}) {
Text("Green")
}
Button(action: {
delegate?.setColor(vector_float3(0.3, 0.2, 1.0))
}) {
Text("Blue")
}
Button(action: {
delegate?.setColor(vector_float3(1.0, 1.0, 1.0))
}) {
Text("Light")
}
Button(action: {
delegate?.setColor(vector_float3(0.0, 0.0, 0.0))
}) {
Text("Dark")
}
Button(
action: {
delegate?.setColor(vector_float3(1.0, 0.3, 0.2))
},
label: {
Text("Red")
})
Button(
action: {
delegate?.setColor(vector_float3(0.2, 1.0, 0.3))
},
label: {
Text("Green")
})
Button(
action: {
delegate?.setColor(vector_float3(0.3, 0.2, 1.0))
},
label: {
Text("Blue")
})
Button(
action: {
delegate?.setColor(vector_float3(1.0, 1.0, 1.0))
},
label: {
Text("Light")
})
Button(
action: {
delegate?.setColor(vector_float3(0.0, 0.0, 0.0))
},
label: {
Text("Dark")
})
}
}
}
Expand Down

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

0 comments on commit c4fc6f7

Please sign in to comment.