Skip to content

cyberegoorg/zbgfx

Repository files navigation

ZBgfx

GitHub Actions

When zig meets bgfx.

Features

  • Zig api.
  • Compile as standard zig library.
  • shaderc as build artifact.
  • Shader compile in build.zig to *.bin.h.
  • Shader compile in build.zig and embed as zig module. (this is zig equivalent of *.bin.h)
  • Shader compile from code (in memory solution, no tmp files).
  • Binding for DebugDraw API
  • imgui render backend. Use build option imgui_include to enable. ex. for zgui: .imgui_include = zgui.path("libs").getPath(b),

Important

This is only zig binding. For BGFX stuff goto bgfx.

Warning

  • shaderc need some time to compile.

Note

  • If you build shaders/app and see something like run shaderc (shader.bin.h) stderr. This is not "true" error (build success), but only in debug build shader print some stuff to stderr and zig build catch it.

License

Folders libs, shaders is copy&paste from bgfx for more sell-contained experience and is licensed by LICENSEE

Zig binding is licensed by WTFPL

Zig version

Minimal is 0.12.0 or 0.13.0-dev.46+3648d7df1. But you know.. try your version and believe.

Getting started

Copy zbgfx to a subdirectory of your project and add the following to your build.zig.zon .dependencies:

    .zbgfx = .{ .path = "path/to/zbgfx" },

Then in your build.zig add:

pub fn build(b: *std.Build) void {
    const exe = b.addExecutable(.{ ... });

const zbgfx = b.dependency("zbgfx", .{});
exe.root_module.addImport("zbgfx", zbgfx.module("zbgfx"));
exe.linkLibrary(zbgfx.artifact("bgfx"));

// This install shaderc to install dir
// For shader build in build =D check examples
// b.installArtifact(zbgfx.artifact("shaderc"));
}

Usage

See examples for binding usage and bgfx for bgfx stuff.

Build options

Build option Default Description
imgui_include null Path to ImGui includes (need for imgui bgfx backend)
multithread true Compile with BGFX_CONFIG_MULTITHREADED
with_shaderc true Compile with shaderc

Examples

Examples use zig-gamedev as submodule.

Run this to fetch zig-gamedev:

git submodule update --init --depth=1

And this for build all examples:

cd examples
zig build

Minimal setup with GLFW for window and input.

examples/zig-out/bin/00-minimal
Key Description
v Vsync on/off
d Debug on/off

Minimal setup for zgui/ImGui.

examples/zig-out/bin/01-zgui
Key Description
v Vsync on/off
d Debug on/off

Basic usage of shader compile in runtime. Try edit shaders in zig-out/bin/shaders and hit r to recompile.

examples/zig-out/bin/02-runtime-shaderc
Key Description
v Vsync on/off
d Debug on/off
r Recompile shaders form file

DebugDraw api usage example.

examples/zig-out/bin/03-debugdraw
Key Description
v Vsync on/off
d Debug on/off