From 545e67d72f7bdcd15ebb629837b30da3eb3f35ce Mon Sep 17 00:00:00 2001 From: adrien Date: Mon, 18 May 2026 00:19:37 +0200 Subject: [PATCH] Added a example.zig --- build.zig | 37 +++++++++++++++++++++++++++++++++++-- src/example.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 src/example.zig diff --git a/build.zig b/build.zig index 3e81041..ea522f2 100644 --- a/build.zig +++ b/build.zig @@ -29,7 +29,7 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }), - .name = "gpu_matrix_add", + .name = "bench", }); // wgpu-native headers + pre-built static library @@ -57,5 +57,38 @@ pub fn build(b: *std.Build) void { const run = b.addRunArtifact(exe); run.step.dependOn(b.getInstallStep()); - b.step("bench", "Benchmark a simple add vector").dependOn(&run.step); + b.step("bench", "Benchmark a simple add vector.").dependOn(&run.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); } diff --git a/src/example.zig b/src/example.zig new file mode 100644 index 0000000..560b01e --- /dev/null +++ b/src/example.zig @@ -0,0 +1,43 @@ +const std = @import("std"); +const GpuDevice = @import("GpuDevice.zig"); +const GpuAllocator = @import("GpuAllocator.zig"); +const GpuPipeline = @import("GpuPipeline.zig"); +const Vec = @import("Vec.zig"); + +const c = @import("utils.zig").c; + +pub fn main(init: std.process.Init) !void { + const allocator = init.gpa; + + const device = try GpuDevice.init(.{ .vram_bytes_limit = 4 * 1024 * 1024 * 1024 }); + defer device.deinit(); + + var gloc = try GpuAllocator.init(allocator, device); + defer gloc.deinit(); + + const add_pip = try GpuPipeline.init(device, @embedFile("shaders/add.wgsl")); + defer add_pip.deinit(); + + const data_a = try allocator.alloc(f32, 1024); + defer allocator.free(data_a); + const data_b = try allocator.alloc(f32, 1024); + defer allocator.free(data_b); + + for (0..1024) |i| { + data_a[i] = @floatFromInt(i); + data_b[i] = @floatFromInt(1024 - 1 - i); + } + + const a = try Vec.initLoad(&gloc, data_a); + defer a.deinit(); + const b = try Vec.initLoad(&gloc, data_b); + defer b.deinit(); + + const sum = try a.run(&gloc, b, add_pip); + defer sum.deinit(); + + const out = try sum.read(&gloc, allocator); + defer allocator.free(out); + + std.debug.print("{any}\n", .{out}); +}