Search code examples
swiftgpgpumetal

Metal Compute - enqueue an already enqueued command buffer


I'm very new to Metal and I am experimenting with its compute functionalities. I have the following piece of code written in Swift 3 on macOS:

repeat
{ 
    metalCommandBuffer.enqueue()
    metalCommandBuffer.commit()
    metalCommandBuffer.waitUntilCompleted()
    copy variable back to CPU
}
while {check the variable == 0}

I want to execute a compute kernel, copy the variable back to the host and check if some condition is met. If it is then continue to execute the kernel until otherwise.

However I get the following error:

error 'enqueue an already enqueued command buffer'

I have tried just having the commit and waitUntilCompleted inside the loop but then I get:

error 'commit an already committed command buffer'

I have a bit of CUDA knowledge and as a result of that I may be somewhat misusing Metal.
Thanks.


Solution

  • You can't reuse a command buffer like that.

    You have to create a new command buffer, create a new compute command encoder, encode the command(s), end encoding, and then commit the command buffer every time through the loop.