---
title: "sampleCounters(sampleBuffer:sampleIndex:barrier:)"
framework: metal
role: symbol
role_heading: Instance Method
path: "metal/mtlblitcommandencoder/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.

## Declaration

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

## Mentioned in

Sampling GPU data into counter sample buffers

## Discussion

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.

## See Also

### Sampling counters

- [resolveCounters(_:range:destinationBuffer:destinationOffset:)](metal/mtlblitcommandencoder/resolvecounters(_:range:destinationbuffer:destinationoffset:).md)
