@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.
- package/dist/backend.d.ts +113 -0
- package/dist/backend.d.ts.map +1 -0
- package/dist/backend.js +2 -0
- package/dist/backend.js.map +1 -0
- package/dist/filter-plan.d.ts +13 -1
- package/dist/filter-plan.d.ts.map +1 -1
- package/dist/filter-plan.js +39 -3
- package/dist/filter-plan.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +1 -1
- package/dist/measure.d.ts +11 -1
- package/dist/measure.d.ts.map +1 -1
- package/dist/measure.js +32 -1
- package/dist/measure.js.map +1 -1
- package/dist/webgl/backend.d.ts +34 -4
- package/dist/webgl/backend.d.ts.map +1 -1
- package/dist/webgl/backend.js +80 -11
- package/dist/webgl/backend.js.map +1 -1
- package/dist/webgl/draws.d.ts +2 -1
- package/dist/webgl/draws.d.ts.map +1 -1
- package/dist/webgl/draws.js.map +1 -1
- package/dist/webgl/filters.d.ts +15 -7
- package/dist/webgl/filters.d.ts.map +1 -1
- package/dist/webgl/filters.js +105 -39
- package/dist/webgl/filters.js.map +1 -1
- package/dist/webgl/holds.d.ts +2 -1
- package/dist/webgl/holds.d.ts.map +1 -1
- package/dist/webgl/holds.js +2 -2
- package/dist/webgl/holds.js.map +1 -1
- package/dist/webgl/precompile.d.ts +5 -3
- package/dist/webgl/precompile.d.ts.map +1 -1
- package/dist/webgl/precompile.js +10 -8
- package/dist/webgl/precompile.js.map +1 -1
- package/dist/webgl/ramps.d.ts +5 -2
- package/dist/webgl/ramps.d.ts.map +1 -1
- package/dist/webgl/ramps.js +25 -18
- package/dist/webgl/ramps.js.map +1 -1
- package/dist/webgl/targets.d.ts +0 -16
- package/dist/webgl/targets.d.ts.map +1 -1
- package/dist/webgl/targets.js.map +1 -1
- package/package.json +3 -3
- package/src/backend.ts +188 -0
- package/src/filter-plan.ts +45 -3
- package/src/index.ts +1 -0
- package/src/measure.ts +45 -1
- package/src/webgl/backend.ts +100 -12
- package/src/webgl/draws.ts +2 -1
- package/src/webgl/filters.ts +125 -39
- package/src/webgl/holds.ts +3 -1
- package/src/webgl/precompile.ts +20 -11
- package/src/webgl/ramps.ts +25 -18
- package/src/webgl/targets.ts +0 -17
package/src/backend.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// What the player's view asks of whatever executes its commands
|
|
2
|
+
// (docs/renderer.md, "The backend"): WebGL 2's backend now, and one that
|
|
3
|
+
// draws on the CPU later (the CPU rasterizer's design, #1013). No GPU
|
|
4
|
+
// types, no DOM: the view reaches its backend through this alone, so that
|
|
5
|
+
// it reads nothing of a context itself.
|
|
6
|
+
import type { Commands } from "./commands.js";
|
|
7
|
+
import type { Filters } from "./filters.js";
|
|
8
|
+
import type { Geometries } from "./geometry.js";
|
|
9
|
+
import type { Keeps } from "./keeps.js";
|
|
10
|
+
import type { Rows } from "./rows.js";
|
|
11
|
+
import type { SceneList } from "./scene.js";
|
|
12
|
+
import type { Textures } from "./textures.js";
|
|
13
|
+
|
|
14
|
+
/** Where a frame is drawn: the target's size in pixels, and how stage pixels map onto it. */
|
|
15
|
+
export interface Target {
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
/** Target pixels a stage pixel, and where the stage's origin lies on the target. */
|
|
19
|
+
scale: number;
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
/** Target pixels a unit of the target's own, which filters read their input's size in. */
|
|
23
|
+
resolution: number;
|
|
24
|
+
/**
|
|
25
|
+
* Target pixels a side of a pixel of Flash's, which a mesh's texture is
|
|
26
|
+
* read once in: a pixel of the screen, or a BitmapData's draw's sample.
|
|
27
|
+
*/
|
|
28
|
+
pixel: number;
|
|
29
|
+
/**
|
|
30
|
+
* Target pixels a side of a pixel its filters run at: 1, or the samples
|
|
31
|
+
* of a draw into a bitmap at a texel a pixel, a power of two, whose
|
|
32
|
+
* filtered layers are halved to the bitmap's pixels before their chains
|
|
33
|
+
* run, as Flash filters them (planChain).
|
|
34
|
+
*/
|
|
35
|
+
filterPixel: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A backend: it executes a frame's commands over the registries the view
|
|
40
|
+
* fills, draws them into BitmapData's stores and 3D objects' bitmaps, and
|
|
41
|
+
* reads, fills and copies the stores' textures. The texture ids it takes
|
|
42
|
+
* are the `Textures` registry's.
|
|
43
|
+
*/
|
|
44
|
+
export interface Backend {
|
|
45
|
+
/**
|
|
46
|
+
* Samples a pixel the stage and its layers are drawn with, 1 for none:
|
|
47
|
+
* Flash's anti-aliasing by the quality, as the host sets it.
|
|
48
|
+
*/
|
|
49
|
+
samples: number;
|
|
50
|
+
/**
|
|
51
|
+
* Whether what it draws on is lost (a WebGL context), till it is
|
|
52
|
+
* restored: nothing is drawn, read or filled meanwhile.
|
|
53
|
+
*/
|
|
54
|
+
readonly lost: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Whether frames wait (resume): what it draws on was lost and is not
|
|
57
|
+
* back yet, or work was given up on as lost before that was known.
|
|
58
|
+
*/
|
|
59
|
+
readonly stale: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* The largest texture side it makes, which a draw's bitmap and a
|
|
62
|
+
* filtered layer's padding must fit: 0 for one made lost, till restored.
|
|
63
|
+
*/
|
|
64
|
+
readonly textureLimit: number;
|
|
65
|
+
/**
|
|
66
|
+
* The size in pixels the stage's frames are drawn at (execute): a
|
|
67
|
+
* canvas's drawing buffer, which a browser may hold smaller than asked.
|
|
68
|
+
*/
|
|
69
|
+
readonly targetWidth: number;
|
|
70
|
+
readonly targetHeight: number;
|
|
71
|
+
/** The stage's frames asked to be drawn `width` by `height` pixels: a canvas's size. */
|
|
72
|
+
resize(width: number, height: number): void;
|
|
73
|
+
/** Whether a stale backend draws again; it may throw the failure it gave up on. */
|
|
74
|
+
resume(): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* What it draws on came back after it was lost: everything made again,
|
|
77
|
+
* and the geometries it lost given back to their registry
|
|
78
|
+
* (Geometries.lost). The textures' registry is the caller's to tell
|
|
79
|
+
* (Textures.lost).
|
|
80
|
+
*/
|
|
81
|
+
restore(geometries: Geometries): void;
|
|
82
|
+
/**
|
|
83
|
+
* `listener` called whenever what it draws on comes back after it was
|
|
84
|
+
* lost (a WebGL context restored), for what holds the backend to drop
|
|
85
|
+
* what named the old one and call `restore`. One listener is kept: a
|
|
86
|
+
* second call replaces the first.
|
|
87
|
+
*/
|
|
88
|
+
onRestore(listener: () => void): void;
|
|
89
|
+
/** A stage's frame drawn: what the geometries added and removed first, then the commands. */
|
|
90
|
+
execute(
|
|
91
|
+
commands: Commands,
|
|
92
|
+
scene: SceneList,
|
|
93
|
+
rows: Rows,
|
|
94
|
+
geometries: Geometries,
|
|
95
|
+
textures: Textures,
|
|
96
|
+
filters: Filters,
|
|
97
|
+
keeps: Keeps,
|
|
98
|
+
target: Target,
|
|
99
|
+
): void;
|
|
100
|
+
/**
|
|
101
|
+
* The commands drawn at `samples` times the size of a `width` by `height`
|
|
102
|
+
* bitmap a side, halved till a sample a pixel, and read back:
|
|
103
|
+
* premultiplied RGBA bytes, a row at a time from the top.
|
|
104
|
+
*/
|
|
105
|
+
snapshot(
|
|
106
|
+
commands: Commands,
|
|
107
|
+
scene: SceneList,
|
|
108
|
+
rows: Rows,
|
|
109
|
+
geometries: Geometries,
|
|
110
|
+
textures: Textures,
|
|
111
|
+
filters: Filters,
|
|
112
|
+
keeps: Keeps,
|
|
113
|
+
target: Target,
|
|
114
|
+
width: number,
|
|
115
|
+
height: number,
|
|
116
|
+
samples: number,
|
|
117
|
+
): Uint8Array;
|
|
118
|
+
/**
|
|
119
|
+
* The commands drawn as `snapshot` draws them, then composited source
|
|
120
|
+
* over into texture `id` at (x, y), where they stay; `opaque` for an
|
|
121
|
+
* opaque store's texture. Drawn `margin` pixels wider each way, only what
|
|
122
|
+
* lies within composited. Whether it was drawn.
|
|
123
|
+
*/
|
|
124
|
+
drawIntoTexture(
|
|
125
|
+
commands: Commands,
|
|
126
|
+
scene: SceneList,
|
|
127
|
+
rows: Rows,
|
|
128
|
+
geometries: Geometries,
|
|
129
|
+
textures: Textures,
|
|
130
|
+
filters: Filters,
|
|
131
|
+
keeps: Keeps,
|
|
132
|
+
target: Target,
|
|
133
|
+
width: number,
|
|
134
|
+
height: number,
|
|
135
|
+
samples: number,
|
|
136
|
+
id: number,
|
|
137
|
+
x: number,
|
|
138
|
+
y: number,
|
|
139
|
+
opaque: boolean,
|
|
140
|
+
margin?: number,
|
|
141
|
+
): boolean;
|
|
142
|
+
/** Texture `id`'s `width` by `height` texels read back, premultiplied RGBA; null where it cannot. */
|
|
143
|
+
readTexture(id: number, width: number, height: number): Uint8Array | null;
|
|
144
|
+
/** Texture `id`'s rectangle cleared to `pixel`, premultiplied ARGB; whether it was. */
|
|
145
|
+
fillTexture(
|
|
146
|
+
textures: Textures,
|
|
147
|
+
id: number,
|
|
148
|
+
x: number,
|
|
149
|
+
y: number,
|
|
150
|
+
width: number,
|
|
151
|
+
height: number,
|
|
152
|
+
pixel: number,
|
|
153
|
+
): boolean;
|
|
154
|
+
/** Texture `from`'s rectangle copied to (dx, dy) of texture `to`; whether it was. */
|
|
155
|
+
copyTexture(
|
|
156
|
+
textures: Textures,
|
|
157
|
+
from: number,
|
|
158
|
+
x: number,
|
|
159
|
+
y: number,
|
|
160
|
+
width: number,
|
|
161
|
+
height: number,
|
|
162
|
+
to: number,
|
|
163
|
+
dx: number,
|
|
164
|
+
dy: number,
|
|
165
|
+
): boolean;
|
|
166
|
+
/**
|
|
167
|
+
* A 3D object's bitmap drawn (PLANE): the commands drawn as `snapshot`
|
|
168
|
+
* draws them, and kept as kept output `keep` (Keeps).
|
|
169
|
+
*/
|
|
170
|
+
drawPlane(
|
|
171
|
+
keep: number,
|
|
172
|
+
commands: Commands,
|
|
173
|
+
scene: SceneList,
|
|
174
|
+
rows: Rows,
|
|
175
|
+
geometries: Geometries,
|
|
176
|
+
textures: Textures,
|
|
177
|
+
filters: Filters,
|
|
178
|
+
keeps: Keeps,
|
|
179
|
+
target: Target,
|
|
180
|
+
width: number,
|
|
181
|
+
height: number,
|
|
182
|
+
samples: number,
|
|
183
|
+
): void;
|
|
184
|
+
/** Advanced text's outlines, the first `count` floats of `data`, as of `epoch`. */
|
|
185
|
+
uploadOutlines(data: Float32Array, count: number, epoch: number): void;
|
|
186
|
+
/** What it made, let go of. */
|
|
187
|
+
destroy(): void;
|
|
188
|
+
}
|
package/src/filter-plan.ts
CHANGED
|
@@ -35,6 +35,10 @@ export const CONVOLUTION_PASS = 8;
|
|
|
35
35
|
export const DISPLACE_PASS = 9;
|
|
36
36
|
/** A Pixel Bender kernel's pass, its program FilterPass.kernel. */
|
|
37
37
|
export const KERNEL_PASS = 10;
|
|
38
|
+
/** The input at half its size each way, each texel the mean of the four it covers. */
|
|
39
|
+
export const HALVE_PASS = 11;
|
|
40
|
+
/** The input over a frame a whole number of times its size, each texel that of the one it lies in. */
|
|
41
|
+
export const BACK_PASS = 12;
|
|
38
42
|
|
|
39
43
|
/**
|
|
40
44
|
* How a uniform's values are set: one to four floats, an array of vec4s, a
|
|
@@ -252,6 +256,14 @@ export interface KernelObject {
|
|
|
252
256
|
* input as it is, as one that changes nothing does; `links` whether a
|
|
253
257
|
* kernel's pass's fragment shader links, as one that does not leaves it
|
|
254
258
|
* too; `object` where the layer's object lies in it.
|
|
259
|
+
*
|
|
260
|
+
* A layer of `pixel` texels a side to a pixel its filters run at (a draw
|
|
261
|
+
* into a bitmap, at its samples: Target.filterPixel), a power of two and
|
|
262
|
+
* its sides multiples of it, is halved to those pixels first, as the
|
|
263
|
+
* draw's output is halved; the chain runs over them, at `resolution` of
|
|
264
|
+
* them a unit; and its output is drawn back over the layer's frame, each
|
|
265
|
+
* texel its pixel's, which the draw's own halving then gives back as it
|
|
266
|
+
* is. Flash filters what it draws into a bitmap at the bitmap's pixels.
|
|
255
267
|
*/
|
|
256
268
|
export function planChain(
|
|
257
269
|
plan: FilterPlan,
|
|
@@ -263,13 +275,34 @@ export function planChain(
|
|
|
263
275
|
hasTexture: (id: number) => boolean,
|
|
264
276
|
links: (fragment: string) => boolean = () => true,
|
|
265
277
|
object: KernelObject | null = null,
|
|
278
|
+
pixel = 1,
|
|
266
279
|
): void {
|
|
267
280
|
plan.reset(width, height, resolution);
|
|
281
|
+
let w = width;
|
|
282
|
+
let h = height;
|
|
283
|
+
let within = object;
|
|
284
|
+
|
|
285
|
+
for (let k = pixel; k > 1; k /= 2) {
|
|
286
|
+
w = Math.ceil(w / 2);
|
|
287
|
+
h = Math.ceil(h / 2);
|
|
288
|
+
const half = plan.slot(w, h);
|
|
289
|
+
plan.pass(HALVE_PASS, plan.result, w * 2, h * 2, half, w, h);
|
|
290
|
+
plan.result = half;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (pixel > 1 && object) {
|
|
294
|
+
coarseObject.x = object.x / pixel;
|
|
295
|
+
coarseObject.y = object.y / pixel;
|
|
296
|
+
coarseObject.width = object.width / pixel;
|
|
297
|
+
coarseObject.height = object.height / pixel;
|
|
298
|
+
within = coarseObject;
|
|
299
|
+
}
|
|
300
|
+
|
|
268
301
|
const end = filters.chains[chain + 1];
|
|
269
302
|
for (let i = filters.chains[chain]; i < end; i++) {
|
|
270
303
|
plan.result =
|
|
271
304
|
filters.kind[i] === SHADER
|
|
272
|
-
? shader(plan, filters, i, plan.result,
|
|
305
|
+
? shader(plan, filters, i, plan.result, w, h, hasTexture, links, within)
|
|
273
306
|
: planFilter(
|
|
274
307
|
plan,
|
|
275
308
|
filters.kind[i],
|
|
@@ -277,13 +310,22 @@ export function planChain(
|
|
|
277
310
|
filters.start[i],
|
|
278
311
|
filters.texture[i],
|
|
279
312
|
plan.result,
|
|
280
|
-
|
|
281
|
-
|
|
313
|
+
w,
|
|
314
|
+
h,
|
|
282
315
|
hasTexture,
|
|
283
316
|
);
|
|
284
317
|
}
|
|
318
|
+
|
|
319
|
+
if (pixel > 1) {
|
|
320
|
+
const out = plan.slot(width, height);
|
|
321
|
+
plan.pass(BACK_PASS, plan.result, w, h, out, width, height);
|
|
322
|
+
plan.result = out;
|
|
323
|
+
}
|
|
285
324
|
}
|
|
286
325
|
|
|
326
|
+
/** A layer's object in the pixels its filters run at (planChain's `pixel`). */
|
|
327
|
+
const coarseObject: KernelObject = { x: 0, y: 0, width: 0, height: 0 };
|
|
328
|
+
|
|
287
329
|
/** One filter, its values from `v[at]`, over slot `input`: the slot it drew into, or `input`. */
|
|
288
330
|
function planFilter(
|
|
289
331
|
plan: FilterPlan,
|
package/src/index.ts
CHANGED
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
|
|
package/src/webgl/backend.ts
CHANGED
|
@@ -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,
|
|
41
|
+
import { type Layer, type Open, objectOf, TargetPool } from "./targets.js";
|
|
41
42
|
|
|
42
|
-
export type { Target } from "
|
|
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
|
-
|
|
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
|
-
|
|
268
|
-
|
|
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
|
/**
|
package/src/webgl/draws.ts
CHANGED
|
@@ -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
|
|
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
|