微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – CVMetalTextureCacheCreateTextureFromImage始终返回null

我正试图通过MetalKit渲染I420(ycbcr规划器)

大多数例子都是使用来自Camera的CMSampleBuffer,

但我的目标是使用给定的I420字节.

我做这样的事情:

let data = NSMutableData(contentsOfURL: NSBundle.mainBundle().URLForResource("yuv_640_360",withExtension: "yuv")!)

// Cache for Y

CVMetalTextureCacheCreate(kcfAllocatorDefault,nil,self.device!,&videoTextureCache)

var pixelBuffer: CVPixelBuffer?

CVPixelBufferCreateWithBytes(kcfAllocatorDefault,Int(size.width),Int(size.height),kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,data.mutableBytes,[
    "kCVPixelBufferMetalCompatibilityKey": true,"kCVPixelBufferOpenGLCompatibilityKey": true,"kCVPixelBufferIOSurfacePropertiesKey": []
    ],&pixelBuffer)

// Y texture

var yTextureRef : Unmanaged<CVMetalTexture>?

let yWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer,0)

let yHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer,0)

let result = CVMetalTextureCacheCreateTextureFromImage(kcfAllocatorDefault,(videoTextureCache?.takeUnretainedValue())!,pixelBuffer,MTLPixelFormat.R8Unorm,yWidth,yHeight,&yTextureRef);

基本上代码与其他示例几乎相同,但我自己创建自己的CVPixelBuffer.

我创建CVPixelBuffer和CVMetalTexture时没有出错,

但它总是为yTexture返回null.

如何创建正确的CVPixelBuffer并使用它进行渲染?

解决方法

问题解决了.

iosurface很重要,如果你通过CVPixelBufferCreateWithBytes或CVPixelBufferCreateWithPlanarBytes创建CVPixelBuffer,我发现iosurface总是为null.

一旦你使用了iosurface为null的CVPixelBuffer,那么MetalTexture总是为空.

应该做这样的事情:

let result = CVPixelBufferCreate(kcfAllocatorDefault,width,height,kCVPixelFormatType_420YpCbCr8Planar,[
    String(kCVPixelBufferIOSurfacePropertiesKey): [
        "IOSurfaceOpenGLESFBOCompatibility": true,"IOSurfaceOpenGLESTextureCompatibility": true,"IOSurfaceCoreAnimationCompatibility": true,]
    ],&self.pixelBuffer)

CVPixelBufferLockBaseAddress(self.pixelBuffer!,0)

for index in 0...2 {

    memcpy(CVPixelBufferGetBaseAddressOfPlane(self.pixelBuffer!,index),planesAddress[index],planesWidth[index] * planesHeight[index])

}

CVPixelBufferUnlockBaseAddress(self.pixelBuffer!,0)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐