Shader compilation utility

This commit is contained in:
Robin Voetter
2020-07-04 04:19:32 +02:00
parent 72917ccdb3
commit 0be71d1614
3 changed files with 42 additions and 47 deletions

View File

@@ -1,56 +1,48 @@
const std = @import("std");
const vkgen = @import("generator/index.zig");
const Step = std.build.Step;
const Builder = std.build.Builder;
const path = std.fs.path;
const Resources = struct {
pub const ResourceGenStep = struct {
step: Step,
shader_step: *vkgen.ShaderCompileStep,
builder: *Builder,
wfs: *std.build.WriteFileStep,
data: std.ArrayList(u8),
full_out_path: []const u8,
resources: std.ArrayList(u8),
fn init(builder: *Builder) *Resources {
const res = builder.allocator.create(Resources) catch unreachable;
res.* = .{
pub fn init(builder: *Builder, out: []const u8) *ResourceGenStep {
const self = builder.allocator.create(ResourceGenStep) catch unreachable;
self.* = .{
.step = Step.init(.Custom, "resources", builder.allocator, make),
.shader_step = vkgen.ShaderCompileStep.init(builder, "glslc"),
.builder = builder,
.wfs = builder.addWriteFiles(),
.data = std.ArrayList(u8).init(builder.allocator),
.full_out_path = path.join(builder.allocator, &[_][]const u8{
self.builder.build_root,
builder.cache_root,
out,
}) catch unreachable,
.resources = std.ArrayList(u8).init(builder.allocator),
};
return res;
self.step.dependOn(&self.shader_step.step);
return self;
}
fn addShader(self: *Resources, shader: []const u8) void {
const spv_name = std.mem.join(
self.builder.allocator,
"",
&[_][]const u8{shader, ".spv"},
pub fn addShader(self: *ResourceGenStep, name: []const u8, source: []const u8) void {
self.resources.writer().print(
"pub const {} = @embedFile(\"{}\");\n",
.{name, self.shader_step.add("examples/shaders/test.frag")}
) catch unreachable;
const dst = path.join(
self.builder.allocator,
&[_][]const u8{self.builder.cache_root, "test.frag.spv"}
) catch unreachable;
const src = path.join(
self.builder.allocator,
&[_][]const u8{self.builder.build_root, shader}
) catch unreachable;
const compile_step = self.builder.addSystemCommand(&[_][]const u8{
"glslc",
"--target-env=vulkan1.2",
src,
"-o",
dst
});
self.wfs.step.dependOn(&compile_step.step);
const writer = self.data.writer();
writer.print("const @\"{}\" = @embedFile(\"{}\");", .{src, dst}) catch unreachable;
}
fn finalize(self: *Resources) *std.build.WriteFileStep {
self.wfs.add("resources.zig", self.data.toOwnedSlice());
return self.wfs;
fn make(step: *Step) !void {
const self = @fieldParentPtr(ResourceGenStep, "step", step);
const cwd = std.fs.cwd();
const dir = path.dirname(self.full_out_path).?;
try cwd.makePath(dir);
try cwd.writeFile(self.full_out_path, self.resources.items);
}
};
@@ -58,10 +50,6 @@ pub fn build(b: *Builder) void {
var test_step = b.step("test", "Run all the tests");
test_step.dependOn(&b.addTest("generator/index.zig").step);
const res = Resources.init(b);
res.addShader("examples/shaders/test.frag");
const wfs = res.finalize();
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const example_exe = b.addExecutable("example", "examples/main.zig");
@@ -70,11 +58,15 @@ pub fn build(b: *Builder) void {
example_exe.install();
example_exe.linkSystemLibrary("c");
example_exe.linkSystemLibrary("glfw");
example_exe.step.dependOn(&wfs.step);
const gen_step = vkgen.VkGenerateStep.init(b, "examples/vk.xml", "vk.zig");
example_exe.step.dependOn(&gen_step.step);
example_exe.addPackagePath("vulkan", gen_step.full_out_path);
const gen = vkgen.VkGenerateStep.init(b, "examples/vk.xml", "vk.zig");
example_exe.step.dependOn(&gen.step);
example_exe.addPackagePath("vulkan", gen.full_out_path);
const res = ResourceGenStep.init(b, "resources.zig");
res.addShader("test_frag", "examples/shaders/test.frag");
example_exe.step.dependOn(&res.step);
example_exe.addPackagePath("resources", res.full_out_path);
const example_run_cmd = example_exe.run();
example_run_cmd.step.dependOn(b.getInstallStep());

View File

@@ -1,6 +1,7 @@
const std = @import("std");
const vk = @import("vulkan");
const c = @import("c.zig");
const resources = @import("resources");
const GraphicsContext = @import("graphics_context.zig").GraphicsContext;
const Swapchain = @import("swapchain.zig").Swapchain;
const Allocator = std.mem.Allocator;

View File

@@ -1,4 +1,6 @@
pub const generateVk = @import("vulkan/generator.zig").generate;
pub const VkGenerateStep = @import("vulkan/build-integration.zig").GenerateStep;
pub const ShaderCompileStep = @import("build-integration.zig").ShaderCompileStep;
test "main" {
_ = @import("xml.zig");