There is a situation where Apple's Metal graphics API needs to be directly called in a C++ project. Therefore, metal-cpp provides a lightweight bridge.
What is Metal-cpp
The C++ interface library officially released by Apple is Metal-cpp, which allows developers to bypass Objective-C and use Metal functions directly in C++ code. This library is mainly aimed at graphics applications, games or engines written in C++, allowing them to integrate the graphics acceleration capabilities of Apple devices.
This is a thin layer of encapsulation, and its essence is to convert Metal's Objective-C API into a form that can be called by C++. This design avoids the complexity and overhead of cross-language calls, allowing C++ developers to use Metal objects and methods more naturally.
Why choose Metal-cpp
If your game engine or graphics application core is written in C++, then introducing Metal-cpp can avoid mixing a large amount of Objective-C code into calling Metal. It maintains the consistency of the project language and simplifies the build process.
If you develop graphics programs that require high performance, a direct C++ interface can reduce function call overhead. metal-cpp is a header file library that is directly inlined during the compilation process. Compared with calling through a bridge, it can obtain closer to the underlying performance.
#define NS_PRIVATE_IMPLEMENTATION
#define CA_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#include
#include
#include Preparation before you start
An Apple computer needs to run macOS and install the latest version of the Xcode development tool. Metal is a proprietary technology of the Apple platform, so the development environment cannot be bypassed.
Download the release package named metal-cpp from the Apple developer website, and after unzipping it, you will see a "metal-cpp" folder containing a large number of header files. This is the entire library.
#include
#include
#include Project configuration and integration
In the Xcode project, the key step is to add the path of the metal-cpp folder to the "Header Search Paths" of the project. This way the compiler can find all necessary header files.
Since metal-cpp is a pure header file library, it still requires a small piece of code to initialize the underlying implementation. You need to add a line of macro definition like #define NS_PRIVATE_IMPLEMENTATION to any .cpp source file. This part of the code should only appear once.
Use a single header file
To facilitate inclusion, metal-cpp provides a script for generating a single aggregate header file. Go to the decompression directory in the terminal and run the provided Python script, and a file named Metal.hpp will be generated.
./SingleHeader/MakeSingleHeader.py Foundation/Foundation.hpp QuartzCore/QuartzCore.hpp Metal/Metal.hppAfter adding this file named Metal.hpp to your project, you only need to include this single file to take advantage of all Metal-cpp features. However, remember that the macro definition used to implement the code still has to be completed in a certain .cpp file.
Memory management essentials
Metal-cpp objects that follow Apple's reference counting rules need to be managed manually because C++ objects do not participate in Objective-C's automatic reference counting (ARC).
After creating an object through Metal-cpp, call release() method at the appropriate time just like in Objective-C. Ignoring this can lead to memory leaks, which is a detail that C++ developers need to pay special attention to during use.
#define NS_PRIVATE_IMPLEMENTATION
#define CA_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#include Are you currently developing, or planning to develop, a C++ graphics project across different platforms? And will you consider using metal-cpp to optimize the performance of Apple devices in a targeted manner? Welcome to share your opinions or experiences in the comment area. If you feel this article is helpful, please support it by giving it a like.


