As part of a project to create a GPU based reaction diffusion simulation, I stated to look at using Metal in Swift this weekend.

I've done similar work in the past targeting the Flash Player and using AGAL. Metal is a far higher level language than AGAL: it's based on C++ with a richer syntax and includes compute functions. Whereas in AGAL, to run cellular automata, I'd create a rectangle out of two triangles with a vertex shader and execute the reaction diffusion functions in a separate fragment shader, a compute shader is more direct: I can get and set textures and it can operate of individual pixels of that texture without the need for a vertex shader.

The Swift code I discuss in this article is based heavily on two articles at Metal By Example: Introduction to Compute Programming in Metal and Fundamentals of Image Processing in Metal. Both of which include Objective-C source code, so hopefully my Swift implementation will help some. 

My application has four main steps: initialise Metal, create a Metal texture from a UIImage, apply a kernel function to that texture, convert the newly generated texture back into a UIImage and display it. I'm using a simple example shader that changes the saturation of the input image. so I've also added a slider that changes the saturation value.

Let's look at each step one by one:

Initialising Metal

Initialising Metal is pretty simple: inside my view controller's overridden viewDidLoad(), I create a pointer to the default Metal device:

    var device: MTLDevice! = nil
    [...]
    device = MTLCreateSystemDefaultDevice()

I also need to create a library and command queue:

    defaultLibrary = device.newDefaultLibrary()
    commandQueue = device.newCommandQueue()

Finally, I add a reference to my Metal function to the library and synchronously create and compile a compute pipeline state:

    let kernelFunction = defaultLibrary.newFunctionWithName("kernelShader")
    pipelineState = device.newComputePipelineStateWithFunction(kernelFunction!, error: nil)

The kernelShader points to the saturation image processing function, written in Metal, that lives in my Shaders.metal file:

    kernel void kernelShader(texture2d<float, access::read> inTexture [[texture(0)]],
                         texture2d<float, access::write> outTexture [[texture(1)]],
                         constant AdjustSaturationUniforms &uniforms [[buffer(0)]],
                         uint2 gid [[thread_position_in_grid]])
    {
        float4 inColor = inTexture.read(gid);
        float value = dot(inColor.rgb, float3(0.299, 0.587, 0.114));
        float4 grayColor(value, value, value, 1.0);
        float4 outColor = mix(grayColor, inColor, uniforms.saturationFactor);
        outTexture.write(outColor, gid);
    }

Creating a Metal Texture from a UIIMage

There are a few steps in converting a UIImage into a MTLTexture instance. I create an array of UInt8 to hold an empty CGBitmapInfo, then use CGContextDrawImage() to copy the image into a bitmap context 

    let image = UIImage(named: "grand_canyon.jpg")
    let imageRef = image.CGImage
        
    let imageWidth = CGImageGetWidth(imageRef)
    let imageHeight = CGImageGetHeight(imageRef)

    let bytesPerRow = bytesPerPixel * imageWidth
        
    var rawData = [UInt8](count: Int(imageWidth * imageHeight * 4), repeatedValue: 0)
  
    let bitmapInfo = CGBitmapInfo(CGBitmapInfo.ByteOrder32Big.toRaw() | CGImageAlphaInfo.PremultipliedLast.toRaw())

    let context = CGBitmapContextCreate(&rawData, imageWidth, imageHeight, bitsPerComponent, bytesPerRow, rgbColorSpace, bitmapInfo)
        
    CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(imageWidth), CGFloat(imageHeight)), imageRef)

Once all of those steps have executed, I can create a new texture use its replaceRegion() method to write the image into it:

    let textureDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(MTLPixelFormat.RGBA8Unorm, width: Int(imageWidth), height: Int(imageHeight), mipmapped: true)
        
    texture = device.newTextureWithDescriptor(textureDescriptor)

    let region = MTLRegionMake2D(0, 0, Int(imageWidth), Int(imageHeight))
    texture.replaceRegion(region, mipmapLevel: 0, withBytes: &rawData, bytesPerRow: Int(bytesPerRow))

I also create an empty texture which the kernel function will write into:

    let outTextureDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(texture.pixelFormat, width: texture.width, height: texture.height, mipmapped: false)
    outTexture = device.newTextureWithDescriptor(outTextureDescriptor)

Invoking the Kernel Function

The next block of work is to set the textures and another variable on the kerne function and execute the shader. The first step is to instantiate a command buffer and command encoder:

    let commandBuffer = commandQueue.commandBuffer()
    let commandEncoder = commandBuffer.computeCommandEncoder()

...then set the pipeline state (we got from device.newComputePipelineStateWithFunction() earlier) and textures on the command encoder:

    commandEncoder.setComputePipelineState(pipelineState)
    commandEncoder.setTexture(texture, atIndex: 0)
    commandEncoder.setTexture(outTexture, atIndex: 1)

The filter requires an addition parameter that defines the saturation amount. This is passed into the shader via an MTLBuffer. To populate the buffer, I've created a small struct:

    struct AdjustSaturationUniforms 
    {
        var saturationFactor: Float
    }

Then newBufferWithBytes() to pass in my saturationFactor float value:

    var saturationFactor = AdjustSaturationUniforms(saturationFactor: self.saturationFactor)
    var buffer: MTLBuffer = device.newBufferWithBytes(&saturationFactor, length: sizeof(AdjustSaturationUniforms), options: nil)
    commandEncoder.setBuffer(buffer, offset: 0, atIndex: 0)

This is now accessible inside the shader as an argument to its kernel function:

    constant AdjustSaturationUniforms &uniforms [[buffer(0)]]

Now I'm ready invoke the function itself. Metal kernel functions use thread groups to break up their workload into chunks. In my example, I create 64 thread groups, then send them off to the GPU:

    let threadGroupCount = MTLSizeMake(8, 8, 1)
    let threadGroups = MTLSizeMake(texture.width / threadGroupCount.width, texture.height / threadGroupCount.height, 1)
        
    commandQueue = device.newCommandQueue()
        
    commandEncoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount)
    commandEncoder.endEncoding()
    commandBuffer.commit()
    commandBuffer.waitUntilCompleted()

Converting the Texture to a UIImage

Finally, now that the kernel function has executed, we need to do the reverse of above and get the image held in outTexture into a UIImage so it can be displayed. Again, I use a region  to define the size and the texture's getBytes() to populate an array on UInt8:

    let imageSize = CGSize(width: texture.width, height: texture.height)
    let imageByteCount = Int(imageSize.width * imageSize.height * 4)
        
    let bytesPerRow = bytesPerPixel * UInt(imageSize.width)
    var imageBytes = [UInt8](count: imageByteCount, repeatedValue: 0)
    let region = MTLRegionMake2D(0, 0, Int(imageSize.width), Int(imageSize.height))
        
    outTexture.getBytes(&imageBytes, bytesPerRow: Int(bytesPerRow), fromRegion: region, mipmapLevel: 0)

Now that imageBytes holds the raw data, it's a few lines to create a CGImage:

    let providerRef = CGDataProviderCreateWithCFData(
            NSData(bytes: &imageBytes, length: imageBytes.count * sizeof(UInt8))
        )
        
    let bitmapInfo = CGBitmapInfo(CGBitmapInfo.ByteOrder32Big.toRaw() | CGImageAlphaInfo.PremultipliedLast.toRaw())
    let renderingIntent = kCGRenderingIntentDefault
        
    let imageRef = CGImageCreate(UInt(imageSize.width), UInt(imageSize.height), bitsPerComponent, bitsPerPixel, bytesPerRow, rgbColorSpace, bitmapInfo, providerRef, nil, false, renderingIntent)
        
    imageView.image = UIImage(CGImage: imageRef)

...and we're done! 

Metal requires an A7 or A8 processor and this code has been built and tested under Xcode 6. All the source code is available at my GitHub repository here.


4

View comments

  1. Anonymous10:10 AM

    Thanks for the nice tutorial. While running some practice leveraging your example, I ran into an issue related to memory alignment. I am practicing zero-copy data transfer by using 'newBufferWithBytesNoCopy.' This seems to require the memory to be aligned to a certain size. Could you please give me some advice on how to align pointer to an Obj-C structure in Swift for creating a Metal buffer object with newBufferWithBytesNoCopy?

    ReplyDelete
  2. Anonymous9:44 PM

    Thanks for the article. Any thoughts on how to apply this to a SCNScene or SCNRenderer to get barrel distortion?

    ReplyDelete
  3. Actually, I have a barrel distortion CIKernel which you can apply as a CIFilter to an SCNScene. It's part of y CRT Core Image filter and available here: https://github.com/FlexMonkey/Filterpedia/tree/master/Filterpedia/customFilters

    ReplyDelete
  4. Hi Simon,
    Is there any chance that you could migrate your code to Swift 3 or later? Especially for the Filterpedia app? I tried converting myself, but am running into lots of Async errors that I don't know how to address (and that don't get resolved by Xcode's code migration)...I'm following along your excellent image processing book, but my swift knowhow is a bit lacking. Thank you for all these amazing resources...

    ReplyDelete
Primordial Particle System in SideFX Houdini
5
"Sparse Vermiform" Rendering of Fluids with Pressure Based Color
3
Particle Advection by Gray Scott Reaction Diffusion Revisited
Particle Advection by Reaction Diffusion in SideFX Houdini
1
Revisiting Mitosis in SideFX Houdini
4
Faux Grain / Fluid Interaction in Houdini
1
Parametric Fibonacci Spheres in Houdini
Houdini Grain Solver with Custom VEX Forces
Animating Rayleigh-Taylor Instability in SideFX Houdini
1
Faking Toroidal Eddies in Side FX Houdini
Mixing Fluids in Houdini
Animating Gravitational Tides with Houdini FLIP Fluids
1
Houdini FLIP Fluid & Radial Gravity
1
Reaction Diffusion in SideFX Houdini
2
Animating Planet Engulfment in Houdini
1
Simulating Accretion with Houdini's Grains Solver
Chaotic Magnetic Pendulum with Custom Radial Forces in Houdini
More Chaos in Houdini: Simulating a Double Pendulum
Creating a Geometric Structure from Mitosis
2
Simulating Mitosis in Houdini
4
Stripy Viscous Fluid Impacts in Houdini
1
Using Houdini VOPs to Deform Geometry
1
Creating a Swarm Chemistry Digital Asset in Houdini
Swarm Chemistry in SideFX Houdini
1
Randomly Transforming Scattered Cones in Houdini
1
Metaball Morphogenesis in Houdini
Melting Geometry in Houdini
2
Simulating Belousov-Zhabotinsky Reaction in Houdini
Chaos in Houdini! Modeling Strange Attractors with Particles
Experimenting with Impacts in SideFX Houdini
Chaos in Swift! Visualising the Lorenz Attractor with Metal
Swift 3.0 for Core Image Developers
4
A Core Image Transverse Chromatic Aberration Filter in Swift
1
Random Numbers in Core Image Kernel Language
Core Image for Swift Version 1.3
Core Image for Swift Version 1.3
Nodality for AudioKit: Node Based Synth for iPad
Histogram Equalisation with Metal Performance Shaders
A Histogram Display Component in Swift for iOS
Simulating Depth-of-Field with Variable Bokeh in Core Image
Simulating Bokeh with Metal Performance Shaders
1
Creating a Lens Flare Filter in Core Image
Loading, Filtering & Saving Videos in Swift
3
vImage Histogram Functions Part II: Specification
Histogram Functions in Accelerate vImage
1
New Core Image Procedural Noise Generators for Filterpedia
Recreating Kai's Power Tools Goo in Swift
11
Creating Procedural Normal Maps for SceneKit
New Custom Core Image Filters
1
A Look at Perspective Transform & Correction with Core Image
8
Creating a Custom Variable Blur Filter in Core Image
3
Core Image for Swift v1.2 Released!
3
Creating a Selective HSL Adjustment Filter in Core Image
Creating a Bulging Eyes Purikura Effect with Core Image
1
Sweetcorn: A Node Based Core Image Kernel Builder
1
Core Image for Swift: Advanced Image Processing for iOS
2
Properly Typed Selectors in Xcode 7.3 beta 4
7
Core Image for Swift Available for Pre-Order!
10
Playing with Interpolation Functions in Swift
1
Metal Kernel Functions as Core Image Filter Engines
New Custom Core Image Filters Added to Filterpedia
5
Computational Fluid Dynamics in CoreImage with CIKernel
3
Creating a Slide Show App with Core Image Transitions
Filterpedia: Core Image Filter Explorer
5
CartoonEyes: Compositing Cartoon Eyes over Face from Front Camera in Swift
3
Apple Pencil Controlled Christmas Tree Bowling with SceneKit
1
BristlePaint: Embossed Painting with Individual Bristles using SpriteKit Normal Mapping
1
Scribe: A Handwriting Recognition Component for iOS
2
MercurialPaint: Globular Embossed Painting with Metal & Core Image
MercurialText: Embossed Type using SceneKit and CIShadedMaterial
1
FurrySketch: Hirsute Drawing with an Apple Pencil
PencilSynth - An Apple Pencil Controlled Synthesiser
PencilController - Using Apple Pencil as a 3D Controller for Image Editing
5
A Look at Agents, Goals & Behaviours in GameplayKit
PencilScale - Using an Apple Pencil with an iPad Pro as an Electronic Scale
2
A First Look at Metal Performance on the iPad Pro
Smooth Drawing for iOS in Swift with Hermite Spline Interpolation
3
Introducing Image Processing in Metal
Swift Hierarchical Selector Component based on UIPickerView & UICollectionView
The Plum-O-Meter: Weighing Plums Using 3D Touch in Swift
27
Book Review: Swift Documentation Markup by Erica Sadun
1
3D Touch in Swift: A Retrospective
3D ReTouch: An Experimental Retouching App Using 3D Touch
1
ForceZoom: Popup Image Detail View using 3D Touch Peek
Globular: Colourful Metaballs Controlled by 3D Touch
2
ForceSketch: A 3D Touch Drawing App using CIImageAccumulator
DeepPressGestureRecognizer - A 3D Touch Custom Gesture Recogniser in Swift
2
Rotatable: A Swift Protocol Extension to Rotate any UIView
ChromaTouch: a 3D Touch Colour Picker in Swift
3
3D Touch in Swift: Implementing Peek & Pop
1
A First Look at Metal Performance on the iPhone 6s
Applying Gaussian Blur to UIViews with Swift Protocol Extensions
7
Advanced Touch Handling in iOS9: Coalescing and Prediction
3
A Swift Node Based User Interface Component for iOS
2
Using an iPhone as a 3D Mouse with Multipeer Connectivity in Swift
6
A Swift Nixie Tube Display Component
CoreMotion Controlled 3D Sketching on an iPhone with Swift
2
iOS Live Camera Controlled Particles in Swift & Metal
6
Metal Performance Shaders & Fallback Copy Allocators
Event Dispatching in Swift with Protocol Extensions
1
Hybrid Marking Menu / Radial Slider Swift Component for iOS
About Me
About Me
Labels
Labels
Blog Archive
Loading