假设我在金属中分配两个计算着色器A和B。我不想B在A完成之前跑。目前,我正在自己的命令缓冲区中对每个着色器进行编码,并按如下方式提交:
commandBufferA.commit()
commandBufferA.waitUntilCompleted()
commandBufferB.commit()这是正确的技术吗?
发布于 2016-11-18 21:25:49
如果需要在CPU上使用内核的结果,那么在命令缓冲区上调用waitUntilCompleted()是很有用的,但是如果您的目的只是要在后续的计算命令中使用计算命令(调度)的结果,则是不必要的和低效的。如果计算命令之间存在数据依赖关系,则前者编写的结果可以保证对后者可见,即使在单个命令缓冲区中也是如此。所以,你可以这样构造它:
let commandBuffer = commandQueue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
commandEncoder.setComputePipelineState(pipelineStateA)
commandEncoder.setTexture(inputTexture, at: 0)
commandEncoder.setTexture(intermediateTexture, at: 1)
commandEncoder.dispatchThreadgroups(threadgroupCount,
threadsPerThreadgroup: threadgroupSize)
commandEncoder.setComputePipelineState(pipelineStateB)
commandEncoder.setTexture(intermediateTexture, at: 0)
commandEncoder.setTexture(outputTexture, at: 1)
commandEncoder.dispatchThreadgroups(threadgroupCount,
threadsPerThreadgroup: threadgroupSize)
commandEncoder.endEncoding()
commandBuffer.commit()
commandBuffer.waitUntilCompleted() // optional; only if you need to read the result on the CPUhttps://stackoverflow.com/questions/40685098
复制相似问题