How to create a MTLTexture backed by a CVPixelBuffer
<p>What's the correct way to generate a MTLTexture backed by a CVPixelBuffer?</p>

<p>I have the following code, but it seems to leak:</p>

<pre><code>func PixelBufferToMTLTexture(pixelBuffer:CVPixelBuffer) -&gt; MTLTexture
{
    var texture:MTLTexture!

    let width = CVPixelBufferGetWidth(pixelBuffer)
    let height = CVPixelBufferGetHeight(pixelBuffer)

    let format:MTLPixelFormat = .BGRA8Unorm


    var textureRef : Unmanaged&lt;CVMetalTextureRef&gt;?

    let status = CVMetalTextureCacheCreateTextureFromImage(nil,
                                                           videoTextureCache!.takeUnretainedValue(),
                                                           pixelBuffer,
                                                           nil,
                                                           format,
                                                           width,
                                                           height,
                                                           0,
                                                           &amp;textureRef)

    if(status == kCVReturnSuccess)
    {
        texture = CVMetalTextureGetTexture(textureRef!.takeUnretainedValue())
    }

    return texture
}
</code></pre>