Skip to main content
August 26, 2021

Bringing The Latest Metal API to Wave Engine

In our effort to collaborate between the Mobile Team (for which I work) and the Research one, I have been assigned the task of building our own .NET binding for Apple’s Metal API.

The main goals are:

  1. Be able to keep it updated with the latest releases.
  2. Performance: Making it as thin and lightweight as possible.

Metal is the set of graphic APIs that Apple offers along with its devices. We can quickly see it as the replacement for OpenGL, or the equivalent of DirectX on Windows. My colleagues, working on Wave Engine, have been testing different bindings provided by the community but are lacking the latest updates from Apple.

METAL

Metal API

Metal API has evolved since such bindings were created and, as we have already done with WebGL, we would like to auto-generate them on demand. This allows us to quickly align with the latest graphic APIs in order to take advantage of all their benefits.

The Metal API is available in the form of a set of Objective-C files, which can be found inside Xcode.app. We need a tool to read those and generate a C# equivalent, consumable from .NET. We want it to be platform agnostic, in terms of generating a small DLL which directly calls Metal API with the help of the Objective-C Runtime, everything through P/Invoke.

Since Objective-C is a much more complex language than, for example, IDL (WebGL was defined in IDL), we have chosen ANTLR, along with its public Objective-C grammar. This last needs some effort to support some characteristics used along with the Metal API, but it looks promising enough to provide an abstract syntax tree (AST) representation in memory, which later is translated into C#.

MTLResource.h (Objective-C) vs. its equivalent MTLResource.cs (C#)

We are currently working out the enums defined along with the entire API. Step by step, the grammar is getting stronger when passed through the entire codebase, which will help us with the rest of the translations.

Once we have something complete and stable enough, we will turn the repository public, so everyone can benefit from it, along with OpenGL.NET, Vulkan.NET and OpenXR.NET.

Also, if you want to know more about our latest graphic engine, discover Evergine!

Author
Marcos Cobeña Morián
Plain Concepts Mobile