@swf2es/render 0.1.42 → 0.1.43

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 (59) hide show
  1. package/dist/commands.d.ts +26 -5
  2. package/dist/commands.d.ts.map +1 -1
  3. package/dist/commands.js +42 -5
  4. package/dist/commands.js.map +1 -1
  5. package/dist/index.d.ts +2 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +2 -2
  8. package/dist/index.js.map +1 -1
  9. package/dist/measure.d.ts +7 -2
  10. package/dist/measure.d.ts.map +1 -1
  11. package/dist/measure.js +189 -5
  12. package/dist/measure.js.map +1 -1
  13. package/dist/plan.d.ts.map +1 -1
  14. package/dist/plan.js +8 -4
  15. package/dist/plan.js.map +1 -1
  16. package/dist/scene.d.ts +35 -4
  17. package/dist/scene.d.ts.map +1 -1
  18. package/dist/scene.js +41 -4
  19. package/dist/scene.js.map +1 -1
  20. package/dist/webgl/backdrops.d.ts +60 -0
  21. package/dist/webgl/backdrops.d.ts.map +1 -0
  22. package/dist/webgl/backdrops.js +130 -0
  23. package/dist/webgl/backdrops.js.map +1 -0
  24. package/dist/webgl/backend.d.ts +19 -1
  25. package/dist/webgl/backend.d.ts.map +1 -1
  26. package/dist/webgl/backend.js +88 -29
  27. package/dist/webgl/backend.js.map +1 -1
  28. package/dist/webgl/composite.d.ts +29 -2
  29. package/dist/webgl/composite.d.ts.map +1 -1
  30. package/dist/webgl/composite.js +96 -27
  31. package/dist/webgl/composite.js.map +1 -1
  32. package/dist/webgl/draws.d.ts +5 -3
  33. package/dist/webgl/draws.d.ts.map +1 -1
  34. package/dist/webgl/draws.js +7 -5
  35. package/dist/webgl/draws.js.map +1 -1
  36. package/dist/webgl/holds.d.ts +5 -2
  37. package/dist/webgl/holds.d.ts.map +1 -1
  38. package/dist/webgl/holds.js +9 -4
  39. package/dist/webgl/holds.js.map +1 -1
  40. package/dist/webgl/programs.d.ts +11 -0
  41. package/dist/webgl/programs.d.ts.map +1 -1
  42. package/dist/webgl/programs.js +42 -6
  43. package/dist/webgl/programs.js.map +1 -1
  44. package/dist/webgl/targets.d.ts +4 -0
  45. package/dist/webgl/targets.d.ts.map +1 -1
  46. package/dist/webgl/targets.js.map +1 -1
  47. package/package.json +2 -2
  48. package/src/commands.ts +47 -5
  49. package/src/index.ts +9 -0
  50. package/src/measure.ts +247 -2
  51. package/src/plan.ts +24 -4
  52. package/src/scene.ts +42 -3
  53. package/src/webgl/backdrops.ts +150 -0
  54. package/src/webgl/backend.ts +114 -32
  55. package/src/webgl/composite.ts +120 -48
  56. package/src/webgl/draws.ts +10 -3
  57. package/src/webgl/holds.ts +11 -3
  58. package/src/webgl/programs.ts +46 -5
  59. package/src/webgl/targets.ts +4 -0
@@ -5,11 +5,13 @@ import { ADD, ALPHA_MASK, KERNEL, LAYER, MULTIPLY, NORMAL, SCREEN } from "../sce
5
5
  import type { KernelHost, KernelProgram, KernelPrograms } from "./kernels.js";
6
6
  import {
7
7
  BACK_UNIT,
8
+ BELOW_STEP,
8
9
  compositeFragment,
9
10
  compositeVertex,
10
11
  FRESH,
11
12
  LAYER_COLOR,
12
13
  LAYER_UNIT,
14
+ SOLID_FRONT,
13
15
  } from "./programs.js";
14
16
  import { framebufferOf, LAYER_STEP, type Layer, type Open } from "./targets.js";
15
17
 
@@ -18,16 +20,19 @@ import { framebufferOf, LAYER_STEP, type Layer, type Open } from "./targets.js";
18
20
  * not compute, onto a target `opaque` throughout or not. Over an opaque
19
21
  * one, multiply's src × (1 − da) term is none, and premultiplied it is
20
22
  * s × d + d × (1 − sa), alpha as normal's: GL's blending (setModeBlend).
21
- * An alpha mask's is d × sa (ALPHA_MASK).
23
+ * An alpha mask's is d × sa (ALPHA_MASK). Any mode reads what is below in
24
+ * a layer drawn for its blend mode alone where that reads otherwise than
25
+ * it is stored (`below`, SceneList.below), which GL's blending cannot.
22
26
  */
23
- export function readsBelow(mode: number, opaque = false): boolean {
27
+ export function readsBelow(mode: number, opaque = false, below = 0): boolean {
24
28
  return !(
25
- mode === NORMAL ||
26
- mode === ALPHA_MASK ||
27
- mode === LAYER ||
28
- mode === ADD ||
29
- mode === SCREEN ||
30
- (mode === MULTIPLY && opaque)
29
+ below === 0 &&
30
+ (mode === NORMAL ||
31
+ mode === ALPHA_MASK ||
32
+ mode === LAYER ||
33
+ mode === ADD ||
34
+ mode === SCREEN ||
35
+ (mode === MULTIPLY && opaque))
31
36
  );
32
37
  }
33
38
 
@@ -86,6 +91,16 @@ export class Compositor {
86
91
  private backFramebuffer: WebGLFramebuffer | null = null;
87
92
  private backWidth = 0;
88
93
  private backHeight = 0;
94
+ /** Copies made (copyBoxes), so that a blend sharing one knows it is still the last. */
95
+ copies = 0;
96
+ /** The rectangle copyBelow copies, as copyBoxes takes it. */
97
+ private readonly one = [0, 0, 0, 0];
98
+ /**
99
+ * Where the copy copyBoxes made has each box's corner, less where the
100
+ * target has it, two numbers a box: a blend's `backX` and `backY`.
101
+ */
102
+ readonly offsets: number[] = [];
103
+
89
104
  constructor(
90
105
  gl: WebGL2RenderingContext,
91
106
  state: GlState,
@@ -132,7 +147,8 @@ export class Compositor {
132
147
  const kernel = given >= KERNEL ? this.useKernel(given - KERNEL) : null;
133
148
  const mode = given >= KERNEL && !kernel ? NORMAL : given;
134
149
  const state = this.state;
135
- const reads = readsBelow(mode, under.opaque);
150
+ const below = kernel ? 0 : (open.below ?? 0);
151
+ const reads = readsBelow(mode, under.opaque, below);
136
152
  if (!this.cover(open, under) || (reads && !this.copyBelow(under))) {
137
153
  return;
138
154
  }
@@ -161,7 +177,13 @@ export class Compositor {
161
177
  } else {
162
178
  state.useProgram(this.composite);
163
179
  // Its pixel alone where GL's blending computes the mode (MULTIPLY over an opaque target).
164
- gl.uniform1i(this.modeLocation, (reads ? mode : NORMAL) + (under.fresh ? FRESH : 0));
180
+ gl.uniform1i(
181
+ this.modeLocation,
182
+ (reads ? mode : NORMAL) +
183
+ (under.fresh ? FRESH : 0) +
184
+ (open.solid ? SOLID_FRONT : 0) +
185
+ below * BELOW_STEP,
186
+ );
165
187
  gl.uniform1i(this.compositeRowLocation, row);
166
188
  gl.uniform2i(this.offsetLocation, dx + ox, offsetY);
167
189
  gl.uniform1i(this.flipLocation, flip ? 1 : 0);
@@ -230,40 +252,78 @@ export class Compositor {
230
252
  * where the target has it, into place, and `under`'s framebuffer bound again.
231
253
  */
232
254
  copyBelow(under: Open): boolean {
255
+ const place = this.place;
256
+ const one = this.one;
257
+ one[0] = place.fx;
258
+ one[1] = place.fy;
259
+ one[2] = place.width;
260
+ one[3] = place.height;
261
+ this.copyBoxes(under, one, 1);
262
+ place.backX = this.offsets[0];
263
+ place.backY = this.offsets[1];
264
+ return true;
265
+ }
266
+
267
+ /**
268
+ * Whether copyBoxes copies its boxes where they lie on `under`, so that
269
+ * it may copy more than one (Backdrops' `spread`); else it copies one,
270
+ * from its corner.
271
+ */
272
+ copiesWhereLying(under: Open): boolean {
273
+ return under.msaa !== null && !(under.layer !== null && under.ox !== undefined);
274
+ }
275
+
276
+ /**
277
+ * What is below `count` boxes of `under`'s framebuffer copied in one go,
278
+ * x, y, width and height each, for blends that read it
279
+ * (Backdrops): bound to BACK_UNIT, each box's offset into offsets, and
280
+ * `under`'s framebuffer bound again. More than one only where
281
+ * copiesWhereLying.
282
+ */
283
+ copyBoxes(under: Open, boxes: readonly number[], count: number): void {
284
+ // Boxes copied from the corner would all land on it.
285
+ const lying = this.copiesWhereLying(under);
286
+ if (count > 1 && !lying) {
287
+ throw new Error("more than one backdrop box copied other than where they lie");
288
+ }
289
+
233
290
  const gl = this.gl;
234
291
  const state = this.state;
235
- const { fx, fy, width, height } = this.place;
236
- // Out of a multisampled page, its rectangle resolved into the page's own texture first, which
237
- // a blit may then copy anywhere: copied where it lies, the copy would stay as large as the
292
+ this.copies++;
293
+ // Out of a multisampled page, its boxes resolved into the page's own texture first, which a
294
+ // blit may then copy anywhere: copied where it lies, the copy would stay as large as the
238
295
  // page's far corner.
239
296
  let from = framebufferOf(under);
240
297
  const paged = under.msaa !== null && under.layer !== null && under.ox !== undefined;
241
298
  if (paged) {
242
299
  gl.bindFramebuffer(gl.READ_FRAMEBUFFER, from);
243
300
  gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, (under.layer as Layer).framebuffer);
244
- gl.blitFramebuffer(
245
- fx,
246
- fy,
247
- fx + width,
248
- fy + height,
249
- fx,
250
- fy,
251
- fx + width,
252
- fy + height,
253
- gl.COLOR_BUFFER_BIT,
254
- gl.NEAREST,
255
- );
301
+ for (let b = 0; b < count; b++) {
302
+ const fx = boxes[b * 4];
303
+ const fy = boxes[b * 4 + 1];
304
+ const right = fx + boxes[b * 4 + 2];
305
+ const top = fy + boxes[b * 4 + 3];
306
+ gl.blitFramebuffer(fx, fy, right, top, fx, fy, right, top, gl.COLOR_BUFFER_BIT, gl.NEAREST);
307
+ }
308
+
256
309
  from = (under.layer as Layer).framebuffer;
257
310
  }
258
311
 
259
312
  // Copied from its corner, or out of a multisampled target to where it lies, as a blit
260
- // resolving must keep its rectangle: then as large as the rectangle's far corner.
261
- const bx = under.msaa && !paged ? fx : 0;
262
- const by = under.msaa && !paged ? fy : 0;
263
- this.place.backX = fx - bx;
264
- this.place.backY = fy - by;
265
- const right = bx + width;
266
- const top = by + height;
313
+ // resolving must keep its rectangle: then as large as the boxes' far corner.
314
+ const offsets = this.offsets;
315
+ offsets.length = count * 2;
316
+ let right = 0;
317
+ let top = 0;
318
+
319
+ for (let b = 0; b < count; b++) {
320
+ const at = b * 4;
321
+ offsets[b * 2] = lying ? 0 : boxes[at];
322
+ offsets[b * 2 + 1] = lying ? 0 : boxes[at + 1];
323
+ right = Math.max(right, (lying ? boxes[at] : 0) + boxes[at + 2]);
324
+ top = Math.max(top, (lying ? boxes[at + 1] : 0) + boxes[at + 3]);
325
+ }
326
+
267
327
  if (!this.back || this.backWidth < right || this.backHeight < top) {
268
328
  if (this.back) {
269
329
  state.dropTexture(this.back);
@@ -294,7 +354,7 @@ export class Compositor {
294
354
 
295
355
  // Blitted, not copied: the canvas' colour may have no alpha, which copyTexSubImage2D into an
296
356
  // RGBA texture refuses, where a blit fills it in.
297
- // Out of a multisampled target the blit resolves the rectangle alone.
357
+ // Out of a multisampled target the blit resolves the box alone.
298
358
  if (!this.backFramebuffer) {
299
359
  this.backFramebuffer = gl.createFramebuffer() as WebGLFramebuffer;
300
360
  gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this.backFramebuffer);
@@ -310,23 +370,35 @@ export class Compositor {
310
370
  }
311
371
 
312
372
  gl.bindFramebuffer(gl.READ_FRAMEBUFFER, from);
313
- gl.blitFramebuffer(
314
- fx,
315
- fy,
316
- fx + width,
317
- fy + height,
318
- bx,
319
- by,
320
- right,
321
- top,
322
- gl.COLOR_BUFFER_BIT,
323
- gl.NEAREST,
324
- );
373
+ for (let b = 0; b < count; b++) {
374
+ const at = b * 4;
375
+ const fx = boxes[at];
376
+ const fy = boxes[at + 1];
377
+ const bx = fx - offsets[b * 2];
378
+ const by = fy - offsets[b * 2 + 1];
379
+ gl.blitFramebuffer(
380
+ fx,
381
+ fy,
382
+ fx + boxes[at + 2],
383
+ fy + boxes[at + 3],
384
+ bx,
385
+ by,
386
+ bx + boxes[at + 2],
387
+ by + boxes[at + 3],
388
+ gl.COLOR_BUFFER_BIT,
389
+ gl.NEAREST,
390
+ );
391
+ }
392
+
325
393
  // Both bindings back as the state cache has them.
326
394
  gl.bindFramebuffer(gl.FRAMEBUFFER, framebufferOf(under));
327
- state.bindTexture(BACK_UNIT, this.back);
328
- state.bindSampler(BACK_UNIT, null);
329
- return true;
395
+ this.bindBack();
396
+ }
397
+
398
+ /** The copy copyBoxes made bound to BACK_UNIT again, for a blend after another that shares it. */
399
+ bindBack(): void {
400
+ this.state.bindTexture(BACK_UNIT, this.back);
401
+ this.state.bindSampler(BACK_UNIT, null);
330
402
  }
331
403
 
332
404
  /**
@@ -22,6 +22,7 @@ import type { GpuTextures } from "./gpu-textures.js";
22
22
  import type { KernelHost, KernelPrograms } from "./kernels.js";
23
23
  import {
24
24
  BACK_UNIT,
25
+ BELOW_STEP,
25
26
  blendFillFragment,
26
27
  FILL_SLOT,
27
28
  FILL_TEXTURES,
@@ -556,11 +557,13 @@ export class Draws {
556
557
  /**
557
558
  * Entry `i`, blendable, drawn onto the target bound in blend mode `mode`,
558
559
  * a layer drawn for its blend mode alone where `fresh` (SceneList.fresh),
560
+ * what is below read as `below` says there (SceneList.below),
559
561
  * coloured by its layer's row `layerRow` (or -1), where the masks pushed
560
562
  * pass: GL's blending computing add, screen and, where it does not
561
563
  * `reads` below, multiply, the program the rest from the copy of what is
562
- * below (Compositor.copyBelow), whose texel at a fragment is (`backX`,
563
- * `backY`) short of it.
564
+ * below (Compositor.copyBoxes, made for it or shared with the blends
565
+ * drawn direct before it: Backdrops), whose texel at a fragment is
566
+ * (`backX`, `backY`) short of it.
564
567
  */
565
568
  drawBlended(
566
569
  scene: SceneList,
@@ -568,6 +571,7 @@ export class Draws {
568
571
  mode: number,
569
572
  reads: boolean,
570
573
  fresh: boolean | undefined,
574
+ below: number,
571
575
  layerRow: number,
572
576
  backX: number,
573
577
  backY: number,
@@ -579,7 +583,10 @@ export class Draws {
579
583
  gl.uniform4fv(this.blendViewLocation, this.view);
580
584
  gl.uniform1f(this.blendUnitLocation, this.unit);
581
585
  // Its pixel alone where GL's blending computes the mode (readsBelow).
582
- gl.uniform1i(this.blendModeLocation, (reads ? mode : NORMAL) + (fresh ? FRESH : 0));
586
+ gl.uniform1i(
587
+ this.blendModeLocation,
588
+ (reads ? mode : NORMAL) + (fresh ? FRESH : 0) + below * BELOW_STEP,
589
+ );
583
590
  gl.uniform1i(this.blendLayerRowLocation, layerRow);
584
591
  gl.uniform2i(this.blendBackLocation, backX, backY);
585
592
  this.gatherRun(scene, i, i + 1, false);
@@ -4,6 +4,9 @@ import {
4
4
  HELD,
5
5
  HOLD_POP,
6
6
  HOLD_PUSH,
7
+ operandBelow,
8
+ operandMode,
9
+ operandSolid,
7
10
  RUN,
8
11
  STENCIL_POP,
9
12
  STENCIL_PUSH,
@@ -545,8 +548,11 @@ export class HeldLayers {
545
548
  state.stencil(null);
546
549
  }
547
550
 
548
- /** Held output `held` composited onto `under`, bound, in blend mode `mode`, coloured by `row`. */
549
- compositeHeld(held: number, under: Open, mode: number, row: number): void {
551
+ /**
552
+ * Held output `held` composited onto `under`, bound, in blend mode and
553
+ * below `operand` (modeWithBelow), coloured by `row`.
554
+ */
555
+ compositeHeld(held: number, under: Open, operand: number, row: number): void {
550
556
  const hold = this.holds[held];
551
557
  const result = hold.result;
552
558
  if (!result) {
@@ -561,7 +567,9 @@ export class HeldLayers {
561
567
  open.height = hold.height;
562
568
  open.ox = result.x;
563
569
  open.oy = result.y;
564
- this.compositor.compositeLayer(open, under, mode, row);
570
+ open.below = operandBelow(operand);
571
+ open.solid = operandSolid(operand);
572
+ this.compositor.compositeLayer(open, under, operandMode(operand), row);
565
573
  }
566
574
 
567
575
  /**
@@ -3,6 +3,9 @@ import { ROWS_A_LINE } from "../rows.js";
3
3
  import {
4
4
  ADD,
5
5
  ALPHA,
6
+ BELOW_CLEARED,
7
+ BELOW_INVERTED,
8
+ BELOW_SUBTRACTED,
6
9
  DARKEN,
7
10
  DIFFERENCE,
8
11
  ERASE,
@@ -574,6 +577,19 @@ const layerColor = (row: string) => ` if (${row} >= 0 && front.a > 0.0) {
574
577
  */
575
578
  export const FRESH = 64;
576
579
 
580
+ /**
581
+ * Added to a composite's uMode where the layer is the bitmap of its
582
+ * filters (SceneList.solids), whose clear pixels BLEND_FRONT draws as it
583
+ * draws the rest.
584
+ */
585
+ export const SOLID_FRONT = 128;
586
+
587
+ /**
588
+ * What a blend program's uMode has, times this, for how it reads what is
589
+ * below it in a layer drawn for its blend mode alone (SceneList.below).
590
+ */
591
+ export const BELOW_STEP = 256;
592
+
577
593
  /** A composited layer's pixel, `front`, coloured by its row's colour transform where it has one (uRow not -1). */
578
594
  export const LAYER_COLOR = layerColor("uRow");
579
595
 
@@ -594,11 +610,21 @@ export const LAYER_COLOR = layerColor("uRow");
594
610
  * formulas, so they mark it DRAWN_CLEAR, clear but for a red no
595
611
  * premultiplied colour has, which the formulas read as clear and
596
612
  * compositeFragment drops, as Flash's premultiplied pixels of no alpha
597
- * have no colour.
613
+ * have no colour. What is drawn over a pixel so drawn as it is reads it as
614
+ * the mode would have left it over nothing, which the planner works out
615
+ * where it can (uMode / BELOW_STEP, SceneList.below): an invert's as white,
616
+ * a subtract's as black, an erase's or alpha's as clear, at its alpha, in
617
+ * any mode, normal too, for which GL's blending cannot read it. Where
618
+ * `front` is clear the pixel keeps what it holds, and alpha leaves it be,
619
+ * as Flash draws a shape, but not where `front` is the bitmap of a display
620
+ * object's filters (SOLID_FRONT added to uMode): adl draws that bitmap's
621
+ * clear pixels as it does the rest.
598
622
  */
599
623
  const BLEND_FRONT = ` int mode = uMode & ${FRESH - 1};
600
- bool fresh = uMode >= ${FRESH};
601
- if (mode == ${NORMAL} || mode == ${LAYER} || mode == ${SCREEN} || mode == ${ADD}) {
624
+ bool fresh = (uMode & ${FRESH}) != 0;
625
+ bool solid = (uMode & ${SOLID_FRONT}) != 0;
626
+ int below = uMode / ${BELOW_STEP};
627
+ if (below == 0 && (mode == ${NORMAL} || mode == ${LAYER} || mode == ${SCREEN} || mode == ${ADD})) {
602
628
  finalColor = front;
603
629
  return;
604
630
  }
@@ -609,8 +635,17 @@ const BLEND_FRONT = ` int mode = uMode & ${FRESH - 1};
609
635
  return;
610
636
  }
611
637
 
612
- if (back.a == 0.0) {
638
+ if (below != 0 && front.a == 0.0 && !solid) {
639
+ finalColor = back;
640
+ return;
641
+ }
642
+
643
+ if (back.a == 0.0 || below == ${BELOW_CLEARED}) {
613
644
  back = vec4(0.0);
645
+ } else if (below == ${BELOW_INVERTED}) {
646
+ back = vec4(back.a);
647
+ } else if (below == ${BELOW_SUBTRACTED}) {
648
+ back = vec4(0.0, 0.0, 0.0, back.a);
614
649
  }
615
650
 
616
651
  vec3 cb = straight(back);
@@ -623,6 +658,12 @@ const BLEND_FRONT = ` int mode = uMode & ${FRESH - 1};
623
658
  case ${DIFFERENCE}: b = abs(cb - cf); break;
624
659
  case ${OVERLAY}: b = hardlight(cf, cb); break;
625
660
  case ${HARDLIGHT}: b = hardlight(cb, cf); break;
661
+ case ${ADD}:
662
+ finalColor = min(front + back, 1.0);
663
+ return;
664
+ case ${SCREEN}:
665
+ finalColor = front + back * (1.0 - front);
666
+ return;
626
667
  case ${SUBTRACT}:
627
668
  finalColor = vec4(max(back.rgb - front.rgb, 0.0), min(front.a + back.a, 1.0));
628
669
  return;
@@ -633,7 +674,7 @@ const BLEND_FRONT = ` int mode = uMode & ${FRESH - 1};
633
674
  );
634
675
  return;
635
676
  case ${ALPHA}:
636
- finalColor = drawn(front.a > 0.0 ? back * front.a : back, fresh);
677
+ finalColor = drawn(front.a > 0.0 || solid ? back * front.a : back, fresh);
637
678
  return;
638
679
  case ${ERASE}:
639
680
  finalColor = drawn(back * (1.0 - front.a), fresh);
@@ -145,6 +145,10 @@ export interface Open {
145
145
  opaque?: boolean;
146
146
  /** Whether it is a layer drawn for its blend mode alone (SceneList.fresh). */
147
147
  fresh?: boolean;
148
+ /** How it reads what is below it in a layer drawn for its blend mode alone (SceneList.below). */
149
+ below?: number;
150
+ /** Whether it is the bitmap of its filters, its clear pixels drawn (SceneList.solids). */
151
+ solid?: boolean;
148
152
  }
149
153
 
150
154
  /** A whole layer's object from params `p` from `at` (plan.ts, layerParams): null for none. */