Featured
  • Rendering reflections in real time using ray tracing

    Render realistic real-time lighting by dynamically generating reflection maps using a ray-tracing compute pass. This sample implements a hybrid renderer to draw a model using rasterization and then create ray traced reflections to apply to the model’s surfaces.

    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
  • Implementing Order-Independent Transparency with Image Blocks

    Draw overlapping, transparent surfaces in any order by using tile shaders and image blocks. This sample draws a series of squares, stores the fragment color and depth values in tile memory, and then blends the color values in back-to-front order.

    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
  • 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
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
  • Using a Render Pipeline to Render Primitives

    Render a simple 2D triangle. 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

    Load image data into a texture and apply it to a quadrangle. 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
  • 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
  • Synchronizing CPU and GPU Work

    Avoid stalls between CPU and GPU work by using multiple instances of a resource. This sample shows how to render animated resources, efficiently update data each frame, avoid data access issues and execute CPU and GPU work in parallel.

    macOS iOS tvOS

    View sample code
  • Customizing Render Pass Setup

    Render into an offscreen texture by creating a custom render pass. This sample renders a triangle to an offscreen texture by setting up a render pass descriptor rather than depending on one provided by an MTKView.

    macOS iOS tvOS

    View sample code
  • Processing a Texture in a Compute Function

    Perform parallel calculations on structured data by placing the data in textures. This sample uses a compute kernel to convert the pixels in a texture to grayscale.

    macOS iOS tvOS

    View sample code
  • Reading Pixel Data from a Drawable Texture

    Access texture data with the CPU by copying it to a buffer. This sample shows how to copy pixels from an on-screen drawable texture and upload it to to the CPU for further processing.

    macOS iOS

    View sample code
Mastering Metal
  • 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
  • Using Argument Buffers with Resource Heaps

    Reduce CPU overhead by using arrays inside argument buffers and combining them with resource heaps. 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
  • Encoding Argument Buffers on the GPU

    Use a compute pass to encode an argument buffer and access its arguments in a subsequent render pass. This sample shows how to improve performance by using the GPU to write data into an argument buffer.

    macOS

    View sample code
  • Encoding Indirect Command Buffers on the CPU

    Reduce CPU overhead and simplify your command execution by reusing commands. 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

    Maximize CPU to GPU parallelization by generating render commands on 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
  • 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
  • Customizing shaders using function pointers and stitching

    Define custom shader behavior at runtime by combining shader functions and preferentially linking to others in a dynamic library. This sample stitches together many small functions into a function stitching graph and builds a function to calculate an iteration in the Mandelbrot set from this graph.

    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
  • Control the Ray Tracing Process Using Intersection Queries

    Explicitly enumerate a ray’s intersections with acceleration structures by creating an intersection query object. The sample creates an acceleration structure containing both triangle geometry and custom sphere geometry. It then performs a ray/sphere intersection test when a ray hits a bounding box enclosing a sphere.

    macOS iOS

    View sample code
  • Basic Tessellation

    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
  • 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
  • Selecting Device Objects for Graphics Rendering

    Switch dynamically between multiple GPUs to efficiently render to a display. This sample shows how to set up rendering on multiple GPUs, choose the best GPU for rendering or display, respond to the addition and removal of an eGPU or external display, or enable seamless switching between them.

    macOS

    View sample code
  • Selecting Device Objects for Compute Processing

    Switch dynamically between multiple GPUs to efficiently execute a compute-intensive simulation. This sample runs 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

    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
  • Implementing Order-Independent Transparency with Image Blocks

    Draw overlapping, transparent surfaces in any order by using tile shaders and image blocks. This sample draws a series of squares, stores the fragment color and depth values in tile memory, and then blends the color values in back-to-front order.

    macOS iOS

    View sample code
  • Transferring Data with Infinity Fabric Link

    Efficiently execute and visualize a compute-intensive simulation using multiple GPUs with a peer-to-peer connection. On multiGPU systems which have an Infinity Fabric Link, this sample uses one GPU to performa a complex simulation and uses the link to transfer data to the other GPU for visualization.

    macOS

    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
  • Rendering Reflections with Fewer Render Passes

    Use layer selection to reduce the number of render passes needed to generate an environment map. This sample demonstrates dynamic reflections on a chrome sphere, using layer selection to render the frame in two passes.

    macOS iOS

    View sample code
  • Rendering Terrain Dynamically with Argument Buffers

    Use argument buffers to render terrain in real-time with a GPU-driven pipeline. This sample demonstrates dynamic terrain generation 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
  • Rendering a Scene with Forward Plus Lighting Using Tile Shaders

    Implement a forward plus renderer using the latest features on Apple GPUs. 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
  • 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
  • Implementing a Multistage Image Filter Using Heaps and Events

    Use events to synchronize access to resources allocated on a heap. This sample demonstrates an alternative to using fences to track data hazards in a multistage image filter.

    macOS iOS tvOS

    View sample code
  • Rendering reflections in real time using ray tracing

    Render realistic real-time lighting by dynamically generating reflection maps using a ray-tracing compute pass. This sample implements a hybrid renderer to draw a model using rasterization and then create ray traced reflections to apply to the model’s surfaces.

    macOS

    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
  • Rendering a Scene with Deferred Lighting in Objective-C

    Avoid expensive lighting calculations by implementing a deferred lighting renderer optimized for immediate mode and tile-based deferred renderer GPUs. This sample uses Objective-C and 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
  • Rendering a Scene with Deferred Lighting in C++

    Avoid expensive lighting calculations by implementing a deferred lighting renderer optimized for immediate mode and tile-based deferred renderer GPUs. This sample uses C++ and 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
  • Rendering a Scene with Deferred Lighting in Swift

    Avoid expensive lighting calculations by implementing a deferred lighting renderer optimized for immediate mode and tile-based deferred renderer GPUs. This sample uses Swift and 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
  • Customizing a TensorFlow operation

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

    View sample code
Rendering with Metal Performance Shaders
  • Metal Performance Shaders for Accelerating Ray Tracing

    Use the Metal Performance Shaders ray intersector to perform ray-traced rendering. This sample demonstrates how accelerate a path-traced renderer 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 with Metal Performance Shaders

    Support dynamic scenes and denoising by extending your ray tracer with Metal Performance Shaders. This sample shows how to render and denoise a dynamic scene in your ray tracer with Metal Performance Shaders.

    macOS iOS

    View sample code