@babylonjs/lite-gl 1.20.0 → 1.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -19
- package/context.js +2 -0
- package/context.js.map +1 -1
- package/depth-stencil.d.ts +1 -2
- package/depth-stencil.js +2 -2
- package/depth-stencil.js.map +1 -1
- package/dynamic-texture.d.ts +18 -18
- package/dynamic-texture.js +28 -24
- package/dynamic-texture.js.map +1 -1
- package/effect-ready.d.ts +1 -3
- package/effect-ready.js +3 -3
- package/effect-ready.js.map +1 -1
- package/effect-renderer.d.ts +39 -12
- package/effect-renderer.js +47 -17
- package/effect-renderer.js.map +1 -1
- package/effect.d.ts +25 -21
- package/effect.js +69 -51
- package/effect.js.map +1 -1
- package/gpu-frame-time.d.ts +49 -0
- package/gpu-frame-time.js +130 -0
- package/gpu-frame-time.js.map +1 -0
- package/html-texture.d.ts +24 -34
- package/html-texture.js +34 -55
- package/html-texture.js.map +1 -1
- package/index.d.ts +9 -8
- package/index.js +9 -6
- package/index.js.map +1 -1
- package/mesh.d.ts +8 -14
- package/mesh.js +23 -19
- package/mesh.js.map +1 -1
- package/package.json +3 -3
- package/render-loop.d.ts +14 -0
- package/render-loop.js +23 -0
- package/render-loop.js.map +1 -1
- package/render-target.d.ts +109 -39
- package/render-target.js +136 -58
- package/render-target.js.map +1 -1
- package/sprites.d.ts +8 -2
- package/sprites.js +17 -13
- package/sprites.js.map +1 -1
- package/state.d.ts +1 -1
- package/state.js.map +1 -1
- package/text/_gpu/text-effect.js +5 -5
- package/text/_gpu/text-effect.js.map +1 -1
- package/text/_gpu/text-instances.js +4 -4
- package/text/_gpu/text-instances.js.map +1 -1
- package/text/_gpu/text-textures.js +5 -3
- package/text/_gpu/text-textures.js.map +1 -1
- package/text/glyph-storage.js +3 -3
- package/text/glyph-storage.js.map +1 -1
- package/text/text-data.js +2 -2
- package/text/text-data.js.map +1 -1
- package/text/text-renderer.js +11 -11
- package/text/text-renderer.js.map +1 -1
- package/texture-3d.d.ts +10 -7
- package/texture-3d.js +27 -12
- package/texture-3d.js.map +1 -1
- package/texture-source.d.ts +3 -4
- package/texture-source.js +5 -5
- package/texture-source.js.map +1 -1
- package/texture.d.ts +195 -31
- package/texture.js +215 -35
- package/texture.js.map +1 -1
package/texture.js
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
/** High-level sampling presets, mirroring Babylon's `Texture.*_SAMPLINGMODE`
|
|
2
|
+
* numeric constants. Resolve one to GL min/mag filters with
|
|
3
|
+
* {@link getSamplingModeFilters} (≙ Babylon's `_getSamplingParameters`); only
|
|
4
|
+
* `TRILINEAR` samples the mip chain, and only when the texture has mipmaps. */
|
|
5
|
+
export const GLSamplingMode = {
|
|
6
|
+
/** Nearest mag; nearest min, even with mipmaps.
|
|
7
|
+
* (`Texture.NEAREST_SAMPLINGMODE`) */
|
|
8
|
+
NEAREST: 1,
|
|
9
|
+
/** Linear mag; linear min, even with mipmaps.
|
|
10
|
+
* (`Texture.BILINEAR_SAMPLINGMODE`) */
|
|
11
|
+
BILINEAR: 2,
|
|
12
|
+
/** Linear mag; linear min (`LINEAR_MIPMAP_LINEAR` with mipmaps).
|
|
13
|
+
* (`Texture.TRILINEAR_SAMPLINGMODE`) */
|
|
14
|
+
TRILINEAR: 3,
|
|
15
|
+
};
|
|
1
16
|
/**
|
|
2
17
|
* Apply the three `UNPACK_*` pixel-store flags through the GL-state cache,
|
|
3
18
|
* eliding redundant `pixelStorei` calls. Centralising this guarantees EVERY
|
|
@@ -30,16 +45,71 @@ export function setBoundTextureParams(gl, minFilter, magFilter, wrapS, wrapT) {
|
|
|
30
45
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);
|
|
31
46
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);
|
|
32
47
|
}
|
|
33
|
-
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Resolve a Babylon-style sampling preset to GL min/mag filters — the lite-gl
|
|
50
|
+
* counterpart of Babylon's `_getSamplingParameters(samplingMode, generateMipMaps)`.
|
|
51
|
+
* Pure (no GL context needed; WebGL filter enums are spec constants) and
|
|
52
|
+
* tree-shaken unless imported, so preset-free bundles ship none of it.
|
|
53
|
+
*
|
|
54
|
+
* | Preset | min (no mips) | min (mips) | mag |
|
|
55
|
+
* | ----------- | ------------- | ---------------------- | --------- |
|
|
56
|
+
* | `NEAREST` | `NEAREST` | `NEAREST` | `NEAREST` |
|
|
57
|
+
* | `BILINEAR` | `LINEAR` | `LINEAR` | `LINEAR` |
|
|
58
|
+
* | `TRILINEAR` | `LINEAR` | `LINEAR_MIPMAP_LINEAR` | `LINEAR` |
|
|
59
|
+
*
|
|
60
|
+
* Babylon's mipmapped nearest / bilinear variants are separate modes
|
|
61
|
+
* (`NEAREST_LINEAR_MIPNEAREST` …); pass their raw filters instead.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* const tex = createRawTexture(engine, data, w, h, getSamplingModeFilters(GLSamplingMode.TRILINEAR, true));
|
|
66
|
+
* generateTextureMipMaps(tex); // required by the mipmapped min filter
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @param samplingMode - The {@link GLSamplingMode} preset.
|
|
70
|
+
* @param useMipMaps - Whether the texture has (or will get, via
|
|
71
|
+
* {@link generateTextureMipMaps}) a mip chain. Default false, so the min
|
|
72
|
+
* filter never references a missing chain.
|
|
73
|
+
* @returns The preset's `{ minFilter, magFilter }`.
|
|
74
|
+
*/
|
|
75
|
+
export function getSamplingModeFilters(samplingMode, useMipMaps = false) {
|
|
76
|
+
// WebGL filter enums: NEAREST 0x2600, LINEAR 0x2601, LINEAR_MIPMAP_LINEAR 0x2703.
|
|
77
|
+
const magFilter = samplingMode === GLSamplingMode.NEAREST ? 0x2600 : 0x2601;
|
|
78
|
+
return { minFilter: useMipMaps && samplingMode === GLSamplingMode.TRILINEAR ? 0x2703 : magFilter, magFilter };
|
|
79
|
+
}
|
|
80
|
+
/** Write the pixel size of a `TexImageSource` as `texImage2D` will upload it
|
|
81
|
+
* into `tex.width` / `tex.height`: the intrinsic size for video
|
|
82
|
+
* (`videoWidth`), image (`naturalWidth`) and `VideoFrame` (`displayWidth`)
|
|
83
|
+
* sources, `width`/`height` otherwise. Clamped to ≥ 1. Allocation-free — it
|
|
84
|
+
* runs on every dynamic / HTML-element re-upload. @internal */
|
|
85
|
+
export function setTexImageSourceSize(tex, source) {
|
|
86
|
+
tex.width = (source.videoWidth ?? source.naturalWidth ?? source.displayWidth ?? source.width) || 1;
|
|
87
|
+
tex.height = (source.videoHeight ?? source.naturalHeight ?? source.displayHeight ?? source.height) || 1;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Create a raw texture from a typed array (or an uninitialised allocation).
|
|
91
|
+
* RGBA8 by default; set {@link GLRawTextureOptions.format} / `type` for other
|
|
92
|
+
* layouts. The pixel data can be replaced later via {@link updateRawTexture};
|
|
93
|
+
* the sampling and wrap can be changed via {@link updateTextureSamplingMode} /
|
|
94
|
+
* {@link updateTextureWrapMode}.
|
|
95
|
+
*
|
|
96
|
+
* @param engine - The engine.
|
|
97
|
+
* @param data - Initial pixels matching `format` / `type`, or `null` for an
|
|
98
|
+
* uninitialised allocation.
|
|
99
|
+
* @param width - Texture width in texels.
|
|
100
|
+
* @param height - Texture height in texels.
|
|
101
|
+
* @param options - See {@link GLRawTextureOptions}.
|
|
102
|
+
* @returns The new {@link GLTexture}.
|
|
103
|
+
*/
|
|
104
|
+
export function createRawTexture(engine, data, width, height, options) {
|
|
37
105
|
const gl = engine.gl;
|
|
38
106
|
const handle = gl.createTexture();
|
|
39
107
|
if (handle === null) {
|
|
40
108
|
throw new Error("lite-gl: gl.createTexture returned null");
|
|
41
109
|
}
|
|
42
110
|
const opts = options ?? {};
|
|
111
|
+
const format = opts.format ?? gl.RGBA;
|
|
112
|
+
const type = opts.type ?? gl.UNSIGNED_BYTE;
|
|
43
113
|
const minFilter = opts.minFilter ?? gl.LINEAR;
|
|
44
114
|
const magFilter = opts.magFilter ?? gl.LINEAR;
|
|
45
115
|
const wrapS = opts.wrapS ?? gl.CLAMP_TO_EDGE;
|
|
@@ -60,7 +130,7 @@ export function createRawTexture(engine, data, width, height, format, type, opti
|
|
|
60
130
|
let curAlign = opts.unpackAlignment ?? 4;
|
|
61
131
|
const upload = (target) => {
|
|
62
132
|
const g = target.gl;
|
|
63
|
-
setUnpackState(target, invertY, premultiply, curAlign);
|
|
133
|
+
setUnpackState(target, tex._invertY ?? invertY, tex._premultiplyAlpha ?? premultiply, curAlign);
|
|
64
134
|
bindTextureForUpload(target, tex.handle);
|
|
65
135
|
g.texImage2D(g.TEXTURE_2D, 0, internalFormat, curWidth, curHeight, 0, format, type, curData);
|
|
66
136
|
};
|
|
@@ -73,6 +143,7 @@ export function createRawTexture(engine, data, width, height, format, type, opti
|
|
|
73
143
|
width,
|
|
74
144
|
height,
|
|
75
145
|
isReady: true,
|
|
146
|
+
_engine: engine,
|
|
76
147
|
_disposed: false,
|
|
77
148
|
_refCount: 1,
|
|
78
149
|
_upload: upload,
|
|
@@ -119,42 +190,86 @@ export function createFloatTexture(engine, data, width, height, options) {
|
|
|
119
190
|
// `createRawTexture`'s inline LDR resolver is bypassed and the float table
|
|
120
191
|
// stays out of byte-only bundles.
|
|
121
192
|
const internalFormat = pickSizedInternalFormat(gl, format, type);
|
|
122
|
-
return createRawTexture(engine, data, width, height, format, type,
|
|
193
|
+
return createRawTexture(engine, data, width, height, { ...o, format, type, internalFormat });
|
|
123
194
|
}
|
|
124
|
-
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
|
|
195
|
+
/**
|
|
196
|
+
* Generate the mip chain for a texture from its level-0 contents and keep it
|
|
197
|
+
* current (≙ Babylon's `useMipMaps` / lite-gl's `generateTexture3DMipMaps`).
|
|
198
|
+
* The intent is retained: every later level-0 upload — `updateRawTexture`,
|
|
199
|
+
* `updateDynamicTexture`, `updateHtmlElementTexture`, the `loadTexture2D` image
|
|
200
|
+
* arrival and `webglcontextrestored` replay — rebuilds the chain, so a
|
|
201
|
+
* mipmapped min filter never samples an incomplete texture. Only bundles that
|
|
202
|
+
* call it pay for mipmapping. Calling it again just regenerates now.
|
|
203
|
+
*
|
|
204
|
+
* Binds for upload (unit 0). The format must be filterable (float textures need
|
|
205
|
+
* the matching linear-filtering cap). Render targets use
|
|
206
|
+
* `generateRenderTargetMipMaps` instead. No-op on a lost/disposed context or a
|
|
207
|
+
* disposed texture.
|
|
208
|
+
*
|
|
209
|
+
* @param tex - The texture whose mip chain to generate.
|
|
210
|
+
*/
|
|
211
|
+
export function generateTextureMipMaps(tex) {
|
|
212
|
+
const engine = tex._engine;
|
|
128
213
|
if (engine._isLost || engine._disposed || tex._disposed || tex.handle === null) {
|
|
129
214
|
return;
|
|
130
215
|
}
|
|
131
216
|
bindTextureForUpload(engine, tex.handle);
|
|
132
217
|
engine.gl.generateMipmap(engine.gl.TEXTURE_2D);
|
|
218
|
+
if (!tex._hasMipMaps) {
|
|
219
|
+
tex._hasMipMaps = true;
|
|
220
|
+
// Every `_upload` that runs (update / restore-registry replay) leaves
|
|
221
|
+
// this texture bound for upload; handle/RT textures are never replayed.
|
|
222
|
+
const upload = tex._upload;
|
|
223
|
+
tex._upload = (target) => {
|
|
224
|
+
upload(target);
|
|
225
|
+
target.gl.generateMipmap(target.gl.TEXTURE_2D);
|
|
226
|
+
};
|
|
227
|
+
}
|
|
133
228
|
}
|
|
134
229
|
/**
|
|
135
230
|
* Re-upload the pixel data (and optionally resize) of a texture created by
|
|
136
231
|
* {@link createRawTexture} — the lite-gl equivalent of Babylon's
|
|
137
232
|
* `updateRawTexture` / `_uploadDataToTextureDirectly`. Goes through the same
|
|
138
233
|
* upload closure used by context-restore, so the new contents survive a context
|
|
139
|
-
* loss
|
|
234
|
+
* loss, and rebuilds the mip chain after {@link generateTextureMipMaps}. No-op
|
|
235
|
+
* on a lost/disposed/non-raw texture.
|
|
140
236
|
*
|
|
141
|
-
* @param engine - The engine.
|
|
142
237
|
* @param tex - The raw texture to update.
|
|
143
238
|
* @param data - New pixel data (must match the original `format`/`type`).
|
|
144
|
-
* @param options - Optional new `width`/`height` (default: unchanged)
|
|
145
|
-
* `unpackAlignment` (default 4)
|
|
239
|
+
* @param options - Optional new `width`/`height` (default: unchanged),
|
|
240
|
+
* `unpackAlignment` (default 4) and `invertY` / `premultiplyAlpha` (default:
|
|
241
|
+
* the creation values; ≙ Babylon's `updateRawTexture(…, invertY)`). The flags
|
|
242
|
+
* are reused by context-restore replay.
|
|
146
243
|
*/
|
|
147
|
-
export function updateRawTexture(
|
|
244
|
+
export function updateRawTexture(tex, data, options) {
|
|
245
|
+
const engine = tex._engine;
|
|
148
246
|
if (engine._isLost || engine._disposed || tex._disposed || tex._updateRaw === undefined) {
|
|
149
247
|
return;
|
|
150
248
|
}
|
|
151
249
|
const o = options ?? {};
|
|
250
|
+
tex._invertY = o.invertY;
|
|
251
|
+
tex._premultiplyAlpha = o.premultiplyAlpha;
|
|
152
252
|
tex._updateRaw(engine, data, o.width ?? tex.width, o.height ?? tex.height, o.unpackAlignment ?? 4);
|
|
253
|
+
if (tex._hasMipMaps) {
|
|
254
|
+
// `_updateRaw` leaves the texture bound for upload (unit 0).
|
|
255
|
+
engine.gl.generateMipmap(engine.gl.TEXTURE_2D);
|
|
256
|
+
}
|
|
153
257
|
}
|
|
154
|
-
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
|
|
258
|
+
/**
|
|
259
|
+
* Update a texture's raw GL min/mag sampling filters (e.g. `gl.NEAREST`,
|
|
260
|
+
* `gl.LINEAR_MIPMAP_LINEAR`). A mipmapped min filter needs
|
|
261
|
+
* {@link generateTextureMipMaps}; for Babylon-style presets use
|
|
262
|
+
* {@link updateTextureSamplingPreset}. Binds for upload (unit 0) so the
|
|
263
|
+
* `texParameteri` lands on this texture. The change is not replayed on
|
|
264
|
+
* `webglcontextrestored` (creation options are). No-op on a lost/disposed
|
|
265
|
+
* context or a disposed texture.
|
|
266
|
+
*
|
|
267
|
+
* @param tex - The texture to update.
|
|
268
|
+
* @param minFilter - `TEXTURE_MIN_FILTER` value.
|
|
269
|
+
* @param magFilter - `TEXTURE_MAG_FILTER` value.
|
|
270
|
+
*/
|
|
271
|
+
export function updateTextureSamplingMode(tex, minFilter, magFilter) {
|
|
272
|
+
const engine = tex._engine;
|
|
158
273
|
if (engine._isLost || engine._disposed || tex._disposed) {
|
|
159
274
|
return;
|
|
160
275
|
}
|
|
@@ -163,10 +278,56 @@ export function updateTextureSamplingMode(engine, tex, minFilter, magFilter) {
|
|
|
163
278
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
|
|
164
279
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
|
|
165
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Update a texture's sampling mode from a Babylon-style preset — the lite-gl
|
|
283
|
+
* equivalent of Babylon's
|
|
284
|
+
* `updateTextureSamplingMode(samplingMode, texture, generateMipMaps)`. The
|
|
285
|
+
* filters come from {@link getSamplingModeFilters}, using the mipmapped
|
|
286
|
+
* `TRILINEAR` min filter when the texture already has mipmaps (from
|
|
287
|
+
* {@link generateTextureMipMaps}) or `generateMipMaps` is true. Like Babylon,
|
|
288
|
+
* `generateMipMaps` only generates (and retains) the chain for `TRILINEAR`,
|
|
289
|
+
* right after the filters are set; `NEAREST` / `BILINEAR` never sample mips.
|
|
290
|
+
* Unlike Babylon, an existing chain is not regenerated (lite-gl already
|
|
291
|
+
* rebuilds it on every upload); call {@link generateTextureMipMaps} to force it.
|
|
292
|
+
*
|
|
293
|
+
* Kept separate from the raw {@link updateTextureSamplingMode} so raw-filter
|
|
294
|
+
* callers don't bundle the preset and mipmap code. Same binding, replay and
|
|
295
|
+
* lost/disposed behavior.
|
|
296
|
+
*
|
|
297
|
+
* @param tex - The texture to update.
|
|
298
|
+
* @param samplingMode - The {@link GLSamplingMode} preset.
|
|
299
|
+
* @param generateMipMaps - Generate the mip chain now (`TRILINEAR` only). Default false.
|
|
300
|
+
*/
|
|
301
|
+
export function updateTextureSamplingPreset(tex, samplingMode, generateMipMaps) {
|
|
302
|
+
// ≙ Babylon: mips are generated only for a mipmapped preset (`TRILINEAR`).
|
|
303
|
+
const generate = generateMipMaps === true && samplingMode === GLSamplingMode.TRILINEAR;
|
|
304
|
+
const filters = getSamplingModeFilters(samplingMode, generate || tex._hasMipMaps === true);
|
|
305
|
+
updateTextureSamplingMode(tex, filters.minFilter, filters.magFilter);
|
|
306
|
+
// An existing chain is already rebuilt on every upload; call
|
|
307
|
+
// `generateTextureMipMaps` directly to force a regeneration.
|
|
308
|
+
if (generate && !tex._hasMipMaps) {
|
|
309
|
+
generateTextureMipMaps(tex);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Whether a texture holds its final content and can be sampled — the function
|
|
314
|
+
* form of {@link GLTexture.isReady}, mirroring `isEffectReady(effect)`
|
|
315
|
+
* (≙ Babylon's `texture.isReady()`). False while `loadTexture2D` is still
|
|
316
|
+
* decoding (the 1×1 placeholder is bound meanwhile), after disposal, and while
|
|
317
|
+
* the context is lost/disposed.
|
|
318
|
+
*
|
|
319
|
+
* @param tex - The texture to query.
|
|
320
|
+
* @returns `true` when the texture is ready to sample.
|
|
321
|
+
*/
|
|
322
|
+
export function isTextureReady(tex) {
|
|
323
|
+
const engine = tex._engine;
|
|
324
|
+
return tex.isReady && !tex._disposed && !engine._isLost && !engine._disposed;
|
|
325
|
+
}
|
|
166
326
|
/** Update a texture's S/T wrap modes (`gl.CLAMP_TO_EDGE` / `gl.REPEAT` /
|
|
167
327
|
* `gl.MIRRORED_REPEAT`) — the lite-gl equivalent of Babylon's
|
|
168
328
|
* `updateTextureWrappingMode`. No-op on a lost/disposed context. */
|
|
169
|
-
export function updateTextureWrapMode(
|
|
329
|
+
export function updateTextureWrapMode(tex, wrapS, wrapT) {
|
|
330
|
+
const engine = tex._engine;
|
|
170
331
|
if (engine._isLost || engine._disposed || tex._disposed) {
|
|
171
332
|
return;
|
|
172
333
|
}
|
|
@@ -189,7 +350,9 @@ export function updateTextureWrapMode(engine, tex, wrapS, wrapT) {
|
|
|
189
350
|
* @param handle - The external `WebGLTexture`.
|
|
190
351
|
* @param width - Texture width in texels.
|
|
191
352
|
* @param height - Texture height in texels.
|
|
192
|
-
* @param options - Optional sampling
|
|
353
|
+
* @param options - Optional sampling state to apply once (min/mag/wrapS/wrapT).
|
|
354
|
+
* Upload-only options (`invertY`, `premultiplyAlpha`, `unpackAlignment`,
|
|
355
|
+
* `internalFormat`) do not apply — the handle's pixels are owned externally.
|
|
193
356
|
* @returns A {@link GLTexture} wrapping the handle.
|
|
194
357
|
*/
|
|
195
358
|
export function createTextureFromHandle(engine, handle, width, height, options) {
|
|
@@ -199,6 +362,7 @@ export function createTextureFromHandle(engine, handle, width, height, options)
|
|
|
199
362
|
width,
|
|
200
363
|
height,
|
|
201
364
|
isReady: true,
|
|
365
|
+
_engine: engine,
|
|
202
366
|
_disposed: false,
|
|
203
367
|
_refCount: 1,
|
|
204
368
|
// External handle — restore replay is the external owner's job.
|
|
@@ -215,7 +379,10 @@ export function createTextureFromHandle(engine, handle, width, height, options)
|
|
|
215
379
|
/** Asynchronous image upload.The returned texture is immediately usable (1×1
|
|
216
380
|
* transparent placeholder); `isReady` flips true once the image has been
|
|
217
381
|
* decoded and uploaded. The decoded `ImageBitmap` is retained on the texture
|
|
218
|
-
* for offline-safe `webglcontextrestored` replay.
|
|
382
|
+
* for offline-safe `webglcontextrestored` replay. `invertY` and
|
|
383
|
+
* `premultiplyAlpha` are applied at decode time (`createImageBitmap`'s
|
|
384
|
+
* `imageOrientation` / `premultiplyAlpha`) because the `UNPACK_*` flags are
|
|
385
|
+
* ignored for `ImageBitmap` sources. */
|
|
219
386
|
export function loadTexture2D(engine, url, options, onLoad, onError) {
|
|
220
387
|
const gl = engine.gl;
|
|
221
388
|
const handle = gl.createTexture();
|
|
@@ -228,13 +395,15 @@ export function loadTexture2D(engine, url, options, onLoad, onError) {
|
|
|
228
395
|
const wrapS = opts.wrapS ?? gl.CLAMP_TO_EDGE;
|
|
229
396
|
const wrapT = opts.wrapT ?? gl.CLAMP_TO_EDGE;
|
|
230
397
|
const invertY = opts.invertY ?? false;
|
|
398
|
+
const premultiplyAlpha = opts.premultiplyAlpha ?? false;
|
|
231
399
|
let bitmap = null;
|
|
232
400
|
const placeholderPixels = new Uint8Array([0, 0, 0, 0]);
|
|
233
401
|
const upload = (target) => {
|
|
234
402
|
const g = target.gl;
|
|
235
|
-
// invertY
|
|
236
|
-
// because UNPACK_FLIP_Y_WEBGL
|
|
237
|
-
//
|
|
403
|
+
// invertY / premultiplyAlpha are applied at decode time via createImageBitmap
|
|
404
|
+
// because UNPACK_FLIP_Y_WEBGL / UNPACK_PREMULTIPLY_ALPHA_WEBGL are IGNORED for
|
|
405
|
+
// ImageBitmap sources (and the transparent 1×1 placeholder is invariant to
|
|
406
|
+
// both), so keep flip + premultiply off here.
|
|
238
407
|
setUnpackState(target, false, false);
|
|
239
408
|
bindTextureForUpload(target, tex.handle);
|
|
240
409
|
if (bitmap !== null) {
|
|
@@ -255,6 +424,7 @@ export function loadTexture2D(engine, url, options, onLoad, onError) {
|
|
|
255
424
|
width: 1,
|
|
256
425
|
height: 1,
|
|
257
426
|
isReady: false,
|
|
427
|
+
_engine: engine,
|
|
258
428
|
_disposed: false,
|
|
259
429
|
_refCount: 1,
|
|
260
430
|
_upload: upload,
|
|
@@ -274,7 +444,7 @@ export function loadTexture2D(engine, url, options, onLoad, onError) {
|
|
|
274
444
|
}
|
|
275
445
|
return r.blob();
|
|
276
446
|
})
|
|
277
|
-
.then((blob) => createImageBitmap(blob, { premultiplyAlpha: "none", imageOrientation: invertY ? "flipY" : "none" }))
|
|
447
|
+
.then((blob) => createImageBitmap(blob, { premultiplyAlpha: premultiplyAlpha ? "premultiply" : "none", imageOrientation: invertY ? "flipY" : "none" }))
|
|
278
448
|
.then((bm) => {
|
|
279
449
|
if (tex._disposed) {
|
|
280
450
|
bm.close();
|
|
@@ -285,7 +455,8 @@ export function loadTexture2D(engine, url, options, onLoad, onError) {
|
|
|
285
455
|
// handler will call _upload again, which will then see `bitmap !== null`
|
|
286
456
|
// and replay the real image.
|
|
287
457
|
if (!engine._isLost) {
|
|
288
|
-
|
|
458
|
+
// Through `tex._upload` so a retained mip chain is rebuilt.
|
|
459
|
+
tex._upload(engine);
|
|
289
460
|
tex.isReady = true;
|
|
290
461
|
}
|
|
291
462
|
tex._wasReady = true;
|
|
@@ -304,16 +475,23 @@ export function loadTexture2D(engine, url, options, onLoad, onError) {
|
|
|
304
475
|
});
|
|
305
476
|
return tex;
|
|
306
477
|
}
|
|
307
|
-
/** Cached bind
|
|
308
|
-
* changes. No-op when `tex._disposed` or
|
|
309
|
-
|
|
310
|
-
|
|
478
|
+
/** Cached bind of `tex` on sampler `unit`. Skips `gl.activeTexture` and/or
|
|
479
|
+
* `gl.bindTexture` when nothing changes. No-op when `tex._disposed` or the
|
|
480
|
+
* context is lost/disposed. Use {@link unbindTexture} to clear a unit. */
|
|
481
|
+
export function bindTexture(tex, unit) {
|
|
482
|
+
const engine = tex._engine;
|
|
483
|
+
if (engine._isLost || engine._disposed || tex._disposed) {
|
|
311
484
|
return;
|
|
312
485
|
}
|
|
313
|
-
|
|
486
|
+
bindTextureRaw(engine, unit, tex.handle);
|
|
487
|
+
}
|
|
488
|
+
/** Unbind `TEXTURE_2D` on sampler `unit` (cached, like {@link bindTexture}).
|
|
489
|
+
* No-op on a lost/disposed context. */
|
|
490
|
+
export function unbindTexture(engine, unit) {
|
|
491
|
+
if (engine._isLost || engine._disposed) {
|
|
314
492
|
return;
|
|
315
493
|
}
|
|
316
|
-
bindTextureRaw(engine, unit,
|
|
494
|
+
bindTextureRaw(engine, unit, null);
|
|
317
495
|
}
|
|
318
496
|
/** @internal — for callers that already hold a raw `WebGLTexture`. Not part of
|
|
319
497
|
* the public API: `bindTexture` (public) and the texture `_upload` closures bind
|
|
@@ -357,10 +535,11 @@ export function bindTextureForUpload(engine, handle) {
|
|
|
357
535
|
* Shared references keep the texture registered until its final release;
|
|
358
536
|
* repeated disposal is a no-op. Unregistered wrappers keep their existing
|
|
359
537
|
* handle-deletion semantics without affecting the live registry. */
|
|
360
|
-
export function disposeTexture(
|
|
538
|
+
export function disposeTexture(tex) {
|
|
361
539
|
if (tex._disposed) {
|
|
362
540
|
return;
|
|
363
541
|
}
|
|
542
|
+
const engine = tex._engine;
|
|
364
543
|
if (tex._refCount > 1) {
|
|
365
544
|
tex._refCount--;
|
|
366
545
|
return;
|
|
@@ -381,8 +560,9 @@ export function disposeTexture(engine, tex) {
|
|
|
381
560
|
* paths ONLY: RGBA→RGBA8, RGB→RGB8, RG→RG8, RED→R8, LUMINANCE passthrough.
|
|
382
561
|
* Deliberately knows nothing about FLOAT / HALF_FLOAT so the float-format table
|
|
383
562
|
* in {@link pickSizedInternalFormat} tree-shakes out of byte-only bundles.
|
|
563
|
+
* Shared with the render-target module (which validates the format first).
|
|
384
564
|
* @internal */
|
|
385
|
-
function resolveLdrInternalFormat(gl, format, type) {
|
|
565
|
+
export function resolveLdrInternalFormat(gl, format, type) {
|
|
386
566
|
if (type === gl.UNSIGNED_BYTE) {
|
|
387
567
|
if (format === gl.RGBA) {
|
|
388
568
|
return gl.RGBA8;
|
package/texture.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"texture.js","sourceRoot":"","sources":["../src/texture.ts"],"names":[],"mappings":"AAoGA;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,MAAuB,EAAE,KAAc,EAAE,gBAAyB,EAAE,SAAS,GAAG,CAAC;IAC5G,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;IACrB,MAAM,CAAC,G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KAAK,EAAE,CAAC,IAAI,EAAE,CAAC;YACrB,OAAO,EAAE,CAAC,OAAO,CAAC;QACtB,CAAC;QACD,IAAI,MAAM,KAAK,EAAE,CAAC,GAAG,EAAE,CAAC;YACpB,OAAO,EAAE,CAAC,MAAM,CAAC;QACrB,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC","sourcesContent":["import type { GLEngineContext } from \"./context.js\";\n\n/** Texture sampling / wrap options. All have GL-spec defaults. Mipmaps are NOT\n * a create option — generate the chain explicitly via {@link generateTextureMipMaps}. */\nexport interface GLTextureOptions {\n /** Default: false (matches Babylon's default raw-texture behaviour). */\n invertY?: boolean;\n /** Default: gl.LINEAR. */\n minFilter?: GLenum;\n /** Default: gl.LINEAR. */\n magFilter?: GLenum;\n /** Default: gl.CLAMP_TO_EDGE. May be `gl.REPEAT` / `gl.MIRRORED_REPEAT`\n * (NPOT-safe in WebGL2). */\n wrapS?: GLenum;\n /** Default: gl.CLAMP_TO_EDGE. May be `gl.REPEAT` / `gl.MIRRORED_REPEAT`. */\n wrapT?: GLenum;\n /** `gl.pixelStorei(UNPACK_ALIGNMENT)` for the upload (1/2/4/8). Default 4.\n * Use 1 for tightly-packed non-RGBA rows. */\n unpackAlignment?: number;\n /** Premultiply alpha at upload (`UNPACK_PREMULTIPLY_ALPHA_WEBGL`). Default\n * false. */\n premultiplyAlpha?: boolean;\n /** Explicit sized internalFormat for `texImage2D`. When provided it is used\n * verbatim and the inline LDR resolver is bypassed — this is how\n * {@link createFloatTexture} injects its `RGBA16F` / `RGBA32F` choice\n * without the byte path ever referencing the float-format table (keeping it\n * tree-shakeable out of RGBA8-only bundles). Default: derived from\n * `format`/`type` via the LDR resolver. */\n internalFormat?: GLenum;\n}\n\n/**\n * Pure-state texture handle. The `handle` field is MUTABLE so the same logical\n * texture survives a `webglcontextrestored` event — every consumer keeps the\n * same `GLTexture` reference; only the internal `WebGLTexture` is swapped.\n *\n * `loadTexture2D` also uses the same handle for the 1×1 placeholder upload AND\n * the final image upload — so a `bindTexture(engine, unit, tex)` made before the\n * image has decoded remains valid once the image arrives.\n */\nexport interface GLTexture {\n /** The live `WebGLTexture`. MUTABLE — swapped for a fresh handle on\n * `webglcontextrestored` while consumers keep the same `GLTexture` reference. */\n handle: WebGLTexture;\n /** GL texture target (normally `gl.TEXTURE_2D`); 3D textures use their own API. */\n readonly target: GLenum;\n /** Texture width in texels. Updated once an async upload resolves. */\n width: number;\n /** Texture height in texels. Updated once an async upload resolves. */\n height: number;\n /** True when the texture is safe to sample with final content (placeholders\n * read as not-ready until their image/upload completes). */\n isReady: boolean;\n /** @internal */\n _disposed: boolean;\n /** @internal */\n _refCount: number;\n /** Replays the latest pixel content into the currently bound texture handle. @internal */\n _upload: (engine: GLEngineContext) => void;\n /** Initializes creation-time sampling state on the bound texture handle. @internal */\n _initializeParameters?: (engine: GLEngineContext) => void;\n /**\n * Snapshot of `isReady` captured on `webglcontextlost` so the restore\n * handler knows whether to flip it back on after `_upload`. Textures\n * that were still mid-load (e.g. `loadTexture2D` whose bitmap hadn't\n * arrived) stay not-ready; the async path will set `isReady=true` once\n * the bitmap finishes decoding into the new handle.\n * @internal\n */\n _wasReady: boolean;\n /**\n * Live re-upload hook installed by `createRawTexture` — lets\n * `updateRawTexture` replace the pixel data (and optionally the size)\n * through the same closure that the context-restore replay uses, keeping\n * restore correct. Absent on image / HTML-element / external textures.\n * @internal\n */\n _updateRaw?: (engine: GLEngineContext, data: ArrayBufferView | null, width: number, height: number, unpackAlignment: number) => void;\n /**\n * Dynamic-texture source captured by `updateDynamicTexture`\n * (`@babylonjs/lite-gl`) and replayed into the fresh handle\n * on `webglcontextrestored`. Null/absent on non-dynamic textures.\n * @internal\n */\n _dynSource?: TexImageSource | null;\n /** UNPACK_FLIP_Y applied when replaying {@link GLTexture._dynSource}. @internal */\n _dynInvertY?: boolean;\n /** UNPACK_PREMULTIPLY_ALPHA applied when replaying {@link GLTexture._dynSource}. @internal */\n _dynPremultiplyAlpha?: boolean;\n /**\n * The `handle` the dynamic texture's `texParameteri` filter/wrap state was\n * last applied to. `texParameteri` is per-texture GL state that survives pixel\n * re-uploads, so the dynamic-texture `_upload` re-applies it ONLY when this no\n * longer matches `handle` — i.e. at creation and after `webglcontextrestored`\n * installs a fresh handle — not on every per-frame `updateDynamicTexture`.\n * @internal\n */\n _dynParamsHandle?: WebGLTexture | null;\n}\n\n/**\n * Apply the three `UNPACK_*` pixel-store flags through the GL-state cache,\n * eliding redundant `pixelStorei` calls. Centralising this guarantees EVERY\n * upload path sets all three explicitly, so a premultiplying / flipping upload\n * never leaks its flag into a later upload that forgot to set it.\n * @internal\n */\nexport function setUnpackState(engine: GLEngineContext, flipY: boolean, premultiplyAlpha: boolean, alignment = 4): void {\n const gl = engine.gl;\n const s = engine._state;\n const fy = flipY ? 1 : 0;\n if (s.unpackFlipY !== fy) {\n gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, fy);\n s.unpackFlipY = fy;\n }\n const pm = premultiplyAlpha ? 1 : 0;\n if (s.unpackPremultiplyAlpha !== pm) {\n gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, pm);\n s.unpackPremultiplyAlpha = pm;\n }\n if (s.unpackAlignment !== alignment) {\n gl.pixelStorei(gl.UNPACK_ALIGNMENT, alignment);\n s.unpackAlignment = alignment;\n }\n}\n\n/** Apply sampling and wrap state to the texture currently bound for upload. @internal */\nexport function setBoundTextureParams(gl: WebGL2RenderingContext, minFilter: GLenum, magFilter: GLenum, wrapS: GLenum, wrapT: GLenum): void {\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);\n}\n\n/** Uint8 / float raw texture upload. The pixel data can be replaced later via\n * {@link updateRawTexture}; the sampling and wrap can be changed via\n * {@link updateTextureSamplingMode} / {@link updateTextureWrapMode}. */\nexport function createRawTexture(\n engine: GLEngineContext,\n data: ArrayBufferView | null,\n width: number,\n height: number,\n format: GLenum,\n type: GLenum,\n options?: GLTextureOptions\n): GLTexture {\n const gl = engine.gl;\n const handle = gl.createTexture();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null\");\n }\n const opts = options ?? {};\n const minFilter = opts.minFilter ?? gl.LINEAR;\n const magFilter = opts.magFilter ?? gl.LINEAR;\n const wrapS = opts.wrapS ?? gl.CLAMP_TO_EDGE;\n const wrapT = opts.wrapT ?? gl.CLAMP_TO_EDGE;\n const invertY = opts.invertY ?? false;\n const premultiply = opts.premultiplyAlpha ?? false;\n // LDR byte formats are resolved INLINE here; the float-format table in\n // `pickSizedInternalFormat` is deliberately NOT referenced from this path so\n // it tree-shakes out of byte-only bundles. `createFloatTexture` injects its\n // sized float format via `opts.internalFormat`.\n const internalFormat = opts.internalFormat ?? resolveLdrInternalFormat(gl, format, type);\n\n // Mutable upload state — `updateRawTexture` mutates these and re-runs\n // `upload`, so the context-restore replay (which calls the same `upload`)\n // always reproduces the latest contents.\n let curData = data;\n let curWidth = width;\n let curHeight = height;\n let curAlign = opts.unpackAlignment ?? 4;\n\n const upload = (target: GLEngineContext): void => {\n const g = target.gl;\n setUnpackState(target, invertY, premultiply, curAlign);\n bindTextureForUpload(target, tex.handle);\n g.texImage2D(g.TEXTURE_2D, 0, internalFormat, curWidth, curHeight, 0, format, type, curData);\n };\n\n const initializeParameters = (target: GLEngineContext): void => {\n setBoundTextureParams(target.gl, minFilter, magFilter, wrapS, wrapT);\n };\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: upload,\n _initializeParameters: initializeParameters,\n _wasReady: true,\n };\n tex._updateRaw = (target: GLEngineContext, newData: ArrayBufferView | null, newWidth: number, newHeight: number, unpackAlignment: number): void => {\n curData = newData;\n curWidth = newWidth;\n curHeight = newHeight;\n curAlign = unpackAlignment;\n tex.width = newWidth;\n tex.height = newHeight;\n upload(target);\n };\n upload(engine);\n initializeParameters(engine);\n engine._textures.add(tex);\n return tex;\n}\n\n/** Options for {@link createFloatTexture} — the shared {@link GLTextureOptions}\n * plus the float-specific `type` / `format`. */\nexport interface GLFloatTextureOptions extends GLTextureOptions {\n /** Float texel type — `gl.HALF_FLOAT` (default) or `gl.FLOAT`. */\n type?: GLenum;\n /** Color format. Default `gl.RGBA`. */\n format?: GLenum;\n}\n\n/**\n * Create a float / half-float raw texture — the HDR counterpart of\n * {@link createRawTexture}. This is the ONLY texture factory that carries the\n * `RGBA16F` / `RGBA32F` sized-format knowledge (via {@link pickSizedInternalFormat}),\n * so byte-only consumers calling {@link createRawTexture} ship none of it.\n * Defaults to `gl.HALF_FLOAT`; pass `options.type = gl.FLOAT` for full 32-bit.\n * The caller is responsible for the matching engine cap (e.g.\n * `caps.textureFloatLinearFiltering`) when sampling these with `LINEAR`.\n *\n * @param engine - The engine.\n * @param data - Initial pixels (`Float32Array` for FLOAT, `Uint16Array` for\n * HALF_FLOAT), or `null` for an uninitialised allocation.\n * @param width - Texture width in texels (≥ 1).\n * @param height - Texture height in texels (≥ 1).\n * @param options - See {@link GLFloatTextureOptions}.\n * @returns The new {@link GLTexture}.\n */\nexport function createFloatTexture(engine: GLEngineContext, data: ArrayBufferView | null, width: number, height: number, options?: GLFloatTextureOptions): GLTexture {\n const gl = engine.gl;\n const o = options ?? {};\n const format = o.format ?? gl.RGBA;\n const type = o.type ?? gl.HALF_FLOAT;\n // Precompute the sized float internalFormat HERE and pass it through, so\n // `createRawTexture`'s inline LDR resolver is bypassed and the float table\n // stays out of byte-only bundles.\n const internalFormat = pickSizedInternalFormat(gl, format, type);\n return createRawTexture(engine, data, width, height, format, type, { ...o, internalFormat });\n}\n\n/** Generate the mip chain for a texture from its level-0 contents — mipmaps as\n * an explicit opt-in function rather than baked into the create path. Binds for\n * upload (unit 0). No-op on a lost/disposed context or a handle-less texture. */\nexport function generateTextureMipMaps(engine: GLEngineContext, tex: GLTexture): void {\n if (engine._isLost || engine._disposed || tex._disposed || tex.handle === null) {\n return;\n }\n bindTextureForUpload(engine, tex.handle);\n engine.gl.generateMipmap(engine.gl.TEXTURE_2D);\n}\n\n/**\n * Re-upload the pixel data (and optionally resize) of a texture created by\n * {@link createRawTexture} — the lite-gl equivalent of Babylon's\n * `updateRawTexture` / `_uploadDataToTextureDirectly`. Goes through the same\n * upload closure used by context-restore, so the new contents survive a context\n * loss. No-op on a lost/disposed/non-raw texture.\n *\n * @param engine - The engine.\n * @param tex - The raw texture to update.\n * @param data - New pixel data (must match the original `format`/`type`).\n * @param options - Optional new `width`/`height` (default: unchanged) and\n * `unpackAlignment` (default 4).\n */\nexport function updateRawTexture(\n engine: GLEngineContext,\n tex: GLTexture,\n data: ArrayBufferView | null,\n options?: { width?: number; height?: number; unpackAlignment?: number }\n): void {\n if (engine._isLost || engine._disposed || tex._disposed || tex._updateRaw === undefined) {\n return;\n }\n const o = options ?? {};\n tex._updateRaw(engine, data, o.width ?? tex.width, o.height ?? tex.height, o.unpackAlignment ?? 4);\n}\n\n/** Update a texture's min/mag sampling filters — the lite-gl equivalent of\n * Babylon's `updateTextureSamplingMode`. Binds for upload (unit 0) so the\n * `texParameteri` lands on this texture. No-op on a lost/disposed context. */\nexport function updateTextureSamplingMode(engine: GLEngineContext, tex: GLTexture, minFilter: GLenum, magFilter: GLenum): void {\n if (engine._isLost || engine._disposed || tex._disposed) {\n return;\n }\n const gl = engine.gl;\n bindTextureForUpload(engine, tex.handle);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);\n}\n\n/** Update a texture's S/T wrap modes (`gl.CLAMP_TO_EDGE` / `gl.REPEAT` /\n * `gl.MIRRORED_REPEAT`) — the lite-gl equivalent of Babylon's\n * `updateTextureWrappingMode`. No-op on a lost/disposed context. */\nexport function updateTextureWrapMode(engine: GLEngineContext, tex: GLTexture, wrapS: GLenum, wrapT: GLenum): void {\n if (engine._isLost || engine._disposed || tex._disposed) {\n return;\n }\n const gl = engine.gl;\n bindTextureForUpload(engine, tex.handle);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);\n}\n\n/**\n * Wrap an existing raw `WebGLTexture` (e.g. one created by a host renderer or a\n * previous engine) as a {@link GLTexture} — the lite-gl equivalent of\n * ShapeBuilder's `createTextureGraphicsResourceFromExternalWebGLTexture`.\n *\n * The wrapper does NOT own the underlying handle's upload — it is NOT registered\n * for context-restore replay (the external owner is responsible for that) and is\n * marked ready immediately. `disposeTexture` will still `gl.deleteTexture` it, so\n * only wrap a handle whose deletion you intend lite-gl to manage.\n *\n * @param engine - The engine.\n * @param handle - The external `WebGLTexture`.\n * @param width - Texture width in texels.\n * @param height - Texture height in texels.\n * @param options - Optional sampling/wrap to apply once (min/mag/wrapS/wrapT).\n * @returns A {@link GLTexture} wrapping the handle.\n */\nexport function createTextureFromHandle(engine: GLEngineContext, handle: WebGLTexture, width: number, height: number, options?: GLTextureOptions): GLTexture {\n const tex: GLTexture = {\n handle,\n target: engine.gl.TEXTURE_2D,\n width,\n height,\n isReady: true,\n _disposed: false,\n _refCount: 1,\n // External handle — restore replay is the external owner's job.\n _upload: () => {},\n _wasReady: true,\n };\n if (options !== undefined) {\n const gl = engine.gl;\n bindTextureForUpload(engine, handle);\n setBoundTextureParams(gl, options.minFilter ?? gl.LINEAR, options.magFilter ?? gl.LINEAR, options.wrapS ?? gl.CLAMP_TO_EDGE, options.wrapT ?? gl.CLAMP_TO_EDGE);\n }\n return tex;\n}\n\n/** Asynchronous image upload.The returned texture is immediately usable (1×1\n * transparent placeholder); `isReady` flips true once the image has been\n * decoded and uploaded. The decoded `ImageBitmap` is retained on the texture\n * for offline-safe `webglcontextrestored` replay. */\nexport function loadTexture2D(engine: GLEngineContext, url: string, options?: GLTextureOptions, onLoad?: (tex: GLTexture) => void, onError?: (err: Error) => void): GLTexture {\n const gl = engine.gl;\n const handle = gl.createTexture();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null\");\n }\n const opts = options ?? {};\n const minFilter = opts.minFilter ?? gl.LINEAR;\n const magFilter = opts.magFilter ?? gl.LINEAR;\n const wrapS = opts.wrapS ?? gl.CLAMP_TO_EDGE;\n const wrapT = opts.wrapT ?? gl.CLAMP_TO_EDGE;\n const invertY = opts.invertY ?? false;\n\n let bitmap: ImageBitmap | null = null;\n const placeholderPixels = new Uint8Array([0, 0, 0, 0]);\n\n const upload = (target: GLEngineContext): void => {\n const g = target.gl;\n // invertY is applied at decode time via createImageBitmap({ imageOrientation })\n // because UNPACK_FLIP_Y_WEBGL is IGNORED for ImageBitmap sources (and the 1×1\n // placeholder is flip-invariant), so keep flip + premultiply off here.\n setUnpackState(target, false, false);\n bindTextureForUpload(target, tex.handle);\n if (bitmap !== null) {\n g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, g.RGBA, g.UNSIGNED_BYTE, bitmap);\n tex.width = bitmap.width;\n tex.height = bitmap.height;\n } else {\n g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, 1, 1, 0, g.RGBA, g.UNSIGNED_BYTE, placeholderPixels);\n }\n };\n\n const initializeParameters = (target: GLEngineContext): void => {\n setBoundTextureParams(target.gl, minFilter, magFilter, wrapS, wrapT);\n };\n\n const tex: GLTexture = {\n handle,\n target: gl.TEXTURE_2D,\n width: 1,\n height: 1,\n isReady: false,\n _disposed: false,\n _refCount: 1,\n _upload: upload,\n _initializeParameters: initializeParameters,\n _wasReady: false,\n };\n // Placeholder upload — makes the texture sampleable before the real image arrives.\n upload(engine);\n initializeParameters(engine);\n engine._textures.add(tex);\n\n // Fetch + decode the real image. Re-uploads via the same closure once the\n // bitmap is in hand (so context-restore replay sees the real image too).\n fetch(url)\n .then((r) => {\n if (!r.ok) {\n throw new Error(`lite-gl: fetch ${url} -> HTTP ${r.status}`);\n }\n return r.blob();\n })\n .then((blob) => createImageBitmap(blob, { premultiplyAlpha: \"none\", imageOrientation: invertY ? \"flipY\" : \"none\" }))\n .then((bm) => {\n if (tex._disposed) {\n bm.close();\n return;\n }\n bitmap = bm;\n // If the context is lost mid-flight, defer the upload — the restore\n // handler will call _upload again, which will then see `bitmap !== null`\n // and replay the real image.\n if (!engine._isLost) {\n upload(engine);\n tex.isReady = true;\n }\n tex._wasReady = true;\n if (onLoad !== undefined) {\n onLoad(tex);\n }\n })\n .catch((err: unknown) => {\n const e = err instanceof Error ? err : new Error(String(err));\n if (onError !== undefined) {\n onError(e);\n } else {\n console.error(\"lite-gl: loadTexture2D failed\", e);\n }\n });\n\n return tex;\n}\n\n/** Cached bind. Skips `gl.activeTexture` and/or `gl.bindTexture` when nothing\n * changes. No-op when `tex._disposed` or `engine._isLost`. */\nexport function bindTexture(engine: GLEngineContext, unit: number, tex: GLTexture | null): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n if (tex !== null && tex._disposed) {\n return;\n }\n bindTextureRaw(engine, unit, tex === null ? null : tex.handle);\n}\n\n/** @internal — for callers that already hold a raw `WebGLTexture`. Not part of\n * the public API: `bindTexture` (public) and the texture `_upload` closures bind\n * through the cache, so consumers never call this directly. */\nexport function bindTextureRaw(engine: GLEngineContext, unit: number, handle: WebGLTexture | null): void {\n const s = engine._state;\n const gl = engine.gl;\n if (s.boundTextures[unit] === handle) {\n return;\n }\n if (s.activeTextureUnit !== unit) {\n gl.activeTexture(gl.TEXTURE0 + unit);\n s.activeTextureUnit = unit;\n }\n gl.bindTexture(gl.TEXTURE_2D, handle);\n s.boundTextures[unit] = handle;\n}\n\n/** @internal Bind `handle` on texture unit 0 **for an upload** (`texImage2D`).\n * Unlike {@link bindTextureRaw} — which elides the entire bind when the handle is\n * already bound on the unit (correct for *sampling*, where the active unit is\n * irrelevant) — an upload writes into the texture on the ACTIVE unit. A prior\n * multi-sampler draw may have left a non-zero unit active while this handle is\n * still bound on unit 0, so we must force unit 0 active even on a bind cache-hit,\n * keeping the `_state` cache coherent. */\nexport function bindTextureForUpload(engine: GLEngineContext, handle: WebGLTexture): void {\n const s = engine._state;\n const gl = engine.gl;\n if (s.activeTextureUnit !== 0) {\n gl.activeTexture(gl.TEXTURE0);\n s.activeTextureUnit = 0;\n }\n if (s.boundTextures[0] !== handle) {\n gl.bindTexture(gl.TEXTURE_2D, handle);\n s.boundTextures[0] = handle;\n }\n}\n\n/** Disposes the texture. Walks `_state.boundTextures` and clears every slot\n * that still references the handle — otherwise a later `bindTexture(unit, B)`\n * to the same unit would be wrongly elided when slot still showed handle A.\n * Deregistration is expected O(1), independent of the live texture count.\n * Shared references keep the texture registered until its final release;\n * repeated disposal is a no-op. Unregistered wrappers keep their existing\n * handle-deletion semantics without affecting the live registry. */\nexport function disposeTexture(engine: GLEngineContext, tex: GLTexture): 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.boundTextures;\n for (let u = 0; u < bound.length; u++) {\n if (bound[u] === tex.handle) {\n bound[u] = null;\n }\n }\n}\n\n/** Internal — resolve the sized internalFormat for the LDR (byte) `texImage2D`\n * paths ONLY: RGBA→RGBA8, RGB→RGB8, RG→RG8, RED→R8, LUMINANCE passthrough.\n * Deliberately knows nothing about FLOAT / HALF_FLOAT so the float-format table\n * in {@link pickSizedInternalFormat} tree-shakes out of byte-only bundles.\n * @internal */\nfunction resolveLdrInternalFormat(gl: WebGL2RenderingContext, format: GLenum, type: GLenum): GLenum {\n if (type === gl.UNSIGNED_BYTE) {\n if (format === gl.RGBA) {\n return gl.RGBA8;\n }\n if (format === gl.RGB) {\n return gl.RGB8;\n }\n if (format === gl.RG) {\n return gl.RG8;\n }\n if (format === gl.RED) {\n return gl.R8;\n }\n if (format === gl.LUMINANCE) {\n return gl.LUMINANCE;\n }\n }\n return format;\n}\n\n/** Internal — pick a sized internalFormat for `texImage2D`. WebGL2 prefers\n * sized formats for non-color-renderable / non-readback paths; for the\n * NeonBrush use cases (sample-only) the unsized format works too, but sized\n * is more portable. Shared with the render-target module.\n * @internal */\nexport function pickSizedInternalFormat(gl: WebGL2RenderingContext, format: GLenum, type: GLenum): GLenum {\n if (type === gl.UNSIGNED_BYTE) {\n if (format === gl.RGBA) {\n return gl.RGBA8;\n }\n if (format === gl.RGB) {\n return gl.RGB8;\n }\n if (format === gl.LUMINANCE) {\n return gl.LUMINANCE;\n }\n }\n if (type === gl.FLOAT) {\n if (format === gl.RGBA) {\n return gl.RGBA32F;\n }\n if (format === gl.RGB) {\n return gl.RGB32F;\n }\n }\n if (type === gl.HALF_FLOAT) {\n if (format === gl.RGBA) {\n return gl.RGBA16F;\n }\n if (format === gl.RGB) {\n return gl.RGB16F;\n }\n }\n return format;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"texture.js","sourceRoot":"","sources":["../src/texture.ts"],"names":[],"mappings":"AAEA;;;gFAGgF;AAChF,MAAM,CAAC,MAAM,cAAc,GAAG;IAC1B;2CACuC;IACvC,OAAO,EAAE,CAAC;IACV;4CACwC;IACxC,QAAQ,EAAE,CAAC;IACX;6CACyC;IACzC,SAAS,EAAE,CAAC;CACN,CAAC;AA6IX;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,MAAuB,EAAE,KAAc,EAAE,gBAAyB,EAAE,SAAS,GAAG,CAAC;IAC5G,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;IACrB,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;I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AC1B,OAAO,EAAE,CAAC,SAAS,CAAC;QACxB,CAAC;IACL,CAAC;IACD,IAAI,IAAI,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC;QACpB,IAAI,MAAM,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC;YACrB,OAAO,EAAE,CAAC,OAAO,CAAC;QACtB,CAAC;QACD,IAAI,MAAM,KAAK,EAAE,CAAC,GAAG,EAAE,CAAC;YACpB,OAAO,EAAE,CAAC,MAAM,CAAC;QACrB,CAAC;IACL,CAAC;IACD,IAAI,IAAI,KAAK,EAAE,CAAC,UAAU,EAAE,CAAC;QACzB,IAAI,MAAM,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC;YACrB,OAAO,EAAE,CAAC,OAAO,CAAC;QACtB,CAAC;QACD,IAAI,MAAM,KAAK,EAAE,CAAC,GAAG,EAAE,CAAC;YACpB,OAAO,EAAE,CAAC,MAAM,CAAC;QACrB,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC","sourcesContent":["import type { GLEngineContext } from \"./context.js\";\n\n/** High-level sampling presets, mirroring Babylon's `Texture.*_SAMPLINGMODE`\n * numeric constants. Resolve one to GL min/mag filters with\n * {@link getSamplingModeFilters} (≙ Babylon's `_getSamplingParameters`); only\n * `TRILINEAR` samples the mip chain, and only when the texture has mipmaps. */\nexport const GLSamplingMode = {\n /** Nearest mag; nearest min, even with mipmaps.\n * (`Texture.NEAREST_SAMPLINGMODE`) */\n NEAREST: 1,\n /** Linear mag; linear min, even with mipmaps.\n * (`Texture.BILINEAR_SAMPLINGMODE`) */\n BILINEAR: 2,\n /** Linear mag; linear min (`LINEAR_MIPMAP_LINEAR` with mipmaps).\n * (`Texture.TRILINEAR_SAMPLINGMODE`) */\n TRILINEAR: 3,\n} as const;\n\n/** One of the {@link GLSamplingMode} preset values (`1`, `2` or `3`). */\nexport type GLSamplingMode = (typeof GLSamplingMode)[keyof typeof GLSamplingMode];\n\n/** Creation options shared by EVERY texture factory (`createRawTexture`,\n * `createFloatTexture`, `loadTexture2D`, `createTextureFromSource`,\n * `createDynamicTexture`, `createHtmlElementTexture`,\n * `createTextureFromHandle`). All have GL-spec defaults.\n *\n * Babylon-style sampling presets are opt-in so factories don't ship them:\n * spread {@link getSamplingModeFilters} into the options. Mipmaps are not a\n * creation option either: call {@link generateTextureMipMaps} once, after\n * which every later upload and `webglcontextrestored` replay rebuilds the\n * chain. */\nexport interface GLTextureOptions {\n /** Flip rows vertically on upload. Default: false (matches Babylon's\n * default raw-texture behaviour). For raw / dynamic / HTML-element textures\n * this is the default for every update that omits `invertY`. */\n invertY?: boolean;\n /** Default: gl.LINEAR. See {@link getSamplingModeFilters} for presets. */\n minFilter?: GLenum;\n /** Default: gl.LINEAR. */\n magFilter?: GLenum;\n /** Default: gl.CLAMP_TO_EDGE. May be `gl.REPEAT` / `gl.MIRRORED_REPEAT`\n * (NPOT-safe in WebGL2). */\n wrapS?: GLenum;\n /** Default: gl.CLAMP_TO_EDGE. May be `gl.REPEAT` / `gl.MIRRORED_REPEAT`. */\n wrapT?: GLenum;\n /** `gl.pixelStorei(UNPACK_ALIGNMENT)` for the upload (1/2/4/8). Default 4.\n * Use 1 for tightly-packed non-RGBA rows. */\n unpackAlignment?: number;\n /** Premultiply alpha at upload (`UNPACK_PREMULTIPLY_ALPHA_WEBGL`, or\n * decode-time for `loadTexture2D`). Default false. For raw / dynamic /\n * HTML-element textures this is the default for every update that omits it. */\n premultiplyAlpha?: boolean;\n /** Explicit sized internalFormat for `texImage2D`. When provided it is used\n * verbatim and the inline LDR resolver is bypassed — this is how\n * {@link createFloatTexture} injects its `RGBA16F` / `RGBA32F` choice\n * without the byte path ever referencing the float-format table (keeping it\n * tree-shakeable out of RGBA8-only bundles). Default: derived from\n * `format`/`type` via the LDR resolver. */\n internalFormat?: GLenum;\n}\n\n/** Per-update pixel-store flags shared by `updateRawTexture`,\n * `updateDynamicTexture` and `updateHtmlElementTexture` (≙ lite's\n * `DynamicTextureUpdateOptions`, Babylon's `updateRawTexture(…, invertY)`). Each\n * omitted flag falls back to the texture's creation {@link GLTextureOptions}\n * value; the flags of the latest update are reused on `webglcontextrestored`. */\nexport interface GLTextureUpdateOptions {\n /** Flip rows vertically on upload (`UNPACK_FLIP_Y_WEBGL`). */\n invertY?: boolean;\n /** Premultiply alpha on upload (`UNPACK_PREMULTIPLY_ALPHA_WEBGL`). */\n premultiplyAlpha?: boolean;\n}\n\n/**\n * Pure-state texture handle. The `handle` field is MUTABLE so the same logical\n * texture survives a `webglcontextrestored` event — every consumer keeps the\n * same `GLTexture` reference; only the internal `WebGLTexture` is swapped.\n *\n * `loadTexture2D` also uses the same handle for the 1×1 placeholder upload AND\n * the final image upload — so a `bindTexture(tex, unit)` made before the\n * image has decoded remains valid once the image arrives.\n */\nexport interface GLTexture {\n /** The live `WebGLTexture`. MUTABLE — swapped for a fresh handle on\n * `webglcontextrestored` while consumers keep the same `GLTexture` reference. */\n handle: WebGLTexture;\n /** GL texture target (normally `gl.TEXTURE_2D`); 3D textures use their own API. */\n readonly target: GLenum;\n /** Texture width in texels. Updated once an async upload resolves. */\n width: number;\n /** Texture height in texels. Updated once an async upload resolves. */\n height: number;\n /** True when the texture is safe to sample with final content (placeholders\n * read as not-ready until their image/upload completes). */\n isReady: boolean;\n /** The engine that created (and owns) this texture. Texture operations\n * derive their engine from it, so they take the texture alone. @internal */\n _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n /** @internal */\n _refCount: number;\n /** Replays the latest pixel content into the currently bound texture handle. @internal */\n _upload: (engine: GLEngineContext) => void;\n /** Initializes creation-time sampling state on the bound texture handle. @internal */\n _initializeParameters?: (engine: GLEngineContext) => void;\n /**\n * Snapshot of `isReady` captured on `webglcontextlost` so the restore\n * handler knows whether to flip it back on after `_upload`. Textures\n * that were still mid-load (e.g. `loadTexture2D` whose bitmap hadn't\n * arrived) stay not-ready; the async path will set `isReady=true` once\n * the bitmap finishes decoding into the new handle.\n * @internal\n */\n _wasReady: boolean;\n /**\n * Live re-upload hook installed by `createRawTexture` — lets\n * `updateRawTexture` replace the pixel data (and optionally the size)\n * through the same closure that the context-restore replay uses, keeping\n * restore correct. Absent on image / HTML-element / external textures.\n * @internal\n */\n _updateRaw?: (engine: GLEngineContext, data: ArrayBufferView | null, width: number, height: number, unpackAlignment: number) => void;\n /**\n * Dynamic-texture source captured by `updateDynamicTexture`\n * (`@babylonjs/lite-gl`) and replayed into the fresh handle\n * on `webglcontextrestored`. Null/absent on non-dynamic textures.\n * @internal\n */\n _dynSource?: TexImageSource | null;\n /** `invertY` of the latest raw / dynamic / HTML-element update\n * ({@link GLTextureUpdateOptions}), reused by context-restore replay;\n * `undefined` → the texture's creation `invertY`. @internal */\n _invertY?: boolean;\n /** `premultiplyAlpha` of the latest raw / dynamic / HTML-element update\n * ({@link GLTextureUpdateOptions}), reused by context-restore replay;\n * `undefined` → the texture's creation `premultiplyAlpha`. @internal */\n _premultiplyAlpha?: boolean;\n /**\n * The `handle` the dynamic texture's `texParameteri` filter/wrap state was\n * last applied to. `texParameteri` is per-texture GL state that survives pixel\n * re-uploads, so the dynamic-texture `_upload` re-applies it ONLY when this no\n * longer matches `handle` — i.e. at creation and after `webglcontextrestored`\n * installs a fresh handle — not on every per-frame `updateDynamicTexture`.\n * @internal\n */\n _dynParamsHandle?: WebGLTexture | null;\n /**\n * Set by {@link generateTextureMipMaps} (≙ Babylon\n * `InternalTexture.useMipMaps`, and the 3D API's `_hasMipMaps`). The mip\n * intent is retained: `_upload` is wrapped once so every later level-0\n * upload, including `webglcontextrestored` replay, rebuilds the chain.\n * @internal\n */\n _hasMipMaps?: boolean;\n}\n\n/**\n * Apply the three `UNPACK_*` pixel-store flags through the GL-state cache,\n * eliding redundant `pixelStorei` calls. Centralising this guarantees EVERY\n * upload path sets all three explicitly, so a premultiplying / flipping upload\n * never leaks its flag into a later upload that forgot to set it.\n * @internal\n */\nexport function setUnpackState(engine: GLEngineContext, flipY: boolean, premultiplyAlpha: boolean, alignment = 4): void {\n const gl = engine.gl;\n const s = engine._state;\n const fy = flipY ? 1 : 0;\n if (s.unpackFlipY !== fy) {\n gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, fy);\n s.unpackFlipY = fy;\n }\n const pm = premultiplyAlpha ? 1 : 0;\n if (s.unpackPremultiplyAlpha !== pm) {\n gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, pm);\n s.unpackPremultiplyAlpha = pm;\n }\n if (s.unpackAlignment !== alignment) {\n gl.pixelStorei(gl.UNPACK_ALIGNMENT, alignment);\n s.unpackAlignment = alignment;\n }\n}\n\n/** Apply sampling and wrap state to the texture currently bound for upload. @internal */\nexport function setBoundTextureParams(gl: WebGL2RenderingContext, minFilter: GLenum, magFilter: GLenum, wrapS: GLenum, wrapT: GLenum): void {\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);\n}\n\n/** GL min/mag filters resolved from a {@link GLSamplingMode} preset. Spread\n * into any {@link GLTextureOptions}, or pass to {@link updateTextureSamplingMode}\n * (or use {@link updateTextureSamplingPreset} directly). */\nexport interface GLSamplingFilters {\n /** `TEXTURE_MIN_FILTER` value. */\n minFilter: GLenum;\n /** `TEXTURE_MAG_FILTER` value. */\n magFilter: GLenum;\n}\n\n/**\n * Resolve a Babylon-style sampling preset to GL min/mag filters — the lite-gl\n * counterpart of Babylon's `_getSamplingParameters(samplingMode, generateMipMaps)`.\n * Pure (no GL context needed; WebGL filter enums are spec constants) and\n * tree-shaken unless imported, so preset-free bundles ship none of it.\n *\n * | Preset | min (no mips) | min (mips) | mag |\n * | ----------- | ------------- | ---------------------- | --------- |\n * | `NEAREST` | `NEAREST` | `NEAREST` | `NEAREST` |\n * | `BILINEAR` | `LINEAR` | `LINEAR` | `LINEAR` |\n * | `TRILINEAR` | `LINEAR` | `LINEAR_MIPMAP_LINEAR` | `LINEAR` |\n *\n * Babylon's mipmapped nearest / bilinear variants are separate modes\n * (`NEAREST_LINEAR_MIPNEAREST` …); pass their raw filters instead.\n *\n * @example\n * ```ts\n * const tex = createRawTexture(engine, data, w, h, getSamplingModeFilters(GLSamplingMode.TRILINEAR, true));\n * generateTextureMipMaps(tex); // required by the mipmapped min filter\n * ```\n *\n * @param samplingMode - The {@link GLSamplingMode} preset.\n * @param useMipMaps - Whether the texture has (or will get, via\n * {@link generateTextureMipMaps}) a mip chain. Default false, so the min\n * filter never references a missing chain.\n * @returns The preset's `{ minFilter, magFilter }`.\n */\nexport function getSamplingModeFilters(samplingMode: GLSamplingMode, useMipMaps = false): GLSamplingFilters {\n // WebGL filter enums: NEAREST 0x2600, LINEAR 0x2601, LINEAR_MIPMAP_LINEAR 0x2703.\n const magFilter = samplingMode === GLSamplingMode.NEAREST ? 0x2600 : 0x2601;\n return { minFilter: useMipMaps && samplingMode === GLSamplingMode.TRILINEAR ? 0x2703 : magFilter, magFilter };\n}\n\n/** Size-bearing fields across the `TexImageSource` variants. @internal */\ninterface TexImageSourceSize {\n width?: number;\n height?: number;\n videoWidth?: number;\n videoHeight?: number;\n naturalWidth?: number;\n naturalHeight?: number;\n displayWidth?: number;\n displayHeight?: number;\n}\n\n/** Write the pixel size of a `TexImageSource` as `texImage2D` will upload it\n * into `tex.width` / `tex.height`: the intrinsic size for video\n * (`videoWidth`), image (`naturalWidth`) and `VideoFrame` (`displayWidth`)\n * sources, `width`/`height` otherwise. Clamped to ≥ 1. Allocation-free — it\n * runs on every dynamic / HTML-element re-upload. @internal */\nexport function setTexImageSourceSize(tex: GLTexture, source: TexImageSourceSize): void {\n tex.width = (source.videoWidth ?? source.naturalWidth ?? source.displayWidth ?? source.width) || 1;\n tex.height = (source.videoHeight ?? source.naturalHeight ?? source.displayHeight ?? source.height) || 1;\n}\n\n/** Options for {@link createRawTexture}: the shared {@link GLTextureOptions}\n * plus the pixel `format` / `type` of `data`. */\nexport interface GLRawTextureOptions extends GLTextureOptions {\n /** Pixel format of `data` (`gl.RGBA`, `gl.RGB`, `gl.RG`, `gl.RED`,\n * `gl.LUMINANCE`…). Default `gl.RGBA`. */\n format?: GLenum;\n /** Pixel type of `data`. Default `gl.UNSIGNED_BYTE`. For float data, prefer\n * {@link createFloatTexture}, which also picks the sized float internalFormat. */\n type?: GLenum;\n}\n\n/**\n * Create a raw texture from a typed array (or an uninitialised allocation).\n * RGBA8 by default; set {@link GLRawTextureOptions.format} / `type` for other\n * layouts. The pixel data can be replaced later via {@link updateRawTexture};\n * the sampling and wrap can be changed via {@link updateTextureSamplingMode} /\n * {@link updateTextureWrapMode}.\n *\n * @param engine - The engine.\n * @param data - Initial pixels matching `format` / `type`, or `null` for an\n * uninitialised allocation.\n * @param width - Texture width in texels.\n * @param height - Texture height in texels.\n * @param options - See {@link GLRawTextureOptions}.\n * @returns The new {@link GLTexture}.\n */\nexport function createRawTexture(engine: GLEngineContext, data: ArrayBufferView | null, width: number, height: number, options?: GLRawTextureOptions): GLTexture {\n const gl = engine.gl;\n const handle = gl.createTexture();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null\");\n }\n const opts = options ?? {};\n const format = opts.format ?? gl.RGBA;\n const type = opts.type ?? gl.UNSIGNED_BYTE;\n const minFilter = opts.minFilter ?? gl.LINEAR;\n const magFilter = opts.magFilter ?? gl.LINEAR;\n const wrapS = opts.wrapS ?? gl.CLAMP_TO_EDGE;\n const wrapT = opts.wrapT ?? gl.CLAMP_TO_EDGE;\n const invertY = opts.invertY ?? false;\n const premultiply = opts.premultiplyAlpha ?? false;\n // LDR byte formats are resolved INLINE here; the float-format table in\n // `pickSizedInternalFormat` is deliberately NOT referenced from this path so\n // it tree-shakes out of byte-only bundles. `createFloatTexture` injects its\n // sized float format via `opts.internalFormat`.\n const internalFormat = opts.internalFormat ?? resolveLdrInternalFormat(gl, format, type);\n\n // Mutable upload state — `updateRawTexture` mutates these and re-runs\n // `upload`, so the context-restore replay (which calls the same `upload`)\n // always reproduces the latest contents.\n let curData = data;\n let curWidth = width;\n let curHeight = height;\n let curAlign = opts.unpackAlignment ?? 4;\n\n const upload = (target: GLEngineContext): void => {\n const g = target.gl;\n setUnpackState(target, tex._invertY ?? invertY, tex._premultiplyAlpha ?? premultiply, curAlign);\n bindTextureForUpload(target, tex.handle);\n g.texImage2D(g.TEXTURE_2D, 0, internalFormat, curWidth, curHeight, 0, format, type, curData);\n };\n\n const initializeParameters = (target: GLEngineContext): void => {\n setBoundTextureParams(target.gl, minFilter, magFilter, wrapS, wrapT);\n };\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: upload,\n _initializeParameters: initializeParameters,\n _wasReady: true,\n };\n tex._updateRaw = (target: GLEngineContext, newData: ArrayBufferView | null, newWidth: number, newHeight: number, unpackAlignment: number): void => {\n curData = newData;\n curWidth = newWidth;\n curHeight = newHeight;\n curAlign = unpackAlignment;\n tex.width = newWidth;\n tex.height = newHeight;\n upload(target);\n };\n upload(engine);\n initializeParameters(engine);\n engine._textures.add(tex);\n return tex;\n}\n\n/** Options for {@link createFloatTexture} — the shared {@link GLTextureOptions}\n * plus the float-specific `type` / `format`. */\nexport interface GLFloatTextureOptions extends GLTextureOptions {\n /** Float texel type — `gl.HALF_FLOAT` (default) or `gl.FLOAT`. */\n type?: GLenum;\n /** Color format. Default `gl.RGBA`. */\n format?: GLenum;\n}\n\n/**\n * Create a float / half-float raw texture — the HDR counterpart of\n * {@link createRawTexture}. This is the ONLY texture factory that carries the\n * `RGBA16F` / `RGBA32F` sized-format knowledge (via {@link pickSizedInternalFormat}),\n * so byte-only consumers calling {@link createRawTexture} ship none of it.\n * Defaults to `gl.HALF_FLOAT`; pass `options.type = gl.FLOAT` for full 32-bit.\n * The caller is responsible for the matching engine cap (e.g.\n * `caps.textureFloatLinearFiltering`) when sampling these with `LINEAR`.\n *\n * @param engine - The engine.\n * @param data - Initial pixels (`Float32Array` for FLOAT, `Uint16Array` for\n * HALF_FLOAT), or `null` for an uninitialised allocation.\n * @param width - Texture width in texels (≥ 1).\n * @param height - Texture height in texels (≥ 1).\n * @param options - See {@link GLFloatTextureOptions}.\n * @returns The new {@link GLTexture}.\n */\nexport function createFloatTexture(engine: GLEngineContext, data: ArrayBufferView | null, width: number, height: number, options?: GLFloatTextureOptions): GLTexture {\n const gl = engine.gl;\n const o = options ?? {};\n const format = o.format ?? gl.RGBA;\n const type = o.type ?? gl.HALF_FLOAT;\n // Precompute the sized float internalFormat HERE and pass it through, so\n // `createRawTexture`'s inline LDR resolver is bypassed and the float table\n // stays out of byte-only bundles.\n const internalFormat = pickSizedInternalFormat(gl, format, type);\n return createRawTexture(engine, data, width, height, { ...o, format, type, internalFormat });\n}\n\n/**\n * Generate the mip chain for a texture from its level-0 contents and keep it\n * current (≙ Babylon's `useMipMaps` / lite-gl's `generateTexture3DMipMaps`).\n * The intent is retained: every later level-0 upload — `updateRawTexture`,\n * `updateDynamicTexture`, `updateHtmlElementTexture`, the `loadTexture2D` image\n * arrival and `webglcontextrestored` replay — rebuilds the chain, so a\n * mipmapped min filter never samples an incomplete texture. Only bundles that\n * call it pay for mipmapping. Calling it again just regenerates now.\n *\n * Binds for upload (unit 0). The format must be filterable (float textures need\n * the matching linear-filtering cap). Render targets use\n * `generateRenderTargetMipMaps` instead. No-op on a lost/disposed context or a\n * disposed texture.\n *\n * @param tex - The texture whose mip chain to generate.\n */\nexport function generateTextureMipMaps(tex: GLTexture): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed || tex.handle === null) {\n return;\n }\n bindTextureForUpload(engine, tex.handle);\n engine.gl.generateMipmap(engine.gl.TEXTURE_2D);\n if (!tex._hasMipMaps) {\n tex._hasMipMaps = true;\n // Every `_upload` that runs (update / restore-registry replay) leaves\n // this texture bound for upload; handle/RT textures are never replayed.\n const upload = tex._upload;\n tex._upload = (target: GLEngineContext): void => {\n upload(target);\n target.gl.generateMipmap(target.gl.TEXTURE_2D);\n };\n }\n}\n\n/**\n * Re-upload the pixel data (and optionally resize) of a texture created by\n * {@link createRawTexture} — the lite-gl equivalent of Babylon's\n * `updateRawTexture` / `_uploadDataToTextureDirectly`. Goes through the same\n * upload closure used by context-restore, so the new contents survive a context\n * loss, and rebuilds the mip chain after {@link generateTextureMipMaps}. No-op\n * on a lost/disposed/non-raw texture.\n *\n * @param tex - The raw texture to update.\n * @param data - New pixel data (must match the original `format`/`type`).\n * @param options - Optional new `width`/`height` (default: unchanged),\n * `unpackAlignment` (default 4) and `invertY` / `premultiplyAlpha` (default:\n * the creation values; ≙ Babylon's `updateRawTexture(…, invertY)`). The flags\n * are reused by context-restore replay.\n */\nexport function updateRawTexture(\n tex: GLTexture,\n data: ArrayBufferView | null,\n options?: GLTextureUpdateOptions & { width?: number; height?: number; unpackAlignment?: number }\n): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed || tex._updateRaw === undefined) {\n return;\n }\n const o = options ?? {};\n tex._invertY = o.invertY;\n tex._premultiplyAlpha = o.premultiplyAlpha;\n tex._updateRaw(engine, data, o.width ?? tex.width, o.height ?? tex.height, o.unpackAlignment ?? 4);\n if (tex._hasMipMaps) {\n // `_updateRaw` leaves the texture bound for upload (unit 0).\n engine.gl.generateMipmap(engine.gl.TEXTURE_2D);\n }\n}\n\n/**\n * Update a texture's raw GL min/mag sampling filters (e.g. `gl.NEAREST`,\n * `gl.LINEAR_MIPMAP_LINEAR`). A mipmapped min filter needs\n * {@link generateTextureMipMaps}; for Babylon-style presets use\n * {@link updateTextureSamplingPreset}. Binds for upload (unit 0) so the\n * `texParameteri` lands on this texture. The change is not replayed on\n * `webglcontextrestored` (creation options are). No-op on a lost/disposed\n * context or a disposed texture.\n *\n * @param tex - The texture to update.\n * @param minFilter - `TEXTURE_MIN_FILTER` value.\n * @param magFilter - `TEXTURE_MAG_FILTER` value.\n */\nexport function updateTextureSamplingMode(tex: GLTexture, minFilter: GLenum, magFilter: GLenum): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed) {\n return;\n }\n const gl = engine.gl;\n bindTextureForUpload(engine, tex.handle);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);\n}\n\n/**\n * Update a texture's sampling mode from a Babylon-style preset — the lite-gl\n * equivalent of Babylon's\n * `updateTextureSamplingMode(samplingMode, texture, generateMipMaps)`. The\n * filters come from {@link getSamplingModeFilters}, using the mipmapped\n * `TRILINEAR` min filter when the texture already has mipmaps (from\n * {@link generateTextureMipMaps}) or `generateMipMaps` is true. Like Babylon,\n * `generateMipMaps` only generates (and retains) the chain for `TRILINEAR`,\n * right after the filters are set; `NEAREST` / `BILINEAR` never sample mips.\n * Unlike Babylon, an existing chain is not regenerated (lite-gl already\n * rebuilds it on every upload); call {@link generateTextureMipMaps} to force it.\n *\n * Kept separate from the raw {@link updateTextureSamplingMode} so raw-filter\n * callers don't bundle the preset and mipmap code. Same binding, replay and\n * lost/disposed behavior.\n *\n * @param tex - The texture to update.\n * @param samplingMode - The {@link GLSamplingMode} preset.\n * @param generateMipMaps - Generate the mip chain now (`TRILINEAR` only). Default false.\n */\nexport function updateTextureSamplingPreset(tex: GLTexture, samplingMode: GLSamplingMode, generateMipMaps?: boolean): void {\n // ≙ Babylon: mips are generated only for a mipmapped preset (`TRILINEAR`).\n const generate = generateMipMaps === true && samplingMode === GLSamplingMode.TRILINEAR;\n const filters = getSamplingModeFilters(samplingMode, generate || tex._hasMipMaps === true);\n updateTextureSamplingMode(tex, filters.minFilter, filters.magFilter);\n // An existing chain is already rebuilt on every upload; call\n // `generateTextureMipMaps` directly to force a regeneration.\n if (generate && !tex._hasMipMaps) {\n generateTextureMipMaps(tex);\n }\n}\n\n/**\n * Whether a texture holds its final content and can be sampled — the function\n * form of {@link GLTexture.isReady}, mirroring `isEffectReady(effect)`\n * (≙ Babylon's `texture.isReady()`). False while `loadTexture2D` is still\n * decoding (the 1×1 placeholder is bound meanwhile), after disposal, and while\n * the context is lost/disposed.\n *\n * @param tex - The texture to query.\n * @returns `true` when the texture is ready to sample.\n */\nexport function isTextureReady(tex: GLTexture): boolean {\n const engine = tex._engine;\n return tex.isReady && !tex._disposed && !engine._isLost && !engine._disposed;\n}\n\n/** Update a texture's S/T wrap modes (`gl.CLAMP_TO_EDGE` / `gl.REPEAT` /\n * `gl.MIRRORED_REPEAT`) — the lite-gl equivalent of Babylon's\n * `updateTextureWrappingMode`. No-op on a lost/disposed context. */\nexport function updateTextureWrapMode(tex: GLTexture, wrapS: GLenum, wrapT: GLenum): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed) {\n return;\n }\n const gl = engine.gl;\n bindTextureForUpload(engine, tex.handle);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);\n}\n\n/**\n * Wrap an existing raw `WebGLTexture` (e.g. one created by a host renderer or a\n * previous engine) as a {@link GLTexture} — the lite-gl equivalent of\n * ShapeBuilder's `createTextureGraphicsResourceFromExternalWebGLTexture`.\n *\n * The wrapper does NOT own the underlying handle's upload — it is NOT registered\n * for context-restore replay (the external owner is responsible for that) and is\n * marked ready immediately. `disposeTexture` will still `gl.deleteTexture` it, so\n * only wrap a handle whose deletion you intend lite-gl to manage.\n *\n * @param engine - The engine.\n * @param handle - The external `WebGLTexture`.\n * @param width - Texture width in texels.\n * @param height - Texture height in texels.\n * @param options - Optional sampling state to apply once (min/mag/wrapS/wrapT).\n * Upload-only options (`invertY`, `premultiplyAlpha`, `unpackAlignment`,\n * `internalFormat`) do not apply — the handle's pixels are owned externally.\n * @returns A {@link GLTexture} wrapping the handle.\n */\nexport function createTextureFromHandle(engine: GLEngineContext, handle: WebGLTexture, width: number, height: number, options?: GLTextureOptions): GLTexture {\n const tex: GLTexture = {\n handle,\n target: engine.gl.TEXTURE_2D,\n width,\n height,\n isReady: true,\n _engine: engine,\n _disposed: false,\n _refCount: 1,\n // External handle — restore replay is the external owner's job.\n _upload: () => {},\n _wasReady: true,\n };\n if (options !== undefined) {\n const gl = engine.gl;\n bindTextureForUpload(engine, handle);\n setBoundTextureParams(gl, options.minFilter ?? gl.LINEAR, options.magFilter ?? gl.LINEAR, options.wrapS ?? gl.CLAMP_TO_EDGE, options.wrapT ?? gl.CLAMP_TO_EDGE);\n }\n return tex;\n}\n\n/** Asynchronous image upload.The returned texture is immediately usable (1×1\n * transparent placeholder); `isReady` flips true once the image has been\n * decoded and uploaded. The decoded `ImageBitmap` is retained on the texture\n * for offline-safe `webglcontextrestored` replay. `invertY` and\n * `premultiplyAlpha` are applied at decode time (`createImageBitmap`'s\n * `imageOrientation` / `premultiplyAlpha`) because the `UNPACK_*` flags are\n * ignored for `ImageBitmap` sources. */\nexport function loadTexture2D(engine: GLEngineContext, url: string, options?: GLTextureOptions, onLoad?: (tex: GLTexture) => void, onError?: (err: Error) => void): GLTexture {\n const gl = engine.gl;\n const handle = gl.createTexture();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createTexture returned null\");\n }\n const opts = options ?? {};\n const minFilter = opts.minFilter ?? gl.LINEAR;\n const magFilter = opts.magFilter ?? gl.LINEAR;\n const wrapS = opts.wrapS ?? gl.CLAMP_TO_EDGE;\n const wrapT = opts.wrapT ?? gl.CLAMP_TO_EDGE;\n const invertY = opts.invertY ?? false;\n const premultiplyAlpha = opts.premultiplyAlpha ?? false;\n\n let bitmap: ImageBitmap | null = null;\n const placeholderPixels = new Uint8Array([0, 0, 0, 0]);\n\n const upload = (target: GLEngineContext): void => {\n const g = target.gl;\n // invertY / premultiplyAlpha are applied at decode time via createImageBitmap\n // because UNPACK_FLIP_Y_WEBGL / UNPACK_PREMULTIPLY_ALPHA_WEBGL are IGNORED for\n // ImageBitmap sources (and the transparent 1×1 placeholder is invariant to\n // both), so keep flip + premultiply off here.\n setUnpackState(target, false, false);\n bindTextureForUpload(target, tex.handle);\n if (bitmap !== null) {\n g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, g.RGBA, g.UNSIGNED_BYTE, bitmap);\n tex.width = bitmap.width;\n tex.height = bitmap.height;\n } else {\n g.texImage2D(g.TEXTURE_2D, 0, g.RGBA, 1, 1, 0, g.RGBA, g.UNSIGNED_BYTE, placeholderPixels);\n }\n };\n\n const initializeParameters = (target: GLEngineContext): void => {\n setBoundTextureParams(target.gl, minFilter, magFilter, wrapS, wrapT);\n };\n\n const tex: GLTexture = {\n handle,\n target: gl.TEXTURE_2D,\n width: 1,\n height: 1,\n isReady: false,\n _engine: engine,\n _disposed: false,\n _refCount: 1,\n _upload: upload,\n _initializeParameters: initializeParameters,\n _wasReady: false,\n };\n // Placeholder upload — makes the texture sampleable before the real image arrives.\n upload(engine);\n initializeParameters(engine);\n engine._textures.add(tex);\n\n // Fetch + decode the real image. Re-uploads via the same closure once the\n // bitmap is in hand (so context-restore replay sees the real image too).\n fetch(url)\n .then((r) => {\n if (!r.ok) {\n throw new Error(`lite-gl: fetch ${url} -> HTTP ${r.status}`);\n }\n return r.blob();\n })\n .then((blob) => createImageBitmap(blob, { premultiplyAlpha: premultiplyAlpha ? \"premultiply\" : \"none\", imageOrientation: invertY ? \"flipY\" : \"none\" }))\n .then((bm) => {\n if (tex._disposed) {\n bm.close();\n return;\n }\n bitmap = bm;\n // If the context is lost mid-flight, defer the upload — the restore\n // handler will call _upload again, which will then see `bitmap !== null`\n // and replay the real image.\n if (!engine._isLost) {\n // Through `tex._upload` so a retained mip chain is rebuilt.\n tex._upload(engine);\n tex.isReady = true;\n }\n tex._wasReady = true;\n if (onLoad !== undefined) {\n onLoad(tex);\n }\n })\n .catch((err: unknown) => {\n const e = err instanceof Error ? err : new Error(String(err));\n if (onError !== undefined) {\n onError(e);\n } else {\n console.error(\"lite-gl: loadTexture2D failed\", e);\n }\n });\n\n return tex;\n}\n\n/** Cached bind of `tex` on sampler `unit`. Skips `gl.activeTexture` and/or\n * `gl.bindTexture` when nothing changes. No-op when `tex._disposed` or the\n * context is lost/disposed. Use {@link unbindTexture} to clear a unit. */\nexport function bindTexture(tex: GLTexture, unit: number): void {\n const engine = tex._engine;\n if (engine._isLost || engine._disposed || tex._disposed) {\n return;\n }\n bindTextureRaw(engine, unit, tex.handle);\n}\n\n/** Unbind `TEXTURE_2D` on sampler `unit` (cached, like {@link bindTexture}).\n * No-op on a lost/disposed context. */\nexport function unbindTexture(engine: GLEngineContext, unit: number): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n bindTextureRaw(engine, unit, null);\n}\n\n/** @internal — for callers that already hold a raw `WebGLTexture`. Not part of\n * the public API: `bindTexture` (public) and the texture `_upload` closures bind\n * through the cache, so consumers never call this directly. */\nexport function bindTextureRaw(engine: GLEngineContext, unit: number, handle: WebGLTexture | null): void {\n const s = engine._state;\n const gl = engine.gl;\n if (s.boundTextures[unit] === handle) {\n return;\n }\n if (s.activeTextureUnit !== unit) {\n gl.activeTexture(gl.TEXTURE0 + unit);\n s.activeTextureUnit = unit;\n }\n gl.bindTexture(gl.TEXTURE_2D, handle);\n s.boundTextures[unit] = handle;\n}\n\n/** @internal Bind `handle` on texture unit 0 **for an upload** (`texImage2D`).\n * Unlike {@link bindTextureRaw} — which elides the entire bind when the handle is\n * already bound on the unit (correct for *sampling*, where the active unit is\n * irrelevant) — an upload writes into the texture on the ACTIVE unit. A prior\n * multi-sampler draw may have left a non-zero unit active while this handle is\n * still bound on unit 0, so we must force unit 0 active even on a bind cache-hit,\n * keeping the `_state` cache coherent. */\nexport function bindTextureForUpload(engine: GLEngineContext, handle: WebGLTexture): void {\n const s = engine._state;\n const gl = engine.gl;\n if (s.activeTextureUnit !== 0) {\n gl.activeTexture(gl.TEXTURE0);\n s.activeTextureUnit = 0;\n }\n if (s.boundTextures[0] !== handle) {\n gl.bindTexture(gl.TEXTURE_2D, handle);\n s.boundTextures[0] = handle;\n }\n}\n\n/** Disposes the texture. Walks `_state.boundTextures` and clears every slot\n * that still references the handle — otherwise a later `bindTexture(unit, B)`\n * to the same unit would be wrongly elided when slot still showed handle A.\n * Deregistration is expected O(1), independent of the live texture count.\n * Shared references keep the texture registered until its final release;\n * repeated disposal is a no-op. Unregistered wrappers keep their existing\n * handle-deletion semantics without affecting the live registry. */\nexport function disposeTexture(tex: GLTexture): void {\n if (tex._disposed) {\n return;\n }\n const engine = tex._engine;\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.boundTextures;\n for (let u = 0; u < bound.length; u++) {\n if (bound[u] === tex.handle) {\n bound[u] = null;\n }\n }\n}\n\n/** Internal — resolve the sized internalFormat for the LDR (byte) `texImage2D`\n * paths ONLY: RGBA→RGBA8, RGB→RGB8, RG→RG8, RED→R8, LUMINANCE passthrough.\n * Deliberately knows nothing about FLOAT / HALF_FLOAT so the float-format table\n * in {@link pickSizedInternalFormat} tree-shakes out of byte-only bundles.\n * Shared with the render-target module (which validates the format first).\n * @internal */\nexport function resolveLdrInternalFormat(gl: WebGL2RenderingContext, format: GLenum, type: GLenum): GLenum {\n if (type === gl.UNSIGNED_BYTE) {\n if (format === gl.RGBA) {\n return gl.RGBA8;\n }\n if (format === gl.RGB) {\n return gl.RGB8;\n }\n if (format === gl.RG) {\n return gl.RG8;\n }\n if (format === gl.RED) {\n return gl.R8;\n }\n if (format === gl.LUMINANCE) {\n return gl.LUMINANCE;\n }\n }\n return format;\n}\n\n/** Internal — pick a sized internalFormat for `texImage2D`. WebGL2 prefers\n * sized formats for non-color-renderable / non-readback paths; for the\n * NeonBrush use cases (sample-only) the unsized format works too, but sized\n * is more portable. Shared with the render-target module.\n * @internal */\nexport function pickSizedInternalFormat(gl: WebGL2RenderingContext, format: GLenum, type: GLenum): GLenum {\n if (type === gl.UNSIGNED_BYTE) {\n if (format === gl.RGBA) {\n return gl.RGBA8;\n }\n if (format === gl.RGB) {\n return gl.RGB8;\n }\n if (format === gl.LUMINANCE) {\n return gl.LUMINANCE;\n }\n }\n if (type === gl.FLOAT) {\n if (format === gl.RGBA) {\n return gl.RGBA32F;\n }\n if (format === gl.RGB) {\n return gl.RGB32F;\n }\n }\n if (type === gl.HALF_FLOAT) {\n if (format === gl.RGBA) {\n return gl.RGBA16F;\n }\n if (format === gl.RGB) {\n return gl.RGB16F;\n }\n }\n return format;\n}\n"]}
|