ios - 如何将 CVPixelBuffer 设置为顶点着色器的输入?

标签 ios rendering metal

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

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

最佳答案

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

#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。如果您需要帮助,请在评论中告诉我。

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

相关文章:

ios - 使用MTKTextureLoader加载远程图像

ios - 与 NSArray 的指针转换不兼容的整数

ios - 坚持一个 RLMObject

javascript - 全日历事件呈现性能问题

android - 如何使用 android api V2 在 GoogleMap 上绘制数千个正方形

metal - 如何在 Metal 顶点着色器 MTL 顶点属性描述符中描述 packed_float3?

ios - 关于使用 eventIdentifier 以编程方式获取 EKEvent 的问题

ios - 如何使用 iOS Swift 在 REST Web 服务 URL 上应用 Cookie

javascript - IE 不正确地呈现动态内容,直到进行样式表更改

ios - 未找到 Metal 内核函数