<!--
{
  "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/MTLBlitCommandEncoder/sampleCounters(sampleBuffer:sampleIndex:barrier:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Metal"
    ],
    "preciseIdentifier" : "c:objc(pl)MTLBlitCommandEncoder(im)sampleCountersInBuffer:atSampleIndex:withBarrier:"
  },
  "title" : "sampleCounters(sampleBuffer:sampleIndex:barrier:)"
}
-->

# sampleCounters(sampleBuffer:sampleIndex:barrier:)

Encodes a command that samples the GPU’s hardware counters during a blit pass and stores the data in a counter sample buffer.

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

## Parameters

`sampleBuffer`

A counter sample buffer where the command stores the sample data.

`sampleIndex`

A location within `sampleBuffer` where the command stores the sample data.

`barrier`

A Boolean value that indicates whether the command inserts a barrier before taking the sample.

## Discussion

Inserting a barrier ensures that any work you encode with this encoder is complete before the GPU samples the hardware counters. If you don’t insert a barrier, the GPU can sample the counters concurrently with other commands you encode with this encoder. Using a barrier can help the counter results be more predictable and repeatable, but it may adversely affect your app’s runtime performance.

> Note:
> The GPU doesn’t isolate this sampling command from any commands that come from another encoder, with or without a barrier.

---

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)