Moved add and bench into an examples directory so src is just the library

This commit is contained in:
adrien 2026-05-18 22:57:13 +02:00
parent 6b933465b0
commit 6ec53bb909
7 changed files with 63 additions and 124 deletions

View File

@ -16,11 +16,13 @@ The library exports five primary components:
Below is a complete, self-contained example demonstrating how to initialize the GPU, load data, run a compute shader, and read the results back to the CPU: Below is a complete, self-contained example demonstrating how to initialize the GPU, load data, run a compute shader, and read the results back to the CPU:
```zig ```zig
const std = @import("std"); const std = @import("std");
const GpuDevice = @import("GpuDevice.zig"); const gpu = @import("gpu");
const GpuArena = @import("GpuArena.zig"); const GpuDevice = gpu.GpuDevice;
const GpuBuffer = @import("GpuBuffer.zig"); const GpuArena = gpu.GpuArena;
const GpuProcess = @import("GpuProcess.zig"); const GpuBuffer = gpu.GpuBuffer;
const GpuProcess = gpu.GpuProcess;
pub fn main(init: std.process.Init) !void { pub fn main(init: std.process.Init) !void {
const allocator = init.gpa; const allocator = init.gpa;

112
build.zig
View File

@ -1,94 +1,60 @@
// build.zig
// zig build run
//
// Expects wgpu-native pre-built in libs/wgpu-native/:
// include/wgpu.h
// lib/libwgpu_native.a (or .so / .dylib / .dll)
//
// Download release: https://github.com/gfx-rs/wgpu-native/releases
// Pick the archive matching your OS/arch, e.g.:
// wgpu-linux-x86_64-release.zip libwgpu_native.a + wgpu.h
// wgpu-macos-aarch64-release.zip
// wgpu-windows-x86_64-msvc-release.zip
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.Build) void { pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
// 1. Define the module so other projects can import it // Define the module so other projects can import it
_ = b.addModule("zig-wgpu", .{ const mod = b.addModule("zig-wgpu", .{
.root_source_file = b.path("src/lib.zig"), .root_source_file = b.path("src/lib.zig"),
});
const exe = b.addExecutable(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/bench.zig"),
.link_libc = true,
.target = target, .target = target,
.optimize = optimize,
}),
.name = "bench",
}); });
// wgpu-native headers + pre-built static library mod.addIncludePath(b.path("libs/wgpu-native/include"));
exe.root_module.addIncludePath(b.path("libs/wgpu-native/include")); mod.addLibraryPath(b.path("libs/wgpu-native/lib"));
exe.root_module.addLibraryPath(b.path("libs/wgpu-native/lib")); mod.addObjectFile(b.path("libs/wgpu-native/lib/libwgpu_native.a"));
exe.root_module.addObjectFile(b.path("libs/wgpu-native/lib/libwgpu_native.a"));
// Platform-specific system frameworks needed by wgpu-native // Platform-specific system frameworks needed by wgpu-native
const t = target.result; const t = target.result;
if (t.os.tag == .macos) { if (t.os.tag == .macos) {
exe.root_module.linkFramework("Metal", .{}); mod.linkFramework("Metal", .{});
exe.root_module.linkFramework("QuartzCore", .{}); mod.linkFramework("QuartzCore", .{});
exe.root_module.linkFramework("Foundation", .{}); mod.linkFramework("Foundation", .{});
exe.root_module.linkFramework("CoreGraphics", .{}); mod.linkFramework("CoreGraphics", .{});
} else if (t.os.tag == .windows) { } else if (t.os.tag == .windows) {
exe.root_module.linkSystemLibrary("d3d12", .{}); mod.linkSystemLibrary("d3d12", .{});
exe.root_module.linkSystemLibrary("dxgi", .{}); mod.linkSystemLibrary("dxgi", .{});
exe.root_module.linkSystemLibrary("user32", .{}); mod.linkSystemLibrary("user32", .{});
} else { } else {
exe.root_module.linkSystemLibrary("vulkan", .{}); mod.linkSystemLibrary("vulkan", .{});
exe.root_module.linkSystemLibrary("gcc_s", .{}); mod.linkSystemLibrary("gcc_s", .{});
} }
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
var buf: [1024]u8 = undefined;
const exemples = try std.Io.Dir.cwd().openDir(io, "examples", .{ .access_sub_paths = false, .iterate = true });
var iter = exemples.iterate();
while (try iter.next(io)) |entry| {
if (entry.kind != .file) continue;
if (!std.mem.eql(u8, entry.name[entry.name.len - 4 ..], ".zig")) continue;
const exe = b.addExecutable(.{
.name = entry.name[0 .. entry.name.len - 4],
.root_module = b.createModule(.{
.root_source_file = b.path(try std.fmt.bufPrint(&buf, "examples/{s}", .{entry.name})),
.target = target,
.optimize = optimize,
.imports = &.{},
}),
});
exe.root_module.addImport("gpu", mod);
b.installArtifact(exe); b.installArtifact(exe);
const run = b.addRunArtifact(exe); const run_step = b.step(entry.name[0 .. entry.name.len - 4], try std.fmt.bufPrint(&buf, "Run {s} demo", .{entry.name}));
run.step.dependOn(b.getInstallStep()); const run_cmd = b.addRunArtifact(exe);
b.step("bench", "Benchmark a simple add vector.").dependOn(&run.step); run_step.dependOn(&run_cmd.step);
const exe_examp = b.addExecutable(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/example.zig"),
.link_libc = true,
.target = target,
.optimize = optimize,
}),
.name = "bench",
});
// wgpu-native headers + pre-built static library
exe_examp.root_module.addIncludePath(b.path("libs/wgpu-native/include"));
exe_examp.root_module.addLibraryPath(b.path("libs/wgpu-native/lib"));
exe_examp.root_module.addObjectFile(b.path("libs/wgpu-native/lib/libwgpu_native.a"));
if (t.os.tag == .macos) {
exe_examp.root_module.linkFramework("Metal", .{});
exe_examp.root_module.linkFramework("QuartzCore", .{});
exe_examp.root_module.linkFramework("Foundation", .{});
exe_examp.root_module.linkFramework("CoreGraphics", .{});
} else if (t.os.tag == .windows) {
exe_examp.root_module.linkSystemLibrary("d3d12", .{});
exe_examp.root_module.linkSystemLibrary("dxgi", .{});
exe_examp.root_module.linkSystemLibrary("user32", .{});
} else {
exe_examp.root_module.linkSystemLibrary("vulkan", .{});
exe_examp.root_module.linkSystemLibrary("gcc_s", .{});
} }
const examp = b.addRunArtifact(exe_examp);
run.step.dependOn(b.getInstallStep());
b.step("example", "Run basic example.").dependOn(&examp.step);
} }

View File

@ -1,43 +1,13 @@
.{ .{
// This is the default name used by packages depending on this one. For
// example, when a user runs `zig fetch --save <url>`, this field is used
// as the key in the `dependencies` table. Although the user can choose a
// different name, most users will stick with this provided value.
//
// It is redundant to include "zig" in this name because it is already
// within the Zig package namespace.
.name = .zig_wgpu, .name = .zig_wgpu,
// This is a [Semantic Version](https://semver.org/). .version = "0.1.0",
// In a future version of Zig it will be used for package deduplication. .fingerprint = 0x5d0e853acbc0c2c6,
.version = "0.0.0",
// Together with name, this represents a globally unique package
// identifier. This field is generated by the Zig toolchain when the
// package is first created, and then *never changes*. This allows
// unambiguous detection of one package being an updated version of
// another.
//
// When forking a Zig project, this id should be regenerated (delete the
// field and run `zig build`) if the upstream project is still maintained.
// Otherwise, the fork is *hostile*, attempting to take control over the
// original project's identity. Thus it is recommended to leave the comment
// on the following line intact, so that it shows up in code reviews that
// modify the field.
.fingerprint = 0x5d0e853acbc0c2c6, // Changing this has security and trust implications.
// Tracks the earliest Zig version that the package considers to be a
// supported use case.
.minimum_zig_version = "0.16.0", .minimum_zig_version = "0.16.0",
// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{}, .dependencies = .{},
.paths = .{ .paths = .{
"build.zig", "build.zig",
"build.zig.zon", "build.zig.zon",
"src", "src",
// For example... "libs",
//"LICENSE",
//"README.md",
}, },
} }

View File

@ -1,8 +1,9 @@
const std = @import("std"); const std = @import("std");
const GpuDevice = @import("GpuDevice.zig"); const gpu = @import("gpu");
const GpuArena = @import("GpuArena.zig"); const GpuDevice = gpu.GpuDevice;
const GpuBuffer = @import("GpuBuffer.zig"); const GpuArena = gpu.GpuArena;
const GpuProcess = @import("GpuProcess.zig"); const GpuBuffer = gpu.GpuBuffer;
const GpuProcess = gpu.GpuProcess;
pub fn main(init: std.process.Init) !void { pub fn main(init: std.process.Init) !void {
const allocator = init.gpa; const allocator = init.gpa;

View File

@ -1,11 +1,10 @@
const std = @import("std"); const std = @import("std");
const GpuDevice = @import("GpuDevice.zig"); const gpu = @import("gpu");
const GpuArena = @import("GpuArena.zig"); const GpuDevice = gpu.GpuDevice;
const GpuAllocator = @import("GpuAllocator.zig"); const GpuArena = gpu.GpuArena;
const GpuBuffer = @import("GpuBuffer.zig"); const GpuAllocator = gpu.GpuAllocator;
const GpuProcess = @import("GpuProcess.zig"); const GpuBuffer = gpu.GpuBuffer;
const GpuProcess = gpu.GpuProcess;
const c = @import("utils.zig").c;
/// Minimal implementation of a f16 Vector /// Minimal implementation of a f16 Vector
const Vec = struct { const Vec = struct {
@ -80,9 +79,9 @@ pub fn main(init: std.process.Init) !void {
4 * 4 * 4 * 1024, 4 * 4 * 4 * 1024,
4 * 4 * 4 * 4 * 1024, 4 * 4 * 4 * 4 * 1024,
1024 * 1024, 1024 * 1024,
// 4 * 1024 * 1024, 4 * 1024 * 1024,
// 4 * 4 * 1024 * 1024, 4 * 4 * 1024 * 1024,
// 4 * 4 * 4 * 1024 * 1024, 4 * 4 * 4 * 1024 * 1024,
// 4 * 4 * 4 * 4 * 1024 * 1024, // 4 * 4 * 4 * 4 * 1024 * 1024,
// 4 * 4 * 4 * 4 * 4 * 1024 * 1024, // 4 * 4 * 4 * 4 * 4 * 1024 * 1024,
}; };
@ -137,7 +136,7 @@ pub fn main(init: std.process.Init) !void {
if (grena.allocated_vram_bytes > peak_vram_bytes) if (grena.allocated_vram_bytes > peak_vram_bytes)
peak_vram_bytes = grena.allocated_vram_bytes; peak_vram_bytes = grena.allocated_vram_bytes;
_ = c.wgpuDevicePoll(device.device, 1, null); device.poll();
const compute_duration = compute_start.durationTo(std.Io.Clock.awake.now(init.io)); const compute_duration = compute_start.durationTo(std.Io.Clock.awake.now(init.io));
const compute_ns = @as(u64, @intCast(compute_duration.toNanoseconds())); const compute_ns = @as(u64, @intCast(compute_duration.toNanoseconds()));

View File

@ -93,6 +93,7 @@ pub fn deinit(self: @This()) void {
c.wgpuInstanceRelease(self.instance); c.wgpuInstanceRelease(self.instance);
} }
/// Wait for thing to be done
pub fn poll(self: @This()) void { pub fn poll(self: @This()) void {
_ = c.wgpuDevicePoll(self.device, 1, null); _ = c.wgpuDevicePoll(self.device, 1, null);
} }