Instance Method

addCompletedHandler(_:)

Registers a block of code that Metal calls immediately after the GPU finishes executing the commands in the command buffer.

Required.

Declaration

func addCompletedHandler(_ block: @escaping MTLCommandBufferHandler)

Parameters

block

A block of code to be invoked.

Discussion

You can register multiple handlers for the same command buffer. You can’t add a completion handler after you commit the command buffer.

The buffer calls the completion handlers after it finishes execution. Check the status property to determine whether execution was successful or had errors.

Listing 1 demonstrates how to use the gpuEndTime and gpuStartTime properties to determine how long it took the GPU to execute the command buffer.

Listing 1

Obtaining timestamp reflection data

commandBuffer.addCompletedHandler { cb in
    let executionDuration = cb.gpuEndTime - cb.gpuStartTime
    /* ... */
}

See Also

Scheduling and Executing Commands

func enqueue()

Reserves a place for the command buffer on the associated command queue.

Required.

func commit()

Commits the command buffer for execution.

Required.

func addScheduledHandler(MTLCommandBufferHandler)

Registers a block of code that Metal calls immediately after it schedules the command buffer for execution on the GPU.

Required.

func waitUntilScheduled()

Blocks execution of the current thread until the command buffer is scheduled.

Required.

func waitUntilCompleted()

Blocks execution of the current thread until execution of the command buffer is completed.

Required.

typealias MTLCommandBufferHandler

A block of code invoked when a command buffer is scheduled for execution or has completed execution.