我正在尝试通过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!, nil, &videoTextureCache)

var pixelBuffer: CVPixelBuffer?

CVPixelBufferCreateWithBytes(kCFAllocatorDefault, Int(size.width), Int(size.height), kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, data.mutableBytes, Int(size.width), nil, nil, [
    "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, nil, MTLPixelFormat.R8Unorm, yWidth, yHeight, 0, &yTextureRef);

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

创建CVPixelBuffer和CVMetalTexture时没有错误,

但对于yTexture,它始终返回null。

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

最佳答案

问题解决了。

iosurface很重要,我发现如果通过CVPixelBufferCreateWithBytes或CVPixelBufferCreateWithPlanarBytes创建CVPixelBuffer,则iosurface始终为null。

一旦使用iosurface为null的CVPixelBuffer,则MetalTexture始终为null。

应该做这样的事情:

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)

10-08 06:26