명령 버퍼에 waitUntilCompleted()
을 호출하면 CPU에서 커널 결과를 사용해야 할 경우 유용하지만 단순히 명령 (디스패치)의 결과를 사용하려는 경우 불필요하고 비효율적입니다. 후속 계산 명령. compute 명령간에 데이터 종속성이있는 경우 전자로 작성된 결과는 단일 명령 버퍼 내에서도 해당 명령에서 볼 수 있도록 보장됩니다. 그래서 다음과 같이 구조화 할 수 있습니다 :
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
성능 향상을 위해 MBE의 이미지 필터링 장에 적용 할 수 있습니까? 내가 올바르게 기억하고 있다면, 연결된 각 필터에는 자체 명령 인코더가 있습니까? –
인코더 자체는 매우 저렴하지만 참조하는 샘플에는이 질문에서 설명한 문제가 있습니다. 모든 디스패치에 대해 명령 버퍼를 만들고 CPU가 결과를 동 기적으로 제공 할 수 있도록 대기합니다. 뒤늦은 지혜와 Metal Performance Shaders의 장점을 살려서 내가 다시하고 있다면, 비동기로 만들고 여러 필터가 동일한 버퍼에 작업을 인코딩하도록 할 것입니다. 라이브로 배우십시오. – warrenm