【发布时间】:2016-02-29 15:13:23
【问题描述】:
我有一个 MPSImageGaussianBlur 对象在计算通道的每一帧上工作(模糊中间纹理的内容)。
虽然应用程序仍然以 60fps 运行没问题,但我发现启用模糊通道后 CPU 使用率增加了约 15%。我想知道这是否正常?
我只是想知道MPSImageGaussianBlur 的encodeToCommandBuffer: 操作的后台会发生什么,它会看到如此多的 CPU 利用率。在我(虽然很天真)的理解中,我想会有一些简单的编码如下:
MPSImageGaussianBlur.encodeToCommandBuffer:伪方法:
func encodeToCommandBuffer(commandBuffer: MTLCommandBuffer, sourceTexture: MTLTexture, destinationTexture: MTLTexture) {
let encoder = commandBuffer.computeCommandEncoder()
encoder.setComputePipelineState(...)
encoder.setTexture(sourceTexture, atIndex: 0)
encoder.setTexture(destinationTexture, atIndex: 1)
// kernel weights would be built at initialization and
// present here as a `kernelWeights` property
encoder.setTexture(self.kernelWeights, atIndex: 2)
let threadgroupsPerGrid = ...
let threadsPerThreadgroup = ...
encoder.dispatchThreadgroups(threadgroupsPerGrid, threadsPerThreadgroup: threadsPerThreadgroup)
encoder.endEncoding()
}
大部分“性能魔法”将在计算内核函数中运行的算法上实现。我可以欣赏这一点,因为性能(在 GPU 上)非常棒,与我初始化 MPSImageGaussianBlur 的 blurRadius 无关。
一些可能与我的具体设置无关的细节:
-
MPSImageGaussianBlur初始化为模糊半径 8 像素。 - 我要模糊的纹理是 128 x 128 像素。
- 在 MTKViewDelegate 的
drawInMTKView:方法中执行所有渲染。
我希望这个问题的意图有点清楚。
【问题讨论】:
-
为了确定,您只创建了一个
MPSImageGaussianBlur实例,并跨帧重复使用它,对吗? -
是的,只有一个实例
-
您在哪个设备和 iOS 版本上看到此行为?
-
我在装有 iOS 9.3 的 iPhone 6 上运行它。我正在使用 Xcode 7.3 beta 4 并跟踪仪器和调试导航器中的 CPU 使用情况。感谢您对此进行调查!
-
更多细节:当您说~15% 时,您是说增加 15 个百分点,还是增加 15% 的 CPU 使用率?如果是后者,我会说这是意料之中的。如果是前者,这听起来像是一个相当大的倒退。您的典型模糊半径 (sigma) 是多少?
标签: metal