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, .link_libc = true,
}); });
// exe.addIncludePath(b.path("include/"));
// --- Dependencies --- // --- Dependencies ---
// Vulkan // Vulkan
@ -26,17 +24,11 @@ pub fn build(b: *std.Build) void {
const vkzig_bindings = vkzig_dep.module("vulkan-zig"); const vkzig_bindings = vkzig_dep.module("vulkan-zig");
exe.root_module.addImport("vulkan", vkzig_bindings); exe.root_module.addImport("vulkan", vkzig_bindings);
const shader_comp = vkgen.ShaderCompileStep.create( // Shaders
b, compileShader(b, exe, "shader_frag", "shader.frag");
.{ .real_path = "glslc" }, compileShader(b, exe, "shader_vert", "shader.vert");
&[_][]const u8{"--target-env=vulkan1.3"}, compileShader(b, exe, "second_frag", "second.frag");
"-o", compileShader(b, exe, "second_vert", "second.vert");
);
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());
// SDL2 // SDL2
const sdl_sdk = sdl.init(b, .{}); 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"); const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step); 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" }, .zstbi = .{ .path = "libs/zstbi" },
.sdl = .{ .path = "libs/sdl" }, .sdl = .{ .path = "libs/sdl" },
.vulkan = .{ .vulkan = .{
.url = "https://github.com/Snektron/vulkan-zig/archive/f7b21d034f527765f62935de1b62855033621989.tar.gz", .url = "https://github.com/Snektron/vulkan-zig/archive/06dae6c9201863837a92064e2e7814aa71064067.tar.gz",
.hash = "12201e484e173e70634e664864763223427703e677f28c63ebec9332513c8ca5121c", .hash = "1220edeb3fc7dfc40e6fde705a108edce0a3cc76d165a7c9919d1fb037eccec43372",
},
.obj = .{
.url = "https://github.com/chip2n/zig-obj/archive/58f524ed6834790b29ac1e97b2f9e6b7de7b5346.tar.gz",
.hash = "1220ff46dcbeb40677c0ce8560b954885beec8b699835d9e6686beab72aa9d422c79",
}, },
// .obj = .{
// .url = "https://github.com/chip2n/zig-obj/archive/58f524ed6834790b29ac1e97b2f9e6b7de7b5346.tar.gz",
// .hash = "1220ff46dcbeb40677c0ce8560b954885beec8b699835d9e6686beab72aa9d422c79",
// },
}, },
.paths = .{ .paths = .{

View file

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