title: Converting CMSampleBuffer to CVMetalTexture in Swift with CVMetalTextureCacheCreateTextureFromImage
body: <p>I'm trying to get a simple render-camera-output-to-metal-layer pipeline going, and it works well enough in Objective-C (There's the MetalVideoCapture sample app) but there seems to be some formatting weirdness when I try to translate that to swift.  My ultrasimple capture buffer looks like this (ignore the lack of sanitization...)</p>
  
  <pre><code>    func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
      var error: CVReturn! = nil
      let sourceImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
      let width = CVPixelBufferGetWidth(sourceImageBuffer!)
      let height = CVPixelBufferGetHeight(sourceImageBuffer!)
      var outTexture: CVMetalTextureRef? = nil
  
      error  = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, videoTextureCache!, sourceImageBuffer!, nil, MTLPixelFormat.BGRA8Unorm, width, height, 0, &amp;outTexture!)
  
      if error != nil {
          print("Error! \(error)")
      }
  
      let videoTexture = CVMetalTextureGetTexture(outTexture!)
      self.imageTexture = videoTexture!
  }
  </code></pre>
  
  <p>Where videoTextureCache is <code>var videoTextureCache: CVMetalTextureCache? = nil
  </code></p>
  
  <p>But it gives me <code>Cannot invoke 'CVMetalTextureCacheCreateTextureFromImage' with an argument list of type '(CFAllocator!, CVMetalTextureCache, CVImageBuffer, nil, MTLPixelFormat, Int, Int, Int, inout CVMetalTextureRef)'</code></p>
  
  <p>The thing is, if I replace outTexture with nil it stops throwing the error but clearly that's not going to help me.  According to the reference for the function I need UnsafeMutablePointer?> for that last value.  Which I'm not sure how to get.</p>