Instance Method

addScheduledHandler(_:)

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

Required.

Declaration

func addScheduledHandler(_ 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 schedule handler after you commit the command buffer.

The device object schedules the command buffer after it identifies any dependencies with work tasks submitted by other command buffers or other APIs in the system. During the execution of the completion handler, the command buffer status is at least MTLCommandBufferStatus.scheduled.

Listing 1 demonstrates how to use the kernelEndTime and kernelStartTime properties to determine how long it took the CPU to schedule the command buffer.

Listing 1

Obtaining timestamp reflection data

commandBuffer.addScheduledHandler { cb in
    let scheduleDuration = cb.kernelEndTime - cb.kernelStartTime
    /* ... */
}

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 addCompletedHandler(MTLCommandBufferHandler)

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

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.