@swf2es/render 0.1.42 → 0.1.44

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 (69) 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/filter-shaders.d.ts +2 -2
  6. package/dist/filter-shaders.d.ts.map +1 -1
  7. package/dist/filter-shaders.js +7 -4
  8. package/dist/filter-shaders.js.map +1 -1
  9. package/dist/index.d.ts +2 -2
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +2 -2
  12. package/dist/index.js.map +1 -1
  13. package/dist/measure.d.ts +7 -2
  14. package/dist/measure.d.ts.map +1 -1
  15. package/dist/measure.js +189 -5
  16. package/dist/measure.js.map +1 -1
  17. package/dist/plan.d.ts.map +1 -1
  18. package/dist/plan.js +8 -4
  19. package/dist/plan.js.map +1 -1
  20. package/dist/registry.d.ts +12 -0
  21. package/dist/registry.d.ts.map +1 -1
  22. package/dist/registry.js +19 -0
  23. package/dist/registry.js.map +1 -1
  24. package/dist/scene.d.ts +35 -4
  25. package/dist/scene.d.ts.map +1 -1
  26. package/dist/scene.js +41 -4
  27. package/dist/scene.js.map +1 -1
  28. package/dist/webgl/backdrops.d.ts +60 -0
  29. package/dist/webgl/backdrops.d.ts.map +1 -0
  30. package/dist/webgl/backdrops.js +130 -0
  31. package/dist/webgl/backdrops.js.map +1 -0
  32. package/dist/webgl/backend.d.ts +31 -1
  33. package/dist/webgl/backend.d.ts.map +1 -1
  34. package/dist/webgl/backend.js +140 -31
  35. package/dist/webgl/backend.js.map +1 -1
  36. package/dist/webgl/composite.d.ts +29 -2
  37. package/dist/webgl/composite.d.ts.map +1 -1
  38. package/dist/webgl/composite.js +96 -27
  39. package/dist/webgl/composite.js.map +1 -1
  40. package/dist/webgl/draws.d.ts +5 -3
  41. package/dist/webgl/draws.d.ts.map +1 -1
  42. package/dist/webgl/draws.js +7 -5
  43. package/dist/webgl/draws.js.map +1 -1
  44. package/dist/webgl/holds.d.ts +5 -2
  45. package/dist/webgl/holds.d.ts.map +1 -1
  46. package/dist/webgl/holds.js +9 -4
  47. package/dist/webgl/holds.js.map +1 -1
  48. package/dist/webgl/programs.d.ts +11 -0
  49. package/dist/webgl/programs.d.ts.map +1 -1
  50. package/dist/webgl/programs.js +42 -6
  51. package/dist/webgl/programs.js.map +1 -1
  52. package/dist/webgl/targets.d.ts +4 -0
  53. package/dist/webgl/targets.d.ts.map +1 -1
  54. package/dist/webgl/targets.js.map +1 -1
  55. package/package.json +2 -2
  56. package/src/commands.ts +47 -5
  57. package/src/filter-shaders.ts +7 -4
  58. package/src/index.ts +9 -0
  59. package/src/measure.ts +247 -2
  60. package/src/plan.ts +24 -4
  61. package/src/registry.ts +21 -0
  62. package/src/scene.ts +42 -3
  63. package/src/webgl/backdrops.ts +150 -0
  64. package/src/webgl/backend.ts +175 -34
  65. package/src/webgl/composite.ts +120 -48
  66. package/src/webgl/draws.ts +10 -3
  67. package/src/webgl/holds.ts +11 -3
  68. package/src/webgl/programs.ts +46 -5
  69. package/src/webgl/targets.ts +4 -0
@@ -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,19 @@ 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();
124
+ /**
125
+ * The blends drawn direct that shareCopy's look-ahead found, from command
126
+ * `aheadFrom` to before `aheadTo`, one every three commands: where each
127
+ * lies on its target (Compositor.cover), four numbers each in aheadPlaces
128
+ * (x and y in its framebuffer, width and height, of no size off it). As
129
+ * nothing draws between them but they, blendDirect takes these rather
130
+ * than finding them again. This frame's commands alone (execute).
131
+ */
132
+ private aheadFrom = 0;
133
+ private aheadTo = 0;
134
+ private readonly aheadPlaces: number[] = [];
116
135
  /** The mask counts of the targets below the layers drawn into now, two numbers each. */
117
136
  private readonly below: number[] = [];
118
137
  /** The program `compositeOver` draws with, linked ahead (programs); null till then and after a lost context. */
@@ -411,6 +430,9 @@ export class WebGLBackend {
411
430
  opaque: commands.opaqueStage,
412
431
  };
413
432
  this.layers.length = 0;
433
+ this.backdrops.begin(false);
434
+ this.aheadFrom = 0;
435
+ this.aheadTo = 0;
414
436
  this.draws.drawInto(canvas, target);
415
437
 
416
438
  this.draws.forgetRow();
@@ -482,7 +504,7 @@ export class WebGLBackend {
482
504
  const from = ops[at + 1];
483
505
  const width = p[from + 2];
484
506
  const height = p[from + 3];
485
- const filtered = ops[at + 2] === 1;
507
+ const filtered = (ops[at + 2] & LAYER_FILTERED) !== 0;
486
508
  // A filtered one of the size its filters' targets take for it, which they read alike.
487
509
  const layer = this.pool.takeLayer(width, height, filtered);
488
510
  const open: Open = {
@@ -493,7 +515,9 @@ export class WebGLBackend {
493
515
  width,
494
516
  height,
495
517
  object: objectOf(p, from + 4),
496
- fresh: ops[at + 3] === 1,
518
+ fresh: (ops[at + 3] & 1) === 1,
519
+ below: ops[at + 3] >> 1,
520
+ solid: (ops[at + 2] & LAYER_SOLID) !== 0,
497
521
  };
498
522
  this.layers.push(open);
499
523
  this.draws.drawInto(open, target);
@@ -563,12 +587,14 @@ export class WebGLBackend {
563
587
  y: p[from + 1],
564
588
  width: p[from + 2],
565
589
  height: p[from + 3],
590
+ below: operandBelow(ops[at + 2]),
591
+ solid: operandSolid(ops[at + 2]),
566
592
  };
567
593
  this.draws.maskBy(masks);
568
594
  this.compositor.compositeLayer(
569
595
  kept,
570
596
  this.layers.at(-1) ?? canvas,
571
- ops[at + 2],
597
+ operandMode(ops[at + 2]),
572
598
  ops[at + 3],
573
599
  );
574
600
  }
@@ -1130,60 +1156,175 @@ export class WebGLBackend {
1130
1156
  * inside a pixel, each blended by what it covers there is as adl blends
1131
1157
  * them, not the pixel they make together, which a layer would blend. A
1132
1158
  * layer and its resolve and composite left out, what reads what is below
1133
- * copies it as the composite would. Whether it was.
1159
+ * copies it as the composite would, or shares the copy made for one
1160
+ * drawn direct before it (shareCopy). Whether it was.
1134
1161
  */
1135
1162
  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
- ) {
1163
+ const ahead = k - this.aheadFrom;
1164
+ const known = k < this.aheadTo && ahead >= 0 && ahead % 3 === 0;
1165
+ if (!known && !this.drawsDirect(commands, k, scene)) {
1151
1166
  return false;
1152
1167
  }
1153
1168
 
1154
- const p = commands.params;
1155
- const from = ops[at + 1];
1169
+ const ops = commands.ops;
1170
+ const pop = k * 4 + 8;
1156
1171
  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
1172
  const mode = ops[pop + 1];
1173
+ const below = ops[k * 4 + 3] >> 1;
1163
1174
  const compositor = this.compositor;
1164
- if (!compositor.cover(rect, under)) {
1175
+ let fx: number;
1176
+ let fy: number;
1177
+ let width: number;
1178
+ let height: number;
1179
+ if (known) {
1180
+ const places = this.aheadPlaces;
1181
+ const at = (ahead / 3) * 4;
1182
+ fx = places[at];
1183
+ fy = places[at + 1];
1184
+ width = places[at + 2];
1185
+ height = places[at + 3];
1186
+ } else {
1187
+ const place = compositor.place;
1188
+ compositor.cover(this.rectAt(commands, k), under);
1189
+ fx = place.fx;
1190
+ fy = place.fy;
1191
+ width = place.width;
1192
+ height = place.height;
1193
+ }
1194
+
1195
+ if (!(width > 0 && height > 0)) {
1165
1196
  return true;
1166
1197
  }
1167
1198
 
1168
1199
  // Drawn into already, as a kept output is composited (COMPOSITE); a copy of what is below
1169
1200
  // binds its framebuffer back.
1170
- const reads = readsBelow(mode, under.opaque);
1171
- const back = reads && compositor.copyBelow(under);
1201
+ const reads = readsBelow(mode, under.opaque, below);
1202
+ let box = -1;
1203
+ if (reads) {
1204
+ // Shared only while the copy is the last made, which nothing but blends sharing it come
1205
+ // between.
1206
+ const backdrops = this.backdrops;
1207
+ box = backdrops.copy === compositor.copies ? backdrops.boxFor(k) : -1;
1208
+ if (box < 0) {
1209
+ box = this.shareCopy(commands, k, scene, under, fx, fy, width, height);
1210
+ } else {
1211
+ compositor.bindBack();
1212
+ }
1213
+ }
1214
+
1172
1215
  this.draws.maskBy(this.masks);
1173
- const place = compositor.place;
1174
1216
  // Kept to the layer's rectangle, as the composite is, past which nothing below is copied.
1175
- this.state.scissor([place.fx, place.fy, place.width, place.height]);
1217
+ this.state.scissor([fx, fy, width, height]);
1176
1218
  this.draws.drawBlended(
1177
1219
  scene,
1178
- ops[run + 1],
1220
+ ops[k * 4 + 5],
1179
1221
  mode,
1180
1222
  reads,
1181
1223
  under.fresh,
1224
+ below,
1182
1225
  ops[pop + 2],
1183
- back ? place.backX : 0,
1184
- back ? place.backY : 0,
1226
+ reads ? compositor.offsets[box * 2] : 0,
1227
+ reads ? compositor.offsets[box * 2 + 1] : 0,
1185
1228
  );
1186
1229
  this.state.scissor(null);
1187
1230
  return true;
1188
1231
  }
1232
+
1233
+ /**
1234
+ * Whether the layer pushed at command `k` may be drawn straight onto the
1235
+ * target below (blendDirect): unfiltered, not kept, one run of a flat
1236
+ * geometry alone drawn normally in it, in a mode that blendsDirect.
1237
+ */
1238
+ private drawsDirect(commands: Commands, k: number, scene: SceneList): boolean {
1239
+ const ops = commands.ops;
1240
+ const at = k * 4;
1241
+ const run = at + 4;
1242
+ const pop = at + 8;
1243
+ return (
1244
+ k + 2 < commands.length &&
1245
+ ops[at] === LAYER_PUSH &&
1246
+ ops[at + 2] === 0 &&
1247
+ ops[run] === RUN &&
1248
+ ops[run + 2] === 1 &&
1249
+ ops[run + 3] === NORMAL &&
1250
+ ops[pop] === LAYER_POP &&
1251
+ ops[pop + 3] < 0 &&
1252
+ blendsDirect(ops[pop + 1]) &&
1253
+ this.draws.blendable(scene, ops[run + 1])
1254
+ );
1255
+ }
1256
+
1257
+ /** The rectangle of the layer pushed at command `k`, into rect. */
1258
+ private rectAt(
1259
+ commands: Commands,
1260
+ k: number,
1261
+ ): { x: number; y: number; width: number; height: number } {
1262
+ const p = commands.params;
1263
+ const from = commands.ops[k * 4 + 1];
1264
+ const rect = this.rect;
1265
+ rect.x = p[from];
1266
+ rect.y = p[from + 1];
1267
+ rect.width = p[from + 2];
1268
+ rect.height = p[from + 3];
1269
+ return rect;
1270
+ }
1271
+
1272
+ /**
1273
+ * What is below the blend drawn direct at command `k`, which reads it and
1274
+ * lies at `fx`, `fy`, `width` by `height` of its target's framebuffer,
1275
+ * copied once for it and for the blends drawn direct right after it that
1276
+ * may share the copy (Backdrops): the box that holds its rectangle. What
1277
+ * the look-ahead finds of those blends is kept for them (aheadPlaces).
1278
+ */
1279
+ private shareCopy(
1280
+ commands: Commands,
1281
+ k: number,
1282
+ scene: SceneList,
1283
+ under: Open,
1284
+ fx: number,
1285
+ fy: number,
1286
+ width: number,
1287
+ height: number,
1288
+ ): number {
1289
+ const compositor = this.compositor;
1290
+ const backdrops = this.backdrops;
1291
+ const place = compositor.place;
1292
+ const places = this.aheadPlaces;
1293
+ backdrops.begin(compositor.copiesWhereLying(under));
1294
+ // Drawn direct and placed already (blendDirect), and the first offered always takes the copy.
1295
+ backdrops.offer(k, fx, fy, width, height, true);
1296
+ places[0] = fx;
1297
+ places[1] = fy;
1298
+ places[2] = width;
1299
+ places[3] = height;
1300
+ let n = 1;
1301
+
1302
+ for (let j = k + 3; j < commands.length && this.drawsDirect(commands, j, scene); j += 3) {
1303
+ const lies = compositor.cover(this.rectAt(commands, j), under);
1304
+ const at = n++ * 4;
1305
+ places[at] = place.fx;
1306
+ places[at + 1] = place.fy;
1307
+ places[at + 2] = place.width;
1308
+ places[at + 3] = place.height;
1309
+ if (
1310
+ lies &&
1311
+ !backdrops.offer(
1312
+ j,
1313
+ place.fx,
1314
+ place.fy,
1315
+ place.width,
1316
+ place.height,
1317
+ readsBelow(commands.ops[j * 4 + 9], under.opaque, commands.ops[j * 4 + 3] >> 1),
1318
+ )
1319
+ ) {
1320
+ break;
1321
+ }
1322
+ }
1323
+
1324
+ this.aheadFrom = k;
1325
+ this.aheadTo = k + n * 3;
1326
+ compositor.copyBoxes(under, backdrops.boxes, backdrops.boxCount);
1327
+ backdrops.copy = compositor.copies;
1328
+ return backdrops.boxFor(k);
1329
+ }
1189
1330
  }
@@ -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
  /**