@swf2es/render 0.1.46 → 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 (54) 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/ramps.d.ts +5 -2
  37. package/dist/webgl/ramps.d.ts.map +1 -1
  38. package/dist/webgl/ramps.js +25 -18
  39. package/dist/webgl/ramps.js.map +1 -1
  40. package/dist/webgl/targets.d.ts +0 -16
  41. package/dist/webgl/targets.d.ts.map +1 -1
  42. package/dist/webgl/targets.js.map +1 -1
  43. package/package.json +3 -3
  44. package/src/backend.ts +188 -0
  45. package/src/filter-plan.ts +45 -3
  46. package/src/index.ts +1 -0
  47. package/src/measure.ts +45 -1
  48. package/src/webgl/backend.ts +100 -12
  49. package/src/webgl/draws.ts +2 -1
  50. package/src/webgl/filters.ts +125 -39
  51. package/src/webgl/holds.ts +3 -1
  52. package/src/webgl/precompile.ts +20 -11
  53. package/src/webgl/ramps.ts +25 -18
  54. package/src/webgl/targets.ts +0 -17
@@ -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);
@@ -51,7 +51,10 @@ void main(void) {
51
51
  /**
52
52
  * Each texel the entry of its line's ramp at its column, as rampOf finds it:
53
53
  * the stop's colour, or the span's c0 + floor(d * j / w) in integers, the
54
- * colour channels back through TO_SRGB in linear light, premultiplied.
54
+ * colour channels back through TO_SRGB in linear light, premultiplied. The
55
+ * ratios are read four a fetch, as the walk comes to them, and the quotient
56
+ * is found in floats and put right by its remainder: a fetch for each stop
57
+ * and four integer divisions an entry took SwiftShader a quarter more.
55
58
  */
56
59
  export const RAMP_FRAGMENT = `#version 300 es
57
60
  precision highp float;
@@ -61,19 +64,10 @@ uniform highp usampler2D uSrgb;
61
64
  out vec4 finalColor;
62
65
  int line;
63
66
 
64
- int ratioOf(int i) {
65
- uvec4 t = texelFetch(uRecords, ivec2(${RAMP_RATIOS / 4} + (i >> 2), line), 0);
66
- return int(t[i & 3]);
67
- }
68
-
69
67
  ivec4 colorOf(int i) {
70
68
  return ivec4(texelFetch(uRecords, ivec2(${RAMP_COLORS / 4} + i, line), 0));
71
69
  }
72
70
 
73
- int floorDiv(int n, int w) {
74
- return n >= 0 ? n / w : -((w - 1 - n) / w);
75
- }
76
-
77
71
  int srgb(int l) {
78
72
  return int(texelFetch(uSrgb, ivec2(l, 0), 0).r);
79
73
  }
@@ -84,7 +78,8 @@ void main(void) {
84
78
  uvec4 head = texelFetch(uRecords, ivec2(0, line), 0);
85
79
  int count = int(head[${RAMP_COUNT}]);
86
80
  bool linear = head[${RAMP_LINEAR}] != 0u;
87
- int from = ratioOf(0);
81
+ uvec4 ratios = texelFetch(uRecords, ivec2(${RAMP_RATIOS / 4}, line), 0);
82
+ int from = int(ratios.x);
88
83
  ivec4 c;
89
84
  if (k <= from) {
90
85
  c = colorOf(0);
@@ -98,7 +93,11 @@ void main(void) {
98
93
  break;
99
94
  }
100
95
 
101
- int to = ratioOf(i);
96
+ if ((i & 3) == 0) {
97
+ ratios = texelFetch(uRecords, ivec2(${RAMP_RATIOS / 4} + (i >> 2), line), 0);
98
+ }
99
+
100
+ int to = int(ratios[i & 3]);
102
101
  w = to - from;
103
102
  int end = w <= 0 ? at : max(at, min(to, 255));
104
103
  if (k <= end) {
@@ -118,16 +117,24 @@ void main(void) {
118
117
  if (w > 0) {
119
118
  ivec4 d = colorOf(span) - x;
120
119
  int j = k - from;
121
- c = ivec4(
122
- x.r + floorDiv(d.r * j, w),
123
- x.g + floorDiv(d.g * j, w),
124
- x.b + floorDiv(d.b * j, w),
125
- x.a + floorDiv(d.a * j, w)) & 255;
120
+ // floor(n / w) exactly: n is within 255 * 255 of 0, so (n + 0.5) / w is
121
+ // at least 0.5 / w from a whole number, which a division up to about 16 ULP
122
+ // out cannot cross; the remainder puts the quotient right on a GPU that
123
+ // divides worse.
124
+ ivec4 n = d * j;
125
+ ivec4 q = ivec4(floor((vec4(n) + 0.5) / float(w)));
126
+ ivec4 r = n - q * w;
127
+ q += ivec4(greaterThanEqual(r, ivec4(w))) - ivec4(lessThan(r, ivec4(0)));
128
+ c = (x + q) & 255;
126
129
  }
127
130
  }
128
131
  }
129
132
 
130
- ivec3 rgb = linear ? ivec3(srgb(c.r), srgb(c.g), srgb(c.b)) : c.rgb;
133
+ ivec3 rgb = c.rgb;
134
+ if (linear) {
135
+ rgb = ivec3(srgb(c.r), srgb(c.g), srgb(c.b));
136
+ }
137
+
131
138
  rgb = (rgb * (c.a + 1)) >> 8;
132
139
  finalColor = vec4(vec3(rgb), float(c.a)) / 255.0;
133
140
  }
@@ -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