<!--
{
  "availability" : [
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: 14.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Metal",
  "identifier" : "/documentation/Metal/MTLComputeCommandEncoder/sampleCounters(sampleBuffer:sampleIndex:barrier:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Metal"
    ],
    "preciseIdentifier" : "c:objc(pl)MTLComputeCommandEncoder(im)sampleCountersInBuffer:atSampleIndex:withBarrier:"
  },
  "title" : "sampleCounters(sampleBuffer:sampleIndex:barrier:)"
}
-->

# sampleCounters(sampleBuffer:sampleIndex:barrier:)

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

```
func sampleCounters(sampleBuffer: any MTLCounterSampleBuffer, sampleIndex: Int, barrier: Bool)
```

## Parameters

`sampleBuffer`

An [`MTLCounterSampleBuffer`](/documentation/Metal/MTLCounterSampleBuffer) instance that stores the GPU hardware data.

`sampleIndex`

An index within `sampleBuffer` the command stores the data to.

`barrier`

Whether or not the command inserts a barrier before sampling the counter’s data.

    A barrier ensures that the commands you encode before this one complete before the GPU samples the hardware counters, but can negatively impact runtime performance.

    Running this command without a barrier means the GPU can sample counters concurrently with other commands from the encoder.

    The     `barrier`     parameter for the command has no impact on sampling commands from other passes.

## Discussion> Important:
> To use a sample buffer, it needs to be part of the ``doc://com.apple.metal/documentation/Metal/MTLComputePassDescriptor/sampleBufferAttachments`` on the compute pass descriptor.

See [GPU counters and counter sample buffers](/documentation/Metal/gpu-counters-and-counter-sample-buffers), [Sampling GPU data into counter sample buffers](/documentation/Metal/sampling-gpu-data-into-counter-sample-buffers), and [`MTLCounter`](/documentation/Metal/MTLCounter) for more 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)