@swf2es/render 0.1.28 → 0.1.30

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 (46) hide show
  1. package/dist/geometry.d.ts +17 -1
  2. package/dist/geometry.d.ts.map +1 -1
  3. package/dist/geometry.js +23 -0
  4. package/dist/geometry.js.map +1 -1
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/webgl/atlas.d.ts +2 -0
  10. package/dist/webgl/atlas.d.ts.map +1 -1
  11. package/dist/webgl/atlas.js +4 -1
  12. package/dist/webgl/atlas.js.map +1 -1
  13. package/dist/webgl/backend.d.ts +53 -5
  14. package/dist/webgl/backend.d.ts.map +1 -1
  15. package/dist/webgl/backend.js +166 -35
  16. package/dist/webgl/backend.js.map +1 -1
  17. package/dist/webgl/draws.d.ts +26 -3
  18. package/dist/webgl/draws.d.ts.map +1 -1
  19. package/dist/webgl/draws.js +86 -8
  20. package/dist/webgl/draws.js.map +1 -1
  21. package/dist/webgl/kernels.d.ts +6 -1
  22. package/dist/webgl/kernels.d.ts.map +1 -1
  23. package/dist/webgl/kernels.js +11 -1
  24. package/dist/webgl/kernels.js.map +1 -1
  25. package/dist/webgl/losses.d.ts +44 -0
  26. package/dist/webgl/losses.d.ts.map +1 -0
  27. package/dist/webgl/losses.js +80 -0
  28. package/dist/webgl/losses.js.map +1 -0
  29. package/dist/webgl/precompile.d.ts +1 -1
  30. package/dist/webgl/precompile.d.ts.map +1 -1
  31. package/dist/webgl/precompile.js +5 -3
  32. package/dist/webgl/precompile.js.map +1 -1
  33. package/dist/webgl/programs.d.ts +14 -7
  34. package/dist/webgl/programs.d.ts.map +1 -1
  35. package/dist/webgl/programs.js +49 -8
  36. package/dist/webgl/programs.js.map +1 -1
  37. package/package.json +2 -2
  38. package/src/geometry.ts +32 -1
  39. package/src/index.ts +1 -0
  40. package/src/webgl/atlas.ts +6 -1
  41. package/src/webgl/backend.ts +236 -60
  42. package/src/webgl/draws.ts +101 -7
  43. package/src/webgl/kernels.ts +10 -0
  44. package/src/webgl/losses.ts +92 -0
  45. package/src/webgl/precompile.ts +5 -3
  46. package/src/webgl/programs.ts +56 -8
@@ -0,0 +1,92 @@
1
+ import { lostShader, type ShaderError } from "@swf2es/context3d/device";
2
+
3
+ /**
4
+ * How long link failures that say nothing are taken for a lost context
5
+ * that is not flagged yet: Chrome flags a GPU process that died a task or
6
+ * so later, which a host drawing more than once a task, or a busy page,
7
+ * may stretch over a few frames.
8
+ */
9
+ export const LOSS_GRACE_MS = 500;
10
+
11
+ /**
12
+ * Which program links that failed are a lost context's (ShaderError.lost):
13
+ * all, while the context is flagged lost; a failure that says nothing,
14
+ * with the context not flagged, only for LOSS_GRACE_MS after the first of
15
+ * a run of them, which a frame drawn without one ends, as does a grace's
16
+ * time passing with none. One that outlives that is a fault of the
17
+ * shaders after all: thrown, or drawn without, as before, till a frame is
18
+ * drawn without a failure.
19
+ */
20
+ export class LinkLosses {
21
+ private readonly gl: WebGL2RenderingContext;
22
+ /** When the first failure that said nothing came, the context not flagged lost; -1 for none. */
23
+ private since = -1;
24
+ /** When the last came. */
25
+ private last = -1;
26
+ /** Whether the run's failures are taken for the shaders' (concede). */
27
+ private conceded = false;
28
+ /** Whether a failure came in the frame being drawn (endFrame). */
29
+ private failed = false;
30
+
31
+ constructor(gl: WebGL2RenderingContext) {
32
+ this.gl = gl;
33
+ }
34
+
35
+ /** Whether `e` is taken for a lost context's (see the class). */
36
+ lost(e: unknown): e is ShaderError {
37
+ if (!lostShader(e)) {
38
+ return false;
39
+ }
40
+
41
+ if (this.gl.isContextLost()) {
42
+ return true;
43
+ }
44
+
45
+ const now = performance.now();
46
+ this.failed = true;
47
+ if (this.conceded) {
48
+ this.last = now;
49
+ return false;
50
+ }
51
+
52
+ // A failure a grace after the last, as after work given up on and nothing drawn since, starts
53
+ // a run of its own: a loss long after a fault is a loss.
54
+ if (this.since < 0 || now - this.last >= LOSS_GRACE_MS) {
55
+ this.since = now;
56
+ }
57
+
58
+ this.last = now;
59
+ return now - this.since < LOSS_GRACE_MS;
60
+ }
61
+
62
+ /** Whether a failure taken for a loss has outlived the grace, the context still not flagged lost. */
63
+ get expired(): boolean {
64
+ return this.since < 0 || performance.now() - this.since >= LOSS_GRACE_MS;
65
+ }
66
+
67
+ /**
68
+ * The run's failures taken for the shaders', its grace over with the
69
+ * context not flagged lost: those that follow too, till a frame is drawn
70
+ * without one, which ends the run (WebGLBackend.resume).
71
+ */
72
+ concede(): void {
73
+ this.conceded = true;
74
+ }
75
+
76
+ /** A frame drawn whole: one without a failure ends the run. */
77
+ endFrame(): void {
78
+ if (!this.failed) {
79
+ this.since = -1;
80
+ this.conceded = false;
81
+ }
82
+
83
+ this.failed = false;
84
+ }
85
+
86
+ /** The context restored: what failed before was its loss. */
87
+ clear(): void {
88
+ this.since = -1;
89
+ this.conceded = false;
90
+ this.failed = false;
91
+ }
92
+ }
@@ -85,7 +85,7 @@ export class ProgramQueue {
85
85
  }
86
86
 
87
87
  /**
88
- * A frame's share, after its draws, from the second frame: every program
88
+ * A frame's share, before its draws, from the second frame: every program
89
89
  * begun where the driver compiles in parallel, else one linked; those
90
90
  * whose links completed told to their owners; and one drawn with for the
91
91
  * first time.
@@ -163,8 +163,10 @@ export class ProgramQueue {
163
163
  try {
164
164
  program = endProgram(this.gl, job.pending as PendingProgram);
165
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) {
166
+ // A program that does not link is linked again as it is needed, which says why, or gives
167
+ // up on the frame where the context is lost unflagged (WebGLBackend.gaveUp); one on a
168
+ // context flagged lost gives up on it now.
169
+ if (e instanceof ShaderError && !this.gl.isContextLost()) {
168
170
  return false;
169
171
  }
170
172
 
@@ -1,3 +1,4 @@
1
+ import { TARGET_TEXEL } from "../geometry.js";
1
2
  import { ROWS_A_LINE } from "../rows.js";
2
3
  import {
3
4
  ADD,
@@ -84,6 +85,16 @@ export const MERGED_LINE = 1024;
84
85
  /** The glyph tables' unit (GlyphTables), past the merged fills' rows. */
85
86
  export const TABLE_UNIT = MERGED_UNIT + 1;
86
87
 
88
+ /**
89
+ * The bit of a fill's vSlot past its texture's slot (FILL_TEXTURES of them,
90
+ * in the bits below), in the stage fill program alone: its texel is the
91
+ * stage's pixel (TARGET_TEXEL).
92
+ */
93
+ const STAGE_TEXEL = 16;
94
+ if (FILL_TEXTURES >= STAGE_TEXEL) {
95
+ throw new Error("the fill textures' slots run into vSlot's stage texel bit");
96
+ }
97
+
87
98
  /**
88
99
  * Places a vertex by its row's transform, on a quarter of a pixel of
89
100
  * Flash's (uUnit of them a stage pixel, from the stage's origin) where it
@@ -95,9 +106,11 @@ export const TABLE_UNIT = MERGED_UNIT + 1;
95
106
  * `multi`, a merged fill's vertices carry as aDraw where its packed row is
96
107
  * in uMergedRows, which the stream keeps for its copy, so that the row
97
108
  * changes from frame to frame where the vertices need not; the atlas leaves
98
- * that attribute disabled and negative.
109
+ * that attribute disabled and negative. With `stage`, the stage fill
110
+ * program's (stageFillFragment), a texel of TARGET_TEXEL or below samples
111
+ * the stage's pixel under the fragment instead.
99
112
  */
100
- export const fillVertex = (multi: boolean, merged = multi) => `#version 300 es
113
+ export const fillVertex = (multi: boolean, merged = multi, stage = false) => `#version 300 es
101
114
  ${multi ? "#extension GL_ANGLE_multi_draw : require" : ""}
102
115
  precision highp float;
103
116
  layout(location = ${POSITION_AT}) in vec2 aPosition;
@@ -130,7 +143,14 @@ void main(void) {
130
143
 
131
144
  gl_Position = vec4(p * uView.xy + uView.zw, 0.0, 1.0);
132
145
  vColor = aColor;
133
- vTexel = aTexel;
146
+ ${
147
+ stage
148
+ ? ` // The fragment finds the stage's pixel from its place (STAGE_TEXEL in vSlot).
149
+ bool onStage = aTexel.x <= ${TARGET_TEXEL / 2};
150
+ vTexel = onStage ? p : aTexel;
151
+ vSlot |= onStage ? ${STAGE_TEXEL} : 0;`
152
+ : " vTexel = aTexel;"
153
+ }
134
154
  }
135
155
  `;
136
156
 
@@ -153,9 +173,10 @@ const slotTexel = Array.from(
153
173
  * Flash's colour transform on the straight colour, clamped, then
154
174
  * premultiplied for blending: the vertex' colour, or with a texture the
155
175
  * texel's (premultiplied in the texture) times it, as the colour batcher
156
- * did, a clear texel staying clear.
176
+ * did, a clear texel staying clear. With `stage`, the stage fill program's
177
+ * (stageFillFragment).
157
178
  */
158
- export const fillFragment = `#version 300 es
179
+ const fillFragmentOf = (stage: boolean) => `#version 300 es
159
180
  precision highp float;
160
181
  in vec4 vColor;
161
182
  in vec2 vTexel;
@@ -163,11 +184,24 @@ flat in vec4 vMul;
163
184
  flat in vec4 vAdd;
164
185
  flat in int vSlot;
165
186
  uniform sampler2D uTextures[${FILL_TEXTURES}];
166
- out vec4 finalColor;
187
+ ${stage ? "uniform vec4 uStage;\n" : ""}out vec4 finalColor;
167
188
 
168
189
  vec4 texel(vec2 at) {
169
190
  vec4 t = vec4(0.0);
170
- switch (vSlot) {
191
+ ${
192
+ stage
193
+ ? ` if (vSlot >= ${STAGE_TEXEL}) {
194
+ // The stage's pixel under the pixel's centre, from the fragment's place, not the position
195
+ // interpolated across the triangle: at a scale of a half, a quarter or a tenth that centre
196
+ // falls on a whole stage pixel, which interpolation leaves a hair either side of, a texel or
197
+ // its neighbour as each triangle rounds. A sixty-fourth on keeps it off that edge.
198
+ vec2 f = vec2(gl_FragCoord.x + uStage.x, uStage.y + uStage.z * gl_FragCoord.y) * uStage.w;
199
+ at = floor(f + 1.0 / 64.0) + 0.5;
200
+ }
201
+
202
+ switch (vSlot & ${STAGE_TEXEL - 1}) {`
203
+ : " switch (vSlot) {"
204
+ }
171
205
  ${slotTexel}
172
206
  }
173
207
 
@@ -176,7 +210,7 @@ ${slotTexel}
176
210
 
177
211
  void main(void) {
178
212
  vec4 straight = vColor;
179
- if (vSlot != 0) {
213
+ if (${stage ? `(vSlot & ${STAGE_TEXEL - 1})` : "vSlot"} != 0) {
180
214
  vec4 t = texel(vTexel);
181
215
  if (t.a == 0.0) {
182
216
  finalColor = vec4(0.0);
@@ -191,6 +225,20 @@ void main(void) {
191
225
  }
192
226
  `;
193
227
 
228
+ /** The fill program's fragment shader, which every fill draws with but those of stageFillFragment. */
229
+ export const fillFragment = fillFragmentOf(false);
230
+
231
+ /**
232
+ * The stage fill program's, for a fill some of whose texels are the
233
+ * stage's pixel (TARGET_TEXEL, Place.stage), drawn alone: the stage's
234
+ * pixel found from gl_FragCoord through uStage, which takes a fragment to
235
+ * the target's pixels from the stage's origin, down (x + uStage.x,
236
+ * uStage.y + uStage.z * y), then to stage pixels (times w). A program of
237
+ * its own: in the fill program, that path, though no fill took it, made a
238
+ * frame of plain fills take Intel's GPUs over half as long again.
239
+ */
240
+ export const stageFillFragment = fillFragmentOf(true);
241
+
194
242
  /**
195
243
  * drawTriangles' textured triangles (MESH), placed as fills are, their u
196
244
  * and v carried times their depth t, and t, which the GPU interpolates