glo*_*loo 2 compute-shader swift metal
假设我在金属中分派了两个计算着色器 A 和 B。我不希望 B 在 A 完成之前运行。目前,我在自己的命令缓冲区中对每个着色器进行编码,并像这样提交:
commandBufferA.commit()
commandBufferA.waitUntilCompleted()
commandBufferB.commit()
Run Code Online (Sandbox Code Playgroud)
这是正确的技术吗?
waitUntilCompleted()如果您需要在 CPU 上使用内核的结果,则调用命令缓冲区很有用,但如果您的目的只是在后续计算命令中使用计算命令(调度)的结果,则调用命令缓冲区是不必要且低效的。如果计算命令之间存在数据依赖性,则保证前者写入的结果对后者可见,即使在单个命令缓冲区中也是如此。所以,你可以像这样构建它:
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 CPU
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
630 次 |
| 最近记录: |