Skip to content

Commit

Permalink
[progress #146] shaderc resolve include
Browse files Browse the repository at this point in the history
  • Loading branch information
tiawl committed Apr 13, 2024
1 parent e55d411 commit bb3b81e
Show file tree
Hide file tree
Showing 58 changed files with 393 additions and 240 deletions.
49 changes: 24 additions & 25 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const zig_version = @import ("builtin").zig_version;
const glfw = @import ("build/glfw.zig");
const vk = @import ("build/vk.zig");
const imgui = @import ("build/imgui.zig");
const shaders_compile = @import ("build/shaders/step.zig");
const shaders = @import ("build/shaders.zig");

const utils = @import ("build/utils.zig");
const Options = utils.Options;
Expand All @@ -16,8 +16,8 @@ pub fn build (builder: *std.Build) !void
{
try requirements ();
const profile = try parse_options (builder);
const shaders = try run_shader_compiler (builder, &profile);
try run_exe (builder, &profile, shaders);
const shaders_module = try run_shaders_compiler (builder, &profile);
try run_exe (builder, &profile, shaders_module);
try run_test (builder, &profile);
}

Expand All @@ -42,7 +42,7 @@ fn turbo (profile: *Profile) !void
profile.compile_options = .{
.optimization = .Performance,
.vulkan_env_version = std.meta.stringToEnum (
shaders_compile.Options.VulkanEnvVersion, profile.options.vkminor).?,
shaders.Options.VulkanEnvVersion, profile.options.vkminor).?,
};
}

Expand Down Expand Up @@ -70,7 +70,7 @@ fn dev (builder: *std.Build, profile: *Profile) !void
profile.compile_options = .{
.optimization = .Zero,
.vulkan_env_version = std.meta.stringToEnum (
shaders_compile.Options.VulkanEnvVersion, profile.options.vkminor).?,
shaders.Options.VulkanEnvVersion, profile.options.vkminor).?,
};
}

Expand All @@ -86,7 +86,7 @@ fn default (builder: *std.Build, profile: *Profile) !void
profile.compile_options = .{
.optimization = .Zero,
.vulkan_env_version = std.meta.stringToEnum (
shaders_compile.Options.VulkanEnvVersion,
shaders.Options.VulkanEnvVersion,
profile.options.vkminor).?,
};
}
Expand Down Expand Up @@ -157,7 +157,7 @@ fn link (builder: *std.Build, profile: *const Profile) !*Package
const cimgui = imgui_dep.artifact ("cimgui");

const c = try Package.init (builder, profile, "c", try builder.build_root.join (
builder.allocator, &.{ "src", "binding", "raw.zig", }));
builder.allocator, &.{ "src", zon.name, "bindings", "raw.zig", }));
c.link (glfw_lib);
c.link (cimgui);
c.include (imgui_dep.path ("imgui"));
Expand All @@ -181,7 +181,6 @@ fn import (builder: *std.Build, exe: *std.Build.Step.Compile,
});
const datetime = datetime_dep.module ("zig-datetime");

// const shaders_module = try shaders.import (builder, exe, profile);
const c = try link (builder, profile);
const glfw_pkg = try glfw.import (builder, profile, c);
const vk_pkg = try vk.import (builder, profile, c);
Expand All @@ -192,7 +191,7 @@ fn import (builder: *std.Build, exe: *std.Build.Step.Compile,
const build_options = profile.variables.createModule ();
const logger = builder.createModule (.{
.root_source_file = .{ .path = try builder.build_root.join (
builder.allocator, &.{ "src", "logger.zig", }), },
builder.allocator, &.{ "src", zon.name, "logger.zig", }), },
.target = profile.target,
.optimize = profile.optimize,
});
Expand All @@ -201,7 +200,7 @@ fn import (builder: *std.Build, exe: *std.Build.Step.Compile,

const instance = builder.createModule (.{
.root_source_file = .{ .path = try builder.build_root.join (
builder.allocator, &.{ "src", "vk", "instance",
builder.allocator, &.{ "src", zon.name, "vk", "instance",
if (profile.options.turbo) "turbo.zig" else "default.zig", }), },
.target = profile.target,
.optimize = profile.optimize,
Expand All @@ -220,13 +219,13 @@ fn import (builder: *std.Build, exe: *std.Build.Step.Compile,
}) |module| exe.root_module.addImport (module.name, module.ptr);
}

fn run_shader_compiler (builder: *std.Build,
fn run_shaders_compiler (builder: *std.Build,
profile: *const Profile) !*std.Build.Module
{
const shader_compiler = builder.addExecutable (.{
.name = "shader_compiler",
const shaders_compiler = builder.addExecutable (.{
.name = "shaders_compiler",
.root_source_file = .{ .path = try builder.build_root.join (
builder.allocator, &.{ "build", "shaders", "compiler.zig", }), },
builder.allocator, &.{ "src", "compiler", "main.zig", }), },
.target = builder.host,
.optimize = .Debug,
});
Expand All @@ -239,42 +238,42 @@ fn run_shader_compiler (builder: *std.Build,

const c = builder.createModule (.{
.root_source_file = .{ .path = try builder.build_root.join (
builder.allocator, &.{ "build", "shaders", "raw.zig", }), },
builder.allocator, &.{ "src", "compiler", "bindings", "raw.zig", }), },
.target = builder.host,
.optimize = .Debug,
});
c.linkLibrary (shaderc);
shader_compiler.root_module.addImport ("c", c);
shaders_compiler.root_module.addImport ("c", c);

const install_shader_compiler =
builder.addInstallArtifact (shader_compiler, .{});
const install_shaders_compiler =
builder.addInstallArtifact (shaders_compiler, .{});

builder.install_tls.step.dependOn (&install_shader_compiler.step);
builder.install_tls.step.dependOn (&install_shaders_compiler.step);

var self_dependency = std.Build.Dependency { .builder = builder, };

const shaders_module = try shaders_compile.Step.compileModule (
const shaders_module = try shaders.Step.compileModule (
&self_dependency, profile.compile_options);

const shader_compile_step = builder.addRunArtifact (shader_compiler);
const shaders_compile_step = builder.addRunArtifact (shaders_compiler);

shader_compile_step.step.dependOn (&install_shader_compiler.step);
shaders_compile_step.step.dependOn (&install_shaders_compiler.step);

return shaders_module;
}

fn run_exe (builder: *std.Build, profile: *const Profile,
shaders: *std.Build.Module) !void
shaders_module: *std.Build.Module) !void
{
const exe = builder.addExecutable (.{
.name = zon.name,
.root_source_file = .{ .path = try builder.build_root.join (
builder.allocator, &.{ "src", "main.zig", }), },
builder.allocator, &.{ "src", zon.name, "main.zig", }), },
.target = profile.target,
.optimize = profile.optimize,
});

try import (builder, exe, profile, shaders);
try import (builder, exe, profile, shaders_module);

builder.installArtifact (exe);

Expand Down
3 changes: 2 additions & 1 deletion build/glfw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const std = @import ("std");
const utils = @import ("utils.zig");
const Package = utils.Package;
const Profile = utils.Profile;
const zon = utils.zon;

pub fn import (builder: *std.Build, profile: *const Profile,
c: *Package) !*Package
{
const path = try builder.build_root.join (builder.allocator,
&.{ "src", "binding", "glfw", });
&.{ "src", zon.name, "bindings", "glfw", });

var glfw = try Package.init (builder, profile, "glfw",
try std.fs.path.join (builder.allocator, &.{ path, "glfw.zig", }));
Expand Down
3 changes: 2 additions & 1 deletion build/imgui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const std = @import ("std");
const utils = @import ("utils.zig");
const Package = utils.Package;
const Profile = utils.Profile;
const zon = utils.zon;

pub fn import (builder: *std.Build, profile: *const Profile,
c: *Package, glfw: *Package, vk: *Package) !*Package
{
const path = try builder.build_root.join (builder.allocator,
&.{ "src", "binding", "imgui", });
&.{ "src", zon.name, "bindings", "imgui", });

var imgui = try Package.init (builder, profile, "imgui",
try std.fs.path.join (builder.allocator, &.{ path, "imgui.zig", }));
Expand Down
122 changes: 63 additions & 59 deletions build/shaders/step.zig → build/shaders.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import ("std");

const utils = @import ("../utils.zig");
const utils = @import ("utils.zig");
const digest = utils.digest;

const Node = struct
Expand Down Expand Up @@ -31,7 +31,7 @@ pub const Options = struct
pub const Step = struct
{
step: std.Build.Step,
shader_compiler: *std.Build.Step.Run,
shaders_compiler: *std.Build.Step.Run,
generated_file: std.Build.GeneratedFile,
options: Options,
tree: Node,
Expand All @@ -41,15 +41,15 @@ pub const Step = struct
{
const builder = dependency.builder;
const self = try builder.allocator.create (@This ());
const shader_compiler = dependency.artifact ("shader_compiler");
const shaders_compiler = dependency.artifact ("shaders_compiler");
self.* = .{
.step = std.Build.Step.init (.{
.id = .custom,
.name = "shaders compilation",
.owner = builder,
.makeFn = make,
}),
.shader_compiler = builder.addRunArtifact (shader_compiler),
.shaders_compiler = builder.addRunArtifact (shaders_compiler),
.generated_file = undefined,
.options = options,
.tree = .{
Expand All @@ -58,11 +58,7 @@ pub const Step = struct
},
};
self.generated_file = .{ .step = &self.step, };
self.step.dependOn (&shader_compiler.step);
for ([_][] const u8 {
@tagName (self.options.optimization),
@tagName (self.options.vulkan_env_version),
}) |arg| self.shader_compiler.addArg (arg);
self.step.dependOn (&shaders_compiler.step);
return self;
}

Expand All @@ -71,40 +67,43 @@ pub const Step = struct
ptr: *Node, depth: *usize) !void
{
depth.* += 1;
if (std.mem.eql (u8, entry.basename, dupe))
{
const path = builder.dupe (entry.path);
const source = try dir.readFileAlloc (builder.allocator, path,
std.math.maxInt (usize));
try ptr.nodes.append (.{
.name = std.fs.path.extension (dupe) [1 ..],
.nodes = std.ArrayList (Node).init (builder.allocator),
.hash = digest (self.options, source),
.depth = depth.*,
});

const out = try std.fs.path.join (builder.allocator, &.{
try builder.cache_root.join (builder.allocator, &.{ "shaders", }),
&ptr.nodes.items [ptr.nodes.items.len - 1].hash,
});
const in = try std.fs.path.join (builder.allocator, &.{
try builder.build_root.join (builder.allocator, &.{ "shaders", }), path,
});

// If we have a cache hit, we can save some compile time by not
// invoking the compiler again
shader_not_found: {
std.fs.accessAbsolute (out, .{}) catch |err| switch (err)
{
error.FileNotFound => break :shader_not_found,
else => return err,
};
return;
}

for ([_][] const u8 { in, out, }) |arg|
self.shader_compiler.addArg (arg);
if (!std.mem.eql (u8, entry.basename, dupe)) return;

const path = builder.dupe (entry.path);
const source = try dir.readFileAlloc (builder.allocator, path,
std.math.maxInt (usize));

if (!std.mem.startsWith (u8, source, "#version ")) return;

try ptr.nodes.append (.{
.name = std.fs.path.extension (dupe) [1 ..],
.nodes = std.ArrayList (Node).init (builder.allocator),
.hash = digest (self.options, source),
.depth = depth.*,
});

const in = try std.fs.path.join (builder.allocator, &.{
try builder.build_root.join (builder.allocator, &.{ "shaders", }), path,
});

const out = try std.fs.path.join (builder.allocator, &.{
try builder.cache_root.join (builder.allocator, &.{ "shaders", }),
&ptr.nodes.items [ptr.nodes.items.len - 1].hash,
});

// If we have a cache hit, we can save some compile time by not
// invoking the compiler again
shader_not_found: {
std.fs.accessAbsolute (out, .{}) catch |err| switch (err)
{
error.FileNotFound => break :shader_not_found,
else => return err,
};
return;
}

for ([_][] const u8 { in, out, }) |arg| self.shaders_compiler.addArg (arg);
}

fn walk_through (self: *@This (), builder: *std.Build) !void
Expand All @@ -128,7 +127,7 @@ pub const Step = struct
depth = 0;
next: while (iterator.next ()) |*component|
{
const dupe = try builder.allocator.dupe (u8, component.name);
const dupe = builder.dupe (component.name);
const stem = std.fs.path.stem (dupe);
for (pointer.nodes.items) |*node|
{
Expand Down Expand Up @@ -164,23 +163,23 @@ pub const Step = struct
{
node = stack.pop ();

try writer.print ("pub const @\"{s}\" ", .{ node.name, });

if (node.nodes.items.len == 0 and (std.mem.eql (u8, node.name, "frag")
or std.mem.eql (u8, node.name, "vert")))
if (node.name.len == 0)
{
try writer.print ("align(@alignOf(u32)) = @embedFile(\"{s}\").*;\n",
.{ node.hash, });
} else try writer.print ("= struct {c}\n", .{ '{', });

for (node.nodes.items) |*child| try stack.append (child.*);

if (stack.getLastOrNull ()) |last|
{
if (node.depth > last.depth)
for (0 .. node.depth - last.depth) |_|
try writer.print ("{c};\n", .{ '}', });
} else try writer.print ("{c};\n", .{ '}', });
try writer.print ("{c};\n", .{ '}', });
} else if (node.nodes.items.len == 0 and
(std.mem.eql (u8, node.name, "frag") or
std.mem.eql (u8, node.name, "vert"))) {
try writer.print ("pub const @\"{s}\" align(@alignOf(u32)) = @embedFile(\"{s}\").*;\n",
.{ node.name, node.hash, });
} else {
try writer.print ("pub const @\"{s}\" = struct {c}\n",
.{ node.name, '{', });
try stack.append (.{
.name = "",
.nodes = undefined,
});
try stack.appendSlice (node.nodes.items);
}
}

try buffer.append (0);
Expand Down Expand Up @@ -215,9 +214,14 @@ pub const Step = struct
if (err != error.PathAlreadyExists) return err;
};

for ([_][] const u8 {
@tagName (self.options.optimization),
@tagName (self.options.vulkan_env_version),
}) |arg| self.shaders_compiler.addArg (arg);

try self.walk_through (builder);

try self.shader_compiler.step.makeFn (&self.shader_compiler.step,
try self.shaders_compiler.step.makeFn (&self.shaders_compiler.step,
progress_node);

try self.write_index (builder);
Expand Down
Loading

0 comments on commit bb3b81e

Please sign in to comment.