dauthleikr
Topic Author
Posts: 4
Joined: 27 Dec 2023, 00:30

Integration into mixed C++/C# engine

15 Feb 2025, 20:56

We are trying to integrate noesis into our engine, which has a thin C++ layer using vulkan, and a C# layer (with mostly game logic) on top. We would like to use noesis from C#, but since the core rendering code is C++, we have to implement our RenderDevice there.

Please consider the following code:
	void GrNoesis_InitRenderer(GrInstance instance, Noesis::IRenderer* renderer)
	{
		renderer->Init(instance.Handle->NoesisRenderDevice);
	}
	
This initializes a renderer of a View with a previously constructed RenderDevice. We call this method from C#:
    [DllImport(LowLevelLibrary)]
    private static extern void GrNoesis_InitRenderer(GrInstance instance, IntPtr renderer);

    public static void GrNoesis_InitRenderer(GrInstance instance, Renderer renderer)
    {
        GrNoesis_InitRenderer(instance, BaseComponent.getCPtr(renderer).Handle);
    }
Sadly initialization seems to not work (the next Update-call on the view crashes with an access-violation, and if we do any other calls, e.g., renderer->GetClassType()->GetName(), we crash immediately). It seems like the pointer we are passing is not what we expected (IRenderer). We'd appreciate any help, and information whether we are approaching this the correct way (we could also use a C# RenderDevice, which just PInvokes our C++ layer, but to have C++ -> C# -> C++ transitions for every call seems wasteful).
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Integration into mixed C++/C# engine

17 Feb 2025, 19:29

Our Noesis managed library includes a native implementation of the RenderDevice for Vulkan. Unfortunately it is not exposed to C# as it happens for D3D11, D3D12 or GL. We have plans to expose Vulkan too: #2630

In the PInvoke you're probably passing the pointer to the implementation class, not the interface, so you'll need something like this:
void GrNoesis_InitRenderer(GrInstance instance, Noesis::BaseComponent* renderer_)
{
    Noesis::IRenderer* renderer = Noesis::DynamicCast<Noesis::IRenderer*>(renderer_);
    if (renderer) renderer->Init(instance.Handle->NoesisRenderDevice);
}
And the IRenderer::Init() expects an instance of the RenderDevice (the Vulkan RenderDevice implementation in your case).
 
dauthleikr
Topic Author
Posts: 4
Joined: 27 Dec 2023, 00:30

Re: Integration into mixed C++/C# engine

19 Feb 2025, 19:36

Oh hey, bug 2630 is me from a year ago :D

Unfortunately the renderer pointer is null after the Noesis::DynamicCast.

It's kind of a pity that the managed library does not play nice with the kind of mixed language engine design we are running ... for example there is also an _initialized flag in the GUI class, which does not get set of course if we do the initialization from C++.

I guess we could implement the vulkan render device on the C# side, but that is a lot of work for something we already have. But if you think that is our best bet to get running within a month or two, we'll do it :)
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Integration into mixed C++/C# engine

24 Feb 2025, 11:24

It's kind of a pity that the managed library does not play nice with the kind of mixed language engine design we are running ... for example there is also an _initialized flag in the GUI class, which does not get set of course if we do the initialization from C++.
If you are using the C# SDK, you must initialize Noesis with the C# API. Otherwise, you will likely encounter many issues like the one you mentioned.

This shouldn't prevent you from extending Noesis with your custom C++ classes. However, I am not sure how flexible the API is for this without access to our source code. Please feel free to report any issues you encounter, but make sure that Noesis is initialized from C#.
 
dauthleikr
Topic Author
Posts: 4
Joined: 27 Dec 2023, 00:30

Re: Integration into mixed C++/C# engine

13 Mar 2025, 21:05

Thanks for your answers. In the end we got it working by doing the full initialization procedure in C#, and implementing a RenderDevice in C#, which PInvokes into our C++ implementation. The current solution is not optimal since calls go like (Noesis) -> (C# implementation) -> (Our Vulkan C++ Engine), so doing without the C# layer would have been nice, but oh well.

If you ever expose the built-in vulkan render device to C#, we would gladly ditch our Frankenstein :)
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Integration into mixed C++/C# engine

14 Mar 2025, 15:06

Exposing the Vulkan RenderDevice to C# should be trivial (it’s just a matter of exposing a CreateVulkanDevice function to C#). If this is what you need, please create a ticket for it.

#2630 is for creating the RenderContext, which is more time-consuming. However, I assume you’ve already solved that.
 
dauthleikr
Topic Author
Posts: 4
Joined: 27 Dec 2023, 00:30

Re: Integration into mixed C++/C# engine

17 Mar 2025, 21:28

Thank you - you are right, I did not understand the difference between RenderDevice and RenderContext back then :)
Ticket: https://www.noesisengine.com/bugs/view.php?id=4003
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Integration into mixed C++/C# engine

18 Mar 2025, 04:24

Thank you! :)

Who is online

Users browsing this forum: No registered users and 7 guests