2016-11-18 5 views
2

두 개의 계산 쉐이더 A와 B를 메탈에 디스패치한다고 가정합니다. 나는 A가 끝날 때까지 B가 뛰길 원하지 않는다. 현재 각 셰이더를 자신의 명령 버퍼에 인코딩하고 다음과 같이 커밋하고 있습니다.다음 계산 쉐이더에서 하나의 계산 쉐이더 결과를 사용하는 적절한 방법은 무엇입니까

commandBufferA.commit() 
commandBufferA.waitUntilCompleted() 
commandBufferB.commit() 

적절한 방법입니까?

답변

2

명령 버퍼에 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 
+0

성능 향상을 위해 MBE의 이미지 필터링 장에 적용 할 수 있습니까? 내가 올바르게 기억하고 있다면, 연결된 각 필터에는 자체 명령 인코더가 있습니까? –

+0

인코더 자체는 매우 저렴하지만 참조하는 샘플에는이 질문에서 설명한 문제가 있습니다. 모든 디스패치에 대해 명령 버퍼를 만들고 CPU가 결과를 동 기적으로 제공 할 수 있도록 대기합니다. 뒤늦은 지혜와 Metal Performance Shaders의 장점을 살려서 내가 다시하고 있다면, 비동기로 만들고 여러 필터가 동일한 버퍼에 작업을 인코딩하도록 할 것입니다. 라이브로 배우십시오. – warrenm