@babylonjs/lite-gl 1.20.0 → 1.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +58 -19
  2. package/context.js +2 -0
  3. package/context.js.map +1 -1
  4. package/depth-stencil.d.ts +1 -2
  5. package/depth-stencil.js +2 -2
  6. package/depth-stencil.js.map +1 -1
  7. package/dynamic-texture.d.ts +18 -18
  8. package/dynamic-texture.js +28 -24
  9. package/dynamic-texture.js.map +1 -1
  10. package/effect-ready.d.ts +1 -3
  11. package/effect-ready.js +3 -3
  12. package/effect-ready.js.map +1 -1
  13. package/effect-renderer.d.ts +39 -12
  14. package/effect-renderer.js +47 -17
  15. package/effect-renderer.js.map +1 -1
  16. package/effect.d.ts +25 -21
  17. package/effect.js +69 -51
  18. package/effect.js.map +1 -1
  19. package/gpu-frame-time.d.ts +49 -0
  20. package/gpu-frame-time.js +130 -0
  21. package/gpu-frame-time.js.map +1 -0
  22. package/html-texture.d.ts +24 -34
  23. package/html-texture.js +34 -55
  24. package/html-texture.js.map +1 -1
  25. package/index.d.ts +9 -8
  26. package/index.js +9 -6
  27. package/index.js.map +1 -1
  28. package/mesh.d.ts +8 -14
  29. package/mesh.js +23 -19
  30. package/mesh.js.map +1 -1
  31. package/package.json +3 -3
  32. package/render-loop.d.ts +14 -0
  33. package/render-loop.js +23 -0
  34. package/render-loop.js.map +1 -1
  35. package/render-target.d.ts +109 -39
  36. package/render-target.js +136 -58
  37. package/render-target.js.map +1 -1
  38. package/sprites.d.ts +8 -2
  39. package/sprites.js +17 -13
  40. package/sprites.js.map +1 -1
  41. package/state.d.ts +1 -1
  42. package/state.js.map +1 -1
  43. package/text/_gpu/text-effect.js +5 -5
  44. package/text/_gpu/text-effect.js.map +1 -1
  45. package/text/_gpu/text-instances.js +4 -4
  46. package/text/_gpu/text-instances.js.map +1 -1
  47. package/text/_gpu/text-textures.js +5 -3
  48. package/text/_gpu/text-textures.js.map +1 -1
  49. package/text/glyph-storage.js +3 -3
  50. package/text/glyph-storage.js.map +1 -1
  51. package/text/text-data.js +2 -2
  52. package/text/text-data.js.map +1 -1
  53. package/text/text-renderer.js +11 -11
  54. package/text/text-renderer.js.map +1 -1
  55. package/texture-3d.d.ts +10 -7
  56. package/texture-3d.js +27 -12
  57. package/texture-3d.js.map +1 -1
  58. package/texture-source.d.ts +3 -4
  59. package/texture-source.js +5 -5
  60. package/texture-source.js.map +1 -1
  61. package/texture.d.ts +195 -31
  62. package/texture.js +215 -35
  63. package/texture.js.map +1 -1
@@ -12,8 +12,9 @@
12
12
  * depth and/or stencil renderbuffer, all wrapped in a single
13
13
  * `WebGLFramebuffer`.
14
14
  *
15
- * The default {@link createRenderTarget} makes an **RGBA8** color target and
16
- * ships none of the HDR sized-format knowledge; {@link createFloatRenderTarget}
15
+ * The default {@link createRenderTarget} makes an 8-bit **RGBA8** (or, via
16
+ * `format`, `R8` / `RG8`) color target and ships none of the HDR sized-format
17
+ * knowledge; {@link createFloatRenderTarget}
17
18
  * is the separate opt-in for float / half-float color attachments (it alone
18
19
  * carries the `RGBA16F` / `RGBA32F` table, so RGBA8 consumers tree-shake it
19
20
  * away).
@@ -27,21 +28,21 @@
27
28
  */
28
29
  import { type GLTexture } from "./texture.js";
29
30
  import type { GLEngineContext } from "./context.js";
30
- /** Options for {@link createRenderTarget}. `width`/`height` are required; every
31
- * other field has a Babylon-matching default. The bare
32
- * `createRenderTarget(engine, { width, height })` makes an RGBA8 color-only
33
- * target with linear filtering and clamp wrapping. */
31
+ /** Options for {@link createRenderTarget}. Every field has a Babylon-matching
32
+ * default: the bare `createRenderTarget(engine, width, height)` makes an RGBA8
33
+ * color-only target with linear filtering and clamp wrapping. */
34
34
  export interface GLRenderTargetOptions {
35
- /** Color attachment width in texels. Must be a positive integer. */
36
- width: number;
37
- /** Color attachment height in texels. Must be a positive integer. */
38
- height: number;
39
35
  /** Allocate a depth renderbuffer (`DEPTH_COMPONENT16`). Default `false`.
40
36
  * Stencil is NOT a create option — opt in (packed depth+stencil, or
41
37
  * stencil-only) via `generateRenderTargetStencil`
42
38
  * (`@babylonjs/lite-gl`), which keeps the stencil/packed
43
39
  * renderbuffer code out of the render-target core bundle. */
44
40
  generateDepthBuffer?: boolean;
41
+ /** 8-bit color attachment format: `gl.RGBA` (default, `RGBA8`), `gl.RG`
42
+ * (`RG8`) or `gl.RED` (`R8`) — ≙ Babylon's `RenderTargetCreationOptions.format`.
43
+ * Ignored when {@link colorTexture} is supplied. Read single/two-channel
44
+ * targets tightly via `readRenderTargetPixels(rt, { format })`. */
45
+ format?: GLenum;
45
46
  /** Color texture minification filter. Default `gl.LINEAR`. */
46
47
  minFilter?: GLenum;
47
48
  /** Color texture magnification filter. Default `gl.LINEAR`. */
@@ -54,18 +55,50 @@ export interface GLRenderTargetOptions {
54
55
  * one. When supplied the render target does NOT own or restore it — the
55
56
  * texture is engine-managed (restored by the standard texture-restore path
56
57
  * first) and the RT re-attaches its swapped handle afterwards. The caller is
57
- * responsible for sizing it to `width`×`height` and for disposing it. */
58
+ * responsible for sizing it to `width`×`height` and for disposing it.
59
+ * Readbacks use the creator's texel type, so a float / half-float texture
60
+ * must be attached via `createFloatRenderTarget` with a matching `type`
61
+ * (`createRenderTarget` reads as `UNSIGNED_BYTE`). */
58
62
  colorTexture?: GLTexture;
59
63
  }
60
64
  /** Options for {@link createFloatRenderTarget} — {@link GLRenderTargetOptions}
61
- * plus the float color `type`. */
62
- export interface GLFloatRenderTargetOptions extends GLRenderTargetOptions {
65
+ * (always RGBA, so without `format`) plus the float color `type`. */
66
+ export interface GLFloatRenderTargetOptions extends Omit<GLRenderTargetOptions, "format"> {
63
67
  /** Float color attachment type. Default `gl.HALF_FLOAT`. Pass `gl.FLOAT` for
64
68
  * full 32-bit. Downgraded to the best renderable type the engine supports
65
69
  * (`caps.textureFloatRender` / `caps.textureHalfFloatRender`), mirroring
66
70
  * Babylon's `getTextureType`. */
67
71
  type?: GLenum;
68
72
  }
73
+ /** Read rectangle and channel layout shared by {@link readRenderTargetPixels}
74
+ * and {@link readRenderTargetPixelsToBuffer}. Every field is optional: the
75
+ * defaults read the whole target as RGBA. */
76
+ export interface GLReadPixelsRegionOptions {
77
+ /** Lower-left X of the read rectangle, in texels. Default `0`. */
78
+ x?: number;
79
+ /** Lower-left Y of the read rectangle, in texels. Default `0`. */
80
+ y?: number;
81
+ /** Read rectangle width in texels. Default: the target's width. */
82
+ width?: number;
83
+ /** Read rectangle height in texels. Default: the target's height. */
84
+ height?: number;
85
+ /** Read format: `gl.RGBA` (default, 4 channels), `gl.RG` (2) or `gl.RED` (1). */
86
+ format?: GLenum;
87
+ }
88
+ /** Options for {@link readRenderTargetPixels}. Every field is optional: the bare
89
+ * `readRenderTargetPixels(rt)` reads the whole target as RGBA into a fresh
90
+ * buffer. */
91
+ export interface GLReadPixelsOptions extends GLReadPixelsRegionOptions {
92
+ /** Preallocated buffer (`width*height*channels` elements of the matching
93
+ * type), reused to avoid per-call allocation. Default: a fresh buffer. */
94
+ into?: ArrayBufferView;
95
+ }
96
+ /** Options for {@link readRenderTargetPixelsToBuffer}. */
97
+ export interface GLReadPixelsToBufferOptions extends GLReadPixelsRegionOptions {
98
+ /** Byte offset into the destination `WebGLBuffer` where the first texel is
99
+ * written. Must be a multiple of the read type's size. Default `0`. */
100
+ offset?: number;
101
+ }
69
102
  /**
70
103
  * An offscreen render target — a `WebGLFramebuffer` wrapping a color
71
104
  * {@link GLTexture} and an optional depth / stencil renderbuffer. The lite-gl
@@ -85,20 +118,24 @@ export interface GLRenderTarget {
85
118
  isReady: boolean;
86
119
  }
87
120
  /**
88
- * Create an offscreen **RGBA8** render target.
121
+ * Create an offscreen 8-bit render target — **RGBA8** by default, or `R8` /
122
+ * `RG8` via {@link GLRenderTargetOptions.format}.
89
123
  *
90
124
  * The color attachment is an owned {@link GLTexture} (rebuilt by this target's
91
125
  * own restore hook), unless {@link GLRenderTargetOptions.colorTexture} supplies a
92
126
  * caller-managed (BYO) one. Mirrors Babylon's `createRenderTargetTexture`.
93
127
  *
94
128
  * @param engine - The engine to allocate GL resources on.
95
- * @param options - See {@link GLRenderTargetOptions} (`width`/`height` required).
129
+ * @param width - Color attachment width in texels. Must be a positive integer.
130
+ * @param height - Color attachment height in texels. Must be a positive integer.
131
+ * @param options - See {@link GLRenderTargetOptions}.
96
132
  * @returns The new {@link GLRenderTarget}.
97
- * @throws If `width`/`height` are not positive integers, a GL handle could not
98
- * be allocated, or the resulting framebuffer is not complete. On failure every
99
- * partial GPU object (including an owned color texture) is released first.
133
+ * @throws If `format` is not `RGBA` / `RG` / `RED`, `width`/`height` are not
134
+ * positive integers, a GL handle could not be allocated, or the resulting
135
+ * framebuffer is not complete. On failure every partial GPU object (including
136
+ * an owned color texture) is released first.
100
137
  */
101
- export declare function createRenderTarget(engine: GLEngineContext, options: GLRenderTargetOptions): GLRenderTarget;
138
+ export declare function createRenderTarget(engine: GLEngineContext, width: number, height: number, options?: GLRenderTargetOptions): GLRenderTarget;
102
139
  /**
103
140
  * Create an offscreen **float / half-float** render target — the HDR opt-in
104
141
  * counterpart of {@link createRenderTarget}. This is the only render-target
@@ -110,31 +147,39 @@ export declare function createRenderTarget(engine: GLEngineContext, options: GLR
110
147
  * supports (mirroring Babylon's `getTextureType`).
111
148
  *
112
149
  * @param engine - The engine to allocate GL resources on.
150
+ * @param width - Color attachment width in texels. Must be a positive integer.
151
+ * @param height - Color attachment height in texels. Must be a positive integer.
113
152
  * @param options - See {@link GLFloatRenderTargetOptions}.
114
153
  * @returns The new {@link GLRenderTarget}.
115
154
  * @throws As {@link createRenderTarget}.
116
155
  */
117
- export declare function createFloatRenderTarget(engine: GLEngineContext, options: GLFloatRenderTargetOptions): GLRenderTarget;
156
+ export declare function createFloatRenderTarget(engine: GLEngineContext, width: number, height: number, options?: GLFloatRenderTargetOptions): GLRenderTarget;
118
157
  /**
119
158
  * Bind the render target's framebuffer as the draw target and set the viewport
120
- * to cover it. `rt = null` binds the default (canvas) framebuffer and resets the
121
- * viewport to the full canvas — the counterpart of Babylon's
122
- * `restoreDefaultFramebuffer`. Subsequent `drawEffect` / `drawIndexed` /
123
- * `clearEngine` calls write into the bound target.
159
+ * to cover it. Use {@link restoreDefaultFramebuffer} to return to the canvas.
160
+ * Subsequent `drawEffect` / `drawIndexed` / `clearEngine` calls write into the
161
+ * bound target.
124
162
  *
125
163
  * Cached. No-op on a lost/disposed context or a disposed `rt`. Mipmaps are NOT
126
164
  * regenerated here — refresh a target's mip chain explicitly via
127
165
  * {@link generateRenderTargetMipMaps} after rendering into it.
128
166
  *
167
+ * @param rt - The render target to draw into.
168
+ */
169
+ export declare function bindRenderTarget(rt: GLRenderTarget): void;
170
+ /**
171
+ * Bind the default (canvas) framebuffer and reset the viewport to the full
172
+ * canvas — the counterpart of Babylon's `restoreDefaultFramebuffer`. Cached;
173
+ * no-op on a lost/disposed context.
174
+ *
129
175
  * @param engine - The engine.
130
- * @param rt - The render target to draw into, or `null` for the canvas.
131
176
  */
132
- export declare function bindRenderTarget(engine: GLEngineContext, rt: GLRenderTarget | null): void;
177
+ export declare function restoreDefaultFramebuffer(engine: GLEngineContext): void;
133
178
  /** Regenerate a render target's color-attachment mip chain from its (freshly
134
179
  * rendered) level-0 — mipmaps for render targets are a pure manual opt-in
135
180
  * (call this after rendering into the target). No-op for a disposed target, a
136
181
  * handle-less color attachment, or a lost/disposed context. */
137
- export declare function generateRenderTargetMipMaps(engine: GLEngineContext, rt: GLRenderTarget): void;
182
+ export declare function generateRenderTargetMipMaps(rt: GLRenderTarget): void;
138
183
  /**
139
184
  * Resize the render target's color attachment (and depth/stencil renderbuffer).
140
185
  * Reallocates storage at the new size; the contents are discarded. The
@@ -146,12 +191,11 @@ export declare function generateRenderTargetMipMaps(engine: GLEngineContext, rt:
146
191
  * lost the new size is recorded but the GL reallocation is deferred to the
147
192
  * restore hook.
148
193
  *
149
- * @param engine - The engine.
150
194
  * @param rt - The render target to resize.
151
195
  * @param width - New width in texels (≥ 1).
152
196
  * @param height - New height in texels (≥ 1).
153
197
  */
154
- export declare function resizeRenderTarget(engine: GLEngineContext, rt: GLRenderTarget, width: number, height: number): void;
198
+ export declare function resizeRenderTarget(rt: GLRenderTarget, width: number, height: number): void;
155
199
  /**
156
200
  * Synchronously read back a rectangle of the render target's color attachment
157
201
  * via `gl.readPixels` — the lite-gl equivalent of Babylon's
@@ -162,18 +206,45 @@ export declare function resizeRenderTarget(engine: GLEngineContext, rt: GLRender
162
206
  * `Uint8Array` for `UNSIGNED_BYTE`, `Float32Array` for `FLOAT`, `Uint16Array`
163
207
  * for `HALF_FLOAT`. Origin is GL bottom-left.
164
208
  *
165
- * @param engine - The engine.
209
+ * Reads `RGBA` by default (like Babylon; always readable, a `RED` target reads
210
+ * back as `R,0,0,1`). Pass `format` `gl.RED` / `gl.RG` to read 1 / 2 channels
211
+ * per texel into a tightly packed buffer (`PACK_ALIGNMENT` is set to 1 and left
212
+ * there — lite-gl does not otherwise use it). WebGL only guarantees non-RGBA
213
+ * reads when they match the implementation-chosen
214
+ * `gl.IMPLEMENTATION_COLOR_READ_FORMAT` (`RED` for `R8`, `RG` for `RG8` in all
215
+ * major browsers).
216
+ *
166
217
  * @param rt - The render target to read from.
167
- * @param x - Lower-left X of the read rectangle, in texels.
168
- * @param y - Lower-left Y of the read rectangle, in texels.
169
- * @param width - Read rectangle width in texels.
170
- * @param height - Read rectangle height in texels.
171
- * @param into - Optional preallocated buffer (`width*height*4` elements of the
172
- * matching type). Reused to avoid per-call allocation.
218
+ * @param options - See {@link GLReadPixelsOptions}. Default: the whole target,
219
+ * as RGBA, into a fresh buffer.
173
220
  * @returns The pixel buffer (the provided `into`, or a freshly allocated one).
174
221
  * Empty buffer on a lost/disposed context.
175
222
  */
176
- export declare function readRenderTargetPixels(engine: GLEngineContext, rt: GLRenderTarget, x: number, y: number, width: number, height: number, into?: ArrayBufferView): ArrayBufferView;
223
+ export declare function readRenderTargetPixels(rt: GLRenderTarget, options?: GLReadPixelsOptions): ArrayBufferView;
224
+ /**
225
+ * Read a rectangle of the render target's color attachment into a GPU
226
+ * `WebGLBuffer` (`PIXEL_PACK_BUFFER` readback): `gl.readPixels` with a byte
227
+ * offset instead of a CPU array, so the copy stays on the GPU and can be
228
+ * consumed later without a sync stall (e.g. `gl.fenceSync` +
229
+ * `gl.getBufferSubData`, or as vertex data).
230
+ *
231
+ * Same rectangle / `format` rules as {@link readRenderTargetPixels}: whole
232
+ * target as RGBA by default, `gl.RED` / `gl.RG` read 1 / 2 channels per texel
233
+ * tightly packed (`PACK_ALIGNMENT` 1). The texel type follows the color
234
+ * attachment type. `buffer` must already be allocated (`gl.bufferData`) large
235
+ * enough for `offset + width*height*channels*bytesPerChannel` bytes, and (per
236
+ * WebGL2) must never have been bound to `ELEMENT_ARRAY_BUFFER`.
237
+ *
238
+ * Binds the target's framebuffer (left bound, like
239
+ * {@link readRenderTargetPixels}), binds `buffer` to `PIXEL_PACK_BUFFER` for
240
+ * the read and then unbinds it (`null`), so later CPU readbacks keep working.
241
+ * No-op on a lost/disposed context or a disposed target.
242
+ *
243
+ * @param rt - The render target to read from.
244
+ * @param buffer - Destination `WebGLBuffer`, created on `rt`'s context.
245
+ * @param options - See {@link GLReadPixelsToBufferOptions}.
246
+ */
247
+ export declare function readRenderTargetPixelsToBuffer(rt: GLRenderTarget, buffer: WebGLBuffer, options?: GLReadPixelsToBufferOptions): void;
177
248
  /**
178
249
  * Release the render target's framebuffer, depth/stencil renderbuffer and (iff
179
250
  * owned) color texture, and unregister it from the engine. Idempotent, and a
@@ -184,7 +255,6 @@ export declare function readRenderTargetPixels(engine: GLEngineContext, rt: GLRe
184
255
  * A BYO {@link GLRenderTargetOptions.colorTexture} is NOT disposed here — it is
185
256
  * engine-managed and the caller owns its lifetime.
186
257
  *
187
- * @param engine - The engine.
188
258
  * @param rt - The render target to dispose, or `null`/`undefined` for a no-op.
189
259
  */
190
- export declare function disposeRenderTarget(engine: GLEngineContext, rt: GLRenderTarget | null | undefined): void;
260
+ export declare function disposeRenderTarget(rt: GLRenderTarget | null | undefined): void;
package/render-target.js CHANGED
@@ -12,8 +12,9 @@
12
12
  * depth and/or stencil renderbuffer, all wrapped in a single
13
13
  * `WebGLFramebuffer`.
14
14
  *
15
- * The default {@link createRenderTarget} makes an **RGBA8** color target and
16
- * ships none of the HDR sized-format knowledge; {@link createFloatRenderTarget}
15
+ * The default {@link createRenderTarget} makes an 8-bit **RGBA8** (or, via
16
+ * `format`, `R8` / `RG8`) color target and ships none of the HDR sized-format
17
+ * knowledge; {@link createFloatRenderTarget}
17
18
  * is the separate opt-in for float / half-float color attachments (it alone
18
19
  * carries the `RGBA16F` / `RGBA32F` table, so RGBA8 consumers tree-shake it
19
20
  * away).
@@ -25,7 +26,7 @@
25
26
  * (BYO) color texture lives in the engine's `_textures` registry and is restored
26
27
  * there first; the RT then re-attaches its freshly-swapped handle.
27
28
  */
28
- import { bindTextureForUpload, pickSizedInternalFormat, setBoundTextureParams } from "./texture.js";
29
+ import { bindTextureForUpload, pickSizedInternalFormat, resolveLdrInternalFormat, setBoundTextureParams } from "./texture.js";
29
30
  /** GL `gl.UNSIGNED_BYTE` — the default (RGBA8) color attachment type. */
30
31
  const UNSIGNED_BYTE = 0x1401;
31
32
  /** GL `gl.HALF_FLOAT`. */
@@ -34,28 +35,39 @@ const HALF_FLOAT = 0x140b;
34
35
  const FLOAT = 0x1406;
35
36
  /** GL `gl.RGBA`. */
36
37
  const RGBA = 0x1908;
37
- /** GL `gl.RGBA8` — the default sized internalFormat. */
38
- const RGBA8 = 0x8058;
38
+ /** GL `gl.RED`. */
39
+ const RED = 0x1903;
40
+ /** GL `gl.RG`. */
41
+ const RG = 0x8227;
39
42
  /** GL `gl.LINEAR`. */
40
43
  const LINEAR = 0x2601;
41
44
  /** GL `gl.CLAMP_TO_EDGE`. */
42
45
  const CLAMP_TO_EDGE = 0x812f;
43
46
  /**
44
- * Create an offscreen **RGBA8** render target.
47
+ * Create an offscreen 8-bit render target — **RGBA8** by default, or `R8` /
48
+ * `RG8` via {@link GLRenderTargetOptions.format}.
45
49
  *
46
50
  * The color attachment is an owned {@link GLTexture} (rebuilt by this target's
47
51
  * own restore hook), unless {@link GLRenderTargetOptions.colorTexture} supplies a
48
52
  * caller-managed (BYO) one. Mirrors Babylon's `createRenderTargetTexture`.
49
53
  *
50
54
  * @param engine - The engine to allocate GL resources on.
51
- * @param options - See {@link GLRenderTargetOptions} (`width`/`height` required).
55
+ * @param width - Color attachment width in texels. Must be a positive integer.
56
+ * @param height - Color attachment height in texels. Must be a positive integer.
57
+ * @param options - See {@link GLRenderTargetOptions}.
52
58
  * @returns The new {@link GLRenderTarget}.
53
- * @throws If `width`/`height` are not positive integers, a GL handle could not
54
- * be allocated, or the resulting framebuffer is not complete. On failure every
55
- * partial GPU object (including an owned color texture) is released first.
59
+ * @throws If `format` is not `RGBA` / `RG` / `RED`, `width`/`height` are not
60
+ * positive integers, a GL handle could not be allocated, or the resulting
61
+ * framebuffer is not complete. On failure every partial GPU object (including
62
+ * an owned color texture) is released first.
56
63
  */
57
- export function createRenderTarget(engine, options) {
58
- return buildRT(engine, options, RGBA8, RGBA, UNSIGNED_BYTE);
64
+ export function createRenderTarget(engine, width, height, options) {
65
+ const opts = options ?? {};
66
+ const format = opts.format ?? RGBA;
67
+ if (format !== RGBA && format !== RG && format !== RED) {
68
+ throw new Error(`lite-gl: unsupported render target format 0x${format.toString(16)}`);
69
+ }
70
+ return buildRT(engine, width, height, opts, resolveLdrInternalFormat(engine.gl, format, UNSIGNED_BYTE), format, UNSIGNED_BYTE);
59
71
  }
60
72
  /**
61
73
  * Create an offscreen **float / half-float** render target — the HDR opt-in
@@ -68,56 +80,65 @@ export function createRenderTarget(engine, options) {
68
80
  * supports (mirroring Babylon's `getTextureType`).
69
81
  *
70
82
  * @param engine - The engine to allocate GL resources on.
83
+ * @param width - Color attachment width in texels. Must be a positive integer.
84
+ * @param height - Color attachment height in texels. Must be a positive integer.
71
85
  * @param options - See {@link GLFloatRenderTargetOptions}.
72
86
  * @returns The new {@link GLRenderTarget}.
73
87
  * @throws As {@link createRenderTarget}.
74
88
  */
75
- export function createFloatRenderTarget(engine, options) {
76
- const type = resolveColorType(engine, options.type ?? HALF_FLOAT);
89
+ export function createFloatRenderTarget(engine, width, height, options) {
90
+ const opts = options ?? {};
91
+ const type = resolveColorType(engine, opts.type ?? HALF_FLOAT);
77
92
  const internalFormat = pickSizedInternalFormat(engine.gl, RGBA, type);
78
- return buildRT(engine, options, internalFormat, RGBA, type);
93
+ return buildRT(engine, width, height, opts, internalFormat, RGBA, type);
79
94
  }
80
95
  /**
81
96
  * Bind the render target's framebuffer as the draw target and set the viewport
82
- * to cover it. `rt = null` binds the default (canvas) framebuffer and resets the
83
- * viewport to the full canvas — the counterpart of Babylon's
84
- * `restoreDefaultFramebuffer`. Subsequent `drawEffect` / `drawIndexed` /
85
- * `clearEngine` calls write into the bound target.
97
+ * to cover it. Use {@link restoreDefaultFramebuffer} to return to the canvas.
98
+ * Subsequent `drawEffect` / `drawIndexed` / `clearEngine` calls write into the
99
+ * bound target.
86
100
  *
87
101
  * Cached. No-op on a lost/disposed context or a disposed `rt`. Mipmaps are NOT
88
102
  * regenerated here — refresh a target's mip chain explicitly via
89
103
  * {@link generateRenderTargetMipMaps} after rendering into it.
90
104
  *
91
- * @param engine - The engine.
92
- * @param rt - The render target to draw into, or `null` for the canvas.
105
+ * @param rt - The render target to draw into.
93
106
  */
94
- export function bindRenderTarget(engine, rt) {
95
- if (engine._isLost || engine._disposed) {
107
+ export function bindRenderTarget(rt) {
108
+ const engine = rt._engine;
109
+ if (engine._isLost || engine._disposed || rt._disposed) {
96
110
  return;
97
111
  }
98
- if (rt !== null && rt._disposed) {
112
+ bindFramebufferAndViewport(engine, rt, rt._framebuffer, rt.width, rt.height);
113
+ }
114
+ /**
115
+ * Bind the default (canvas) framebuffer and reset the viewport to the full
116
+ * canvas — the counterpart of Babylon's `restoreDefaultFramebuffer`. Cached;
117
+ * no-op on a lost/disposed context.
118
+ *
119
+ * @param engine - The engine.
120
+ */
121
+ export function restoreDefaultFramebuffer(engine) {
122
+ if (engine._isLost || engine._disposed) {
99
123
  return;
100
124
  }
101
- const gl = engine.gl;
125
+ bindFramebufferAndViewport(engine, null, null, engine.canvas.width, engine.canvas.height);
126
+ }
127
+ function bindFramebufferAndViewport(engine, rt, fb, width, height) {
102
128
  const s = engine._state;
103
- const fb = rt === null ? null : rt._framebuffer;
104
129
  if (s.boundFramebuffer !== fb) {
105
- gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
130
+ engine.gl.bindFramebuffer(engine.gl.FRAMEBUFFER, fb);
106
131
  s.boundFramebuffer = fb;
107
132
  }
108
133
  engine._currentRenderTarget = rt;
109
- if (rt === null) {
110
- setViewportCached(engine, 0, 0, engine.canvas.width, engine.canvas.height);
111
- }
112
- else {
113
- setViewportCached(engine, 0, 0, rt.width, rt.height);
114
- }
134
+ setViewportCached(engine, 0, 0, width, height);
115
135
  }
116
136
  /** Regenerate a render target's color-attachment mip chain from its (freshly
117
137
  * rendered) level-0 — mipmaps for render targets are a pure manual opt-in
118
138
  * (call this after rendering into the target). No-op for a disposed target, a
119
139
  * handle-less color attachment, or a lost/disposed context. */
120
- export function generateRenderTargetMipMaps(engine, rt) {
140
+ export function generateRenderTargetMipMaps(rt) {
141
+ const engine = rt._engine;
121
142
  if (engine._isLost || engine._disposed || rt._disposed || rt.texture.handle === null) {
122
143
  return;
123
144
  }
@@ -135,12 +156,12 @@ export function generateRenderTargetMipMaps(engine, rt) {
135
156
  * lost the new size is recorded but the GL reallocation is deferred to the
136
157
  * restore hook.
137
158
  *
138
- * @param engine - The engine.
139
159
  * @param rt - The render target to resize.
140
160
  * @param width - New width in texels (≥ 1).
141
161
  * @param height - New height in texels (≥ 1).
142
162
  */
143
- export function resizeRenderTarget(engine, rt, width, height) {
163
+ export function resizeRenderTarget(rt, width, height) {
164
+ const engine = rt._engine;
144
165
  if (rt._disposed) {
145
166
  return;
146
167
  }
@@ -170,7 +191,7 @@ export function resizeRenderTarget(engine, rt, width, height) {
170
191
  rt._deleteGpu(engine.gl);
171
192
  allocateRenderTargetGpu(engine, rt);
172
193
  if (wasBound) {
173
- bindRenderTarget(engine, rt);
194
+ bindRenderTarget(rt);
174
195
  }
175
196
  }
176
197
  /**
@@ -183,35 +204,92 @@ export function resizeRenderTarget(engine, rt, width, height) {
183
204
  * `Uint8Array` for `UNSIGNED_BYTE`, `Float32Array` for `FLOAT`, `Uint16Array`
184
205
  * for `HALF_FLOAT`. Origin is GL bottom-left.
185
206
  *
186
- * @param engine - The engine.
207
+ * Reads `RGBA` by default (like Babylon; always readable, a `RED` target reads
208
+ * back as `R,0,0,1`). Pass `format` `gl.RED` / `gl.RG` to read 1 / 2 channels
209
+ * per texel into a tightly packed buffer (`PACK_ALIGNMENT` is set to 1 and left
210
+ * there — lite-gl does not otherwise use it). WebGL only guarantees non-RGBA
211
+ * reads when they match the implementation-chosen
212
+ * `gl.IMPLEMENTATION_COLOR_READ_FORMAT` (`RED` for `R8`, `RG` for `RG8` in all
213
+ * major browsers).
214
+ *
187
215
  * @param rt - The render target to read from.
188
- * @param x - Lower-left X of the read rectangle, in texels.
189
- * @param y - Lower-left Y of the read rectangle, in texels.
190
- * @param width - Read rectangle width in texels.
191
- * @param height - Read rectangle height in texels.
192
- * @param into - Optional preallocated buffer (`width*height*4` elements of the
193
- * matching type). Reused to avoid per-call allocation.
216
+ * @param options - See {@link GLReadPixelsOptions}. Default: the whole target,
217
+ * as RGBA, into a fresh buffer.
194
218
  * @returns The pixel buffer (the provided `into`, or a freshly allocated one).
195
219
  * Empty buffer on a lost/disposed context.
196
220
  */
197
- export function readRenderTargetPixels(engine, rt, x, y, width, height, into) {
198
- const elements = width * height * 4;
199
- if (engine._isLost || engine._disposed || rt._disposed) {
221
+ export function readRenderTargetPixels(rt, options) {
222
+ const opts = options ?? {};
223
+ const width = opts.width ?? rt.width;
224
+ const height = opts.height ?? rt.height;
225
+ const format = opts.format ?? RGBA;
226
+ const into = opts.into;
227
+ const gl = bindForRead(rt, format);
228
+ if (gl === null) {
200
229
  return into ?? new Uint8Array(0);
201
230
  }
231
+ const type = rt._config.type;
232
+ let buffer = into;
233
+ if (buffer === undefined) {
234
+ const elements = width * height * (format === RED ? 1 : format === RG ? 2 : 4);
235
+ buffer = type === gl.FLOAT ? new Float32Array(elements) : type === gl.HALF_FLOAT ? new Uint16Array(elements) : new Uint8Array(elements);
236
+ }
237
+ gl.readPixels(opts.x ?? 0, opts.y ?? 0, width, height, format, type, buffer);
238
+ return buffer;
239
+ }
240
+ /**
241
+ * Read a rectangle of the render target's color attachment into a GPU
242
+ * `WebGLBuffer` (`PIXEL_PACK_BUFFER` readback): `gl.readPixels` with a byte
243
+ * offset instead of a CPU array, so the copy stays on the GPU and can be
244
+ * consumed later without a sync stall (e.g. `gl.fenceSync` +
245
+ * `gl.getBufferSubData`, or as vertex data).
246
+ *
247
+ * Same rectangle / `format` rules as {@link readRenderTargetPixels}: whole
248
+ * target as RGBA by default, `gl.RED` / `gl.RG` read 1 / 2 channels per texel
249
+ * tightly packed (`PACK_ALIGNMENT` 1). The texel type follows the color
250
+ * attachment type. `buffer` must already be allocated (`gl.bufferData`) large
251
+ * enough for `offset + width*height*channels*bytesPerChannel` bytes, and (per
252
+ * WebGL2) must never have been bound to `ELEMENT_ARRAY_BUFFER`.
253
+ *
254
+ * Binds the target's framebuffer (left bound, like
255
+ * {@link readRenderTargetPixels}), binds `buffer` to `PIXEL_PACK_BUFFER` for
256
+ * the read and then unbinds it (`null`), so later CPU readbacks keep working.
257
+ * No-op on a lost/disposed context or a disposed target.
258
+ *
259
+ * @param rt - The render target to read from.
260
+ * @param buffer - Destination `WebGLBuffer`, created on `rt`'s context.
261
+ * @param options - See {@link GLReadPixelsToBufferOptions}.
262
+ */
263
+ export function readRenderTargetPixelsToBuffer(rt, buffer, options) {
264
+ const opts = options ?? {};
265
+ const format = opts.format ?? RGBA;
266
+ const gl = bindForRead(rt, format);
267
+ if (gl === null) {
268
+ return;
269
+ }
270
+ gl.bindBuffer(gl.PIXEL_PACK_BUFFER, buffer);
271
+ gl.readPixels(opts.x ?? 0, opts.y ?? 0, opts.width ?? rt.width, opts.height ?? rt.height, format, rt._config.type, opts.offset ?? 0);
272
+ gl.bindBuffer(gl.PIXEL_PACK_BUFFER, null);
273
+ }
274
+ /** Shared readback prologue: bail on a lost/disposed context or target, bind
275
+ * `rt`'s framebuffer (cached), and set `PACK_ALIGNMENT` 1 for the tightly
276
+ * packed `RED` / `RG` reads. Returns the context, or `null` to skip the read. */
277
+ function bindForRead(rt, format) {
278
+ const engine = rt._engine;
279
+ if (engine._isLost || engine._disposed || rt._disposed) {
280
+ return null;
281
+ }
202
282
  const gl = engine.gl;
203
283
  const s = engine._state;
204
284
  if (s.boundFramebuffer !== rt._framebuffer) {
205
285
  gl.bindFramebuffer(gl.FRAMEBUFFER, rt._framebuffer);
206
286
  s.boundFramebuffer = rt._framebuffer;
207
287
  }
208
- const type = rt._config.type;
209
- let buffer = into;
210
- if (buffer === undefined) {
211
- buffer = type === gl.FLOAT ? new Float32Array(elements) : type === gl.HALF_FLOAT ? new Uint16Array(elements) : new Uint8Array(elements);
288
+ if (format !== RGBA) {
289
+ // Not cached: a sync readback dwarfs it, and callers may change it directly.
290
+ gl.pixelStorei(gl.PACK_ALIGNMENT, 1);
212
291
  }
213
- gl.readPixels(x, y, width, height, gl.RGBA, type, buffer);
214
- return buffer;
292
+ return gl;
215
293
  }
216
294
  /**
217
295
  * Release the render target's framebuffer, depth/stencil renderbuffer and (iff
@@ -223,13 +301,13 @@ export function readRenderTargetPixels(engine, rt, x, y, width, height, into) {
223
301
  * A BYO {@link GLRenderTargetOptions.colorTexture} is NOT disposed here — it is
224
302
  * engine-managed and the caller owns its lifetime.
225
303
  *
226
- * @param engine - The engine.
227
304
  * @param rt - The render target to dispose, or `null`/`undefined` for a no-op.
228
305
  */
229
- export function disposeRenderTarget(engine, rt) {
306
+ export function disposeRenderTarget(rt) {
230
307
  if (rt === null || rt === undefined || rt._disposed) {
231
308
  return;
232
309
  }
310
+ const engine = rt._engine;
233
311
  rt._disposed = true;
234
312
  // Only the owned color texture is marked disposed here; a BYO texture stays
235
313
  // live for its caller-owner.
@@ -266,9 +344,7 @@ export function disposeRenderTarget(engine, rt) {
266
344
  * exact sized `internalFormat` by the caller — NO format-table lookup), wires
267
345
  * the owned-or-BYO color texture, allocates the GPU set atomically, and
268
346
  * registers the target. */
269
- function buildRT(engine, options, internalFormat, format, type) {
270
- const width = options.width;
271
- const height = options.height;
347
+ function buildRT(engine, width, height, options, internalFormat, format, type) {
272
348
  validateSize(width, height);
273
349
  const gl = engine.gl;
274
350
  const byo = options.colorTexture;
@@ -289,6 +365,7 @@ function buildRT(engine, options, internalFormat, format, type) {
289
365
  width,
290
366
  height,
291
367
  isReady: false,
368
+ _engine: engine,
292
369
  _disposed: false,
293
370
  _refCount: 1,
294
371
  // Owned-by-RT: its storage is (re)allocated by allocateRenderTargetGpu,
@@ -305,6 +382,7 @@ function buildRT(engine, options, internalFormat, format, type) {
305
382
  _depthStencil: null,
306
383
  _rebuildDepthStencil: undefined,
307
384
  _config: config,
385
+ _engine: engine,
308
386
  _disposed: false,
309
387
  _deleteGpu: () => { },
310
388
  _restore: () => { },