From 31da379b4060fd726dd55bdabe8f44012533df4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Gasi=C5=84ski?= Date: Sun, 6 Oct 2024 18:41:22 +0200 Subject: [PATCH] Update the way shaders are compiled --- build.zig | 31 ++++++++++++++++++------------- build.zig.zon | 12 ++++++------ src/vulkan_renderer.zig | 23 ++++++++++++++--------- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/build.zig b/build.zig index 612b8ca..6462702 100644 --- a/build.zig +++ b/build.zig @@ -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 }); +} diff --git a/build.zig.zon b/build.zig.zon index bd0d9d0..137b837 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -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 = .{ diff --git a/src/vulkan_renderer.zig b/src/vulkan_renderer.zig index 14925d5..e3a37ad 100644 --- a/src/vulkan_renderer.zig +++ b/src/vulkan_renderer.zig @@ -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);