【问题标题】:Getting the ObjectiveC reference of a C# Object (Xamarin.iOS, CAMetalLayer)获取 C# 对象的 ObjectiveC 引用(Xamarin.iOS、CAMetalLayer)
【发布时间】:2021-05-06 15:15:43
【问题描述】:

我是 Objective C 的新手,我正在尝试将基于 OpenGL 的 3D 渲染引擎移植到 Metal。在当前状态下,Window 创建和 View 控制器管理在 C# 中(基于 Xamarin.iOS),而 Rendering 部分在 Native(C++ & OpenGL)中处理。

我正在考虑在 C# 中的 ViewController 中创建 CAMetalLayer,并在 Objective C 中创建与 Metal 相关的其余渲染代码。谁能告诉我如何传递 CAMetalLayer 实例的引用并在 Objective C 代码中使用它。

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);  



}

如何将在 C# 中创建的 mtlLayer 的引用传递给目标 C?

或者我可以在 Objective C 中创建 mtlLayer,但是如何执行 View.Layer.AddSublayer()?

【问题讨论】:

    标签: ios objective-c metal cametallayer


    【解决方案1】:

    经过一番阅读,找到了我自己问题的答案。

    来自 C#

    1. 通过 DllImport 将 mtlLayer.Handle(IntPtr) 传递给原生

       public class Engine
       {
           [DllImport("__Internal", EntryPoint = "nativeMetalInit")]
           public static extern void Init(
               IntPtr device,
               IntPtr layer);
      
       }
      
       Engine.Init(mtlDevice.Handle, mtlLayer.Handle);
      

    在原生 C/Cpp 库中

    API void nativeMetalInit(void* device, void *layer)
    {
        metalInit(device, layer);
    }
    

    在目标 Cpp(.mm 文件)中

    void metalInit(void *device, void *layer)
    {
        pRenderer = [[NativeRenderer alloc] init];
        [pRenderer initilalizeMetal: device andLayer:layer];
    }
    
    - (void)initilalizeMetal: (void*) pDevice andLayer: (void*) pLayer 
    {
        self.mtlLayer = (__bridge CAMetalLayer*) pLayer;
        self.mtlDevice = (__bridge id<MTLDevice>)pDevice;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-13
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多