CVPixelBuffer の処理に Apple Metal レンダー パスを使用したいと考えています。
頂点シェーダーの入力として適合するように CVPixelBuffer を変換するにはどうすればよいですか? CVPixelBuffer から色/位置の値を抽出する方法がわからないので、ホストから設定できます。
CVPixelBuffer データをメタル テクスチャに変換するために使用するコードを次に示します。
#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
id<MTLTexture> texture = nil;
{
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
MTLPixelFormat pixelFormat = MTLPixelFormatBGRA8Unorm;
CVMetalTextureRef metalTextureRef = NULL;
CVReturn status = CVMetalTextureCacheCreateTextureFromImage(NULL, _textureCache, pixelBuffer, NULL, pixelFormat, width, height, 0, &metalTextureRef);
if(status == kCVReturnSuccess)
{
texture = CVMetalTextureGetTexture(metalTextureRef);
if (self.delegate){
[self.delegate textureUpdated:texture];
}
CFRelease(metalTextureRef);
}
}
}
テクスチャを取得したら、それを renderCommandEncoder に渡すだけです。それについて助けが必要な場合は、コメントでお知らせください。