@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/mesh.js CHANGED
@@ -22,7 +22,7 @@ const UNSIGNED_INT = 0x1405;
22
22
  * Create a GPU vertex buffer from interleaved float data.
23
23
  *
24
24
  * @param engine - The engine.
25
- * @param data - The vertex data. Retained by reference for context-restore — do
25
+ * @param data - The vertex data. Retained by reference unless the engine opts out of context restoration — do
26
26
  * not mutate it in place; use {@link updateVertexBuffer} to change contents.
27
27
  * @param dynamic - Hint that the buffer will be updated frequently
28
28
  * (`DYNAMIC_DRAW`). Default `false` (`STATIC_DRAW`).
@@ -37,26 +37,26 @@ export function createVertexBuffer(engine, data, dynamic = false) {
37
37
  const vb = {
38
38
  handle,
39
39
  byteLength: data.byteLength,
40
- _data: data,
41
40
  _dynamic: dynamic,
42
41
  _engine: engine,
43
42
  _disposed: false,
44
- _deleteGpu: () => { },
45
- _restore: () => { },
46
- };
47
- vb._deleteGpu = (g) => {
48
- g.deleteBuffer(vb.handle);
49
- };
50
- vb._restore = (target) => {
51
- const g = target.gl;
52
- const fresh = g.createBuffer();
53
- if (fresh === null) {
54
- return;
55
- }
56
- vb.handle = fresh;
57
- bindArrayBufferRaw(target, fresh);
58
- g.bufferData(ARRAY_BUFFER, vb._data, vb._dynamic ? DYNAMIC_DRAW : STATIC_DRAW);
43
+ _deleteGpu: (g) => {
44
+ g.deleteBuffer(vb.handle);
45
+ },
59
46
  };
47
+ if (!engine._noRestore) {
48
+ vb._data = data;
49
+ vb._restore = (target) => {
50
+ const g = target.gl;
51
+ const fresh = g.createBuffer();
52
+ if (fresh === null) {
53
+ return;
54
+ }
55
+ vb.handle = fresh;
56
+ bindArrayBufferRaw(target, fresh);
57
+ g.bufferData(ARRAY_BUFFER, vb._data, vb._dynamic ? DYNAMIC_DRAW : STATIC_DRAW);
58
+ };
59
+ }
60
60
  bindArrayBufferRaw(engine, handle);
61
61
  gl.bufferData(ARRAY_BUFFER, data, dynamic ? DYNAMIC_DRAW : STATIC_DRAW);
62
62
  engine._buffers.push(vb);
@@ -65,7 +65,7 @@ export function createVertexBuffer(engine, data, dynamic = false) {
65
65
  /**
66
66
  * Upload new contents into (part of) a vertex buffer via `bufferSubData`. A
67
67
  * full-buffer update from offset 0 also refreshes the retained CPU data used
68
- * for context-restore.
68
+ * for context-restore unless the engine opts out. Neither mode copies the input.
69
69
  *
70
70
  * @param vb - The vertex buffer to update.
71
71
  * @param data - The new float data.
@@ -79,7 +79,9 @@ export function updateVertexBuffer(vb, data, dstByteOffset = 0) {
79
79
  bindArrayBufferRaw(engine, vb.handle);
80
80
  engine.gl.bufferSubData(ARRAY_BUFFER, dstByteOffset, data);
81
81
  if (dstByteOffset === 0 && data.byteLength >= vb.byteLength) {
82
- vb._data = data;
82
+ if (!engine._noRestore) {
83
+ vb._data = data;
84
+ }
83
85
  vb.byteLength = data.byteLength;
84
86
  }
85
87
  }
@@ -89,7 +91,7 @@ export function updateVertexBuffer(vb, data, dstByteOffset = 0) {
89
91
  * VAO element bindings.
90
92
  *
91
93
  * @param engine - The engine.
92
- * @param data - The index data. Retained by reference for context-restore.
94
+ * @param data - The index data. Retained by reference unless the engine opts out of context restoration.
93
95
  * @returns The new {@link GLIndexBuffer}.
94
96
  */
95
97
  export function createIndexBuffer(engine, data) {
@@ -103,27 +105,27 @@ export function createIndexBuffer(engine, data) {
103
105
  handle,
104
106
  count: data.length,
105
107
  is32Bits: is32,
106
- _data: data,
107
108
  _engine: engine,
108
109
  _disposed: false,
109
- _deleteGpu: () => { },
110
- _restore: () => { },
111
- };
112
- ib._deleteGpu = (g) => {
113
- g.deleteBuffer(ib.handle);
114
- };
115
- ib._restore = (target) => {
116
- const g = target.gl;
117
- const fresh = g.createBuffer();
118
- if (fresh === null) {
119
- return;
120
- }
121
- ib.handle = fresh;
122
- bindDefaultVao(target);
123
- g.bindBuffer(ELEMENT_ARRAY_BUFFER, fresh);
124
- target._state.boundElementBuffer = fresh;
125
- g.bufferData(ELEMENT_ARRAY_BUFFER, ib._data, STATIC_DRAW);
110
+ _deleteGpu: (g) => {
111
+ g.deleteBuffer(ib.handle);
112
+ },
126
113
  };
114
+ if (!engine._noRestore) {
115
+ ib._data = data;
116
+ ib._restore = (target) => {
117
+ const g = target.gl;
118
+ const fresh = g.createBuffer();
119
+ if (fresh === null) {
120
+ return;
121
+ }
122
+ ib.handle = fresh;
123
+ bindDefaultVao(target);
124
+ g.bindBuffer(ELEMENT_ARRAY_BUFFER, fresh);
125
+ target._state.boundElementBuffer = fresh;
126
+ g.bufferData(ELEMENT_ARRAY_BUFFER, ib._data, STATIC_DRAW);
127
+ };
128
+ }
127
129
  bindDefaultVao(engine);
128
130
  gl.bindBuffer(ELEMENT_ARRAY_BUFFER, handle);
129
131
  engine._state.boundElementBuffer = handle;
@@ -132,13 +134,15 @@ export function createIndexBuffer(engine, data) {
132
134
  return ib;
133
135
  }
134
136
  /** Dispose a vertex or index buffer (delete the GL buffer + unregister). Clears
135
- * the array/element-buffer cache slot if it pointed at this buffer. Idempotent. */
137
+ * the array/element-buffer cache slot if it pointed at this buffer and releases
138
+ * retained upload/restore references in both recovery modes. Idempotent. */
136
139
  export function disposeBuffer(buffer) {
137
140
  const engine = buffer._engine;
138
141
  if (buffer._disposed) {
139
142
  return;
140
143
  }
141
144
  buffer._disposed = true;
145
+ buffer._data = buffer._restore = undefined;
142
146
  const i = engine._buffers.indexOf(buffer);
143
147
  if (i !== -1) {
144
148
  engine._buffers.splice(i, 1);
@@ -153,6 +157,11 @@ export function disposeBuffer(buffer) {
153
157
  if (s.boundElementBuffer === buffer.handle) {
154
158
  s.boundElementBuffer = null;
155
159
  }
160
+ for (let location = 0; location < s.enabledAttribs.length; location++) {
161
+ if (s.enabledAttribs[location] === buffer.handle) {
162
+ s.enabledAttribs[location] = true;
163
+ }
164
+ }
156
165
  }
157
166
  /* ────────────────────────────── binding + draw ──────────────────────────── */
158
167
  /**
@@ -177,18 +186,22 @@ export function bindIndexBuffer(ib) {
177
186
  /**
178
187
  * Configure vertex attributes from `vb`, reproducing Babylon's
179
188
  * `bindInstancesBuffer` exactly. For each descriptor: resolves the location
180
- * (explicit `index` or via the effect), enables the attribute array, issues
181
- * `vertexAttribPointer`, and sets the vertex divisor (`undefined → 1`). Each
182
- * instanced location (divisor ≠ 0) is tracked once so {@link unbindInstanceAttributes}
189
+ * (explicit `index` or via the effect), enables the attribute array (cached),
190
+ * issues `vertexAttribPointer`, and sets the vertex divisor (`undefined → 1`).
191
+ * Each instanced location (divisor ≠ 0) is tracked once so {@link unbindInstanceAttributes}
183
192
  * can reset its divisor afterwards; per-vertex locations already have divisor 0 and are
184
193
  * not tracked, so callers that never instance can rebind every frame without growth.
185
194
  *
186
195
  * `computeStride` controls the GL stride passed to `vertexAttribPointer`:
187
196
  * - `false` (default) → stride `0`: each attribute is independently tightly
188
197
  * packed. Combined with overlapping `offset`s this yields the "sliding window"
189
- * ShapeBuilder uses for distance-field tape buffers.
198
+ * used by distance-field tape buffers.
190
199
  * - `true` → stride = Σ(`size`·4 bytes): interleaved per-vertex/per-instance.
191
200
  *
201
+ * Every call re-issues `vertexAttribPointer`. Callers that rebind an equivalent
202
+ * layout every draw (e.g. a 2D shape renderer) should use {@link bindAttributesCached},
203
+ * which elides unchanged pointers; the two can be mixed freely.
204
+ *
192
205
  * Runs on the default (null) VAO — never corrupts the quad / sprite VAOs.
193
206
  * No-op on a lost/disposed context or before the effect is ready.
194
207
  *
@@ -220,8 +233,9 @@ export function bindAttributes(vb, descriptors, effect, computeStride = false) {
220
233
  }
221
234
  if (!s.enabledAttribs[loc]) {
222
235
  gl.enableVertexAttribArray(loc);
223
- s.enabledAttribs[loc] = true;
224
236
  }
237
+ // Enabled, but with a pointer bindAttributesCached must not trust.
238
+ s.enabledAttribs[loc] = true;
225
239
  gl.vertexAttribPointer(loc, d.size, d.type ?? FLOAT, d.normalized ?? false, stride, d.offset ?? 0);
226
240
  const divisor = d.divisor ?? 1;
227
241
  gl.vertexAttribDivisor(loc, divisor);
@@ -230,11 +244,134 @@ export function bindAttributes(vb, descriptors, effect, computeStride = false) {
230
244
  }
231
245
  }
232
246
  }
247
+ /**
248
+ * {@link bindAttributes} with a default-VAO attribute cache: `vertexAttribPointer`
249
+ * is issued only when a location's (buffer handle, size, type, normalized,
250
+ * stride, offset) tuple changes, and `vertexAttribDivisor` only when its divisor
251
+ * changes, so rebinding an equivalent layout every draw (e.g. per-shape binds in a
252
+ * 2D shape renderer) issues no attribute calls. Divisors are tracked through
253
+ * `instanceLocations`, so {@link unbindInstanceAttributes} needs no cache code.
254
+ * Semantics, parameters and no-op conditions are
255
+ * otherwise identical to {@link bindAttributes}.
256
+ *
257
+ * The cache (4 numbers per `MAX_VERTEX_ATTRIBS` location) is allocated once on
258
+ * the first effective call; later binds allocate nothing. Disabling an
259
+ * attribute, {@link bindAttributes}, cache wipes/resets and context loss
260
+ * invalidate the affected tuples, and a recreated buffer handle never matches
261
+ * a stale one. Named VAOs keep independent state.
262
+ *
263
+ * @param vb - The buffer supplying the attribute data.
264
+ * @param descriptors - The attribute layout.
265
+ * @param effect - The effect whose attribute locations resolve unnamed indices.
266
+ * @param computeStride - See {@link bindAttributes}. Default `false`.
267
+ */
268
+ export function bindAttributesCached(vb, descriptors, effect, computeStride = false) {
269
+ const engine = vb._engine;
270
+ if (engine._isLost || engine._disposed || vb._disposed || !effect.isReady) {
271
+ return;
272
+ }
273
+ const gl = engine.gl;
274
+ const s = engine._state;
275
+ bindDefaultVao(engine);
276
+ bindArrayBufferRaw(engine, vb.handle);
277
+ const values = (s._attributePointers ??= new Float64Array(gl.getParameter(gl.MAX_VERTEX_ATTRIBS) * 4));
278
+ let stride = 0;
279
+ if (computeStride) {
280
+ for (let i = 0; i < descriptors.length; i++) {
281
+ stride += descriptors[i].size * 4;
282
+ }
283
+ }
284
+ for (let i = 0; i < descriptors.length; i++) {
285
+ const d = descriptors[i];
286
+ const loc = d.index !== undefined ? d.index : d.name !== undefined ? getEffectAttributeLocation(effect, d.name) : -1;
287
+ if (loc < 0) {
288
+ continue;
289
+ }
290
+ const enabled = s.enabledAttribs[loc];
291
+ if (!enabled) {
292
+ gl.enableVertexAttribArray(loc);
293
+ }
294
+ const type = d.type ?? FLOAT;
295
+ const normalized = d.normalized ?? false;
296
+ const offset = d.offset ?? 0;
297
+ // A GLenum fits in 32 bits; packing the valid 1..4 size and normalize
298
+ // flag leaves one exact (53-bit) number instead of three cache slots.
299
+ const format = type * 16 + d.size * 2 + +normalized;
300
+ const slot = loc * 4;
301
+ if (enabled !== vb.handle || values[slot] !== format || values[slot + 1] !== stride || values[slot + 2] !== offset) {
302
+ gl.vertexAttribPointer(loc, d.size, type, normalized, stride, offset);
303
+ values[slot] = format;
304
+ values[slot + 1] = stride;
305
+ values[slot + 2] = offset;
306
+ }
307
+ const divisor = d.divisor ?? 1;
308
+ // While this binder owns the location (a buffer handle, not `true`/null),
309
+ // every non-zero divisor it sets is listed in instanceLocations, and only
310
+ // unbindInstanceAttributes (which resets listed divisors to 0) or a cache
311
+ // reset (which also drops ownership) clears that list. An owned, unlisted
312
+ // location therefore has divisor 0; a listed one has the cached divisor.
313
+ const listed = s.instanceLocations.includes(loc);
314
+ if (!enabled || enabled === true || (listed ? values[slot + 3] : 0) !== divisor) {
315
+ gl.vertexAttribDivisor(loc, divisor);
316
+ values[slot + 3] = divisor;
317
+ if (divisor !== 0 && !listed) {
318
+ s.instanceLocations.push(loc);
319
+ }
320
+ }
321
+ s.enabledAttribs[loc] = vb.handle;
322
+ }
323
+ }
324
+ /**
325
+ * Disable an enabled attribute on the default VAO, eliding redundant calls.
326
+ * Leaves its divisor unchanged, like ThinEngine's `disableAttributeByIndex`;
327
+ * use {@link unbindInstanceAttributes} for divisor cleanup. Named VAOs are
328
+ * untouched. No-op on a lost/disposed engine. The enable cache is cleared on
329
+ * context loss or cache reset, so subsequent binds replay their enables. After
330
+ * a plain `wipeGLStateCache`, a location lite-gl has not confirmed
331
+ * disabled is treated as unknown (the host may have enabled it) and is disabled.
332
+ *
333
+ * @param engine - The engine.
334
+ * @param location - The attribute location to disable.
335
+ */
336
+ export function disableAttributeByIndex(engine, location) {
337
+ const s = engine._state;
338
+ const enabled = s.enabledAttribs[location];
339
+ // `-1` is the "optimized out" sentinel from `getAttribLocation`: always a no-op.
340
+ if (location < 0 || engine._isLost || engine._disposed || (s._unknownAttribs ? enabled === null : !enabled)) {
341
+ return;
342
+ }
343
+ bindDefaultVao(engine);
344
+ engine.gl.disableVertexAttribArray(location);
345
+ s.enabledAttribs[location] = null;
346
+ }
347
+ /**
348
+ * Disable all enabled default-VAO attributes in ascending location order.
349
+ * Cached across frames and allocation-free; named VAOs and instance divisors
350
+ * are unchanged (ThinEngine's `unbindAllAttributes` semantics). No-op on a
351
+ * lost/disposed engine. Context loss/cache reset clears the enable cache;
352
+ * subsequent {@link bindAttributes} calls replay their enables. The first call
353
+ * after a plain `wipeGLStateCache` disables every location up to
354
+ * `MAX_VERTEX_ATTRIBS` once, since the host may have enabled any of them.
355
+ *
356
+ * @param engine - The engine.
357
+ */
358
+ export function unbindAllAttributes(engine) {
359
+ if (engine._isLost || engine._disposed) {
360
+ return;
361
+ }
362
+ const s = engine._state;
363
+ const count = s._unknownAttribs ? engine.gl.getParameter(engine.gl.MAX_VERTEX_ATTRIBS) : s.enabledAttribs.length;
364
+ for (let location = 0; location < count; location++) {
365
+ disableAttributeByIndex(engine, location);
366
+ }
367
+ s._unknownAttribs = false;
368
+ }
233
369
  /**
234
370
  * Reset the vertex divisor of every attribute touched by {@link bindAttributes}
235
- * back to 0 — the lite-gl equivalent of Babylon's `unbindInstanceAttributes`.
371
+ * or {@link bindAttributesCached} back to 0 — the lite-gl equivalent of Babylon's `unbindInstanceAttributes`.
236
372
  * Call after an instanced draw so a following non-instanced draw is not skewed.
237
- * No-op on a lost/disposed context.
373
+ * The divisors are reset on the default VAO (selected if a named VAO is bound),
374
+ * where these binders set them. No-op on a lost/disposed context.
238
375
  *
239
376
  * @param engine - The engine.
240
377
  */
@@ -242,8 +379,14 @@ export function unbindInstanceAttributes(engine) {
242
379
  if (engine._isLost || engine._disposed) {
243
380
  return;
244
381
  }
245
- const gl = engine.gl;
246
382
  const locs = engine._state.instanceLocations;
383
+ if (locs.length === 0) {
384
+ return;
385
+ }
386
+ // The listed divisors live on the default VAO; a named VAO bound since then
387
+ // owns its own divisors and must not be reset.
388
+ bindDefaultVao(engine);
389
+ const gl = engine.gl;
247
390
  for (let i = 0; i < locs.length; i++) {
248
391
  gl.vertexAttribDivisor(locs[i], 0);
249
392
  }
@@ -333,20 +476,25 @@ export function createMeshVao(engine, vertexBuffers, indexBuffer, effect) {
333
476
  }
334
477
  bindings.push({ buffer: vbb.buffer, attrs });
335
478
  }
336
- const vao = { handle, _engine: engine, _disposed: false, _indexBuffer: indexBuffer, _bindings: bindings, _restore: () => { } };
337
- vao._restore = () => {
338
- if (engine._isLost || engine._disposed || vao._disposed) {
339
- return;
340
- }
341
- const fresh = engine.gl.createVertexArray();
342
- if (fresh === null) {
343
- return;
344
- }
345
- vao.handle = fresh;
346
- recordMeshVao(engine, vao);
347
- };
479
+ const vao = { handle, _engine: engine, _disposed: false, _indexBuffer: indexBuffer, _bindings: bindings };
480
+ if (!engine._noRestore) {
481
+ vao._restore = () => {
482
+ if (engine._isLost || engine._disposed || vao._disposed) {
483
+ return;
484
+ }
485
+ const fresh = engine.gl.createVertexArray();
486
+ if (fresh === null) {
487
+ return;
488
+ }
489
+ vao.handle = fresh;
490
+ recordMeshVao(engine, vao);
491
+ };
492
+ onContextRestored(engine, vao._restore);
493
+ }
348
494
  recordMeshVao(engine, vao);
349
- onContextRestored(engine, vao._restore);
495
+ if (engine._noRestore) {
496
+ bindings.length = 0;
497
+ }
350
498
  return vao;
351
499
  }
352
500
  /**
@@ -378,8 +526,15 @@ export function bindMeshVao(vao) {
378
526
  *
379
527
  * @param vao - The mesh VAO to draw.
380
528
  * @param instanceCount - Instance count for instanced draws. Default 0 (non-instanced).
529
+ * @param indexStart - First index in the retained mesh.
530
+ * @param indexCount - Number of indices; defaults to the indices from `indexStart` to the end of the mesh.
531
+ * @throws RangeError when the range is not whole indices within the mesh.
381
532
  */
382
- export function drawMesh(vao, instanceCount = 0) {
533
+ export function drawMesh(vao, instanceCount = 0, indexStart = 0, indexCount = vao._indexBuffer.count - indexStart) {
534
+ const total = vao._indexBuffer.count;
535
+ if (!Number.isInteger(indexStart) || !Number.isInteger(indexCount) || indexStart < 0 || indexCount < 0 || indexStart + indexCount > total) {
536
+ throw new RangeError(`lite-gl: drawMesh range (start ${indexStart}, count ${indexCount}) must be whole indices within the mesh's ${total}`);
537
+ }
383
538
  const engine = vao._engine;
384
539
  if (engine._isLost || engine._disposed || vao._disposed) {
385
540
  return;
@@ -393,10 +548,10 @@ export function drawMesh(vao, instanceCount = 0) {
393
548
  const gl = engine.gl;
394
549
  const type = ib.is32Bits ? UNSIGNED_INT : UNSIGNED_SHORT;
395
550
  if (instanceCount > 0) {
396
- gl.drawElementsInstanced(TRIANGLES, ib.count, type, 0, instanceCount);
551
+ gl.drawElementsInstanced(TRIANGLES, indexCount, type, indexStart * (ib.is32Bits ? 4 : 2), instanceCount);
397
552
  }
398
553
  else {
399
- gl.drawElements(TRIANGLES, ib.count, type, 0);
554
+ gl.drawElements(TRIANGLES, indexCount, type, indexStart * (ib.is32Bits ? 4 : 2));
400
555
  }
401
556
  }
402
557
  /** Dispose a {@link GLMeshVao}: delete the VAO and unregister its restore hook.
@@ -407,7 +562,9 @@ export function disposeMeshVao(vao) {
407
562
  return;
408
563
  }
409
564
  vao._disposed = true;
410
- offContextRestored(engine, vao._restore);
565
+ if (vao._restore) {
566
+ offContextRestored(engine, vao._restore);
567
+ }
411
568
  if (!engine._isLost && !engine._disposed) {
412
569
  const s = engine._state;
413
570
  engine.gl.deleteVertexArray(vao.handle);
package/mesh.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mesh.js","sourceRoot":"","sources":["../src/mesh.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,EAAE,0BAA0B,EAAiB,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,4BAA4B;AAC5B,MAAM,YAAY,GAAG,MAAM,CAAC;AAC5B,oCAAoC;AACpC,MAAM,oBAAoB,GAAG,MAAM,CAAC;AACpC,2BAA2B;AAC3B,MAAM,WAAW,GAAG,MAAM,CAAC;AAC3B,4BAA4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,IAAI,CAAC,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtB,MAAM,CAAC,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;QAChC,CAAC,CAAC,QAAQ,GAAG,IAAI,CAAC;QAClB,CAAC,CAAC,kBAAkB,GAAG,IAAI,CAAC;IAChC,CAAC;AACL,CAAC;AAED;iEACiE;AACjE,SAAS,kBAAkB,CAAC,MAAuB,EAAE,MAAmB;IACpE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,IAAI,CAAC,CAAC,gBAAgB,KAAK,MAAM,EAAE,CAAC;QAChC,OAAO;IACX,CAAC;IACD,MAAM,CAAC,EAAE,CAAC,UAAU,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IAC3C,CAAC,CAAC,gBAAgB,GAAG,MAAM,CAAC;AAChC,CAAC","sourcesContent":["/**\n * Indexed meshes, dynamic vertex/index buffers, and hardware instancing.\n *\n * Part of the public API via the `@babylonjs/lite-gl` barrel. The package is\n * `sideEffects: false`, so consumers that only render the fullscreen quad /\n * sprites tree-shake the mesh code out.\n *\n * This is the lite-gl equivalent of Babylon's `ThinEngine.createVertexBuffer` /\n * `createIndexBuffer` / `_releaseBuffer` / `bindIndexBuffer` /\n * `bindInstancesBuffer` / `unbindInstanceAttributes` / `drawElementsType`. The\n * attribute binder reproduces Babylon's instancing semantics EXACTLY, including\n * the `computeStride = false → stride 0` \"sliding window\" used by ShapeBuilder's\n * tape buffers (consecutive instances read overlapping vec4 windows) and the\n * `divisor === undefined → 1` default.\n *\n * The mesh path runs on the DEFAULT (null) VAO — every buffer/attribute op binds\n * `gl.bindVertexArray(null)` first, so it never corrupts the quad / sprite VAOs.\n * Vertex and index buffers retain their CPU data and re-upload automatically on\n * `webglcontextrestored`.\n */\nimport type { GLEngineContext } from \"./context.js\";\nimport { onContextRestored, offContextRestored } from \"./context.js\";\nimport { getEffectAttributeLocation, type GLEffect } from \"./effect.js\";\nimport { applyGLStates } from \"./apply-states.js\";\n\n/** GL `gl.ARRAY_BUFFER`. */\nconst ARRAY_BUFFER = 0x8892;\n/** GL `gl.ELEMENT_ARRAY_BUFFER`. */\nconst ELEMENT_ARRAY_BUFFER = 0x8893;\n/** GL `gl.STATIC_DRAW`. */\nconst STATIC_DRAW = 0x88e4;\n/** GL `gl.DYNAMIC_DRAW`. */\nconst DYNAMIC_DRAW = 0x88e8;\n/** GL `gl.FLOAT`. */\nconst FLOAT = 0x1406;\n/** GL `gl.TRIANGLES`. */\nconst TRIANGLES = 0x0004;\n/** GL `gl.UNSIGNED_SHORT`. */\nconst UNSIGNED_SHORT = 0x1403;\n/** GL `gl.UNSIGNED_INT`. */\nconst UNSIGNED_INT = 0x1405;\n\n/** A GPU vertex buffer holding interleaved float vertex data. The lite-gl\n * counterpart of Babylon's `DataBuffer` for vertex data. */\nexport interface GLVertexBuffer {\n /** The live `WebGLBuffer`. Swapped on `webglcontextrestored`. */\n handle: WebGLBuffer;\n /** Size of the GL buffer in bytes. */\n byteLength: number;\n /** The engine that created (and owns) this buffer. Operations on it derive\n * their engine from here, so they take the buffer alone. @internal */\n _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n /** @internal Delete the underlying GL buffer. */\n _deleteGpu: (gl: WebGL2RenderingContext) => void;\n /** @internal Re-create + re-upload the retained CPU data into a fresh handle. */\n _restore: (engine: GLEngineContext) => void;\n /** @internal Retained CPU data for context-restore replay (the creation /\n * last full-buffer upload). */\n _data: Float32Array;\n /** @internal `true` for `DYNAMIC_DRAW` (updated via `updateVertexBuffer`). */\n _dynamic: boolean;\n}\n\n/** A GPU index buffer. The lite-gl counterpart of Babylon's `DataBuffer` for\n * index data. */\nexport interface GLIndexBuffer {\n /** The live `WebGLBuffer`. Swapped on `webglcontextrestored`. */\n handle: WebGLBuffer;\n /** Number of indices. */\n count: number;\n /** `true` for 32-bit (`Uint32Array`) indices, `false` for 16-bit. */\n is32Bits: boolean;\n /** The engine that created (and owns) this buffer. Operations on it derive\n * their engine from here, so they take the buffer alone. @internal */\n _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n /** @internal Delete the underlying GL buffer. */\n _deleteGpu: (gl: WebGL2RenderingContext) => void;\n /** @internal Re-create + re-upload the retained CPU data into a fresh handle. */\n _restore: (engine: GLEngineContext) => void;\n /** @internal Retained CPU data for context-restore replay. */\n _data: Uint16Array | Uint32Array;\n}\n\n/**\n * Describes one vertex attribute fed from a buffer — the lite-gl equivalent of\n * Babylon's `InstancingAttributeInfo`. Pass an array of these to\n * {@link bindAttributes}.\n */\nexport interface GLAttributeDescriptor {\n /** Attribute name; resolved to a location via the effect when `index` is\n * omitted (`Effect.getAttributeLocationByName`). */\n name?: string;\n /** Explicit attribute location. When set, overrides the `name` lookup. */\n index?: number;\n /** Number of components, 1–4. */\n size: number;\n /** Byte offset of this attribute's first element within the buffer. Default 0. */\n offset?: number;\n /**\n * Per-instance vertex divisor. **Omitted/`undefined` → `1` (instanced)**,\n * matching Babylon's `bindInstancesBuffer`. Pass `0` explicitly for a\n * per-vertex attribute (e.g. the base mesh position).\n */\n divisor?: number;\n /** GL component type. Default `gl.FLOAT`. */\n type?: GLenum;\n /** Normalize fixed-point integer data to `[0,1]`/`[-1,1]`. Default `false`. */\n normalized?: boolean;\n}\n\n/* ──────────────────────────────── buffers ──────────────────────────────── */\n\n/**\n * Create a GPU vertex buffer from interleaved float data.\n *\n * @param engine - The engine.\n * @param data - The vertex data. Retained by reference for context-restore — do\n * not mutate it in place; use {@link updateVertexBuffer} to change contents.\n * @param dynamic - Hint that the buffer will be updated frequently\n * (`DYNAMIC_DRAW`). Default `false` (`STATIC_DRAW`).\n * @returns The new {@link GLVertexBuffer}.\n */\nexport function createVertexBuffer(engine: GLEngineContext, data: Float32Array, dynamic = false): GLVertexBuffer {\n const gl = engine.gl;\n const handle = gl.createBuffer();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createBuffer returned null (vertex buffer)\");\n }\n const vb: GLVertexBuffer = {\n handle,\n byteLength: data.byteLength,\n _data: data,\n _dynamic: dynamic,\n _engine: engine,\n _disposed: false,\n _deleteGpu: () => {},\n _restore: () => {},\n };\n vb._deleteGpu = (g: WebGL2RenderingContext): void => {\n g.deleteBuffer(vb.handle);\n };\n vb._restore = (target: GLEngineContext): void => {\n const g = target.gl;\n const fresh = g.createBuffer();\n if (fresh === null) {\n return;\n }\n vb.handle = fresh;\n bindArrayBufferRaw(target, fresh);\n g.bufferData(ARRAY_BUFFER, vb._data, vb._dynamic ? DYNAMIC_DRAW : STATIC_DRAW);\n };\n bindArrayBufferRaw(engine, handle);\n gl.bufferData(ARRAY_BUFFER, data, dynamic ? DYNAMIC_DRAW : STATIC_DRAW);\n engine._buffers.push(vb);\n return vb;\n}\n\n/**\n * Upload new contents into (part of) a vertex buffer via `bufferSubData`. A\n * full-buffer update from offset 0 also refreshes the retained CPU data used\n * for context-restore.\n *\n * @param vb - The vertex buffer to update.\n * @param data - The new float data.\n * @param dstByteOffset - Destination byte offset within the buffer. Default 0.\n */\nexport function updateVertexBuffer(vb: GLVertexBuffer, data: Float32Array, dstByteOffset = 0): void {\n const engine = vb._engine;\n if (engine._isLost || engine._disposed || vb._disposed) {\n return;\n }\n bindArrayBufferRaw(engine, vb.handle);\n engine.gl.bufferSubData(ARRAY_BUFFER, dstByteOffset, data);\n if (dstByteOffset === 0 && data.byteLength >= vb.byteLength) {\n vb._data = data;\n vb.byteLength = data.byteLength;\n }\n}\n\n/**\n * Create a GPU index buffer. `Uint16Array` → 16-bit indices, `Uint32Array` →\n * 32-bit. Binds the default VAO first so it never corrupts the quad / sprite\n * VAO element bindings.\n *\n * @param engine - The engine.\n * @param data - The index data. Retained by reference for context-restore.\n * @returns The new {@link GLIndexBuffer}.\n */\nexport function createIndexBuffer(engine: GLEngineContext, data: Uint16Array | Uint32Array): GLIndexBuffer {\n const gl = engine.gl;\n const handle = gl.createBuffer();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createBuffer returned null (index buffer)\");\n }\n const is32 = data instanceof Uint32Array;\n const ib: GLIndexBuffer = {\n handle,\n count: data.length,\n is32Bits: is32,\n _data: data,\n _engine: engine,\n _disposed: false,\n _deleteGpu: () => {},\n _restore: () => {},\n };\n ib._deleteGpu = (g: WebGL2RenderingContext): void => {\n g.deleteBuffer(ib.handle);\n };\n ib._restore = (target: GLEngineContext): void => {\n const g = target.gl;\n const fresh = g.createBuffer();\n if (fresh === null) {\n return;\n }\n ib.handle = fresh;\n bindDefaultVao(target);\n g.bindBuffer(ELEMENT_ARRAY_BUFFER, fresh);\n target._state.boundElementBuffer = fresh;\n g.bufferData(ELEMENT_ARRAY_BUFFER, ib._data, STATIC_DRAW);\n };\n bindDefaultVao(engine);\n gl.bindBuffer(ELEMENT_ARRAY_BUFFER, handle);\n engine._state.boundElementBuffer = handle;\n gl.bufferData(ELEMENT_ARRAY_BUFFER, data, STATIC_DRAW);\n engine._buffers.push(ib);\n return ib;\n}\n\n/** Dispose a vertex or index buffer (delete the GL buffer + unregister). Clears\n * the array/element-buffer cache slot if it pointed at this buffer. Idempotent. */\nexport function disposeBuffer(buffer: GLVertexBuffer | GLIndexBuffer): void {\n const engine = buffer._engine;\n if (buffer._disposed) {\n return;\n }\n buffer._disposed = true;\n const i = engine._buffers.indexOf(buffer);\n if (i !== -1) {\n engine._buffers.splice(i, 1);\n }\n const s = engine._state;\n if (!engine._isLost && !engine._disposed) {\n engine.gl.deleteBuffer(buffer.handle);\n }\n if (s.boundArrayBuffer === buffer.handle) {\n s.boundArrayBuffer = null;\n }\n if (s.boundElementBuffer === buffer.handle) {\n s.boundElementBuffer = null;\n }\n}\n\n/* ────────────────────────────── binding + draw ──────────────────────────── */\n\n/**\n * Bind an index buffer as the current element-array buffer (on the default\n * VAO). Cached. The lite-gl equivalent of Babylon's `_bindIndexBufferWithCache`.\n *\n * @param ib - The index buffer to bind.\n */\nexport function bindIndexBuffer(ib: GLIndexBuffer): void {\n const engine = ib._engine;\n if (engine._isLost || engine._disposed || ib._disposed) {\n return;\n }\n bindDefaultVao(engine);\n const s = engine._state;\n if (s.boundElementBuffer === ib.handle) {\n return;\n }\n engine.gl.bindBuffer(ELEMENT_ARRAY_BUFFER, ib.handle);\n s.boundElementBuffer = ib.handle;\n}\n\n/**\n * Configure vertex attributes from `vb`, reproducing Babylon's\n * `bindInstancesBuffer` exactly. For each descriptor: resolves the location\n * (explicit `index` or via the effect), enables the attribute array, issues\n * `vertexAttribPointer`, and sets the vertex divisor (`undefined → 1`). Each\n * instanced location (divisor ≠ 0) is tracked once so {@link unbindInstanceAttributes}\n * can reset its divisor afterwards; per-vertex locations already have divisor 0 and are\n * not tracked, so callers that never instance can rebind every frame without growth.\n *\n * `computeStride` controls the GL stride passed to `vertexAttribPointer`:\n * - `false` (default) → stride `0`: each attribute is independently tightly\n * packed. Combined with overlapping `offset`s this yields the \"sliding window\"\n * ShapeBuilder uses for distance-field tape buffers.\n * - `true` → stride = Σ(`size`·4 bytes): interleaved per-vertex/per-instance.\n *\n * Runs on the default (null) VAO — never corrupts the quad / sprite VAOs.\n * No-op on a lost/disposed context or before the effect is ready.\n *\n * @param vb - The buffer supplying the attribute data.\n * @param descriptors - The attribute layout.\n * @param effect - The effect whose attribute locations resolve unnamed indices.\n * @param computeStride - See above. Default `false`.\n */\nexport function bindAttributes(vb: GLVertexBuffer, descriptors: readonly GLAttributeDescriptor[], effect: GLEffect, computeStride = false): void {\n const engine = vb._engine;\n if (engine._isLost || engine._disposed || vb._disposed || !effect.isReady) {\n return;\n }\n const gl = engine.gl;\n const s = engine._state;\n bindDefaultVao(engine);\n bindArrayBufferRaw(engine, vb.handle);\n\n let stride = 0;\n if (computeStride) {\n for (let i = 0; i < descriptors.length; i++) {\n stride += (descriptors[i] as GLAttributeDescriptor).size * 4;\n }\n }\n\n for (let i = 0; i < descriptors.length; i++) {\n const d = descriptors[i] as GLAttributeDescriptor;\n const loc = d.index !== undefined ? d.index : d.name !== undefined ? getEffectAttributeLocation(effect, d.name) : -1;\n if (loc < 0) {\n continue;\n }\n if (!s.enabledAttribs[loc]) {\n gl.enableVertexAttribArray(loc);\n s.enabledAttribs[loc] = true;\n }\n gl.vertexAttribPointer(loc, d.size, d.type ?? FLOAT, d.normalized ?? false, stride, d.offset ?? 0);\n const divisor = d.divisor ?? 1;\n gl.vertexAttribDivisor(loc, divisor);\n if (divisor !== 0 && !s.instanceLocations.includes(loc)) {\n s.instanceLocations.push(loc);\n }\n }\n}\n\n/**\n * Reset the vertex divisor of every attribute touched by {@link bindAttributes}\n * back to 0 — the lite-gl equivalent of Babylon's `unbindInstanceAttributes`.\n * Call after an instanced draw so a following non-instanced draw is not skewed.\n * No-op on a lost/disposed context.\n *\n * @param engine - The engine.\n */\nexport function unbindInstanceAttributes(engine: GLEngineContext): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const gl = engine.gl;\n const locs = engine._state.instanceLocations;\n for (let i = 0; i < locs.length; i++) {\n gl.vertexAttribDivisor(locs[i] as number, 0);\n }\n locs.length = 0;\n}\n\n/**\n * Draw indexed triangles from `ib` — the lite-gl equivalent of Babylon's\n * `drawElementsType` (triangle fill mode). When `instanceCount > 0` issues\n * `drawElementsInstanced`, otherwise `drawElements`. No-op on a lost/disposed\n * context or when no program is current.\n *\n * @param ib - The index buffer (also bound as a side-effect, cached).\n * @param indexCount - Number of indices to draw.\n * @param indexStart - First index offset (in indices, not bytes). Default 0.\n * @param instanceCount - Instance count for instanced draws. Default 0\n * (non-instanced).\n */\nexport function drawIndexed(ib: GLIndexBuffer, indexCount: number, indexStart = 0, instanceCount = 0): void {\n const engine = ib._engine;\n if (engine._isLost || engine._disposed || ib._disposed) {\n return;\n }\n if (engine._state.currentProgram === null) {\n return;\n }\n bindIndexBuffer(ib);\n const gl = engine.gl;\n const type = ib.is32Bits ? UNSIGNED_INT : UNSIGNED_SHORT;\n const byteOffset = indexStart * (ib.is32Bits ? 4 : 2);\n applyGLStates(engine);\n if (instanceCount > 0) {\n gl.drawElementsInstanced(TRIANGLES, indexCount, type, byteOffset, instanceCount);\n } else {\n gl.drawElements(TRIANGLES, indexCount, type, byteOffset);\n }\n}\n\n/* ────────────────────────── static mesh VAO ─────────────────────────────\n * The VAO-cached counterpart of the manual `bindAttributes` path above — Babylon's\n * `recordVertexArrayObject` / `_cachedVertexArrayObject` for STATIC meshes. A\n * `GLMeshVao` records the whole attribute layout (enable + pointer + divisor for\n * every attribute, across one or more vertex buffers) AND the index binding into a\n * single VAO ONCE. The per-frame draw is then just `bindVertexArray` +\n * `drawElements[Instanced]` — zero per-draw `vertexAttribPointer`/`vertexAttribDivisor`,\n * and no `unbindInstanceAttributes` (the VAO isolates the divisors from the default\n * VAO). Use this for fixed-layout meshes; keep `bindAttributes` for dynamic layouts\n * (e.g. ShapeBuilder's per-draw sliding-window tape buffers). */\n\n/** One vertex buffer + its attribute layout, for {@link createMeshVao}. */\nexport interface GLMeshVertexBuffer {\n /** The buffer supplying this group's attributes. */\n buffer: GLVertexBuffer;\n /** The attributes read from `buffer` (same shape as {@link bindAttributes}). */\n attributes: readonly GLAttributeDescriptor[];\n /** Stride mode (see {@link bindAttributes}): `false` (default) → stride `0`\n * per attribute; `true` → interleaved stride = Σ(`size`·4). */\n computeStride?: boolean;\n}\n\n/** A recorded Vertex Array Object capturing a static mesh's attribute layout +\n * index binding. Bind + draw it with {@link drawMesh} each frame — the GPU\n * replays the entire attribute setup from one `bindVertexArray`. Re-recorded\n * automatically on `webglcontextrestored`. */\nexport interface GLMeshVao {\n /** The live `WebGLVertexArrayObject`. Swapped on `webglcontextrestored`. */\n handle: WebGLVertexArrayObject;\n /** The engine that created (and owns) this VAO. Operations on it derive\n * their engine from here, so they take the VAO alone. @internal */\n _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n /** @internal The index buffer recorded into the VAO. */\n _indexBuffer: GLIndexBuffer;\n /** @internal Resolved attribute bindings, reused to re-record on restore. */\n _bindings: _ResolvedBinding[];\n /** @internal `webglcontextrestored` re-record hook. */\n _restore: () => void;\n}\n\n/** @internal A single attribute with its location resolved once at record time\n * (locations are stable across re-link via `bindAttribLocation`, so the VAO can\n * re-record on context-restore without the effect). */\ninterface _ResolvedAttr {\n loc: number;\n size: number;\n type: number;\n normalized: boolean;\n stride: number;\n offset: number;\n divisor: number;\n}\n\n/** @internal A vertex buffer + its resolved attributes. */\ninterface _ResolvedBinding {\n buffer: GLVertexBuffer;\n attrs: _ResolvedAttr[];\n}\n\n/**\n * Record a static mesh's attribute layout + index binding into a new VAO. The\n * effect MUST be ready (its attribute locations are resolved here, once). Returns\n * a {@link GLMeshVao} to draw with {@link drawMesh}.\n *\n * @param engine - The engine.\n * @param vertexBuffers - One or more buffers + their attribute layouts.\n * @param indexBuffer - The index buffer recorded into the VAO.\n * @param effect - The effect whose attribute locations resolve unnamed indices.\n * @returns The recorded {@link GLMeshVao}.\n */\nexport function createMeshVao(engine: GLEngineContext, vertexBuffers: readonly GLMeshVertexBuffer[], indexBuffer: GLIndexBuffer, effect: GLEffect): GLMeshVao {\n const gl = engine.gl;\n // The effect's attribute locations are resolved (once) below; an unready effect\n // would resolve every name to -1 → a silently-empty VAO. Fail fast — callers\n // must gate on isEffectReady (see scene10's first-ready-frame creation).\n if (!effect.isReady) {\n throw new Error(\"lite-gl: createMeshVao requires a ready effect (poll isEffectReady first)\");\n }\n const handle = gl.createVertexArray();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createVertexArray returned null (mesh VAO)\");\n }\n // Resolve every attribute location ONCE (stable across re-link), so a restore\n // re-record needs only the buffer handles, not the effect.\n const bindings: _ResolvedBinding[] = [];\n for (const vbb of vertexBuffers) {\n let stride = 0;\n if (vbb.computeStride) {\n for (let i = 0; i < vbb.attributes.length; i++) {\n stride += (vbb.attributes[i] as GLAttributeDescriptor).size * 4;\n }\n }\n const attrs: _ResolvedAttr[] = [];\n for (let i = 0; i < vbb.attributes.length; i++) {\n const d = vbb.attributes[i] as GLAttributeDescriptor;\n const loc = d.index !== undefined ? d.index : d.name !== undefined ? getEffectAttributeLocation(effect, d.name) : -1;\n if (loc < 0) {\n continue;\n }\n attrs.push({\n loc,\n size: d.size,\n type: d.type ?? FLOAT,\n normalized: d.normalized ?? false,\n stride,\n offset: d.offset ?? 0,\n divisor: d.divisor === undefined ? 1 : d.divisor,\n });\n }\n bindings.push({ buffer: vbb.buffer, attrs });\n }\n const vao: GLMeshVao = { handle, _engine: engine, _disposed: false, _indexBuffer: indexBuffer, _bindings: bindings, _restore: () => {} };\n vao._restore = (): void => {\n if (engine._isLost || engine._disposed || vao._disposed) {\n return;\n }\n const fresh = engine.gl.createVertexArray();\n if (fresh === null) {\n return;\n }\n vao.handle = fresh;\n recordMeshVao(engine, vao);\n };\n recordMeshVao(engine, vao);\n onContextRestored(engine, vao._restore);\n return vao;\n}\n\n/**\n * Bind a {@link GLMeshVao} (cached `gl.bindVertexArray`). Rarely called directly —\n * {@link drawMesh} binds it for you. Binding restores the VAO's recorded element\n * binding, so the element-buffer cache is updated in lock-step.\n *\n * @param vao - The mesh VAO to bind.\n */\nexport function bindMeshVao(vao: GLMeshVao): void {\n const engine = vao._engine;\n if (engine._isLost || engine._disposed || vao._disposed) {\n return;\n }\n const s = engine._state;\n if (s.boundVao === vao.handle) {\n return;\n }\n engine.gl.bindVertexArray(vao.handle);\n s.boundVao = vao.handle;\n s.boundElementBuffer = vao._indexBuffer.handle;\n}\n\n/**\n * Draw a static mesh recorded with {@link createMeshVao}: binds its VAO (cached),\n * flushes deferred GL state, and issues ONE `drawElements` (or\n * `drawElementsInstanced` when `instanceCount > 0`) over the VAO's full index\n * buffer. No per-draw attribute (re)binding. No-op on a lost/disposed context, a\n * disposed VAO, or when no program is current.\n *\n * @param vao - The mesh VAO to draw.\n * @param instanceCount - Instance count for instanced draws. Default 0 (non-instanced).\n */\nexport function drawMesh(vao: GLMeshVao, instanceCount = 0): void {\n const engine = vao._engine;\n if (engine._isLost || engine._disposed || vao._disposed) {\n return;\n }\n if (engine._state.currentProgram === null) {\n return;\n }\n bindMeshVao(vao);\n applyGLStates(engine);\n const ib = vao._indexBuffer;\n const gl = engine.gl;\n const type = ib.is32Bits ? UNSIGNED_INT : UNSIGNED_SHORT;\n if (instanceCount > 0) {\n gl.drawElementsInstanced(TRIANGLES, ib.count, type, 0, instanceCount);\n } else {\n gl.drawElements(TRIANGLES, ib.count, type, 0);\n }\n}\n\n/** Dispose a {@link GLMeshVao}: delete the VAO and unregister its restore hook.\n * Does NOT dispose the vertex/index buffers (the caller owns those). Idempotent. */\nexport function disposeMeshVao(vao: GLMeshVao): void {\n const engine = vao._engine;\n if (vao._disposed) {\n return;\n }\n vao._disposed = true;\n offContextRestored(engine, vao._restore);\n if (!engine._isLost && !engine._disposed) {\n const s = engine._state;\n engine.gl.deleteVertexArray(vao.handle);\n if (s.boundVao === vao.handle) {\n // Return to the default VAO so the cache stays coherent.\n engine.gl.bindVertexArray(null);\n s.boundVao = null;\n s.boundElementBuffer = null;\n }\n }\n}\n\n/** @internal Record `vao._bindings` + the index binding into `vao.handle`. Leaves\n * the default (null) VAO bound afterwards (so the mesh VAO is not left current),\n * forgetting the element-buffer cache. Touches neither `s.enabledAttribs` (those\n * track the DEFAULT VAO) nor the divisor reset — all of that lives in the VAO. */\nfunction recordMeshVao(engine: GLEngineContext, vao: GLMeshVao): void {\n const gl = engine.gl;\n const s = engine._state;\n gl.bindVertexArray(vao.handle);\n s.boundVao = vao.handle;\n for (const b of vao._bindings) {\n bindArrayBufferRaw(engine, b.buffer.handle);\n for (const a of b.attrs) {\n gl.enableVertexAttribArray(a.loc);\n gl.vertexAttribPointer(a.loc, a.size, a.type, a.normalized, a.stride, a.offset);\n gl.vertexAttribDivisor(a.loc, a.divisor);\n }\n }\n gl.bindBuffer(ELEMENT_ARRAY_BUFFER, vao._indexBuffer.handle);\n gl.bindVertexArray(null);\n s.boundVao = null;\n s.boundElementBuffer = null;\n}\n\n/* ──────────────────────────── internal helpers ──────────────────────────── */\n\n/** Bind the default (null) VAO if not already bound. The mesh path lives here so\n * it never disturbs the quad / sprite VAO state. Binding a VAO restores ITS\n * element-array binding, so the element-buffer cache is forgotten on the\n * switch (the next `bindIndexBuffer` re-binds). */\nfunction bindDefaultVao(engine: GLEngineContext): void {\n const s = engine._state;\n if (s.boundVao !== null) {\n engine.gl.bindVertexArray(null);\n s.boundVao = null;\n s.boundElementBuffer = null;\n }\n}\n\n/** Cached `gl.bindBuffer(ARRAY_BUFFER, …)`. ARRAY_BUFFER binding is global (not\n * VAO state), so this cache is coherent across VAO switches. */\nfunction bindArrayBufferRaw(engine: GLEngineContext, handle: WebGLBuffer): void {\n const s = engine._state;\n if (s.boundArrayBuffer === handle) {\n return;\n }\n engine.gl.bindBuffer(ARRAY_BUFFER, handle);\n s.boundArrayBuffer = handle;\n}\n"]}
1
+ {"version":3,"file":"mesh.js","sourceRoot":"","sources":["../src/mesh.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,EAAE,0BAA0B,EAAiB,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,4BAA4B;AAC5B,MAAM,YAAY,GAAG,MAAM,CAAC;AAC5B,oCAAoC;AACpC,MAAM,oBAAoB,GAAG,MAAM,CAAC;AACpC,2BAA2B;AAC3B,MAAM,WAAW,GAAG,MAAM,CAAC;AAC3B,4BAA4B;AAC5B,MAAM,YAAY,GAAG,MAAM,CAAC;AAC5B,qBAAqB;AACrB,MAAM,KAAK,GAAG,MAAM,CAAC;AACrB,yBAAyB;AACzB,MAAM,SAAS,GAAG,MAAM,CAAC;AACzB,8BAA8B;AAC9B,MAAM,cAAc,GAAG,MAAM,CAAC;AAC9B,4BAA4B;AAC5B,MAAM,YAAY,GAAG,MAAM,CAAC;AA0E5B,iFAAiF;AAEjF;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAuB,EAAE,IAAkB,EAAE,OAAO,GAAG,KAAK;IAC3F,MAAM,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AO,CAAC,CAAC;QAC7C,CAAC;IACL,CAAC;IACD,EAAE,CAAC,UAAU,CAAC,oBAAoB,EAAE,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;IAC7D,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC,CAAC,QAAQ,GAAG,IAAI,CAAC;IAClB,CAAC,CAAC,kBAAkB,GAAG,IAAI,CAAC;AAChC,CAAC;AAED,kFAAkF;AAElF;;;oDAGoD;AACpD,SAAS,cAAc,CAAC,MAAuB;IAC3C,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,IAAI,CAAC,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtB,MAAM,CAAC,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;QAChC,CAAC,CAAC,QAAQ,GAAG,IAAI,CAAC;QAClB,CAAC,CAAC,kBAAkB,GAAG,IAAI,CAAC;IAChC,CAAC;AACL,CAAC;AAED;iEACiE;AACjE,SAAS,kBAAkB,CAAC,MAAuB,EAAE,MAAmB;IACpE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,IAAI,CAAC,CAAC,gBAAgB,KAAK,MAAM,EAAE,CAAC;QAChC,OAAO;IACX,CAAC;IACD,MAAM,CAAC,EAAE,CAAC,UAAU,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IAC3C,CAAC,CAAC,gBAAgB,GAAG,MAAM,CAAC;AAChC,CAAC","sourcesContent":["/**\n * Indexed meshes, dynamic vertex/index buffers, and hardware instancing.\n *\n * Part of the public API via the `@babylonjs/lite-gl` barrel. The package is\n * `sideEffects: false`, so consumers that only render the fullscreen quad /\n * sprites tree-shake the mesh code out.\n *\n * This is the lite-gl equivalent of Babylon's `ThinEngine.createVertexBuffer` /\n * `createIndexBuffer` / `_releaseBuffer` / `bindIndexBuffer` /\n * `bindInstancesBuffer` / `unbindInstanceAttributes` / `drawElementsType`. The\n * attribute binder reproduces Babylon's instancing semantics EXACTLY, including\n * the `computeStride = false → stride 0` \"sliding window\" used by distance-field\n * tape buffers (consecutive instances read overlapping vec4 windows) and the\n * `divisor === undefined → 1` default.\n *\n * The mesh path runs on the DEFAULT (null) VAO — every buffer/attribute op binds\n * `gl.bindVertexArray(null)` first, so it never corrupts the quad / sprite VAOs.\n * Unless the engine is host-owned (`createHostOwnedGLEngine`), vertex/index buffers retain their CPU\n * data and re-upload automatically on `webglcontextrestored`.\n */\nimport type { GLEngineContext } from \"./context.js\";\nimport { onContextRestored, offContextRestored } from \"./context.js\";\nimport { getEffectAttributeLocation, type GLEffect } from \"./effect.js\";\nimport { applyGLStates } from \"./apply-states.js\";\n\n/** GL `gl.ARRAY_BUFFER`. */\nconst ARRAY_BUFFER = 0x8892;\n/** GL `gl.ELEMENT_ARRAY_BUFFER`. */\nconst ELEMENT_ARRAY_BUFFER = 0x8893;\n/** GL `gl.STATIC_DRAW`. */\nconst STATIC_DRAW = 0x88e4;\n/** GL `gl.DYNAMIC_DRAW`. */\nconst DYNAMIC_DRAW = 0x88e8;\n/** GL `gl.FLOAT`. */\nconst FLOAT = 0x1406;\n/** GL `gl.TRIANGLES`. */\nconst TRIANGLES = 0x0004;\n/** GL `gl.UNSIGNED_SHORT`. */\nconst UNSIGNED_SHORT = 0x1403;\n/** GL `gl.UNSIGNED_INT`. */\nconst UNSIGNED_INT = 0x1405;\n\n/** A GPU vertex buffer holding interleaved float vertex data. The lite-gl\n * counterpart of Babylon's `DataBuffer` for vertex data. */\nexport interface GLVertexBuffer {\n /** The live `WebGLBuffer`. Swapped on `webglcontextrestored`. */\n handle: WebGLBuffer;\n /** Size of the GL buffer in bytes. */\n byteLength: number;\n /** The engine that created (and owns) this buffer. Operations on it derive\n * their engine from here, so they take the buffer alone. @internal */\n _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n /** @internal Delete the underlying GL buffer. */\n _deleteGpu: (gl: WebGL2RenderingContext) => void;\n /** @internal Re-create + re-upload the retained CPU data into a fresh handle. */\n _restore?: (engine: GLEngineContext) => void;\n /** @internal Retained CPU data for context-restore replay (the creation /\n * last full-buffer upload). */\n _data?: Float32Array;\n /** @internal `true` for `DYNAMIC_DRAW` (updated via `updateVertexBuffer`). */\n _dynamic: boolean;\n}\n\n/** A GPU index buffer. The lite-gl counterpart of Babylon's `DataBuffer` for\n * index data. */\nexport interface GLIndexBuffer {\n /** The live `WebGLBuffer`. Swapped on `webglcontextrestored`. */\n handle: WebGLBuffer;\n /** Number of indices. */\n count: number;\n /** `true` for 32-bit (`Uint32Array`) indices, `false` for 16-bit. */\n is32Bits: boolean;\n /** The engine that created (and owns) this buffer. Operations on it derive\n * their engine from here, so they take the buffer alone. @internal */\n _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n /** @internal Delete the underlying GL buffer. */\n _deleteGpu: (gl: WebGL2RenderingContext) => void;\n /** @internal Re-create + re-upload the retained CPU data into a fresh handle. */\n _restore?: (engine: GLEngineContext) => void;\n /** @internal Retained CPU data for context-restore replay. */\n _data?: Uint16Array | Uint32Array;\n}\n\n/**\n * Describes one vertex attribute fed from a buffer — the lite-gl equivalent of\n * Babylon's `InstancingAttributeInfo`. Pass an array of these to\n * {@link bindAttributes}.\n */\nexport interface GLAttributeDescriptor {\n /** Attribute name; resolved to a location via the effect when `index` is\n * omitted (`Effect.getAttributeLocationByName`). */\n name?: string;\n /** Explicit attribute location. When set, overrides the `name` lookup. */\n index?: number;\n /** Number of components, 1–4. */\n size: number;\n /** Byte offset of this attribute's first element within the buffer. Default 0. */\n offset?: number;\n /**\n * Per-instance vertex divisor. **Omitted/`undefined` → `1` (instanced)**,\n * matching Babylon's `bindInstancesBuffer`. Pass `0` explicitly for a\n * per-vertex attribute (e.g. the base mesh position).\n */\n divisor?: number;\n /** GL component type. Default `gl.FLOAT`. */\n type?: GLenum;\n /** Normalize fixed-point integer data to `[0,1]`/`[-1,1]`. Default `false`. */\n normalized?: boolean;\n}\n\n/* ──────────────────────────────── buffers ──────────────────────────────── */\n\n/**\n * Create a GPU vertex buffer from interleaved float data.\n *\n * @param engine - The engine.\n * @param data - The vertex data. Retained by reference unless the engine opts out of context restoration — do\n * not mutate it in place; use {@link updateVertexBuffer} to change contents.\n * @param dynamic - Hint that the buffer will be updated frequently\n * (`DYNAMIC_DRAW`). Default `false` (`STATIC_DRAW`).\n * @returns The new {@link GLVertexBuffer}.\n */\nexport function createVertexBuffer(engine: GLEngineContext, data: Float32Array, dynamic = false): GLVertexBuffer {\n const gl = engine.gl;\n const handle = gl.createBuffer();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createBuffer returned null (vertex buffer)\");\n }\n const vb: GLVertexBuffer = {\n handle,\n byteLength: data.byteLength,\n _dynamic: dynamic,\n _engine: engine,\n _disposed: false,\n _deleteGpu: (g: WebGL2RenderingContext): void => {\n g.deleteBuffer(vb.handle);\n },\n };\n if (!engine._noRestore) {\n vb._data = data;\n vb._restore = (target: GLEngineContext): void => {\n const g = target.gl;\n const fresh = g.createBuffer();\n if (fresh === null) {\n return;\n }\n vb.handle = fresh;\n bindArrayBufferRaw(target, fresh);\n g.bufferData(ARRAY_BUFFER, vb._data!, vb._dynamic ? DYNAMIC_DRAW : STATIC_DRAW);\n };\n }\n bindArrayBufferRaw(engine, handle);\n gl.bufferData(ARRAY_BUFFER, data, dynamic ? DYNAMIC_DRAW : STATIC_DRAW);\n engine._buffers.push(vb);\n return vb;\n}\n\n/**\n * Upload new contents into (part of) a vertex buffer via `bufferSubData`. A\n * full-buffer update from offset 0 also refreshes the retained CPU data used\n * for context-restore unless the engine opts out. Neither mode copies the input.\n *\n * @param vb - The vertex buffer to update.\n * @param data - The new float data.\n * @param dstByteOffset - Destination byte offset within the buffer. Default 0.\n */\nexport function updateVertexBuffer(vb: GLVertexBuffer, data: Float32Array, dstByteOffset = 0): void {\n const engine = vb._engine;\n if (engine._isLost || engine._disposed || vb._disposed) {\n return;\n }\n bindArrayBufferRaw(engine, vb.handle);\n engine.gl.bufferSubData(ARRAY_BUFFER, dstByteOffset, data);\n if (dstByteOffset === 0 && data.byteLength >= vb.byteLength) {\n if (!engine._noRestore) {\n vb._data = data;\n }\n vb.byteLength = data.byteLength;\n }\n}\n\n/**\n * Create a GPU index buffer. `Uint16Array` → 16-bit indices, `Uint32Array` →\n * 32-bit. Binds the default VAO first so it never corrupts the quad / sprite\n * VAO element bindings.\n *\n * @param engine - The engine.\n * @param data - The index data. Retained by reference unless the engine opts out of context restoration.\n * @returns The new {@link GLIndexBuffer}.\n */\nexport function createIndexBuffer(engine: GLEngineContext, data: Uint16Array | Uint32Array): GLIndexBuffer {\n const gl = engine.gl;\n const handle = gl.createBuffer();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createBuffer returned null (index buffer)\");\n }\n const is32 = data instanceof Uint32Array;\n const ib: GLIndexBuffer = {\n handle,\n count: data.length,\n is32Bits: is32,\n _engine: engine,\n _disposed: false,\n _deleteGpu: (g: WebGL2RenderingContext): void => {\n g.deleteBuffer(ib.handle);\n },\n };\n if (!engine._noRestore) {\n ib._data = data;\n ib._restore = (target: GLEngineContext): void => {\n const g = target.gl;\n const fresh = g.createBuffer();\n if (fresh === null) {\n return;\n }\n ib.handle = fresh;\n bindDefaultVao(target);\n g.bindBuffer(ELEMENT_ARRAY_BUFFER, fresh);\n target._state.boundElementBuffer = fresh;\n g.bufferData(ELEMENT_ARRAY_BUFFER, ib._data!, STATIC_DRAW);\n };\n }\n bindDefaultVao(engine);\n gl.bindBuffer(ELEMENT_ARRAY_BUFFER, handle);\n engine._state.boundElementBuffer = handle;\n gl.bufferData(ELEMENT_ARRAY_BUFFER, data, STATIC_DRAW);\n engine._buffers.push(ib);\n return ib;\n}\n\n/** Dispose a vertex or index buffer (delete the GL buffer + unregister). Clears\n * the array/element-buffer cache slot if it pointed at this buffer and releases\n * retained upload/restore references in both recovery modes. Idempotent. */\nexport function disposeBuffer(buffer: GLVertexBuffer | GLIndexBuffer): void {\n const engine = buffer._engine;\n if (buffer._disposed) {\n return;\n }\n buffer._disposed = true;\n buffer._data = buffer._restore = undefined;\n const i = engine._buffers.indexOf(buffer);\n if (i !== -1) {\n engine._buffers.splice(i, 1);\n }\n const s = engine._state;\n if (!engine._isLost && !engine._disposed) {\n engine.gl.deleteBuffer(buffer.handle);\n }\n if (s.boundArrayBuffer === buffer.handle) {\n s.boundArrayBuffer = null;\n }\n if (s.boundElementBuffer === buffer.handle) {\n s.boundElementBuffer = null;\n }\n for (let location = 0; location < s.enabledAttribs.length; location++) {\n if (s.enabledAttribs[location] === buffer.handle) {\n s.enabledAttribs[location] = true;\n }\n }\n}\n\n/* ────────────────────────────── binding + draw ──────────────────────────── */\n\n/**\n * Bind an index buffer as the current element-array buffer (on the default\n * VAO). Cached. The lite-gl equivalent of Babylon's `_bindIndexBufferWithCache`.\n *\n * @param ib - The index buffer to bind.\n */\nexport function bindIndexBuffer(ib: GLIndexBuffer): void {\n const engine = ib._engine;\n if (engine._isLost || engine._disposed || ib._disposed) {\n return;\n }\n bindDefaultVao(engine);\n const s = engine._state;\n if (s.boundElementBuffer === ib.handle) {\n return;\n }\n engine.gl.bindBuffer(ELEMENT_ARRAY_BUFFER, ib.handle);\n s.boundElementBuffer = ib.handle;\n}\n\n/**\n * Configure vertex attributes from `vb`, reproducing Babylon's\n * `bindInstancesBuffer` exactly. For each descriptor: resolves the location\n * (explicit `index` or via the effect), enables the attribute array (cached),\n * issues `vertexAttribPointer`, and sets the vertex divisor (`undefined → 1`).\n * Each instanced location (divisor ≠ 0) is tracked once so {@link unbindInstanceAttributes}\n * can reset its divisor afterwards; per-vertex locations already have divisor 0 and are\n * not tracked, so callers that never instance can rebind every frame without growth.\n *\n * `computeStride` controls the GL stride passed to `vertexAttribPointer`:\n * - `false` (default) → stride `0`: each attribute is independently tightly\n * packed. Combined with overlapping `offset`s this yields the \"sliding window\"\n * used by distance-field tape buffers.\n * - `true` → stride = Σ(`size`·4 bytes): interleaved per-vertex/per-instance.\n *\n * Every call re-issues `vertexAttribPointer`. Callers that rebind an equivalent\n * layout every draw (e.g. a 2D shape renderer) should use {@link bindAttributesCached},\n * which elides unchanged pointers; the two can be mixed freely.\n *\n * Runs on the default (null) VAO — never corrupts the quad / sprite VAOs.\n * No-op on a lost/disposed context or before the effect is ready.\n *\n * @param vb - The buffer supplying the attribute data.\n * @param descriptors - The attribute layout.\n * @param effect - The effect whose attribute locations resolve unnamed indices.\n * @param computeStride - See above. Default `false`.\n */\nexport function bindAttributes(vb: GLVertexBuffer, descriptors: readonly GLAttributeDescriptor[], effect: GLEffect, computeStride = false): void {\n const engine = vb._engine;\n if (engine._isLost || engine._disposed || vb._disposed || !effect.isReady) {\n return;\n }\n const gl = engine.gl;\n const s = engine._state;\n bindDefaultVao(engine);\n bindArrayBufferRaw(engine, vb.handle);\n\n let stride = 0;\n if (computeStride) {\n for (let i = 0; i < descriptors.length; i++) {\n stride += (descriptors[i] as GLAttributeDescriptor).size * 4;\n }\n }\n\n for (let i = 0; i < descriptors.length; i++) {\n const d = descriptors[i] as GLAttributeDescriptor;\n const loc = d.index !== undefined ? d.index : d.name !== undefined ? getEffectAttributeLocation(effect, d.name) : -1;\n if (loc < 0) {\n continue;\n }\n if (!s.enabledAttribs[loc]) {\n gl.enableVertexAttribArray(loc);\n }\n // Enabled, but with a pointer bindAttributesCached must not trust.\n s.enabledAttribs[loc] = true;\n gl.vertexAttribPointer(loc, d.size, d.type ?? FLOAT, d.normalized ?? false, stride, d.offset ?? 0);\n const divisor = d.divisor ?? 1;\n gl.vertexAttribDivisor(loc, divisor);\n if (divisor !== 0 && !s.instanceLocations.includes(loc)) {\n s.instanceLocations.push(loc);\n }\n }\n}\n\n/**\n * {@link bindAttributes} with a default-VAO attribute cache: `vertexAttribPointer`\n * is issued only when a location's (buffer handle, size, type, normalized,\n * stride, offset) tuple changes, and `vertexAttribDivisor` only when its divisor\n * changes, so rebinding an equivalent layout every draw (e.g. per-shape binds in a\n * 2D shape renderer) issues no attribute calls. Divisors are tracked through\n * `instanceLocations`, so {@link unbindInstanceAttributes} needs no cache code.\n * Semantics, parameters and no-op conditions are\n * otherwise identical to {@link bindAttributes}.\n *\n * The cache (4 numbers per `MAX_VERTEX_ATTRIBS` location) is allocated once on\n * the first effective call; later binds allocate nothing. Disabling an\n * attribute, {@link bindAttributes}, cache wipes/resets and context loss\n * invalidate the affected tuples, and a recreated buffer handle never matches\n * a stale one. Named VAOs keep independent state.\n *\n * @param vb - The buffer supplying the attribute data.\n * @param descriptors - The attribute layout.\n * @param effect - The effect whose attribute locations resolve unnamed indices.\n * @param computeStride - See {@link bindAttributes}. Default `false`.\n */\nexport function bindAttributesCached(vb: GLVertexBuffer, descriptors: readonly GLAttributeDescriptor[], effect: GLEffect, computeStride = false): void {\n const engine = vb._engine;\n if (engine._isLost || engine._disposed || vb._disposed || !effect.isReady) {\n return;\n }\n const gl = engine.gl;\n const s = engine._state;\n bindDefaultVao(engine);\n bindArrayBufferRaw(engine, vb.handle);\n const values = (s._attributePointers ??= new Float64Array((gl.getParameter(gl.MAX_VERTEX_ATTRIBS) as number) * 4));\n\n let stride = 0;\n if (computeStride) {\n for (let i = 0; i < descriptors.length; i++) {\n stride += (descriptors[i] as GLAttributeDescriptor).size * 4;\n }\n }\n\n for (let i = 0; i < descriptors.length; i++) {\n const d = descriptors[i] as GLAttributeDescriptor;\n const loc = d.index !== undefined ? d.index : d.name !== undefined ? getEffectAttributeLocation(effect, d.name) : -1;\n if (loc < 0) {\n continue;\n }\n const enabled = s.enabledAttribs[loc];\n if (!enabled) {\n gl.enableVertexAttribArray(loc);\n }\n const type = d.type ?? FLOAT;\n const normalized = d.normalized ?? false;\n const offset = d.offset ?? 0;\n // A GLenum fits in 32 bits; packing the valid 1..4 size and normalize\n // flag leaves one exact (53-bit) number instead of three cache slots.\n const format = type * 16 + d.size * 2 + +normalized;\n const slot = loc * 4;\n if (enabled !== vb.handle || values[slot] !== format || values[slot + 1] !== stride || values[slot + 2] !== offset) {\n gl.vertexAttribPointer(loc, d.size, type, normalized, stride, offset);\n values[slot] = format;\n values[slot + 1] = stride;\n values[slot + 2] = offset;\n }\n const divisor = d.divisor ?? 1;\n // While this binder owns the location (a buffer handle, not `true`/null),\n // every non-zero divisor it sets is listed in instanceLocations, and only\n // unbindInstanceAttributes (which resets listed divisors to 0) or a cache\n // reset (which also drops ownership) clears that list. An owned, unlisted\n // location therefore has divisor 0; a listed one has the cached divisor.\n const listed = s.instanceLocations.includes(loc);\n if (!enabled || enabled === true || (listed ? values[slot + 3] : 0) !== divisor) {\n gl.vertexAttribDivisor(loc, divisor);\n values[slot + 3] = divisor;\n if (divisor !== 0 && !listed) {\n s.instanceLocations.push(loc);\n }\n }\n s.enabledAttribs[loc] = vb.handle;\n }\n}\n\n/**\n * Disable an enabled attribute on the default VAO, eliding redundant calls.\n * Leaves its divisor unchanged, like ThinEngine's `disableAttributeByIndex`;\n * use {@link unbindInstanceAttributes} for divisor cleanup. Named VAOs are\n * untouched. No-op on a lost/disposed engine. The enable cache is cleared on\n * context loss or cache reset, so subsequent binds replay their enables. After\n * a plain `wipeGLStateCache`, a location lite-gl has not confirmed\n * disabled is treated as unknown (the host may have enabled it) and is disabled.\n *\n * @param engine - The engine.\n * @param location - The attribute location to disable.\n */\nexport function disableAttributeByIndex(engine: GLEngineContext, location: number): void {\n const s = engine._state;\n const enabled = s.enabledAttribs[location];\n // `-1` is the \"optimized out\" sentinel from `getAttribLocation`: always a no-op.\n if (location < 0 || engine._isLost || engine._disposed || (s._unknownAttribs ? enabled === null : !enabled)) {\n return;\n }\n bindDefaultVao(engine);\n engine.gl.disableVertexAttribArray(location);\n s.enabledAttribs[location] = null;\n}\n\n/**\n * Disable all enabled default-VAO attributes in ascending location order.\n * Cached across frames and allocation-free; named VAOs and instance divisors\n * are unchanged (ThinEngine's `unbindAllAttributes` semantics). No-op on a\n * lost/disposed engine. Context loss/cache reset clears the enable cache;\n * subsequent {@link bindAttributes} calls replay their enables. The first call\n * after a plain `wipeGLStateCache` disables every location up to\n * `MAX_VERTEX_ATTRIBS` once, since the host may have enabled any of them.\n *\n * @param engine - The engine.\n */\nexport function unbindAllAttributes(engine: GLEngineContext): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const s = engine._state;\n const count = s._unknownAttribs ? (engine.gl.getParameter(engine.gl.MAX_VERTEX_ATTRIBS) as number) : s.enabledAttribs.length;\n for (let location = 0; location < count; location++) {\n disableAttributeByIndex(engine, location);\n }\n s._unknownAttribs = false;\n}\n\n/**\n * Reset the vertex divisor of every attribute touched by {@link bindAttributes}\n * or {@link bindAttributesCached} back to 0 — the lite-gl equivalent of Babylon's `unbindInstanceAttributes`.\n * Call after an instanced draw so a following non-instanced draw is not skewed.\n * The divisors are reset on the default VAO (selected if a named VAO is bound),\n * where these binders set them. No-op on a lost/disposed context.\n *\n * @param engine - The engine.\n */\nexport function unbindInstanceAttributes(engine: GLEngineContext): void {\n if (engine._isLost || engine._disposed) {\n return;\n }\n const locs = engine._state.instanceLocations;\n if (locs.length === 0) {\n return;\n }\n // The listed divisors live on the default VAO; a named VAO bound since then\n // owns its own divisors and must not be reset.\n bindDefaultVao(engine);\n const gl = engine.gl;\n for (let i = 0; i < locs.length; i++) {\n gl.vertexAttribDivisor(locs[i] as number, 0);\n }\n locs.length = 0;\n}\n\n/**\n * Draw indexed triangles from `ib` — the lite-gl equivalent of Babylon's\n * `drawElementsType` (triangle fill mode). When `instanceCount > 0` issues\n * `drawElementsInstanced`, otherwise `drawElements`. No-op on a lost/disposed\n * context or when no program is current.\n *\n * @param ib - The index buffer (also bound as a side-effect, cached).\n * @param indexCount - Number of indices to draw.\n * @param indexStart - First index offset (in indices, not bytes). Default 0.\n * @param instanceCount - Instance count for instanced draws. Default 0\n * (non-instanced).\n */\nexport function drawIndexed(ib: GLIndexBuffer, indexCount: number, indexStart = 0, instanceCount = 0): void {\n const engine = ib._engine;\n if (engine._isLost || engine._disposed || ib._disposed) {\n return;\n }\n if (engine._state.currentProgram === null) {\n return;\n }\n bindIndexBuffer(ib);\n const gl = engine.gl;\n const type = ib.is32Bits ? UNSIGNED_INT : UNSIGNED_SHORT;\n const byteOffset = indexStart * (ib.is32Bits ? 4 : 2);\n applyGLStates(engine);\n if (instanceCount > 0) {\n gl.drawElementsInstanced(TRIANGLES, indexCount, type, byteOffset, instanceCount);\n } else {\n gl.drawElements(TRIANGLES, indexCount, type, byteOffset);\n }\n}\n\n/* ────────────────────────── static mesh VAO ─────────────────────────────\n * The VAO-cached counterpart of the manual `bindAttributes` path above — Babylon's\n * `recordVertexArrayObject` / `_cachedVertexArrayObject` for STATIC meshes. A\n * `GLMeshVao` records the whole attribute layout (enable + pointer + divisor for\n * every attribute, across one or more vertex buffers) AND the index binding into a\n * single VAO ONCE. The per-frame draw is then just `bindVertexArray` +\n * `drawElements[Instanced]` — zero per-draw `vertexAttribPointer`/`vertexAttribDivisor`,\n * and no `unbindInstanceAttributes` (the VAO isolates the divisors from the default\n * VAO). Use this for fixed-layout meshes; keep `bindAttributes` for dynamic layouts\n * (e.g. per-draw sliding-window tape buffers). */\n\n/** One vertex buffer + its attribute layout, for {@link createMeshVao}. */\nexport interface GLMeshVertexBuffer {\n /** The buffer supplying this group's attributes. */\n buffer: GLVertexBuffer;\n /** The attributes read from `buffer` (same shape as {@link bindAttributes}). */\n attributes: readonly GLAttributeDescriptor[];\n /** Stride mode (see {@link bindAttributes}): `false` (default) → stride `0`\n * per attribute; `true` → interleaved stride = Σ(`size`·4). */\n computeStride?: boolean;\n}\n\n/** A recorded Vertex Array Object capturing a static mesh's attribute layout +\n * index binding. Bind + draw it with {@link drawMesh} each frame — the GPU\n * replays the entire attribute setup from one `bindVertexArray`. Re-recorded\n * automatically on `webglcontextrestored` unless the engine is host-owned\n * (`createHostOwnedGLEngine`). */\nexport interface GLMeshVao {\n /** The live `WebGLVertexArrayObject`. Swapped on `webglcontextrestored`. */\n handle: WebGLVertexArrayObject;\n /** The engine that created (and owns) this VAO. Operations on it derive\n * their engine from here, so they take the VAO alone. @internal */\n _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n /** @internal The index buffer recorded into the VAO. */\n _indexBuffer: GLIndexBuffer;\n /** @internal Resolved attribute bindings, reused to re-record on restore. */\n _bindings: _ResolvedBinding[];\n /** @internal `webglcontextrestored` re-record hook. */\n _restore?: () => void;\n}\n\n/** @internal A single attribute with its location resolved once at record time\n * (locations are stable across re-link via `bindAttribLocation`, so the VAO can\n * re-record on context-restore without the effect). */\ninterface _ResolvedAttr {\n loc: number;\n size: number;\n type: number;\n normalized: boolean;\n stride: number;\n offset: number;\n divisor: number;\n}\n\n/** @internal A vertex buffer + its resolved attributes. */\ninterface _ResolvedBinding {\n buffer: GLVertexBuffer;\n attrs: _ResolvedAttr[];\n}\n\n/**\n * Record a static mesh's attribute layout + index binding into a new VAO. The\n * effect MUST be ready (its attribute locations are resolved here, once). Returns\n * a {@link GLMeshVao} to draw with {@link drawMesh}.\n *\n * @param engine - The engine.\n * @param vertexBuffers - One or more buffers + their attribute layouts.\n * @param indexBuffer - The index buffer recorded into the VAO.\n * @param effect - The effect whose attribute locations resolve unnamed indices.\n * @returns The recorded {@link GLMeshVao}.\n */\nexport function createMeshVao(engine: GLEngineContext, vertexBuffers: readonly GLMeshVertexBuffer[], indexBuffer: GLIndexBuffer, effect: GLEffect): GLMeshVao {\n const gl = engine.gl;\n // The effect's attribute locations are resolved (once) below; an unready effect\n // would resolve every name to -1 → a silently-empty VAO. Fail fast — callers\n // must gate on isEffectReady (see scene10's first-ready-frame creation).\n if (!effect.isReady) {\n throw new Error(\"lite-gl: createMeshVao requires a ready effect (poll isEffectReady first)\");\n }\n const handle = gl.createVertexArray();\n if (handle === null) {\n throw new Error(\"lite-gl: gl.createVertexArray returned null (mesh VAO)\");\n }\n // Resolve every attribute location ONCE (stable across re-link), so a restore\n // re-record needs only the buffer handles, not the effect.\n const bindings: _ResolvedBinding[] = [];\n for (const vbb of vertexBuffers) {\n let stride = 0;\n if (vbb.computeStride) {\n for (let i = 0; i < vbb.attributes.length; i++) {\n stride += (vbb.attributes[i] as GLAttributeDescriptor).size * 4;\n }\n }\n const attrs: _ResolvedAttr[] = [];\n for (let i = 0; i < vbb.attributes.length; i++) {\n const d = vbb.attributes[i] as GLAttributeDescriptor;\n const loc = d.index !== undefined ? d.index : d.name !== undefined ? getEffectAttributeLocation(effect, d.name) : -1;\n if (loc < 0) {\n continue;\n }\n attrs.push({\n loc,\n size: d.size,\n type: d.type ?? FLOAT,\n normalized: d.normalized ?? false,\n stride,\n offset: d.offset ?? 0,\n divisor: d.divisor === undefined ? 1 : d.divisor,\n });\n }\n bindings.push({ buffer: vbb.buffer, attrs });\n }\n const vao: GLMeshVao = { handle, _engine: engine, _disposed: false, _indexBuffer: indexBuffer, _bindings: bindings };\n if (!engine._noRestore) {\n vao._restore = (): void => {\n if (engine._isLost || engine._disposed || vao._disposed) {\n return;\n }\n const fresh = engine.gl.createVertexArray();\n if (fresh === null) {\n return;\n }\n vao.handle = fresh;\n recordMeshVao(engine, vao);\n };\n onContextRestored(engine, vao._restore);\n }\n recordMeshVao(engine, vao);\n if (engine._noRestore) {\n bindings.length = 0;\n }\n return vao;\n}\n\n/**\n * Bind a {@link GLMeshVao} (cached `gl.bindVertexArray`). Rarely called directly —\n * {@link drawMesh} binds it for you. Binding restores the VAO's recorded element\n * binding, so the element-buffer cache is updated in lock-step.\n *\n * @param vao - The mesh VAO to bind.\n */\nexport function bindMeshVao(vao: GLMeshVao): void {\n const engine = vao._engine;\n if (engine._isLost || engine._disposed || vao._disposed) {\n return;\n }\n const s = engine._state;\n if (s.boundVao === vao.handle) {\n return;\n }\n engine.gl.bindVertexArray(vao.handle);\n s.boundVao = vao.handle;\n s.boundElementBuffer = vao._indexBuffer.handle;\n}\n\n/**\n * Draw a static mesh recorded with {@link createMeshVao}: binds its VAO (cached),\n * flushes deferred GL state, and issues ONE `drawElements` (or\n * `drawElementsInstanced` when `instanceCount > 0`) over the VAO's full index\n * buffer. No per-draw attribute (re)binding. No-op on a lost/disposed context, a\n * disposed VAO, or when no program is current.\n *\n * @param vao - The mesh VAO to draw.\n * @param instanceCount - Instance count for instanced draws. Default 0 (non-instanced).\n * @param indexStart - First index in the retained mesh.\n * @param indexCount - Number of indices; defaults to the indices from `indexStart` to the end of the mesh.\n * @throws RangeError when the range is not whole indices within the mesh.\n */\nexport function drawMesh(vao: GLMeshVao, instanceCount = 0, indexStart = 0, indexCount = vao._indexBuffer.count - indexStart): void {\n const total = vao._indexBuffer.count;\n if (!Number.isInteger(indexStart) || !Number.isInteger(indexCount) || indexStart < 0 || indexCount < 0 || indexStart + indexCount > total) {\n throw new RangeError(`lite-gl: drawMesh range (start ${indexStart}, count ${indexCount}) must be whole indices within the mesh's ${total}`);\n }\n const engine = vao._engine;\n if (engine._isLost || engine._disposed || vao._disposed) {\n return;\n }\n if (engine._state.currentProgram === null) {\n return;\n }\n bindMeshVao(vao);\n applyGLStates(engine);\n const ib = vao._indexBuffer;\n const gl = engine.gl;\n const type = ib.is32Bits ? UNSIGNED_INT : UNSIGNED_SHORT;\n if (instanceCount > 0) {\n gl.drawElementsInstanced(TRIANGLES, indexCount, type, indexStart * (ib.is32Bits ? 4 : 2), instanceCount);\n } else {\n gl.drawElements(TRIANGLES, indexCount, type, indexStart * (ib.is32Bits ? 4 : 2));\n }\n}\n\n/** Dispose a {@link GLMeshVao}: delete the VAO and unregister its restore hook.\n * Does NOT dispose the vertex/index buffers (the caller owns those). Idempotent. */\nexport function disposeMeshVao(vao: GLMeshVao): void {\n const engine = vao._engine;\n if (vao._disposed) {\n return;\n }\n vao._disposed = true;\n if (vao._restore) {\n offContextRestored(engine, vao._restore);\n }\n if (!engine._isLost && !engine._disposed) {\n const s = engine._state;\n engine.gl.deleteVertexArray(vao.handle);\n if (s.boundVao === vao.handle) {\n // Return to the default VAO so the cache stays coherent.\n engine.gl.bindVertexArray(null);\n s.boundVao = null;\n s.boundElementBuffer = null;\n }\n }\n}\n\n/** @internal Record `vao._bindings` + the index binding into `vao.handle`. Leaves\n * the default (null) VAO bound afterwards (so the mesh VAO is not left current),\n * forgetting the element-buffer cache. Touches neither `s.enabledAttribs` (those\n * track the DEFAULT VAO) nor the divisor reset — all of that lives in the VAO. */\nfunction recordMeshVao(engine: GLEngineContext, vao: GLMeshVao): void {\n const gl = engine.gl;\n const s = engine._state;\n gl.bindVertexArray(vao.handle);\n s.boundVao = vao.handle;\n for (const b of vao._bindings) {\n bindArrayBufferRaw(engine, b.buffer.handle);\n for (const a of b.attrs) {\n gl.enableVertexAttribArray(a.loc);\n gl.vertexAttribPointer(a.loc, a.size, a.type, a.normalized, a.stride, a.offset);\n gl.vertexAttribDivisor(a.loc, a.divisor);\n }\n }\n gl.bindBuffer(ELEMENT_ARRAY_BUFFER, vao._indexBuffer.handle);\n gl.bindVertexArray(null);\n s.boundVao = null;\n s.boundElementBuffer = null;\n}\n\n/* ──────────────────────────── internal helpers ──────────────────────────── */\n\n/** Bind the default (null) VAO if not already bound. The mesh path lives here so\n * it never disturbs the quad / sprite VAO state. Binding a VAO restores ITS\n * element-array binding, so the element-buffer cache is forgotten on the\n * switch (the next `bindIndexBuffer` re-binds). */\nfunction bindDefaultVao(engine: GLEngineContext): void {\n const s = engine._state;\n if (s.boundVao !== null) {\n engine.gl.bindVertexArray(null);\n s.boundVao = null;\n s.boundElementBuffer = null;\n }\n}\n\n/** Cached `gl.bindBuffer(ARRAY_BUFFER, …)`. ARRAY_BUFFER binding is global (not\n * VAO state), so this cache is coherent across VAO switches. */\nfunction bindArrayBufferRaw(engine: GLEngineContext, handle: WebGLBuffer): void {\n const s = engine._state;\n if (s.boundArrayBuffer === handle) {\n return;\n }\n engine.gl.bindBuffer(ARRAY_BUFFER, handle);\n s.boundArrayBuffer = handle;\n}\n"]}
@@ -0,0 +1 @@
1
+ export {};