---
title: MTLComputeCommandEncoder
framework: metal
role: symbol
role_heading: Protocol
path: metal/mtlcomputecommandencoder
---

# MTLComputeCommandEncoder

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

## Declaration

```swift
protocol MTLComputeCommandEncoder : MTLCommandEncoder
```

## Mentioned in

Understanding the Metal 4 core API Improving CPU performance by using argument buffers Sampling GPU data into counter sample buffers Setting up a command structure Simplifying GPU resource management with residency sets Tracking the resource residency of argument buffers

## Overview

Overview Create a compute encoder by calling one of the factory methods on an MTLCommandBuffer instance, such as 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: Configure an MTLComputePipelineState instance with a kernel, using a method such as makeComputePipelineState(function:). See the Creating compute pipeline states section of Pipeline state creation for all MTLDevice methods that create a new pipeline state for your command encoder. Set the pipeline state with the setComputePipelineState(_:) method on your command encoder. Set kernel arguments by binding buffers, textures, and other resources with methods such as setBuffer(_:offset:index:) and setTexture(_:index:). Encode compute commands that call your kernel by either Dispatching kernel calls directly or Dispatching from indirect command buffers. Call 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:  |   |   |   |   |   |   |  The executeCommandsInBuffer(_:range:) and 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 applies to the same stages as when you encode the equivalent command directly. For more information about stages and synchronization, see MTLStages and Resource synchronization.

## Topics

### Configuring the pipeline state

- [setComputePipelineState(_:)](metal/mtlcomputecommandencoder/setcomputepipelinestate(_:).md)
- [dispatchType](metal/mtlcomputecommandencoder/dispatchtype.md)

### Binding buffers

- [setBuffer(_:offset:index:)](metal/mtlcomputecommandencoder/setbuffer(_:offset:index:).md)
- [setBuffer(_:offset:attributeStride:index:)](metal/mtlcomputecommandencoder/setbuffer(_:offset:attributestride:index:).md)
- [setBuffers(_:offsets:range:)](metal/mtlcomputecommandencoder/setbuffers(_:offsets:range:).md)
- [setBuffers(_:offsets:attributeStrides:range:)](metal/mtlcomputecommandencoder/setbuffers(_:offsets:attributestrides:range:).md)
- [setBufferOffset(_:index:)](metal/mtlcomputecommandencoder/setbufferoffset(_:index:).md)
- [setBufferOffset(offset:attributeStride:index:)](metal/mtlcomputecommandencoder/setbufferoffset(offset:attributestride:index:).md)

### Binding raw bytes

- [setBytes(_:length:index:)](metal/mtlcomputecommandencoder/setbytes(_:length:index:).md)
- [setBytes(_:length:attributeStride:index:)](metal/mtlcomputecommandencoder/setbytes(_:length:attributestride:index:).md)

### Binding textures

- [setTexture(_:index:)](metal/mtlcomputecommandencoder/settexture(_:index:).md)
- [setTextures(_:range:)](metal/mtlcomputecommandencoder/settextures(_:range:).md)

### Binding texture samplers

- [setSamplerState(_:index:)](metal/mtlcomputecommandencoder/setsamplerstate(_:index:).md)
- [setSamplerState(_:lodMinClamp:lodMaxClamp:index:)](metal/mtlcomputecommandencoder/setsamplerstate(_:lodminclamp:lodmaxclamp:index:).md)
- [setSamplerStates(_:range:)](metal/mtlcomputecommandencoder/setsamplerstates(_:range:).md)
- [setSamplerStates(_:lodMinClamps:lodMaxClamps:range:)](metal/mtlcomputecommandencoder/setsamplerstates(_:lodminclamps:lodmaxclamps:range:).md)

### Binding function tables

- [setVisibleFunctionTable(_:bufferIndex:)](metal/mtlcomputecommandencoder/setvisiblefunctiontable(_:bufferindex:).md)
- [setVisibleFunctionTables(_:bufferRange:)](metal/mtlcomputecommandencoder/setvisiblefunctiontables(_:bufferrange:).md)
- [setIntersectionFunctionTables(_:bufferRange:)](metal/mtlcomputecommandencoder/setintersectionfunctiontables(_:bufferrange:).md)

### Binding arguments for acceleration structures

- [setAccelerationStructure(_:bufferIndex:)](metal/mtlcomputecommandencoder/setaccelerationstructure(_:bufferindex:).md)
- [setIntersectionFunctionTable(_:bufferIndex:)](metal/mtlcomputecommandencoder/setintersectionfunctiontable(_:bufferindex:).md)

### Making indirect resources resident

- [useResource(_:usage:)](metal/mtlcomputecommandencoder/useresource(_:usage:).md)
- [useResources(_:usage:)](metal/mtlcomputecommandencoder/useresources(_:usage:).md)
- [useHeap(_:)](metal/mtlcomputecommandencoder/useheap(_:).md)
- [useHeaps(_:)](metal/mtlcomputecommandencoder/useheaps(_:).md)

### Configuring tile memory

- [setThreadgroupMemoryLength(_:index:)](metal/mtlcomputecommandencoder/setthreadgroupmemorylength(_:index:).md)
- [setImageblockWidth(_:height:)](metal/mtlcomputecommandencoder/setimageblockwidth(_:height:).md)

### Configuring stage-in data

- [setStageInRegion(_:)](metal/mtlcomputecommandencoder/setstageinregion(_:).md)
- [setStageInRegionWithIndirectBuffer(_:indirectBufferOffset:)](metal/mtlcomputecommandencoder/setstageinregionwithindirectbuffer(_:indirectbufferoffset:).md)

### Dispatching kernel calls directly

- [dispatchThreads(_:threadsPerThreadgroup:)](metal/mtlcomputecommandencoder/dispatchthreads(_:threadsperthreadgroup:).md)
- [dispatchThreadgroups(_:threadsPerThreadgroup:)](metal/mtlcomputecommandencoder/dispatchthreadgroups(_:threadsperthreadgroup:).md)

### Dispatching from indirect command buffers

- [dispatchThreadgroups(indirectBuffer:indirectBufferOffset:threadsPerThreadgroup:)](metal/mtlcomputecommandencoder/dispatchthreadgroups(indirectbuffer:indirectbufferoffset:threadsperthreadgroup:).md)
- [executeCommandsInBuffer(_:range:)](metal/mtlcomputecommandencoder/executecommandsinbuffer(_:range:).md)
- [executeCommandsInBuffer(_:indirectBuffer:offset:)](metal/mtlcomputecommandencoder/executecommandsinbuffer(_:indirectbuffer:offset:).md)
- [executeCommands(in:indirectBuffer:indirectBufferOffset:)](metal/mtlcomputecommandencoder/executecommands(in:indirectbuffer:indirectbufferoffset:).md)
- [executeCommands(in:with:)](metal/mtlcomputecommandencoder/executecommands(in:with:).md)

### Preventing resource access conflicts

- [waitForFence(_:)](metal/mtlcomputecommandencoder/waitforfence(_:).md)
- [updateFence(_:)](metal/mtlcomputecommandencoder/updatefence(_:).md)
- [memoryBarrier(scope:)](metal/mtlcomputecommandencoder/memorybarrier(scope:).md)
- [memoryBarrier(resources:)](metal/mtlcomputecommandencoder/memorybarrier(resources:).md)

### Sampling counters

- [sampleCounters(sampleBuffer:sampleIndex:barrier:)](metal/mtlcomputecommandencoder/samplecounters(samplebuffer:sampleindex:barrier:).md)

## Relationships

### Inherits From

- [MTLCommandEncoder](metal/mtlcommandencoder.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)

## See Also

### Encoding a compute pass

- [Creating threads and threadgroups](metal/creating-threads-and-threadgroups.md)
- [Calculating threadgroup and grid sizes](metal/calculating-threadgroup-and-grid-sizes.md)
- [MTL4ComputeCommandEncoder](metal/mtl4computecommandencoder.md)
