Using metal-cpp with Swift

Is there any way to use metal-cpp in a Swift project? I have a platform layer I've written in Swift that handles Window/View creation, as well as event handling, etc. I've been trying to bridge this layer with my C++ layer as you normally would using a pure C interface, but using Metal instances that cross this boundary just doesn't seem to work.

e.g. Currently I initialize a CAMetalLayer for my NSView, setting that as the layer for the view. I've tried passing this Metal layer into my C++ code via a void* pointer through a C interface, and then casting it to a CA::MetalView to be used. When this didn't work, I tried creating the CA::MetalLayer in C++ and passing that back to the Swift layer as a void* pointer, then binding it to a CAMetalLayer type. And of course, this didn't work either.

So are the options for metal-cpp to use either Objective-C or just pure C++ (using AppKit.hpp)? Or am I missing something for how to integrate with Swift?

Hi Flyingsand,

This setup can become tricky because Swift uses ARC and metal-cpp requires manual memory management (MRR).

In general terms, since you are using a bridging header, this is a good place to import a header file that declares a C interface taking Objective-C types (like CAMetalLayer*). When Swift passes argument to these functions, they will be still under the control of ARC.

In the implementation of your C interface, forward the parameters to your C++ code by performing bridging casts. Not only do these allow you to pass arguments between ARC and non-ARC contexts, but also to express transfer of ownership into and out of ARC.

Using metal-cpp with Swift
 
 
Q