Compile shaders in build.zig
This commit is contained in:
30
build.zig
30
build.zig
@@ -48,6 +48,8 @@ pub fn build(b: *std.Build) !void {
|
||||
exe_mod.addImport("zglfw", zglfw_mod);
|
||||
exe_mod.linkLibrary(zglfw_lib);
|
||||
|
||||
try addShaders(b, exe_mod);
|
||||
|
||||
const options = b.addOptions();
|
||||
options.addOption([]const u8, "version", zon.version);
|
||||
exe_mod.addOptions("config", options);
|
||||
@@ -70,3 +72,31 @@ pub fn build(b: *std.Build) !void {
|
||||
const run_step = b.step("run", "Run the game");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
}
|
||||
|
||||
fn isShaderExtension(extension: []const u8) bool {
|
||||
return std.mem.eql(u8, extension, ".vert") or
|
||||
std.mem.eql(u8, extension, ".frag") or
|
||||
std.mem.eql(u8, extension, ".comp");
|
||||
}
|
||||
|
||||
fn addShaders(b: *std.Build, exe_mod: *std.Build.Module) !void {
|
||||
var dir = try b.build_root.handle.openDir(b.graph.io, "assets/shaders", .{ .iterate = true });
|
||||
defer dir.close(b.graph.io);
|
||||
|
||||
var walker = try dir.walk(b.allocator);
|
||||
defer walker.deinit();
|
||||
|
||||
while (try walker.next(b.graph.io)) |entry| {
|
||||
if (entry.kind != .file) continue;
|
||||
if (!isShaderExtension(std.fs.path.extension(entry.path))) continue;
|
||||
|
||||
const glslc = b.addSystemCommand(&.{ "glslc", "-g", "--target-env=vulkan1.2" });
|
||||
glslc.addFileArg(b.path(b.pathJoin(&.{ "assets/shaders", entry.path })));
|
||||
glslc.addArg("-o");
|
||||
const spv = glslc.addOutputFileArg(b.fmt("{s}.spv", .{entry.basename}));
|
||||
glslc.addArgs(&.{ "-MD", "-MF" });
|
||||
_ = glslc.addDepFileOutputArg(b.fmt("{s}.d", .{entry.basename}));
|
||||
|
||||
exe_mod.addAnonymousImport(b.fmt("shaders/{s}", .{entry.path}), .{ .root_source_file = spv });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user