@swf2es/render 0.1.24 → 0.1.26

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.
@@ -1,5 +1,6 @@
1
1
  import type { GlState } from "@swf2es/context3d/device";
2
2
  import { type Geometries, type GeometryData, GLYPH_FLOATS } from "../geometry.js";
3
+ import { FreeRanges } from "./free-ranges.js";
3
4
  import { GlyphTables } from "./glyph-tables.js";
4
5
  import {
5
6
  COLOR_AT,
@@ -23,8 +24,6 @@ const GLYPH_BYTES = GLYPH_FLOATS * 4;
23
24
  const GLYPHS_LEAST = 1024;
24
25
  /** The fewest vertices the atlas holds room for, and its indices thrice that. */
25
26
  const LEAST = 1 << 16;
26
- /** Holes past which, and past the live vertices, the atlas is packed again. */
27
- const SLACK = 1 << 16;
28
27
  /**
29
28
  * The vertices, and the indices, staged before an upload: geometries placed
30
29
  * one after another go to the GPU in one call for each buffer, not two a
@@ -69,10 +68,27 @@ function interleave(
69
68
  }
70
69
 
71
70
  /**
72
- * Where a geometry lies in the atlas, or the glyph atlas with its text's
73
- * mode and table, and their slot in the glyph tables (-1 past their room).
71
+ * A vertex and an index buffer of the atlas, its vertex array reading
72
+ * them, the room of each and the vertices live in it.
73
+ */
74
+ export interface Page {
75
+ /** Its index in the atlas' pages, which places name it by. */
76
+ index: number;
77
+ vertexArray: WebGLVertexArrayObject;
78
+ vertexBuffer: WebGLBuffer;
79
+ indexBuffer: WebGLBuffer;
80
+ vertices: FreeRanges;
81
+ indices: FreeRanges;
82
+ live: number;
83
+ }
84
+
85
+ /**
86
+ * Where a geometry lies in the atlas, its page and its ranges there, or in
87
+ * the glyph atlas with its text's mode and table, and their slot in the
88
+ * glyph tables (-1 past their room).
74
89
  */
75
90
  export interface Place {
91
+ page: number;
76
92
  vertexStart: number;
77
93
  vertices: number;
78
94
  indexStart: number;
@@ -85,31 +101,31 @@ export interface Place {
85
101
  }
86
102
 
87
103
  /**
88
- * The geometry atlas: every geometry copied once into a vertex and an index
89
- * buffer, and advanced text's glyph quads into an atlas of their own, where
90
- * the draws find them by their places. A geometry let go of leaves a hole,
91
- * and each atlas is packed again from the geometries' own arrays once the
92
- * holes outweigh what is live.
104
+ * The geometry atlas: every geometry copied once into a page's vertex and
105
+ * index buffers, and advanced text's glyph quads into an atlas of their
106
+ * own, where the draws find them by their places. A geometry let go of
107
+ * leaves holes in its page, which later geometries are placed in once the
108
+ * top is full; a page is packed again from the geometries' own arrays only
109
+ * when what is added finds no room. Repacking every geometry for its holes
110
+ * alone made frames that copied and uploaded the whole atlas again, tens of
111
+ * megabytes, every few seconds of a busy SWF. The glyph atlas is packed
112
+ * again once its holes outweigh what is live, which also frees the texts'
113
+ * tables' slots.
93
114
  */
94
115
  export class GeometryAtlas {
95
116
  frame = 0;
96
117
  private readonly gl: WebGL2RenderingContext;
97
118
  private readonly state: GlState;
98
- vertexArray!: WebGLVertexArrayObject;
99
- private vertexBuffer!: WebGLBuffer;
100
- private indexBuffer!: WebGLBuffer;
101
- private vertexCapacity = 0;
102
- private indexCapacity = 0;
103
- private vertexTop = 0;
104
- private indexTop = 0;
105
- private live = 0;
106
- private holes = 0;
107
- /** Vertices staged for the atlas from vertex `stagedVertexAt`, as floats and as words. */
119
+ /** The pages, one for now, which places name by index. */
120
+ pages: Page[] = [];
121
+ /** The page the staging is for. */
122
+ private stagedPage: Page | null = null;
123
+ /** Vertices staged for `stagedPage` from vertex `stagedVertexAt`, as floats and as words. */
108
124
  private readonly stagedFloats = new Float32Array(STAGED_VERTICES * (VERTEX_BYTES / 4));
109
125
  private readonly stagedWords = new Uint32Array(this.stagedFloats.buffer);
110
126
  private stagedVertexAt = 0;
111
127
  private stagedVertices = 0;
112
- /** Indices staged for the atlas from index `stagedIndexAt`. */
128
+ /** Indices staged for `stagedPage` from index `stagedIndexAt`. */
113
129
  private readonly stagedIndices = new Uint32Array(STAGED_INDICES);
114
130
  private stagedIndexAt = 0;
115
131
  private stagedIndexCount = 0;
@@ -138,21 +154,7 @@ export class GeometryAtlas {
138
154
  /** The buffers and vertex arrays made, both atlases empty, as the context comes or comes back. */
139
155
  setUp(): void {
140
156
  const gl = this.gl;
141
- this.vertexArray = gl.createVertexArray() as WebGLVertexArrayObject;
142
- this.vertexBuffer = gl.createBuffer() as WebGLBuffer;
143
- this.indexBuffer = gl.createBuffer() as WebGLBuffer;
144
- this.state.bindVertexArray(this.vertexArray);
145
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
146
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
147
- gl.enableVertexAttribArray(POSITION_AT);
148
- gl.vertexAttribPointer(POSITION_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 0);
149
- gl.enableVertexAttribArray(TEXEL_AT);
150
- gl.vertexAttribPointer(TEXEL_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 8);
151
- gl.enableVertexAttribArray(COLOR_AT);
152
- gl.vertexAttribPointer(COLOR_AT, 4, gl.UNSIGNED_BYTE, true, VERTEX_BYTES, 16);
153
- // The same bytes as a mesh's depth, which the mesh program reads in place of the colour.
154
- gl.enableVertexAttribArray(DEPTH_AT);
155
- gl.vertexAttribPointer(DEPTH_AT, 1, gl.FLOAT, false, VERTEX_BYTES, 16);
157
+ this.pages = [this.makePage(0)];
156
158
  this.glyphArray = gl.createVertexArray() as WebGLVertexArrayObject;
157
159
  this.glyphBuffer = gl.createBuffer() as WebGLBuffer;
158
160
  this.glyphIndexBuffer = gl.createBuffer() as WebGLBuffer;
@@ -179,40 +181,79 @@ export class GeometryAtlas {
179
181
  live: 0,
180
182
  holes: 0,
181
183
  });
182
- this.vertexCapacity = 0;
183
- this.indexCapacity = 0;
184
+ this.stagedPage = null;
184
185
  this.stagedVertices = 0;
185
186
  this.stagedIndexCount = 0;
186
187
  this.places = [];
187
188
  this.tables.setUp();
188
189
  }
189
190
 
191
+ /** A page's buffers and vertex array made, with no room till it is packed. */
192
+ private makePage(index: number): Page {
193
+ const gl = this.gl;
194
+ const page: Page = {
195
+ index,
196
+ vertexArray: gl.createVertexArray() as WebGLVertexArrayObject,
197
+ vertexBuffer: gl.createBuffer() as WebGLBuffer,
198
+ indexBuffer: gl.createBuffer() as WebGLBuffer,
199
+ vertices: new FreeRanges(),
200
+ indices: new FreeRanges(),
201
+ live: 0,
202
+ };
203
+ this.state.bindVertexArray(page.vertexArray);
204
+ gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
205
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, page.indexBuffer);
206
+ gl.enableVertexAttribArray(POSITION_AT);
207
+ gl.vertexAttribPointer(POSITION_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 0);
208
+ gl.enableVertexAttribArray(TEXEL_AT);
209
+ gl.vertexAttribPointer(TEXEL_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 8);
210
+ gl.enableVertexAttribArray(COLOR_AT);
211
+ gl.vertexAttribPointer(COLOR_AT, 4, gl.UNSIGNED_BYTE, true, VERTEX_BYTES, 16);
212
+ // The same bytes as a mesh's depth, which the mesh program reads in place of the colour.
213
+ gl.enableVertexAttribArray(DEPTH_AT);
214
+ gl.vertexAttribPointer(DEPTH_AT, 1, gl.FLOAT, false, VERTEX_BYTES, 16);
215
+ return page;
216
+ }
217
+
218
+ /** Geometry `id`'s place, if it has one, let go of: a shape's ranges free in its page. */
219
+ private letGo(id: number): void {
220
+ const place = this.places[id];
221
+ if (!place) {
222
+ return;
223
+ }
224
+
225
+ this.places[id] = null;
226
+ if (place.glyphs) {
227
+ this.glyphs.live -= place.vertices;
228
+ this.glyphs.holes += place.vertices;
229
+ } else {
230
+ const page = this.pages[place.page];
231
+ page.vertices.give(place.vertexStart, place.vertices);
232
+ page.indices.give(place.indexStart, place.indices);
233
+ page.live -= place.vertices;
234
+ }
235
+ }
236
+
190
237
  /**
191
238
  * The geometries removed since the last frame let go of, and those added
192
- * copied in: at the top while they fit, else into an atlas packed again,
193
- * grown to twice what is live.
239
+ * copied in: the shapes together at their page's top or in a hole that
240
+ * holds them all, else each in the first hole that holds it, else into a
241
+ * page packed again with room for twice what is live; a text at the
242
+ * glyph atlas' top, else into one packed again.
194
243
  */
195
244
  place(geometries: Geometries): void {
196
245
  this.frame++;
197
246
  for (const id of geometries.removed) {
198
- const place = this.places[id];
199
- if (place?.glyphs) {
200
- this.places[id] = null;
201
- this.glyphs.live -= place.vertices;
202
- this.glyphs.holes += place.vertices;
203
- } else if (place) {
204
- this.places[id] = null;
205
- this.live -= place.vertices;
206
- this.holes += place.vertices;
207
- }
247
+ this.letGo(id);
208
248
  }
209
249
 
210
250
  geometries.removed.length = 0;
211
251
  // Nothing left in it: filled again from its start, in the buffers it has.
212
- if (this.live === 0) {
213
- this.vertexTop = 0;
214
- this.indexTop = 0;
215
- this.holes = 0;
252
+ for (const page of this.pages) {
253
+ if (page.live === 0) {
254
+ page.vertices.reset(page.vertices.capacity);
255
+ page.indices.reset(page.indices.capacity);
256
+ }
216
257
  }
217
258
 
218
259
  // Changed in place: copied again where they lie, else as added.
@@ -220,18 +261,10 @@ export class GeometryAtlas {
220
261
  const place = this.places[id];
221
262
  const data = geometries.get(id);
222
263
  if (place && data && !data.glyphs && place.vertices === data.positions.length / 2) {
223
- this.copyVertices(data, place.vertexStart);
264
+ this.copyVertices(this.pages[place.page], data, place.vertexStart);
224
265
  place.batch = this.batch(data);
225
266
  } else if (place && data) {
226
- this.places[id] = null;
227
- if (place.glyphs) {
228
- this.glyphs.live -= place.vertices;
229
- this.glyphs.holes += place.vertices;
230
- } else {
231
- this.live -= place.vertices;
232
- this.holes += place.vertices;
233
- }
234
-
267
+ this.letGo(id);
235
268
  geometries.added.push(id);
236
269
  }
237
270
  }
@@ -253,12 +286,11 @@ export class GeometryAtlas {
253
286
  }
254
287
 
255
288
  // Each atlas packed again on its own, so that texts' quads made again often never repack
256
- // the shapes', nor shapes the texts'.
289
+ // the shapes', nor shapes the texts'. The shapes' page only when what is added outweighs its
290
+ // room, or below, when holes too small for one of them leave it none.
257
291
  const g = this.glyphs;
258
- const shapes =
259
- this.vertexTop + vertices > this.vertexCapacity ||
260
- this.indexTop + indices > this.indexCapacity ||
261
- (this.holes > SLACK && this.holes > this.live);
292
+ const page = this.pages[0];
293
+ let shapes = vertices > page.vertices.room() || indices > page.indices.room();
262
294
  const texts =
263
295
  g.top + glyphVertices > g.capacity ||
264
296
  g.indexTop + glyphIndices > g.indexCapacity ||
@@ -272,10 +304,51 @@ export class GeometryAtlas {
272
304
  this.repackGlyphs(geometries);
273
305
  }
274
306
 
307
+ // The shapes added together in one range where one holds them all, at the top or in a
308
+ // hole, so that they go up in one upload each for vertices and indices; else each in the
309
+ // first that holds it, which a churning atlas would leave to a call or two a geometry.
310
+ let vertexAt = -1;
311
+ let indexAt = -1;
312
+ if (!shapes && vertices > 0) {
313
+ vertexAt = page.vertices.take(vertices);
314
+ indexAt = vertexAt < 0 ? -1 : page.indices.take(indices);
315
+ if (vertexAt >= 0 && indexAt < 0) {
316
+ page.vertices.give(vertexAt, vertices);
317
+ vertexAt = -1;
318
+ }
319
+ }
320
+
275
321
  for (const id of geometries.added) {
276
322
  const data = geometries.get(id);
277
- if (data && !(data.glyphs ? texts : shapes)) {
278
- this.copyIn(id, data);
323
+ if (!data) {
324
+ continue;
325
+ }
326
+
327
+ // Added twice in a frame, as an id let go of and taken again can be: its first copy's
328
+ // room given back, which only packing would otherwise find again. A packed atlas holds
329
+ // its geometries already.
330
+ if (data.glyphs) {
331
+ if (!texts) {
332
+ this.letGo(id);
333
+ this.copyGlyphs(id, data);
334
+ }
335
+
336
+ continue;
337
+ }
338
+
339
+ if (shapes) {
340
+ continue;
341
+ }
342
+
343
+ this.letGo(id);
344
+ if (vertexAt >= 0) {
345
+ this.copyAt(page, id, data, vertexAt, indexAt);
346
+ vertexAt += data.positions.length / 2;
347
+ indexAt += data.indices.length;
348
+ } else if (!this.copyIn(page, id, data)) {
349
+ // Packing copies every live shape, those added after this one too.
350
+ shapes = true;
351
+ this.repack(geometries);
279
352
  }
280
353
  }
281
354
 
@@ -284,7 +357,7 @@ export class GeometryAtlas {
284
357
  this.tables.upload();
285
358
  }
286
359
 
287
- /** Every live shape's geometry copied into new buffers, with room for twice what is live. */
360
+ /** Every live shape copied into its page's buffers made anew, with room for twice them. */
288
361
  repack(geometries: Geometries): void {
289
362
  const gl = this.gl;
290
363
  const ids = geometries.live();
@@ -298,23 +371,22 @@ export class GeometryAtlas {
298
371
  }
299
372
  }
300
373
 
301
- this.vertexCapacity = Math.max(LEAST, vertices * 2);
302
- this.indexCapacity = Math.max(LEAST * 3, indices * 2);
303
- this.state.bindVertexArray(this.vertexArray);
304
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
305
- gl.bufferData(gl.ARRAY_BUFFER, this.vertexCapacity * VERTEX_BYTES, gl.STATIC_DRAW);
306
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.indexCapacity * 4, gl.STATIC_DRAW);
374
+ const page = this.pages[0];
375
+ page.vertices.reset(Math.max(LEAST, vertices * 2));
376
+ page.indices.reset(Math.max(LEAST * 3, indices * 2));
377
+ page.live = 0;
378
+ this.state.bindVertexArray(page.vertexArray);
379
+ gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
380
+ gl.bufferData(gl.ARRAY_BUFFER, page.vertices.capacity * VERTEX_BYTES, gl.STATIC_DRAW);
381
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, page.indices.capacity * 4, gl.STATIC_DRAW);
307
382
  // What was staged for the buffers replaced is copied again below.
308
383
  this.stagedVertices = 0;
309
384
  this.stagedIndexCount = 0;
310
- this.vertexTop = 0;
311
- this.indexTop = 0;
312
- this.live = 0;
313
- this.holes = 0;
385
+ // Each at the top, which holds twice them all.
314
386
  for (const id of ids) {
315
387
  const data = geometries.get(id) as GeometryData;
316
388
  if (!data.glyphs) {
317
- this.copyIn(id, data);
389
+ this.copyIn(page, id, data);
318
390
  }
319
391
  }
320
392
 
@@ -358,18 +430,40 @@ export class GeometryAtlas {
358
430
  this.tables.upload();
359
431
  }
360
432
 
361
- /** `data` copied in at the top of the atlas, its indices made absolute. */
362
- private copyIn(id: number, data: GeometryData): void {
363
- if (data.glyphs) {
364
- this.copyGlyphs(id, data);
365
- return;
433
+ /**
434
+ * A shape's `data` copied into `page`, at its top or in a hole, its
435
+ * indices made absolute from where its vertices went; false where the
436
+ * page has no room for it.
437
+ */
438
+ private copyIn(page: Page, id: number, data: GeometryData): boolean {
439
+ const count = data.positions.length / 2;
440
+ const base = page.vertices.take(count);
441
+ if (base < 0) {
442
+ return false;
443
+ }
444
+
445
+ const indexStart = page.indices.take(data.indices.length);
446
+ if (indexStart < 0) {
447
+ page.vertices.give(base, count);
448
+ return false;
366
449
  }
367
450
 
451
+ this.copyAt(page, id, data, base, indexStart);
452
+ return true;
453
+ }
454
+
455
+ /** A shape's `data` copied into `page`'s room taken for it, from vertex `base` and index `indexStart`. */
456
+ private copyAt(
457
+ page: Page,
458
+ id: number,
459
+ data: GeometryData,
460
+ base: number,
461
+ indexStart: number,
462
+ ): void {
368
463
  const count = data.positions.length / 2;
369
- const base = this.vertexTop;
370
- this.copyVertices(data, base);
371
464
  const source = data.indices;
372
465
  const length = source.length;
466
+ this.copyVertices(page, data, base);
373
467
  if (length > STAGED_INDICES) {
374
468
  const indices = new Uint32Array(length);
375
469
  for (let i = 0; i < length; i++) {
@@ -377,15 +471,16 @@ export class GeometryAtlas {
377
471
  }
378
472
 
379
473
  this.flushIndices();
380
- this.state.bindVertexArray(this.vertexArray);
381
- this.gl.bufferSubData(this.gl.ELEMENT_ARRAY_BUFFER, this.indexTop * 4, indices);
474
+ this.state.bindVertexArray(page.vertexArray);
475
+ this.gl.bufferSubData(this.gl.ELEMENT_ARRAY_BUFFER, indexStart * 4, indices);
382
476
  } else {
477
+ this.stagePage(page);
383
478
  if (
384
479
  this.stagedIndexCount + length > STAGED_INDICES ||
385
- this.stagedIndexAt + this.stagedIndexCount !== this.indexTop
480
+ this.stagedIndexAt + this.stagedIndexCount !== indexStart
386
481
  ) {
387
482
  this.flushIndices();
388
- this.stagedIndexAt = this.indexTop;
483
+ this.stagedIndexAt = indexStart;
389
484
  }
390
485
 
391
486
  const staged = this.stagedIndices;
@@ -398,31 +493,31 @@ export class GeometryAtlas {
398
493
  }
399
494
 
400
495
  this.places[id] = {
496
+ page: page.index,
401
497
  vertexStart: base,
402
498
  vertices: count,
403
- indexStart: this.indexTop,
499
+ indexStart,
404
500
  indices: length,
405
501
  glyphs: null,
406
502
  flat: data.flat === true,
407
503
  batch: this.batch(data),
408
504
  };
409
- this.vertexTop += count;
410
- this.indexTop += length;
411
- this.live += count;
505
+ page.live += count;
412
506
  }
413
507
 
414
- /** `data`'s vertices interleaved for the atlas from vertex `base`: staged, or uploaded if too many. */
415
- private copyVertices(data: GeometryData, base: number): void {
508
+ /** `data`'s vertices interleaved for `page` from vertex `base`: staged, or uploaded if too many. */
509
+ private copyVertices(page: Page, data: GeometryData, base: number): void {
416
510
  const count = data.positions.length / 2;
417
511
  if (count > STAGED_VERTICES) {
418
512
  const floats = new Float32Array(count * (VERTEX_BYTES / 4));
419
513
  interleave(data, count, floats, new Uint32Array(floats.buffer), 0);
420
514
  this.flushVertices();
421
- this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer);
515
+ this.gl.bindBuffer(this.gl.ARRAY_BUFFER, page.vertexBuffer);
422
516
  this.gl.bufferSubData(this.gl.ARRAY_BUFFER, base * VERTEX_BYTES, floats);
423
517
  return;
424
518
  }
425
519
 
520
+ this.stagePage(page);
426
521
  if (
427
522
  this.stagedVertices + count > STAGED_VERTICES ||
428
523
  this.stagedVertexAt + this.stagedVertices !== base
@@ -441,10 +536,18 @@ export class GeometryAtlas {
441
536
  this.flushIndices();
442
537
  }
443
538
 
539
+ /** The staging made `page`'s, what it held for another uploaded first. */
540
+ private stagePage(page: Page): void {
541
+ if (this.stagedPage !== page) {
542
+ this.flush();
543
+ this.stagedPage = page;
544
+ }
545
+ }
546
+
444
547
  private flushVertices(): void {
445
548
  if (this.stagedVertices > 0) {
446
549
  const gl = this.gl;
447
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
550
+ gl.bindBuffer(gl.ARRAY_BUFFER, (this.stagedPage as Page).vertexBuffer);
448
551
  gl.bufferSubData(
449
552
  gl.ARRAY_BUFFER,
450
553
  this.stagedVertexAt * VERTEX_BYTES,
@@ -459,7 +562,7 @@ export class GeometryAtlas {
459
562
  private flushIndices(): void {
460
563
  if (this.stagedIndexCount > 0) {
461
564
  const gl = this.gl;
462
- this.state.bindVertexArray(this.vertexArray);
565
+ this.state.bindVertexArray((this.stagedPage as Page).vertexArray);
463
566
  gl.bufferSubData(
464
567
  gl.ELEMENT_ARRAY_BUFFER,
465
568
  this.stagedIndexAt * 4,
@@ -493,6 +596,7 @@ export class GeometryAtlas {
493
596
  gl.bufferSubData(gl.ARRAY_BUFFER, g.top * GLYPH_BYTES, glyphs.vertices);
494
597
  gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, g.indexTop * 4, indices);
495
598
  this.places[id] = {
599
+ page: 0,
496
600
  vertexStart: g.top,
497
601
  vertices: count,
498
602
  indexStart: g.indexTop,
@@ -513,9 +617,12 @@ export class GeometryAtlas {
513
617
  /** The buffers and vertex arrays made on the context, deleted. */
514
618
  destroy(): void {
515
619
  const gl = this.gl;
516
- gl.deleteVertexArray(this.vertexArray);
517
- gl.deleteBuffer(this.vertexBuffer);
518
- gl.deleteBuffer(this.indexBuffer);
620
+ for (const page of this.pages) {
621
+ gl.deleteVertexArray(page.vertexArray);
622
+ gl.deleteBuffer(page.vertexBuffer);
623
+ gl.deleteBuffer(page.indexBuffer);
624
+ }
625
+
519
626
  gl.deleteVertexArray(this.glyphArray);
520
627
  gl.deleteBuffer(this.glyphBuffer);
521
628
  gl.deleteBuffer(this.glyphIndexBuffer);
@@ -38,6 +38,7 @@ import { FilterRunner } from "./filters.js";
38
38
  import { GpuTextures } from "./gpu-textures.js";
39
39
  import { HeldLayers } from "./holds.js";
40
40
  import { KernelPrograms } from "./kernels.js";
41
+ import { ProgramQueue } from "./precompile.js";
41
42
  import { LAYER_UNIT, TEXTURE_UNIT } from "./programs.js";
42
43
  import { Stage } from "./stage.js";
43
44
  import { type Layer, type Open, objectOf, type Target, TargetPool } from "./targets.js";
@@ -122,8 +123,10 @@ export class WebGLBackend {
122
123
  private readonly holds: HeldLayers;
123
124
  /** Pixel Bender kernels' programs, which the filters, the compositor and the draws share. */
124
125
  private readonly kernels: KernelPrograms;
125
- /** The filters' passes, their programs made as first run. */
126
+ /** The filters' passes, their programs linked ahead (programs). */
126
127
  private filters!: FilterRunner<Layer>;
128
+ /** Programs linked and warmed over the frames after the first, ahead of their first use. */
129
+ private programs!: ProgramQueue;
127
130
  /** The layers drawn into now, innermost last. */
128
131
  private layers: Open[] = [];
129
132
  /** The frame's filters being drawn, whose chains blend shaders' modes name (KERNEL). */
@@ -139,7 +142,7 @@ export class WebGLBackend {
139
142
  private readonly rect = { x: 0, y: 0, width: 0, height: 0 };
140
143
  /** The mask counts of the targets below the layers drawn into now, two numbers each. */
141
144
  private readonly below: number[] = [];
142
- /** The program `compositeOver` draws with, linked as first used; null till then and after a lost context. */
145
+ /** The program `compositeOver` draws with, linked ahead (programs); null till then and after a lost context. */
143
146
  private overProgram: OverProgram | null = null;
144
147
  /** Framebuffers onto registry textures, drawn into or read, by texture id, with the texture they were made for. */
145
148
  private textureFramebuffers: ({ texture: WebGLTexture; framebuffer: WebGLFramebuffer } | null)[] =
@@ -214,6 +217,12 @@ export class WebGLBackend {
214
217
  const textureMost = gl.getParameter(gl.MAX_TEXTURE_SIZE) as number;
215
218
  this.draws.setUp(textureMost);
216
219
  this.compositor.setUp();
220
+ // A lost context's programs went with it: all queued again.
221
+ this.programs = new ProgramQueue(gl, this.state);
222
+ this.overProgram = null;
223
+ this.programs.add("over", OVER_VERTEX, OVER_FRAGMENT, [TEXTURE_UNIT, LAYER_UNIT], (program) =>
224
+ this.overMade(program),
225
+ );
217
226
  this.filters = new FilterRunner(
218
227
  gl,
219
228
  this.state,
@@ -224,6 +233,7 @@ export class WebGLBackend {
224
233
  sampler: (bits) => this.gpu.sampler(bits),
225
234
  },
226
235
  this.kernels,
236
+ this.programs,
227
237
  );
228
238
  this.holds.setUp(this.filters, textureMost);
229
239
  this.pool.setUp();
@@ -233,7 +243,6 @@ export class WebGLBackend {
233
243
  this.gpu.setUp();
234
244
  // The context's own, gone with it.
235
245
  this.textureFramebuffers = [];
236
- this.overProgram = null;
237
246
  }
238
247
 
239
248
  /**
@@ -484,6 +493,8 @@ export class WebGLBackend {
484
493
  // The free layers, and the stage's (stageAlpha), undrawn into for LAYER_IDLE_FRAMES let go of.
485
494
  this.stage.dropIdleLayer();
486
495
  this.pool.endFrame();
496
+ // Once the first frame is drawn, the programs not drawn with yet are made, a frame's share.
497
+ this.programs.step();
487
498
  }
488
499
  }
489
500
 
@@ -626,15 +637,20 @@ export class WebGLBackend {
626
637
  this.pool.giveLayer(below);
627
638
  }
628
639
 
629
- /** compositeOver's program, linked as first asked for. */
640
+ /** compositeOver's program: linked ahead, its link finished now if not yet, or linked now. */
630
641
  private overProgramMade(): OverProgram {
631
- if (this.overProgram) {
632
- return this.overProgram;
642
+ if (!this.overProgram && !this.programs.take("over")) {
643
+ const program = linkProgram(this.gl, OVER_VERTEX, OVER_FRAGMENT);
644
+ this.state.useProgram(program);
645
+ this.overMade(program);
633
646
  }
634
647
 
648
+ return this.overProgram as OverProgram;
649
+ }
650
+
651
+ /** compositeOver's program of `program`, linked and in use, its samplers told their units. */
652
+ private overMade(program: WebGLProgram): void {
635
653
  const gl = this.gl;
636
- const program = linkProgram(gl, OVER_VERTEX, OVER_FRAGMENT);
637
- this.state.useProgram(program);
638
654
  gl.uniform1i(gl.getUniformLocation(program, "uSource"), TEXTURE_UNIT);
639
655
  gl.uniform1i(gl.getUniformLocation(program, "uBelow"), LAYER_UNIT);
640
656
  this.overProgram = {
@@ -642,7 +658,6 @@ export class WebGLBackend {
642
658
  at: gl.getUniformLocation(program, "uAt"),
643
659
  opaque: gl.getUniformLocation(program, "uOpaque"),
644
660
  };
645
- return this.overProgram;
646
661
  }
647
662
 
648
663
  /**
@@ -912,6 +927,7 @@ export class WebGLBackend {
912
927
  this.atlas.destroy();
913
928
  this.compositor.destroy();
914
929
  this.filters.destroy();
930
+ this.programs.destroy();
915
931
  this.kernels.clear();
916
932
  for (const open of this.layers) {
917
933
  if (open.layer) {
@@ -190,6 +190,9 @@ export class FillBatches {
190
190
  let vertices = 0;
191
191
  let indices = 0;
192
192
  // The run's geometries by where they lie in the atlas, which no two live geometries share.
193
+ // A range let go of and taken again holds a geometry added that frame, which no run keys
194
+ // till the next, and the frame before keyed only what was live beside it, never the one
195
+ // let go of: no key left from that one matches.
193
196
  let key = count;
194
197
  for (let i = 0; i < count; i++) {
195
198
  const place = places[i];