How to create a MTLTexture backed by a CVPixelBuffer

落花浮王杯 提交于 2021-02-07 13:22:51

问题


What's the correct way to generate a MTLTexture backed by a CVPixelBuffer?

I have the following code, but it seems to leak:

func PixelBufferToMTLTexture(pixelBuffer:CVPixelBuffer) -> MTLTexture
{
    var texture:MTLTexture!

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

    let format:MTLPixelFormat = .BGRA8Unorm


    var textureRef : Unmanaged<CVMetalTextureRef>?

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

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

    return texture
}

回答1:


Ah, I was missing: textureRef?.release()



来源:https://stackoverflow.com/questions/37445052/how-to-create-a-mtltexture-backed-by-a-cvpixelbuffer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!