@babylonjs/lite-gl 1.24.0 → 1.24.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +5 -4
- package/blend.d.ts +1 -1
- package/blend.js +1 -1
- package/blend.js.map +1 -1
- package/context.d.ts +47 -4
- package/context.js +100 -11
- package/context.js.map +1 -1
- package/depth-stencil.d.ts +3 -2
- package/depth-stencil.js +10 -11
- package/depth-stencil.js.map +1 -1
- package/dynamic-texture.d.ts +6 -3
- package/dynamic-texture.js +16 -4
- package/dynamic-texture.js.map +1 -1
- package/effect.d.ts +12 -3
- package/effect.js +35 -24
- package/effect.js.map +1 -1
- package/family.d.ts +5 -1
- package/family.js.map +1 -1
- package/framebuffer-validation.d.ts +20 -0
- package/framebuffer-validation.js +37 -0
- package/framebuffer-validation.js.map +1 -0
- package/html-texture.d.ts +2 -2
- package/html-texture.js.map +1 -1
- package/index.d.ts +3 -2
- package/index.js +3 -2
- package/index.js.map +1 -1
- package/mesh.d.ts +68 -14
- package/mesh.js +211 -61
- package/mesh.js.map +1 -1
- package/package.json +3 -3
- package/render-target.d.ts +11 -9
- package/render-target.js +50 -35
- package/render-target.js.map +1 -1
- package/scissor.d.ts +1 -1
- package/scissor.js.map +1 -1
- package/sprites.d.ts +5 -3
- package/sprites.js +24 -23
- package/sprites.js.map +1 -1
- package/state.d.ts +25 -13
- package/state.js +13 -10
- package/state.js.map +1 -1
- package/text/_gpu/text-instances.js +1 -1
- package/text/_gpu/text-instances.js.map +1 -1
- package/texture-3d.d.ts +2 -1
- package/texture-3d.js +7 -2
- package/texture-3d.js.map +1 -1
- package/texture-source.d.ts +4 -2
- package/texture-source.js +3 -2
- package/texture-source.js.map +1 -1
- package/texture.d.ts +18 -8
- package/texture.js +61 -35
- package/texture.js.map +1 -1
- package/web-family.d.ts +2 -1
- package/web-family.js.map +1 -1
package/state.d.ts
CHANGED
|
@@ -28,22 +28,29 @@
|
|
|
28
28
|
*/
|
|
29
29
|
export interface GLState {
|
|
30
30
|
currentProgram: WebGLProgram | null;
|
|
31
|
+
/** Active texture unit, or -1 when unknown. */
|
|
31
32
|
activeTextureUnit: number;
|
|
32
|
-
/** Per-unit TEXTURE_2D binding;
|
|
33
|
-
|
|
33
|
+
/** Per-unit TEXTURE_2D binding; undefined = unknown, null = confirmed unbound.
|
|
34
|
+
* Length === caps.maxTextureUnits. */
|
|
35
|
+
boundTextures: (WebGLTexture | null | undefined)[];
|
|
34
36
|
boundArrayBuffer: WebGLBuffer | null;
|
|
35
37
|
boundElementBuffer: WebGLBuffer | null;
|
|
36
|
-
|
|
38
|
+
/** Bound VAO; `null` = the default VAO, `undefined` = unknown (fresh or wiped
|
|
39
|
+
* cache, context loss), so the next VAO bind is always issued. */
|
|
40
|
+
boundVao: WebGLVertexArrayObject | null | undefined;
|
|
37
41
|
viewportX: number;
|
|
38
42
|
viewportY: number;
|
|
39
43
|
viewportW: number;
|
|
40
44
|
viewportH: number;
|
|
41
45
|
/**
|
|
42
|
-
* Currently-bound draw framebuffer,
|
|
43
|
-
* framebuffer
|
|
44
|
-
*
|
|
46
|
+
* Currently-bound draw framebuffer, `null` for the default (canvas)
|
|
47
|
+
* framebuffer, or `undefined` when unknown (fresh or wiped cache — the
|
|
48
|
+
* context may be shared with a host that left its own framebuffer bound).
|
|
49
|
+
* Owned by the render-target module's `bindRenderTarget` (binding `null`
|
|
50
|
+
* returns to the canvas). Reset to `undefined` by `wipeGLStateCache` and on
|
|
51
|
+
* context loss.
|
|
45
52
|
*/
|
|
46
|
-
boundFramebuffer: WebGLFramebuffer | null;
|
|
53
|
+
boundFramebuffer: WebGLFramebuffer | null | undefined;
|
|
47
54
|
/**
|
|
48
55
|
* Deferred render-state (blend / depth / cull / stencil / color-mask) packed
|
|
49
56
|
* into a flat `Float64Array(46)`. Slots `0..20` (indexed by the `RS_*`
|
|
@@ -95,12 +102,14 @@ export interface GLState {
|
|
|
95
102
|
/** Cached `gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL)`, or `-1` unset. */
|
|
96
103
|
unpackPremultiplyAlpha: number;
|
|
97
104
|
/**
|
|
98
|
-
* Per-location vertex-attribute
|
|
105
|
+
* Per-location vertex-attribute bindings for the DEFAULT (null) VAO —
|
|
99
106
|
* the mesh / instancing path (mirrors Babylon's `_vertexAttribArraysEnabled`).
|
|
100
|
-
*
|
|
101
|
-
*
|
|
107
|
+
* A buffer handle means enabled with that pointer; true means enabled with
|
|
108
|
+
* an invalidated pointer; null means confirmed disabled; undefined means
|
|
109
|
+
* disabled — or unknown while `_unknownAttribs` is set. Index is the
|
|
110
|
+
* attribute location. Cleared on context loss or cache wipe.
|
|
102
111
|
*/
|
|
103
|
-
enabledAttribs:
|
|
112
|
+
enabledAttribs: (WebGLBuffer | true | null)[];
|
|
104
113
|
/**
|
|
105
114
|
* Attribute locations currently configured with a non-default vertex divisor
|
|
106
115
|
* (instanced attributes), mirroring Babylon's `_currentInstanceLocations`.
|
|
@@ -113,7 +122,8 @@ export interface GLState {
|
|
|
113
122
|
quadIbo: WebGLBuffer | null;
|
|
114
123
|
quadVao: WebGLVertexArrayObject | null;
|
|
115
124
|
}
|
|
116
|
-
/** Allocate a fresh
|
|
125
|
+
/** Allocate a fresh GLState with unknown texture, VAO and framebuffer bindings
|
|
126
|
+
* (the canvas context may already be in use by a host), sized for `maxTextureUnits`. */
|
|
117
127
|
export declare function createGLState(maxTextureUnits: number): GLState;
|
|
118
128
|
/** Reset only the cached "current GL state" — every binding (program / buffers /
|
|
119
129
|
* textures / VAO / framebuffer) and render-state (blend / depth / stencil /
|
|
@@ -121,7 +131,9 @@ export declare function createGLState(maxTextureUnits: number): GLState;
|
|
|
121
131
|
* deferred render-state array (BOTH the actual applied values and the DESIRED
|
|
122
132
|
* twins) plus `statesDirty` — to its unset sentinel, WITHOUT discarding owned
|
|
123
133
|
* GPU resources (the shared quad). After this, the next setter in each category
|
|
124
|
-
* is re-issued rather than elided.
|
|
134
|
+
* is re-issued rather than elided. The VAO and framebuffer bindings become
|
|
135
|
+
* `undefined` (unknown), NOT `null`: `null` is a real binding (default VAO /
|
|
136
|
+
* canvas) and would elide the rebind while the host's object is still bound.
|
|
125
137
|
*
|
|
126
138
|
* Used by `resetGLState` (context-lost) and by `wipeGLStateCache` (a host that
|
|
127
139
|
* shares the GL context calling in after mutating raw `gl.*` state). The shared
|
package/state.js
CHANGED
|
@@ -74,20 +74,21 @@ function resetRenderStateArray(rs) {
|
|
|
74
74
|
rs.fill(0);
|
|
75
75
|
applyRenderStateSentinels(rs);
|
|
76
76
|
}
|
|
77
|
-
/** Allocate a fresh
|
|
77
|
+
/** Allocate a fresh GLState with unknown texture, VAO and framebuffer bindings
|
|
78
|
+
* (the canvas context may already be in use by a host), sized for `maxTextureUnits`. */
|
|
78
79
|
export function createGLState(maxTextureUnits) {
|
|
79
80
|
return {
|
|
80
81
|
currentProgram: null,
|
|
81
|
-
activeTextureUnit:
|
|
82
|
-
boundTextures: new Array(maxTextureUnits)
|
|
82
|
+
activeTextureUnit: -1,
|
|
83
|
+
boundTextures: new Array(maxTextureUnits),
|
|
83
84
|
boundArrayBuffer: null,
|
|
84
85
|
boundElementBuffer: null,
|
|
85
|
-
boundVao:
|
|
86
|
+
boundVao: undefined,
|
|
86
87
|
viewportX: 0,
|
|
87
88
|
viewportY: 0,
|
|
88
89
|
viewportW: 0,
|
|
89
90
|
viewportH: 0,
|
|
90
|
-
boundFramebuffer:
|
|
91
|
+
boundFramebuffer: undefined,
|
|
91
92
|
rs: createRenderStateArray(),
|
|
92
93
|
statesDirty: false,
|
|
93
94
|
scissorEnabled: -1,
|
|
@@ -111,7 +112,9 @@ export function createGLState(maxTextureUnits) {
|
|
|
111
112
|
* deferred render-state array (BOTH the actual applied values and the DESIRED
|
|
112
113
|
* twins) plus `statesDirty` — to its unset sentinel, WITHOUT discarding owned
|
|
113
114
|
* GPU resources (the shared quad). After this, the next setter in each category
|
|
114
|
-
* is re-issued rather than elided.
|
|
115
|
+
* is re-issued rather than elided. The VAO and framebuffer bindings become
|
|
116
|
+
* `undefined` (unknown), NOT `null`: `null` is a real binding (default VAO /
|
|
117
|
+
* canvas) and would elide the rebind while the host's object is still bound.
|
|
115
118
|
*
|
|
116
119
|
* Used by `resetGLState` (context-lost) and by `wipeGLStateCache` (a host that
|
|
117
120
|
* shares the GL context calling in after mutating raw `gl.*` state). The shared
|
|
@@ -119,16 +122,16 @@ export function createGLState(maxTextureUnits) {
|
|
|
119
122
|
* here to avoid leaking + needlessly rebuilding them every render scope. */
|
|
120
123
|
export function resetGLStateCache(state) {
|
|
121
124
|
state.currentProgram = null;
|
|
122
|
-
state.activeTextureUnit =
|
|
123
|
-
state.boundTextures.fill(
|
|
125
|
+
state.activeTextureUnit = -1;
|
|
126
|
+
state.boundTextures.fill(undefined);
|
|
124
127
|
state.boundArrayBuffer = null;
|
|
125
128
|
state.boundElementBuffer = null;
|
|
126
|
-
state.boundVao =
|
|
129
|
+
state.boundVao = undefined;
|
|
127
130
|
state.viewportX = 0;
|
|
128
131
|
state.viewportY = 0;
|
|
129
132
|
state.viewportW = 0;
|
|
130
133
|
state.viewportH = 0;
|
|
131
|
-
state.boundFramebuffer =
|
|
134
|
+
state.boundFramebuffer = undefined;
|
|
132
135
|
resetRenderStateArray(state.rs);
|
|
133
136
|
state.statesDirty = false;
|
|
134
137
|
state.scissorEnabled = -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,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AAK,CAAC,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.quadV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BAAgB,GAAG,IAAI,CAAC;IAC9B,KAAK,CAAC,kBAAkB,GAAG,IAAI,CAAC;IAChC,KAAK,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC3B,KAAK,CAAC,SAAS,GAAG,CAAC,CAAC;IACpB,KAAK,CAAC,SAAS,GAAG,CAAC,CAAC;IACpB,KAAK,CAAC,SAAS,GAAG,CAAC,CAAC;IACpB,KAAK,CAAC,SAAS,GAAG,CAAC,CAAC;IACpB,KAAK,CAAC,gBAAgB,GAAG,SAAS,CAAC;IACnC,qBAAqB,CAAC,KAAK,CAAC,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 /** Active texture unit, or -1 when unknown. */\n activeTextureUnit: number;\n /** Default-VAO pointer format/stride/offset/divisor quadruples\n * (`MAX_VERTEX_ATTRIBS * 4`), allocated once on first bindAttributesCached.\n * Buffer identity and enable state live together in enabledAttribs. @internal */\n _attributePointers?: Float64Array;\n /** Per-unit TEXTURE_2D binding; undefined = unknown, null = confirmed unbound.\n * Length === caps.maxTextureUnits. */\n boundTextures: (WebGLTexture | null | undefined)[];\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 /** Bound VAO; `null` = the default VAO, `undefined` = unknown (fresh or wiped\n * cache, context loss), so the next VAO bind is always issued. */\n boundVao: WebGLVertexArrayObject | null | undefined;\n viewportX: number;\n viewportY: number;\n viewportW: number;\n viewportH: number;\n /**\n * Currently-bound draw framebuffer, `null` for the default (canvas)\n * framebuffer, or `undefined` when unknown (fresh or wiped cache — the\n * context may be shared with a host that left its own framebuffer bound).\n * Owned by the render-target module's `bindRenderTarget` (binding `null`\n * returns to the canvas). Reset to `undefined` by `wipeGLStateCache` and on\n * context loss.\n */\n boundFramebuffer: WebGLFramebuffer | null | undefined;\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 bindings for the DEFAULT (null) VAO —\n * the mesh / instancing path (mirrors Babylon's `_vertexAttribArraysEnabled`).\n * A buffer handle means enabled with that pointer; true means enabled with\n * an invalidated pointer; null means confirmed disabled; undefined means\n * disabled — or unknown while `_unknownAttribs` is set. Index is the\n * attribute location. Cleared on context loss or cache wipe.\n */\n enabledAttribs: (WebGLBuffer | true | null)[];\n /** Set by a plain (non-brute-force) cache wipe: unrecorded default-VAO\n * attribute slots may have been enabled by the host, so disables must not\n * elide them until `unbindAllAttributes` normalizes every slot. @internal */\n _unknownAttribs?: 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 GLState with unknown texture, VAO and framebuffer bindings\n * (the canvas context may already be in use by a host), sized for `maxTextureUnits`. */\nexport function createGLState(maxTextureUnits: number): GLState {\n return {\n currentProgram: null,\n activeTextureUnit: -1,\n boundTextures: new Array<WebGLTexture | null | undefined>(maxTextureUnits),\n boundArrayBuffer: null,\n boundElementBuffer: null,\n boundVao: undefined,\n viewportX: 0,\n viewportY: 0,\n viewportW: 0,\n viewportH: 0,\n boundFramebuffer: undefined,\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. The VAO and framebuffer bindings become\n * `undefined` (unknown), NOT `null`: `null` is a real binding (default VAO /\n * canvas) and would elide the rebind while the host's object is still bound.\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 = -1;\n state.boundTextures.fill(undefined);\n state.boundArrayBuffer = null;\n state.boundElementBuffer = null;\n state.boundVao = undefined;\n state.viewportX = 0;\n state.viewportY = 0;\n state.viewportW = 0;\n state.viewportH = 0;\n state.boundFramebuffer = undefined;\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"]}
|
|
@@ -26,7 +26,7 @@ export function ensureTextDataGpu(engine, data) {
|
|
|
26
26
|
throw error;
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
else if (gpu._instanceBuf.
|
|
29
|
+
else if (gpu._instanceBuf.byteLength !== data._instances.byteLength) {
|
|
30
30
|
// Retain the live CPU pool, including partial edits, for context restoration.
|
|
31
31
|
const buffer = createVertexBuffer(engine, data._instances, true);
|
|
32
32
|
disposeBuffer(gpu._instanceBuf);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text-instances.js","sourceRoot":"","sources":["../../../src/text/_gpu/text-instances.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAEtF,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAC/F,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAElG,uFAAuF;AACvF,MAAM,UAAU,iBAAiB,CAAC,MAAuB,EAAE,IAAgB;IACvE,IAAI,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC;IACpB,IAAI,GAAG,IAAI,GAAG,CAAC,OAAO,KAAK,MAAM,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,oGAAoG,CAAC,CAAC;IAC1H,CAAC;IACD,IAAI,CAAC,GAAG,EAAE,CAAC;QACP,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,GAAG,iBAAiB,CAAC,CAAC;QAC7E,MAAM,MAAM,GAAG,kBAAkB,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;QACjE,IAAI,CAAC;YACD,GAAG,GAAG;gBACF,OAAO,EAAE,MAAM;gBACf,YAAY,EAAE,MAAM;gBACpB,SAAS,EAAE,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,CAAC,MAAM,GAAG,CAAC,WAAW,GAAG,CAAC,CAAC,CAAC;gBAC5F,YAAY,EAAE,MAAM;gBACpB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,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,CAAC,WAAW,GAAG,mBAAmB,CAAC,CAAC;QACzF,CAAC;aAAM,CAAC;YACJ,kBAAkB,CAAC,GAAG,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;QAC1D,CAAC;QACD,GAAG,CAAC,gBAAgB,GAAG,IAAI,CAAC,QAAQ,CAAC;IACzC,CAAC;IACD,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;IACtC,IAAI,GAAG,CAAC,qBAAqB,KAAK,IAAI,CAAC,aAAa,EAAE,CAAC;QACnD,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,GAAG,iBAAiB,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC;QAC/F,iBAAiB,CAAC,MAAM,EAAE,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,CAAC,MAAM,GAAG,CAAC,WAAW,GAAG,CAAC,CAAC,CAAC,CAAC;QACjG,GAAG,CAAC,YAAY,GAAG,MAAM,CAAC;QAC1B,GAAG,CAAC,qBAAqB,GAAG,IAAI,CAAC,aAAa,CAAC;IACnD,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC","sourcesContent":["import type { GLEngineContext } from \"../../context.js\";\nimport { createVertexBuffer, disposeBuffer, updateVertexBuffer } from \"../../mesh.js\";\nimport type { GLTextData, TextDataGpu } from \"../text-data.js\";\nimport { TEXT_INSTANCE_BYTES, TEXT_INSTANCE_FLOATS, TEXT_STYLE_FLOATS } from \"../text-data.js\";\nimport { createTextTexture, packTable, TABLE_WIDTH, uploadTextTexture } from \"./text-textures.js\";\n\n/** @internal One upload per data mutation, even when several layers share the data. */\nexport function ensureTextDataGpu(engine: GLEngineContext, data: GLTextData): TextDataGpu {\n let gpu = data._gpu;\n if (gpu && gpu._engine !== engine) {\n throw new Error(\"lite-gl: text data cannot be drawn on multiple engines. Create separate text data for each engine.\");\n }\n if (!gpu) {\n const pixels = packTable(data._styles, data._styleCount * TEXT_STYLE_FLOATS);\n const buffer = createVertexBuffer(engine, data._instances, true);\n try {\n gpu = {\n _engine: engine,\n _instanceBuf: buffer,\n _styleTex: createTextTexture(engine, pixels, TABLE_WIDTH, pixels.length / (TABLE_WIDTH * 4)),\n _stylePixels: pixels,\n _uploadedVersion: data._version,\n _uploadedStyleVersion: data._styleVersion,\n };\n data._gpu = gpu;\n } catch (error) {\n disposeBuffer(buffer);\n throw error;\n }\n } else if (gpu._instanceBuf.byteLength !== data._instances.byteLength) {\n // Retain the live CPU pool, including partial edits, for context restoration.\n const buffer = createVertexBuffer(engine, data._instances, true);\n disposeBuffer(gpu._instanceBuf);\n gpu._instanceBuf = buffer;\n gpu._uploadedVersion = data._version;\n } else if (gpu._uploadedVersion !== data._version) {\n if (data._dirtyEnd > data._dirtyStart) {\n const start = data._dirtyStart * TEXT_INSTANCE_FLOATS;\n const end = data._dirtyEnd * TEXT_INSTANCE_FLOATS;\n const source = start === 0 && end === data._instances.length ? data._instances : data._instances.subarray(start, end);\n updateVertexBuffer(gpu._instanceBuf, source, data._dirtyStart * TEXT_INSTANCE_BYTES);\n } else {\n updateVertexBuffer(gpu._instanceBuf, data._instances);\n }\n gpu._uploadedVersion = data._version;\n }\n data._dirtyStart = data._dirtyEnd = 0;\n if (gpu._uploadedStyleVersion !== data._styleVersion) {\n const pixels = packTable(data._styles, data._styleCount * TEXT_STYLE_FLOATS, gpu._stylePixels);\n uploadTextTexture(engine, gpu._styleTex, pixels, TABLE_WIDTH, pixels.length / (TABLE_WIDTH * 4));\n gpu._stylePixels = pixels;\n gpu._uploadedStyleVersion = data._styleVersion;\n }\n return gpu;\n}\n"]}
|
package/texture-3d.d.ts
CHANGED
|
@@ -31,7 +31,8 @@ export declare function setEffectTexture3D(effect: GLEffect, samplerName: string
|
|
|
31
31
|
* Upload a tightly packed RGBA8 volume to a native WebGL2 3D texture. Useful
|
|
32
32
|
* for color-grading LUTs without flattening the cube into a 2D atlas. The
|
|
33
33
|
* default linear, clamp-to-edge sampling interpolates all three axes in
|
|
34
|
-
* hardware.
|
|
34
|
+
* hardware. Unless the engine is host-owned (`createHostOwnedGLEngine`), the source
|
|
35
|
+
* is retained and must remain unchanged for context-restore replay;
|
|
35
36
|
* release the texture with `disposeTexture3D` when done.
|
|
36
37
|
*
|
|
37
38
|
* @param engine - WebGL2 engine.
|
package/texture-3d.js
CHANGED
|
@@ -78,7 +78,8 @@ export function setEffectTexture3D(effect, samplerName, tex) {
|
|
|
78
78
|
* Upload a tightly packed RGBA8 volume to a native WebGL2 3D texture. Useful
|
|
79
79
|
* for color-grading LUTs without flattening the cube into a 2D atlas. The
|
|
80
80
|
* default linear, clamp-to-edge sampling interpolates all three axes in
|
|
81
|
-
* hardware.
|
|
81
|
+
* hardware. Unless the engine is host-owned (`createHostOwnedGLEngine`), the source
|
|
82
|
+
* is retained and must remain unchanged for context-restore replay;
|
|
82
83
|
* release the texture with `disposeTexture3D` when done.
|
|
83
84
|
*
|
|
84
85
|
* @param engine - WebGL2 engine.
|
|
@@ -111,12 +112,13 @@ export function createTexture3DFromPixels(engine, data, width, height, depth, op
|
|
|
111
112
|
if (handle === null) {
|
|
112
113
|
throw new Error("lite-gl: gl.createTexture returned null");
|
|
113
114
|
}
|
|
115
|
+
let pixels = data;
|
|
114
116
|
const upload = (target) => {
|
|
115
117
|
const g = target.gl;
|
|
116
118
|
setUnpackState(target, false, false, 4);
|
|
117
119
|
setUnpack3DLayout(target);
|
|
118
120
|
bindTexture3DRaw(target, 0, tex.handle, true);
|
|
119
|
-
g.texImage3D(g.TEXTURE_3D, 0, format, width, height, depth, 0, g.RGBA, g.UNSIGNED_BYTE,
|
|
121
|
+
g.texImage3D(g.TEXTURE_3D, 0, format, width, height, depth, 0, g.RGBA, g.UNSIGNED_BYTE, pixels);
|
|
120
122
|
if (tex._hasMipMaps) {
|
|
121
123
|
g.generateMipmap(g.TEXTURE_3D);
|
|
122
124
|
}
|
|
@@ -150,6 +152,9 @@ export function createTexture3DFromPixels(engine, data, width, height, depth, op
|
|
|
150
152
|
_wasReady: true,
|
|
151
153
|
};
|
|
152
154
|
upload(engine);
|
|
155
|
+
if (engine._noRestore) {
|
|
156
|
+
pixels = null;
|
|
157
|
+
}
|
|
153
158
|
initializeParameters(engine);
|
|
154
159
|
engine._textures.add(tex);
|
|
155
160
|
return tex;
|
package/texture-3d.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"texture-3d.js","sourceRoot":"","sources":["../src/texture-3d.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAwB,MAAM,cAAc,CAAC;AAEnE,OAAO,EAAE,cAAc,EAAkB,MAAM,cAAc,CAAC;AAgC9D,SAAS,eAAe,CAAC,MAAuB;IAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5B,IAAI,KAAK,CAAC,gBAAgB,KAAK,SAAS,EAAE,CAAC;QACvC,OAAO,KAAK,CAAC,gBAAgB,CAAC;IAClC,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,KAAK,CAAkC,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACrG,KAAK,CAAC,gBAAgB,GAAG,KAAK,CAAC;IAC/B,MAAM,UAAU,GAAG,GAAS,EAAE;QAC1B,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACtB,KAAK,CAAC,oBAAoB,GAAG,KAAK,CAAC;IACvC,CAAC,CAAC;IACF,aAAa,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAClC,CAAC,MAAM,CAAC,uBAAuB,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACzD,OAAO,KAAK,CAAC;AACjB,CAAC;AAED,SAAS,gBAAgB,CAAC,MAAuB,EAAE,IAAY,EAAE,MAA2B,EAAE,SAAkB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AAC;QACtB,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC;YAC7C,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,MAAM,EAAE,CAAC;gBAC7B,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;YACvB,CAAC;QACL,CAAC;IACL,CAAC;AACL,CAAC","sourcesContent":["import { onContextLost, type GLEngineContext } from \"./context.js\";\nimport type { GLEffect } from \"./effect.js\";\nimport { setUnpackState, type GLTexture } from \"./texture.js\";\n\n/** Sampling and format overrides for {@link createTexture3DFromPixels}. */\nexport interface GLPixelsTexture3DOptions {\n /** Wrap mode on all three axes. Defaults to `gl.CLAMP_TO_EDGE`. */\n addressMode?: GLenum;\n /** Minification and magnification filter. Defaults to `gl.LINEAR` (trilinear sampling without mipmaps). */\n filter?: GLenum;\n /** Decode sRGB texels on sampling. Defaults to false (`gl.RGBA8`). */\n srgb?: boolean;\n}\n\n/** A managed `gl.TEXTURE_3D` handle, usable with `setEffectTexture3D` and `sampler3D`. */\nexport interface GLTexture3D extends Omit<GLTexture, \"target\"> {\n /** This target distinguishes 3D volumes from textures accepted by the 2D APIs. */\n readonly target: WebGL2RenderingContext[\"TEXTURE_3D\"];\n /** Number of slices in the volume. */\n depth: number;\n /** @internal */\n _minFilter: GLenum;\n /** @internal */\n _magFilter: GLenum;\n /** @internal */\n _wrapS: GLenum;\n /** @internal */\n _wrapT: GLenum;\n /** @internal */\n _wrapR: GLenum;\n /** @internal */\n _hasMipMaps: boolean;\n}\n\nfunction boundTextures3D(engine: GLEngineContext): (WebGLTexture | null | undefined)[] {\n const state = engine._state;\n if (state._boundTextures3D !== undefined) {\n return state._boundTextures3D;\n }\n const bound = new Array<WebGLTexture | null | undefined>(state.boundTextures.length).fill(undefined);\n state._boundTextures3D = bound;\n const invalidate = (): void => {\n bound.fill(undefined);\n state._unpack3DLayoutKnown = false;\n };\n onContextLost(engine, invalidate);\n (engine._stateCacheInvalidators ??= []).push(invalidate);\n return bound;\n}\n\nfunction bindTexture3DRaw(engine: GLEngineContext, unit: number, handle: WebGLTexture | null, forUpload: boolean): void {\n const state = engine._state;\n const bound = boundTextures3D(engine);\n if (bound[unit] === handle && !forUpload) {\n return;\n }\n const gl = engine.gl;\n if (state.activeTextureUnit !== unit || bound[unit] === undefined) {\n gl.activeTexture(gl.TEXTURE0 + unit);\n state.activeTextureUnit = unit;\n }\n if (bound[unit] !== handle) {\n gl.bindTexture(gl.TEXTURE_3D, handle);\n bound[unit] = handle;\n }\n}\n\nfunction setUnpack3DLayout(engine: GLEngineContext): void {\n if (engine._state._unpack3DLayoutKnown) {\n return;\n }\n const gl = engine.gl;\n gl.pixelStorei(gl.UNPACK_ROW_LENGTH, 0);\n gl.pixelStorei(gl.UNPACK_IMAGE_HEIGHT, 0);\n gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, 0);\n gl.pixelStorei(gl.UNPACK_SKIP_ROWS, 0);\n gl.pixelStorei(gl.UNPACK_SKIP_IMAGES, 0);\n engine._state._unpack3DLayoutKnown = true;\n}\n\n/** Bind a 3D texture to one sampler unit; repeated binds are elided. Use\n * {@link unbindTexture3D} to clear a unit. */\nexport function bindTexture3D(tex: GLTexture3D, unit: number): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed) {\n return;\n }\n bindTexture3DRaw(engine, unit, tex.handle, false);\n}\n\n/** Unbind only `TEXTURE_3D` on sampler `unit` (cached). No-op on a lost/disposed context. */\nexport function unbindTexture3D(engine: GLEngineContext, unit: number): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n bindTexture3DRaw(engine, unit, null, false);\n}\n\n/**\n * Bind a volume to an effect's pre-assigned `sampler3D` unit without reissuing\n * `uniform1i`. Pass `null` to unbind only the 3D target on that unit.\n */\nexport function setEffectTexture3D(effect: GLEffect, samplerName: string, tex: GLTexture3D | null): void {\n const engine = effect._engine;\n if (engine._isLost || engine._disposed || !effect.isReady) {\n return;\n }\n const unit = effect.samplerUnits[samplerName];\n if (unit === undefined || tex?._disposed) {\n return;\n }\n bindTexture3DRaw(engine, unit, tex === null ? null : tex.handle, false);\n}\n\n/**\n * Upload a tightly packed RGBA8 volume to a native WebGL2 3D texture. Useful\n * for color-grading LUTs without flattening the cube into a 2D atlas. The\n * default linear, clamp-to-edge sampling interpolates all three axes in\n * hardware. The source must remain unchanged for context-restore replay;\n * release the texture with `disposeTexture3D` when done.\n *\n * @param engine - WebGL2 engine.\n * @param data - `width * height * depth * 4` RGBA bytes; x fastest, then y, then z.\n * @param width - X dimension, a positive integer at most `MAX_3D_TEXTURE_SIZE`.\n * @param height - Y dimension, subject to the same limit.\n * @param depth - Z dimension, subject to the same limit.\n * @param options - Filter, wrap, and sRGB overrides.\n */\nexport function createTexture3DFromPixels(\n engine: GLEngineContext,\n data: Uint8Array,\n width: number,\n height: number,\n depth: number,\n options: GLPixelsTexture3DOptions = {}\n): GLTexture3D {\n const gl = engine.gl;\n const maxSize = gl.getParameter(gl.MAX_3D_TEXTURE_SIZE) as number;\n if (![width, height, depth].every((size) => Number.isSafeInteger(size) && size >= 1 && size <= maxSize)) {\n throw new Error(`lite-gl: 3D texture dimensions must be positive integers <= ${maxSize} (got ${width}x${height}x${depth})`);\n }\n const expected = width * height * depth * 4;\n if (data.length < expected) {\n throw new Error(`lite-gl: 3D texture data too short: need ${expected} RGBA bytes, got ${data.length}`);\n }\n const format = options.srgb ? gl.SRGB8_ALPHA8 : gl.RGBA8;\n const filter = options.filter ?? gl.LINEAR;\n const address = options.addressMode ?? gl.CLAMP_TO_EDGE;\n if (filter !== gl.NEAREST && filter !== gl.LINEAR) {\n throw new Error(\"lite-gl: 3D texture filter must be gl.NEAREST or gl.LINEAR\");\n }\n if (address !== gl.CLAMP_TO_EDGE && address !== gl.REPEAT && address !== gl.MIRRORED_REPEAT) {\n throw new Error(\"lite-gl: 3D texture addressMode must be gl.CLAMP_TO_EDGE, gl.REPEAT or gl.MIRRORED_REPEAT\");\n }\n const handle = gl.createTexture();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null\");\n }\n\n const upload = (target: GLEngineContext): void => {\n const g = target.gl;\n setUnpackState(target, false, false, 4);\n setUnpack3DLayout(target);\n bindTexture3DRaw(target, 0, tex.handle, true);\n g.texImage3D(g.TEXTURE_3D, 0, format, width, height, depth, 0, g.RGBA, g.UNSIGNED_BYTE, data);\n if (tex._hasMipMaps) {\n g.generateMipmap(g.TEXTURE_3D);\n }\n };\n const initializeParameters = (target: GLEngineContext): void => {\n const g = target.gl;\n g.texParameteri(g.TEXTURE_3D, g.TEXTURE_MIN_FILTER, tex._minFilter);\n g.texParameteri(g.TEXTURE_3D, g.TEXTURE_MAG_FILTER, tex._magFilter);\n g.texParameteri(g.TEXTURE_3D, g.TEXTURE_WRAP_S, tex._wrapS);\n g.texParameteri(g.TEXTURE_3D, g.TEXTURE_WRAP_T, tex._wrapT);\n g.texParameteri(g.TEXTURE_3D, g.TEXTURE_WRAP_R, tex._wrapR);\n };\n const tex: GLTexture3D = {\n handle,\n target: gl.TEXTURE_3D,\n width,\n height,\n depth,\n isReady: true,\n _minFilter: filter,\n _magFilter: filter,\n _wrapS: address,\n _wrapT: address,\n _wrapR: address,\n _hasMipMaps: false,\n _engine: engine,\n _disposed: false,\n _refCount: 1,\n _upload: upload,\n _initializeParameters: initializeParameters,\n _wasReady: true,\n };\n upload(engine);\n initializeParameters(engine);\n engine._textures.add(tex);\n return tex;\n}\n\n/** Generate a full 3D mip chain for an existing volume and replay it on context restoration. No-op after loss or disposal. */\nexport function generateTexture3DMipMaps(tex: GLTexture3D): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed) {\n return;\n }\n bindTexture3DRaw(engine, 0, tex.handle, true);\n engine.gl.generateMipmap(engine.gl.TEXTURE_3D);\n tex._hasMipMaps = true;\n}\n\n/** Update changed 3D min/mag filters and retain them across context restoration. Unchanged values are elided. */\nexport function updateTexture3DSamplingMode(tex: GLTexture3D, minFilter: GLenum, magFilter: GLenum): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed || (tex._minFilter === minFilter && tex._magFilter === magFilter)) {\n return;\n }\n const gl = engine.gl;\n if (\n minFilter !== gl.NEAREST &&\n minFilter !== gl.LINEAR &&\n minFilter !== gl.NEAREST_MIPMAP_NEAREST &&\n minFilter !== gl.LINEAR_MIPMAP_NEAREST &&\n minFilter !== gl.NEAREST_MIPMAP_LINEAR &&\n minFilter !== gl.LINEAR_MIPMAP_LINEAR\n ) {\n throw new Error(\"lite-gl: invalid 3D texture minification filter\");\n }\n if (magFilter !== gl.NEAREST && magFilter !== gl.LINEAR) {\n throw new Error(\"lite-gl: 3D texture magnification filter must be gl.NEAREST or gl.LINEAR\");\n }\n bindTexture3DRaw(engine, 0, tex.handle, true);\n if (tex._minFilter !== minFilter) {\n gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MIN_FILTER, minFilter);\n tex._minFilter = minFilter;\n }\n if (tex._magFilter !== magFilter) {\n gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAG_FILTER, magFilter);\n tex._magFilter = magFilter;\n }\n}\n\nfunction validTexture3DWrap(gl: WebGL2RenderingContext, mode: GLenum): boolean {\n return mode === gl.CLAMP_TO_EDGE || mode === gl.REPEAT || mode === gl.MIRRORED_REPEAT;\n}\n\n/** Update changed S/T/R wrapping and retain it across context restoration. Unchanged values are elided. */\nexport function updateTexture3DWrapMode(tex: GLTexture3D, wrapS: GLenum, wrapT: GLenum, wrapR: GLenum): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed || (tex._wrapS === wrapS && tex._wrapT === wrapT && tex._wrapR === wrapR)) {\n return;\n }\n const gl = engine.gl;\n if (!validTexture3DWrap(gl, wrapS) || !validTexture3DWrap(gl, wrapT) || !validTexture3DWrap(gl, wrapR)) {\n throw new Error(\"lite-gl: 3D texture wrap mode must be gl.CLAMP_TO_EDGE, gl.REPEAT or gl.MIRRORED_REPEAT\");\n }\n bindTexture3DRaw(engine, 0, tex.handle, true);\n if (tex._wrapS !== wrapS) {\n gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_S, wrapS);\n tex._wrapS = wrapS;\n }\n if (tex._wrapT !== wrapT) {\n gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_T, wrapT);\n tex._wrapT = wrapT;\n }\n if (tex._wrapR !== wrapR) {\n gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_R, wrapR);\n tex._wrapR = wrapR;\n }\n}\n\n/** Dispose a 3D texture and invalidate any units that still reference its handle.\n * Deregistration is expected O(1); shared references retain their registry entry\n * until the final release, and repeated disposal is a no-op. */\nexport function disposeTexture3D(tex: GLTexture3D): void {\n const engine = tex._engine;\n if (tex._disposed) {\n return;\n }\n if (tex._refCount > 1) {\n tex._refCount--;\n return;\n }\n tex._disposed = true;\n engine._textures.delete(tex);\n if (!engine._isLost && !engine._disposed) {\n engine.gl.deleteTexture(tex.handle);\n }\n const bound = engine._state._boundTextures3D;\n if (bound !== undefined) {\n for (let unit = 0; unit < bound.length; unit++) {\n if (bound[unit] === tex.handle) {\n bound[unit] = null;\n }\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"texture-3d.js","sourceRoot":"","sources":["../src/texture-3d.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAwB,MAAM,cAAc,CAAC;AAEnE,OAAO,EAAE,cAAc,EAAkB,MAAM,cAAc,CAAC;AAgC9D,SAAS,eAAe,CAAC,MAAuB;IAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5B,IAAI,KAAK,CAAC,gBAAgB,KAAK,SAAS,EAAE,CAAC;QACvC,OAAO,KAAK,CAAC,gBAAgB,CAAC;IAClC,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,KAAK,CAAkC,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC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AC7C,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC;IAC3B,IAAI,GAAG,CAAC,SAAS,EAAE,CAAC;QAChB,OAAO;IACX,CAAC;IACD,IAAI,GAAG,CAAC,SAAS,GAAG,CAAC,EAAE,CAAC;QACpB,GAAG,CAAC,SAAS,EAAE,CAAC;QAChB,OAAO;IACX,CAAC;IACD,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC;IACrB,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC7B,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;QACvC,MAAM,CAAC,EAAE,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACxC,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC;IAC7C,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACtB,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC;YAC7C,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,MAAM,EAAE,CAAC;gBAC7B,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;YACvB,CAAC;QACL,CAAC;IACL,CAAC;AACL,CAAC","sourcesContent":["import { onContextLost, type GLEngineContext } from \"./context.js\";\nimport type { GLEffect } from \"./effect.js\";\nimport { setUnpackState, type GLTexture } from \"./texture.js\";\n\n/** Sampling and format overrides for {@link createTexture3DFromPixels}. */\nexport interface GLPixelsTexture3DOptions {\n /** Wrap mode on all three axes. Defaults to `gl.CLAMP_TO_EDGE`. */\n addressMode?: GLenum;\n /** Minification and magnification filter. Defaults to `gl.LINEAR` (trilinear sampling without mipmaps). */\n filter?: GLenum;\n /** Decode sRGB texels on sampling. Defaults to false (`gl.RGBA8`). */\n srgb?: boolean;\n}\n\n/** A managed `gl.TEXTURE_3D` handle, usable with `setEffectTexture3D` and `sampler3D`. */\nexport interface GLTexture3D extends Omit<GLTexture, \"target\"> {\n /** This target distinguishes 3D volumes from textures accepted by the 2D APIs. */\n readonly target: WebGL2RenderingContext[\"TEXTURE_3D\"];\n /** Number of slices in the volume. */\n depth: number;\n /** @internal */\n _minFilter: GLenum;\n /** @internal */\n _magFilter: GLenum;\n /** @internal */\n _wrapS: GLenum;\n /** @internal */\n _wrapT: GLenum;\n /** @internal */\n _wrapR: GLenum;\n /** @internal */\n _hasMipMaps: boolean;\n}\n\nfunction boundTextures3D(engine: GLEngineContext): (WebGLTexture | null | undefined)[] {\n const state = engine._state;\n if (state._boundTextures3D !== undefined) {\n return state._boundTextures3D;\n }\n const bound = new Array<WebGLTexture | null | undefined>(state.boundTextures.length).fill(undefined);\n state._boundTextures3D = bound;\n const invalidate = (): void => {\n bound.fill(undefined);\n state._unpack3DLayoutKnown = false;\n };\n onContextLost(engine, invalidate);\n (engine._stateCacheInvalidators ??= []).push(invalidate);\n return bound;\n}\n\nfunction bindTexture3DRaw(engine: GLEngineContext, unit: number, handle: WebGLTexture | null, forUpload: boolean): void {\n const state = engine._state;\n const bound = boundTextures3D(engine);\n if (bound[unit] === handle && !forUpload) {\n return;\n }\n const gl = engine.gl;\n if (state.activeTextureUnit !== unit || bound[unit] === undefined) {\n gl.activeTexture(gl.TEXTURE0 + unit);\n state.activeTextureUnit = unit;\n }\n if (bound[unit] !== handle) {\n gl.bindTexture(gl.TEXTURE_3D, handle);\n bound[unit] = handle;\n }\n}\n\nfunction setUnpack3DLayout(engine: GLEngineContext): void {\n if (engine._state._unpack3DLayoutKnown) {\n return;\n }\n const gl = engine.gl;\n gl.pixelStorei(gl.UNPACK_ROW_LENGTH, 0);\n gl.pixelStorei(gl.UNPACK_IMAGE_HEIGHT, 0);\n gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, 0);\n gl.pixelStorei(gl.UNPACK_SKIP_ROWS, 0);\n gl.pixelStorei(gl.UNPACK_SKIP_IMAGES, 0);\n engine._state._unpack3DLayoutKnown = true;\n}\n\n/** Bind a 3D texture to one sampler unit; repeated binds are elided. Use\n * {@link unbindTexture3D} to clear a unit. */\nexport function bindTexture3D(tex: GLTexture3D, unit: number): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed) {\n return;\n }\n bindTexture3DRaw(engine, unit, tex.handle, false);\n}\n\n/** Unbind only `TEXTURE_3D` on sampler `unit` (cached). No-op on a lost/disposed context. */\nexport function unbindTexture3D(engine: GLEngineContext, unit: number): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n bindTexture3DRaw(engine, unit, null, false);\n}\n\n/**\n * Bind a volume to an effect's pre-assigned `sampler3D` unit without reissuing\n * `uniform1i`. Pass `null` to unbind only the 3D target on that unit.\n */\nexport function setEffectTexture3D(effect: GLEffect, samplerName: string, tex: GLTexture3D | null): void {\n const engine = effect._engine;\n if (engine._isLost || engine._disposed || !effect.isReady) {\n return;\n }\n const unit = effect.samplerUnits[samplerName];\n if (unit === undefined || tex?._disposed) {\n return;\n }\n bindTexture3DRaw(engine, unit, tex === null ? null : tex.handle, false);\n}\n\n/**\n * Upload a tightly packed RGBA8 volume to a native WebGL2 3D texture. Useful\n * for color-grading LUTs without flattening the cube into a 2D atlas. The\n * default linear, clamp-to-edge sampling interpolates all three axes in\n * hardware. Unless the engine is host-owned (`createHostOwnedGLEngine`), the source\n * is retained and must remain unchanged for context-restore replay;\n * release the texture with `disposeTexture3D` when done.\n *\n * @param engine - WebGL2 engine.\n * @param data - `width * height * depth * 4` RGBA bytes; x fastest, then y, then z.\n * @param width - X dimension, a positive integer at most `MAX_3D_TEXTURE_SIZE`.\n * @param height - Y dimension, subject to the same limit.\n * @param depth - Z dimension, subject to the same limit.\n * @param options - Filter, wrap, and sRGB overrides.\n */\nexport function createTexture3DFromPixels(\n engine: GLEngineContext,\n data: Uint8Array,\n width: number,\n height: number,\n depth: number,\n options: GLPixelsTexture3DOptions = {}\n): GLTexture3D {\n const gl = engine.gl;\n const maxSize = gl.getParameter(gl.MAX_3D_TEXTURE_SIZE) as number;\n if (![width, height, depth].every((size) => Number.isSafeInteger(size) && size >= 1 && size <= maxSize)) {\n throw new Error(`lite-gl: 3D texture dimensions must be positive integers <= ${maxSize} (got ${width}x${height}x${depth})`);\n }\n const expected = width * height * depth * 4;\n if (data.length < expected) {\n throw new Error(`lite-gl: 3D texture data too short: need ${expected} RGBA bytes, got ${data.length}`);\n }\n const format = options.srgb ? gl.SRGB8_ALPHA8 : gl.RGBA8;\n const filter = options.filter ?? gl.LINEAR;\n const address = options.addressMode ?? gl.CLAMP_TO_EDGE;\n if (filter !== gl.NEAREST && filter !== gl.LINEAR) {\n throw new Error(\"lite-gl: 3D texture filter must be gl.NEAREST or gl.LINEAR\");\n }\n if (address !== gl.CLAMP_TO_EDGE && address !== gl.REPEAT && address !== gl.MIRRORED_REPEAT) {\n throw new Error(\"lite-gl: 3D texture addressMode must be gl.CLAMP_TO_EDGE, gl.REPEAT or gl.MIRRORED_REPEAT\");\n }\n const handle = gl.createTexture();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null\");\n }\n\n let pixels: Uint8Array | null = data;\n const upload = (target: GLEngineContext): void => {\n const g = target.gl;\n setUnpackState(target, false, false, 4);\n setUnpack3DLayout(target);\n bindTexture3DRaw(target, 0, tex.handle, true);\n g.texImage3D(g.TEXTURE_3D, 0, format, width, height, depth, 0, g.RGBA, g.UNSIGNED_BYTE, pixels);\n if (tex._hasMipMaps) {\n g.generateMipmap(g.TEXTURE_3D);\n }\n };\n const initializeParameters = (target: GLEngineContext): void => {\n const g = target.gl;\n g.texParameteri(g.TEXTURE_3D, g.TEXTURE_MIN_FILTER, tex._minFilter);\n g.texParameteri(g.TEXTURE_3D, g.TEXTURE_MAG_FILTER, tex._magFilter);\n g.texParameteri(g.TEXTURE_3D, g.TEXTURE_WRAP_S, tex._wrapS);\n g.texParameteri(g.TEXTURE_3D, g.TEXTURE_WRAP_T, tex._wrapT);\n g.texParameteri(g.TEXTURE_3D, g.TEXTURE_WRAP_R, tex._wrapR);\n };\n const tex: GLTexture3D = {\n handle,\n target: gl.TEXTURE_3D,\n width,\n height,\n depth,\n isReady: true,\n _minFilter: filter,\n _magFilter: filter,\n _wrapS: address,\n _wrapT: address,\n _wrapR: address,\n _hasMipMaps: false,\n _engine: engine,\n _disposed: false,\n _refCount: 1,\n _upload: upload,\n _initializeParameters: initializeParameters,\n _wasReady: true,\n };\n upload(engine);\n if (engine._noRestore) {\n pixels = null;\n }\n initializeParameters(engine);\n engine._textures.add(tex);\n return tex;\n}\n\n/** Generate a full 3D mip chain for an existing volume and replay it on context restoration. No-op after loss or disposal. */\nexport function generateTexture3DMipMaps(tex: GLTexture3D): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed) {\n return;\n }\n bindTexture3DRaw(engine, 0, tex.handle, true);\n engine.gl.generateMipmap(engine.gl.TEXTURE_3D);\n tex._hasMipMaps = true;\n}\n\n/** Update changed 3D min/mag filters and retain them across context restoration. Unchanged values are elided. */\nexport function updateTexture3DSamplingMode(tex: GLTexture3D, minFilter: GLenum, magFilter: GLenum): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed || (tex._minFilter === minFilter && tex._magFilter === magFilter)) {\n return;\n }\n const gl = engine.gl;\n if (\n minFilter !== gl.NEAREST &&\n minFilter !== gl.LINEAR &&\n minFilter !== gl.NEAREST_MIPMAP_NEAREST &&\n minFilter !== gl.LINEAR_MIPMAP_NEAREST &&\n minFilter !== gl.NEAREST_MIPMAP_LINEAR &&\n minFilter !== gl.LINEAR_MIPMAP_LINEAR\n ) {\n throw new Error(\"lite-gl: invalid 3D texture minification filter\");\n }\n if (magFilter !== gl.NEAREST && magFilter !== gl.LINEAR) {\n throw new Error(\"lite-gl: 3D texture magnification filter must be gl.NEAREST or gl.LINEAR\");\n }\n bindTexture3DRaw(engine, 0, tex.handle, true);\n if (tex._minFilter !== minFilter) {\n gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MIN_FILTER, minFilter);\n tex._minFilter = minFilter;\n }\n if (tex._magFilter !== magFilter) {\n gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAG_FILTER, magFilter);\n tex._magFilter = magFilter;\n }\n}\n\nfunction validTexture3DWrap(gl: WebGL2RenderingContext, mode: GLenum): boolean {\n return mode === gl.CLAMP_TO_EDGE || mode === gl.REPEAT || mode === gl.MIRRORED_REPEAT;\n}\n\n/** Update changed S/T/R wrapping and retain it across context restoration. Unchanged values are elided. */\nexport function updateTexture3DWrapMode(tex: GLTexture3D, wrapS: GLenum, wrapT: GLenum, wrapR: GLenum): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed || (tex._wrapS === wrapS && tex._wrapT === wrapT && tex._wrapR === wrapR)) {\n return;\n }\n const gl = engine.gl;\n if (!validTexture3DWrap(gl, wrapS) || !validTexture3DWrap(gl, wrapT) || !validTexture3DWrap(gl, wrapR)) {\n throw new Error(\"lite-gl: 3D texture wrap mode must be gl.CLAMP_TO_EDGE, gl.REPEAT or gl.MIRRORED_REPEAT\");\n }\n bindTexture3DRaw(engine, 0, tex.handle, true);\n if (tex._wrapS !== wrapS) {\n gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_S, wrapS);\n tex._wrapS = wrapS;\n }\n if (tex._wrapT !== wrapT) {\n gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_T, wrapT);\n tex._wrapT = wrapT;\n }\n if (tex._wrapR !== wrapR) {\n gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_R, wrapR);\n tex._wrapR = wrapR;\n }\n}\n\n/** Dispose a 3D texture and invalidate any units that still reference its handle.\n * Deregistration is expected O(1); shared references retain their registry entry\n * until the final release, and repeated disposal is a no-op. */\nexport function disposeTexture3D(tex: GLTexture3D): void {\n const engine = tex._engine;\n if (tex._disposed) {\n return;\n }\n if (tex._refCount > 1) {\n tex._refCount--;\n return;\n }\n tex._disposed = true;\n engine._textures.delete(tex);\n if (!engine._isLost && !engine._disposed) {\n engine.gl.deleteTexture(tex.handle);\n }\n const bound = engine._state._boundTextures3D;\n if (bound !== undefined) {\n for (let unit = 0; unit < bound.length; unit++) {\n if (bound[unit] === tex.handle) {\n bound[unit] = null;\n }\n }\n }\n}\n"]}
|
package/texture-source.d.ts
CHANGED
|
@@ -3,7 +3,8 @@ import { type GLTexture, type GLTextureOptions } from "./texture.js";
|
|
|
3
3
|
/** Upload and sampling options for {@link createTextureFromSource}. */
|
|
4
4
|
export interface GLTextureSourceOptions extends Omit<GLTextureOptions, "internalFormat"> {
|
|
5
5
|
/**
|
|
6
|
-
* Keep the source for automatic context-restore replay. Default true
|
|
6
|
+
* Keep the source for automatic context-restore replay. Default true;
|
|
7
|
+
* overridden by a host-owned engine (`createHostOwnedGLEngine`).
|
|
7
8
|
* With false, release the reference after upload and restore transparent
|
|
8
9
|
* black at the original size instead; the caller must recreate content
|
|
9
10
|
* (for example from an `onContextRestored` callback).
|
|
@@ -25,7 +26,8 @@ export interface GLTextureSourceOptions extends Omit<GLTextureOptions, "internal
|
|
|
25
26
|
* source reference is dropped after upload and restoration creates a ready,
|
|
26
27
|
* transparent-black texture at the original size; re-upload content from an
|
|
27
28
|
* `onContextRestored` callback. This function never closes a bitmap or video
|
|
28
|
-
* frame.
|
|
29
|
+
* frame. A host-owned engine (`createHostOwnedGLEngine`) always drops the source after
|
|
30
|
+
* upload, regardless of `retainSource`, and does not restore the texture.
|
|
29
31
|
*
|
|
30
32
|
* Restoration replays level 0 and the sampling parameters. When using a
|
|
31
33
|
* mipmapped `minFilter`, call `generateTextureMipMaps` once after creation:
|
package/texture-source.js
CHANGED
|
@@ -14,7 +14,8 @@ import { bindTextureForUpload, disposeTexture, setBoundTextureParams, setUnpackS
|
|
|
14
14
|
* source reference is dropped after upload and restoration creates a ready,
|
|
15
15
|
* transparent-black texture at the original size; re-upload content from an
|
|
16
16
|
* `onContextRestored` callback. This function never closes a bitmap or video
|
|
17
|
-
* frame.
|
|
17
|
+
* frame. A host-owned engine (`createHostOwnedGLEngine`) always drops the source after
|
|
18
|
+
* upload, regardless of `retainSource`, and does not restore the texture.
|
|
18
19
|
*
|
|
19
20
|
* Restoration replays level 0 and the sampling parameters. When using a
|
|
20
21
|
* mipmapped `minFilter`, call `generateTextureMipMaps` once after creation:
|
|
@@ -119,7 +120,7 @@ export function createTextureFromSource(engine, source, options = {}) {
|
|
|
119
120
|
disposeTexture(tex);
|
|
120
121
|
throw error;
|
|
121
122
|
}
|
|
122
|
-
if (options.retainSource === false) {
|
|
123
|
+
if (engine._noRestore || options.retainSource === false) {
|
|
123
124
|
retainedSource = null;
|
|
124
125
|
}
|
|
125
126
|
engine._textures.add(tex);
|
package/texture-source.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"texture-source.js","sourceRoot":"","sources":["../src/texture-source.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,qBAAqB,EAAE,cAAc,EAAyC,MAAM,cAAc,CAAC;AAalJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,MAAM,UAAU,uBAAuB,CAAC,MAAuB,EAAE,MAAsB,EAAE,UAAkC,EAAE;IACzH,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,sEAAsE,CAAC,CAAC;IAC5F,CAAC;IACD,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACnD,MAAM,SAAS,GAAG,OAAO,CAAC,eAAe,IAAI,CAAC,CAAC;IAC/C,IAAI,SAAS,KAAK,CAAC,IAAI,SAAS,KAAK,CAAC,IAAI,SAAS,KAAK,CAAC,IAAI,SAAS,KAAK,CAAC,EAAE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CAAC;IAClC,CAAC;SAAM,CAAC;QACJ,2FAA2F;QAC3F,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC/C,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;QACpE,CAAC;QACD,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QACrB,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IAC3B,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC;IAC3C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,MAAM,GAAG,CAAC,IAAI,KAAK,GAAG,OAAO,IAAI,MAAM,GAAG,OAAO,EAAE,CAAC;QAClI,MAAM,IAAI,KAAK,CAAC,mEAAmE,OAAO,SAAS,KAAK,IAAI,MAAM,GAAG,CAAC,CAAC;IAC3H,CAAC;IACD,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import type { GLEngineContext } from \"./context.js\";\nimport { bindTextureForUpload, disposeTexture, setBoundTextureParams, setUnpackState, type GLTexture, type GLTextureOptions } from \"./texture.js\";\n\n/** Upload and sampling options for {@link createTextureFromSource}. */\nexport interface GLTextureSourceOptions extends Omit<GLTextureOptions, \"internalFormat\"> {\n /**\n * Keep the source for automatic context-restore replay. Default true.\n * With false, release the reference after upload and restore transparent\n * black at the original size instead; the caller must recreate content\n * (for example from an `onContextRestored` callback).\n */\n retainSource?: boolean;\n}\n\n/**\n * Create an immediately ready RGBA 2D texture from already-decoded pixels with\n * one source upload, no placeholder or blank pre-allocation. Sampling defaults\n * to linear filtering and clamp-to-edge wrapping; mipmaps are a separate opt-in\n * via `generateTextureMipMaps`.\n *\n * The source is retained for context restoration by default. Keep it usable\n * while the texture is live; mutable sources replay their current content and\n * intrinsic size. If a retained source has become unusable by restore time\n * (for example a closed `ImageBitmap`), the error is logged, the source is\n * dropped and the texture restores transparent black at its last uploaded size\n * so the rest of the engine still restores. With `retainSource: false`, the\n * source reference is dropped after upload and restoration creates a ready,\n * transparent-black texture at the original size; re-upload content from an\n * `onContextRestored` callback. This function never closes a bitmap or video\n * frame.\n *\n * Restoration replays level 0 and the sampling parameters. When using a\n * mipmapped `minFilter`, call `generateTextureMipMaps` once after creation:\n * the mip intent is retained, so restoration rebuilds the chain too.\n *\n * WebGL ignores unpack flip/premultiplication for `ImageBitmap`; select\n * `imageOrientation` / `premultiplyAlpha` when decoding those sources instead.\n *\n * The texture is immutable from the API's point of view: to change its pixels,\n * dispose it and create a new one. Do not pass it to `updateDynamicTexture`\n * (the new source would be ignored) or `updateHtmlElementTexture` (it would\n * run the context-restore replay on the live texture).\n *\n * Image elements are sized from `naturalWidth` / `naturalHeight`. For SVG\n * images and density-selected (`srcset` / `x` descriptor) images, WebGL may\n * upload a different pixel size, so the reported and restored dimensions can\n * be wrong; convert those to an `ImageBitmap` (`createImageBitmap`) first.\n * Video elements must have a decoded current frame (`readyState >= 2`,\n * `HAVE_CURRENT_DATA`); positive metadata dimensions alone are insufficient.\n *\n * @param engine - The live engine that will own the texture.\n * @param source - An ImageBitmap, ImageData (with an attached buffer), canvas,\n * OffscreenCanvas, decoded raster image, video with a decoded current frame,\n * or VideoFrame with positive intrinsic dimensions.\n * @param options - Unpack and sampling overrides; defaults are invertY false,\n * premultiplyAlpha false, unpackAlignment 4 and retainSource true.\n * @returns A managed texture disposed with {@link disposeTexture}.\n * @throws If the engine is lost/disposed, dimensions exceed the texture limit\n * or are not positive integers, the source is detached or has no decoded\n * current frame, unpack alignment is invalid, or allocation fails.\n */\nexport function createTextureFromSource(engine: GLEngineContext, source: TexImageSource, options: GLTextureSourceOptions = {}): GLTexture {\n if (engine._isLost || engine._disposed) {\n throw new Error(\"lite-gl: cannot create a source texture on a lost or disposed engine\");\n }\n const [width, height] = sourceSize(engine, source);\n const alignment = options.unpackAlignment ?? 4;\n if (alignment !== 1 && alignment !== 2 && alignment !== 4 && alignment !== 8) {\n throw new Error(\"lite-gl: texture unpackAlignment must be 1, 2, 4 or 8\");\n }\n const gl = engine.gl;\n const minFilter = options.minFilter ?? gl.LINEAR;\n const magFilter = options.magFilter ?? gl.LINEAR;\n const wrapS = options.wrapS ?? gl.CLAMP_TO_EDGE;\n const wrapT = options.wrapT ?? gl.CLAMP_TO_EDGE;\n const invertY = options.invertY ?? false;\n const premultiplyAlpha = options.premultiplyAlpha ?? false;\n let retainedSource: TexImageSource | null = source;\n const handle = gl.createTexture();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null\");\n }\n\n const uploadSource = (target: GLEngineContext, src: TexImageSource, w: number, h: number): void => {\n const g = target.gl;\n setUnpackState(target, invertY, premultiplyAlpha, alignment);\n bindTextureForUpload(target, tex.handle);\n g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, g.RGBA, g.UNSIGNED_BYTE, src);\n tex.width = w;\n tex.height = h;\n };\n // Context-restore replay. A retained source that became unusable (closed\n // bitmap, emptied video, tainted image) must not abort the engine-wide\n // restore, so fall back to transparent black at the last uploaded size.\n const restore = (target: GLEngineContext): void => {\n if (retainedSource !== null) {\n try {\n const [w, h] = sourceSize(target, retainedSource);\n uploadSource(target, retainedSource, w, h);\n return;\n } catch (err) {\n console.error(\"lite-gl: retained texture source unusable on restore; restored blank\", err);\n retainedSource = null;\n }\n }\n const g = target.gl;\n setUnpackState(target, invertY, premultiplyAlpha, alignment);\n bindTextureForUpload(target, tex.handle);\n g.texImage2D(g.TEXTURE_2D, 0, g.RGBA8, tex.width, tex.height, 0, g.RGBA, g.UNSIGNED_BYTE, null);\n };\n const initializeParameters = (target: GLEngineContext): void => {\n setBoundTextureParams(target.gl, minFilter, magFilter, wrapS, wrapT);\n };\n const tex: GLTexture = {\n handle,\n target: gl.TEXTURE_2D,\n width,\n height,\n isReady: true,\n _engine: engine,\n _disposed: false,\n _refCount: 1,\n _upload: restore,\n _initializeParameters: initializeParameters,\n _wasReady: true,\n };\n try {\n uploadSource(engine, source, width, height);\n initializeParameters(engine);\n } catch (error) {\n disposeTexture(tex);\n throw error;\n }\n if (options.retainSource === false) {\n retainedSource = null;\n }\n engine._textures.add(tex);\n return tex;\n}\n\n/** Use intrinsic upload dimensions without depending on DOM constructor globals.\n * Also rejects sources WebGL would fail on without throwing (a detached\n * `ImageData` buffer or video without a current frame raises `INVALID_VALUE`), so creation throws and\n * restore takes the blank fallback instead of marking empty storage ready. */\nfunction sourceSize(engine: GLEngineContext, source: TexImageSource): [number, number] {\n let width: number;\n let height: number;\n if (\"videoWidth\" in source) {\n // HAVE_CURRENT_DATA: dimensions alone do not guarantee a decoded frame.\n if (source.readyState < 2) {\n throw new Error(\"lite-gl: source video has no decoded current frame\");\n }\n width = source.videoWidth;\n height = source.videoHeight;\n } else if (\"naturalWidth\" in source) {\n width = source.naturalWidth;\n height = source.naturalHeight;\n } else if (\"displayWidth\" in source) {\n width = source.displayWidth;\n height = source.displayHeight;\n } else {\n // ImageData is never zero-sized, so an empty pixel array means its buffer was transferred.\n if (\"data\" in source && source.data.length === 0) {\n throw new Error(\"lite-gl: source ImageData buffer is detached\");\n }\n width = source.width;\n height = source.height;\n }\n const maxSize = engine.caps.maxTextureSize;\n if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width < 1 || height < 1 || width > maxSize || height > maxSize) {\n throw new Error(`lite-gl: source texture dimensions must be positive integers <= ${maxSize} (got ${width}x${height})`);\n }\n return [width, height];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"texture-source.js","sourceRoot":"","sources":["../src/texture-source.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,qBAAqB,EAAE,cAAc,EAAyC,MAAM,cAAc,CAAC;AAclJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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EngineContext } from \"./context.js\";\nimport { bindTextureForUpload, disposeTexture, setBoundTextureParams, setUnpackState, type GLTexture, type GLTextureOptions } from \"./texture.js\";\n\n/** Upload and sampling options for {@link createTextureFromSource}. */\nexport interface GLTextureSourceOptions extends Omit<GLTextureOptions, \"internalFormat\"> {\n /**\n * Keep the source for automatic context-restore replay. Default true;\n * overridden by a host-owned engine (`createHostOwnedGLEngine`).\n * With false, release the reference after upload and restore transparent\n * black at the original size instead; the caller must recreate content\n * (for example from an `onContextRestored` callback).\n */\n retainSource?: boolean;\n}\n\n/**\n * Create an immediately ready RGBA 2D texture from already-decoded pixels with\n * one source upload, no placeholder or blank pre-allocation. Sampling defaults\n * to linear filtering and clamp-to-edge wrapping; mipmaps are a separate opt-in\n * via `generateTextureMipMaps`.\n *\n * The source is retained for context restoration by default. Keep it usable\n * while the texture is live; mutable sources replay their current content and\n * intrinsic size. If a retained source has become unusable by restore time\n * (for example a closed `ImageBitmap`), the error is logged, the source is\n * dropped and the texture restores transparent black at its last uploaded size\n * so the rest of the engine still restores. With `retainSource: false`, the\n * source reference is dropped after upload and restoration creates a ready,\n * transparent-black texture at the original size; re-upload content from an\n * `onContextRestored` callback. This function never closes a bitmap or video\n * frame. A host-owned engine (`createHostOwnedGLEngine`) always drops the source after\n * upload, regardless of `retainSource`, and does not restore the texture.\n *\n * Restoration replays level 0 and the sampling parameters. When using a\n * mipmapped `minFilter`, call `generateTextureMipMaps` once after creation:\n * the mip intent is retained, so restoration rebuilds the chain too.\n *\n * WebGL ignores unpack flip/premultiplication for `ImageBitmap`; select\n * `imageOrientation` / `premultiplyAlpha` when decoding those sources instead.\n *\n * The texture is immutable from the API's point of view: to change its pixels,\n * dispose it and create a new one. Do not pass it to `updateDynamicTexture`\n * (the new source would be ignored) or `updateHtmlElementTexture` (it would\n * run the context-restore replay on the live texture).\n *\n * Image elements are sized from `naturalWidth` / `naturalHeight`. For SVG\n * images and density-selected (`srcset` / `x` descriptor) images, WebGL may\n * upload a different pixel size, so the reported and restored dimensions can\n * be wrong; convert those to an `ImageBitmap` (`createImageBitmap`) first.\n * Video elements must have a decoded current frame (`readyState >= 2`,\n * `HAVE_CURRENT_DATA`); positive metadata dimensions alone are insufficient.\n *\n * @param engine - The live engine that will own the texture.\n * @param source - An ImageBitmap, ImageData (with an attached buffer), canvas,\n * OffscreenCanvas, decoded raster image, video with a decoded current frame,\n * or VideoFrame with positive intrinsic dimensions.\n * @param options - Unpack and sampling overrides; defaults are invertY false,\n * premultiplyAlpha false, unpackAlignment 4 and retainSource true.\n * @returns A managed texture disposed with {@link disposeTexture}.\n * @throws If the engine is lost/disposed, dimensions exceed the texture limit\n * or are not positive integers, the source is detached or has no decoded\n * current frame, unpack alignment is invalid, or allocation fails.\n */\nexport function createTextureFromSource(engine: GLEngineContext, source: TexImageSource, options: GLTextureSourceOptions = {}): GLTexture {\n if (engine._isLost || engine._disposed) {\n throw new Error(\"lite-gl: cannot create a source texture on a lost or disposed engine\");\n }\n const [width, height] = sourceSize(engine, source);\n const alignment = options.unpackAlignment ?? 4;\n if (alignment !== 1 && alignment !== 2 && alignment !== 4 && alignment !== 8) {\n throw new Error(\"lite-gl: texture unpackAlignment must be 1, 2, 4 or 8\");\n }\n const gl = engine.gl;\n const minFilter = options.minFilter ?? gl.LINEAR;\n const magFilter = options.magFilter ?? gl.LINEAR;\n const wrapS = options.wrapS ?? gl.CLAMP_TO_EDGE;\n const wrapT = options.wrapT ?? gl.CLAMP_TO_EDGE;\n const invertY = options.invertY ?? false;\n const premultiplyAlpha = options.premultiplyAlpha ?? false;\n let retainedSource: TexImageSource | null = source;\n const handle = gl.createTexture();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null\");\n }\n\n const uploadSource = (target: GLEngineContext, src: TexImageSource, w: number, h: number): void => {\n const g = target.gl;\n setUnpackState(target, invertY, premultiplyAlpha, alignment);\n bindTextureForUpload(target, tex.handle);\n g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, g.RGBA, g.UNSIGNED_BYTE, src);\n tex.width = w;\n tex.height = h;\n };\n // Context-restore replay. A retained source that became unusable (closed\n // bitmap, emptied video, tainted image) must not abort the engine-wide\n // restore, so fall back to transparent black at the last uploaded size.\n const restore = (target: GLEngineContext): void => {\n if (retainedSource !== null) {\n try {\n const [w, h] = sourceSize(target, retainedSource);\n uploadSource(target, retainedSource, w, h);\n return;\n } catch (err) {\n console.error(\"lite-gl: retained texture source unusable on restore; restored blank\", err);\n retainedSource = null;\n }\n }\n const g = target.gl;\n setUnpackState(target, invertY, premultiplyAlpha, alignment);\n bindTextureForUpload(target, tex.handle);\n g.texImage2D(g.TEXTURE_2D, 0, g.RGBA8, tex.width, tex.height, 0, g.RGBA, g.UNSIGNED_BYTE, null);\n };\n const initializeParameters = (target: GLEngineContext): void => {\n setBoundTextureParams(target.gl, minFilter, magFilter, wrapS, wrapT);\n };\n const tex: GLTexture = {\n handle,\n target: gl.TEXTURE_2D,\n width,\n height,\n isReady: true,\n _engine: engine,\n _disposed: false,\n _refCount: 1,\n _upload: restore,\n _initializeParameters: initializeParameters,\n _wasReady: true,\n };\n try {\n uploadSource(engine, source, width, height);\n initializeParameters(engine);\n } catch (error) {\n disposeTexture(tex);\n throw error;\n }\n if (engine._noRestore || options.retainSource === false) {\n retainedSource = null;\n }\n engine._textures.add(tex);\n return tex;\n}\n\n/** Use intrinsic upload dimensions without depending on DOM constructor globals.\n * Also rejects sources WebGL would fail on without throwing (a detached\n * `ImageData` buffer or video without a current frame raises `INVALID_VALUE`), so creation throws and\n * restore takes the blank fallback instead of marking empty storage ready. */\nfunction sourceSize(engine: GLEngineContext, source: TexImageSource): [number, number] {\n let width: number;\n let height: number;\n if (\"videoWidth\" in source) {\n // HAVE_CURRENT_DATA: dimensions alone do not guarantee a decoded frame.\n if (source.readyState < 2) {\n throw new Error(\"lite-gl: source video has no decoded current frame\");\n }\n width = source.videoWidth;\n height = source.videoHeight;\n } else if (\"naturalWidth\" in source) {\n width = source.naturalWidth;\n height = source.naturalHeight;\n } else if (\"displayWidth\" in source) {\n width = source.displayWidth;\n height = source.displayHeight;\n } else {\n // ImageData is never zero-sized, so an empty pixel array means its buffer was transferred.\n if (\"data\" in source && source.data.length === 0) {\n throw new Error(\"lite-gl: source ImageData buffer is detached\");\n }\n width = source.width;\n height = source.height;\n }\n const maxSize = engine.caps.maxTextureSize;\n if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width < 1 || height < 1 || width > maxSize || height > maxSize) {\n throw new Error(`lite-gl: source texture dimensions must be positive integers <= ${maxSize} (got ${width}x${height})`);\n }\n return [width, height];\n}\n"]}
|
package/texture.d.ts
CHANGED
|
@@ -141,7 +141,8 @@ export interface GLRawTextureOptions extends GLTextureOptions {
|
|
|
141
141
|
* RGBA8 by default; set {@link GLRawTextureOptions.format} / `type` for other
|
|
142
142
|
* layouts. The pixel data can be replaced later via {@link updateRawTexture};
|
|
143
143
|
* the sampling and wrap can be changed via {@link updateTextureSamplingMode} /
|
|
144
|
-
* {@link updateTextureWrapMode}.
|
|
144
|
+
* {@link updateTextureWrapMode}. Upload data is retained by reference for
|
|
145
|
+
* automatic recovery unless the engine is host-owned (`createHostOwnedGLEngine`).
|
|
145
146
|
*
|
|
146
147
|
* @param engine - The engine.
|
|
147
148
|
* @param data - Initial pixels matching `format` / `type`, or `null` for an
|
|
@@ -267,13 +268,14 @@ export declare function isTextureReady(tex: GLTexture): boolean;
|
|
|
267
268
|
export declare function updateTextureWrapMode(tex: GLTexture, wrapS: GLenum, wrapT: GLenum): void;
|
|
268
269
|
/**
|
|
269
270
|
* Wrap an existing raw `WebGLTexture` (e.g. one created by a host renderer or a
|
|
270
|
-
* previous engine) as a {@link GLTexture}
|
|
271
|
-
* ShapeBuilder's `createTextureGraphicsResourceFromExternalWebGLTexture`.
|
|
271
|
+
* previous engine) as a {@link GLTexture}.
|
|
272
272
|
*
|
|
273
273
|
* The wrapper does NOT own the underlying handle's upload — it is NOT registered
|
|
274
274
|
* for context-restore replay (the external owner is responsible for that) and is
|
|
275
|
-
* marked ready immediately. `disposeTexture`
|
|
276
|
-
*
|
|
275
|
+
* marked ready immediately. By default `disposeTexture` still `gl.deleteTexture`s
|
|
276
|
+
* the handle; pass `ownsHandle = false` to BORROW a handle the host keeps
|
|
277
|
+
* owning (e.g. a texture shared with another renderer), so disposal only
|
|
278
|
+
* releases the wrapper and its cached bindings.
|
|
277
279
|
*
|
|
278
280
|
* @param engine - The engine.
|
|
279
281
|
* @param handle - The external `WebGLTexture`.
|
|
@@ -282,13 +284,19 @@ export declare function updateTextureWrapMode(tex: GLTexture, wrapS: GLenum, wra
|
|
|
282
284
|
* @param options - Optional sampling state to apply once (min/mag/wrapS/wrapT).
|
|
283
285
|
* Upload-only options (`invertY`, `premultiplyAlpha`, `unpackAlignment`,
|
|
284
286
|
* `internalFormat`) do not apply — the handle's pixels are owned externally.
|
|
287
|
+
* @param ownsHandle - When `false`, `disposeTexture` never deletes `handle`. Default `true`.
|
|
285
288
|
* @returns A {@link GLTexture} wrapping the handle.
|
|
286
289
|
*/
|
|
287
|
-
export declare function createTextureFromHandle(engine: GLEngineContext, handle: WebGLTexture, width: number, height: number, options?: GLTextureOptions): GLTexture;
|
|
290
|
+
export declare function createTextureFromHandle(engine: GLEngineContext, handle: WebGLTexture, width: number, height: number, options?: GLTextureOptions, ownsHandle?: boolean): GLTexture;
|
|
288
291
|
/** Asynchronous image upload.The returned texture is immediately usable (1×1
|
|
289
292
|
* transparent placeholder); `isReady` flips true once the image has been
|
|
290
293
|
* decoded and uploaded. The decoded `ImageBitmap` is retained on the texture
|
|
291
|
-
* for offline-safe `webglcontextrestored` replay
|
|
294
|
+
* for offline-safe `webglcontextrestored` replay by default; on a host-owned
|
|
295
|
+
* engine (`createHostOwnedGLEngine`), it is closed/released after the upload,
|
|
296
|
+
* and a decode finishing while that engine is lost closes the bitmap and
|
|
297
|
+
* reports a context-loss error through `onError` instead of `onLoad`, even when
|
|
298
|
+
* the host already disposed the texture (e.g. from `onContextLost`). Otherwise,
|
|
299
|
+
* disposing the texture before its decode finishes cancels the load silently. `invertY` and
|
|
292
300
|
* `premultiplyAlpha` are applied at decode time (`createImageBitmap`'s
|
|
293
301
|
* `imageOrientation` / `premultiplyAlpha`) because the `UNPACK_*` flags are
|
|
294
302
|
* ignored for `ImageBitmap` sources. */
|
|
@@ -306,5 +314,7 @@ export declare function unbindTexture(engine: GLEngineContext, unit: number): vo
|
|
|
306
314
|
* Deregistration is expected O(1), independent of the live texture count.
|
|
307
315
|
* Shared references keep the texture registered until its final release;
|
|
308
316
|
* repeated disposal is a no-op. Unregistered wrappers keep their existing
|
|
309
|
-
* handle-deletion semantics without affecting the live registry
|
|
317
|
+
* handle-deletion semantics without affecting the live registry; borrowed
|
|
318
|
+
* wrappers (`createTextureFromHandle(…, ownsHandle = false)`) never delete
|
|
319
|
+
* their handle. */
|
|
310
320
|
export declare function disposeTexture(tex: GLTexture): void;
|