---
title: "setBytes(_:length:attributeStride:index:)"
framework: metal
role: symbol
role_heading: Instance Method
path: "metal/mtlcomputecommandencoder/setbytes(_:length:attributestride:index:)"
---

# setBytes(_:length:attributeStride:index:)

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

## Declaration

```swift
func setBytes(_ bytes: UnsafeRawPointer, length: Int, attributeStride stride: Int, index: Int)
```

## Parameters

- `bytes`: A pointer to the memory where the data to copy starts.
- `length`: The number of bytes to copy.
- `stride`: The number of bytes between the start of one element and the start of the next.
- `index`: The index the data binds to in the argument table.

## Discussion

Discussion important: This method only works for data smaller than 4 kilobytes that doesn’t persist. Create an MTLBuffer instance if your data exceeds 4 KB, needs to persist on the GPU, or you access results on the CPU. This method allows Metal to copy data directly onto the GPU, rather than creating a new MTLBuffer instance and binding it. Binding data directly can improve performance, especially when making many small allocations.

## See Also

### Binding raw bytes

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