@swf2es/render 0.1.34 → 0.1.35

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 (50) hide show
  1. package/dist/commands.d.ts +11 -2
  2. package/dist/commands.d.ts.map +1 -1
  3. package/dist/commands.js +36 -6
  4. package/dist/commands.js.map +1 -1
  5. package/dist/geometry.d.ts +62 -5
  6. package/dist/geometry.d.ts.map +1 -1
  7. package/dist/geometry.js +100 -17
  8. package/dist/geometry.js.map +1 -1
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +1 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/measure.d.ts.map +1 -1
  14. package/dist/measure.js +118 -61
  15. package/dist/measure.js.map +1 -1
  16. package/dist/plan.js +4 -4
  17. package/dist/plan.js.map +1 -1
  18. package/dist/rows.d.ts +4 -0
  19. package/dist/rows.d.ts.map +1 -1
  20. package/dist/rows.js +59 -10
  21. package/dist/rows.js.map +1 -1
  22. package/dist/scene.d.ts.map +1 -1
  23. package/dist/scene.js +37 -5
  24. package/dist/scene.js.map +1 -1
  25. package/dist/webgl/atlas.d.ts +83 -18
  26. package/dist/webgl/atlas.d.ts.map +1 -1
  27. package/dist/webgl/atlas.js +271 -110
  28. package/dist/webgl/atlas.js.map +1 -1
  29. package/dist/webgl/backend.d.ts +5 -3
  30. package/dist/webgl/backend.d.ts.map +1 -1
  31. package/dist/webgl/backend.js +9 -9
  32. package/dist/webgl/backend.js.map +1 -1
  33. package/dist/webgl/batches.d.ts +36 -11
  34. package/dist/webgl/batches.d.ts.map +1 -1
  35. package/dist/webgl/batches.js +94 -51
  36. package/dist/webgl/batches.js.map +1 -1
  37. package/dist/webgl/draws.js +2 -2
  38. package/dist/webgl/draws.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/commands.ts +48 -6
  41. package/src/geometry.ts +109 -19
  42. package/src/index.ts +1 -0
  43. package/src/measure.ts +132 -68
  44. package/src/plan.ts +4 -4
  45. package/src/rows.ts +62 -19
  46. package/src/scene.ts +41 -5
  47. package/src/webgl/atlas.ts +321 -120
  48. package/src/webgl/backend.ts +9 -9
  49. package/src/webgl/batches.ts +108 -57
  50. package/src/webgl/draws.ts +2 -2
@@ -1,5 +1,11 @@
1
1
  import type { GlState } from "@swf2es/context3d/device";
2
- import { type Geometries, type GeometryData, GLYPH_FLOATS, samplesStage } from "../geometry.js";
2
+ import {
3
+ type Geometries,
4
+ type GeometryData,
5
+ GLYPH_FLOATS,
6
+ RELEASE_FRAMES,
7
+ samplesStage,
8
+ } from "../geometry.js";
3
9
  import { FreeRanges } from "./free-ranges.js";
4
10
  import { GlyphTables } from "./glyph-tables.js";
5
11
  import {
@@ -17,7 +23,7 @@ import {
17
23
  * Bytes a vertex in the atlas: its position's two floats, its texel's two, then its colour's four
18
24
  * bytes, or a mesh's depth's float.
19
25
  */
20
- const VERTEX_BYTES = 20;
26
+ export const VERTEX_BYTES = 20;
21
27
  /** Bytes a vertex of advanced text's glyph quads, in an atlas of their own. */
22
28
  const GLYPH_BYTES = GLYPH_FLOATS * 4;
23
29
  /** The fewest glyph vertices the glyph atlas holds once a text needs it. */
@@ -82,6 +88,18 @@ export interface Page {
82
88
  live: number;
83
89
  }
84
90
 
91
+ const NO_INDICES = new Uint16Array(0);
92
+
93
+ /**
94
+ * `source`, indices into `count` vertices, copied into an array of their own: in 16 bits where
95
+ * they fit, and in a buffer of their own, not the geometry's, which is let go of.
96
+ */
97
+ function localIndices(source: Uint16Array | Uint32Array, count: number): Uint16Array | Uint32Array {
98
+ const local = count <= 1 << 16 ? new Uint16Array(source.length) : new Uint32Array(source.length);
99
+ local.set(source);
100
+ return local;
101
+ }
102
+
85
103
  /**
86
104
  * Where a geometry lies in the atlas, its page and its ranges there, or in
87
105
  * the glyph atlas with its text's mode and table, and their slot in the
@@ -98,18 +116,31 @@ export interface Place {
98
116
  flat: boolean;
99
117
  /** Whether it samples the stage's pixel (samplesStage), which the stage fill program draws. */
100
118
  stage: boolean;
101
- /** Small, unchanged geometries may be copied into a run's streaming buffer. */
102
- batch: { data: GeometryData; frame: number } | null;
119
+ /**
120
+ * Small, unchanged geometries may be copied into a run's streaming buffer: their vertices from
121
+ * the page, on the GPU, and their indices from `local`. The frame it was placed on.
122
+ */
123
+ batch: { frame: number } | null;
124
+ /**
125
+ * Its indices counted from its first vertex: what moving it in the atlas (repack) or copying it
126
+ * into a run's stream needs, as WebGL copies an index buffer only as it is. Its geometry's own
127
+ * till it has stood RELEASE_FRAMES, then a copy of its own, in 16 bits where its vertices fit,
128
+ * as its arrays are let go of (Geometries.release).
129
+ */
130
+ local: Uint16Array | Uint32Array;
103
131
  }
104
132
 
105
133
  /**
106
134
  * The geometry atlas: every geometry copied once into a page's vertex and
107
135
  * index buffers, and advanced text's glyph quads into an atlas of their
108
- * own, where the draws find them by their places. A geometry let go of
109
- * leaves holes in its page, which later geometries are placed in once the
110
- * top is full; a page is packed again from the geometries' own arrays only
111
- * when what is added finds no room. Repacking every geometry for its holes
112
- * alone made frames that copied and uploaded the whole atlas again, tens of
136
+ * own, where the draws find them by their places. Only a geometry's indices
137
+ * stay on the CPU once copied (Place.local), counted from its first vertex,
138
+ * and its registry lets go of its arrays (Geometries.release). A geometry
139
+ * let go of leaves holes in its page, which later geometries are placed in
140
+ * once the top is full; a page is packed again only when what is added
141
+ * finds no room, its vertices moved on the GPU and its indices written
142
+ * again from its own. Repacking every geometry for its holes alone made
143
+ * frames that copied and uploaded the whole atlas again, tens of
113
144
  * megabytes, every few seconds of a busy SWF. The glyph atlas is packed
114
145
  * again once its holes outweigh what is live, which also frees the texts'
115
146
  * tables' slots.
@@ -146,6 +177,15 @@ export class GeometryAtlas {
146
177
  };
147
178
  /** The texts' modes and tables, which the glyph program reads. */
148
179
  readonly tables: GlyphTables;
180
+ /** The ids counted among a frame's additions, each once (place). */
181
+ private readonly counted = new Set<number>();
182
+ /**
183
+ * The places made, with their ids and frames, whose geometries' arrays are let go of once they
184
+ * have stood RELEASE_FRAMES of their registry's (settle), oldest first.
185
+ */
186
+ private settling: Place[] = [];
187
+ private settlingIds: number[] = [];
188
+ private settlingFrames: number[] = [];
149
189
 
150
190
  constructor(gl: WebGL2RenderingContext, state: GlState) {
151
191
  this.gl = gl;
@@ -187,6 +227,9 @@ export class GeometryAtlas {
187
227
  this.stagedVertices = 0;
188
228
  this.stagedIndexCount = 0;
189
229
  this.places = [];
230
+ this.settling = [];
231
+ this.settlingIds = [];
232
+ this.settlingFrames = [];
190
233
  this.tables.setUp();
191
234
  }
192
235
 
@@ -225,6 +268,8 @@ export class GeometryAtlas {
225
268
  }
226
269
 
227
270
  this.places[id] = null;
271
+ // Its geometry's own, where it has not settled: a settling entry would keep them alive.
272
+ place.local = NO_INDICES;
228
273
  if (place.glyphs) {
229
274
  this.glyphs.live -= place.vertices;
230
275
  this.glyphs.holes += place.vertices;
@@ -238,10 +283,11 @@ export class GeometryAtlas {
238
283
 
239
284
  /**
240
285
  * The geometries removed since the last frame let go of, and those added
241
- * copied in: the shapes together at their page's top or in a hole that
242
- * holds them all, else each in the first hole that holds it, else into a
243
- * page packed again with room for twice what is live; a text at the
244
- * glyph atlas' top, else into one packed again.
286
+ * copied in, their arrays then let go of by the registry: the shapes
287
+ * together at their page's top or in a hole that holds them all, else
288
+ * each in the first hole that holds it, else into a page packed again
289
+ * with room for twice what is live; a text at the glyph atlas' top, else
290
+ * into one packed again.
245
291
  */
246
292
  place(geometries: Geometries): void {
247
293
  this.frame++;
@@ -258,7 +304,8 @@ export class GeometryAtlas {
258
304
  }
259
305
  }
260
306
 
261
- // Changed in place: copied again where they lie, else as added.
307
+ // Changed in place, which only a geometry added to be kept is: copied again where they lie,
308
+ // else as added.
262
309
  for (const id of geometries.updated) {
263
310
  const place = this.places[id];
264
311
  const data = geometries.get(id);
@@ -273,25 +320,34 @@ export class GeometryAtlas {
273
320
  }
274
321
 
275
322
  geometries.updated.length = 0;
276
- // Found flat: what is added after reads it from its data.
323
+ // Found flat: what is added after reads it from its registry.
277
324
  for (const id of geometries.flattened) {
278
325
  const place = this.places[id];
279
326
  if (place) {
280
- place.flat = geometries.get(id)?.flat === true;
327
+ place.flat = geometries.flat(id);
281
328
  }
282
329
  }
283
330
 
284
331
  geometries.flattened.length = 0;
332
+ // An id let go of and taken again in a frame comes twice, its arrays the second's: counted
333
+ // once, and copied once, as its arrays are let go of once copied.
334
+ const counted = this.counted;
335
+ counted.clear();
285
336
  let vertices = 0;
286
337
  let indices = 0;
287
338
  let glyphVertices = 0;
288
339
  let glyphIndices = 0;
289
340
  for (const id of geometries.added) {
290
341
  const data = geometries.get(id);
291
- if (data?.glyphs) {
342
+ if (!data || counted.has(id)) {
343
+ continue;
344
+ }
345
+
346
+ counted.add(id);
347
+ if (data.glyphs) {
292
348
  glyphVertices += data.glyphs.vertices.length / GLYPH_FLOATS;
293
349
  glyphIndices += data.indices.length;
294
- } else if (data) {
350
+ } else {
295
351
  vertices += data.positions.length / 2;
296
352
  indices += data.indices.length;
297
353
  }
@@ -302,18 +358,17 @@ export class GeometryAtlas {
302
358
  // room, or below, when holes too small for one of them leave it none.
303
359
  const g = this.glyphs;
304
360
  const page = this.pages[0];
305
- let shapes = vertices > page.vertices.room() || indices > page.indices.room();
306
- const texts =
361
+ if (vertices > page.vertices.room() || indices > page.indices.room()) {
362
+ this.repack(vertices, indices);
363
+ }
364
+
365
+ if (
307
366
  g.top + glyphVertices > g.capacity ||
308
367
  g.indexTop + glyphIndices > g.indexCapacity ||
309
368
  (g.holes > GLYPHS_LEAST && g.holes > g.live) ||
310
- this.tables.full;
311
- if (shapes) {
312
- this.repack(geometries);
313
- }
314
-
315
- if (texts) {
316
- this.repackGlyphs(geometries);
369
+ this.tables.full
370
+ ) {
371
+ this.repackGlyphs(glyphVertices, glyphIndices);
317
372
  }
318
373
 
319
374
  // The shapes added together in one range where one holds them all, at the top or in a
@@ -321,7 +376,7 @@ export class GeometryAtlas {
321
376
  // first that holds it, which a churning atlas would leave to a call or two a geometry.
322
377
  let vertexAt = -1;
323
378
  let indexAt = -1;
324
- if (!shapes && vertices > 0) {
379
+ if (vertices > 0) {
325
380
  vertexAt = page.vertices.take(vertices);
326
381
  indexAt = vertexAt < 0 ? -1 : page.indices.take(indices);
327
382
  if (vertexAt >= 0 && indexAt < 0) {
@@ -332,98 +387,194 @@ export class GeometryAtlas {
332
387
 
333
388
  for (const id of geometries.added) {
334
389
  const data = geometries.get(id);
335
- if (!data) {
336
- continue;
337
- }
338
-
339
- // Added twice in a frame, as an id let go of and taken again can be: its first copy's
340
- // room given back, which only packing would otherwise find again. A packed atlas holds
341
- // its geometries already.
342
- if (data.glyphs) {
343
- if (!texts) {
344
- this.letGo(id);
345
- this.copyGlyphs(id, data);
346
- }
347
-
348
- continue;
349
- }
350
-
351
- if (shapes) {
390
+ if (!data || !counted.delete(id)) {
352
391
  continue;
353
392
  }
354
393
 
394
+ // Added again after it was placed, as a geometry kept and changed can be, or after a lost
395
+ // context: its first copy's room given back, which only packing would otherwise find again.
355
396
  this.letGo(id);
356
- if (vertexAt >= 0) {
397
+ if (data.glyphs) {
398
+ this.copyGlyphs(id, data);
399
+ } else if (vertexAt >= 0) {
357
400
  this.copyAt(page, id, data, vertexAt, indexAt);
358
401
  vertexAt += data.positions.length / 2;
359
402
  indexAt += data.indices.length;
360
403
  } else if (!this.copyIn(page, id, data)) {
361
- // Packing copies every live shape, those added after this one too.
362
- shapes = true;
363
- this.repack(geometries);
404
+ // Packed with room for this one and those after it, which it then holds.
405
+ this.repack(vertices, indices);
406
+ this.copyIn(page, id, data);
364
407
  }
408
+
409
+ if (!data.glyphs) {
410
+ vertices -= data.positions.length / 2;
411
+ indices -= data.indices.length;
412
+ }
413
+
414
+ this.settling.push(this.places[id] as Place);
415
+ this.settlingIds.push(id);
416
+ this.settlingFrames.push(geometries.frame);
365
417
  }
366
418
 
367
419
  geometries.added.length = 0;
368
420
  this.flush();
369
421
  this.tables.upload();
422
+ this.settle(geometries);
370
423
  }
371
424
 
372
- /** Every live shape copied into its page's buffers made anew, with room for twice them. */
373
- repack(geometries: Geometries): void {
374
- const gl = this.gl;
375
- const ids = geometries.live();
376
- let vertices = 0;
377
- let indices = 0;
378
- for (const id of ids) {
379
- const data = geometries.get(id) as GeometryData;
380
- if (!data.glyphs) {
381
- vertices += data.positions.length / 2;
382
- indices += data.indices.length;
425
+ /**
426
+ * The geometries placed RELEASE_FRAMES of their registry's ago or more, by its frames, not the
427
+ * atlas', which a BitmapData's draws advance too, and placed still: their indices copied into
428
+ * arrays of their own and their arrays let go of (Geometries.release), only while the context
429
+ * is not known to be lost: copies made into a lost one are no copies, and the arrays stay for
430
+ * the restore. In Chrome's window before it flags a loss (webgl/losses.ts), arrays may go
431
+ * after copies that never reached the GPU; the restore's Geometries.lost rebuilds them.
432
+ */
433
+ private settle(geometries: Geometries): void {
434
+ const places = this.settling;
435
+ const ids = this.settlingIds;
436
+ const frames = this.settlingFrames;
437
+ let k = 0;
438
+ while (k < places.length && geometries.frame - frames[k] >= RELEASE_FRAMES) {
439
+ k++;
440
+ }
441
+
442
+ if (k === 0 || this.gl.isContextLost()) {
443
+ return;
444
+ }
445
+
446
+ for (let i = 0; i < k; i++) {
447
+ const place = places[i];
448
+ if (this.places[ids[i]] === place && geometries.get(ids[i])) {
449
+ place.local = localIndices(place.local, place.vertices);
450
+ geometries.release(ids[i]);
383
451
  }
384
452
  }
385
453
 
454
+ places.splice(0, k);
455
+ ids.splice(0, k);
456
+ frames.splice(0, k);
457
+ }
458
+
459
+ /**
460
+ * The shapes' page packed again, with room for twice its live shapes and
461
+ * `vertices` and `indices` more: its shapes moved in a row from its start,
462
+ * their vertices copied on the GPU, through a buffer of their own, as a
463
+ * new store loses the old one's, and their indices written again from
464
+ * their own (Place.local). Their places are moved, not made anew, so that a run's
465
+ * stream still finds them (Place.batch).
466
+ */
467
+ private repack(vertices: number, indices: number): void {
468
+ const gl = this.gl;
386
469
  const page = this.pages[0];
470
+ const moved: Place[] = [];
471
+ let used = 0;
472
+ for (const place of this.places) {
473
+ if (place && !place.glyphs) {
474
+ moved.push(place);
475
+ vertices += place.vertices;
476
+ indices += place.indices;
477
+ used = Math.max(used, place.vertexStart + place.vertices);
478
+ }
479
+ }
480
+
481
+ // In their order in the page, so that shapes placed together move in one copy.
482
+ moved.sort((a, b) => a.vertexStart - b.vertexStart);
483
+ // What is staged goes up first, for the copy to hold it.
484
+ this.flush();
485
+ this.state.bindVertexArray(page.vertexArray);
486
+ gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
487
+ const copy = used > 0 ? this.copyOf(used * VERTEX_BYTES) : null;
387
488
  page.vertices.reset(Math.max(LEAST, vertices * 2));
388
489
  page.indices.reset(Math.max(LEAST * 3, indices * 2));
389
490
  page.live = 0;
390
- this.state.bindVertexArray(page.vertexArray);
391
- gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
392
491
  gl.bufferData(gl.ARRAY_BUFFER, page.vertices.capacity * VERTEX_BYTES, gl.STATIC_DRAW);
393
492
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, page.indices.capacity * 4, gl.STATIC_DRAW);
394
- // What was staged for the buffers replaced is copied again below.
395
- this.stagedVertices = 0;
396
- this.stagedIndexCount = 0;
397
- // Each at the top, which holds twice them all.
398
- for (const id of ids) {
399
- const data = geometries.get(id) as GeometryData;
400
- if (!data.glyphs) {
401
- this.copyIn(page, id, data);
493
+ let from = 0;
494
+ let to = 0;
495
+ let run = 0;
496
+ for (const place of moved) {
497
+ const base = page.vertices.take(place.vertices);
498
+ const indexStart = page.indices.take(place.indices);
499
+ if (run > 0 && place.vertexStart === from + run && base === to + run) {
500
+ run += place.vertices;
501
+ } else {
502
+ this.copyRun(from, to, run, VERTEX_BYTES);
503
+ from = place.vertexStart;
504
+ to = base;
505
+ run = place.vertices;
402
506
  }
507
+
508
+ this.writeIndices(page, place.local, base, indexStart);
509
+ place.vertexStart = base;
510
+ place.indexStart = indexStart;
511
+ page.live += place.vertices;
403
512
  }
404
513
 
514
+ this.copyRun(from, to, run, VERTEX_BYTES);
515
+ this.dropCopy(copy);
405
516
  this.flush();
406
517
  }
407
518
 
408
- /** Every live text's quads copied into new glyph buffers: none till a text needs them. */
409
- repackGlyphs(geometries: Geometries): void {
519
+ /**
520
+ * A copy on the GPU of the first `bytes` of the buffer bound to ARRAY_BUFFER, bound to
521
+ * COPY_READ_BUFFER, for copyRun to read from once the buffer is made anew.
522
+ */
523
+ private copyOf(bytes: number): WebGLBuffer {
524
+ const gl = this.gl;
525
+ const copy = gl.createBuffer() as WebGLBuffer;
526
+ gl.bindBuffer(gl.COPY_WRITE_BUFFER, copy);
527
+ gl.bufferData(gl.COPY_WRITE_BUFFER, bytes, gl.STREAM_COPY);
528
+ gl.copyBufferSubData(gl.ARRAY_BUFFER, gl.COPY_WRITE_BUFFER, 0, 0, bytes);
529
+ gl.bindBuffer(gl.COPY_WRITE_BUFFER, null);
530
+ gl.bindBuffer(gl.COPY_READ_BUFFER, copy);
531
+ return copy;
532
+ }
533
+
534
+ /** `count` vertices of `bytes` each copied from vertex `from` of the copy (copyOf) to vertex `to` of ARRAY_BUFFER's. */
535
+ private copyRun(from: number, to: number, count: number, bytes: number): void {
536
+ if (count > 0) {
537
+ this.gl.copyBufferSubData(
538
+ this.gl.COPY_READ_BUFFER,
539
+ this.gl.ARRAY_BUFFER,
540
+ from * bytes,
541
+ to * bytes,
542
+ count * bytes,
543
+ );
544
+ }
545
+ }
546
+
547
+ private dropCopy(copy: WebGLBuffer | null): void {
548
+ if (copy) {
549
+ this.gl.bindBuffer(this.gl.COPY_READ_BUFFER, null);
550
+ this.gl.deleteBuffer(copy);
551
+ }
552
+ }
553
+
554
+ /**
555
+ * The glyph atlas packed again, with room for twice its live texts' quads
556
+ * and `vertices` and `indices` more, none till a text needs them: its
557
+ * texts moved in a row from its start as the shapes' are (repack), and
558
+ * their tables given slots again, only the live texts' kept.
559
+ */
560
+ private repackGlyphs(vertices: number, indices: number): void {
410
561
  const gl = this.gl;
411
562
  const g = this.glyphs;
412
- const ids = geometries.live();
413
- let vertices = 0;
414
- let indices = 0;
415
- for (const id of ids) {
416
- const data = geometries.get(id) as GeometryData;
417
- if (data.glyphs) {
418
- vertices += data.glyphs.vertices.length / GLYPH_FLOATS;
419
- indices += data.indices.length;
563
+ const moved: Place[] = [];
564
+ for (const place of this.places) {
565
+ if (place?.glyphs) {
566
+ moved.push(place);
567
+ vertices += place.vertices;
568
+ indices += place.indices;
420
569
  }
421
570
  }
422
571
 
423
- g.capacity = vertices > 0 ? Math.max(GLYPHS_LEAST, vertices * 2) : 0;
424
- g.indexCapacity = vertices > 0 ? Math.max(GLYPHS_LEAST * 2, indices * 2) : 0;
572
+ moved.sort((a, b) => a.vertexStart - b.vertexStart);
425
573
  this.state.bindVertexArray(this.glyphArray);
426
574
  gl.bindBuffer(gl.ARRAY_BUFFER, this.glyphBuffer);
575
+ const copy = moved.length > 0 && g.top > 0 ? this.copyOf(g.top * GLYPH_BYTES) : null;
576
+ g.capacity = vertices > 0 ? Math.max(GLYPHS_LEAST, vertices * 2) : 0;
577
+ g.indexCapacity = vertices > 0 ? Math.max(GLYPHS_LEAST * 2, indices * 2) : 0;
427
578
  gl.bufferData(gl.ARRAY_BUFFER, g.capacity * GLYPH_BYTES, gl.STATIC_DRAW);
428
579
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, g.indexCapacity * 4, gl.STATIC_DRAW);
429
580
  g.top = 0;
@@ -432,14 +583,44 @@ export class GeometryAtlas {
432
583
  g.holes = 0;
433
584
  // Only the live texts' tables kept.
434
585
  this.tables.reset();
435
- for (const id of ids) {
436
- const data = geometries.get(id) as GeometryData;
437
- if (data.glyphs) {
438
- this.copyGlyphs(id, data);
586
+ let from = 0;
587
+ let to = 0;
588
+ let run = 0;
589
+ let written = 0;
590
+ for (const place of moved) {
591
+ written += place.indices;
592
+ }
593
+
594
+ const absolute = new Uint32Array(written);
595
+ for (const place of moved) {
596
+ if (run > 0 && place.vertexStart === from + run && g.top === to + run) {
597
+ run += place.vertices;
598
+ } else {
599
+ this.copyRun(from, to, run, GLYPH_BYTES);
600
+ from = place.vertexStart;
601
+ to = g.top;
602
+ run = place.vertices;
603
+ }
604
+
605
+ const local = place.local;
606
+ for (let i = 0; i < local.length; i++) {
607
+ absolute[g.indexTop + i] = local[i] + g.top;
439
608
  }
609
+
610
+ const glyphs = place.glyphs as NonNullable<Place["glyphs"]>;
611
+ glyphs.slot = this.tables.slot(glyphs.mode, glyphs.table);
612
+ place.vertexStart = g.top;
613
+ place.indexStart = g.indexTop;
614
+ g.top += place.vertices;
615
+ g.indexTop += place.indices;
616
+ g.live += place.vertices;
440
617
  }
441
618
 
442
- this.tables.upload();
619
+ this.copyRun(from, to, run, GLYPH_BYTES);
620
+ this.dropCopy(copy);
621
+ if (written > 0) {
622
+ gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, absolute);
623
+ }
443
624
  }
444
625
 
445
626
  /**
@@ -464,7 +645,10 @@ export class GeometryAtlas {
464
645
  return true;
465
646
  }
466
647
 
467
- /** A shape's `data` copied into `page`'s room taken for it, from vertex `base` and index `indexStart`. */
648
+ /**
649
+ * A shape's `data` copied into `page`'s room taken for it, from vertex
650
+ * `base` and index `indexStart`, its indices kept in a copy of their own.
651
+ */
468
652
  private copyAt(
469
653
  page: Page,
470
654
  id: number,
@@ -474,8 +658,31 @@ export class GeometryAtlas {
474
658
  ): void {
475
659
  const count = data.positions.length / 2;
476
660
  const source = data.indices;
477
- const length = source.length;
478
661
  this.copyVertices(page, data, base);
662
+ this.writeIndices(page, source, base, indexStart);
663
+ this.places[id] = {
664
+ page: page.index,
665
+ vertexStart: base,
666
+ vertices: count,
667
+ indexStart,
668
+ indices: source.length,
669
+ glyphs: null,
670
+ flat: data.flat === true,
671
+ stage: samplesStage(data),
672
+ batch: this.batch(data),
673
+ local: source,
674
+ };
675
+ page.live += count;
676
+ }
677
+
678
+ /** `source`, counted from a geometry's first vertex, made absolute from `base` into `page` from index `indexStart`: staged, or uploaded if too many. */
679
+ private writeIndices(
680
+ page: Page,
681
+ source: Uint16Array | Uint32Array,
682
+ base: number,
683
+ indexStart: number,
684
+ ): void {
685
+ const length = source.length;
479
686
  if (length > STAGED_INDICES) {
480
687
  const indices = new Uint32Array(length);
481
688
  for (let i = 0; i < length; i++) {
@@ -485,37 +692,25 @@ export class GeometryAtlas {
485
692
  this.flushIndices();
486
693
  this.state.bindVertexArray(page.vertexArray);
487
694
  this.gl.bufferSubData(this.gl.ELEMENT_ARRAY_BUFFER, indexStart * 4, indices);
488
- } else {
489
- this.stagePage(page);
490
- if (
491
- this.stagedIndexCount + length > STAGED_INDICES ||
492
- this.stagedIndexAt + this.stagedIndexCount !== indexStart
493
- ) {
494
- this.flushIndices();
495
- this.stagedIndexAt = indexStart;
496
- }
695
+ return;
696
+ }
497
697
 
498
- const staged = this.stagedIndices;
499
- const at = this.stagedIndexCount;
500
- for (let i = 0; i < length; i++) {
501
- staged[at + i] = source[i] + base;
502
- }
698
+ this.stagePage(page);
699
+ if (
700
+ this.stagedIndexCount + length > STAGED_INDICES ||
701
+ this.stagedIndexAt + this.stagedIndexCount !== indexStart
702
+ ) {
703
+ this.flushIndices();
704
+ this.stagedIndexAt = indexStart;
705
+ }
503
706
 
504
- this.stagedIndexCount = at + length;
707
+ const staged = this.stagedIndices;
708
+ const at = this.stagedIndexCount;
709
+ for (let i = 0; i < length; i++) {
710
+ staged[at + i] = source[i] + base;
505
711
  }
506
712
 
507
- this.places[id] = {
508
- page: page.index,
509
- vertexStart: base,
510
- vertices: count,
511
- indexStart,
512
- indices: length,
513
- glyphs: null,
514
- flat: data.flat === true,
515
- stage: samplesStage(data),
516
- batch: this.batch(data),
517
- };
518
- page.live += count;
713
+ this.stagedIndexCount = at + length;
519
714
  }
520
715
 
521
716
  /** `data`'s vertices interleaved for `page` from vertex `base`: staged, or uploaded if too many. */
@@ -589,19 +784,24 @@ export class GeometryAtlas {
589
784
 
590
785
  private batch(data: GeometryData): Place["batch"] {
591
786
  return data.positions.length <= 512 && data.indices.length <= 1024 && !data.depths
592
- ? { data, frame: this.frame }
787
+ ? { frame: this.frame }
593
788
  : null;
594
789
  }
595
790
 
596
- /** Glyph quads copied in at the top of the glyph atlas, their indices made absolute in it. */
791
+ /**
792
+ * Glyph quads copied in at the top of the glyph atlas, their indices made
793
+ * absolute in it, and kept counted from the text's first vertex as a
794
+ * shape's are.
795
+ */
597
796
  private copyGlyphs(id: number, data: GeometryData): void {
598
797
  const gl = this.gl;
599
798
  const g = this.glyphs;
600
799
  const glyphs = data.glyphs as NonNullable<GeometryData["glyphs"]>;
601
800
  const count = glyphs.vertices.length / GLYPH_FLOATS;
602
- const indices = new Uint32Array(data.indices.length);
801
+ const source = data.indices;
802
+ const indices = new Uint32Array(source.length);
603
803
  for (let i = 0; i < indices.length; i++) {
604
- indices[i] = data.indices[i] + g.top;
804
+ indices[i] = source[i] + g.top;
605
805
  }
606
806
 
607
807
  this.state.bindVertexArray(this.glyphArray);
@@ -622,6 +822,7 @@ export class GeometryAtlas {
622
822
  flat: false,
623
823
  stage: false,
624
824
  batch: null,
825
+ local: source,
625
826
  };
626
827
  g.top += count;
627
828
  g.indexTop += indices.length;
@@ -221,26 +221,26 @@ export class WebGLBackend {
221
221
  }
222
222
 
223
223
  /**
224
- * The context came back after it was lost: everything made again, every
225
- * geometry copied in again from the registry's arrays. The textures were
226
- * lost with it, which their owners add again (Textures.lost). Lost again
224
+ * The context came back after it was lost: everything made again. The
225
+ * geometries whose arrays the registry let go of are lost with it, which
226
+ * their owners add again as they draw them, and those it keeps are copied
227
+ * in again by the next frame (Geometries.lost). The textures were lost
228
+ * too, which their owners add again (Textures.lost). Lost again
227
229
  * meanwhile, it stays stale, for the next restore.
228
230
  */
229
231
  restore(geometries: Geometries): void {
230
232
  this.losses.clear();
231
233
  this.made = this.ready = false;
232
- geometries.added.length = 0;
233
- geometries.removed.length = 0;
234
- geometries.updated.length = 0;
235
- geometries.flattened.length = 0;
234
+ geometries.lost();
235
+ // Lost again already: the atlas keeps the places and settling geometries of the context
236
+ // before, naming ids Geometries.lost removed, till the next restore makes it anew (setUp).
237
+ // Nothing reads them meanwhile, as no frame is drawn or planned while the context is lost.
236
238
  if (this.gl.isContextLost()) {
237
239
  return;
238
240
  }
239
241
 
240
242
  try {
241
243
  this.setUp();
242
- this.atlas.repack(geometries);
243
- this.atlas.repackGlyphs(geometries);
244
244
  this.made = this.ready = true;
245
245
  } catch (e) {
246
246
  this.gaveUp(e);