@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
package/src/scene.ts CHANGED
@@ -22,6 +22,18 @@ export const HARDLIGHT = 13;
22
22
  * alone takes it, never a draw or a kept layer, whose modes are bytes.
23
23
  */
24
24
  export const KERNEL = 14;
25
+
26
+ /**
27
+ * In a layer drawn for its blend mode alone (SceneList.fresh), adl shows a
28
+ * pixel one object alone drew as that object is, in any mode, but what is
29
+ * drawn over it later reads it as the object's mode would have left it over
30
+ * nothing: an invert's as white (BELOW_INVERTED), a subtract's as black
31
+ * (BELOW_SUBTRACTED), an erase's or alpha's as clear (BELOW_CLEARED), each
32
+ * at the object's alpha. SceneList.below.
33
+ */
34
+ export const BELOW_INVERTED = 1;
35
+ export const BELOW_SUBTRACTED = 2;
36
+ export const BELOW_CLEARED = 3;
25
37
  /**
26
38
  * Not Flash's: an alpha mask's layer composited onto the unfiltered layer
27
39
  * that ends after it, the layer of what it masks, keeping only what the
@@ -191,6 +203,21 @@ export class SceneList {
191
203
  * (programs.ts, BLEND_FRONT).
192
204
  */
193
205
  fresh = new Uint8Array(16);
206
+ /**
207
+ * How a layer in a fresh one reads the pixels below it there, where all
208
+ * that lies under it there are blends each alone on its pixels and in one
209
+ * of the modes whose pixel adl reads otherwise than it shows it
210
+ * (BELOW_INVERTED and the rest, measureLayers); else 0, as it is stored.
211
+ */
212
+ below = new Uint8Array(16);
213
+ /**
214
+ * Whether each filtered layer, or kept output, covers the bitmap Flash
215
+ * draws its object's filters into, as their padding is that bitmap's on
216
+ * every side: its clear pixels are then that bitmap's, which an alpha
217
+ * blend clears what is below by and a blend over a fresh layer reads it
218
+ * through (programs.ts, BLEND_FRONT), as it does a pixel it draws.
219
+ */
220
+ solids = new Uint8Array(16);
194
221
  /**
195
222
  * Each whole layer's object, Flash's bitmap of it, uncut by the target:
196
223
  * x, y, width and height in target pixels, as measureLayers finds it.
@@ -313,7 +340,8 @@ export class SceneList {
313
340
 
314
341
  /**
315
342
  * Kept output `keep` composited again over the rectangle x y width height
316
- * of the target, in blend mode `mode`, coloured by `row`.
343
+ * of the target, in blend mode `mode`, coloured by `row`, the bitmap of
344
+ * its filters where `solid` (solids).
317
345
  */
318
346
  keptLayer(
319
347
  keep: number,
@@ -323,8 +351,10 @@ export class SceneList {
323
351
  y: number,
324
352
  width: number,
325
353
  height: number,
354
+ solid = false,
326
355
  ): void {
327
356
  const layer = this.newLayer(-1, keep);
357
+ this.solids[layer] = solid ? 1 : 0;
328
358
  this.layerRect(layer, x, y, width, height);
329
359
  this.push(KEPT, layer, row, mode, keep, 0);
330
360
  }
@@ -337,6 +367,8 @@ export class SceneList {
337
367
  this.wholes = grow(this.wholes);
338
368
  this.groups = grow(this.groups);
339
369
  this.fresh = grow(this.fresh);
370
+ this.below = grow(this.below);
371
+ this.solids = grow(this.solids);
340
372
  const objects = new Float64Array(this.objects.length * 2);
341
373
  objects.set(this.objects);
342
374
  this.objects = objects;
@@ -351,15 +383,22 @@ export class SceneList {
351
383
  this.wholes[layer] = NO_WHOLE;
352
384
  this.groups[layer] = 0;
353
385
  this.fresh[layer] = 0;
386
+ this.below[layer] = 0;
387
+ this.solids[layer] = 0;
354
388
  this.keeps[layer] = keep;
355
389
  this.parents[layer] = this.open.at(-1) ?? -1;
356
390
  this.layerRect(layer, 0, 0, 0, 0);
357
391
  return layer;
358
392
  }
359
393
 
360
- /** Filtered layer `layer` padded by `padding` after all, as its filters were fitted once it was drawn. */
361
- padLayer(layer: number, padding: number): void {
394
+ /**
395
+ * Filtered layer `layer` padded by `padding` after all, as its filters
396
+ * were fitted once it was drawn, the bitmap of its filters where `solid`
397
+ * (solids).
398
+ */
399
+ padLayer(layer: number, padding: number, solid = false): void {
362
400
  this.pads[layer] = padding;
401
+ this.solids[layer] = solid ? 1 : 0;
363
402
  }
364
403
 
365
404
  /** The layer begun last ended: filtered by `chain` (or -1), composited in `mode`, coloured by `row`. */
@@ -0,0 +1,150 @@
1
+ /**
2
+ * The most blends drawn direct, reading what is below or not, offered
3
+ * after one copy of it, which bounds the rectangles each is compared with.
4
+ */
5
+ export const SHARED_MOST = 64;
6
+
7
+ /**
8
+ * How much larger than the rectangles it holds a box copied for them may
9
+ * be, by area, before a rectangle far from the others is copied apart.
10
+ */
11
+ export const SPREAD = 2;
12
+
13
+ /**
14
+ * Which consecutive blends drawn direct (WebGLBackend.blendDirect) share
15
+ * one copy of what is below (Compositor.copyBoxes), and the boxes it
16
+ * copies. The copy made before the first is still what is below each one
17
+ * after it whose rectangle meets none drawn since: a blend drawn direct
18
+ * draws inside its rectangle alone. The blends are offered in order, those
19
+ * that do not read what is below as well, as what they draw counts.
20
+ *
21
+ * A rectangle is copied in the box of others it lies near: one box where
22
+ * all of them together are at most SPREAD times their area, else apart, as
23
+ * a union of rectangles far apart would copy far more than they hold, or,
24
+ * where the copy is not made where its rectangles lie (`spread` false),
25
+ * not shared.
26
+ */
27
+ export class Backdrops {
28
+ /** The blends' commands that share the copy, each with its box, `shared` of them. */
29
+ private readonly commands: number[] = [];
30
+ private readonly boxOf: number[] = [];
31
+ private shared = 0;
32
+ /** What was drawn since the copy, as least x and y, greatest x and y, `drawnCount` of them. */
33
+ private readonly drawn: number[] = [];
34
+ private drawnCount = 0;
35
+ /**
36
+ * The boxes copied, x, y, width and height each in the target's
37
+ * framebuffer, as Compositor.copyBoxes takes them, and the area of the
38
+ * rectangles each holds; `boxCount` of them, past which the arrays hold
39
+ * an earlier copy's.
40
+ */
41
+ readonly boxes: number[] = [];
42
+ private readonly areas: number[] = [];
43
+ boxCount = 0;
44
+ /** Which copy the blends share (Compositor.copies), set once it is made. */
45
+ copy = -1;
46
+ private spread = true;
47
+
48
+ /** None shared, for a copy to be made for the blends offered from here on. */
49
+ begin(spread: boolean): void {
50
+ this.shared = 0;
51
+ this.drawnCount = 0;
52
+ this.boxCount = 0;
53
+ this.copy = -1;
54
+ this.spread = spread;
55
+ }
56
+
57
+ /**
58
+ * The next blend drawn direct, at command `command`, over rectangle `x`,
59
+ * `y`, `width`, `height` of the target's framebuffer, which `reads` what
60
+ * is below or not: whether the copy holds what is below it as it draws.
61
+ * One that does not ends the blends that share it, as does the one past
62
+ * SHARED_MOST.
63
+ */
64
+ offer(
65
+ command: number,
66
+ x: number,
67
+ y: number,
68
+ width: number,
69
+ height: number,
70
+ reads: boolean,
71
+ ): boolean {
72
+ if (this.drawnCount >= SHARED_MOST) {
73
+ return false;
74
+ }
75
+
76
+ const drawn = this.drawn;
77
+ const right = x + width;
78
+ const top = y + height;
79
+
80
+ if (reads) {
81
+ for (let a = 0; a < this.drawnCount * 4; a += 4) {
82
+ if (drawn[a] < right && x < drawn[a + 2] && drawn[a + 1] < top && y < drawn[a + 3]) {
83
+ return false;
84
+ }
85
+ }
86
+
87
+ const box = this.box(x, y, width, height);
88
+ if (box < 0) {
89
+ return false;
90
+ }
91
+
92
+ this.commands[this.shared] = command;
93
+ this.boxOf[this.shared] = box;
94
+ this.shared++;
95
+ }
96
+
97
+ const at = this.drawnCount++ * 4;
98
+ drawn[at] = x;
99
+ drawn[at + 1] = y;
100
+ drawn[at + 2] = right;
101
+ drawn[at + 3] = top;
102
+ return true;
103
+ }
104
+
105
+ /** The box that holds the rectangle of the blend at command `command`, -1 for one not shared. */
106
+ boxFor(command: number): number {
107
+ const commands = this.commands;
108
+ for (let n = 0; n < this.shared; n++) {
109
+ if (commands[n] === command) {
110
+ return this.boxOf[n];
111
+ }
112
+ }
113
+
114
+ return -1;
115
+ }
116
+
117
+ /** The box a rectangle is copied in, grown to hold it or new; -1 for none. */
118
+ private box(x: number, y: number, width: number, height: number): number {
119
+ const boxes = this.boxes;
120
+ const areas = this.areas;
121
+ const area = width * height;
122
+ for (let b = 0; b < this.boxCount; b++) {
123
+ const at = b * 4;
124
+ const x0 = Math.min(boxes[at], x);
125
+ const y0 = Math.min(boxes[at + 1], y);
126
+ const x1 = Math.max(boxes[at] + boxes[at + 2], x + width);
127
+ const y1 = Math.max(boxes[at + 1] + boxes[at + 3], y + height);
128
+ if ((x1 - x0) * (y1 - y0) <= SPREAD * (areas[b] + area)) {
129
+ boxes[at] = x0;
130
+ boxes[at + 1] = y0;
131
+ boxes[at + 2] = x1 - x0;
132
+ boxes[at + 3] = y1 - y0;
133
+ areas[b] += area;
134
+ return b;
135
+ }
136
+ }
137
+
138
+ if (this.boxCount > 0 && !this.spread) {
139
+ return -1;
140
+ }
141
+
142
+ const b = this.boxCount++;
143
+ boxes[b * 4] = x;
144
+ boxes[b * 4 + 1] = y;
145
+ boxes[b * 4 + 2] = width;
146
+ boxes[b * 4 + 3] = height;
147
+ areas[b] = area;
148
+ return b;
149
+ }
150
+ }
@@ -7,8 +7,13 @@ import {
7
7
  HELD,
8
8
  HOLD_POP,
9
9
  HOLD_PUSH,
10
+ LAYER_FILTERED,
10
11
  LAYER_POP,
11
12
  LAYER_PUSH,
13
+ LAYER_SOLID,
14
+ operandBelow,
15
+ operandMode,
16
+ operandSolid,
12
17
  RUN,
13
18
  STENCIL_POP,
14
19
  STENCIL_PUSH,
@@ -20,6 +25,7 @@ import type { Rows } from "../rows.js";
20
25
  import { blendsDirect, NORMAL, type SceneList } from "../scene.js";
21
26
  import type { Textures } from "../textures.js";
22
27
  import { GeometryAtlas } from "./atlas.js";
28
+ import { Backdrops } from "./backdrops.js";
23
29
  import { Compositor, readsBelow } from "./composite.js";
24
30
  import { Draws, type Masks } from "./draws.js";
25
31
  import { FilterRunner } from "./filters.js";
@@ -113,6 +119,8 @@ export class WebGLBackend {
113
119
  private readonly masks: Masks = { depth: 0, past: 0 };
114
120
  /** A layer's rectangle, as blendDirect reads it. */
115
121
  private readonly rect = { x: 0, y: 0, width: 0, height: 0 };
122
+ /** The blends drawn direct that share the copy of what is below made last (shareCopy). */
123
+ private readonly backdrops = new Backdrops();
116
124
  /** The mask counts of the targets below the layers drawn into now, two numbers each. */
117
125
  private readonly below: number[] = [];
118
126
  /** The program `compositeOver` draws with, linked ahead (programs); null till then and after a lost context. */
@@ -411,6 +419,7 @@ export class WebGLBackend {
411
419
  opaque: commands.opaqueStage,
412
420
  };
413
421
  this.layers.length = 0;
422
+ this.backdrops.begin(false);
414
423
  this.draws.drawInto(canvas, target);
415
424
 
416
425
  this.draws.forgetRow();
@@ -482,7 +491,7 @@ export class WebGLBackend {
482
491
  const from = ops[at + 1];
483
492
  const width = p[from + 2];
484
493
  const height = p[from + 3];
485
- const filtered = ops[at + 2] === 1;
494
+ const filtered = (ops[at + 2] & LAYER_FILTERED) !== 0;
486
495
  // A filtered one of the size its filters' targets take for it, which they read alike.
487
496
  const layer = this.pool.takeLayer(width, height, filtered);
488
497
  const open: Open = {
@@ -493,7 +502,9 @@ export class WebGLBackend {
493
502
  width,
494
503
  height,
495
504
  object: objectOf(p, from + 4),
496
- fresh: ops[at + 3] === 1,
505
+ fresh: (ops[at + 3] & 1) === 1,
506
+ below: ops[at + 3] >> 1,
507
+ solid: (ops[at + 2] & LAYER_SOLID) !== 0,
497
508
  };
498
509
  this.layers.push(open);
499
510
  this.draws.drawInto(open, target);
@@ -563,12 +574,14 @@ export class WebGLBackend {
563
574
  y: p[from + 1],
564
575
  width: p[from + 2],
565
576
  height: p[from + 3],
577
+ below: operandBelow(ops[at + 2]),
578
+ solid: operandSolid(ops[at + 2]),
566
579
  };
567
580
  this.draws.maskBy(masks);
568
581
  this.compositor.compositeLayer(
569
582
  kept,
570
583
  this.layers.at(-1) ?? canvas,
571
- ops[at + 2],
584
+ operandMode(ops[at + 2]),
572
585
  ops[at + 3],
573
586
  );
574
587
  }
@@ -1130,60 +1143,129 @@ export class WebGLBackend {
1130
1143
  * inside a pixel, each blended by what it covers there is as adl blends
1131
1144
  * them, not the pixel they make together, which a layer would blend. A
1132
1145
  * layer and its resolve and composite left out, what reads what is below
1133
- * copies it as the composite would. Whether it was.
1146
+ * copies it as the composite would, or shares the copy made for one
1147
+ * drawn direct before it (shareCopy). Whether it was.
1134
1148
  */
1135
1149
  private blendDirect(commands: Commands, k: number, scene: SceneList, canvas: Open): boolean {
1136
- const ops = commands.ops;
1137
- const at = k * 4;
1138
- const run = at + 4;
1139
- const pop = at + 8;
1140
- if (
1141
- k + 2 >= commands.length ||
1142
- ops[at + 2] !== 0 ||
1143
- ops[run] !== RUN ||
1144
- ops[run + 2] !== 1 ||
1145
- ops[run + 3] !== NORMAL ||
1146
- ops[pop] !== LAYER_POP ||
1147
- ops[pop + 3] >= 0 ||
1148
- !blendsDirect(ops[pop + 1]) ||
1149
- !this.draws.blendable(scene, ops[run + 1])
1150
- ) {
1150
+ if (!this.drawsDirect(commands, k, scene)) {
1151
1151
  return false;
1152
1152
  }
1153
1153
 
1154
- const p = commands.params;
1155
- const from = ops[at + 1];
1154
+ const ops = commands.ops;
1155
+ const pop = k * 4 + 8;
1156
1156
  const under = this.layers.at(-1) ?? canvas;
1157
- const rect = this.rect;
1158
- rect.x = p[from];
1159
- rect.y = p[from + 1];
1160
- rect.width = p[from + 2];
1161
- rect.height = p[from + 3];
1162
1157
  const mode = ops[pop + 1];
1158
+ const below = ops[k * 4 + 3] >> 1;
1163
1159
  const compositor = this.compositor;
1164
- if (!compositor.cover(rect, under)) {
1160
+ if (!compositor.cover(this.rectAt(commands, k), under)) {
1165
1161
  return true;
1166
1162
  }
1167
1163
 
1168
1164
  // Drawn into already, as a kept output is composited (COMPOSITE); a copy of what is below
1169
1165
  // binds its framebuffer back.
1170
- const reads = readsBelow(mode, under.opaque);
1171
- const back = reads && compositor.copyBelow(under);
1166
+ const reads = readsBelow(mode, under.opaque, below);
1167
+ let box = -1;
1168
+ if (reads) {
1169
+ // Shared only while the copy is the last made, which nothing but blends sharing it come
1170
+ // between.
1171
+ const backdrops = this.backdrops;
1172
+ box = backdrops.copy === compositor.copies ? backdrops.boxFor(k) : -1;
1173
+ if (box < 0) {
1174
+ box = this.shareCopy(commands, k, scene, under);
1175
+ } else {
1176
+ compositor.bindBack();
1177
+ }
1178
+ }
1179
+
1172
1180
  this.draws.maskBy(this.masks);
1173
1181
  const place = compositor.place;
1174
1182
  // Kept to the layer's rectangle, as the composite is, past which nothing below is copied.
1175
1183
  this.state.scissor([place.fx, place.fy, place.width, place.height]);
1176
1184
  this.draws.drawBlended(
1177
1185
  scene,
1178
- ops[run + 1],
1186
+ ops[k * 4 + 5],
1179
1187
  mode,
1180
1188
  reads,
1181
1189
  under.fresh,
1190
+ below,
1182
1191
  ops[pop + 2],
1183
- back ? place.backX : 0,
1184
- back ? place.backY : 0,
1192
+ reads ? compositor.offsets[box * 2] : 0,
1193
+ reads ? compositor.offsets[box * 2 + 1] : 0,
1185
1194
  );
1186
1195
  this.state.scissor(null);
1187
1196
  return true;
1188
1197
  }
1198
+
1199
+ /**
1200
+ * Whether the layer pushed at command `k` may be drawn straight onto the
1201
+ * target below (blendDirect): unfiltered, not kept, one run of a flat
1202
+ * geometry alone drawn normally in it, in a mode that blendsDirect.
1203
+ */
1204
+ private drawsDirect(commands: Commands, k: number, scene: SceneList): boolean {
1205
+ const ops = commands.ops;
1206
+ const at = k * 4;
1207
+ const run = at + 4;
1208
+ const pop = at + 8;
1209
+ return (
1210
+ k + 2 < commands.length &&
1211
+ ops[at] === LAYER_PUSH &&
1212
+ ops[at + 2] === 0 &&
1213
+ ops[run] === RUN &&
1214
+ ops[run + 2] === 1 &&
1215
+ ops[run + 3] === NORMAL &&
1216
+ ops[pop] === LAYER_POP &&
1217
+ ops[pop + 3] < 0 &&
1218
+ blendsDirect(ops[pop + 1]) &&
1219
+ this.draws.blendable(scene, ops[run + 1])
1220
+ );
1221
+ }
1222
+
1223
+ /** The rectangle of the layer pushed at command `k`, into rect. */
1224
+ private rectAt(
1225
+ commands: Commands,
1226
+ k: number,
1227
+ ): { x: number; y: number; width: number; height: number } {
1228
+ const p = commands.params;
1229
+ const from = commands.ops[k * 4 + 1];
1230
+ const rect = this.rect;
1231
+ rect.x = p[from];
1232
+ rect.y = p[from + 1];
1233
+ rect.width = p[from + 2];
1234
+ rect.height = p[from + 3];
1235
+ return rect;
1236
+ }
1237
+
1238
+ /**
1239
+ * What is below the blend drawn direct at command `k`, which reads it,
1240
+ * copied once for it and for the blends drawn direct right after it that
1241
+ * may share the copy (Backdrops): the box that holds its rectangle, and
1242
+ * the compositor's place its own again.
1243
+ */
1244
+ private shareCopy(commands: Commands, k: number, scene: SceneList, under: Open): number {
1245
+ const compositor = this.compositor;
1246
+ const backdrops = this.backdrops;
1247
+ const place = compositor.place;
1248
+ backdrops.begin(compositor.copiesWhereLying(under));
1249
+
1250
+ for (let j = k; j < commands.length && this.drawsDirect(commands, j, scene); j += 3) {
1251
+ if (
1252
+ compositor.cover(this.rectAt(commands, j), under) &&
1253
+ !backdrops.offer(
1254
+ j,
1255
+ place.fx,
1256
+ place.fy,
1257
+ place.width,
1258
+ place.height,
1259
+ readsBelow(commands.ops[j * 4 + 9], under.opaque, commands.ops[j * 4 + 3] >> 1),
1260
+ )
1261
+ ) {
1262
+ break;
1263
+ }
1264
+ }
1265
+
1266
+ compositor.copyBoxes(under, backdrops.boxes, backdrops.boxCount);
1267
+ backdrops.copy = compositor.copies;
1268
+ compositor.cover(this.rectAt(commands, k), under);
1269
+ return backdrops.boxFor(k);
1270
+ }
1189
1271
  }