Recommendations for typesafe graphics API wrappers?

Hello,

I am fairly new to graphics programming and there are a lot of APIs with a lot of zig libraries for them, so I wanted to ask if there are any that someone has had a good experience with. I am intending to use it with Zig 0.16.0.

zgl has served me well for openGL, and vulkan-zig for VK.

Any specific things you’re looking for in a wrapper other than type safety?

1 Like

Can vouch for vulkan-zig too, learning Vulkan at the moment and I’ve had a great experience with the Vulkan API being ziggified. You are still gonna have to get your hands dirty in C style stuff for something like VMA, but I suppose a light wrapper could help.

Other than that, sokol’s zig bindings were great to work with too last time I used them

2 Likes

I can vouch that zokol’s zig bindings are great

For anyone learning vulkan-zig, this is the best book I’ve found on it. It uses Zig bindings whenever possible, which I haven’t seen in other tutorials. It’s still a WIP but it already has a significant amount of content.

3 Likes

I am very new to GPU Programming, so really just something that allows me to forget that there is c somewhere below the API and has proven to work well so I know that my mistakes are truly mine.

One minor thing I’ve run into with the zig and vulkan is trying to cast from *T to [*]T.

They don’t coerce, which is very good, but using @ptrCast to cast them feels like overkill, since if you change the type from T to U, zig will still cast *U to [*]T.

This comes up in the various Vulkan Info types that take a list of items (like semaphores or fences), but luckily the validation layer will catch it. Be sure to @panic on validation errors!!!

Take a look at this part of the Zig documentation: Documentation - The Zig Programming Language.

const single: *const i32 = &0;
const many: [*]const i32 = single[0..1];

I prefer to just create an array and reference that.

Oh! That’s perfect, thank you

Tbf, type-safety on top of what a 3D-API C-API provides won’t help you figure out why you’re staring at a black screen instead of a hello-world-triangle :wink: Pretty much all problems in 3D rendering can only be caught at runtime. What you rather want is a validation layer in the 3D API (or wrapper library) which tells you what’s wrong while running the code.

GL has nothing of that sort (at least nothing directly built in, and KHR_debug extension support isn’t all that great last I checked), and Vulkan will spam you with tons of too detailed messages which will leave you more confused than without.

D3D11 and Metal are the sweet spot when it comes to helpful validation layer messages (and not to toot my own horn, but the sokol-gfx validation layer should also catch most API ‘misuse’).

Also, if possible use a 3D API debugger like RenderDoc, this is worth a thousand validation layer messages :wink:

4 Likes

As mentioned by others, vulkan-zig is great.

The Vulkan API is essentially a series of XML files describing what functions and types are available, vulkan-zig consumes these XML files and produces Zig code that exposes the API to you.

This is nice because you can get the benefits of Zig’s type system without having to give up working with the actual API. If you follow a tutorial for using Vulkan from C or C++, everything will work exactly the same way but with additional type safety.

1 Like

If you use Windows, I believe the best API to learn and get started with is D3D11. It doesn’t have many modern features, but it more than makes up for it in debug tooling, ease of use, documentation, etc etc. Probably the nicest native GPU API to this day. You could get started with Vulkan, but it’s quite painful.

If you don’t mind “layers of abstraction”, SDL_GPU has a full Zig wrapper and it’s one of the nicest GPU API’s I’ve used, with much larger access to modern GPU techniques (excluding bindless). It should also be wayyyy easier to learn…

1 Like