@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
@@ -154,6 +154,225 @@ export function clipToNeighbourhood(
154
154
  return out;
155
155
  }
156
156
 
157
+ /**
158
+ * **The anti-flicker: a still pixel's neighbourhood widened by its own flicker.**
159
+ *
160
+ * A thread, a crack of light round a door, a leaf finer than a pixel: the jitter catches it in some
161
+ * phases and misses it in others, and in a phase that misses it the nine taps round a pixel can all
162
+ * miss it at once. The box collapses onto the background, the history — which had settled on the
163
+ * thread's share of the pixel — is clipped out, and it grows back a tenth a frame until the next
164
+ * miss. That is a paused frame flashing at every fine edge, and the clip is doing its job: nothing
165
+ * in one frame says the thread is still there.
166
+ *
167
+ * What does say so is the pixel's own record. **A still camera over a still scene samples every pixel
168
+ * the same way every period of the jitter, exactly**, so aliasing repeats itself; a surface that moves
169
+ * across the pixel, a flame, a light that shimmers, does not. So each pixel keeps the mean of its
170
+ * samples over the last period, the sum it is gathering for this one, how many periods running have
171
+ * repeated, and the running mean of how far its samples land from their period's mean — and its box
172
+ * widens by that spread only once two periods running have repeated. Measured on the samples rather
173
+ * than against the history, because the history is what the clip cuts. **And a sample farther from
174
+ * its period's mean than the box would widen to is not the flicker the pixel has shown**: something
175
+ * has arrived, and the count starts again on that frame rather than at the end of the period. **Two, and a mean rather than
176
+ * one sample**, because one sample repeats by coincidence: stripes sliding across a pixel land on the
177
+ * same colour at two period starts about as often as not.
178
+ *
179
+ * **What it gives up.** A surface arriving over a pixel that was flickering is held back until the
180
+ * period ends and fails to repeat — at most eight frames, fading a tenth a frame meanwhile; a still
181
+ * pixel needs three periods before it is helped at all; and a pixel that flickers in a scene that is
182
+ * itself moving is not helped, since nothing repeats there. Nothing moving past a twentieth of a
183
+ * pixel a frame is touched, since the reprojection is then what decides.
184
+ *
185
+ * **A moving camera had a weaker proof of its own, and it was taken out.** It widened a pixel's box
186
+ * while its samples swung both ways about their mean, with the record carried through the
187
+ * reprojection. Measured on a moving courtyard it removed a twentieth of what flickered, and it broke
188
+ * a sun's shadow line on the floor into dashes that were not there before: the record is read at the
189
+ * nearest texel, so under sub-pixel motion one pixel widened and its neighbour did not. The resolve
190
+ * already removes nineteen twentieths of a moving frame's flicker without it. **What would make it wrong** is a jitter
191
+ * whose sequence does not repeat, where no period is the same twice however still the scene is.
192
+ */
193
+
194
+ /** This frame's share of the running spread, and of a period's mean: one period of the jitter. */
195
+ export const FLICKER_MEMORY = 1 / JITTER_PERIOD;
196
+
197
+ /**
198
+ * How many spreads a flickering pixel's box widens by. The hardest thread is one missed in a single
199
+ * phase of eight: that miss needs the box to reach 7/8 of its contrast, and the running spread,
200
+ * worn down by seven catches that each land an eighth of the contrast from the period's mean, has
201
+ * fallen to a = 1/8 + (7/8 a + 7/64 − 1/8)·(7/8)⁷ of the contrast by then — 0.181, a steady state
202
+ * solved by hand — against the 7/32 it averages. Seven eighths over that is 4.8; six leaves room.
203
+ * Threads caught in a share nearer a half spread more and need fewer.
204
+ */
205
+ export const FLICKER_WIDTH = 6;
206
+
207
+ /**
208
+ * How far apart two periods' means may be, relative to the larger, and still repeat: fully below
209
+ * this, not at all at twice it. Above an eight-bit record's rounding and a lamp's slow breathing; far
210
+ * below what anything crossing the pixel does to it.
211
+ */
212
+ export const FLICKER_REPEAT = 0.05;
213
+
214
+ /**
215
+ * How much of the history a pixel proven to repeat keeps, where an unproven one keeps
216
+ * `TEMPORAL_HISTORY_BLEND`. A settled thread still breathes with the jitter by the share of each
217
+ * frame's sample that goes into what is shown — a tenth of its contrast at the base blend — and a
218
+ * pixel that has repeated for two periods has nothing to respond to, so it keeps more: three
219
+ * hundredths of each frame instead of a tenth. What it costs is a surface arriving inside the
220
+ * pixel's own swing, which fades in at that rate until the period's check lets it through.
221
+ */
222
+ export const FLICKER_BLEND = 0.97;
223
+
224
+ /**
225
+ * How far last frame's depth may be from the depth a surface should have had there, relative, and
226
+ * still be that surface: two percent beyond the pixel's own depth swing, taken as the nearest of the
227
+ * texel where it stood and the four beside it. **The nearest of five and not the one**, because this
228
+ * frame's sample and last frame's can straddle an edge with nothing moved — the lesson
229
+ * `recon/resolve.ts` learned the expensive way.
230
+ *
231
+ * **And beyond its own swing, because depth aliases the way colour does.** A crack of light round a
232
+ * door is a pixel whose sample lands on the door in some phases and on what is behind it in others,
233
+ * so its depth jumps metres with nothing moved, and a test held to two percent reset the record of
234
+ * exactly the pixels the anti-flicker is for: a held frame went from 5 flickering pixels back to 774.
235
+ * So each pixel keeps the running mean of how far last frame's depth missed — kept whatever the test
236
+ * says, since resetting it would stop it ever learning the swing — and the test allows
237
+ * `FLICKER_WIDTH` of those, as the box does of the colour's. A flat surface's swing is none and its
238
+ * test stays tight; a surface revealed by the camera is a jump nothing in the pixel's past explains.
239
+ */
240
+ export const FLICKER_SURFACE = 0.02;
241
+
242
+ /** Whether a record follows the surface it was kept for: see `FLICKER_SURFACE`. */
243
+ export function sameSurface(expected: number, stood: ArrayLike<number>, swing: number): boolean {
244
+ return missedBy(expected, stood) <= FLICKER_WIDTH * swing + FLICKER_SURFACE * expected;
245
+ }
246
+
247
+ /**
248
+ * How far the nearest of last frame's depths missed the one expected. A depth of nought is a texel
249
+ * with nothing recorded — the frame after a cut — and is no texel; with none, nothing is missed by
250
+ * any amount.
251
+ */
252
+ export function missedBy(expected: number, stood: ArrayLike<number>): number {
253
+ let nearest = Number.POSITIVE_INFINITY;
254
+ for (let i = 0; i < stood.length; i++) {
255
+ const depth = stood[i] as number;
256
+ if (depth > 0) nearest = Math.min(nearest, Math.abs(depth - expected));
257
+ }
258
+ return nearest;
259
+ }
260
+
261
+ /**
262
+ * The pixel's depth swing carried on: the running mean of `missedBy`, over a period, and none where
263
+ * there was nothing to miss — a swing learned from a cut would loosen the test for a whole period.
264
+ */
265
+ export function nextDepthSwing(swing: number, missed: number): number {
266
+ return Number.isFinite(missed) ? swing + (missed - swing) * FLICKER_MEMORY : 0;
267
+ }
268
+
269
+ /**
270
+ * Reprojected motion in pixels a frame: still below the first, moving from the second. **Small,
271
+ * because repetition breaks long before motion is visible**: a camera drifting a fifth of a pixel a
272
+ * frame shifts the grid more than a pixel a period, so nothing repeats, and a ramp to half a pixel
273
+ * counted that as mostly still and kept vouching for samples that had stopped repeating.
274
+ */
275
+ export const STILL_FROM = 0.01;
276
+ export const STILL_TO = 0.05;
277
+
278
+ /**
279
+ * A pixel's flicker record, in luma: the running spread of its samples about their period's mean,
280
+ * the sum this period has gathered so far, the last period's mean, and how many periods running have
281
+ * repeated — none, one or two, as 0, 0.5 and 1.
282
+ */
283
+ export interface Flicker {
284
+ spread: number;
285
+ sum: number;
286
+ mean: number;
287
+ repeated: number;
288
+ }
289
+
290
+ /**
291
+ * A fresh record for a surface first seen this frame: no spread and nothing repeated. **A
292
+ * record of another surface is no record**, so the resolve starts one over wherever last frame's
293
+ * depth says the surface is not the one it followed, before this frame widens by anything.
294
+ */
295
+ export function freshFlicker(out: Flicker, sample: number): Flicker {
296
+ out.spread = 0;
297
+ out.sum = sample * FLICKER_MEMORY;
298
+ out.mean = sample;
299
+ out.repeated = 0;
300
+ return out;
301
+ }
302
+
303
+ function smoothstep(edge0: number, edge1: number, x: number): number {
304
+ const t = Math.min(1, Math.max(0, (x - edge0) / (edge1 - edge0)));
305
+ return t * t * (3 - 2 * t);
306
+ }
307
+
308
+ /** Rec. 709 luma, which is what the record measures in. */
309
+ export function luma(c: readonly [number, number, number]): number {
310
+ return c[0] * 0.2126 + c[1] * 0.7152 + c[2] * 0.0722;
311
+ }
312
+
313
+ /** One for a pixel whose reprojection did not move, falling to none at a twentieth of a pixel. */
314
+ export function stillness(motionPixels: number): number {
315
+ return 1 - smoothstep(STILL_FROM, STILL_TO, motionPixels);
316
+ }
317
+
318
+ /**
319
+ * Whether a pixel is proven to repeat this frame: two periods running have, and this frame's sample
320
+ * lands within the reach of the box it would widen to. A sample beyond that reach is something
321
+ * arriving, and it disqualifies the frame it lands in as well as the count it carries out.
322
+ */
323
+ export function proven(record: Flicker, sample: number): boolean {
324
+ return record.repeated > 0.75 && within(record, sample);
325
+ }
326
+
327
+ /** Whether this frame's sample lands inside the reach of the box the record would widen to. */
328
+ function within(record: Flicker, sample: number): boolean {
329
+ return Math.abs(sample - record.mean) <= FLICKER_WIDTH * record.spread + 1 / 255;
330
+ }
331
+
332
+ /** How far a pixel's box widens this frame: by its spread, once proven, and only while still. */
333
+ export function flickerWidening(record: Flicker, sample: number, still: number): number {
334
+ return proven(record, sample) ? FLICKER_WIDTH * record.spread * still : 0;
335
+ }
336
+
337
+ /** How much of the history this frame keeps: the base blend, or more once proven. */
338
+ export function flickerBlend(base: number, record: Flicker, sample: number, still: number): number {
339
+ return proven(record, sample) ? base + (FLICKER_BLEND - base) * still : base;
340
+ }
341
+
342
+ /** Whether two periods' means are the same, one to zero. */
343
+ export function repeats(mean: number, previous: number): number {
344
+ const apart = Math.abs(mean - previous) / Math.max(mean, previous, 1 / 255);
345
+ return 1 - smoothstep(FLICKER_REPEAT, 2 * FLICKER_REPEAT, apart);
346
+ }
347
+
348
+ /**
349
+ * The record carried out, from the one carried in and this frame's sample in luma. `periodStart`
350
+ * is the first frame of the jitter's period, where the period just gathered is compared with the
351
+ * one before. Forgotten in proportion to motion — a moving pixel's record would be a record of
352
+ * somewhere else.
353
+ */
354
+ export function nextFlicker(
355
+ out: Flicker,
356
+ previous: Flicker,
357
+ sample: number,
358
+ still: number,
359
+ periodStart: boolean,
360
+ ): Flicker {
361
+ const apart = Math.abs(sample - previous.mean);
362
+ let { sum, mean, repeated } = previous;
363
+ if (apart > FLICKER_WIDTH * previous.spread + 1 / 255) repeated = 0;
364
+ if (periodStart) {
365
+ repeated = repeats(sum, mean) > 0.5 ? Math.min(1, repeated + 0.5) : 0;
366
+ mean = sum;
367
+ sum = 0;
368
+ }
369
+ out.spread = previous.spread + (apart - previous.spread) * FLICKER_MEMORY;
370
+ out.sum = sum + sample * FLICKER_MEMORY;
371
+ out.mean = mean;
372
+ out.repeated = repeated * still;
373
+ return out;
374
+ }
375
+
157
376
  /**
158
377
  * Whether the last frame left a picture this one may sample, and where the jitter has got to.
159
378
  *
@@ -185,6 +404,14 @@ export class TemporalHistory {
185
404
  return usable;
186
405
  }
187
406
 
407
+ /**
408
+ * Whether this frame samples the first phase of the jitter's period, where the anti-flicker
409
+ * compares the period just gathered with the one before. One answer for both backends.
410
+ */
411
+ get periodStart(): boolean {
412
+ return this.frameIndex % JITTER_PERIOD === 0;
413
+ }
414
+
188
415
  /** A resolved picture is now in the history, at the size the frame was opened with. */
189
416
  accumulated(): void {
190
417
  this.picture = true;
@@ -15,6 +15,9 @@ import { TEMPORAL_RESOLVE_FRAG } from './shaders/temporalResolve.ts';
15
15
  * grade all read the scene texture, and reading the unresolved one would grade a stable picture
16
16
  * from an unstable source and put the crawl back on screen through the bloom.
17
17
  */
18
+ /** The resolve's three outputs: the picture and the two halves of the flicker record. */
19
+ const DRAW_ALL = [0x8ce0, 0x8ce1, 0x8ce2]; // COLOR_ATTACHMENT0, 1, 2
20
+
18
21
  export class TemporalPass {
19
22
  private readonly program: WebGLProgram;
20
23
  private readonly uniforms: Record<string, WebGLUniformLocation | null>;
@@ -22,6 +25,21 @@ export class TemporalPass {
22
25
 
23
26
  /** The pair, and which of them the next resolve writes into. */
24
27
  private textures: (WebGLTexture | null)[] = [null, null];
28
+ /**
29
+ * Each pixel's flicker record, a pair beside the history's and swapped with it: see the
30
+ * anti-flicker in `temporalAa.ts`. In the history's own format, which holds what the record does —
31
+ * a change and a sample are colours' luma, and eight bits hold them wherever eight bits hold the
32
+ * colour.
33
+ */
34
+ private records: (WebGLTexture | null)[] = [null, null];
35
+ /** The rest of each pixel's record — depth, pull and recent swing — beside the first. */
36
+ private motions: (WebGLTexture | null)[] = [null, null];
37
+ /**
38
+ * Whether the records can be half float, which the depth in them needs. Asked of the context
39
+ * rather than taken from the scene's format, since a record of an eight-bit scene still holds
40
+ * metres. Without it the anti-flicker is off and the resolve is the one before it existed.
41
+ */
42
+ private readonly recordFloat: boolean;
25
43
  private framebuffers: (WebGLFramebuffer | null)[] = [null, null];
26
44
  private write = 0;
27
45
 
@@ -40,6 +58,7 @@ export class TemporalPass {
40
58
  */
41
59
  private readonly floatColor: boolean,
42
60
  ) {
61
+ this.recordFloat = gl.getExtension('EXT_color_buffer_float') !== null;
43
62
  this.program = compileProgram(gl, FULLSCREEN_VERT, TEMPORAL_RESOLVE_FRAG, 'temporalResolve');
44
63
  this.uniforms = uniformLocations(gl, this.program, 'temporalPass');
45
64
  const vao = gl.createVertexArray();
@@ -55,19 +74,45 @@ export class TemporalPass {
55
74
 
56
75
  for (let i = 0; i < 2; i++) {
57
76
  if (this.textures[i] === null) this.textures[i] = gl.createTexture();
58
- gl.bindTexture(gl.TEXTURE_2D, this.textures[i]);
59
- if (this.floatColor) {
60
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, width, height, 0, gl.RGBA, gl.HALF_FLOAT, null);
61
- } else {
62
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
77
+ if (this.records[i] === null) this.records[i] = gl.createTexture();
78
+ if (this.motions[i] === null) this.motions[i] = gl.createTexture();
79
+ for (const texture of [this.textures[i], this.records[i], this.motions[i]]) {
80
+ gl.bindTexture(gl.TEXTURE_2D, texture);
81
+ if (texture === this.textures[i] ? this.floatColor : this.recordFloat) {
82
+ /* The depth record is two numbers a pixel: metres, and how far they swing. */
83
+ const pair = texture === this.motions[i];
84
+ gl.texImage2D(
85
+ gl.TEXTURE_2D,
86
+ 0,
87
+ pair ? gl.RG16F : gl.RGBA16F,
88
+ width,
89
+ height,
90
+ 0,
91
+ pair ? gl.RG : gl.RGBA,
92
+ gl.HALF_FLOAT,
93
+ null,
94
+ );
95
+ } else {
96
+ gl.texImage2D(
97
+ gl.TEXTURE_2D,
98
+ 0,
99
+ gl.RGBA,
100
+ width,
101
+ height,
102
+ 0,
103
+ gl.RGBA,
104
+ gl.UNSIGNED_BYTE,
105
+ null,
106
+ );
107
+ }
108
+ /* Linear, because the history is sampled between texels wherever the reprojection lands;
109
+ clamped, because a sample that wrapped would pull the far edge of the screen into the
110
+ corner — and the shader rejects an off-screen reprojection before it can anyway. */
111
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
112
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
113
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
114
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
63
115
  }
64
- /* Linear, because the history is sampled between texels wherever the reprojection lands;
65
- clamped, because a sample that wrapped would pull the far edge of the screen into the
66
- corner — and the shader rejects an off-screen reprojection before it can anyway. */
67
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
68
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
69
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
70
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
71
116
 
72
117
  if (this.framebuffers[i] === null) this.framebuffers[i] = gl.createFramebuffer();
73
118
  gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffers[i]);
@@ -78,6 +123,22 @@ export class TemporalPass {
78
123
  this.textures[i],
79
124
  0,
80
125
  );
126
+ gl.framebufferTexture2D(
127
+ gl.FRAMEBUFFER,
128
+ gl.COLOR_ATTACHMENT1,
129
+ gl.TEXTURE_2D,
130
+ this.records[i],
131
+ 0,
132
+ );
133
+ gl.framebufferTexture2D(
134
+ gl.FRAMEBUFFER,
135
+ gl.COLOR_ATTACHMENT2,
136
+ gl.TEXTURE_2D,
137
+ this.motions[i],
138
+ 0,
139
+ );
140
+ /* Per framebuffer, so set once here: the resolve writes the picture and the record. */
141
+ gl.drawBuffers(DRAW_ALL);
81
142
  }
82
143
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
83
144
  }
@@ -96,6 +157,8 @@ export class TemporalPass {
96
157
  width: number,
97
158
  height: number,
98
159
  blend: number,
160
+ periodStart: boolean,
161
+ depthToViewZ: Float32Array,
99
162
  ): WebGLTexture {
100
163
  const { gl } = this;
101
164
  this.ensureSize(width, height);
@@ -120,10 +183,19 @@ export class TemporalPass {
120
183
  gl.activeTexture(gl.TEXTURE2);
121
184
  gl.bindTexture(gl.TEXTURE_2D, depth);
122
185
  gl.uniform1i(this.uniforms['uDepth'] ?? null, 2);
186
+ gl.activeTexture(gl.TEXTURE3);
187
+ gl.bindTexture(gl.TEXTURE_2D, this.records[history]);
188
+ gl.uniform1i(this.uniforms['uFlicker'] ?? null, 3);
189
+ gl.activeTexture(gl.TEXTURE4);
190
+ gl.bindTexture(gl.TEXTURE_2D, this.motions[history]);
191
+ gl.uniform1i(this.uniforms['uFlickerMotion'] ?? null, 4);
192
+ gl.uniform4fv(this.uniforms['uDepthToViewZ'] ?? null, depthToViewZ);
193
+ gl.uniform1f(this.uniforms['uAntiFlicker'] ?? null, this.recordFloat ? 1 : 0);
123
194
 
124
195
  gl.uniformMatrix4fv(this.uniforms['uReprojection'] ?? null, false, reprojection);
125
196
  gl.uniform2f(this.uniforms['uTexel'] ?? null, 1 / width, 1 / height);
126
197
  gl.uniform1f(this.uniforms['uHistoryBlend'] ?? null, blend);
198
+ gl.uniform1f(this.uniforms['uPeriodStart'] ?? null, periodStart ? 1 : 0);
127
199
 
128
200
  gl.bindVertexArray(this.vao);
129
201
  gl.drawArrays(gl.TRIANGLES, 0, 3);
@@ -141,8 +213,12 @@ export class TemporalPass {
141
213
  const { gl } = this;
142
214
  for (let i = 0; i < 2; i++) {
143
215
  if (this.textures[i] !== null) gl.deleteTexture(this.textures[i]);
216
+ if (this.records[i] !== null) gl.deleteTexture(this.records[i]);
217
+ if (this.motions[i] !== null) gl.deleteTexture(this.motions[i]);
144
218
  if (this.framebuffers[i] !== null) gl.deleteFramebuffer(this.framebuffers[i]);
145
219
  this.textures[i] = null;
220
+ this.records[i] = null;
221
+ this.motions[i] = null;
146
222
  this.framebuffers[i] = null;
147
223
  }
148
224
  gl.deleteProgram(this.program);
@@ -3,6 +3,8 @@ import type { ReadonlyMat4 } from 'gl-matrix';
3
3
  /* Type-only, and from the backend that declares it — the same import `backend/api.ts` makes,
4
4
  for the same reason: the shape belongs to the draw verb and erases at compile time. */
5
5
  import type { TranslucentMeshOptions } from './backend/webgl2/renderer.ts';
6
+ import { resolveGlass } from './glass.ts';
7
+ import type { ResolvedGlass } from './glass.ts';
6
8
 
7
9
  /**
8
10
  * The frame's translucent draws, held so they can be submitted more than once.
@@ -51,10 +53,14 @@ export interface TranslucentDraw {
51
53
  /** What survives one metre of the medium. Null where the draw named none; otherwise a copy. */
52
54
  refractTint: Float32Array | null;
53
55
  thicknessM: number;
56
+ /** Glass, resolved, or null for a draw that is not glass. See `glass.ts`. */
57
+ glass: ResolvedGlass | null;
54
58
  }
55
59
 
56
60
  /** A record with its storage, reused across frames. */
57
61
  interface PooledDraw extends TranslucentDraw {
62
+ /** Where `glass` points when set: allocated once with the record, never released. */
63
+ readonly glassStore: ResolvedGlass;
58
64
  /** Allocated once and pointed at by `tint` when a draw has one. */
59
65
  readonly tintStore: Float32Array;
60
66
  /** The same arrangement for the refraction tint: allocated once, never released. */
@@ -77,6 +83,8 @@ function newRecord(): PooledDraw {
77
83
  refractTint: null,
78
84
  thicknessM: 0,
79
85
  refractTintStore: new Float32Array(3),
86
+ glass: null,
87
+ glassStore: { transmission: 0, frost: 0, tint: [1, 1, 1] },
80
88
  };
81
89
  }
82
90
 
@@ -84,6 +92,7 @@ export class TranslucentQueue {
84
92
  private readonly pool: PooledDraw[] = [];
85
93
  private count = 0;
86
94
  private refracting = 0;
95
+ private glassing = 0;
87
96
 
88
97
  /** How many draws this frame has recorded. */
89
98
  get length(): number {
@@ -98,6 +107,11 @@ export class TranslucentQueue {
98
107
  return this.refracting > 0;
99
108
  }
100
109
 
110
+ /** Whether anything recorded this frame is glass, so the copy it reads needs its mip chain. */
111
+ get glassy(): boolean {
112
+ return this.glassing > 0;
113
+ }
114
+
101
115
  /** How many records are pooled, which a test uses to assert a steady scene stops allocating. */
102
116
  get capacity(): number {
103
117
  return this.pool.length;
@@ -137,7 +151,11 @@ export class TranslucentQueue {
137
151
  record.tint = record.tintStore;
138
152
  }
139
153
  record.refraction = options.refraction ?? 0;
140
- if (record.refraction > 0) this.refracting++;
154
+ /* Glass reads the copy too, whether or not it bends: see `glass.ts`. */
155
+ const glassy = resolveGlass(options.glass, record.glassStore);
156
+ record.glass = glassy ? record.glassStore : null;
157
+ if (glassy) this.glassing++;
158
+ if (record.refraction > 0 || glassy) this.refracting++;
141
159
  record.thicknessM = options.thicknessM ?? 0;
142
160
  const refractTint = options.refractTint ?? null;
143
161
  if (refractTint === null) {
@@ -168,5 +186,6 @@ export class TranslucentQueue {
168
186
  reset(): void {
169
187
  this.count = 0;
170
188
  this.refracting = 0;
189
+ this.glassing = 0;
171
190
  }
172
191
  }