/zig-gamedev

Building game development ecosystem for @ziglang!

Primary LanguageZigMIT LicenseMIT

NOTE: To compile this project you need latest version of Zig compiler (zig-windows-x86_64-0.9.0-dev.2023 or newer). You can download it here.

zig-gamedev project

This repository contains a collection of sample applications and libraries written in Zig programming language and using DirectX 12, DirectML, XAudio2, WASAPI, Direct2D, DirectWrite, Bullet Physics SDK. Prebuilt binaries for all sample applications can be found in Releases. Project is under active development, see Roadmap and Progress Reports for the details.

I build game development stuff in Zig full-time. As a sample of my work please see this video. If you like my project and my mission to promote the language, please consider supporting me.

Some features:

  • Zero dependency except Zig compiler (master) - no Visual Studio/Build Tools/Windows SDK is needed - this repo + Zig compiler package (60 MB) is enough to start developing (any debugger can be used)
  • Building is as easy as running zig build (see: Building)
  • Helper libraries for working with DirectX 12 and real-time graphics (see: graphics lib, vectormath lib)
  • Interop with Direct2D and DirectWrite for high-quality vector graphics and text rendering
  • Support for GPU profiling with PIX (markers and events API, see: pix lib)
  • Support for CPU profiling with Tracy (zones API, see: tracy lib)
  • Uses some great C/C++ libraries which are seamlessly built by zig cc compiler (see: external/src)

Sample applications

Some of the sample applications are listed below. More can be found in samples directory.

  1. physically based rendering: This sample implements physically based shading and image based lighting to achive realistic looking rendering results.

    physically based rendering

  2. simple raytracer: This sample implements basic hybrid renderer. It uses rasterization to resolve primary rays and raytracing (DXR) for shadow rays.

    simple raytracer

  3. mesh shader test: This sample shows how to use DirectX 12 Mesh Shader.

    mesh shader test

  4. virtual physics lab: This sample applications demonstrates how to use full 3D physics engine in your Zig applications.

    virtual physics lab

  5. audio playback test: This sample demonstrates how to decode .mp3 file using Microsoft Media Foundation and play it back using Windows Audio Session API (WASAPI).

    audio playback test

  6. DirectML convolution test: This sample demonstrates how to perform GPU-accelerated convolution operation using DirectML.

    directml convolution test

  7. vector graphics test: This sample demonstrates how to use Direct3D 12 <-> Direct2D interop to render vector graphics and high-quality text.

    vector graphics test

Libraries

Some features of graphics library:

  • Basic DirectX 12 context management (descriptor heaps, memory heaps, swapchain, CPU and GPU sync, etc.)
  • Basic DirectX 12 resource management (handle-based resources and pipelines)
  • Basic resource barriers management with simple state-tracking
  • Fast image loading using WIC (Windows Imaging Component)
  • Helpers for uploading data to the GPU
  • Fast mipmap generator running on the GPU
  • Interop with Direct2D and DirectWrite for high-quality vector graphics and text rendering
  • Custom integration of dear imgui library

This libarary implements all basic linear algebra operations for Vec2, Vec3, Vec4, Mat4 and Quat.

Easy to use C API for Bullet physics library that can be used by any project. Some features:

  • Most collision shapes
  • Rigid bodies
  • Most constraint types
  • Tries to minimize number of memory allocations
    • Multiple rigid bodies and motion states can be created with one memory allocation
    • New physics objects can re-use existing memory
  • Lots of error checks in debug builds

This is a simple libarary that lets you mark named events on the GPU timeline. Those events can be then anaylzed in PIX. Additionaly, you can programmatically record PIX traces to a file. Note, that this library does not require WinPixEventRuntime.dll to work. Following operations are supported:

  • beginCapture, endCapture
  • beginEventOnCommandList, endEventOnCommandList, setMarkerOnCommandList
  • beginEventOnCommandQueue, endEventOnCommandQueue, setMarkerOnCommandQueue

This is a simple libarary that lets you mark named events (zones) on the CPU timeline. Zones can be then anaylzed in Tracy profiler. Following operation are supported:

  • zone, zoneN, zoneNC
  • frameMark, frameMarkNamed

Building sample applications

As mentioned above, the only dependency needed to build this project is Zig compiler, neither Visual Studio nor Windows SDK has to be installed.

Zig compiler consists of a single ~60MB .zip file that needs to be downloaded separately. Latest development build of the compiler must be used (master) you can download prebuilt binaries here.

To build a sample application (assuming zig.exe is in the PATH):

  1. Open terminal window.
  2. 'cd' to sample application root directory (for example, cd samples/simple_raytracer).
  3. Run zig build command.
  4. Sample application will be build, assets and build artifacts will be copied to samples/<sample_name>/zig-out/bin directory.

Behind the scenes zig build command performs following steps:

  1. zig cc builds all C/C++ libraries that application uses (imgui, cgltf).
  2. DirectX Shader Compiler (which can be found in external/bin/dxc directory) is invoked to build all HLSL shaders.
  3. Zig code is compiled.
  4. Everything is linked together into single executable.
  5. Assets and build artifacts are copied to destination directory.

You can look at samples/simple_raytracer/build.zig file to see how those steps are implemented in Zig build script.

Build options

All sample applications support following build options:

  • -Drelease-safe=[bool] - Optimizations on and safety on
  • -Drelease-fast=[bool] - Optimizations on and safety off
  • -Denable-pix=[bool] - PIX markers and events enabled
  • -Denable-dx-debug=[bool] - Direct3D 12, Direct2D, DXGI debug layers enabled
  • -Denable-dx-gpu-debug=[bool] - Direct3D 12 GPU-Based Validation enabled (requires -Denable-dx-debug=true)
  • -Dtracy=[path/to/tracy/source] - Tracy profiler zones enabled

Examples:
zig build -Denable-dx-debug=true -Drelease-fast=true
zig build -Dtracy="C:/Development/tools/Tracy/tracy-0.7.8"

To build and run an application you can use:
zig build run <- Builds and runs debug build.
zig build run -Drelease-fast=true -Denable-dx-debug=true <- Builds and runs release build with DirectX debug layers enabled.

Requirements

This project uses DirectX 12 Agility SDK which allows to always use latest DirectX 12 features regardless of Windows version installed (this works from Windows 10 November 2019 Update). In particular, following Windows versions are supported:

  • Windows 10 May 2021 (Build 19043) or newer
  • Windows 10 October 2020 (Build 19042.789+)
  • Windows 10 May 2020 (Build 19041.789+)
  • Windows 10 November 2019 (Build 18363.1350+)

GitHub Sponsors

Thanks to all people who sponsor zig-gamedev project! In particular, these fine folks sponsor zig-gamedev for $25/month or more:

  • mzet (mzet-)
  • Zig Software Foundation (ziglang)
  • Ian (LinuXY)