如何在iOS版Metal中链接过滤器? [英] How to chain filters in Metal for iOS?

查看:104
本文介绍了如何在iOS版Metal中链接过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I completed this tutorial by Simon Gladman (@flexmonkey) to capture images from AVFoundation and apply a filter to the output. However, I'm struggling to find a way to replace the blur filter with my own compute shader. In other words, I need to concatenate my custom shader after the YCbCrColorConversion filter mentioned there.

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来编译第二个内核函数吗?这会将第一个过滤器的输出作为第二个过滤器的输入.有没有更有效的方法可以做到这一点,或者这是最佳选择?

Should I create a new commandBuffer, commandEncoder and a separate pipelineState that compiles the second kernel function? This would take the output of the first filter as an input to the second. Is there a more efficient way to do this, or this is optimal?

我是Metal的初学者,因此非常感谢您对该管道的工作方式进行任何解释.

I'm a beginner with Metal, so any explanations on how the pipeline works are highly appreciated.

推荐答案

您不需要创建新的命令缓冲区或其他计算编码器,但是需要创建使用自己的内核函数的计算管道状态.无论当前正在创建YCbCr转换管道状态的任何位置,都应执行一次此操作.

You don't need to create a new command buffer or another compute encoder, but you do need to create a compute pipeline state that uses your own kernel function. You should do this once, during initialization, wherever you're currently creating the YCbCr conversion pipeline state.

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

To chain the effects together, you'll need to create an intermediate texture that acts as the output texture of the YCbCr conversion and the input of your kernel. The drawable texture will then be the output texture of your kernel function. You can dispatch the work for your own kernel just as you're currently dispatching the work for the YCbCr conversion (i.e. with the same number of threads per threadgroup and threadgroup count).

中间纹理应该与可绘制对象具有相同的尺寸和格式.您可以懒惰地创建它并保留对其的引用,然后在可绘制尺寸更改时重新创建它.

The intermediate texture should probably be of the same dimensions and format as the drawable. You can create it lazily and hold a reference to it, re-creating it when the drawable size changes.

这篇关于如何在iOS版Metal中链接过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆