将 MTLTexture 转换为 CVPixelBuffer

2024-01-10

我目前正在使用 Metal 开发实时滤波器。 定义 CIImage 后,我将图像渲染到 MTLTexture。

下面是我的渲染代码。context是由 Metal 支持的 CIContext;targetTexture是附加到的纹理的别名currentDrawable我的 MTKView 实例的属性:

context?.render(drawImage, to: targetTexture, commandBuffer: commandBuffer, bounds: targetRect, colorSpace: colorSpace)

它渲染正确,因为我可以看到金属视图上显示的图像。

问题是,在渲染图像(并显示它)之后,我想提取 CVPixelBuffer 并使用 AVAssetWriter 类将其保存到磁盘。

另一种替代方案是有两个渲染步骤,一个渲染到纹理,另一个渲染到 CVPixelBuffer。 (但尚不清楚如何创建这样的缓冲区,或者两个渲染步骤对帧速率的影响)

任何帮助将不胜感激,谢谢!


您可以尝试从 MTLTexture 复制原始数据,如下所示:

var outPixelbuffer: CVPixelBuffer?
if let datas = targetTexture.texture.buffer?.contents() {
    CVPixelBufferCreateWithBytes(kCFAllocatorDefault, targetTexture.width, 
    targetTexture.height, kCVPixelFormatType_64RGBAHalf, datas, 
    targetTexture.texture.bufferBytesPerRow, nil, nil, nil, &outPixelbuffer);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 MTLTexture 转换为 CVPixelBuffer 的相关文章

随机推荐