Writing Compute Shaders In/With Zig

If anyone is looking for an actual implementation and wiring of SPIR-V shaders written in Zig, I recently ported my vertex shaders from glsl to Zig and am now compiling them with the Zig build system (without an external dependency). You can see my changes on this PR, I think the most interesting shader to look at is the Zig implementation of Eric Lengyel’s slug font rendering algorithm, you can find the source here https://codeberg.org/shahwali/knots/src/branch/main/src/gpu/backend/vulkan/shaders/slug_vertex.zig

Some limitations I ran in to (re my comment above, asking if anyone was aware of any limitations).

  • I was unable to port my fragment shaders due to their reliance on some glsl builtins like fwidth and texelFetch. I tried implementing them via inline SPIR-V assembly but ran in to the following SPIR-V codegen bug: https://codeberg.org/ziglang/zig/issues/35238
  • The gpu namespace is very minimal currently, and I had to implement some util stuff to reduce boilerplate for input/output bindings.

Besides that, I was quite surprised at how easy it was. I was expecting a lot more friction. And having my shaders in Zig is gonna be really nice as it gets rid of my glslc dependency, also nice to write my shaders in a much more powerful language!

8 Likes