I am new to Objective C and I am trying to port my OpenGL based 3D rendering engine to Metal. In the current state, the Window creation and View controller management are in C# (based on Xamarin.iOS) and Rendering part is handled in Native ( C++ & OpenGL).
I am thinking of creating the CAMetalLayer in the ViewController in C# and rest of the Metal related rendering code in Objective C. Can anyone tell me how can I pass the reference of the CAMetalLayer instance and use it in Objective C code.
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
mtlDevice = Metal.MTLDevice.SystemDefault;
mtlLayer = new CAMetalLayer();
mtlLayer.Device = mtlDevice;
mtlLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm;
mtlLayer.FramebufferOnly = true;
mtlLayer.Frame = View.Layer.Frame;
View.Layer.AddSublayer(mtlLayer);
}
How can I pass the reference of the mtlLayer which is created in C# to objective C?
Or I can create the mtlLayer in Objective C, but how do I do the View.Layer.AddSublayer()?
Found the answer to my own question after some reading.
From C#
Pass the mtlLayer.Handle(IntPtr) to native through DllImport
In Native C/Cpp library
In the Objective Cpp (.mm file)