Metal Sample Code
View sample code to see how Metal APIs are implemented.
View sample code to see how Metal APIs are implemented.
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 codeReduce 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 codeDraw 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 codeChoose 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 codeStream 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 codeGenerate 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 codeLearn 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 codeUse 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 codeCreate 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 codeRender 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 codeLoad 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 codeDetermine 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 codeAvoid 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 codeRender 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 codePerform 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 codeAccess 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 codeCreate 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 codeReduce 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 codeUse 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 codeReduce 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 codeMaximize 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 codeChoose 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 codeStream 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 codeReduce 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 codeCompile 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 codeDefine 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 codeImplement 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 codeExplicitly 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 codeTessellate 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 codeImplement 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 codeSwitch 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 codeSwitch 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 codeReplace 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 codeDraw 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 codeInvoke 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 codeModify 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 codeUse 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 codeDraw 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 codeEfficiently 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 codeCreate 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 codeUse 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 codeUse 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 codeImplement 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 codeUse 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 codeUse 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 codeRender 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 codeGenerate 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 codeImplement 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 codeAvoid 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 codeAvoid 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 codeAvoid 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 codeUse Metal to accelerate neural-network training performance of a custom Tensorflow operation.
View sample code
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 codeSupport 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