Skip to content

Commit

Permalink
Update for Swift 3.0.1 / Xcode 8.1 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikaderstedt authored and jtbandes committed Nov 18, 2016
1 parent 6bae408 commit b70801c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions Metalbrot.playground/Contents.swift
Expand Up @@ -6,6 +6,7 @@ import Metal
*[Jacob Bandes-Storch](http://bandes-stor.ch/), Feb 2016*
*Nov 2016: [Updated for Swift 3.0.1 / Xcode 8.1](https://github.com/jtbandes/Metalbrot.playground/pull/2)*
*Sep 2016: Updated for Swift 3*
This playground provides a small interactive example of how to use Metal to render visualizations of [fractals](https://en.wikipedia.org/wiki/Fractal) (namely, the [Mandelbrot set](https://en.wikipedia.org/wiki/Mandelbrot_set) and [Julia sets](https://en.wikipedia.org/wiki/Julia_set)). This certainly isn’t a comprehensive overview of Metal, but hopefully it’s easy to follow and modify. Enjoy!
Expand All @@ -28,7 +29,7 @@ import Metal
let device = require(MTLCopyAllDevices().first{ $0.isLowPower } ?? MTLCreateSystemDefaultDevice(),
orDie: "need a Metal device")

let commandQueue = device.newCommandQueue()
let commandQueue = device.makeCommandQueue()

let drawingQueue = DispatchQueue(label: "drawingQueue", qos: .userInteractive)

Expand All @@ -41,34 +42,34 @@ let drawingQueue = DispatchQueue(label: "drawingQueue", qos: .userInteractive)
let shaderSource = require(try String(contentsOf: #fileLiteral(resourceName: "Shaders.metal")),
orDie: "unable to read shader source file")

let library = require(try device.newLibrary(withSource: shaderSource, options: nil),
let library = require(try device.makeLibrary(source: shaderSource, options: nil),
orDie: "compiling shaders failed")

/*:
- Experiment: Open up `Shaders.metal` and glance through it to understand what the shaders are doing.
- Important: If your shader has a syntax error, `newLibraryWithSource()` will throw an error here when it tries to compile the program.
- Important: If your shader has a syntax error, `makeLibrary(source:)` will throw an error here when it tries to compile the program.
*/
let mandelbrotShader = require(library.newFunction(withName: "mandelbrotShader"),
let mandelbrotShader = require(library.makeFunction(name: "mandelbrotShader"),
orDie: "unable to get mandelbrotShader")

let juliaShader = require(library.newFunction(withName: "juliaShader"),
let juliaShader = require(library.makeFunction(name: "juliaShader"),
orDie: "unable to get juliaShader")

//: The Julia set shader also needs some extra input, an *(x, y)* point, from the CPU. We can pass this via a shared buffer.
let juliaBuffer = device.newBuffer(withLength: 2 * MemoryLayout<Float32>.size, options: [])
let juliaBuffer = device.makeBuffer(length: 2 * MemoryLayout<Float32>.size, options: [])

/*:
----
Before we can use these shaders, Metal needs to know how to request they be executed on the GPU. This information is precomputed and stored as ***compute pipeline state***, which we can reuse repeatedly.
When executing the program, we’ll also have to decide how to utilize the GPU’s threads (how many groups of threads to use, and the number of threads per group). This will depend on the size of the view we want to draw into.
*/
let mandelbrotPipelineState = require(try device.newComputePipelineState(with: mandelbrotShader),
let mandelbrotPipelineState = require(try device.makeComputePipelineState(function: mandelbrotShader),
orDie: "unable to create compute pipeline state")

let juliaPipelineState = require(try device.newComputePipelineState(with: juliaShader),
let juliaPipelineState = require(try device.makeComputePipelineState(function: juliaShader),
orDie: "unable to create compute pipeline state")

var threadgroupSizes = ThreadgroupSizes.zeros // To be calculated later
Expand Down
4 changes: 2 additions & 2 deletions Metalbrot.playground/Sources/Helpers.swift
Expand Up @@ -90,8 +90,8 @@ public extension MTLCommandQueue
return
}

let buffer = self.commandBuffer()
let encoder = buffer.computeCommandEncoder()
let buffer = self.makeCommandBuffer()
let encoder = buffer.makeComputeCommandEncoder()
encoder.setTexture(drawable.texture, at: 0)

drawBlock(encoder)
Expand Down

0 comments on commit b70801c

Please sign in to comment.