@babylonjs/lite-gl 1.23.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.
Files changed (94) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +103 -6
  3. package/blend.d.ts +1 -1
  4. package/blend.js +1 -1
  5. package/blend.js.map +1 -1
  6. package/context.d.ts +52 -4
  7. package/context.js +108 -11
  8. package/context.js.map +1 -1
  9. package/depth-stencil.d.ts +4 -16
  10. package/depth-stencil.js +10 -11
  11. package/depth-stencil.js.map +1 -1
  12. package/dynamic-texture.d.ts +6 -3
  13. package/dynamic-texture.js +16 -4
  14. package/dynamic-texture.js.map +1 -1
  15. package/effect-renderer.d.ts +1 -11
  16. package/effect-renderer.js.map +1 -1
  17. package/effect.d.ts +16 -29
  18. package/effect.js +52 -19
  19. package/effect.js.map +1 -1
  20. package/family.d.ts +474 -0
  21. package/family.js +14 -0
  22. package/family.js.map +1 -0
  23. package/framebuffer-validation.d.ts +20 -0
  24. package/framebuffer-validation.js +37 -0
  25. package/framebuffer-validation.js.map +1 -0
  26. package/html-texture.d.ts +2 -2
  27. package/html-texture.js.map +1 -1
  28. package/index.d.ts +10 -7
  29. package/index.js +5 -4
  30. package/index.js.map +1 -1
  31. package/mesh.d.ts +72 -15
  32. package/mesh.js +221 -64
  33. package/mesh.js.map +1 -1
  34. package/native/command-stream.d.ts +1 -0
  35. package/native/command-stream.js +135 -0
  36. package/native/command-stream.js.map +1 -0
  37. package/native/context.d.ts +26 -0
  38. package/native/context.js +89 -0
  39. package/native/context.js.map +1 -0
  40. package/native/effect.d.ts +44 -0
  41. package/native/effect.js +276 -0
  42. package/native/effect.js.map +1 -0
  43. package/native/family.d.ts +75 -0
  44. package/native/family.js +2 -0
  45. package/native/family.js.map +1 -0
  46. package/native/identity.d.ts +11 -0
  47. package/native/identity.js +2 -0
  48. package/native/identity.js.map +1 -0
  49. package/native/index.d.ts +25 -0
  50. package/native/index.js +7 -0
  51. package/native/index.js.map +1 -0
  52. package/native/mesh.d.ts +53 -0
  53. package/native/mesh.js +137 -0
  54. package/native/mesh.js.map +1 -0
  55. package/native/render-target.d.ts +35 -0
  56. package/native/render-target.js +159 -0
  57. package/native/render-target.js.map +1 -0
  58. package/native/runtime.d.ts +1 -0
  59. package/native/runtime.js +12 -0
  60. package/native/runtime.js.map +1 -0
  61. package/native/state.d.ts +19 -0
  62. package/native/state.js +104 -0
  63. package/native/state.js.map +1 -0
  64. package/native/texture.d.ts +37 -0
  65. package/native/texture.js +86 -0
  66. package/native/texture.js.map +1 -0
  67. package/package.json +11 -3
  68. package/render-target.d.ts +18 -9
  69. package/render-target.js +84 -31
  70. package/render-target.js.map +1 -1
  71. package/scissor.d.ts +3 -3
  72. package/scissor.js +2 -2
  73. package/scissor.js.map +1 -1
  74. package/sprites.d.ts +5 -3
  75. package/sprites.js +24 -23
  76. package/sprites.js.map +1 -1
  77. package/state.d.ts +26 -13
  78. package/state.js +15 -15
  79. package/state.js.map +1 -1
  80. package/text/_gpu/text-instances.js +1 -1
  81. package/text/_gpu/text-instances.js.map +1 -1
  82. package/text/text-renderer.js.map +1 -1
  83. package/texture-3d.d.ts +2 -1
  84. package/texture-3d.js +7 -2
  85. package/texture-3d.js.map +1 -1
  86. package/texture-source.d.ts +4 -2
  87. package/texture-source.js +3 -2
  88. package/texture-source.js.map +1 -1
  89. package/texture.d.ts +18 -8
  90. package/texture.js +61 -35
  91. package/texture.js.map +1 -1
  92. package/web-family.d.ts +88 -0
  93. package/web-family.js +2 -0
  94. package/web-family.js.map +1 -0
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. The source must remain unchanged for context-restore replay;
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. The source must remain unchanged for context-restore replay;
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, data);
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.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AAI,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,GAAgB;IAC7C,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. 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,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,C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AAC,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"]}
@@ -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);
@@ -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,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M,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,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,CAAC;QAC3E,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;IAC7E,CAAC;IACD,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;IACrB,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC,MAAM,CAAC;IACjD,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,E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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 * 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} — the lite-gl equivalent of
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` will still `gl.deleteTexture` it, so
276
- * only wrap a handle whose deletion you intend lite-gl to manage.
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. `invertY` and
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;
package/texture.js CHANGED
@@ -23,12 +23,12 @@ export const GLSamplingMode = {
23
23
  export function setUnpackState(engine, flipY, premultiplyAlpha, alignment = 4) {
24
24
  const gl = engine.gl;
25
25
  const s = engine._state;
26
- const fy = flipY ? 1 : 0;
26
+ const fy = +flipY;
27
27
  if (s.unpackFlipY !== fy) {
28
28
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, fy);
29
29
  s.unpackFlipY = fy;
30
30
  }
31
- const pm = premultiplyAlpha ? 1 : 0;
31
+ const pm = +premultiplyAlpha;
32
32
  if (s.unpackPremultiplyAlpha !== pm) {
33
33
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, pm);
34
34
  s.unpackPremultiplyAlpha = pm;
@@ -40,10 +40,11 @@ export function setUnpackState(engine, flipY, premultiplyAlpha, alignment = 4) {
40
40
  }
41
41
  /** Apply sampling and wrap state to the texture currently bound for upload. @internal */
42
42
  export function setBoundTextureParams(gl, minFilter, magFilter, wrapS, wrapT) {
43
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
44
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
45
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);
46
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);
43
+ const target = gl.TEXTURE_2D;
44
+ gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, minFilter);
45
+ gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, magFilter);
46
+ gl.texParameteri(target, gl.TEXTURE_WRAP_S, wrapS);
47
+ gl.texParameteri(target, gl.TEXTURE_WRAP_T, wrapT);
47
48
  }
48
49
  /**
49
50
  * Resolve a Babylon-style sampling preset to GL min/mag filters — the lite-gl
@@ -91,7 +92,8 @@ export function setTexImageSourceSize(tex, source) {
91
92
  * RGBA8 by default; set {@link GLRawTextureOptions.format} / `type` for other
92
93
  * layouts. The pixel data can be replaced later via {@link updateRawTexture};
93
94
  * the sampling and wrap can be changed via {@link updateTextureSamplingMode} /
94
- * {@link updateTextureWrapMode}.
95
+ * {@link updateTextureWrapMode}. Upload data is retained by reference for
96
+ * automatic recovery unless the engine is host-owned (`createHostOwnedGLEngine`).
95
97
  *
96
98
  * @param engine - The engine.
97
99
  * @param data - Initial pixels matching `format` / `type`, or `null` for an
@@ -123,16 +125,24 @@ export function createRawTexture(engine, data, width, height, options) {
123
125
  const internalFormat = opts.internalFormat ?? resolveLdrInternalFormat(gl, format, type);
124
126
  // Mutable upload state — `updateRawTexture` mutates these and re-runs
125
127
  // `upload`, so the context-restore replay (which calls the same `upload`)
126
- // always reproduces the latest contents.
128
+ // always reproduces the latest contents. The replayed dimensions stay
129
+ // paired with the replayed pixels: `tex.width` / `height` may run ahead of
130
+ // them (a render-target resize during loss, applied by its restore).
127
131
  let curData = data;
128
132
  let curWidth = width;
129
133
  let curHeight = height;
130
134
  let curAlign = opts.unpackAlignment ?? 4;
131
- const upload = (target) => {
135
+ const upload = (target, pixels = curData, w = curWidth, h = curHeight, alignment = curAlign) => {
136
+ curData = target._noRestore ? null : pixels;
137
+ curWidth = w;
138
+ curHeight = h;
139
+ curAlign = alignment;
140
+ tex.width = w;
141
+ tex.height = h;
132
142
  const g = target.gl;
133
- setUnpackState(target, tex._invertY ?? invertY, tex._premultiplyAlpha ?? premultiply, curAlign);
143
+ setUnpackState(target, tex._invertY ?? invertY, tex._premultiplyAlpha ?? premultiply, alignment);
134
144
  bindTextureForUpload(target, tex.handle);
135
- g.texImage2D(g.TEXTURE_2D, 0, internalFormat, curWidth, curHeight, 0, format, type, curData);
145
+ g.texImage2D(g.TEXTURE_2D, 0, internalFormat, w, h, 0, format, type, pixels);
136
146
  };
137
147
  const initializeParameters = (target) => {
138
148
  setBoundTextureParams(target.gl, minFilter, magFilter, wrapS, wrapT);
@@ -147,18 +157,10 @@ export function createRawTexture(engine, data, width, height, options) {
147
157
  _disposed: false,
148
158
  _refCount: 1,
149
159
  _upload: upload,
160
+ _updateRaw: upload,
150
161
  _initializeParameters: initializeParameters,
151
162
  _wasReady: true,
152
163
  };
153
- tex._updateRaw = (target, newData, newWidth, newHeight, unpackAlignment) => {
154
- curData = newData;
155
- curWidth = newWidth;
156
- curHeight = newHeight;
157
- curAlign = unpackAlignment;
158
- tex.width = newWidth;
159
- tex.height = newHeight;
160
- upload(target);
161
- };
162
164
  upload(engine);
163
165
  initializeParameters(engine);
164
166
  engine._textures.add(tex);
@@ -338,13 +340,14 @@ export function updateTextureWrapMode(tex, wrapS, wrapT) {
338
340
  }
339
341
  /**
340
342
  * Wrap an existing raw `WebGLTexture` (e.g. one created by a host renderer or a
341
- * previous engine) as a {@link GLTexture} — the lite-gl equivalent of
342
- * ShapeBuilder's `createTextureGraphicsResourceFromExternalWebGLTexture`.
343
+ * previous engine) as a {@link GLTexture}.
343
344
  *
344
345
  * The wrapper does NOT own the underlying handle's upload — it is NOT registered
345
346
  * for context-restore replay (the external owner is responsible for that) and is
346
- * marked ready immediately. `disposeTexture` will still `gl.deleteTexture` it, so
347
- * only wrap a handle whose deletion you intend lite-gl to manage.
347
+ * marked ready immediately. By default `disposeTexture` still `gl.deleteTexture`s
348
+ * the handle; pass `ownsHandle = false` to BORROW a handle the host keeps
349
+ * owning (e.g. a texture shared with another renderer), so disposal only
350
+ * releases the wrapper and its cached bindings.
348
351
  *
349
352
  * @param engine - The engine.
350
353
  * @param handle - The external `WebGLTexture`.
@@ -353,9 +356,10 @@ export function updateTextureWrapMode(tex, wrapS, wrapT) {
353
356
  * @param options - Optional sampling state to apply once (min/mag/wrapS/wrapT).
354
357
  * Upload-only options (`invertY`, `premultiplyAlpha`, `unpackAlignment`,
355
358
  * `internalFormat`) do not apply — the handle's pixels are owned externally.
359
+ * @param ownsHandle - When `false`, `disposeTexture` never deletes `handle`. Default `true`.
356
360
  * @returns A {@link GLTexture} wrapping the handle.
357
361
  */
358
- export function createTextureFromHandle(engine, handle, width, height, options) {
362
+ export function createTextureFromHandle(engine, handle, width, height, options, ownsHandle = true) {
359
363
  const tex = {
360
364
  handle,
361
365
  target: engine.gl.TEXTURE_2D,
@@ -369,6 +373,9 @@ export function createTextureFromHandle(engine, handle, width, height, options)
369
373
  _upload: () => { },
370
374
  _wasReady: true,
371
375
  };
376
+ if (!ownsHandle) {
377
+ tex._borrowed = true;
378
+ }
372
379
  if (options !== undefined) {
373
380
  const gl = engine.gl;
374
381
  bindTextureForUpload(engine, handle);
@@ -379,7 +386,12 @@ export function createTextureFromHandle(engine, handle, width, height, options)
379
386
  /** Asynchronous image upload.The returned texture is immediately usable (1×1
380
387
  * transparent placeholder); `isReady` flips true once the image has been
381
388
  * decoded and uploaded. The decoded `ImageBitmap` is retained on the texture
382
- * for offline-safe `webglcontextrestored` replay. `invertY` and
389
+ * for offline-safe `webglcontextrestored` replay by default; on a host-owned
390
+ * engine (`createHostOwnedGLEngine`), it is closed/released after the upload,
391
+ * and a decode finishing while that engine is lost closes the bitmap and
392
+ * reports a context-loss error through `onError` instead of `onLoad`, even when
393
+ * the host already disposed the texture (e.g. from `onContextLost`). Otherwise,
394
+ * disposing the texture before its decode finishes cancels the load silently. `invertY` and
383
395
  * `premultiplyAlpha` are applied at decode time (`createImageBitmap`'s
384
396
  * `imageOrientation` / `premultiplyAlpha`) because the `UNPACK_*` flags are
385
397
  * ignored for `ImageBitmap` sources. */
@@ -397,7 +409,6 @@ export function loadTexture2D(engine, url, options, onLoad, onError) {
397
409
  const invertY = opts.invertY ?? false;
398
410
  const premultiplyAlpha = opts.premultiplyAlpha ?? false;
399
411
  let bitmap = null;
400
- const placeholderPixels = new Uint8Array([0, 0, 0, 0]);
401
412
  const upload = (target) => {
402
413
  const g = target.gl;
403
414
  // invertY / premultiplyAlpha are applied at decode time via createImageBitmap
@@ -412,7 +423,8 @@ export function loadTexture2D(engine, url, options, onLoad, onError) {
412
423
  tex.height = bitmap.height;
413
424
  }
414
425
  else {
415
- g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, 1, 1, 0, g.RGBA, g.UNSIGNED_BYTE, placeholderPixels);
426
+ // null leaves WebGL2's zero-initialized texel: transparent black.
427
+ g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, 1, 1, 0, g.RGBA, g.UNSIGNED_BYTE, null);
416
428
  }
417
429
  };
418
430
  const initializeParameters = (target) => {
@@ -446,19 +458,29 @@ export function loadTexture2D(engine, url, options, onLoad, onError) {
446
458
  })
447
459
  .then((blob) => createImageBitmap(blob, { premultiplyAlpha: premultiplyAlpha ? "premultiply" : "none", imageOrientation: invertY ? "flipY" : "none" }))
448
460
  .then((bm) => {
461
+ // An opted-out host recreates its resources after a loss, so a load
462
+ // finishing during loss fails — even if the host already disposed
463
+ // the texture — and a callback-wrapping Promise always settles.
464
+ if (engine._noRestore && engine._isLost) {
465
+ bm.close();
466
+ throw new Error(`lite-gl: context lost while loading ${url}`);
467
+ }
449
468
  if (tex._disposed) {
450
469
  bm.close();
451
470
  return;
452
471
  }
453
472
  bitmap = bm;
454
- // If the context is lost mid-flight, defer the upload — the restore
455
- // handler will call _upload again, which will then see `bitmap !== null`
456
- // and replay the real image.
473
+ // Default recovery defers an in-flight image until restore.
457
474
  if (!engine._isLost) {
458
475
  // Through `tex._upload` so a retained mip chain is rebuilt.
459
476
  tex._upload(engine);
460
477
  tex.isReady = true;
461
478
  }
479
+ // An opted-out host releases the bitmap once uploaded.
480
+ if (engine._noRestore) {
481
+ bitmap = null;
482
+ bm.close();
483
+ }
462
484
  tex._wasReady = true;
463
485
  if (onLoad !== undefined) {
464
486
  onLoad(tex);
@@ -534,7 +556,9 @@ export function bindTextureForUpload(engine, handle) {
534
556
  * Deregistration is expected O(1), independent of the live texture count.
535
557
  * Shared references keep the texture registered until its final release;
536
558
  * repeated disposal is a no-op. Unregistered wrappers keep their existing
537
- * handle-deletion semantics without affecting the live registry. */
559
+ * handle-deletion semantics without affecting the live registry; borrowed
560
+ * wrappers (`createTextureFromHandle(…, ownsHandle = false)`) never delete
561
+ * their handle. */
538
562
  export function disposeTexture(tex) {
539
563
  if (tex._disposed) {
540
564
  return;
@@ -546,13 +570,15 @@ export function disposeTexture(tex) {
546
570
  }
547
571
  tex._disposed = true;
548
572
  engine._textures.delete(tex);
549
- if (!engine._isLost && !engine._disposed) {
573
+ if (!tex._borrowed && !engine._isLost && !engine._disposed) {
550
574
  engine.gl.deleteTexture(tex.handle);
551
575
  }
552
576
  const bound = engine._state.boundTextures;
553
577
  for (let u = 0; u < bound.length; u++) {
554
578
  if (bound[u] === tex.handle) {
555
- bound[u] = null;
579
+ // A deleted handle leaves the unit unbound; a borrowed one is still
580
+ // bound in GL, so the slot becomes unknown and the next unbind is issued.
581
+ bound[u] = tex._borrowed ? undefined : null;
556
582
  }
557
583
  }
558
584
  }
@@ -584,7 +610,7 @@ export function resolveLdrInternalFormat(gl, format, type) {
584
610
  }
585
611
  /** Internal — pick a sized internalFormat for `texImage2D`. WebGL2 prefers
586
612
  * sized formats for non-color-renderable / non-readback paths; for the
587
- * NeonBrush use cases (sample-only) the unsized format works too, but sized
613
+ * sample-only use cases the unsized format works too, but sized
588
614
  * is more portable. Shared with the render-target module.
589
615
  * @internal */
590
616
  export function pickSizedInternalFormat(gl, format, type) {