@swf2es/render 0.1.29 → 0.1.31

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.
@@ -1,5 +1,5 @@
1
1
  import type { GlState } from "@swf2es/context3d/device";
2
- import { type Geometries, type GeometryData, GLYPH_FLOATS } from "../geometry.js";
2
+ import { type Geometries, type GeometryData, GLYPH_FLOATS, samplesStage } from "../geometry.js";
3
3
  import { FreeRanges } from "./free-ranges.js";
4
4
  import { GlyphTables } from "./glyph-tables.js";
5
5
  import {
@@ -96,6 +96,8 @@ export interface Place {
96
96
  glyphs: { mode: Float32Array; table: Float32Array; slot: number } | null;
97
97
  /** Its geometry's flat (GeometryData.flat). */
98
98
  flat: boolean;
99
+ /** Whether it samples the stage's pixel (samplesStage), which the stage fill program draws. */
100
+ stage: boolean;
99
101
  /** Small, unchanged geometries may be copied into a run's streaming buffer. */
100
102
  batch: { data: GeometryData; frame: number } | null;
101
103
  }
@@ -262,6 +264,7 @@ export class GeometryAtlas {
262
264
  const data = geometries.get(id);
263
265
  if (place && data && !data.glyphs && place.vertices === data.positions.length / 2) {
264
266
  this.copyVertices(this.pages[place.page], data, place.vertexStart);
267
+ place.stage = samplesStage(data);
265
268
  place.batch = this.batch(data);
266
269
  } else if (place && data) {
267
270
  this.letGo(id);
@@ -500,6 +503,7 @@ export class GeometryAtlas {
500
503
  indices: length,
501
504
  glyphs: null,
502
505
  flat: data.flat === true,
506
+ stage: samplesStage(data),
503
507
  batch: this.batch(data),
504
508
  };
505
509
  page.live += count;
@@ -607,6 +611,7 @@ export class GeometryAtlas {
607
611
  slot: this.tables.slot(glyphs.mode, glyphs.table),
608
612
  },
609
613
  flat: false,
614
+ stage: false,
610
615
  batch: null,
611
616
  };
612
617
  g.top += count;
@@ -385,6 +385,15 @@ export class WebGLBackend {
385
385
  // A frame drawn into a layer, for BitmapData, is no frame of the canvas': idle layers are
386
386
  // counted in the canvas' frames.
387
387
  const counted = root === null;
388
+ // Once the first frame is drawn, the programs not drawn with yet are made, a frame's share,
389
+ // before this frame draws anything: Chrome clears a canvas it composited at the first draw
390
+ // call after, into whatever framebuffer, and the stage's blit onto the canvas is none. A
391
+ // warming draw after a frame that blitted and drew nothing else, as of a stage holding
392
+ // nothing, wiped that frame, and the canvas showed black.
393
+ if (counted) {
394
+ this.programs.step();
395
+ }
396
+
388
397
  const gl = this.gl;
389
398
  this.frameFilters = filters;
390
399
  this.atlas.place(geometries);
@@ -610,8 +619,6 @@ export class WebGLBackend {
610
619
  // The free layers, and the stage's (stageAlpha), undrawn into for LAYER_IDLE_FRAMES let go of.
611
620
  this.stage.dropIdleLayer();
612
621
  this.pool.endFrame();
613
- // Once the first frame is drawn, the programs not drawn with yet are made, a frame's share.
614
- this.programs.step();
615
622
  }
616
623
  }
617
624
 
@@ -37,6 +37,7 @@ import {
37
37
  RUN_MOST,
38
38
  RUN_UNIT,
39
39
  SNAP_BIT,
40
+ stageFillFragment,
40
41
  TABLE_UNIT,
41
42
  TEXTURE_UNIT,
42
43
  } from "./programs.js";
@@ -100,6 +101,17 @@ export class Draws {
100
101
  private rowLocation: WebGLUniformLocation | null = null;
101
102
  private viewLocation: WebGLUniformLocation | null = null;
102
103
  private fillUnitLocation: WebGLUniformLocation | null = null;
104
+ /**
105
+ * The stage fill program (stageFillFragment), for fills that sample the
106
+ * stage's pixel (Place.stage), each drawn alone: linked as one is first
107
+ * drawn, null till then and after the context is made again; and where it
108
+ * takes its row, view, Flash's pixels a stage pixel and the stage.
109
+ */
110
+ private stageProgram: WebGLProgram | null = null;
111
+ private stageRowLocation: WebGLUniformLocation | null = null;
112
+ private stageViewLocation: WebGLUniformLocation | null = null;
113
+ private stageUnitLocation: WebGLUniformLocation | null = null;
114
+ private stageLocation: WebGLUniformLocation | null = null;
103
115
  /**
104
116
  * The fill program that blends a fill straight onto what is below it
105
117
  * (drawBlended), a draw a call, and where it takes what the fill program
@@ -161,6 +173,10 @@ export class Draws {
161
173
  */
162
174
  private readonly view = new Float32Array(4);
163
175
  private readonly frag = new Float32Array(4);
176
+ /** A fragment to the target's pixels from the stage's origin, as frag, then a stage pixel of them. */
177
+ private readonly stage = new Float32Array(4);
178
+ /** Whether the stage fill program is to be told the view, unit and stage as it next draws. */
179
+ private stageStale = true;
164
180
  private meshStale = true;
165
181
  /** The row and mode (REPEAT, SMOOTH) the mesh program was told last, -1 for none. */
166
182
  private meshRow = -1;
@@ -240,6 +256,8 @@ export class Draws {
240
256
  this.unitIds.fill(-1);
241
257
  this.rowLocation = gl.getUniformLocation(this.program, "uRow");
242
258
  this.viewLocation = gl.getUniformLocation(this.program, "uView");
259
+ // The context's own, gone with it, or not made yet.
260
+ this.stageProgram = null;
243
261
  this.state.useProgram(this.program);
244
262
  gl.uniform1i(gl.getUniformLocation(this.program, "uRows"), 0);
245
263
  gl.uniform1iv(
@@ -343,7 +361,11 @@ export class Draws {
343
361
  }
344
362
 
345
363
  const place = this.atlas.places[scene.geometry[i]];
346
- if (!place || place.glyphs || (!stencil && scene.sampler[i] & (MESH | PLANE | SHADED))) {
364
+ if (
365
+ !place ||
366
+ place.glyphs ||
367
+ (!stencil && (place.stage || scene.sampler[i] & (MESH | PLANE | SHADED)))
368
+ ) {
347
369
  continue;
348
370
  }
349
371
 
@@ -478,8 +500,14 @@ export class Draws {
478
500
  frag[1] = (open.layer ? open.y : open.y + open.height) - target.y - oy;
479
501
  frag[2] = down;
480
502
  frag[3] = target.pixel;
503
+ const stage = this.stage;
504
+ stage[0] = frag[0];
505
+ stage[1] = frag[1];
506
+ stage[2] = down;
507
+ stage[3] = 1 / target.scale;
481
508
  this.unit = target.scale / target.pixel;
482
509
  gl.uniform1f(this.fillUnitLocation, this.unit);
510
+ this.stageStale = true;
483
511
  this.meshStale = true;
484
512
  this.glyphStale = true;
485
513
  }
@@ -509,7 +537,8 @@ export class Draws {
509
537
  * one fill of a shape, of no mesh, shader fill or text, whose texture, if
510
538
  * it samples one, is there. Drawn straight onto what is below in a blend
511
539
  * mode, it is as its layer would be composited, as no part of it is over
512
- * another.
540
+ * another. Not one that samples the stage's pixel, which the blend
541
+ * program does not: its layer takes it.
513
542
  */
514
543
  blendable(scene: SceneList, i: number): boolean {
515
544
  if (scene.kind[i] !== DRAW || scene.sampler[i] & (MESH | PLANE | SHADED)) {
@@ -518,7 +547,9 @@ export class Draws {
518
547
 
519
548
  const place = this.atlas.places[scene.geometry[i]];
520
549
  const id = scene.texture[i];
521
- return !!place && place.flat && !place.glyphs && (id < 0 || !!this.gpu.textures[id]);
550
+ return (
551
+ !!place && place.flat && !place.glyphs && !place.stage && (id < 0 || !!this.gpu.textures[id])
552
+ );
522
553
  }
523
554
 
524
555
  /**
@@ -646,6 +677,12 @@ export class Draws {
646
677
  continue;
647
678
  }
648
679
 
680
+ // In a mask, its coverage as any fill's.
681
+ if (!stencil && place.stage) {
682
+ other = this.drawStage(scene, i) || other;
683
+ continue;
684
+ }
685
+
649
686
  if (other) {
650
687
  this.useFill();
651
688
  other = false;
@@ -751,8 +788,9 @@ export class Draws {
751
788
  * geometries too large to merge (Place.batch), of geometries added this
752
789
  * frame, or of stable ones alone, so that a stable run's merged copy is
753
790
  * drawn again while a part of it changes from frame to frame; and without
754
- * merging each fill is a run. The last entry gathered, past those whose
755
- * texture is gone, which draw nothing.
791
+ * merging each fill is a run. A fill that samples the stage's pixel
792
+ * (Place.stage) ends a run, drawn alone (drawStage). The last entry
793
+ * gathered, past those whose texture is gone, which draw nothing.
756
794
  */
757
795
  private gatherRun(scene: SceneList, i: number, end: number, stencil: boolean): number {
758
796
  const places = this.atlas.places;
@@ -777,7 +815,10 @@ export class Draws {
777
815
  continue;
778
816
  }
779
817
 
780
- if (place.glyphs || (!stencil && scene.sampler[j] & (MESH | PLANE | SHADED))) {
818
+ if (
819
+ place.glyphs ||
820
+ (!stencil && ((place.stage && j > i) || scene.sampler[j] & (MESH | PLANE | SHADED)))
821
+ ) {
781
822
  break;
782
823
  }
783
824
 
@@ -856,6 +897,57 @@ export class Draws {
856
897
  return free + 1;
857
898
  }
858
899
 
900
+ /**
901
+ * Entry `i`, a fill that samples the stage's pixel (Place.stage), drawn
902
+ * alone by the stage fill program, linked as it is first needed and told
903
+ * the view as it changed; whether it was drawn, that program left in use.
904
+ */
905
+ private drawStage(scene: SceneList, i: number): boolean {
906
+ this.gatherRun(scene, i, i + 1, false);
907
+ if (this.runCount === 0) {
908
+ return false;
909
+ }
910
+
911
+ const gl = this.gl;
912
+ const state = this.state;
913
+ if (!this.stageProgram) {
914
+ // A link that fails is given up on as the fill program's would be (WebGLBackend.gaveUp).
915
+ const program = linkProgram(gl, fillVertex(false, false, true), stageFillFragment);
916
+ this.stageProgram = program;
917
+ this.stageRowLocation = gl.getUniformLocation(program, "uRow");
918
+ this.stageViewLocation = gl.getUniformLocation(program, "uView");
919
+ this.stageUnitLocation = gl.getUniformLocation(program, "uUnit");
920
+ this.stageLocation = gl.getUniformLocation(program, "uStage");
921
+ state.useProgram(program);
922
+ gl.uniform1i(gl.getUniformLocation(program, "uRows"), 0);
923
+ gl.uniform1iv(
924
+ gl.getUniformLocation(program, "uTextures"),
925
+ Array.from({ length: FILL_TEXTURES }, (_, k) => FILL_UNIT + k),
926
+ );
927
+ this.stageStale = true;
928
+ }
929
+
930
+ state.useProgram(this.stageProgram);
931
+ if (this.stageStale) {
932
+ gl.uniform4fv(this.stageViewLocation, this.view);
933
+ gl.uniform1f(this.stageUnitLocation, this.unit);
934
+ gl.uniform4fv(this.stageLocation, this.stage);
935
+ this.stageStale = false;
936
+ }
937
+
938
+ state.bindVertexArray(this.atlas.pages[this.runPlaces[0].page].vertexArray);
939
+ for (let k = 0; k < FILL_TEXTURES; k++) {
940
+ if (this.runUnits & (1 << k)) {
941
+ state.bindTexture(FILL_UNIT + k, this.gpu.textures[this.unitIds[k]] as WebGLTexture);
942
+ state.bindSampler(FILL_UNIT + k, this.gpu.sampler(this.unitSampling[k]));
943
+ }
944
+ }
945
+
946
+ gl.uniform1i(this.stageRowLocation, this.runRows[0]);
947
+ gl.drawElements(gl.TRIANGLES, this.runCounts[0], gl.UNSIGNED_INT, this.runOffsets[0]);
948
+ return true;
949
+ }
950
+
859
951
  /**
860
952
  * Entry `i`, advanced text's glyph quads (GeometryData.glyphs), drawn by the glyph
861
953
  * program from its atlas, which is told the view as it changed, and its
@@ -1018,6 +1110,7 @@ export class Draws {
1018
1110
 
1019
1111
  const gl = this.gl;
1020
1112
  gl.deleteProgram(this.program);
1113
+ gl.deleteProgram(this.stageProgram);
1021
1114
  gl.deleteProgram(this.mesh);
1022
1115
  gl.deleteProgram(this.glyphProgram);
1023
1116
  }
@@ -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.
@@ -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