Featured
  • Streaming large images with Metal sparse textures

    Use Metal sparse textures to render a high-resolution image without the need to allocate memory for the entire image. This sample queries a residency and access counter buffer to stream and unload mipmaps and image tiles as needed.

    macOS iOS

    View sample code
  • Starting a game port with Metal

    Follow several chapters of an interactive tutorial that demonstrates the process of porting your game from other platforms to macOS and subsequently to iOS. This tutorial covers essential technologies for implementing games on Apple platforms, including Metal rendering, audio, input, and display APIs.

    macOS iOS

    Download sample code
  • Rendering a curve primitive in a ray tracing scene

    Enhance the visual quality of your games and apps using curves with Metal ray tracing. This sample implements ray-traced rendering using GPU-based parallel processing.

    macOS iOS

    View sample code
  • Achieving smooth frame rates with Metal’s display link

    Pace rendering with minimal input latency while providing essential information to the operating system for power-efficient rendering, thermal mitigation, and the scheduling of sustainable workloads.

    macOS iOS tvOS

    View sample code
Metal Fundamentals
  • Learn Metal with C++

    Learn Metal fundamentals in this series of introductory samples. This series begins by showing how to create a window and render primitives and advances with samples showing 3D rendering and compute processing.

    macOS

    Download sample code
  • Performing Calculations on a GPU

    Use Metal to find GPUs and perform calculations on them. This sample shows how to obtain a Metal device, create simple buffer resources, and execute a basic compute kernel.

    macOS

    View sample code
  • Using Metal to Draw a View’s Contents

    Create a MetalKit view and a render pass to draw the view’s contents. This sample shows how to configure a MetalKit view, set up Metal for rendering, and display rendered content.

    macOS iOS tvOS

    View sample code
  • Calculating Primitive Visibility Using Depth Testing

    Determine which pixels are visible in a scene by using a depth texture. This sample draws geometry with varying depth values allowing Metal to remove pixels in triangles obscured by other triangles.

    macOS iOS

    View sample code
Mastering Metal
  • Improving edge-rendering quality with multisample antialiasing (MSAA)

    Utilize Metal’s multisample anti-aliasing capabilities to enhance the rendering of edges by employing custom resolve options with both immediate mode and tile-based resolve paths. This sample also demonstrates the utilization of a memoryless render target to eliminate the need to allocate a substantial multisample backing store.

    macOS iOS tvOS

    View sample code
  • Streaming large images with Metal sparse textures

    Use Metal sparse textures to render a high-resolution image without the need to allocate memory for the entire image. This sample queries a residency and access counter buffer to stream and unload mipmaps and image tiles as needed.

    macOS iOS

    View sample code
  • Managing groups of resources with argument buffers

    Create argument buffers to organize related resources. This sample shows how to set up a simple Metal argument buffer, encode textures, buffers, samplers, and constant values, and access these in a shader for rendering.

    macOS iOS

    View sample code
  • Adjusting the level of detail using Metal mesh shaders

    Choose and render meshes with several levels of detail using object and mesh shaders. This sample uses a mesh render pipeline to render a series of meshlets. It chooses a primitive and vertex count in the object shader depending upon a user-selected LOD for each one and renders a point, line, or triangle version in the mesh shader.

    macOS iOS

    View sample code
  • Loading textures and models using Metal fast resource loading

    Stream texture and buffer data directly from disk into Metal resources using fast resource loading. This sample streams high LOD textures and vertex data for three basic geometric objects from the disk as needed.

    macOS

    View sample code
  • Applying temporal antialiasing and upscaling using MetalFX

    Reduce render workloads while increasing image detail with MetalFX. The sample shows how to improve image quality by adding jitter during rendering and creating a motion vector texture for the Metal FX temporal scaler. It also shows how to only upscale a source image using the spatial scaler effect.

    macOS

    View sample code
  • Creating a Metal Dynamic Library

    Compile a library of shaders and write it to a file as a dynamically linked library. This sample displays a rudimentary source editor to develop and compile a Metal function into a dynamic library, linking to the library and then invoking the function from a shader.

    macOS iOS

    View sample code
  • Accelerating Raytracing Using Metal

    Implement ray-traced rendering using GPU-based parallel processing. This sample creates a custom raytracing pipeline with an acceleration structure and custom ray-intersection function to render a simple scene using path tracing.

    macOS iOS

    View sample code
  • Rendering a curve primitive in a ray tracing scene

    Enhance the visual quality of your games and apps using curves with Metal ray tracing. This sample implements ray-traced rendering using GPU-based parallel processing.

    macOS iOS

    View sample code
Metal Integration
  • Achieving smooth frame rates with Metal’s display link

    Pace rendering with minimal input latency while providing essential information to the operating system for power-efficient rendering, thermal mitigation, and the scheduling of sustainable workloads.

    macOS iOS tvOS

    View sample code
  • Creating a Custom Metal View

    Implement a lightweight view for Metal rendering that’s customized to your app’s needs. This sample shows how to create a custom Metal view by interacting directly with CoreAnimation to obtain drawable textures, control rendering on the main thread or secondary thread, and execute rendering in a loop in-sync with the display or in response to a system event.

    macOS iOS tvOS

    View sample code
  • Migrating OpenGL Code to Metal

    Replace your app’s deprecated OpenGL code with Metal. This sample shows how shaders, renderable textures, and buffers work with Metal by comparing them with typical OpenGL and OpenGL ES rendering constructs.

    macOS iOS

    View sample code
  • Mixing Metal and OpenGL Rendering in a View

    Draw with Metal and OpenGL in the same view using an interoperable texture. This sample shows how to transition your application's rendering from OpenGL incrementally by setting up a shared memory buffer for textures managed by Metal and OpenGL.

    macOS iOS

    View sample code
  • Capturing Metal Commands Programmatically

    Invoke Metal’s frame capture from your app, then save the resulting GPU trace to a file or view it in Xcode. This sample shows how to save a GPU trace to a file or view it in Xcode to explore the Metal commands produced by your app.

    macOS

    View sample code
  • Supporting Simulator in a Metal App

    Modify Metal Apps to Run in Simulator. This sample will help you learn how to run your Metal app on the iOS simulator while using Metal features only available on iOS devices.

    macOS iOS tvOS

    View sample code
Advanced Techniques
  • Modern Rendering with Metal

    Use advanced Metal features such as Indirect Command Buffers, Sparse Textures, and Variable Rate Rasterization to implement modern rendering algorithms. This sample uses advanced Metal features to render a complex scene with the latest rendering techniques and effects like GPU-based mesh culling, tile-based deferred lighting, ambient occlusion, volumetric fog, and cascaded shadow maps.

    macOS iOS

    View sample code
  • Using Function Specialization to Build Pipeline Variants

    Create pipelines for different levels of detail from a common shader source. This sample uses Metal function constants to demonstrate dynamic level of detail (LOD) selection for a model without needing separate shaders for each LOD.

    macOS iOS tvOS

    View sample code
  • Implementing a Multistage Image Filter Using Heaps and Fences

    Use fences to synchronize access to resources allocated on a heap. This sample implements a multistage image filter that efficiently reuses memory from a Metal heap for different tasks during processing while also using fences to track data hazards.

    macOS iOS tvOS

    View sample code
  • Accelerating ray tracing and motion blur using Metal

    Generate ray-traced images with motion blur using GPU-based parallel processing. This sample uses primitive or instanced motion to blur a model by calculating colors using multiple keyframes.

    macOS iOS

    View sample code
  • Processing HDR Images with Metal

    Implement a post-processing pipeline using the latest features on Apple GPUs. This sample demonstrates how to apply HDR processing like bloom, exposure, and tone-mapping operators.

    macOS iOS

    View sample code
  • Customizing a TensorFlow operation

    Use Metal to accelerate neural-network training performance of a custom Tensorflow operation.

    macOS

    View sample code
  • Customizing a PyTorch operation

    Implement a custom operation in PyTorch that uses Metal kernels to improve performance.

    macOS

    View sample code
Game Porting
  • Starting a game port with Metal

    Follow several chapters of an interactive tutorial that demonstrates the process of porting your game from other platforms to macOS and subsequently to iOS. This tutorial covers essential technologies for implementing games on Apple platforms, including Metal rendering, audio, input, and display APIs.

    macOS iOS

    Download sample code
  • Porting tessellation, geometry, and instancing pipelines

    Convert and utilize vertex, fragment, and compute shaders from HLSL to Metal IR. This sample also demonstrates how to implement more intricate pipelines that employ HLSL geometry and tessellation shaders using the Metal mesh shading API.

    macOS

    Download sample code
  • Porting ray tracing pipelines

    Utilize the Metal shader converter to transform ray tracing HLSL shaders into Metal IR, enabling the rendering of triangles and procedural spheres.

    macOS

    Download sample code
  • Porting ray query pipelines

    Utilize the Metal shader converter to convert HLSL ray query shaders into Metal IR. Employ these converted shaders to render two triangles, displaying their barycentric coordinates.

    macOS

    Download sample code