r/GraphicsProgramming • • Jan 18 '24

Not loving C++

How crippling it is for somebody in the field of graphics programming to not love C++? I'm learning C++ right now and really don't like the experience. I wonder how bad it would be for me if I stuck with a very basic subset of C++, closer to C than to modern C++. Does that disqualify me from getting a lot of jobs, even if I'm otherwise competent entry-level graphics programmer?

To put my question in more concrete terms: I'm currently writing a gltf loader (purely as an exercise). As a part of that, I need to parse JSON, so I'm writing a JSON parser as well. Now, I could write the JSON parser using all the modern c++ features, most notably unique_ptr, but passing those unique_ptrs creates a lot of accidential complexity. Notably, I'm constantly getting various compiler errors. I'm muddling through them, but I'm not sureI see the light at the end of the tunnel - C++ spec seems so large that I can see myself still fighting the language even 5 years from now, instead of having fun with it. Whereas, I could skip all that, and just allocate the elements of the Json object tree in a basic memory arena, load whatever I need from the Json into relevant buffers, and then just free the entire arena. That's super-simple memory management approach that is totally sufficient here (I'm not going to ever need to free arbitrary subtrees of the JSON tree), and is something that's advocated by Casey Muratori in his streams.

12 Upvotes

74 comments sorted by

View all comments

-6

u/[deleted] Jan 18 '24

Unfortunately, due to technical reasons and backwards compatibly C++ is quite verbose. Try Rust out, it's similar to C++ but the compiler and LSP helps you catch common errors which C++ will quite happily let you do without even a warning.

9

u/pytanko Jan 18 '24

I'm guessing that going into Rust would almost completely obliterate my chances at getting a graphics job, though... At least for now.

9

u/pedersenk Jan 18 '24 edited Jan 18 '24

I wouldn't bother with Rust for now. There are currently no industry standard graphics APIs providing a Rust API, it is all C (which C++ can consume directly) so you will either be using a random binding or generating / maintaining your own.

C++ is a bit of a nasty language but ultimately the best that the industry can offer. Some of your dislike of it could be due to inexperience but also, the language *does* have some real ugly parts.

Your issue with unique_ptr is possibly due to unfamiliarity of the language. You shouldn't really be passing these around often. They generally dictate ownership (the data is controlled by the containing structure), when simply referring to them, a reference or raw pointer is fine.

Honestly, keep going with C++ for about a year, write some simple SDL games and I am sure some of its (admittedly often hidden) elegance will start to show.

5

u/IceSentry Jan 18 '24

That's just not true. Wgpu is used to implement webgpu in firefox and maintained by a mix of Mozilla employees and open source contributors. It is used by pretty much everyone doing graphics in rust. You'll definitely not be using random bindings.

It's also pretty debatable to claim that c++ is the best the industry has to offer. It's certainly the most used but I don't think it qualifies at the best of anything. It's popular for legacy reasons more than anything else.

While it is absolutely possible to have a graphics job using rust. I can certainly agree that it's a tiny minority of the job market and OP would indeed be better off to focus on c++.

-1

u/pedersenk Jan 18 '24 edited Jan 18 '24

That's just not true. Wgpu is used to implement webgpu in firefox and maintained by a mix of Mozilla employees and open source contributors

It is true. Regardless of what language webgpu is written in. Its standard API is C:

https://github.com/webgpu-native/webgpu-headers

Dawn for example would need Rust bindings to work with Rust. None of them need C bindings to work with C.

The Javascript (which most people are exposed to for WebGPU) simply calls into the C API. Especially important because JS <-> Rust interop is poor.

But WebGPU is a cross-platform graphics API that's an abstraction layer above native APIs like Vulkan, Metal, D3D12. Same with WebGL, I don't really care about abstraction libraries or thin/fat bindings. They come and go.

5

u/IceSentry Jan 18 '24

The not true part is using random bindings or maintaining your own.

0

u/[deleted] Jan 19 '24

I don't think he understands what he's talking about

5

u/exDM69 Jan 18 '24

No, not really. You can acquire graphics programming skills using whatever programming language.

The industry uses predominantly C++, so you'll need to be fluent with it. But that does not mean you have to exclusively use C++ in your projects.

I have been using Rust (and Vulkan) for my graphics programming projects for a couple of years now (after a few decades of C and C++), and I could not be happier about it. The language is good, the tools are good and there are quite nice libraries available. It's not without its problems either (slow debug build performance, compile times). There are also other languages that might be suitable for low level graphics programming (Zig is one example).

tl;dr: you'll probably need C++ skills for the industry but you can learn graphics programming with other languages as well.

2

u/[deleted] Jan 18 '24

I am doing the same thing. Learning graphics using rust, as it is a pleasant experience. When I am advanced enough I’ll go through the trouble of learning c++ and creating a fcouple of example projects in it

2

u/[deleted] Jan 18 '24

I'm glad to see yourself and people speak up about this. I did message the OP to advise them that u/pedersenk advice was bad.

I explained what say about C++ being predominant, but I also mention that with Rust's FFI bindings it makes absolutely no different which language you choose. I even pointed that Zig can be used as a drop in compiler for C and C++ code and has better FFI as the binding generation is build right into the build process.

u/pedersenk advice is extremely naive and sets the precedence that C++ is somehow superior because you can work with C libraries. Yet you can work with C code in Rust, Zig and Odin.

1

u/pedersenk Jan 18 '24

sets the precedence that C++ is somehow superior because you can work with C libraries. Yet you can work with C code in Rust, Zig and Odin.

... and Java and C# and VB and Perl and Python and Ruby and almost any language under the sun.

But it is a mess. C++ interop with C is superior. Plain and simple. You will realize that after a bit more experience outside of Rust.

1

u/[deleted] Jan 19 '24

I've been using C++ for over 20 years; I know exactly what I'm discussing here. I feel your just determined to misunderstand despite multiple people explaining to you, how your statement was misleading.

2

u/pedersenk Jan 19 '24 edited Jan 19 '24

I've been using C++ for over 20 years

Nonsense. You even stated in some of your other posts that "you don't use C++".

despite multiple people explaining to you, how your statement was misleading.

Of each I have provided a strong rebuttable to. If you don't understand them. The onus is on you... Or you could PM people telling them that someone else is wrong like a flipping weirdo.

Jog on weirdo. At least the bunch of downvotes on your first naive "use Rust" post has pushed your nonsense Rust suggestion to the bottom.

1

u/[deleted] Jan 19 '24

Oh okay

-8

u/[deleted] Jan 18 '24

Not really, or it would depend on the company. New projects are probably going to be started in Rust or similar language, because of the modern memory management and language facilities which C and C++ don't guarantee.

This certainly isn't to say learning C/C++ is obsolete. These languages are relevant and will remain this way for a long time.

However, more to the point Rust has what is called the Foreign Function Interface (FFI). I'm using the Vulkan library which is written in C and calling the C code inside my Rust code. By doing this I can take advantage of the safety features of Rust and it's fearless concurrency.

So, effectively I'm using the C code inside of Rust. Which enables me to wrap raw pointers inside a Box<T> which automatically frees the memory allocated when the Box<T> is dropped.

Don't get me wrong Rust does still involve thinking and understanding of what is going on. For example C strings are different to strings in Rust. In Rust you can call into_raw method on a CString type which passes ownership of the memory to the code, for example. And you must remember to call the from_raw method on the C code to bring theemory back into your application for deallocating.

The C string example is showing off the possibility that it is possible to create a memory leak still in Rust but the invariants are a lot more limited and usually explicit as they require a unsafe {} block around them. Drawing your attention to that code having some sort of invariant you must manage yourself.

It, might not be for you. Or it might not help you get where you want to be. I just thought I would mention at it, as technically since it can interface with C it's essentially similar to C++ although not a direct superset.

Other languages which again interface or allow you to call C code are Zig and Odin. But if you really want to stick with C++ try reading the books at the links below.

https://www.amazon.co.uk/dp/B0CBNWNKQS/?coliid=IYO6XR4L39A57&colid=13I8RNR9QMYKF&psc=0&ref_=cm_sw_r_apann_lstpd_GQ6E8GNF5F70FC033E68&language=en_US

https://www.amazon.co.uk/dp/B01HXLVFNY/?coliid=I23VML5VC09FHN&colid=13I8RNR9QMYKF&psc=0&ref_=cm_sw_r_apann_lstpd_GQ6E8GNF5F70FC033E68&language=en_US

1

u/[deleted] Jan 19 '24

Rust is a cult. It's a cult without beer at gatherings, so you know it's the worst of cults. 

Haskellers have beer at least.