Possibility of using C++ directly with Metal API

Is there any plans to introduce the possibility of using C++ directly with Metal API? C++ would seem to be a perfect fit as a proven performance language for connection to Metal. The best performance in games will be achievable by fusion C++ and Metal. What do you think about this feature?

Yes I would like that as well. I was initially suprised that the Metal API was in Objective-C, but I think they did that so the API could be transpiled to Swift. At least Objective-C++ works rather well and so Metal was easy to integrate in our C++ engine.

I think persuing this would be counterproductive for the human race. ⚠


C++ is old and not as suitable for human use as Swift is. The solution is to stop using fake-C++ for writing shaders, as Swift becomes able to fully replace what we have now. Although Metal SL is a decent step up from GLSL and Cg, it's not Swift. Not embracing Swift for all programming tasks, and improving that dog food via the eating and getting back to the chef, keeps us from programming well.

You can intermix C++ and Objective-C in Object-C++.


So if you have an Objective-C file called foo.m just change the file extension to .mm (foo.mm)


Then the compiler can handle C++ along with Objective-C.


Works like a charm.

I was thinking of something along the lines of NativeActivity for Android. Solution intended mainly for games and multimedia apps. The current intermix C++ and Objective-C in Object-C++ gives additional overhead that could be eliminated.

According to an Apple GPU driver developer, they did prototype a non-Objective C API but the overhead of Objective-C method calls in Metal was about the same or slightly better than the alternative.


Having the external Metal APIs be in another language wouldn't magically give better real-world performance, and it's trivial to call Objective-C APIs from C or C++ code by making it compile as Objective-C or Objective-C++.

"it's trivial to call Objective-C APIs from C or C++ code"


Agreed. But there are non-trivial performance costs. Just profile your app and see how much time objc_msgSend() eats. You realize method calls in Obj-c are dynamic dispatch, right? Same with properties. Just compiling obj-c with c/c++ is completely not the issue.


"in another language wouldn't magically give better real-world performance"


Yes, it can. Obj-C and C++ have some fundamentally different internals. People are asking this for real reasons, not for language religion. My desire is based on our own real on-device profiling data.


In our engine, we've resorted to making C++ shadow versions of some Metal api classes just to reduce costs on hot paths. I'll add another vote for a real native compiled API, but not expecting it to happen.

As I said, Apple already prototyped what you're suggesting and did not find any worthwhile performance benefit. Calling C++ (virtual) methods has its own overhead as well, and objc_msgSend has become very optimized over the years for what it does.


Look into this function if you want the absolute maximum performance at all costs: https://developer.apple.com/library/ios/documentation/Metal/Reference/MTLCommandQueue_Ref/index.html#//apple_ref/occ/intfm/MTLCommandQueue/commandBufferWithUnretainedReferences

which language can take interaction with hardwares directly?

Only c/c++ until now, I think that's why shader languanges(GLSL, HLSL, MetalShader) are writen in c++ syntax

Every cross-platform engine is written in C + + . Performance aside, having Metal C + + API would save tremendous amount of effort for developers worldwide.

Try to use some C++ Wrappers like as https://github.com/naleksiev/mtlpp

Good news! We now have a C++ wrapper for Metal!

https://developer.apple.com/metal/cpp/

That's great news. Thanks for sharing. Hopefully this will help with the porting efforts of many cross-platform programs.

Possibility of using C++ directly with Metal API
 
 
Q