@swf2es/render 0.1.41 → 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 +27 -5
  2. package/dist/commands.d.ts.map +1 -1
  3. package/dist/commands.js +43 -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 +45 -7
  17. package/dist/scene.d.ts.map +1 -1
  18. package/dist/scene.js +54 -7
  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 -28
  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 +6 -3
  33. package/dist/webgl/draws.d.ts.map +1 -1
  34. package/dist/webgl/draws.js +8 -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 +16 -0
  41. package/dist/webgl/programs.d.ts.map +1 -1
  42. package/dist/webgl/programs.js +82 -9
  43. package/dist/webgl/programs.js.map +1 -1
  44. package/dist/webgl/targets.d.ts +6 -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 +48 -5
  49. package/src/index.ts +9 -0
  50. package/src/measure.ts +247 -2
  51. package/src/plan.ts +24 -3
  52. package/src/scene.ts +55 -6
  53. package/src/webgl/backdrops.ts +150 -0
  54. package/src/webgl/backend.ts +115 -31
  55. package/src/webgl/composite.ts +121 -48
  56. package/src/webgl/draws.ts +13 -3
  57. package/src/webgl/holds.ts +11 -3
  58. package/src/webgl/programs.ts +87 -8
  59. package/src/webgl/targets.ts +6 -0
package/src/measure.ts CHANGED
@@ -1,9 +1,16 @@
1
1
  import type { Geometries } from "./geometry.js";
2
2
  import { ROW_FLOATS, type Rows } from "./rows.js";
3
3
  import {
4
+ ALPHA,
4
5
  ALPHA_MASK,
6
+ BELOW_CLEARED,
7
+ BELOW_INVERTED,
8
+ BELOW_SUBTRACTED,
5
9
  DRAW,
10
+ ERASE,
11
+ INVERT,
6
12
  KEPT,
13
+ KERNEL,
7
14
  LAYER_BEGIN,
8
15
  LAYER_END,
9
16
  MASK_BEGIN,
@@ -11,6 +18,7 @@ import {
11
18
  MASK_USE,
12
19
  NO_WHOLE,
13
20
  type SceneList,
21
+ SUBTRACT,
14
22
  WHOLE_ALIASED,
15
23
  } from "./scene.js";
16
24
 
@@ -94,6 +102,26 @@ let open = new Int32Array(16);
94
102
  let openTop = 0;
95
103
  // drawnArea's area as it is measured.
96
104
  const drawn = new Float64Array(4);
105
+ // A draw's own area, as a fresh layer's items take it.
106
+ const own = new Float64Array(4);
107
+
108
+ /**
109
+ * What each open fresh layer has had drawn in it, in order (belowOf): five
110
+ * numbers an item, the whole pixels it may touch, least x and y and past
111
+ * the greatest, then what a pixel it alone drew reads as (BELOW_INVERTED
112
+ * and the rest, 0 for as it is stored).
113
+ */
114
+ let items = new Float64Array(80);
115
+ let itemTop = 0;
116
+ // Where each open layer's items begin, as `open`.
117
+ let itemFrom = new Int32Array(16);
118
+ // The most items a layer keeps apart, past which they are taken as one drawn as it is stored.
119
+ const MOST_ITEMS = 32;
120
+ // Pixels an item is taken to reach past its box each way, as layers are: a snapped vertex may lie
121
+ // a part of a pixel past it, and a sample there may make two items share the pixel.
122
+ const ITEM_SLACK = 1;
123
+ // How deep in a mask's own draws, which draw nothing, the walk is.
124
+ let maskDraws = 0;
97
125
 
98
126
  /**
99
127
  * Each layer's rectangle of the target (SceneList.layerRect), from what is
@@ -123,6 +151,8 @@ export function measureLayers(
123
151
  clips.top = 0;
124
152
  cuts.top = 0;
125
153
  openTop = 0;
154
+ itemTop = 0;
155
+ maskDraws = 0;
126
156
  areas.pushEmpty();
127
157
  const k = target.scale;
128
158
  const box = geometries.box;
@@ -131,20 +161,38 @@ export function measureLayers(
131
161
  for (let i = 0; i < scene.length; i++) {
132
162
  switch (scene.kind[i]) {
133
163
  case DRAW:
134
- placeDraw(scene, i, box, m, k, areas.data, areas.top);
164
+ if (maskDraws === 0 && openTop > 0 && scene.fresh[open[openTop - 1]] === 1) {
165
+ own[0] = Number.POSITIVE_INFINITY;
166
+ own[1] = Number.POSITIVE_INFINITY;
167
+ own[2] = Number.NEGATIVE_INFINITY;
168
+ own[3] = Number.NEGATIVE_INFINITY;
169
+ placeDraw(scene, i, box, m, k, own, 4);
170
+ if (own[2] >= own[0] && own[3] >= own[1]) {
171
+ unionTop(own[0], own[1], own[2], own[3]);
172
+ addItem(itemFrom[openTop - 1], own[0], own[1], own[2], own[3], 0);
173
+ }
174
+ } else {
175
+ placeDraw(scene, i, box, m, k, areas.data, areas.top);
176
+ }
177
+
135
178
  break;
136
179
 
137
180
  case MASK_BEGIN:
138
181
  // The mask's own draws, measured apart.
139
182
  areas.pushEmpty();
183
+ maskDraws++;
140
184
  break;
141
185
  case LAYER_BEGIN:
142
186
  if (openTop === open.length) {
143
187
  const grown = new Int32Array(open.length * 2);
144
188
  grown.set(open);
145
189
  open = grown;
190
+ const from = new Int32Array(itemFrom.length * 2);
191
+ from.set(itemFrom);
192
+ itemFrom = from;
146
193
  }
147
194
 
195
+ itemFrom[openTop] = itemTop;
148
196
  open[openTop++] = scene.geometry[i];
149
197
  areas.pushEmpty();
150
198
  cuts.push(
@@ -158,6 +206,7 @@ export function measureLayers(
158
206
  // The mask's draws clip what follows, measured apart till it pops.
159
207
  areas.moveTop(clips);
160
208
  areas.pushEmpty();
209
+ maskDraws--;
161
210
  break;
162
211
  case MASK_POP: {
163
212
  const a = areas.data;
@@ -180,7 +229,9 @@ export function measureLayers(
180
229
  case LAYER_END: {
181
230
  const a = areas.data;
182
231
  const n = areas.top;
232
+ itemTop = itemFrom[openTop - 1];
183
233
  const layer = open[--openTop];
234
+ const parent = openTop > 0 ? open[openTop - 1] : -1;
184
235
  const pad = scene.pads[layer];
185
236
  const cut = cuts.data;
186
237
  const c = cuts.top - 4;
@@ -189,6 +240,21 @@ export function measureLayers(
189
240
  const top = Math.max(0, Math.floor(a[n - 3]) - 1, cut[c + 1]);
190
241
  const right = Math.min(target.width, Math.ceil(a[n - 2]) + 1, cut[c + 2]);
191
242
  const bottom = Math.min(target.height, Math.ceil(a[n - 1]) + 1, cut[c + 3]);
243
+ if (parent >= 0) {
244
+ const x0 = Math.floor(a[n - 4]);
245
+ const y0 = Math.floor(a[n - 3]);
246
+ belowOf(
247
+ scene,
248
+ scene.geometry[i],
249
+ layer,
250
+ parent,
251
+ x0,
252
+ y0,
253
+ Math.ceil(a[n - 2]) - x0,
254
+ Math.ceil(a[n - 1]) - y0,
255
+ );
256
+ }
257
+
192
258
  areas.top = n - 4;
193
259
  cuts.top = c;
194
260
  if (scene.geometry[i] === ALPHA_MASK) {
@@ -234,6 +300,7 @@ export function measureLayers(
234
300
  layer * 4,
235
301
  scene.wholes[layer],
236
302
  scene.objects,
303
+ scene.solids[layer] === 1,
237
304
  );
238
305
  areas.top = n - 4;
239
306
  const r = scene.layers;
@@ -242,6 +309,10 @@ export function measureLayers(
242
309
  unionTop(r[at], r[at + 1], r[at] + r[at + 2], r[at + 1] + r[at + 3]);
243
310
  }
244
311
 
312
+ if (parent >= 0) {
313
+ belowOf(scene, scene.geometry[i], layer, parent, r[at], r[at + 1], r[at + 2], r[at + 3]);
314
+ }
315
+
245
316
  break;
246
317
  }
247
318
 
@@ -251,6 +322,19 @@ export function measureLayers(
251
322
  const at = scene.geometry[i] * 4;
252
323
  if (r[at + 2] > 0 && r[at + 3] > 0) {
253
324
  unionTop(r[at], r[at + 1], r[at] + r[at + 2], r[at + 1] + r[at + 3]);
325
+ if (openTop > 0) {
326
+ const layer = scene.geometry[i];
327
+ belowOf(
328
+ scene,
329
+ scene.blend[i],
330
+ layer,
331
+ open[openTop - 1],
332
+ r[at],
333
+ r[at + 1],
334
+ r[at + 2],
335
+ r[at + 3],
336
+ );
337
+ }
254
338
  }
255
339
 
256
340
  break;
@@ -268,7 +352,12 @@ export function measureLayers(
268
352
  * padded; nothing where it has no size then; its edges snapped to the
269
353
  * pixels they lie on. Into `out` from `to`: x, y, width, height, of no
270
354
  * size for nothing. A whole layer's object, uncut, goes into `objects` at
271
- * `to` too (SceneList.objects).
355
+ * `to` too (SceneList.objects). The bitmap of a display object's filters
356
+ * (`solid`, SceneList.solids) starts on the stage pixel its object's left
357
+ * edge lies on and is as many pixels wide as the object, rounded up, as
358
+ * adl's is: a square of 10 at x + 0.3 covers x to x + 10, one of 10.9 at x
359
+ * covers x to x + 11. Padded by the reach, a pixel at least, its edge
360
+ * texels are clear, which the filters' clamped reads take past it.
272
361
  */
273
362
  export function filteredRect(
274
363
  area: ArrayLike<number>,
@@ -279,6 +368,7 @@ export function filteredRect(
279
368
  to: number,
280
369
  whole = NO_WHOLE,
281
370
  objects: Float64Array | null = null,
371
+ solid = false,
282
372
  ): void {
283
373
  const res = target.resolution;
284
374
  let x0 = area[at] / res;
@@ -319,6 +409,17 @@ export function filteredRect(
319
409
  y0 = Math.max(oy0, 0) / res - pad;
320
410
  x1 = Math.min(ox1, target.width) / res + pad;
321
411
  y1 = Math.min(oy1, target.height) / res + pad;
412
+ } else if (solid) {
413
+ // In stage pixels, whatever the stage's scale; cut to the target only after.
414
+ const k = target.scale;
415
+ const sx = Math.floor(area[at] / k + TEXEL_SNAP);
416
+ const sy = Math.floor(area[at + 1] / k + TEXEL_SNAP);
417
+ const sw = Math.ceil((area[at + 2] - area[at]) / k - TEXEL_SNAP);
418
+ const sh = Math.ceil((area[at + 3] - area[at + 1]) / k - TEXEL_SNAP);
419
+ x0 = Math.max(sx * k, 0) / res - pad;
420
+ y0 = Math.max(sy * k, 0) / res - pad;
421
+ x1 = Math.min((sx + sw) * k, target.width) / res + pad;
422
+ y1 = Math.min((sy + sh) * k, target.height) / res + pad;
322
423
  } else {
323
424
  // Each edge moved in alone, as Bounds.fitBounds does: an area off the target turns inside out,
324
425
  // and its padding may yet give it a size.
@@ -453,6 +554,150 @@ function placeDraw(
453
554
  stack[n - 1] = Math.max(stack[n - 1], ty + Math.max(b * x0, b * x1) + Math.max(d * y0, d * y1));
454
555
  }
455
556
 
557
+ /**
558
+ * How what is drawn in mode `mode` over a fresh layer's pixel nothing drew
559
+ * reads to what is drawn over it later (SceneList.below).
560
+ */
561
+ function itemReads(mode: number): number {
562
+ switch (mode) {
563
+ case INVERT:
564
+ return BELOW_INVERTED;
565
+ case SUBTRACT:
566
+ return BELOW_SUBTRACTED;
567
+ case ERASE:
568
+ case ALPHA:
569
+ return BELOW_CLEARED;
570
+ default:
571
+ return 0;
572
+ }
573
+ }
574
+
575
+ /**
576
+ * An item drawn into the open fresh layer whose items begin at `from`, the
577
+ * last, over the area from `left`, `top` to `right`, `bottom` in target
578
+ * pixels, read as `reads` says (itemReads): taken in with the last where
579
+ * both read as they are stored, as only items that read otherwise tell
580
+ * blends anything (belowOf); all its items taken as one so read past
581
+ * MOST_ITEMS.
582
+ */
583
+ function addItem(
584
+ from: number,
585
+ left: number,
586
+ top: number,
587
+ right: number,
588
+ bottom: number,
589
+ reads: number,
590
+ ): void {
591
+ if (!(right > left && bottom > top)) {
592
+ return;
593
+ }
594
+
595
+ const x0 = Math.floor(left) - ITEM_SLACK;
596
+ const y0 = Math.floor(top) - ITEM_SLACK;
597
+ const x1 = Math.ceil(right) + ITEM_SLACK;
598
+ const y1 = Math.ceil(bottom) + ITEM_SLACK;
599
+ const last = itemTop - 5;
600
+ if (reads === 0 && last >= from && items[last + 4] === 0) {
601
+ items[last] = Math.min(items[last], x0);
602
+ items[last + 1] = Math.min(items[last + 1], y0);
603
+ items[last + 2] = Math.max(items[last + 2], x1);
604
+ items[last + 3] = Math.max(items[last + 3], y1);
605
+ return;
606
+ }
607
+
608
+ if (itemTop - from >= MOST_ITEMS * 5) {
609
+ for (let at = from + 5; at < itemTop; at += 5) {
610
+ items[from] = Math.min(items[from], items[at]);
611
+ items[from + 1] = Math.min(items[from + 1], items[at + 1]);
612
+ items[from + 2] = Math.max(items[from + 2], items[at + 2]);
613
+ items[from + 3] = Math.max(items[from + 3], items[at + 3]);
614
+ }
615
+
616
+ items[from + 4] = 0;
617
+ itemTop = from + 5;
618
+ addItem(from, left, top, right, bottom, 0);
619
+ return;
620
+ }
621
+
622
+ if (itemTop + 5 > items.length) {
623
+ const grown = new Float64Array(items.length * 2);
624
+ grown.set(items);
625
+ items = grown;
626
+ }
627
+
628
+ items[itemTop] = x0;
629
+ items[itemTop + 1] = y0;
630
+ items[itemTop + 2] = x1;
631
+ items[itemTop + 3] = y1;
632
+ items[itemTop + 4] = reads;
633
+ itemTop += 5;
634
+ }
635
+
636
+ /**
637
+ * The layer `layer`, of blend mode `mode`, ended or a kept output's, in
638
+ * layer `parent`, over the whole pixels from `x`, `y`, `width` by `height`
639
+ * that it may touch: where `parent` is fresh, how what lies under it there
640
+ * reads (SceneList.below), and it an item of `parent`'s. adl shows a pixel
641
+ * one object alone drew in a fresh layer as the object is, but blends what
642
+ * is drawn over it later with what the object's mode would have left over
643
+ * nothing. A pixel's own colour cannot tell which, so a blend is told from
644
+ * what was drawn before it: where all under it are of one mode that reads
645
+ * otherwise than it is stored, and none of them meets another there, each
646
+ * pixel under it that was drawn was drawn by one of them alone. Anything
647
+ * else under it, and it reads them as they are stored.
648
+ */
649
+ function belowOf(
650
+ scene: SceneList,
651
+ mode: number,
652
+ layer: number,
653
+ parent: number,
654
+ x0: number,
655
+ y0: number,
656
+ width: number,
657
+ height: number,
658
+ ): void {
659
+ if (scene.fresh[parent] !== 1 || mode === ALPHA_MASK || !(width > 0 && height > 0)) {
660
+ return;
661
+ }
662
+
663
+ const x1 = x0 + width;
664
+ const y1 = y0 + height;
665
+ const from = itemFrom[openTop - 1];
666
+ let reads = 0;
667
+ if (mode < KERNEL) {
668
+ for (let a = from; a < itemTop; a += 5) {
669
+ if (!(items[a] < x1 && x0 < items[a + 2] && items[a + 1] < y1 && y0 < items[a + 3])) {
670
+ continue;
671
+ }
672
+
673
+ if (items[a + 4] === 0 || (reads !== 0 && items[a + 4] !== reads)) {
674
+ reads = 0;
675
+ break;
676
+ }
677
+
678
+ reads = items[a + 4];
679
+ for (let b = from; b < a; b += 5) {
680
+ if (
681
+ items[b + 4] !== 0 &&
682
+ Math.max(items[a], items[b], x0) < Math.min(items[a + 2], items[b + 2], x1) &&
683
+ Math.max(items[a + 1], items[b + 1], y0) < Math.min(items[a + 3], items[b + 3], y1)
684
+ ) {
685
+ reads = -1;
686
+ break;
687
+ }
688
+ }
689
+
690
+ if (reads < 0) {
691
+ reads = 0;
692
+ break;
693
+ }
694
+ }
695
+ }
696
+
697
+ scene.below[layer] = reads;
698
+ addItem(from, x0, y0, x1, y1, itemReads(mode));
699
+ }
700
+
456
701
  /** The open area on top grown to take in the rectangle. */
457
702
  function unionTop(left: number, top: number, right: number, bottom: number): void {
458
703
  const a = areas.data;
package/src/plan.ts CHANGED
@@ -6,8 +6,11 @@ import {
6
6
  HELD,
7
7
  HOLD_POP,
8
8
  HOLD_PUSH,
9
+ LAYER_FILTERED,
9
10
  LAYER_POP,
10
11
  LAYER_PUSH,
12
+ LAYER_SOLID,
13
+ modeWithBelow,
11
14
  RUN,
12
15
  STENCIL_POP,
13
16
  STENCIL_PUSH,
@@ -135,7 +138,16 @@ function emit(scene: SceneList, from: number, to: number, out: Commands, stage:
135
138
  close(i);
136
139
  if (heldAt[i] >= 0) {
137
140
  const end = layerEnd(scene, i);
138
- out.push(HELD, heldAt[i], scene.geometry[end], scene.row[end]);
141
+ out.push(
142
+ HELD,
143
+ heldAt[i],
144
+ modeWithBelow(
145
+ scene.geometry[end],
146
+ scene.below[scene.geometry[i]],
147
+ scene.solids[scene.geometry[i]] === 1,
148
+ ),
149
+ scene.row[end],
150
+ );
139
151
  if (stage) {
140
152
  noteClearing(out, scene.geometry[end]);
141
153
  }
@@ -156,7 +168,12 @@ function emit(scene: SceneList, from: number, to: number, out: Commands, stage:
156
168
  out.push(
157
169
  LAYER_PUSH,
158
170
  layerParams(out, scene, scene.geometry[i]),
159
- scene.pads[scene.geometry[i]] >= 0 ? 1 : scene.groups[scene.geometry[i]] ? 2 : 0,
171
+ scene.pads[scene.geometry[i]] >= 0
172
+ ? LAYER_FILTERED | (scene.solids[scene.geometry[i]] ? LAYER_SOLID : 0)
173
+ : scene.groups[scene.geometry[i]]
174
+ ? 2
175
+ : 0,
176
+ scene.fresh[scene.geometry[i]] | (scene.below[scene.geometry[i]] << 1),
160
177
  );
161
178
  break;
162
179
  }
@@ -177,7 +194,11 @@ function emit(scene: SceneList, from: number, to: number, out: Commands, stage:
177
194
  out.push(
178
195
  COMPOSITE,
179
196
  out.param5(r[at], r[at + 1], r[at + 2], r[at + 3], scene.texture[i]),
180
- scene.blend[i],
197
+ modeWithBelow(
198
+ scene.blend[i],
199
+ scene.below[scene.geometry[i]],
200
+ scene.solids[scene.geometry[i]] === 1,
201
+ ),
181
202
  scene.row[i],
182
203
  );
183
204
  noteClearing(out, scene.blend[i]);
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
@@ -184,6 +196,28 @@ export class SceneList {
184
196
  * keeps a bitmap of, which it blends as one picture.
185
197
  */
186
198
  groups = new Uint8Array(16);
199
+ /**
200
+ * Whether each layer is one Flash draws for its blend mode alone, of no
201
+ * filters, alpha mask or bitmap of its own: there a pixel nothing in it
202
+ * has drawn takes what is blended onto it as it is, in any mode
203
+ * (programs.ts, BLEND_FRONT).
204
+ */
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);
187
221
  /**
188
222
  * Each whole layer's object, Flash's bitmap of it, uncut by the target:
189
223
  * x, y, width and height in target pixels, as measureLayers finds it.
@@ -291,13 +325,14 @@ export class SceneList {
291
325
  /**
292
326
  * A layer begun, padded for its filters (or -1), over whole units as
293
327
  * `whole` says (wholes), drawn as a layer whatever it holds where
294
- * `grouped` (groups), its rectangle none till measured (layerRect): its
295
- * number.
328
+ * `grouped` (groups), drawn for its blend mode alone where `fresh`
329
+ * (fresh), its rectangle none till measured (layerRect): its number.
296
330
  */
297
- layerBegin(padding = -1, keep = -1, whole = NO_WHOLE, grouped = false): number {
331
+ layerBegin(padding = -1, keep = -1, whole = NO_WHOLE, grouped = false, fresh = false): number {
298
332
  const layer = this.newLayer(padding, keep);
299
333
  this.wholes[layer] = whole;
300
334
  this.groups[layer] = grouped ? 1 : 0;
335
+ this.fresh[layer] = fresh ? 1 : 0;
301
336
  this.push(LAYER_BEGIN, layer, 0, 0, -1, 0);
302
337
  this.open.push(layer);
303
338
  return layer;
@@ -305,7 +340,8 @@ export class SceneList {
305
340
 
306
341
  /**
307
342
  * Kept output `keep` composited again over the rectangle x y width height
308
- * 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).
309
345
  */
310
346
  keptLayer(
311
347
  keep: number,
@@ -315,8 +351,10 @@ export class SceneList {
315
351
  y: number,
316
352
  width: number,
317
353
  height: number,
354
+ solid = false,
318
355
  ): void {
319
356
  const layer = this.newLayer(-1, keep);
357
+ this.solids[layer] = solid ? 1 : 0;
320
358
  this.layerRect(layer, x, y, width, height);
321
359
  this.push(KEPT, layer, row, mode, keep, 0);
322
360
  }
@@ -328,6 +366,9 @@ export class SceneList {
328
366
  this.pads = grow(this.pads);
329
367
  this.wholes = grow(this.wholes);
330
368
  this.groups = grow(this.groups);
369
+ this.fresh = grow(this.fresh);
370
+ this.below = grow(this.below);
371
+ this.solids = grow(this.solids);
331
372
  const objects = new Float64Array(this.objects.length * 2);
332
373
  objects.set(this.objects);
333
374
  this.objects = objects;
@@ -341,15 +382,23 @@ export class SceneList {
341
382
  this.pads[layer] = padding;
342
383
  this.wholes[layer] = NO_WHOLE;
343
384
  this.groups[layer] = 0;
385
+ this.fresh[layer] = 0;
386
+ this.below[layer] = 0;
387
+ this.solids[layer] = 0;
344
388
  this.keeps[layer] = keep;
345
389
  this.parents[layer] = this.open.at(-1) ?? -1;
346
390
  this.layerRect(layer, 0, 0, 0, 0);
347
391
  return layer;
348
392
  }
349
393
 
350
- /** Filtered layer `layer` padded by `padding` after all, as its filters were fitted once it was drawn. */
351
- 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 {
352
400
  this.pads[layer] = padding;
401
+ this.solids[layer] = solid ? 1 : 0;
353
402
  }
354
403
 
355
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
+ }