@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.
- package/dist/commands.d.ts +11 -2
- package/dist/commands.d.ts.map +1 -1
- package/dist/commands.js +36 -6
- package/dist/commands.js.map +1 -1
- package/dist/geometry.d.ts +62 -5
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +100 -17
- package/dist/geometry.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/measure.d.ts.map +1 -1
- package/dist/measure.js +118 -61
- package/dist/measure.js.map +1 -1
- package/dist/plan.js +4 -4
- package/dist/plan.js.map +1 -1
- package/dist/rows.d.ts +4 -0
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +59 -10
- package/dist/rows.js.map +1 -1
- package/dist/scene.d.ts.map +1 -1
- package/dist/scene.js +37 -5
- package/dist/scene.js.map +1 -1
- package/dist/webgl/atlas.d.ts +83 -18
- package/dist/webgl/atlas.d.ts.map +1 -1
- package/dist/webgl/atlas.js +271 -110
- package/dist/webgl/atlas.js.map +1 -1
- package/dist/webgl/backend.d.ts +5 -3
- package/dist/webgl/backend.d.ts.map +1 -1
- package/dist/webgl/backend.js +9 -9
- package/dist/webgl/backend.js.map +1 -1
- package/dist/webgl/batches.d.ts +36 -11
- package/dist/webgl/batches.d.ts.map +1 -1
- package/dist/webgl/batches.js +94 -51
- package/dist/webgl/batches.js.map +1 -1
- package/dist/webgl/draws.js +2 -2
- package/dist/webgl/draws.js.map +1 -1
- package/package.json +2 -2
- package/src/commands.ts +48 -6
- package/src/geometry.ts +109 -19
- package/src/index.ts +1 -0
- package/src/measure.ts +132 -68
- package/src/plan.ts +4 -4
- package/src/rows.ts +62 -19
- package/src/scene.ts +41 -5
- package/src/webgl/atlas.ts +321 -120
- package/src/webgl/backend.ts +9 -9
- package/src/webgl/batches.ts +108 -57
- package/src/webgl/draws.ts +2 -2
package/dist/webgl/atlas.js
CHANGED
|
@@ -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.
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
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
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
* glyph atlas' top, else
|
|
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,
|
|
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
|
|
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.
|
|
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
|
|
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
|
|
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
|
-
|
|
244
|
-
|
|
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
|
-
|
|
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 (
|
|
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
|
|
273
|
-
// room given back, which only packing would otherwise find again.
|
|
274
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
293
|
-
|
|
294
|
-
this.
|
|
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
|
-
/**
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
const
|
|
328
|
-
if (
|
|
329
|
-
|
|
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
|
-
/**
|
|
335
|
-
|
|
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
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
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
|
-
|
|
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
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
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
|
-
/**
|
|
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
|
-
|
|
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
|
-
? {
|
|
634
|
+
? { frame: this.frame }
|
|
480
635
|
: null;
|
|
481
636
|
}
|
|
482
|
-
/**
|
|
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
|
|
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] =
|
|
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;
|