View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vidéos

Ouvrir le menu Fermer le menu
  • Collections
  • Toutes les vidéos
  • À propos

Plus de vidéos

  • À propos
  • Code
  • Explore Live GPU Profiling with Metal Counters

    Take advantage of the Metal Counters API for GPU profiling in macOS Big Sur and iOS 14. This API provides access at runtime to low-level GPU profiling information, which was previously available only through offline tools in Xcode and Instruments. Metal Counters accelerate the optimization process by giving you access to important GPU information, helping you fine-tune your app's performance to create faster and more fluid apps and gaming experiences. Learn to collect and parse these low-level GPU timestamps and use the in-depth information to help with performance tuning in Metal.

    Ressources

      • Vidéo HD
      • Vidéo SD
  • Rechercher dans cette vidéo…
    • 3:38 - Checking for available Metal counters

      if (@available(macOS 11.0, iOS 14.0, *))
      {
          _supportsStageBoundary = [_device supportsCounterSampling:MTLCounterSamplingPointAtStageBoundary];
          _supportsDrawBoundary  = [_device supportsCounterSampling:MTLCounterSamplingPointAtDrawBoundary];
      }
    • 3:52 - Counter sets

      [_device.counterSets enumerateObjectsUsingBlock:^(id<MTLCounterSet> nonnull obj,
                                                        NSUInteger                idx,
                                                        BOOL * nonnull            stop) {
             if ([[obj name] isEqualToString:MTLCommonCounterSetTimestamp])
                  _counterSetTimestamp = obj;
      }];
    • 5:05 - Sampling counters on Apple GPUs


      // When setting up the render pass descriptor
      
      if (_supportsStageBoundary || _supportsDrawBoundary)
      {
          MTLCounterSampleBufferDescriptor *desc = [MTLCounterSampleBufferDescriptor new];
      
          desc.sampleCount = 6; // Number of samples to store 
          desc.storageMode = MTLStorageModeShared;
          desc.label       = @"Live Profiling HUD Metal counter sample buffer";
          desc.counterSet  = _counterSetTimestamp;
      
          id<MTLCounterSampleBuffer> sampleBuffer =
                                     [_device newCounterSampleBufferWithDescriptor:desc error:nil];
      
          MTLRenderPassSampleBufferAttachmentDescriptor *sampleBufferDesc =
                                        renderPassDescriptor.sampleBufferAttachments[0];
      
          if (_supportsStageBoundary)
          {
              sampleBufferDesc.startOfVertexSampleIndex   = 0;
              sampleBufferDesc.endOfVertexSampleIndex     = 1;
              sampleBufferDesc.startOfFragmentSampleIndex = 2;
              sampleBufferDesc.endOfFragmentSampleIndex   = 3;
          }
      
          sampleBufferDesc.sampleBuffer = sampleBuffer;
      }
    • 6:23 - Sampling counters at draw boundary

      // After creating a new render command encoder
      [renderCommandEncoder sampleCountersInBuffer:sampleBuffer
                                     atSampleIndex:4
                                       withBarrier:NO];
      
      // All draw calls
      [renderCommandEncoder sampleCountersInBuffer:sampleBuffer
                                     atSampleIndex:5
                                       withBarrier:NO];
      
      // End encoding
    • 7:28 - Collecting timestamps

      // For each tracked sampleBuffer, resolve the counters
      NSData *data = [sampleBuffer resolveCounterRange:NSMakeRange(0, 6)];
      
      MTLCounterResultTimestamp *sample = (MTLCounterResultTimestamp *)[data bytes];
      
      // And simply access the timestamps
      if (_supportsStageBoundary)
      {
          double vertexStart = sample[0].timestamp / (double)NSEC_PER_SEC;
      }
      
      // Check for errors
      if (sample[0].timestamp == MTLCounterErrorValue) 
      {
        // Handle error
      }
    • 9:05 - Aligning timestamps

      // On immediate mode GPU
      MTLTimestamp cpuTimestamp;
      MTLTimestamp gpuTimestamp;
      [_device sampleTimestamps:&cpuTimestamp gpuTimestamp:&gpuTimestamp];
      
      // Do a linear interpolation between correlated timestamps
      gpu_ns = cpu_t0 + (cpu_t1 - cpu_t0) * (gpu_timestamp - gpu_t0) / (gpu_t1 - gpu_t0);

Developer Footer

  • Vidéos
  • Tech Talks
  • Explore Live GPU Profiling with Metal Counters
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines