@swf2es/render 0.1.24 → 0.1.26

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,11 +1,28 @@
1
1
  import type { GlState } from "@swf2es/context3d/device";
2
2
  import { type Geometries, type GeometryData } from "../geometry.js";
3
+ import { FreeRanges } from "./free-ranges.js";
3
4
  import { GlyphTables } from "./glyph-tables.js";
4
5
  /**
5
- * Where a geometry lies in the atlas, or the glyph atlas with its text's
6
- * mode and table, and their slot in the glyph tables (-1 past their room).
6
+ * A vertex and an index buffer of the atlas, its vertex array reading
7
+ * them, the room of each and the vertices live in it.
8
+ */
9
+ export interface Page {
10
+ /** Its index in the atlas' pages, which places name it by. */
11
+ index: number;
12
+ vertexArray: WebGLVertexArrayObject;
13
+ vertexBuffer: WebGLBuffer;
14
+ indexBuffer: WebGLBuffer;
15
+ vertices: FreeRanges;
16
+ indices: FreeRanges;
17
+ live: number;
18
+ }
19
+ /**
20
+ * Where a geometry lies in the atlas, its page and its ranges there, or in
21
+ * the glyph atlas with its text's mode and table, and their slot in the
22
+ * glyph tables (-1 past their room).
7
23
  */
8
24
  export interface Place {
25
+ page: number;
9
26
  vertexStart: number;
10
27
  vertices: number;
11
28
  indexStart: number;
@@ -24,31 +41,31 @@ export interface Place {
24
41
  } | null;
25
42
  }
26
43
  /**
27
- * The geometry atlas: every geometry copied once into a vertex and an index
28
- * buffer, and advanced text's glyph quads into an atlas of their own, where
29
- * the draws find them by their places. A geometry let go of leaves a hole,
30
- * and each atlas is packed again from the geometries' own arrays once the
31
- * holes outweigh what is live.
44
+ * The geometry atlas: every geometry copied once into a page's vertex and
45
+ * index buffers, and advanced text's glyph quads into an atlas of their
46
+ * own, where the draws find them by their places. A geometry let go of
47
+ * leaves holes in its page, which later geometries are placed in once the
48
+ * top is full; a page is packed again from the geometries' own arrays only
49
+ * when what is added finds no room. Repacking every geometry for its holes
50
+ * alone made frames that copied and uploaded the whole atlas again, tens of
51
+ * megabytes, every few seconds of a busy SWF. The glyph atlas is packed
52
+ * again once its holes outweigh what is live, which also frees the texts'
53
+ * tables' slots.
32
54
  */
33
55
  export declare class GeometryAtlas {
34
56
  frame: number;
35
57
  private readonly gl;
36
58
  private readonly state;
37
- vertexArray: WebGLVertexArrayObject;
38
- private vertexBuffer;
39
- private indexBuffer;
40
- private vertexCapacity;
41
- private indexCapacity;
42
- private vertexTop;
43
- private indexTop;
44
- private live;
45
- private holes;
46
- /** Vertices staged for the atlas from vertex `stagedVertexAt`, as floats and as words. */
59
+ /** The pages, one for now, which places name by index. */
60
+ pages: Page[];
61
+ /** The page the staging is for. */
62
+ private stagedPage;
63
+ /** Vertices staged for `stagedPage` from vertex `stagedVertexAt`, as floats and as words. */
47
64
  private readonly stagedFloats;
48
65
  private readonly stagedWords;
49
66
  private stagedVertexAt;
50
67
  private stagedVertices;
51
- /** Indices staged for the atlas from index `stagedIndexAt`. */
68
+ /** Indices staged for `stagedPage` from index `stagedIndexAt`. */
52
69
  private readonly stagedIndices;
53
70
  private stagedIndexAt;
54
71
  private stagedIndexCount;
@@ -63,22 +80,36 @@ export declare class GeometryAtlas {
63
80
  constructor(gl: WebGL2RenderingContext, state: GlState);
64
81
  /** The buffers and vertex arrays made, both atlases empty, as the context comes or comes back. */
65
82
  setUp(): void;
83
+ /** A page's buffers and vertex array made, with no room till it is packed. */
84
+ private makePage;
85
+ /** Geometry `id`'s place, if it has one, let go of: a shape's ranges free in its page. */
86
+ private letGo;
66
87
  /**
67
88
  * The geometries removed since the last frame let go of, and those added
68
- * copied in: at the top while they fit, else into an atlas packed again,
69
- * grown to twice what is live.
89
+ * copied in: the shapes together at their page's top or in a hole that
90
+ * holds them all, else each in the first hole that holds it, else into a
91
+ * page packed again with room for twice what is live; a text at the
92
+ * glyph atlas' top, else into one packed again.
70
93
  */
71
94
  place(geometries: Geometries): void;
72
- /** Every live shape's geometry copied into new buffers, with room for twice what is live. */
95
+ /** Every live shape copied into its page's buffers made anew, with room for twice them. */
73
96
  repack(geometries: Geometries): void;
74
97
  /** Every live text's quads copied into new glyph buffers: none till a text needs them. */
75
98
  repackGlyphs(geometries: Geometries): void;
76
- /** `data` copied in at the top of the atlas, its indices made absolute. */
99
+ /**
100
+ * A shape's `data` copied into `page`, at its top or in a hole, its
101
+ * indices made absolute from where its vertices went; false where the
102
+ * page has no room for it.
103
+ */
77
104
  private copyIn;
78
- /** `data`'s vertices interleaved for the atlas from vertex `base`: staged, or uploaded if too many. */
105
+ /** A shape's `data` copied into `page`'s room taken for it, from vertex `base` and index `indexStart`. */
106
+ private copyAt;
107
+ /** `data`'s vertices interleaved for `page` from vertex `base`: staged, or uploaded if too many. */
79
108
  private copyVertices;
80
109
  /** What is staged, uploaded: before anything draws from the atlas. */
81
110
  private flush;
111
+ /** The staging made `page`'s, what it held for another uploaded first. */
112
+ private stagePage;
82
113
  private flushVertices;
83
114
  private flushIndices;
84
115
  private batch;
@@ -1 +1 @@
1
- {"version":3,"file":"atlas.d.ts","sourceRoot":"","sources":["../../src/webgl/atlas.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,YAAY,EAAgB,MAAM,gBAAgB,CAAC;AAClF,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAoEhD;;;GAGG;AACH,MAAM,WAAW,KAAK;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE;QAAE,IAAI,EAAE,YAAY,CAAC;QAAC,KAAK,EAAE,YAAY,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IACzE,+CAA+C;IAC/C,IAAI,EAAE,OAAO,CAAC;IACd,+EAA+E;IAC/E,KAAK,EAAE;QAAE,IAAI,EA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0BhB,0FAA0F;IAC1F,OAAO,CAAC,KAAK;IAkBb;;;;;;OAMG;IACH,KAAK,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI,CAkHlC;IAED,2FAA2F;IAC3F,MAAM,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI,CAiCnC;IAED,0FAA0F;IAC1F,YAAY,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI,CAkCzC;IAED;;;;OAIG;IACH,OAAO,CAAC,MAAM;IAiBd,0GAA0G;IAC1G,OAAO,CAAC,MAAM;IAoDd,oGAAoG;IACpG,OAAO,CAAC,YAAY;IAwBpB,sEAAsE;IACtE,OAAO,CAAC,KAAK;IAKb,0EAA0E;IAC1E,OAAO,CAAC,SAAS;IAOjB,OAAO,CAAC,aAAa;IAerB,OAAO,CAAC,YAAY;IAepB,OAAO,CAAC,KAAK;IAMb,8FAA8F;IAC9F,OAAO,CAAC,UAAU;IAiClB,kEAAkE;IAClE,OAAO,IAAI,IAAI,CAYd;CACF"}
@@ -1,4 +1,5 @@
1
1
  import { GLYPH_FLOATS } from "../geometry.js";
2
+ import { FreeRanges } from "./free-ranges.js";
2
3
  import { GlyphTables } from "./glyph-tables.js";
3
4
  import { COLOR_AT, DEPTH_AT, GLYPH_AT, ORIGIN_AT, POSITION_AT, RUN_AT, TEXEL_AT, TINT_AT, } from "./programs.js";
4
5
  /**
@@ -12,8 +13,6 @@ const GLYPH_BYTES = GLYPH_FLOATS * 4;
12
13
  const GLYPHS_LEAST = 1024;
13
14
  /** The fewest vertices the atlas holds room for, and its indices thrice that. */
14
15
  const LEAST = 1 << 16;
15
- /** Holes past which, and past the live vertices, the atlas is packed again. */
16
- const SLACK = 1 << 16;
17
16
  /**
18
17
  * The vertices, and the indices, staged before an upload: geometries placed
19
18
  * one after another go to the GPU in one call for each buffer, not two a
@@ -51,31 +50,31 @@ function interleave(data, count, floats, words, at) {
51
50
  }
52
51
  }
53
52
  /**
54
- * The geometry atlas: every geometry copied once into a vertex and an index
55
- * buffer, and advanced text's glyph quads into an atlas of their own, where
56
- * the draws find them by their places. A geometry let go of leaves a hole,
57
- * and each atlas is packed again from the geometries' own arrays once the
58
- * holes outweigh what is live.
53
+ * The geometry atlas: every geometry copied once into a page's vertex and
54
+ * 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
60
+ * megabytes, every few seconds of a busy SWF. The glyph atlas is packed
61
+ * again once its holes outweigh what is live, which also frees the texts'
62
+ * tables' slots.
59
63
  */
60
64
  export class GeometryAtlas {
61
65
  frame = 0;
62
66
  gl;
63
67
  state;
64
- vertexArray;
65
- vertexBuffer;
66
- indexBuffer;
67
- vertexCapacity = 0;
68
- indexCapacity = 0;
69
- vertexTop = 0;
70
- indexTop = 0;
71
- live = 0;
72
- holes = 0;
73
- /** Vertices staged for the atlas from vertex `stagedVertexAt`, as floats and as words. */
68
+ /** The pages, one for now, which places name by index. */
69
+ pages = [];
70
+ /** The page the staging is for. */
71
+ stagedPage = null;
72
+ /** Vertices staged for `stagedPage` from vertex `stagedVertexAt`, as floats and as words. */
74
73
  stagedFloats = new Float32Array(STAGED_VERTICES * (VERTEX_BYTES / 4));
75
74
  stagedWords = new Uint32Array(this.stagedFloats.buffer);
76
75
  stagedVertexAt = 0;
77
76
  stagedVertices = 0;
78
- /** Indices staged for the atlas from index `stagedIndexAt`. */
77
+ /** Indices staged for `stagedPage` from index `stagedIndexAt`. */
79
78
  stagedIndices = new Uint32Array(STAGED_INDICES);
80
79
  stagedIndexAt = 0;
81
80
  stagedIndexCount = 0;
@@ -102,21 +101,7 @@ export class GeometryAtlas {
102
101
  /** The buffers and vertex arrays made, both atlases empty, as the context comes or comes back. */
103
102
  setUp() {
104
103
  const gl = this.gl;
105
- this.vertexArray = gl.createVertexArray();
106
- this.vertexBuffer = gl.createBuffer();
107
- this.indexBuffer = gl.createBuffer();
108
- this.state.bindVertexArray(this.vertexArray);
109
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
110
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
111
- gl.enableVertexAttribArray(POSITION_AT);
112
- gl.vertexAttribPointer(POSITION_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 0);
113
- gl.enableVertexAttribArray(TEXEL_AT);
114
- gl.vertexAttribPointer(TEXEL_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 8);
115
- gl.enableVertexAttribArray(COLOR_AT);
116
- gl.vertexAttribPointer(COLOR_AT, 4, gl.UNSIGNED_BYTE, true, VERTEX_BYTES, 16);
117
- // The same bytes as a mesh's depth, which the mesh program reads in place of the colour.
118
- gl.enableVertexAttribArray(DEPTH_AT);
119
- gl.vertexAttribPointer(DEPTH_AT, 1, gl.FLOAT, false, VERTEX_BYTES, 16);
104
+ this.pages = [this.makePage(0)];
120
105
  this.glyphArray = gl.createVertexArray();
121
106
  this.glyphBuffer = gl.createBuffer();
122
107
  this.glyphIndexBuffer = gl.createBuffer();
@@ -142,58 +127,86 @@ export class GeometryAtlas {
142
127
  live: 0,
143
128
  holes: 0,
144
129
  });
145
- this.vertexCapacity = 0;
146
- this.indexCapacity = 0;
130
+ this.stagedPage = null;
147
131
  this.stagedVertices = 0;
148
132
  this.stagedIndexCount = 0;
149
133
  this.places = [];
150
134
  this.tables.setUp();
151
135
  }
136
+ /** A page's buffers and vertex array made, with no room till it is packed. */
137
+ makePage(index) {
138
+ const gl = this.gl;
139
+ const page = {
140
+ index,
141
+ vertexArray: gl.createVertexArray(),
142
+ vertexBuffer: gl.createBuffer(),
143
+ indexBuffer: gl.createBuffer(),
144
+ vertices: new FreeRanges(),
145
+ indices: new FreeRanges(),
146
+ live: 0,
147
+ };
148
+ this.state.bindVertexArray(page.vertexArray);
149
+ gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
150
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, page.indexBuffer);
151
+ gl.enableVertexAttribArray(POSITION_AT);
152
+ gl.vertexAttribPointer(POSITION_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 0);
153
+ gl.enableVertexAttribArray(TEXEL_AT);
154
+ gl.vertexAttribPointer(TEXEL_AT, 2, gl.FLOAT, false, VERTEX_BYTES, 8);
155
+ gl.enableVertexAttribArray(COLOR_AT);
156
+ gl.vertexAttribPointer(COLOR_AT, 4, gl.UNSIGNED_BYTE, true, VERTEX_BYTES, 16);
157
+ // The same bytes as a mesh's depth, which the mesh program reads in place of the colour.
158
+ gl.enableVertexAttribArray(DEPTH_AT);
159
+ gl.vertexAttribPointer(DEPTH_AT, 1, gl.FLOAT, false, VERTEX_BYTES, 16);
160
+ return page;
161
+ }
162
+ /** Geometry `id`'s place, if it has one, let go of: a shape's ranges free in its page. */
163
+ letGo(id) {
164
+ const place = this.places[id];
165
+ if (!place) {
166
+ return;
167
+ }
168
+ this.places[id] = null;
169
+ if (place.glyphs) {
170
+ this.glyphs.live -= place.vertices;
171
+ this.glyphs.holes += place.vertices;
172
+ }
173
+ else {
174
+ const page = this.pages[place.page];
175
+ page.vertices.give(place.vertexStart, place.vertices);
176
+ page.indices.give(place.indexStart, place.indices);
177
+ page.live -= place.vertices;
178
+ }
179
+ }
152
180
  /**
153
181
  * The geometries removed since the last frame let go of, and those added
154
- * copied in: at the top while they fit, else into an atlas packed again,
155
- * grown to twice what is live.
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.
156
186
  */
157
187
  place(geometries) {
158
188
  this.frame++;
159
189
  for (const id of geometries.removed) {
160
- const place = this.places[id];
161
- if (place?.glyphs) {
162
- this.places[id] = null;
163
- this.glyphs.live -= place.vertices;
164
- this.glyphs.holes += place.vertices;
165
- }
166
- else if (place) {
167
- this.places[id] = null;
168
- this.live -= place.vertices;
169
- this.holes += place.vertices;
170
- }
190
+ this.letGo(id);
171
191
  }
172
192
  geometries.removed.length = 0;
173
193
  // Nothing left in it: filled again from its start, in the buffers it has.
174
- if (this.live === 0) {
175
- this.vertexTop = 0;
176
- this.indexTop = 0;
177
- this.holes = 0;
194
+ for (const page of this.pages) {
195
+ if (page.live === 0) {
196
+ page.vertices.reset(page.vertices.capacity);
197
+ page.indices.reset(page.indices.capacity);
198
+ }
178
199
  }
179
200
  // Changed in place: copied again where they lie, else as added.
180
201
  for (const id of geometries.updated) {
181
202
  const place = this.places[id];
182
203
  const data = geometries.get(id);
183
204
  if (place && data && !data.glyphs && place.vertices === data.positions.length / 2) {
184
- this.copyVertices(data, place.vertexStart);
205
+ this.copyVertices(this.pages[place.page], data, place.vertexStart);
185
206
  place.batch = this.batch(data);
186
207
  }
187
208
  else if (place && data) {
188
- this.places[id] = null;
189
- if (place.glyphs) {
190
- this.glyphs.live -= place.vertices;
191
- this.glyphs.holes += place.vertices;
192
- }
193
- else {
194
- this.live -= place.vertices;
195
- this.holes += place.vertices;
196
- }
209
+ this.letGo(id);
197
210
  geometries.added.push(id);
198
211
  }
199
212
  }
@@ -214,11 +227,11 @@ export class GeometryAtlas {
214
227
  }
215
228
  }
216
229
  // Each atlas packed again on its own, so that texts' quads made again often never repack
217
- // the shapes', nor shapes the texts'.
230
+ // the shapes', nor shapes the texts'. The shapes' page only when what is added outweighs its
231
+ // room, or below, when holes too small for one of them leave it none.
218
232
  const g = this.glyphs;
219
- const shapes = this.vertexTop + vertices > this.vertexCapacity ||
220
- this.indexTop + indices > this.indexCapacity ||
221
- (this.holes > SLACK && this.holes > this.live);
233
+ const page = this.pages[0];
234
+ let shapes = vertices > page.vertices.room() || indices > page.indices.room();
222
235
  const texts = g.top + glyphVertices > g.capacity ||
223
236
  g.indexTop + glyphIndices > g.indexCapacity ||
224
237
  (g.holes > GLYPHS_LEAST && g.holes > g.live) ||
@@ -229,17 +242,54 @@ export class GeometryAtlas {
229
242
  if (texts) {
230
243
  this.repackGlyphs(geometries);
231
244
  }
245
+ // The shapes added together in one range where one holds them all, at the top or in a
246
+ // hole, so that they go up in one upload each for vertices and indices; else each in the
247
+ // first that holds it, which a churning atlas would leave to a call or two a geometry.
248
+ let vertexAt = -1;
249
+ let indexAt = -1;
250
+ if (!shapes && vertices > 0) {
251
+ vertexAt = page.vertices.take(vertices);
252
+ indexAt = vertexAt < 0 ? -1 : page.indices.take(indices);
253
+ if (vertexAt >= 0 && indexAt < 0) {
254
+ page.vertices.give(vertexAt, vertices);
255
+ vertexAt = -1;
256
+ }
257
+ }
232
258
  for (const id of geometries.added) {
233
259
  const data = geometries.get(id);
234
- if (data && !(data.glyphs ? texts : shapes)) {
235
- this.copyIn(id, data);
260
+ if (!data) {
261
+ continue;
262
+ }
263
+ // Added twice in a frame, as an id let go of and taken again can be: its first copy's
264
+ // room given back, which only packing would otherwise find again. A packed atlas holds
265
+ // its geometries already.
266
+ if (data.glyphs) {
267
+ if (!texts) {
268
+ this.letGo(id);
269
+ this.copyGlyphs(id, data);
270
+ }
271
+ continue;
272
+ }
273
+ if (shapes) {
274
+ continue;
275
+ }
276
+ this.letGo(id);
277
+ if (vertexAt >= 0) {
278
+ this.copyAt(page, id, data, vertexAt, indexAt);
279
+ vertexAt += data.positions.length / 2;
280
+ indexAt += data.indices.length;
281
+ }
282
+ else if (!this.copyIn(page, id, data)) {
283
+ // Packing copies every live shape, those added after this one too.
284
+ shapes = true;
285
+ this.repack(geometries);
236
286
  }
237
287
  }
238
288
  geometries.added.length = 0;
239
289
  this.flush();
240
290
  this.tables.upload();
241
291
  }
242
- /** Every live shape's geometry copied into new buffers, with room for twice what is live. */
292
+ /** Every live shape copied into its page's buffers made anew, with room for twice them. */
243
293
  repack(geometries) {
244
294
  const gl = this.gl;
245
295
  const ids = geometries.live();
@@ -252,23 +302,22 @@ export class GeometryAtlas {
252
302
  indices += data.indices.length;
253
303
  }
254
304
  }
255
- this.vertexCapacity = Math.max(LEAST, vertices * 2);
256
- this.indexCapacity = Math.max(LEAST * 3, indices * 2);
257
- this.state.bindVertexArray(this.vertexArray);
258
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
259
- gl.bufferData(gl.ARRAY_BUFFER, this.vertexCapacity * VERTEX_BYTES, gl.STATIC_DRAW);
260
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.indexCapacity * 4, gl.STATIC_DRAW);
305
+ const page = this.pages[0];
306
+ page.vertices.reset(Math.max(LEAST, vertices * 2));
307
+ page.indices.reset(Math.max(LEAST * 3, indices * 2));
308
+ page.live = 0;
309
+ this.state.bindVertexArray(page.vertexArray);
310
+ gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
311
+ gl.bufferData(gl.ARRAY_BUFFER, page.vertices.capacity * VERTEX_BYTES, gl.STATIC_DRAW);
312
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, page.indices.capacity * 4, gl.STATIC_DRAW);
261
313
  // What was staged for the buffers replaced is copied again below.
262
314
  this.stagedVertices = 0;
263
315
  this.stagedIndexCount = 0;
264
- this.vertexTop = 0;
265
- this.indexTop = 0;
266
- this.live = 0;
267
- this.holes = 0;
316
+ // Each at the top, which holds twice them all.
268
317
  for (const id of ids) {
269
318
  const data = geometries.get(id);
270
319
  if (!data.glyphs) {
271
- this.copyIn(id, data);
320
+ this.copyIn(page, id, data);
272
321
  }
273
322
  }
274
323
  this.flush();
@@ -307,31 +356,46 @@ export class GeometryAtlas {
307
356
  }
308
357
  this.tables.upload();
309
358
  }
310
- /** `data` copied in at the top of the atlas, its indices made absolute. */
311
- copyIn(id, data) {
312
- if (data.glyphs) {
313
- this.copyGlyphs(id, data);
314
- return;
359
+ /**
360
+ * A shape's `data` copied into `page`, at its top or in a hole, its
361
+ * indices made absolute from where its vertices went; false where the
362
+ * page has no room for it.
363
+ */
364
+ copyIn(page, id, data) {
365
+ const count = data.positions.length / 2;
366
+ const base = page.vertices.take(count);
367
+ if (base < 0) {
368
+ return false;
369
+ }
370
+ const indexStart = page.indices.take(data.indices.length);
371
+ if (indexStart < 0) {
372
+ page.vertices.give(base, count);
373
+ return false;
315
374
  }
375
+ this.copyAt(page, id, data, base, indexStart);
376
+ return true;
377
+ }
378
+ /** A shape's `data` copied into `page`'s room taken for it, from vertex `base` and index `indexStart`. */
379
+ copyAt(page, id, data, base, indexStart) {
316
380
  const count = data.positions.length / 2;
317
- const base = this.vertexTop;
318
- this.copyVertices(data, base);
319
381
  const source = data.indices;
320
382
  const length = source.length;
383
+ this.copyVertices(page, data, base);
321
384
  if (length > STAGED_INDICES) {
322
385
  const indices = new Uint32Array(length);
323
386
  for (let i = 0; i < length; i++) {
324
387
  indices[i] = source[i] + base;
325
388
  }
326
389
  this.flushIndices();
327
- this.state.bindVertexArray(this.vertexArray);
328
- this.gl.bufferSubData(this.gl.ELEMENT_ARRAY_BUFFER, this.indexTop * 4, indices);
390
+ this.state.bindVertexArray(page.vertexArray);
391
+ this.gl.bufferSubData(this.gl.ELEMENT_ARRAY_BUFFER, indexStart * 4, indices);
329
392
  }
330
393
  else {
394
+ this.stagePage(page);
331
395
  if (this.stagedIndexCount + length > STAGED_INDICES ||
332
- this.stagedIndexAt + this.stagedIndexCount !== this.indexTop) {
396
+ this.stagedIndexAt + this.stagedIndexCount !== indexStart) {
333
397
  this.flushIndices();
334
- this.stagedIndexAt = this.indexTop;
398
+ this.stagedIndexAt = indexStart;
335
399
  }
336
400
  const staged = this.stagedIndices;
337
401
  const at = this.stagedIndexCount;
@@ -341,29 +405,29 @@ export class GeometryAtlas {
341
405
  this.stagedIndexCount = at + length;
342
406
  }
343
407
  this.places[id] = {
408
+ page: page.index,
344
409
  vertexStart: base,
345
410
  vertices: count,
346
- indexStart: this.indexTop,
411
+ indexStart,
347
412
  indices: length,
348
413
  glyphs: null,
349
414
  flat: data.flat === true,
350
415
  batch: this.batch(data),
351
416
  };
352
- this.vertexTop += count;
353
- this.indexTop += length;
354
- this.live += count;
417
+ page.live += count;
355
418
  }
356
- /** `data`'s vertices interleaved for the atlas from vertex `base`: staged, or uploaded if too many. */
357
- copyVertices(data, base) {
419
+ /** `data`'s vertices interleaved for `page` from vertex `base`: staged, or uploaded if too many. */
420
+ copyVertices(page, data, base) {
358
421
  const count = data.positions.length / 2;
359
422
  if (count > STAGED_VERTICES) {
360
423
  const floats = new Float32Array(count * (VERTEX_BYTES / 4));
361
424
  interleave(data, count, floats, new Uint32Array(floats.buffer), 0);
362
425
  this.flushVertices();
363
- this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer);
426
+ this.gl.bindBuffer(this.gl.ARRAY_BUFFER, page.vertexBuffer);
364
427
  this.gl.bufferSubData(this.gl.ARRAY_BUFFER, base * VERTEX_BYTES, floats);
365
428
  return;
366
429
  }
430
+ this.stagePage(page);
367
431
  if (this.stagedVertices + count > STAGED_VERTICES ||
368
432
  this.stagedVertexAt + this.stagedVertices !== base) {
369
433
  this.flushVertices();
@@ -377,10 +441,17 @@ export class GeometryAtlas {
377
441
  this.flushVertices();
378
442
  this.flushIndices();
379
443
  }
444
+ /** The staging made `page`'s, what it held for another uploaded first. */
445
+ stagePage(page) {
446
+ if (this.stagedPage !== page) {
447
+ this.flush();
448
+ this.stagedPage = page;
449
+ }
450
+ }
380
451
  flushVertices() {
381
452
  if (this.stagedVertices > 0) {
382
453
  const gl = this.gl;
383
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
454
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.stagedPage.vertexBuffer);
384
455
  gl.bufferSubData(gl.ARRAY_BUFFER, this.stagedVertexAt * VERTEX_BYTES, this.stagedFloats, 0, this.stagedVertices * 5);
385
456
  this.stagedVertices = 0;
386
457
  }
@@ -388,7 +459,7 @@ export class GeometryAtlas {
388
459
  flushIndices() {
389
460
  if (this.stagedIndexCount > 0) {
390
461
  const gl = this.gl;
391
- this.state.bindVertexArray(this.vertexArray);
462
+ this.state.bindVertexArray(this.stagedPage.vertexArray);
392
463
  gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, this.stagedIndexAt * 4, this.stagedIndices, 0, this.stagedIndexCount);
393
464
  this.stagedIndexCount = 0;
394
465
  }
@@ -413,6 +484,7 @@ export class GeometryAtlas {
413
484
  gl.bufferSubData(gl.ARRAY_BUFFER, g.top * GLYPH_BYTES, glyphs.vertices);
414
485
  gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, g.indexTop * 4, indices);
415
486
  this.places[id] = {
487
+ page: 0,
416
488
  vertexStart: g.top,
417
489
  vertices: count,
418
490
  indexStart: g.indexTop,
@@ -432,9 +504,11 @@ export class GeometryAtlas {
432
504
  /** The buffers and vertex arrays made on the context, deleted. */
433
505
  destroy() {
434
506
  const gl = this.gl;
435
- gl.deleteVertexArray(this.vertexArray);
436
- gl.deleteBuffer(this.vertexBuffer);
437
- gl.deleteBuffer(this.indexBuffer);
507
+ for (const page of this.pages) {
508
+ gl.deleteVertexArray(page.vertexArray);
509
+ gl.deleteBuffer(page.vertexBuffer);
510
+ gl.deleteBuffer(page.indexBuffer);
511
+ }
438
512
  gl.deleteVertexArray(this.glyphArray);
439
513
  gl.deleteBuffer(this.glyphBuffer);
440
514
  gl.deleteBuffer(this.glyphIndexBuffer);