OGeek|极客世界-中国程序员成长平台

标题: ios - 如何将 CVPixelBuffer 设置为顶点着色器的输入? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:11
标题: ios - 如何将 CVPixelBuffer 设置为顶点着色器的输入?

我想使用 Apple Metal 渲染路径来处理 CVPixelBuffer。

如何转换 CVPixelBuffer 使其符合顶点着色器的输入? 不知道如何从 CVPixelBuffer 中提取颜色/位置值,以便我可以从主机设置它们。



Best Answer-推荐答案


这是我用来将 CVPixelBuffer 数据转换为 Metal 纹理的代码。

#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate

- (void)captureOutputAVCaptureOutput *)captureOutput didOutputSampleBufferCMSampleBufferRef)sampleBuffer fromConnectionAVCaptureConnection *)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。如果您需要这方面的帮助,请在评论中告诉我。

关于ios - 如何将 CVPixelBuffer 设置为顶点着色器的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26568409/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4