Skip to content

Add support for aligned memory alloc/realloc/free in GDExtension #12336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
iiMidknightii opened this issue Apr 30, 2025 · 5 comments
Open

Add support for aligned memory alloc/realloc/free in GDExtension #12336

iiMidknightii opened this issue Apr 30, 2025 · 5 comments

Comments

@iiMidknightii
Copy link

Describe the project you are working on

A fluid simulation written in GDExtension that tries to make use of compiler vectorization for performance.

Describe the problem or limitation you are having in your project

The Memory class in Godot currently allows for managing aligned memory through the use of alloc_aligned_static, etc. However, these methods are not exposed to the GDExtension interface like the default ones.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

I propose GDExtensionInterface gains access to those methods. On the other side, godot-cpp would also need to be updated to add the other side of the interface as well.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

It's probably as simple as adding the new methods and implementing them similarly to how the existing non-aligned methods behave. I could probably implement it myself if I knew a little more about the internals of GDExtensionInterface. I think I have a handle on the godot-cpp side, but haven't found the actual implementation of that on the engine side.

If this enhancement will not be used often, can it be worked around with a few lines of script?

It's part of the extension interface so no scripting could tackle this.

Is there a reason why this should be core and not an add-on in the asset library?

I don't think you can modify the extension interface in an add-on.

@dsnopek
Copy link

dsnopek commented Apr 30, 2025

In principle, this makes sense to me: if Godot has it, then godot-cpp should have it too.

However, it doesn't look like these methods are actually used much in the engine. I see it in two files rendering_context_driver_vulkan.cpp and jolt_globals.cpp. I also find it sort of weird that alloc_aligned_static() doesn't use alloc_static(), so any tracking or accounting we want to do there won't happen in the aligned versions.

So, it would be good to get an idea of the intended usage of those functions from the core team: are they meant to be a general purpose thing used by any part of the engine?

It looks like they were added ~10 months ago to support rendering in PR godotengine/godot#90993

In any case, if we do want to expose them to godot-cpp, we have two options:

  1. Reimplement them in godot-cpp (probably using alloc_static() to allocate the memory so it still flows through Godot), or
  2. Add new functions to the GDExtension interface and have godot-cpp use those

Which one makes the most sense I think depends on the intended usage of those functions. I'd personally probably lean towards option nr 1?

I could probably implement it myself if I knew a little more about the internals of GDExtensionInterface.

With this approach, you'd add new function pointers to gdextension_interface.h and register implementations in gdextension_interface.cpp

UPDATE: Ah, I see you posted PR godotengine/godot#105947 doing this :-)

@iiMidknightii
Copy link
Author

I agree that using the existing alloc methods would be best, but I dont think they support any kind of alignment so I don't see how that information would be communicated to them, unless you can pass it through the padding?

Also if the aligned methods aren't using the same tracking as the standard methods, that seems more like an issue with the engine side implementation rather than the interface with extensions. I'd be willing to look at implementing that if you're patient enough to let me learn how.

@dsnopek
Copy link

dsnopek commented Apr 30, 2025

I agree that using the existing alloc methods would be best, but I dont think they support any kind of alignment so I don't see how that information would be communicated to them, unless you can pass it through the padding?

I'm not an expert in this area, but looking at the code, alloc_aligned_static() seems to be allocating some extra bytes, and then adjusting the starting address so that the memory is aligned.

I think we could do the same thing in a custom alloc_aligned_static() within godot-cpp using mem_alloc from the GDExtension interface in place of malloc()

@iiMidknightii
Copy link
Author

iiMidknightii commented Apr 30, 2025

Okay that makes sense. I could try implementing that instead since I haven't finished the godot-cpp side yet.

@iiMidknightii
Copy link
Author

iiMidknightii commented Apr 30, 2025

I created a PR on the godot-cpp side that implements both an update to the extension interface (# 2 on your suggestions) as commented code, as well as a re-implementation of the engine's aligned managment using the existing interface methods (# 1 on your suggestions). If/when you decide which is the better implementation (if any is desirable at all), I can update this accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants