@swf2es/render 0.1.33 → 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 (61) 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 +2 -1
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +2 -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/registry.d.ts +4 -0
  19. package/dist/registry.d.ts.map +1 -1
  20. package/dist/registry.js +8 -0
  21. package/dist/registry.js.map +1 -1
  22. package/dist/rows.d.ts +8 -0
  23. package/dist/rows.d.ts.map +1 -1
  24. package/dist/rows.js +78 -9
  25. package/dist/rows.js.map +1 -1
  26. package/dist/scene.d.ts +13 -0
  27. package/dist/scene.d.ts.map +1 -1
  28. package/dist/scene.js +73 -0
  29. package/dist/scene.js.map +1 -1
  30. package/dist/spans.d.ts +27 -0
  31. package/dist/spans.d.ts.map +1 -0
  32. package/dist/spans.js +146 -0
  33. package/dist/spans.js.map +1 -0
  34. package/dist/webgl/atlas.d.ts +83 -18
  35. package/dist/webgl/atlas.d.ts.map +1 -1
  36. package/dist/webgl/atlas.js +271 -110
  37. package/dist/webgl/atlas.js.map +1 -1
  38. package/dist/webgl/backend.d.ts +5 -3
  39. package/dist/webgl/backend.d.ts.map +1 -1
  40. package/dist/webgl/backend.js +9 -9
  41. package/dist/webgl/backend.js.map +1 -1
  42. package/dist/webgl/batches.d.ts +36 -11
  43. package/dist/webgl/batches.d.ts.map +1 -1
  44. package/dist/webgl/batches.js +94 -51
  45. package/dist/webgl/batches.js.map +1 -1
  46. package/dist/webgl/draws.js +2 -2
  47. package/dist/webgl/draws.js.map +1 -1
  48. package/package.json +2 -2
  49. package/src/commands.ts +48 -6
  50. package/src/geometry.ts +109 -19
  51. package/src/index.ts +2 -0
  52. package/src/measure.ts +132 -68
  53. package/src/plan.ts +4 -4
  54. package/src/registry.ts +10 -0
  55. package/src/rows.ts +85 -18
  56. package/src/scene.ts +82 -0
  57. package/src/spans.ts +223 -0
  58. package/src/webgl/atlas.ts +321 -120
  59. package/src/webgl/backend.ts +9 -9
  60. package/src/webgl/batches.ts +108 -57
  61. package/src/webgl/draws.ts +2 -2
@@ -1,4 +1,4 @@
1
- import { GLYPH_FLOATS, samplesStage } from "../geometry.js";
1
+ import { GLYPH_FLOATS, RELEASE_FRAMES, samplesStage, } from "../geometry.js";
2
2
  import { FreeRanges } from "./free-ranges.js";
3
3
  import { GlyphTables } from "./glyph-tables.js";
4
4
  import { COLOR_AT, DEPTH_AT, GLYPH_AT, ORIGIN_AT, POSITION_AT, RUN_AT, TEXEL_AT, TINT_AT, } from "./programs.js";
@@ -6,7 +6,7 @@ import { COLOR_AT, DEPTH_AT, GLYPH_AT, ORIGIN_AT, POSITION_AT, RUN_AT, TEXEL_AT,
6
6
  * Bytes a vertex in the atlas: its position's two floats, its texel's two, then its colour's four
7
7
  * bytes, or a mesh's depth's float.
8
8
  */
9
- const VERTEX_BYTES = 20;
9
+ export const VERTEX_BYTES = 20;
10
10
  /** Bytes a vertex of advanced text's glyph quads, in an atlas of their own. */
11
11
  const GLYPH_BYTES = GLYPH_FLOATS * 4;
12
12
  /** The fewest glyph vertices the glyph atlas holds once a text needs it. */
@@ -49,14 +49,27 @@ function interleave(data, count, floats, words, at) {
49
49
  }
50
50
  }
51
51
  }
52
+ const NO_INDICES = new Uint16Array(0);
53
+ /**
54
+ * `source`, indices into `count` vertices, copied into an array of their own: in 16 bits where
55
+ * they fit, and in a buffer of their own, not the geometry's, which is let go of.
56
+ */
57
+ function localIndices(source, count) {
58
+ const local = count <= 1 << 16 ? new Uint16Array(source.length) : new Uint32Array(source.length);
59
+ local.set(source);
60
+ return local;
61
+ }
52
62
  /**
53
63
  * The geometry atlas: every geometry copied once into a page's vertex and
54
64
  * index buffers, and advanced text's glyph quads into an atlas of their
55
- * own, where the draws find them by their places. A geometry let go of
56
- * leaves holes in its page, which later geometries are placed in once the
57
- * top is full; a page is packed again from the geometries' own arrays only
58
- * when what is added finds no room. Repacking every geometry for its holes
59
- * alone made frames that copied and uploaded the whole atlas again, tens of
65
+ * own, where the draws find them by their places. Only a geometry's indices
66
+ * stay on the CPU once copied (Place.local), counted from its first vertex,
67
+ * and its registry lets go of its arrays (Geometries.release). A geometry
68
+ * let go of leaves holes in its page, which later geometries are placed in
69
+ * once the top is full; a page is packed again only when what is added
70
+ * finds no room, its vertices moved on the GPU and its indices written
71
+ * again from its own. Repacking every geometry for its holes alone made
72
+ * frames that copied and uploaded the whole atlas again, tens of
60
73
  * megabytes, every few seconds of a busy SWF. The glyph atlas is packed
61
74
  * again once its holes outweigh what is live, which also frees the texts'
62
75
  * tables' slots.
@@ -93,6 +106,15 @@ export class GeometryAtlas {
93
106
  };
94
107
  /** The texts' modes and tables, which the glyph program reads. */
95
108
  tables;
109
+ /** The ids counted among a frame's additions, each once (place). */
110
+ counted = new Set();
111
+ /**
112
+ * The places made, with their ids and frames, whose geometries' arrays are let go of once they
113
+ * have stood RELEASE_FRAMES of their registry's (settle), oldest first.
114
+ */
115
+ settling = [];
116
+ settlingIds = [];
117
+ settlingFrames = [];
96
118
  constructor(gl, state) {
97
119
  this.gl = gl;
98
120
  this.state = state;
@@ -131,6 +153,9 @@ export class GeometryAtlas {
131
153
  this.stagedVertices = 0;
132
154
  this.stagedIndexCount = 0;
133
155
  this.places = [];
156
+ this.settling = [];
157
+ this.settlingIds = [];
158
+ this.settlingFrames = [];
134
159
  this.tables.setUp();
135
160
  }
136
161
  /** A page's buffers and vertex array made, with no room till it is packed. */
@@ -166,6 +191,8 @@ export class GeometryAtlas {
166
191
  return;
167
192
  }
168
193
  this.places[id] = null;
194
+ // Its geometry's own, where it has not settled: a settling entry would keep them alive.
195
+ place.local = NO_INDICES;
169
196
  if (place.glyphs) {
170
197
  this.glyphs.live -= place.vertices;
171
198
  this.glyphs.holes += place.vertices;
@@ -179,10 +206,11 @@ export class GeometryAtlas {
179
206
  }
180
207
  /**
181
208
  * The geometries removed since the last frame let go of, and those added
182
- * copied in: the shapes together at their page's top or in a hole that
183
- * holds them all, else each in the first hole that holds it, else into a
184
- * page packed again with room for twice what is live; a text at the
185
- * glyph atlas' top, else into one packed again.
209
+ * copied in, their arrays then let go of by the registry: the shapes
210
+ * together at their page's top or in a hole that holds them all, else
211
+ * each in the first hole that holds it, else into a page packed again
212
+ * with room for twice what is live; a text at the glyph atlas' top, else
213
+ * into one packed again.
186
214
  */
187
215
  place(geometries) {
188
216
  this.frame++;
@@ -197,7 +225,8 @@ export class GeometryAtlas {
197
225
  page.indices.reset(page.indices.capacity);
198
226
  }
199
227
  }
200
- // Changed in place: copied again where they lie, else as added.
228
+ // Changed in place, which only a geometry added to be kept is: copied again where they lie,
229
+ // else as added.
201
230
  for (const id of geometries.updated) {
202
231
  const place = this.places[id];
203
232
  const data = geometries.get(id);
@@ -212,25 +241,33 @@ export class GeometryAtlas {
212
241
  }
213
242
  }
214
243
  geometries.updated.length = 0;
215
- // Found flat: what is added after reads it from its data.
244
+ // Found flat: what is added after reads it from its registry.
216
245
  for (const id of geometries.flattened) {
217
246
  const place = this.places[id];
218
247
  if (place) {
219
- place.flat = geometries.get(id)?.flat === true;
248
+ place.flat = geometries.flat(id);
220
249
  }
221
250
  }
222
251
  geometries.flattened.length = 0;
252
+ // An id let go of and taken again in a frame comes twice, its arrays the second's: counted
253
+ // once, and copied once, as its arrays are let go of once copied.
254
+ const counted = this.counted;
255
+ counted.clear();
223
256
  let vertices = 0;
224
257
  let indices = 0;
225
258
  let glyphVertices = 0;
226
259
  let glyphIndices = 0;
227
260
  for (const id of geometries.added) {
228
261
  const data = geometries.get(id);
229
- if (data?.glyphs) {
262
+ if (!data || counted.has(id)) {
263
+ continue;
264
+ }
265
+ counted.add(id);
266
+ if (data.glyphs) {
230
267
  glyphVertices += data.glyphs.vertices.length / GLYPH_FLOATS;
231
268
  glyphIndices += data.indices.length;
232
269
  }
233
- else if (data) {
270
+ else {
234
271
  vertices += data.positions.length / 2;
235
272
  indices += data.indices.length;
236
273
  }
@@ -240,23 +277,21 @@ export class GeometryAtlas {
240
277
  // room, or below, when holes too small for one of them leave it none.
241
278
  const g = this.glyphs;
242
279
  const page = this.pages[0];
243
- let shapes = vertices > page.vertices.room() || indices > page.indices.room();
244
- const texts = g.top + glyphVertices > g.capacity ||
280
+ if (vertices > page.vertices.room() || indices > page.indices.room()) {
281
+ this.repack(vertices, indices);
282
+ }
283
+ if (g.top + glyphVertices > g.capacity ||
245
284
  g.indexTop + glyphIndices > g.indexCapacity ||
246
285
  (g.holes > GLYPHS_LEAST && g.holes > g.live) ||
247
- this.tables.full;
248
- if (shapes) {
249
- this.repack(geometries);
250
- }
251
- if (texts) {
252
- this.repackGlyphs(geometries);
286
+ this.tables.full) {
287
+ this.repackGlyphs(glyphVertices, glyphIndices);
253
288
  }
254
289
  // The shapes added together in one range where one holds them all, at the top or in a
255
290
  // hole, so that they go up in one upload each for vertices and indices; else each in the
256
291
  // first that holds it, which a churning atlas would leave to a call or two a geometry.
257
292
  let vertexAt = -1;
258
293
  let indexAt = -1;
259
- if (!shapes && vertices > 0) {
294
+ if (vertices > 0) {
260
295
  vertexAt = page.vertices.take(vertices);
261
296
  indexAt = vertexAt < 0 ? -1 : page.indices.take(indices);
262
297
  if (vertexAt >= 0 && indexAt < 0) {
@@ -266,89 +301,174 @@ export class GeometryAtlas {
266
301
  }
267
302
  for (const id of geometries.added) {
268
303
  const data = geometries.get(id);
269
- if (!data) {
304
+ if (!data || !counted.delete(id)) {
270
305
  continue;
271
306
  }
272
- // Added twice in a frame, as an id let go of and taken again can be: its first copy's
273
- // room given back, which only packing would otherwise find again. A packed atlas holds
274
- // its geometries already.
307
+ // Added again after it was placed, as a geometry kept and changed can be, or after a lost
308
+ // context: its first copy's room given back, which only packing would otherwise find again.
309
+ this.letGo(id);
275
310
  if (data.glyphs) {
276
- if (!texts) {
277
- this.letGo(id);
278
- this.copyGlyphs(id, data);
279
- }
280
- continue;
281
- }
282
- if (shapes) {
283
- continue;
311
+ this.copyGlyphs(id, data);
284
312
  }
285
- this.letGo(id);
286
- if (vertexAt >= 0) {
313
+ else if (vertexAt >= 0) {
287
314
  this.copyAt(page, id, data, vertexAt, indexAt);
288
315
  vertexAt += data.positions.length / 2;
289
316
  indexAt += data.indices.length;
290
317
  }
291
318
  else if (!this.copyIn(page, id, data)) {
292
- // Packing copies every live shape, those added after this one too.
293
- shapes = true;
294
- this.repack(geometries);
319
+ // Packed with room for this one and those after it, which it then holds.
320
+ this.repack(vertices, indices);
321
+ this.copyIn(page, id, data);
295
322
  }
323
+ if (!data.glyphs) {
324
+ vertices -= data.positions.length / 2;
325
+ indices -= data.indices.length;
326
+ }
327
+ this.settling.push(this.places[id]);
328
+ this.settlingIds.push(id);
329
+ this.settlingFrames.push(geometries.frame);
296
330
  }
297
331
  geometries.added.length = 0;
298
332
  this.flush();
299
333
  this.tables.upload();
334
+ this.settle(geometries);
300
335
  }
301
- /** Every live shape copied into its page's buffers made anew, with room for twice them. */
302
- repack(geometries) {
303
- const gl = this.gl;
304
- const ids = geometries.live();
305
- let vertices = 0;
306
- let indices = 0;
307
- for (const id of ids) {
308
- const data = geometries.get(id);
309
- if (!data.glyphs) {
310
- vertices += data.positions.length / 2;
311
- indices += data.indices.length;
336
+ /**
337
+ * The geometries placed RELEASE_FRAMES of their registry's ago or more, by its frames, not the
338
+ * atlas', which a BitmapData's draws advance too, and placed still: their indices copied into
339
+ * arrays of their own and their arrays let go of (Geometries.release), only while the context
340
+ * is not known to be lost: copies made into a lost one are no copies, and the arrays stay for
341
+ * the restore. In Chrome's window before it flags a loss (webgl/losses.ts), arrays may go
342
+ * after copies that never reached the GPU; the restore's Geometries.lost rebuilds them.
343
+ */
344
+ settle(geometries) {
345
+ const places = this.settling;
346
+ const ids = this.settlingIds;
347
+ const frames = this.settlingFrames;
348
+ let k = 0;
349
+ while (k < places.length && geometries.frame - frames[k] >= RELEASE_FRAMES) {
350
+ k++;
351
+ }
352
+ if (k === 0 || this.gl.isContextLost()) {
353
+ return;
354
+ }
355
+ for (let i = 0; i < k; i++) {
356
+ const place = places[i];
357
+ if (this.places[ids[i]] === place && geometries.get(ids[i])) {
358
+ place.local = localIndices(place.local, place.vertices);
359
+ geometries.release(ids[i]);
312
360
  }
313
361
  }
362
+ places.splice(0, k);
363
+ ids.splice(0, k);
364
+ frames.splice(0, k);
365
+ }
366
+ /**
367
+ * The shapes' page packed again, with room for twice its live shapes and
368
+ * `vertices` and `indices` more: its shapes moved in a row from its start,
369
+ * their vertices copied on the GPU, through a buffer of their own, as a
370
+ * new store loses the old one's, and their indices written again from
371
+ * their own (Place.local). Their places are moved, not made anew, so that a run's
372
+ * stream still finds them (Place.batch).
373
+ */
374
+ repack(vertices, indices) {
375
+ const gl = this.gl;
314
376
  const page = this.pages[0];
377
+ const moved = [];
378
+ let used = 0;
379
+ for (const place of this.places) {
380
+ if (place && !place.glyphs) {
381
+ moved.push(place);
382
+ vertices += place.vertices;
383
+ indices += place.indices;
384
+ used = Math.max(used, place.vertexStart + place.vertices);
385
+ }
386
+ }
387
+ // In their order in the page, so that shapes placed together move in one copy.
388
+ moved.sort((a, b) => a.vertexStart - b.vertexStart);
389
+ // What is staged goes up first, for the copy to hold it.
390
+ this.flush();
391
+ this.state.bindVertexArray(page.vertexArray);
392
+ gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
393
+ const copy = used > 0 ? this.copyOf(used * VERTEX_BYTES) : null;
315
394
  page.vertices.reset(Math.max(LEAST, vertices * 2));
316
395
  page.indices.reset(Math.max(LEAST * 3, indices * 2));
317
396
  page.live = 0;
318
- this.state.bindVertexArray(page.vertexArray);
319
- gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
320
397
  gl.bufferData(gl.ARRAY_BUFFER, page.vertices.capacity * VERTEX_BYTES, gl.STATIC_DRAW);
321
398
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, page.indices.capacity * 4, gl.STATIC_DRAW);
322
- // What was staged for the buffers replaced is copied again below.
323
- this.stagedVertices = 0;
324
- this.stagedIndexCount = 0;
325
- // Each at the top, which holds twice them all.
326
- for (const id of ids) {
327
- const data = geometries.get(id);
328
- if (!data.glyphs) {
329
- this.copyIn(page, id, data);
399
+ let from = 0;
400
+ let to = 0;
401
+ let run = 0;
402
+ for (const place of moved) {
403
+ const base = page.vertices.take(place.vertices);
404
+ const indexStart = page.indices.take(place.indices);
405
+ if (run > 0 && place.vertexStart === from + run && base === to + run) {
406
+ run += place.vertices;
407
+ }
408
+ else {
409
+ this.copyRun(from, to, run, VERTEX_BYTES);
410
+ from = place.vertexStart;
411
+ to = base;
412
+ run = place.vertices;
330
413
  }
414
+ this.writeIndices(page, place.local, base, indexStart);
415
+ place.vertexStart = base;
416
+ place.indexStart = indexStart;
417
+ page.live += place.vertices;
331
418
  }
419
+ this.copyRun(from, to, run, VERTEX_BYTES);
420
+ this.dropCopy(copy);
332
421
  this.flush();
333
422
  }
334
- /** Every live text's quads copied into new glyph buffers: none till a text needs them. */
335
- repackGlyphs(geometries) {
423
+ /**
424
+ * A copy on the GPU of the first `bytes` of the buffer bound to ARRAY_BUFFER, bound to
425
+ * COPY_READ_BUFFER, for copyRun to read from once the buffer is made anew.
426
+ */
427
+ copyOf(bytes) {
428
+ const gl = this.gl;
429
+ const copy = gl.createBuffer();
430
+ gl.bindBuffer(gl.COPY_WRITE_BUFFER, copy);
431
+ gl.bufferData(gl.COPY_WRITE_BUFFER, bytes, gl.STREAM_COPY);
432
+ gl.copyBufferSubData(gl.ARRAY_BUFFER, gl.COPY_WRITE_BUFFER, 0, 0, bytes);
433
+ gl.bindBuffer(gl.COPY_WRITE_BUFFER, null);
434
+ gl.bindBuffer(gl.COPY_READ_BUFFER, copy);
435
+ return copy;
436
+ }
437
+ /** `count` vertices of `bytes` each copied from vertex `from` of the copy (copyOf) to vertex `to` of ARRAY_BUFFER's. */
438
+ copyRun(from, to, count, bytes) {
439
+ if (count > 0) {
440
+ this.gl.copyBufferSubData(this.gl.COPY_READ_BUFFER, this.gl.ARRAY_BUFFER, from * bytes, to * bytes, count * bytes);
441
+ }
442
+ }
443
+ dropCopy(copy) {
444
+ if (copy) {
445
+ this.gl.bindBuffer(this.gl.COPY_READ_BUFFER, null);
446
+ this.gl.deleteBuffer(copy);
447
+ }
448
+ }
449
+ /**
450
+ * The glyph atlas packed again, with room for twice its live texts' quads
451
+ * and `vertices` and `indices` more, none till a text needs them: its
452
+ * texts moved in a row from its start as the shapes' are (repack), and
453
+ * their tables given slots again, only the live texts' kept.
454
+ */
455
+ repackGlyphs(vertices, indices) {
336
456
  const gl = this.gl;
337
457
  const g = this.glyphs;
338
- const ids = geometries.live();
339
- let vertices = 0;
340
- let indices = 0;
341
- for (const id of ids) {
342
- const data = geometries.get(id);
343
- if (data.glyphs) {
344
- vertices += data.glyphs.vertices.length / GLYPH_FLOATS;
345
- indices += data.indices.length;
458
+ const moved = [];
459
+ for (const place of this.places) {
460
+ if (place?.glyphs) {
461
+ moved.push(place);
462
+ vertices += place.vertices;
463
+ indices += place.indices;
346
464
  }
347
465
  }
348
- g.capacity = vertices > 0 ? Math.max(GLYPHS_LEAST, vertices * 2) : 0;
349
- g.indexCapacity = vertices > 0 ? Math.max(GLYPHS_LEAST * 2, indices * 2) : 0;
466
+ moved.sort((a, b) => a.vertexStart - b.vertexStart);
350
467
  this.state.bindVertexArray(this.glyphArray);
351
468
  gl.bindBuffer(gl.ARRAY_BUFFER, this.glyphBuffer);
469
+ const copy = moved.length > 0 && g.top > 0 ? this.copyOf(g.top * GLYPH_BYTES) : null;
470
+ g.capacity = vertices > 0 ? Math.max(GLYPHS_LEAST, vertices * 2) : 0;
471
+ g.indexCapacity = vertices > 0 ? Math.max(GLYPHS_LEAST * 2, indices * 2) : 0;
352
472
  gl.bufferData(gl.ARRAY_BUFFER, g.capacity * GLYPH_BYTES, gl.STATIC_DRAW);
353
473
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, g.indexCapacity * 4, gl.STATIC_DRAW);
354
474
  g.top = 0;
@@ -357,13 +477,41 @@ export class GeometryAtlas {
357
477
  g.holes = 0;
358
478
  // Only the live texts' tables kept.
359
479
  this.tables.reset();
360
- for (const id of ids) {
361
- const data = geometries.get(id);
362
- if (data.glyphs) {
363
- this.copyGlyphs(id, data);
480
+ let from = 0;
481
+ let to = 0;
482
+ let run = 0;
483
+ let written = 0;
484
+ for (const place of moved) {
485
+ written += place.indices;
486
+ }
487
+ const absolute = new Uint32Array(written);
488
+ for (const place of moved) {
489
+ if (run > 0 && place.vertexStart === from + run && g.top === to + run) {
490
+ run += place.vertices;
491
+ }
492
+ else {
493
+ this.copyRun(from, to, run, GLYPH_BYTES);
494
+ from = place.vertexStart;
495
+ to = g.top;
496
+ run = place.vertices;
364
497
  }
498
+ const local = place.local;
499
+ for (let i = 0; i < local.length; i++) {
500
+ absolute[g.indexTop + i] = local[i] + g.top;
501
+ }
502
+ const glyphs = place.glyphs;
503
+ glyphs.slot = this.tables.slot(glyphs.mode, glyphs.table);
504
+ place.vertexStart = g.top;
505
+ place.indexStart = g.indexTop;
506
+ g.top += place.vertices;
507
+ g.indexTop += place.indices;
508
+ g.live += place.vertices;
509
+ }
510
+ this.copyRun(from, to, run, GLYPH_BYTES);
511
+ this.dropCopy(copy);
512
+ if (written > 0) {
513
+ gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, absolute);
365
514
  }
366
- this.tables.upload();
367
515
  }
368
516
  /**
369
517
  * A shape's `data` copied into `page`, at its top or in a hole, its
@@ -384,48 +532,55 @@ export class GeometryAtlas {
384
532
  this.copyAt(page, id, data, base, indexStart);
385
533
  return true;
386
534
  }
387
- /** A shape's `data` copied into `page`'s room taken for it, from vertex `base` and index `indexStart`. */
535
+ /**
536
+ * A shape's `data` copied into `page`'s room taken for it, from vertex
537
+ * `base` and index `indexStart`, its indices kept in a copy of their own.
538
+ */
388
539
  copyAt(page, id, data, base, indexStart) {
389
540
  const count = data.positions.length / 2;
390
541
  const source = data.indices;
391
- const length = source.length;
392
542
  this.copyVertices(page, data, base);
393
- if (length > STAGED_INDICES) {
394
- const indices = new Uint32Array(length);
395
- for (let i = 0; i < length; i++) {
396
- indices[i] = source[i] + base;
397
- }
398
- this.flushIndices();
399
- this.state.bindVertexArray(page.vertexArray);
400
- this.gl.bufferSubData(this.gl.ELEMENT_ARRAY_BUFFER, indexStart * 4, indices);
401
- }
402
- else {
403
- this.stagePage(page);
404
- if (this.stagedIndexCount + length > STAGED_INDICES ||
405
- this.stagedIndexAt + this.stagedIndexCount !== indexStart) {
406
- this.flushIndices();
407
- this.stagedIndexAt = indexStart;
408
- }
409
- const staged = this.stagedIndices;
410
- const at = this.stagedIndexCount;
411
- for (let i = 0; i < length; i++) {
412
- staged[at + i] = source[i] + base;
413
- }
414
- this.stagedIndexCount = at + length;
415
- }
543
+ this.writeIndices(page, source, base, indexStart);
416
544
  this.places[id] = {
417
545
  page: page.index,
418
546
  vertexStart: base,
419
547
  vertices: count,
420
548
  indexStart,
421
- indices: length,
549
+ indices: source.length,
422
550
  glyphs: null,
423
551
  flat: data.flat === true,
424
552
  stage: samplesStage(data),
425
553
  batch: this.batch(data),
554
+ local: source,
426
555
  };
427
556
  page.live += count;
428
557
  }
558
+ /** `source`, counted from a geometry's first vertex, made absolute from `base` into `page` from index `indexStart`: staged, or uploaded if too many. */
559
+ writeIndices(page, source, base, indexStart) {
560
+ const length = source.length;
561
+ if (length > STAGED_INDICES) {
562
+ const indices = new Uint32Array(length);
563
+ for (let i = 0; i < length; i++) {
564
+ indices[i] = source[i] + base;
565
+ }
566
+ this.flushIndices();
567
+ this.state.bindVertexArray(page.vertexArray);
568
+ this.gl.bufferSubData(this.gl.ELEMENT_ARRAY_BUFFER, indexStart * 4, indices);
569
+ return;
570
+ }
571
+ this.stagePage(page);
572
+ if (this.stagedIndexCount + length > STAGED_INDICES ||
573
+ this.stagedIndexAt + this.stagedIndexCount !== indexStart) {
574
+ this.flushIndices();
575
+ this.stagedIndexAt = indexStart;
576
+ }
577
+ const staged = this.stagedIndices;
578
+ const at = this.stagedIndexCount;
579
+ for (let i = 0; i < length; i++) {
580
+ staged[at + i] = source[i] + base;
581
+ }
582
+ this.stagedIndexCount = at + length;
583
+ }
429
584
  /** `data`'s vertices interleaved for `page` from vertex `base`: staged, or uploaded if too many. */
430
585
  copyVertices(page, data, base) {
431
586
  const count = data.positions.length / 2;
@@ -476,18 +631,23 @@ export class GeometryAtlas {
476
631
  }
477
632
  batch(data) {
478
633
  return data.positions.length <= 512 && data.indices.length <= 1024 && !data.depths
479
- ? { data, frame: this.frame }
634
+ ? { frame: this.frame }
480
635
  : null;
481
636
  }
482
- /** Glyph quads copied in at the top of the glyph atlas, their indices made absolute in it. */
637
+ /**
638
+ * Glyph quads copied in at the top of the glyph atlas, their indices made
639
+ * absolute in it, and kept counted from the text's first vertex as a
640
+ * shape's are.
641
+ */
483
642
  copyGlyphs(id, data) {
484
643
  const gl = this.gl;
485
644
  const g = this.glyphs;
486
645
  const glyphs = data.glyphs;
487
646
  const count = glyphs.vertices.length / GLYPH_FLOATS;
488
- const indices = new Uint32Array(data.indices.length);
647
+ const source = data.indices;
648
+ const indices = new Uint32Array(source.length);
489
649
  for (let i = 0; i < indices.length; i++) {
490
- indices[i] = data.indices[i] + g.top;
650
+ indices[i] = source[i] + g.top;
491
651
  }
492
652
  this.state.bindVertexArray(this.glyphArray);
493
653
  gl.bindBuffer(gl.ARRAY_BUFFER, this.glyphBuffer);
@@ -507,6 +667,7 @@ export class GeometryAtlas {
507
667
  flat: false,
508
668
  stage: false,
509
669
  batch: null,
670
+ local: source,
510
671
  };
511
672
  g.top += count;
512
673
  g.indexTop += indices.length;