|
1 |
| -# zwin32 |
| 1 | +# zwindows |
2 | 2 |
|
3 |
| -Zig bindings for Win32 API |
| 3 | +Windows development SDK for Zig game developers. |
4 | 4 |
|
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) |
17 | 21 |
|
18 |
| -## Getting started |
| 22 | +## Using the Zig package |
19 | 23 |
|
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 | +``` |
21 | 28 |
|
| 29 | +Example build.zig |
22 | 30 | ```zig
|
23 | 31 | pub fn build(b: *std.Build) !void {
|
24 |
| - const exe = b.addExecutable(.{ ... }); |
25 | 32 |
|
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 | + }); |
30 | 40 |
|
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")); |
32 | 43 |
|
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")); |
34 | 46 |
|
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); |
36 | 55 | }
|
37 | 56 | ```
|
38 | 57 |
|
39 |
| -Now in your code you may import and use `zwin32`: |
40 |
| - |
| 58 | +### Bindings Usage Example |
41 | 59 | ```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 |
49 | 68 |
|
50 | 69 | pub fn main() !void {
|
51 | 70 | ...
|
52 |
| - const winclass = w32.WNDCLASSEXA{ |
| 71 | + const winclass = windows.WNDCLASSEXA{ |
53 | 72 | .style = 0,
|
54 | 73 | .lpfnWndProc = processWindowMessage,
|
55 | 74 | .cbClsExtra = 0,
|
56 | 75 | .cbWndExtra = 0,
|
57 |
| - .hInstance = @ptrCast(w32.HINSTANCE, w32.GetModuleHandleA(null)), |
| 76 | + .hInstance = @ptrCast(windows.HINSTANCE, windows.GetModuleHandleA(null)), |
58 | 77 | .hIcon = null,
|
59 |
| - .hCursor = w32.LoadCursorA(null, @intToPtr(w32.LPCSTR, 32512)), |
| 78 | + .hCursor = windows.LoadCursorA(null, @intToPtr(windows.LPCSTR, 32512)), |
60 | 79 | .hbrBackground = null,
|
61 | 80 | .lpszMenuName = null,
|
62 | 81 | .lpszClassName = name,
|
63 | 82 | .hIconSm = null,
|
64 | 83 | };
|
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 | + ... |
66 | 166 | }
|
67 | 167 | ```
|
0 commit comments