@swf2es/render 0.1.34 → 0.1.36

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 (45) 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 +89 -16
  26. package/dist/webgl/atlas.d.ts.map +1 -1
  27. package/dist/webgl/atlas.js +285 -109
  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/draws.js +1 -1
  34. package/dist/webgl/draws.js.map +1 -1
  35. package/package.json +2 -2
  36. package/src/commands.ts +48 -6
  37. package/src/geometry.ts +109 -19
  38. package/src/index.ts +1 -0
  39. package/src/measure.ts +132 -68
  40. package/src/plan.ts +4 -4
  41. package/src/rows.ts +62 -19
  42. package/src/scene.ts +41 -5
  43. package/src/webgl/atlas.ts +334 -118
  44. package/src/webgl/backend.ts +9 -9
  45. package/src/webgl/draws.ts +1 -1
@@ -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,33 @@ 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. */
119
+ /**
120
+ * Small, unchanged geometries may be copied into a run's streaming buffer from their arrays,
121
+ * which their registry keeps (settle): the frame it was placed on.
122
+ */
102
123
  batch: { data: GeometryData; frame: number } | null;
124
+ /**
125
+ * Its indices counted from its first vertex: what moving it in the atlas (repack) needs, as
126
+ * WebGL copies an index buffer only as it is. Its geometry's own till it has stood
127
+ * RELEASE_FRAMES, then, where its arrays are let go of (Geometries.release), a copy of its own,
128
+ * in 16 bits where its vertices fit.
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. Once copied, a geometry
137
+ * too large to merge keeps only its indices on the CPU (Place.local),
138
+ * counted from its first vertex, and its registry lets go of its arrays
139
+ * (Geometries.release); one small enough to merge keeps its arrays, which a
140
+ * run's stream stages again whenever moving content makes its runs anew. A geometry
141
+ * let go of leaves holes in its page, which later geometries are placed in
142
+ * once the top is full; a page is packed again only when what is added
143
+ * finds no room, its vertices moved on the GPU and its indices written
144
+ * again from its own. Repacking every geometry for its holes alone made
145
+ * frames that copied and uploaded the whole atlas again, tens of
113
146
  * megabytes, every few seconds of a busy SWF. The glyph atlas is packed
114
147
  * again once its holes outweigh what is live, which also frees the texts'
115
148
  * tables' slots.
@@ -146,6 +179,15 @@ export class GeometryAtlas {
146
179
  };
147
180
  /** The texts' modes and tables, which the glyph program reads. */
148
181
  readonly tables: GlyphTables;
182
+ /** The ids counted among a frame's additions, each once (place). */
183
+ private readonly counted = new Set<number>();
184
+ /**
185
+ * The places made, with their ids and frames, whose geometries' arrays are let go of once they
186
+ * have stood RELEASE_FRAMES of their registry's (settle), oldest first.
187
+ */
188
+ private settling: Place[] = [];
189
+ private settlingIds: number[] = [];
190
+ private settlingFrames: number[] = [];
149
191
 
150
192
  constructor(gl: WebGL2RenderingContext, state: GlState) {
151
193
  this.gl = gl;
@@ -187,6 +229,9 @@ export class GeometryAtlas {
187
229
  this.stagedVertices = 0;
188
230
  this.stagedIndexCount = 0;
189
231
  this.places = [];
232
+ this.settling = [];
233
+ this.settlingIds = [];
234
+ this.settlingFrames = [];
190
235
  this.tables.setUp();
191
236
  }
192
237
 
@@ -225,6 +270,9 @@ export class GeometryAtlas {
225
270
  }
226
271
 
227
272
  this.places[id] = null;
273
+ // Its geometry's own, where it has not settled: a settling entry would keep them alive.
274
+ place.local = NO_INDICES;
275
+ place.batch = null;
228
276
  if (place.glyphs) {
229
277
  this.glyphs.live -= place.vertices;
230
278
  this.glyphs.holes += place.vertices;
@@ -238,10 +286,11 @@ export class GeometryAtlas {
238
286
 
239
287
  /**
240
288
  * 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.
289
+ * copied in, their arrays then let go of by the registry: the shapes
290
+ * together at their page's top or in a hole that holds them all, else
291
+ * each in the first hole that holds it, else into a page packed again
292
+ * with room for twice what is live; a text at the glyph atlas' top, else
293
+ * into one packed again.
245
294
  */
246
295
  place(geometries: Geometries): void {
247
296
  this.frame++;
@@ -258,7 +307,8 @@ export class GeometryAtlas {
258
307
  }
259
308
  }
260
309
 
261
- // Changed in place: copied again where they lie, else as added.
310
+ // Changed in place, which only a geometry added to be kept is: copied again where they lie,
311
+ // else as added.
262
312
  for (const id of geometries.updated) {
263
313
  const place = this.places[id];
264
314
  const data = geometries.get(id);
@@ -273,25 +323,34 @@ export class GeometryAtlas {
273
323
  }
274
324
 
275
325
  geometries.updated.length = 0;
276
- // Found flat: what is added after reads it from its data.
326
+ // Found flat: what is added after reads it from its registry.
277
327
  for (const id of geometries.flattened) {
278
328
  const place = this.places[id];
279
329
  if (place) {
280
- place.flat = geometries.get(id)?.flat === true;
330
+ place.flat = geometries.flat(id);
281
331
  }
282
332
  }
283
333
 
284
334
  geometries.flattened.length = 0;
335
+ // An id let go of and taken again in a frame comes twice, its arrays the second's: counted
336
+ // once, and copied once, as its arrays are let go of once copied.
337
+ const counted = this.counted;
338
+ counted.clear();
285
339
  let vertices = 0;
286
340
  let indices = 0;
287
341
  let glyphVertices = 0;
288
342
  let glyphIndices = 0;
289
343
  for (const id of geometries.added) {
290
344
  const data = geometries.get(id);
291
- if (data?.glyphs) {
345
+ if (!data || counted.has(id)) {
346
+ continue;
347
+ }
348
+
349
+ counted.add(id);
350
+ if (data.glyphs) {
292
351
  glyphVertices += data.glyphs.vertices.length / GLYPH_FLOATS;
293
352
  glyphIndices += data.indices.length;
294
- } else if (data) {
353
+ } else {
295
354
  vertices += data.positions.length / 2;
296
355
  indices += data.indices.length;
297
356
  }
@@ -302,18 +361,17 @@ export class GeometryAtlas {
302
361
  // room, or below, when holes too small for one of them leave it none.
303
362
  const g = this.glyphs;
304
363
  const page = this.pages[0];
305
- let shapes = vertices > page.vertices.room() || indices > page.indices.room();
306
- const texts =
364
+ if (vertices > page.vertices.room() || indices > page.indices.room()) {
365
+ this.repack(vertices, indices);
366
+ }
367
+
368
+ if (
307
369
  g.top + glyphVertices > g.capacity ||
308
370
  g.indexTop + glyphIndices > g.indexCapacity ||
309
371
  (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);
372
+ this.tables.full
373
+ ) {
374
+ this.repackGlyphs(glyphVertices, glyphIndices);
317
375
  }
318
376
 
319
377
  // The shapes added together in one range where one holds them all, at the top or in a
@@ -321,7 +379,7 @@ export class GeometryAtlas {
321
379
  // first that holds it, which a churning atlas would leave to a call or two a geometry.
322
380
  let vertexAt = -1;
323
381
  let indexAt = -1;
324
- if (!shapes && vertices > 0) {
382
+ if (vertices > 0) {
325
383
  vertexAt = page.vertices.take(vertices);
326
384
  indexAt = vertexAt < 0 ? -1 : page.indices.take(indices);
327
385
  if (vertexAt >= 0 && indexAt < 0) {
@@ -332,98 +390,206 @@ export class GeometryAtlas {
332
390
 
333
391
  for (const id of geometries.added) {
334
392
  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) {
393
+ if (!data || !counted.delete(id)) {
352
394
  continue;
353
395
  }
354
396
 
397
+ // Added again after it was placed, as a geometry kept and changed can be, or after a lost
398
+ // context: its first copy's room given back, which only packing would otherwise find again.
355
399
  this.letGo(id);
356
- if (vertexAt >= 0) {
400
+ if (data.glyphs) {
401
+ this.copyGlyphs(id, data);
402
+ } else if (vertexAt >= 0) {
357
403
  this.copyAt(page, id, data, vertexAt, indexAt);
358
404
  vertexAt += data.positions.length / 2;
359
405
  indexAt += data.indices.length;
360
406
  } 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);
407
+ // Packed with room for this one and those after it, which it then holds.
408
+ this.repack(vertices, indices);
409
+ this.copyIn(page, id, data);
410
+ }
411
+
412
+ if (!data.glyphs) {
413
+ vertices -= data.positions.length / 2;
414
+ indices -= data.indices.length;
364
415
  }
416
+
417
+ this.settling.push(this.places[id] as Place);
418
+ this.settlingIds.push(id);
419
+ this.settlingFrames.push(geometries.frame);
365
420
  }
366
421
 
367
422
  geometries.added.length = 0;
368
423
  this.flush();
369
424
  this.tables.upload();
425
+ this.settle(geometries);
370
426
  }
371
427
 
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;
428
+ /**
429
+ * The geometries placed RELEASE_FRAMES of their registry's ago or more, by its frames, not the
430
+ * atlas', which a BitmapData's draws advance too, placed still and too large to merge: their
431
+ * arrays let go of (Geometries.release) and their indices copied into arrays of their own. One
432
+ * small enough to merge keeps its arrays, which a run's stream stages again whenever moving
433
+ * content makes its runs anew: copying such geometries from the page on the GPU instead, a copy
434
+ * for each, made a frame of moving timelines thousands of copies and ANGLE's GL wait twice as
435
+ * long (0.1.35), and getting them back by tessellating them again stalls a frame wherever a
436
+ * still run changes or the stream is made anew. Only while the context
437
+ * is not known to be lost: copies made into a lost one are no copies, and the arrays stay for
438
+ * the restore. In Chrome's window before it flags a loss (webgl/losses.ts), arrays may go
439
+ * after copies that never reached the GPU; the restore's Geometries.lost rebuilds them.
440
+ */
441
+ private settle(geometries: Geometries): void {
442
+ const places = this.settling;
443
+ const ids = this.settlingIds;
444
+ const frames = this.settlingFrames;
445
+ let k = 0;
446
+ while (k < places.length && geometries.frame - frames[k] >= RELEASE_FRAMES) {
447
+ k++;
448
+ }
449
+
450
+ if (k === 0 || this.gl.isContextLost()) {
451
+ return;
452
+ }
453
+
454
+ for (let i = 0; i < k; i++) {
455
+ const place = places[i];
456
+ // Small enough to merge (Place.batch): its arrays kept for a run's stream to stage.
457
+ if (this.places[ids[i]] === place && !place.batch && geometries.get(ids[i])) {
458
+ geometries.release(ids[i]);
459
+ if (geometries.released(ids[i])) {
460
+ place.local = localIndices(place.local, place.vertices);
461
+ }
383
462
  }
384
463
  }
385
464
 
465
+ // Moved down in place: a frame's settling allocates nothing.
466
+ places.copyWithin(0, k);
467
+ ids.copyWithin(0, k);
468
+ frames.copyWithin(0, k);
469
+ places.length -= k;
470
+ ids.length -= k;
471
+ frames.length -= k;
472
+ }
473
+
474
+ /**
475
+ * The shapes' page packed again, with room for twice its live shapes and
476
+ * `vertices` and `indices` more: its shapes moved in a row from its start,
477
+ * their vertices copied on the GPU, through a buffer of their own, as a
478
+ * new store loses the old one's, and their indices written again from
479
+ * their own (Place.local). Their places are moved, not made anew, so that a run's
480
+ * stream still finds them (Place.batch).
481
+ */
482
+ private repack(vertices: number, indices: number): void {
483
+ const gl = this.gl;
386
484
  const page = this.pages[0];
485
+ const moved: Place[] = [];
486
+ let used = 0;
487
+ for (const place of this.places) {
488
+ if (place && !place.glyphs) {
489
+ moved.push(place);
490
+ vertices += place.vertices;
491
+ indices += place.indices;
492
+ used = Math.max(used, place.vertexStart + place.vertices);
493
+ }
494
+ }
495
+
496
+ // In their order in the page, so that shapes placed together move in one copy.
497
+ moved.sort((a, b) => a.vertexStart - b.vertexStart);
498
+ // What is staged goes up first, for the copy to hold it.
499
+ this.flush();
500
+ this.state.bindVertexArray(page.vertexArray);
501
+ gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
502
+ const copy = used > 0 ? this.copyOf(used * VERTEX_BYTES) : null;
387
503
  page.vertices.reset(Math.max(LEAST, vertices * 2));
388
504
  page.indices.reset(Math.max(LEAST * 3, indices * 2));
389
505
  page.live = 0;
390
- this.state.bindVertexArray(page.vertexArray);
391
- gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
392
506
  gl.bufferData(gl.ARRAY_BUFFER, page.vertices.capacity * VERTEX_BYTES, gl.STATIC_DRAW);
393
507
  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);
508
+ let from = 0;
509
+ let to = 0;
510
+ let run = 0;
511
+ for (const place of moved) {
512
+ const base = page.vertices.take(place.vertices);
513
+ const indexStart = page.indices.take(place.indices);
514
+ if (run > 0 && place.vertexStart === from + run && base === to + run) {
515
+ run += place.vertices;
516
+ } else {
517
+ this.copyRun(from, to, run, VERTEX_BYTES);
518
+ from = place.vertexStart;
519
+ to = base;
520
+ run = place.vertices;
402
521
  }
522
+
523
+ this.writeIndices(page, place.local, base, indexStart);
524
+ place.vertexStart = base;
525
+ place.indexStart = indexStart;
526
+ page.live += place.vertices;
403
527
  }
404
528
 
529
+ this.copyRun(from, to, run, VERTEX_BYTES);
530
+ this.dropCopy(copy);
405
531
  this.flush();
406
532
  }
407
533
 
408
- /** Every live text's quads copied into new glyph buffers: none till a text needs them. */
409
- repackGlyphs(geometries: Geometries): void {
534
+ /**
535
+ * A copy on the GPU of the first `bytes` of the buffer bound to ARRAY_BUFFER, bound to
536
+ * COPY_READ_BUFFER, for copyRun to read from once the buffer is made anew.
537
+ */
538
+ private copyOf(bytes: number): WebGLBuffer {
539
+ const gl = this.gl;
540
+ const copy = gl.createBuffer() as WebGLBuffer;
541
+ gl.bindBuffer(gl.COPY_WRITE_BUFFER, copy);
542
+ gl.bufferData(gl.COPY_WRITE_BUFFER, bytes, gl.STREAM_COPY);
543
+ gl.copyBufferSubData(gl.ARRAY_BUFFER, gl.COPY_WRITE_BUFFER, 0, 0, bytes);
544
+ gl.bindBuffer(gl.COPY_WRITE_BUFFER, null);
545
+ gl.bindBuffer(gl.COPY_READ_BUFFER, copy);
546
+ return copy;
547
+ }
548
+
549
+ /** `count` vertices of `bytes` each copied from vertex `from` of the copy (copyOf) to vertex `to` of ARRAY_BUFFER's. */
550
+ private copyRun(from: number, to: number, count: number, bytes: number): void {
551
+ if (count > 0) {
552
+ this.gl.copyBufferSubData(
553
+ this.gl.COPY_READ_BUFFER,
554
+ this.gl.ARRAY_BUFFER,
555
+ from * bytes,
556
+ to * bytes,
557
+ count * bytes,
558
+ );
559
+ }
560
+ }
561
+
562
+ private dropCopy(copy: WebGLBuffer | null): void {
563
+ if (copy) {
564
+ this.gl.bindBuffer(this.gl.COPY_READ_BUFFER, null);
565
+ this.gl.deleteBuffer(copy);
566
+ }
567
+ }
568
+
569
+ /**
570
+ * The glyph atlas packed again, with room for twice its live texts' quads
571
+ * and `vertices` and `indices` more, none till a text needs them: its
572
+ * texts moved in a row from its start as the shapes' are (repack), and
573
+ * their tables given slots again, only the live texts' kept.
574
+ */
575
+ private repackGlyphs(vertices: number, indices: number): void {
410
576
  const gl = this.gl;
411
577
  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;
578
+ const moved: Place[] = [];
579
+ for (const place of this.places) {
580
+ if (place?.glyphs) {
581
+ moved.push(place);
582
+ vertices += place.vertices;
583
+ indices += place.indices;
420
584
  }
421
585
  }
422
586
 
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;
587
+ moved.sort((a, b) => a.vertexStart - b.vertexStart);
425
588
  this.state.bindVertexArray(this.glyphArray);
426
589
  gl.bindBuffer(gl.ARRAY_BUFFER, this.glyphBuffer);
590
+ const copy = moved.length > 0 && g.top > 0 ? this.copyOf(g.top * GLYPH_BYTES) : null;
591
+ g.capacity = vertices > 0 ? Math.max(GLYPHS_LEAST, vertices * 2) : 0;
592
+ g.indexCapacity = vertices > 0 ? Math.max(GLYPHS_LEAST * 2, indices * 2) : 0;
427
593
  gl.bufferData(gl.ARRAY_BUFFER, g.capacity * GLYPH_BYTES, gl.STATIC_DRAW);
428
594
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, g.indexCapacity * 4, gl.STATIC_DRAW);
429
595
  g.top = 0;
@@ -432,14 +598,44 @@ export class GeometryAtlas {
432
598
  g.holes = 0;
433
599
  // Only the live texts' tables kept.
434
600
  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);
601
+ let from = 0;
602
+ let to = 0;
603
+ let run = 0;
604
+ let written = 0;
605
+ for (const place of moved) {
606
+ written += place.indices;
607
+ }
608
+
609
+ const absolute = new Uint32Array(written);
610
+ for (const place of moved) {
611
+ if (run > 0 && place.vertexStart === from + run && g.top === to + run) {
612
+ run += place.vertices;
613
+ } else {
614
+ this.copyRun(from, to, run, GLYPH_BYTES);
615
+ from = place.vertexStart;
616
+ to = g.top;
617
+ run = place.vertices;
618
+ }
619
+
620
+ const local = place.local;
621
+ for (let i = 0; i < local.length; i++) {
622
+ absolute[g.indexTop + i] = local[i] + g.top;
439
623
  }
624
+
625
+ const glyphs = place.glyphs as NonNullable<Place["glyphs"]>;
626
+ glyphs.slot = this.tables.slot(glyphs.mode, glyphs.table);
627
+ place.vertexStart = g.top;
628
+ place.indexStart = g.indexTop;
629
+ g.top += place.vertices;
630
+ g.indexTop += place.indices;
631
+ g.live += place.vertices;
440
632
  }
441
633
 
442
- this.tables.upload();
634
+ this.copyRun(from, to, run, GLYPH_BYTES);
635
+ this.dropCopy(copy);
636
+ if (written > 0) {
637
+ gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, absolute);
638
+ }
443
639
  }
444
640
 
445
641
  /**
@@ -464,7 +660,10 @@ export class GeometryAtlas {
464
660
  return true;
465
661
  }
466
662
 
467
- /** A shape's `data` copied into `page`'s room taken for it, from vertex `base` and index `indexStart`. */
663
+ /**
664
+ * A shape's `data` copied into `page`'s room taken for it, from vertex
665
+ * `base` and index `indexStart`, its indices kept in a copy of their own.
666
+ */
468
667
  private copyAt(
469
668
  page: Page,
470
669
  id: number,
@@ -474,8 +673,31 @@ export class GeometryAtlas {
474
673
  ): void {
475
674
  const count = data.positions.length / 2;
476
675
  const source = data.indices;
477
- const length = source.length;
478
676
  this.copyVertices(page, data, base);
677
+ this.writeIndices(page, source, base, indexStart);
678
+ this.places[id] = {
679
+ page: page.index,
680
+ vertexStart: base,
681
+ vertices: count,
682
+ indexStart,
683
+ indices: source.length,
684
+ glyphs: null,
685
+ flat: data.flat === true,
686
+ stage: samplesStage(data),
687
+ batch: this.batch(data),
688
+ local: source,
689
+ };
690
+ page.live += count;
691
+ }
692
+
693
+ /** `source`, counted from a geometry's first vertex, made absolute from `base` into `page` from index `indexStart`: staged, or uploaded if too many. */
694
+ private writeIndices(
695
+ page: Page,
696
+ source: Uint16Array | Uint32Array,
697
+ base: number,
698
+ indexStart: number,
699
+ ): void {
700
+ const length = source.length;
479
701
  if (length > STAGED_INDICES) {
480
702
  const indices = new Uint32Array(length);
481
703
  for (let i = 0; i < length; i++) {
@@ -485,37 +707,25 @@ export class GeometryAtlas {
485
707
  this.flushIndices();
486
708
  this.state.bindVertexArray(page.vertexArray);
487
709
  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
- }
710
+ return;
711
+ }
497
712
 
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
- }
713
+ this.stagePage(page);
714
+ if (
715
+ this.stagedIndexCount + length > STAGED_INDICES ||
716
+ this.stagedIndexAt + this.stagedIndexCount !== indexStart
717
+ ) {
718
+ this.flushIndices();
719
+ this.stagedIndexAt = indexStart;
720
+ }
503
721
 
504
- this.stagedIndexCount = at + length;
722
+ const staged = this.stagedIndices;
723
+ const at = this.stagedIndexCount;
724
+ for (let i = 0; i < length; i++) {
725
+ staged[at + i] = source[i] + base;
505
726
  }
506
727
 
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;
728
+ this.stagedIndexCount = at + length;
519
729
  }
520
730
 
521
731
  /** `data`'s vertices interleaved for `page` from vertex `base`: staged, or uploaded if too many. */
@@ -593,15 +803,20 @@ export class GeometryAtlas {
593
803
  : null;
594
804
  }
595
805
 
596
- /** Glyph quads copied in at the top of the glyph atlas, their indices made absolute in it. */
806
+ /**
807
+ * Glyph quads copied in at the top of the glyph atlas, their indices made
808
+ * absolute in it, and kept counted from the text's first vertex as a
809
+ * shape's are.
810
+ */
597
811
  private copyGlyphs(id: number, data: GeometryData): void {
598
812
  const gl = this.gl;
599
813
  const g = this.glyphs;
600
814
  const glyphs = data.glyphs as NonNullable<GeometryData["glyphs"]>;
601
815
  const count = glyphs.vertices.length / GLYPH_FLOATS;
602
- const indices = new Uint32Array(data.indices.length);
816
+ const source = data.indices;
817
+ const indices = new Uint32Array(source.length);
603
818
  for (let i = 0; i < indices.length; i++) {
604
- indices[i] = data.indices[i] + g.top;
819
+ indices[i] = source[i] + g.top;
605
820
  }
606
821
 
607
822
  this.state.bindVertexArray(this.glyphArray);
@@ -622,6 +837,7 @@ export class GeometryAtlas {
622
837
  flat: false,
623
838
  stage: false,
624
839
  batch: null,
840
+ local: source,
625
841
  };
626
842
  g.top += count;
627
843
  g.indexTop += indices.length;