@babylonjs/lite-gl 1.20.0 → 1.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +58 -19
  2. package/context.js +2 -0
  3. package/context.js.map +1 -1
  4. package/depth-stencil.d.ts +1 -2
  5. package/depth-stencil.js +2 -2
  6. package/depth-stencil.js.map +1 -1
  7. package/dynamic-texture.d.ts +18 -18
  8. package/dynamic-texture.js +28 -24
  9. package/dynamic-texture.js.map +1 -1
  10. package/effect-ready.d.ts +1 -3
  11. package/effect-ready.js +3 -3
  12. package/effect-ready.js.map +1 -1
  13. package/effect-renderer.d.ts +39 -12
  14. package/effect-renderer.js +47 -17
  15. package/effect-renderer.js.map +1 -1
  16. package/effect.d.ts +25 -21
  17. package/effect.js +69 -51
  18. package/effect.js.map +1 -1
  19. package/gpu-frame-time.d.ts +49 -0
  20. package/gpu-frame-time.js +130 -0
  21. package/gpu-frame-time.js.map +1 -0
  22. package/html-texture.d.ts +24 -34
  23. package/html-texture.js +34 -55
  24. package/html-texture.js.map +1 -1
  25. package/index.d.ts +9 -8
  26. package/index.js +9 -6
  27. package/index.js.map +1 -1
  28. package/mesh.d.ts +8 -14
  29. package/mesh.js +23 -19
  30. package/mesh.js.map +1 -1
  31. package/package.json +3 -3
  32. package/render-loop.d.ts +14 -0
  33. package/render-loop.js +23 -0
  34. package/render-loop.js.map +1 -1
  35. package/render-target.d.ts +109 -39
  36. package/render-target.js +136 -58
  37. package/render-target.js.map +1 -1
  38. package/sprites.d.ts +8 -2
  39. package/sprites.js +17 -13
  40. package/sprites.js.map +1 -1
  41. package/state.d.ts +1 -1
  42. package/state.js.map +1 -1
  43. package/text/_gpu/text-effect.js +5 -5
  44. package/text/_gpu/text-effect.js.map +1 -1
  45. package/text/_gpu/text-instances.js +4 -4
  46. package/text/_gpu/text-instances.js.map +1 -1
  47. package/text/_gpu/text-textures.js +5 -3
  48. package/text/_gpu/text-textures.js.map +1 -1
  49. package/text/glyph-storage.js +3 -3
  50. package/text/glyph-storage.js.map +1 -1
  51. package/text/text-data.js +2 -2
  52. package/text/text-data.js.map +1 -1
  53. package/text/text-renderer.js +11 -11
  54. package/text/text-renderer.js.map +1 -1
  55. package/texture-3d.d.ts +10 -7
  56. package/texture-3d.js +27 -12
  57. package/texture-3d.js.map +1 -1
  58. package/texture-source.d.ts +3 -4
  59. package/texture-source.js +5 -5
  60. package/texture-source.js.map +1 -1
  61. package/texture.d.ts +195 -31
  62. package/texture.js +215 -35
  63. package/texture.js.map +1 -1
package/texture-3d.js CHANGED
@@ -43,26 +43,36 @@ function setUnpack3DLayout(engine) {
43
43
  gl.pixelStorei(gl.UNPACK_SKIP_IMAGES, 0);
44
44
  engine._state._unpack3DLayoutKnown = true;
45
45
  }
46
- /** Bind a 3D texture to one sampler unit. `null` unbinds only `TEXTURE_3D`; repeated binds are elided. */
47
- export function bindTexture3D(engine, unit, tex) {
48
- if (engine._isLost || engine._disposed || tex?._disposed) {
46
+ /** Bind a 3D texture to one sampler unit; repeated binds are elided. Use
47
+ * {@link unbindTexture3D} to clear a unit. */
48
+ export function bindTexture3D(tex, unit) {
49
+ const engine = tex._engine;
50
+ if (engine._isLost || engine._disposed || tex._disposed) {
51
+ return;
52
+ }
53
+ bindTexture3DRaw(engine, unit, tex.handle, false);
54
+ }
55
+ /** Unbind only `TEXTURE_3D` on sampler `unit` (cached). No-op on a lost/disposed context. */
56
+ export function unbindTexture3D(engine, unit) {
57
+ if (engine._isLost || engine._disposed) {
49
58
  return;
50
59
  }
51
- bindTexture3DRaw(engine, unit, tex?.handle ?? null, false);
60
+ bindTexture3DRaw(engine, unit, null, false);
52
61
  }
53
62
  /**
54
63
  * Bind a volume to an effect's pre-assigned `sampler3D` unit without reissuing
55
64
  * `uniform1i`. Pass `null` to unbind only the 3D target on that unit.
56
65
  */
57
- export function setEffectTexture3D(engine, effect, samplerName, tex) {
58
- if (engine._isLost || !effect.isReady) {
66
+ export function setEffectTexture3D(effect, samplerName, tex) {
67
+ const engine = effect._engine;
68
+ if (engine._isLost || engine._disposed || !effect.isReady) {
59
69
  return;
60
70
  }
61
71
  const unit = effect.samplerUnits[samplerName];
62
- if (unit === undefined) {
72
+ if (unit === undefined || tex?._disposed) {
63
73
  return;
64
74
  }
65
- bindTexture3D(engine, unit, tex);
75
+ bindTexture3DRaw(engine, unit, tex === null ? null : tex.handle, false);
66
76
  }
67
77
  /**
68
78
  * Upload a tightly packed RGBA8 volume to a native WebGL2 3D texture. Useful
@@ -132,6 +142,7 @@ export function createTexture3DFromPixels(engine, data, width, height, depth, op
132
142
  _wrapT: address,
133
143
  _wrapR: address,
134
144
  _hasMipMaps: false,
145
+ _engine: engine,
135
146
  _disposed: false,
136
147
  _refCount: 1,
137
148
  _upload: upload,
@@ -144,7 +155,8 @@ export function createTexture3DFromPixels(engine, data, width, height, depth, op
144
155
  return tex;
145
156
  }
146
157
  /** Generate a full 3D mip chain for an existing volume and replay it on context restoration. No-op after loss or disposal. */
147
- export function generateTexture3DMipMaps(engine, tex) {
158
+ export function generateTexture3DMipMaps(tex) {
159
+ const engine = tex._engine;
148
160
  if (engine._isLost || engine._disposed || tex._disposed) {
149
161
  return;
150
162
  }
@@ -153,7 +165,8 @@ export function generateTexture3DMipMaps(engine, tex) {
153
165
  tex._hasMipMaps = true;
154
166
  }
155
167
  /** Update changed 3D min/mag filters and retain them across context restoration. Unchanged values are elided. */
156
- export function updateTexture3DSamplingMode(engine, tex, minFilter, magFilter) {
168
+ export function updateTexture3DSamplingMode(tex, minFilter, magFilter) {
169
+ const engine = tex._engine;
157
170
  if (engine._isLost || engine._disposed || tex._disposed || (tex._minFilter === minFilter && tex._magFilter === magFilter)) {
158
171
  return;
159
172
  }
@@ -183,7 +196,8 @@ function validTexture3DWrap(gl, mode) {
183
196
  return mode === gl.CLAMP_TO_EDGE || mode === gl.REPEAT || mode === gl.MIRRORED_REPEAT;
184
197
  }
185
198
  /** Update changed S/T/R wrapping and retain it across context restoration. Unchanged values are elided. */
186
- export function updateTexture3DWrapMode(engine, tex, wrapS, wrapT, wrapR) {
199
+ export function updateTexture3DWrapMode(tex, wrapS, wrapT, wrapR) {
200
+ const engine = tex._engine;
187
201
  if (engine._isLost || engine._disposed || tex._disposed || (tex._wrapS === wrapS && tex._wrapT === wrapT && tex._wrapR === wrapR)) {
188
202
  return;
189
203
  }
@@ -208,7 +222,8 @@ export function updateTexture3DWrapMode(engine, tex, wrapS, wrapT, wrapR) {
208
222
  /** Dispose a 3D texture and invalidate any units that still reference its handle.
209
223
  * Deregistration is expected O(1); shared references retain their registry entry
210
224
  * until the final release, and repeated disposal is a no-op. */
211
- export function disposeTexture3D(engine, tex) {
225
+ export function disposeTexture3D(tex) {
226
+ const engine = tex._engine;
212
227
  if (tex._disposed) {
213
228
  return;
214
229
  }
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,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AAM,GAAG,KAAK,CAAC;IACvB,CAAC;IACD,IAAI,GAAG,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;QACvB,EAAE,CAAC,aAAa,CAAC,EAAE,CAAC,UAAU,EAAE,EAAE,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;QAC1D,GAAG,CAAC,MAAM,GAAG,KAAK,CAAC;IACvB,CAAC;AACL,CAAC;AAED;;gEAEgE;AAChE,MAAM,UAAU,gBAAgB,CAAC,MAAuB,EAAE,GAAgB;IACtE,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. `null` unbinds only `TEXTURE_3D`; repeated binds are elided. */\nexport function bindTexture3D(engine: GLEngineContext, unit: number, tex: GLTexture3D | null): void {\n if (engine._isLost || engine._disposed || tex?._disposed) {\n return;\n }\n bindTexture3DRaw(engine, unit, tex?.handle ?? 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(engine: GLEngineContext, effect: GLEffect, samplerName: string, tex: GLTexture3D | null): void {\n if (engine._isLost || !effect.isReady) {\n return;\n }\n const unit = effect.samplerUnits[samplerName];\n if (unit === undefined) {\n return;\n }\n bindTexture3D(engine, unit, tex);\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 _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(engine: GLEngineContext, tex: GLTexture3D): void {\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(engine: GLEngineContext, tex: GLTexture3D, minFilter: GLenum, magFilter: GLenum): void {\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(engine: GLEngineContext, tex: GLTexture3D, wrapS: GLenum, wrapT: GLenum, wrapR: GLenum): void {\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(engine: GLEngineContext, tex: GLTexture3D): void {\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,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;IAC5G,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5B,MAAM,KAAK,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;IACtC,IAAI,K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,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"]}
@@ -27,10 +27,9 @@ export interface GLTextureSourceOptions extends Omit<GLTextureOptions, "internal
27
27
  * `onContextRestored` callback. This function never closes a bitmap or video
28
28
  * frame.
29
29
  *
30
- * Restoration replays level 0 and the sampling parameters only. When using a
31
- * mipmapped `minFilter`, call `generateTextureMipMaps` after creation and
32
- * again from an `onContextRestored` callback, otherwise the restored texture
33
- * is mipmap-incomplete and samples black.
30
+ * Restoration replays level 0 and the sampling parameters. When using a
31
+ * mipmapped `minFilter`, call `generateTextureMipMaps` once after creation:
32
+ * the mip intent is retained, so restoration rebuilds the chain too.
34
33
  *
35
34
  * WebGL ignores unpack flip/premultiplication for `ImageBitmap`; select
36
35
  * `imageOrientation` / `premultiplyAlpha` when decoding those sources instead.
package/texture-source.js CHANGED
@@ -16,10 +16,9 @@ import { bindTextureForUpload, disposeTexture, setBoundTextureParams, setUnpackS
16
16
  * `onContextRestored` callback. This function never closes a bitmap or video
17
17
  * frame.
18
18
  *
19
- * Restoration replays level 0 and the sampling parameters only. When using a
20
- * mipmapped `minFilter`, call `generateTextureMipMaps` after creation and
21
- * again from an `onContextRestored` callback, otherwise the restored texture
22
- * is mipmap-incomplete and samples black.
19
+ * Restoration replays level 0 and the sampling parameters. When using a
20
+ * mipmapped `minFilter`, call `generateTextureMipMaps` once after creation:
21
+ * the mip intent is retained, so restoration rebuilds the chain too.
23
22
  *
24
23
  * WebGL ignores unpack flip/premultiplication for `ImageBitmap`; select
25
24
  * `imageOrientation` / `premultiplyAlpha` when decoding those sources instead.
@@ -105,6 +104,7 @@ export function createTextureFromSource(engine, source, options = {}) {
105
104
  width,
106
105
  height,
107
106
  isReady: true,
107
+ _engine: engine,
108
108
  _disposed: false,
109
109
  _refCount: 1,
110
110
  _upload: restore,
@@ -116,7 +116,7 @@ export function createTextureFromSource(engine, source, options = {}) {
116
116
  initializeParameters(engine);
117
117
  }
118
118
  catch (error) {
119
- disposeTexture(engine, tex);
119
+ disposeTexture(tex);
120
120
  throw error;
121
121
  }
122
122
  if (options.retainSource === false) {
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;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,G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CAAC;IAClC,CAAC;SAAM,IAAI,cAAc,IAAI,MAAM,EAAE,CAAC;QAClC,KAAK,GAAG,MAAM,CAAC,YAAY,CAAC;QAC5B,MAAM,GAAG,MAAM,CAAC,aAAa,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 only. When using a\n * mipmapped `minFilter`, call `generateTextureMipMaps` after creation and\n * again from an `onContextRestored` callback, otherwise the restored texture\n * is mipmap-incomplete and samples black.\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 _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(engine, 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,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K,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"]}
package/texture.d.ts CHANGED
@@ -1,10 +1,37 @@
1
1
  import type { GLEngineContext } from "./context.js";
2
- /** Texture sampling / wrap options. All have GL-spec defaults. Mipmaps are NOT
3
- * a create option — generate the chain explicitly via {@link generateTextureMipMaps}. */
2
+ /** High-level sampling presets, mirroring Babylon's `Texture.*_SAMPLINGMODE`
3
+ * numeric constants. Resolve one to GL min/mag filters with
4
+ * {@link getSamplingModeFilters} (≙ Babylon's `_getSamplingParameters`); only
5
+ * `TRILINEAR` samples the mip chain, and only when the texture has mipmaps. */
6
+ export declare const GLSamplingMode: {
7
+ /** Nearest mag; nearest min, even with mipmaps.
8
+ * (`Texture.NEAREST_SAMPLINGMODE`) */
9
+ readonly NEAREST: 1;
10
+ /** Linear mag; linear min, even with mipmaps.
11
+ * (`Texture.BILINEAR_SAMPLINGMODE`) */
12
+ readonly BILINEAR: 2;
13
+ /** Linear mag; linear min (`LINEAR_MIPMAP_LINEAR` with mipmaps).
14
+ * (`Texture.TRILINEAR_SAMPLINGMODE`) */
15
+ readonly TRILINEAR: 3;
16
+ };
17
+ /** One of the {@link GLSamplingMode} preset values (`1`, `2` or `3`). */
18
+ export type GLSamplingMode = (typeof GLSamplingMode)[keyof typeof GLSamplingMode];
19
+ /** Creation options shared by EVERY texture factory (`createRawTexture`,
20
+ * `createFloatTexture`, `loadTexture2D`, `createTextureFromSource`,
21
+ * `createDynamicTexture`, `createHtmlElementTexture`,
22
+ * `createTextureFromHandle`). All have GL-spec defaults.
23
+ *
24
+ * Babylon-style sampling presets are opt-in so factories don't ship them:
25
+ * spread {@link getSamplingModeFilters} into the options. Mipmaps are not a
26
+ * creation option either: call {@link generateTextureMipMaps} once, after
27
+ * which every later upload and `webglcontextrestored` replay rebuilds the
28
+ * chain. */
4
29
  export interface GLTextureOptions {
5
- /** Default: false (matches Babylon's default raw-texture behaviour). */
30
+ /** Flip rows vertically on upload. Default: false (matches Babylon's
31
+ * default raw-texture behaviour). For raw / dynamic / HTML-element textures
32
+ * this is the default for every update that omits `invertY`. */
6
33
  invertY?: boolean;
7
- /** Default: gl.LINEAR. */
34
+ /** Default: gl.LINEAR. See {@link getSamplingModeFilters} for presets. */
8
35
  minFilter?: GLenum;
9
36
  /** Default: gl.LINEAR. */
10
37
  magFilter?: GLenum;
@@ -16,8 +43,9 @@ export interface GLTextureOptions {
16
43
  /** `gl.pixelStorei(UNPACK_ALIGNMENT)` for the upload (1/2/4/8). Default 4.
17
44
  * Use 1 for tightly-packed non-RGBA rows. */
18
45
  unpackAlignment?: number;
19
- /** Premultiply alpha at upload (`UNPACK_PREMULTIPLY_ALPHA_WEBGL`). Default
20
- * false. */
46
+ /** Premultiply alpha at upload (`UNPACK_PREMULTIPLY_ALPHA_WEBGL`, or
47
+ * decode-time for `loadTexture2D`). Default false. For raw / dynamic /
48
+ * HTML-element textures this is the default for every update that omits it. */
21
49
  premultiplyAlpha?: boolean;
22
50
  /** Explicit sized internalFormat for `texImage2D`. When provided it is used
23
51
  * verbatim and the inline LDR resolver is bypassed — this is how
@@ -27,13 +55,24 @@ export interface GLTextureOptions {
27
55
  * `format`/`type` via the LDR resolver. */
28
56
  internalFormat?: GLenum;
29
57
  }
58
+ /** Per-update pixel-store flags shared by `updateRawTexture`,
59
+ * `updateDynamicTexture` and `updateHtmlElementTexture` (≙ lite's
60
+ * `DynamicTextureUpdateOptions`, Babylon's `updateRawTexture(…, invertY)`). Each
61
+ * omitted flag falls back to the texture's creation {@link GLTextureOptions}
62
+ * value; the flags of the latest update are reused on `webglcontextrestored`. */
63
+ export interface GLTextureUpdateOptions {
64
+ /** Flip rows vertically on upload (`UNPACK_FLIP_Y_WEBGL`). */
65
+ invertY?: boolean;
66
+ /** Premultiply alpha on upload (`UNPACK_PREMULTIPLY_ALPHA_WEBGL`). */
67
+ premultiplyAlpha?: boolean;
68
+ }
30
69
  /**
31
70
  * Pure-state texture handle. The `handle` field is MUTABLE so the same logical
32
71
  * texture survives a `webglcontextrestored` event — every consumer keeps the
33
72
  * same `GLTexture` reference; only the internal `WebGLTexture` is swapped.
34
73
  *
35
74
  * `loadTexture2D` also uses the same handle for the 1×1 placeholder upload AND
36
- * the final image upload — so a `bindTexture(engine, unit, tex)` made before the
75
+ * the final image upload — so a `bindTexture(tex, unit)` made before the
37
76
  * image has decoded remains valid once the image arrives.
38
77
  */
39
78
  export interface GLTexture {
@@ -50,10 +89,69 @@ export interface GLTexture {
50
89
  * read as not-ready until their image/upload completes). */
51
90
  isReady: boolean;
52
91
  }
53
- /** Uint8 / float raw texture upload. The pixel data can be replaced later via
54
- * {@link updateRawTexture}; the sampling and wrap can be changed via
55
- * {@link updateTextureSamplingMode} / {@link updateTextureWrapMode}. */
56
- export declare function createRawTexture(engine: GLEngineContext, data: ArrayBufferView | null, width: number, height: number, format: GLenum, type: GLenum, options?: GLTextureOptions): GLTexture;
92
+ /** GL min/mag filters resolved from a {@link GLSamplingMode} preset. Spread
93
+ * into any {@link GLTextureOptions}, or pass to {@link updateTextureSamplingMode}
94
+ * (or use {@link updateTextureSamplingPreset} directly). */
95
+ export interface GLSamplingFilters {
96
+ /** `TEXTURE_MIN_FILTER` value. */
97
+ minFilter: GLenum;
98
+ /** `TEXTURE_MAG_FILTER` value. */
99
+ magFilter: GLenum;
100
+ }
101
+ /**
102
+ * Resolve a Babylon-style sampling preset to GL min/mag filters — the lite-gl
103
+ * counterpart of Babylon's `_getSamplingParameters(samplingMode, generateMipMaps)`.
104
+ * Pure (no GL context needed; WebGL filter enums are spec constants) and
105
+ * tree-shaken unless imported, so preset-free bundles ship none of it.
106
+ *
107
+ * | Preset | min (no mips) | min (mips) | mag |
108
+ * | ----------- | ------------- | ---------------------- | --------- |
109
+ * | `NEAREST` | `NEAREST` | `NEAREST` | `NEAREST` |
110
+ * | `BILINEAR` | `LINEAR` | `LINEAR` | `LINEAR` |
111
+ * | `TRILINEAR` | `LINEAR` | `LINEAR_MIPMAP_LINEAR` | `LINEAR` |
112
+ *
113
+ * Babylon's mipmapped nearest / bilinear variants are separate modes
114
+ * (`NEAREST_LINEAR_MIPNEAREST` …); pass their raw filters instead.
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * const tex = createRawTexture(engine, data, w, h, getSamplingModeFilters(GLSamplingMode.TRILINEAR, true));
119
+ * generateTextureMipMaps(tex); // required by the mipmapped min filter
120
+ * ```
121
+ *
122
+ * @param samplingMode - The {@link GLSamplingMode} preset.
123
+ * @param useMipMaps - Whether the texture has (or will get, via
124
+ * {@link generateTextureMipMaps}) a mip chain. Default false, so the min
125
+ * filter never references a missing chain.
126
+ * @returns The preset's `{ minFilter, magFilter }`.
127
+ */
128
+ export declare function getSamplingModeFilters(samplingMode: GLSamplingMode, useMipMaps?: boolean): GLSamplingFilters;
129
+ /** Options for {@link createRawTexture}: the shared {@link GLTextureOptions}
130
+ * plus the pixel `format` / `type` of `data`. */
131
+ export interface GLRawTextureOptions extends GLTextureOptions {
132
+ /** Pixel format of `data` (`gl.RGBA`, `gl.RGB`, `gl.RG`, `gl.RED`,
133
+ * `gl.LUMINANCE`…). Default `gl.RGBA`. */
134
+ format?: GLenum;
135
+ /** Pixel type of `data`. Default `gl.UNSIGNED_BYTE`. For float data, prefer
136
+ * {@link createFloatTexture}, which also picks the sized float internalFormat. */
137
+ type?: GLenum;
138
+ }
139
+ /**
140
+ * Create a raw texture from a typed array (or an uninitialised allocation).
141
+ * RGBA8 by default; set {@link GLRawTextureOptions.format} / `type` for other
142
+ * layouts. The pixel data can be replaced later via {@link updateRawTexture};
143
+ * the sampling and wrap can be changed via {@link updateTextureSamplingMode} /
144
+ * {@link updateTextureWrapMode}.
145
+ *
146
+ * @param engine - The engine.
147
+ * @param data - Initial pixels matching `format` / `type`, or `null` for an
148
+ * uninitialised allocation.
149
+ * @param width - Texture width in texels.
150
+ * @param height - Texture height in texels.
151
+ * @param options - See {@link GLRawTextureOptions}.
152
+ * @returns The new {@link GLTexture}.
153
+ */
154
+ export declare function createRawTexture(engine: GLEngineContext, data: ArrayBufferView | null, width: number, height: number, options?: GLRawTextureOptions): GLTexture;
57
155
  /** Options for {@link createFloatTexture} — the shared {@link GLTextureOptions}
58
156
  * plus the float-specific `type` / `format`. */
59
157
  export interface GLFloatTextureOptions extends GLTextureOptions {
@@ -80,36 +178,93 @@ export interface GLFloatTextureOptions extends GLTextureOptions {
80
178
  * @returns The new {@link GLTexture}.
81
179
  */
82
180
  export declare function createFloatTexture(engine: GLEngineContext, data: ArrayBufferView | null, width: number, height: number, options?: GLFloatTextureOptions): GLTexture;
83
- /** Generate the mip chain for a texture from its level-0 contents — mipmaps as
84
- * an explicit opt-in function rather than baked into the create path. Binds for
85
- * upload (unit 0). No-op on a lost/disposed context or a handle-less texture. */
86
- export declare function generateTextureMipMaps(engine: GLEngineContext, tex: GLTexture): void;
181
+ /**
182
+ * Generate the mip chain for a texture from its level-0 contents and keep it
183
+ * current (≙ Babylon's `useMipMaps` / lite-gl's `generateTexture3DMipMaps`).
184
+ * The intent is retained: every later level-0 upload — `updateRawTexture`,
185
+ * `updateDynamicTexture`, `updateHtmlElementTexture`, the `loadTexture2D` image
186
+ * arrival and `webglcontextrestored` replay — rebuilds the chain, so a
187
+ * mipmapped min filter never samples an incomplete texture. Only bundles that
188
+ * call it pay for mipmapping. Calling it again just regenerates now.
189
+ *
190
+ * Binds for upload (unit 0). The format must be filterable (float textures need
191
+ * the matching linear-filtering cap). Render targets use
192
+ * `generateRenderTargetMipMaps` instead. No-op on a lost/disposed context or a
193
+ * disposed texture.
194
+ *
195
+ * @param tex - The texture whose mip chain to generate.
196
+ */
197
+ export declare function generateTextureMipMaps(tex: GLTexture): void;
87
198
  /**
88
199
  * Re-upload the pixel data (and optionally resize) of a texture created by
89
200
  * {@link createRawTexture} — the lite-gl equivalent of Babylon's
90
201
  * `updateRawTexture` / `_uploadDataToTextureDirectly`. Goes through the same
91
202
  * upload closure used by context-restore, so the new contents survive a context
92
- * loss. No-op on a lost/disposed/non-raw texture.
203
+ * loss, and rebuilds the mip chain after {@link generateTextureMipMaps}. No-op
204
+ * on a lost/disposed/non-raw texture.
93
205
  *
94
- * @param engine - The engine.
95
206
  * @param tex - The raw texture to update.
96
207
  * @param data - New pixel data (must match the original `format`/`type`).
97
- * @param options - Optional new `width`/`height` (default: unchanged) and
98
- * `unpackAlignment` (default 4).
208
+ * @param options - Optional new `width`/`height` (default: unchanged),
209
+ * `unpackAlignment` (default 4) and `invertY` / `premultiplyAlpha` (default:
210
+ * the creation values; ≙ Babylon's `updateRawTexture(…, invertY)`). The flags
211
+ * are reused by context-restore replay.
99
212
  */
100
- export declare function updateRawTexture(engine: GLEngineContext, tex: GLTexture, data: ArrayBufferView | null, options?: {
213
+ export declare function updateRawTexture(tex: GLTexture, data: ArrayBufferView | null, options?: GLTextureUpdateOptions & {
101
214
  width?: number;
102
215
  height?: number;
103
216
  unpackAlignment?: number;
104
217
  }): void;
105
- /** Update a texture's min/mag sampling filters — the lite-gl equivalent of
106
- * Babylon's `updateTextureSamplingMode`. Binds for upload (unit 0) so the
107
- * `texParameteri` lands on this texture. No-op on a lost/disposed context. */
108
- export declare function updateTextureSamplingMode(engine: GLEngineContext, tex: GLTexture, minFilter: GLenum, magFilter: GLenum): void;
218
+ /**
219
+ * Update a texture's raw GL min/mag sampling filters (e.g. `gl.NEAREST`,
220
+ * `gl.LINEAR_MIPMAP_LINEAR`). A mipmapped min filter needs
221
+ * {@link generateTextureMipMaps}; for Babylon-style presets use
222
+ * {@link updateTextureSamplingPreset}. Binds for upload (unit 0) so the
223
+ * `texParameteri` lands on this texture. The change is not replayed on
224
+ * `webglcontextrestored` (creation options are). No-op on a lost/disposed
225
+ * context or a disposed texture.
226
+ *
227
+ * @param tex - The texture to update.
228
+ * @param minFilter - `TEXTURE_MIN_FILTER` value.
229
+ * @param magFilter - `TEXTURE_MAG_FILTER` value.
230
+ */
231
+ export declare function updateTextureSamplingMode(tex: GLTexture, minFilter: GLenum, magFilter: GLenum): void;
232
+ /**
233
+ * Update a texture's sampling mode from a Babylon-style preset — the lite-gl
234
+ * equivalent of Babylon's
235
+ * `updateTextureSamplingMode(samplingMode, texture, generateMipMaps)`. The
236
+ * filters come from {@link getSamplingModeFilters}, using the mipmapped
237
+ * `TRILINEAR` min filter when the texture already has mipmaps (from
238
+ * {@link generateTextureMipMaps}) or `generateMipMaps` is true. Like Babylon,
239
+ * `generateMipMaps` only generates (and retains) the chain for `TRILINEAR`,
240
+ * right after the filters are set; `NEAREST` / `BILINEAR` never sample mips.
241
+ * Unlike Babylon, an existing chain is not regenerated (lite-gl already
242
+ * rebuilds it on every upload); call {@link generateTextureMipMaps} to force it.
243
+ *
244
+ * Kept separate from the raw {@link updateTextureSamplingMode} so raw-filter
245
+ * callers don't bundle the preset and mipmap code. Same binding, replay and
246
+ * lost/disposed behavior.
247
+ *
248
+ * @param tex - The texture to update.
249
+ * @param samplingMode - The {@link GLSamplingMode} preset.
250
+ * @param generateMipMaps - Generate the mip chain now (`TRILINEAR` only). Default false.
251
+ */
252
+ export declare function updateTextureSamplingPreset(tex: GLTexture, samplingMode: GLSamplingMode, generateMipMaps?: boolean): void;
253
+ /**
254
+ * Whether a texture holds its final content and can be sampled — the function
255
+ * form of {@link GLTexture.isReady}, mirroring `isEffectReady(effect)`
256
+ * (≙ Babylon's `texture.isReady()`). False while `loadTexture2D` is still
257
+ * decoding (the 1×1 placeholder is bound meanwhile), after disposal, and while
258
+ * the context is lost/disposed.
259
+ *
260
+ * @param tex - The texture to query.
261
+ * @returns `true` when the texture is ready to sample.
262
+ */
263
+ export declare function isTextureReady(tex: GLTexture): boolean;
109
264
  /** Update a texture's S/T wrap modes (`gl.CLAMP_TO_EDGE` / `gl.REPEAT` /
110
265
  * `gl.MIRRORED_REPEAT`) — the lite-gl equivalent of Babylon's
111
266
  * `updateTextureWrappingMode`. No-op on a lost/disposed context. */
112
- export declare function updateTextureWrapMode(engine: GLEngineContext, tex: GLTexture, wrapS: GLenum, wrapT: GLenum): void;
267
+ export declare function updateTextureWrapMode(tex: GLTexture, wrapS: GLenum, wrapT: GLenum): void;
113
268
  /**
114
269
  * Wrap an existing raw `WebGLTexture` (e.g. one created by a host renderer or a
115
270
  * previous engine) as a {@link GLTexture} — the lite-gl equivalent of
@@ -124,18 +279,27 @@ export declare function updateTextureWrapMode(engine: GLEngineContext, tex: GLTe
124
279
  * @param handle - The external `WebGLTexture`.
125
280
  * @param width - Texture width in texels.
126
281
  * @param height - Texture height in texels.
127
- * @param options - Optional sampling/wrap to apply once (min/mag/wrapS/wrapT).
282
+ * @param options - Optional sampling state to apply once (min/mag/wrapS/wrapT).
283
+ * Upload-only options (`invertY`, `premultiplyAlpha`, `unpackAlignment`,
284
+ * `internalFormat`) do not apply — the handle's pixels are owned externally.
128
285
  * @returns A {@link GLTexture} wrapping the handle.
129
286
  */
130
287
  export declare function createTextureFromHandle(engine: GLEngineContext, handle: WebGLTexture, width: number, height: number, options?: GLTextureOptions): GLTexture;
131
288
  /** Asynchronous image upload.The returned texture is immediately usable (1×1
132
289
  * transparent placeholder); `isReady` flips true once the image has been
133
290
  * decoded and uploaded. The decoded `ImageBitmap` is retained on the texture
134
- * for offline-safe `webglcontextrestored` replay. */
291
+ * for offline-safe `webglcontextrestored` replay. `invertY` and
292
+ * `premultiplyAlpha` are applied at decode time (`createImageBitmap`'s
293
+ * `imageOrientation` / `premultiplyAlpha`) because the `UNPACK_*` flags are
294
+ * ignored for `ImageBitmap` sources. */
135
295
  export declare function loadTexture2D(engine: GLEngineContext, url: string, options?: GLTextureOptions, onLoad?: (tex: GLTexture) => void, onError?: (err: Error) => void): GLTexture;
136
- /** Cached bind. Skips `gl.activeTexture` and/or `gl.bindTexture` when nothing
137
- * changes. No-op when `tex._disposed` or `engine._isLost`. */
138
- export declare function bindTexture(engine: GLEngineContext, unit: number, tex: GLTexture | null): void;
296
+ /** Cached bind of `tex` on sampler `unit`. Skips `gl.activeTexture` and/or
297
+ * `gl.bindTexture` when nothing changes. No-op when `tex._disposed` or the
298
+ * context is lost/disposed. Use {@link unbindTexture} to clear a unit. */
299
+ export declare function bindTexture(tex: GLTexture, unit: number): void;
300
+ /** Unbind `TEXTURE_2D` on sampler `unit` (cached, like {@link bindTexture}).
301
+ * No-op on a lost/disposed context. */
302
+ export declare function unbindTexture(engine: GLEngineContext, unit: number): void;
139
303
  /** Disposes the texture. Walks `_state.boundTextures` and clears every slot
140
304
  * that still references the handle — otherwise a later `bindTexture(unit, B)`
141
305
  * to the same unit would be wrongly elided when slot still showed handle A.
@@ -143,4 +307,4 @@ export declare function bindTexture(engine: GLEngineContext, unit: number, tex:
143
307
  * Shared references keep the texture registered until its final release;
144
308
  * repeated disposal is a no-op. Unregistered wrappers keep their existing
145
309
  * handle-deletion semantics without affecting the live registry. */
146
- export declare function disposeTexture(engine: GLEngineContext, tex: GLTexture): void;
310
+ export declare function disposeTexture(tex: GLTexture): void;