@swf2es/render 0.1.28 → 0.1.30

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 (46) hide show
  1. package/dist/geometry.d.ts +17 -1
  2. package/dist/geometry.d.ts.map +1 -1
  3. package/dist/geometry.js +23 -0
  4. package/dist/geometry.js.map +1 -1
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/webgl/atlas.d.ts +2 -0
  10. package/dist/webgl/atlas.d.ts.map +1 -1
  11. package/dist/webgl/atlas.js +4 -1
  12. package/dist/webgl/atlas.js.map +1 -1
  13. package/dist/webgl/backend.d.ts +53 -5
  14. package/dist/webgl/backend.d.ts.map +1 -1
  15. package/dist/webgl/backend.js +166 -35
  16. package/dist/webgl/backend.js.map +1 -1
  17. package/dist/webgl/draws.d.ts +26 -3
  18. package/dist/webgl/draws.d.ts.map +1 -1
  19. package/dist/webgl/draws.js +86 -8
  20. package/dist/webgl/draws.js.map +1 -1
  21. package/dist/webgl/kernels.d.ts +6 -1
  22. package/dist/webgl/kernels.d.ts.map +1 -1
  23. package/dist/webgl/kernels.js +11 -1
  24. package/dist/webgl/kernels.js.map +1 -1
  25. package/dist/webgl/losses.d.ts +44 -0
  26. package/dist/webgl/losses.d.ts.map +1 -0
  27. package/dist/webgl/losses.js +80 -0
  28. package/dist/webgl/losses.js.map +1 -0
  29. package/dist/webgl/precompile.d.ts +1 -1
  30. package/dist/webgl/precompile.d.ts.map +1 -1
  31. package/dist/webgl/precompile.js +5 -3
  32. package/dist/webgl/precompile.js.map +1 -1
  33. package/dist/webgl/programs.d.ts +14 -7
  34. package/dist/webgl/programs.d.ts.map +1 -1
  35. package/dist/webgl/programs.js +49 -8
  36. package/dist/webgl/programs.js.map +1 -1
  37. package/package.json +2 -2
  38. package/src/geometry.ts +32 -1
  39. package/src/index.ts +1 -0
  40. package/src/webgl/atlas.ts +6 -1
  41. package/src/webgl/backend.ts +236 -60
  42. package/src/webgl/draws.ts +101 -7
  43. package/src/webgl/kernels.ts +10 -0
  44. package/src/webgl/losses.ts +92 -0
  45. package/src/webgl/precompile.ts +5 -3
  46. package/src/webgl/programs.ts +56 -8
@@ -38,6 +38,7 @@ import { FilterRunner } from "./filters.js";
38
38
  import { GpuTextures } from "./gpu-textures.js";
39
39
  import { HeldLayers } from "./holds.js";
40
40
  import { KernelPrograms } from "./kernels.js";
41
+ import { LinkLosses } from "./losses.js";
41
42
  import { ProgramQueue } from "./precompile.js";
42
43
  import { LAYER_UNIT, TEXTURE_UNIT } from "./programs.js";
43
44
  import { Stage } from "./stage.js";
@@ -147,11 +148,20 @@ export class WebGLBackend {
147
148
  /** Framebuffers onto registry textures, drawn into or read, by texture id, with the texture they were made for. */
148
149
  private textureFramebuffers: ({ texture: WebGLTexture; framebuffer: WebGLFramebuffer } | null)[] =
149
150
  [];
151
+ /** Which failed links are a lost context's (gaveUp, and the kernels'). */
152
+ private readonly losses: LinkLosses;
153
+ /** Whether what the context holds is made (setUp): not after it was lost, till it is back. */
154
+ private made = false;
155
+ /** Whether frames draw: made, and no work given up on since (gaveUp). */
156
+ private ready = false;
157
+ /** The failure work was last given up on for, the context not flagged lost (resume). */
158
+ private fault: unknown = null;
150
159
 
151
160
  constructor(gl: WebGL2RenderingContext) {
152
161
  this.gl = gl;
153
162
  this.state = new GlState(gl);
154
- this.kernels = new KernelPrograms(gl, this.state);
163
+ this.losses = new LinkLosses(gl);
164
+ this.kernels = new KernelPrograms(gl, this.state, this.losses);
155
165
  this.pool = new TargetPool(gl, this.state);
156
166
  this.stage = new Stage(gl, this.state, this.pool);
157
167
  this.gpu = new GpuTextures(gl, this.state);
@@ -180,21 +190,105 @@ export class WebGLBackend {
180
190
  this.kernels,
181
191
  );
182
192
  this.holds = new HeldLayers(gl, this.state, this.pool, this.draws, this.compositor, this.gpu);
183
- this.setUp();
193
+ // A context lost already gives up here, and is made again once it is restored.
194
+ try {
195
+ this.setUp();
196
+ this.made = this.ready = true;
197
+ } catch (e) {
198
+ this.gaveUp(e);
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Whether frames wait (resume): the context was lost and is not back
204
+ * yet, or work was given up on as lost before the browser said so.
205
+ */
206
+ get stale(): boolean {
207
+ return !this.ready;
208
+ }
209
+
210
+ /**
211
+ * Whether a stale backend draws again, the context not flagged lost: not
212
+ * while a failure that said nothing may yet be a loss Chrome has not
213
+ * flagged (LinkLosses); after that, it was the shaders' fault, and frames
214
+ * draw on as before the failure, linking again what failed, which then
215
+ * throws. Nothing is made again on a live context, whose objects are
216
+ * all there, a BitmapData's newest pixels among them. Throws that
217
+ * failure where the setUp it came from never finished.
218
+ */
219
+ resume(): boolean {
220
+ if (this.ready) {
221
+ return true;
222
+ }
223
+
224
+ if (this.gl.isContextLost() || !this.losses.expired) {
225
+ return false;
226
+ }
227
+
228
+ if (!this.made) {
229
+ throw this.fault;
230
+ }
231
+
232
+ this.losses.concede();
233
+ this.ready = true;
234
+ return true;
235
+ }
236
+
237
+ /** Whether frames draw: the context there, and what it holds made. */
238
+ private get usable(): boolean {
239
+ return this.ready && !this.gl.isContextLost();
240
+ }
241
+
242
+ /**
243
+ * Whether what the context holds may be drawn into and read: made, and
244
+ * not lost. Not stopped by a frame given up on as lost (stale): a
245
+ * BitmapData's texture, whose pixels may be newer than its store's, is
246
+ * read, filled and drawn into meanwhile as ever, unless that needs a
247
+ * program that fails.
248
+ */
249
+ private get live(): boolean {
250
+ return this.made && !this.gl.isContextLost();
184
251
  }
185
252
 
186
253
  /**
187
254
  * The context came back after it was lost: everything made again, every
188
255
  * geometry copied in again from the registry's arrays. The textures were
189
- * lost with it, which their owners add again (Textures.lost).
256
+ * lost with it, which their owners add again (Textures.lost). Lost again
257
+ * meanwhile, it stays stale, for the next restore.
190
258
  */
191
259
  restore(geometries: Geometries): void {
192
- this.setUp();
260
+ this.losses.clear();
261
+ this.made = this.ready = false;
193
262
  geometries.added.length = 0;
194
263
  geometries.removed.length = 0;
195
264
  geometries.updated.length = 0;
196
- this.atlas.repack(geometries);
197
- this.atlas.repackGlyphs(geometries);
265
+ if (this.gl.isContextLost()) {
266
+ return;
267
+ }
268
+
269
+ try {
270
+ this.setUp();
271
+ this.atlas.repack(geometries);
272
+ this.atlas.repackGlyphs(geometries);
273
+ this.made = this.ready = true;
274
+ } catch (e) {
275
+ this.gaveUp(e);
276
+ }
277
+ }
278
+
279
+ /**
280
+ * Work that threw `e` given up on, where a program would not be made as
281
+ * the context was lost (LinkLosses): frames wait (stale) till it is
282
+ * restored, or till the failure turns out the shaders' (resume), with
283
+ * nothing thrown. Anything else is thrown on.
284
+ */
285
+ private gaveUp(e: unknown): void {
286
+ if (!this.losses.lost(e)) {
287
+ throw e;
288
+ }
289
+
290
+ this.ready = false;
291
+ this.fault = e;
198
292
  }
199
293
 
200
294
  /** Programs, buffers and textures made, as the context comes or comes back. */
@@ -215,8 +309,6 @@ export class WebGLBackend {
215
309
  // Kernels' programs made again as first used, the context's own gone with it.
216
310
  this.kernels.clear();
217
311
  const textureMost = gl.getParameter(gl.MAX_TEXTURE_SIZE) as number;
218
- this.draws.setUp(textureMost);
219
- this.compositor.setUp();
220
312
  // A lost context's programs went with it: all queued again.
221
313
  this.programs = new ProgramQueue(gl, this.state);
222
314
  this.overProgram = null;
@@ -243,12 +335,15 @@ export class WebGLBackend {
243
335
  this.gpu.setUp();
244
336
  // The context's own, gone with it.
245
337
  this.textureFramebuffers = [];
338
+ // Linked last: a context lost meanwhile throws here (gaveUp), the rest made, for destroy.
339
+ this.draws.setUp(textureMost);
340
+ this.compositor.setUp();
246
341
  }
247
342
 
248
343
  /**
249
344
  * Draw a frame: what the geometries added and removed first, then the
250
- * rows, then the commands; onto the canvas, or into `root`, a layer the
251
- * target's size, as BitmapData.draw's frames go.
345
+ * rows, then the commands, onto the canvas. Nothing while the context is
346
+ * lost; a frame the context is lost in midway is given up on (gaveUp).
252
347
  */
253
348
  execute(
254
349
  commands: Commands,
@@ -259,16 +354,47 @@ export class WebGLBackend {
259
354
  filters: Filters,
260
355
  keeps: Keeps,
261
356
  target: Target,
262
- root: Layer | null = null,
357
+ ): void {
358
+ if (!this.usable) {
359
+ return;
360
+ }
361
+
362
+ try {
363
+ this.run(commands, scene, rows, geometries, textures, filters, keeps, target, null);
364
+ this.losses.endFrame();
365
+ } catch (e) {
366
+ this.gaveUp(e);
367
+ }
368
+ }
369
+
370
+ /**
371
+ * A frame drawn as `execute` draws it, onto the canvas or into `root`, a
372
+ * layer the target's size, as BitmapData.draw's frames go.
373
+ */
374
+ private run(
375
+ commands: Commands,
376
+ scene: SceneList,
377
+ rows: Rows,
378
+ geometries: Geometries,
379
+ textures: Textures,
380
+ filters: Filters,
381
+ keeps: Keeps,
382
+ target: Target,
383
+ root: Layer | null,
263
384
  ): void {
264
385
  // A frame drawn into a layer, for BitmapData, is no frame of the canvas': idle layers are
265
386
  // counted in the canvas' frames.
266
387
  const counted = root === null;
267
- const gl = this.gl;
268
- if (gl.isContextLost()) {
269
- return;
388
+ // Once the first frame is drawn, the programs not drawn with yet are made, a frame's share,
389
+ // before this frame draws anything: Chrome clears a canvas it composited at the first draw
390
+ // call after, into whatever framebuffer, and the stage's blit onto the canvas is none. A
391
+ // warming draw after a frame that blitted and drew nothing else, as of a stage holding
392
+ // nothing, wiped that frame, and the canvas showed black.
393
+ if (counted) {
394
+ this.programs.step();
270
395
  }
271
396
 
397
+ const gl = this.gl;
272
398
  this.frameFilters = filters;
273
399
  this.atlas.place(geometries);
274
400
  this.gpu.uploadTextures(textures, this.clearTexture);
@@ -493,8 +619,6 @@ export class WebGLBackend {
493
619
  // The free layers, and the stage's (stageAlpha), undrawn into for LAYER_IDLE_FRAMES let go of.
494
620
  this.stage.dropIdleLayer();
495
621
  this.pool.endFrame();
496
- // Once the first frame is drawn, the programs not drawn with yet are made, a frame's share.
497
- this.programs.step();
498
622
  }
499
623
  }
500
624
 
@@ -519,26 +643,31 @@ export class WebGLBackend {
519
643
  ): Uint8Array {
520
644
  const gl = this.gl;
521
645
  const bytes = new Uint8Array(width * height * 4);
522
- if (gl.isContextLost()) {
646
+ if (!this.live) {
523
647
  return bytes;
524
648
  }
525
649
 
526
- const layer = this.drawSampled(
527
- commands,
528
- scene,
529
- rows,
530
- geometries,
531
- textures,
532
- filters,
533
- keeps,
534
- target,
535
- width,
536
- height,
537
- samples,
538
- );
539
- this.state.bindFramebuffer(layer.framebuffer);
540
- gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, bytes);
541
- this.pool.giveLayer(layer);
650
+ try {
651
+ const layer = this.drawSampled(
652
+ commands,
653
+ scene,
654
+ rows,
655
+ geometries,
656
+ textures,
657
+ filters,
658
+ keeps,
659
+ target,
660
+ width,
661
+ height,
662
+ samples,
663
+ );
664
+ this.state.bindFramebuffer(layer.framebuffer);
665
+ gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, bytes);
666
+ this.pool.giveLayer(layer);
667
+ } catch (e) {
668
+ this.gaveUp(e);
669
+ }
670
+
542
671
  return bytes;
543
672
  }
544
673
 
@@ -568,31 +697,35 @@ export class WebGLBackend {
568
697
  y: number,
569
698
  opaque: boolean,
570
699
  ): boolean {
571
- const gl = this.gl;
572
- if (gl.isContextLost()) {
700
+ if (!this.live) {
573
701
  return false;
574
702
  }
575
703
 
576
- const layer = this.drawSampled(
577
- commands,
578
- scene,
579
- rows,
580
- geometries,
581
- textures,
582
- filters,
583
- keeps,
584
- target,
585
- width,
586
- height,
587
- samples,
588
- );
589
- const into = this.textureFramebuffer(id);
590
- if (into) {
591
- this.compositeOver(layer, into, x, y, width, height, opaque);
592
- }
704
+ try {
705
+ const layer = this.drawSampled(
706
+ commands,
707
+ scene,
708
+ rows,
709
+ geometries,
710
+ textures,
711
+ filters,
712
+ keeps,
713
+ target,
714
+ width,
715
+ height,
716
+ samples,
717
+ );
718
+ const into = this.textureFramebuffer(id);
719
+ if (into) {
720
+ this.compositeOver(layer, into, x, y, width, height, opaque);
721
+ }
593
722
 
594
- this.pool.giveLayer(layer);
595
- return into !== null;
723
+ this.pool.giveLayer(layer);
724
+ return into !== null;
725
+ } catch (e) {
726
+ this.gaveUp(e);
727
+ return false;
728
+ }
596
729
  }
597
730
 
598
731
  /**
@@ -665,7 +798,7 @@ export class WebGLBackend {
665
798
  * row by row; null on a lost context or without the texture.
666
799
  */
667
800
  readTexture(id: number, width: number, height: number): Uint8Array | null {
668
- const framebuffer = this.gl.isContextLost() ? null : this.textureFramebuffer(id);
801
+ const framebuffer = this.live ? this.textureFramebuffer(id) : null;
669
802
  if (!framebuffer) {
670
803
  return null;
671
804
  }
@@ -690,8 +823,12 @@ export class WebGLBackend {
690
823
  height: number,
691
824
  pixel: number,
692
825
  ): boolean {
826
+ if (!this.live) {
827
+ return false;
828
+ }
829
+
693
830
  this.gpu.uploadTextures(textures, this.clearTexture);
694
- const framebuffer = this.gl.isContextLost() ? null : this.textureFramebuffer(id);
831
+ const framebuffer = this.textureFramebuffer(id);
695
832
  if (!framebuffer) {
696
833
  return false;
697
834
  }
@@ -750,8 +887,12 @@ export class WebGLBackend {
750
887
  dy: number,
751
888
  ): boolean {
752
889
  const gl = this.gl;
890
+ if (!this.live) {
891
+ return false;
892
+ }
893
+
753
894
  this.gpu.uploadTextures(textures, this.clearTexture);
754
- const source = gl.isContextLost() ? null : this.textureFramebuffer(from);
895
+ const source = this.textureFramebuffer(from);
755
896
  const target = source ? this.textureFramebuffer(to) : null;
756
897
  if (!source || !target) {
757
898
  return false;
@@ -849,11 +990,46 @@ export class WebGLBackend {
849
990
  height: number,
850
991
  samples: number,
851
992
  ): void {
852
- const gl = this.gl;
853
- if (gl.isContextLost()) {
993
+ if (!this.usable) {
854
994
  return;
855
995
  }
856
996
 
997
+ try {
998
+ this.planeInto(
999
+ keep,
1000
+ commands,
1001
+ scene,
1002
+ rows,
1003
+ geometries,
1004
+ textures,
1005
+ filters,
1006
+ keeps,
1007
+ target,
1008
+ width,
1009
+ height,
1010
+ samples,
1011
+ );
1012
+ } catch (e) {
1013
+ this.gaveUp(e);
1014
+ }
1015
+ }
1016
+
1017
+ /** drawPlane's drawing, what throws thrown on. */
1018
+ private planeInto(
1019
+ keep: number,
1020
+ commands: Commands,
1021
+ scene: SceneList,
1022
+ rows: Rows,
1023
+ geometries: Geometries,
1024
+ textures: Textures,
1025
+ filters: Filters,
1026
+ keeps: Keeps,
1027
+ target: Target,
1028
+ width: number,
1029
+ height: number,
1030
+ samples: number,
1031
+ ): void {
1032
+ const gl = this.gl;
857
1033
  const layer = this.drawSampled(
858
1034
  commands,
859
1035
  scene,
@@ -906,7 +1082,7 @@ export class WebGLBackend {
906
1082
  samples: number,
907
1083
  ): Layer {
908
1084
  let layer = this.pool.takeLayer(width * samples, height * samples);
909
- this.execute(commands, scene, rows, geometries, textures, filters, keeps, target, layer);
1085
+ this.run(commands, scene, rows, geometries, textures, filters, keeps, target, layer);
910
1086
  for (let k = samples; k > 1; k /= 2) {
911
1087
  const half = this.filters.halve(layer, (width * k) / 2, (height * k) / 2);
912
1088
  this.pool.giveLayer(layer);
@@ -37,6 +37,7 @@ import {
37
37
  RUN_MOST,
38
38
  RUN_UNIT,
39
39
  SNAP_BIT,
40
+ stageFillFragment,
40
41
  TABLE_UNIT,
41
42
  TEXTURE_UNIT,
42
43
  } from "./programs.js";
@@ -100,6 +101,17 @@ export class Draws {
100
101
  private rowLocation: WebGLUniformLocation | null = null;
101
102
  private viewLocation: WebGLUniformLocation | null = null;
102
103
  private fillUnitLocation: WebGLUniformLocation | null = null;
104
+ /**
105
+ * The stage fill program (stageFillFragment), for fills that sample the
106
+ * stage's pixel (Place.stage), each drawn alone: linked as one is first
107
+ * drawn, null till then and after the context is made again; and where it
108
+ * takes its row, view, Flash's pixels a stage pixel and the stage.
109
+ */
110
+ private stageProgram: WebGLProgram | null = null;
111
+ private stageRowLocation: WebGLUniformLocation | null = null;
112
+ private stageViewLocation: WebGLUniformLocation | null = null;
113
+ private stageUnitLocation: WebGLUniformLocation | null = null;
114
+ private stageLocation: WebGLUniformLocation | null = null;
103
115
  /**
104
116
  * The fill program that blends a fill straight onto what is below it
105
117
  * (drawBlended), a draw a call, and where it takes what the fill program
@@ -161,6 +173,10 @@ export class Draws {
161
173
  */
162
174
  private readonly view = new Float32Array(4);
163
175
  private readonly frag = new Float32Array(4);
176
+ /** A fragment to the target's pixels from the stage's origin, as frag, then a stage pixel of them. */
177
+ private readonly stage = new Float32Array(4);
178
+ /** Whether the stage fill program is to be told the view, unit and stage as it next draws. */
179
+ private stageStale = true;
164
180
  private meshStale = true;
165
181
  /** The row and mode (REPEAT, SMOOTH) the mesh program was told last, -1 for none. */
166
182
  private meshRow = -1;
@@ -227,7 +243,8 @@ export class Draws {
227
243
  try {
228
244
  this.program = endProgram(gl, fill);
229
245
  } catch (e) {
230
- if (!this.multi) {
246
+ // Not where the context is flagged lost, whose every link fails.
247
+ if (!this.multi || gl.isContextLost()) {
231
248
  throw e;
232
249
  }
233
250
 
@@ -239,6 +256,8 @@ export class Draws {
239
256
  this.unitIds.fill(-1);
240
257
  this.rowLocation = gl.getUniformLocation(this.program, "uRow");
241
258
  this.viewLocation = gl.getUniformLocation(this.program, "uView");
259
+ // The context's own, gone with it, or not made yet.
260
+ this.stageProgram = null;
242
261
  this.state.useProgram(this.program);
243
262
  gl.uniform1i(gl.getUniformLocation(this.program, "uRows"), 0);
244
263
  gl.uniform1iv(
@@ -342,7 +361,11 @@ export class Draws {
342
361
  }
343
362
 
344
363
  const place = this.atlas.places[scene.geometry[i]];
345
- if (!place || place.glyphs || (!stencil && scene.sampler[i] & (MESH | PLANE | SHADED))) {
364
+ if (
365
+ !place ||
366
+ place.glyphs ||
367
+ (!stencil && (place.stage || scene.sampler[i] & (MESH | PLANE | SHADED)))
368
+ ) {
346
369
  continue;
347
370
  }
348
371
 
@@ -477,8 +500,14 @@ export class Draws {
477
500
  frag[1] = (open.layer ? open.y : open.y + open.height) - target.y - oy;
478
501
  frag[2] = down;
479
502
  frag[3] = target.pixel;
503
+ const stage = this.stage;
504
+ stage[0] = frag[0];
505
+ stage[1] = frag[1];
506
+ stage[2] = down;
507
+ stage[3] = 1 / target.scale;
480
508
  this.unit = target.scale / target.pixel;
481
509
  gl.uniform1f(this.fillUnitLocation, this.unit);
510
+ this.stageStale = true;
482
511
  this.meshStale = true;
483
512
  this.glyphStale = true;
484
513
  }
@@ -508,7 +537,8 @@ export class Draws {
508
537
  * one fill of a shape, of no mesh, shader fill or text, whose texture, if
509
538
  * it samples one, is there. Drawn straight onto what is below in a blend
510
539
  * mode, it is as its layer would be composited, as no part of it is over
511
- * another.
540
+ * another. Not one that samples the stage's pixel, which the blend
541
+ * program does not: its layer takes it.
512
542
  */
513
543
  blendable(scene: SceneList, i: number): boolean {
514
544
  if (scene.kind[i] !== DRAW || scene.sampler[i] & (MESH | PLANE | SHADED)) {
@@ -517,7 +547,9 @@ export class Draws {
517
547
 
518
548
  const place = this.atlas.places[scene.geometry[i]];
519
549
  const id = scene.texture[i];
520
- return !!place && place.flat && !place.glyphs && (id < 0 || !!this.gpu.textures[id]);
550
+ return (
551
+ !!place && place.flat && !place.glyphs && !place.stage && (id < 0 || !!this.gpu.textures[id])
552
+ );
521
553
  }
522
554
 
523
555
  /**
@@ -645,6 +677,12 @@ export class Draws {
645
677
  continue;
646
678
  }
647
679
 
680
+ // In a mask, its coverage as any fill's.
681
+ if (!stencil && place.stage) {
682
+ other = this.drawStage(scene, i) || other;
683
+ continue;
684
+ }
685
+
648
686
  if (other) {
649
687
  this.useFill();
650
688
  other = false;
@@ -750,8 +788,9 @@ export class Draws {
750
788
  * geometries too large to merge (Place.batch), of geometries added this
751
789
  * frame, or of stable ones alone, so that a stable run's merged copy is
752
790
  * drawn again while a part of it changes from frame to frame; and without
753
- * merging each fill is a run. The last entry gathered, past those whose
754
- * texture is gone, which draw nothing.
791
+ * merging each fill is a run. A fill that samples the stage's pixel
792
+ * (Place.stage) ends a run, drawn alone (drawStage). The last entry
793
+ * gathered, past those whose texture is gone, which draw nothing.
755
794
  */
756
795
  private gatherRun(scene: SceneList, i: number, end: number, stencil: boolean): number {
757
796
  const places = this.atlas.places;
@@ -776,7 +815,10 @@ export class Draws {
776
815
  continue;
777
816
  }
778
817
 
779
- if (place.glyphs || (!stencil && scene.sampler[j] & (MESH | PLANE | SHADED))) {
818
+ if (
819
+ place.glyphs ||
820
+ (!stencil && ((place.stage && j > i) || scene.sampler[j] & (MESH | PLANE | SHADED)))
821
+ ) {
780
822
  break;
781
823
  }
782
824
 
@@ -855,6 +897,57 @@ export class Draws {
855
897
  return free + 1;
856
898
  }
857
899
 
900
+ /**
901
+ * Entry `i`, a fill that samples the stage's pixel (Place.stage), drawn
902
+ * alone by the stage fill program, linked as it is first needed and told
903
+ * the view as it changed; whether it was drawn, that program left in use.
904
+ */
905
+ private drawStage(scene: SceneList, i: number): boolean {
906
+ this.gatherRun(scene, i, i + 1, false);
907
+ if (this.runCount === 0) {
908
+ return false;
909
+ }
910
+
911
+ const gl = this.gl;
912
+ const state = this.state;
913
+ if (!this.stageProgram) {
914
+ // A link that fails is given up on as the fill program's would be (WebGLBackend.gaveUp).
915
+ const program = linkProgram(gl, fillVertex(false, false, true), stageFillFragment);
916
+ this.stageProgram = program;
917
+ this.stageRowLocation = gl.getUniformLocation(program, "uRow");
918
+ this.stageViewLocation = gl.getUniformLocation(program, "uView");
919
+ this.stageUnitLocation = gl.getUniformLocation(program, "uUnit");
920
+ this.stageLocation = gl.getUniformLocation(program, "uStage");
921
+ state.useProgram(program);
922
+ gl.uniform1i(gl.getUniformLocation(program, "uRows"), 0);
923
+ gl.uniform1iv(
924
+ gl.getUniformLocation(program, "uTextures"),
925
+ Array.from({ length: FILL_TEXTURES }, (_, k) => FILL_UNIT + k),
926
+ );
927
+ this.stageStale = true;
928
+ }
929
+
930
+ state.useProgram(this.stageProgram);
931
+ if (this.stageStale) {
932
+ gl.uniform4fv(this.stageViewLocation, this.view);
933
+ gl.uniform1f(this.stageUnitLocation, this.unit);
934
+ gl.uniform4fv(this.stageLocation, this.stage);
935
+ this.stageStale = false;
936
+ }
937
+
938
+ state.bindVertexArray(this.atlas.pages[this.runPlaces[0].page].vertexArray);
939
+ for (let k = 0; k < FILL_TEXTURES; k++) {
940
+ if (this.runUnits & (1 << k)) {
941
+ state.bindTexture(FILL_UNIT + k, this.gpu.textures[this.unitIds[k]] as WebGLTexture);
942
+ state.bindSampler(FILL_UNIT + k, this.gpu.sampler(this.unitSampling[k]));
943
+ }
944
+ }
945
+
946
+ gl.uniform1i(this.stageRowLocation, this.runRows[0]);
947
+ gl.drawElements(gl.TRIANGLES, this.runCounts[0], gl.UNSIGNED_INT, this.runOffsets[0]);
948
+ return true;
949
+ }
950
+
858
951
  /**
859
952
  * Entry `i`, advanced text's glyph quads (GeometryData.glyphs), drawn by the glyph
860
953
  * program from its atlas, which is told the view as it changed, and its
@@ -1017,6 +1110,7 @@ export class Draws {
1017
1110
 
1018
1111
  const gl = this.gl;
1019
1112
  gl.deleteProgram(this.program);
1113
+ gl.deleteProgram(this.stageProgram);
1020
1114
  gl.deleteProgram(this.mesh);
1021
1115
  gl.deleteProgram(this.glyphProgram);
1022
1116
  }
@@ -2,6 +2,7 @@ import { type GlState, linkProgram, ShaderError } from "@swf2es/context3d/device
2
2
  import type { Filters } from "../filters.js";
3
3
  import type { KernelImage } from "../kernel-shaders.js";
4
4
  import { CLAMP } from "../scene.js";
5
+ import type { LinkLosses } from "./losses.js";
5
6
 
6
7
  /**
7
8
  * A Pixel Bender kernel's program: where it takes its uniforms, asked
@@ -47,6 +48,8 @@ export class KernelPrograms {
47
48
  constructor(
48
49
  private readonly gl: WebGL2RenderingContext,
49
50
  private readonly state: GlState,
51
+ /** Which failed links are a lost context's, which are not kept as failed. */
52
+ private readonly losses: LinkLosses,
50
53
  ) {}
51
54
 
52
55
  /**
@@ -102,6 +105,13 @@ export class KernelPrograms {
102
105
  if (!(e instanceof ShaderError)) {
103
106
  throw e;
104
107
  }
108
+
109
+ // A kernel that would not link is drawn without. One lost with the context is drawn
110
+ // without too, into nothing or for a frame or so, but not kept as failed: it is linked
111
+ // again as it is next used, or once the context is back.
112
+ if (this.losses.lost(e)) {
113
+ return null;
114
+ }
105
115
  }
106
116
  }
107
117