---
title: MTL4ComputeCommandEncoder
framework: metal
role: symbol
role_heading: Protocol
path: metal/mtl4computecommandencoder
---

# MTL4ComputeCommandEncoder

Encodes computation dispatches, resource copying commands, and acceleration structure building commands for a single pass into a command buffer.

## Declaration

```swift
protocol MTL4ComputeCommandEncoder : MTL4CommandEncoder
```

## Mentioned in

Understanding the Metal 4 core API

## Overview

Overview Each Metal 4 compute encoder combines compute dispatch commands, blit commands, and acceleration structure commands into a single pass. The unified nature of this encoder type eliminates the overhead from creating separate encoders like MTLComputeCommandEncoder, MTLBlitCommandEncoder, and MTLAccelerationStructureCommandEncoder, and then encoding separate passes with them. Create a compute encoder by calling a factory method of an MTL4CommandBuffer instance, such as makeComputeCommandEncoder(). Command stages Most compute commands apply to one stage within a pass. The following table shows which stage applies to each command:  |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |  The executeCommands(buffer:range:) and executeCommands(buffer:indirectBuffer:) 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 pass

- [setComputePipelineState(_:)](metal/mtl4computecommandencoder/setcomputepipelinestate(_:).md)
- [setArgumentTable(_:)](metal/mtl4computecommandencoder/setargumenttable(_:).md)
- [setThreadgroupMemoryLength(_:index:)](metal/mtl4computecommandencoder/setthreadgroupmemorylength(_:index:).md)
- [setImageblockSize(width:height:)](metal/mtl4computecommandencoder/setimageblocksize(width:height:).md)

### Inspecting the pass

- [stages()](metal/mtl4computecommandencoder/stages().md)

### Running dispatch commands

- [dispatchThreads(threadsPerGrid:threadsPerThreadgroup:)](metal/mtl4computecommandencoder/dispatchthreads(threadspergrid:threadsperthreadgroup:).md)
- [dispatchThreads(indirectBuffer:)](metal/mtl4computecommandencoder/dispatchthreads(indirectbuffer:).md)
- [dispatchThreadgroups(threadgroupsPerGrid:threadsPerThreadgroup:)](metal/mtl4computecommandencoder/dispatchthreadgroups(threadgroupspergrid:threadsperthreadgroup:).md)
- [dispatchThreadgroups(indirectBuffer:threadsPerThreadgroup:)](metal/mtl4computecommandencoder/dispatchthreadgroups(indirectbuffer:threadsperthreadgroup:).md)

### Encoding buffer copy commands

- [copy(sourceBuffer:sourceOffset:destinationBuffer:destinationOffset:size:)](metal/mtl4computecommandencoder/copy(sourcebuffer:sourceoffset:destinationbuffer:destinationoffset:size:).md)

### Encoding buffer-to-texture copy commands

- [copy(sourceBuffer:sourceOffset:sourceBytesPerRow:sourceBytesPerImage:sourceSize:destinationTexture:destinationSlice:destinationLevel:destinationOrigin:options:)](metal/mtl4computecommandencoder/copy(sourcebuffer:sourceoffset:sourcebytesperrow:sourcebytesperimage:sourcesize:destinationtexture:destinationslice:destinationlevel:destinationorigin:options:).md)

### Encoding texture copy commands

- [copy(sourceTensor:sourceOrigin:sourceDimensions:destinationTensor:destinationOrigin:destinationDimensions:)](metal/mtl4computecommandencoder/copy(sourcetensor:sourceorigin:sourcedimensions:destinationtensor:destinationorigin:destinationdimensions:).md)
- [copy(sourceTexture:destinationTexture:)](metal/mtl4computecommandencoder/copy(sourcetexture:destinationtexture:).md)
- [copy(sourceTexture:sourceSlice:sourceLevel:destinationTexture:destinationSlice:destinationLevel:sliceCount:levelCount:)](metal/mtl4computecommandencoder/copy(sourcetexture:sourceslice:sourcelevel:destinationtexture:destinationslice:destinationlevel:slicecount:levelcount:).md)
- [copy(sourceTexture:sourceSlice:sourceLevel:sourceOrigin:sourceSize:destinationTexture:destinationSlice:destinationLevel:destinationOrigin:)](metal/mtl4computecommandencoder/copy(sourcetexture:sourceslice:sourcelevel:sourceorigin:sourcesize:destinationtexture:destinationslice:destinationlevel:destinationorigin:).md)

### Encoding texture-to-buffer copy commands

- [copy(sourceTexture:sourceSlice:sourceLevel:sourceOrigin:sourceSize:destinationBuffer:destinationOffset:destinationBytesPerRow:destinationBytesPerImage:options:)](metal/mtl4computecommandencoder/copy(sourcetexture:sourceslice:sourcelevel:sourceorigin:sourcesize:destinationbuffer:destinationoffset:destinationbytesperrow:destinationbytesperimage:options:).md)

### Encoding indirect command buffer copy commands

- [copyCommands(sourceBuffer:sourceRange:destinationBuffer:destinationIndex:)](metal/mtl4computecommandencoder/copycommands(sourcebuffer:sourcerange:destinationbuffer:destinationindex:).md)

### Encoding buffer fill commands

- [fill(buffer:range:value:)](metal/mtl4computecommandencoder/fill(buffer:range:value:).md)

### Encoding mipmap generation commands

- [generateMipmaps(texture:)](metal/mtl4computecommandencoder/generatemipmaps(texture:).md)

### Encoding optimization commands

- [optimizeCommands(buffer:range:)](metal/mtl4computecommandencoder/optimizecommands(buffer:range:).md)
- [optimizeContents(forCPUAccess:)](metal/mtl4computecommandencoder/optimizecontents(forcpuaccess:).md)
- [optimizeContents(forCPUAccess:slice:level:)](metal/mtl4computecommandencoder/optimizecontents(forcpuaccess:slice:level:).md)
- [optimizeContents(forGPUAccess:)](metal/mtl4computecommandencoder/optimizecontents(forgpuaccess:).md)
- [optimizeContents(forGPUAccess:slice:level:)](metal/mtl4computecommandencoder/optimizecontents(forgpuaccess:slice:level:).md)

### Encoding reset commands

- [resetCommands(buffer:range:)](metal/mtl4computecommandencoder/resetcommands(buffer:range:).md)

### Encoding acceleration structure build commands

- [build(destinationAccelerationStructure:descriptor:scratchBuffer:)](metal/mtl4computecommandencoder/build(destinationaccelerationstructure:descriptor:scratchbuffer:).md)

### Encoding acceleration structure copy commands

- [copy(sourceAccelerationStructure:destinationAccelerationStructure:)](metal/mtl4computecommandencoder/copy(sourceaccelerationstructure:destinationaccelerationstructure:).md)
- [copyAndCompact(sourceAccelerationStructure:destinationAccelerationStructure:)](metal/mtl4computecommandencoder/copyandcompact(sourceaccelerationstructure:destinationaccelerationstructure:).md)
- [writeCompactedSize(sourceAccelerationStructure:destinationBuffer:)](metal/mtl4computecommandencoder/writecompactedsize(sourceaccelerationstructure:destinationbuffer:).md)

### Encoding acceleration structure refit commands

- [refit(sourceAccelerationStructure:descriptor:destinationAccelerationStructure:scratchBuffer:options:)](metal/mtl4computecommandencoder/refit(sourceaccelerationstructure:descriptor:destinationaccelerationstructure:scratchbuffer:options:).md)

### Encoding indirect command buffers

- [executeCommands(buffer:range:)](metal/mtl4computecommandencoder/executecommands(buffer:range:).md)
- [executeCommands(buffer:indirectBuffer:)](metal/mtl4computecommandencoder/executecommands(buffer:indirectbuffer:).md)

### Encoding performance measurement commands

- [writeTimestamp(granularity:counterHeap:index:)](metal/mtl4computecommandencoder/writetimestamp(granularity:counterheap:index:).md)

### Instance Methods

- [copy(sourceTensor:sourceOrigin:sourceDimensions:sourcePlane:destinationTensor:destinationOrigin:destinationDimensions:destinationPlane:)](metal/mtl4computecommandencoder/copy(sourcetensor:sourceorigin:sourcedimensions:sourceplane:destinationtensor:destinationorigin:destinationdimensions:destinationplane:).md)

## Relationships

### Inherits From

- [MTL4CommandEncoder](metal/mtl4commandencoder.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)
- [MTLComputeCommandEncoder](metal/mtlcomputecommandencoder.md)
