Introduction to Metal: Apple’s own GPU framework

  1. MTLDevice | Machine
  2. MTLLibrary | Available set of tools
  3. MTLCommandQueue | Manufacturing Line
  4. MTLCommandBuffer | Container
  5. MTLCommandEncoder | Worker
  6. MTLComputePipelineState | Configuration of Machine
  7. Metal Shader (not a class)| Tool.
a flow chart showing the model
// instantiate an MTLDevice
// expensive call do it once at the beginning
let metalDevice = MTLCreateSystemDefaultDevice()
// creates the libraryguard let library = metalDevice?.makeDefaultLibrary() 
else { return }
// fetches the metal shader named sepiaFilterlet shader = library.makeFunction(name: "sepiaFilter")
// creates the command queue
// also an expensive call
guard let commandQueue = metalDevice?.makeCommandQueue()
else { return }
// creates the command bufferguard let commandBuffer = commandQueue.makeCommandBuffer()
else { return }
// creates a compute command encoder
// rendering and blit purpose encoders also exist
guard let commandEncoder = commandBuffer.makeComputeCommandEncoder() else { return }// setting up texture data to add to the command buffercommandEncoder.setTexture(inputTexture, index: 0)
commandEncoder.setTexture(outputTexture, index: 1)
// setting up pipleline statecommandEncoder.setComputePipelineState(computePipelineState!)// end it so other encoders can be used
// encoders are cheap to set up, so no worries
commandEncoder.endEncoding()
// fetches the metal shader named sepiaFilterguard let shader = library.makeFunction(name: "sepiaFilter")
else { return }
// creates a compute Pipeline state with the shader function we just createdguard let computePipelineState = try? metalDevice?.makeComputePipelineState(function: shader)
else { return }

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Avinash

Avinash

Data Science at ShareChat. Ola. IIT Madras.