|
| 1 | +# [zgpu](https://github.com/zig-gamedev/zgpu) |
| 2 | + |
| 3 | +Cross-platform graphics lib for Zig built on top of [Dawn](https://github.com/zig-gamedev/dawn) native WebGPU implementation. |
| 4 | + |
| 5 | +Supports Windows 10+ (DirectX 12), macOS 12+ (Metal) and Linux (Vulkan). |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Zero-overhead wgpu API bindings ([source code](https://github.com/zig-gamedev/zgpu/blob/main/src/wgpu.zig)) |
| 10 | +- Uniform buffer pool for fast CPU->GPU transfers |
| 11 | +- Resource pools and handle-based GPU resources |
| 12 | +- Async shader compilation |
| 13 | +- GPU mipmap generator |
| 14 | + |
| 15 | +## Getting started |
| 16 | + |
| 17 | +Example `build.zig`: |
| 18 | + |
| 19 | +```zig |
| 20 | +pub fn build(b: *std.Build) void { |
| 21 | + const exe = b.addExecutable(.{ ... }); |
| 22 | +
|
| 23 | + @import("zgpu").addLibraryPathsTo(exe); |
| 24 | +
|
| 25 | + const zgpu = b.dependency("zgpu", .{}); |
| 26 | + exe.root_module.addImport("zgpu", zgpu.module("root")); |
| 27 | + exe.linkLibrary(zgpu.artifact("zdawn")); |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Sample applications |
| 32 | + |
| 33 | +- [gui test (wgpu)](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/gui_test_wgpu) |
| 34 | +- [physically based rendering (wgpu)](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/physically_based_rendering_wgpu) |
| 35 | +- [bullet physics test (wgpu)](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/bullet_physics_test_wgpu) |
| 36 | +- [procedural mesh (wgpu)](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/procedural_mesh_wgpu) |
| 37 | +- [textured quad (wgpu)](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/textured_quad_wgpu) |
| 38 | +- [triangle (wgpu)](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/triangle_wgpu) |
| 39 | + |
| 40 | +## Library overview |
| 41 | + |
| 42 | +Below you can find an overview of main `zgpu` features. |
| 43 | + |
| 44 | +### Compile-time options |
| 45 | + |
| 46 | +You can override default options in your `build.zig`: |
| 47 | + |
| 48 | +```zig |
| 49 | +pub fn build(b: *std.Build) void { |
| 50 | + ... |
| 51 | +
|
| 52 | + const zgpu = @import("zgpu").package(b, target, optimize, .{ |
| 53 | + .options = .{ |
| 54 | + .uniforms_buffer_size = 4 * 1024 * 1024, |
| 55 | + .dawn_skip_validation = false, |
| 56 | + .buffer_pool_size = 256, |
| 57 | + .texture_pool_size = 256, |
| 58 | + .texture_view_pool_size = 256, |
| 59 | + .sampler_pool_size = 16, |
| 60 | + .render_pipeline_pool_size = 128, |
| 61 | + .compute_pipeline_pool_size = 128, |
| 62 | + .bind_group_pool_size = 32, |
| 63 | + .bind_group_layout_pool_size = 32, |
| 64 | + .pipeline_layout_pool_size = 32, |
| 65 | + }, |
| 66 | + }); |
| 67 | +
|
| 68 | + zgpu.link(exe); |
| 69 | +
|
| 70 | + ... |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### Graphics Context |
| 75 | + |
| 76 | +Create a `GraphicsContext` using a `WindowProvider`. For example, using [zglfw](https://github.com/zig-gamedev/zglfw): |
| 77 | + |
| 78 | +```zig |
| 79 | +const gctx = try zgpu.GraphicsContext.create( |
| 80 | + allocator, |
| 81 | + .{ |
| 82 | + .window = window, |
| 83 | + .fn_getTime = @ptrCast(&zglfw.getTime), |
| 84 | + .fn_getFramebufferSize = @ptrCast(&zglfw.Window.getFramebufferSize), |
| 85 | +
|
| 86 | + // optional fields |
| 87 | + .fn_getWin32Window = @ptrCast(&zglfw.getWin32Window), |
| 88 | + .fn_getX11Display = @ptrCast(&zglfw.getX11Display), |
| 89 | + .fn_getX11Window = @ptrCast(&zglfw.getX11Window), |
| 90 | + .fn_getWaylandDisplay = @ptrCast(&zglfw.getWaylandDisplay), |
| 91 | + .fn_getWaylandSurface = @ptrCast(&zglfw.getWaylandWindow), |
| 92 | + .fn_getCocoaWindow = @ptrCast(&zglfw.getCocoaWindow), |
| 93 | + }, |
| 94 | + .{}, // default context creation options |
| 95 | +); |
| 96 | +``` |
| 97 | + |
| 98 | +### Uniforms |
| 99 | + |
| 100 | +- Implemented as a uniform buffer pool |
| 101 | +- Easy to use |
| 102 | +- Efficient - only one copy operation per frame |
| 103 | + |
| 104 | +```zig |
| 105 | +struct DrawUniforms = extern struct { |
| 106 | + object_to_world: zm.Mat, |
| 107 | +}; |
| 108 | +const mem = gctx.uniformsAllocate(DrawUniforms, 1); |
| 109 | +mem.slice[0] = .{ .object_to_world = zm.transpose(zm.translation(...)) }; |
| 110 | +
|
| 111 | +pass.setBindGroup(0, bind_group, &.{mem.offset}); |
| 112 | +pass.drawIndexed(...); |
| 113 | +
|
| 114 | +// When you are done encoding all commands for a frame: |
| 115 | +gctx.submit(...); // Injects *one* copy operation to transfer *all* allocated uniforms |
| 116 | +``` |
| 117 | + |
| 118 | +### Resource pools |
| 119 | + |
| 120 | +- Every GPU resource is identified by 32-bit integer handle |
| 121 | +- All resources are stored in one system |
| 122 | +- We keep basic info about each resource (size of the buffer, format of the texture, etc.) |
| 123 | +- You can always check if resource is valid (very useful for async operations) |
| 124 | +- System keeps basic info about resource dependencies, for example, `TextureViewHandle` knows about its |
| 125 | + parent texture and becomes invalid when parent texture becomes invalid; `BindGroupHandle` knows |
| 126 | + about all resources it binds so it becomes invalid if any of those resources become invalid |
| 127 | + |
| 128 | +```zig |
| 129 | +const buffer_handle = gctx.createBuffer(...); |
| 130 | +
|
| 131 | +if (gctx.isResourceValid(buffer_handle)) { |
| 132 | + const buffer = gctx.lookupResource(buffer_handle).?; // Returns `wgpu.Buffer` |
| 133 | +
|
| 134 | + const buffer_info = gctx.lookupResourceInfo(buffer_handle).?; // Returns `zgpu.BufferInfo` |
| 135 | + std.debug.print("Buffer size is: {d}", .{buffer_info.size}); |
| 136 | +} |
| 137 | +
|
| 138 | +// If you want to destroy a resource before shutting down graphics context: |
| 139 | +gctx.destroyResource(buffer_handle); |
| 140 | +
|
| 141 | +``` |
| 142 | + |
| 143 | +### Async shader compilation |
| 144 | + |
| 145 | +- Thanks to resource pools and resources identified by handles we can easily async compile all our shaders |
| 146 | + |
| 147 | +```zig |
| 148 | +const DemoState = struct { |
| 149 | + pipeline_handle: zgpu.PipelineLayoutHandle = .{}, |
| 150 | + ... |
| 151 | +}; |
| 152 | +const demo = try allocator.create(DemoState); |
| 153 | +
|
| 154 | +// Below call schedules pipeline compilation and returns immediately. When compilation is complete |
| 155 | +// valid pipeline handle will be stored in `demo.pipeline_handle`. |
| 156 | +gctx.createRenderPipelineAsync(allocator, pipeline_layout, pipeline_descriptor, &demo.pipeline_handle); |
| 157 | +
|
| 158 | +// Pass using our pipeline will be skipped until compilation is ready |
| 159 | +pass: { |
| 160 | + const pipeline = gctx.lookupResource(demo.pipeline_handle) orelse break :pass; |
| 161 | + ... |
| 162 | +
|
| 163 | + pass.setPipeline(pipeline); |
| 164 | + pass.drawIndexed(...); |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +### Mipmap generation on the GPU |
| 169 | + |
| 170 | +- wgpu API does not provide mipmap generator |
| 171 | +- zgpu provides decent mipmap generator implemented in a compute shader |
| 172 | +- It supports 2D textures, array textures and cubemap textures of any format |
| 173 | + (`rgba8_unorm`, `rg16_float`, `rgba32_float`, etc.) |
| 174 | +- Currently it requires that: `texture_width == texture_height and isPowerOfTwo(texture_width)` |
| 175 | +- It takes ~260 microsec to generate all mips for 1024x1024 `rgba8_unorm` texture on GTX 1660 |
| 176 | + |
| 177 | +```zig |
| 178 | +// Usage: |
| 179 | +gctx.generateMipmaps(arena, command_encoder, texture_handle); |
| 180 | +``` |
0 commit comments