by billfruit · 5y ago · 0 comments
Basically, the amount of code interfacing with OpenGL should be fairly minimal.
There are higher level graphics libraries on top of OpenGL, Vulkan, Metal, WebGL & DirectX, for example bgfx or sokol, but IIRC they still go C style for the same reason. Then there are language-specific libraries that will be idiomatic, like wgpu for Rust. Finally, you reach entire very opinionated game engines at various levels (Unity, UE, Godot, etc).
It took NVidia beating them to the ground for OpenCL to ever consider anything other than C and create SPIR.
Then the existing C++ bindings for Vulkan were originally created by NVidia (again).
Now ANARI is yet again a C API, they just cannot move beyond C.
Modern C also lack portability due to UBOs and compiler optimizations, but still good enough so is C++.
I would not consider a C++ API particularly more expressive than a C API.
RAII handles are nice. But that’s about all the expressiveness you get from C++ at the API level.
Languages that provide expressiveness generally do so at the implementation level, in my experience.
Opengl also gets away with because you're almost always working with either floats or integers (for say index buffers) but once you start dealing with containers of containers, a C api ends up really messy with everything being an "object" and having to call function_str(my_obj_handle) with the correct handle - see sentry's api [1] for an example of what this looks like.
A C++ api for OpenGL would allow for type safe flags, type and bounds checked buffers, RAII handles with all the good (and granted, bad) that comes with them.
[0] https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl...
When managing resources on separate memory, RAII is also rarely something you’d want. There are different memory management strategies and tight control over memory is very important.
Even if you implement your library in C++ you will need to expose a C API to be compatible with other languages. Not everyone uses C++.
It's the opposite.
When managing resources in system memory, RAII is rarely something I’d want because malloc() is slow.
When managing resources on separate device, RAII is invaluable. These external resources often have API designed like bind/use/unbind, map/update/unmap and similar. You forget that one close/unbind/unmap and you violate your part of the contract i.e. anything may happen including crashes and memory leaks.
DirectX is only barely C++. It's structs and virtual interfaces. They actually provide a full C interface. The API itself is exceptionally C-like IMHO. It does not, for example, return unique_ptr or vectors. There's no std::string or std::function. In fact there's no std:: anything.
Don't get me wrong I like namespaces, constructors, and methods over C-style "namespace", no constructors, and an ocean of loose functions. If people want to create and use C++ wrappers around C APIs that's great. But OP's question was "Why does OpenGL persist with a C style API instead of a more expressive one?".
Writing a C API and wrapping with C++ is very different than writing an "expressive" C++ interface imho.
COM is language agnostic and definitly not C.
A C++ wrapper doesn't need std::anything to be C++.
Barely C++ is still an improvement over bare bones C.
LibGNM(X) and GX2 are also not std::whatever_else, but again build up on not being a bare bones C API stuck in the days of IrisGL.
Finally Metal is a mix of Objective-C and C++, both definitly an improvment.
Then there is the whole issue of they are proper frameworks, not "here is a specification and now go hunting how to load fonts, models, materials" that Khronos does.
If that's all you want, its not hard to write your own RAII wrapper for things anyway.
A C++ API imposes its style on you. Usually badly. Never mind the ABI issues.
Most interfaces could be improved by re-writing them in C. I can’t say the same for the inverse.
That said, OpenGL is a terrible API that is nearly impossible to use correctly. But neither C nor C++ have much impact on its horrifically stateful architecture.
> Never mind ABI issues.
A C ABI is far more brittle than a C++ one.
On some platforms you can make even single compiler builds in C++ incompatible if you use stl - e.g. a stl::vector may be quite different in debug and release build which leads naturally to faulty memory acceess.
I made it a library with a single header: https://github.com/redgpu/framework
Supports WASM and WebGL too!