A collection of open source C++ examples for Metal
These examples use a modified version of Metal-CPP originally published by Apple. This modified version is maintained here: metal-cpp and includes some additional API bindings not yet supported by Apple, however it is actively updated with the latest fixes and additions from Apple as well.
These samples are developed using C++20 and target the following platforms and os versions:
Platform | OS Version |
---|---|
macOS | 14.0+ |
iOS | 17.0+ |
tvOS | 17.0+ |
These example applications use CMake and are actively developed using both CLion and Xcode
For those wanting to build the projects from a terminal invoking CMake directly or to generate the Xcode project files here are some example commands:
git clone https://github.com/MattGuerrette/Metal.git
cd Metal
mkdir cmake-build-debug
cd cmake-build-debug
cmake .. -GNinja
cmake --build .
git clone https://github.com/MattGuerrette/Metal.git
cd Metal
mkdir cmake-build-xcode
cd cmake-build-xcode
cmake .. -GXcode -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=<Apple Developer ID>
cmake --build .
For codesigning purposes, you will want to specify the development team ID as shown above to avoid needing to manually set the development team for each target project.
Basic example for rendering a colored triangle using Metal
Rendering of multiple cube geometry instances
Showcases loading of KTX compressed textures in ASTC format into a MTLHeap and using Argument Buffers to bindlessly render multiple textures from GPU memory.