HexaEngine / Hexa.NET.ImGui

A .NET wrapper for the Dear ImGui.
MIT License
57 stars 5 forks source link

Using Hexa.NET.ImGui with Raylib #12

Closed MEMESCOEP closed 3 days ago

MEMESCOEP commented 2 weeks ago

How can I use this library (along with ImGuizmo) with Raylib? Is it possible to do that?

JunaMeinhold commented 2 weeks ago

I never used raylib, but i found a repo/person using it along with raylib. So probably ask them or just rip from their code.

https://github.com/mufaka/Nickel.AI https://github.com/mufaka/Nickel.AI/blob/master/Nickel.AI.Desktop/rlImGui/rlImGui.cs

But you could also wait a bit until i upload the Raylib bindings on nuget (https://github.com/HexaEngine/Hexa.NET.Raylib) I make a sample with ImGui when that's done (on the Raylib repo from me).

MEMESCOEP commented 2 weeks ago

@JunaMeinhold I tried implementing the code form that repo, but there were types that didn't exist. Once those were commented out, I received the following error: Could not load file or assembly 'Hexa.NET.ImGui, Version=1.91.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

Do you know what could be causing this?

On a separate note, how close are the raylib bindings to being complete?

MEMESCOEP commented 2 weeks ago

Ignore that error, clearing my build cache fixed it.

JunaMeinhold commented 2 weeks ago

image The minimal example works. I currently port the raylib imgui backend from cpp to c#. So it will not take long.

JunaMeinhold commented 2 weeks ago

Update: it looks good image

JunaMeinhold commented 2 weeks ago

Done. Everything is available under https://github.com/HexaEngine/Hexa.NET.Raylib

MEMESCOEP commented 2 weeks ago

Awesome, thanks! I'll test it out. Hopefully I can get ImGuizmo to render things now

MEMESCOEP commented 2 weeks ago

@JunaMeinhold I'm unable to draw anything without crashing now. The error I get is:

Runtime error: Object reference not set to an instance of an object.
       at MAGE.ImGuiRaylibPlatform.ImGuiRenderTriangles(UInt32 count, UInt32 indexStart, ImVector`1* indexBuffer, ImVector`1* vertBuffer, Void* texturePtr) in /media/andrew/1D8E170E787C927A/Programming/C#/MAGE/Shared/Objects/ImGuiRaylib.cs:line 185
   at MAGE.ImGuiRaylibPlatform.RenderDrawData(ImDrawData* draw_data) in /media/andrew/1D8E170E787C927A/Programming/C#/MAGE/Shared/Objects/ImGuiRaylib.cs:line 433
   at MAGE.ImGuiManager.EndFrame() in /media/andrew/1D8E170E787C927A/Programming/C#/MAGE/Shared/Objects/ImGuiManager.cs:line 186
   at MAGE.Editor.WindowLoop() in /media/andrew/1D8E170E787C927A/Programming/C#/MAGE/Editor/Editor.cs:line 1204

-- EDIT -- I'm also getting segmentation faults, which I narrowed down to the SetShaderValue function. I suspect it's because of the void pointer argument. Do you know how to set up shaders with this library?

JunaMeinhold commented 1 week ago

The same way as with the c lib itself, its a 1:1 mapping, but with C# naming conventions.

JunaMeinhold commented 1 week ago

https://www.raylib.com/examples.html

https://github.com/raysan5/raylib/blob/master/examples/shaders/shaders_model_shader.c

JunaMeinhold commented 1 week ago

image It's strange because the example works on both windows (11) and linux (kubuntu 24.04, KDE Plasma, Wayland)

MEMESCOEP commented 1 week ago

@JunaMeinhold Dunno why, but I never got any notifications about your responses, and refreshing the page didn't show anything new (browser cache maybe?)

Anyway, I did get ImGui to work, but there are some memory leaks that I wanted to address. It looks like calling the GetWindowSize function enough causes memory leaks (about 100kb / 3-10 seconds in my case).

I'll keep testing, but for now it seems like some objects just aren't being freed like they should be. I haven't yet gotten ImGuizmo to render anything.

MEMESCOEP commented 1 week ago

@JunaMeinhold I'm unable to draw anything without crashing now. The error I get is:

Runtime error: Object reference not set to an instance of an object.
       at MAGE.ImGuiRaylibPlatform.ImGuiRenderTriangles(UInt32 count, UInt32 indexStart, ImVector`1* indexBuffer, ImVector`1* vertBuffer, Void* texturePtr) in /media/andrew/1D8E170E787C927A/Programming/C#/MAGE/Shared/Objects/ImGuiRaylib.cs:line 185
   at MAGE.ImGuiRaylibPlatform.RenderDrawData(ImDrawData* draw_data) in /media/andrew/1D8E170E787C927A/Programming/C#/MAGE/Shared/Objects/ImGuiRaylib.cs:line 433
   at MAGE.ImGuiManager.EndFrame() in /media/andrew/1D8E170E787C927A/Programming/C#/MAGE/Shared/Objects/ImGuiManager.cs:line 186
   at MAGE.Editor.WindowLoop() in /media/andrew/1D8E170E787C927A/Programming/C#/MAGE/Editor/Editor.cs:line 1204

-- EDIT -- I'm also getting segmentation faults, which I narrowed down to the SetShaderValue function. I suspect it's because of the void pointer argument. Do you know how to set up shaders with this library?

Going back to this, I was able to pinpoint the crashing to drawing images that were loaded with Hexa.NET.Raylib. I suspect it also has to do with pointers, and the fact that I haven't updated it to draw image data yet. How can I draw images?

MEMESCOEP commented 1 week ago

https://www.raylib.com/examples.html

https://github.com/raysan5/raylib/blob/master/examples/shaders/shaders_model_shader.c

I've set up the shader like the C example, however it does not show up and instead turns black. Were you able to get a skybox rendering?

JunaMeinhold commented 1 week ago

@JunaMeinhold Dunno why, but I never got any notifications about your responses, and refreshing the page didn't show anything new (browser cache maybe?)

Anyway, I did get ImGui to work, but there are some memory leaks that I wanted to address. It looks like calling the GetWindowSize function enough causes memory leaks (about 100kb / 3-10 seconds in my case).

I'll keep testing, but for now it seems like some objects just aren't being freed like they should be. I haven't yet gotten ImGuizmo to render anything.

The memory leak is weird especially on that frequency, do you call ImGui.GetWindowSize or the raylib GetWindowSize. Because the first one is guaranteed memory leak free.

But it's also possible that ImGui allocates memory it has something like a GC and it's collecting in a fixed interval.

Try using profilers like dotMemory or .NET Memory Profiler

JunaMeinhold commented 1 week ago

Could you maybe show me your code, it's hard to find anything out without it.

There could be a million things that cause things like that, like your shader is invalid and the compilation returns null and you forgot to handle that. Or you are passing invalid parameters. If you give me the code i can do mixed mode debugging and look what causes it.

@MEMESCOEP

MEMESCOEP commented 1 week ago

@JunaMeinhold I fixed the shader issue (dotnet compilation weirdness again), but not the memory leak issue. It could be due to calling GetWindowSize from Lua, but I'm not really sure how that would cAuse memory leaks. I can send some code for that in a little while.

I'll run some profilers like you suggested, and I'll report back if I find anything useful

JunaMeinhold commented 1 week ago

Could be a NLua issue, i used it for my game engine but then gave up on it since it does reflections and creates GC Pressure. And i couldn't find a way to avoid these issues so i switched to c# scripting, GC Pressure kills your framerate and creates stutter.

@MEMESCOEP

MEMESCOEP commented 1 week ago

@JunaMeinhold it's most likely an NLua issue, I've been trying to get C# scripting working but I just don't know how to go about it. Roslyn didn't go well either as I couldn't get it to install properly.

JunaMeinhold commented 1 week ago

i do it like this https://github.com/HexaEngine/HexaEngine/blob/main/HexaEngine/Scripts/ScriptAssemblyManager.cs @MEMESCOEP

MEMESCOEP commented 1 week ago

@JunaMeinhold does it load precompiled code? If so, how do you give users access to engine libraries?

JunaMeinhold commented 1 week ago

image this in your script assembly project and .NET handles the rest automagically. And then pass the script assembly dll path to the Load function and done. @MEMESCOEP

MEMESCOEP commented 1 week ago

So you can just export some dlls and then dotnet will handle the rest?

JunaMeinhold commented 1 week ago

Yep, dotnet resolves them for you. The following condition must be true so that it works, the references assemblies are loaded already, the referenced assemblies are in the AppDomain.CurrentDomain.BaseDirectory (there where your executable is), or in a system specific location like system32 or usr.

@MEMESCOEP

MEMESCOEP commented 1 week ago

I'll give this a try on a little bit, it seems quite promising!

Thank you for your help :)

MEMESCOEP commented 1 week ago

@JunaMeinhold You have separate csproj files for each of those DLLs right?

JunaMeinhold commented 1 week ago

Yes i made my game engine modular, mainly because of the custom graphics agnostic api which is still wip but it's working. And I use some modules in other projects that are not related to the engine, the modules that are not part of the engine itself or connected in anyway have the names Hexa.NET.* @MEMESCOEP

MEMESCOEP commented 1 week ago

Ah

MEMESCOEP commented 3 days ago

I think I've gotten this reasonably figured out, so I'll close this issue