Skip to content

Commit 6034a56

Browse files
authored
Merge pull request #40 from carrot-sticks/main
2 parents 3a3f0d2 + 37c6268 commit 6034a56

File tree

3 files changed

+64
-25
lines changed

3 files changed

+64
-25
lines changed

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn main() !void {
2929
const window = try glfw.createWindow(600, 600, "zig-gamedev: minimal_glfw_gl", null);
3030
defer glfw.destroyWindow(window);
3131
32-
// or, using the equivilent, encapsulated, "objecty" API:
32+
// or, using the equivalent, encapsulated, "objecty" API:
3333
const window = try glfw.Window.create(600, 600, "zig-gamedev: minimal_glfw_gl", null);
3434
defer window.destroy();
3535
@@ -39,10 +39,44 @@ pub fn main() !void {
3939
glfw.pollEvents();
4040
4141
// render your things here
42-
42+
4343
window.swapBuffers();
4444
}
4545
}
4646
```
4747

4848
See [zig-gamedev samples](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples) for more complete usage examples.
49+
50+
51+
## Usage with Vulkan
52+
53+
To match types from `zglfw` functions and Vulkan library `import_vulkan` option may be used. When using this option `vulkan` import must be provided to the root module.
54+
55+
Example `build.zig` with [`vulkan-zig`](https://github.com/Snektron/vulkan-zig):
56+
57+
```zig
58+
const vulkan_headers = b.dependency("vulkan_headers");
59+
const vulkan = b.dependency("vulkan_zig", .{
60+
.registry = vulkan_headers.path("registry/vk.xml"),
61+
}).module("vulkan-zig");
62+
63+
const zglfw = b.dependency("zglfw", .{ .import_vulkan = true });
64+
65+
const zglfw_mod = zglfw.module("root");
66+
zglfw_mod.addImport("vulkan", vulkan);
67+
68+
const exe = b.addExecutable(.{
69+
.name = "vk_setup",
70+
.root_module = b.createModule(.{
71+
.root_source_file = b.path("src/main.zig"),
72+
.target = target,
73+
.optimize = optimize,
74+
.imports = &.{
75+
.{ .name = "zglfw", .module = zglfw_mod },
76+
.{ .name = "vulkan", .module = vulkan },
77+
},
78+
}),
79+
});
80+
81+
exe.root_module.linkLibrary(zglfw.artifact("glfw"));
82+
```

build.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ pub fn build(b: *std.Build) void {
2020
"wayland",
2121
"Whether to build with Wayland support (default: true)",
2222
) orelse true,
23+
.enable_vulkan_import = b.option(
24+
bool,
25+
"import_vulkan",
26+
"Whether to build with external Vulkan dependency (default: false)",
27+
) orelse false,
2328
};
2429

2530
const options_step = b.addOptions();

src/zglfw.zig

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,40 +91,40 @@ pub fn getRequiredInstanceExtensions() Error![][*:0]const u8 {
9191
extern fn glfwGetRequiredInstanceExtensions(count: *u32) ?*?[*:0]const u8;
9292

9393
pub const VulkanFn = *const fn () callconv(.c) void;
94-
pub const VkInstance = ?*const anyopaque;
95-
pub const VkPhysicalDevice = ?*const anyopaque;
96-
pub const VkAllocationCallbacks = anyopaque;
97-
pub const VkSurfaceKHR = anyopaque;
98-
99-
pub fn getInstanceProcAddress(instance: VkInstance, procname: [*:0]const u8) Error!VulkanFn {
100-
if (glfwGetInstanceProcAddress(instance, procname)) |address| {
101-
return address;
102-
}
103-
try maybeError();
104-
return error.APIUnavailable;
105-
}
106-
extern fn glfwGetInstanceProcAddress(instance: VkInstance, procname: [*:0]const u8) ?VulkanFn;
94+
95+
const vk = if (options.enable_vulkan_import)
96+
@import("vulkan")
97+
else
98+
struct {
99+
pub const Instance = ?*const anyopaque;
100+
pub const PhysicalDevice = ?*const anyopaque;
101+
pub const AllocationCallbacks = anyopaque;
102+
pub const SurfaceKHR = anyopaque;
103+
};
104+
105+
pub const getInstanceProcAddress = glfwGetInstanceProcAddress;
106+
extern fn glfwGetInstanceProcAddress(instance: vk.Instance, procname: [*:0]const u8) ?VulkanFn;
107107

108108
pub fn getPhysicalDevicePresentationSupport(
109-
instance: VkInstance,
110-
device: VkPhysicalDevice,
109+
instance: vk.Instance,
110+
device: vk.PhysicalDevice,
111111
queuefamily: u32,
112112
) Error!bool {
113113
const result = glfwGetPhysicalDevicePresentationSupport(instance, device, queuefamily) == TRUE;
114114
try maybeError();
115115
return result;
116116
}
117117
extern fn glfwGetPhysicalDevicePresentationSupport(
118-
instance: VkInstance,
119-
device: VkPhysicalDevice,
118+
instance: vk.Instance,
119+
device: vk.PhysicalDevice,
120120
queuefamily: u32,
121121
) Bool;
122122

123123
pub fn createWindowSurface(
124-
instance: VkInstance,
124+
instance: vk.Instance,
125125
window: *Window,
126-
allocator: ?*const VkAllocationCallbacks,
127-
surface: *VkSurfaceKHR,
126+
allocator: ?*const vk.AllocationCallbacks,
127+
surface: *vk.SurfaceKHR,
128128
) Error!void {
129129
if (glfwCreateWindowSurface(instance, window, allocator, surface) == 0) {
130130
return;
@@ -133,10 +133,10 @@ pub fn createWindowSurface(
133133
return Error.APIUnavailable;
134134
}
135135
extern fn glfwCreateWindowSurface(
136-
instance: VkInstance,
136+
instance: vk.Instance,
137137
window: *Window,
138-
allocator: ?*const VkAllocationCallbacks,
139-
surface: *VkSurfaceKHR,
138+
allocator: ?*const vk.AllocationCallbacks,
139+
surface: *vk.SurfaceKHR,
140140
) c_int;
141141

142142
/// `pub fn getTime() f64`

0 commit comments

Comments
 (0)