<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Metal",
  "identifier" : "/documentation/Metal/MTLComputeCommandEncoder",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Metal"
    ],
    "preciseIdentifier" : "c:objc(pl)MTLComputeCommandEncoder"
  },
  "title" : "MTLComputeCommandEncoder"
}
-->

# MTLComputeCommandEncoder

Encodes computation dispatch commands for a single compute pass into a command buffer.

```
protocol MTLComputeCommandEncoder : MTLCommandEncoder
```

## Overview

Create a compute encoder by calling one of the factory methods on an [`MTLCommandBuffer`](/documentation/Metal/MTLCommandBuffer) instance,
such as [`makeComputeCommandEncoder(dispatchType:)`](/documentation/Metal/MTLCommandBuffer/makeComputeCommandEncoder(dispatchType:)).
You can encode multiple commands that each run a compute kernel as part of a single pass of the encoder
with the following steps:

1. Configure an [`MTLComputePipelineState`](/documentation/Metal/MTLComputePipelineState) instance with a kernel, using a method such as [`makeComputePipelineState(function:)`](/documentation/Metal/MTLDevice/makeComputePipelineState(function:)). See the [Creating compute pipeline states](/documentation/Metal/pipeline-state-creation#Creating-compute-pipeline-states) section of [Pipeline state creation](/documentation/Metal/pipeline-state-creation) for all [`MTLDevice`](/documentation/Metal/MTLDevice) methods that create a new pipeline state for your command encoder.
2. Set the pipeline state with the [`setComputePipelineState(_:)`](/documentation/Metal/MTLComputeCommandEncoder/setComputePipelineState(_:)) method on your command encoder.
3. Set kernel arguments by binding buffers, textures, and other resources with methods such as [`setBuffer(_:offset:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setBuffer(_:offset:index:)) and [`setTexture(_:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setTexture(_:index:)).
4. Encode compute commands that call your kernel by either [`Dispatching kernel calls directly`](/documentation/Metal/MTLComputeCommandEncoder#Dispatching-kernel-calls-directly) or [`Dispatching from indirect command buffers`](/documentation/Metal/MTLComputeCommandEncoder#Dispatching-from-indirect-command-buffers).
5. Call [`endEncoding()`](/documentation/Metal/MTLCommandEncoder/endEncoding()) to finish encoding the kernel call of the compute pass.

### Command stages

Most compute commands apply to one stage within a pass.
The following table shows which stage applies to each command:

|Function                                                                                                                                                                                                                                                              |MTLStages                                                       |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------|
|``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder/dispatchThreads(_:threadsPerThreadgroup:)``                                                                                                                                                      |``doc://com.apple.metal/documentation/Metal/MTLStages/dispatch``|
|``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder/dispatchThreadgroups(_:threadsPerThreadgroup:)``                                                                                                                                                 |``doc://com.apple.metal/documentation/Metal/MTLStages/dispatch``|
|``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder/dispatchThreadgroups(indirectBuffer:indirectBufferOffset:threadsPerThreadgroup:)``                                                                                                               |``doc://com.apple.metal/documentation/Metal/MTLStages/dispatch``|
|``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder/executeCommandsInBuffer(_:range:)``![](spacer)``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder/executeCommandsInBuffer:withRange:``                                          |None                                                            |
|``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder/executeCommandsInBuffer(_:indirectBuffer:offset:)``![](spacer)``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder/executeCommandsInBuffer:indirectBuffer:indirectBufferOffset:``|None                                                            |
|``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder/sampleCounters(sampleBuffer:sampleIndex:barrier:)``                                                                                                                                              |None                                                            |

The [`executeCommandsInBuffer(_:range:)`](/documentation/Metal/MTLComputeCommandEncoder/executeCommandsInBuffer(_:range:)) and [`executeCommandsInBuffer(_:indirectBuffer:offset:)`](/documentation/Metal/MTLComputeCommandEncoder/executeCommandsInBuffer(_:indirectBuffer:offset:)) commands don’t apply to any stage,
which means you can’t use a barrier to wait for all commands in an indirect command buffer to complete.
However, each command within the [`MTLIndirectCommandBuffer`](/documentation/Metal/MTLIndirectCommandBuffer) applies to the same stages as when you encode the equivalent command directly.

For more information about stages and synchronization, see [`MTLStages`](/documentation/Metal/MTLStages) and [Resource synchronization](/documentation/Metal/resource-synchronization).

## Topics

### Configuring the pipeline state

Configure a compute pipeline state to describe the runtime environment of an encoder.

[`setComputePipelineState(_:)`](/documentation/Metal/MTLComputeCommandEncoder/setComputePipelineState(_:))

Configures the compute encoder with a pipeline state for subsequent kernel calls.

[`dispatchType`](/documentation/Metal/MTLComputeCommandEncoder/dispatchType)

The dispatch type to use when submitting compute work to the GPU.

### Binding buffers

Bind buffers to kernel argument entries on the GPU.

[`setBuffer(_:offset:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setBuffer(_:offset:index:))

Binds a buffer to the buffer argument table, allowing compute kernels to access its data on the GPU.

[`setBuffer(_:offset:attributeStride:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setBuffer(_:offset:attributeStride:index:))

Binds a buffer with a stride to the buffer argument table, allowing compute kernels to access its data on the GPU.

[`setBuffers(_:offsets:range:)`](/documentation/Metal/MTLComputeCommandEncoder/setBuffers(_:offsets:range:))

Binds multiple buffers to the buffer argument table at once, allowing compute kernels to access their data on the GPU.

[`setBuffers(_:offsets:attributeStrides:range:)`](/documentation/Metal/MTLComputeCommandEncoder/setBuffers(_:offsets:attributeStrides:range:))

Binds multiple buffers with data in stride to the buffer argument table at once, allowing compute kernels to access their data on the GPU.

[`setBuffers:offsets:withRange:`](/documentation/Metal/MTLComputeCommandEncoder/setBuffers:offsets:withRange:)

Binds multiple buffers to the buffer argument table at once, allowing compute kernels to access their data on the GPU.

[`setBuffers:offsets:attributeStrides:withRange:`](/documentation/Metal/MTLComputeCommandEncoder/setBuffers:offsets:attributeStrides:withRange:)

Binds multiple buffers with data in stride to the buffer argument table at once, allowing compute kernels to access their data on the GPU.

[`setBufferOffset(_:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setBufferOffset(_:index:))

Changes where the data begins in a buffer already bound to the buffer argument table.

[`setBufferOffset(offset:attributeStride:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setBufferOffset(offset:attributeStride:index:))

Changes where the data begins and the distance between adjacent elements in a buffer already bound to the buffer argument table.

### Binding raw bytes

Bind copies of raw bytes to kernel argument entries on the GPU, up to 4 KB.

[`setBytes(_:length:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setBytes(_:length:index:))

Copies data directly to the GPU to populate an entry in the buffer argument table.

[`setBytes(_:length:attributeStride:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setBytes(_:length:attributeStride:index:))

Copies data with a given stride directly to the GPU to populate an entry in the buffer argument table.

### Binding textures

Bind textures to kernel argument entries on the GPU.

[`setTexture(_:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setTexture(_:index:))

Binds a texture to the texture argument table, allowing compute kernels to access its data on the GPU.

[`setTextures(_:range:)`](/documentation/Metal/MTLComputeCommandEncoder/setTextures(_:range:))

Binds multiple textures to the texture argument table, allowing compute functions to access their data on the GPU.

[`setTextures:withRange:`](/documentation/Metal/MTLComputeCommandEncoder/setTextures:withRange:)

Binds multiple textures to the texture argument table, allowing compute kernels to access their data on the GPU.

### Binding texture samplers

Bind texture samplers to kernel argument entries on the GPU.

[`setSamplerState(_:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setSamplerState(_:index:))

Encodes a texture sampler, allowing compute kernels to use it for sampling textures on the GPU.

[`setSamplerState(_:lodMinClamp:lodMaxClamp:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setSamplerState(_:lodMinClamp:lodMaxClamp:index:))

Encodes a texture sampler with a custom level of detail clamping, allowing compute kernels to use it for sampling textures on the GPU.

[`setSamplerStates(_:range:)`](/documentation/Metal/MTLComputeCommandEncoder/setSamplerStates(_:range:))

Encodes multiple texture samplers to the sampler argument table, allowing compute kernels to use them for sampling textures on the GPU.

[`setSamplerStates(_:lodMinClamps:lodMaxClamps:range:)`](/documentation/Metal/MTLComputeCommandEncoder/setSamplerStates(_:lodMinClamps:lodMaxClamps:range:))

Encodes multiple texture samplers for the compute function, specifying clamp values for the level of detail of each sampler.

[`setSamplerStates:withRange:`](/documentation/Metal/MTLComputeCommandEncoder/setSamplerStates:withRange:)

Encodes multiple texture samplers, allowing compute kernels to use them for sampling textures on the GPU.

[`setSamplerStates:lodMinClamps:lodMaxClamps:withRange:`](/documentation/Metal/MTLComputeCommandEncoder/setSamplerStates:lodMinClamps:lodMaxClamps:withRange:)

Encodes multiple texture samplers with custom levels of detail clamping, allowing compute kernels to use them for sampling textures on the GPU.

### Binding function tables

Bind tables of function pointers to kernel argument entries on the GPU.

[`setVisibleFunctionTable(_:bufferIndex:)`](/documentation/Metal/MTLComputeCommandEncoder/setVisibleFunctionTable(_:bufferIndex:))

Binds a visible function table to the buffer argument table, allowing you to call its functions on the GPU.

[`setVisibleFunctionTables(_:bufferRange:)`](/documentation/Metal/MTLComputeCommandEncoder/setVisibleFunctionTables(_:bufferRange:))

Binds multiple visible function tables to the buffer argument table, allowing you to call their functions on the GPU.

[`setIntersectionFunctionTables(_:bufferRange:)`](/documentation/Metal/MTLComputeCommandEncoder/setIntersectionFunctionTables(_:bufferRange:))

Binds multiple intersection function tables to the buffer argument table, allowing you to call their functions on the GPU.

[`setVisibleFunctionTables:withBufferRange:`](/documentation/Metal/MTLComputeCommandEncoder/setVisibleFunctionTables:withBufferRange:)

Binds multiple visible function tables to the buffer argument table, allowing you to call their functions on the GPU.

[`setIntersectionFunctionTables:withBufferRange:`](/documentation/Metal/MTLComputeCommandEncoder/setIntersectionFunctionTables:withBufferRange:)

Binds multiple intersection function tables to the buffer argument table, allowing you to call their functions on the GPU.

### Binding arguments for acceleration structures

Bind acceleration structures to kernel argument entries on the GPU.

[`setAccelerationStructure(_:bufferIndex:)`](/documentation/Metal/MTLComputeCommandEncoder/setAccelerationStructure(_:bufferIndex:))

Binds an acceleration structure to the buffer argument table, allowing functions to access it on the GPU.

[`setIntersectionFunctionTable(_:bufferIndex:)`](/documentation/Metal/MTLComputeCommandEncoder/setIntersectionFunctionTable(_:bufferIndex:))

Binds an intersection function table to the buffer argument table, making it callable in your Metal shaders.

### Making indirect resources resident

Move data for resources without direct bindings into GPU-accessible memory — such as resources the pass accesses through an argument buffer.

[`useResource(_:usage:)`](/documentation/Metal/MTLComputeCommandEncoder/useResource(_:usage:))

Ensures kernel calls that the system encodes in subsequent commands have access to a resource.

[`useResources(_:usage:)`](/documentation/Metal/MTLComputeCommandEncoder/useResources(_:usage:))

Ensures kernel calls that the system encodes in subsequent commands have access to multiple resources.

[`useResources:count:usage:`](/documentation/Metal/MTLComputeCommandEncoder/useResources:count:usage:)

Ensures kernel calls that the system encodes in subsequent commands have access to multiple resources.

[`useHeap(_:)`](/documentation/Metal/MTLComputeCommandEncoder/useHeap(_:))

Ensures the shaders in the render pass’s subsequent draw commands have access to all of the resources you allocate from a heap.

[`useHeaps(_:)`](/documentation/Metal/MTLComputeCommandEncoder/useHeaps(_:))

Ensures the shaders in the render pass’s subsequent draw commands have access to all of the resources you allocate from multiple heaps.

[`useHeaps:count:`](/documentation/Metal/MTLComputeCommandEncoder/useHeaps:count:)

Ensures the shaders in the render pass’s subsequent draw commands have access to all of the resources you allocate from multiple heaps.

### Configuring tile memory

Reserve space in tile memory for threadgroups and imageblocks.

[`setThreadgroupMemoryLength(_:index:)`](/documentation/Metal/MTLComputeCommandEncoder/setThreadgroupMemoryLength(_:index:))

Configures the size of a block of threadgroup memory.

[`setImageblockWidth(_:height:)`](/documentation/Metal/MTLComputeCommandEncoder/setImageblockWidth(_:height:))

Sets the size, in pixels, of imageblock data in tile memory.

### Configuring stage-in data

Set per-thread input data for compute kernels.

[`setStageInRegion(_:)`](/documentation/Metal/MTLComputeCommandEncoder/setStageInRegion(_:))

Sets the dimensions over the thread grid of how your compute kernel receives stage-in arguments.

[`setStageInRegionWithIndirectBuffer(_:indirectBufferOffset:)`](/documentation/Metal/MTLComputeCommandEncoder/setStageInRegionWithIndirectBuffer(_:indirectBufferOffset:))

Sets the region of the stage-in attributes to apply to a compute kernel using an indirect buffer.

### Dispatching kernel calls directly

Run compute kernels as part of a compute pass.

[`dispatchThreads(_:threadsPerThreadgroup:)`](/documentation/Metal/MTLComputeCommandEncoder/dispatchThreads(_:threadsPerThreadgroup:))

Encodes a compute command using an arbitrarily sized grid.

[`dispatchThreadgroups(_:threadsPerThreadgroup:)`](/documentation/Metal/MTLComputeCommandEncoder/dispatchThreadgroups(_:threadsPerThreadgroup:))

Encodes a compute dispatch command using a grid aligned to threadgroup boundaries.

### Dispatching from indirect command buffers

Run commands from an indirect command buffer.

[`dispatchThreadgroups(indirectBuffer:indirectBufferOffset:threadsPerThreadgroup:)`](/documentation/Metal/MTLComputeCommandEncoder/dispatchThreadgroups(indirectBuffer:indirectBufferOffset:threadsPerThreadgroup:))

Encodes a dispatch call for a compute pass, using an indirect buffer that defines the size of a grid that aligns to threadgroup boundaries.

[`executeCommandsInBuffer(_:range:)`](/documentation/Metal/MTLComputeCommandEncoder/executeCommandsInBuffer(_:range:))

Encodes an instruction to run commands from an indirect buffer.

[`executeCommandsInBuffer(_:indirectBuffer:offset:)`](/documentation/Metal/MTLComputeCommandEncoder/executeCommandsInBuffer(_:indirectBuffer:offset:))

Encodes an instruction to run commands from an indirect buffer, using another buffer to provide the command range.

[`executeCommands(in:indirectBuffer:indirectBufferOffset:)`](/documentation/Metal/MTLComputeCommandEncoder/executeCommands(in:indirectBuffer:indirectBufferOffset:))

Encodes an instruction to run commands from an indirect buffer, using another buffer to provide the command range.

[`executeCommands(in:with:)`](/documentation/Metal/MTLComputeCommandEncoder/executeCommands(in:with:))

Encodes an instruction to run commands from an indirect buffer.

[`executeCommandsInBuffer:indirectBuffer:indirectBufferOffset:`](/documentation/Metal/MTLComputeCommandEncoder/executeCommandsInBuffer:indirectBuffer:indirectBufferOffset:)

Encodes an instruction to run commands from an indirect buffer, using another buffer to provide the command range.

[`executeCommandsInBuffer:withRange:`](/documentation/Metal/MTLComputeCommandEncoder/executeCommandsInBuffer:withRange:)

Encodes an instruction to run commands from an indirect buffer.

### Preventing resource access conflicts

Address hazards for untracked resources with fences and barriers.

[`waitForFence(_:)`](/documentation/Metal/MTLComputeCommandEncoder/waitForFence(_:))

Encodes a command that instructs the GPU to pause the compute pass until another pass updates a fence.

[`updateFence(_:)`](/documentation/Metal/MTLComputeCommandEncoder/updateFence(_:))

Encodes a command that instructs the GPU to update a fence after the compute pass completes.

[`memoryBarrier(scope:)`](/documentation/Metal/MTLComputeCommandEncoder/memoryBarrier(scope:))

Creates a memory barrier that enforces the order of write and read operations for specific resource types.

[`memoryBarrier(resources:)`](/documentation/Metal/MTLComputeCommandEncoder/memoryBarrier(resources:))

Creates a memory barrier that enforces the order of write and read operations for specific resources.

[`memoryBarrierWithResources:count:`](/documentation/Metal/MTLComputeCommandEncoder/memoryBarrierWithResources:count:)

Creates a memory barrier that enforces the order of write and read operations for specific resources.

### Sampling counters

Capture runtime data from GPU hardware counters.

[`sampleCounters(sampleBuffer:sampleIndex:barrier:)`](/documentation/Metal/MTLComputeCommandEncoder/sampleCounters(sampleBuffer:sampleIndex:barrier:))

Encodes a command to sample hardware counters, providing performance information.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)