@bun-win32/d3d12 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AI.md ADDED
@@ -0,0 +1,71 @@
1
+ # AI Guide for @bun-win32/d3d12
2
+
3
+ How to use this package, not what the Win32 API does.
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import D3d12, { SomeFlag } from '@bun-win32/d3d12';
9
+
10
+ // Methods bind lazily on first call
11
+ const result = D3d12.SomeFunctionW(arg1, arg2);
12
+
13
+ // Preload: array, single string, or no args (all symbols)
14
+ D3d12.Preload(['SomeFunctionW', 'AnotherFunction']);
15
+ D3d12.Preload('SomeFunctionW');
16
+ D3d12.Preload();
17
+ ```
18
+
19
+ ## Where To Look
20
+
21
+ | Need | Read |
22
+ | --------------------------------- | -------------------- |
23
+ | Find a method or its MS Docs link | `structs/D3d12.ts` |
24
+ | Find types, enums, constants | `types/D3d12.ts` |
25
+ | Quick examples | `README.md` |
26
+
27
+ `index.ts` re-exports the class and all types — import from `@bun-win32/d3d12` directly.
28
+
29
+ ## Calling Convention
30
+
31
+ All documented `d3d12.dll` exports are bound. Each method maps 1:1 to its DLL export. Names, parameter names, and order match Microsoft Docs.
32
+
33
+ ### Strings
34
+
35
+ `W` methods take UTF-16LE NUL-terminated buffers. `A` methods take ANSI strings.
36
+
37
+ ```ts
38
+ const wide = Buffer.from('Hello\0', 'utf16le'); // LPCWSTR
39
+ D3d12.SomeFunctionW(wide.ptr);
40
+
41
+ // Reading a wide string back from a buffer:
42
+ const text = new TextDecoder('utf-16').decode(buf).replace(/\0.*$/, '');
43
+ ```
44
+
45
+ ### Return types
46
+
47
+ - `HANDLE`, `HWND`, etc. → `bigint`
48
+ - `DWORD`, `UINT`, `BOOL`, `INT`, `LONG` → `number`
49
+ - `LPVOID`, `LPWSTR`, etc. → `Pointer`
50
+ - Win32 `BOOL` is `number` (0 or non-zero), **not** JS `boolean`. Do not compare with `=== true`.
51
+
52
+ ### Pointers, handles, out-parameters
53
+
54
+ - **Pointer** params (`LP*`, `P*`, `Pointer`): pass `buffer.ptr` from a caller-allocated `Buffer`.
55
+ - **Handle** params (`HANDLE`, `HWND`, etc.): pass a `bigint` value.
56
+ - **Out-parameters**: allocate a `Buffer`, pass `.ptr`, read the result after the call.
57
+
58
+ ```ts
59
+ const out = Buffer.alloc(4);
60
+ D3d12.SomeFunction(out.ptr);
61
+ const value = out.readUInt32LE(0);
62
+ ```
63
+
64
+ ### Nullability
65
+
66
+ - `| NULL` in a signature → pass `null` (optional pointer).
67
+ - `| 0n` in a signature → pass `0n` (optional handle).
68
+
69
+ ## Errors and Cleanup
70
+
71
+ Return values are raw. If the Win32 function uses last-error semantics, read via `GetLastError()`. Resource cleanup is your responsibility — same as raw Win32.
package/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # @bun-win32/d3d12
2
+
3
+ Zero-dependency, zero-overhead Win32 D3D12 bindings for [Bun](https://bun.sh) on Windows.
4
+
5
+ ## Overview
6
+
7
+ `@bun-win32/d3d12` exposes the `d3d12.dll` exports using [Bun](https://bun.sh)'s FFI. It provides a single class, `D3d12`, which lazily binds native symbols on first use. You can optionally preload a subset or all symbols up-front via `Preload()`.
8
+
9
+ The bindings are strongly typed for a smooth DX in TypeScript.
10
+
11
+ ## Features
12
+
13
+ - [Bun](https://bun.sh)-first ergonomics on Windows 10/11.
14
+ - Direct FFI to `d3d12.dll` (Direct3D 12 device creation, debug-layer access, global interface retrieval, and root-signature serialize/deserialize).
15
+ - In-source docs in `structs/D3d12.ts` with links to Microsoft Docs.
16
+ - Lazy binding on first call; optional eager preload (`D3d12.Preload()`).
17
+ - No wrapper overhead; calls map 1:1 to native APIs.
18
+ - Strongly-typed Win32 aliases (see `types/D3d12.ts`).
19
+
20
+ ## Requirements
21
+
22
+ - [Bun](https://bun.sh) runtime
23
+ - Windows 10 or later
24
+
25
+ ## Installation
26
+
27
+ ```sh
28
+ bun add @bun-win32/d3d12
29
+ ```
30
+
31
+ ## Quick Start
32
+
33
+ ```ts
34
+ import D3d12, { D3D_FEATURE_LEVEL } from '@bun-win32/d3d12';
35
+
36
+ // IID_ID3D12Device {189819f1-1db6-4b57-be54-1821339b85f7}
37
+ const iidDevice = Buffer.alloc(16);
38
+ iidDevice.writeUInt32LE(0x189819f1, 0);
39
+ iidDevice.writeUInt16LE(0x1db6, 4);
40
+ iidDevice.writeUInt16LE(0x4b57, 6);
41
+ Buffer.from([0xbe, 0x54, 0x18, 0x21, 0x33, 0x9b, 0x85, 0xf7]).copy(iidDevice, 8);
42
+
43
+ // Pass NULL for ppDevice to test support without creating the device:
44
+ // S_OK / S_FALSE means a D3D12-capable adapter exists at this feature level.
45
+ const hr = D3d12.D3D12CreateDevice(null, D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, iidDevice.ptr, null);
46
+ if (hr === 0 || hr === 1) {
47
+ console.log('D3D12 supported at feature level 11_0');
48
+ }
49
+
50
+ // Allocate an 8-byte slot and pass its .ptr to actually receive the device,
51
+ // then drive ID3D12Device via its COM vtable and call Release when done.
52
+ const ppDevice = Buffer.alloc(8);
53
+ const created = D3d12.D3D12CreateDevice(null, D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, iidDevice.ptr, ppDevice.ptr);
54
+ if (created === 0) {
55
+ const deviceAddress = ppDevice.readBigUInt64LE(0);
56
+ console.log(`ID3D12Device @ 0x${deviceAddress.toString(16)}`);
57
+ }
58
+ ```
59
+
60
+ > [!NOTE]
61
+ > AI agents: see `AI.md` for the package binding contract and source-navigation guidance. It explains how to use the package without scanning the entire implementation.
62
+
63
+ ## Examples
64
+
65
+ Run the included examples:
66
+
67
+ ```sh
68
+ # Thorough flat-FFI diagnostic: feature-level ladder, debug/SDK interfaces,
69
+ # and root-signature serialize -> deserialize round-trips
70
+ bun run example:d3d12-device-probe
71
+
72
+ # Creative: create a real ID3D12Device and x-ray its hidden capability
73
+ # matrix (ray tracing, mesh shaders, wave ops, ...) via CheckFeatureSupport
74
+ bun run example:d3d12-capability-xray
75
+ ```
76
+
77
+ ## Notes
78
+
79
+ - Either rely on lazy binding or call `D3d12.Preload()`.
80
+ - Windows only. Bun runtime required.
package/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ import D3d12 from './structs/D3d12';
2
+
3
+ export * from './types/D3d12';
4
+ export default D3d12;
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "author": "Stev Peifer <stev@bell.net>",
3
+ "bugs": {
4
+ "url": "https://github.com/ObscuritySRL/bun-win32/issues"
5
+ },
6
+ "dependencies": {
7
+ "@bun-win32/core": "1.1.2"
8
+ },
9
+ "description": "Zero-dependency, zero-overhead Win32 D3D12 bindings for Bun (FFI) on Windows.",
10
+ "devDependencies": {
11
+ "@types/bun": "latest"
12
+ },
13
+ "exports": {
14
+ ".": "./index.ts"
15
+ },
16
+ "license": "MIT",
17
+ "module": "index.ts",
18
+ "name": "@bun-win32/d3d12",
19
+ "peerDependencies": {
20
+ "typescript": "^5"
21
+ },
22
+ "private": false,
23
+ "homepage": "https://github.com/ObscuritySRL/bun-win32#readme",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git://github.com/ObscuritySRL/bun-win32.git",
27
+ "directory": "packages/d3d12"
28
+ },
29
+ "type": "module",
30
+ "version": "1.0.0",
31
+ "main": "./index.ts",
32
+ "keywords": [
33
+ "bun",
34
+ "ffi",
35
+ "win32",
36
+ "windows",
37
+ "d3d12",
38
+ "bindings",
39
+ "typescript",
40
+ "dll"
41
+ ],
42
+ "files": [
43
+ "AI.md",
44
+ "README.md",
45
+ "index.ts",
46
+ "structs/*.ts",
47
+ "types/*.ts"
48
+ ],
49
+ "sideEffects": false,
50
+ "engines": {
51
+ "bun": ">=1.1.0"
52
+ },
53
+ "scripts": {
54
+ "example:d3d12-device-probe": "bun ./example/d3d12-device-probe.ts",
55
+ "example:d3d12-capability-xray": "bun ./example/d3d12-capability-xray.ts"
56
+ }
57
+ }
@@ -0,0 +1,88 @@
1
+ import { type FFIFunction, FFIType } from 'bun:ffi';
2
+
3
+ import { Win32 } from '@bun-win32/core';
4
+
5
+ import type { HRESULT, IUnknown, LPCVOID, LPLPVOID, LPVOID, NULL, PD3D12_ROOT_SIGNATURE_DESC, PD3D12_VERSIONED_ROOT_SIGNATURE_DESC, PID3DBlob, PUINT, REFCLSID, REFIID, SIZE_T, UINT } from '../types/D3d12';
6
+ import type { D3D_FEATURE_LEVEL, D3D_ROOT_SIGNATURE_VERSION } from '../types/D3d12';
7
+
8
+ /**
9
+ * Thin, lazy-loaded FFI bindings for `d3d12.dll`.
10
+ *
11
+ * Each static method corresponds one-to-one with a Win32 export declared in `Symbols`.
12
+ * The first call to a method binds the underlying native symbol via `bun:ffi` and
13
+ * memoizes it on the class for subsequent calls. For bulk, up-front binding, use `Preload`.
14
+ *
15
+ * Symbols are defined with explicit `FFIType` signatures and kept alphabetized.
16
+ * You normally do not access `Symbols` directly; call the static methods or preload
17
+ * a subset for hot paths.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import D3d12, { D3D_FEATURE_LEVEL } from './structs/D3d12';
22
+ *
23
+ * // Lazy: bind on first call
24
+ * const iidDevice = Buffer.alloc(16);
25
+ * const ppDevice = Buffer.alloc(8);
26
+ * const hr = D3d12.D3D12CreateDevice(null, D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, iidDevice.ptr, ppDevice.ptr);
27
+ *
28
+ * // Or preload a subset to avoid per-symbol lazy binding cost
29
+ * D3d12.Preload(['D3D12CreateDevice', 'D3D12GetDebugInterface']);
30
+ * ```
31
+ */
32
+ class D3d12 extends Win32 {
33
+ protected static override name = 'd3d12.dll';
34
+
35
+ /** @inheritdoc */
36
+ protected static override readonly Symbols = {
37
+ D3D12CreateDevice: { args: [FFIType.ptr, FFIType.u32, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
38
+ D3D12CreateRootSignatureDeserializer: { args: [FFIType.ptr, FFIType.u64, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
39
+ D3D12CreateVersionedRootSignatureDeserializer: { args: [FFIType.ptr, FFIType.u64, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
40
+ D3D12EnableExperimentalFeatures: { args: [FFIType.u32, FFIType.ptr, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
41
+ D3D12GetDebugInterface: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
42
+ D3D12GetInterface: { args: [FFIType.ptr, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
43
+ D3D12SerializeRootSignature: { args: [FFIType.ptr, FFIType.u32, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
44
+ D3D12SerializeVersionedRootSignature: { args: [FFIType.ptr, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
45
+ } as const satisfies Record<string, FFIFunction>;
46
+
47
+ // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12createdevice
48
+ public static D3D12CreateDevice(pAdapter: IUnknown | NULL, MinimumFeatureLevel: D3D_FEATURE_LEVEL, riid: REFIID, ppDevice: LPLPVOID | NULL): HRESULT {
49
+ return D3d12.Load('D3D12CreateDevice')(pAdapter, MinimumFeatureLevel, riid, ppDevice);
50
+ }
51
+
52
+ // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12createrootsignaturedeserializer
53
+ public static D3D12CreateRootSignatureDeserializer(pSrcData: LPCVOID, SrcDataSizeInBytes: SIZE_T, pRootSignatureDeserializerInterface: REFIID, ppRootSignatureDeserializer: LPLPVOID): HRESULT {
54
+ return D3d12.Load('D3D12CreateRootSignatureDeserializer')(pSrcData, SrcDataSizeInBytes, pRootSignatureDeserializerInterface, ppRootSignatureDeserializer);
55
+ }
56
+
57
+ // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12createversionedrootsignaturedeserializer
58
+ public static D3D12CreateVersionedRootSignatureDeserializer(pSrcData: LPCVOID, SrcDataSizeInBytes: SIZE_T, pRootSignatureDeserializerInterface: REFIID, ppRootSignatureDeserializer: LPLPVOID): HRESULT {
59
+ return D3d12.Load('D3D12CreateVersionedRootSignatureDeserializer')(pSrcData, SrcDataSizeInBytes, pRootSignatureDeserializerInterface, ppRootSignatureDeserializer);
60
+ }
61
+
62
+ // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12enableexperimentalfeatures
63
+ public static D3D12EnableExperimentalFeatures(NumFeatures: UINT, pIIDs: REFIID, pConfigurationStructs: LPVOID | NULL, pConfigurationStructSizes: PUINT | NULL): HRESULT {
64
+ return D3d12.Load('D3D12EnableExperimentalFeatures')(NumFeatures, pIIDs, pConfigurationStructs, pConfigurationStructSizes);
65
+ }
66
+
67
+ // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12getdebuginterface
68
+ public static D3D12GetDebugInterface(riid: REFIID, ppvDebug: LPLPVOID | NULL): HRESULT {
69
+ return D3d12.Load('D3D12GetDebugInterface')(riid, ppvDebug);
70
+ }
71
+
72
+ // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12getinterface
73
+ public static D3D12GetInterface(rclsid: REFCLSID, riid: REFIID, ppvDebug: LPLPVOID | NULL): HRESULT {
74
+ return D3d12.Load('D3D12GetInterface')(rclsid, riid, ppvDebug);
75
+ }
76
+
77
+ // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12serializerootsignature
78
+ public static D3D12SerializeRootSignature(pRootSignature: PD3D12_ROOT_SIGNATURE_DESC, Version: D3D_ROOT_SIGNATURE_VERSION, ppBlob: PID3DBlob, ppErrorBlob: PID3DBlob | NULL): HRESULT {
79
+ return D3d12.Load('D3D12SerializeRootSignature')(pRootSignature, Version, ppBlob, ppErrorBlob);
80
+ }
81
+
82
+ // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12serializeversionedrootsignature
83
+ public static D3D12SerializeVersionedRootSignature(pRootSignature: PD3D12_VERSIONED_ROOT_SIGNATURE_DESC, ppBlob: PID3DBlob, ppErrorBlob: PID3DBlob | NULL): HRESULT {
84
+ return D3d12.Load('D3D12SerializeVersionedRootSignature')(pRootSignature, ppBlob, ppErrorBlob);
85
+ }
86
+ }
87
+
88
+ export default D3d12;
package/types/D3d12.ts ADDED
@@ -0,0 +1,32 @@
1
+ import type { Pointer } from 'bun:ffi';
2
+
3
+ export type { HRESULT, LPCVOID, LPVOID, NULL, SIZE_T, UINT } from '@bun-win32/core';
4
+
5
+ export enum D3D_FEATURE_LEVEL {
6
+ D3D_FEATURE_LEVEL_10_0 = 0xa000,
7
+ D3D_FEATURE_LEVEL_10_1 = 0xa100,
8
+ D3D_FEATURE_LEVEL_11_0 = 0xb000,
9
+ D3D_FEATURE_LEVEL_11_1 = 0xb100,
10
+ D3D_FEATURE_LEVEL_12_0 = 0xc000,
11
+ D3D_FEATURE_LEVEL_12_1 = 0xc100,
12
+ D3D_FEATURE_LEVEL_12_2 = 0xc200,
13
+ D3D_FEATURE_LEVEL_1_0_CORE = 0x1000,
14
+ D3D_FEATURE_LEVEL_9_1 = 0x9100,
15
+ D3D_FEATURE_LEVEL_9_2 = 0x9200,
16
+ D3D_FEATURE_LEVEL_9_3 = 0x9300,
17
+ }
18
+
19
+ export enum D3D_ROOT_SIGNATURE_VERSION {
20
+ D3D_ROOT_SIGNATURE_VERSION_1 = 0x1,
21
+ D3D_ROOT_SIGNATURE_VERSION_1_0 = 0x1,
22
+ D3D_ROOT_SIGNATURE_VERSION_1_1 = 0x2,
23
+ }
24
+
25
+ export type IUnknown = Pointer;
26
+ export type LPLPVOID = Pointer;
27
+ export type PD3D12_ROOT_SIGNATURE_DESC = Pointer;
28
+ export type PD3D12_VERSIONED_ROOT_SIGNATURE_DESC = Pointer;
29
+ export type PID3DBlob = Pointer;
30
+ export type PUINT = Pointer;
31
+ export type REFCLSID = Pointer;
32
+ export type REFIID = Pointer;