@swf2es/render 0.1.35 → 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.
@@ -1,7 +1,13 @@
1
1
  import type { GlState } from "@swf2es/context3d/device";
2
- import { type GeometryAtlas, type Place, VERTEX_BYTES } from "./atlas.js";
2
+ import type { Place } 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;
5
11
  /** Bound the extra CPU and GPU storage, falling back to the atlas after it fills. */
6
12
  const VERTICES_MOST = 2 << 20;
7
13
  const INDICES_MOST = 6 << 20;
@@ -30,35 +36,20 @@ interface Segment {
30
36
  * Small geometries merged run by run in painter's order, each run's copy a
31
37
  * segment of one stream that any later run of the same geometries draws
32
38
  * again, wherever it comes in the frame: its rows are in a texture of its
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.
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.
44
44
  */
45
45
  export class FillBatches {
46
46
  vertexArray!: WebGLVertexArrayObject;
47
47
  private vertexBuffer!: WebGLBuffer;
48
- private slotBuffer!: WebGLBuffer;
49
48
  private indexBuffer!: WebGLBuffer;
50
- /** The staged slots and indices: the stream from fromVertex and fromIndex. */
51
- private slots = new Int32Array(0);
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);
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;
62
53
  private vertexCapacity = 0;
63
54
  private indexCapacity = 0;
64
55
  private vertexTop = 0;
@@ -103,7 +94,6 @@ export class FillBatches {
103
94
  constructor(
104
95
  private readonly gl: WebGL2RenderingContext,
105
96
  private readonly state: GlState,
106
- private readonly atlas: GeometryAtlas,
107
97
  ) {}
108
98
 
109
99
  /** The buffers and the rows' texture made, `textureMost` lines at most, as the context comes. */
@@ -115,20 +105,18 @@ export class FillBatches {
115
105
  this.slotsMost = MERGED_LINE * textureMost;
116
106
  this.vertexArray = gl.createVertexArray() as WebGLVertexArrayObject;
117
107
  this.vertexBuffer = gl.createBuffer() as WebGLBuffer;
118
- this.slotBuffer = gl.createBuffer() as WebGLBuffer;
119
108
  this.indexBuffer = gl.createBuffer() as WebGLBuffer;
120
109
  this.state.bindVertexArray(this.vertexArray);
121
110
  gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
122
111
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
123
112
  gl.enableVertexAttribArray(POSITION_AT);
124
- gl.vertexAttribPointer(POSITION_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 0);
113
+ gl.vertexAttribPointer(POSITION_AT, 2, gl.FLOAT, false, WORDS * 4, 0);
125
114
  gl.enableVertexAttribArray(TEXEL_AT);
126
- gl.vertexAttribPointer(TEXEL_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 8);
115
+ gl.vertexAttribPointer(TEXEL_AT, 2, gl.FLOAT, false, WORDS * 4, 8);
127
116
  gl.enableVertexAttribArray(COLOR_AT);
128
- gl.vertexAttribPointer(COLOR_AT, 4, gl.UNSIGNED_BYTE, true, VERTEX_BYTES, 16);
129
- gl.bindBuffer(gl.ARRAY_BUFFER, this.slotBuffer);
117
+ gl.vertexAttribPointer(COLOR_AT, 4, gl.UNSIGNED_BYTE, true, WORDS * 4, 16);
130
118
  gl.enableVertexAttribArray(DRAW_AT);
131
- gl.vertexAttribIPointer(DRAW_AT, 1, gl.INT, 4, 0);
119
+ gl.vertexAttribIPointer(DRAW_AT, 1, gl.INT, WORDS * 4, 20);
132
120
  // Disabled in the atlas VAO: a negative attribute selects the existing run-table path.
133
121
  gl.vertexAttribI4i(DRAW_AT, -1, 0, 0, 0);
134
122
  this.vertexCapacity = 0;
@@ -164,7 +152,6 @@ export class FillBatches {
164
152
  }
165
153
 
166
154
  this.full = false;
167
- this.copies = 0;
168
155
  const seen = this.seenBefore;
169
156
  this.seenBefore = this.seen;
170
157
  this.seen = seen;
@@ -264,20 +251,28 @@ export class FillBatches {
264
251
  this.vertexTop + vertices - this.fromVertex,
265
252
  this.indexTop + indices - this.fromIndex,
266
253
  );
267
- const slots = this.slots;
254
+ const floats = this.floats;
255
+ const words = this.words;
268
256
  const staged = this.indices;
269
- const pages = this.atlas.pages;
270
257
  for (let i = 0; i < count; i++) {
271
258
  const place = places[i];
272
- segment.batches[i] = place.batch as Batch;
259
+ const batch = place.batch as Batch;
260
+ segment.batches[i] = batch;
261
+ const data = batch.data;
273
262
  const base = this.vertexTop;
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;
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
+
278
273
  let at = this.indexTop - this.fromIndex;
279
- for (let j = 0; j < local.length; j++) {
280
- staged[at++] = base + local[j];
274
+ for (let j = 0; j < place.indices; j++) {
275
+ staged[at++] = base + data.indices[j];
281
276
  }
282
277
 
283
278
  this.vertexTop += place.vertices;
@@ -295,30 +290,6 @@ export class FillBatches {
295
290
  return segment;
296
291
  }
297
292
 
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
-
322
293
  /** Whether the `count` rows from `slot` are `rows`. */
323
294
  private rowsAre(slot: number, rows: Int32Array, count: number): boolean {
324
295
  for (let i = 0; i < count; i++) {
@@ -450,27 +421,22 @@ export class FillBatches {
450
421
  this.watch(staging);
451
422
  }
452
423
 
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
- */
424
+ /** What add staged uploaded where it lies, the buffers grown first to fit the stream. */
457
425
  private uploadStaged(): void {
458
426
  const gl = this.gl;
459
427
  this.state.bindVertexArray(this.vertexArray);
428
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
460
429
  if (this.vertexTop > this.vertexCapacity) {
461
430
  this.vertexCapacity = Math.min(
462
431
  VERTICES_MOST,
463
432
  Math.max(this.vertexTop, this.vertexCapacity * 2),
464
433
  );
465
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
466
434
  this.resize(
467
435
  gl.ARRAY_BUFFER,
468
436
  this.vertexBuffer,
469
- this.vertexCapacity * VERTEX_BYTES,
470
- this.fromVertex * VERTEX_BYTES,
437
+ this.vertexCapacity * WORDS * 4,
438
+ this.fromVertex * WORDS * 4,
471
439
  );
472
- gl.bindBuffer(gl.ARRAY_BUFFER, this.slotBuffer);
473
- this.resize(gl.ARRAY_BUFFER, this.slotBuffer, this.vertexCapacity * 4, this.fromVertex * 4);
474
440
  }
475
441
 
476
442
  if (this.indexTop > this.indexCapacity) {
@@ -483,28 +449,13 @@ export class FillBatches {
483
449
  );
484
450
  }
485
451
 
486
- gl.bindBuffer(gl.ARRAY_BUFFER, this.slotBuffer);
487
452
  gl.bufferSubData(
488
453
  gl.ARRAY_BUFFER,
489
- this.fromVertex * 4,
490
- this.slots,
454
+ this.fromVertex * WORDS * 4,
455
+ this.words,
491
456
  0,
492
- this.vertexTop - this.fromVertex,
457
+ (this.vertexTop - this.fromVertex) * WORDS,
493
458
  );
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;
508
459
  gl.bufferSubData(
509
460
  gl.ELEMENT_ARRAY_BUFFER,
510
461
  this.fromIndex * 4,
@@ -523,22 +474,21 @@ export class FillBatches {
523
474
 
524
475
  this.gl.deleteVertexArray(this.vertexArray);
525
476
  this.gl.deleteBuffer(this.vertexBuffer);
526
- this.gl.deleteBuffer(this.slotBuffer);
527
477
  this.gl.deleteBuffer(this.indexBuffer);
528
- this.slots = new Int32Array(0);
478
+ this.words = new Uint32Array(0);
479
+ this.floats = new Float32Array(0);
529
480
  this.indices = new Uint32Array(0);
530
- this.copyPage.length = 0;
531
- this.copies = 0;
532
481
  }
533
482
 
534
483
  /** Staging for at least `vertices` and `indices` from its start, what is staged kept. */
535
484
  private stageRoom(vertices: number, indices: number): void {
536
- if (vertices > this.slots.length) {
537
- const grown = new Int32Array(
538
- Math.min(VERTICES_MOST, Math.max(vertices, this.slots.length * 2)),
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)),
539
488
  );
540
- grown.set(this.slots);
541
- this.slots = grown;
489
+ grown.set(this.words);
490
+ this.words = grown;
491
+ this.floats = new Float32Array(grown.buffer);
542
492
  }
543
493
 
544
494
  if (indices > this.indices.length) {
@@ -579,11 +529,9 @@ export class FillBatches {
579
529
  this.resize(
580
530
  gl.ARRAY_BUFFER,
581
531
  this.vertexBuffer,
582
- vertices * VERTEX_BYTES,
583
- keepVertices * VERTEX_BYTES,
532
+ vertices * WORDS * 4,
533
+ keepVertices * WORDS * 4,
584
534
  );
585
- gl.bindBuffer(gl.ARRAY_BUFFER, this.slotBuffer);
586
- this.resize(gl.ARRAY_BUFFER, this.slotBuffer, vertices * 4, keepVertices * 4);
587
535
  }
588
536
 
589
537
  const indices = Math.max(INDICES_LEAST, this.mostIndices * 2);
@@ -593,9 +541,10 @@ export class FillBatches {
593
541
  this.resize(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer, indices * 4, keepIndices * 4);
594
542
  }
595
543
 
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);
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);
599
548
  }
600
549
 
601
550
  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, atlas);
228
+ this.batches = new FillBatches(gl, state);
229
229
  this.gpu = gpu;
230
230
  this.pool = pool;
231
231
  }