Update the way shaders are compiled

This commit is contained in:
Przemyslaw Gasinski 2024-10-06 18:41:22 +02:00
parent d318dfa4a2
commit 31da379b40
3 changed files with 38 additions and 28 deletions

View file

@ -15,8 +15,6 @@ pub fn build(b: *std.Build) void {
.link_libc = true,
});
// exe.addIncludePath(b.path("include/"));
// --- Dependencies ---
// Vulkan
@ -26,17 +24,11 @@ pub fn build(b: *std.Build) void {
const vkzig_bindings = vkzig_dep.module("vulkan-zig");
exe.root_module.addImport("vulkan", vkzig_bindings);
const shader_comp = vkgen.ShaderCompileStep.create(
b,
.{ .real_path = "glslc" },
&[_][]const u8{"--target-env=vulkan1.3"},
"-o",
);
shader_comp.add("shader_frag", "src/shaders/shader.frag", .{});
shader_comp.add("shader_vert", "src/shaders/shader.vert", .{});
shader_comp.add("second_frag", "src/shaders/second.frag", .{});
shader_comp.add("second_vert", "src/shaders/second.vert", .{});
exe.root_module.addImport("shaders", shader_comp.getModule());
// Shaders
compileShader(b, exe, "shader_frag", "shader.frag");
compileShader(b, exe, "shader_vert", "shader.vert");
compileShader(b, exe, "second_frag", "second.frag");
compileShader(b, exe, "second_vert", "second.vert");
// SDL2
const sdl_sdk = sdl.init(b, .{});
@ -91,3 +83,16 @@ pub fn build(b: *std.Build) void {
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
}
fn compileShader(
b: *std.Build,
exe: *std.Build.Step.Compile,
comptime name: []const u8,
comptime file_name: []const u8,
) void {
const cmd = b.addSystemCommand(&.{ "glslc", "--target-env=vulkan1.3", "-o" });
const spv = cmd.addOutputFileArg(name ++ ".spv");
cmd.addFileArg(b.path("src/shaders/" ++ file_name));
exe.root_module.addAnonymousImport(name, .{ .root_source_file = spv });
}

View file

@ -8,13 +8,13 @@
.zstbi = .{ .path = "libs/zstbi" },
.sdl = .{ .path = "libs/sdl" },
.vulkan = .{
.url = "https://github.com/Snektron/vulkan-zig/archive/f7b21d034f527765f62935de1b62855033621989.tar.gz",
.hash = "12201e484e173e70634e664864763223427703e677f28c63ebec9332513c8ca5121c",
},
.obj = .{
.url = "https://github.com/chip2n/zig-obj/archive/58f524ed6834790b29ac1e97b2f9e6b7de7b5346.tar.gz",
.hash = "1220ff46dcbeb40677c0ce8560b954885beec8b699835d9e6686beab72aa9d422c79",
.url = "https://github.com/Snektron/vulkan-zig/archive/06dae6c9201863837a92064e2e7814aa71064067.tar.gz",
.hash = "1220edeb3fc7dfc40e6fde705a108edce0a3cc76d165a7c9919d1fb037eccec43372",
},
// .obj = .{
// .url = "https://github.com/chip2n/zig-obj/archive/58f524ed6834790b29ac1e97b2f9e6b7de7b5346.tar.gz",
// .hash = "1220ff46dcbeb40677c0ce8560b954885beec8b699835d9e6686beab72aa9d422c79",
// },
},
.paths = .{

View file

@ -2,7 +2,6 @@ const std = @import("std");
const sdl = @import("sdl");
const vk = @import("vulkan");
const builtin = @import("builtin");
const shaders = @import("shaders");
const zm = @import("zmath");
const img = @import("zstbi");
const ai = @import("assimp.zig").c;
@ -620,16 +619,19 @@ pub const VulkanRenderer = struct {
}
fn createGraphicsPipeline(self: *Self) !void {
const v_shader align(@alignOf(u32)) = @embedFile("shader_vert").*;
const f_shader align(@alignOf(u32)) = @embedFile("shader_frag").*;
// Create shader modules
const vert = try self.ctx.device.createShaderModule(&.{
.code_size = shaders.shader_vert.len,
.p_code = @ptrCast(&shaders.shader_vert),
.code_size = v_shader.len,
.p_code = @ptrCast(&v_shader),
}, null);
defer self.ctx.device.destroyShaderModule(vert, null);
const frag = try self.ctx.device.createShaderModule(&.{
.code_size = shaders.shader_frag.len,
.p_code = @ptrCast(&shaders.shader_frag),
.code_size = f_shader.len,
.p_code = @ptrCast(&f_shader),
}, null);
defer self.ctx.device.destroyShaderModule(frag, null);
@ -837,15 +839,18 @@ pub const VulkanRenderer = struct {
// -- Create second pass pipeline
// Second pass shaders
const v_second align(@alignOf(u32)) = @embedFile("second_vert").*;
const f_second align(@alignOf(u32)) = @embedFile("second_frag").*;
const second_vert_shader_module = try self.ctx.device.createShaderModule(&.{
.code_size = shaders.second_vert.len,
.p_code = @ptrCast(&shaders.second_vert),
.code_size = v_second.len,
.p_code = @ptrCast(&v_second),
}, null);
defer self.ctx.device.destroyShaderModule(second_vert_shader_module, null);
const second_frag_shader_module = try self.ctx.device.createShaderModule(&.{
.code_size = shaders.second_frag.len,
.p_code = @ptrCast(&shaders.second_frag),
.code_size = f_second.len,
.p_code = @ptrCast(&f_second),
}, null);
defer self.ctx.device.destroyShaderModule(second_frag_shader_module, null);