<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Metal",
  "identifier" : "/documentation/Metal/MTLComputeCommandEncoder/setBytes(_:length:attributeStride:index:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Metal"
    ],
    "preciseIdentifier" : "c:objc(pl)MTLComputeCommandEncoder(im)setBytes:length:attributeStride:atIndex:"
  },
  "title" : "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.

```
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> Important:
> This method only works for data smaller than 4 kilobytes that doesn’t persist. Create an ``doc://com.apple.metal/documentation/Metal/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`](/documentation/Metal/MTLBuffer) instance and binding it. Binding data directly can improve performance, especially when making many small allocations.

---

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)