forked from mirror/vulkan-zig
Merge pull request #69 from ashpil/arbitrary-shader-compiler
Remove a couple glslc specific assumptions to allow an arbitrary shader compiler
This commit is contained in:
@@ -27,6 +27,7 @@ pub fn build(b: *Builder) void {
|
|||||||
const shaders = vkgen.ShaderCompileStep.create(
|
const shaders = vkgen.ShaderCompileStep.create(
|
||||||
b,
|
b,
|
||||||
&[_][]const u8{ "glslc", "--target-env=vulkan1.2" },
|
&[_][]const u8{ "glslc", "--target-env=vulkan1.2" },
|
||||||
|
"-o",
|
||||||
);
|
);
|
||||||
shaders.add("triangle_vert", "examples/shaders/triangle.vert", .{});
|
shaders.add("triangle_vert", "examples/shaders/triangle.vert", .{});
|
||||||
shaders.add("triangle_frag", "examples/shaders/triangle.frag", .{});
|
shaders.add("triangle_frag", "examples/shaders/triangle.frag", .{});
|
||||||
|
|||||||
@@ -4,42 +4,24 @@ const Builder = std.build.Builder;
|
|||||||
const Step = std.build.Step;
|
const Step = std.build.Step;
|
||||||
const GeneratedFile = std.build.GeneratedFile;
|
const GeneratedFile = std.build.GeneratedFile;
|
||||||
|
|
||||||
/// Stage the shader should be built for. This is passed to the -fshader-stage
|
|
||||||
/// argument when invoking glslc.
|
|
||||||
pub const ShaderStage = enum {
|
|
||||||
vertex,
|
|
||||||
fragment,
|
|
||||||
tesscontrol,
|
|
||||||
tesseval,
|
|
||||||
geometry,
|
|
||||||
compute,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Utility functionality to help with compiling shaders from build.zig.
|
/// Utility functionality to help with compiling shaders from build.zig.
|
||||||
/// Invokes glslc (or another shader compiler passed to `init`) for each shader
|
/// Invokes a shader compile command (e.g., glslc ...) for each shader
|
||||||
/// added via `addShader`.
|
/// added via `addShader`.
|
||||||
pub const ShaderCompileStep = struct {
|
pub const ShaderCompileStep = struct {
|
||||||
/// The directory within the zig-cache directory that is used to store
|
/// The directory within the zig-cache directory that is used to store
|
||||||
/// shader artifacts.
|
/// shader artifacts.
|
||||||
pub const cache_dir = "shaders";
|
pub const cache_dir = "shaders";
|
||||||
|
|
||||||
/// This structure contains additional options that can be passed to glslc when shaders are compiled.
|
/// This structure contains additional options that pertain to specific shaders only.
|
||||||
pub const ShaderOptions = struct {
|
pub const ShaderOptions = struct {
|
||||||
/// The entry point to use when compiling the shader.
|
/// Additional arguments that should be passed to the shader compiler.
|
||||||
entry_point: ?[]const u8 = null,
|
additional_args: [][]const u8 = &.{},
|
||||||
|
|
||||||
/// The stage to use when building. If not null, this is passed to
|
|
||||||
/// the -fshader-stage argument.
|
|
||||||
stage: ?ShaderStage = null,
|
|
||||||
|
|
||||||
/// To ensure that if compilation options change, the shader is recompiled
|
/// To ensure that if compilation options change, the shader is recompiled
|
||||||
/// properly.
|
/// properly.
|
||||||
fn hash(self: ShaderOptions, hasher: anytype) void {
|
fn hash(self: ShaderOptions, hasher: anytype) void {
|
||||||
if (self.entry_point) |entry_point| {
|
for (self.additional_args) |arg| {
|
||||||
hasher.update(entry_point);
|
hasher.update(arg);
|
||||||
}
|
|
||||||
if (self.stage) |stage| {
|
|
||||||
hasher.update(std.mem.asBytes(&@enumToInt(stage)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -61,7 +43,10 @@ pub const ShaderCompileStep = struct {
|
|||||||
b: *Builder,
|
b: *Builder,
|
||||||
|
|
||||||
/// The command and optional arguments used to invoke the shader compiler.
|
/// The command and optional arguments used to invoke the shader compiler.
|
||||||
glslc_cmd: []const []const u8,
|
compile_command: []const []const u8,
|
||||||
|
|
||||||
|
/// The compiler flag used to specify the output path, `-o` most of the time
|
||||||
|
output_flag: []u8,
|
||||||
|
|
||||||
/// List of shaders that are to be compiled.
|
/// List of shaders that are to be compiled.
|
||||||
shaders: std.ArrayList(Shader),
|
shaders: std.ArrayList(Shader),
|
||||||
@@ -71,13 +56,16 @@ pub const ShaderCompileStep = struct {
|
|||||||
generated_file: GeneratedFile,
|
generated_file: GeneratedFile,
|
||||||
|
|
||||||
/// Create a ShaderCompileStep for `builder`. When this step is invoked by the build
|
/// Create a ShaderCompileStep for `builder`. When this step is invoked by the build
|
||||||
/// system, `<glcl_cmd...> <shader_source> -o <path>` is invoked for each shader.
|
/// system, `<compile_command...> <shader_source> <output_flag> <path>` is invoked for each shader.
|
||||||
pub fn create(builder: *Builder, glslc_cmd: []const []const u8) *ShaderCompileStep {
|
/// For example, if one calls this with `create(b, "glslc", "-o")` and then
|
||||||
|
/// `c.addShader("vertex", "vertex.glsl", .{})`, the command will be `glslc vertex.glsl -o <path>`
|
||||||
|
pub fn create(builder: *Builder, compile_command: []const []const u8, output_flag: []const u8) *ShaderCompileStep {
|
||||||
const self = builder.allocator.create(ShaderCompileStep) catch unreachable;
|
const self = builder.allocator.create(ShaderCompileStep) catch unreachable;
|
||||||
self.* = .{
|
self.* = .{
|
||||||
.step = Step.init(.custom, "shaders", builder.allocator, make),
|
.step = Step.init(.custom, "shaders", builder.allocator, make),
|
||||||
.b = builder,
|
.b = builder,
|
||||||
.glslc_cmd = builder.dupeStrings(glslc_cmd),
|
.compile_command = builder.dupeStrings(compile_command),
|
||||||
|
.output_flag = builder.dupe(output_flag),
|
||||||
.shaders = std.ArrayList(Shader).init(builder.allocator),
|
.shaders = std.ArrayList(Shader).init(builder.allocator),
|
||||||
.generated_file = undefined,
|
.generated_file = undefined,
|
||||||
};
|
};
|
||||||
@@ -98,8 +86,7 @@ pub const ShaderCompileStep = struct {
|
|||||||
/// Add a shader to be compiled. `src` is shader source path, relative to the project root.
|
/// Add a shader to be compiled. `src` is shader source path, relative to the project root.
|
||||||
/// Returns the full path where the compiled binary will be stored upon successful compilation.
|
/// Returns the full path where the compiled binary will be stored upon successful compilation.
|
||||||
/// This path can then be used to include the binary into an executable, for example by passing it
|
/// This path can then be used to include the binary into an executable, for example by passing it
|
||||||
/// to @embedFile via an additional generated file. `entry_point` is the entry point to pass to the compiler.
|
/// to @embedFile via an additional generated file.
|
||||||
/// `stage` is an optional shader stage to pass to the compiler with the flag `-fshader-stage` when building the shader.
|
|
||||||
pub fn add(self: *ShaderCompileStep, name: []const u8, src: []const u8, options: ShaderOptions) void {
|
pub fn add(self: *ShaderCompileStep, name: []const u8, src: []const u8, options: ShaderOptions) void {
|
||||||
const full_source_path = std.fs.path.join(self.b.allocator, &.{
|
const full_source_path = std.fs.path.join(self.b.allocator, &.{
|
||||||
self.b.build_root,
|
self.b.build_root,
|
||||||
@@ -136,7 +123,7 @@ pub const ShaderCompileStep = struct {
|
|||||||
// the compilation options must be the same as well!
|
// the compilation options must be the same as well!
|
||||||
shader.options.hash(&hasher);
|
shader.options.hash(&hasher);
|
||||||
// And the compile command, too.
|
// And the compile command, too.
|
||||||
for (self.glslc_cmd) |cmd| {
|
for (self.compile_command) |cmd| {
|
||||||
hasher.update(cmd);
|
hasher.update(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,7 +145,7 @@ pub const ShaderCompileStep = struct {
|
|||||||
const cwd = std.fs.cwd();
|
const cwd = std.fs.cwd();
|
||||||
|
|
||||||
var cmd = std.ArrayList([]const u8).init(self.b.allocator);
|
var cmd = std.ArrayList([]const u8).init(self.b.allocator);
|
||||||
try cmd.appendSlice(self.glslc_cmd);
|
try cmd.appendSlice(self.compile_command);
|
||||||
const base_cmd_len = cmd.items.len;
|
const base_cmd_len = cmd.items.len;
|
||||||
|
|
||||||
var shaders_file_contents = std.ArrayList(u8).init(self.b.allocator);
|
var shaders_file_contents = std.ArrayList(u8).init(self.b.allocator);
|
||||||
@@ -183,7 +170,7 @@ pub const ShaderCompileStep = struct {
|
|||||||
&shader_basename,
|
&shader_basename,
|
||||||
});
|
});
|
||||||
|
|
||||||
// If we have a cache hit, we can save some compile time by not invoking glslc.
|
// If we have a cache hit, we can save some compile time by not invoking the compile command.
|
||||||
compile_shader: {
|
compile_shader: {
|
||||||
std.fs.accessAbsolute(shader_out_path, .{}) catch |err| switch (err) {
|
std.fs.accessAbsolute(shader_out_path, .{}) catch |err| switch (err) {
|
||||||
error.FileNotFound => break :compile_shader,
|
error.FileNotFound => break :compile_shader,
|
||||||
@@ -195,15 +182,8 @@ pub const ShaderCompileStep = struct {
|
|||||||
|
|
||||||
cmd.items.len = base_cmd_len;
|
cmd.items.len = base_cmd_len;
|
||||||
|
|
||||||
if (shader.options.entry_point) |entry_point| {
|
try cmd.appendSlice(shader.options.additional_args);
|
||||||
try cmd.append(try std.fmt.allocPrint(self.b.allocator, "-fentry-point={s}", .{entry_point}));
|
try cmd.appendSlice(&.{ shader.source_path, self.output_flag, shader_out_path });
|
||||||
}
|
|
||||||
|
|
||||||
if (shader.options.stage) |stage| {
|
|
||||||
try cmd.append(try std.fmt.allocPrint(self.b.allocator, "-fshader-stage={s}", .{@tagName(stage)}));
|
|
||||||
}
|
|
||||||
|
|
||||||
try cmd.appendSlice(&.{ shader.source_path, "-o", shader_out_path });
|
|
||||||
try self.b.spawnChild(cmd.items);
|
try self.b.spawnChild(cmd.items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user