@driftengine/native-host 4.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.
Files changed (134) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +29 -0
  3. package/README.md +58 -0
  4. package/dist/activation.d.ts +26 -0
  5. package/dist/activation.js +38 -0
  6. package/dist/alphaCopy.d.ts +36 -0
  7. package/dist/alphaCopy.js +178 -0
  8. package/dist/audio.d.ts +55 -0
  9. package/dist/audio.js +193 -0
  10. package/dist/audioParams.d.ts +25 -0
  11. package/dist/audioParams.js +128 -0
  12. package/dist/bridge.d.ts +70 -0
  13. package/dist/bridge.js +170 -0
  14. package/dist/canvas.d.ts +97 -0
  15. package/dist/canvas.js +197 -0
  16. package/dist/canvasStyle.d.ts +23 -0
  17. package/dist/canvasStyle.js +79 -0
  18. package/dist/device.d.ts +34 -0
  19. package/dist/device.js +144 -0
  20. package/dist/dialogs.d.ts +23 -0
  21. package/dist/dialogs.js +85 -0
  22. package/dist/domEvents.d.ts +37 -0
  23. package/dist/domEvents.js +145 -0
  24. package/dist/domTree.d.ts +34 -0
  25. package/dist/domTree.js +194 -0
  26. package/dist/dualRumble.d.ts +19 -0
  27. package/dist/dualRumble.js +47 -0
  28. package/dist/eventClasses.d.ts +12 -0
  29. package/dist/eventClasses.js +35 -0
  30. package/dist/files.d.ts +16 -0
  31. package/dist/files.js +59 -0
  32. package/dist/fullscreen.d.ts +33 -0
  33. package/dist/fullscreen.js +65 -0
  34. package/dist/game.d.ts +25 -0
  35. package/dist/game.js +38 -0
  36. package/dist/gamepads.d.ts +65 -0
  37. package/dist/gamepads.js +178 -0
  38. package/dist/globals.d.ts +24 -0
  39. package/dist/globals.js +37 -0
  40. package/dist/gpu.d.ts +26 -0
  41. package/dist/gpu.js +47 -0
  42. package/dist/images.d.ts +50 -0
  43. package/dist/images.js +111 -0
  44. package/dist/index.d.ts +23 -0
  45. package/dist/index.js +21 -0
  46. package/dist/keyCodes.d.ts +30 -0
  47. package/dist/keyCodes.js +205 -0
  48. package/dist/keyEvents.d.ts +54 -0
  49. package/dist/keyEvents.js +117 -0
  50. package/dist/mediaQueries.d.ts +62 -0
  51. package/dist/mediaQueries.js +152 -0
  52. package/dist/mouseEvents.d.ts +96 -0
  53. package/dist/mouseEvents.js +287 -0
  54. package/dist/page.d.ts +118 -0
  55. package/dist/page.js +302 -0
  56. package/dist/pointerCapture.d.ts +32 -0
  57. package/dist/pointerCapture.js +69 -0
  58. package/dist/present.d.ts +27 -0
  59. package/dist/present.js +83 -0
  60. package/dist/rawPad.d.ts +44 -0
  61. package/dist/rawPad.js +65 -0
  62. package/dist/readback.d.ts +18 -0
  63. package/dist/readback.js +46 -0
  64. package/dist/runtime.d.ts +69 -0
  65. package/dist/runtime.js +103 -0
  66. package/dist/standardPad.d.ts +49 -0
  67. package/dist/standardPad.js +80 -0
  68. package/dist/textHost.d.ts +37 -0
  69. package/dist/textHost.js +43 -0
  70. package/dist/threads.d.ts +27 -0
  71. package/dist/threads.js +59 -0
  72. package/dist/tileFiles.d.ts +16 -0
  73. package/dist/tileFiles.js +36 -0
  74. package/dist/touchEvents.d.ts +67 -0
  75. package/dist/touchEvents.js +210 -0
  76. package/dist/uiEvents.d.ts +157 -0
  77. package/dist/uiEvents.js +206 -0
  78. package/dist/uiTouch.d.ts +59 -0
  79. package/dist/uiTouch.js +75 -0
  80. package/dist/window.d.ts +71 -0
  81. package/dist/window.js +164 -0
  82. package/dist/windowMode.d.ts +19 -0
  83. package/dist/windowMode.js +15 -0
  84. package/dist/windowTitle.d.ts +18 -0
  85. package/dist/windowTitle.js +33 -0
  86. package/dist/workerScope.mjs +48 -0
  87. package/licenses/GPL-3.0.txt +674 -0
  88. package/package.json +83 -0
  89. package/src/activation.ts +42 -0
  90. package/src/alphaCopy.ts +201 -0
  91. package/src/audio.ts +246 -0
  92. package/src/audioParams.ts +150 -0
  93. package/src/bridge.ts +230 -0
  94. package/src/canvas.ts +227 -0
  95. package/src/canvasStyle.ts +89 -0
  96. package/src/device.ts +167 -0
  97. package/src/dialogs.ts +98 -0
  98. package/src/domEvents.ts +170 -0
  99. package/src/domTree.ts +215 -0
  100. package/src/drs.d.ts +9 -0
  101. package/src/dualRumble.ts +58 -0
  102. package/src/eventClasses.ts +46 -0
  103. package/src/files.ts +61 -0
  104. package/src/fixtures/grey.jpg +0 -0
  105. package/src/fixtures/rgb.png +0 -0
  106. package/src/fixtures/tone.opus +0 -0
  107. package/src/fullscreen.ts +71 -0
  108. package/src/game.ts +53 -0
  109. package/src/gamepads.ts +195 -0
  110. package/src/globals.ts +39 -0
  111. package/src/gpu.ts +59 -0
  112. package/src/images.ts +148 -0
  113. package/src/index.ts +24 -0
  114. package/src/keyCodes.ts +203 -0
  115. package/src/keyEvents.ts +135 -0
  116. package/src/mediaQueries.ts +173 -0
  117. package/src/mouseEvents.ts +328 -0
  118. package/src/page.ts +337 -0
  119. package/src/pointerCapture.ts +73 -0
  120. package/src/present.ts +92 -0
  121. package/src/rawPad.ts +85 -0
  122. package/src/readback.ts +61 -0
  123. package/src/runtime.ts +162 -0
  124. package/src/standardPad.ts +126 -0
  125. package/src/textHost.ts +66 -0
  126. package/src/threads.ts +66 -0
  127. package/src/tileFiles.ts +39 -0
  128. package/src/touchEvents.ts +238 -0
  129. package/src/uiEvents.ts +268 -0
  130. package/src/uiTouch.ts +97 -0
  131. package/src/window.ts +192 -0
  132. package/src/windowMode.ts +24 -0
  133. package/src/windowTitle.ts +36 -0
  134. package/src/workerScope.mjs +48 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Drift Technologies
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,29 @@
1
+ DriftEngine
2
+ Copyright 2026 Drift Technologies
3
+
4
+ This product includes software developed at Drift Technologies
5
+ (https://github.com/drftrun/driftengine).
6
+
7
+ Licensed under the Apache License, Version 2.0. Section 4(d) of that licence
8
+ requires this NOTICE to be reproduced in any derivative work you distribute,
9
+ in the source, the documentation, or a display generated by the work.
10
+
11
+ Models run by DriftCapture
12
+ --------------------------
13
+ DriftCapture runs the models below. Their weights are not in this repository
14
+ and are not distributed with it: tools/capture-weights/ fetches each at the
15
+ revision its manifest pins, verifies it, and converts it, and a game that
16
+ ships one ships it under that model's own licence. Each model's definition in
17
+ this engine is written from its project's published code.
18
+
19
+ - Depth Anything 3 Small and Depth Anything 3 Base. Apache License 2.0.
20
+ Copyright (c) 2025 ByteDance Ltd. and/or its affiliates.
21
+ - Depth Anything V2 Small. Apache License 2.0. The University of Hong Kong
22
+ and TikTok; converted to the Transformers layout by The HuggingFace Team.
23
+ - MobileSAM. Apache License 2.0. Its image encoder is TinyViT, Copyright (c)
24
+ 2022 Microsoft; its prompt encoder and mask decoder are Segment Anything's,
25
+ Copyright (c) Meta Platforms, Inc. and affiliates.
26
+ - SAM 2.1 tiny. Apache License 2.0. Copyright (c) Meta Platforms, Inc. and
27
+ affiliates.
28
+ - OWLv2 base patch16. Apache License 2.0. Copyright 2023 Google AI and The
29
+ HuggingFace Team.
package/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # `@driftengine/native-host`
2
+
3
+ The engine on a native window and a native WebGPU device, with no browser between them.
4
+
5
+ A game built on the engine runs here unchanged. The host is Node, Dawn — the WebGPU implementation
6
+ Chrome runs — through `@kmamal/gpu`, SDL through `@kmamal/sdl` for the window, input and
7
+ controllers, and Web Audio from `node-web-audio-api`. Around the canvas it supplies the page a game
8
+ expects: `window`, `document`, animation frames, the browser's input events in the order Chrome
9
+ sends them, pointer lock, fullscreen, gamepads, `matchMedia`, and the desktop shell's bridge
10
+ (`__driftHost`) for storage, window modes and quit.
11
+
12
+ **A game installs it and the packager ships it.** `npm install @driftengine/native-host` in the
13
+ game, at the engine's version, and `drift-package build --target=native-linux-x64` bundles the game
14
+ with it into an archive that carries its own Node. The packager names no range on this package and
15
+ resolves it from the game's own tree, so a game that builds only for the browser or for Electron
16
+ never downloads a native binary.
17
+
18
+ **A game's contract with it is one export.** In a browser a page finds its canvas in its markup;
19
+ here there is no markup, so the manifest's `native.entry` names a module exporting
20
+ `mount(canvas)`, and the host calls it with its canvas once it has started. A game that already
21
+ mounts onto a canvas it is given needs a file of one line:
22
+
23
+ ```ts
24
+ export { mount } from './game.ts';
25
+ ```
26
+
27
+ `runGame` is that start, and what the packager's generated entry calls; `startHost` is the same
28
+ start without a game, for a tool that draws its own frames. `tileFiles(dir)` is the one thing a
29
+ page has no equivalent of: the texture streamer's tiles read from a directory on disk, for a
30
+ native entry to hand its streamer where a page would fetch them.
31
+
32
+ ## What it is measured against
33
+
34
+ Every published scene is drawn by this host and by Chrome and compared pixel for pixel on the same
35
+ machine (`npm run native:gate` in the engine's repository). The engine's reference mix is compared
36
+ against Chrome's too. Each behaviour a page reads — an event's order, a refusal's wording, a double
37
+ click's timing — is Chrome's, measured over the DevTools protocol where it can be, and asserted by a
38
+ test beside the module that implements it.
39
+
40
+ ## What it gives up
41
+
42
+ - Linux x64 is the platform it has been measured on, and the one the packager builds for. Windows
43
+ and macOS come after 4.0.0: the three bindings publish binaries for both, and what each still
44
+ needs is a launcher, its signing, and the same measurements against Chrome on its own GPU.
45
+ - There is no DOM beyond the canvas: `document.createElement` refuses by name, so a game's own
46
+ HTML interface does not come with it.
47
+ - WebGL2 is not offered; the engine runs on WebGPU here.
48
+ - Clip export, `navigator.share` and WebXR are absent, and the engine says so where it asks.
49
+ - The engine's badge is not drawn, and there is no store: `steam.appId` does nothing here, which
50
+ `drift-package doctor` says for a manifest that sets it.
51
+ - Pipelines are compiled on every start, because the binding exposes no cache for them: most of
52
+ the start of a small program, 355 ms of 750 measured on 2026-09-19.
53
+
54
+ Apache-2.0. Dawn, SDL, Node and the Web Audio engine carry their own licences, which a packaged game
55
+ ships in its `licenses/` with every other part's. **One of them is the LGPL**: the Opus decoder
56
+ reads Ogg pages with `codec-parser`, which is LGPL-3.0, so a packaged game carries it as a file of
57
+ its own that a person can replace rather than inside the bundle, with the GPL's text beside it —
58
+ `licenses/GPL-3.0.txt` in this package, which that licence asks to accompany it.
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Whether a person has just done something, as a browser's transient activation answers it — the
3
+ * gate on what a page may do only in answer to a person: pointer lock, fullscreen.
4
+ *
5
+ * **Chrome's rules, measured 2026-09-19 in Chrome 151 over the DevTools protocol.** A key down
6
+ * other than Escape, a mouse press or a finger lifted makes a page active for five seconds: a
7
+ * request 4.5 s after an unused click was granted and one at 5.5 s refused. Fullscreen spends it —
8
+ * a second request 100 ms after a granted one was refused — and pointer lock does not: a lock and
9
+ * then fullscreen from one click were both granted. The input is noted before its event is sent,
10
+ * so a listener for that very event may already ask.
11
+ *
12
+ * What it gives up: a script's own `dispatchEvent` is never a gesture, as in a browser, so input a
13
+ * capture replays goes through SDL's events (`HostWindow.replay`) to count as one.
14
+ */
15
+ export declare class UserActivation {
16
+ private readonly now;
17
+ private at;
18
+ private ever;
19
+ constructor(now: () => number);
20
+ /** A person did something: a key down, a mouse press, a finger lifted. */
21
+ notify(): void;
22
+ /** Spent, by an ask that takes the gesture with it. */
23
+ consume(): void;
24
+ get isActive(): boolean;
25
+ get hasBeenActive(): boolean;
26
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Whether a person has just done something, as a browser's transient activation answers it — the
3
+ * gate on what a page may do only in answer to a person: pointer lock, fullscreen.
4
+ *
5
+ * **Chrome's rules, measured 2026-09-19 in Chrome 151 over the DevTools protocol.** A key down
6
+ * other than Escape, a mouse press or a finger lifted makes a page active for five seconds: a
7
+ * request 4.5 s after an unused click was granted and one at 5.5 s refused. Fullscreen spends it —
8
+ * a second request 100 ms after a granted one was refused — and pointer lock does not: a lock and
9
+ * then fullscreen from one click were both granted. The input is noted before its event is sent,
10
+ * so a listener for that very event may already ask.
11
+ *
12
+ * What it gives up: a script's own `dispatchEvent` is never a gesture, as in a browser, so input a
13
+ * capture replays goes through SDL's events (`HostWindow.replay`) to count as one.
14
+ */
15
+ const ACTIVE_MS = 5000;
16
+ export class UserActivation {
17
+ now;
18
+ at = Number.NEGATIVE_INFINITY;
19
+ ever = false;
20
+ constructor(now) {
21
+ this.now = now;
22
+ }
23
+ /** A person did something: a key down, a mouse press, a finger lifted. */
24
+ notify() {
25
+ this.at = this.now();
26
+ this.ever = true;
27
+ }
28
+ /** Spent, by an ask that takes the gesture with it. */
29
+ consume() {
30
+ this.at = Number.NEGATIVE_INFINITY;
31
+ }
32
+ get isActive() {
33
+ return this.now() - this.at < ACTIVE_MS;
34
+ }
35
+ get hasBeenActive() {
36
+ return this.ever;
37
+ }
38
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The alpha step of a browser's image copy, done where the browser does it: on the device.
3
+ *
4
+ * **Chrome converts alpha in a copy with a shader**, Dawn's `CopyTextureForBrowser`: it divides a
5
+ * premultiplied image's colour by its alpha, or multiplies a straight one's, in single precision on
6
+ * the GPU, and into an sRGB texture it decodes the result first so the write's own encode lands on
7
+ * it. This GPU's division is not correctly rounded, so no arithmetic on the CPU gives the same
8
+ * bytes: a rounded `c · 255 / a` differed from Chrome on 4,299 of 65,024 (colour, alpha) pairs.
9
+ * The same steps run here, in the same order and precision, on the same device.
10
+ *
11
+ * Measured 2026-09-19 against Chrome on this machine, for every colour at every alpha: the
12
+ * divide and the multiply, into `rgba8unorm` and into `rgba8unorm-srgb` — see `device.test.ts` for
13
+ * the pairs held as literals.
14
+ *
15
+ * **What it gives up: the bytes are this GPU's.** Another GPU divides differently, and so does
16
+ * Chrome on it, so the claim is "what Chrome writes on the machine the host runs on", which is
17
+ * the only thing a pixel gate on that machine can compare.
18
+ */
19
+ /** Which conversion a copy needs. */
20
+ export type AlphaStep = 'unpremultiply' | 'premultiply';
21
+ export declare class AlphaCopier {
22
+ private readonly device;
23
+ private readonly pipelines;
24
+ private module;
25
+ constructor(device: GPUDevice);
26
+ /**
27
+ * Write an image through `step` into `destination`: `bytes` are four a texel, `imageWidth` wide,
28
+ * and the top-left `width` by `height` of them is what is copied.
29
+ *
30
+ * The destination must allow rendering into it, which a copy's destination already must
31
+ * (`copyExternalImageToTexture` asks for `RENDER_ATTACHMENT`). Submitted here, so it is ordered
32
+ * on the queue where the copy it stands in for was called.
33
+ */
34
+ copy(bytes: Uint8Array, imageWidth: number, imageHeight: number, step: AlphaStep, destination: GPUCopyExternalImageDestInfo, width: number, height: number): void;
35
+ private pipelineFor;
36
+ }
@@ -0,0 +1,178 @@
1
+ /**
2
+ * The alpha step of a browser's image copy, done where the browser does it: on the device.
3
+ *
4
+ * **Chrome converts alpha in a copy with a shader**, Dawn's `CopyTextureForBrowser`: it divides a
5
+ * premultiplied image's colour by its alpha, or multiplies a straight one's, in single precision on
6
+ * the GPU, and into an sRGB texture it decodes the result first so the write's own encode lands on
7
+ * it. This GPU's division is not correctly rounded, so no arithmetic on the CPU gives the same
8
+ * bytes: a rounded `c · 255 / a` differed from Chrome on 4,299 of 65,024 (colour, alpha) pairs.
9
+ * The same steps run here, in the same order and precision, on the same device.
10
+ *
11
+ * Measured 2026-09-19 against Chrome on this machine, for every colour at every alpha: the
12
+ * divide and the multiply, into `rgba8unorm` and into `rgba8unorm-srgb` — see `device.test.ts` for
13
+ * the pairs held as literals.
14
+ *
15
+ * **What it gives up: the bytes are this GPU's.** Another GPU divides differently, and so does
16
+ * Chrome on it, so the claim is "what Chrome writes on the machine the host runs on", which is
17
+ * the only thing a pixel gate on that machine can compare.
18
+ */
19
+ const STEP_WGSL = /* wgsl */ `
20
+ struct Params {
21
+ origin: vec2i,
22
+ steps: u32,
23
+ g: f32, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32,
24
+ }
25
+
26
+ @group(0) @binding(0) var source: texture_2d<f32>;
27
+ @group(0) @binding(1) var<uniform> params: Params;
28
+
29
+ @vertex
30
+ fn vertexMain(@builtin(vertex_index) index: u32) -> @builtin(position) vec4f {
31
+ let x = f32((index << 1u) & 2u);
32
+ let y = f32(index & 2u);
33
+ return vec4f(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
34
+ }
35
+
36
+ /* Dawn's parametric transfer function, as its copy shader writes it. */
37
+ fn transfer(v: f32) -> f32 {
38
+ let absV = abs(v);
39
+ let sgn = sign(v);
40
+ if (absV < params.d) {
41
+ return sgn * (params.c * absV + params.f);
42
+ }
43
+ return sgn * (pow(params.a * absV + params.b, params.g) + params.e);
44
+ }
45
+
46
+ @fragment
47
+ fn fragmentMain(@builtin(position) position: vec4f) -> @location(0) vec4f {
48
+ var color = textureLoad(source, vec2i(position.xy) - params.origin, 0);
49
+ if ((params.steps & 1u) != 0u) {
50
+ if (color.a != 0.0) {
51
+ color = vec4f(color.rgb / color.a, color.a);
52
+ }
53
+ }
54
+ if ((params.steps & 2u) != 0u) {
55
+ color = vec4f(color.rgb * color.a, color.a);
56
+ }
57
+ if ((params.steps & 4u) != 0u) {
58
+ color = vec4f(transfer(color.r), transfer(color.g), transfer(color.b), color.a);
59
+ }
60
+ return color;
61
+ }
62
+ `;
63
+ /* Usage flags, named here rather than read off a global a host may not have installed. */
64
+ const COPY_DST = 0x2;
65
+ const TEXTURE_BINDING = 0x4;
66
+ const BUFFER_COPY_DST = 0x8;
67
+ const UNIFORM = 0x40;
68
+ const UNPREMULTIPLY = 1;
69
+ const PREMULTIPLY = 2;
70
+ const DECODE_FOR_SRGB = 4;
71
+ /**
72
+ * The sRGB decode's parameters, as Dawn passes them: computed in double and stored as float, which
73
+ * is what a `Float32Array` does with the same expressions.
74
+ */
75
+ const SRGB_DECODE = [2.4, 1 / 1.055, 0.055 / 1.055, 1 / 12.92, 0.04045, 0, 0];
76
+ /** 48 bytes: the struct is 40, and a uniform binding is sized in sixteens. */
77
+ const PARAMS_BYTES = 48;
78
+ export class AlphaCopier {
79
+ device;
80
+ pipelines = new Map();
81
+ module = null;
82
+ constructor(device) {
83
+ this.device = device;
84
+ }
85
+ /**
86
+ * Write an image through `step` into `destination`: `bytes` are four a texel, `imageWidth` wide,
87
+ * and the top-left `width` by `height` of them is what is copied.
88
+ *
89
+ * The destination must allow rendering into it, which a copy's destination already must
90
+ * (`copyExternalImageToTexture` asks for `RENDER_ATTACHMENT`). Submitted here, so it is ordered
91
+ * on the queue where the copy it stands in for was called.
92
+ */
93
+ copy(bytes, imageWidth, imageHeight, step, destination, width, height) {
94
+ const device = this.device;
95
+ const target = destination.texture;
96
+ const format = target.format;
97
+ const origin = destination.origin;
98
+ const x = origin?.x ?? 0;
99
+ const y = origin?.y ?? 0;
100
+ const source = device.createTexture({
101
+ label: 'native alpha copy',
102
+ size: [imageWidth, imageHeight],
103
+ format: 'rgba8unorm',
104
+ usage: COPY_DST | TEXTURE_BINDING,
105
+ });
106
+ device.queue.writeTexture({ texture: source }, bytes, { bytesPerRow: imageWidth * 4 }, [
107
+ imageWidth,
108
+ imageHeight,
109
+ ]);
110
+ const params = new ArrayBuffer(PARAMS_BYTES);
111
+ new Int32Array(params, 0, 2).set([x, y]);
112
+ const steps = (step === 'unpremultiply' ? UNPREMULTIPLY : PREMULTIPLY) |
113
+ (format.endsWith('-srgb') ? DECODE_FOR_SRGB : 0);
114
+ new Uint32Array(params, 8, 1)[0] = steps;
115
+ new Float32Array(params, 12, 7).set(SRGB_DECODE);
116
+ const uniforms = device.createBuffer({
117
+ label: 'native alpha copy',
118
+ size: PARAMS_BYTES,
119
+ usage: UNIFORM | BUFFER_COPY_DST,
120
+ });
121
+ device.queue.writeBuffer(uniforms, 0, params);
122
+ const pipeline = this.pipelineFor(format);
123
+ const group = device.createBindGroup({
124
+ label: 'native alpha copy',
125
+ layout: pipeline.getBindGroupLayout(0),
126
+ entries: [
127
+ { binding: 0, resource: source.createView() },
128
+ { binding: 1, resource: { buffer: uniforms } },
129
+ ],
130
+ });
131
+ const encoder = device.createCommandEncoder({ label: 'native alpha copy' });
132
+ const pass = encoder.beginRenderPass({
133
+ label: 'native alpha copy',
134
+ colorAttachments: [
135
+ {
136
+ view: target.createView({
137
+ dimension: '2d',
138
+ baseMipLevel: destination.mipLevel ?? 0,
139
+ mipLevelCount: 1,
140
+ baseArrayLayer: origin?.z ?? 0,
141
+ arrayLayerCount: 1,
142
+ }),
143
+ /* Kept, because the copy covers a rectangle and the rest of the level is not its own. */
144
+ loadOp: 'load',
145
+ storeOp: 'store',
146
+ },
147
+ ],
148
+ });
149
+ pass.setPipeline(pipeline);
150
+ pass.setBindGroup(0, group);
151
+ pass.setViewport(x, y, width, height, 0, 1);
152
+ pass.setScissorRect(x, y, width, height);
153
+ pass.draw(3);
154
+ pass.end();
155
+ device.queue.submit([encoder.finish()]);
156
+ /* Released once the work that reads them is done; Dawn's binding asserts on a release under
157
+ work in flight. */
158
+ void device.queue.onSubmittedWorkDone().then(() => {
159
+ source.destroy();
160
+ uniforms.destroy();
161
+ });
162
+ }
163
+ pipelineFor(format) {
164
+ const held = this.pipelines.get(format);
165
+ if (held !== undefined)
166
+ return held;
167
+ this.module ??= this.device.createShaderModule({ label: 'native alpha copy', code: STEP_WGSL });
168
+ const pipeline = this.device.createRenderPipeline({
169
+ label: 'native alpha copy',
170
+ layout: 'auto',
171
+ vertex: { module: this.module, entryPoint: 'vertexMain' },
172
+ fragment: { module: this.module, entryPoint: 'fragmentMain', targets: [{ format }] },
173
+ primitive: { topology: 'triangle-list' },
174
+ });
175
+ this.pipelines.set(format, pipeline);
176
+ return pipeline;
177
+ }
178
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Web Audio for a host with no browser: the Rust engine's contexts, with Opus decoded where it
3
+ * cannot, and underruns counted.
4
+ *
5
+ * **The engine's mix is Web Audio from end to end** — `AudioGraph` and `MixConsole` build nodes on a
6
+ * context and never touch a device — so the host's job is a Web Audio implementation, not a mixer.
7
+ * It is `node-web-audio-api` (IRCAM, BSD-3-Clause), `web-audio-api-rs` with prebuilt binaries,
8
+ * which plays through JACK on Linux, and PipeWire provides that. Chosen by the maintainer on
9
+ * 2026-09-19.
10
+ *
11
+ * **Opus is decoded here**, by libopus as WebAssembly (`ogg-opus-decoder`, MIT), because the Rust
12
+ * engine's decoder refuses it (measured: "unsupported audio codec") and Opus is the first format the
13
+ * engine's sound registry looks for. Anything else goes to the Rust engine's own decoder. A decode
14
+ * comes back at the context's sample rate, as the specification's `decodeAudioData` does: Opus is
15
+ * always 48 kHz, so a context at another rate has it resampled, by the same engine rendering it
16
+ * offline.
17
+ *
18
+ * **Sample rate and buffer size are the host's to ask for** — `sampleRate` and `latencyHint`, given
19
+ * to every context the engine creates, and what the device gave is reported once it runs; under
20
+ * JACK the server sets the buffer, so the latency asked for is a request. **An underrun is counted**
21
+ * from the context's `renderCapacity` and said once, rather than heard as a click and never
22
+ * reported.
23
+ *
24
+ * What it gives up: a browser's autoplay rule, which has no meaning here, so a context runs from
25
+ * the moment it is made; and the decoders' sample-for-sample agreement with Chrome's, which no
26
+ * gate compares yet.
27
+ */
28
+ type WebAudioModule = {
29
+ AudioContext: typeof AudioContext;
30
+ OfflineAudioContext: typeof OfflineAudioContext;
31
+ AudioBuffer: typeof AudioBuffer;
32
+ };
33
+ export interface AudioDefaults {
34
+ readonly sampleRate?: number;
35
+ /** A category, or seconds of buffer. */
36
+ readonly latencyHint?: AudioContextLatencyCategory | number;
37
+ /** `none` for a context with no output device: a check, a CI machine. */
38
+ readonly sink?: 'default' | 'none';
39
+ }
40
+ /** An Ogg stream whose first page carries an Opus header. */
41
+ export declare function isOggOpus(bytes: Uint8Array): boolean;
42
+ /** Watches a context's render capacity and counts the updates in which it could not keep up. */
43
+ export declare class UnderrunCounter {
44
+ underruns: number;
45
+ constructor(capacity: EventTarget & {
46
+ start(options?: {
47
+ updateInterval?: number;
48
+ }): void;
49
+ });
50
+ }
51
+ /** The two context classes a page has, over the Rust engine, with the host's defaults. */
52
+ export declare function hostAudioContexts(defaults?: AudioDefaults): WebAudioModule;
53
+ /** Give the page its audio globals, and hand back what takes them away again. */
54
+ export declare function installAudio(defaults?: AudioDefaults, scope?: Record<string, unknown>): () => void;
55
+ export {};