recp / cmt

🎮 C Bindings/Wrappers for Apple's METAL framework
MIT License
164 stars 6 forks source link

New Metal API Bindings #5

Open recp opened 2 years ago

PhilipVinc commented 2 years ago

ideally we should find a way to parse apple docs/headers from the frameworks and autogenerate cmt bindings...

recp commented 2 years ago

@PhilipVinc that could be nice, documentations also could be auto-imported maybe in this way

Lu-TheCoder commented 9 months ago

Can you give an example how to properly use this library? I can't seem to get it working especially when I have to get the Drawable

recp commented 9 months ago

Hi @Lu-TheCoder

cmt does need some updates to fully support all functionalities of Metal. However, I haven’t had the time to work on it recently.

Apple has released Metal for C++ ( https://developer.apple.com/metal/cpp/) which might be a suitable alternative if using C isn't a strict requirement for your project.

Example project[s] would be nice, but unfortunately, I can’t promise to provide any in the near future due to my current time constraints. I hope to revisit this later when I have more availability.


Currently, my primary focus has shifted to libgpu ( https://github.com/recp/gpu ), which aims to provide a unified API on top of Metal, DirectX12, Vulkan, etc., and the Universal Shading Language ( https://github.com/UniversalShading/spec ) which aims to provide a single shading language that can be used with/without libgpu.

Thanks

Lu-TheCoder commented 9 months ago

Thank you for the response...

I totally understand and yes I am aware of the Metal-CPP alternative and I was hoping for a C only solution as I'm strictly developing an engine in C... I have also began building on top of your C-Bindings and the issue I'm faced with is Memory management I can't seem to get rid of memory leaks once the Objective-c objects are passed onto C it becomes a challenge to manage that memory since ARC doesn't work in C nor do the autorelease pools truly natively work without including Objective-c headers...

recp commented 9 months ago
MT_EXPORT
void*
mtRetain(void *obj);

MT_EXPORT
void
mtRelease(void *obj);

These could help to manage memory manually, enabling ARC when building cmt could be an option to let ObjC objects do retain/release automatically. In this case pointers in C may become invalid if ARC objects are released in somewhere... you should be careful...

In cmt, new/create functions are marked as CF_RETURNS_RETAINED which is a hint for ARC also manual memory management, actually if function name contains Create, New, Alloc, Copy clang assumes the returned memory is retained (IIRC), in cmt these functions [should] return retained result.

If there are still memory leaks, Xcode Instruments could help to catch them more quickly. If there are any bugs in cmt which cause mem leaks, then it would be nice to fix them asap.

--

FWIW, https://github.com/recp/gpu is written in C and similar to Metal but there is no ETA for now.