Skip to content

Commit 8713429

Browse files
committed
change from monorepo, zwin32->zwindows
1 parent 4668907 commit 8713429

26 files changed

+4144
-453
lines changed

README.md

Lines changed: 137 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,167 @@
1-
# zwin32
1+
# zwindows
22

3-
Zig bindings for Win32 API
3+
Windows development SDK for Zig game developers.
44

5-
Can be used on Windows and Linux. Contains partial bindings for:
6-
* Win32 API
7-
* Direct3D 12
8-
* Direct3D 11
9-
* DXGI
10-
* DirectML
11-
* Direct2D
12-
* XAudio2
13-
* Wincodec (WIC)
14-
* WASAPI
15-
* Media Foundation
16-
* DirectWrite
5+
- Vendored DirectX Compiler binaries for Windows and Linux
6+
- Vendored DirectX and DirectML runtime libraries
7+
- Lightweight partial bindings for:
8+
* Win32 API (extends std.os.windows)
9+
* Direct3D 12
10+
* Direct3D 11
11+
* DXGI
12+
* DirectML
13+
* Direct2D
14+
* XAudio2
15+
* Wincodec (WIC)
16+
* WASAPI
17+
* Media Foundation
18+
* DirectWrite
19+
- Optional D3D12 helper library (zd3d12)
20+
- Optional XAudio2 helper library (zxaudio2)
1721

18-
## Getting started
22+
## Using the Zig package
1923

20-
Add `zwin32` your build.zig.zon dependencies and in your `build.zig` add:
24+
Copy `zwindows` to a subdirectory of your project and add the following to your `build.zig.zon` .dependencies:
25+
```zig
26+
.zwindows = .{ .path = "libs/zwindows" },
27+
```
2128

29+
Example build.zig
2230
```zig
2331
pub fn build(b: *std.Build) !void {
24-
const exe = b.addExecutable(.{ ... });
2532
26-
const zwin32 = b.dependency("zwin32", .{});
27-
const zwin32_path = zwin32.path("").getPath(b);
28-
29-
exe.root_module.addImport("zwin32", zwin32.module("root"));
33+
...
34+
35+
const zwindows_dependency = b.dependency("zwindows", .{
36+
.zxaudio2_debug_layer = (builtin.mode == .Debug),
37+
.zd3d12_debug_layer = (builtin.mode == .Debug),
38+
.zd3d12_gbv = b.option("zd3d12_gbv", "Enable GPU-Based Validation") orelse false,
39+
});
3040
31-
try @import("zwin32").install_xaudio2(&tests.step, .bin, zwin32_path);
41+
// Import the Windows API bindings
42+
exe.root_module.addImport("zwindows", zwindows_dependency.module("zwindows"));
3243
33-
try @import("zwin32").install_d3d12(&tests.step, .bin, zwin32_path);
44+
// Import the optional zd3d12 helper library
45+
exe.root_module.addImport("zd3d12", zwindows_dependency.module("zd3d12"));
3446
35-
try @import("zwin32").install_directml(&tests.step, .bin, zwin32_path);
47+
// Import the optional zxaudio2 helper library
48+
exe.root_module.addImport("zxaudio2", zwindows_dependency.module("zxaudio2"));
49+
50+
// Install vendored binaries
51+
const zwindows = @import("zwindows");
52+
try zwindows.install_xaudio2(&exe.step, .bin);
53+
try zwindows.install_d3d12(&exe.step, .bin);
54+
try zwindows.install_directml(&exe.step, .bin);
3655
}
3756
```
3857

39-
Now in your code you may import and use `zwin32`:
40-
58+
### Bindings Usage Example
4159
```zig
42-
const zwin32 = @import("zwin32");
43-
const w32 = zwin32.w32;
44-
const dwrite = zwin32.dwrite;
45-
const dxgi = zwin32.dxgi;
46-
const d3d12 = zwin32.d3d12;
47-
const d3d12d = zwin32.d3d12d;
48-
const dml = zwin32.directml;
60+
const zwindows = @import("zwindows");
61+
const windows = zwindows.windows;
62+
const dwrite = zwindows.dwrite;
63+
const dxgi = zwindows.dxgi;
64+
const d3d12 = zwindows.d3d12;
65+
const d3d12d = zwindows.d3d12d;
66+
const dml = zwindows.directml;
67+
// etc
4968
5069
pub fn main() !void {
5170
...
52-
const winclass = w32.WNDCLASSEXA{
71+
const winclass = windows.WNDCLASSEXA{
5372
.style = 0,
5473
.lpfnWndProc = processWindowMessage,
5574
.cbClsExtra = 0,
5675
.cbWndExtra = 0,
57-
.hInstance = @ptrCast(w32.HINSTANCE, w32.GetModuleHandleA(null)),
76+
.hInstance = @ptrCast(windows.HINSTANCE, windows.GetModuleHandleA(null)),
5877
.hIcon = null,
59-
.hCursor = w32.LoadCursorA(null, @intToPtr(w32.LPCSTR, 32512)),
78+
.hCursor = windows.LoadCursorA(null, @intToPtr(windows.LPCSTR, 32512)),
6079
.hbrBackground = null,
6180
.lpszMenuName = null,
6281
.lpszClassName = name,
6382
.hIconSm = null,
6483
};
65-
_ = w32.RegisterClassExA(&winclass);
84+
_ = windows.RegisterClassExA(&winclass);
85+
}
86+
```
87+
88+
## zd3d12
89+
zd3d12 is an optional helper library for Direct3d 12 build ontop of the zwindows bindings
90+
91+
### Features
92+
- Basic DirectX 12 context management (descriptor heaps, memory heaps, swapchain, CPU and GPU sync, etc.)
93+
- Basic DirectX 12 resource management (handle-based resources and pipelines)
94+
- Basic resource barriers management with simple state-tracking
95+
- Transient and persistent descriptor allocation
96+
- Fast image loading using WIC (Windows Imaging Component)
97+
- Helpers for uploading data to the GPU
98+
- Fast mipmap generator running on the GPU
99+
- Interop with Direct2D and DirectWrite for high-quality vector graphics and text rendering (optional)
100+
101+
### Example applications
102+
- https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/triangle
103+
- https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/textured_quad
104+
- https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/vector_graphics_test
105+
- https://github.com/zig-gamedev/zig-gamedev/blob/main/samples/rasterization
106+
- https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/bindless
107+
- https://github.com/zig-gamedev/zig-gamedev/blob/main/samples/mesh_shader_test
108+
- https://github.com/zig-gamedev/zig-gamedev/blob/main/samples/directml_convolution_test
109+
110+
### Usage Example
111+
```zig
112+
const zd3d12 = @import("zd3d12");
113+
114+
// We need to export below symbols for DirectX 12 Agility SDK.
115+
pub export const D3D12SDKVersion: u32 = 610;
116+
pub export const D3D12SDKPath: [*:0]const u8 = ".\\d3d12\\";
117+
118+
pub fn main() !void {
119+
...
120+
var gctx = zd3d12.GraphicsContext.init(.{
121+
.allocator = allocator,
122+
.window = win32_window,
123+
});
124+
defer gctx.deinit(allocator);
125+
126+
while (...) {
127+
gctx.beginFrame();
128+
129+
const back_buffer = gctx.getBackBuffer();
130+
gctx.addTransitionBarrier(back_buffer.resource_handle, .{ .RENDER_TARGET = true });
131+
gctx.flushResourceBarriers();
132+
133+
gctx.cmdlist.OMSetRenderTargets(
134+
1,
135+
&.{back_buffer.descriptor_handle},
136+
TRUE,
137+
null,
138+
);
139+
gctx.cmdlist.ClearRenderTargetView(back_buffer.descriptor_handle, &.{ 0.2, 0.4, 0.8, 1.0 }, 0, null);
140+
141+
gctx.addTransitionBarrier(back_buffer.resource_handle, d3d12.RESOURCE_STATES.PRESENT);
142+
gctx.flushResourceBarriers();
143+
144+
gctx.endFrame();
145+
}
146+
}
147+
```
148+
149+
## zxaudio2
150+
zxaudio2 is an optional helper library for XAudio2 build ontop of the zwindows bindings
151+
152+
### Usage Example
153+
```zig
154+
const zxaudio2 = @import("zxaudio2");
155+
156+
pub fn main() !void {
157+
...
158+
var actx = zxaudio2.AudioContext.init(allocator);
159+
160+
const sound_handle = actx.loadSound("content/drum_bass_hard.flac");
161+
actx.playSound(sound_handle, .{});
162+
163+
var music = zxaudio2.Stream.create(allocator, actx.device, "content/Broke For Free - Night Owl.mp3");
164+
hrPanicOnFail(music.voice.Start(0, xaudio2.COMMIT_NOW));
165+
...
66166
}
67167
```

build.zig

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,49 @@ const std = @import("std");
22
const builtin = @import("builtin");
33

44
pub fn build(b: *std.Build) !void {
5-
_ = b.addModule("root", .{
6-
.root_source_file = b.path("src/zwin32.zig"),
5+
const options = .{
6+
.zxaudio2_debug_layer = b.option(
7+
bool,
8+
"zxaudio2_debug_layer",
9+
"Enable XAudio2 debug layer",
10+
) orelse false,
11+
.zd3d12_debug_layer = b.option(
12+
bool,
13+
"zd3d12_debug_layer",
14+
"Enable DirectX 12 debug layer",
15+
) orelse false,
16+
.zd3d12_gbv = b.option(
17+
bool,
18+
"zd3d12_gbv",
19+
"Enable DirectX 12 GPU-Based Validation (GBV)",
20+
) orelse false,
21+
};
22+
23+
const options_step = b.addOptions();
24+
inline for (std.meta.fields(@TypeOf(options))) |field| {
25+
options_step.addOption(field.type, field.name, @field(options, field.name));
26+
}
27+
28+
const zwindows_module = b.addModule("zwindows", .{
29+
.root_source_file = b.path("src/zwindows.zig"),
30+
});
31+
32+
const options_module = options_step.createModule();
33+
34+
_ = b.addModule("zd3d12", .{
35+
.root_source_file = b.path("src/zd3d12.zig"),
36+
.imports = &.{
37+
.{ .name = "options", .module = options_module },
38+
.{ .name = "zwindows", .module = zwindows_module },
39+
},
40+
});
41+
42+
_ = b.addModule("zxaudio2", .{
43+
.root_source_file = b.path("src/zxaudio2.zig"),
44+
.imports = &.{
45+
.{ .name = "options", .module = options_module },
46+
.{ .name = "zwindows", .module = zwindows_module },
47+
},
748
});
849
}
950

@@ -12,13 +53,13 @@ pub fn install_xaudio2(
1253
install_dir: std.Build.InstallDir,
1354
) void {
1455
const b = step.owner;
56+
const source_path_prefix = comptime std.fs.path.dirname(@src().file) orelse ".";
1557
step.dependOn(
1658
&b.addInstallFileWithDir(
1759
.{
18-
.dependency = .{
19-
.dependency = b.dependency("zwin32", .{}),
20-
.sub_path = "bin/x64/xaudio2_9redist.dll",
21-
},
60+
.cwd_relative = b.pathJoin(
61+
&.{ source_path_prefix, "bin/x64/xaudio2_9redist.dll" },
62+
),
2263
},
2364
install_dir,
2465
"xaudio2_9redist.dll",
@@ -31,14 +72,13 @@ pub fn install_d3d12(
3172
install_dir: std.Build.InstallDir,
3273
) void {
3374
const b = step.owner;
34-
const zwin32 = b.dependency("zwin32", .{});
75+
const source_path_prefix = comptime std.fs.path.dirname(@src().file) orelse ".";
3576
step.dependOn(
3677
&b.addInstallFileWithDir(
3778
.{
38-
.dependency = .{
39-
.dependency = zwin32,
40-
.sub_path = "bin/x64/D3D12Core.dll",
41-
},
79+
.cwd_relative = b.pathJoin(
80+
&.{ source_path_prefix, "bin/x64/D3D12Core.dll" },
81+
),
4282
},
4383
install_dir,
4484
"d3d12/D3D12Core.dll",
@@ -47,10 +87,9 @@ pub fn install_d3d12(
4787
step.dependOn(
4888
&b.addInstallFileWithDir(
4989
.{
50-
.dependency = .{
51-
.dependency = zwin32,
52-
.sub_path = "bin/x64/D3D12SDKLayers.dll",
53-
},
90+
.cwd_relative = b.pathJoin(
91+
&.{ source_path_prefix, "bin/x64/D3D12SDKLayers.dll" },
92+
),
5493
},
5594
install_dir,
5695
"d3d12/D3D12SDKLayers.dll",
@@ -63,13 +102,13 @@ pub fn install_directml(
63102
install_dir: std.Build.InstallDir,
64103
) void {
65104
const b = step.owner;
105+
const source_path_prefix = comptime std.fs.path.dirname(@src().file) orelse ".";
66106
step.dependOn(
67107
&b.addInstallFileWithDir(
68108
.{
69-
.dependency = .{
70-
.dependency = b.dependency("zwin32", .{}),
71-
.sub_path = "bin/x64/DirectML.dll",
72-
},
109+
.cwd_relative = b.pathJoin(
110+
&.{ source_path_prefix, "bin/x64/DirectML.dll" },
111+
),
73112
},
74113
install_dir,
75114
"DirectML.dll",
@@ -78,10 +117,9 @@ pub fn install_directml(
78117
step.dependOn(
79118
&b.addInstallFileWithDir(
80119
.{
81-
.dependency = .{
82-
.dependency = b.dependency("zwin32", .{}),
83-
.sub_path = "bin/x64/DirectML.Debug.dll",
84-
},
120+
.cwd_relative = b.pathJoin(
121+
&.{ source_path_prefix, "bin/x64/DirectML.Debug.dll" },
122+
),
85123
},
86124
install_dir,
87125
"DirectML.Debug.dll",
@@ -166,11 +204,10 @@ pub const CompileShaders = struct {
166204
) void {
167205
const b = self.step.owner;
168206

169-
const zwin32 = b.dependency("zwin32", .{});
170-
207+
const zwindows_path = comptime std.fs.path.dirname(@src().file) orelse ".";
171208
const dxc_path = switch (builtin.target.os.tag) {
172-
.windows => zwin32.path("bin/x64/dxc.exe").getPath(b),
173-
.linux => zwin32.path("bin/x64/dxc").getPath(b),
209+
.windows => zwindows_path ++ "/bin/x64/dxc.exe",
210+
.linux => zwindows_path ++ "/bin/x64/dxc",
174211
else => @panic("Unsupported target"),
175212
};
176213

@@ -188,7 +225,10 @@ pub const CompileShaders = struct {
188225

189226
const cmd_step = b.addSystemCommand(&dxc_command);
190227
if (builtin.target.os.tag == .linux) {
191-
cmd_step.setEnvironmentVariable("LD_LIBRARY_PATH", zwin32.path("bin/x64").getPath(b));
228+
cmd_step.setEnvironmentVariable(
229+
"LD_LIBRARY_PATH",
230+
zwindows_path ++ "/bin/x64",
231+
);
192232
}
193233
self.step.dependOn(&cmd_step.step);
194234
}

build.zig.zon

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.{
2-
.name = "zwin32",
3-
.version = "0.10.0",
4-
.minimum_zig_version = "0.13.0-dev.351+64ef45eb0",
2+
.name = "zwindows",
3+
.version = "0.1.0",
54
.paths = .{
65
"build.zig",
76
"build.zig.zon",

0 commit comments

Comments
 (0)