@swf2es/render 0.1.25 → 0.1.27

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 (43) hide show
  1. package/dist/textures.d.ts +5 -0
  2. package/dist/textures.d.ts.map +1 -1
  3. package/dist/textures.js +22 -0
  4. package/dist/textures.js.map +1 -1
  5. package/dist/webgl/atlas.d.ts +54 -23
  6. package/dist/webgl/atlas.d.ts.map +1 -1
  7. package/dist/webgl/atlas.js +177 -103
  8. package/dist/webgl/atlas.js.map +1 -1
  9. package/dist/webgl/backend.d.ts +7 -3
  10. package/dist/webgl/backend.d.ts.map +1 -1
  11. package/dist/webgl/backend.js +22 -10
  12. package/dist/webgl/backend.js.map +1 -1
  13. package/dist/webgl/batches.d.ts.map +1 -1
  14. package/dist/webgl/batches.js +3 -0
  15. package/dist/webgl/batches.js.map +1 -1
  16. package/dist/webgl/draws.d.ts.map +1 -1
  17. package/dist/webgl/draws.js +16 -9
  18. package/dist/webgl/draws.js.map +1 -1
  19. package/dist/webgl/filters.d.ts +10 -2
  20. package/dist/webgl/filters.d.ts.map +1 -1
  21. package/dist/webgl/filters.js +37 -15
  22. package/dist/webgl/filters.js.map +1 -1
  23. package/dist/webgl/free-ranges.d.ts +25 -0
  24. package/dist/webgl/free-ranges.d.ts.map +1 -0
  25. package/dist/webgl/free-ranges.js +108 -0
  26. package/dist/webgl/free-ranges.js.map +1 -0
  27. package/dist/webgl/gpu-textures.d.ts.map +1 -1
  28. package/dist/webgl/gpu-textures.js +27 -1
  29. package/dist/webgl/gpu-textures.js.map +1 -1
  30. package/dist/webgl/precompile.d.ts +66 -0
  31. package/dist/webgl/precompile.d.ts.map +1 -0
  32. package/dist/webgl/precompile.js +205 -0
  33. package/dist/webgl/precompile.js.map +1 -0
  34. package/package.json +2 -2
  35. package/src/textures.ts +26 -0
  36. package/src/webgl/atlas.ts +212 -105
  37. package/src/webgl/backend.ts +25 -9
  38. package/src/webgl/batches.ts +3 -0
  39. package/src/webgl/draws.ts +16 -9
  40. package/src/webgl/filters.ts +44 -14
  41. package/src/webgl/free-ranges.ts +114 -0
  42. package/src/webgl/gpu-textures.ts +40 -1
  43. package/src/webgl/precompile.ts +256 -0
@@ -1,4 +1,4 @@
1
- import { type GlState, linkProgram } from "@swf2es/context3d/device";
1
+ import { beginProgram, endProgram, type GlState, linkProgram } from "@swf2es/context3d/device";
2
2
  import { type Commands, RUN, STENCIL_POP, STENCIL_PUSH } from "../commands.js";
3
3
  import { FILL_SHADER, type Filters } from "../filters.js";
4
4
  import { fillKernel, kernelImagesFit } from "../kernel-shaders.js";
@@ -218,8 +218,14 @@ export class Draws {
218
218
  setUp(textureMost: number): void {
219
219
  const gl = this.gl;
220
220
  this.multi = gl.getExtension("WEBGL_multi_draw") as MultiDraw | null;
221
+ // All begun before any is asked after, so that a driver that compiles on threads of its own
222
+ // (KHR_parallel_shader_compile) compiles them at once.
223
+ const fill = beginProgram(gl, fillVertex(this.multi !== null, true), fillFragment);
224
+ const blend = beginProgram(gl, fillVertex(false), blendFillFragment);
225
+ const mesh = beginProgram(gl, meshVertex, meshFragment);
226
+ const glyphs = beginProgram(gl, glyphsVertex, glyphsFragment);
221
227
  try {
222
- this.program = linkProgram(gl, fillVertex(this.multi !== null, true), fillFragment);
228
+ this.program = endProgram(gl, fill);
223
229
  } catch (e) {
224
230
  if (!this.multi) {
225
231
  throw e;
@@ -253,7 +259,7 @@ export class Draws {
253
259
  }
254
260
 
255
261
  this.fillUnitLocation = gl.getUniformLocation(this.program, "uUnit");
256
- this.blendProgram = linkProgram(gl, fillVertex(false), blendFillFragment);
262
+ this.blendProgram = endProgram(gl, blend);
257
263
  this.blendRowLocation = gl.getUniformLocation(this.blendProgram, "uRow");
258
264
  this.blendViewLocation = gl.getUniformLocation(this.blendProgram, "uView");
259
265
  this.blendUnitLocation = gl.getUniformLocation(this.blendProgram, "uUnit");
@@ -267,7 +273,7 @@ export class Draws {
267
273
  gl.getUniformLocation(this.blendProgram, "uTextures"),
268
274
  Array.from({ length: FILL_TEXTURES }, (_, k) => FILL_UNIT + k),
269
275
  );
270
- this.mesh = linkProgram(gl, meshVertex, meshFragment);
276
+ this.mesh = endProgram(gl, mesh);
271
277
  this.meshRowLocation = gl.getUniformLocation(this.mesh, "uRow");
272
278
  this.meshViewLocation = gl.getUniformLocation(this.mesh, "uView");
273
279
  this.meshFragLocation = gl.getUniformLocation(this.mesh, "uFrag");
@@ -278,7 +284,7 @@ export class Draws {
278
284
  this.meshStale = true;
279
285
  this.meshRow = -1;
280
286
  this.meshMode = -1;
281
- this.glyphProgram = linkProgram(gl, glyphsVertex, glyphsFragment);
287
+ this.glyphProgram = endProgram(gl, glyphs);
282
288
  this.glyphRowLocation = gl.getUniformLocation(this.glyphProgram, "uRow");
283
289
  this.glyphViewLocation = gl.getUniformLocation(this.glyphProgram, "uView");
284
290
  this.glyphUnitLocation = gl.getUniformLocation(this.glyphProgram, "uUnit");
@@ -534,7 +540,6 @@ export class Draws {
534
540
  const gl = this.gl;
535
541
  const state = this.state;
536
542
  state.useProgram(this.blendProgram);
537
- state.bindVertexArray(this.atlas.vertexArray);
538
543
  state.bindTexture(0, this.gpu.rowsTexture);
539
544
  gl.uniform4fv(this.blendViewLocation, this.view);
540
545
  gl.uniform1f(this.blendUnitLocation, this.unit);
@@ -547,6 +552,7 @@ export class Draws {
547
552
  return;
548
553
  }
549
554
 
555
+ state.bindVertexArray(this.atlas.pages[this.runPlaces[0].page].vertexArray);
550
556
  for (let k = 0; k < FILL_TEXTURES; k++) {
551
557
  if (this.runUnits & (1 << k)) {
552
558
  state.bindTexture(FILL_UNIT + k, this.gpu.textures[this.unitIds[k]] as WebGLTexture);
@@ -685,7 +691,8 @@ export class Draws {
685
691
  continue;
686
692
  }
687
693
 
688
- state.bindVertexArray(this.atlas.vertexArray);
694
+ // A run's geometries all in the page of its first.
695
+ state.bindVertexArray(this.atlas.pages[place.page].vertexArray);
689
696
  if (!this.multi) {
690
697
  // A run not merged: a call a fill, its row told as it changes.
691
698
  const rows = this.prepared ? this.tableRows : this.runRows;
@@ -908,7 +915,7 @@ export class Draws {
908
915
  const state = this.state;
909
916
  state.useProgram(this.mesh);
910
917
  // The atlas' vertices, which a glyph draw before may have left bound for its own.
911
- state.bindVertexArray(this.atlas.vertexArray);
918
+ state.bindVertexArray(this.atlas.pages[place.page].vertexArray);
912
919
  if (this.meshStale) {
913
920
  gl.uniform4fv(this.meshViewLocation, this.view);
914
921
  gl.uniform4fv(this.meshFragLocation, this.frag);
@@ -963,7 +970,7 @@ export class Draws {
963
970
  }
964
971
 
965
972
  const gl = this.gl;
966
- this.state.bindVertexArray(this.atlas.vertexArray);
973
+ this.state.bindVertexArray(this.atlas.pages[place.page].vertexArray);
967
974
  gl.uniform4fv(kernel.at("uView"), this.view);
968
975
  gl.uniform4fv(kernel.at("uFrag"), this.frag);
969
976
  gl.uniform1i(kernel.at("uRow"), scene.row[i]);
@@ -25,6 +25,7 @@ import * as shaders from "../filter-shaders.js";
25
25
  import type { Filters } from "../filters.js";
26
26
  import { CLAMP, SMOOTH } from "../scene.js";
27
27
  import type { KernelPrograms } from "./kernels.js";
28
+ import type { ProgramQueue } from "./precompile.js";
28
29
 
29
30
  /**
30
31
  * A texture a pass draws into through its framebuffer, and reads: at least
@@ -118,6 +119,20 @@ void main(void) {
118
119
  finalColor = texture(uTexture, vTextureCoord);
119
120
  }`;
120
121
 
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
+ ];
135
+
121
136
  interface Pass {
122
137
  program: WebGLProgram;
123
138
  uniforms: Map<string, WebGLUniformLocation | null>;
@@ -159,10 +174,20 @@ export class FilterRunner<T extends FilterTarget> {
159
174
  state: GlState,
160
175
  host: FilterHost<T>,
161
176
  private readonly kernels: KernelPrograms,
177
+ private readonly queue: ProgramQueue,
162
178
  ) {
163
179
  this.gl = gl;
164
180
  this.state = state;
165
181
  this.host = host;
182
+ for (const [name, fragment] of PASSES) {
183
+ queue.add(
184
+ `filter:${name}`,
185
+ VERTEX,
186
+ HEADER + withinRects(fragment),
187
+ [INPUT_UNIT, BLURRED_UNIT, EXTRA_UNIT],
188
+ (program) => this.made(name, program),
189
+ );
190
+ }
166
191
  }
167
192
 
168
193
  /**
@@ -501,29 +526,34 @@ export class FilterRunner<T extends FilterTarget> {
501
526
  gl.drawArrays(gl.TRIANGLES, 0, 3);
502
527
  }
503
528
 
504
- /** The pass of `fragment`, linked as first used, in use. */
529
+ /**
530
+ * The pass of `fragment`, in use: linked ahead (ProgramQueue), its link
531
+ * finished now if it is not yet, or linked now where it would not link
532
+ * ahead, which throws why.
533
+ */
505
534
  private use(name: string, fragment: string): Pass {
506
- const gl = this.gl;
507
535
  let pass = this.passes.get(name);
508
- if (!pass) {
509
- const program = linkProgram(gl, VERTEX, HEADER + withinRects(fragment));
510
- pass = {
511
- program,
512
- uniforms: new Map(),
513
- told: new Map(),
514
- };
515
- this.passes.set(name, pass);
536
+ if (!pass && !this.queue.take(`filter:${name}`)) {
537
+ const program = linkProgram(this.gl, VERTEX, HEADER + withinRects(fragment));
516
538
  this.state.useProgram(program);
517
- gl.uniform1i(gl.getUniformLocation(program, "uTexture"), INPUT_UNIT);
518
- gl.uniform1i(gl.getUniformLocation(program, "uBlurred"), BLURRED_UNIT);
519
- gl.uniform1i(gl.getUniformLocation(program, "uGradient"), EXTRA_UNIT);
520
- gl.uniform1i(gl.getUniformLocation(program, "uMap"), EXTRA_UNIT);
539
+ this.made(name, program);
521
540
  }
522
541
 
542
+ pass ??= this.passes.get(name) as Pass;
523
543
  this.state.useProgram(pass.program);
524
544
  return pass;
525
545
  }
526
546
 
547
+ /** Pass `name` of `program`, linked and in use, its samplers told their units. */
548
+ private made(name: string, program: WebGLProgram): void {
549
+ const gl = this.gl;
550
+ this.passes.set(name, { program, uniforms: new Map(), told: new Map() });
551
+ gl.uniform1i(gl.getUniformLocation(program, "uTexture"), INPUT_UNIT);
552
+ gl.uniform1i(gl.getUniformLocation(program, "uBlurred"), BLURRED_UNIT);
553
+ gl.uniform1i(gl.getUniformLocation(program, "uGradient"), EXTRA_UNIT);
554
+ gl.uniform1i(gl.getUniformLocation(program, "uMap"), EXTRA_UNIT);
555
+ }
556
+
527
557
  /** The pass of a kernel's fragment shader (KernelPrograms), in use; null where it would not link. */
528
558
  private kernel(fragment: string): Pass | null {
529
559
  return this.kernels.use(VERTEX, fragment, (program) => {
@@ -0,0 +1,114 @@
1
+ /**
2
+ * The room of a buffer of `capacity` units: taken from its top while it
3
+ * fits, so that what a frame adds lies in a row and goes up in one upload,
4
+ * else from the first hole it fits, the holes kept sorted by start and
5
+ * merged with their neighbours as ranges come back. A range given back at
6
+ * the top lowers the top, with the hole below it, instead.
7
+ */
8
+ export class FreeRanges {
9
+ capacity = 0;
10
+ top = 0;
11
+ /** The units in holes below the top. */
12
+ free = 0;
13
+ /** The holes' starts, ascending, and their lengths: [start, start + length) each. */
14
+ readonly starts: number[] = [];
15
+ readonly lengths: number[] = [];
16
+
17
+ /** Every unit free, of `capacity`. */
18
+ reset(capacity: number): void {
19
+ this.capacity = capacity;
20
+ this.top = 0;
21
+ this.free = 0;
22
+ this.starts.length = 0;
23
+ this.lengths.length = 0;
24
+ }
25
+
26
+ /** The units free, at the top and in holes, which no take larger than can find. */
27
+ room(): number {
28
+ return this.capacity - this.top + this.free;
29
+ }
30
+
31
+ /** Where `length` units were taken: at the top, else from the first hole that holds them; -1 for none. */
32
+ take(length: number): number {
33
+ if (this.top + length <= this.capacity) {
34
+ const at = this.top;
35
+ this.top += length;
36
+ return at;
37
+ }
38
+
39
+ if (length > this.free) {
40
+ return -1;
41
+ }
42
+
43
+ const starts = this.starts;
44
+ const lengths = this.lengths;
45
+ for (let k = 0; k < starts.length; k++) {
46
+ if (lengths[k] >= length) {
47
+ const at = starts[k];
48
+ if (lengths[k] === length) {
49
+ starts.splice(k, 1);
50
+ lengths.splice(k, 1);
51
+ } else {
52
+ starts[k] += length;
53
+ lengths[k] -= length;
54
+ }
55
+
56
+ this.free -= length;
57
+ return at;
58
+ }
59
+ }
60
+
61
+ return -1;
62
+ }
63
+
64
+ /** [start, start + length), taken before, free again. */
65
+ give(start: number, length: number): void {
66
+ if (length === 0) {
67
+ return;
68
+ }
69
+
70
+ const starts = this.starts;
71
+ const lengths = this.lengths;
72
+ const n = starts.length;
73
+ if (start + length === this.top) {
74
+ this.top = start;
75
+ if (n > 0 && starts[n - 1] + lengths[n - 1] === start) {
76
+ this.top = starts[n - 1];
77
+ this.free -= lengths[n - 1];
78
+ starts.pop();
79
+ lengths.pop();
80
+ }
81
+
82
+ return;
83
+ }
84
+
85
+ // The first hole past it.
86
+ let lo = 0;
87
+ let hi = n;
88
+ while (lo < hi) {
89
+ const mid = (lo + hi) >> 1;
90
+ if (starts[mid] < start) {
91
+ lo = mid + 1;
92
+ } else {
93
+ hi = mid;
94
+ }
95
+ }
96
+
97
+ this.free += length;
98
+ const before = lo > 0 && starts[lo - 1] + lengths[lo - 1] === start;
99
+ const after = lo < n && start + length === starts[lo];
100
+ if (before && after) {
101
+ lengths[lo - 1] += length + lengths[lo];
102
+ starts.splice(lo, 1);
103
+ lengths.splice(lo, 1);
104
+ } else if (before) {
105
+ lengths[lo - 1] += length;
106
+ } else if (after) {
107
+ starts[lo] = start;
108
+ lengths[lo] += length;
109
+ } else {
110
+ starts.splice(lo, 0, start);
111
+ lengths.splice(lo, 0, length);
112
+ }
113
+ }
114
+ }
@@ -124,7 +124,7 @@ export class GpuTextures {
124
124
  for (const id of textures.pending) {
125
125
  const data = textures.get(id);
126
126
  const fill = data?.pixels ? null : (data?.fill ?? null);
127
- if (!data || (!data.pixels && fill === null)) {
127
+ if (!data || (!data.pixels && !data.source && fill === null)) {
128
128
  continue;
129
129
  }
130
130
 
@@ -137,6 +137,45 @@ export class GpuTextures {
137
137
 
138
138
  this.state.bindTexture(TEXTURE_UNIT, texture);
139
139
  this.state.activeTexture(TEXTURE_UNIT);
140
+ if (data.source) {
141
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
142
+ try {
143
+ if (this.sizes[id * 2] === data.width && this.sizes[id * 2 + 1] === data.height) {
144
+ gl.texSubImage2D(
145
+ gl.TEXTURE_2D,
146
+ 0,
147
+ 0,
148
+ 0,
149
+ gl.RGBA,
150
+ gl.UNSIGNED_BYTE,
151
+ data.source as TexImageSource,
152
+ );
153
+ } else {
154
+ gl.texImage2D(
155
+ gl.TEXTURE_2D,
156
+ 0,
157
+ gl.RGBA8,
158
+ gl.RGBA,
159
+ gl.UNSIGNED_BYTE,
160
+ data.source as TexImageSource,
161
+ );
162
+ }
163
+ this.sizes[id * 2] = data.width;
164
+ this.sizes[id * 2 + 1] = data.height;
165
+ } catch {
166
+ // A closed or tainted host picture must not strand all subsequent texture updates.
167
+ if (this.sizes[id * 2] < 0) {
168
+ this.state.dropTexture(texture);
169
+ gl.deleteTexture(texture);
170
+ this.textures[id] = null;
171
+ }
172
+ } finally {
173
+ data.source = null;
174
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
175
+ }
176
+ continue;
177
+ }
178
+
140
179
  // Floats, a kernel's image, make the texture again each time, as bytes may follow them.
141
180
  if (data.pixels instanceof Float32Array) {
142
181
  this.sizes[id * 2] = -1;
@@ -0,0 +1,256 @@
1
+ import {
2
+ beginProgram,
3
+ endProgram,
4
+ type GlState,
5
+ type PendingProgram,
6
+ ShaderError,
7
+ } from "@swf2es/context3d/device";
8
+
9
+ /** KHR_parallel_shader_compile: a program's link asked after without waiting for it. */
10
+ interface ParallelCompile {
11
+ readonly COMPLETION_STATUS_KHR: number;
12
+ }
13
+
14
+ /** A program made ahead of its first use (ProgramQueue.add). */
15
+ interface Job {
16
+ key: string;
17
+ vertex: string;
18
+ fragment: string;
19
+ /** The texture units its samplers read, 0 besides, which its warming draw binds a texel to. */
20
+ units: readonly number[];
21
+ made: (program: WebGLProgram) => void;
22
+ pending: PendingProgram | null;
23
+ }
24
+
25
+ /**
26
+ * Programs linked and warmed in a frame, at most, without KHR_parallel_shader_compile: each a few
27
+ * milliseconds, and a first draw may take a driver tens to build its pipeline.
28
+ */
29
+ const LINKS_A_FRAME = 1;
30
+ const WARMS_A_FRAME = 1;
31
+
32
+ /**
33
+ * Programs made ahead of the frame that first draws with them. A filter's
34
+ * pass linked as a frame first needs it stalls that frame while the driver
35
+ * compiles: on ANGLE's Direct3D back end, where GLSL becomes HLSL and that
36
+ * is compiled, for tens or hundreds of milliseconds, and a driver may put
37
+ * off more till the program's first draw. So once the first frame is drawn
38
+ * (step, from the second), every program queued is compiled and linked: with
39
+ * KHR_parallel_shader_compile all at once, on the driver's threads, each
40
+ * asked after on later frames only once complete; without it, one a frame.
41
+ * Each linked is then drawn with once into a target of a texel, one a
42
+ * frame, so that what the driver builds at a program's first draw is built
43
+ * then. A frame that needs a program before it is ready takes it at once
44
+ * (take), waiting only for what is left of its link.
45
+ */
46
+ export class ProgramQueue {
47
+ private readonly gl: WebGL2RenderingContext;
48
+ private readonly state: GlState;
49
+ private readonly parallel: ParallelCompile | null;
50
+ /** Not begun, begun and not yet asked after, linked and not yet drawn with. */
51
+ private readonly waiting: Job[] = [];
52
+ private readonly begun: Job[] = [];
53
+ private readonly warming: { program: WebGLProgram; units: readonly number[] }[] = [];
54
+ /** The target of a texel the warming draws go into, and the texel their samplers read. */
55
+ private framebuffer: WebGLFramebuffer | null = null;
56
+ private target: WebGLTexture | null = null;
57
+ private texel: WebGLTexture | null = null;
58
+ /** Frames drawn so far, but for the first, which is shown before anything is made for later. */
59
+ private frames = 0;
60
+
61
+ constructor(gl: WebGL2RenderingContext, state: GlState) {
62
+ this.gl = gl;
63
+ this.state = state;
64
+ this.parallel = gl.getExtension("KHR_parallel_shader_compile") as ParallelCompile | null;
65
+ }
66
+
67
+ /**
68
+ * Program `key` of GLSL `vertex` and `fragment` to be made ahead, `made`
69
+ * told it once it is linked (step or take), with it in use. Its samplers
70
+ * read `units`.
71
+ */
72
+ add(
73
+ key: string,
74
+ vertex: string,
75
+ fragment: string,
76
+ units: readonly number[],
77
+ made: (program: WebGLProgram) => void,
78
+ ): void {
79
+ this.waiting.push({ key, vertex, fragment, units, made, pending: null });
80
+ }
81
+
82
+ /** Whether any program is still to be linked or warmed. */
83
+ get busy(): boolean {
84
+ return this.waiting.length + this.begun.length + this.warming.length > 0;
85
+ }
86
+
87
+ /**
88
+ * A frame's share, after its draws, from the second frame: every program
89
+ * begun where the driver compiles in parallel, else one linked; those
90
+ * whose links completed told to their owners; and one drawn with for the
91
+ * first time.
92
+ */
93
+ step(): void {
94
+ // A context lost mid-frame makes no shaders; a restore makes a new queue.
95
+ if (!this.busy || this.frames++ === 0 || this.gl.isContextLost()) {
96
+ return;
97
+ }
98
+
99
+ const gl = this.gl;
100
+ if (this.parallel) {
101
+ // Each moved as it is begun, so that a throw leaves none in both lists.
102
+ while (this.waiting.length > 0) {
103
+ const job = this.waiting[0];
104
+ job.pending = beginProgram(gl, job.vertex, job.fragment);
105
+ this.waiting.shift();
106
+ this.begun.push(job);
107
+ }
108
+
109
+ const status = this.parallel.COMPLETION_STATUS_KHR;
110
+ for (let k = 0; k < this.begun.length; k++) {
111
+ const job = this.begun[k];
112
+ if (gl.getProgramParameter((job.pending as PendingProgram).program, status)) {
113
+ this.begun.splice(k--, 1);
114
+ this.finish(job, true);
115
+ }
116
+ }
117
+ } else {
118
+ for (let k = 0; k < LINKS_A_FRAME && this.waiting.length > 0; k++) {
119
+ const job = this.waiting.shift() as Job;
120
+ job.pending = beginProgram(gl, job.vertex, job.fragment);
121
+ this.finish(job, true);
122
+ }
123
+ }
124
+
125
+ 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);
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Program `key`, needed now: its link finished, waited for where it is
136
+ * not complete, or begun and finished where it was not begun, and its
137
+ * owner told. False where there is no such job, as once it was made or
138
+ * where its link failed, which the owner then links itself to see why.
139
+ */
140
+ take(key: string): boolean {
141
+ let job: Job | undefined;
142
+ for (const list of [this.begun, this.waiting]) {
143
+ const k = list.findIndex((j) => j.key === key);
144
+ if (k >= 0) {
145
+ job = list[k];
146
+ list.splice(k, 1);
147
+ break;
148
+ }
149
+ }
150
+
151
+ if (!job) {
152
+ return false;
153
+ }
154
+
155
+ job.pending ??= beginProgram(this.gl, job.vertex, job.fragment);
156
+ // Drawn with at once, as it is needed: no warming.
157
+ return this.finish(job, false);
158
+ }
159
+
160
+ /** `job`'s link finished and its owner told; whether it linked. */
161
+ private finish(job: Job, warm: boolean): boolean {
162
+ let program: WebGLProgram;
163
+ try {
164
+ program = endProgram(this.gl, job.pending as PendingProgram);
165
+ } catch (e) {
166
+ // A program that does not link is linked again as it is needed, which says why.
167
+ if (e instanceof ShaderError) {
168
+ return false;
169
+ }
170
+
171
+ throw e;
172
+ }
173
+
174
+ this.state.useProgram(program);
175
+ job.made(program);
176
+ if (warm) {
177
+ this.warming.push({ program, units: job.units });
178
+ }
179
+
180
+ return true;
181
+ }
182
+
183
+ /**
184
+ * `program` drawn with once, a triangle into a target of a texel, its
185
+ * samplers reading another, with no attributes, blending, stencil nor
186
+ * scissor, as the filters' passes draw.
187
+ */
188
+ private warm(program: WebGLProgram, units: readonly number[]): void {
189
+ const gl = this.gl;
190
+ const state = this.state;
191
+ // Bound again after, as the frame left it for what reads it.
192
+ const bound = state.boundFramebuffer ?? null;
193
+ if (!this.framebuffer) {
194
+ this.target = this.texture();
195
+ this.texel = this.texture();
196
+ this.framebuffer = gl.createFramebuffer();
197
+ state.bindFramebuffer(this.framebuffer);
198
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.target, 0);
199
+ }
200
+
201
+ state.bindFramebuffer(this.framebuffer);
202
+ state.viewport(0, 0, 1, 1);
203
+ state.colorMask(15);
204
+ state.stencil(null);
205
+ state.setBlend(null);
206
+ state.scissor(null);
207
+ state.useProgram(program);
208
+ state.bindVertexArray(null);
209
+ // Unit 0 too, which a sampler its owner sets only as it draws reads till then.
210
+ state.bindTexture(0, this.texel);
211
+ state.bindSampler(0, null);
212
+ for (const unit of units) {
213
+ state.bindTexture(unit, this.texel);
214
+ state.bindSampler(unit, null);
215
+ }
216
+
217
+ gl.drawArrays(gl.TRIANGLES, 0, 3);
218
+ state.bindFramebuffer(bound);
219
+ }
220
+
221
+ /** A texture of a texel, clear, complete as it is (one level, nearest). */
222
+ private texture(): WebGLTexture {
223
+ const gl = this.gl;
224
+ const texture = gl.createTexture() as WebGLTexture;
225
+ this.state.bindTexture(0, texture);
226
+ this.state.activeTexture(0);
227
+ gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA8, 1, 1);
228
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
229
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
230
+ return texture;
231
+ }
232
+
233
+ /** What it made on the context, deleted: its target, and the programs begun and not finished. */
234
+ destroy(): void {
235
+ const gl = this.gl;
236
+ for (const job of this.begun) {
237
+ const pending = job.pending as PendingProgram;
238
+ gl.deleteShader(pending.vertex);
239
+ gl.deleteShader(pending.fragment);
240
+ gl.deleteProgram(pending.program);
241
+ }
242
+
243
+ this.begun.length = 0;
244
+ this.waiting.length = 0;
245
+ this.warming.length = 0;
246
+ for (const texture of [this.target, this.texel]) {
247
+ if (texture) {
248
+ this.state.dropTexture(texture);
249
+ gl.deleteTexture(texture);
250
+ }
251
+ }
252
+
253
+ gl.deleteFramebuffer(this.framebuffer);
254
+ this.framebuffer = this.target = this.texel = null;
255
+ }
256
+ }