@driftengine/core 4.4.1 → 4.5.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 (218) hide show
  1. package/CHANGELOG.json +72 -0
  2. package/README.md +1 -1
  3. package/dist/index.d.ts +5 -1
  4. package/dist/index.js +7 -0
  5. package/dist/render/backend/api.d.ts +17 -11
  6. package/dist/render/backend/webgl2/glassTintPrograms.d.ts +13 -0
  7. package/dist/render/backend/webgl2/glassTintPrograms.js +38 -0
  8. package/dist/render/backend/webgl2/renderer.d.ts +82 -8
  9. package/dist/render/backend/webgl2/renderer.js +373 -105
  10. package/dist/render/backend/webgpu/buffers.d.ts +19 -1
  11. package/dist/render/backend/webgpu/buffers.js +27 -1
  12. package/dist/render/backend/webgpu/depthPass.d.ts +1 -1
  13. package/dist/render/backend/webgpu/depthPass.js +2 -1
  14. package/dist/render/backend/webgpu/flatPass.d.ts +2 -0
  15. package/dist/render/backend/webgpu/flatPass.js +20 -1
  16. package/dist/render/backend/webgpu/glassTintPass.d.ts +28 -0
  17. package/dist/render/backend/webgpu/glassTintPass.js +205 -0
  18. package/dist/render/backend/webgpu/gpuDrivenPass.d.ts +60 -1
  19. package/dist/render/backend/webgpu/gpuDrivenPass.js +305 -5
  20. package/dist/render/backend/webgpu/instanced.d.ts +12 -1
  21. package/dist/render/backend/webgpu/instanced.js +20 -1
  22. package/dist/render/backend/webgpu/latePass.d.ts +26 -0
  23. package/dist/render/backend/webgpu/latePass.js +133 -0
  24. package/dist/render/backend/webgpu/lightVolumePass.js +5 -1
  25. package/dist/render/backend/webgpu/motionPass.d.ts +95 -0
  26. package/dist/render/backend/webgpu/motionPass.js +491 -0
  27. package/dist/render/backend/webgpu/pipelineCache.d.ts +10 -1
  28. package/dist/render/backend/webgpu/pipelineCache.js +11 -1
  29. package/dist/render/backend/webgpu/pointGlassTintPass.d.ts +32 -0
  30. package/dist/render/backend/webgpu/pointGlassTintPass.js +193 -0
  31. package/dist/render/backend/webgpu/pointShadowPass.d.ts +35 -6
  32. package/dist/render/backend/webgpu/pointShadowPass.js +105 -20
  33. package/dist/render/backend/webgpu/postPass.d.ts +21 -15
  34. package/dist/render/backend/webgpu/postPass.js +17 -4
  35. package/dist/render/backend/webgpu/renderer.d.ts +181 -54
  36. package/dist/render/backend/webgpu/renderer.js +1011 -394
  37. package/dist/render/backend/webgpu/scatterPass.js +2 -1
  38. package/dist/render/backend/webgpu/skinPaletteRing.d.ts +4 -0
  39. package/dist/render/backend/webgpu/skinPaletteRing.js +4 -0
  40. package/dist/render/backend/webgpu/snapshotMips.d.ts +29 -0
  41. package/dist/render/backend/webgpu/snapshotMips.js +52 -0
  42. package/dist/render/backend/webgpu/surfaceTexturePass.d.ts +5 -0
  43. package/dist/render/backend/webgpu/surfaceTexturePass.js +1 -1
  44. package/dist/render/flameLight.js +15 -2
  45. package/dist/render/glass.d.ts +45 -0
  46. package/dist/render/glass.js +39 -0
  47. package/dist/render/glassCasters.d.ts +40 -0
  48. package/dist/render/glassCasters.js +84 -0
  49. package/dist/render/glassShadow.d.ts +78 -0
  50. package/dist/render/glassShadow.js +125 -0
  51. package/dist/render/globalMediumPass.d.ts +2 -3
  52. package/dist/render/globalMediumPass.js +2 -8
  53. package/dist/render/gpudriven/lit.d.ts +12 -0
  54. package/dist/render/gpudriven/lit.js +23 -0
  55. package/dist/render/gpudriven/materialTable.d.ts +15 -5
  56. package/dist/render/gpudriven/materialTable.js +26 -6
  57. package/dist/render/gpudriven/pipeline.d.ts +1 -1
  58. package/dist/render/gpudriven/pipeline.js +4 -3
  59. package/dist/render/gpudriven/sunGlass.d.ts +35 -0
  60. package/dist/render/gpudriven/sunGlass.js +89 -0
  61. package/dist/render/lightBudget.d.ts +17 -5
  62. package/dist/render/lightBudget.js +17 -5
  63. package/dist/render/pointGlassTint.d.ts +59 -0
  64. package/dist/render/pointGlassTint.js +198 -0
  65. package/dist/render/pointShadowArray.d.ts +56 -3
  66. package/dist/render/pointShadowArray.js +127 -16
  67. package/dist/render/pointShadowBudget.js +51 -41
  68. package/dist/render/pointShadowMap.d.ts +23 -1
  69. package/dist/render/pointShadowMap.js +27 -1
  70. package/dist/render/recon/changeFrames.d.ts +15 -0
  71. package/dist/render/recon/changeFrames.js +21 -0
  72. package/dist/render/recon/disocclusion.d.ts +36 -3
  73. package/dist/render/recon/disocclusion.js +56 -4
  74. package/dist/render/recon/lateRouting.d.ts +14 -0
  75. package/dist/render/recon/lateRouting.js +18 -0
  76. package/dist/render/recon/mover.d.ts +57 -0
  77. package/dist/render/recon/mover.js +86 -0
  78. package/dist/render/recon/resolve.js +95 -12
  79. package/dist/render/renderQuality.d.ts +35 -0
  80. package/dist/render/renderQuality.js +7 -0
  81. package/dist/render/rendererHarness.js +12 -0
  82. package/dist/render/sceneTarget.d.ts +9 -1
  83. package/dist/render/sceneTarget.js +59 -13
  84. package/dist/render/shaders/depth.d.ts +16 -1
  85. package/dist/render/shaders/depth.js +85 -9
  86. package/dist/render/shaders/flat/directionalShadow.js +91 -6
  87. package/dist/render/shaders/flat/glassTint.d.ts +6 -0
  88. package/dist/render/shaders/flat/glassTint.js +25 -0
  89. package/dist/render/shaders/flat/index.d.ts +13 -0
  90. package/dist/render/shaders/flat/index.js +4 -0
  91. package/dist/render/shaders/flat/lampGlass.d.ts +1 -0
  92. package/dist/render/shaders/flat/lampGlass.js +183 -0
  93. package/dist/render/shaders/flat/main.d.ts +1 -1
  94. package/dist/render/shaders/flat/main.js +115 -15
  95. package/dist/render/shaders/flat/pointShadow.d.ts +1 -1
  96. package/dist/render/shaders/flat/pointShadow.js +2 -0
  97. package/dist/render/shaders/flat/preamble.js +21 -6
  98. package/dist/render/shaders/generated/depth.wgsl.d.ts +227 -4
  99. package/dist/render/shaders/generated/depth.wgsl.js +227 -4
  100. package/dist/render/shaders/generated/flat.wgsl.d.ts +360 -312
  101. package/dist/render/shaders/generated/flat.wgsl.js +376 -328
  102. package/dist/render/shaders/generated/globalMedium.wgsl.d.ts +3 -13
  103. package/dist/render/shaders/generated/globalMedium.wgsl.js +3 -13
  104. package/dist/render/shaders/generated/lightVolume.wgsl.d.ts +3 -13
  105. package/dist/render/shaders/generated/lightVolume.wgsl.js +4 -14
  106. package/dist/render/shaders/generated/octahedralResolve.wgsl.d.ts +29 -0
  107. package/dist/render/shaders/generated/octahedralResolve.wgsl.js +29 -0
  108. package/dist/render/shaders/generated/temporalResolve.wgsl.d.ts +31 -6
  109. package/dist/render/shaders/generated/temporalResolve.wgsl.js +31 -6
  110. package/dist/render/shaders/globalMedium.js +6 -6
  111. package/dist/render/shaders/gpudriven/blendRaster.wgsl.d.ts +7 -0
  112. package/dist/render/shaders/gpudriven/blendRaster.wgsl.js +86 -1
  113. package/dist/render/shaders/gpudriven/cull.wgsl.d.ts +1 -1
  114. package/dist/render/shaders/gpudriven/cull.wgsl.js +11 -2
  115. package/dist/render/shaders/gpudriven/materialTable.wgsl.d.ts +1 -1
  116. package/dist/render/shaders/gpudriven/materialTable.wgsl.js +3 -1
  117. package/dist/render/shaders/gpudriven/shade.wgsl.js +50 -1
  118. package/dist/render/shaders/gpudriven/sunGlass.wgsl.d.ts +9 -0
  119. package/dist/render/shaders/gpudriven/sunGlass.wgsl.js +133 -0
  120. package/dist/render/shaders/lightVolume.js +6 -6
  121. package/dist/render/shaders/octahedralResolve.d.ts +8 -0
  122. package/dist/render/shaders/octahedralResolve.js +48 -17
  123. package/dist/render/shaders/recon/lateDepth.wgsl.d.ts +1 -0
  124. package/dist/render/shaders/recon/lateDepth.wgsl.js +44 -0
  125. package/dist/render/shaders/recon/motion.wgsl.d.ts +1 -1
  126. package/dist/render/shaders/recon/motion.wgsl.js +84 -0
  127. package/dist/render/shaders/recon/resolve.wgsl.d.ts +1 -1
  128. package/dist/render/shaders/recon/resolve.wgsl.js +112 -8
  129. package/dist/render/shaders/temporalResolve.js +133 -5
  130. package/dist/render/shadowCasters.d.ts +20 -1
  131. package/dist/render/shadowCasters.js +15 -1
  132. package/dist/render/shadowMap.d.ts +54 -5
  133. package/dist/render/shadowMap.js +190 -36
  134. package/dist/render/sunGlassTint.d.ts +25 -0
  135. package/dist/render/sunGlassTint.js +79 -0
  136. package/dist/render/temporalAa.d.ts +145 -0
  137. package/dist/render/temporalAa.js +190 -0
  138. package/dist/render/temporalPass.d.ts +16 -14
  139. package/dist/render/temporalPass.js +59 -14
  140. package/dist/render/translucentQueue.d.ts +6 -0
  141. package/dist/render/translucentQueue.js +15 -1
  142. package/package.json +6 -6
  143. package/src/index.ts +10 -0
  144. package/src/render/backend/api.ts +15 -9
  145. package/src/render/backend/webgl2/glassTintPrograms.ts +58 -0
  146. package/src/render/backend/webgl2/renderer.ts +422 -109
  147. package/src/render/backend/webgpu/buffers.ts +50 -2
  148. package/src/render/backend/webgpu/depthPass.ts +2 -1
  149. package/src/render/backend/webgpu/flatPass.ts +22 -1
  150. package/src/render/backend/webgpu/glassTintPass.ts +244 -0
  151. package/src/render/backend/webgpu/gpuDrivenPass.ts +349 -5
  152. package/src/render/backend/webgpu/instanced.ts +21 -1
  153. package/src/render/backend/webgpu/latePass.ts +152 -0
  154. package/src/render/backend/webgpu/lightVolumePass.ts +5 -1
  155. package/src/render/backend/webgpu/motionPass.ts +535 -0
  156. package/src/render/backend/webgpu/pipelineCache.ts +12 -1
  157. package/src/render/backend/webgpu/pointGlassTintPass.ts +233 -0
  158. package/src/render/backend/webgpu/pointShadowPass.ts +158 -26
  159. package/src/render/backend/webgpu/postPass.ts +34 -4
  160. package/src/render/backend/webgpu/renderer.ts +1178 -425
  161. package/src/render/backend/webgpu/scatterPass.ts +2 -1
  162. package/src/render/backend/webgpu/skinPaletteRing.ts +4 -0
  163. package/src/render/backend/webgpu/snapshotMips.ts +80 -0
  164. package/src/render/backend/webgpu/surfaceTexturePass.ts +1 -1
  165. package/src/render/flameLight.ts +15 -2
  166. package/src/render/glass.ts +74 -0
  167. package/src/render/glassCasters.ts +164 -0
  168. package/src/render/glassShadow.ts +153 -0
  169. package/src/render/globalMediumPass.ts +4 -11
  170. package/src/render/gpudriven/lit.ts +31 -0
  171. package/src/render/gpudriven/materialTable.ts +28 -6
  172. package/src/render/gpudriven/pipeline.ts +4 -3
  173. package/src/render/gpudriven/sunGlass.ts +117 -0
  174. package/src/render/lightBudget.ts +19 -5
  175. package/src/render/pointGlassTint.ts +250 -0
  176. package/src/render/pointShadowArray.ts +188 -22
  177. package/src/render/pointShadowBudget.ts +49 -39
  178. package/src/render/pointShadowMap.ts +41 -0
  179. package/src/render/recon/changeFrames.ts +24 -0
  180. package/src/render/recon/disocclusion.ts +54 -3
  181. package/src/render/recon/lateRouting.ts +29 -0
  182. package/src/render/recon/mover.ts +118 -0
  183. package/src/render/recon/resolve.ts +137 -10
  184. package/src/render/renderQuality.ts +47 -0
  185. package/src/render/rendererHarness.ts +12 -0
  186. package/src/render/sceneTarget.ts +66 -12
  187. package/src/render/shaders/depth.ts +109 -9
  188. package/src/render/shaders/flat/directionalShadow.ts +97 -6
  189. package/src/render/shaders/flat/glassTint.ts +25 -0
  190. package/src/render/shaders/flat/index.ts +17 -0
  191. package/src/render/shaders/flat/lampGlass.ts +185 -0
  192. package/src/render/shaders/flat/main.ts +115 -15
  193. package/src/render/shaders/flat/pointShadow.ts +2 -0
  194. package/src/render/shaders/flat/preamble.ts +21 -6
  195. package/src/render/shaders/generated/depth.wgsl.ts +234 -4
  196. package/src/render/shaders/generated/flat.wgsl.ts +376 -328
  197. package/src/render/shaders/generated/globalMedium.wgsl.ts +3 -13
  198. package/src/render/shaders/generated/lightVolume.wgsl.ts +4 -14
  199. package/src/render/shaders/generated/octahedralResolve.wgsl.ts +30 -0
  200. package/src/render/shaders/generated/temporalResolve.wgsl.ts +31 -6
  201. package/src/render/shaders/globalMedium.ts +6 -6
  202. package/src/render/shaders/gpudriven/blendRaster.wgsl.ts +88 -1
  203. package/src/render/shaders/gpudriven/cull.wgsl.ts +11 -2
  204. package/src/render/shaders/gpudriven/materialTable.wgsl.ts +3 -1
  205. package/src/render/shaders/gpudriven/shade.wgsl.ts +50 -1
  206. package/src/render/shaders/gpudriven/sunGlass.wgsl.ts +139 -0
  207. package/src/render/shaders/lightVolume.ts +6 -6
  208. package/src/render/shaders/octahedralResolve.ts +50 -17
  209. package/src/render/shaders/recon/lateDepth.wgsl.ts +47 -0
  210. package/src/render/shaders/recon/motion.wgsl.ts +84 -0
  211. package/src/render/shaders/recon/resolve.wgsl.ts +112 -8
  212. package/src/render/shaders/temporalResolve.ts +141 -5
  213. package/src/render/shadowCasters.ts +28 -1
  214. package/src/render/shadowMap.ts +231 -37
  215. package/src/render/sunGlassTint.ts +86 -0
  216. package/src/render/temporalAa.ts +227 -0
  217. package/src/render/temporalPass.ts +88 -12
  218. package/src/render/translucentQueue.ts +20 -1
@@ -0,0 +1,152 @@
1
+ /**
2
+ * What a reconstructing frame draws after the upscale: its blended draws, at the output size.
3
+ *
4
+ * **Why late.** A blended surface writes no depth the motion pass could test against and has no
5
+ * single motion per pixel — a caption over a wall is two surfaces moving two ways — so a
6
+ * reconstruction can only smear it. Drawn after the resolve, at output resolution and unjittered,
7
+ * it is as sharp as a native frame and has no history to ghost.
8
+ *
9
+ * **What it holds**: a command pool of its own, because the frame graph's pool is spent at every
10
+ * scene flush and these must survive until the reconstruction has run; an output-size depth
11
+ * target; and the pass that fills it from the render's depth. Cost: translucent pixels are shaded
12
+ * at output resolution — 2.25x as many at ratio 1.5, only where translucency is — plus one
13
+ * full-screen depth pass.
14
+ */
15
+ import { DEPTH_CLEAR, DEPTH_FORMAT } from '../../depthConvention.ts';
16
+ import { LATE_DEPTH_WGSL } from '../../shaders/recon/lateDepth.wgsl.ts';
17
+ import type { CommandPool, DrawCommand } from './drawCommand.ts';
18
+ import { createCommandPool, resetPool, takeCommand } from './drawCommand.ts';
19
+
20
+ export class LatePass {
21
+ private readonly device: GPUDevice;
22
+ private readonly commands: CommandPool = createCommandPool(64);
23
+ private depth: GPUTexture | null = null;
24
+ private depthTextureView: GPUTextureView | null = null;
25
+ private readonly layout: GPUBindGroupLayout;
26
+ private readonly pipeline: GPURenderPipeline;
27
+ private readonly sizes: GPUBuffer;
28
+ private readonly sizeStaging = new Float32Array(4);
29
+ private group: GPUBindGroup | null = null;
30
+ private groupSource: GPUTextureView | null = null;
31
+
32
+ constructor(device: GPUDevice) {
33
+ this.device = device;
34
+ this.layout = device.createBindGroupLayout({
35
+ label: 'recon.lateDepth.layout',
36
+ entries: [
37
+ { binding: 0, visibility: 0x2, texture: { sampleType: 'depth' } },
38
+ { binding: 1, visibility: 0x2, buffer: { type: 'uniform' } },
39
+ ],
40
+ });
41
+ const module = device.createShaderModule({ label: 'recon.lateDepth', code: LATE_DEPTH_WGSL });
42
+ this.pipeline = device.createRenderPipeline({
43
+ label: 'recon.lateDepth',
44
+ layout: device.createPipelineLayout({ bindGroupLayouts: [this.layout] }),
45
+ vertex: { module, entryPoint: 'lateDepthVert' },
46
+ fragment: { module, entryPoint: 'lateDepthFrag', targets: [] },
47
+ primitive: { topology: 'triangle-list' },
48
+ depthStencil: { format: DEPTH_FORMAT, depthWriteEnabled: true, depthCompare: 'always' },
49
+ });
50
+ this.sizes = device.createBuffer({
51
+ label: 'recon.lateDepth.sizes',
52
+ size: 16,
53
+ usage: 0x40 | 0x8, // UNIFORM | COPY_DST
54
+ });
55
+ }
56
+
57
+ /** The output-size depth, (re)made when the output changes size. */
58
+ depthView(width: number, height: number): GPUTextureView {
59
+ if (this.depth === null || this.depth.width !== width || this.depth.height !== height) {
60
+ this.depth?.destroy();
61
+ this.depth = this.device.createTexture({
62
+ label: 'recon.lateDepth',
63
+ size: [width, height],
64
+ format: DEPTH_FORMAT,
65
+ usage: 0x10, // RENDER_ATTACHMENT
66
+ });
67
+ this.depthTextureView = this.depth.createView();
68
+ }
69
+ return this.depthTextureView as GPUTextureView;
70
+ }
71
+
72
+ /** Fill the output-size depth from `source`, the render's single-sample depth. */
73
+ upscale(
74
+ encoder: GPUCommandEncoder,
75
+ source: GPUTextureView,
76
+ renderWidth: number,
77
+ renderHeight: number,
78
+ outputWidth: number,
79
+ outputHeight: number,
80
+ ): void {
81
+ const target = this.depthView(outputWidth, outputHeight);
82
+ this.sizeStaging[0] = renderWidth;
83
+ this.sizeStaging[1] = renderHeight;
84
+ this.sizeStaging[2] = outputWidth;
85
+ this.sizeStaging[3] = outputHeight;
86
+ this.device.queue.writeBuffer(this.sizes, 0, this.sizeStaging);
87
+ if (this.group === null || this.groupSource !== source) {
88
+ this.group = this.device.createBindGroup({
89
+ label: 'recon.lateDepth.group',
90
+ layout: this.layout,
91
+ entries: [
92
+ { binding: 0, resource: source },
93
+ { binding: 1, resource: { buffer: this.sizes } },
94
+ ],
95
+ });
96
+ this.groupSource = source;
97
+ }
98
+ const pass = encoder.beginRenderPass({
99
+ label: 'recon.lateDepth',
100
+ colorAttachments: [],
101
+ depthStencilAttachment: {
102
+ view: target,
103
+ depthClearValue: DEPTH_CLEAR,
104
+ depthLoadOp: 'clear',
105
+ depthStoreOp: 'store',
106
+ },
107
+ });
108
+ pass.setPipeline(this.pipeline);
109
+ pass.setBindGroup(0, this.group);
110
+ pass.draw(3);
111
+ pass.end();
112
+ }
113
+
114
+ /** A command slot for one late draw. Every field a caller uses it fills; see `takeCommand`. */
115
+ take(): DrawCommand {
116
+ const at = takeCommand(this.commands);
117
+ const command = this.commands.commands[at] as DrawCommand;
118
+ command.offsetCount = 0;
119
+ command.vertexCount = 0;
120
+ command.indexBuffer = null;
121
+ command.indexed = false;
122
+ command.instances = 1;
123
+ return command;
124
+ }
125
+
126
+ /** Late draws recorded this frame. */
127
+ get pending(): number {
128
+ return this.commands.taken;
129
+ }
130
+
131
+ /** Every late draw, in the order it was recorded, through the renderer's own issue. */
132
+ replay(
133
+ pass: GPURenderPassEncoder,
134
+ issue: (pass: GPURenderPassEncoder, command: DrawCommand) => void,
135
+ ): void {
136
+ for (let i = 0; i < this.commands.taken; i += 1) {
137
+ const command = this.commands.commands[i];
138
+ if (command !== undefined) issue(pass, command);
139
+ }
140
+ }
141
+
142
+ reset(): void {
143
+ resetPool(this.commands);
144
+ }
145
+
146
+ dispose(): void {
147
+ this.depth?.destroy();
148
+ this.depth = null;
149
+ this.depthTextureView = null;
150
+ this.sizes.destroy();
151
+ }
152
+ }
@@ -87,7 +87,11 @@ export function createLightVolumeBindGroupLayout(
87
87
  entries.push({
88
88
  binding: texture.texture,
89
89
  visibility: VISIBILITY_FRAGMENT,
90
- texture: { sampleType: 'unfilterable-float' },
90
+ texture: {
91
+ sampleType: 'unfilterable-float',
92
+ /* The sun's maps are one array since 4.5.0; see `shadowMap.ts`. */
93
+ ...(texture.type === 'sampler2DArray' ? { viewDimension: '2d-array' as const } : {}),
94
+ },
91
95
  });
92
96
  entries.push({
93
97
  binding: texture.sampler,
@@ -0,0 +1,535 @@
1
+ /**
2
+ * Where every mover was last frame, drawn into the reconstruction's motion target.
3
+ *
4
+ * Moved out of `renderer.ts` when it grew variants: the renderer decides *which* draws moved and
5
+ * hands them here; this owns how their motion is drawn. See `shaders/recon/motion.wgsl.ts` for what
6
+ * a texel carries, why it is a pass of its own, and why its matrices are unjittered.
7
+ */
8
+ import { DEPTH_COMPARE_EQUAL, DEPTH_FORMAT } from '../../depthConvention.ts';
9
+ import {
10
+ MOTION_DRAW_FLOATS,
11
+ MOTION_DRAW_STRIDE,
12
+ MOTION_FRAME_FLOATS,
13
+ MOTION_TARGET_FORMAT,
14
+ RECON_MOTION_WGSL,
15
+ } from '../../shaders/recon/motion.wgsl.ts';
16
+ import { INSTANCE_STRIDE } from '../../instances.ts';
17
+ import type { GpuMesh } from './buffers.ts';
18
+
19
+ /** What a record is, which decides the vertex stage and what else it binds. */
20
+ const RIGID = 0;
21
+ const SKINNED = 1;
22
+ const DYNAMIC = 2;
23
+ const INSTANCED = 3;
24
+
25
+ /** An instanced record's per-draw matrices are not read; this fills their slot. */
26
+ const IDENTITY = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
27
+
28
+ export class MotionPass {
29
+ private readonly device: GPUDevice;
30
+ /** The frame's movers, in the order they were drawn. */
31
+ private readonly meshes: GpuMesh[] = [];
32
+ /** Two matrices a mover — this frame's model and last frame's — at `MOTION_DRAW_FLOATS` apart. */
33
+ private matrices = new Float32Array(0);
34
+ private count = 0;
35
+ /** `RIGID` or `SKINNED` per record, grown beside `matrices`. */
36
+ private kinds = new Uint8Array(0);
37
+ /** A skinned record's two palettes, this frame's and last frame's, by record index. */
38
+ private readonly paletteNow: (GPUTextureView | null)[] = [];
39
+ private readonly paletteWas: (GPUTextureView | null)[] = [];
40
+ /** A rewritten record's last-frame rows, by record index. */
41
+ private readonly previousRows: (GPUBuffer | null)[] = [];
42
+ /** One pipeline per stride for rewritten meshes: current and previous rows at that stride. */
43
+ private readonly dynamicPipelines = new Map<number, GPURenderPipeline>();
44
+ /** An instanced record's placement this frame and last frame, and how many slots pair. */
45
+ private readonly instancesNow: (GPUBuffer | null)[] = [];
46
+ private readonly instancesWas: (GPUBuffer | null)[] = [];
47
+ private instanceCounts = new Uint32Array(0);
48
+ private readonly instancedPipelines = new Map<number, GPURenderPipeline>();
49
+ private layout: GPUBindGroupLayout | null = null;
50
+ private paletteLayout: GPUBindGroupLayout | null = null;
51
+ private skinnedLayout: GPUPipelineLayout | null = null;
52
+ /** Keyed by stride and the two skin offsets, packed into one number so a lookup builds nothing. */
53
+ private readonly skinnedPipelines = new Map<number, GPURenderPipeline>();
54
+ /**
55
+ * A bind group per pair of palette views. The ring keeps its textures from frame to frame, so a
56
+ * steady scene settles on a handful of pairs and builds nothing per frame.
57
+ */
58
+ private readonly paletteGroups = new Map<GPUTextureView, Map<GPUTextureView, GPUBindGroup>>();
59
+ private module: GPUShaderModule | null = null;
60
+ private pipelineLayout: GPUPipelineLayout | null = null;
61
+ private readonly pipelines = new Map<number, GPURenderPipeline>();
62
+ private frameBuffer: GPUBuffer | null = null;
63
+ private drawBuffer: GPUBuffer | null = null;
64
+ private drawCapacity = 0;
65
+ private group: GPUBindGroup | null = null;
66
+ private readonly frameStaging = new Float32Array(MOTION_FRAME_FLOATS);
67
+ private drawStaging = new Uint8Array(0);
68
+ /** A float view of `drawStaging`, made when it is, so a frame's upload allocates nothing. */
69
+ private drawFloats = new Float32Array(0);
70
+
71
+ constructor(device: GPUDevice) {
72
+ this.device = device;
73
+ }
74
+
75
+ /** A frame states its movers afresh. */
76
+ reset(): void {
77
+ this.count = 0;
78
+ }
79
+
80
+ /** A rigid mover: the same positions under last frame's model. */
81
+ recordRigid(mesh: GpuMesh, model: ArrayLike<number>, previousModel: ArrayLike<number>): void {
82
+ this.take(mesh, model, previousModel, RIGID);
83
+ }
84
+
85
+ /** A skinned mover: its positions under last frame's pose as well as last frame's model. */
86
+ recordSkinned(
87
+ mesh: GpuMesh,
88
+ model: ArrayLike<number>,
89
+ previousModel: ArrayLike<number>,
90
+ palette: GPUTextureView,
91
+ previousPalette: GPUTextureView,
92
+ ): void {
93
+ const at = this.take(mesh, model, previousModel, SKINNED);
94
+ this.paletteNow[at] = palette;
95
+ this.paletteWas[at] = previousPalette;
96
+ }
97
+
98
+ /** A rewritten mesh: its vertices as they were, read from `previous`, under last frame's model. */
99
+ recordDynamic(
100
+ mesh: GpuMesh,
101
+ model: ArrayLike<number>,
102
+ previousModel: ArrayLike<number>,
103
+ previous: GPUBuffer,
104
+ ): void {
105
+ const at = this.take(mesh, model, previousModel, DYNAMIC);
106
+ this.previousRows[at] = previous;
107
+ }
108
+
109
+ /** An instanced batch: its first `count` slots, each under last frame's placement of that slot. */
110
+ recordInstanced(
111
+ mesh: GpuMesh,
112
+ placement: GPUBuffer,
113
+ previousPlacement: GPUBuffer,
114
+ count: number,
115
+ ): void {
116
+ const at = this.take(mesh, IDENTITY, IDENTITY, INSTANCED);
117
+ this.instancesNow[at] = placement;
118
+ this.instancesWas[at] = previousPlacement;
119
+ this.instanceCounts[at] = count;
120
+ }
121
+
122
+ /** One record's slot: its mesh, its two matrices and its kind. Returns the record's index. */
123
+ private take(
124
+ mesh: GpuMesh,
125
+ model: ArrayLike<number>,
126
+ previousModel: ArrayLike<number>,
127
+ kind: number,
128
+ ): number {
129
+ const at = this.count;
130
+ const wanted = (at + 1) * MOTION_DRAW_FLOATS;
131
+ if (this.matrices.length < wanted) {
132
+ /* Doubling, so a scene settles on one allocation rather than one a draw. */
133
+ const grown = new Float32Array(Math.max(wanted, this.matrices.length * 2, 64));
134
+ grown.set(this.matrices);
135
+ this.matrices = grown;
136
+ const kinds = new Uint8Array(grown.length / MOTION_DRAW_FLOATS);
137
+ kinds.set(this.kinds);
138
+ this.kinds = kinds;
139
+ const counts = new Uint32Array(kinds.length);
140
+ counts.set(this.instanceCounts);
141
+ this.instanceCounts = counts;
142
+ }
143
+ this.matrices.set(model as ArrayLike<number> & Iterable<number>, at * MOTION_DRAW_FLOATS);
144
+ this.matrices.set(
145
+ previousModel as ArrayLike<number> & Iterable<number>,
146
+ at * MOTION_DRAW_FLOATS + 16,
147
+ );
148
+ this.meshes[at] = mesh;
149
+ this.kinds[at] = kind;
150
+ this.count = at + 1;
151
+ return at;
152
+ }
153
+
154
+ /**
155
+ * The pipeline layout and module, built once.
156
+ *
157
+ * **The position alone**, at offset zero of the mesh's interleaved rows: that is all the rigid
158
+ * stage reads, and positions are the layout's first attribute and never optional.
159
+ */
160
+ private buildLayout(): void {
161
+ if (this.layout !== null) return;
162
+ this.layout = this.device.createBindGroupLayout({
163
+ label: 'recon.motion.layout',
164
+ entries: [
165
+ { binding: 0, visibility: 0x1, buffer: { type: 'uniform' as const } },
166
+ {
167
+ binding: 1,
168
+ visibility: 0x1,
169
+ buffer: { type: 'uniform' as const, hasDynamicOffset: true },
170
+ },
171
+ ],
172
+ });
173
+ this.module = this.device.createShaderModule({
174
+ label: 'recon.motion',
175
+ code: RECON_MOTION_WGSL,
176
+ });
177
+ this.pipelineLayout = this.device.createPipelineLayout({
178
+ bindGroupLayouts: [this.layout],
179
+ });
180
+ /* The two palettes, read with `textureLoad` at integer texels: unfilterable is exact. */
181
+ this.paletteLayout = this.device.createBindGroupLayout({
182
+ label: 'recon.motion.palettes',
183
+ entries: [
184
+ { binding: 0, visibility: 0x1, texture: { sampleType: 'unfilterable-float' as const } },
185
+ { binding: 1, visibility: 0x1, texture: { sampleType: 'unfilterable-float' as const } },
186
+ ],
187
+ });
188
+ this.skinnedLayout = this.device.createPipelineLayout({
189
+ bindGroupLayouts: [this.layout, this.paletteLayout],
190
+ });
191
+ }
192
+
193
+ /**
194
+ * The skinned pipeline for one stride and one placement of the joints and weights in it.
195
+ *
196
+ * Same fragment stage, winding and depth test as the rigid one — see `pipelineFor` — with the two
197
+ * skinning attributes read from the interleaved rows at the offsets the mesh records.
198
+ */
199
+ private skinnedPipelineFor(
200
+ stride: number,
201
+ offsets: { readonly joints: number; readonly weights: number },
202
+ ): GPURenderPipeline | null {
203
+ const key = (stride * 4096 + offsets.joints) * 4096 + offsets.weights;
204
+ const held = this.skinnedPipelines.get(key);
205
+ if (held !== undefined) return held;
206
+ const module = this.module;
207
+ const layout = this.skinnedLayout;
208
+ if (module === null || layout === null) return null;
209
+ const pipeline = this.device.createRenderPipeline({
210
+ label: `recon.motion.skinned.${String(stride)}`,
211
+ layout,
212
+ vertex: {
213
+ module,
214
+ entryPoint: 'motionSkinnedVert',
215
+ buffers: [
216
+ {
217
+ arrayStride: stride,
218
+ attributes: [
219
+ { shaderLocation: 0, offset: 0, format: 'float32x3' },
220
+ { shaderLocation: 11, offset: offsets.joints, format: 'float32x4' },
221
+ { shaderLocation: 12, offset: offsets.weights, format: 'float32x4' },
222
+ ],
223
+ },
224
+ ],
225
+ },
226
+ fragment: { module, entryPoint: 'motionFrag', targets: [{ format: MOTION_TARGET_FORMAT }] },
227
+ primitive: { topology: 'triangle-list', cullMode: 'back' },
228
+ depthStencil: {
229
+ format: DEPTH_FORMAT,
230
+ depthWriteEnabled: false,
231
+ depthCompare: DEPTH_COMPARE_EQUAL,
232
+ },
233
+ });
234
+ this.skinnedPipelines.set(key, pipeline);
235
+ return pipeline;
236
+ }
237
+
238
+ /** The rewritten pipeline for one stride: position from each of two buffers of that stride. */
239
+ private dynamicPipelineFor(stride: number): GPURenderPipeline | null {
240
+ const held = this.dynamicPipelines.get(stride);
241
+ if (held !== undefined) return held;
242
+ const module = this.module;
243
+ const layout = this.pipelineLayout;
244
+ if (module === null || layout === null) return null;
245
+ const pipeline = this.device.createRenderPipeline({
246
+ label: `recon.motion.dynamic.${String(stride)}`,
247
+ layout,
248
+ vertex: {
249
+ module,
250
+ entryPoint: 'motionDynamicVert',
251
+ buffers: [
252
+ {
253
+ arrayStride: stride,
254
+ attributes: [{ shaderLocation: 0, offset: 0, format: 'float32x3' }],
255
+ },
256
+ {
257
+ arrayStride: stride,
258
+ attributes: [{ shaderLocation: 1, offset: 0, format: 'float32x3' }],
259
+ },
260
+ ],
261
+ },
262
+ fragment: { module, entryPoint: 'motionFrag', targets: [{ format: MOTION_TARGET_FORMAT }] },
263
+ primitive: { topology: 'triangle-list', cullMode: 'back' },
264
+ depthStencil: {
265
+ format: DEPTH_FORMAT,
266
+ depthWriteEnabled: false,
267
+ depthCompare: DEPTH_COMPARE_EQUAL,
268
+ },
269
+ });
270
+ this.dynamicPipelines.set(stride, pipeline);
271
+ return pipeline;
272
+ }
273
+
274
+ /**
275
+ * The instanced pipeline for one mesh stride: the mesh's positions, then this frame's and last
276
+ * frame's placements, one matrix an instance, as the scene's instanced stage reads them.
277
+ */
278
+ private instancedPipelineFor(stride: number): GPURenderPipeline | null {
279
+ const held = this.instancedPipelines.get(stride);
280
+ if (held !== undefined) return held;
281
+ const module = this.module;
282
+ const layout = this.pipelineLayout;
283
+ if (module === null || layout === null) return null;
284
+ const placement = (first: number): GPUVertexBufferLayout => ({
285
+ arrayStride: INSTANCE_STRIDE,
286
+ stepMode: 'instance',
287
+ attributes: [
288
+ { shaderLocation: first, offset: 0, format: 'float32x4' },
289
+ { shaderLocation: first + 1, offset: 16, format: 'float32x4' },
290
+ { shaderLocation: first + 2, offset: 32, format: 'float32x4' },
291
+ { shaderLocation: first + 3, offset: 48, format: 'float32x4' },
292
+ ],
293
+ });
294
+ const pipeline = this.device.createRenderPipeline({
295
+ label: `recon.motion.instanced.${String(stride)}`,
296
+ layout,
297
+ vertex: {
298
+ module,
299
+ entryPoint: 'motionInstancedVert',
300
+ buffers: [
301
+ {
302
+ arrayStride: stride,
303
+ attributes: [{ shaderLocation: 0, offset: 0, format: 'float32x3' }],
304
+ },
305
+ placement(2),
306
+ placement(6),
307
+ ],
308
+ },
309
+ fragment: { module, entryPoint: 'motionFrag', targets: [{ format: MOTION_TARGET_FORMAT }] },
310
+ primitive: { topology: 'triangle-list', cullMode: 'back' },
311
+ depthStencil: {
312
+ format: DEPTH_FORMAT,
313
+ depthWriteEnabled: false,
314
+ depthCompare: DEPTH_COMPARE_EQUAL,
315
+ },
316
+ });
317
+ this.instancedPipelines.set(stride, pipeline);
318
+ return pipeline;
319
+ }
320
+
321
+ /** The bind group holding one pair of palettes, built the first time the pair is seen. */
322
+ private paletteGroup(now: GPUTextureView, was: GPUTextureView): GPUBindGroup | null {
323
+ const layout = this.paletteLayout;
324
+ if (layout === null) return null;
325
+ let byWas = this.paletteGroups.get(now);
326
+ if (byWas === undefined) {
327
+ byWas = new Map();
328
+ this.paletteGroups.set(now, byWas);
329
+ }
330
+ let group = byWas.get(was);
331
+ if (group === undefined) {
332
+ group = this.device.createBindGroup({
333
+ label: 'recon.motion.palettes',
334
+ layout,
335
+ entries: [
336
+ { binding: 0, resource: now },
337
+ { binding: 1, resource: was },
338
+ ],
339
+ });
340
+ byWas.set(was, group);
341
+ }
342
+ return group;
343
+ }
344
+
345
+ /** The pipeline for one vertex stride, built on first sight of it. */
346
+ private pipelineFor(stride: number): GPURenderPipeline | null {
347
+ const held = this.pipelines.get(stride);
348
+ if (held !== undefined) return held;
349
+ const module = this.module;
350
+ const layout = this.pipelineLayout;
351
+ if (module === null || layout === null) return null;
352
+ const pipeline = this.device.createRenderPipeline({
353
+ label: `recon.motion.${String(stride)}`,
354
+ layout,
355
+ vertex: {
356
+ module,
357
+ entryPoint: 'motionVert',
358
+ buffers: [
359
+ {
360
+ arrayStride: stride,
361
+ attributes: [{ shaderLocation: 0, offset: 0, format: 'float32x3' }],
362
+ },
363
+ ],
364
+ },
365
+ fragment: { module, entryPoint: 'motionFrag', targets: [{ format: MOTION_TARGET_FORMAT }] },
366
+ /*
367
+ * **The scene's own winding**, because this draws the scene's geometry through the scene's
368
+ * clip transform — the product the renderer hands over already carries the negation that
369
+ * `naga` writes into every generated vertex stage, so the faces come out the way they did
370
+ * and a `cw` front face here would draw the inside of every mesh. §3 row 57.
371
+ */
372
+ primitive: { topology: 'triangle-list', cullMode: 'back' },
373
+ depthStencil: {
374
+ format: DEPTH_FORMAT,
375
+ /*
376
+ * **Tested and not written.** The frame's depth is finished; this pass only wants the
377
+ * pixels the scene actually kept, and `DEPTH_COMPARE_EQUAL` is the comparison that admits
378
+ * exactly them — the same transform on the same positions gives the same depth, which is
379
+ * why the rasterisation matrix is handed over whole rather than rebuilt here.
380
+ *
381
+ * **And the equality is not fragile here**, which was worth measuring rather than
382
+ * assuming: this driver contracts multiply-adds (§3 row 107) and two shader modules can
383
+ * contract a matrix product differently, so an equality across them could have admitted
384
+ * only some of a surface. Run with `greater-equal` instead, the ghost measured the same
385
+ * to the pixel — 5,942 either way — so nothing is being dropped.
386
+ */
387
+ depthWriteEnabled: false,
388
+ depthCompare: DEPTH_COMPARE_EQUAL,
389
+ },
390
+ });
391
+ this.pipelines.set(stride, pipeline);
392
+ return pipeline;
393
+ }
394
+
395
+ /**
396
+ * Every mover of the frame, into `target`, tested against the finished `depth`.
397
+ *
398
+ * **After everything that draws and before the resolve reads it.** `raster` is the scene's clip
399
+ * transform with the generated stages' y negation folded in; `viewProj` and `previousViewProj`
400
+ * are this frame's and last frame's unjittered view-projections.
401
+ *
402
+ * **Cleared every frame, even when nothing moved.** A texel left from last frame is a flag saying
403
+ * "a draw wrote this", and the resolve would take a motion belonging to a surface that is no
404
+ * longer there. The clear is the pass's own load operation, so an empty frame still runs it.
405
+ */
406
+ run(
407
+ encoder: GPUCommandEncoder,
408
+ target: GPUTextureView,
409
+ depth: GPUTextureView,
410
+ raster: Float32Array,
411
+ viewProj: Float32Array,
412
+ previousViewProj: Float32Array,
413
+ timestampWrites: GPURenderPassTimestampWrites | undefined,
414
+ ): void {
415
+ this.buildLayout();
416
+ const layout = this.layout;
417
+ if (layout === null) return;
418
+ const pass = encoder.beginRenderPass({
419
+ label: 'recon.motion',
420
+ timestampWrites,
421
+ colorAttachments: [
422
+ { view: target, loadOp: 'clear', storeOp: 'store', clearValue: [0, 0, 0, 0] },
423
+ ],
424
+ depthStencilAttachment: {
425
+ view: depth,
426
+ depthLoadOp: 'load',
427
+ depthStoreOp: 'store',
428
+ depthReadOnly: false,
429
+ },
430
+ });
431
+
432
+ const count = this.count;
433
+ if (count > 0) {
434
+ const bytes = count * MOTION_DRAW_STRIDE;
435
+ if (this.drawCapacity < count) {
436
+ this.drawBuffer?.destroy();
437
+ this.drawCapacity = Math.max(count, this.drawCapacity * 2, 16);
438
+ this.drawBuffer = this.device.createBuffer({
439
+ label: 'recon.motion.draws',
440
+ size: this.drawCapacity * MOTION_DRAW_STRIDE,
441
+ usage: 0x40 | 0x8, // UNIFORM | COPY_DST
442
+ });
443
+ this.drawStaging = new Uint8Array(this.drawCapacity * MOTION_DRAW_STRIDE);
444
+ this.drawFloats = new Float32Array(this.drawStaging.buffer);
445
+ this.group = null;
446
+ }
447
+ this.frameBuffer ??= this.device.createBuffer({
448
+ label: 'recon.motion.frame',
449
+ size: MOTION_FRAME_FLOATS * 4,
450
+ usage: 0x40 | 0x8,
451
+ });
452
+ this.group ??= this.device.createBindGroup({
453
+ label: 'recon.motion.group',
454
+ layout,
455
+ entries: [
456
+ { binding: 0, resource: { buffer: this.frameBuffer } },
457
+ {
458
+ binding: 1,
459
+ resource: { buffer: this.drawBuffer as GPUBuffer, size: MOTION_DRAW_STRIDE },
460
+ },
461
+ ],
462
+ });
463
+
464
+ this.frameStaging.set(raster, 0);
465
+ this.frameStaging.set(viewProj, 16);
466
+ this.frameStaging.set(previousViewProj, 32);
467
+ this.device.queue.writeBuffer(this.frameBuffer, 0, this.frameStaging);
468
+
469
+ /* Copied a float at a time rather than through `subarray`, which is a view allocated per
470
+ mover per frame. */
471
+ for (let i = 0; i < count; i += 1) {
472
+ const from = i * MOTION_DRAW_FLOATS;
473
+ const to = (i * MOTION_DRAW_STRIDE) / 4;
474
+ for (let k = 0; k < MOTION_DRAW_FLOATS; k += 1) {
475
+ this.drawFloats[to + k] = this.matrices[from + k] as number;
476
+ }
477
+ }
478
+ this.device.queue.writeBuffer(this.drawBuffer as GPUBuffer, 0, this.drawStaging, 0, bytes);
479
+
480
+ let bound: GPURenderPipeline | null = null;
481
+ for (let i = 0; i < count; i += 1) {
482
+ const mesh = this.meshes[i];
483
+ if (mesh === undefined) continue;
484
+ const kind = this.kinds[i];
485
+ let pipeline: GPURenderPipeline | null = null;
486
+ let instances = 1;
487
+ if (kind === INSTANCED) {
488
+ const now = this.instancesNow[i] ?? null;
489
+ const was = this.instancesWas[i] ?? null;
490
+ if (now === null || was === null) continue;
491
+ pipeline = this.instancedPipelineFor(mesh.vertexStride);
492
+ pass.setVertexBuffer(1, now);
493
+ pass.setVertexBuffer(2, was);
494
+ instances = this.instanceCounts[i] ?? 0;
495
+ } else if (kind === DYNAMIC) {
496
+ const previous = this.previousRows[i] ?? null;
497
+ if (previous === null) continue;
498
+ pipeline = this.dynamicPipelineFor(mesh.vertexStride);
499
+ pass.setVertexBuffer(1, previous);
500
+ } else if (kind === SKINNED) {
501
+ const offsets = mesh.skinOffsets;
502
+ const now = this.paletteNow[i] ?? null;
503
+ const was = this.paletteWas[i] ?? null;
504
+ if (offsets === null || now === null || was === null) continue;
505
+ pipeline = this.skinnedPipelineFor(mesh.vertexStride, offsets);
506
+ const palettes = this.paletteGroup(now, was);
507
+ if (pipeline === null || palettes === null) continue;
508
+ pass.setBindGroup(1, palettes);
509
+ } else {
510
+ pipeline = this.pipelineFor(mesh.vertexStride);
511
+ }
512
+ if (pipeline === null) continue;
513
+ if (pipeline !== bound) {
514
+ pass.setPipeline(pipeline);
515
+ bound = pipeline;
516
+ }
517
+ pass.setBindGroup(0, this.group, [i * MOTION_DRAW_STRIDE]);
518
+ pass.setVertexBuffer(0, mesh.vertexBuffers[0] as GPUBuffer);
519
+ pass.setIndexBuffer(mesh.indexBuffer, 'uint32');
520
+ pass.drawIndexed(mesh.indexCount, instances);
521
+ }
522
+ }
523
+ pass.end();
524
+ }
525
+
526
+ dispose(): void {
527
+ this.paletteGroups.clear();
528
+ this.frameBuffer?.destroy();
529
+ this.drawBuffer?.destroy();
530
+ this.frameBuffer = null;
531
+ this.drawBuffer = null;
532
+ this.drawCapacity = 0;
533
+ this.group = null;
534
+ }
535
+ }