Skip to content

Commit

Permalink
accept registry option as LazyPath
Browse files Browse the repository at this point in the history
This allows passing LazyPaths obtained from dependencies to
the vulkan-zig dependency, which streamlines using vulkan-zig
with Vulkan-Headers provided by the package manager.
  • Loading branch information
Snektron committed Dec 29, 2024
1 parent dd6e61d commit bb470f1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,35 @@ There is also support for adding this project as a dependency through zig packag
```
And then in your build.zig file, you'll need to add a line like this to your build function:
```zig
const vkzig_dep = b.dependency("vulkan_zig", .{
.registry = @as([]const u8, b.pathFromRoot("path/to/vk.xml")),
});
const vkzig_bindings = vkzig_dep.module("vulkan-zig");
exe.root_module.addImport("vulkan", vkzig_bindings);
const vulkan = b.dependency("vulkan_zig", .{
.registry = b.path("path/to/vk.xml"),
}).module("vulkan-zig");
exe.root_module.addImport("vulkan", vulkan);
```
That will allow you to `@import("vulkan")` in your executable's source.

#### Generating bindings directly from Vulkan-Headers

Bindings can be generated directly from the Vulkan-Headers repository by adding Vulkan-Headers as a dependency, and then passing the path to `vk.xml` from that dependency:
```zig
.{
// -- snip --
.dependencies = .{
// -- snip --
.vulkan_headers = .{
.url = "https://github.com/KhronosGroup/Vulkan-Headers/archive/v1.3.283.tar.gz",
.hash = "<dependency hash>",
},
},
}
```
```zig
const vulkan = b.dependency("vulkan_zig", .{
.registry = b.dependency("vulkan_headers", .{}).path("registry/vk.xml"),
}).module("vulkan-zig");
exe.root_module.addImport("vulkan", vulkan);
```

### Manual generation with the package manager from build.zig

Bindings can also be generated by invoking the generator directly. This may be useful is some special cases, for example, it integrates particularly well with fetching the registry via the package manager. This can be done by adding the Vulkan-Headers repository to your dependencies, and then passing the `vk.xml` inside it to vulkan-zig-generator:
Expand Down
4 changes: 2 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const maybe_registry: ?[]const u8 = b.option([]const u8, "registry", "Set the path to the Vulkan registry (vk.xml)");
const maybe_registry = b.option(std.Build.LazyPath, "registry", "Set the path to the Vulkan registry (vk.xml)");
const test_step = b.step("test", "Run all the tests");

// Using the package manager, this artifact can be obtained by the user
Expand All @@ -23,7 +23,7 @@ pub fn build(b: *std.Build) void {
if (maybe_registry) |registry| {
const vk_generate_cmd = b.addRunArtifact(generator_exe);

vk_generate_cmd.addArg(registry);
vk_generate_cmd.addFileArg(registry);

const vk_zig = vk_generate_cmd.addOutputFileArg("vk.zig");
const vk_zig_module = b.addModule("vulkan-zig", .{
Expand Down
18 changes: 8 additions & 10 deletions examples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ pub fn build(b: *std.Build) void {
b.installArtifact(triangle_exe);
triangle_exe.linkSystemLibrary("glfw");

const vk_gen = b.dependency("vulkan_zig", .{}).artifact("vulkan-zig-generator");
const vk_generate_cmd = b.addRunArtifact(vk_gen);
const registry_path: std.Build.LazyPath = if (maybe_override_registry) |override_registry|
.{ .cwd_relative = override_registry }
else
registry;

if (maybe_override_registry) |override_registry| {
vk_generate_cmd.addFileArg(.{ .cwd_relative = override_registry });
} else {
vk_generate_cmd.addFileArg(registry);
}
const vulkan = b.dependency("vulkan_zig", .{
.registry = registry_path,
}).module("vulkan-zig");

triangle_exe.root_module.addAnonymousImport("vulkan", .{
.root_source_file = vk_generate_cmd.addOutputFileArg("vk.zig"),
});
triangle_exe.root_module.addImport("vulkan", vulkan);

const vert_cmd = b.addSystemCommand(&.{
"glslc",
Expand Down

0 comments on commit bb470f1

Please sign in to comment.