Added a example.zig
This commit is contained in:
parent
0fcb9ee351
commit
545e67d72f
37
build.zig
37
build.zig
@ -29,7 +29,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
}),
|
}),
|
||||||
.name = "gpu_matrix_add",
|
.name = "bench",
|
||||||
});
|
});
|
||||||
|
|
||||||
// wgpu-native headers + pre-built static library
|
// wgpu-native headers + pre-built static library
|
||||||
@ -57,5 +57,38 @@ pub fn build(b: *std.Build) void {
|
|||||||
|
|
||||||
const run = b.addRunArtifact(exe);
|
const run = b.addRunArtifact(exe);
|
||||||
run.step.dependOn(b.getInstallStep());
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
43
src/example.zig
Normal file
43
src/example.zig
Normal file
@ -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});
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user