@babylonjs/lite-gl 1.20.0 → 1.20.1
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/README.md +58 -19
- package/context.js +2 -0
- package/context.js.map +1 -1
- package/depth-stencil.d.ts +1 -2
- package/depth-stencil.js +2 -2
- package/depth-stencil.js.map +1 -1
- package/dynamic-texture.d.ts +18 -18
- package/dynamic-texture.js +28 -24
- package/dynamic-texture.js.map +1 -1
- package/effect-ready.d.ts +1 -3
- package/effect-ready.js +3 -3
- package/effect-ready.js.map +1 -1
- package/effect-renderer.d.ts +39 -12
- package/effect-renderer.js +47 -17
- package/effect-renderer.js.map +1 -1
- package/effect.d.ts +25 -21
- package/effect.js +69 -51
- package/effect.js.map +1 -1
- package/gpu-frame-time.d.ts +49 -0
- package/gpu-frame-time.js +130 -0
- package/gpu-frame-time.js.map +1 -0
- package/html-texture.d.ts +24 -34
- package/html-texture.js +34 -55
- package/html-texture.js.map +1 -1
- package/index.d.ts +9 -8
- package/index.js +9 -6
- package/index.js.map +1 -1
- package/mesh.d.ts +8 -14
- package/mesh.js +23 -19
- package/mesh.js.map +1 -1
- package/package.json +3 -3
- package/render-loop.d.ts +14 -0
- package/render-loop.js +23 -0
- package/render-loop.js.map +1 -1
- package/render-target.d.ts +109 -39
- package/render-target.js +136 -58
- package/render-target.js.map +1 -1
- package/sprites.d.ts +8 -2
- package/sprites.js +17 -13
- package/sprites.js.map +1 -1
- package/state.d.ts +1 -1
- package/state.js.map +1 -1
- package/text/_gpu/text-effect.js +5 -5
- package/text/_gpu/text-effect.js.map +1 -1
- package/text/_gpu/text-instances.js +4 -4
- package/text/_gpu/text-instances.js.map +1 -1
- package/text/_gpu/text-textures.js +5 -3
- package/text/_gpu/text-textures.js.map +1 -1
- package/text/glyph-storage.js +3 -3
- package/text/glyph-storage.js.map +1 -1
- package/text/text-data.js +2 -2
- package/text/text-data.js.map +1 -1
- package/text/text-renderer.js +11 -11
- package/text/text-renderer.js.map +1 -1
- package/texture-3d.d.ts +10 -7
- package/texture-3d.js +27 -12
- package/texture-3d.js.map +1 -1
- package/texture-source.d.ts +3 -4
- package/texture-source.js +5 -5
- package/texture-source.js.map +1 -1
- package/texture.d.ts +195 -31
- package/texture.js +215 -35
- package/texture.js.map +1 -1
package/dynamic-texture.d.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* internally and replayed into the fresh handle on `webglcontextrestored` by the
|
|
13
13
|
* engine's standard context-restore protocol.
|
|
14
14
|
*/
|
|
15
|
-
import { type GLTexture, type GLTextureOptions } from "./texture.js";
|
|
15
|
+
import { type GLTexture, type GLTextureOptions, type GLTextureUpdateOptions } from "./texture.js";
|
|
16
16
|
import type { GLEngineContext } from "./context.js";
|
|
17
17
|
/** Create a texture backed by an empty (blank) `width × height` RGBA8
|
|
18
18
|
* allocation, whose pixels are pushed on demand with
|
|
@@ -24,36 +24,36 @@ import type { GLEngineContext } from "./context.js";
|
|
|
24
24
|
* {@link updateDynamicTexture} is replayed into the fresh handle on
|
|
25
25
|
* `webglcontextrestored` (and re-blanked if no update happened yet).
|
|
26
26
|
*
|
|
27
|
-
* `options.invertY` is ignored here (the blank allocation is flip-invariant);
|
|
28
|
-
* per-update flip is controlled by {@link updateDynamicTexture}'s `invertY`.
|
|
29
|
-
*
|
|
30
27
|
* @param engine - The engine to create GL resources on.
|
|
31
28
|
* @param width - Texture width in texels (clamped to ≥ 1).
|
|
32
29
|
* @param height - Texture height in texels (clamped to ≥ 1).
|
|
33
|
-
* @param options -
|
|
34
|
-
*
|
|
35
|
-
*
|
|
30
|
+
* @param options - The shared {@link GLTextureOptions}. `invertY` /
|
|
31
|
+
* `premultiplyAlpha` become the defaults of every {@link updateDynamicTexture}
|
|
32
|
+
* call that omits them. For mipmaps, call `generateTextureMipMaps` once; every
|
|
33
|
+
* later update rebuilds the chain.
|
|
36
34
|
* @returns The new {@link GLTexture}. */
|
|
37
|
-
export declare function createDynamicTexture(engine: GLEngineContext, width: number, height: number, options?: GLTextureOptions
|
|
38
|
-
generateMipMaps?: boolean;
|
|
39
|
-
}): GLTexture;
|
|
35
|
+
export declare function createDynamicTexture(engine: GLEngineContext, width: number, height: number, options?: GLTextureOptions): GLTexture;
|
|
40
36
|
/** Push 2D pixels into a {@link createDynamicTexture} texture from a canvas /
|
|
41
37
|
* `OffscreenCanvas` / image / video / `ImageBitmap` / `ImageData` source — the
|
|
42
38
|
* WebGL counterpart of Babylon's `ThinEngine.updateDynamicTexture`. The source
|
|
43
|
-
* is retained on the texture so it is replayed on `webglcontextrestored
|
|
44
|
-
*
|
|
39
|
+
* is retained on the texture so it is replayed on `webglcontextrestored`, and
|
|
40
|
+
* `tex.width` / `tex.height` follow the source's size (`texImage2D` resizes
|
|
41
|
+
* the storage). No-op when the context is lost/disposed or the texture is
|
|
42
|
+
* disposed.
|
|
45
43
|
*
|
|
46
|
-
* @param engine - The engine that owns the texture.
|
|
47
44
|
* @param tex - A texture created by {@link createDynamicTexture}.
|
|
48
45
|
* @param source - The 2D pixel source to upload.
|
|
49
|
-
* @param
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
export declare function updateDynamicTexture(
|
|
46
|
+
* @param options - Per-update `invertY` / `premultiplyAlpha`; each defaults to
|
|
47
|
+
* the texture's creation value (like Babylon's `texture.invertY` fallback),
|
|
48
|
+
* itself `false` by default. */
|
|
49
|
+
export declare function updateDynamicTexture(tex: GLTexture, source: TexImageSource, options?: GLTextureUpdateOptions): void;
|
|
53
50
|
/** Forget the source a dynamic texture retained for context-restore replay.
|
|
54
51
|
* The current GPU pixels are kept (no re-upload), but the source reference is
|
|
55
52
|
* dropped so it can be garbage-collected — and a subsequent
|
|
56
53
|
* `webglcontextrestored` will re-blank the texture instead of replaying. Use
|
|
57
54
|
* after the final {@link updateDynamicTexture} once the source (e.g. a large
|
|
58
|
-
* atlas canvas) is no longer needed. No-op on a non-dynamic texture.
|
|
55
|
+
* atlas canvas) is no longer needed. No-op on a non-dynamic texture. No GL
|
|
56
|
+
* call is made.
|
|
57
|
+
*
|
|
58
|
+
* @param tex - A texture created by {@link createDynamicTexture}. */
|
|
59
59
|
export declare function clearDynamicTextureSource(tex: GLTexture): void;
|
package/dynamic-texture.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* internally and replayed into the fresh handle on `webglcontextrestored` by the
|
|
13
13
|
* engine's standard context-restore protocol.
|
|
14
14
|
*/
|
|
15
|
-
import { bindTextureForUpload, setBoundTextureParams, setUnpackState } from "./texture.js";
|
|
15
|
+
import { bindTextureForUpload, setTexImageSourceSize, setBoundTextureParams, setUnpackState, } from "./texture.js";
|
|
16
16
|
/** Create a texture backed by an empty (blank) `width × height` RGBA8
|
|
17
17
|
* allocation, whose pixels are pushed on demand with
|
|
18
18
|
* {@link updateDynamicTexture} — the WebGL counterpart of Babylon's
|
|
@@ -23,15 +23,13 @@ import { bindTextureForUpload, setBoundTextureParams, setUnpackState } from "./t
|
|
|
23
23
|
* {@link updateDynamicTexture} is replayed into the fresh handle on
|
|
24
24
|
* `webglcontextrestored` (and re-blanked if no update happened yet).
|
|
25
25
|
*
|
|
26
|
-
* `options.invertY` is ignored here (the blank allocation is flip-invariant);
|
|
27
|
-
* per-update flip is controlled by {@link updateDynamicTexture}'s `invertY`.
|
|
28
|
-
*
|
|
29
26
|
* @param engine - The engine to create GL resources on.
|
|
30
27
|
* @param width - Texture width in texels (clamped to ≥ 1).
|
|
31
28
|
* @param height - Texture height in texels (clamped to ≥ 1).
|
|
32
|
-
* @param options -
|
|
33
|
-
*
|
|
34
|
-
*
|
|
29
|
+
* @param options - The shared {@link GLTextureOptions}. `invertY` /
|
|
30
|
+
* `premultiplyAlpha` become the defaults of every {@link updateDynamicTexture}
|
|
31
|
+
* call that omits them. For mipmaps, call `generateTextureMipMaps` once; every
|
|
32
|
+
* later update rebuilds the chain.
|
|
35
33
|
* @returns The new {@link GLTexture}. */
|
|
36
34
|
export function createDynamicTexture(engine, width, height, options) {
|
|
37
35
|
const gl = engine.gl;
|
|
@@ -46,7 +44,8 @@ export function createDynamicTexture(engine, width, height, options) {
|
|
|
46
44
|
const magFilter = opts.magFilter ?? gl.LINEAR;
|
|
47
45
|
const wrapS = opts.wrapS ?? gl.CLAMP_TO_EDGE;
|
|
48
46
|
const wrapT = opts.wrapT ?? gl.CLAMP_TO_EDGE;
|
|
49
|
-
const
|
|
47
|
+
const invertY = opts.invertY ?? false;
|
|
48
|
+
const premultiplyAlpha = opts.premultiplyAlpha ?? false;
|
|
50
49
|
// Single `_upload` closure used for both the initial allocation AND every
|
|
51
50
|
// `webglcontextrestored` replay AND every `updateDynamicTexture`. The pixel
|
|
52
51
|
// upload runs every time; the four `texParameteri` calls run ONLY when the
|
|
@@ -58,20 +57,21 @@ export function createDynamicTexture(engine, width, height, options) {
|
|
|
58
57
|
bindTextureForUpload(target, tex.handle);
|
|
59
58
|
const src = tex._dynSource;
|
|
60
59
|
if (src !== null && src !== undefined) {
|
|
61
|
-
setUnpackState(target, tex.
|
|
60
|
+
setUnpackState(target, tex._invertY ?? invertY, tex._premultiplyAlpha ?? premultiplyAlpha);
|
|
62
61
|
g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, g.RGBA, g.UNSIGNED_BYTE, src);
|
|
62
|
+
// `texImage2D(source)` resizes the GL storage to the source size.
|
|
63
|
+
setTexImageSourceSize(tex, src);
|
|
63
64
|
}
|
|
64
65
|
else {
|
|
65
66
|
setUnpackState(target, false, false);
|
|
66
67
|
g.texImage2D(g.TEXTURE_2D, 0, g.RGBA8, w, h, 0, g.RGBA, g.UNSIGNED_BYTE, null);
|
|
68
|
+
tex.width = w;
|
|
69
|
+
tex.height = h;
|
|
67
70
|
}
|
|
68
71
|
if (tex._dynParamsHandle !== tex.handle) {
|
|
69
72
|
setBoundTextureParams(g, minFilter, magFilter, wrapS, wrapT);
|
|
70
73
|
tex._dynParamsHandle = tex.handle;
|
|
71
74
|
}
|
|
72
|
-
if (generateMipMaps && src !== null && src !== undefined) {
|
|
73
|
-
g.generateMipmap(g.TEXTURE_2D);
|
|
74
|
-
}
|
|
75
75
|
};
|
|
76
76
|
const tex = {
|
|
77
77
|
handle,
|
|
@@ -79,13 +79,12 @@ export function createDynamicTexture(engine, width, height, options) {
|
|
|
79
79
|
width: w,
|
|
80
80
|
height: h,
|
|
81
81
|
isReady: true,
|
|
82
|
+
_engine: engine,
|
|
82
83
|
_disposed: false,
|
|
83
84
|
_refCount: 1,
|
|
84
85
|
_upload: upload,
|
|
85
86
|
_wasReady: true,
|
|
86
87
|
_dynSource: null,
|
|
87
|
-
_dynInvertY: false,
|
|
88
|
-
_dynPremultiplyAlpha: false,
|
|
89
88
|
};
|
|
90
89
|
upload(engine);
|
|
91
90
|
engine._textures.add(tex);
|
|
@@ -94,22 +93,24 @@ export function createDynamicTexture(engine, width, height, options) {
|
|
|
94
93
|
/** Push 2D pixels into a {@link createDynamicTexture} texture from a canvas /
|
|
95
94
|
* `OffscreenCanvas` / image / video / `ImageBitmap` / `ImageData` source — the
|
|
96
95
|
* WebGL counterpart of Babylon's `ThinEngine.updateDynamicTexture`. The source
|
|
97
|
-
* is retained on the texture so it is replayed on `webglcontextrestored
|
|
98
|
-
*
|
|
96
|
+
* is retained on the texture so it is replayed on `webglcontextrestored`, and
|
|
97
|
+
* `tex.width` / `tex.height` follow the source's size (`texImage2D` resizes
|
|
98
|
+
* the storage). No-op when the context is lost/disposed or the texture is
|
|
99
|
+
* disposed.
|
|
99
100
|
*
|
|
100
|
-
* @param engine - The engine that owns the texture.
|
|
101
101
|
* @param tex - A texture created by {@link createDynamicTexture}.
|
|
102
102
|
* @param source - The 2D pixel source to upload.
|
|
103
|
-
* @param
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
export function updateDynamicTexture(
|
|
103
|
+
* @param options - Per-update `invertY` / `premultiplyAlpha`; each defaults to
|
|
104
|
+
* the texture's creation value (like Babylon's `texture.invertY` fallback),
|
|
105
|
+
* itself `false` by default. */
|
|
106
|
+
export function updateDynamicTexture(tex, source, options) {
|
|
107
|
+
const engine = tex._engine;
|
|
107
108
|
if (engine._isLost || engine._disposed || tex._disposed) {
|
|
108
109
|
return;
|
|
109
110
|
}
|
|
110
111
|
tex._dynSource = source;
|
|
111
|
-
tex.
|
|
112
|
-
tex.
|
|
112
|
+
tex._invertY = options?.invertY;
|
|
113
|
+
tex._premultiplyAlpha = options?.premultiplyAlpha;
|
|
113
114
|
tex._upload(engine);
|
|
114
115
|
}
|
|
115
116
|
/** Forget the source a dynamic texture retained for context-restore replay.
|
|
@@ -117,7 +118,10 @@ export function updateDynamicTexture(engine, tex, source, invertY = false, premu
|
|
|
117
118
|
* dropped so it can be garbage-collected — and a subsequent
|
|
118
119
|
* `webglcontextrestored` will re-blank the texture instead of replaying. Use
|
|
119
120
|
* after the final {@link updateDynamicTexture} once the source (e.g. a large
|
|
120
|
-
* atlas canvas) is no longer needed. No-op on a non-dynamic texture.
|
|
121
|
+
* atlas canvas) is no longer needed. No-op on a non-dynamic texture. No GL
|
|
122
|
+
* call is made.
|
|
123
|
+
*
|
|
124
|
+
* @param tex - A texture created by {@link createDynamicTexture}. */
|
|
121
125
|
export function clearDynamicTextureSource(tex) {
|
|
122
126
|
tex._dynSource = null;
|
|
123
127
|
}
|
package/dynamic-texture.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-texture.js","sourceRoot":"","sources":["../src/dynamic-texture.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,
|
|
1
|
+
{"version":3,"file":"dynamic-texture.js","sourceRoot":"","sources":["../src/dynamic-texture.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EACH,oBAAoB,EACpB,qBAAqB,EACrB,qBAAqB,EACrB,cAAc,GAIjB,MAAM,cAAc,CAAC;AAGtB;;;;;;;;;;;;;;;;;0CAiB0C;AAC1C,MAAM,UAAU,oBAAoB,CAAC,MAAuB,EAAE,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QACf,UAAU,EAAE,IAAI;KACnB,CAAC;IACF,MAAM,CAAC,MAAM,CAAC,CAAC;IACf,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC1B,OAAO,GAAG,CAAC;AACf,CAAC;AAED;;;;;;;;;;;;kCAYkC;AAClC,MAAM,UAAU,oBAAoB,CAAC,GAAc,EAAE,MAAsB,EAAE,OAAgC;IACzG,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC;IAC3B,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,SAAS,IAAI,GAAG,CAAC,SAAS,EAAE,CAAC;QACtD,OAAO;IACX,CAAC;IACD,GAAG,CAAC,UAAU,GAAG,MAAM,CAAC;IACxB,GAAG,CAAC,QAAQ,GAAG,OAAO,EAAE,OAAO,CAAC;IAChC,GAAG,CAAC,iBAAiB,GAAG,OAAO,EAAE,gBAAgB,CAAC;IAClD,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;AACxB,CAAC;AAED;;;;;;;;sEAQsE;AACtE,MAAM,UAAU,yBAAyB,CAAC,GAAc;IACpD,GAAG,CAAC,UAAU,GAAG,IAAI,CAAC;AAC1B,CAAC","sourcesContent":["/**\n * Dynamic (canvas-/bitmap-backed) textures.\n *\n * Part of the public API via the `@babylonjs/lite-gl` barrel. The package is\n * `sideEffects: false`, so consumers that never push 2D pixel sources into a\n * texture tree-shake this code out.\n *\n * The WebGL counterpart of Babylon's `ThinEngine.createDynamicTexture` +\n * `updateDynamicTexture`. A dynamic texture is a blank RGBA8 allocation whose\n * pixels are pushed on demand from a canvas / `OffscreenCanvas` / image / video\n * / `ImageBitmap` / `ImageData` source. The most recent source is retained\n * internally and replayed into the fresh handle on `webglcontextrestored` by the\n * engine's standard context-restore protocol.\n */\nimport {\n bindTextureForUpload,\n setTexImageSourceSize,\n setBoundTextureParams,\n setUnpackState,\n type GLTexture,\n type GLTextureOptions,\n type GLTextureUpdateOptions,\n} from \"./texture.js\";\nimport type { GLEngineContext } from \"./context.js\";\n\n/** Create a texture backed by an empty (blank) `width × height` RGBA8\n * allocation, whose pixels are pushed on demand with\n * {@link updateDynamicTexture} — the WebGL counterpart of Babylon's\n * `ThinEngine.createDynamicTexture` + `ThinTexture` wrapper.\n *\n * The texture is immediately sampleable (reads as transparent black until the\n * first update). It is context-restore-safe: the most recent source pushed via\n * {@link updateDynamicTexture} is replayed into the fresh handle on\n * `webglcontextrestored` (and re-blanked if no update happened yet).\n *\n * @param engine - The engine to create GL resources on.\n * @param width - Texture width in texels (clamped to ≥ 1).\n * @param height - Texture height in texels (clamped to ≥ 1).\n * @param options - The shared {@link GLTextureOptions}. `invertY` /\n * `premultiplyAlpha` become the defaults of every {@link updateDynamicTexture}\n * call that omits them. For mipmaps, call `generateTextureMipMaps` once; every\n * later update rebuilds the chain.\n * @returns The new {@link GLTexture}. */\nexport function createDynamicTexture(engine: GLEngineContext, width: number, height: number, options?: GLTextureOptions): GLTexture {\n const gl = engine.gl;\n const handle = gl.createTexture();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null\");\n }\n const w = Math.max(1, width | 0);\n const h = Math.max(1, height | 0);\n const opts = options ?? {};\n const minFilter = opts.minFilter ?? gl.LINEAR;\n const magFilter = opts.magFilter ?? gl.LINEAR;\n const wrapS = opts.wrapS ?? gl.CLAMP_TO_EDGE;\n const wrapT = opts.wrapT ?? gl.CLAMP_TO_EDGE;\n const invertY = opts.invertY ?? false;\n const premultiplyAlpha = opts.premultiplyAlpha ?? false;\n\n // Single `_upload` closure used for both the initial allocation AND every\n // `webglcontextrestored` replay AND every `updateDynamicTexture`. The pixel\n // upload runs every time; the four `texParameteri` calls run ONLY when the\n // handle changed (initial create + restore install a fresh, GL-default\n // handle) — a per-frame pixel update keeps the same handle, whose filter/wrap\n // GL retains, so re-applying them would be a pure redundant cost.\n const upload = (target: GLEngineContext): void => {\n const g = target.gl;\n bindTextureForUpload(target, tex.handle);\n const src = tex._dynSource;\n if (src !== null && src !== undefined) {\n setUnpackState(target, tex._invertY ?? invertY, tex._premultiplyAlpha ?? premultiplyAlpha);\n g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, g.RGBA, g.UNSIGNED_BYTE, src);\n // `texImage2D(source)` resizes the GL storage to the source size.\n setTexImageSourceSize(tex, src);\n } else {\n setUnpackState(target, false, false);\n g.texImage2D(g.TEXTURE_2D, 0, g.RGBA8, w, h, 0, g.RGBA, g.UNSIGNED_BYTE, null);\n tex.width = w;\n tex.height = h;\n }\n if (tex._dynParamsHandle !== tex.handle) {\n setBoundTextureParams(g, minFilter, magFilter, wrapS, wrapT);\n tex._dynParamsHandle = tex.handle;\n }\n };\n\n const tex: GLTexture = {\n handle,\n target: gl.TEXTURE_2D,\n width: w,\n height: h,\n isReady: true,\n _engine: engine,\n _disposed: false,\n _refCount: 1,\n _upload: upload,\n _wasReady: true,\n _dynSource: null,\n };\n upload(engine);\n engine._textures.add(tex);\n return tex;\n}\n\n/** Push 2D pixels into a {@link createDynamicTexture} texture from a canvas /\n * `OffscreenCanvas` / image / video / `ImageBitmap` / `ImageData` source — the\n * WebGL counterpart of Babylon's `ThinEngine.updateDynamicTexture`. The source\n * is retained on the texture so it is replayed on `webglcontextrestored`, and\n * `tex.width` / `tex.height` follow the source's size (`texImage2D` resizes\n * the storage). No-op when the context is lost/disposed or the texture is\n * disposed.\n *\n * @param tex - A texture created by {@link createDynamicTexture}.\n * @param source - The 2D pixel source to upload.\n * @param options - Per-update `invertY` / `premultiplyAlpha`; each defaults to\n * the texture's creation value (like Babylon's `texture.invertY` fallback),\n * itself `false` by default. */\nexport function updateDynamicTexture(tex: GLTexture, source: TexImageSource, options?: GLTextureUpdateOptions): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed) {\n return;\n }\n tex._dynSource = source;\n tex._invertY = options?.invertY;\n tex._premultiplyAlpha = options?.premultiplyAlpha;\n tex._upload(engine);\n}\n\n/** Forget the source a dynamic texture retained for context-restore replay.\n * The current GPU pixels are kept (no re-upload), but the source reference is\n * dropped so it can be garbage-collected — and a subsequent\n * `webglcontextrestored` will re-blank the texture instead of replaying. Use\n * after the final {@link updateDynamicTexture} once the source (e.g. a large\n * atlas canvas) is no longer needed. No-op on a non-dynamic texture. No GL\n * call is made.\n *\n * @param tex - A texture created by {@link createDynamicTexture}. */\nexport function clearDynamicTextureSource(tex: GLTexture): void {\n tex._dynSource = null;\n}\n"]}
|
package/effect-ready.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { type GLEngineContext } from "./context.js";
|
|
2
1
|
import { type GLEffect } from "./effect.js";
|
|
3
2
|
/** Options for the opt-in `waitForEffect` readiness scheduler. */
|
|
4
3
|
export interface GLEffectWaitOptions {
|
|
@@ -12,9 +11,8 @@ export interface GLEffectWaitOptions {
|
|
|
12
11
|
* on the next poll), or signal cancellation. All exits cancel the pending
|
|
13
12
|
* frame and remove listeners. Call again after context restoration to wait
|
|
14
13
|
* for the replacement program. Scheduling code is tree-shaken when unused.
|
|
15
|
-
* @param engine - Engine owning the effect.
|
|
16
14
|
* @param effect - Effect to finalize.
|
|
17
15
|
* @param options - Optional cancellation signal.
|
|
18
16
|
* @returns The ready effect, or a rejected promise with the failure reason.
|
|
19
17
|
*/
|
|
20
|
-
export declare function waitForEffect(
|
|
18
|
+
export declare function waitForEffect(effect: GLEffect, options?: GLEffectWaitOptions): Promise<GLEffect>;
|
package/effect-ready.js
CHANGED
|
@@ -6,12 +6,12 @@ import { getEffectCompilationError } from "./effect.js";
|
|
|
6
6
|
* on the next poll), or signal cancellation. All exits cancel the pending
|
|
7
7
|
* frame and remove listeners. Call again after context restoration to wait
|
|
8
8
|
* for the replacement program. Scheduling code is tree-shaken when unused.
|
|
9
|
-
* @param engine - Engine owning the effect.
|
|
10
9
|
* @param effect - Effect to finalize.
|
|
11
10
|
* @param options - Optional cancellation signal.
|
|
12
11
|
* @returns The ready effect, or a rejected promise with the failure reason.
|
|
13
12
|
*/
|
|
14
|
-
export function waitForEffect(
|
|
13
|
+
export function waitForEffect(effect, options) {
|
|
14
|
+
const engine = effect._engine;
|
|
15
15
|
return new Promise((resolve, reject) => {
|
|
16
16
|
const signal = options?.signal;
|
|
17
17
|
let frame = null;
|
|
@@ -62,7 +62,7 @@ export function waitForEffect(engine, effect, options) {
|
|
|
62
62
|
lost();
|
|
63
63
|
return;
|
|
64
64
|
}
|
|
65
|
-
const error = getEffectCompilationError(
|
|
65
|
+
const error = getEffectCompilationError(effect);
|
|
66
66
|
// Abort/loss callbacks set settled synchronously during finalization.
|
|
67
67
|
if (settled) {
|
|
68
68
|
return;
|
package/effect-ready.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effect-ready.js","sourceRoot":"","sources":["../src/effect-ready.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"effect-ready.js","sourceRoot":"","sources":["../src/effect-ready.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,yBAAyB,EAAiB,MAAM,aAAa,CAAC;AASvE;;;;;;;;;GASG;AACH,MAAM,UAAU,aAAa,CAAC,MAAgB,EAAE,OAA6B;IACzE,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;IAC9B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACnC,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC;QAC/B,IAAI,KAAK,GAAkB,IAAI,CAAC;QAChC,IAAI,OAAO,GAAG,KAAK,CAAC;QAEpB,MAAM,OAAO,GAAG,GAAS,EAAE;YACvB,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACjB,oBAAoB,CAAC,KAAK,CAAC,CAAC;gBAC5B,KAAK,GAAG,IAAI,CAAC;YACjB,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CAAC","sourcesContent":["import { offContextLost, onContextLost } from \"./context.js\";\nimport { getEffectCompilationError, type GLEffect } from \"./effect.js\";\n\n/** Options for the opt-in `waitForEffect` readiness scheduler. */\nexport interface GLEffectWaitOptions {\n /** Cancels only this wait. Error reasons are preserved; other reasons are\n * the cause of a native AbortError DOMException. Does not dispose or cancel the effect. */\n signal?: AbortSignal;\n}\n\n/** Wait for readiness without starting a render loop. Polls immediately and\n * then once per animation frame; resolves with the finalized effect.\n * Rejects on compile/link/restore failure, context loss, disposal (observed\n * on the next poll), or signal cancellation. All exits cancel the pending\n * frame and remove listeners. Call again after context restoration to wait\n * for the replacement program. Scheduling code is tree-shaken when unused.\n * @param effect - Effect to finalize.\n * @param options - Optional cancellation signal.\n * @returns The ready effect, or a rejected promise with the failure reason.\n */\nexport function waitForEffect(effect: GLEffect, options?: GLEffectWaitOptions): Promise<GLEffect> {\n const engine = effect._engine;\n return new Promise((resolve, reject) => {\n const signal = options?.signal;\n let frame: number | null = null;\n let settled = false;\n\n const cleanup = (): void => {\n settled = true;\n if (frame !== null) {\n cancelAnimationFrame(frame);\n frame = null;\n }\n offContextLost(engine, lost);\n signal?.removeEventListener(\"abort\", abort);\n };\n const fail = (error: Error): void => {\n if (settled) {\n return;\n }\n cleanup();\n reject(error);\n };\n const lost = (): void => fail(new Error(`lite-gl: ${effect.name} context lost while waiting for compilation`));\n const abort = (): void => {\n const reason: unknown = signal?.reason;\n if (reason instanceof Error) {\n fail(reason);\n } else {\n fail(Object.assign(new DOMException(`lite-gl: ${effect.name} compilation wait aborted`, \"AbortError\"), { cause: reason }));\n }\n };\n const poll = (): void => {\n frame = null;\n if (settled) {\n return;\n }\n try {\n // Registering an abort listener does not replay a prior abort.\n if (signal?.aborted) {\n abort();\n return;\n }\n // Disposal has no callback; these guards also cover already-disposed/lost inputs.\n if (engine._disposed || effect._disposed) {\n fail(new Error(`lite-gl: ${effect.name} disposed while waiting for compilation`));\n return;\n }\n if (engine._isLost) {\n lost();\n return;\n }\n const error = getEffectCompilationError(effect);\n // Abort/loss callbacks set settled synchronously during finalization.\n if (settled) {\n return;\n }\n // Finalization callbacks can dispose without any notification.\n if (engine._disposed || effect._disposed) {\n fail(new Error(`lite-gl: ${effect.name} disposed while waiting for compilation`));\n } else if (error !== null) {\n fail(new Error(`lite-gl: ${effect.name} ${error}`));\n } else if (effect.isReady) {\n cleanup();\n resolve(effect);\n } else {\n frame = requestAnimationFrame(poll);\n }\n } catch (error) {\n fail(error instanceof Error ? error : new Error(`lite-gl: ${effect.name} compilation wait failed`, { cause: error }));\n }\n };\n\n onContextLost(engine, lost);\n signal?.addEventListener(\"abort\", abort, { once: true });\n poll();\n });\n}\n"]}
|
package/effect-renderer.d.ts
CHANGED
|
@@ -23,9 +23,9 @@ export interface GLEffectWrapperOptions {
|
|
|
23
23
|
defines?: string;
|
|
24
24
|
}
|
|
25
25
|
/** A reusable fullscreen effect that compiles and OWNS its `GLEffect` — the
|
|
26
|
-
* WebGL counterpart of lite's `EffectWrapper`.
|
|
27
|
-
*
|
|
28
|
-
*
|
|
26
|
+
* WebGL counterpart of lite's `EffectWrapper`. Like lite, the wrapper functions
|
|
27
|
+
* take the wrapper alone (the engine is derived from its effect):
|
|
28
|
+
* `applyEffectWrapper(wrapper)` / `disposeEffectWrapper(wrapper)`. */
|
|
29
29
|
export interface GLEffectWrapper {
|
|
30
30
|
/** Name alias for the wrapper (and its effect). */
|
|
31
31
|
readonly name: string;
|
|
@@ -39,8 +39,9 @@ export interface GLEffectWrapper {
|
|
|
39
39
|
* When `vertexSource` is omitted, a built-in fullscreen-quad vertex shader is
|
|
40
40
|
* used, so callers can supply only `fragmentSource`. */
|
|
41
41
|
export declare function createEffectWrapper(engine: GLEngineContext, options: GLEffectWrapperOptions): GLEffectWrapper;
|
|
42
|
-
/** Dispose the wrapper and the effect it owns (idempotent).
|
|
43
|
-
*
|
|
42
|
+
/** Dispose the wrapper and the effect it owns (idempotent).
|
|
43
|
+
*
|
|
44
|
+
* @param wrapper - The wrapper to dispose. */
|
|
44
45
|
export declare function disposeEffectWrapper(wrapper: GLEffectWrapper): void;
|
|
45
46
|
/** Pixel-space viewport rectangle passed to `setViewport`. */
|
|
46
47
|
export interface GLViewport {
|
|
@@ -55,11 +56,37 @@ export interface GLViewport {
|
|
|
55
56
|
}
|
|
56
57
|
/** Cached `gl.viewport`. Defaults to the full canvas in pixel coordinates. */
|
|
57
58
|
export declare function setViewport(engine: GLEngineContext, viewport?: GLViewport): void;
|
|
58
|
-
/** Make `wrapper.effect` current
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
*
|
|
59
|
+
/** Make `wrapper.effect` current — the counterpart of Babylon's
|
|
60
|
+
* `EffectRenderer.applyEffectWrapper` (lite-gl has no `setEffectWrapper`;
|
|
61
|
+
* this is the only wrapper "activate" call). The shared fullscreen quad is
|
|
62
|
+
* bound by {@link drawEffect}, so an existing `GLEffect` needs no wrapper:
|
|
63
|
+
* `if (isEffectReady(e)) { useEffect(e); …setters…; drawEffect(engine); }`.
|
|
64
|
+
*
|
|
65
|
+
* The wrapper's effect must be ready: this polls {@link isEffectReady} itself
|
|
66
|
+
* (finalizing the effect on its first ready poll) and returns `false` without
|
|
67
|
+
* binding anything while the shaders are still compiling, failed to compile,
|
|
68
|
+
* or the context is lost/disposed — skip the frame's `setEffect*` /
|
|
69
|
+
* `drawEffect` calls in that case, otherwise they target whatever program
|
|
70
|
+
* was current before.
|
|
71
|
+
*
|
|
72
|
+
* This MUST be called BEFORE any `setEffect*` call for the same effect in the
|
|
73
|
+
* current frame (uniform setters write to the currently bound program).
|
|
74
|
+
* Typical frame:
|
|
75
|
+
* `if (!applyEffectWrapper(w)) return;` →
|
|
76
|
+
* `setEffectFloat(w.effect, …)` → `drawEffect(engine)`.
|
|
77
|
+
*
|
|
78
|
+
* @param wrapper - The wrapper whose effect to make current.
|
|
79
|
+
* @returns `true` when the effect is ready and now current; `false` when
|
|
80
|
+
* nothing was bound. */
|
|
81
|
+
export declare function applyEffectWrapper(wrapper: GLEffectWrapper): boolean;
|
|
82
|
+
/** Draw the shared fullscreen quad with the current program (from
|
|
83
|
+
* {@link applyEffectWrapper} or `useEffect`) — the counterpart of Babylon's
|
|
84
|
+
* `EffectRenderer.draw`. Lazily creates the quad on first use (and after
|
|
85
|
+
* context restore), binds its VAO (cached), flushes the deferred GL states and
|
|
86
|
+
* issues `gl.drawElements(TRIANGLES, 6, UNSIGNED_SHORT, 0)`. The program's
|
|
87
|
+
* vertex shader must read the quad positions at attribute location 0 (as
|
|
88
|
+
* `createEffectWrapper`'s default vertex shader does). No-op when the context
|
|
89
|
+
* is lost/disposed or there is no current program.
|
|
90
|
+
*
|
|
91
|
+
* @param engine - The engine. */
|
|
65
92
|
export declare function drawEffect(engine: GLEngineContext): void;
|
package/effect-renderer.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createEffect, disposeEffect, useEffect } from "./effect.js";
|
|
1
|
+
import { createEffect, disposeEffect, isEffectReady, useEffect } from "./effect.js";
|
|
2
2
|
import { applyGLStates } from "./apply-states.js";
|
|
3
3
|
/** Built-in fullscreen-quad vertex shader, used when `GLEffectWrapperOptions`
|
|
4
4
|
* omits `vertexSource`. Maps the package's fullscreen-quad positions
|
|
@@ -27,16 +27,17 @@ export function createEffectWrapper(engine, options) {
|
|
|
27
27
|
attributeNames: options.attributeNames,
|
|
28
28
|
defines: options.defines,
|
|
29
29
|
});
|
|
30
|
-
return { name, effect,
|
|
30
|
+
return { name, effect, _disposed: false };
|
|
31
31
|
}
|
|
32
|
-
/** Dispose the wrapper and the effect it owns (idempotent).
|
|
33
|
-
*
|
|
32
|
+
/** Dispose the wrapper and the effect it owns (idempotent).
|
|
33
|
+
*
|
|
34
|
+
* @param wrapper - The wrapper to dispose. */
|
|
34
35
|
export function disposeEffectWrapper(wrapper) {
|
|
35
36
|
if (wrapper._disposed) {
|
|
36
37
|
return;
|
|
37
38
|
}
|
|
38
39
|
wrapper._disposed = true;
|
|
39
|
-
disposeEffect(wrapper.
|
|
40
|
+
disposeEffect(wrapper.effect);
|
|
40
41
|
}
|
|
41
42
|
/** Cached `gl.viewport`. Defaults to the full canvas in pixel coordinates. */
|
|
42
43
|
export function setViewport(engine, viewport) {
|
|
@@ -57,20 +58,48 @@ export function setViewport(engine, viewport) {
|
|
|
57
58
|
s.viewportH = h;
|
|
58
59
|
engine.gl.viewport(x, y, w, h);
|
|
59
60
|
}
|
|
60
|
-
/** Make `wrapper.effect` current
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
61
|
+
/** Make `wrapper.effect` current — the counterpart of Babylon's
|
|
62
|
+
* `EffectRenderer.applyEffectWrapper` (lite-gl has no `setEffectWrapper`;
|
|
63
|
+
* this is the only wrapper "activate" call). The shared fullscreen quad is
|
|
64
|
+
* bound by {@link drawEffect}, so an existing `GLEffect` needs no wrapper:
|
|
65
|
+
* `if (isEffectReady(e)) { useEffect(e); …setters…; drawEffect(engine); }`.
|
|
66
|
+
*
|
|
67
|
+
* The wrapper's effect must be ready: this polls {@link isEffectReady} itself
|
|
68
|
+
* (finalizing the effect on its first ready poll) and returns `false` without
|
|
69
|
+
* binding anything while the shaders are still compiling, failed to compile,
|
|
70
|
+
* or the context is lost/disposed — skip the frame's `setEffect*` /
|
|
71
|
+
* `drawEffect` calls in that case, otherwise they target whatever program
|
|
72
|
+
* was current before.
|
|
73
|
+
*
|
|
74
|
+
* This MUST be called BEFORE any `setEffect*` call for the same effect in the
|
|
75
|
+
* current frame (uniform setters write to the currently bound program).
|
|
76
|
+
* Typical frame:
|
|
77
|
+
* `if (!applyEffectWrapper(w)) return;` →
|
|
78
|
+
* `setEffectFloat(w.effect, …)` → `drawEffect(engine)`.
|
|
79
|
+
*
|
|
80
|
+
* @param wrapper - The wrapper whose effect to make current.
|
|
81
|
+
* @returns `true` when the effect is ready and now current; `false` when
|
|
82
|
+
* nothing was bound. */
|
|
64
83
|
export function applyEffectWrapper(wrapper) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
84
|
+
// Bail before polling so a disposed wrapper sharing a live effect can't
|
|
85
|
+
// finalize it (useProgram + callbacks); re-check after the poll because
|
|
86
|
+
// ready callbacks it fires may dispose the wrapper.
|
|
87
|
+
if (wrapper._disposed || !isEffectReady(wrapper.effect) || wrapper._disposed) {
|
|
88
|
+
return false;
|
|
68
89
|
}
|
|
69
|
-
|
|
70
|
-
|
|
90
|
+
useEffect(wrapper.effect);
|
|
91
|
+
return true;
|
|
71
92
|
}
|
|
72
|
-
/**
|
|
73
|
-
*
|
|
93
|
+
/** Draw the shared fullscreen quad with the current program (from
|
|
94
|
+
* {@link applyEffectWrapper} or `useEffect`) — the counterpart of Babylon's
|
|
95
|
+
* `EffectRenderer.draw`. Lazily creates the quad on first use (and after
|
|
96
|
+
* context restore), binds its VAO (cached), flushes the deferred GL states and
|
|
97
|
+
* issues `gl.drawElements(TRIANGLES, 6, UNSIGNED_SHORT, 0)`. The program's
|
|
98
|
+
* vertex shader must read the quad positions at attribute location 0 (as
|
|
99
|
+
* `createEffectWrapper`'s default vertex shader does). No-op when the context
|
|
100
|
+
* is lost/disposed or there is no current program.
|
|
101
|
+
*
|
|
102
|
+
* @param engine - The engine. */
|
|
74
103
|
export function drawEffect(engine) {
|
|
75
104
|
if (engine._isLost || engine._disposed) {
|
|
76
105
|
return;
|
|
@@ -78,13 +107,14 @@ export function drawEffect(engine) {
|
|
|
78
107
|
if (engine._state.currentProgram === null) {
|
|
79
108
|
return;
|
|
80
109
|
}
|
|
110
|
+
ensureQuad(engine);
|
|
81
111
|
applyGLStates(engine);
|
|
82
112
|
engine.gl.drawElements(engine.gl.TRIANGLES, 6, engine.gl.UNSIGNED_SHORT, 0);
|
|
83
113
|
}
|
|
84
114
|
/** Lazy fullscreen quad. Built on first call; thereafter the VAO is cached on
|
|
85
115
|
* `_state.quadVao` and rebinding is a single cached call. Cleared by
|
|
86
116
|
* `webglcontextlost` and transparently rebuilt by the next
|
|
87
|
-
* `
|
|
117
|
+
* `drawEffect` after restore.
|
|
88
118
|
*
|
|
89
119
|
* Position attribute is enabled at location 0 — every effect's
|
|
90
120
|
* `createEffect` calls `gl.bindAttribLocation(program, 0, attributeNames[0])`
|
package/effect-renderer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effect-renderer.js","sourceRoot":"","sources":["../src/effect-renderer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAiB,SAAS,EAAE,MAAM,aAAa,CAAC;AACpF,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD;;;;6BAI6B;AAC7B,MAAM,gCAAgC,GAAG;;;;;;EAMvC,CAAC;AA0CH;;;yDAGyD;AACzD,MAAM,UAAU,mBAAmB,CAAC,MAAuB,EAAE,OAA+B;IACxF,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,gBAAgB,CAAC;IAC9C,MAAM,MAAM,GAAG,YAAY,CAAC,MAAM,EAAE;QAChC,IAAI;QACJ,YAAY,EAAE,OAAO,CAAC,YAAY,IAAI,gCAAgC;QACtE,cAAc,EAAE,OAAO,CAAC,cAAc;QACtC,YAAY,EAAE,OAAO,CAAC,YAAY,IAAI,EAAE;QACxC,YAAY,EAAE,OAAO,CAAC,YAAY,IAAI,EAAE;QACxC,cAAc,EAAE,OAAO,CAAC,cAAc;QACtC,OAAO,EAAE,OAAO,CAAC,OAAO;KAC3B,CAAC,CAAC;IACH,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AAC/D,CAAC;AAED;uCACuC;AACvC,MAAM,UAAU,oBAAoB,CAAC,OAAwB;IACzD,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;QACpB,OAAO;IACX,CAAC;IACD,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;IACzB,aAAa,CAAC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CAAC;AACtE,MAAM,YAAY,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC","sourcesContent":["import type { GLEngineContext } from \"./context.js\";\nimport { createEffect, disposeEffect, type GLEffect, useEffect } from \"./effect.js\";\nimport { applyGLStates } from \"./apply-states.js\";\n\n/** Built-in fullscreen-quad vertex shader, used when `GLEffectWrapperOptions`\n * omits `vertexSource`. Maps the package's fullscreen-quad positions\n * (attribute location 0) to clip space and forwards a 0..1 `vUv` varying —\n * the WebGL counterpart of lite's default `vertexWGSL`, so callers can pass\n * only `fragmentSource`. */\nconst DEFAULT_FULLSCREEN_VERTEX_SOURCE = `#version 300 es\nlayout(location = 0) in vec2 position;\nout vec2 vUv;\nvoid main() {\n vUv = position * 0.5 + 0.5;\n gl_Position = vec4(position, 0.0, 1.0);\n}`;\n\n/** Inputs to `createEffectWrapper`. Mirrors lite's `EffectWrapperOptions`: the\n * wrapper compiles and OWNS the effect built from this shader source. */\nexport interface GLEffectWrapperOptions {\n /** Human-readable label for the wrapper and its effect. Defaults to\n * `\"effect-wrapper\"`. */\n name?: string;\n /** GLSL ES 3.00 vertex source. Defaults to a built-in fullscreen-quad\n * vertex shader (exposing a `vUv` varying), mirroring lite's default\n * `vertexWGSL`. */\n vertexSource?: string;\n /** GLSL ES 3.00 fragment source (≙ lite's `fragmentWGSL`). Required. */\n fragmentSource: string;\n /** Declared uniform names. Defaults to none. */\n uniformNames?: readonly string[];\n /** Declared sampler names, in unit-assignment order. Defaults to none. */\n samplerNames?: readonly string[];\n /** Attribute names; the first is bound to location 0. Defaults to\n * `[\"position\"]`. */\n attributeNames?: readonly string[];\n /** Optional `#define` block prepended to both shader stages. */\n defines?: string;\n}\n\n/** A reusable fullscreen effect that compiles and OWNS its `GLEffect` — the\n * WebGL counterpart of lite's `EffectWrapper`. The wrapper retains the engine\n * it was created for, so `disposeEffectWrapper` / `applyEffectWrapper` take\n * only the wrapper. */\nexport interface GLEffectWrapper {\n /** Name alias for the wrapper (and its effect). */\n readonly name: string;\n /** The compiled effect this wrapper owns. Exposed so the per-uniform\n * setters (`setEffectFloat`/`setEffectTexture`/…) can target it — the\n * WebGL divergence from lite's UBO-based `setEffectUniforms(wrapper, …)`. */\n readonly effect: GLEffect;\n /** @internal The engine the wrapper was created for. */\n _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n}\n\n/** Compile a fullscreen effect from shader source and wrap it; the wrapper OWNS\n * the resulting `GLEffect`. Mirrors lite's `createEffectWrapper(engine, options)`.\n * When `vertexSource` is omitted, a built-in fullscreen-quad vertex shader is\n * used, so callers can supply only `fragmentSource`. */\nexport function createEffectWrapper(engine: GLEngineContext, options: GLEffectWrapperOptions): GLEffectWrapper {\n const name = options.name ?? \"effect-wrapper\";\n const effect = createEffect(engine, {\n name,\n vertexSource: options.vertexSource ?? DEFAULT_FULLSCREEN_VERTEX_SOURCE,\n fragmentSource: options.fragmentSource,\n uniformNames: options.uniformNames ?? [],\n samplerNames: options.samplerNames ?? [],\n attributeNames: options.attributeNames,\n defines: options.defines,\n });\n return { name, effect, _engine: engine, _disposed: false };\n}\n\n/** Dispose the wrapper and the effect it owns (idempotent). Mirrors lite's\n * `disposeEffectWrapper(wrapper)`. */\nexport function disposeEffectWrapper(wrapper: GLEffectWrapper): void {\n if (wrapper._disposed) {\n return;\n }\n wrapper._disposed = true;\n disposeEffect(wrapper._engine, wrapper.effect);\n}\n\n/** Pixel-space viewport rectangle passed to `setViewport`. */\nexport interface GLViewport {\n /** Lower-left X origin in physical pixels. */\n x: number;\n /** Lower-left Y origin in physical pixels. */\n y: number;\n /** Width in physical pixels. */\n w: number;\n /** Height in physical pixels. */\n h: number;\n}\n\n/** Cached `gl.viewport`. Defaults to the full canvas in pixel coordinates. */\nexport function setViewport(engine: GLEngineContext, viewport?: GLViewport): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const x = viewport?.x ?? 0;\n const y = viewport?.y ?? 0;\n const w = viewport?.w ?? engine.canvas.width;\n const h = viewport?.h ?? engine.canvas.height;\n const s = engine._state;\n if (s.viewportX === x && s.viewportY === y && s.viewportW === w && s.viewportH === h) {\n return;\n }\n s.viewportX = x;\n s.viewportY = y;\n s.viewportW = w;\n s.viewportH = h;\n engine.gl.viewport(x, y, w, h);\n}\n\n/** Make `wrapper.effect` current and ensure the shared fullscreen quad VAO\n * is bound. This MUST be called BEFORE any `setEffect*` call for the same\n * effect in the current frame (uniform setters write to the currently bound\n * program). */\nexport function applyEffectWrapper(wrapper: GLEffectWrapper): void {\n const engine = wrapper._engine;\n if (engine._isLost || engine._disposed || wrapper._disposed) {\n return;\n }\n ensureQuad(engine);\n useEffect(engine, wrapper.effect);\n}\n\n/** `gl.drawElements(TRIANGLES, 6, UNSIGNED_SHORT, 0)`. No-op when the\n * context is lost or there is no current program. */\nexport function drawEffect(engine: GLEngineContext): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n if (engine._state.currentProgram === null) {\n return;\n }\n applyGLStates(engine);\n engine.gl.drawElements(engine.gl.TRIANGLES, 6, engine.gl.UNSIGNED_SHORT, 0);\n}\n\n/** Lazy fullscreen quad. Built on first call; thereafter the VAO is cached on\n * `_state.quadVao` and rebinding is a single cached call. Cleared by\n * `webglcontextlost` and transparently rebuilt by the next\n * `applyEffectWrapper` after restore.\n *\n * Position attribute is enabled at location 0 — every effect's\n * `createEffect` calls `gl.bindAttribLocation(program, 0, attributeNames[0])`\n * BEFORE link, so the shared VAO is correct across all programs. */\nfunction ensureQuad(engine: GLEngineContext): void {\n const s = engine._state;\n const gl = engine.gl;\n if (s.quadVao !== null) {\n if (s.boundVao !== s.quadVao) {\n gl.bindVertexArray(s.quadVao);\n s.boundVao = s.quadVao;\n }\n return;\n }\n const vao = gl.createVertexArray();\n if (vao === null) {\n throw new Error(\"lite-gl: gl.createVertexArray returned null\");\n }\n s.quadVao = vao;\n gl.bindVertexArray(vao);\n s.boundVao = vao;\n\n const vbo = gl.createBuffer();\n if (vbo === null) {\n throw new Error(\"lite-gl: gl.createBuffer returned null (VBO)\");\n }\n s.quadVbo = vbo;\n gl.bindBuffer(gl.ARRAY_BUFFER, vbo);\n s.boundArrayBuffer = vbo;\n gl.bufferData(gl.ARRAY_BUFFER, QUAD_POSITIONS, gl.STATIC_DRAW);\n\n const ibo = gl.createBuffer();\n if (ibo === null) {\n throw new Error(\"lite-gl: gl.createBuffer returned null (IBO)\");\n }\n s.quadIbo = ibo;\n gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo);\n s.boundElementBuffer = ibo;\n gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, QUAD_INDICES, gl.STATIC_DRAW);\n\n gl.enableVertexAttribArray(0);\n gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n}\n\n/** Typed-array literal — pure per bundler convention. Matches Babylon's\n * `EffectRenderer` default geometry exactly. */\nconst QUAD_POSITIONS = new Float32Array([1, 1, -1, 1, -1, -1, 1, -1]);\nconst QUAD_INDICES = new Uint16Array([0, 1, 2, 0, 2, 3]);\n"]}
|
|
1
|
+
{"version":3,"file":"effect-renderer.js","sourceRoot":"","sources":["../src/effect-renderer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAiB,aAAa,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACnG,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD;;;;6BAI6B;AAC7B,MAAM,gCAAgC,GAAG;;;;;;EAMvC,CAAC;AAwCH;;;yDAGyD;AACzD,MAAM,UAAU,mBAAmB,CAAC,MAAuB,EAAE,OAA+B;IACxF,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,gBAAgB,CAAC;IAC9C,MAAM,MAAM,GAAG,YAAY,CAAC,MAAM,EAAE;QAChC,IAAI;QACJ,YAAY,EAAE,OAAO,CAAC,YAAY,IAAI,gCAAgC;QACtE,cAAc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AAC,CAAC;IAC5C,CAAC,CAAC,kBAAkB,GAAG,GAAG,CAAC;IAC3B,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC,oBAAoB,EAAE,YAAY,EAAE,EAAE,CAAC,WAAW,CAAC,CAAC;IAErE,EAAE,CAAC,uBAAuB,CAAC,CAAC,CAAC,CAAC;IAC9B,EAAE,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AACxD,CAAC;AAED;iDACiD;AACjD,MAAM,cAAc,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AACtE,MAAM,YAAY,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC","sourcesContent":["import type { GLEngineContext } from \"./context.js\";\nimport { createEffect, disposeEffect, type GLEffect, isEffectReady, useEffect } from \"./effect.js\";\nimport { applyGLStates } from \"./apply-states.js\";\n\n/** Built-in fullscreen-quad vertex shader, used when `GLEffectWrapperOptions`\n * omits `vertexSource`. Maps the package's fullscreen-quad positions\n * (attribute location 0) to clip space and forwards a 0..1 `vUv` varying —\n * the WebGL counterpart of lite's default `vertexWGSL`, so callers can pass\n * only `fragmentSource`. */\nconst DEFAULT_FULLSCREEN_VERTEX_SOURCE = `#version 300 es\nlayout(location = 0) in vec2 position;\nout vec2 vUv;\nvoid main() {\n vUv = position * 0.5 + 0.5;\n gl_Position = vec4(position, 0.0, 1.0);\n}`;\n\n/** Inputs to `createEffectWrapper`. Mirrors lite's `EffectWrapperOptions`: the\n * wrapper compiles and OWNS the effect built from this shader source. */\nexport interface GLEffectWrapperOptions {\n /** Human-readable label for the wrapper and its effect. Defaults to\n * `\"effect-wrapper\"`. */\n name?: string;\n /** GLSL ES 3.00 vertex source. Defaults to a built-in fullscreen-quad\n * vertex shader (exposing a `vUv` varying), mirroring lite's default\n * `vertexWGSL`. */\n vertexSource?: string;\n /** GLSL ES 3.00 fragment source (≙ lite's `fragmentWGSL`). Required. */\n fragmentSource: string;\n /** Declared uniform names. Defaults to none. */\n uniformNames?: readonly string[];\n /** Declared sampler names, in unit-assignment order. Defaults to none. */\n samplerNames?: readonly string[];\n /** Attribute names; the first is bound to location 0. Defaults to\n * `[\"position\"]`. */\n attributeNames?: readonly string[];\n /** Optional `#define` block prepended to both shader stages. */\n defines?: string;\n}\n\n/** A reusable fullscreen effect that compiles and OWNS its `GLEffect` — the\n * WebGL counterpart of lite's `EffectWrapper`. Like lite, the wrapper functions\n * take the wrapper alone (the engine is derived from its effect):\n * `applyEffectWrapper(wrapper)` / `disposeEffectWrapper(wrapper)`. */\nexport interface GLEffectWrapper {\n /** Name alias for the wrapper (and its effect). */\n readonly name: string;\n /** The compiled effect this wrapper owns. Exposed so the per-uniform\n * setters (`setEffectFloat`/`setEffectTexture`/…) can target it — the\n * WebGL divergence from lite's UBO-based `setEffectUniforms(wrapper, …)`. */\n readonly effect: GLEffect;\n /** @internal */\n _disposed: boolean;\n}\n\n/** Compile a fullscreen effect from shader source and wrap it; the wrapper OWNS\n * the resulting `GLEffect`. Mirrors lite's `createEffectWrapper(engine, options)`.\n * When `vertexSource` is omitted, a built-in fullscreen-quad vertex shader is\n * used, so callers can supply only `fragmentSource`. */\nexport function createEffectWrapper(engine: GLEngineContext, options: GLEffectWrapperOptions): GLEffectWrapper {\n const name = options.name ?? \"effect-wrapper\";\n const effect = createEffect(engine, {\n name,\n vertexSource: options.vertexSource ?? DEFAULT_FULLSCREEN_VERTEX_SOURCE,\n fragmentSource: options.fragmentSource,\n uniformNames: options.uniformNames ?? [],\n samplerNames: options.samplerNames ?? [],\n attributeNames: options.attributeNames,\n defines: options.defines,\n });\n return { name, effect, _disposed: false };\n}\n\n/** Dispose the wrapper and the effect it owns (idempotent).\n *\n * @param wrapper - The wrapper to dispose. */\nexport function disposeEffectWrapper(wrapper: GLEffectWrapper): void {\n if (wrapper._disposed) {\n return;\n }\n wrapper._disposed = true;\n disposeEffect(wrapper.effect);\n}\n\n/** Pixel-space viewport rectangle passed to `setViewport`. */\nexport interface GLViewport {\n /** Lower-left X origin in physical pixels. */\n x: number;\n /** Lower-left Y origin in physical pixels. */\n y: number;\n /** Width in physical pixels. */\n w: number;\n /** Height in physical pixels. */\n h: number;\n}\n\n/** Cached `gl.viewport`. Defaults to the full canvas in pixel coordinates. */\nexport function setViewport(engine: GLEngineContext, viewport?: GLViewport): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const x = viewport?.x ?? 0;\n const y = viewport?.y ?? 0;\n const w = viewport?.w ?? engine.canvas.width;\n const h = viewport?.h ?? engine.canvas.height;\n const s = engine._state;\n if (s.viewportX === x && s.viewportY === y && s.viewportW === w && s.viewportH === h) {\n return;\n }\n s.viewportX = x;\n s.viewportY = y;\n s.viewportW = w;\n s.viewportH = h;\n engine.gl.viewport(x, y, w, h);\n}\n\n/** Make `wrapper.effect` current — the counterpart of Babylon's\n * `EffectRenderer.applyEffectWrapper` (lite-gl has no `setEffectWrapper`;\n * this is the only wrapper \"activate\" call). The shared fullscreen quad is\n * bound by {@link drawEffect}, so an existing `GLEffect` needs no wrapper:\n * `if (isEffectReady(e)) { useEffect(e); …setters…; drawEffect(engine); }`.\n *\n * The wrapper's effect must be ready: this polls {@link isEffectReady} itself\n * (finalizing the effect on its first ready poll) and returns `false` without\n * binding anything while the shaders are still compiling, failed to compile,\n * or the context is lost/disposed — skip the frame's `setEffect*` /\n * `drawEffect` calls in that case, otherwise they target whatever program\n * was current before.\n *\n * This MUST be called BEFORE any `setEffect*` call for the same effect in the\n * current frame (uniform setters write to the currently bound program).\n * Typical frame:\n * `if (!applyEffectWrapper(w)) return;` →\n * `setEffectFloat(w.effect, …)` → `drawEffect(engine)`.\n *\n * @param wrapper - The wrapper whose effect to make current.\n * @returns `true` when the effect is ready and now current; `false` when\n * nothing was bound. */\nexport function applyEffectWrapper(wrapper: GLEffectWrapper): boolean {\n // Bail before polling so a disposed wrapper sharing a live effect can't\n // finalize it (useProgram + callbacks); re-check after the poll because\n // ready callbacks it fires may dispose the wrapper.\n if (wrapper._disposed || !isEffectReady(wrapper.effect) || wrapper._disposed) {\n return false;\n }\n useEffect(wrapper.effect);\n return true;\n}\n\n/** Draw the shared fullscreen quad with the current program (from\n * {@link applyEffectWrapper} or `useEffect`) — the counterpart of Babylon's\n * `EffectRenderer.draw`. Lazily creates the quad on first use (and after\n * context restore), binds its VAO (cached), flushes the deferred GL states and\n * issues `gl.drawElements(TRIANGLES, 6, UNSIGNED_SHORT, 0)`. The program's\n * vertex shader must read the quad positions at attribute location 0 (as\n * `createEffectWrapper`'s default vertex shader does). No-op when the context\n * is lost/disposed or there is no current program.\n *\n * @param engine - The engine. */\nexport function drawEffect(engine: GLEngineContext): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n if (engine._state.currentProgram === null) {\n return;\n }\n ensureQuad(engine);\n applyGLStates(engine);\n engine.gl.drawElements(engine.gl.TRIANGLES, 6, engine.gl.UNSIGNED_SHORT, 0);\n}\n\n/** Lazy fullscreen quad. Built on first call; thereafter the VAO is cached on\n * `_state.quadVao` and rebinding is a single cached call. Cleared by\n * `webglcontextlost` and transparently rebuilt by the next\n * `drawEffect` after restore.\n *\n * Position attribute is enabled at location 0 — every effect's\n * `createEffect` calls `gl.bindAttribLocation(program, 0, attributeNames[0])`\n * BEFORE link, so the shared VAO is correct across all programs. */\nfunction ensureQuad(engine: GLEngineContext): void {\n const s = engine._state;\n const gl = engine.gl;\n if (s.quadVao !== null) {\n if (s.boundVao !== s.quadVao) {\n gl.bindVertexArray(s.quadVao);\n s.boundVao = s.quadVao;\n }\n return;\n }\n const vao = gl.createVertexArray();\n if (vao === null) {\n throw new Error(\"lite-gl: gl.createVertexArray returned null\");\n }\n s.quadVao = vao;\n gl.bindVertexArray(vao);\n s.boundVao = vao;\n\n const vbo = gl.createBuffer();\n if (vbo === null) {\n throw new Error(\"lite-gl: gl.createBuffer returned null (VBO)\");\n }\n s.quadVbo = vbo;\n gl.bindBuffer(gl.ARRAY_BUFFER, vbo);\n s.boundArrayBuffer = vbo;\n gl.bufferData(gl.ARRAY_BUFFER, QUAD_POSITIONS, gl.STATIC_DRAW);\n\n const ibo = gl.createBuffer();\n if (ibo === null) {\n throw new Error(\"lite-gl: gl.createBuffer returned null (IBO)\");\n }\n s.quadIbo = ibo;\n gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo);\n s.boundElementBuffer = ibo;\n gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, QUAD_INDICES, gl.STATIC_DRAW);\n\n gl.enableVertexAttribArray(0);\n gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n}\n\n/** Typed-array literal — pure per bundler convention. Matches Babylon's\n * `EffectRenderer` default geometry exactly. */\nconst QUAD_POSITIONS = new Float32Array([1, 1, -1, 1, -1, -1, 1, -1]);\nconst QUAD_INDICES = new Uint16Array([0, 1, 2, 0, 2, 3]);\n"]}
|