@swf2es/render 0.1.47 → 0.1.48

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 (49) hide show
  1. package/dist/backend.d.ts +113 -0
  2. package/dist/backend.d.ts.map +1 -0
  3. package/dist/backend.js +2 -0
  4. package/dist/backend.js.map +1 -0
  5. package/dist/filter-plan.d.ts +13 -1
  6. package/dist/filter-plan.d.ts.map +1 -1
  7. package/dist/filter-plan.js +39 -3
  8. package/dist/filter-plan.js.map +1 -1
  9. package/dist/index.d.ts +1 -0
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +0 -2
  12. package/dist/index.js.map +1 -1
  13. package/dist/measure.d.ts +11 -1
  14. package/dist/measure.d.ts.map +1 -1
  15. package/dist/measure.js +32 -1
  16. package/dist/measure.js.map +1 -1
  17. package/dist/webgl/backend.d.ts +34 -4
  18. package/dist/webgl/backend.d.ts.map +1 -1
  19. package/dist/webgl/backend.js +80 -11
  20. package/dist/webgl/backend.js.map +1 -1
  21. package/dist/webgl/draws.d.ts +2 -1
  22. package/dist/webgl/draws.d.ts.map +1 -1
  23. package/dist/webgl/draws.js.map +1 -1
  24. package/dist/webgl/filters.d.ts +15 -7
  25. package/dist/webgl/filters.d.ts.map +1 -1
  26. package/dist/webgl/filters.js +105 -39
  27. package/dist/webgl/filters.js.map +1 -1
  28. package/dist/webgl/holds.d.ts +2 -1
  29. package/dist/webgl/holds.d.ts.map +1 -1
  30. package/dist/webgl/holds.js +2 -2
  31. package/dist/webgl/holds.js.map +1 -1
  32. package/dist/webgl/precompile.d.ts +5 -3
  33. package/dist/webgl/precompile.d.ts.map +1 -1
  34. package/dist/webgl/precompile.js +10 -8
  35. package/dist/webgl/precompile.js.map +1 -1
  36. package/dist/webgl/targets.d.ts +0 -16
  37. package/dist/webgl/targets.d.ts.map +1 -1
  38. package/dist/webgl/targets.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/backend.ts +188 -0
  41. package/src/filter-plan.ts +45 -3
  42. package/src/index.ts +1 -0
  43. package/src/measure.ts +45 -1
  44. package/src/webgl/backend.ts +100 -12
  45. package/src/webgl/draws.ts +2 -1
  46. package/src/webgl/filters.ts +125 -39
  47. package/src/webgl/holds.ts +3 -1
  48. package/src/webgl/precompile.ts +20 -11
  49. package/src/webgl/targets.ts +0 -17
package/src/measure.ts CHANGED
@@ -32,6 +32,13 @@ export interface MeasureTarget {
32
32
  height: number;
33
33
  scale: number;
34
34
  resolution: number;
35
+ /**
36
+ * Its pixels a side of a pixel its filters run at, where that is more
37
+ * than one: the samples of a draw into a bitmap, whose filtered layers
38
+ * are halved to the bitmap's pixels before their chains run. A filtered
39
+ * layer then covers whole such pixels, and is padded in them.
40
+ */
41
+ filterPixel?: number;
35
42
  }
36
43
 
37
44
  /** A hair, which keeps a padded edge on the pixel it lies on at any resolution. */
@@ -357,7 +364,10 @@ export function measureLayers(
357
364
  * edge lies on and is as many pixels wide as the object, rounded up, as
358
365
  * adl's is: a square of 10 at x + 0.3 covers x to x + 10, one of 10.9 at x
359
366
  * 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.
367
+ * texels are clear, which the filters' clamped reads take past it. On a
368
+ * target whose filters run at pixels of several of its own
369
+ * (MeasureTarget.filterPixel), all of it is in those, the rectangle whole
370
+ * ones.
361
371
  */
362
372
  export function filteredRect(
363
373
  area: ArrayLike<number>,
@@ -370,6 +380,36 @@ export function filteredRect(
370
380
  objects: Float64Array | null = null,
371
381
  solid = false,
372
382
  ): void {
383
+ const f = target.filterPixel ?? 1;
384
+ if (f > 1) {
385
+ // Measured in the pixels its filters run at, as a target of them would have it: then every
386
+ // edge lies on one, and the layer halves to them whole.
387
+ for (let k = 0; k < 4; k++) {
388
+ coarseArea[k] = area[at + k] / f;
389
+ }
390
+
391
+ coarse.width = target.width / f;
392
+ coarse.height = target.height / f;
393
+ coarse.scale = target.scale / f;
394
+ coarse.resolution = target.resolution;
395
+ filteredRect(coarseArea, 0, pad, coarse, out, to, whole, objects, solid);
396
+ // Its object's rectangle too, written where the area has a size.
397
+ const placed =
398
+ objects !== null &&
399
+ whole !== NO_WHOLE &&
400
+ coarseArea[2] > coarseArea[0] &&
401
+ coarseArea[3] > coarseArea[1];
402
+
403
+ for (let k = 0; k < 4; k++) {
404
+ out[to + k] *= f;
405
+ if (placed) {
406
+ objects[to + k] *= f;
407
+ }
408
+ }
409
+
410
+ return;
411
+ }
412
+
373
413
  const res = target.resolution;
374
414
  let x0 = area[at] / res;
375
415
  let y0 = area[at + 1] / res;
@@ -441,6 +481,10 @@ export function filteredRect(
441
481
  out[to + 3] = Math.ceil(y1 * res - TEXEL_SNAP) - top;
442
482
  }
443
483
 
484
+ /** A filtered layer's area and target in the pixels its filters run at (MeasureTarget.filterPixel). */
485
+ const coarseArea = new Float64Array(4);
486
+ const coarse: MeasureTarget = { width: 0, height: 0, scale: 1, resolution: 1 };
487
+
444
488
  /** What wholeBitmap gives, read before it is asked again. */
445
489
  const bitmap = [0, 0, 0, 0];
446
490
 
@@ -1,4 +1,5 @@
1
1
  import { GlState, linkProgram } from "@swf2es/context3d/device";
2
+ import type { Backend, Target } from "../backend.js";
2
3
  import {
3
4
  CLEAR,
4
5
  COMPOSITE,
@@ -37,9 +38,9 @@ import { ProgramQueue } from "./precompile.js";
37
38
  import { LAYER_UNIT, TEXTURE_UNIT } from "./programs.js";
38
39
  import { RampBuilder } from "./ramps.js";
39
40
  import { Stage } from "./stage.js";
40
- import { type Layer, type Open, objectOf, type Target, TargetPool } from "./targets.js";
41
+ import { type Layer, type Open, objectOf, TargetPool } from "./targets.js";
41
42
 
42
- export type { Target } from "./targets.js";
43
+ export type { Target } from "../backend.js";
43
44
 
44
45
  /**
45
46
  * Executes a frame's commands on a WebGL 2 context it owns, through one
@@ -91,7 +92,7 @@ void main() {
91
92
  finalColor = vec4(o) / 255.0;
92
93
  }`;
93
94
 
94
- export class WebGLBackend {
95
+ export class WebGLBackend implements Backend {
95
96
  readonly gl: WebGL2RenderingContext;
96
97
  readonly state: GlState;
97
98
  private readonly pool: TargetPool;
@@ -150,6 +151,32 @@ export class WebGLBackend {
150
151
  private ready = false;
151
152
  /** The failure work was last given up on for, the context not flagged lost (resume). */
152
153
  private fault: unknown = null;
154
+ /** The largest texture side the context makes (textureLimit). */
155
+ private limit: number;
156
+ /** Whether the backend made its context's canvas, and loses the context with itself (onCanvas). */
157
+ private owned = false;
158
+ /** What is told of the context's coming back (onRestore). */
159
+ private restoredListener: (() => void) | null = null;
160
+
161
+ /**
162
+ * A backend on a WebGL 2 context of `canvas`, made with `attributes`, or
163
+ * of a canvas of its own, whose context it then loses with itself
164
+ * (destroy). Throws where the browser gives no WebGL 2.
165
+ */
166
+ static onCanvas(
167
+ canvas: HTMLCanvasElement | undefined,
168
+ attributes: WebGLContextAttributes,
169
+ ): WebGLBackend {
170
+ const gl = (canvas ?? document.createElement("canvas")).getContext("webgl2", attributes);
171
+ if (!gl) {
172
+ throw new Error("the renderer needs WebGL 2");
173
+ }
174
+
175
+ const backend = new WebGLBackend(gl);
176
+ backend.owned = !canvas;
177
+
178
+ return backend;
179
+ }
153
180
 
154
181
  constructor(gl: WebGL2RenderingContext) {
155
182
  this.gl = gl;
@@ -192,6 +219,57 @@ export class WebGLBackend {
192
219
  } catch (e) {
193
220
  this.gaveUp(e);
194
221
  }
222
+
223
+ // None of a context made lost, till it is restored.
224
+ this.limit = (gl.getParameter(gl.MAX_TEXTURE_SIZE) as number | null) ?? 0;
225
+
226
+ const canvas: EventTarget = gl.canvas;
227
+ canvas.addEventListener("webglcontextlost", this.contextLost);
228
+ canvas.addEventListener("webglcontextrestored", this.contextRestored);
229
+ }
230
+
231
+ /** A lost context is asked back, as Flash's player would have it, by preventing the default. */
232
+ private readonly contextLost = (event: Event) => {
233
+ event.preventDefault();
234
+ };
235
+
236
+ /** The context came back: told, for the backend to be restored (restore) by what holds it. */
237
+ private readonly contextRestored = () => {
238
+ this.restoredListener?.();
239
+ };
240
+
241
+ /** `listener` told when the context is restored, in place of any told before. */
242
+ onRestore(listener: () => void): void {
243
+ this.restoredListener = listener;
244
+ }
245
+
246
+ /** The canvas made `width` by `height` pixels, which its drawing buffer follows as it may. */
247
+ resize(width: number, height: number): void {
248
+ const canvas = this.gl.canvas;
249
+ canvas.width = width;
250
+ canvas.height = height;
251
+ }
252
+
253
+ /** Whether the context is lost, till the browser restores it. */
254
+ get lost(): boolean {
255
+ return this.gl.isContextLost();
256
+ }
257
+
258
+ /**
259
+ * The largest texture side the context makes: 0 for a context made lost,
260
+ * till it is restored, and the first context's after that.
261
+ */
262
+ get textureLimit(): number {
263
+ return this.limit;
264
+ }
265
+
266
+ /** The drawing buffer's size, which a browser may hold smaller than the canvas asks for. */
267
+ get targetWidth(): number {
268
+ return this.gl.drawingBufferWidth;
269
+ }
270
+
271
+ get targetHeight(): number {
272
+ return this.gl.drawingBufferHeight;
195
273
  }
196
274
 
197
275
  /**
@@ -260,16 +338,17 @@ export class WebGLBackend {
260
338
  // Lost again already: the atlas keeps the places and settling geometries of the context
261
339
  // before, naming ids Geometries.lost removed, till the next restore makes it anew (setUp).
262
340
  // Nothing reads them meanwhile, as no frame is drawn or planned while the context is lost.
263
- if (this.gl.isContextLost()) {
264
- return;
341
+ if (!this.gl.isContextLost()) {
342
+ try {
343
+ this.setUp();
344
+ this.made = this.ready = true;
345
+ } catch (e) {
346
+ this.gaveUp(e);
347
+ }
265
348
  }
266
349
 
267
- try {
268
- this.setUp();
269
- this.made = this.ready = true;
270
- } catch (e) {
271
- this.gaveUp(e);
272
- }
350
+ // Asked again only where the backend was made on a context lost already.
351
+ this.limit ||= (this.gl.getParameter(this.gl.MAX_TEXTURE_SIZE) as number | null) ?? 0;
273
352
  }
274
353
 
275
354
  /**
@@ -553,6 +632,7 @@ export class WebGLBackend {
553
632
  open.height,
554
633
  target.resolution,
555
634
  open.object,
635
+ target.filterPixel,
556
636
  );
557
637
  }
558
638
 
@@ -1129,8 +1209,13 @@ export class WebGLBackend {
1129
1209
  this.gpu.uploadOutlines(data, count, epoch);
1130
1210
  }
1131
1211
 
1132
- /** What the backend made on the context, deleted. */
1212
+ /** What the backend made on the context, deleted, and the context lost if its canvas is the backend's own. */
1133
1213
  destroy(): void {
1214
+ const canvas: EventTarget = this.gl.canvas;
1215
+ canvas.removeEventListener("webglcontextlost", this.contextLost);
1216
+ canvas.removeEventListener("webglcontextrestored", this.contextRestored);
1217
+ this.restoredListener = null;
1218
+
1134
1219
  this.draws.destroy();
1135
1220
  this.atlas.destroy();
1136
1221
  this.compositor.destroy();
@@ -1160,6 +1245,9 @@ export class WebGLBackend {
1160
1245
  this.ramps.destroy();
1161
1246
  this.gpu.destroy();
1162
1247
  this.state.forget();
1248
+ if (this.owned && !this.gl.isContextLost()) {
1249
+ this.gl.getExtension("WEBGL_lose_context")?.loseContext();
1250
+ }
1163
1251
  }
1164
1252
 
1165
1253
  /**
@@ -5,6 +5,7 @@ import {
5
5
  linkProgram,
6
6
  type PendingProgram,
7
7
  } from "@swf2es/context3d/device";
8
+ import type { Target } from "../backend.js";
8
9
  import { type Commands, RUN, STENCIL_POP, STENCIL_PUSH } from "../commands.js";
9
10
  import { FILL_SHADER, type Filters } from "../filters.js";
10
11
  import { fillKernel, kernelImagesFit } from "../kernel-shaders.js";
@@ -52,7 +53,7 @@ import {
52
53
  TABLE_UNIT,
53
54
  TEXTURE_UNIT,
54
55
  } from "./programs.js";
55
- import { framebufferOf, type Open, type Target, type TargetPool } from "./targets.js";
56
+ import { framebufferOf, type Open, type TargetPool } from "./targets.js";
56
57
 
57
58
  /**
58
59
  * A bit of a run's units (runUnits) above theirs: one of its fills is a
@@ -1,5 +1,6 @@
1
1
  import { type GlState, linkProgram } from "@swf2es/context3d/device";
2
2
  import {
3
+ BACK_PASS,
3
4
  BEVEL_PASS,
4
5
  BOX_PASS,
5
6
  CONVOLUTION_PASS,
@@ -9,6 +10,7 @@ import {
9
10
  FLOATS,
10
11
  GLOW_PASS,
11
12
  GRADIENT_PASS,
13
+ HALVE_PASS,
12
14
  IVEC4S,
13
15
  KERNEL_PASS,
14
16
  type KernelObject,
@@ -111,7 +113,7 @@ function withinRects(fragment: string): string {
111
113
  return reads.slice(0, end) + READS + reads.slice(end);
112
114
  }
113
115
 
114
- /** The input as it is, read where the vertex shader says: halved, at a corner four texels share. */
116
+ /** The input as it is, read where the vertex shader says. */
115
117
  const COPY = `in vec2 vTextureCoord;
116
118
  out vec4 finalColor;
117
119
  uniform sampler2D uTexture;
@@ -119,19 +121,71 @@ void main(void) {
119
121
  finalColor = texture(uTexture, vTextureCoord);
120
122
  }`;
121
123
 
122
- /** Every pass a chain may run, by name, and its fragment shader: all linked ahead (ProgramQueue). */
123
- const PASSES: readonly (readonly [string, string])[] = [
124
- ["copy", COPY],
125
- ["box", shaders.BOX],
126
- ["shrink", shaders.SHRINK],
127
- ["readBack", shaders.READ_BACK],
128
- ["glow", shaders.GLOW],
129
- ["bevel", shaders.BEVEL],
130
- ["gradient", shaders.GRADIENT],
131
- ["colorMatrix", shaders.MATRIX],
132
- ["convolution", shaders.CONVOLUTION],
133
- ["displacement", shaders.DISPLACE],
134
- ];
124
+ /**
125
+ * The input at half its size each way (halve, HALVE_PASS): each texel the
126
+ * mean of the four it covers, their premultiplied bytes summed and a half
127
+ * rounded up, floor((sum + 2) / 4), as the CPU rasterizer will halve.
128
+ * Fetched texel by texel within the input's rectangle, a texel past its
129
+ * edge the edge's: one linear read at the corner the four share weighs them
130
+ * by the float the corner's coordinate comes to, which the texture's size
131
+ * and the driver decide, and a sum that is a half then rounds either way.
132
+ * Each texel's bytes are rounded to whole numbers before they are summed,
133
+ * which is exact at any precision a sampler reads at, 16-bit floats too:
134
+ * summed as floats at a sampler's own, low, four texels came out a level
135
+ * off on an RTX 4060. The sampler's high precision is kept besides.
136
+ */
137
+ const HALVE = `precision highp int;
138
+ out vec4 finalColor;
139
+ uniform highp sampler2D uTexture;
140
+ uniform vec4 uTextureRect;
141
+ uniform vec2 uOutputAt;
142
+ ivec4 bytes(ivec2 at) {
143
+ return ivec4(round(texelFetch(uTexture, at, 0) * 255.0));
144
+ }
145
+ void main(void) {
146
+ ivec2 first = ivec2(uTextureRect.xy);
147
+ ivec2 last = first + ivec2(uTextureRect.zw) - 1;
148
+ ivec2 at = first + 2 * ivec2(gl_FragCoord.xy - uOutputAt);
149
+ ivec4 sum = bytes(min(at, last)) + bytes(min(at + ivec2(1, 0), last))
150
+ + bytes(min(at + ivec2(0, 1), last)) + bytes(min(at + 1, last));
151
+ finalColor = vec4((sum + 2) >> 2) / 255.0;
152
+ }`;
153
+
154
+ /**
155
+ * The input over a frame a whole number of times its size (BACK_PASS): each
156
+ * texel that of the one it lies in, read at its centre, which a linear read
157
+ * gives as it is.
158
+ */
159
+ const BACK = `in vec2 vTextureCoord;
160
+ out vec4 finalColor;
161
+ uniform sampler2D uTexture;
162
+ uniform vec4 uInputPixel;
163
+ void main(void) {
164
+ vec2 texel = floor(vTextureCoord * uInputPixel.xy) + 0.5;
165
+ finalColor = texture(uTexture, texel * uInputPixel.zw);
166
+ }`;
167
+
168
+ /**
169
+ * Every pass a chain may run, by name, and its fragment shader, reading its
170
+ * input within its rectangle: all linked ahead (ProgramQueue).
171
+ */
172
+ const PASSES = {
173
+ copy: withinRects(COPY),
174
+ // Its fetches are within the rectangle as written.
175
+ halve: HALVE,
176
+ back: withinRects(BACK),
177
+ box: withinRects(shaders.BOX),
178
+ shrink: withinRects(shaders.SHRINK),
179
+ readBack: withinRects(shaders.READ_BACK),
180
+ glow: withinRects(shaders.GLOW),
181
+ bevel: withinRects(shaders.BEVEL),
182
+ gradient: withinRects(shaders.GRADIENT),
183
+ colorMatrix: withinRects(shaders.MATRIX),
184
+ convolution: withinRects(shaders.CONVOLUTION),
185
+ displacement: withinRects(shaders.DISPLACE),
186
+ } as const;
187
+
188
+ type PassName = keyof typeof PASSES;
135
189
 
136
190
  interface Pass {
137
191
  program: WebGLProgram;
@@ -152,7 +206,7 @@ export class FilterRunner<T extends FilterTarget> {
152
206
  private readonly gl: WebGL2RenderingContext;
153
207
  private readonly state: GlState;
154
208
  private readonly host: FilterHost<T>;
155
- private readonly passes = new Map<string, Pass>();
209
+ private readonly passes = new Map<PassName, Pass>();
156
210
  /** The kernel image units a kernel's pass has bound so far. */
157
211
  private samplers = 0;
158
212
  /** Whether a kernel's fragment shader links (planChain's `links`), linking it now if not yet. */
@@ -179,13 +233,14 @@ export class FilterRunner<T extends FilterTarget> {
179
233
  this.gl = gl;
180
234
  this.state = state;
181
235
  this.host = host;
182
- for (const [name, fragment] of PASSES) {
236
+ for (const name of Object.keys(PASSES) as PassName[]) {
183
237
  queue.add(
184
238
  `filter:${name}`,
185
239
  VERTEX,
186
- HEADER + withinRects(fragment),
240
+ HEADER + PASSES[name],
187
241
  [INPUT_UNIT, BLURRED_UNIT, EXTRA_UNIT],
188
242
  (program) => this.made(name, program),
243
+ () => this.warm(name),
189
244
  );
190
245
  }
191
246
  }
@@ -194,8 +249,8 @@ export class FilterRunner<T extends FilterTarget> {
194
249
  * Chain `chain` of `filters` run over `input`'s first `width` by `height`
195
250
  * texels: the target its last pass drew into, `input` itself where none
196
251
  * changed it. Every other target, `input` too where it was replaced, goes
197
- * back to the host. `object` is where the layer's object lies in it
198
- * (planChain).
252
+ * back to the host. `object` is where the layer's object lies in it, and
253
+ * `pixel` its texels a side to a pixel its filters run at (planChain).
199
254
  */
200
255
  run(
201
256
  filters: Filters,
@@ -205,22 +260,34 @@ export class FilterRunner<T extends FilterTarget> {
205
260
  height: number,
206
261
  resolution: number,
207
262
  object: KernelObject | null = null,
263
+ pixel = 1,
208
264
  ): T {
209
265
  const plan = this.plan;
210
- planChain(plan, filters, chain, width, height, resolution, this.hasTexture, this.links, object);
266
+ planChain(
267
+ plan,
268
+ filters,
269
+ chain,
270
+ width,
271
+ height,
272
+ resolution,
273
+ this.hasTexture,
274
+ this.links,
275
+ object,
276
+ pixel,
277
+ );
211
278
  return this.execute(plan, input);
212
279
  }
213
280
 
214
281
  /**
215
282
  * `input`'s first `width` by `height` texels at half the size each way,
216
- * each texel the mean of the four it covers, read once linearly at the
217
- * corner they share: a target the host lends, which the caller gives
218
- * back. Not a filter's pass, so at a texel a unit.
283
+ * each texel the mean of the four it covers, a half rounded up (HALVE): a
284
+ * target the host lends, which the caller gives back. Not a filter's
285
+ * pass, so at a texel a unit.
219
286
  */
220
287
  halve(input: T, width: number, height: number): T {
221
288
  this.resolution = 1;
222
289
  const out = this.host.take(width, height);
223
- this.draw(this.use("copy", COPY), input, width * 2, height * 2, out, width, height);
290
+ this.draw(this.use("halve"), input, width * 2, height * 2, out, width, height);
224
291
  return out;
225
292
  }
226
293
 
@@ -286,7 +353,7 @@ export class FilterRunner<T extends FilterTarget> {
286
353
  // A kernel that will not link was left out as the chain was planned (links).
287
354
  const program =
288
355
  pass.program === KERNEL_PASS
289
- ? (this.kernel(pass.kernel as string) ?? this.use("copy", COPY))
356
+ ? (this.kernel(pass.kernel as string) ?? this.use("copy"))
290
357
  : this.program(pass.program);
291
358
  this.samplers = 0;
292
359
  for (let u = pass.first; u < pass.end; u++) {
@@ -448,23 +515,27 @@ export class FilterRunner<T extends FilterTarget> {
448
515
  private program(id: number): Pass {
449
516
  switch (id) {
450
517
  case BOX_PASS:
451
- return this.use("box", shaders.BOX);
518
+ return this.use("box");
452
519
  case SHRINK_PASS:
453
- return this.use("shrink", shaders.SHRINK);
520
+ return this.use("shrink");
454
521
  case READ_BACK_PASS:
455
- return this.use("readBack", shaders.READ_BACK);
522
+ return this.use("readBack");
456
523
  case GLOW_PASS:
457
- return this.use("glow", shaders.GLOW);
524
+ return this.use("glow");
458
525
  case BEVEL_PASS:
459
- return this.use("bevel", shaders.BEVEL);
526
+ return this.use("bevel");
460
527
  case GRADIENT_PASS:
461
- return this.use("gradient", shaders.GRADIENT);
528
+ return this.use("gradient");
462
529
  case MATRIX_PASS:
463
- return this.use("colorMatrix", shaders.MATRIX);
530
+ return this.use("colorMatrix");
464
531
  case CONVOLUTION_PASS:
465
- return this.use("convolution", shaders.CONVOLUTION);
532
+ return this.use("convolution");
466
533
  case DISPLACE_PASS:
467
- return this.use("displacement", shaders.DISPLACE);
534
+ return this.use("displacement");
535
+ case HALVE_PASS:
536
+ return this.use("halve");
537
+ case BACK_PASS:
538
+ return this.use("back");
468
539
  }
469
540
 
470
541
  throw new Error(`no filter pass ${id}`);
@@ -518,7 +589,7 @@ export class FilterRunner<T extends FilterTarget> {
518
589
  this.set(pass, "uInputPixel", 4, input.width, input.height, 1 / input.width, 1 / input.height);
519
590
  this.set(pass, "uCoord", 2, readWidth / input.width, readHeight / input.height);
520
591
  this.set(pass, "uTextureRect", 4, input.x ?? 0, input.y ?? 0, input.width, input.height);
521
- // A kernel's alone: the others need not be told.
592
+ // A kernel's and the halving's alone: the others need not be told.
522
593
  if (this.at(pass, "uOutputAt")) {
523
594
  this.set(pass, "uOutputAt", 2, x, y);
524
595
  }
@@ -527,14 +598,14 @@ export class FilterRunner<T extends FilterTarget> {
527
598
  }
528
599
 
529
600
  /**
530
- * The pass of `fragment`, in use: linked ahead (ProgramQueue), its link
601
+ * Pass `name` of PASSES, in use: linked ahead (ProgramQueue), its link
531
602
  * finished now if it is not yet, or linked now where it would not link
532
603
  * ahead, which throws why.
533
604
  */
534
- private use(name: string, fragment: string): Pass {
605
+ private use(name: PassName): Pass {
535
606
  let pass = this.passes.get(name);
536
607
  if (!pass && !this.queue.take(`filter:${name}`)) {
537
- const program = linkProgram(this.gl, VERTEX, HEADER + withinRects(fragment));
608
+ const program = linkProgram(this.gl, VERTEX, HEADER + PASSES[name]);
538
609
  this.state.useProgram(program);
539
610
  this.made(name, program);
540
611
  }
@@ -544,8 +615,23 @@ export class FilterRunner<T extends FilterTarget> {
544
615
  return pass;
545
616
  }
546
617
 
618
+ /**
619
+ * Pass `name`, in use, told the texel its warming draw reads and the one
620
+ * it draws into (ProgramQueue), as a draw tells it its input's rectangle
621
+ * and its output's corner: the halving fetches its input's texels by
622
+ * them, and those of no draw yet, or of one since it was linked, are not
623
+ * the texel's.
624
+ */
625
+ private warm(name: PassName): void {
626
+ const pass = this.passes.get(name) as Pass;
627
+ this.set(pass, "uTextureRect", 4, 0, 0, 1, 1);
628
+ if (this.at(pass, "uOutputAt")) {
629
+ this.set(pass, "uOutputAt", 2, 0, 0);
630
+ }
631
+ }
632
+
547
633
  /** Pass `name` of `program`, linked and in use, its samplers told their units. */
548
- private made(name: string, program: WebGLProgram): void {
634
+ private made(name: PassName, program: WebGLProgram): void {
549
635
  const gl = this.gl;
550
636
  this.passes.set(name, { program, uniforms: new Map(), told: new Map() });
551
637
  gl.uniform1i(gl.getUniformLocation(program, "uTexture"), INPUT_UNIT);
@@ -1,4 +1,5 @@
1
1
  import type { GlState } from "@swf2es/context3d/device";
2
+ import type { Target } from "../backend.js";
2
3
  import {
3
4
  type Commands,
4
5
  HELD,
@@ -26,7 +27,6 @@ import {
26
27
  layerSide,
27
28
  type Open,
28
29
  objectOf,
29
- type Target,
30
30
  type TargetPool,
31
31
  } from "./targets.js";
32
32
 
@@ -257,6 +257,7 @@ export class HeldLayers {
257
257
  this.gpu.hasTexture,
258
258
  this.filters.links,
259
259
  hold.object,
260
+ target.filterPixel,
260
261
  );
261
262
  hold.targets.length = plan.slots;
262
263
  hold.targets.fill(null, 1);
@@ -612,6 +613,7 @@ export class HeldLayers {
612
613
  hold.height,
613
614
  target.resolution,
614
615
  hold.object,
616
+ target.filterPixel,
615
617
  );
616
618
  const index = this.addPage(output, true);
617
619
  hold.result = this.slotOf(hold, 0, index, 0, 0, output.width, output.height);
@@ -19,9 +19,18 @@ interface Job {
19
19
  /** The texture units its samplers read, 0 besides, which its warming draw binds a texel to. */
20
20
  units: readonly number[];
21
21
  made: (program: WebGLProgram) => void;
22
+ /** Told before its warming draw, with it in use, to set what that draw reads by; if any. */
23
+ warm: (() => void) | null;
22
24
  pending: PendingProgram | null;
23
25
  }
24
26
 
27
+ /** A program linked and not yet drawn with. */
28
+ interface Warming {
29
+ program: WebGLProgram;
30
+ units: readonly number[];
31
+ warm: (() => void) | null;
32
+ }
33
+
25
34
  /**
26
35
  * Programs linked and warmed in a frame, at most, without KHR_parallel_shader_compile: each a few
27
36
  * milliseconds, and a first draw may take a driver tens to build its pipeline.
@@ -50,7 +59,7 @@ export class ProgramQueue {
50
59
  /** Not begun, begun and not yet asked after, linked and not yet drawn with. */
51
60
  private readonly waiting: Job[] = [];
52
61
  private readonly begun: Job[] = [];
53
- private readonly warming: { program: WebGLProgram; units: readonly number[] }[] = [];
62
+ private readonly warming: Warming[] = [];
54
63
  /** The target of a texel the warming draws go into, and the texel their samplers read. */
55
64
  private framebuffer: WebGLFramebuffer | null = null;
56
65
  private target: WebGLTexture | null = null;
@@ -67,7 +76,9 @@ export class ProgramQueue {
67
76
  /**
68
77
  * Program `key` of GLSL `vertex` and `fragment` to be made ahead, `made`
69
78
  * told it once it is linked (step or take), with it in use. Its samplers
70
- * read `units`.
79
+ * read `units`. `warm` is called before its warming draw, with it in
80
+ * use, for a program that reads its input by uniforms: a frame may have
81
+ * drawn with it since it was linked, and left a draw's.
71
82
  */
72
83
  add(
73
84
  key: string,
@@ -75,8 +86,9 @@ export class ProgramQueue {
75
86
  fragment: string,
76
87
  units: readonly number[],
77
88
  made: (program: WebGLProgram) => void,
89
+ warm: (() => void) | null = null,
78
90
  ): void {
79
- this.waiting.push({ key, vertex, fragment, units, made, pending: null });
91
+ this.waiting.push({ key, vertex, fragment, units, made, warm, pending: null });
80
92
  }
81
93
 
82
94
  /** Whether any program is still to be linked or warmed. */
@@ -123,11 +135,7 @@ export class ProgramQueue {
123
135
  }
124
136
 
125
137
  for (let k = 0; k < WARMS_A_FRAME && this.warming.length > 0; k++) {
126
- const { program, units } = this.warming.shift() as {
127
- program: WebGLProgram;
128
- units: readonly number[];
129
- };
130
- this.warm(program, units);
138
+ this.warm(this.warming.shift() as Warming);
131
139
  }
132
140
  }
133
141
 
@@ -176,7 +184,7 @@ export class ProgramQueue {
176
184
  this.state.useProgram(program);
177
185
  job.made(program);
178
186
  if (warm) {
179
- this.warming.push({ program, units: job.units });
187
+ this.warming.push({ program, units: job.units, warm: job.warm });
180
188
  }
181
189
 
182
190
  return true;
@@ -185,9 +193,9 @@ export class ProgramQueue {
185
193
  /**
186
194
  * `program` drawn with once, a triangle into a target of a texel, its
187
195
  * samplers reading another, with no attributes, blending, stencil nor
188
- * scissor, as the filters' passes draw.
196
+ * scissor, as the filters' passes draw; its owner told first (add).
189
197
  */
190
- private warm(program: WebGLProgram, units: readonly number[]): void {
198
+ private warm({ program, units, warm }: Warming): void {
191
199
  const gl = this.gl;
192
200
  const state = this.state;
193
201
  // Bound again after, as the frame left it for what reads it.
@@ -207,6 +215,7 @@ export class ProgramQueue {
207
215
  state.setBlend(null);
208
216
  state.scissor(null);
209
217
  state.useProgram(program);
218
+ warm?.();
210
219
  state.bindVertexArray(null);
211
220
  // Unit 0 too, which a sampler its owner sets only as it draws reads till then.
212
221
  state.bindTexture(0, this.texel);
@@ -3,23 +3,6 @@ import type { KernelObject } from "../filter-plan.js";
3
3
  import { SizedPool, trimTogether } from "./pool.js";
4
4
  import { LAYER_UNIT } from "./programs.js";
5
5
 
6
- /** Where a frame is drawn: the target's size in pixels, and how stage pixels map onto it. */
7
- export interface Target {
8
- width: number;
9
- height: number;
10
- /** Target pixels a stage pixel, and where the stage's origin lies on the target. */
11
- scale: number;
12
- x: number;
13
- y: number;
14
- /** Target pixels a unit of the target's own, which filters read their input's size in. */
15
- resolution: number;
16
- /**
17
- * Target pixels a side of a pixel of Flash's, which a mesh's texture is
18
- * read once in: a pixel of the screen, or a BitmapData's draw's sample.
19
- */
20
- pixel: number;
21
- }
22
-
23
6
  /**
24
7
  * A target a layer is drawn into: a texture at least its rectangle's size,
25
8
  * with a stencil of its own for masks within it. Its rows run down, the