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

# sampleCounters(sampleBuffer:sampleIndex:barrier:)

Encodes a command that samples hardware counters during the render pass and stores the data into a counter sample buffer.

```
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`

A Boolean value that indicates whether 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.

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

## Discussion

---

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)