# Metal-C++ bindings

**URL:** <https://forums.unrealengine.com/t/metal-c-bindings/1159694>\
**Category:** Platform & Builds\
**Tags:** apple, metal, unreal-engine, Engine-Source-Code, CPP\
**Created:** [May 9, 2023, 3:19am UTC](https://forums.unrealengine.com/t/metal-c-bindings/1159694 "2023-05-09T03:19:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![philipturner](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@philipturner](https://forums.unrealengine.com/u/philipturner)\
**Post date:** [May 9, 2023, 3:19am UTC](https://forums.unrealengine.com/t/metal-c-bindings/1159694/1 "2023-05-09T03:19:47Z")

</div>

Unreal Engine currently uses `mtlpp`, a C++ wrapper for Metal that was developed in 2016. At that time, Metal could only be used from Objective-C, which was a major hindrance. For example, I was an iOS beginner who had just invested time learning Swift, the language that replaced Objective-C. Yet the Metal sample code was all in ObjC! I had to manually convert it to another language I could understand (either Swift or C++).

`mtlpp` has been [unmaintained](https://github.com/naleksiev/mtlpp) since 2018, but UE5 still depends on it. It lacks support for Metal 3 features, such as GPU virtual addresses, and will experience more software rot in the future. Meanwhile, Apple released [sample code](https://developer.apple.com/documentation/metal/metal_sample_code_library/rendering_a_scene_with_deferred_lighting_in_c) in 2019 for using Metal with C++. In 2022, they officially announced the [metal-cpp](https://developer.apple.com/metal/cpp/) bindings. The API is very easy to use and has a pleasant [tutorial](https://developer.apple.com/metal/LearnMetalCPP.zip). The documentation explains things like Cocoa’s ARC memory management. It is constantly being maintained and has an official C++ API from Apple.

For example, here are various examples of metal-cpp being used:

```cpp
 // Use the buffer's allocated size, never its actual length, when doing size
// calculations.
MTL::Buffer* phantom_buffer = _heap->newBuffer(size, 0);
int64_t offset = phantom_buffer->gpuAddress() - _heap_va;
int64_t phantom_size = phantom_buffer->allocatedSize();
_phantom_buffers.insert({ offset, NS::TransferPtr(phantom_buffer) });

auto heapDesc = NS::TransferPtr(MTL::HeapDescriptor::alloc()->init());
heapDesc->setHazardTrackingMode(MTL::HazardTrackingModeUntracked);
heapDesc->setStorageMode(MTL::StorageModeShared);
heapDesc->setType(MTL::HeapTypeAutomatic);
heapDesc->setSize(0);
auto heap = device->newHeap(heapDesc.get());
if(!heap) {
  throw error_type::memory_allocation_error;
}
this->_heap = NS::TransferPtr(heap);
heap->setPurgeableState(MTL::PurgeableStateEmpty);

// There should be only one device.
NS::Array* all_devices = MTL::CopyAllDevices();
NS::UInteger num_devices = all_devices->count();

for (NS::UInteger i = 0; i < num_devices; ++i) {
  MTL::Device* mtl_device = (MTL::Device*)all_devices->object(i);
  metal_hardware_context hardware_context = metal_hardware_context{mtl_device};
  this->_devices.push_back(hardware_context);
}

```

I am humbly requesting that Epic Games migrate to the new bindings for engine source code.
