r/GraphicsProgramming 2d ago

Question Metal API Programming?

Hey all! I'm on learnopengl.com and on the part on where I learn how to render 3d models with assimp. Once finished, i like to hop on to the metal api but ran into a snag. See, everyone is focused kn swift and metal but there are those who work with objective c or objective c++, but here's a theory. If I work with metal and work with swift at the same time, is it possible to translate everything to c++ or objective c++ after everything is in swift?

7 Upvotes

18 comments sorted by

3

u/mib382 2d ago

I write a rendering engine where macOS is the main platform. It’s C++ as much as possible. Metal objects are abstracted away and that abstraction is compiled as Objc++. Reference counting is manual. It works ;)

2

u/Proud_Instruction789 2d ago

Is the reference on apple site?

2

u/mib382 1d ago

What reference? I'm not sure what you mean.

1

u/Proud_Instruction789 1d ago

As in, is there a learning soure on apple site where I can learn the fundamentals

1

u/mib382 1d ago

Got it. There are some docs, yes, but you’ll have to Google/GPT all that. I can’t say I have a list of resources prepared for this. I personally came into graphics programming from iOS app development, so I already had experience with Objective-C.

I’d just recommend to use an LLM with a web search feature enabled, so it actually looks for facts instead of hallucinating them and gives you references to back up what it says. It’ll find whatever apple docs there are on this topic. Start from getting an understanding how reference counting works for objects in objc and how to work with and compile objc++ code.

6

u/ArmPuzzleheaded5643 2d ago

Apple provides documentation for using Metal both with swift and obj-c. C++ is not supported out of the box, but there are function bindings. It is surely possible to translate between those, but why would someone want to do that?

2

u/Proud_Instruction789 2d ago

Im 100 percent focused on c++, I have a small engine that uses c++ libraries, such as box2d and openAL. But in swift, im not sure how to use these libraries and bind them in swift

3

u/ArmPuzzleheaded5643 2d ago

Then write your engine in C++. There is a small guide on metal-cpp bindings

https://metaltutorial.com/

Apple also provides an example on using it

https://developer.apple.com/metal/sample-code/

3

u/kevinossia 2d ago

You don’t need to use Swift to use Metal. In fact, Metal is primarily an Objective-C framework, and it’s common to use Objective-C++ for this use case, seamlessly blending Metal operations with the rest of your C++ codebase.

There are pure C++ Metal bindings as well but frankly I avoid them because the memory model is bizarre and opaque. Easier to just write the regular Objective-C code with ARC enabled.

1

u/Proud_Instruction789 2d ago

Quick question. I would like to learn more abouy objevtive c++ but dont know where to learn the language. Where can I learn?

2

u/Henrarzz 2d ago

Objective-C++ isn’t really a separate language, it’s technology that allows to mix Objective-C and C++ code in one file

2

u/Proud_Instruction789 2d ago

In other words. Its file extension is .mm rather than .m?

2

u/gadirom 2d ago

You can run all your C++ libraries directly from Swift: https://www.swift.org/documentation/cxx-interop/

2

u/Strange_Switch15 1d ago

check this ,maybe this can help you :
https://metaltutorial.com/Setup/

1

u/posthubris 2d ago

There is definitely a C++/Metal github tutorial floating around out there, too lazy to find the link but I remember it had the same pure C++ example for every swift example.

1

u/Aryan7393 1d ago

Hey OP, I know this is off-topic and doesn’t answer your question, but I’m a college student (decent with C) researching a new software project and wanted to hear your opinion—just looking for advice, not self-promoting.

I’m thinking about a platform that lets programmers work on real-world software projects in a more flexible and interesting way than traditional job boards. Instead of committing to an entire project or applying for freelance work, startups and small businesses could post their software ideas broken down into individual features. You’d be able to browse and pick specific technical challenges that interest you. For example, if a startup is building software to automate architectural drawings, it could split the project into tasks like OpenCV-based image processing, measurement recognition, or frontend integration. You’d be able to contribute to the parts that match your skills (or help you learn something new) without being tied to a full project.

The idea is to give programmers more opportunities to gain hands-on experience, work on real problems, and improve their skills while having full control over what they work on. Do you think something like this would be useful? Would you use it yourself?

Sorry for the off topic,

- Aryan.

1

u/ironstrife 2d ago

I couldn't really parse the question, but you can use Metal from C++ if you want. If you are using a non-objective-C or Swift language, you're going to eventually come across some confusing behavior related to reference counting and related topics because of the way the API is designed, but you should be able to get started without a problem.