我最近使用乒乓球缓冲区和计算着色器在Metal中实现了一个水模拟。它工作得很好,但我只是想知道交换几次纹理的成本是多少,以及是否可以通过使用Blit命令编码器来改善这一点?
以下是代码的示例:
let computeEncoder = commandBuffer.makeComputeCommandEncoder()!
computeEncoder.setComputePipelineState(pipelineState)
computeEncoder.setTexture(textureA, index: 0)
computeEncoder.setTexture(textureB, index: 1)
var width = pipelineState.threadExecutionWidth
var height = pipelineState.maxTotalThreadsPerThreadgroup / width
let threadsPerThreadGroup = MTLSizeMake(width, height, 1)
width = Int(textureA.width)
height = Int(textureA.height)
let threadsPerGrid = MTLSizeMake(width, height, 1)
computeEncoder.dispatchThreads(threadsPerGrid, threadsPerThreadgroup: threadsPerThreadGroup)
computeEncoder.endEncoding()
swap(&textureA, &textureB)
发布于 2020-02-02 18:48:30
这种方法工作得很好:
let blitEncoder = commandBuffer.makeBlitCommandEncoder()
blitEncoder?.copy(from: textureA, to: temp)
blitEncoder?.copy(from: textureB, to: textureA)
blitEncoder?.copy(from: temp, to: textureB)
blitEncoder?.endEncoding()
我还没有做任何测试来看这是否会提高性能,尽管我假设是这样的。我会发布结果,一旦我有!
https://stackoverflow.com/questions/60010598
复制