Compile shaders in build.zig

This commit is contained in:
2026-08-30 00:48:43 +02:00
parent 40fcba9323
commit 8d5a2db342
6 changed files with 47 additions and 17 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,3 @@
.zig-cache
zig-out
zig-pkg
*.spv

View File

@@ -3,6 +3,8 @@
#extension GL_EXT_scalar_block_layout : require
#extension GL_EXT_shader_16bit_storage : require
layout(location = 0) out vec4 fragColor;
uint calcRootCode(float y1, float y2, float y3) {
uint i1 = floatBitsToUint(y1) >> 31U;
uint i2 = floatBitsToUint(y2) >> 30U;
@@ -13,3 +15,7 @@ uint calcRootCode(float y1, float y2, float y3) {
return ((0x2E74U >> shift) & 0x0101U);
}
void main() {
fragColor = vec4(1.0, 0.0, 1.0, 1.0);
}

View File

@@ -8,3 +8,7 @@ layout(location = 1) in vec4 texCoordEM_band_flags;
layout(location = 2) in vec4 jacobian;
layout(location = 3) in vec4 scale_offset;
layout(location = 4) in vec4 color;
void main() {
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
}

View File

@@ -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 });
}
}

View File

@@ -1,9 +0,0 @@
#!/bin/sh
glslc -g --target-env=vulkan1.2 assets/shaders/equirect_to_cube.comp -o src/shaders/equirect_to_cube_comp.spv
glslc -g --target-env=vulkan1.2 assets/shaders/gui_box.frag -o src/shaders/gui_box_frag.spv
glslc -g --target-env=vulkan1.2 assets/shaders/gui_box.vert -o src/shaders/gui_box_vert.spv
glslc -g --target-env=vulkan1.2 assets/shaders/main.frag -o src/shaders/main_frag.spv
glslc -g --target-env=vulkan1.2 assets/shaders/main.vert -o src/shaders/main_vert.spv
glslc -g --target-env=vulkan1.2 assets/shaders/skybox.frag -o src/shaders/skybox_frag.spv
glslc -g --target-env=vulkan1.2 assets/shaders/skybox.vert -o src/shaders/skybox_vert.spv

View File

@@ -96,10 +96,10 @@ pub const ObjectUniforms = extern struct {
material: Materials.Id,
};
pub const equirect_to_cube_comp_spv align(4) = @embedFile("shaders/equirect_to_cube_comp.spv").*;
pub const gui_box_vert_spv align(4) = @embedFile("shaders/gui_box_vert.spv").*;
pub const gui_box_frag_spv align(4) = @embedFile("shaders/gui_box_frag.spv").*;
pub const main_vert_spv align(4) = @embedFile("shaders/main_vert.spv").*;
pub const main_frag_spv align(4) = @embedFile("shaders/main_frag.spv").*;
pub const skybox_vert_spv align(4) = @embedFile("shaders/skybox_vert.spv").*;
pub const skybox_frag_spv align(4) = @embedFile("shaders/skybox_frag.spv").*;
pub const equirect_to_cube_comp_spv align(4) = @embedFile("shaders/equirect_to_cube.comp").*;
pub const gui_box_vert_spv align(4) = @embedFile("shaders/gui_box.vert").*;
pub const gui_box_frag_spv align(4) = @embedFile("shaders/gui_box.frag").*;
pub const main_vert_spv align(4) = @embedFile("shaders/main.vert").*;
pub const main_frag_spv align(4) = @embedFile("shaders/main.frag").*;
pub const skybox_vert_spv align(4) = @embedFile("shaders/skybox.vert").*;
pub const skybox_frag_spv align(4) = @embedFile("shaders/skybox.frag").*;