A Metal implementation on top of Vulkan. Implements a C++ API similar to the official Metal C++ API. This is meant to be a library used internally by an ABI-compatible implementation of Metal. It is NOT a drop-in replacement for Metal in an existing codebase.
The current implementation targets Vulkan 1.3. This version of Vulkan includes
several dynamic pipeline features which allow us to match the behavior of
several similar Metal features. Alternatively, the
VK_EXT_extended_dynamic_state and VK_EXT_extended_dynamic_state2 features
should also be enough, though support for these extensions as substitutes for
Vulkan 1.3 support is not currently implemented.
Indium also requires timeline semaphore support (supported by most devices).
Because this library isn't meant to depend on Foundation or CoreFoundation or
even an Objective-C runtime, we do have to deviate from the official C++ in some
notable ways. For example, where the official API returns NSArray, we return
std::vector; where it returns NSString, we return std::string; where
Clang blocks are used, std::functions are used instead.
Additionally, instead of using manual reference counting tied to Objective-C's
retain and release methods, we use C++ shared_ptrs.
Additionally, Metal's official C++ API is essentially a thin wrapper over the Objective-C Metal API. As such, all its structures, including mutable descriptor structures only used to create other objects, are created as shared objects. In contrast, Indium's descriptor structures are not created as shared objects since these aren't required to stick around; we only need them to hold user information for creating other objects. An ABI-compatible Metal implementation that uses Indium internally is expected to implement its own (shared) versions of these descriptors and translate their information into an Indium descriptor to pass it to a method that needs it.
This repository also includes a supplementary library called IndiumKit which aims to replicate the MetalKit API. It's mainly intended to be used for testing code that uses Indium and not for actual use e.g. in an ABI-compatible implementation of Metal that uses Indium. Most of the MetalKit API requires implementation details that an ABI-compatible implementation of Metal would need to implement itself.
Iridium is a library that can be used independently of Indium for translating pre-compiled Metal shaders (in the AIR format) to SPIR-V shaders that Indium can use.
Indium uses Iridium internally to translate shaders at runtime, but a CLI tool
for translating shaders called mtl2spv is also included in this repository,
mainly for testing purposes.
Indium keeps two things on disk so that a launch does not redo them. Both are per user, per device and driver or per translator build, and neither is needed for correctness: anything that fails validation is deleted and made again.
| What | Where (default) | Key |
|---|---|---|
| Translated libraries (SPIR-V and function descriptions) | <cache>/org.darlinghq.indium/translations/v<layout>-<translator>/<key>.indtr |
SHA-256 of the AIR and the translator id |
Vulkan pipelines (VkPipelineCache) |
<cache>/org.darlinghq.indium/pipelines/<device, driver, executable>.indpc |
vendor, device, driver version, pipeline cache UUID, device UUID, executable |
<cache> is $XDG_CACHE_HOME or ~/.cache (the user cache directory,
confstr(_CS_DARWIN_USER_CACHE_DIR), on Darwin, so inside a Darling prefix the
cache lives in that prefix).
The translator id is libiridium's own (Iridium::translatorId()): a SHA-256 made
at build time of Iridium's sources and headers, the LLVM it was built against
and the compiler flags, then the version of the libLLVM it loaded; libindium
adds the id of its record encoder and the host's word size and byte order.
Entries of another build are never read; they are the oldest files and leave
through the size cap. A change to what a record holds (a field added to
Iridium's FunctionInfo, say) must raise TranslationRecord::layoutVersion
and extend translation-record.cpp; the encoder binds each struct's members
by name and checks each enumeration with a -Wswitch error, so it stops
compiling until that is done.
A library that nobody has translated is translated once however many threads
and processes ask for it at the same time (an in-process lock and an flock
on <key>.indtr.lock); the others wait and read the result.
| Variable | Effect |
|---|---|
INDIUM_PIPELINE_CACHE_DIR=off (or empty) |
Kill switch: neither cache touches the disk. |
INDIUM_PIPELINE_CACHE_DIR=<dir> |
Pipeline files go in <dir>, translations in <dir>/translations. |
INDIUM_TRANSLATION_CACHE_DIR=<dir> or off |
Where the translations go, or turn only that cache off. |
INDIUM_TRANSLATION_CACHE_MAX_MB=<n> |
Cap on the translation files (default 512); 0 turns them off. The pipeline file is capped at 32 MiB. |
INDIUM_CACHE_STATS=1 |
Print what each cache did to stderr when the process exits. |
A directory that cannot be created, or that others can write to, turns the disk
cache off with one line on stderr. indium-translation-bench (built with
ENABLE_TESTS) times newLibrary and pipeline creation for given metallibs
and, run once, fills the translation store for them.