From 737cc5290867320d78a4396f82e396a3e23a7589 Mon Sep 17 00:00:00 2001 From: ashpil Date: Sat, 21 Jan 2023 19:13:59 -0500 Subject: [PATCH] allow specifying additional watches files for shader compilation caching --- generator/build_integration.zig | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/generator/build_integration.zig b/generator/build_integration.zig index f16d4e1..6d34e6d 100644 --- a/generator/build_integration.zig +++ b/generator/build_integration.zig @@ -15,14 +15,37 @@ pub const ShaderCompileStep = struct { /// This structure contains additional options that pertain to specific shaders only. pub const ShaderOptions = struct { /// Additional arguments that should be passed to the shader compiler. - additional_args: [][]const u8 = &.{}, + args: []const []const u8 = &.{}, + + /// Paths of additional files that should be watched for changes to + /// trigger recompilation. + watched_files: []const []const u8 = &.{}, /// To ensure that if compilation options change, the shader is recompiled /// properly. - fn hash(self: ShaderOptions, hasher: anytype) void { - for (self.additional_args) |arg| { + fn hash(self: ShaderOptions, b: *Builder, hasher: anytype) !void { + for (self.args) |arg| { hasher.update(arg); } + for (self.watched_files) |file_path| { + const full_path = std.fs.path.join(b.allocator, &.{ + b.build_root, + file_path, + }) catch unreachable; + + const source = std.fs.cwd().readFileAlloc( + b.allocator, + full_path, + std.math.maxInt(usize), + ) catch |err| switch (err) { + error.FileNotFound => { + std.log.err("could not open file '{s}'", .{file_path}); + return error.FileNotFound; + }, + else => |e| return e, + }; + hasher.update(source); + } } }; @@ -121,7 +144,7 @@ pub const ShaderCompileStep = struct { hasher.update(source); // Not only the shader source must be the same to ensure uniqueness - // the compilation options must be the same as well! - shader.options.hash(&hasher); + try shader.options.hash(self.b, &hasher); // And the compile command, too. for (self.compile_command) |cmd| { hasher.update(cmd); @@ -182,7 +205,7 @@ pub const ShaderCompileStep = struct { cmd.items.len = base_cmd_len; - try cmd.appendSlice(shader.options.additional_args); + try cmd.appendSlice(shader.options.args); try cmd.appendSlice(&.{ shader.source_path, self.output_flag, shader_out_path }); try self.b.spawnChild(cmd.items); }