Skip to content

Commit 1c57bd3

Browse files
committed
update for modern vulkan and zig
1 parent 04c344e commit 1c57bd3

File tree

13 files changed

+1721
-9179
lines changed

13 files changed

+1721
-9179
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
zig-cache/
2-
shaders/frag.spv
3-
shaders/vert.spv
1+
/.zig-cache/
2+
/zig-out/

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
# zig-vulkan-triangle
22

3-
Live coding every Thursday at 17:00 EST.
4-
https://www.twitch.tv/andrewrok
3+
Example of using [vulkan-zig](https://github.com/Snektron/vulkan-zig) and
4+
[shader_compiler](https://github.com/Games-by-Mason/shader_compiler) along with
5+
libxcb to open a window and draw a triangle.
56

67
![](https://i.imgur.com/pHEHvMU.png)
78

8-
## Building
9+
## Building and Running
910

1011
```
1112
zig build run
1213
```
14+
15+
## System Configuration
16+
17+
On NixOS, I had to add these to my shell:
18+
19+
```
20+
buildInputs = [
21+
vulkan-loader
22+
vulkan-validation-layers
23+
xorg.libxcb
24+
];
25+
26+
VK_LAYER_PATH="${pkgs.vulkan-validation-layers}/share/vulkan/explicit_layer.d";
27+
```

build.zig

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,78 @@
11
const std = @import("std");
2-
const path = std.fs.path;
3-
const Builder = std.build.Builder;
4-
5-
pub fn build(b: *Builder) !void {
6-
const mode = b.standardReleaseOptions();
7-
const exe = b.addExecutable("zig-vulkan-triangle", "src/main.zig");
8-
exe.setBuildMode(mode);
9-
exe.linkSystemLibrary("glfw");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const registry = b.dependency("vulkan_headers", .{}).path("registry/vk.xml");
8+
const shader_compiler = b.dependency("shader_compiler", .{
9+
.target = b.host,
10+
.optimize = .ReleaseFast,
11+
}).artifact("shader_compiler");
12+
13+
const exe = b.addExecutable(.{
14+
.name = "vulkan-triangle-example",
15+
.root_source_file = b.path("src/main.zig"),
16+
.target = target,
17+
.optimize = optimize,
18+
.link_libc = true,
19+
});
1020
exe.linkSystemLibrary("vulkan");
11-
exe.linkSystemLibrary("c");
21+
exe.linkSystemLibrary("xcb");
22+
b.installArtifact(exe);
23+
24+
const vk_gen = b.dependency("vulkan", .{}).artifact("vulkan-zig-generator");
25+
const vk_generate_cmd = b.addRunArtifact(vk_gen);
26+
vk_generate_cmd.addFileArg(registry);
1227

13-
b.default_step.dependOn(&exe.step);
14-
exe.install();
28+
exe.root_module.addAnonymousImport("vulkan", .{
29+
.root_source_file = vk_generate_cmd.addOutputFileArg("vk.zig"),
30+
});
31+
32+
exe.root_module.addAnonymousImport("shaders.triangle.vert", .{
33+
.root_source_file = compileShader(b, optimize, shader_compiler, b.path("shaders/triangle.vert"), "triangle.vert.spv"),
34+
});
35+
exe.root_module.addAnonymousImport("shaders.triangle.frag", .{
36+
.root_source_file = compileShader(b, optimize, shader_compiler, b.path("shaders/triangle.frag"), "triangle.frag.spv"),
37+
});
38+
39+
const run_cmd = b.addRunArtifact(exe);
40+
run_cmd.step.dependOn(b.getInstallStep());
41+
if (b.args) |args| {
42+
run_cmd.addArgs(args);
43+
}
1544

1645
const run_step = b.step("run", "Run the app");
17-
const run_cmd = exe.run();
1846
run_step.dependOn(&run_cmd.step);
19-
20-
try addShader(b, exe, "shader.vert", "vert.spv");
21-
try addShader(b, exe, "shader.frag", "frag.spv");
2247
}
2348

24-
fn addShader(b: *Builder, exe: anytype, in_file: []const u8, out_file: []const u8) !void {
25-
// example:
26-
// glslc -o shaders/vert.spv shaders/shader.vert
27-
const dirname = "shaders";
28-
const full_in = try path.join(b.allocator, &[_][]const u8{ dirname, in_file });
29-
const full_out = try path.join(b.allocator, &[_][]const u8{ dirname, out_file });
30-
31-
const run_cmd = b.addSystemCommand(&[_][]const u8{
32-
"glslc",
33-
"-o",
34-
full_out,
35-
full_in,
49+
fn compileShader(
50+
b: *std.Build,
51+
optimize: std.builtin.OptimizeMode,
52+
shader_compiler: *std.Build.Step.Compile,
53+
src: std.Build.LazyPath,
54+
out_basename: []const u8,
55+
) std.Build.LazyPath {
56+
const compile_shader = b.addRunArtifact(shader_compiler);
57+
compile_shader.addArgs(&.{
58+
"--target", "Vulkan-1.3",
3659
});
37-
exe.step.dependOn(&run_cmd.step);
60+
switch (optimize) {
61+
.Debug => compile_shader.addArgs(&.{
62+
"--robust-access",
63+
}),
64+
.ReleaseSafe => compile_shader.addArgs(&.{
65+
"--optimize-perf",
66+
"--robust-access",
67+
}),
68+
.ReleaseFast => compile_shader.addArgs(&.{
69+
"--optimize-perf",
70+
}),
71+
.ReleaseSmall => compile_shader.addArgs(&.{
72+
"--optimize-perf",
73+
"--optimize-small",
74+
}),
75+
}
76+
compile_shader.addFileArg(src);
77+
return compile_shader.addOutputFileArg(out_basename);
3878
}

build.zig.zon

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.{
2+
.name = "vulkan-triangle-example",
3+
.version = "0.0.0",
4+
.minimum_zig_version = "0.14.0-dev.1359+e9a00ba7f",
5+
6+
.dependencies = .{
7+
.vulkan = .{
8+
.url = "git+https://github.com/andrewrk/vulkan-zig.git#b6e589d62bc1d9070c47d73ce942168fb92624c8",
9+
.hash = "12201e484e173e70634e664864763223427703e677f28c63ebec9332513c8ca5121c",
10+
},
11+
.vulkan_headers = .{
12+
.url = "https://github.com/KhronosGroup/Vulkan-Headers/archive/v1.3.283.tar.gz",
13+
.hash = "1220a7e73d72a0d56bc2a65f9d8999a7c019e42260a0744c408d1cded111bc205e10",
14+
},
15+
.shader_compiler = .{
16+
.url = "git+https://github.com/andrewrk/shader_compiler.git#89550bb2cbe85276bd1fd89d020f3bbd686937c3",
17+
.hash = "12207b775b98ac87f583d5ca95386537432c22f4bc83bc0c7fd9d6cbfdf265cc0444",
18+
},
19+
},
20+
21+
.paths = .{
22+
"build.zig",
23+
"build.zig.zon",
24+
"src",
25+
"LICENSE",
26+
"README.md",
27+
},
28+
}

shaders/shader.frag

Lines changed: 0 additions & 10 deletions
This file was deleted.

shaders/shader.vert

Lines changed: 0 additions & 27 deletions
This file was deleted.

shaders/triangle.frag

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#version 450
2+
3+
layout(location = 0) in vec3 v_color;
4+
5+
layout(location = 0) out vec4 f_color;
6+
7+
void main() {
8+
f_color = vec4(v_color, 1.0);
9+
}

shaders/triangle.vert

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 450
2+
3+
layout(location = 0) in vec2 a_pos;
4+
layout(location = 1) in vec3 a_color;
5+
6+
layout(location = 0) out vec3 v_color;
7+
8+
void main() {
9+
gl_Position = vec4(a_pos, 0.0, 1.0);
10+
v_color = a_color;
11+
}

0 commit comments

Comments
 (0)