【发布时间】:2014-10-25 23:46:45
【问题描述】:
我想使用 Apple Metal 渲染路径来处理 CVPixelBuffer。
如何转换 CVPixelBuffer 使其符合顶点着色器的输入? 不知道如何从 CVPixelBuffer 中提取颜色/位置值,以便我可以从主机设置它们。
【问题讨论】:
-
顶点缓冲区通常包含 3D 对象的 X、Y、Z 坐标,而不是像素的 RGB 值。也许您想改为创建纹理?
我想使用 Apple Metal 渲染路径来处理 CVPixelBuffer。
如何转换 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。如果您需要这方面的帮助,请在 cmets 中告诉我。
【讨论】:
类似这样的:
vid 将从 0 变为 width*height。然后,您可以根据 vid 的值对纹理进行采样。
记住:
【讨论】: