A transient, single-use buffer that stores encoded commands which are committed to and executed by the GPU.
- iOS 8.0+
- macOS 10.11+
- tvOS 8.0+
Your app does not define classes that implement this protocol. Command buffer objects are created by a
MTLCommandQueue object and can only be committed for execution on the command queue that created it. All command buffers sent to a single command queue are guaranteed to execute in the order in which the command buffers were enqueued. In most cases, you create a command buffer by calling a
After creating a command buffer, you create an encoder object to fill the buffer with commands. The
MTLCommandBuffer protocol has methods to create specific types of command encoders:
MTLParallelRenderCommandEncoder. At any given time, only a single encoder may be active for a particular command buffer. You create an encoder, use it to add commands to the buffer, and then end the encoding process. After you finish with an encoder, you can create another encoder and continue to add commands to the same buffer. When you are ready to execute the set of encoded commands, you call the command buffer’s
commit() method to schedule the buffer for execution.
After a command buffer has been committed for execution, the only valid operations on the command buffer are to wait for it to be scheduled or completed (using synchronous calls or handler blocks) and to check the status of the command buffer execution. When used, scheduled and completed handlers are blocks that are invoked in execution order. These handlers should perform quickly; if expensive or blocking work needs to be scheduled, defer that work to another thread.
In a multithreaded app, it’s advisable to break your overall task into subtasks that can be encoded separately. Create a command buffer for each chunk of work, then call the
enqueue() method on these command buffer objects to establish the order of execution. Fill each buffer object (using multiple threads) and commit them. The command queue automatically schedules and executes these command buffers as they become available.