【发布时间】:2019-05-22 18:03:06
【问题描述】:
在尝试将简单的晕影滤镜应用于 iPhone6 的原始相机馈送时,在 Metal 和 Core Image 的帮助下,我发现在 MTKView 中处理和渲染的帧之间存在很多延迟
我遵循的方法是(MetalViewController.swift):
- 使用
AVCaptureVideoDataOutputSampleBufferDelegate获取原始相机输出 - 转换
CMSampleBuffer>CVPixelBuffer>CGImage - 用这个
CGImage创建一个MTLTexture。
点数2 和 3 在名为:fillMTLTextureToStoreTheImageData
- 将
CIFilter应用于从MTKViewDelegate中的MTLTexture获取的CIImage
func draw(in view: MTKView) {
if let currentDrawable = view.currentDrawable {
let commandBuffer = self.commandQueue.makeCommandBuffer()
if let myTexture = self.sourceTexture{
let inputImage = CIImage(mtlTexture: myTexture, options: nil)
self.vignetteEffect.setValue(inputImage, forKey: kCIInputImageKey)
self.coreImageContext.render(self.vignetteEffect.outputImage!, to: currentDrawable.texture, commandBuffer: commandBuffer, bounds: inputImage!.extent, colorSpace: self.colorSpace)
commandBuffer?.present(currentDrawable)
commandBuffer?.commit()
}
}
}
我错过了什么吗?
【问题讨论】:
-
我对相机部分一无所知,但这里有一个基于金属的快速晕影滤镜:github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…
-
@matt 我想我已经在没有金属的情况下实现了这一点。仅通过 Core Image 本身将 CIFiilter 应用于图像非常快。当我们尝试处理视频中的每个原始帧时,事情就会失控。为了解决这个问题,我使用了金属,但它没有按预期工作。
-
也许金属在这里是一个红鲱鱼。 Apple 有示例代码展示了如何将 CIFilter 效果应用于视频,尽管我倾向于怀疑它是否可以跟上 live 视频。
-
如果您看到我的问题中的链接,苹果明确表示实时渲染实时帧需要金属或 opengl。我遵循了相同的步骤。但我认为苹果提供了一个非常基本的实现,忽略了性能和开销
-
是的,这可能是真的。我只能找到关于这个主题的 FunHouse 示例代码和 WWDC 2013 session 509 视频。
标签: ios avfoundation core-image cifilter metalkit