# Example of Swift and Metal GPU Programming on OS X 10.11 El Capitan

- Canonical URL: https://amund.blog/archive-medium-swift-metal-osx-el-capitan/
- Author: Amund Tveit (https://amund.blog/about/)
- Published: 2015-06-10
- Tags: Apple Silicon, GPU, Archive
- Original Source: medium.com (https://medium.com/@atveit/example-of-swift-and-metal-gpu-programming-on-osx-10-11-el-capitan-a8cf75bd1b43)

---

Apple recently (June 2015 on WWDC) announced Metal GPU Programming support for OS X 10.11 El Capitan, which is great news for Metal-developers on iOS!
This posting gives a basic example how to use Swift with Metal on OSX. The code is available on[**github.com/atveit/SwiftMetalForOSX**](https://github.com/atveit/SwiftMetalForOSX) and I’ve so far only tested it on my laptop (Macbook Pro Retina, Mid 2012 with a NVIDIA GeForce GT 650M 1024 MB GPU)

**But first, what is Metal?**

Apple describes Metal originally as: *“Metal provides the lowest-overhead access to the GPU, enabling you to maximize the graphics and compute potential of your iOS 8 app. With a streamlined API, precompiled shaders, and support for efficient multi-threading, Metal can take your game or graphics app to the next level of performance and capability.”* — source: [**https://developer.apple.com/metal/**](https://developer.apple.com/metal/)

I consider Metal as an alternative to OpenGL for graphics processing, but also as an alternative to OpenCL and Nvidia’s Cuda
on data-parallel programming for GPUs (also called General Purpose GPU programming — GPGPU).

## 1. Swift and Metal GPU Programming on OSX 10.11 / El Capitan

This example shows a very simple Metal shader function — doubler — that doubles each element in the input vector

## 1.1 Summary of steps:

1. Create a new OSX Cocoa Swift project in Xcode (requires Xcode 7.0 beta or newer)
2. Add dependency libraries (added Metal and Quartzcore libraries in this case)
3. Create a Metal Shader method — doubler — in [**Shaders.metal**](https://github.com/atveit/SwiftMetalForOSX/blob/master/SwiftMetalForOSX/Shaders.metal)
4. Create a (convenience) **MetalViewController** class in [** MetalViewController.swift**](https://github.com/atveit/SwiftMetalForOSX/blob/master/SwiftMetalForOSX/MetalViewController.swift) that provides the methods:
5. **setupMetal**() — creates metalDevice, metalCommandQueue, metalDefaultLibrary and metalCommandBuffer objects** setupShaderInMetalPipeline**(shaderName:String) — returns shader function, computePipelineState and computePipelineErrors objects** createMetalBuffer**(var vector:[Float]) — creates a Metal byte buffer from a Swift float array
7. Create a **ViewController** in [** ViewController.swift**](https://github.com/atveit/SwiftMetalForOSX/blob/master/SwiftMetalForOSX/ViewController.swift) that:
8. creates an input vector and (empty) output vector of same lengthcreates Metal Byte buffers for those vectorscreates a **MetalComputeCommandEncoder** — command encoder (primary object for using Metal)adds metal input and output buffers to the command encoderconnects the shader function to the command encoder (indirect via computePipelineState object)configures number of threads and thread groups to use on the GPU for the command encoderfinalizes configuration of command encoder (i.e. ends encoding)starts metal job by commit() and waits until it is completedgets output data from the GPU into an NSData objectconverts NSData object into a regular Swift Arrayprints output array

## Conclusion

Have shown a simple example of using Metal for GPU-programming on OSX, there were as you might see some minor changes in the Metal API that came with Xcode 7 beta compared with prior Xcode versions, but it was quite easy to make it work.

Best regards,
[**Amund Tveit**](http://memkite.com/team/)
