Contents

Creating a counter sample buffer to store a GPU’s counter data during a pass

Make a buffer that provides a place for a GPU to save its runtime performance metrics as it runs a pass.

Overview

You can create and use an MTLCounterSampleBuffer instance to store information from a GPU counter. To check whether a GPU produces data for a specific counter, see Confirming which counters and counter sets a GPU supports. Each counter sample buffer represents memory that a GPU uses to save data from the counter as it runs a pass. Counter sample buffers provide the GPU a place to temporarily store sample data, which avoids the need to synchronize data with the CPU. However, your app has the option to resolve the sample data with the CPU after the pass completes. See Converting a GPU’s counter data into a readable format for more information about resolving sample data.

Create a counter sample buffer for a GPU by:

  1. Confirming a GPU device supports the counter set you want to sample

  2. Retrieving the GPU’s instance of that counter set

  3. Creating an MTLCounterSampleBufferDescriptor instance and configuring its properties for the counter set

  4. Passing the descriptor to the GPU device’s makeCounterSampleBuffer(descriptor:) factory method

The code example above gives the CPU access to the counter sample buffer by configuring the descriptor’s storageMode property to MTLStorageMode.shared. Alternatively, you can set this property to MTLStorageMode.private if your app only uses the GPU to access its data. The example also sets the descriptor’s sampleCount property to 4 to store the starting and completion timestamps for both the vertex and the fragment stages. The value for the descriptor’s sample count in this example is directly related to the following four properties of the MTLRenderPassSampleBufferAttachmentDescriptor type:

When your app has a counter sample buffer, it can then instruct the GPU to save its counter sample data to it during a pass. See Sampling GPU data into counter sample buffers for more information.

See Also

Counter sample buffers