3

シンプルなレンダリング カメラ出力からメタル レイヤーへのパイプラインを実行しようとしていますが、Objective-C で十分に機能します (MetalVideoCapture サンプル アプリがあります)。それを迅速に翻訳します。私の超単純なキャプチャ バッファは次のようになります (サニタイズがないことは無視してください...)

    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, &outTexture!)

    if error != nil {
        print("Error! \(error)")
    }

    let videoTexture = CVMetalTextureGetTexture(outTexture!)
    self.imageTexture = videoTexture!
}

videoTextureCache の場所var videoTextureCache: CVMetalTextureCache? = nil

しかし、それは私に与えますCannot invoke 'CVMetalTextureCacheCreateTextureFromImage' with an argument list of type '(CFAllocator!, CVMetalTextureCache, CVImageBuffer, nil, MTLPixelFormat, Int, Int, Int, inout CVMetalTextureRef)'

問題は、outTexture を nil に置き換えると、エラーがスローされなくなりますが、明らかにそれは役に立たないということです。関数のリファレンスによると、その最後の値には UnsafeMutablePointer?> が必要です。入手方法がわかりません。

4

2 に答える 2

3

事前に textureCache を割り当ててみてください。メンバー変数として使用するものは次のとおりです。

var _videoTextureCache : Unmanaged<CVMetalTextureCacheRef>?

そして、初期化メソッドで textureCache を割り当てます

CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, _context.device, nil, &_videoTextureCache)

ここで、_context.device は MTLDevice です。次に、captureOutput メソッドで、次を使用します (ここにはエラー チェックが含まれていないことに注意してください)。

var textureRef : Unmanaged<CVMetalTextureRef>?
CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _videoTextureCache!.takeUnretainedValue(), imageBuffer, nil, MTLPixelFormat.BGRA8Unorm, width, height, 0, &textureRef)

これが役立つことを願っています!

于 2015-07-06T09:45:03.120 に答える