Featured
  • Modern Rendering with Metal

    Render a complex scene with the latest rendering techniques and effects. This sample uses advanced Metal features, such as indirect command buffers, sparse textures, and variable rate rasterization, to implement rendering algorithms that use GPU based mesh culling, tile based deferred lighting, ambient occlusion, volumetric fog, and cascaded shadow maps.

    macOS iOS

    View sample code
  • Migrating OpenGL Code to Metal

    See how to replace your app’s OpenGL code with Metal. This sample shows how shaders, renderable textures, and buffers work with Metal by comparing them with common OpenGL and OpenGL ES rendering constructs.

    macOS iOS

    View sample code
  • Animating and Denoising a Raytraced Scene

    Render and denoise a dynamic scenes in your ray tracer with Metal Performance Shaders.

    macOS iOS

    View sample code
  • Capturing Metal Commands Programmatically

    Debug your rendering by invoking Metal’s frame capture from within your app. Save the resulting GPU trace to a file or view it in Xcode to explore the Metal commands produced by your app.

    macOS

    View sample code
Metal Fundamentals
  • Performing Calculations on a GPU

    Use Metal to issue data parallel calculations to the GPU. 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

    See how to use Metal for setup and rendering. This sample shows how to configure a MetalKit view, setup Metal for rendering, and display rendered content.

    macOS iOS tvOS

    View sample code
  • Using a Render Pipeline to Render Primitives

    Render simple geometry in Metal. This sample shows how to work with vertex data and SIMD types, configure a graphics rendering pipeline, write GPU functions, and issue draw calls.

    macOS iOS tvOS

    View sample code
  • Creating and Sampling Textures

    Use simple texturing with Metal. This sample shows how to configure texture properties, interpret texture coordinates, and access a texture in a fragment function to display a 2D image.

    macOS iOS tvOS

    View sample code
  • Synchronizing CPU and GPU Work

    Efficiently update and render animated resources while sharing them between the CPU and the GPU. This sample shows how to modify data each frame, avoid data access issues, and execute CPU and GPU work in parallel.

    macOS iOS tvOS

    View sample code
  • Basic Argument Buffers

    Use a simple argument buffer to store references to Metal resources. This sample shows how to encode textures, buffers, samplers and constant values into a Metal buffer and access these in a shader for rendering.

    macOS iOS tvOS

    View sample code
  • Argument Buffers with Arrays and Resource Heaps

    Reduce CPU overhead and improve performance by using a heap to store resources set in an argument buffer. This sample shows how to define an argument buffer structure that contains arrays and how to allocate and use resources from a heap.

    macOS iOS tvOS

    View sample code
  • Argument Buffers with GPU Encoding

    Use the GPU to encode a rendering pass for even better performance using an argument buffer. This sample shows how to write data into the argument buffer from a compute kernel and accessing it in a subsequent render pass.

    macOS

    View sample code
  • Encoding Indirect Command Buffers on the CPU

    Create an indirect command buffer and encode commands using the CPU. This sample shows how to efficiently render many varying objects repeatedly using an indirect command buffer.

    macOS iOS

    View sample code
  • Encoding Indirect Command Buffers on the GPU

    Dynamically encode rendering commands using the GPU. This sample uses a simple culling algorithm to select objects in a compute kernel and increase rendering efficiency by drawing only those that are visible.

    macOS iOS

    View sample code
  • Basic Tesselation

    Tessellate a triangle or quad patch with adjustable per-patch tessellation factors. This sample allows the user to control the number of triangles that make up a patch and visualize the geometry produced.

    macOS

    View sample code
Metal Integration
  • Device Selection and Fallback for Graphics Rendering

    Choose the best GPU for rendering or display and respond to the addition and removal of an eGPU or external display. This sample sets up rendering on multiple GPUs and enables seamless switching between them.

    macOS

    View sample code
  • Device Selection and Fallback for Compute Processing

    Utilize the additional computational power of a second GPU and transfer data it produces to your application. This sample executes a computation intensive N-body simulation on one GPU while continually transferring data to the primary GPU for rendering.

    macOS

    View sample code
  • Migrating OpenGL Code to Metal

    See how to replace your app’s OpenGL code with Metal. This sample shows how shaders, renderable textures, and buffers work with Metal by comparing them with common OpenGL and OpenGL ES rendering constructs.

    macOS iOS

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

    To assist in porting your application to Metal, learn how to incrementally transition your application's rendering from OpenGL. This application passes rendered content between textures managed by Metal and OpenGL.

    macOS iOS

    View sample code
  • Capturing Metal Commands Programmatically

    Debug your rendering by invoking Metal’s frame capture from within your app. Save the resulting 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

    Learn how to run your Metal app on the iOS simulator while continuing to use Metal features only available on iOS devices.

    macOS iOS

    View sample code
Advanced Techniques
  • Modern Rendering with Metal

    Render a complex scene with the latest rendering techniques and effects. This sample uses advanced Metal features, such as indirect command buffers, sparse textures, and variable rate rasterization, to implement rendering algorithms that use GPU based mesh culling, tile based deferred lighting, ambient occlusion, volumetric fog, and cascaded shadow maps.

    macOS iOS

    View sample code
  • LOD with Function Specialization

    Use Metal function constants to reduce the amount of shader code by creating multiple executable versions from a single source function. This sample demonstrates dynamic level of detail (LOD) selection for a model without needing separate shaders for each level of detail.

    macOS iOS tvOS

    View sample code
  • Reflections with Layer Section

    Render to a cubemap by selecting faces in a vertex shader. This sample demonstrates dynamic reflections on a chrome sphere, using layer selection to render the frame in two passes.

    macOS iOS

    View sample code
  • Deferred Lighting

    See how to implement efficient deferred lighting renderer with Metal. This sample makes use of the unique features available to the A-Series GPUs including programmable blending and memoryless textures. It also implements a more standard deferred lighting renderer for macOS.

    macOS iOS tvOS

    View sample code
  • Dynamic Terrain with Argument Buffers

    Use argument buffers to implement a dynamic environment. This sample generates on an outdoor landscape, using argument buffers to select terrain materials, vegetation geometry, and particle effects within a GPU-driven pipeline

    macOS iOS tvOS

    View sample code
  • Forward Plus Lighting with Tile Shading

    Use a tile shader to accelerate a key parts of a forward plus renderer. This sample executes a tile shader to sort light volumes and direct fragment lighting calculations only to lights affecting each tile.

    iOS

    View sample code
  • Image Filter Graph with Heaps and Fences

    Reduce the memory usage of your application by allocating resources from a heap and perform explicit hazard tracking. This sample implements a multistage image filter which efficiently reuses memory from the heap for different tasks during processing.

    macOS iOS tvOS

    View sample code
  • Image Filter Graph with Heaps and Events

    Use events to synchronize GPU operations and reduce memory usage. This sample demonstrates an alternative to using fences in implementing a multistage image filter.

    macOS iOS tvOS

    View sample code
  • Metal for Accelerating Ray Tracing

    Use the ray-intersector functionality available in the Metal Performance Shaders framework to accelerate a path-traced renderer. This sample demonstrates how to cast primary, secondary, and shadow rays in a scene to simulate realistic lighting effects.

    macOS iOS

    View sample code
  • Animating and Denoising a Raytraced Scene

    Render and denoise a dynamic scenes in your ray tracer with Metal Performance Shaders.

    macOS iOS

    View sample code