Mach v0.1 - cross-platform Zig graphics in ~60 seconds

Five months ago we announced some of our vision for Mach & the future of graphics with Zig. Today we’ve reached Mach v0.1 with over 1,100 commits.

Cross-platform graphics in 60 seconds

If you have Zig v0.10 you can get started with cross-platform graphics in under 60 seconds, try it for yourself:

git clone https://github.com/hexops/mach
cd mach/gpu
zig build run-example

(not working? see known issues)

All you need is zig, git, and curl.

One key point we’re solving with Mach is the developer experience. We’re tired of people wasting hours and sometimes days getting the right versions of dependencies on their system in order to build projects!

Our glfw bindings build GLFW 100% from source using Zig. We even go so far as to build the DirectX Shader Compiler from source via Zig’s build system.

For the few inescapable system dependencies, such as Metal.framework or libx11, we package them up ourselves and our build system knows how to fetch them as needed.

Effortless cross-compilation

Because of our aggressive approach to solving dependencies, you get effortless cross-compilation to any OS literally at the flip of a switch:

$ zig build -Dtarget=x86_64-windows
$ zig build -Dtarget=x86_64-linux
$ zig build -Dtarget=x86_64-macos.12
$ zig build -Dtarget=aarch64-macos.12

The binaries you end up with are virtually static.

For Linux, Zig lets you target any glibc version, and musl too, so no more worrying if that binary will run on other machines.

Unified graphics API (Metal, Vulkan, DirectX 12)

Backed by Metal, Vulkan, DirectX 12 & OpenGL (as a fallback), you get a truly cross-platform graphics API for Windows, Linux, and macOS (Browser and Mobile coming in the future)

Unified shader language & compute shaders

There’s no need to write shaders for each graphics backend, instead you write shaders in a single modern language (WGSL):

With compute shaders, you have the ability to leverage computation on the GPU outside of graphical applications (machine learning, physics calculations, etc.) using a straightforward & approachable API that works with every GPU vendor.

Behind the scenes

Mach leverages WebGPU, a successor to WebGL. WebGPU is an up and coming graphics API being built by Mozilla, Google, Apple, Microsoft, Intel and others.

Natively, Mach uses Zig as a C/C++ compiler to build Google Chrome’s native WebGPU implementation and we use Zig’s build system so you don’t even have to deal with cmake/ninja/gn/etc.

Our infrastructure produces binary releases so you don’t even have to wait the handful of minutes it would take to compile by default. From-source builds are literally at your fingertips, though, just add -Ddawn-from-source=true to your zig build command.

mach/gpu: the GPU interface for Zig

mach/gpu is our Zig interface to WebGPU and comes in at just over 250 commits.

It provides a gpu.Interface, similar to how Zig provides a std.mem.Allocator interface, it’s backed by any implementation:

  • A NativeInstance like Dawn (Google Chrome’s WebGPU implementation.)
  • (future) A browser implementation when targeting WebAssembly.

Imagine future implementations: maybe a pure-Zig implementation? Maybe a debugger implementation that wraps an existing one and streams all API calls to disk so you can replay them later and step through graphics API calls. Lots of possibilities here!

It’s a comprehensive interface, covering the 176 methods, 73 data structures, and 43 enum types that WebGPU exposes today - but there’s still much to do around documentation, fixing bugs, and ensuring we match the browser API nicely. but the foundation is all there!

Zig ≈ greatness

Zig provides some excellent runtime safety and catches many of the mistakes people make in C/C++ (memory leaks, integer overflow, index out of bounds, etc.)

In fact, we’ve caught several instances of undefined behavior in GLFW, and even illegal integer coercion in the DirectX Shader Compiler - all just by compiling C/C++ code using Zig.

The reason we’re really ecstatic about Zig, though, are what it promises us in the future:

Entity Component System

We’re building an Entity Component System in a blog series and inspired by:

  • Data Oriented Design
  • Database design
  • Advice from the authors of Bevy (a very popular ECS)

It’s still early stages, we’ve got some ways to go! But we’re on our third major revision and beginning to face the interesting problems. Keep an eye out for updates on that blog series coming soon.

Sounds great! What’s the catch?

Mach (and Zig) are still very early stages! APIs are going to change and break. Mach is missing a lot!

  • Documentation..
  • Examples..
  • Demos..
  • Browser and Mobile support..
  • ..Literally everything else that makes a game engine

If you’re looking for cross-platform graphics in Zig, Mach is for you! Otherwise, you’ll probably need to wait a bit.

Getting started

Check out the GitHub and in particular this example code.

There’s a ton of material out there about WebGPU already, check out this excellent and comprehensive introductory article and these awesome samples. It should be easy to map any of these to the Mach gpu.Interface since it’s the same API, just Ziggified!

Join our Matrix chat room to get help, discuss, etc.

A personal note

No, it’s not Tom from myspace - it’s me, @slimsag! It’s cool that we reached this milestone, but let’s be honest: it’s still just a dumb red triangle!

It’s taken 9 brutal months to get here - staring at broken CI pipelines, C++ compiler errors, buying hardware to test every OS+arch possible - and slugging through the depths of hell where only a footnote from Raymond Chen himself will save you. All to get to a point where it all just works the way you and I expect it to.

Given the chance, I’d scream from a rooftop “f***ing finally! Now we build the cool part!” because I am so ecstatic to have gotten to this point!

There are few things in life that I am more serious about than this. I dedicate ~48 hours/week to my dayjob, and ~50h/week to Zig building Mach and running zigmonthly.org. After three years of aggressively pushing for progress in this exact way, I have no plans to slow down anytime soon.

This is literally just the beginning. Buckle up!

What’s next?

My lightning talk in which I’ll be making the case for Mach engine and conveying the vision for where we go from here will be presented at the first-ever European Zig meetup in Milan, Italy on Apr 9-10.

If like me you are unable to attend in person, the short video will be available afterwards on the @machengine Twitter!

Help us reach Mach v1.0

Consider sponsoring me on GitHub if you like my work, so I can do more of it!

Join our Matrix chat room to discuss ideas - collaboration very welcome!

Thanks for coming along in our journey!