@null3d/engine 0.0.0 → 0.1.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 (311) hide show
  1. package/README.md +109 -0
  2. package/THIRD-PARTY-NOTICES.txt +0 -1
  3. package/dist/wasm/single/null3d.js +19 -6
  4. package/dist/wasm/single/null3d_bg.wasm +0 -0
  5. package/dist/wasm/threaded/null3d.js +19 -6
  6. package/dist/wasm/threaded/null3d_bg.wasm +0 -0
  7. package/docs/api/animation.md +23 -67
  8. package/docs/api/assets.md +42 -26
  9. package/docs/api/cameras.md +8 -6
  10. package/docs/api/controls.md +46 -44
  11. package/docs/api/debug.md +13 -8
  12. package/docs/api/engine.md +17 -11
  13. package/docs/api/feature-draws.md +3 -3
  14. package/docs/api/feature-effects.md +3 -3
  15. package/docs/api/feature-instances.md +2 -2
  16. package/docs/api/features.md +14 -15
  17. package/docs/api/geometry.md +56 -75
  18. package/docs/api/grass.md +1 -1
  19. package/docs/api/impostors.md +120 -9
  20. package/docs/api/input.md +4 -11
  21. package/docs/api/lights.md +14 -38
  22. package/docs/api/lines.md +8 -25
  23. package/docs/api/materials.md +69 -70
  24. package/docs/api/math.md +6 -7
  25. package/docs/api/objects.md +13 -13
  26. package/docs/api/particles.md +2 -14
  27. package/docs/api/points.md +3 -25
  28. package/docs/api/post.md +22 -23
  29. package/docs/api/quality.md +11 -6
  30. package/docs/api/raycast.md +13 -32
  31. package/docs/api/reference/animation.md +9 -9
  32. package/docs/api/reference/assets.md +18 -18
  33. package/docs/api/reference/cameras.md +5 -2
  34. package/docs/api/reference/controls.md +75 -23
  35. package/docs/api/reference/debug.md +23 -8
  36. package/docs/api/reference/engine.md +22 -10
  37. package/docs/api/reference/geometry.md +161 -82
  38. package/docs/api/reference/grass.md +1 -1
  39. package/docs/api/reference/impostors.md +92 -1
  40. package/docs/api/reference/input.md +3 -3
  41. package/docs/api/reference/lights.md +9 -9
  42. package/docs/api/reference/lines.md +7 -7
  43. package/docs/api/reference/materials.md +48 -48
  44. package/docs/api/reference/math.md +22 -22
  45. package/docs/api/reference/objects.md +9 -9
  46. package/docs/api/reference/points.md +4 -4
  47. package/docs/api/reference/post.md +18 -18
  48. package/docs/api/reference/quality.md +7 -3
  49. package/docs/api/reference/raycast.md +6 -6
  50. package/docs/api/reference/render.md +2 -2
  51. package/docs/api/reference/scene.md +29 -26
  52. package/docs/api/reference/sketch.md +4 -0
  53. package/docs/api/reference/sound.md +203 -0
  54. package/docs/api/reference/sprites.md +5 -5
  55. package/docs/api/reference/terrain.md +2 -1
  56. package/docs/api/reference/textures.md +4 -4
  57. package/docs/api/reference/ui.md +3 -3
  58. package/docs/api/reference/vertex-draws.md +3 -3
  59. package/docs/api/reference/water.md +77 -6
  60. package/docs/api/reference/wind.md +2 -2
  61. package/docs/api/render.md +12 -10
  62. package/docs/api/scene.md +40 -29
  63. package/docs/api/sketch.md +64 -3
  64. package/docs/api/sound.md +169 -0
  65. package/docs/api/splats.md +7 -0
  66. package/docs/api/sprites.md +4 -20
  67. package/docs/api/tasks.md +2 -2
  68. package/docs/api/terrain.md +2 -14
  69. package/docs/api/textures.md +9 -9
  70. package/docs/api/ui.md +3 -15
  71. package/docs/api/water.md +198 -15
  72. package/docs/api/wind.md +5 -5
  73. package/docs/cli/null3d.md +93 -18
  74. package/docs/concepts/architecture.md +10 -10
  75. package/docs/concepts/assets.md +2 -19
  76. package/docs/concepts/backends.md +7 -5
  77. package/docs/concepts/color-management.md +15 -26
  78. package/docs/concepts/culling.md +1 -1
  79. package/docs/concepts/instances.md +5 -5
  80. package/docs/concepts/large-worlds.md +1 -1
  81. package/docs/concepts/lighting.md +50 -55
  82. package/docs/concepts/lod.md +2 -15
  83. package/docs/concepts/materials.md +16 -16
  84. package/docs/concepts/post-processing.md +7 -29
  85. package/docs/concepts/quality-preset-tables.md +53 -50
  86. package/docs/concepts/quality-presets.md +56 -27
  87. package/docs/concepts/render-graph.md +19 -14
  88. package/docs/concepts/render-layers.md +3 -17
  89. package/docs/concepts/shadows.md +10 -28
  90. package/docs/concepts/static-dynamic.md +2 -2
  91. package/docs/cookbook/index.md +1 -1
  92. package/docs/data/threejs-mapping.json +136 -102
  93. package/docs/errors/E1214.md +3 -3
  94. package/docs/errors/E1307.md +23 -0
  95. package/docs/errors/E1420.md +3 -3
  96. package/docs/errors/index.md +3 -2
  97. package/docs/getting-started/cdn.md +17 -1
  98. package/docs/getting-started/first-scene.md +1 -1
  99. package/docs/getting-started/hosting.md +50 -9
  100. package/docs/getting-started/install.md +8 -7
  101. package/docs/getting-started/network-use.md +10 -2
  102. package/docs/getting-started/project-structure.md +15 -6
  103. package/docs/getting-started/third-party-notices.md +8 -6
  104. package/docs/guides/accessibility.md +1 -1
  105. package/docs/guides/add-ons.md +3 -2
  106. package/docs/guides/agents.md +16 -5
  107. package/docs/guides/assets-pipeline.md +13 -12
  108. package/docs/guides/audio.md +109 -124
  109. package/docs/guides/car-paint.md +1 -1
  110. package/docs/guides/content-pages.md +10 -6
  111. package/docs/guides/custom-passes.md +9 -8
  112. package/docs/guides/custom-shaders.md +6 -3
  113. package/docs/guides/debugging.md +78 -4
  114. package/docs/guides/deploying.md +105 -5
  115. package/docs/guides/deterministic-simulation.md +1 -1
  116. package/docs/guides/first-use-shaders.md +4 -4
  117. package/docs/guides/loading-screens.md +78 -33
  118. package/docs/guides/multiple-views.md +41 -2
  119. package/docs/guides/performance.md +6 -3
  120. package/docs/guides/phones.md +12 -1
  121. package/docs/guides/physics.md +1 -1
  122. package/docs/guides/screens.md +7 -5
  123. package/docs/guides/start-time.md +328 -0
  124. package/docs/guides/testing.md +23 -5
  125. package/docs/guides/ui-overlays.md +3 -3
  126. package/docs/index.md +12 -8
  127. package/docs/pages.md +26 -23
  128. package/docs/porting/react-three-fiber.md +228 -5
  129. package/docs/porting/threejs-loop-and-threads.md +220 -6
  130. package/docs/porting/threejs-mapping.md +86 -84
  131. package/docs/porting/threejs-materials.md +212 -5
  132. package/docs/porting/threejs-overview.md +218 -5
  133. package/docs/porting/threejs-postprocessing.md +2 -2
  134. package/docs/porting/threejs-shaders.md +298 -5
  135. package/docs/porting/threejs-unsupported.md +82 -6
  136. package/docs/porting/verification.md +181 -5
  137. package/docs/shaders/builtins.md +1 -1
  138. package/docs/shaders/library.md +63 -39
  139. package/docs/shaders/surface-functions.md +11 -10
  140. package/docs/shaders/wgsl-rules.md +1 -1
  141. package/lib/debug/channel.d.ts +103 -0
  142. package/lib/debug/channel.js +269 -0
  143. package/lib/debug/debug.d.ts +9 -7
  144. package/lib/debug/draw.d.ts +9 -0
  145. package/lib/debug/draw.js +49 -2
  146. package/lib/debug/inspector.d.ts +60 -0
  147. package/lib/debug/inspector.js +475 -0
  148. package/lib/debug/stats-panel.d.ts +1 -1
  149. package/lib/debug/stats.d.ts +2 -3
  150. package/lib/errors/codes.js +8 -2
  151. package/lib/errors/engine-error.js +25 -10
  152. package/lib/errors/fixes.d.ts +1 -0
  153. package/lib/errors/fixes.js +3 -2
  154. package/lib/generated/core.d.ts +1 -1
  155. package/lib/generated/core.js +1 -1
  156. package/lib/gpu/early-builds.d.ts +68 -0
  157. package/lib/gpu/early-builds.js +158 -0
  158. package/lib/gpu/pipeline-name.d.ts +6 -0
  159. package/lib/gpu/pipeline-name.js +23 -0
  160. package/lib/gpu/webgl2/backend.d.ts +63 -2
  161. package/lib/gpu/webgl2/backend.js +226 -8
  162. package/lib/gpu/webgl2/programs.d.ts +18 -0
  163. package/lib/gpu/webgl2/programs.js +4 -1
  164. package/lib/gpu/webgpu/backend.d.ts +89 -2
  165. package/lib/gpu/webgpu/backend.js +255 -8
  166. package/lib/gpu/webgpu/pipelines.d.ts +13 -0
  167. package/lib/gpu/webgpu/pipelines.js +15 -1
  168. package/lib/index.d.ts +6 -4
  169. package/lib/index.js +3 -2
  170. package/lib/math/color.d.ts +5 -5
  171. package/lib/math/color.js +5 -5
  172. package/lib/math/mat4.d.ts +6 -12
  173. package/lib/math/mat4.js +6 -12
  174. package/lib/math/namespaces/quat.d.ts +3 -3
  175. package/lib/math/namespaces/quat.js +3 -3
  176. package/lib/math/quat.d.ts +8 -12
  177. package/lib/math/quat.js +8 -12
  178. package/lib/math/random.d.ts +2 -3
  179. package/lib/math/random.js +2 -3
  180. package/lib/math/types.d.ts +4 -4
  181. package/lib/math/vec3.d.ts +3 -6
  182. package/lib/math/vec3.js +3 -6
  183. package/lib/page/capabilities.d.ts +7 -7
  184. package/lib/page/capabilities.js +39 -25
  185. package/lib/page/check-store.d.ts +14 -2
  186. package/lib/page/check-store.js +34 -11
  187. package/lib/page/engine.d.ts +103 -26
  188. package/lib/page/engine.js +420 -97
  189. package/lib/page/first-frame.d.ts +16 -0
  190. package/lib/page/first-frame.js +35 -0
  191. package/lib/page/frame-stats.d.ts +34 -1
  192. package/lib/page/frame-stats.js +15 -0
  193. package/lib/page/label-loop.d.ts +7 -3
  194. package/lib/page/label-loop.js +17 -10
  195. package/lib/page/labels.d.ts +3 -3
  196. package/lib/page/pipeline-store.d.ts +25 -0
  197. package/lib/page/pipeline-store.js +84 -0
  198. package/lib/page/start-marker.d.ts +4 -1
  199. package/lib/page/start-marker.js +5 -2
  200. package/lib/page/switches.d.ts +32 -2
  201. package/lib/page/switches.js +9 -1
  202. package/lib/quality/check.d.ts +24 -3
  203. package/lib/quality/check.js +21 -7
  204. package/lib/quality/chooser.d.ts +6 -0
  205. package/lib/quality/chooser.js +8 -0
  206. package/lib/quality/governor.d.ts +98 -6
  207. package/lib/quality/governor.js +250 -20
  208. package/lib/quality/preset-docs.d.ts +16 -1
  209. package/lib/quality/preset-docs.js +3 -0
  210. package/lib/quality/presets.d.ts +84 -7
  211. package/lib/quality/presets.js +94 -2
  212. package/lib/render/draw.d.ts +3 -2
  213. package/lib/render/draw.js +15 -5
  214. package/lib/render/load-draw.d.ts +7 -5
  215. package/lib/render/load-draw.js +7 -5
  216. package/lib/render/loss.d.ts +28 -10
  217. package/lib/render/loss.js +50 -22
  218. package/lib/render/recovery.d.ts +20 -0
  219. package/lib/render/recovery.js +44 -4
  220. package/lib/render/renderer.d.ts +85 -9
  221. package/lib/render/renderer.js +181 -42
  222. package/lib/render/scene-renderer.d.ts +19 -1
  223. package/lib/render/scene-renderer.js +25 -1
  224. package/lib/render/webgl2-renderers.js +8 -5
  225. package/lib/render/webgpu-renderers.js +5 -4
  226. package/lib/scene/animation.d.ts +28 -32
  227. package/lib/scene/animation.js +12 -15
  228. package/lib/scene/assets.d.ts +61 -86
  229. package/lib/scene/assets.js +103 -236
  230. package/lib/scene/background.d.ts +14 -18
  231. package/lib/scene/background.js +5 -2
  232. package/lib/scene/color.d.ts +3 -3
  233. package/lib/scene/downloads.d.ts +2 -0
  234. package/lib/scene/downloads.js +371 -0
  235. package/lib/scene/environment.d.ts +7 -7
  236. package/lib/scene/environment.js +1 -1
  237. package/lib/scene/fake-call-values.js +1 -1
  238. package/lib/scene/fog.d.ts +11 -4
  239. package/lib/scene/fog.js +8 -2
  240. package/lib/scene/frame-cameras.d.ts +19 -0
  241. package/lib/scene/frame-cameras.js +58 -0
  242. package/lib/scene/gltf.d.ts +32 -2
  243. package/lib/scene/gltf.js +78 -49
  244. package/lib/scene/grading.d.ts +2 -2
  245. package/lib/scene/grading.js +3 -3
  246. package/lib/scene/ktx2.d.ts +9 -1
  247. package/lib/scene/ktx2.js +3 -2
  248. package/lib/scene/level-rule.d.ts +120 -0
  249. package/lib/scene/level-rule.js +130 -0
  250. package/lib/scene/lines.d.ts +13 -17
  251. package/lib/scene/lines.js +3 -3
  252. package/lib/scene/lut.d.ts +1 -2
  253. package/lib/scene/lut.js +1 -2
  254. package/lib/scene/picks.d.ts +2 -2
  255. package/lib/scene/pointer-events.d.ts +4 -7
  256. package/lib/scene/post.d.ts +38 -43
  257. package/lib/scene/post.js +4 -1
  258. package/lib/scene/prefab.d.ts +7 -8
  259. package/lib/scene/prefab.js +7 -8
  260. package/lib/scene/property-animation.d.ts +1 -1
  261. package/lib/scene/property-animation.js +1 -1
  262. package/lib/scene/queries.d.ts +7 -13
  263. package/lib/scene/resources.d.ts +225 -254
  264. package/lib/scene/resources.js +179 -81
  265. package/lib/scene/scene.d.ts +149 -118
  266. package/lib/scene/scene.js +196 -98
  267. package/lib/scene/serial-loads.d.ts +29 -0
  268. package/lib/scene/serial-loads.js +68 -0
  269. package/lib/scene/shader-preloads.d.ts +34 -0
  270. package/lib/scene/shader-preloads.js +38 -0
  271. package/lib/scene/sprites.d.ts +14 -17
  272. package/lib/scene/sprites.js +5 -6
  273. package/lib/scene/textures.d.ts +11 -12
  274. package/lib/scene/textures.js +9 -10
  275. package/lib/scene/time-of-day.d.ts +3 -4
  276. package/lib/scene/time-of-day.js +2 -3
  277. package/lib/scene/vertex-draws.d.ts +5 -4
  278. package/lib/scene/vertex-draws.js +7 -5
  279. package/lib/shader-artifact.json +1 -0
  280. package/lib/shared/core.d.ts +5 -0
  281. package/lib/shared/core.js +1 -0
  282. package/lib/shared/import-once.d.ts +6 -0
  283. package/lib/shared/import-once.js +19 -0
  284. package/lib/shared/metrics.d.ts +38 -0
  285. package/lib/shared/metrics.js +62 -1
  286. package/lib/shared/run-task.js +7 -1
  287. package/lib/shared/task-pending.d.ts +12 -0
  288. package/lib/shared/task-pending.js +61 -1
  289. package/lib/shared/tasks.js +79 -32
  290. package/lib/shared/version.d.ts +17 -1
  291. package/lib/shared/version.js +19 -1
  292. package/lib/shared/wasm.d.ts +12 -4
  293. package/lib/shared/wasm.js +50 -27
  294. package/lib/shared/worker-start.d.ts +7 -0
  295. package/lib/shared/worker-start.js +27 -0
  296. package/lib/sketch/define-sketch.d.ts +35 -0
  297. package/lib/sketch/input.d.ts +2 -2
  298. package/lib/sketch/preset-check.d.ts +20 -6
  299. package/lib/sketch/preset-check.js +42 -17
  300. package/lib/sketch/quality.d.ts +69 -11
  301. package/lib/sketch/quality.js +54 -13
  302. package/lib/sketch/runner.d.ts +76 -9
  303. package/lib/sketch/runner.js +385 -52
  304. package/lib/sketch/ui.d.ts +5 -8
  305. package/lib/workers/probe-worker.d.ts +5 -0
  306. package/lib/workers/probe-worker.js +13 -4
  307. package/lib/workers/protocol.d.ts +61 -13
  308. package/lib/workers/protocol.js +4 -0
  309. package/lib/workers/render-worker.js +25 -4
  310. package/lib/workers/sketch-worker.js +104 -15
  311. package/package.json +2 -2
@@ -3,12 +3,9 @@ import type { Object3D } from '../scene/scene.js';
3
3
  /**
4
4
  * How a label's element sits in the scene.
5
5
  *
6
- * - `'screen'` keeps the element flat on the screen at the label's place, always the same size, as
7
- * three.js's `CSS2DRenderer` does.
8
- * - `'world'` makes the element a plane of the scene: it turns and scales with the object, as
9
- * three.js's `CSS3DObject` does.
10
- * - `'billboard'` makes the element a plane of the scene that always faces the camera, as
11
- * three.js's `CSS3DSprite` does.
6
+ * - `'screen'` keeps the element flat on the screen at the label's place, always the same size.
7
+ * - `'world'` makes the element a plane of the scene: it turns and scales with the object.
8
+ * - `'billboard'` makes the element a plane of the scene that always faces the camera.
12
9
  *
13
10
  * @category api/ui
14
11
  */
@@ -27,8 +24,8 @@ export interface LabelOptions {
27
24
  /** How the element sits in the scene. The default is `'screen'`. */
28
25
  transform?: LabelTransform;
29
26
  /**
30
- * The element's turn in its own plane, in radians, for `'billboard'`. It turns the content of the
31
- * element, as three.js's `CSS3DSprite` `rotation2D` does.
27
+ * The element's turn in its own plane, in radians, for `'billboard'`. It turns the element's
28
+ * content counterclockwise, while the element keeps facing the camera.
32
29
  */
33
30
  rotation?: number;
34
31
  }
@@ -1,4 +1,5 @@
1
1
  import { type DrawPathReason } from '../gpu/webgpu/draw-path-probe.js';
2
+ import type { PowerPreference } from '../page/capabilities.js';
2
3
  /**
3
4
  * What a dedicated worker can do, in `CapabilityReport.worker`. A render worker needs the frame
4
5
  * timer and an offscreen canvas for its GPU path.
@@ -27,3 +28,7 @@ export interface WorkerProbe {
27
28
  * for the script without a time limit, so a slow download never reads as a worker that cannot draw.
28
29
  */
29
30
  export type ProbeMessage = 'loaded' | WorkerProbe;
31
+ /** What the page sends the probe worker: the power preference that picks the GPU to test. */
32
+ export interface ProbeRequest {
33
+ powerPreference?: PowerPreference;
34
+ }
@@ -6,7 +6,11 @@ import { messageOf } from '../errors/message.js';
6
6
  import { probeDrawPath } from '../gpu/webgpu/draw-path-probe.js';
7
7
  import { needsOwnArguments } from '../gpu/webgpu/indirect-arguments.js';
8
8
  const post = (message) => postMessage(message);
9
- async function probe() {
9
+ /**
10
+ * Tests the worker's GPU paths on the GPU that `powerPreference` picks, the one that the engine will
11
+ * draw with.
12
+ */
13
+ async function probe(powerPreference) {
10
14
  const result = {
11
15
  requestAnimationFrame: typeof globalThis.requestAnimationFrame ===
12
16
  'function',
@@ -15,8 +19,10 @@ async function probe() {
15
19
  directDraws: false,
16
20
  drawPath: 'no-probe',
17
21
  };
22
+ // The adapter request runs while the WebGL2 test, which blocks this thread, does.
23
+ const adapterRequest = (async () => navigator.gpu?.requestAdapter({ featureLevel: 'compatibility', powerPreference }))();
18
24
  try {
19
- const gl = new OffscreenCanvas(1, 1).getContext('webgl2');
25
+ const gl = new OffscreenCanvas(1, 1).getContext('webgl2', { powerPreference });
20
26
  result.offscreenWebGL2 = gl !== null;
21
27
  // A browser holds only a few WebGL contexts at once, and frees this one when it collects it.
22
28
  gl?.getExtension('WEBGL_lose_context')?.loseContext();
@@ -25,7 +31,7 @@ async function probe() {
25
31
  result.offscreenWebGL2 = false;
26
32
  }
27
33
  try {
28
- const adapter = await navigator.gpu?.requestAdapter({ featureLevel: 'compatibility' });
34
+ const adapter = await adapterRequest;
29
35
  if (adapter) {
30
36
  const device = await adapter.requestDevice();
31
37
  const context = new OffscreenCanvas(1, 1).getContext('webgpu');
@@ -46,4 +52,7 @@ async function probe() {
46
52
  return result;
47
53
  }
48
54
  post('loaded');
49
- probe().then(post);
55
+ self.onmessage = ({ data }) => {
56
+ self.onmessage = null;
57
+ probe(data.powerPreference).then(post);
58
+ };
@@ -1,3 +1,4 @@
1
+ import type { InspectReply } from '../debug/channel.js';
1
2
  import type { StatsRequest } from '../debug/stats-options.js';
2
3
  import type { PowerPreference } from '../page/capabilities.js';
3
4
  import type { EngineCapabilities } from '../page/engine.js';
@@ -5,7 +6,8 @@ import type { HoldScript, HoldScriptResult } from '../page/hold.js';
5
6
  import type { CoreDevice } from '../page/limits.js';
6
7
  import type { GlTimingMode } from '../page/switches.js';
7
8
  import type { FramePacing } from '../render/loop.js';
8
- import type { Tier } from '../render/renderer.js';
9
+ import type { GpuErrorKind } from '../render/loss.js';
10
+ import type { DrawingPreload, Tier } from '../render/renderer.js';
9
11
  import { type Build, type StartedCore } from '../shared/core.js';
10
12
  import type { ReplayProgram, ReplayResult } from '../shared/recording.js';
11
13
  import type { WAKE } from '../shared/wake.js';
@@ -65,6 +67,16 @@ export interface RendererSetup extends FramePacing {
65
67
  * thread that draws loads the tier's shaders with the first generator that it runs.
66
68
  */
67
69
  cloudSteps?: number;
70
+ /**
71
+ * The pipeline list of the first frame of the sketch's last start, whose pipelines the thread
72
+ * starts to build at once (`gpu/early-builds.ts`).
73
+ */
74
+ earlyBuilds?: Uint32Array;
75
+ /**
76
+ * True when the page stores pipeline lists: the thread sends the list of its first frame's
77
+ * pipelines once that frame has drawn.
78
+ */
79
+ keepBuilds?: boolean;
68
80
  }
69
81
  export type SketchWorkerInit = CoreHandoff & {
70
82
  type: 'init';
@@ -125,15 +137,20 @@ export type JobWorkerInit = CoreHandoff & {
125
137
  };
126
138
  /**
127
139
  * A request any worker that owns a renderer takes: a capture, which it answers with the frame's
128
- * pixels, or with a PNG file of the frame when `image` is true; a simulated loss; a stop, which it
129
- * answers once it has destroyed its GPU objects and its device; or a park after the stop, which lets
130
- * go of the engine's core and keeps the canvas for the next engine.
140
+ * pixels, or with a PNG file of the frame when `image` is true; a simulated loss; the page's entry
141
+ * into the browser's back/forward cache, when `cached` is true, which gives the GPU back, and its
142
+ * return from it, which takes a new device; a stop, which it answers once it has destroyed its GPU
143
+ * objects and its device; or a park after the stop, which lets go of the engine's core and keeps
144
+ * the canvas for the next engine.
131
145
  */
132
146
  export type RendererRequest = {
133
147
  type: 'capture';
134
148
  image?: boolean;
135
149
  } | {
136
150
  type: 'lose-gpu';
151
+ } | {
152
+ type: 'page-cache';
153
+ cached: boolean;
137
154
  } | {
138
155
  type: 'stop-drawing';
139
156
  } | {
@@ -145,14 +162,12 @@ export type RendererRequest = {
145
162
  };
146
163
  /**
147
164
  * Sent to the worker that draws as soon as the probe has chosen the GPU path, before the core
148
- * arrives: start the downloads of that path's renderers and of the device's shaders for that path
149
- * and the fixed bits `bits`.
165
+ * arrives: start the downloads of that path's renderers, of the device's shaders and of the
166
+ * features that the start preloads, and on WebGPU the request for the device.
150
167
  */
151
- export interface ShaderPreload {
168
+ export type ShaderPreload = DrawingPreload & {
152
169
  type: 'load-shaders';
153
- tier: Tier;
154
- bits: number;
155
- }
170
+ };
156
171
  export type WorkerReply = {
157
172
  type: 'ready';
158
173
  role: 'sketch' | 'render' | 'job';
@@ -185,11 +200,14 @@ export type WorkerReply = {
185
200
  index?: number;
186
201
  message: string;
187
202
  }
188
- /** The GPU of the thread that draws ran out of memory or rejected a command; the engine draws on. */
203
+ /**
204
+ * The GPU of the thread that draws ran out of memory, rejected a command, or its driver could not
205
+ * build a pipeline of the scene's objects; the engine draws on.
206
+ */
189
207
  | {
190
208
  type: 'gpu-error';
191
209
  role: 'sketch' | 'render';
192
- outOfMemory: boolean;
210
+ kind: GpuErrorKind;
193
211
  message: string;
194
212
  }
195
213
  /** A job worker left the job system after the engine stopped, so it no longer blocks. */
@@ -212,6 +230,11 @@ export type WorkerReply = {
212
230
  role: 'sketch' | 'render' | 'job';
213
231
  step: string;
214
232
  }
233
+ /** The pipeline list of the pipelines that the first frame built, which the page stores. */
234
+ | {
235
+ type: 'pipeline-list';
236
+ list: Uint32Array;
237
+ }
215
238
  /** The browser took the GPU away from the worker that draws, which stopped drawing. */
216
239
  | {
217
240
  type: 'lost';
@@ -273,7 +296,12 @@ export type WorkerReply = {
273
296
  | {
274
297
  type: 'recording';
275
298
  bytes: Uint8Array;
276
- } | ({
299
+ }
300
+ /** The answer to a development build's inspection call: its value, or why it failed. */
301
+ | ({
302
+ type: 'inspect-reply';
303
+ id: number;
304
+ } & InspectReply) | ({
277
305
  type: 'captured';
278
306
  } & CapturedFrame) | {
279
307
  type: 'captured-image';
@@ -286,11 +314,29 @@ export type SketchWorkerMessage = SketchWorkerInit
286
314
  /** Sent before the core in low-latency mode, where the sketch worker draws: load the renderer. */
287
315
  | {
288
316
  type: 'load-renderer';
317
+ }
318
+ /**
319
+ * Sent as the worker starts, for a live sketch: import the sketch module at `url` now, while the
320
+ * core downloads, and start the files that its options list, whose addresses resolve against
321
+ * `pageUrl`. Hold mode, a replay and a recording send none, since their sketch module loads only
322
+ * after the runner has seeded the thread's random numbers.
323
+ */
324
+ | {
325
+ type: 'load-sketch';
326
+ url: string;
327
+ pageUrl: string;
289
328
  } | ShaderPreload | RendererRequest | {
290
329
  type: 'post';
291
330
  name: string;
292
331
  data: unknown;
293
332
  }
333
+ /** A development build's inspection call, which the worker answers with `inspect-reply`. */
334
+ | {
335
+ type: 'inspect';
336
+ id: number;
337
+ call: string;
338
+ args?: unknown;
339
+ }
294
340
  /** Asks the sketch worker to save the recording of the session so far. */
295
341
  | {
296
342
  type: 'save-recording';
@@ -312,6 +358,8 @@ export type SketchWorkerMessage = SketchWorkerInit
312
358
  | typeof WAKE;
313
359
  /** Sends a reply from a worker to the page, moving the `transfer` objects instead of copying them. */
314
360
  export declare function replyToPage(message: WorkerReply, transfer?: Transferable[]): void;
361
+ /** Sends the page the pipeline list of the first frame, which the page stores for the next start. */
362
+ export declare function sendPipelineList(list: Uint32Array): void;
315
363
  /**
316
364
  * Sends the page a captured frame, as pixels or as an image file, once `capture` resolves, or the
317
365
  * reason it failed.
@@ -10,6 +10,10 @@ export function startWorkerCore(handoff, step) {
10
10
  export function replyToPage(message, transfer = []) {
11
11
  postMessage(message, { transfer });
12
12
  }
13
+ /** Sends the page the pipeline list of the first frame, which the page stores for the next start. */
14
+ export function sendPipelineList(list) {
15
+ replyToPage({ type: 'pipeline-list', list }, [list.buffer]);
16
+ }
13
17
  /**
14
18
  * Sends the page a captured frame, as pixels or as an image file, once `capture` resolves, or the
15
19
  * reason it failed.
@@ -5,24 +5,34 @@
5
5
  // the engine's memory, and the next engine on the same canvas starts it again with its own.
6
6
  import { messageOf } from '../errors/message.js';
7
7
  import { recordPermutations, stopRecordingPermutations } from '../gpu/variants.js';
8
- import { captureFrame, captureImage, preloadDeviceFiles, startDrawing } from '../render/draw.js';
8
+ import { captureFrame, captureImage, startDrawing, startEarly } from '../render/draw.js';
9
9
  import { controlViews } from '../shared/control.js';
10
10
  import { setWakeByMessage } from '../shared/wake.js';
11
11
  import { DrawingHost } from './drawing-host.js';
12
- import { replyToPage, replyWithCapture, startSteps, startWorker, } from './protocol.js';
12
+ import { replyToPage, replyWithCapture, sendPipelineList, startSteps, startWorker, } from './protocol.js';
13
13
  let host = new DrawingHost();
14
14
  let controlSlots;
15
15
  /** The canvas that the first engine moved here, which every later engine draws on. */
16
16
  let canvas;
17
17
  /** The running engine's image port, which a park lets go of. */
18
18
  let imagePort;
19
+ /** What the worker started for the next engine's renderer, until that engine's setup arrives. */
20
+ let early;
21
+ /** Destroys the device of an early start that no renderer took. */
22
+ function releaseEarly() {
23
+ early?.release();
24
+ early = undefined;
25
+ }
19
26
  const step = startSteps('render');
20
27
  startWorker('render', step, async (event) => {
21
28
  const message = event.data;
22
29
  if (message.type === 'load-shaders') {
23
- preloadDeviceFiles(message.tier, message.bits);
30
+ releaseEarly();
31
+ early = startEarly(message);
24
32
  }
25
33
  else if (message.type === 'init') {
34
+ const started = early;
35
+ early = undefined;
26
36
  try {
27
37
  canvas = message.canvas ?? canvas;
28
38
  if (!canvas)
@@ -38,10 +48,12 @@ startWorker('render', step, async (event) => {
38
48
  const drawing = await host.start(startDrawing({
39
49
  ...message,
40
50
  canvas,
51
+ early: started,
41
52
  scene: message.memory && { memory: message.memory, control: message.control },
42
53
  fail: (reason) => replyToPage({ type: 'lost', role: 'render', reason }),
43
54
  fault: (error) => replyToPage({ type: 'fault', role: 'render', message: messageOf(error) }),
44
- gpuError: (outOfMemory, text) => replyToPage({ type: 'gpu-error', role: 'render', outOfMemory, message: text }),
55
+ gpuError: (kind, text) => replyToPage({ type: 'gpu-error', role: 'render', kind, message: text }),
56
+ listBuilds: message.keepBuilds ? sendPipelineList : undefined,
45
57
  }));
46
58
  if (!drawing)
47
59
  return;
@@ -53,6 +65,7 @@ startWorker('render', step, async (event) => {
53
65
  });
54
66
  }
55
67
  catch (e) {
68
+ started?.release();
56
69
  await host.release();
57
70
  replyToPage({
58
71
  type: 'error',
@@ -71,10 +84,18 @@ startWorker('render', step, async (event) => {
71
84
  else if (message.type === 'lose-gpu') {
72
85
  host.drawing?.simulateLoss();
73
86
  }
87
+ else if (message.type === 'page-cache') {
88
+ if (message.cached)
89
+ host.drawing?.enterPageCache();
90
+ else
91
+ await host.drawing?.leavePageCache();
92
+ }
74
93
  else if (message.type === 'stop-drawing') {
94
+ releaseEarly();
75
95
  await host.stop('render');
76
96
  }
77
97
  else if (message.type === 'park') {
98
+ releaseEarly();
78
99
  imagePort?.close();
79
100
  imagePort = undefined;
80
101
  controlSlots = undefined;
@@ -1,28 +1,32 @@
1
1
  // The sketch worker: runs the sketch's code and the engine core. In pipelined mode it computes frame
2
2
  // N+1 while the render worker draws frame N, and waits for the render worker's signal without
3
3
  // blocking, so its event loop stays alive for promises and messages. In low-latency mode it
4
- // also owns the canvas and draws each frame itself; only then does it load the renderer. When the
5
- // engine stops, the sketch's onDestroy runs here. A worker that drew keeps the canvas, which cannot
6
- // go back to the page: it frees its GPU device and lets go of the engine's core, and the next
7
- // engine on the same canvas starts it again.
4
+ // also owns the canvas and draws each frame itself; only then does it load the renderer. A live
5
+ // sketch's module loads as soon as the page names it, while the core downloads, so the module's own
6
+ // imports and its top-level code do not wait for the core. When the engine stops, the sketch's
7
+ // onDestroy runs here. A worker that drew keeps the canvas, which cannot go back to the page: it
8
+ // frees its GPU device and lets go of the engine's core, and the next engine on the same canvas
9
+ // starts it again.
8
10
  import { DEV } from '../errors/checks.js';
9
11
  import { messageOf } from '../errors/message.js';
10
12
  import { recordPermutations, stopRecordingPermutations } from '../gpu/variants.js';
11
- import { loadDrawModule, preloadDeviceFiles } from '../render/load-draw.js';
13
+ import { loadDrawModule, preloadDrawing } from '../render/load-draw.js';
12
14
  import { awaitLater } from '../shared/await-later.js';
13
15
  import { controlViews } from '../shared/control.js';
14
16
  import { drawingSenders, ImageTable } from '../shared/images.js';
15
17
  import { clearJobTasks, jobAsker, setJobTasks } from '../shared/task-host.js';
16
- import { onAddonChecked } from '../shared/version.js';
18
+ import { hearAddonsEarly, onAddonChecked } from '../shared/version.js';
17
19
  import { setWakeByMessage, wakeWaiters } from '../shared/wake.js';
18
20
  import { loadSketch } from '../sketch/define-sketch.js';
19
- import { runPipelined, SketchRunner } from '../sketch/runner.js';
21
+ import { runPipelined, SketchRunner, startListedFiles } from '../sketch/runner.js';
20
22
  import { DrawingHost } from './drawing-host.js';
21
- import { replyToPage, replyWithCapture, startSteps, startWorker, startWorkerCore, } from './protocol.js';
23
+ import { replyToPage, replyWithCapture, sendPipelineList, startSteps, startWorker, startWorkerCore, } from './protocol.js';
22
24
  let runner;
23
25
  /** The renderer, which this worker loads only in low-latency mode, where it draws. */
24
26
  let drawLoad;
25
27
  let draw;
28
+ /** What the worker started for its renderer in low-latency mode, until the renderer takes it. */
29
+ let early;
26
30
  let host = new DrawingHost();
27
31
  let controlSlots;
28
32
  /** The canvas that the first engine moved here in low-latency mode, which later engines draw on. */
@@ -30,6 +34,13 @@ let canvas;
30
34
  let core;
31
35
  /** The job workers' task ports that this worker's on-demand loader uses while the engine runs. */
32
36
  let jobTaskHost;
37
+ /**
38
+ * The import of a live sketch's module that started before the core arrived, with its address, and
39
+ * the downloads of the files that its options list.
40
+ */
41
+ let earlySketch;
42
+ /** In a development build, the add-ons that the sketch's module loaded before the runner existed. */
43
+ let earlyAddons;
33
44
  const step = startSteps('sketch');
34
45
  startWorker('sketch', step, async (event) => {
35
46
  const message = event.data;
@@ -43,9 +54,35 @@ startWorker('sketch', step, async (event) => {
43
54
  }
44
55
  else if (message.type === 'load-shaders') {
45
56
  drawLoad ??= loadDrawModule();
46
- preloadDeviceFiles(drawLoad, message.tier, message.bits);
57
+ releaseEarly();
58
+ early = preloadDrawing(drawLoad, message);
59
+ }
60
+ else if (message.type === 'load-sketch') {
61
+ // A development build hears each add-on that the module loads from before its import begins,
62
+ // and hands them to the runner once it exists.
63
+ if (DEV) {
64
+ endEarlyAddons();
65
+ earlyAddons = hearAddonsEarly();
66
+ }
67
+ earlySketch = {
68
+ url: message.url,
69
+ sketch: awaitLater(loadSketch(message.url, { step })),
70
+ listed: {},
71
+ };
72
+ step('sketch import started');
73
+ // The files that the sketch lists start downloading as soon as its module has loaded.
74
+ const { sketch, listed } = earlySketch;
75
+ sketch.then((loaded) => startListedFiles(listed, loaded, message.pageUrl), () => { });
47
76
  }
48
77
  else if (message.type === 'init') {
78
+ // Only a live start takes the module that loaded early: hold mode, a replay and a recording
79
+ // load it once the runner has seeded the thread's random numbers.
80
+ const live = message.hold === undefined && message.replay === undefined && !message.record;
81
+ const taken = live && earlySketch?.url === message.sketchUrl ? earlySketch : undefined;
82
+ const loadedSketch = taken?.sketch;
83
+ earlySketch = undefined;
84
+ if (DEV && !loadedSketch)
85
+ endEarlyAddons();
49
86
  try {
50
87
  // The renderer loads while the core and the sketch start, if the page did not ask for it
51
88
  // sooner.
@@ -96,13 +133,24 @@ startWorker('sketch', step, async (event) => {
96
133
  wantJobs,
97
134
  sendLabelSlot: (id, table, slot, generation) => replyToPage({ type: 'label', id, table, slot, generation }),
98
135
  reportSketchError: (message) => replyToPage({ type: 'sketch-error', message }),
99
- // The worker exists before its sketch's module loads, so this hears the module's imports.
100
- ...(DEV && { hearAddons: onAddonChecked }),
136
+ listed: taken?.listed,
137
+ // What the sketch's module loaded before the runner existed, then each later load.
138
+ ...(DEV && {
139
+ hearAddons: (heard) => {
140
+ const before = earlyAddons;
141
+ earlyAddons = undefined;
142
+ return before?.hand(heard) ?? onAddonChecked(heard);
143
+ },
144
+ }),
101
145
  }, message.hold, message.holdScript, message.replay, message.record, message.debugView);
146
+ // A runner that hears no add-ons leaves the early hearing to end here.
147
+ if (DEV)
148
+ endEarlyAddons();
102
149
  step('engine created');
103
- // The sketch downloads while the renderer starts. The renderer starts before the setup,
104
- // so a warm-up in the setup has a renderer to build its pipelines.
105
- const sketch = awaitLater(loadSketch(message.sketchUrl, { step }));
150
+ // The sketch downloads while the renderer starts, unless it loaded while the core did. The
151
+ // renderer starts before the setup, so a warm-up in the setup has a renderer to build its
152
+ // pipelines.
153
+ const sketch = loadedSketch ?? awaitLater(loadSketch(message.sketchUrl, { step }));
106
154
  /** The GPU path that this worker draws with, in low-latency mode. */
107
155
  let tier;
108
156
  if (message.renderer && drawModule) {
@@ -112,8 +160,11 @@ startWorker('sketch', step, async (event) => {
112
160
  if (message.renderer.recordPermutations)
113
161
  recordPermutations();
114
162
  step('renderer loaded');
163
+ const started = early;
164
+ early = undefined;
115
165
  const drawing = await host.start(draw.startDrawing({
116
166
  ...message.renderer,
167
+ early: await started,
117
168
  canvas: canvas,
118
169
  metrics: message.metrics,
119
170
  device: message.device,
@@ -123,7 +174,8 @@ startWorker('sketch', step, async (event) => {
123
174
  imageTable,
124
175
  fail: (reason) => replyToPage({ type: 'lost', role: 'sketch', reason }),
125
176
  fault,
126
- gpuError: (outOfMemory, text) => replyToPage({ type: 'gpu-error', role: 'sketch', outOfMemory, message: text }),
177
+ gpuError: (kind, text) => replyToPage({ type: 'gpu-error', role: 'sketch', kind, message: text }),
178
+ listBuilds: message.renderer.keepBuilds ? sendPipelineList : undefined,
127
179
  }));
128
180
  if (!drawing)
129
181
  return;
@@ -155,6 +207,9 @@ startWorker('sketch', step, async (event) => {
155
207
  });
156
208
  }
157
209
  catch (e) {
210
+ if (DEV)
211
+ endEarlyAddons();
212
+ releaseEarly();
158
213
  await host.release();
159
214
  replyToPage({
160
215
  type: 'error',
@@ -177,6 +232,22 @@ startWorker('sketch', step, async (event) => {
177
232
  else if (message.type === 'post') {
178
233
  runner?.receive(message.name, message.data);
179
234
  }
235
+ else if (message.type === 'inspect') {
236
+ // The inspection channel is a development build's: a release build answers that it has none.
237
+ try {
238
+ if (!DEV || runner?.inspect === undefined)
239
+ throw new Error('The inspector needs a development build. Run the Vite dev server, not a production build.');
240
+ replyToPage({
241
+ type: 'inspect-reply',
242
+ id: message.id,
243
+ ok: true,
244
+ result: await runner.inspect(message.call, message.args),
245
+ });
246
+ }
247
+ catch (e) {
248
+ replyToPage({ type: 'inspect-reply', id: message.id, ok: false, error: messageOf(e) });
249
+ }
250
+ }
180
251
  else if (message.type === 'capture' && draw && host.drawing && controlSlots) {
181
252
  const capture = message.image ? draw.captureImage : draw.captureFrame;
182
253
  await replyWithCapture(capture(host.drawing, controlSlots));
@@ -187,12 +258,20 @@ startWorker('sketch', step, async (event) => {
187
258
  else if (message.type === 'lose-gpu') {
188
259
  host.drawing?.simulateLoss();
189
260
  }
261
+ else if (message.type === 'page-cache') {
262
+ if (message.cached)
263
+ host.drawing?.enterPageCache();
264
+ else
265
+ await host.drawing?.leavePageCache();
266
+ }
190
267
  else if (message.type === 'stop-drawing') {
268
+ releaseEarly();
191
269
  runner?.dispose();
192
270
  runner = undefined;
193
271
  await host.stop('sketch');
194
272
  }
195
273
  else if (message.type === 'park') {
274
+ releaseEarly();
196
275
  controlSlots = undefined;
197
276
  clearJobTasks(jobTaskHost);
198
277
  jobTaskHost = undefined;
@@ -200,6 +279,16 @@ startWorker('sketch', step, async (event) => {
200
279
  core = undefined;
201
280
  }
202
281
  });
282
+ /** Ends a development build's hearing of add-ons for a runner that did not exist yet. */
283
+ function endEarlyAddons() {
284
+ earlyAddons?.stop();
285
+ earlyAddons = undefined;
286
+ }
287
+ /** Destroys the device of an early start that no renderer took. */
288
+ function releaseEarly() {
289
+ void early?.then((start) => start?.release());
290
+ early = undefined;
291
+ }
203
292
  /** Tells the page that the sketch's frame loop, or the drawing in low-latency mode, failed. */
204
293
  function fault(error) {
205
294
  replyToPage({ type: 'fault', role: 'sketch', message: messageOf(error) });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@null3d/engine",
3
- "version": "0.0.0",
4
- "description": "The null3D engine, a browser 3D engine for games and heavy 3D apps: the TypeScript API, the page shim, the worker entry points, both WebAssembly builds and the docs.",
3
+ "version": "0.1.0",
4
+ "description": "The null3D engine, a high-performance browser 3D engine for agents, faster than three.js, with wide device compatibility: the TypeScript API, the page shim, the worker entry points, both WebAssembly builds and the docs.",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "author": "Ramesh Nair (https://hiddentao.com)",
7
7
  "homepage": "https://github.com/null3d-engine/null3d#readme",