【问题标题】:How to chain filters in Metal for iOS?如何在 iOS 的 Metal 中链接过滤器?
【发布时间】:2016-10-20 18:33:54
【问题描述】:

我完成了 Simon Gladman (@flexmonkey) 的 this tutorial 以从 AVFoundation 捕获图像并将过滤器应用于输出。但是,我正在努力寻找一种方法来用我自己的计算着色器替换模糊滤镜。换句话说,我需要在此处提到的YCbCrColorConversion 过滤器之后连接我的自定义着色器。

let commandBuffer = commandQueue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
// pipelineState has compiled YCbCrColorConversion filter
commandEncoder.setComputePipelineState(pipelineState)
commandEncoder.setTexture(ytexture, at: 0)
commandEncoder.setTexture(cbcrTexture, at: 1)
commandEncoder.setTexture(drawable.texture, at: 2) // out texture

commandEncoder.dispatchThreadgroups(threadGroups,threadsPerThreadgroup: threadGroupCount)
commandEncoder.endEncoding()

let inPlaceTexture = UnsafeMutablePointer<MTLTexture> .allocate(capacity: 1)
        inPlaceTexture.initialize(to: drawable.texture)

// How to replace this blur with my own filter?????
blur.encodeToCommandBuffer(commandBuffer, inPlaceTexture: inPlaceTexture, fallbackCopyAllocator: nil)

commandBuffer.presentDrawable(drawable)
commandBuffer.commit();

我应该创建一个新的 commandBuffer、commandEncoder 和一个单独的 pipelineState 来编译第二个内核函数吗?这会将第一个过滤器的输出作为第二个过滤器的输入。有没有更有效的方法来做到这一点,或者这是最优的?

我是 Metal 的初学者,因此非常感谢您对管道如何工作的任何解释。

【问题讨论】:

    标签: ios swift ios10 metal


    【解决方案1】:

    您无需创建新的命令缓冲区或其他计算编码器,但您需要创建使用您自己的内核函数的计算管道状态。您应该在初始化期间执行一次,无论您当前正在创建 YCbCr 转换管道状态的何处。

    要将效果链接在一起,您需要创建一个中间纹理,作为 YCbCr 转换的输出纹理和内核的输入。然后,可绘制纹理将成为内核函数的输出纹理。您可以为自己的内核分派工作,就像您当前为 YCbCr 转换分派工作一样(即,每个线程组的线程数和线程组计数相同)。

    中间纹理应该与可绘制对象具有相同的尺寸和格式。您可以懒惰地创建它并持有对它的引用,当可绘制大小更改时重新创建它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 2017-02-10
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      相关资源
      最近更新 更多