@babylonjs/lite-gl 1.23.0 → 1.24.0
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/CHANGELOG.md +12 -0
- package/README.md +98 -2
- package/context.d.ts +5 -0
- package/context.js +8 -0
- package/context.js.map +1 -1
- package/depth-stencil.d.ts +1 -14
- package/depth-stencil.js.map +1 -1
- package/effect-renderer.d.ts +1 -11
- package/effect-renderer.js.map +1 -1
- package/effect.d.ts +5 -27
- package/effect.js +22 -0
- package/effect.js.map +1 -1
- package/family.d.ts +470 -0
- package/family.js +14 -0
- package/family.js.map +1 -0
- package/index.d.ts +8 -6
- package/index.js +3 -3
- package/index.js.map +1 -1
- package/mesh.d.ts +4 -1
- package/mesh.js +10 -3
- package/mesh.js.map +1 -1
- package/native/command-stream.d.ts +1 -0
- package/native/command-stream.js +135 -0
- package/native/command-stream.js.map +1 -0
- package/native/context.d.ts +26 -0
- package/native/context.js +89 -0
- package/native/context.js.map +1 -0
- package/native/effect.d.ts +44 -0
- package/native/effect.js +276 -0
- package/native/effect.js.map +1 -0
- package/native/family.d.ts +75 -0
- package/native/family.js +2 -0
- package/native/family.js.map +1 -0
- package/native/identity.d.ts +11 -0
- package/native/identity.js +2 -0
- package/native/identity.js.map +1 -0
- package/native/index.d.ts +25 -0
- package/native/index.js +7 -0
- package/native/index.js.map +1 -0
- package/native/mesh.d.ts +53 -0
- package/native/mesh.js +137 -0
- package/native/mesh.js.map +1 -0
- package/native/render-target.d.ts +35 -0
- package/native/render-target.js +159 -0
- package/native/render-target.js.map +1 -0
- package/native/runtime.d.ts +1 -0
- package/native/runtime.js +12 -0
- package/native/runtime.js.map +1 -0
- package/native/state.d.ts +19 -0
- package/native/state.js +104 -0
- package/native/state.js.map +1 -0
- package/native/texture.d.ts +37 -0
- package/native/texture.js +86 -0
- package/native/texture.js.map +1 -0
- package/package.json +11 -3
- package/render-target.d.ts +7 -0
- package/render-target.js +38 -0
- package/render-target.js.map +1 -1
- package/scissor.d.ts +2 -2
- package/scissor.js +2 -2
- package/scissor.js.map +1 -1
- package/state.d.ts +1 -0
- package/state.js +2 -5
- package/state.js.map +1 -1
- package/text/text-renderer.js.map +1 -1
- package/web-family.d.ts +87 -0
- package/web-family.js +2 -0
- package/web-family.js.map +1 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
export function configureTexture(engine, texture, linear = true) {
|
|
3
|
+
const native = engine._runtime.Engine;
|
|
4
|
+
const commands = engine._commands;
|
|
5
|
+
commands.start(native.COMMAND_SETTEXTURESAMPLING);
|
|
6
|
+
commands.handle(texture._handle);
|
|
7
|
+
commands.uint(linear ? native.TEXTURE_LINEAR_LINEAR : native.TEXTURE_NEAREST_NEAREST);
|
|
8
|
+
commands.finish();
|
|
9
|
+
commands.start(native.COMMAND_SETTEXTUREWRAPMODE);
|
|
10
|
+
commands.handle(texture._handle);
|
|
11
|
+
commands.uint(native.ADDRESS_MODE_CLAMP);
|
|
12
|
+
commands.uint(native.ADDRESS_MODE_CLAMP);
|
|
13
|
+
commands.uint(native.ADDRESS_MODE_CLAMP);
|
|
14
|
+
commands.finish();
|
|
15
|
+
commands.start(native.COMMAND_SETTEXTUREANISOTROPICLEVEL);
|
|
16
|
+
commands.handle(texture._handle);
|
|
17
|
+
commands.uint(1);
|
|
18
|
+
commands.finish();
|
|
19
|
+
}
|
|
20
|
+
/** @internal */
|
|
21
|
+
export function makeTexture(engine, handle, owned, sampledOnly) {
|
|
22
|
+
const fields = {
|
|
23
|
+
width: engine._native.getTextureWidth(handle),
|
|
24
|
+
height: engine._native.getTextureHeight(handle),
|
|
25
|
+
isReady: true,
|
|
26
|
+
_handle: handle,
|
|
27
|
+
_owned: owned,
|
|
28
|
+
_sampledOnly: sampledOnly,
|
|
29
|
+
_targets: [],
|
|
30
|
+
_engine: engine,
|
|
31
|
+
_disposed: false,
|
|
32
|
+
};
|
|
33
|
+
const texture = fields;
|
|
34
|
+
configureTexture(engine, texture);
|
|
35
|
+
return texture;
|
|
36
|
+
}
|
|
37
|
+
/** Borrows native texture storage without taking responsibility for deleting it. */
|
|
38
|
+
export function wrapNativeTexture(engine, handle) {
|
|
39
|
+
return makeTexture(engine, handle, false, false);
|
|
40
|
+
}
|
|
41
|
+
const GL_RGBA = 0x1908;
|
|
42
|
+
const GL_UNSIGNED_BYTE = 0x1401;
|
|
43
|
+
/** @internal Rejects a texture size the host cannot allocate, before any host call. */
|
|
44
|
+
export function validateTextureSize(engine, width, height) {
|
|
45
|
+
const max = engine.caps.maxTextureSize;
|
|
46
|
+
// 0 means the host has not reported a limit, so only the host can bound the size.
|
|
47
|
+
const limited = max > 0;
|
|
48
|
+
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || (limited && (width > max || height > max))) {
|
|
49
|
+
throw new Error(`Native Lite GL texture size must be positive integers${limited ? ` no larger than ${max}` : ""}, got ${width}x${height}.`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Creates an owned, sampled-only texture from raw pixels. Only `gl.RGBA` / `gl.UNSIGNED_BYTE` data is supported.
|
|
54
|
+
* The host submits the commands recorded so far before uploading, so create raw textures before `beginFrame` when the number of submissions matters.
|
|
55
|
+
*/
|
|
56
|
+
export function createRawTexture(engine, data, width, height, options) {
|
|
57
|
+
if ((options?.format ?? GL_RGBA) !== GL_RGBA || (options?.type ?? GL_UNSIGNED_BYTE) !== GL_UNSIGNED_BYTE) {
|
|
58
|
+
throw new Error("Native Lite GL raw textures support only RGBA / UNSIGNED_BYTE data.");
|
|
59
|
+
}
|
|
60
|
+
validateTextureSize(engine, width, height);
|
|
61
|
+
const handle = engine._native.createTexture();
|
|
62
|
+
try {
|
|
63
|
+
engine._native.loadRawTexture(handle, data, width, height, engine._runtime.Engine.TEXTURE_FORMAT_RGBA8, false, options?.invertY ?? false);
|
|
64
|
+
return makeTexture(engine, handle, true, true);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
engine._native.deleteTexture(handle);
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Releases owned image storage, never a borrowed host texture. The storage
|
|
73
|
+
* outlives every command already recorded, so a frame may still draw with,
|
|
74
|
+
* or into, a texture it disposes. After engine disposal it only marks the
|
|
75
|
+
* texture disposed.
|
|
76
|
+
*/
|
|
77
|
+
export function disposeTexture(texture) {
|
|
78
|
+
if (!texture._disposed) {
|
|
79
|
+
texture._disposed = true;
|
|
80
|
+
texture.isReady = false;
|
|
81
|
+
if (texture._owned && !texture._engine._disposed) {
|
|
82
|
+
texture._engine._commands.deleteTexture(texture._handle);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=texture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"texture.js","sourceRoot":"","sources":["../../src/native/texture.ts"],"names":[],"mappings":"AA4BA,gBAAgB;AAChB,MAAM,UAAU,gBAAgB,CAAC,MAAuB,EAAE,OAAkB,EAAE,MAAM,GAAG,IAAI;IACvF,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;IACtC,MAAM,QAAQ,GAAG,MAAM,CAAC,SAAS,CAAC;IAClC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,0BAA0B,CAAC,CAAC;IAClD,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACjC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC,CAAC,MAAM,CAAC,uBAAuB,CAAC,CAAC;IACtF,QAAQ,CAAC,MAAM,EAAE,CAAC;IAClB,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,0BAA0B,CAAC,CAAC;IAClD,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACj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AE,OAAO,EAAE,OAAO,IAAI,KAAK,CAAC,CAAC;QAC1I,OAAO,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IACnD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QACrC,MAAM,KAAK,CAAC;IAChB,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,OAAkB;IAC7C,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;QACrB,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;QACzB,OAAO,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,IAAI,OAAO,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;YAC/C,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAC7D,CAAC;IACL,CAAC;AACL,CAAC","sourcesContent":["import type { GLEngineContext } from \"./context.js\";\nimport type { NativeResource, NativeResourceFields } from \"./identity.js\";\nimport type { NativeHandle } from \"./runtime.js\";\nimport type { GLRenderTarget } from \"./render-target.js\";\n\n/** Opaque texture storage supplied by the Babylon Native host. */\nexport type NativeTextureHandle = Uint32Array<ArrayBuffer>;\n\n/** Sampled native image with explicit storage ownership. */\nexport interface GLTexture extends NativeResource<\"texture\"> {\n readonly width: number;\n readonly height: number;\n /** Whether the image is available for sampling. */\n isReady: boolean;\n /** @internal */\n _handle: NativeHandle;\n /** @internal */\n readonly _owned: boolean;\n /** @internal Whether the storage is known to be unusable as a render-target attachment. */\n readonly _sampledOnly: boolean;\n /** @internal */\n readonly _targets: GLRenderTarget[];\n /** @internal The engine that owns this texture wrapper. */\n readonly _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n}\n\n/** @internal */\nexport function configureTexture(engine: GLEngineContext, texture: GLTexture, linear = true): void {\n const native = engine._runtime.Engine;\n const commands = engine._commands;\n commands.start(native.COMMAND_SETTEXTURESAMPLING);\n commands.handle(texture._handle);\n commands.uint(linear ? native.TEXTURE_LINEAR_LINEAR : native.TEXTURE_NEAREST_NEAREST);\n commands.finish();\n commands.start(native.COMMAND_SETTEXTUREWRAPMODE);\n commands.handle(texture._handle);\n commands.uint(native.ADDRESS_MODE_CLAMP);\n commands.uint(native.ADDRESS_MODE_CLAMP);\n commands.uint(native.ADDRESS_MODE_CLAMP);\n commands.finish();\n commands.start(native.COMMAND_SETTEXTUREANISOTROPICLEVEL);\n commands.handle(texture._handle);\n commands.uint(1);\n commands.finish();\n}\n\n/** @internal */\nexport function makeTexture(engine: GLEngineContext, handle: NativeHandle, owned: boolean, sampledOnly: boolean): GLTexture {\n const fields: NativeResourceFields<GLTexture> = {\n width: engine._native.getTextureWidth(handle),\n height: engine._native.getTextureHeight(handle),\n isReady: true,\n _handle: handle,\n _owned: owned,\n _sampledOnly: sampledOnly,\n _targets: [],\n _engine: engine,\n _disposed: false,\n };\n const texture = fields as GLTexture;\n configureTexture(engine, texture);\n return texture;\n}\n\n/** Borrows native texture storage without taking responsibility for deleting it. */\nexport function wrapNativeTexture(engine: GLEngineContext, handle: NativeTextureHandle): GLTexture {\n return makeTexture(engine, handle, false, false);\n}\n\n/** Upload options shared by native texture creators. */\nexport interface GLTextureOptions {\n /** Flip rows during upload. Default `false`. */\n invertY?: boolean;\n}\n\n/** Options for {@link createRawTexture}. */\nexport interface GLRawTextureOptions extends GLTextureOptions {\n /** Pixel format of `data`. Only `gl.RGBA` (the default) is supported. */\n format?: number;\n /** Pixel type of `data`. Only `gl.UNSIGNED_BYTE` (the default) is supported. */\n type?: number;\n}\n\nconst GL_RGBA = 0x1908;\nconst GL_UNSIGNED_BYTE = 0x1401;\n\n/** @internal Rejects a texture size the host cannot allocate, before any host call. */\nexport function validateTextureSize(engine: GLEngineContext, width: number, height: number): void {\n const max = engine.caps.maxTextureSize;\n // 0 means the host has not reported a limit, so only the host can bound the size.\n const limited = max > 0;\n if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || (limited && (width > max || height > max))) {\n throw new Error(`Native Lite GL texture size must be positive integers${limited ? ` no larger than ${max}` : \"\"}, got ${width}x${height}.`);\n }\n}\n\n/**\n * Creates an owned, sampled-only texture from raw pixels. Only `gl.RGBA` / `gl.UNSIGNED_BYTE` data is supported.\n * The host submits the commands recorded so far before uploading, so create raw textures before `beginFrame` when the number of submissions matters.\n */\nexport function createRawTexture(engine: GLEngineContext, data: Uint8Array, width: number, height: number, options?: GLRawTextureOptions): GLTexture {\n if ((options?.format ?? GL_RGBA) !== GL_RGBA || (options?.type ?? GL_UNSIGNED_BYTE) !== GL_UNSIGNED_BYTE) {\n throw new Error(\"Native Lite GL raw textures support only RGBA / UNSIGNED_BYTE data.\");\n }\n validateTextureSize(engine, width, height);\n const handle = engine._native.createTexture();\n try {\n engine._native.loadRawTexture(handle, data, width, height, engine._runtime.Engine.TEXTURE_FORMAT_RGBA8, false, options?.invertY ?? false);\n return makeTexture(engine, handle, true, true);\n } catch (error) {\n engine._native.deleteTexture(handle);\n throw error;\n }\n}\n\n/**\n * Releases owned image storage, never a borrowed host texture. The storage\n * outlives every command already recorded, so a frame may still draw with,\n * or into, a texture it disposes. After engine disposal it only marks the\n * texture disposed.\n */\nexport function disposeTexture(texture: GLTexture): void {\n if (!texture._disposed) {\n texture._disposed = true;\n texture.isReady = false;\n if (texture._owned && !texture._engine._disposed) {\n texture._engine._commands.deleteTexture(texture._handle);\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@babylonjs/lite-gl",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.24.0",
|
|
4
4
|
"description": "Function-based, tree-shakeable WebGL2 micro-engine for fullscreen effects, sprites and dynamic textures — the WebGL counterpart of @babylonjs/lite.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"babylon",
|
|
@@ -32,11 +32,19 @@
|
|
|
32
32
|
".": {
|
|
33
33
|
"import": "./index.js",
|
|
34
34
|
"types": "./index.d.ts"
|
|
35
|
+
},
|
|
36
|
+
"./native": {
|
|
37
|
+
"import": "./native/index.js",
|
|
38
|
+
"types": "./native/index.d.ts"
|
|
39
|
+
},
|
|
40
|
+
"./family": {
|
|
41
|
+
"import": "./family.js",
|
|
42
|
+
"types": "./family.d.ts"
|
|
35
43
|
}
|
|
36
44
|
},
|
|
37
45
|
"babylonLiteRelease": {
|
|
38
|
-
"azureBuildId": "
|
|
39
|
-
"sourceVersion": "
|
|
46
|
+
"azureBuildId": "164935908",
|
|
47
|
+
"sourceVersion": "99466d5d19ce3018ffeb1fb6fa844b0f36f5de12"
|
|
40
48
|
},
|
|
41
49
|
"publishConfig": {
|
|
42
50
|
"tag": "latest"
|
package/render-target.d.ts
CHANGED
|
@@ -28,6 +28,13 @@
|
|
|
28
28
|
*/
|
|
29
29
|
import { type GLTexture } from "./texture.js";
|
|
30
30
|
import type { GLEngineContext } from "./context.js";
|
|
31
|
+
/**
|
|
32
|
+
* Presents a retained composition target to the whole canvas without disturbing
|
|
33
|
+
* its contents; an active scissor rectangle does not clip the presentation.
|
|
34
|
+
* Presentation copies framebuffers, which WebGL2 forbids into a multisampled
|
|
35
|
+
* canvas, so it throws for an engine created with `antialias: true`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function presentRenderTarget(target: GLRenderTarget): void;
|
|
31
38
|
/** Options for {@link createRenderTarget}. Every field has a Babylon-matching
|
|
32
39
|
* default: the bare `createRenderTarget(engine, width, height)` makes an RGBA8
|
|
33
40
|
* color-only target with linear filtering and clamp wrapping. */
|
package/render-target.js
CHANGED
|
@@ -27,6 +27,44 @@
|
|
|
27
27
|
* there first; the RT then re-attaches its freshly-swapped handle.
|
|
28
28
|
*/
|
|
29
29
|
import { bindTextureForUpload, pickSizedInternalFormat, resolveLdrInternalFormat, setBoundTextureParams } from "./texture.js";
|
|
30
|
+
/**
|
|
31
|
+
* Presents a retained composition target to the whole canvas without disturbing
|
|
32
|
+
* its contents; an active scissor rectangle does not clip the presentation.
|
|
33
|
+
* Presentation copies framebuffers, which WebGL2 forbids into a multisampled
|
|
34
|
+
* canvas, so it throws for an engine created with `antialias: true`.
|
|
35
|
+
*/
|
|
36
|
+
export function presentRenderTarget(target) {
|
|
37
|
+
const engine = target._engine;
|
|
38
|
+
if (engine._isLost || engine._disposed || target._disposed || !target.isReady) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const gl = engine.gl;
|
|
42
|
+
const s = engine._state;
|
|
43
|
+
// A wiped cache records the canvas binding whatever is actually bound, so that binding is reissued.
|
|
44
|
+
if (s.boundFramebuffer === null) {
|
|
45
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
46
|
+
}
|
|
47
|
+
restoreDefaultFramebuffer(engine);
|
|
48
|
+
// Queried with the canvas framebuffer bound; its sample count is fixed when the context is created.
|
|
49
|
+
engine._canvasSamples ??= Number(gl.getParameter(gl.SAMPLES));
|
|
50
|
+
if (engine._canvasSamples > 0) {
|
|
51
|
+
throw new Error("lite-gl: presentRenderTarget cannot copy into a multisampled canvas; create the engine without antialias");
|
|
52
|
+
}
|
|
53
|
+
let scissorEnabled = s.scissorEnabled;
|
|
54
|
+
if (scissorEnabled === -1) {
|
|
55
|
+
scissorEnabled = gl.isEnabled(gl.SCISSOR_TEST) ? 1 : 0;
|
|
56
|
+
}
|
|
57
|
+
if (scissorEnabled === 1) {
|
|
58
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
59
|
+
}
|
|
60
|
+
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, target._framebuffer);
|
|
61
|
+
gl.blitFramebuffer(0, 0, target.width, target.height, 0, 0, engine.canvas.width, engine.canvas.height, gl.COLOR_BUFFER_BIT, gl.NEAREST);
|
|
62
|
+
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
|
|
63
|
+
if (scissorEnabled === 1) {
|
|
64
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
65
|
+
}
|
|
66
|
+
s.scissorEnabled = scissorEnabled;
|
|
67
|
+
}
|
|
30
68
|
/** GL `gl.UNSIGNED_BYTE` — the default (RGBA8) color attachment type. */
|
|
31
69
|
const UNSIGNED_BYTE = 0x1401;
|
|
32
70
|
/** GL `gl.HALF_FLOAT`. */
|
package/render-target.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render-target.js","sourceRoot":"","sources":["../src/render-target.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,OAAO,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,qBAAqB,EAAkB,MAAM,cAAc,CAAC;AAG9I,yEAAyE;AACzE,MAAM,aAAa,GAAG,MAAM,CAAC;AAC7B,0BAA0B;AAC1B,MAAM,UAAU,GAAG,MAAM,CAAC;AAC1B,qBAAqB;AACrB,MAAM,KAAK,GAAG,MAAM,CAAC;AACrB,oBAAoB;AACpB,MAAM,IAAI,GAAG,MAAM,CAAC;AACpB,mBAAmB;AACnB,MAAM,GAAG,GAAG,MAAM,CAAC;AACnB,kBAAkB;AAClB,MAAM,EAAE,GAAG,MAAM,CAAC;AAClB,sBAAsB;AACtB,MAAM,MAAM,GAAG,MAAM,CAAC;AACtB,6BAA6B;AAC7B,MAAM,aAAa,GAAG,MAAM,CAAC;AA8I7B;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAuB,EAAE,KAAa,EAAE,MAAc,EAAE,OAA+B;IACtH,MAAM,IAAI,GAAG,OAAO,IAAI,EAAE,CAAC;IAC3B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC;IACnC,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,EAAE,IAAI,MAAM,KAAK,GAAG,EAA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reen framebuffer) support.\n *\n * Part of the public API via the `@babylonjs/lite-gl` barrel. The package is\n * `sideEffects: false`, so consumers that only render fullscreen effects to the\n * canvas tree-shake the FBO code out.\n *\n * This is the lite-gl equivalent of Babylon's `RenderTargetWrapper` +\n * `ThinEngine.createRenderTargetTexture` / `bindFramebuffer` /\n * `restoreDefaultFramebuffer` / `_readTexturePixelsSync`. A render target owns a\n * color {@link GLTexture} (or attaches a caller-supplied one) plus an optional\n * depth and/or stencil renderbuffer, all wrapped in a single\n * `WebGLFramebuffer`.\n *\n * The default {@link createRenderTarget} makes an 8-bit **RGBA8** (or, via\n * `format`, `R8` / `RG8`) color target and ships none of the HDR sized-format\n * knowledge; {@link createFloatRenderTarget}\n * is the separate opt-in for float / half-float color attachments (it alone\n * carries the `RGBA16F` / `RGBA32F` table, so RGBA8 consumers tree-shake it\n * away).\n *\n * The full color/depth/stencil GPU set is rebuilt automatically on\n * `webglcontextrestored` — the render target registers itself with the engine's\n * `_renderTargets` registry, and the context-restore protocol calls its\n * `_restore` closure AFTER the standalone texture replay. A caller-supplied\n * (BYO) color texture lives in the engine's `_textures` registry and is restored\n * there first; the RT then re-attaches its freshly-swapped handle.\n */\nimport { bindTextureForUpload, pickSizedInternalFormat, resolveLdrInternalFormat, setBoundTextureParams, type GLTexture } from \"./texture.js\";\nimport type { GLEngineContext } from \"./context.js\";\n\n/** GL `gl.UNSIGNED_BYTE` — the default (RGBA8) color attachment type. */\nconst UNSIGNED_BYTE = 0x1401;\n/** GL `gl.HALF_FLOAT`. */\nconst HALF_FLOAT = 0x140b;\n/** GL `gl.FLOAT`. */\nconst FLOAT = 0x1406;\n/** GL `gl.RGBA`. */\nconst RGBA = 0x1908;\n/** GL `gl.RED`. */\nconst RED = 0x1903;\n/** GL `gl.RG`. */\nconst RG = 0x8227;\n/** GL `gl.LINEAR`. */\nconst LINEAR = 0x2601;\n/** GL `gl.CLAMP_TO_EDGE`. */\nconst CLAMP_TO_EDGE = 0x812f;\n\n/** Options for {@link createRenderTarget}. Every field has a Babylon-matching\n * default: the bare `createRenderTarget(engine, width, height)` makes an RGBA8\n * color-only target with linear filtering and clamp wrapping. */\nexport interface GLRenderTargetOptions {\n /** Allocate a depth renderbuffer (`DEPTH_COMPONENT16`). Default `false`.\n * Stencil is NOT a create option — opt in (packed depth+stencil, or\n * stencil-only) via `generateRenderTargetStencil`\n * (`@babylonjs/lite-gl`), which keeps the stencil/packed\n * renderbuffer code out of the render-target core bundle. */\n generateDepthBuffer?: boolean;\n /** 8-bit color attachment format: `gl.RGBA` (default, `RGBA8`), `gl.RG`\n * (`RG8`) or `gl.RED` (`R8`) — ≙ Babylon's `RenderTargetCreationOptions.format`.\n * Ignored when {@link colorTexture} is supplied. Read single/two-channel\n * targets tightly via `readRenderTargetPixels(rt, { format })`. */\n format?: GLenum;\n /** Color texture minification filter. Default `gl.LINEAR`. */\n minFilter?: GLenum;\n /** Color texture magnification filter. Default `gl.LINEAR`. */\n magFilter?: GLenum;\n /** Color texture S wrap. Default `gl.CLAMP_TO_EDGE`. */\n wrapS?: GLenum;\n /** Color texture T wrap. Default `gl.CLAMP_TO_EDGE`. */\n wrapT?: GLenum;\n /** Attach a caller-supplied (BYO) color {@link GLTexture} instead of creating\n * one. When supplied the render target does NOT own or restore it — the\n * texture is engine-managed (restored by the standard texture-restore path\n * first) and the RT re-attaches its swapped handle afterwards. The caller is\n * responsible for sizing it to `width`×`height` and for disposing it.\n * Readbacks use the creator's texel type, so a float / half-float texture\n * must be attached via `createFloatRenderTarget` with a matching `type`\n * (`createRenderTarget` reads as `UNSIGNED_BYTE`). */\n colorTexture?: GLTexture;\n}\n\n/** Options for {@link createFloatRenderTarget} — {@link GLRenderTargetOptions}\n * (always RGBA, so without `format`) plus the float color `type`. */\nexport interface GLFloatRenderTargetOptions extends Omit<GLRenderTargetOptions, \"format\"> {\n /** Float color attachment type. Default `gl.HALF_FLOAT`. Pass `gl.FLOAT` for\n * full 32-bit. Downgraded to the best renderable type the engine supports\n * (`caps.textureFloatRender` / `caps.textureHalfFloatRender`), mirroring\n * Babylon's `getTextureType`. */\n type?: GLenum;\n}\n\n/** Read rectangle and channel layout shared by {@link readRenderTargetPixels}\n * and {@link readRenderTargetPixelsToBuffer}. Every field is optional: the\n * defaults read the whole target as RGBA. */\nexport interface GLReadPixelsRegionOptions {\n /** Lower-left X of the read rectangle, in texels. Default `0`. */\n x?: number;\n /** Lower-left Y of the read rectangle, in texels. Default `0`. */\n y?: number;\n /** Read rectangle width in texels. Default: the target's width. */\n width?: number;\n /** Read rectangle height in texels. Default: the target's height. */\n height?: number;\n /** Read format: `gl.RGBA` (default, 4 channels), `gl.RG` (2) or `gl.RED` (1). */\n format?: GLenum;\n}\n\n/** Options for {@link readRenderTargetPixels}. Every field is optional: the bare\n * `readRenderTargetPixels(rt)` reads the whole target as RGBA into a fresh\n * buffer. */\nexport interface GLReadPixelsOptions extends GLReadPixelsRegionOptions {\n /** Preallocated buffer (`width*height*channels` elements of the matching\n * type), reused to avoid per-call allocation. Default: a fresh buffer. */\n into?: ArrayBufferView;\n}\n\n/** Options for {@link readRenderTargetPixelsToBuffer}. */\nexport interface GLReadPixelsToBufferOptions extends GLReadPixelsRegionOptions {\n /** Byte offset into the destination `WebGLBuffer` where the first texel is\n * written. Must be a multiple of the read type's size. Default `0`. */\n offset?: number;\n}\n\n/** Resolved (defaults-applied) attachment description, retained on the render\n * target so `webglcontextrestored` can rebuild the exact same GPU set. */\ninterface ResolvedRTConfig {\n internalFormat: GLenum;\n format: GLenum;\n type: GLenum;\n hasDepth: boolean;\n minFilter: GLenum;\n magFilter: GLenum;\n wrapS: GLenum;\n wrapT: GLenum;\n /** True when the RT created (and therefore owns/restores/deletes) its color\n * texture; false for a BYO {@link GLRenderTargetOptions.colorTexture}. */\n ownsColorTexture: boolean;\n}\n\n/**\n * An offscreen render target — a `WebGLFramebuffer` wrapping a color\n * {@link GLTexture} and an optional depth / stencil renderbuffer. The lite-gl\n * counterpart of Babylon's `RenderTargetWrapper`.\n */\nexport interface GLRenderTarget {\n /** The color attachment, sampleable like any other {@link GLTexture}\n * (`setEffectTexture` / `bindTexture`). For an owned attachment its handle\n * is swapped on `webglcontextrestored` while consumers keep this same\n * reference. */\n texture: GLTexture;\n /** Color attachment width in texels. */\n width: number;\n /** Color attachment height in texels. */\n height: number;\n /** True once the color attachment + framebuffer are allocated. */\n isReady: boolean;\n /** @internal The framebuffer object. Swapped on context-restore. */\n _framebuffer: WebGLFramebuffer | null;\n /** @internal Depth-only (`DEPTH_COMPONENT16`) renderbuffer built by the core,\n * OR the packed/stencil renderbuffer installed by `generateRenderTargetStencil`\n * — a single field either way. Null when neither depth nor stencil is used. */\n _depthStencil: WebGLRenderbuffer | null;\n /**\n * @internal Optional stencil rebuild hook installed by\n * `generateRenderTargetStencil` (`@babylonjs/lite-gl`). When\n * present it OWNS the depth/stencil renderbuffer (replacing the core\n * depth-only buffer) and is re-invoked after every FBO rebuild\n * (create-via-helper, resize, context-restore) so the attachment survives.\n */\n _rebuildDepthStencil?: (engine: GLEngineContext) => void;\n /** @internal Resolved attachment config, for context-restore rebuild. */\n _config: ResolvedRTConfig;\n /** The engine that created (and owns) this render target. Operations on it derive\n * their engine from here, so they take the render target alone. @internal */\n _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n /** @internal Delete every GPU object the target owns (FBO, renderbuffer, and\n * the color texture iff owned). Resets the bound-framebuffer cache if it\n * pointed at this target. Called by `disposeRenderTarget`, `resizeRenderTarget`\n * and engine dispose. */\n _deleteGpu: (gl: WebGL2RenderingContext) => void;\n /** @internal Rebuild the FBO + renderbuffer (+ owned color texture) into\n * fresh handles after `webglcontextrestored`. */\n _restore: (engine: GLEngineContext) => void;\n}\n\n/**\n * Create an offscreen 8-bit render target — **RGBA8** by default, or `R8` /\n * `RG8` via {@link GLRenderTargetOptions.format}.\n *\n * The color attachment is an owned {@link GLTexture} (rebuilt by this target's\n * own restore hook), unless {@link GLRenderTargetOptions.colorTexture} supplies a\n * caller-managed (BYO) one. Mirrors Babylon's `createRenderTargetTexture`.\n *\n * @param engine - The engine to allocate GL resources on.\n * @param width - Color attachment width in texels. Must be a positive integer.\n * @param height - Color attachment height in texels. Must be a positive integer.\n * @param options - See {@link GLRenderTargetOptions}.\n * @returns The new {@link GLRenderTarget}.\n * @throws If `format` is not `RGBA` / `RG` / `RED`, `width`/`height` are not\n * positive integers, a GL handle could not be allocated, or the resulting\n * framebuffer is not complete. On failure every partial GPU object (including\n * an owned color texture) is released first.\n */\nexport function createRenderTarget(engine: GLEngineContext, width: number, height: number, options?: GLRenderTargetOptions): GLRenderTarget {\n const opts = options ?? {};\n const format = opts.format ?? RGBA;\n if (format !== RGBA && format !== RG && format !== RED) {\n throw new Error(`lite-gl: unsupported render target format 0x${format.toString(16)}`);\n }\n return buildRT(engine, width, height, opts, resolveLdrInternalFormat(engine.gl, format, UNSIGNED_BYTE), format, UNSIGNED_BYTE);\n}\n\n/**\n * Create an offscreen **float / half-float** render target — the HDR opt-in\n * counterpart of {@link createRenderTarget}. This is the only render-target\n * factory that references the `RGBA16F` / `RGBA32F` sized-format table, so RGBA8\n * consumers ship none of it.\n *\n * Defaults to `gl.HALF_FLOAT`; pass `options.type = gl.FLOAT` for full 32-bit.\n * The requested type is downgraded to the best renderable type the engine\n * supports (mirroring Babylon's `getTextureType`).\n *\n * @param engine - The engine to allocate GL resources on.\n * @param width - Color attachment width in texels. Must be a positive integer.\n * @param height - Color attachment height in texels. Must be a positive integer.\n * @param options - See {@link GLFloatRenderTargetOptions}.\n * @returns The new {@link GLRenderTarget}.\n * @throws As {@link createRenderTarget}.\n */\nexport function createFloatRenderTarget(engine: GLEngineContext, width: number, height: number, options?: GLFloatRenderTargetOptions): GLRenderTarget {\n const opts = options ?? {};\n const type = resolveColorType(engine, opts.type ?? HALF_FLOAT);\n const internalFormat = pickSizedInternalFormat(engine.gl, RGBA, type);\n return buildRT(engine, width, height, opts, internalFormat, RGBA, type);\n}\n\n/**\n * Bind the render target's framebuffer as the draw target and set the viewport\n * to cover it. Use {@link restoreDefaultFramebuffer} to return to the canvas.\n * Subsequent `drawEffect` / `drawIndexed` / `clearEngine` calls write into the\n * bound target.\n *\n * Cached. No-op on a lost/disposed context or a disposed `rt`. Mipmaps are NOT\n * regenerated here — refresh a target's mip chain explicitly via\n * {@link generateRenderTargetMipMaps} after rendering into it.\n *\n * @param rt - The render target to draw into.\n */\nexport function bindRenderTarget(rt: GLRenderTarget): void {\n const engine = rt._engine;\n if (engine._isLost || engine._disposed || rt._disposed) {\n return;\n }\n bindFramebufferAndViewport(engine, rt, rt._framebuffer, rt.width, rt.height);\n}\n\n/**\n * Bind the default (canvas) framebuffer and reset the viewport to the full\n * canvas — the counterpart of Babylon's `restoreDefaultFramebuffer`. Cached;\n * no-op on a lost/disposed context.\n *\n * @param engine - The engine.\n */\nexport function restoreDefaultFramebuffer(engine: GLEngineContext): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n bindFramebufferAndViewport(engine, null, null, engine.canvas.width, engine.canvas.height);\n}\n\nfunction bindFramebufferAndViewport(engine: GLEngineContext, rt: GLRenderTarget | null, fb: WebGLFramebuffer | null, width: number, height: number): void {\n const s = engine._state;\n if (s.boundFramebuffer !== fb) {\n engine.gl.bindFramebuffer(engine.gl.FRAMEBUFFER, fb);\n s.boundFramebuffer = fb;\n }\n engine._currentRenderTarget = rt;\n setViewportCached(engine, 0, 0, width, height);\n}\n\n/** Regenerate a render target's color-attachment mip chain from its (freshly\n * rendered) level-0 — mipmaps for render targets are a pure manual opt-in\n * (call this after rendering into the target). No-op for a disposed target, a\n * handle-less color attachment, or a lost/disposed context. */\nexport function generateRenderTargetMipMaps(rt: GLRenderTarget): void {\n const engine = rt._engine;\n if (engine._isLost || engine._disposed || rt._disposed || rt.texture.handle === null) {\n return;\n }\n bindTextureForUpload(engine, rt.texture.handle);\n engine.gl.generateMipmap(engine.gl.TEXTURE_2D);\n}\n\n/**\n * Resize the render target's color attachment (and depth/stencil renderbuffer).\n * Reallocates storage at the new size; the contents are discarded. The\n * `GLRenderTarget` / `GLTexture` identity is preserved, so consumers and\n * effect-sampler bindings holding the reference stay valid. If this target was\n * the live draw target, it is rebound (with the new-size viewport) afterwards.\n *\n * No-op when the size is unchanged or `rt` is disposed. While the context is\n * lost the new size is recorded but the GL reallocation is deferred to the\n * restore hook.\n *\n * @param rt - The render target to resize.\n * @param width - New width in texels (≥ 1).\n * @param height - New height in texels (≥ 1).\n */\nexport function resizeRenderTarget(rt: GLRenderTarget, width: number, height: number): void {\n const engine = rt._engine;\n if (rt._disposed) {\n return;\n }\n validateSize(width, height);\n if (rt.width === width && rt.height === height) {\n return;\n }\n rt.width = width;\n rt.height = height;\n rt.texture.width = width;\n rt.texture.height = height;\n if (engine._isLost || engine._disposed) {\n return;\n }\n // Capture the live binding BEFORE we delete the old FBO so we can restore it.\n const wasBound = engine._state.boundFramebuffer === rt._framebuffer && rt._framebuffer !== null;\n // For a BYO color texture, re-specify the caller's storage at the new size\n // (contents discarded, like the owned path). `_updateRaw` updates the\n // texture's tracked dims AND re-runs its upload closure — `_upload` alone\n // would re-upload at the stale ORIGINAL size, leaving the attachment a\n // different size than the FBO. An external handle without `_updateRaw` must\n // be resized by its owner; we then just rebuild the FBO around it. An owned\n // texture is recreated wholesale by allocateRenderTargetGpu.\n if (!rt._config.ownsColorTexture) {\n rt.texture._updateRaw?.(engine, null, width, height, 4);\n }\n rt._deleteGpu(engine.gl);\n allocateRenderTargetGpu(engine, rt);\n if (wasBound) {\n bindRenderTarget(rt);\n }\n}\n\n/**\n * Synchronously read back a rectangle of the render target's color attachment\n * via `gl.readPixels` — the lite-gl equivalent of Babylon's\n * `_readTexturePixelsSync`. Binds the target's framebuffer (leaving it bound,\n * matching Babylon).\n *\n * The returned array element type follows the color attachment type:\n * `Uint8Array` for `UNSIGNED_BYTE`, `Float32Array` for `FLOAT`, `Uint16Array`\n * for `HALF_FLOAT`. Origin is GL bottom-left.\n *\n * Reads `RGBA` by default (like Babylon; always readable, a `RED` target reads\n * back as `R,0,0,1`). Pass `format` `gl.RED` / `gl.RG` to read 1 / 2 channels\n * per texel into a tightly packed buffer (`PACK_ALIGNMENT` is set to 1 and left\n * there — lite-gl does not otherwise use it). WebGL only guarantees non-RGBA\n * reads when they match the implementation-chosen\n * `gl.IMPLEMENTATION_COLOR_READ_FORMAT` (`RED` for `R8`, `RG` for `RG8` in all\n * major browsers).\n *\n * @param rt - The render target to read from.\n * @param options - See {@link GLReadPixelsOptions}. Default: the whole target,\n * as RGBA, into a fresh buffer.\n * @returns The pixel buffer (the provided `into`, or a freshly allocated one).\n * Empty buffer on a lost/disposed context.\n */\nexport function readRenderTargetPixels(rt: GLRenderTarget, options?: GLReadPixelsOptions): ArrayBufferView {\n const opts = options ?? {};\n const width = opts.width ?? rt.width;\n const height = opts.height ?? rt.height;\n const format = opts.format ?? RGBA;\n const into = opts.into;\n const gl = bindForRead(rt, format);\n if (gl === null) {\n return into ?? new Uint8Array(0);\n }\n const type = rt._config.type;\n let buffer = into;\n if (buffer === undefined) {\n const elements = width * height * (format === RED ? 1 : format === RG ? 2 : 4);\n buffer = type === gl.FLOAT ? new Float32Array(elements) : type === gl.HALF_FLOAT ? new Uint16Array(elements) : new Uint8Array(elements);\n }\n gl.readPixels(opts.x ?? 0, opts.y ?? 0, width, height, format, type, buffer);\n return buffer;\n}\n\n/**\n * Read a rectangle of the render target's color attachment into a GPU\n * `WebGLBuffer` (`PIXEL_PACK_BUFFER` readback): `gl.readPixels` with a byte\n * offset instead of a CPU array, so the copy stays on the GPU and can be\n * consumed later without a sync stall (e.g. `gl.fenceSync` +\n * `gl.getBufferSubData`, or as vertex data).\n *\n * Same rectangle / `format` rules as {@link readRenderTargetPixels}: whole\n * target as RGBA by default, `gl.RED` / `gl.RG` read 1 / 2 channels per texel\n * tightly packed (`PACK_ALIGNMENT` 1). The texel type follows the color\n * attachment type. `buffer` must already be allocated (`gl.bufferData`) large\n * enough for `offset + width*height*channels*bytesPerChannel` bytes, and (per\n * WebGL2) must never have been bound to `ELEMENT_ARRAY_BUFFER`.\n *\n * Binds the target's framebuffer (left bound, like\n * {@link readRenderTargetPixels}), binds `buffer` to `PIXEL_PACK_BUFFER` for\n * the read and then unbinds it (`null`), so later CPU readbacks keep working.\n * No-op on a lost/disposed context or a disposed target.\n *\n * @param rt - The render target to read from.\n * @param buffer - Destination `WebGLBuffer`, created on `rt`'s context.\n * @param options - See {@link GLReadPixelsToBufferOptions}.\n */\nexport function readRenderTargetPixelsToBuffer(rt: GLRenderTarget, buffer: WebGLBuffer, options?: GLReadPixelsToBufferOptions): void {\n const opts = options ?? {};\n const format = opts.format ?? RGBA;\n const gl = bindForRead(rt, format);\n if (gl === null) {\n return;\n }\n gl.bindBuffer(gl.PIXEL_PACK_BUFFER, buffer);\n gl.readPixels(opts.x ?? 0, opts.y ?? 0, opts.width ?? rt.width, opts.height ?? rt.height, format, rt._config.type, opts.offset ?? 0);\n gl.bindBuffer(gl.PIXEL_PACK_BUFFER, null);\n}\n\n/** Shared readback prologue: bail on a lost/disposed context or target, bind\n * `rt`'s framebuffer (cached), and set `PACK_ALIGNMENT` 1 for the tightly\n * packed `RED` / `RG` reads. Returns the context, or `null` to skip the read. */\nfunction bindForRead(rt: GLRenderTarget, format: GLenum): WebGL2RenderingContext | null {\n const engine = rt._engine;\n if (engine._isLost || engine._disposed || rt._disposed) {\n return null;\n }\n const gl = engine.gl;\n const s = engine._state;\n if (s.boundFramebuffer !== rt._framebuffer) {\n gl.bindFramebuffer(gl.FRAMEBUFFER, rt._framebuffer);\n s.boundFramebuffer = rt._framebuffer;\n }\n if (format !== RGBA) {\n // Not cached: a sync readback dwarfs it, and callers may change it directly.\n gl.pixelStorei(gl.PACK_ALIGNMENT, 1);\n }\n return gl;\n}\n\n/**\n * Release the render target's framebuffer, depth/stencil renderbuffer and (iff\n * owned) color texture, and unregister it from the engine. Idempotent, and a\n * no-op for `null`/`undefined` (so an optional target can be released\n * unconditionally). Clears the bound-framebuffer cache if it pointed at this\n * target, and any sampler slot that held the color texture handle.\n *\n * A BYO {@link GLRenderTargetOptions.colorTexture} is NOT disposed here — it is\n * engine-managed and the caller owns its lifetime.\n *\n * @param rt - The render target to dispose, or `null`/`undefined` for a no-op.\n */\nexport function disposeRenderTarget(rt: GLRenderTarget | null | undefined): void {\n if (rt === null || rt === undefined || rt._disposed) {\n return;\n }\n const engine = rt._engine;\n rt._disposed = true;\n // Only the owned color texture is marked disposed here; a BYO texture stays\n // live for its caller-owner.\n if (rt._config.ownsColorTexture) {\n rt.texture._disposed = true;\n }\n if (engine._currentRenderTarget === rt) {\n engine._currentRenderTarget = null;\n }\n const i = engine._renderTargets.indexOf(rt);\n if (i !== -1) {\n engine._renderTargets.splice(i, 1);\n }\n // Capture the handle before _deleteGpu nulls it (owned case).\n const handle = rt.texture.handle;\n if (!engine._isLost && !engine._disposed) {\n rt._deleteGpu(engine.gl);\n } else {\n rt._framebuffer = null;\n rt._depthStencil = null;\n }\n if (handle !== null) {\n const bound = engine._state.boundTextures;\n for (let u = 0; u < bound.length; u++) {\n if (bound[u] === handle) {\n bound[u] = null;\n }\n }\n }\n}\n\n/* ──────────────────────────── internal helpers ──────────────────────────── */\n\n/** Shared private constructor. Validates size, resolves the config (given the\n * exact sized `internalFormat` by the caller — NO format-table lookup), wires\n * the owned-or-BYO color texture, allocates the GPU set atomically, and\n * registers the target. */\nfunction buildRT(engine: GLEngineContext, width: number, height: number, options: GLRenderTargetOptions, internalFormat: GLenum, format: GLenum, type: GLenum): GLRenderTarget {\n validateSize(width, height);\n const gl = engine.gl;\n const byo = options.colorTexture;\n const config: ResolvedRTConfig = {\n internalFormat,\n format,\n type,\n hasDepth: options.generateDepthBuffer ?? false,\n minFilter: options.minFilter ?? LINEAR,\n magFilter: options.magFilter ?? LINEAR,\n wrapS: options.wrapS ?? CLAMP_TO_EDGE,\n wrapT: options.wrapT ?? CLAMP_TO_EDGE,\n ownsColorTexture: byo === undefined,\n };\n\n const texture: GLTexture = byo ?? {\n handle: null as unknown as WebGLTexture,\n target: gl.TEXTURE_2D,\n width,\n height,\n isReady: false,\n _engine: engine,\n _disposed: false,\n _refCount: 1,\n // Owned-by-RT: its storage is (re)allocated by allocateRenderTargetGpu,\n // never via the engine `_textures` replay (it is NOT registered there).\n _upload: () => {},\n _wasReady: false,\n };\n\n const rt: GLRenderTarget = {\n texture,\n width,\n height,\n isReady: false,\n _framebuffer: null,\n _depthStencil: null,\n _rebuildDepthStencil: undefined,\n _config: config,\n _engine: engine,\n _disposed: false,\n _deleteGpu: () => {},\n _restore: () => {},\n };\n\n rt._deleteGpu = (glc: WebGL2RenderingContext): void => {\n const s = engine._state;\n if (rt._framebuffer !== null) {\n // Deleting the bound FBO reverts GL to framebuffer 0 (spec) — just\n // reset the cache, no explicit rebind needed.\n if (s.boundFramebuffer === rt._framebuffer) {\n s.boundFramebuffer = null;\n }\n glc.deleteFramebuffer(rt._framebuffer);\n rt._framebuffer = null;\n }\n if (rt._depthStencil !== null) {\n glc.deleteRenderbuffer(rt._depthStencil);\n rt._depthStencil = null;\n }\n // Never delete a BYO color texture — the caller-owner manages it.\n if (config.ownsColorTexture && rt.texture.handle !== null) {\n glc.deleteTexture(rt.texture.handle);\n rt.texture.handle = null as unknown as WebGLTexture;\n }\n rt.texture.isReady = false;\n rt.isReady = false;\n };\n\n rt._restore = (target: GLEngineContext): void => {\n // Resilient: a restore failure must not break the engine's restore loop.\n try {\n allocateRenderTargetGpu(target, rt);\n } catch (err) {\n console.error(\"lite-gl: render target restore failed\", err);\n }\n };\n\n allocateRenderTargetGpu(engine, rt);\n engine._renderTargets.push(rt);\n return rt;\n}\n\n/** Downgrade a requested float/half-float color type to the best renderable\n * type the engine supports, mirroring Babylon's `getTextureType`. */\nfunction resolveColorType(engine: GLEngineContext, type: GLenum): GLenum {\n const gl = engine.gl;\n if (type === FLOAT && !engine.caps.textureFloatRender) {\n return engine.caps.textureHalfFloatRender ? gl.HALF_FLOAT : gl.UNSIGNED_BYTE;\n }\n if (type === HALF_FLOAT && !engine.caps.textureHalfFloatRender) {\n return gl.UNSIGNED_BYTE;\n }\n return type;\n}\n\n/** Validate a render-target size is a pair of positive integers. */\nfunction validateSize(width: number, height: number): void {\n if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1) {\n throw new Error(`lite-gl: render target size must be positive integers, got ${width}x${height}`);\n }\n}\n\n/**\n * (Re)allocate the color texture (iff owned), framebuffer and depth/stencil\n * renderbuffer into fresh handles, attach them, and validate completeness.\n * Shared by create / resize / context-restore. No-op on a lost/disposed context.\n *\n * ATOMIC: captures the previously-bound framebuffer and restores it in a\n * `finally`; on any failure deletes the partial FBO / renderbuffer (and the\n * owned color texture it created), nulls the fields, and rethrows — so a failed\n * build never leaks a GPU handle nor leaves a half-attached framebuffer bound.\n */\nfunction allocateRenderTargetGpu(engine: GLEngineContext, rt: GLRenderTarget): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const gl = engine.gl;\n const s = engine._state;\n const c = rt._config;\n const prevFb = s.boundFramebuffer;\n let createdTexture: WebGLTexture | null = null;\n let fb: WebGLFramebuffer | null = null;\n let rb: WebGLRenderbuffer | null = null;\n try {\n // ── Color texture (owned only) ───────────────────────────────────────\n if (c.ownsColorTexture) {\n const texHandle = gl.createTexture();\n if (texHandle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null (render target color)\");\n }\n createdTexture = texHandle;\n rt.texture.handle = texHandle;\n bindTextureForUpload(engine, texHandle);\n gl.texImage2D(gl.TEXTURE_2D, 0, c.internalFormat, rt.width, rt.height, 0, c.format, c.type, null);\n setBoundTextureParams(gl, c.minFilter, c.magFilter, c.wrapS, c.wrapT);\n rt.texture.isReady = true;\n rt.texture._wasReady = true;\n }\n\n // ── Framebuffer + attachments ────────────────────────────────────────\n fb = gl.createFramebuffer();\n if (fb === null) {\n throw new Error(\"lite-gl: gl.createFramebuffer returned null\");\n }\n rt._framebuffer = fb;\n gl.bindFramebuffer(gl.FRAMEBUFFER, fb);\n s.boundFramebuffer = fb;\n gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rt.texture.handle, 0);\n\n // ── Depth (core, DEPTH-ONLY) ─────────────────────────────────────────\n // The core only ever builds a DEPTH_COMPONENT16 depth buffer. Stencil /\n // packed depth-stencil is an opt-in installed by\n // `generateRenderTargetStencil` (`@babylonjs/lite-gl`),\n // which sets `_rebuildDepthStencil` to a closure that REPLACES the\n // depth-only buffer below with its own packed/stencil renderbuffer. That\n // hook is re-run here on every rebuild (create / resize / context-restore)\n // so a helper-added stencil attachment survives at the new size.\n rt._depthStencil = null;\n if (c.hasDepth) {\n rb = gl.createRenderbuffer();\n if (rb === null) {\n throw new Error(\"lite-gl: gl.createRenderbuffer returned null\");\n }\n gl.bindRenderbuffer(gl.RENDERBUFFER, rb);\n gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, rt.width, rt.height);\n gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, rb);\n gl.bindRenderbuffer(gl.RENDERBUFFER, null);\n rt._depthStencil = rb;\n }\n // A helper-attached stencil replaces/augments the depth-only buffer.\n rt._rebuildDepthStencil?.(engine);\n\n const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);\n if (status !== gl.FRAMEBUFFER_COMPLETE) {\n throw new Error(`lite-gl: render target framebuffer incomplete (status 0x${status.toString(16)})`);\n }\n rt.isReady = true;\n } catch (e) {\n // Free whatever depth/stencil renderbuffer is currently attached. After a\n // `_rebuildDepthStencil` hook ran, `rt._depthStencil` may be a packed buffer\n // the hook swapped in (it deletes the core `rb` itself on success), so free\n // the live `rt._depthStencil` rather than the stale local `rb`.\n if (rt._depthStencil !== null) {\n gl.deleteRenderbuffer(rt._depthStencil);\n }\n rt._depthStencil = null;\n if (fb !== null) {\n gl.deleteFramebuffer(fb);\n }\n rt._framebuffer = null;\n // Delete ONLY a texture we created here (owned). Never a BYO texture.\n if (createdTexture !== null) {\n gl.deleteTexture(createdTexture);\n rt.texture.handle = null as unknown as WebGLTexture;\n rt.texture.isReady = false;\n }\n rt.isReady = false;\n throw e;\n } finally {\n // Restore the previously-bound draw target — creation/restore must not\n // silently redirect subsequent draws.\n if (s.boundFramebuffer !== prevFb) {\n gl.bindFramebuffer(gl.FRAMEBUFFER, prevFb);\n s.boundFramebuffer = prevFb;\n }\n }\n}\n\n/** Inline cached `gl.viewport` — kept local so the render-target module has\n * no runtime dependency on the effect-renderer module. */\nfunction setViewportCached(engine: GLEngineContext, x: number, y: number, w: number, h: number): void {\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"]}
|
|
1
|
+
{"version":3,"file":"render-target.js","sourceRoot":"","sources":["../src/render-target.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,OAAO,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,qBAAqB,EAAkB,MAAM,cAAc,CAAC;AAG9I;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAsB;IACtD,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;IAC9B,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,SAAS,IAAI,MAAM,CAAC,SAAS,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAC5E,OAAO;IACX,CAAC;IACD,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;IACrB,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,oGAAoG;IACpG,IAAI,CAAC,CAAC,gBAAgB,KAAK,IAAI,EAAE,CAAC;QAC9B,EAAE,CAAC,eAAe,CAAC,EAAE,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;IAC7C,CAAC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AAM,CAAC;QAChC,CAAC;IACL,CAAC;AACL,CAAC;AAED;2DAC2D;AAC3D,SAAS,iBAAiB,CAAC,MAAuB,EAAE,CAAS,EAAE,CAAS,EAAE,CAAS,EAAE,CAAS;IAC1F,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,IAAI,CAAC,CAAC,SAAS,KAAK,CAAC,IAAI,CAAC,CAAC,SAAS,KAAK,CAAC,IAAI,CAAC,CAAC,SAAS,KAAK,CAAC,IAAI,CAAC,CAAC,SAAS,KAAK,CAAC,EAAE,CAAC;QACnF,OAAO;IACX,CAAC;IACD,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC;IAChB,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC;IAChB,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC;IAChB,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC;IAChB,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AACnC,CAAC","sourcesContent":["/**\n * Render-to-texture (offscreen framebuffer) support.\n *\n * Part of the public API via the `@babylonjs/lite-gl` barrel. The package is\n * `sideEffects: false`, so consumers that only render fullscreen effects to the\n * canvas tree-shake the FBO code out.\n *\n * This is the lite-gl equivalent of Babylon's `RenderTargetWrapper` +\n * `ThinEngine.createRenderTargetTexture` / `bindFramebuffer` /\n * `restoreDefaultFramebuffer` / `_readTexturePixelsSync`. A render target owns a\n * color {@link GLTexture} (or attaches a caller-supplied one) plus an optional\n * depth and/or stencil renderbuffer, all wrapped in a single\n * `WebGLFramebuffer`.\n *\n * The default {@link createRenderTarget} makes an 8-bit **RGBA8** (or, via\n * `format`, `R8` / `RG8`) color target and ships none of the HDR sized-format\n * knowledge; {@link createFloatRenderTarget}\n * is the separate opt-in for float / half-float color attachments (it alone\n * carries the `RGBA16F` / `RGBA32F` table, so RGBA8 consumers tree-shake it\n * away).\n *\n * The full color/depth/stencil GPU set is rebuilt automatically on\n * `webglcontextrestored` — the render target registers itself with the engine's\n * `_renderTargets` registry, and the context-restore protocol calls its\n * `_restore` closure AFTER the standalone texture replay. A caller-supplied\n * (BYO) color texture lives in the engine's `_textures` registry and is restored\n * there first; the RT then re-attaches its freshly-swapped handle.\n */\nimport { bindTextureForUpload, pickSizedInternalFormat, resolveLdrInternalFormat, setBoundTextureParams, type GLTexture } from \"./texture.js\";\nimport type { GLEngineContext } from \"./context.js\";\n\n/**\n * Presents a retained composition target to the whole canvas without disturbing\n * its contents; an active scissor rectangle does not clip the presentation.\n * Presentation copies framebuffers, which WebGL2 forbids into a multisampled\n * canvas, so it throws for an engine created with `antialias: true`.\n */\nexport function presentRenderTarget(target: GLRenderTarget): void {\n const engine = target._engine;\n if (engine._isLost || engine._disposed || target._disposed || !target.isReady) {\n return;\n }\n const gl = engine.gl;\n const s = engine._state;\n // A wiped cache records the canvas binding whatever is actually bound, so that binding is reissued.\n if (s.boundFramebuffer === null) {\n gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n }\n restoreDefaultFramebuffer(engine);\n // Queried with the canvas framebuffer bound; its sample count is fixed when the context is created.\n engine._canvasSamples ??= Number(gl.getParameter(gl.SAMPLES));\n if (engine._canvasSamples > 0) {\n throw new Error(\"lite-gl: presentRenderTarget cannot copy into a multisampled canvas; create the engine without antialias\");\n }\n let scissorEnabled = s.scissorEnabled;\n if (scissorEnabled === -1) {\n scissorEnabled = gl.isEnabled(gl.SCISSOR_TEST) ? 1 : 0;\n }\n if (scissorEnabled === 1) {\n gl.disable(gl.SCISSOR_TEST);\n }\n gl.bindFramebuffer(gl.READ_FRAMEBUFFER, target._framebuffer);\n gl.blitFramebuffer(0, 0, target.width, target.height, 0, 0, engine.canvas.width, engine.canvas.height, gl.COLOR_BUFFER_BIT, gl.NEAREST);\n gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);\n if (scissorEnabled === 1) {\n gl.enable(gl.SCISSOR_TEST);\n }\n s.scissorEnabled = scissorEnabled;\n}\n\n/** GL `gl.UNSIGNED_BYTE` — the default (RGBA8) color attachment type. */\nconst UNSIGNED_BYTE = 0x1401;\n/** GL `gl.HALF_FLOAT`. */\nconst HALF_FLOAT = 0x140b;\n/** GL `gl.FLOAT`. */\nconst FLOAT = 0x1406;\n/** GL `gl.RGBA`. */\nconst RGBA = 0x1908;\n/** GL `gl.RED`. */\nconst RED = 0x1903;\n/** GL `gl.RG`. */\nconst RG = 0x8227;\n/** GL `gl.LINEAR`. */\nconst LINEAR = 0x2601;\n/** GL `gl.CLAMP_TO_EDGE`. */\nconst CLAMP_TO_EDGE = 0x812f;\n\n/** Options for {@link createRenderTarget}. Every field has a Babylon-matching\n * default: the bare `createRenderTarget(engine, width, height)` makes an RGBA8\n * color-only target with linear filtering and clamp wrapping. */\nexport interface GLRenderTargetOptions {\n /** Allocate a depth renderbuffer (`DEPTH_COMPONENT16`). Default `false`.\n * Stencil is NOT a create option — opt in (packed depth+stencil, or\n * stencil-only) via `generateRenderTargetStencil`\n * (`@babylonjs/lite-gl`), which keeps the stencil/packed\n * renderbuffer code out of the render-target core bundle. */\n generateDepthBuffer?: boolean;\n /** 8-bit color attachment format: `gl.RGBA` (default, `RGBA8`), `gl.RG`\n * (`RG8`) or `gl.RED` (`R8`) — ≙ Babylon's `RenderTargetCreationOptions.format`.\n * Ignored when {@link colorTexture} is supplied. Read single/two-channel\n * targets tightly via `readRenderTargetPixels(rt, { format })`. */\n format?: GLenum;\n /** Color texture minification filter. Default `gl.LINEAR`. */\n minFilter?: GLenum;\n /** Color texture magnification filter. Default `gl.LINEAR`. */\n magFilter?: GLenum;\n /** Color texture S wrap. Default `gl.CLAMP_TO_EDGE`. */\n wrapS?: GLenum;\n /** Color texture T wrap. Default `gl.CLAMP_TO_EDGE`. */\n wrapT?: GLenum;\n /** Attach a caller-supplied (BYO) color {@link GLTexture} instead of creating\n * one. When supplied the render target does NOT own or restore it — the\n * texture is engine-managed (restored by the standard texture-restore path\n * first) and the RT re-attaches its swapped handle afterwards. The caller is\n * responsible for sizing it to `width`×`height` and for disposing it.\n * Readbacks use the creator's texel type, so a float / half-float texture\n * must be attached via `createFloatRenderTarget` with a matching `type`\n * (`createRenderTarget` reads as `UNSIGNED_BYTE`). */\n colorTexture?: GLTexture;\n}\n\n/** Options for {@link createFloatRenderTarget} — {@link GLRenderTargetOptions}\n * (always RGBA, so without `format`) plus the float color `type`. */\nexport interface GLFloatRenderTargetOptions extends Omit<GLRenderTargetOptions, \"format\"> {\n /** Float color attachment type. Default `gl.HALF_FLOAT`. Pass `gl.FLOAT` for\n * full 32-bit. Downgraded to the best renderable type the engine supports\n * (`caps.textureFloatRender` / `caps.textureHalfFloatRender`), mirroring\n * Babylon's `getTextureType`. */\n type?: GLenum;\n}\n\n/** Read rectangle and channel layout shared by {@link readRenderTargetPixels}\n * and {@link readRenderTargetPixelsToBuffer}. Every field is optional: the\n * defaults read the whole target as RGBA. */\nexport interface GLReadPixelsRegionOptions {\n /** Lower-left X of the read rectangle, in texels. Default `0`. */\n x?: number;\n /** Lower-left Y of the read rectangle, in texels. Default `0`. */\n y?: number;\n /** Read rectangle width in texels. Default: the target's width. */\n width?: number;\n /** Read rectangle height in texels. Default: the target's height. */\n height?: number;\n /** Read format: `gl.RGBA` (default, 4 channels), `gl.RG` (2) or `gl.RED` (1). */\n format?: GLenum;\n}\n\n/** Options for {@link readRenderTargetPixels}. Every field is optional: the bare\n * `readRenderTargetPixels(rt)` reads the whole target as RGBA into a fresh\n * buffer. */\nexport interface GLReadPixelsOptions extends GLReadPixelsRegionOptions {\n /** Preallocated buffer (`width*height*channels` elements of the matching\n * type), reused to avoid per-call allocation. Default: a fresh buffer. */\n into?: ArrayBufferView;\n}\n\n/** Options for {@link readRenderTargetPixelsToBuffer}. */\nexport interface GLReadPixelsToBufferOptions extends GLReadPixelsRegionOptions {\n /** Byte offset into the destination `WebGLBuffer` where the first texel is\n * written. Must be a multiple of the read type's size. Default `0`. */\n offset?: number;\n}\n\n/** Resolved (defaults-applied) attachment description, retained on the render\n * target so `webglcontextrestored` can rebuild the exact same GPU set. */\ninterface ResolvedRTConfig {\n internalFormat: GLenum;\n format: GLenum;\n type: GLenum;\n hasDepth: boolean;\n minFilter: GLenum;\n magFilter: GLenum;\n wrapS: GLenum;\n wrapT: GLenum;\n /** True when the RT created (and therefore owns/restores/deletes) its color\n * texture; false for a BYO {@link GLRenderTargetOptions.colorTexture}. */\n ownsColorTexture: boolean;\n}\n\n/**\n * An offscreen render target — a `WebGLFramebuffer` wrapping a color\n * {@link GLTexture} and an optional depth / stencil renderbuffer. The lite-gl\n * counterpart of Babylon's `RenderTargetWrapper`.\n */\nexport interface GLRenderTarget {\n /** The color attachment, sampleable like any other {@link GLTexture}\n * (`setEffectTexture` / `bindTexture`). For an owned attachment its handle\n * is swapped on `webglcontextrestored` while consumers keep this same\n * reference. */\n texture: GLTexture;\n /** Color attachment width in texels. */\n width: number;\n /** Color attachment height in texels. */\n height: number;\n /** True once the color attachment + framebuffer are allocated. */\n isReady: boolean;\n /** @internal The framebuffer object. Swapped on context-restore. */\n _framebuffer: WebGLFramebuffer | null;\n /** @internal Depth-only (`DEPTH_COMPONENT16`) renderbuffer built by the core,\n * OR the packed/stencil renderbuffer installed by `generateRenderTargetStencil`\n * — a single field either way. Null when neither depth nor stencil is used. */\n _depthStencil: WebGLRenderbuffer | null;\n /**\n * @internal Optional stencil rebuild hook installed by\n * `generateRenderTargetStencil` (`@babylonjs/lite-gl`). When\n * present it OWNS the depth/stencil renderbuffer (replacing the core\n * depth-only buffer) and is re-invoked after every FBO rebuild\n * (create-via-helper, resize, context-restore) so the attachment survives.\n */\n _rebuildDepthStencil?: (engine: GLEngineContext) => void;\n /** @internal Resolved attachment config, for context-restore rebuild. */\n _config: ResolvedRTConfig;\n /** The engine that created (and owns) this render target. Operations on it derive\n * their engine from here, so they take the render target alone. @internal */\n _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n /** @internal Delete every GPU object the target owns (FBO, renderbuffer, and\n * the color texture iff owned). Resets the bound-framebuffer cache if it\n * pointed at this target. Called by `disposeRenderTarget`, `resizeRenderTarget`\n * and engine dispose. */\n _deleteGpu: (gl: WebGL2RenderingContext) => void;\n /** @internal Rebuild the FBO + renderbuffer (+ owned color texture) into\n * fresh handles after `webglcontextrestored`. */\n _restore: (engine: GLEngineContext) => void;\n}\n\n/**\n * Create an offscreen 8-bit render target — **RGBA8** by default, or `R8` /\n * `RG8` via {@link GLRenderTargetOptions.format}.\n *\n * The color attachment is an owned {@link GLTexture} (rebuilt by this target's\n * own restore hook), unless {@link GLRenderTargetOptions.colorTexture} supplies a\n * caller-managed (BYO) one. Mirrors Babylon's `createRenderTargetTexture`.\n *\n * @param engine - The engine to allocate GL resources on.\n * @param width - Color attachment width in texels. Must be a positive integer.\n * @param height - Color attachment height in texels. Must be a positive integer.\n * @param options - See {@link GLRenderTargetOptions}.\n * @returns The new {@link GLRenderTarget}.\n * @throws If `format` is not `RGBA` / `RG` / `RED`, `width`/`height` are not\n * positive integers, a GL handle could not be allocated, or the resulting\n * framebuffer is not complete. On failure every partial GPU object (including\n * an owned color texture) is released first.\n */\nexport function createRenderTarget(engine: GLEngineContext, width: number, height: number, options?: GLRenderTargetOptions): GLRenderTarget {\n const opts = options ?? {};\n const format = opts.format ?? RGBA;\n if (format !== RGBA && format !== RG && format !== RED) {\n throw new Error(`lite-gl: unsupported render target format 0x${format.toString(16)}`);\n }\n return buildRT(engine, width, height, opts, resolveLdrInternalFormat(engine.gl, format, UNSIGNED_BYTE), format, UNSIGNED_BYTE);\n}\n\n/**\n * Create an offscreen **float / half-float** render target — the HDR opt-in\n * counterpart of {@link createRenderTarget}. This is the only render-target\n * factory that references the `RGBA16F` / `RGBA32F` sized-format table, so RGBA8\n * consumers ship none of it.\n *\n * Defaults to `gl.HALF_FLOAT`; pass `options.type = gl.FLOAT` for full 32-bit.\n * The requested type is downgraded to the best renderable type the engine\n * supports (mirroring Babylon's `getTextureType`).\n *\n * @param engine - The engine to allocate GL resources on.\n * @param width - Color attachment width in texels. Must be a positive integer.\n * @param height - Color attachment height in texels. Must be a positive integer.\n * @param options - See {@link GLFloatRenderTargetOptions}.\n * @returns The new {@link GLRenderTarget}.\n * @throws As {@link createRenderTarget}.\n */\nexport function createFloatRenderTarget(engine: GLEngineContext, width: number, height: number, options?: GLFloatRenderTargetOptions): GLRenderTarget {\n const opts = options ?? {};\n const type = resolveColorType(engine, opts.type ?? HALF_FLOAT);\n const internalFormat = pickSizedInternalFormat(engine.gl, RGBA, type);\n return buildRT(engine, width, height, opts, internalFormat, RGBA, type);\n}\n\n/**\n * Bind the render target's framebuffer as the draw target and set the viewport\n * to cover it. Use {@link restoreDefaultFramebuffer} to return to the canvas.\n * Subsequent `drawEffect` / `drawIndexed` / `clearEngine` calls write into the\n * bound target.\n *\n * Cached. No-op on a lost/disposed context or a disposed `rt`. Mipmaps are NOT\n * regenerated here — refresh a target's mip chain explicitly via\n * {@link generateRenderTargetMipMaps} after rendering into it.\n *\n * @param rt - The render target to draw into.\n */\nexport function bindRenderTarget(rt: GLRenderTarget): void {\n const engine = rt._engine;\n if (engine._isLost || engine._disposed || rt._disposed) {\n return;\n }\n bindFramebufferAndViewport(engine, rt, rt._framebuffer, rt.width, rt.height);\n}\n\n/**\n * Bind the default (canvas) framebuffer and reset the viewport to the full\n * canvas — the counterpart of Babylon's `restoreDefaultFramebuffer`. Cached;\n * no-op on a lost/disposed context.\n *\n * @param engine - The engine.\n */\nexport function restoreDefaultFramebuffer(engine: GLEngineContext): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n bindFramebufferAndViewport(engine, null, null, engine.canvas.width, engine.canvas.height);\n}\n\nfunction bindFramebufferAndViewport(engine: GLEngineContext, rt: GLRenderTarget | null, fb: WebGLFramebuffer | null, width: number, height: number): void {\n const s = engine._state;\n if (s.boundFramebuffer !== fb) {\n engine.gl.bindFramebuffer(engine.gl.FRAMEBUFFER, fb);\n s.boundFramebuffer = fb;\n }\n engine._currentRenderTarget = rt;\n setViewportCached(engine, 0, 0, width, height);\n}\n\n/** Regenerate a render target's color-attachment mip chain from its (freshly\n * rendered) level-0 — mipmaps for render targets are a pure manual opt-in\n * (call this after rendering into the target). No-op for a disposed target, a\n * handle-less color attachment, or a lost/disposed context. */\nexport function generateRenderTargetMipMaps(rt: GLRenderTarget): void {\n const engine = rt._engine;\n if (engine._isLost || engine._disposed || rt._disposed || rt.texture.handle === null) {\n return;\n }\n bindTextureForUpload(engine, rt.texture.handle);\n engine.gl.generateMipmap(engine.gl.TEXTURE_2D);\n}\n\n/**\n * Resize the render target's color attachment (and depth/stencil renderbuffer).\n * Reallocates storage at the new size; the contents are discarded. The\n * `GLRenderTarget` / `GLTexture` identity is preserved, so consumers and\n * effect-sampler bindings holding the reference stay valid. If this target was\n * the live draw target, it is rebound (with the new-size viewport) afterwards.\n *\n * No-op when the size is unchanged or `rt` is disposed. While the context is\n * lost the new size is recorded but the GL reallocation is deferred to the\n * restore hook.\n *\n * @param rt - The render target to resize.\n * @param width - New width in texels (≥ 1).\n * @param height - New height in texels (≥ 1).\n */\nexport function resizeRenderTarget(rt: GLRenderTarget, width: number, height: number): void {\n const engine = rt._engine;\n if (rt._disposed) {\n return;\n }\n validateSize(width, height);\n if (rt.width === width && rt.height === height) {\n return;\n }\n rt.width = width;\n rt.height = height;\n rt.texture.width = width;\n rt.texture.height = height;\n if (engine._isLost || engine._disposed) {\n return;\n }\n // Capture the live binding BEFORE we delete the old FBO so we can restore it.\n const wasBound = engine._state.boundFramebuffer === rt._framebuffer && rt._framebuffer !== null;\n // For a BYO color texture, re-specify the caller's storage at the new size\n // (contents discarded, like the owned path). `_updateRaw` updates the\n // texture's tracked dims AND re-runs its upload closure — `_upload` alone\n // would re-upload at the stale ORIGINAL size, leaving the attachment a\n // different size than the FBO. An external handle without `_updateRaw` must\n // be resized by its owner; we then just rebuild the FBO around it. An owned\n // texture is recreated wholesale by allocateRenderTargetGpu.\n if (!rt._config.ownsColorTexture) {\n rt.texture._updateRaw?.(engine, null, width, height, 4);\n }\n rt._deleteGpu(engine.gl);\n allocateRenderTargetGpu(engine, rt);\n if (wasBound) {\n bindRenderTarget(rt);\n }\n}\n\n/**\n * Synchronously read back a rectangle of the render target's color attachment\n * via `gl.readPixels` — the lite-gl equivalent of Babylon's\n * `_readTexturePixelsSync`. Binds the target's framebuffer (leaving it bound,\n * matching Babylon).\n *\n * The returned array element type follows the color attachment type:\n * `Uint8Array` for `UNSIGNED_BYTE`, `Float32Array` for `FLOAT`, `Uint16Array`\n * for `HALF_FLOAT`. Origin is GL bottom-left.\n *\n * Reads `RGBA` by default (like Babylon; always readable, a `RED` target reads\n * back as `R,0,0,1`). Pass `format` `gl.RED` / `gl.RG` to read 1 / 2 channels\n * per texel into a tightly packed buffer (`PACK_ALIGNMENT` is set to 1 and left\n * there — lite-gl does not otherwise use it). WebGL only guarantees non-RGBA\n * reads when they match the implementation-chosen\n * `gl.IMPLEMENTATION_COLOR_READ_FORMAT` (`RED` for `R8`, `RG` for `RG8` in all\n * major browsers).\n *\n * @param rt - The render target to read from.\n * @param options - See {@link GLReadPixelsOptions}. Default: the whole target,\n * as RGBA, into a fresh buffer.\n * @returns The pixel buffer (the provided `into`, or a freshly allocated one).\n * Empty buffer on a lost/disposed context.\n */\nexport function readRenderTargetPixels(rt: GLRenderTarget, options?: GLReadPixelsOptions): ArrayBufferView {\n const opts = options ?? {};\n const width = opts.width ?? rt.width;\n const height = opts.height ?? rt.height;\n const format = opts.format ?? RGBA;\n const into = opts.into;\n const gl = bindForRead(rt, format);\n if (gl === null) {\n return into ?? new Uint8Array(0);\n }\n const type = rt._config.type;\n let buffer = into;\n if (buffer === undefined) {\n const elements = width * height * (format === RED ? 1 : format === RG ? 2 : 4);\n buffer = type === gl.FLOAT ? new Float32Array(elements) : type === gl.HALF_FLOAT ? new Uint16Array(elements) : new Uint8Array(elements);\n }\n gl.readPixels(opts.x ?? 0, opts.y ?? 0, width, height, format, type, buffer);\n return buffer;\n}\n\n/**\n * Read a rectangle of the render target's color attachment into a GPU\n * `WebGLBuffer` (`PIXEL_PACK_BUFFER` readback): `gl.readPixels` with a byte\n * offset instead of a CPU array, so the copy stays on the GPU and can be\n * consumed later without a sync stall (e.g. `gl.fenceSync` +\n * `gl.getBufferSubData`, or as vertex data).\n *\n * Same rectangle / `format` rules as {@link readRenderTargetPixels}: whole\n * target as RGBA by default, `gl.RED` / `gl.RG` read 1 / 2 channels per texel\n * tightly packed (`PACK_ALIGNMENT` 1). The texel type follows the color\n * attachment type. `buffer` must already be allocated (`gl.bufferData`) large\n * enough for `offset + width*height*channels*bytesPerChannel` bytes, and (per\n * WebGL2) must never have been bound to `ELEMENT_ARRAY_BUFFER`.\n *\n * Binds the target's framebuffer (left bound, like\n * {@link readRenderTargetPixels}), binds `buffer` to `PIXEL_PACK_BUFFER` for\n * the read and then unbinds it (`null`), so later CPU readbacks keep working.\n * No-op on a lost/disposed context or a disposed target.\n *\n * @param rt - The render target to read from.\n * @param buffer - Destination `WebGLBuffer`, created on `rt`'s context.\n * @param options - See {@link GLReadPixelsToBufferOptions}.\n */\nexport function readRenderTargetPixelsToBuffer(rt: GLRenderTarget, buffer: WebGLBuffer, options?: GLReadPixelsToBufferOptions): void {\n const opts = options ?? {};\n const format = opts.format ?? RGBA;\n const gl = bindForRead(rt, format);\n if (gl === null) {\n return;\n }\n gl.bindBuffer(gl.PIXEL_PACK_BUFFER, buffer);\n gl.readPixels(opts.x ?? 0, opts.y ?? 0, opts.width ?? rt.width, opts.height ?? rt.height, format, rt._config.type, opts.offset ?? 0);\n gl.bindBuffer(gl.PIXEL_PACK_BUFFER, null);\n}\n\n/** Shared readback prologue: bail on a lost/disposed context or target, bind\n * `rt`'s framebuffer (cached), and set `PACK_ALIGNMENT` 1 for the tightly\n * packed `RED` / `RG` reads. Returns the context, or `null` to skip the read. */\nfunction bindForRead(rt: GLRenderTarget, format: GLenum): WebGL2RenderingContext | null {\n const engine = rt._engine;\n if (engine._isLost || engine._disposed || rt._disposed) {\n return null;\n }\n const gl = engine.gl;\n const s = engine._state;\n if (s.boundFramebuffer !== rt._framebuffer) {\n gl.bindFramebuffer(gl.FRAMEBUFFER, rt._framebuffer);\n s.boundFramebuffer = rt._framebuffer;\n }\n if (format !== RGBA) {\n // Not cached: a sync readback dwarfs it, and callers may change it directly.\n gl.pixelStorei(gl.PACK_ALIGNMENT, 1);\n }\n return gl;\n}\n\n/**\n * Release the render target's framebuffer, depth/stencil renderbuffer and (iff\n * owned) color texture, and unregister it from the engine. Idempotent, and a\n * no-op for `null`/`undefined` (so an optional target can be released\n * unconditionally). Clears the bound-framebuffer cache if it pointed at this\n * target, and any sampler slot that held the color texture handle.\n *\n * A BYO {@link GLRenderTargetOptions.colorTexture} is NOT disposed here — it is\n * engine-managed and the caller owns its lifetime.\n *\n * @param rt - The render target to dispose, or `null`/`undefined` for a no-op.\n */\nexport function disposeRenderTarget(rt: GLRenderTarget | null | undefined): void {\n if (rt === null || rt === undefined || rt._disposed) {\n return;\n }\n const engine = rt._engine;\n rt._disposed = true;\n // Only the owned color texture is marked disposed here; a BYO texture stays\n // live for its caller-owner.\n if (rt._config.ownsColorTexture) {\n rt.texture._disposed = true;\n }\n if (engine._currentRenderTarget === rt) {\n engine._currentRenderTarget = null;\n }\n const i = engine._renderTargets.indexOf(rt);\n if (i !== -1) {\n engine._renderTargets.splice(i, 1);\n }\n // Capture the handle before _deleteGpu nulls it (owned case).\n const handle = rt.texture.handle;\n if (!engine._isLost && !engine._disposed) {\n rt._deleteGpu(engine.gl);\n } else {\n rt._framebuffer = null;\n rt._depthStencil = null;\n }\n if (handle !== null) {\n const bound = engine._state.boundTextures;\n for (let u = 0; u < bound.length; u++) {\n if (bound[u] === handle) {\n bound[u] = null;\n }\n }\n }\n}\n\n/* ──────────────────────────── internal helpers ──────────────────────────── */\n\n/** Shared private constructor. Validates size, resolves the config (given the\n * exact sized `internalFormat` by the caller — NO format-table lookup), wires\n * the owned-or-BYO color texture, allocates the GPU set atomically, and\n * registers the target. */\nfunction buildRT(engine: GLEngineContext, width: number, height: number, options: GLRenderTargetOptions, internalFormat: GLenum, format: GLenum, type: GLenum): GLRenderTarget {\n validateSize(width, height);\n const gl = engine.gl;\n const byo = options.colorTexture;\n const config: ResolvedRTConfig = {\n internalFormat,\n format,\n type,\n hasDepth: options.generateDepthBuffer ?? false,\n minFilter: options.minFilter ?? LINEAR,\n magFilter: options.magFilter ?? LINEAR,\n wrapS: options.wrapS ?? CLAMP_TO_EDGE,\n wrapT: options.wrapT ?? CLAMP_TO_EDGE,\n ownsColorTexture: byo === undefined,\n };\n\n const texture: GLTexture = byo ?? {\n handle: null as unknown as WebGLTexture,\n target: gl.TEXTURE_2D,\n width,\n height,\n isReady: false,\n _engine: engine,\n _disposed: false,\n _refCount: 1,\n // Owned-by-RT: its storage is (re)allocated by allocateRenderTargetGpu,\n // never via the engine `_textures` replay (it is NOT registered there).\n _upload: () => {},\n _wasReady: false,\n };\n\n const rt: GLRenderTarget = {\n texture,\n width,\n height,\n isReady: false,\n _framebuffer: null,\n _depthStencil: null,\n _rebuildDepthStencil: undefined,\n _config: config,\n _engine: engine,\n _disposed: false,\n _deleteGpu: () => {},\n _restore: () => {},\n };\n\n rt._deleteGpu = (glc: WebGL2RenderingContext): void => {\n const s = engine._state;\n if (rt._framebuffer !== null) {\n // Deleting the bound FBO reverts GL to framebuffer 0 (spec) — just\n // reset the cache, no explicit rebind needed.\n if (s.boundFramebuffer === rt._framebuffer) {\n s.boundFramebuffer = null;\n }\n glc.deleteFramebuffer(rt._framebuffer);\n rt._framebuffer = null;\n }\n if (rt._depthStencil !== null) {\n glc.deleteRenderbuffer(rt._depthStencil);\n rt._depthStencil = null;\n }\n // Never delete a BYO color texture — the caller-owner manages it.\n if (config.ownsColorTexture && rt.texture.handle !== null) {\n glc.deleteTexture(rt.texture.handle);\n rt.texture.handle = null as unknown as WebGLTexture;\n }\n rt.texture.isReady = false;\n rt.isReady = false;\n };\n\n rt._restore = (target: GLEngineContext): void => {\n // Resilient: a restore failure must not break the engine's restore loop.\n try {\n allocateRenderTargetGpu(target, rt);\n } catch (err) {\n console.error(\"lite-gl: render target restore failed\", err);\n }\n };\n\n allocateRenderTargetGpu(engine, rt);\n engine._renderTargets.push(rt);\n return rt;\n}\n\n/** Downgrade a requested float/half-float color type to the best renderable\n * type the engine supports, mirroring Babylon's `getTextureType`. */\nfunction resolveColorType(engine: GLEngineContext, type: GLenum): GLenum {\n const gl = engine.gl;\n if (type === FLOAT && !engine.caps.textureFloatRender) {\n return engine.caps.textureHalfFloatRender ? gl.HALF_FLOAT : gl.UNSIGNED_BYTE;\n }\n if (type === HALF_FLOAT && !engine.caps.textureHalfFloatRender) {\n return gl.UNSIGNED_BYTE;\n }\n return type;\n}\n\n/** Validate a render-target size is a pair of positive integers. */\nfunction validateSize(width: number, height: number): void {\n if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1) {\n throw new Error(`lite-gl: render target size must be positive integers, got ${width}x${height}`);\n }\n}\n\n/**\n * (Re)allocate the color texture (iff owned), framebuffer and depth/stencil\n * renderbuffer into fresh handles, attach them, and validate completeness.\n * Shared by create / resize / context-restore. No-op on a lost/disposed context.\n *\n * ATOMIC: captures the previously-bound framebuffer and restores it in a\n * `finally`; on any failure deletes the partial FBO / renderbuffer (and the\n * owned color texture it created), nulls the fields, and rethrows — so a failed\n * build never leaks a GPU handle nor leaves a half-attached framebuffer bound.\n */\nfunction allocateRenderTargetGpu(engine: GLEngineContext, rt: GLRenderTarget): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const gl = engine.gl;\n const s = engine._state;\n const c = rt._config;\n const prevFb = s.boundFramebuffer;\n let createdTexture: WebGLTexture | null = null;\n let fb: WebGLFramebuffer | null = null;\n let rb: WebGLRenderbuffer | null = null;\n try {\n // ── Color texture (owned only) ───────────────────────────────────────\n if (c.ownsColorTexture) {\n const texHandle = gl.createTexture();\n if (texHandle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null (render target color)\");\n }\n createdTexture = texHandle;\n rt.texture.handle = texHandle;\n bindTextureForUpload(engine, texHandle);\n gl.texImage2D(gl.TEXTURE_2D, 0, c.internalFormat, rt.width, rt.height, 0, c.format, c.type, null);\n setBoundTextureParams(gl, c.minFilter, c.magFilter, c.wrapS, c.wrapT);\n rt.texture.isReady = true;\n rt.texture._wasReady = true;\n }\n\n // ── Framebuffer + attachments ────────────────────────────────────────\n fb = gl.createFramebuffer();\n if (fb === null) {\n throw new Error(\"lite-gl: gl.createFramebuffer returned null\");\n }\n rt._framebuffer = fb;\n gl.bindFramebuffer(gl.FRAMEBUFFER, fb);\n s.boundFramebuffer = fb;\n gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rt.texture.handle, 0);\n\n // ── Depth (core, DEPTH-ONLY) ─────────────────────────────────────────\n // The core only ever builds a DEPTH_COMPONENT16 depth buffer. Stencil /\n // packed depth-stencil is an opt-in installed by\n // `generateRenderTargetStencil` (`@babylonjs/lite-gl`),\n // which sets `_rebuildDepthStencil` to a closure that REPLACES the\n // depth-only buffer below with its own packed/stencil renderbuffer. That\n // hook is re-run here on every rebuild (create / resize / context-restore)\n // so a helper-added stencil attachment survives at the new size.\n rt._depthStencil = null;\n if (c.hasDepth) {\n rb = gl.createRenderbuffer();\n if (rb === null) {\n throw new Error(\"lite-gl: gl.createRenderbuffer returned null\");\n }\n gl.bindRenderbuffer(gl.RENDERBUFFER, rb);\n gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, rt.width, rt.height);\n gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, rb);\n gl.bindRenderbuffer(gl.RENDERBUFFER, null);\n rt._depthStencil = rb;\n }\n // A helper-attached stencil replaces/augments the depth-only buffer.\n rt._rebuildDepthStencil?.(engine);\n\n const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);\n if (status !== gl.FRAMEBUFFER_COMPLETE) {\n throw new Error(`lite-gl: render target framebuffer incomplete (status 0x${status.toString(16)})`);\n }\n rt.isReady = true;\n } catch (e) {\n // Free whatever depth/stencil renderbuffer is currently attached. After a\n // `_rebuildDepthStencil` hook ran, `rt._depthStencil` may be a packed buffer\n // the hook swapped in (it deletes the core `rb` itself on success), so free\n // the live `rt._depthStencil` rather than the stale local `rb`.\n if (rt._depthStencil !== null) {\n gl.deleteRenderbuffer(rt._depthStencil);\n }\n rt._depthStencil = null;\n if (fb !== null) {\n gl.deleteFramebuffer(fb);\n }\n rt._framebuffer = null;\n // Delete ONLY a texture we created here (owned). Never a BYO texture.\n if (createdTexture !== null) {\n gl.deleteTexture(createdTexture);\n rt.texture.handle = null as unknown as WebGLTexture;\n rt.texture.isReady = false;\n }\n rt.isReady = false;\n throw e;\n } finally {\n // Restore the previously-bound draw target — creation/restore must not\n // silently redirect subsequent draws.\n if (s.boundFramebuffer !== prevFb) {\n gl.bindFramebuffer(gl.FRAMEBUFFER, prevFb);\n s.boundFramebuffer = prevFb;\n }\n }\n}\n\n/** Inline cached `gl.viewport` — kept local so the render-target module has\n * no runtime dependency on the effect-renderer module. */\nfunction setViewportCached(engine: GLEngineContext, x: number, y: number, w: number, h: number): void {\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"]}
|
package/scissor.d.ts
CHANGED
|
@@ -17,8 +17,8 @@ import type { GLEngineContext } from "./context.js";
|
|
|
17
17
|
* @param engine - The engine.
|
|
18
18
|
* @param x - Lower-left X of the scissor rectangle, in pixels.
|
|
19
19
|
* @param y - Lower-left Y of the scissor rectangle, in pixels.
|
|
20
|
-
* @param width - Rectangle width in pixels.
|
|
21
|
-
* @param height - Rectangle height in pixels.
|
|
20
|
+
* @param width - Rectangle width in pixels; a nonnegative integer.
|
|
21
|
+
* @param height - Rectangle height in pixels; a nonnegative integer.
|
|
22
22
|
*/
|
|
23
23
|
export declare function setScissor(engine: GLEngineContext, x: number, y: number, width: number, height: number): void;
|
|
24
24
|
/**
|
package/scissor.js
CHANGED
|
@@ -8,8 +8,8 @@ const SCISSOR_TEST = 0x0c11;
|
|
|
8
8
|
* @param engine - The engine.
|
|
9
9
|
* @param x - Lower-left X of the scissor rectangle, in pixels.
|
|
10
10
|
* @param y - Lower-left Y of the scissor rectangle, in pixels.
|
|
11
|
-
* @param width - Rectangle width in pixels.
|
|
12
|
-
* @param height - Rectangle height in pixels.
|
|
11
|
+
* @param width - Rectangle width in pixels; a nonnegative integer.
|
|
12
|
+
* @param height - Rectangle height in pixels; a nonnegative integer.
|
|
13
13
|
*/
|
|
14
14
|
export function setScissor(engine, x, y, width, height) {
|
|
15
15
|
if (engine._isLost || engine._disposed) {
|
package/scissor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scissor.js","sourceRoot":"","sources":["../src/scissor.ts"],"names":[],"mappings":"AAYA,4BAA4B;AAC5B,MAAM,YAAY,GAAG,MAAM,CAAC;AAE5B;;;;;;;;;;GAUG;AACH,MAAM,UAAU,UAAU,CAAC,MAAuB,EAAE,CAAS,EAAE,CAAS,EAAE,KAAa,EAAE,MAAc;IACnG,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrC,OAAO;IACX,CAAC;IACD,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;IACrB,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,IAAI,CAAC,CAAC,cAAc,KAAK,CAAC,EAAE,CAAC;QACzB,CAAC,CAAC,cAAc,GAAG,CAAC,CAAC;QACrB,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;IAC5B,CAAC;IACD,IAAI,CAAC,CAAC,QAAQ,KAAK,CAAC,IAAI,CAAC,CAAC,QAAQ,KAAK,CAAC,IAAI,CAAC,CAAC,QAAQ,KAAK,KAAK,IAAI,CAAC,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxF,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC;QACf,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC;QACf,CAAC,CAAC,QAAQ,GAAG,KAAK,CAAC;QACnB,CAAC,CAAC,QAAQ,GAAG,MAAM,CAAC;QACpB,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;IACpC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,MAAuB;IAClD,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrC,OAAO;IACX,CAAC;IACD,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,IAAI,CAAC,CAAC,cAAc,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO;IACX,CAAC;IACD,CAAC,CAAC,cAAc,GAAG,CAAC,CAAC;IACrB,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;AACpC,CAAC","sourcesContent":["/**\n * Scissor-test state — the lite-gl counterpart of Babylon's\n * `engine.enableScissor` / `disableScissor` (and the raw `gl.scissor` Babylon\n * issues for clip rects). Cached in `GLState`.\n *\n * Coordinates are raw WebGL (lower-left origin). A consumer using a top-left\n * coordinate convention (like ShapeBuilder) flips Y itself\n * (`viewportHeight - rect.bottom`) before calling, exactly as it did against\n * Babylon's `ThinEngine`.\n */\nimport type { GLEngineContext } from \"./context.js\";\n\n/** GL `gl.SCISSOR_TEST`. */\nconst SCISSOR_TEST = 0x0c11;\n\n/**\n * Enable the scissor test and set the clip rectangle (lower-left origin),\n * cached. Re-issues `gl.scissor` only when the rectangle changes, and\n * `gl.enable(SCISSOR_TEST)` only on the disabled→enabled transition.\n *\n * @param engine - The engine.\n * @param x - Lower-left X of the scissor rectangle, in pixels.\n * @param y - Lower-left Y of the scissor rectangle, in pixels.\n * @param width - Rectangle width in pixels.\n * @param height - Rectangle height in pixels.\n */\nexport function setScissor(engine: GLEngineContext, x: number, y: number, width: number, height: number): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const gl = engine.gl;\n const s = engine._state;\n if (s.scissorEnabled !== 1) {\n s.scissorEnabled = 1;\n gl.enable(SCISSOR_TEST);\n }\n if (s.scissorX !== x || s.scissorY !== y || s.scissorW !== width || s.scissorH !== height) {\n s.scissorX = x;\n s.scissorY = y;\n s.scissorW = width;\n s.scissorH = height;\n gl.scissor(x, y, width, height);\n }\n}\n\n/**\n * Disable the scissor test (`gl.disable(SCISSOR_TEST)`), cached. Repeated calls\n * after the first are elided.\n *\n * @param engine - The engine.\n */\nexport function disableScissor(engine: GLEngineContext): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const s = engine._state;\n if (s.scissorEnabled === 0) {\n return;\n }\n s.scissorEnabled = 0;\n engine.gl.disable(SCISSOR_TEST);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"scissor.js","sourceRoot":"","sources":["../src/scissor.ts"],"names":[],"mappings":"AAYA,4BAA4B;AAC5B,MAAM,YAAY,GAAG,MAAM,CAAC;AAE5B;;;;;;;;;;GAUG;AACH,MAAM,UAAU,UAAU,CAAC,MAAuB,EAAE,CAAS,EAAE,CAAS,EAAE,KAAa,EAAE,MAAc;IACnG,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrC,OAAO;IACX,CAAC;IACD,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;IACrB,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,IAAI,CAAC,CAAC,cAAc,KAAK,CAAC,EAAE,CAAC;QACzB,CAAC,CAAC,cAAc,GAAG,CAAC,CAAC;QACrB,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;IAC5B,CAAC;IACD,IAAI,CAAC,CAAC,QAAQ,KAAK,CAAC,IAAI,CAAC,CAAC,QAAQ,KAAK,CAAC,IAAI,CAAC,CAAC,QAAQ,KAAK,KAAK,IAAI,CAAC,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxF,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC;QACf,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC;QACf,CAAC,CAAC,QAAQ,GAAG,KAAK,CAAC;QACnB,CAAC,CAAC,QAAQ,GAAG,MAAM,CAAC;QACpB,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;IACpC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,MAAuB;IAClD,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrC,OAAO;IACX,CAAC;IACD,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,IAAI,CAAC,CAAC,cAAc,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO;IACX,CAAC;IACD,CAAC,CAAC,cAAc,GAAG,CAAC,CAAC;IACrB,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;AACpC,CAAC","sourcesContent":["/**\n * Scissor-test state — the lite-gl counterpart of Babylon's\n * `engine.enableScissor` / `disableScissor` (and the raw `gl.scissor` Babylon\n * issues for clip rects). Cached in `GLState`.\n *\n * Coordinates are raw WebGL (lower-left origin). A consumer using a top-left\n * coordinate convention (like ShapeBuilder) flips Y itself\n * (`viewportHeight - rect.bottom`) before calling, exactly as it did against\n * Babylon's `ThinEngine`.\n */\nimport type { GLEngineContext } from \"./context.js\";\n\n/** GL `gl.SCISSOR_TEST`. */\nconst SCISSOR_TEST = 0x0c11;\n\n/**\n * Enable the scissor test and set the clip rectangle (lower-left origin),\n * cached. Re-issues `gl.scissor` only when the rectangle changes, and\n * `gl.enable(SCISSOR_TEST)` only on the disabled→enabled transition.\n *\n * @param engine - The engine.\n * @param x - Lower-left X of the scissor rectangle, in pixels.\n * @param y - Lower-left Y of the scissor rectangle, in pixels.\n * @param width - Rectangle width in pixels; a nonnegative integer.\n * @param height - Rectangle height in pixels; a nonnegative integer.\n */\nexport function setScissor(engine: GLEngineContext, x: number, y: number, width: number, height: number): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const gl = engine.gl;\n const s = engine._state;\n if (s.scissorEnabled !== 1) {\n s.scissorEnabled = 1;\n gl.enable(SCISSOR_TEST);\n }\n if (s.scissorX !== x || s.scissorY !== y || s.scissorW !== width || s.scissorH !== height) {\n s.scissorX = x;\n s.scissorY = y;\n s.scissorW = width;\n s.scissorH = height;\n gl.scissor(x, y, width, height);\n }\n}\n\n/**\n * Disable the scissor test (`gl.disable(SCISSOR_TEST)`), cached. Repeated calls\n * after the first are elided.\n *\n * @param engine - The engine.\n */\nexport function disableScissor(engine: GLEngineContext): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const s = engine._state;\n if (s.scissorEnabled === 0) {\n return;\n }\n s.scissorEnabled = 0;\n engine.gl.disable(SCISSOR_TEST);\n}\n"]}
|
package/state.d.ts
CHANGED
|
@@ -83,6 +83,7 @@ export interface GLState {
|
|
|
83
83
|
statesDirty: boolean;
|
|
84
84
|
/** Scissor-test enable tri-state (`-1` unset, `0` off, `1` on). */
|
|
85
85
|
scissorEnabled: number;
|
|
86
|
+
/** Cached scissor rectangle; a `scissorW` of `-1` marks it unknown. */
|
|
86
87
|
scissorX: number;
|
|
87
88
|
scissorY: number;
|
|
88
89
|
scissorW: number;
|
package/state.js
CHANGED
|
@@ -93,7 +93,7 @@ export function createGLState(maxTextureUnits) {
|
|
|
93
93
|
scissorEnabled: -1,
|
|
94
94
|
scissorX: 0,
|
|
95
95
|
scissorY: 0,
|
|
96
|
-
scissorW:
|
|
96
|
+
scissorW: -1,
|
|
97
97
|
scissorH: 0,
|
|
98
98
|
unpackAlignment: -1,
|
|
99
99
|
unpackFlipY: -1,
|
|
@@ -132,10 +132,7 @@ export function resetGLStateCache(state) {
|
|
|
132
132
|
resetRenderStateArray(state.rs);
|
|
133
133
|
state.statesDirty = false;
|
|
134
134
|
state.scissorEnabled = -1;
|
|
135
|
-
state.
|
|
136
|
-
state.scissorY = 0;
|
|
137
|
-
state.scissorW = 0;
|
|
138
|
-
state.scissorH = 0;
|
|
135
|
+
state.scissorW = -1;
|
|
139
136
|
state.unpackAlignment = -1;
|
|
140
137
|
state.unpackFlipY = -1;
|
|
141
138
|
state.unpackPremultiplyAlpha = -1;
|
package/state.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.js","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;kFAekF;AAClF,gBAAgB,CAAC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AACnD,gBAAgB,CAAC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AACnD,gBAAgB,CAAC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AACnD,gBAAgB,CAAC,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC;AACjD,gBAAgB,CAAC,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC;AACjD,gBAAgB,CAAC,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAC;AAClD,gBAAgB,CAAC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC;AAChD,gBAAgB,CAAC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC;AAChD,gBAAgB,CAAC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC;AAChD,gBAAgB,CAAC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC;AAChD,gBAAgB,CAAC,MAAM,CAAC,MAAM,eAAe,GAAG,EAAE,CAAC;AACnD,gBAAgB,CA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AChC,KAAK,CAAC,iBAAiB,CAAC,MAAM,GAAG,CAAC,CAAC;AACvC,CAAC;AAED;;;wDAGwD;AACxD,MAAM,UAAU,YAAY,CAAC,KAAc;IACvC,iBAAiB,CAAC,KAAK,CAAC,CAAC;IACzB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;AACzB,CAAC","sourcesContent":["import type { GLEngineContext } from \"./context.js\";\n\n/* ─── Deferred render-state index-array layout ────────────────────────────────\n * The deferred render-state (blend / depth / cull / stencil / color-mask) lives\n * in a flat `Float64Array(46)` on `GLState.rs`: slots `0..20` hold the ACTUAL\n * applied GL state, slots `21..41` (`RS_X + RS_DESIRED`) the DESIRED twin, and\n * slots `42..45` the standalone (no-desired-twin) cached `gl.clearColor` RGBA.\n * `setStencilOpSeparate` lazily expands it to 52 slots for back-face operations.\n *\n * These `@internal` index consts are imported by blend.ts / depth-stencil.ts /\n * apply-states.ts. Because they are plain `const` integers, esbuild inlines each\n * to a 1–2 char literal (`rs[RS_BLEND_SRC_RGB + RS_DESIRED]` → `rs[22]`) when\n * bundling a scene — far smaller than the old cross-module `.dBlendSrcRGB` named\n * props, which could not be mangled and shipped verbatim in every bundle.\n *\n * Float64Array (NOT Int32Array) is required: `RS_STENCIL_MASK` /\n * `RS_STENCIL_FUNC_MASK` can be `0xFFFFFFFF`, which Int32 would store as `-1` and\n * collide with the `-1` \"unset\" sentinel; Float64 keeps `4294967295` distinct. */\n/** @internal */ export const RS_BLEND_ENABLED = 0;\n/** @internal */ export const RS_BLEND_SRC_RGB = 1;\n/** @internal */ export const RS_BLEND_DST_RGB = 2;\n/** @internal */ export const RS_BLEND_SRC_A = 3;\n/** @internal */ export const RS_BLEND_DST_A = 4;\n/** @internal */ export const RS_BLEND_EQ_RGB = 5;\n/** @internal */ export const RS_BLEND_EQ_A = 6;\n/** @internal */ export const RS_DEPTH_TEST = 7;\n/** @internal */ export const RS_DEPTH_MASK = 8;\n/** @internal */ export const RS_DEPTH_FUNC = 9;\n/** @internal */ export const RS_CULL_ENABLED = 10;\n/** @internal */ export const RS_CULL_FACE = 11;\n/** @internal */ export const RS_STENCIL_TEST = 12;\n/** @internal */ export const RS_STENCIL_MASK = 13;\n/** @internal */ export const RS_STENCIL_FUNC_FUNC = 14;\n/** @internal */ export const RS_STENCIL_FUNC_REF = 15;\n/** @internal */ export const RS_STENCIL_FUNC_MASK = 16;\n/** @internal */ export const RS_STENCIL_OP_FAIL = 17;\n/** @internal */ export const RS_STENCIL_OP_ZFAIL = 18;\n/** @internal */ export const RS_STENCIL_OP_ZPASS = 19;\n/** @internal */ export const RS_COLOR_MASK = 20;\n/** @internal Offset from an ACTUAL slot (`0..20`) to its DESIRED twin (`21..41`). */\nexport const RS_DESIRED = 21;\n/* Standalone cached gl.clearColor RGBA slots (`42..45`) — NO desired twin; a\n * simple apply-on-clear cache like viewport/scissor, stored in `rs` (not as named\n * GLState fields) so the index access mangles small instead of shipping\n * `.clearColorR` verbatim in every bundle. */\n/** @internal */ export const RS_CLEAR_R = 42;\n/** @internal */ export const RS_CLEAR_G = 43;\n/** @internal */ export const RS_CLEAR_B = 44;\n/** @internal */ export const RS_CLEAR_A = 45;\n\n/**\n * GL-state cache type. Owned by `GLEngineContext._state`.\n *\n * Two flavours of cached state coexist here:\n *\n * - **Eager bindings** (program / buffers / textures / VAO / framebuffer /\n * viewport / scissor / unpack). Each binding setter issues its `gl.*` call\n * immediately and updates the matching field in lock-step, eliding the call\n * when the cache already matches.\n * - **Deferred render-state** (blend / depth / cull / stencil / color-mask),\n * stored in the {@link GLState.rs} index-array. These follow Babylon's\n * `applyStates()` model: each setter records only the DESIRED slot\n * (`rs[RS_X + RS_DESIRED]`) and sets `statesDirty`; the real `gl.*` calls are\n * flushed by {@link applyGLStates} (apply-states.ts) immediately before each\n * draw / clear, reconciling DESIRED → ACTUAL and writing the actual slots\n * (`rs[RS_X]`) in lock-step. Both halves keep their `-1`/`0` unset sentinels.\n *\n * See `00-lite-gl.md` §4 for the full table of cached operations and the\n * deferred-state flush sites.\n *\n * INVARIANTS:\n * - The cache is the source of truth. If consumers poke `engine.gl.*` directly\n * they will silently corrupt this state.\n * - On `webglcontextlost` the whole `rs` array (both the actual and desired\n * halves) is reset to its initial sentinels (handles are dead anyway;\n * subsequent setters bail out on `engine._isLost`, and a post-restore setter\n * re-marks `statesDirty`).\n */\nexport interface GLState {\n currentProgram: WebGLProgram | null;\n activeTextureUnit: number;\n /** Per-unit TEXTURE_2D binding; length === caps.maxTextureUnits. */\n boundTextures: (WebGLTexture | null)[];\n /** Per-unit TEXTURE_3D binding, allocated only when first used. @internal */\n _boundTextures3D?: (WebGLTexture | null | undefined)[];\n /** Cached zero WebGL2 unpack row/image/skip layout for 3D uploads. @internal */\n _unpack3DLayoutKnown?: boolean;\n boundArrayBuffer: WebGLBuffer | null;\n boundElementBuffer: WebGLBuffer | null;\n boundVao: WebGLVertexArrayObject | null;\n viewportX: number;\n viewportY: number;\n viewportW: number;\n viewportH: number;\n /**\n * Currently-bound draw framebuffer, or `null` for the default (canvas)\n * framebuffer. Owned by the render-target module's `bindRenderTarget`\n * (binding `null` returns to the canvas). Reset to `null` on context-lost.\n */\n boundFramebuffer: WebGLFramebuffer | null;\n /**\n * Deferred render-state (blend / depth / cull / stencil / color-mask) packed\n * into a flat `Float64Array(46)`. Slots `0..20` (indexed by the `RS_*`\n * consts) are the ACTUAL applied GL state — what {@link applyGLStates} last\n * wrote to the context; slots `21..41` (`rs[RS_X + RS_DESIRED]`) are the\n * DESIRED twin the setters record. The setters write ONLY the desired half\n * (never `gl.*`, never the actual half) and raise `statesDirty`;\n * `applyGLStates` reconciles each desired → its actual twin right before a\n * draw / clear, issuing only the GL calls that changed and updating the\n * actual half in lock-step. Both preset and arbitrary blend paths feed the\n * same desired slots, so they can never desync.\n *\n * Sentinels (identical for both halves): `-1` for the tri-state enables\n * (`RS_BLEND_ENABLED` / `RS_DEPTH_TEST` / `RS_DEPTH_MASK` / `RS_CULL_ENABLED`\n * / `RS_STENCIL_TEST`) and the mask caches (`RS_STENCIL_MASK` /\n * `RS_COLOR_MASK`), plus the leading stencil-op slot\n * (`RS_STENCIL_OP_FAIL`); `0` for every other func / equation / op / ref\n * slot. The leading op sentinel lets the first op\n * setter seed omitted fields to WebGL's `KEEP` defaults before merging. An\n * unset desired equals its unset actual and is never flushed; the `-1`\n * blend/test sentinels guarantee the first applied state is never elided.\n * The blend func/equation slots are only trusted while `RS_BLEND_ENABLED` is\n * `1` — the disabled→enabled transition re-issues both (matching Babylon's\n * `AlphaState`, which does not track them while blending is off).\n *\n * `setStencilOpSeparate` lazily replaces this array with a 52-slot copy; the\n * six appended slots cache actual and desired back-face operations without\n * moving any base index.\n *\n * Float64 (not Int32): `RS_STENCIL_MASK` / `RS_STENCIL_FUNC_MASK` can be\n * `0xFFFFFFFF`, which Int32 stores as `-1` — colliding with the `-1` unset\n * sentinel. Reset on context-lost. */\n rs: Float64Array;\n /** `true` when at least one deferred-state setter ran since the last\n * {@link applyGLStates}. Gates the flush so an unchanged frame issues no\n * reconciliation work. */\n statesDirty: boolean;\n /* ─── Deferred render-state appliers (per-category, lazily installed) ──────\n * Each category's reconciler (blend / depth+cull / stencil / color-mask) is\n * installed onto its slot the first time the matching setter runs — a runtime\n * assignment, NOT a module-level side effect. `applyGLStates` dispatches ONLY\n * through these slots, so the reconcilers (and their GL reconciliation code)\n * are reachable only when their setter is in the bundle: a scene that never\n * touches a category tree-shakes its reconciler out entirely (e.g. a clear-\n * only scene drops all four). Left undefined until installed; never cleared on\n * context-lost (a post-restore setter re-installs idempotently). */\n /** @internal Blend reconciler (Babylon's `_alphaState`). */\n _flushBlend?: (engine: GLEngineContext) => void;\n /** @internal Depth + cull reconciler (Babylon's `_depthCullingState`). */\n _flushDepthCull?: (engine: GLEngineContext) => void;\n /** @internal Stencil reconciler (Babylon's `_stencilState`). */\n _flushStencil?: (engine: GLEngineContext) => void;\n /** @internal Lazy hook that mirrors shared stencil operations to the back face. */\n _setStencilBack?: (cache: GLState, state: { opFail?: GLenum; opZFail?: GLenum; opZPass?: GLenum }) => void;\n /** @internal Color-write-mask reconciler (Babylon's `setColorWrite`). */\n _flushColorMask?: (engine: GLEngineContext) => void;\n /** Scissor-test enable tri-state (`-1` unset, `0` off, `1` on). */\n scissorEnabled: number;\n scissorX: number;\n scissorY: number;\n scissorW: number;\n scissorH: number;\n /** Cached `gl.pixelStorei(UNPACK_ALIGNMENT)`, or `-1` when unset. */\n unpackAlignment: number;\n /** Cached `gl.pixelStorei(UNPACK_FLIP_Y_WEBGL)`, or `-1` when unset. */\n unpackFlipY: number;\n /** Cached `gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL)`, or `-1` unset. */\n unpackPremultiplyAlpha: number;\n /**\n * Per-location vertex-attribute enable flags for the DEFAULT (null) VAO —\n * the mesh / instancing path (mirrors Babylon's `_vertexAttribArraysEnabled`).\n * Index is the attribute location. lite-gl's quad / sprite paths use their\n * own VAOs and do not touch this. Cleared on context-lost.\n */\n enabledAttribs: boolean[];\n /**\n * Attribute locations currently configured with a non-default vertex divisor\n * (instanced attributes), mirroring Babylon's `_currentInstanceLocations`.\n * Each location is recorded once, so repeated binds without an unbind stay bounded.\n * `unbindInstanceAttributes` resets each back to divisor 0 and clears this.\n */\n instanceLocations: number[];\n /** Shared fullscreen quad — lazily created on first `drawEffect`. */\n quadVbo: WebGLBuffer | null;\n quadIbo: WebGLBuffer | null;\n quadVao: WebGLVertexArrayObject | null;\n}\n\n/** Indices in `rs` whose unset sentinel is `-1` (the tri-state enables, the\n * `stencilMask` / `colorMask` caches, and the leading shared stencil-op slot).\n * Every other slot defaults to `0`. The sentinel is written to BOTH\n * the actual (`i`) and desired (`i + RS_DESIRED`) halves. Kept as a\n * function-local literal so it stays a runtime value (no module-level\n * allocation / side effect). */\nfunction applyRenderStateSentinels(rs: Float64Array): void {\n for (const i of [RS_BLEND_ENABLED, RS_DEPTH_TEST, RS_DEPTH_MASK, RS_CULL_ENABLED, RS_STENCIL_TEST, RS_STENCIL_MASK, RS_STENCIL_OP_FAIL, RS_COLOR_MASK]) {\n rs[i] = -1;\n rs[i + RS_DESIRED] = -1;\n }\n // clearColor RGBA (standalone, no DESIRED twin) — `-1` forces the first clear\n // to issue gl.clearColor (valid components are 0..1).\n rs[RS_CLEAR_R] = rs[RS_CLEAR_G] = rs[RS_CLEAR_B] = rs[RS_CLEAR_A] = -1;\n}\n\n/** Allocate the 46-slot deferred render-state array (21 actual + 21 desired + 4\n * standalone clearColor) initialised to its unset sentinels — a fresh\n * `Float64Array` is already all `0`, so only the `-1` slots need writing. */\nfunction createRenderStateArray(): Float64Array {\n const rs = new Float64Array(46);\n applyRenderStateSentinels(rs);\n return rs;\n}\n\n/** Re-initialise a live render-state array in place: zero every slot, then\n * restore the `-1` sentinels. */\nfunction resetRenderStateArray(rs: Float64Array): void {\n rs.fill(0);\n applyRenderStateSentinels(rs);\n}\n\n/** Allocate a fresh, fully-null GLState sized for `maxTextureUnits`. */\nexport function createGLState(maxTextureUnits: number): GLState {\n return {\n currentProgram: null,\n activeTextureUnit: 0,\n boundTextures: new Array<WebGLTexture | null>(maxTextureUnits).fill(null),\n boundArrayBuffer: null,\n boundElementBuffer: null,\n boundVao: null,\n viewportX: 0,\n viewportY: 0,\n viewportW: 0,\n viewportH: 0,\n boundFramebuffer: null,\n rs: createRenderStateArray(),\n statesDirty: false,\n scissorEnabled: -1,\n scissorX: 0,\n scissorY: 0,\n scissorW: 0,\n scissorH: 0,\n unpackAlignment: -1,\n unpackFlipY: -1,\n unpackPremultiplyAlpha: -1,\n enabledAttribs: [],\n instanceLocations: [],\n quadVbo: null,\n quadIbo: null,\n quadVao: null,\n };\n}\n\n/** Reset only the cached \"current GL state\" — every binding (program / buffers /\n * textures / VAO / framebuffer) and render-state (blend / depth / stencil /\n * scissor / color-mask / viewport / unpack) field, including the whole `rs`\n * deferred render-state array (BOTH the actual applied values and the DESIRED\n * twins) plus `statesDirty` — to its unset sentinel, WITHOUT discarding owned\n * GPU resources (the shared quad). After this, the next setter in each category\n * is re-issued rather than elided.\n *\n * Used by `resetGLState` (context-lost) and by `wipeGLStateCache` (a host that\n * shares the GL context calling in after mutating raw `gl.*` state). The shared\n * quad's GL objects are still alive in the latter case, so they are preserved\n * here to avoid leaking + needlessly rebuilding them every render scope. */\nexport function resetGLStateCache(state: GLState): void {\n state.currentProgram = null;\n state.activeTextureUnit = 0;\n state.boundTextures.fill(null);\n state.boundArrayBuffer = null;\n state.boundElementBuffer = null;\n state.boundVao = null;\n state.viewportX = 0;\n state.viewportY = 0;\n state.viewportW = 0;\n state.viewportH = 0;\n state.boundFramebuffer = null;\n resetRenderStateArray(state.rs);\n state.statesDirty = false;\n state.scissorEnabled = -1;\n state.scissorX = 0;\n state.scissorY = 0;\n state.scissorW = 0;\n state.scissorH = 0;\n state.unpackAlignment = -1;\n state.unpackFlipY = -1;\n state.unpackPremultiplyAlpha = -1;\n state.enabledAttribs.length = 0;\n state.instanceLocations.length = 0;\n}\n\n/** Zero the cache in-place. Used by the context-lost handler — GL handles are\n * already dead per WebGL spec; we forget what we knew about them (including the\n * shared quad, whose GL objects are gone and must be rebuilt) so the next\n * bind/use after restore is NOT incorrectly elided. */\nexport function resetGLState(state: GLState): void {\n resetGLStateCache(state);\n state.quadVbo = null;\n state.quadIbo = null;\n state.quadVao = null;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;kFAekF;AAClF,gBAAgB,CAAC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AACnD,gBAAgB,CAAC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AACnD,gBAAgB,CAAC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AACnD,gBAAgB,CAAC,MAAM,CAAC,MA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C,EAAE,CAAC,CAAC;IAChC,KAAK,CAAC,WAAW,GAAG,KAAK,CAAC;IAC1B,KAAK,CAAC,cAAc,GAAG,CAAC,CAAC,CAAC;IAC1B,KAAK,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC;IACpB,KAAK,CAAC,eAAe,GAAG,CAAC,CAAC,CAAC;IAC3B,KAAK,CAAC,WAAW,GAAG,CAAC,CAAC,CAAC;IACvB,KAAK,CAAC,sBAAsB,GAAG,CAAC,CAAC,CAAC;IAClC,KAAK,CAAC,cAAc,CAAC,MAAM,GAAG,CAAC,CAAC;IAChC,KAAK,CAAC,iBAAiB,CAAC,MAAM,GAAG,CAAC,CAAC;AACvC,CAAC;AAED;;;wDAGwD;AACxD,MAAM,UAAU,YAAY,CAAC,KAAc;IACvC,iBAAiB,CAAC,KAAK,CAAC,CAAC;IACzB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;AACzB,CAAC","sourcesContent":["import type { GLEngineContext } from \"./context.js\";\n\n/* ─── Deferred render-state index-array layout ────────────────────────────────\n * The deferred render-state (blend / depth / cull / stencil / color-mask) lives\n * in a flat `Float64Array(46)` on `GLState.rs`: slots `0..20` hold the ACTUAL\n * applied GL state, slots `21..41` (`RS_X + RS_DESIRED`) the DESIRED twin, and\n * slots `42..45` the standalone (no-desired-twin) cached `gl.clearColor` RGBA.\n * `setStencilOpSeparate` lazily expands it to 52 slots for back-face operations.\n *\n * These `@internal` index consts are imported by blend.ts / depth-stencil.ts /\n * apply-states.ts. Because they are plain `const` integers, esbuild inlines each\n * to a 1–2 char literal (`rs[RS_BLEND_SRC_RGB + RS_DESIRED]` → `rs[22]`) when\n * bundling a scene — far smaller than the old cross-module `.dBlendSrcRGB` named\n * props, which could not be mangled and shipped verbatim in every bundle.\n *\n * Float64Array (NOT Int32Array) is required: `RS_STENCIL_MASK` /\n * `RS_STENCIL_FUNC_MASK` can be `0xFFFFFFFF`, which Int32 would store as `-1` and\n * collide with the `-1` \"unset\" sentinel; Float64 keeps `4294967295` distinct. */\n/** @internal */ export const RS_BLEND_ENABLED = 0;\n/** @internal */ export const RS_BLEND_SRC_RGB = 1;\n/** @internal */ export const RS_BLEND_DST_RGB = 2;\n/** @internal */ export const RS_BLEND_SRC_A = 3;\n/** @internal */ export const RS_BLEND_DST_A = 4;\n/** @internal */ export const RS_BLEND_EQ_RGB = 5;\n/** @internal */ export const RS_BLEND_EQ_A = 6;\n/** @internal */ export const RS_DEPTH_TEST = 7;\n/** @internal */ export const RS_DEPTH_MASK = 8;\n/** @internal */ export const RS_DEPTH_FUNC = 9;\n/** @internal */ export const RS_CULL_ENABLED = 10;\n/** @internal */ export const RS_CULL_FACE = 11;\n/** @internal */ export const RS_STENCIL_TEST = 12;\n/** @internal */ export const RS_STENCIL_MASK = 13;\n/** @internal */ export const RS_STENCIL_FUNC_FUNC = 14;\n/** @internal */ export const RS_STENCIL_FUNC_REF = 15;\n/** @internal */ export const RS_STENCIL_FUNC_MASK = 16;\n/** @internal */ export const RS_STENCIL_OP_FAIL = 17;\n/** @internal */ export const RS_STENCIL_OP_ZFAIL = 18;\n/** @internal */ export const RS_STENCIL_OP_ZPASS = 19;\n/** @internal */ export const RS_COLOR_MASK = 20;\n/** @internal Offset from an ACTUAL slot (`0..20`) to its DESIRED twin (`21..41`). */\nexport const RS_DESIRED = 21;\n/* Standalone cached gl.clearColor RGBA slots (`42..45`) — NO desired twin; a\n * simple apply-on-clear cache like viewport/scissor, stored in `rs` (not as named\n * GLState fields) so the index access mangles small instead of shipping\n * `.clearColorR` verbatim in every bundle. */\n/** @internal */ export const RS_CLEAR_R = 42;\n/** @internal */ export const RS_CLEAR_G = 43;\n/** @internal */ export const RS_CLEAR_B = 44;\n/** @internal */ export const RS_CLEAR_A = 45;\n\n/**\n * GL-state cache type. Owned by `GLEngineContext._state`.\n *\n * Two flavours of cached state coexist here:\n *\n * - **Eager bindings** (program / buffers / textures / VAO / framebuffer /\n * viewport / scissor / unpack). Each binding setter issues its `gl.*` call\n * immediately and updates the matching field in lock-step, eliding the call\n * when the cache already matches.\n * - **Deferred render-state** (blend / depth / cull / stencil / color-mask),\n * stored in the {@link GLState.rs} index-array. These follow Babylon's\n * `applyStates()` model: each setter records only the DESIRED slot\n * (`rs[RS_X + RS_DESIRED]`) and sets `statesDirty`; the real `gl.*` calls are\n * flushed by {@link applyGLStates} (apply-states.ts) immediately before each\n * draw / clear, reconciling DESIRED → ACTUAL and writing the actual slots\n * (`rs[RS_X]`) in lock-step. Both halves keep their `-1`/`0` unset sentinels.\n *\n * See `00-lite-gl.md` §4 for the full table of cached operations and the\n * deferred-state flush sites.\n *\n * INVARIANTS:\n * - The cache is the source of truth. If consumers poke `engine.gl.*` directly\n * they will silently corrupt this state.\n * - On `webglcontextlost` the whole `rs` array (both the actual and desired\n * halves) is reset to its initial sentinels (handles are dead anyway;\n * subsequent setters bail out on `engine._isLost`, and a post-restore setter\n * re-marks `statesDirty`).\n */\nexport interface GLState {\n currentProgram: WebGLProgram | null;\n activeTextureUnit: number;\n /** Per-unit TEXTURE_2D binding; length === caps.maxTextureUnits. */\n boundTextures: (WebGLTexture | null)[];\n /** Per-unit TEXTURE_3D binding, allocated only when first used. @internal */\n _boundTextures3D?: (WebGLTexture | null | undefined)[];\n /** Cached zero WebGL2 unpack row/image/skip layout for 3D uploads. @internal */\n _unpack3DLayoutKnown?: boolean;\n boundArrayBuffer: WebGLBuffer | null;\n boundElementBuffer: WebGLBuffer | null;\n boundVao: WebGLVertexArrayObject | null;\n viewportX: number;\n viewportY: number;\n viewportW: number;\n viewportH: number;\n /**\n * Currently-bound draw framebuffer, or `null` for the default (canvas)\n * framebuffer. Owned by the render-target module's `bindRenderTarget`\n * (binding `null` returns to the canvas). Reset to `null` on context-lost.\n */\n boundFramebuffer: WebGLFramebuffer | null;\n /**\n * Deferred render-state (blend / depth / cull / stencil / color-mask) packed\n * into a flat `Float64Array(46)`. Slots `0..20` (indexed by the `RS_*`\n * consts) are the ACTUAL applied GL state — what {@link applyGLStates} last\n * wrote to the context; slots `21..41` (`rs[RS_X + RS_DESIRED]`) are the\n * DESIRED twin the setters record. The setters write ONLY the desired half\n * (never `gl.*`, never the actual half) and raise `statesDirty`;\n * `applyGLStates` reconciles each desired → its actual twin right before a\n * draw / clear, issuing only the GL calls that changed and updating the\n * actual half in lock-step. Both preset and arbitrary blend paths feed the\n * same desired slots, so they can never desync.\n *\n * Sentinels (identical for both halves): `-1` for the tri-state enables\n * (`RS_BLEND_ENABLED` / `RS_DEPTH_TEST` / `RS_DEPTH_MASK` / `RS_CULL_ENABLED`\n * / `RS_STENCIL_TEST`) and the mask caches (`RS_STENCIL_MASK` /\n * `RS_COLOR_MASK`), plus the leading stencil-op slot\n * (`RS_STENCIL_OP_FAIL`); `0` for every other func / equation / op / ref\n * slot. The leading op sentinel lets the first op\n * setter seed omitted fields to WebGL's `KEEP` defaults before merging. An\n * unset desired equals its unset actual and is never flushed; the `-1`\n * blend/test sentinels guarantee the first applied state is never elided.\n * The blend func/equation slots are only trusted while `RS_BLEND_ENABLED` is\n * `1` — the disabled→enabled transition re-issues both (matching Babylon's\n * `AlphaState`, which does not track them while blending is off).\n *\n * `setStencilOpSeparate` lazily replaces this array with a 52-slot copy; the\n * six appended slots cache actual and desired back-face operations without\n * moving any base index.\n *\n * Float64 (not Int32): `RS_STENCIL_MASK` / `RS_STENCIL_FUNC_MASK` can be\n * `0xFFFFFFFF`, which Int32 stores as `-1` — colliding with the `-1` unset\n * sentinel. Reset on context-lost. */\n rs: Float64Array;\n /** `true` when at least one deferred-state setter ran since the last\n * {@link applyGLStates}. Gates the flush so an unchanged frame issues no\n * reconciliation work. */\n statesDirty: boolean;\n /* ─── Deferred render-state appliers (per-category, lazily installed) ──────\n * Each category's reconciler (blend / depth+cull / stencil / color-mask) is\n * installed onto its slot the first time the matching setter runs — a runtime\n * assignment, NOT a module-level side effect. `applyGLStates` dispatches ONLY\n * through these slots, so the reconcilers (and their GL reconciliation code)\n * are reachable only when their setter is in the bundle: a scene that never\n * touches a category tree-shakes its reconciler out entirely (e.g. a clear-\n * only scene drops all four). Left undefined until installed; never cleared on\n * context-lost (a post-restore setter re-installs idempotently). */\n /** @internal Blend reconciler (Babylon's `_alphaState`). */\n _flushBlend?: (engine: GLEngineContext) => void;\n /** @internal Depth + cull reconciler (Babylon's `_depthCullingState`). */\n _flushDepthCull?: (engine: GLEngineContext) => void;\n /** @internal Stencil reconciler (Babylon's `_stencilState`). */\n _flushStencil?: (engine: GLEngineContext) => void;\n /** @internal Lazy hook that mirrors shared stencil operations to the back face. */\n _setStencilBack?: (cache: GLState, state: { opFail?: GLenum; opZFail?: GLenum; opZPass?: GLenum }) => void;\n /** @internal Color-write-mask reconciler (Babylon's `setColorWrite`). */\n _flushColorMask?: (engine: GLEngineContext) => void;\n /** Scissor-test enable tri-state (`-1` unset, `0` off, `1` on). */\n scissorEnabled: number;\n /** Cached scissor rectangle; a `scissorW` of `-1` marks it unknown. */\n scissorX: number;\n scissorY: number;\n scissorW: number;\n scissorH: number;\n /** Cached `gl.pixelStorei(UNPACK_ALIGNMENT)`, or `-1` when unset. */\n unpackAlignment: number;\n /** Cached `gl.pixelStorei(UNPACK_FLIP_Y_WEBGL)`, or `-1` when unset. */\n unpackFlipY: number;\n /** Cached `gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL)`, or `-1` unset. */\n unpackPremultiplyAlpha: number;\n /**\n * Per-location vertex-attribute enable flags for the DEFAULT (null) VAO —\n * the mesh / instancing path (mirrors Babylon's `_vertexAttribArraysEnabled`).\n * Index is the attribute location. lite-gl's quad / sprite paths use their\n * own VAOs and do not touch this. Cleared on context-lost.\n */\n enabledAttribs: boolean[];\n /**\n * Attribute locations currently configured with a non-default vertex divisor\n * (instanced attributes), mirroring Babylon's `_currentInstanceLocations`.\n * Each location is recorded once, so repeated binds without an unbind stay bounded.\n * `unbindInstanceAttributes` resets each back to divisor 0 and clears this.\n */\n instanceLocations: number[];\n /** Shared fullscreen quad — lazily created on first `drawEffect`. */\n quadVbo: WebGLBuffer | null;\n quadIbo: WebGLBuffer | null;\n quadVao: WebGLVertexArrayObject | null;\n}\n\n/** Indices in `rs` whose unset sentinel is `-1` (the tri-state enables, the\n * `stencilMask` / `colorMask` caches, and the leading shared stencil-op slot).\n * Every other slot defaults to `0`. The sentinel is written to BOTH\n * the actual (`i`) and desired (`i + RS_DESIRED`) halves. Kept as a\n * function-local literal so it stays a runtime value (no module-level\n * allocation / side effect). */\nfunction applyRenderStateSentinels(rs: Float64Array): void {\n for (const i of [RS_BLEND_ENABLED, RS_DEPTH_TEST, RS_DEPTH_MASK, RS_CULL_ENABLED, RS_STENCIL_TEST, RS_STENCIL_MASK, RS_STENCIL_OP_FAIL, RS_COLOR_MASK]) {\n rs[i] = -1;\n rs[i + RS_DESIRED] = -1;\n }\n // clearColor RGBA (standalone, no DESIRED twin) — `-1` forces the first clear\n // to issue gl.clearColor (valid components are 0..1).\n rs[RS_CLEAR_R] = rs[RS_CLEAR_G] = rs[RS_CLEAR_B] = rs[RS_CLEAR_A] = -1;\n}\n\n/** Allocate the 46-slot deferred render-state array (21 actual + 21 desired + 4\n * standalone clearColor) initialised to its unset sentinels — a fresh\n * `Float64Array` is already all `0`, so only the `-1` slots need writing. */\nfunction createRenderStateArray(): Float64Array {\n const rs = new Float64Array(46);\n applyRenderStateSentinels(rs);\n return rs;\n}\n\n/** Re-initialise a live render-state array in place: zero every slot, then\n * restore the `-1` sentinels. */\nfunction resetRenderStateArray(rs: Float64Array): void {\n rs.fill(0);\n applyRenderStateSentinels(rs);\n}\n\n/** Allocate a fresh, fully-null GLState sized for `maxTextureUnits`. */\nexport function createGLState(maxTextureUnits: number): GLState {\n return {\n currentProgram: null,\n activeTextureUnit: 0,\n boundTextures: new Array<WebGLTexture | null>(maxTextureUnits).fill(null),\n boundArrayBuffer: null,\n boundElementBuffer: null,\n boundVao: null,\n viewportX: 0,\n viewportY: 0,\n viewportW: 0,\n viewportH: 0,\n boundFramebuffer: null,\n rs: createRenderStateArray(),\n statesDirty: false,\n scissorEnabled: -1,\n scissorX: 0,\n scissorY: 0,\n scissorW: -1,\n scissorH: 0,\n unpackAlignment: -1,\n unpackFlipY: -1,\n unpackPremultiplyAlpha: -1,\n enabledAttribs: [],\n instanceLocations: [],\n quadVbo: null,\n quadIbo: null,\n quadVao: null,\n };\n}\n\n/** Reset only the cached \"current GL state\" — every binding (program / buffers /\n * textures / VAO / framebuffer) and render-state (blend / depth / stencil /\n * scissor / color-mask / viewport / unpack) field, including the whole `rs`\n * deferred render-state array (BOTH the actual applied values and the DESIRED\n * twins) plus `statesDirty` — to its unset sentinel, WITHOUT discarding owned\n * GPU resources (the shared quad). After this, the next setter in each category\n * is re-issued rather than elided.\n *\n * Used by `resetGLState` (context-lost) and by `wipeGLStateCache` (a host that\n * shares the GL context calling in after mutating raw `gl.*` state). The shared\n * quad's GL objects are still alive in the latter case, so they are preserved\n * here to avoid leaking + needlessly rebuilding them every render scope. */\nexport function resetGLStateCache(state: GLState): void {\n state.currentProgram = null;\n state.activeTextureUnit = 0;\n state.boundTextures.fill(null);\n state.boundArrayBuffer = null;\n state.boundElementBuffer = null;\n state.boundVao = null;\n state.viewportX = 0;\n state.viewportY = 0;\n state.viewportW = 0;\n state.viewportH = 0;\n state.boundFramebuffer = null;\n resetRenderStateArray(state.rs);\n state.statesDirty = false;\n state.scissorEnabled = -1;\n state.scissorW = -1;\n state.unpackAlignment = -1;\n state.unpackFlipY = -1;\n state.unpackPremultiplyAlpha = -1;\n state.enabledAttribs.length = 0;\n state.instanceLocations.length = 0;\n}\n\n/** Zero the cache in-place. Used by the context-lost handler — GL handles are\n * already dead per WebGL spec; we forget what we knew about them (including the\n * shared quad, whose GL objects are gone and must be rebuilt) so the next\n * bind/use after restore is NOT incorrectly elided. */\nexport function resetGLState(state: GLState): void {\n resetGLStateCache(state);\n state.quadVbo = null;\n state.quadIbo = null;\n state.quadVao = null;\n}\n"]}
|