@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,13 +1,7 @@
1
1
  import type { GlState } from "@swf2es/context3d/device";
2
- import type { Place } from "./atlas.js";
2
+ import { type GeometryAtlas, type Place, VERTEX_BYTES } from "./atlas.js";
3
3
  import { COLOR_AT, DRAW_AT, MERGED_LINE, MERGED_UNIT, POSITION_AT, TEXEL_AT } from "./programs.js";
4
4
 
5
- /**
6
- * Atlas attributes followed by where the draw's packed row is in the rows'
7
- * texture: transforms and snapping stay in the shader, and a row that
8
- * changes changes no vertex.
9
- */
10
- const WORDS = 6;
11
5
  /** Bound the extra CPU and GPU storage, falling back to the atlas after it fills. */
12
6
  const VERTICES_MOST = 2 << 20;
13
7
  const INDICES_MOST = 6 << 20;
@@ -36,20 +30,35 @@ interface Segment {
36
30
  * Small geometries merged run by run in painter's order, each run's copy a
37
31
  * segment of one stream that any later run of the same geometries draws
38
32
  * again, wherever it comes in the frame: its rows are in a texture of its
39
- * own, written each frame, not in its vertices. Only the segments a frame adds are staged on the
40
- * CPU, after the stream's top, and uploaded there at once; what is below
41
- * stays on the GPU alone, and a buffer grown keeps it by a copy there.
42
- * Segments no run used in a frame are let go of, and their room once the
43
- * stream is twice what the last frame used, the stream then made anew.
33
+ * own, written each frame, not in its vertices. A segment's vertices are
34
+ * copied from the atlas on the GPU, as the atlas has them, and beside them
35
+ * in a buffer of their own each one's slot of its draw's row in the rows'
36
+ * texture: transforms and snapping stay in the shader, and a row that
37
+ * changes changes no vertex. Only the slots and indices of the segments a
38
+ * frame adds are staged on the CPU, after the stream's top, and uploaded
39
+ * there at once, the indices from the atlas' copies (Place.local), as the
40
+ * geometries' arrays are let go of once in the atlas; what is below stays
41
+ * on the GPU alone, and a buffer grown keeps it by a copy there. Segments
42
+ * no run used in a frame are let go of, and their room once the stream is
43
+ * twice what the last frame used, the stream then made anew.
44
44
  */
45
45
  export class FillBatches {
46
46
  vertexArray!: WebGLVertexArrayObject;
47
47
  private vertexBuffer!: WebGLBuffer;
48
+ private slotBuffer!: WebGLBuffer;
48
49
  private indexBuffer!: WebGLBuffer;
49
- /** The staged words, the same bytes as floats, and indices: the stream from fromVertex and fromIndex. */
50
- private words = new Uint32Array(0);
51
- private floats = new Float32Array(0);
50
+ /** The staged slots and indices: the stream from fromVertex and fromIndex. */
51
+ private slots = new Int32Array(0);
52
52
  private indices = new Uint32Array(0);
53
+ /**
54
+ * The copies from the atlas a frame's segments stage, made as it uploads: each from its page's
55
+ * vertex buffer, from vertex `copyFrom`, to the stream's vertex `copyTo`, `copyCount` vertices.
56
+ */
57
+ private readonly copyPage: WebGLBuffer[] = [];
58
+ private readonly copyFrom: number[] = [];
59
+ private readonly copyTo: number[] = [];
60
+ private readonly copyCount: number[] = [];
61
+ private copies = 0;
53
62
  private vertexCapacity = 0;
54
63
  private indexCapacity = 0;
55
64
  private vertexTop = 0;
@@ -94,6 +103,7 @@ export class FillBatches {
94
103
  constructor(
95
104
  private readonly gl: WebGL2RenderingContext,
96
105
  private readonly state: GlState,
106
+ private readonly atlas: GeometryAtlas,
97
107
  ) {}
98
108
 
99
109
  /** The buffers and the rows' texture made, `textureMost` lines at most, as the context comes. */
@@ -105,18 +115,20 @@ export class FillBatches {
105
115
  this.slotsMost = MERGED_LINE * textureMost;
106
116
  this.vertexArray = gl.createVertexArray() as WebGLVertexArrayObject;
107
117
  this.vertexBuffer = gl.createBuffer() as WebGLBuffer;
118
+ this.slotBuffer = gl.createBuffer() as WebGLBuffer;
108
119
  this.indexBuffer = gl.createBuffer() as WebGLBuffer;
109
120
  this.state.bindVertexArray(this.vertexArray);
110
121
  gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
111
122
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
112
123
  gl.enableVertexAttribArray(POSITION_AT);
113
- gl.vertexAttribPointer(POSITION_AT, 2, gl.FLOAT, false, WORDS * 4, 0);
124
+ gl.vertexAttribPointer(POSITION_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 0);
114
125
  gl.enableVertexAttribArray(TEXEL_AT);
115
- gl.vertexAttribPointer(TEXEL_AT, 2, gl.FLOAT, false, WORDS * 4, 8);
126
+ gl.vertexAttribPointer(TEXEL_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 8);
116
127
  gl.enableVertexAttribArray(COLOR_AT);
117
- gl.vertexAttribPointer(COLOR_AT, 4, gl.UNSIGNED_BYTE, true, WORDS * 4, 16);
128
+ gl.vertexAttribPointer(COLOR_AT, 4, gl.UNSIGNED_BYTE, true, VERTEX_BYTES, 16);
129
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.slotBuffer);
118
130
  gl.enableVertexAttribArray(DRAW_AT);
119
- gl.vertexAttribIPointer(DRAW_AT, 1, gl.INT, WORDS * 4, 20);
131
+ gl.vertexAttribIPointer(DRAW_AT, 1, gl.INT, 4, 0);
120
132
  // Disabled in the atlas VAO: a negative attribute selects the existing run-table path.
121
133
  gl.vertexAttribI4i(DRAW_AT, -1, 0, 0, 0);
122
134
  this.vertexCapacity = 0;
@@ -152,6 +164,7 @@ export class FillBatches {
152
164
  }
153
165
 
154
166
  this.full = false;
167
+ this.copies = 0;
155
168
  const seen = this.seenBefore;
156
169
  this.seenBefore = this.seen;
157
170
  this.seen = seen;
@@ -251,28 +264,20 @@ export class FillBatches {
251
264
  this.vertexTop + vertices - this.fromVertex,
252
265
  this.indexTop + indices - this.fromIndex,
253
266
  );
254
- const floats = this.floats;
255
- const words = this.words;
267
+ const slots = this.slots;
256
268
  const staged = this.indices;
269
+ const pages = this.atlas.pages;
257
270
  for (let i = 0; i < count; i++) {
258
271
  const place = places[i];
259
- const batch = place.batch as Batch;
260
- segment.batches[i] = batch;
261
- const data = batch.data;
272
+ segment.batches[i] = place.batch as Batch;
262
273
  const base = this.vertexTop;
263
- for (let j = 0; j < place.vertices; j++) {
264
- const at = (base - this.fromVertex + j) * WORDS;
265
- floats[at] = data.positions[j * 2];
266
- floats[at + 1] = data.positions[j * 2 + 1];
267
- floats[at + 2] = data.texels ? data.texels[j * 2] : 0;
268
- floats[at + 3] = data.texels ? data.texels[j * 2 + 1] : 0;
269
- words[at + 4] = data.colors[j];
270
- words[at + 5] = segment.slot + i;
271
- }
272
-
274
+ const page = pages[place.page];
275
+ this.stageCopy(page.vertexBuffer, place.vertexStart, base, place.vertices);
276
+ slots.fill(segment.slot + i, base - this.fromVertex, base - this.fromVertex + place.vertices);
277
+ const local = place.local;
273
278
  let at = this.indexTop - this.fromIndex;
274
- for (let j = 0; j < place.indices; j++) {
275
- staged[at++] = base + data.indices[j];
279
+ for (let j = 0; j < local.length; j++) {
280
+ staged[at++] = base + local[j];
276
281
  }
277
282
 
278
283
  this.vertexTop += place.vertices;
@@ -290,6 +295,30 @@ export class FillBatches {
290
295
  return segment;
291
296
  }
292
297
 
298
+ /**
299
+ * `count` vertices from vertex `from` of an atlas page's `buffer` to be copied to the stream's
300
+ * vertex `to` as the frame uploads: joined to the copy before where both run on from it, as
301
+ * geometries placed together lie together.
302
+ */
303
+ private stageCopy(buffer: WebGLBuffer, from: number, to: number, count: number): void {
304
+ const k = this.copies - 1;
305
+ if (
306
+ k >= 0 &&
307
+ this.copyPage[k] === buffer &&
308
+ this.copyFrom[k] + this.copyCount[k] === from &&
309
+ this.copyTo[k] + this.copyCount[k] === to
310
+ ) {
311
+ this.copyCount[k] += count;
312
+ return;
313
+ }
314
+
315
+ this.copyPage[k + 1] = buffer;
316
+ this.copyFrom[k + 1] = from;
317
+ this.copyTo[k + 1] = to;
318
+ this.copyCount[k + 1] = count;
319
+ this.copies = k + 2;
320
+ }
321
+
293
322
  /** Whether the `count` rows from `slot` are `rows`. */
294
323
  private rowsAre(slot: number, rows: Int32Array, count: number): boolean {
295
324
  for (let i = 0; i < count; i++) {
@@ -421,22 +450,27 @@ export class FillBatches {
421
450
  this.watch(staging);
422
451
  }
423
452
 
424
- /** What add staged uploaded where it lies, the buffers grown first to fit the stream. */
453
+ /**
454
+ * What add staged uploaded where it lies, and the vertices it staged copied from the atlas, the
455
+ * buffers grown first to fit the stream.
456
+ */
425
457
  private uploadStaged(): void {
426
458
  const gl = this.gl;
427
459
  this.state.bindVertexArray(this.vertexArray);
428
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
429
460
  if (this.vertexTop > this.vertexCapacity) {
430
461
  this.vertexCapacity = Math.min(
431
462
  VERTICES_MOST,
432
463
  Math.max(this.vertexTop, this.vertexCapacity * 2),
433
464
  );
465
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
434
466
  this.resize(
435
467
  gl.ARRAY_BUFFER,
436
468
  this.vertexBuffer,
437
- this.vertexCapacity * WORDS * 4,
438
- this.fromVertex * WORDS * 4,
469
+ this.vertexCapacity * VERTEX_BYTES,
470
+ this.fromVertex * VERTEX_BYTES,
439
471
  );
472
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.slotBuffer);
473
+ this.resize(gl.ARRAY_BUFFER, this.slotBuffer, this.vertexCapacity * 4, this.fromVertex * 4);
440
474
  }
441
475
 
442
476
  if (this.indexTop > this.indexCapacity) {
@@ -449,13 +483,28 @@ export class FillBatches {
449
483
  );
450
484
  }
451
485
 
486
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.slotBuffer);
452
487
  gl.bufferSubData(
453
488
  gl.ARRAY_BUFFER,
454
- this.fromVertex * WORDS * 4,
455
- this.words,
489
+ this.fromVertex * 4,
490
+ this.slots,
456
491
  0,
457
- (this.vertexTop - this.fromVertex) * WORDS,
492
+ this.vertexTop - this.fromVertex,
458
493
  );
494
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
495
+ for (let k = 0; k < this.copies; k++) {
496
+ gl.bindBuffer(gl.COPY_READ_BUFFER, this.copyPage[k]);
497
+ gl.copyBufferSubData(
498
+ gl.COPY_READ_BUFFER,
499
+ gl.ARRAY_BUFFER,
500
+ this.copyFrom[k] * VERTEX_BYTES,
501
+ this.copyTo[k] * VERTEX_BYTES,
502
+ this.copyCount[k] * VERTEX_BYTES,
503
+ );
504
+ }
505
+
506
+ gl.bindBuffer(gl.COPY_READ_BUFFER, null);
507
+ this.copies = 0;
459
508
  gl.bufferSubData(
460
509
  gl.ELEMENT_ARRAY_BUFFER,
461
510
  this.fromIndex * 4,
@@ -474,21 +523,22 @@ export class FillBatches {
474
523
 
475
524
  this.gl.deleteVertexArray(this.vertexArray);
476
525
  this.gl.deleteBuffer(this.vertexBuffer);
526
+ this.gl.deleteBuffer(this.slotBuffer);
477
527
  this.gl.deleteBuffer(this.indexBuffer);
478
- this.words = new Uint32Array(0);
479
- this.floats = new Float32Array(0);
528
+ this.slots = new Int32Array(0);
480
529
  this.indices = new Uint32Array(0);
530
+ this.copyPage.length = 0;
531
+ this.copies = 0;
481
532
  }
482
533
 
483
534
  /** Staging for at least `vertices` and `indices` from its start, what is staged kept. */
484
535
  private stageRoom(vertices: number, indices: number): void {
485
- if (vertices * WORDS > this.words.length) {
486
- const grown = new Uint32Array(
487
- Math.min(VERTICES_MOST * WORDS, Math.max(vertices * WORDS, this.words.length * 2)),
536
+ if (vertices > this.slots.length) {
537
+ const grown = new Int32Array(
538
+ Math.min(VERTICES_MOST, Math.max(vertices, this.slots.length * 2)),
488
539
  );
489
- grown.set(this.words);
490
- this.words = grown;
491
- this.floats = new Float32Array(grown.buffer);
540
+ grown.set(this.slots);
541
+ this.slots = grown;
492
542
  }
493
543
 
494
544
  if (indices > this.indices.length) {
@@ -529,9 +579,11 @@ export class FillBatches {
529
579
  this.resize(
530
580
  gl.ARRAY_BUFFER,
531
581
  this.vertexBuffer,
532
- vertices * WORDS * 4,
533
- keepVertices * WORDS * 4,
582
+ vertices * VERTEX_BYTES,
583
+ keepVertices * VERTEX_BYTES,
534
584
  );
585
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.slotBuffer);
586
+ this.resize(gl.ARRAY_BUFFER, this.slotBuffer, vertices * 4, keepVertices * 4);
535
587
  }
536
588
 
537
589
  const indices = Math.max(INDICES_LEAST, this.mostIndices * 2);
@@ -541,10 +593,9 @@ export class FillBatches {
541
593
  this.resize(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer, indices * 4, keepIndices * 4);
542
594
  }
543
595
 
544
- const staged = Math.max(VERTICES_LEAST, this.mostStaged * 2) * WORDS;
545
- if (this.mostStaged * 4 * WORDS <= this.words.length && staged < this.words.length) {
546
- this.words = new Uint32Array(staged);
547
- this.floats = new Float32Array(this.words.buffer);
596
+ const staged = Math.max(VERTICES_LEAST, this.mostStaged * 2);
597
+ if (this.mostStaged * 4 <= this.slots.length && staged < this.slots.length) {
598
+ this.slots = new Int32Array(staged);
548
599
  }
549
600
 
550
601
  const stagedIndices = Math.max(INDICES_LEAST, this.mostStagedIndices * 2);
@@ -225,7 +225,7 @@ export class Draws {
225
225
  this.gl = gl;
226
226
  this.state = state;
227
227
  this.atlas = atlas;
228
- this.batches = new FillBatches(gl, state);
228
+ this.batches = new FillBatches(gl, state, atlas);
229
229
  this.gpu = gpu;
230
230
  this.pool = pool;
231
231
  }
@@ -855,8 +855,8 @@ export class Draws {
855
855
  last = j;
856
856
  }
857
857
 
858
+ // Places past the count are left from runs before: trimmed, the array would grow again.
858
859
  this.runCount = draws;
859
- this.runPlaces.length = draws;
860
860
 
861
861
  return last;
862
862
  }