@driftengine/core 4.9.0 → 4.11.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 (231) hide show
  1. package/CHANGELOG.json +133 -11
  2. package/README.md +43 -38
  3. package/dist/index.d.ts +7 -4
  4. package/dist/index.js +5 -4
  5. package/dist/render/ambientHarmonics.d.ts +1 -1
  6. package/dist/render/ambientHarmonics.js +1 -1
  7. package/dist/render/areaLights.d.ts +4 -4
  8. package/dist/render/areaLights.js +1 -1
  9. package/dist/render/backend/api.d.ts +10 -3
  10. package/dist/render/backend/select.js +11 -5
  11. package/dist/render/backend/webgl2/boneAnimations.d.ts +29 -0
  12. package/dist/render/backend/webgl2/boneAnimations.js +80 -0
  13. package/dist/render/backend/webgl2/instanced.d.ts +5 -1
  14. package/dist/render/backend/webgl2/instanced.js +13 -1
  15. package/dist/render/backend/webgl2/renderer.d.ts +189 -4
  16. package/dist/render/backend/webgl2/renderer.js +614 -66
  17. package/dist/render/backend/webgl2/skinPaletteTexture.d.ts +5 -3
  18. package/dist/render/backend/webgl2/skinPaletteTexture.js +6 -4
  19. package/dist/render/backend/webgpu/boneAnimations.d.ts +34 -0
  20. package/dist/render/backend/webgpu/boneAnimations.js +76 -0
  21. package/dist/render/backend/webgpu/buffers.d.ts +24 -4
  22. package/dist/render/backend/webgpu/buffers.js +116 -59
  23. package/dist/render/backend/webgpu/clothKernels.d.ts +8 -0
  24. package/dist/render/backend/webgpu/clothKernels.js +76 -0
  25. package/dist/render/backend/webgpu/clothOps.d.ts +39 -0
  26. package/dist/render/backend/webgpu/clothOps.js +78 -0
  27. package/dist/render/backend/webgpu/clothSetLayout.d.ts +25 -0
  28. package/dist/render/backend/webgpu/clothSetLayout.js +84 -0
  29. package/dist/render/backend/webgpu/clothSetPack.d.ts +33 -0
  30. package/dist/render/backend/webgpu/clothSetPack.js +144 -0
  31. package/dist/render/backend/webgpu/compressedUpload.d.ts +0 -1
  32. package/dist/render/backend/webgpu/compressedUpload.js +53 -8
  33. package/dist/render/backend/webgpu/depthPass.d.ts +59 -3
  34. package/dist/render/backend/webgpu/depthPass.js +49 -14
  35. package/dist/render/backend/webgpu/drawCommand.d.ts +20 -4
  36. package/dist/render/backend/webgpu/drawCommand.js +11 -3
  37. package/dist/render/backend/webgpu/flatPass.d.ts +85 -42
  38. package/dist/render/backend/webgpu/flatPass.js +137 -20
  39. package/dist/render/backend/webgpu/gpuClothSet.d.ts +80 -0
  40. package/dist/render/backend/webgpu/gpuClothSet.js +462 -0
  41. package/dist/render/backend/webgpu/instanced.d.ts +14 -1
  42. package/dist/render/backend/webgpu/instanced.js +24 -1
  43. package/dist/render/backend/webgpu/latePass.js +1 -0
  44. package/dist/render/backend/webgpu/materialStore.d.ts +101 -0
  45. package/dist/render/backend/webgpu/materialStore.js +265 -0
  46. package/dist/render/backend/webgpu/motionPass.js +15 -6
  47. package/dist/render/backend/webgpu/pipelineCache.d.ts +28 -1
  48. package/dist/render/backend/webgpu/pipelineCache.js +46 -1
  49. package/dist/render/backend/webgpu/postPass.d.ts +1 -1
  50. package/dist/render/backend/webgpu/renderer.d.ts +200 -35
  51. package/dist/render/backend/webgpu/renderer.js +993 -168
  52. package/dist/render/backend/webgpu/shaders/clothSolve.wgsl.d.ts +9 -2
  53. package/dist/render/backend/webgpu/shaders/clothSolve.wgsl.js +121 -47
  54. package/dist/render/backend/webgpu/skinPaletteRing.d.ts +7 -3
  55. package/dist/render/backend/webgpu/skinPaletteRing.js +13 -7
  56. package/dist/render/backend/webgpu/skinPaletteTexture.d.ts +6 -0
  57. package/dist/render/backend/webgpu/skinPaletteTexture.js +7 -1
  58. package/dist/render/backend/webgpu/staticBundles.d.ts +125 -0
  59. package/dist/render/backend/webgpu/staticBundles.js +242 -0
  60. package/dist/render/backend/webgpu/surfaceTexturePass.d.ts +10 -3
  61. package/dist/render/backend/webgpu/surfaceTexturePass.js +20 -2
  62. package/dist/render/backend/webgpu/uniformRing.d.ts +21 -0
  63. package/dist/render/backend/webgpu/uniformRing.js +9 -1
  64. package/dist/render/backend/webgpu/vertexFormats.d.ts +22 -0
  65. package/dist/render/backend/webgpu/vertexFormats.js +97 -0
  66. package/dist/render/bloomChain.d.ts +3 -3
  67. package/dist/render/boneAnimation.d.ts +92 -0
  68. package/dist/render/boneAnimation.js +133 -0
  69. package/dist/render/compressedSource.d.ts +33 -13
  70. package/dist/render/compressedSource.js +70 -25
  71. package/dist/render/glCompressed.js +83 -0
  72. package/dist/render/globalMediumPass.d.ts +2 -0
  73. package/dist/render/globalMediumPass.js +1 -0
  74. package/dist/render/indexWidth.d.ts +17 -0
  75. package/dist/render/indexWidth.js +23 -0
  76. package/dist/render/instances.d.ts +26 -0
  77. package/dist/render/lightBudget.d.ts +10 -0
  78. package/dist/render/lightBudget.js +10 -0
  79. package/dist/render/lightMatrix.d.ts +11 -0
  80. package/dist/render/lightMatrix.js +14 -0
  81. package/dist/render/lightmap.d.ts +1 -1
  82. package/dist/render/mesh.d.ts +14 -0
  83. package/dist/render/mesh.js +44 -14
  84. package/dist/render/pointLightSelection.d.ts +4 -4
  85. package/dist/render/renderQuality.d.ts +30 -0
  86. package/dist/render/renderQuality.js +5 -0
  87. package/dist/render/rendererHarness.d.ts +7 -0
  88. package/dist/render/rendererHarness.js +12 -0
  89. package/dist/render/screenSpaceReflection.d.ts +2 -1
  90. package/dist/render/shader.d.ts +15 -0
  91. package/dist/render/shader.js +41 -0
  92. package/dist/render/shaders/boneAnimation.d.ts +19 -0
  93. package/dist/render/shaders/boneAnimation.js +63 -0
  94. package/dist/render/shaders/depth.d.ts +7 -0
  95. package/dist/render/shaders/depth.js +35 -10
  96. package/dist/render/shaders/flat/directionalShadow.js +84 -10
  97. package/dist/render/shaders/flat/glassTint.d.ts +1 -1
  98. package/dist/render/shaders/flat/glassTint.js +3 -0
  99. package/dist/render/shaders/flat/index.d.ts +21 -0
  100. package/dist/render/shaders/flat/index.js +42 -7
  101. package/dist/render/shaders/flat/layered.d.ts +19 -0
  102. package/dist/render/shaders/flat/layered.js +115 -0
  103. package/dist/render/shaders/flat/main.d.ts +1 -1
  104. package/dist/render/shaders/flat/main.js +58 -21
  105. package/dist/render/shaders/flat/mapReads.d.ts +10 -0
  106. package/dist/render/shaders/flat/mapReads.js +38 -0
  107. package/dist/render/shaders/flat/models.js +1 -1
  108. package/dist/render/shaders/flat/movingSun.d.ts +21 -0
  109. package/dist/render/shaders/flat/movingSun.js +46 -0
  110. package/dist/render/shaders/flat/normalMap.d.ts +6 -2
  111. package/dist/render/shaders/flat/normalMap.js +14 -3
  112. package/dist/render/shaders/flat/overlay.d.ts +3 -2
  113. package/dist/render/shaders/flat/overlay.js +42 -20
  114. package/dist/render/shaders/flat/pointShadow.js +2 -2
  115. package/dist/render/shaders/flat/preamble.js +59 -27
  116. package/dist/render/shaders/flat/worldUv.d.ts +22 -0
  117. package/dist/render/shaders/flat/worldUv.js +113 -0
  118. package/dist/render/shaders/generated/depth.wgsl.d.ts +93 -0
  119. package/dist/render/shaders/generated/depth.wgsl.js +93 -0
  120. package/dist/render/shaders/generated/flat.wgsl.d.ts +3616 -3042
  121. package/dist/render/shaders/generated/flat.wgsl.js +3688 -3113
  122. package/dist/render/shaders/generated/globalMedium.wgsl.d.ts +8 -3
  123. package/dist/render/shaders/generated/globalMedium.wgsl.js +8 -3
  124. package/dist/render/shaders/generated/lightVolume.wgsl.d.ts +15 -10
  125. package/dist/render/shaders/generated/lightVolume.wgsl.js +16 -11
  126. package/dist/render/shaders/globalMedium.js +9 -1
  127. package/dist/render/shaders/lightVolume.js +9 -1
  128. package/dist/render/shaders/outputTransform.d.ts +7 -0
  129. package/dist/render/shaders/outputTransform.js +28 -14
  130. package/dist/render/shaders/tangentFrame.d.ts +1 -1
  131. package/dist/render/shaders/tangentFrame.js +16 -3
  132. package/dist/render/shaders/vertexChannel.d.ts +7 -0
  133. package/dist/render/shaders/vertexChannel.js +26 -0
  134. package/dist/render/skinnedClothRun.d.ts +28 -0
  135. package/dist/render/skinnedClothRun.js +103 -35
  136. package/dist/render/staticDraws.d.ts +47 -0
  137. package/dist/render/staticDraws.js +43 -0
  138. package/dist/render/surfaceLayers.d.ts +39 -0
  139. package/dist/render/surfaceLayers.js +26 -0
  140. package/dist/render/surfaceOverlay.d.ts +16 -3
  141. package/dist/render/surfaceOverlay.js +11 -6
  142. package/dist/render/surfaceProjection.d.ts +34 -0
  143. package/dist/render/surfaceProjection.js +30 -0
  144. package/dist/render/surfaceTexture.d.ts +68 -7
  145. package/dist/render/surfaceTexture.js +56 -17
  146. package/dist/render/vertexPacking.d.ts +48 -0
  147. package/dist/render/vertexPacking.js +68 -0
  148. package/package.json +6 -6
  149. package/src/index.ts +12 -4
  150. package/src/render/ambientHarmonics.ts +1 -1
  151. package/src/render/areaLights.ts +4 -4
  152. package/src/render/backend/api.ts +13 -3
  153. package/src/render/backend/select.ts +10 -5
  154. package/src/render/backend/webgl2/boneAnimations.ts +105 -0
  155. package/src/render/backend/webgl2/instanced.ts +20 -1
  156. package/src/render/backend/webgl2/renderer.ts +723 -79
  157. package/src/render/backend/webgl2/skinPaletteTexture.ts +6 -4
  158. package/src/render/backend/webgpu/boneAnimations.ts +97 -0
  159. package/src/render/backend/webgpu/buffers.ts +195 -70
  160. package/src/render/backend/webgpu/clothKernels.ts +86 -0
  161. package/src/render/backend/webgpu/clothOps.ts +86 -0
  162. package/src/render/backend/webgpu/clothSetLayout.ts +102 -0
  163. package/src/render/backend/webgpu/clothSetPack.ts +197 -0
  164. package/src/render/backend/webgpu/compressedUpload.ts +59 -9
  165. package/src/render/backend/webgpu/depthPass.ts +51 -10
  166. package/src/render/backend/webgpu/drawCommand.ts +25 -4
  167. package/src/render/backend/webgpu/flatPass.ts +212 -41
  168. package/src/render/backend/webgpu/gpuClothSet.ts +564 -0
  169. package/src/render/backend/webgpu/instanced.ts +32 -1
  170. package/src/render/backend/webgpu/latePass.ts +1 -0
  171. package/src/render/backend/webgpu/materialStore.ts +276 -0
  172. package/src/render/backend/webgpu/motionPass.ts +18 -7
  173. package/src/render/backend/webgpu/pipelineCache.ts +53 -7
  174. package/src/render/backend/webgpu/renderer.ts +1201 -202
  175. package/src/render/backend/webgpu/shaders/clothSolve.wgsl.ts +121 -47
  176. package/src/render/backend/webgpu/skinPaletteRing.ts +13 -7
  177. package/src/render/backend/webgpu/skinPaletteTexture.ts +7 -1
  178. package/src/render/backend/webgpu/staticBundles.ts +364 -0
  179. package/src/render/backend/webgpu/surfaceTexturePass.ts +29 -3
  180. package/src/render/backend/webgpu/uniformRing.ts +10 -1
  181. package/src/render/backend/webgpu/vertexFormats.ts +113 -0
  182. package/src/render/bloomChain.ts +3 -3
  183. package/src/render/boneAnimation.ts +223 -0
  184. package/src/render/compressedSource.ts +121 -29
  185. package/src/render/glCompressed.ts +83 -0
  186. package/src/render/globalMediumPass.ts +7 -0
  187. package/src/render/indexWidth.ts +27 -0
  188. package/src/render/instances.ts +27 -0
  189. package/src/render/lightBudget.ts +11 -0
  190. package/src/render/lightMatrix.ts +15 -0
  191. package/src/render/lightmap.ts +1 -1
  192. package/src/render/mesh.ts +72 -14
  193. package/src/render/pointLightSelection.ts +4 -4
  194. package/src/render/renderQuality.ts +38 -0
  195. package/src/render/rendererHarness.ts +15 -0
  196. package/src/render/screenSpaceReflection.ts +2 -1
  197. package/src/render/shader.ts +59 -0
  198. package/src/render/shaders/boneAnimation.ts +63 -0
  199. package/src/render/shaders/depth.ts +45 -10
  200. package/src/render/shaders/flat/directionalShadow.ts +84 -10
  201. package/src/render/shaders/flat/glassTint.ts +7 -1
  202. package/src/render/shaders/flat/index.ts +63 -7
  203. package/src/render/shaders/flat/layered.ts +115 -0
  204. package/src/render/shaders/flat/main.ts +58 -21
  205. package/src/render/shaders/flat/mapReads.ts +38 -0
  206. package/src/render/shaders/flat/models.ts +1 -1
  207. package/src/render/shaders/flat/movingSun.ts +46 -0
  208. package/src/render/shaders/flat/normalMap.ts +14 -3
  209. package/src/render/shaders/flat/overlay.ts +42 -20
  210. package/src/render/shaders/flat/pointShadow.ts +2 -2
  211. package/src/render/shaders/flat/preamble.ts +59 -27
  212. package/src/render/shaders/flat/worldUv.ts +113 -0
  213. package/src/render/shaders/generated/depth.wgsl.ts +95 -0
  214. package/src/render/shaders/generated/flat.wgsl.ts +3688 -3113
  215. package/src/render/shaders/generated/globalMedium.wgsl.ts +8 -3
  216. package/src/render/shaders/generated/lightVolume.wgsl.ts +16 -11
  217. package/src/render/shaders/globalMedium.ts +9 -1
  218. package/src/render/shaders/lightVolume.ts +9 -1
  219. package/src/render/shaders/outputTransform.ts +30 -14
  220. package/src/render/shaders/tangentFrame.ts +16 -3
  221. package/src/render/shaders/vertexChannel.ts +27 -0
  222. package/src/render/skinnedClothRun.ts +138 -38
  223. package/src/render/staticDraws.ts +95 -0
  224. package/src/render/surfaceLayers.ts +57 -0
  225. package/src/render/surfaceOverlay.ts +24 -7
  226. package/src/render/surfaceProjection.ts +57 -0
  227. package/src/render/surfaceTexture.ts +124 -27
  228. package/src/render/vertexPacking.ts +109 -0
  229. package/dist/render/backend/webgpu/gpuSkinnedCloth.d.ts +0 -74
  230. package/dist/render/backend/webgpu/gpuSkinnedCloth.js +0 -452
  231. package/src/render/backend/webgpu/gpuSkinnedCloth.ts +0 -502
@@ -0,0 +1,276 @@
1
+ import type { BudgetCounter } from '../budget.ts';
2
+ import { DYNAMIC_ALIGNMENT, MAX_RING_BYTES } from './uniformRing.ts';
3
+
4
+ /**
5
+ * The lit stage's material blocks, each kept in its slot for as long as later draws ask for the
6
+ * same numbers, so a material that has not changed since the last frame costs no upload at all.
7
+ *
8
+ * **Keyed by content, because nothing else names a material.** A material here is whatever the
9
+ * setters left in the block when a draw took it — a consumer never hands over an object that could
10
+ * carry an identity — so the block's own words are the key: hashed to find a bucket, compared word
11
+ * for word to accept a match, and bound by the slot's offset. What it gives up is a hash and a
12
+ * compare of a few hundred bytes per material change, against copying and uploading the whole
13
+ * block every frame, which a stage of four hundred materials measured at megabytes a frame.
14
+ *
15
+ * **A slot the frame has asked for is never given away within it**, for the reason `UniformRing`
16
+ * exists: a queue write does not interleave with recorded draws, so rewriting a slot a recorded draw
17
+ * reads would hand that draw the new numbers. A frame asking for more distinct blocks than there are
18
+ * slots is refused, counted and skipped, as the ring was. Across frames rewriting is safe, because a
19
+ * write issued after a submit is ordered after the commands in it.
20
+ *
21
+ * **What a new block displaces is a slot nobody asked for this frame or the last**, found by a hand
22
+ * that sweeps the slots in turn; only when every slot was asked for recently does it take one the
23
+ * last frame used. What would make that wrong is a scene whose distinct materials a frame approach
24
+ * the store's size: it then rewrites some of its steady materials every frame, which costs their
25
+ * upload and nothing else.
26
+ */
27
+ export class MaterialStore {
28
+ /** Replaced by `growTo`, so every bind group over it is rebuilt when that says so. */
29
+ buffer: GPUBuffer;
30
+ /** Bytes per slot, rounded up to the alignment a dynamic offset requires. */
31
+ readonly slotSize: number;
32
+
33
+ private readonly words: number;
34
+ private readonly stride: number;
35
+ private capacity: number;
36
+ private staging: ArrayBuffer;
37
+ private ints: Int32Array;
38
+ /** Per slot: its block's hash, the next slot in its bucket or -1, and whether it holds one. */
39
+ private hashes: Int32Array;
40
+ private next: Int32Array;
41
+ private held: Uint8Array;
42
+ /** Per slot: the frame that last asked for it, and whether it was written since the last flush. */
43
+ private askedIn: Int32Array;
44
+ private pending: Uint8Array;
45
+ /** Per slot: how many holders pinned it. A pinned slot is never displaced. */
46
+ private pins: Int32Array;
47
+ /**
48
+ * How many times the store has forgotten everything it held, which growth does: a holder of an
49
+ * offset compares this, because an offset from before is a slot of a buffer that is gone.
50
+ */
51
+ epoch = 0;
52
+ /** Per bucket: its first slot, or -1. A power of two at least twice the slots. */
53
+ private heads: Int32Array;
54
+ private mask: number;
55
+ private lowPending: number;
56
+ private highPending = -1;
57
+ private hand = 0;
58
+ /** Starts past zero, so a slot never asked for reads as long unasked. */
59
+ private frame = 2;
60
+
61
+ constructor(
62
+ private readonly device: GPUDevice,
63
+ bytesPerBlock: number,
64
+ slots: number,
65
+ private readonly usage: number,
66
+ private readonly label = 'materials.store',
67
+ private readonly budget: BudgetCounter | null = null,
68
+ ) {
69
+ this.words = Math.ceil(bytesPerBlock / 4);
70
+ this.slotSize = Math.ceil(bytesPerBlock / DYNAMIC_ALIGNMENT) * DYNAMIC_ALIGNMENT;
71
+ this.stride = this.slotSize / 4;
72
+ this.capacity = slots;
73
+ this.lowPending = slots;
74
+ this.staging = new ArrayBuffer(this.slotSize * slots);
75
+ this.ints = new Int32Array(this.staging);
76
+ this.hashes = new Int32Array(slots);
77
+ this.next = new Int32Array(slots);
78
+ this.held = new Uint8Array(slots);
79
+ this.askedIn = new Int32Array(slots);
80
+ this.pending = new Uint8Array(slots);
81
+ this.pins = new Int32Array(slots);
82
+ this.heads = new Int32Array(bucketsFor(slots)).fill(-1);
83
+ this.mask = this.heads.length - 1;
84
+ this.buffer = device.createBuffer({ label, size: this.staging.byteLength, usage });
85
+ }
86
+
87
+ /** How many distinct blocks a frame may hold. */
88
+ get slots(): number {
89
+ return this.capacity;
90
+ }
91
+
92
+ /** Start a frame: every slot the last one asked for may be displaced from here on, last. */
93
+ beginFrame(): void {
94
+ this.frame += 1;
95
+ }
96
+
97
+ /**
98
+ * The byte offset of a slot holding exactly `block`, written there if no slot did, or null when
99
+ * every slot is one this frame has asked for. `block` is the whole block, word for word.
100
+ */
101
+ place(block: Int32Array): number | null {
102
+ this.budget?.ask();
103
+ const hash = hashWords(block, this.words);
104
+ const bucket = hash & this.mask;
105
+ for (let slot = this.heads[bucket] as number; slot >= 0; slot = this.next[slot] as number) {
106
+ if (this.hashes[slot] === hash && this.holds(slot, block)) {
107
+ this.askedIn[slot] = this.frame;
108
+ return slot * this.slotSize;
109
+ }
110
+ }
111
+ const slot = this.displace();
112
+ if (slot < 0) {
113
+ this.budget?.drop();
114
+ return null;
115
+ }
116
+ if (this.held[slot] === 1) this.unlink(slot);
117
+ this.ints.set(block, slot * this.stride);
118
+ this.hashes[slot] = hash;
119
+ this.next[slot] = this.heads[bucket] as number;
120
+ this.heads[bucket] = slot;
121
+ this.held[slot] = 1;
122
+ this.askedIn[slot] = this.frame;
123
+ this.pending[slot] = 1;
124
+ if (slot < this.lowPending) this.lowPending = slot;
125
+ if (slot > this.highPending) this.highPending = slot;
126
+ return slot * this.slotSize;
127
+ }
128
+
129
+ /** Upload every slot written since the last flush, one write per run of neighbouring slots. */
130
+ flush(): void {
131
+ let slot = this.lowPending;
132
+ while (slot <= this.highPending) {
133
+ if (this.pending[slot] !== 1) {
134
+ slot += 1;
135
+ continue;
136
+ }
137
+ const from = slot;
138
+ while (slot <= this.highPending && this.pending[slot] === 1) {
139
+ this.pending[slot] = 0;
140
+ slot += 1;
141
+ }
142
+ const at = from * this.slotSize;
143
+ this.device.queue.writeBuffer(
144
+ this.buffer,
145
+ at,
146
+ this.staging,
147
+ at,
148
+ (slot - from) * this.slotSize,
149
+ );
150
+ }
151
+ this.lowPending = this.capacity;
152
+ this.highPending = -1;
153
+ }
154
+
155
+ /**
156
+ * Make room for at least `slots`, forgetting every block held, and say whether the buffer was
157
+ * replaced. Called where `UniformRing.growTo` is, at a frame's start, for the reasons it gives,
158
+ * and refused past the same ceiling.
159
+ */
160
+ growTo(slots: number): boolean {
161
+ if (slots <= this.capacity) return false;
162
+ if (this.slotSize * slots > MAX_RING_BYTES) return false;
163
+ this.buffer.destroy();
164
+ this.capacity = slots;
165
+ this.staging = new ArrayBuffer(this.slotSize * slots);
166
+ this.ints = new Int32Array(this.staging);
167
+ this.hashes = new Int32Array(slots);
168
+ this.next = new Int32Array(slots);
169
+ this.held = new Uint8Array(slots);
170
+ this.askedIn = new Int32Array(slots);
171
+ this.pending = new Uint8Array(slots);
172
+ this.pins = new Int32Array(slots);
173
+ this.heads = new Int32Array(bucketsFor(slots)).fill(-1);
174
+ this.mask = this.heads.length - 1;
175
+ this.lowPending = slots;
176
+ this.epoch += 1;
177
+ this.highPending = -1;
178
+ this.hand = 0;
179
+ this.buffer = this.device.createBuffer({
180
+ label: this.label,
181
+ size: this.staging.byteLength,
182
+ usage: this.usage,
183
+ });
184
+ return true;
185
+ }
186
+
187
+ /**
188
+ * Keep the block at `offset` where it is until `unpin`: a holder binding it by offset frame after
189
+ * frame — a list recorded once — whether or not anything asks for it. Counted, so two holders of
190
+ * one block each let go of their own pin.
191
+ */
192
+ pin(offset: number): void {
193
+ const slot = offset / this.slotSize;
194
+ this.pins[slot] = (this.pins[slot] as number) + 1;
195
+ }
196
+
197
+ unpin(offset: number): void {
198
+ const slot = offset / this.slotSize;
199
+ if ((this.pins[slot] as number) > 0) this.pins[slot] = (this.pins[slot] as number) - 1;
200
+ }
201
+
202
+ dispose(): void {
203
+ this.buffer.destroy();
204
+ }
205
+
206
+ private holds(slot: number, block: Int32Array): boolean {
207
+ const base = slot * this.stride;
208
+ for (let word = 0; word < this.words; word++) {
209
+ if (this.ints[base + word] !== block[word]) return false;
210
+ }
211
+ return true;
212
+ }
213
+
214
+ /**
215
+ * The slot a new block goes in: the first from the hand nobody asked for this frame or the last,
216
+ * else the first this frame has not asked for, else none. One sweep at most.
217
+ */
218
+ private displace(): number {
219
+ let fallback = -1;
220
+ for (let step = 0; step < this.capacity; step++) {
221
+ const slot = this.hand;
222
+ this.hand = slot + 1 === this.capacity ? 0 : slot + 1;
223
+ if ((this.pins[slot] as number) > 0) continue;
224
+ const asked = this.askedIn[slot] as number;
225
+ if (asked < this.frame - 1) return slot;
226
+ if (fallback < 0 && asked !== this.frame) fallback = slot;
227
+ }
228
+ return fallback;
229
+ }
230
+
231
+ private unlink(slot: number): void {
232
+ const bucket = (this.hashes[slot] as number) & this.mask;
233
+ let at = this.heads[bucket] as number;
234
+ if (at === slot) {
235
+ this.heads[bucket] = this.next[slot] as number;
236
+ return;
237
+ }
238
+ while (at >= 0) {
239
+ const after = this.next[at] as number;
240
+ if (after === slot) {
241
+ this.next[at] = this.next[slot] as number;
242
+ return;
243
+ }
244
+ at = after;
245
+ }
246
+ }
247
+ }
248
+
249
+ function bucketsFor(slots: number): number {
250
+ let buckets = 16;
251
+ while (buckets < slots * 2) buckets *= 2;
252
+ return buckets;
253
+ }
254
+
255
+ /**
256
+ * MurmurHash3's 32-bit mix over the block's words. Not FNV, whose multiply carries a difference only
257
+ * upward: the bucket is the hash's low bits, and a material's words are mostly floats, whose low
258
+ * mantissa bits are zero for every value a person types — 0.5, 1, 2 — so blocks differing only in
259
+ * those would all have shared one bucket. The compare that follows a match is what decides.
260
+ */
261
+ export function hashWords(block: Int32Array, words: number): number {
262
+ let hash = 0x9747b28c;
263
+ for (let word = 0; word < words; word++) {
264
+ let k = Math.imul(block[word] as number, 0xcc9e2d51);
265
+ k = (k << 15) | (k >>> 17);
266
+ hash ^= Math.imul(k, 0x1b873593);
267
+ hash = (hash << 13) | (hash >>> 19);
268
+ hash = (Math.imul(hash, 5) + 0xe6546b64) | 0;
269
+ }
270
+ hash ^= words * 4;
271
+ hash ^= hash >>> 16;
272
+ hash = Math.imul(hash, 0x85ebca6b);
273
+ hash ^= hash >>> 13;
274
+ hash = Math.imul(hash, 0xc2b2ae35);
275
+ return hash ^ (hash >>> 16);
276
+ }
@@ -254,6 +254,8 @@ export class MotionPass {
254
254
  readonly weights: number;
255
255
  readonly joints2: number;
256
256
  readonly weights2: number;
257
+ readonly weightsFormat: GPUVertexFormat;
258
+ readonly weights2Format: GPUVertexFormat;
257
259
  },
258
260
  cloth = false,
259
261
  ): GPURenderPipeline | null {
@@ -261,13 +263,18 @@ export class MotionPass {
261
263
  const eight = offsets.joints2 >= 0 && offsets.weights2 >= 0;
262
264
  /*
263
265
  * Five byte counts, each under 256 because a vertex is at most 172 bytes, as five base-256
264
- * digits: exact below 2^40, so two layouts can never share a key.
266
+ * digits, and two bits for the weights' formats: exact below 2^42, so two layouts can never
267
+ * share a key.
265
268
  */
266
269
  const key =
267
- (((stride * 256 + offsets.joints) * 256 + offsets.weights) * 256 + offsets.joints2 + 1) *
270
+ ((((stride * 256 + offsets.joints) * 256 + offsets.weights) * 256 + offsets.joints2 + 1) *
268
271
  256 +
269
- offsets.weights2 +
270
- 1;
272
+ offsets.weights2 +
273
+ 1) *
274
+ 4 +
275
+ /* And whether each four weights are floats or fixed point, two bits: `vertexPacking.ts`. */
276
+ (offsets.weightsFormat === 'float32x4' ? 0 : 2) +
277
+ (offsets.weights2Format === 'float32x4' ? 0 : 1);
271
278
  const pipelines = cloth ? this.clothPipelines : this.skinnedPipelines;
272
279
  const held = pipelines.get(key);
273
280
  if (held !== undefined) return held;
@@ -287,11 +294,15 @@ export class MotionPass {
287
294
  attributes: [
288
295
  { shaderLocation: 0, offset: 0, format: 'float32x3' },
289
296
  { shaderLocation: 11, offset: offsets.joints, format: 'float32x4' },
290
- { shaderLocation: 12, offset: offsets.weights, format: 'float32x4' },
297
+ { shaderLocation: 12, offset: offsets.weights, format: offsets.weightsFormat },
291
298
  ...(eight
292
299
  ? ([
293
300
  { shaderLocation: 14, offset: offsets.joints2, format: 'float32x4' },
294
- { shaderLocation: 15, offset: offsets.weights2, format: 'float32x4' },
301
+ {
302
+ shaderLocation: 15,
303
+ offset: offsets.weights2,
304
+ format: offsets.weights2Format,
305
+ },
295
306
  ] as const)
296
307
  : []),
297
308
  ],
@@ -635,7 +646,7 @@ export class MotionPass {
635
646
  }
636
647
  pass.setBindGroup(0, this.group, [i * MOTION_DRAW_STRIDE]);
637
648
  pass.setVertexBuffer(0, mesh.vertexBuffers[0] as GPUBuffer);
638
- pass.setIndexBuffer(mesh.indexBuffer, 'uint32');
649
+ pass.setIndexBuffer(mesh.indexBuffer, mesh.indexFormat);
639
650
  pass.drawIndexed(mesh.indexCount, instances);
640
651
  }
641
652
  }
@@ -58,6 +58,12 @@ export class PipelineCache {
58
58
 
59
59
  /** How to describe each lit pipeline again, so `enable` can rebuild every one. */
60
60
  private readonly litDescribers = new Map<string, () => GPURenderPipelineDescriptor>();
61
+ /**
62
+ * Bumped when a switch's rebuilt set lands, which replaces pipelines already handed out: a holder
63
+ * of one — a recorded render bundle — compares this and records again rather than drawing on
64
+ * with the pipelines from before the switch.
65
+ */
66
+ version = 0;
61
67
  /** Bumped by every switch, so a lit build started before one is not kept after it. */
62
68
  private litGeneration = 0;
63
69
  /** The last rebuild, which the next one waits for so they land in the order they were asked. */
@@ -80,6 +86,16 @@ export class PipelineCache {
80
86
  * wait for readiness rather than discovering it mid-frame.
81
87
  */
82
88
  private readonly compiling = new Map<string, Promise<GPURenderPipeline>>();
89
+ /** Keys whose compile off the main thread failed, which `lit` then builds where asked for. */
90
+ private readonly failed = new Set<string>();
91
+
92
+ /**
93
+ * Whether a lit pipeline first asked for inside a frame is compiled off the main thread while
94
+ * its draw is left out — `RenderQuality.pipelineCompile` `'skip'` — rather than built where it is
95
+ * asked for, which holds the GPU process for the compile. The renderer turns it off while it
96
+ * records static draws, which must hold every draw they were given.
97
+ */
98
+ skipsCompiling = false;
83
99
 
84
100
  constructor(
85
101
  device: GPUDevice,
@@ -133,18 +149,27 @@ export class PipelineCache {
133
149
  typeof this.device.createRenderPipelineAsync === 'function'
134
150
  ? this.device.createRenderPipelineAsync(describe())
135
151
  : Promise.resolve(this.device.createRenderPipeline(describe()));
136
- const promise = build.then((built) => {
137
- /* A lit pipeline started before a switch is not kept: the switch rebuilds it. */
138
- if (!lit || generation === this.litGeneration) this.pipelines.set(key, built);
139
- this.compiling.delete(key);
140
- return built;
141
- });
152
+ const promise = build.then(
153
+ (built) => {
154
+ /* A lit pipeline started before a switch is not kept: the switch rebuilds it. */
155
+ if (!lit || generation === this.litGeneration) this.pipelines.set(key, built);
156
+ this.compiling.delete(key);
157
+ return built;
158
+ },
159
+ /* Out of the list, or `ready` waits on it for ever; `lit` builds it where asked next. */
160
+ (error: unknown) => {
161
+ this.compiling.delete(key);
162
+ this.failed.add(key);
163
+ throw error;
164
+ },
165
+ );
142
166
  this.compiling.set(key, promise);
143
167
  return promise;
144
168
  }
145
169
 
146
170
  /**
147
- * Resolve once every pipeline asked for so far has finished compiling.
171
+ * Resolve once every pipeline asked for so far has finished compiling, a switch's rebuild
172
+ * among them.
148
173
  *
149
174
  * A renderer awaits this before its first frame, which is what moves the compile out of the
150
175
  * queue and in front of it. A failure is not rethrown: a pipeline that cannot be built will
@@ -155,6 +180,8 @@ export class PipelineCache {
155
180
  while (this.compiling.size > 0) {
156
181
  await Promise.allSettled([...this.compiling.values()]);
157
182
  }
183
+ /* And a switch's rebuild, whose set replaces the pipelines in one swap: see `enable`. */
184
+ await this.litRebuild;
158
185
  }
159
186
 
160
187
  /** The pipeline for this key if it is already built, without building one. */
@@ -188,6 +215,24 @@ export class PipelineCache {
188
215
  return built;
189
216
  }
190
217
 
218
+ /**
219
+ * A lit pipeline for a draw inside a frame: the one built, or one built here — or, where draws
220
+ * skip (`skipsCompiling`), none, with its compile started off the main thread for a later frame to
221
+ * find. A draw handed none is left out.
222
+ *
223
+ * **What skipping gives up** is the draw for the frames its compile takes, a few to a few dozen on
224
+ * a phone, against a frame held for the compile where it is asked for: 130 to 400 ms, reported on
225
+ * a phone for a material first drawn mid-scene. A compile that fails is built here the next time,
226
+ * so the device reports it rather than the draw vanishing for good.
227
+ */
228
+ lit(key: string, describe: () => GPURenderPipelineDescriptor): GPURenderPipeline | null {
229
+ const existing = this.pipelines.get(key);
230
+ if (existing !== undefined) return existing;
231
+ if (!this.skipsCompiling || this.failed.has(key)) return this.get(key, describe, true);
232
+ if (!this.compiling.has(key)) void this.getAsync(key, describe, true).catch(() => {});
233
+ return null;
234
+ }
235
+
191
236
  /**
192
237
  * Turn one of the lit stage's switches on, for every lit pipeline built from now on and every one
193
238
  * already built. See `litSwitches`.
@@ -220,6 +265,7 @@ export class PipelineCache {
220
265
  for (const result of results) {
221
266
  if (result.status === 'fulfilled') this.pipelines.set(result.value[0], result.value[1]);
222
267
  }
268
+ this.version += 1;
223
269
  });
224
270
  return this.litRebuild;
225
271
  }