Skip to content

Commit

Permalink
[progress #146] try to replace glslc with shaderc
Browse files Browse the repository at this point in the history
  • Loading branch information
tiawl committed Apr 11, 2024
1 parent b18b20c commit b684645
Show file tree
Hide file tree
Showing 8 changed files with 421 additions and 192 deletions.
85 changes: 70 additions & 15 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ 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 = @import ("build/shaders.zig");
const ShaderCompileStep = @import ("build/shaders/step.zig").Step;
const ShaderCompileOptions = @import ("build/shaders/options.zig").Options;

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

Expand All @@ -28,7 +30,7 @@ fn requirements () !void
.{ zon.name, zon.min_zig_version, });
}

fn turbo (builder: *std.Build, profile: *Profile) !void
fn turbo (profile: *Profile) !void
{
// Keep this for debug purpose
// profile.optimize = .Debug;
Expand All @@ -38,9 +40,11 @@ fn turbo (builder: *std.Build, profile: *Profile) !void
profile.variables.addOption (u8, "log_level", 0);
profile.variables.addOption ([] const [] const [] const u8,
"vk_optional_extensions", &.{});
profile.command = &.{ "glslc", "-O", try std.fmt.allocPrint (
builder.allocator, "--target-env=vulkan1.{s}",
.{ profile.options.vkminor, }), };
profile.compile_options = .{
.optimization = .Performance,
.vulkan_env_version = std.meta.stringToEnum (
ShaderCompileOptions.VulkanEnvVersion, profile.options.vkminor).?,
};
}

fn dev (builder: *std.Build, profile: *Profile) !void
Expand All @@ -64,8 +68,11 @@ fn dev (builder: *std.Build, profile: *Profile) !void
&.{ "EXT", "VALIDATION_FEATURES", },
&.{ "KHR", "SHADER_NON_SEMANTIC_INFO", },
});
profile.command = &.{ "glslc", try std.fmt.allocPrint (builder.allocator,
"--target-env=vulkan1.{s}", .{ profile.options.vkminor, }), };
profile.compile_options = .{
.optimization = .Zero,
.vulkan_env_version = std.meta.stringToEnum (
ShaderCompileOptions.VulkanEnvVersion, profile.options.vkminor).?,
};
}

fn default (builder: *std.Build, profile: *Profile) !void
Expand All @@ -77,8 +84,12 @@ fn default (builder: *std.Build, profile: *Profile) !void
"vk_optional_extensions", &.{
&.{ "EXT", "DEVICE_ADDRESS_BINDING_REPORT", },
});
profile.command = &.{ "glslc", try std.fmt.allocPrint (builder.allocator,
"--target-env=vulkan1.{s}", .{ profile.options.vkminor, }), };
profile.compile_options = .{
.optimization = .Zero,
.vulkan_env_version = std.meta.stringToEnum (
ShaderCompileOptions.VulkanEnvVersion,
profile.options.vkminor).?,
};
}

fn parse_options (builder: *std.Build) !Profile
Expand Down Expand Up @@ -129,7 +140,7 @@ fn parse_options (builder: *std.Build) !Profile
profile.variables.addOption ([] const u8, "vk_minor",
profile.options.vkminor);

if (profile.options.turbo) try turbo (builder, &profile)
if (profile.options.turbo) try turbo (&profile)
else if (profile.options.dev) try dev (builder, &profile)
else try default (builder, &profile);

Expand Down Expand Up @@ -163,15 +174,15 @@ fn manage_deps (glfw_pkg: *Package, vk_pkg: *Package) !void
}

fn import (builder: *std.Build, exe: *std.Build.Step.Compile,
profile: *const Profile) !void
profile: *const Profile, shaders_module: *std.Build.Module) !void
{
const datetime_dep = builder.dependency ("zig-datetime", .{
.target = profile.target,
.optimize = profile.optimize,
});
const datetime = datetime_dep.module ("zig-datetime");

const shaders_module = try shaders.import (builder, exe, profile);
// 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 Down Expand Up @@ -210,7 +221,51 @@ fn import (builder: *std.Build, exe: *std.Build.Step.Compile,
}) |module| exe.root_module.addImport (module.name, module.ptr);
}

fn run_exe (builder: *std.Build, profile: *const Profile) !void
fn run_shader_compiler (builder: *std.Build,
profile: *const Profile) !*std.Build.Module
{
const shader_compiler = builder.addExecutable (.{
.name = "shader_compiler",
.root_source_file = .{ .path = try builder.build_root.join (
builder.allocator, &.{ "build", "shaders", "compiler.zig", }), },
.target = builder.host,
.optimize = .Debug,
});

const shaderc_dep = builder.dependency ("shaderc", .{
.target = profile.target,
.optimize = profile.optimize,
});
const shaderc = shaderc_dep.artifact ("shaderc");

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

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

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

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

const shaders_module = ShaderCompileStep.compileModule (
&self_dependency, &profile.compile_options);

const shader_compile_step = builder.addRunArtifact (shader_compiler);

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

return shaders_module;
}

fn run_exe (builder: *std.Build, profile: *const Profile,
shaders: *std.Build.Module) !void
{
const exe = builder.addExecutable (.{
.name = zon.name,
Expand All @@ -220,7 +275,7 @@ fn run_exe (builder: *std.Build, profile: *const Profile) !void
.optimize = profile.optimize,
});

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

builder.installArtifact (exe);

Expand Down
8 changes: 6 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
.dependencies = .{
.cimgui = .{
.url = "https://github.com/tiawl/cimgui.zig/archive/refs/tags/v1.90.4.tar.gz",
.hash = "12202d5ba1fe72d0052d850357baaaa346c6b9b3c042d6a4dd96e389ba30dae4d0ea",
.hash = "1220a6a97a5b5e866f8bfe7ddbbcfc4e9c0928f1ea101b3d1e5c4eb964bd28f1f235",
},
.glfw = .{
.url = "https://github.com/tiawl/glfw.zig/archive/refs/tags/3.4.tar.gz",
.hash = "1220852152a03963090bd03203a1dea76c5e1eed3d3e6c8bf9787e62d16ac54ed715",
.hash = "1220cae93f3e4476db397a438fbfb3aadce9da67feb6bb0e6dd87c75d861fea9257d",
},
.shaderc = .{
.url = "git+https://github.com/tiawl/shaderc.zig#43be56d5c2f7958f2790ddad74b32c7de25f615c",
.hash = "1220b01d6cb3db77ada6029d92b49cc47111cde29c8109545f31adcd978f7ef2eee3",
},
.@"zig-datetime" = .{
.url = "https://github.com/frmdstryr/zig-datetime/archive/a9c75242dfc119a305c1381c2352d2fc16b2e0aa.tar.gz",
Expand Down
168 changes: 0 additions & 168 deletions build/shaders.zig

This file was deleted.

Loading

0 comments on commit b684645

Please sign in to comment.