@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
@@ -0,0 +1,135 @@
1
+ /** @internal Capacity of the reusable command buffer, in 32-bit words. */
2
+ export const COMMAND_STREAM_WORDS = 16384;
3
+ function capacityError() {
4
+ return new Error(`Native Lite GL command exceeds its ${(COMMAND_STREAM_WORDS * 4) / 1024} KiB buffer.`);
5
+ }
6
+ /** @internal */
7
+ export class CommandStream {
8
+ runtime;
9
+ engine;
10
+ /** Reusable copy of an array upload, read completely before any of its command is recorded. */
11
+ staging = [];
12
+ /** Changes whenever an array upload claims `staging`, so an interrupted upload can detect the overwrite. */
13
+ stagingGeneration = 0;
14
+ words = new Uint32Array(COMMAND_STREAM_WORDS);
15
+ integers = new Int32Array(this.words.buffer);
16
+ floats = new Float32Array(this.words.buffer);
17
+ position = 0;
18
+ frameActive = false;
19
+ retained = [];
20
+ /** Host textures whose deletion waits until the commands recorded before it are submitted. */
21
+ deletedTextures = [];
22
+ /** @internal */
23
+ _nativeDataStream;
24
+ constructor(runtime, engine) {
25
+ this.runtime = runtime;
26
+ this.engine = engine;
27
+ this._nativeDataStream = new runtime.NativeDataStream(() => this.flush());
28
+ engine.setCommandDataStream(this);
29
+ }
30
+ begin() {
31
+ if (this.frameActive) {
32
+ throw new Error("Native Lite GL frame is already active.");
33
+ }
34
+ this.frameActive = true;
35
+ }
36
+ end() {
37
+ if (!this.frameActive) {
38
+ throw new Error("Native Lite GL frame is not active.");
39
+ }
40
+ this.frameActive = false;
41
+ this.submit();
42
+ }
43
+ start(command) {
44
+ this.handle(command);
45
+ }
46
+ uint(value) {
47
+ this.tag(this.runtime.NativeDataStream.VALIDATION_UINT_32);
48
+ this.reserve(1);
49
+ this.words[this.position++] = value;
50
+ }
51
+ int(value) {
52
+ this.tag(this.runtime.NativeDataStream.VALIDATION_INT_32);
53
+ this.reserve(1);
54
+ this.integers[this.position++] = value;
55
+ }
56
+ float(value) {
57
+ this.tag(this.runtime.NativeDataStream.VALIDATION_FLOAT_32);
58
+ this.reserve(1);
59
+ this.floats[this.position++] = value;
60
+ }
61
+ bool(value) {
62
+ this.tag(this.runtime.NativeDataStream.VALIDATION_BOOLEAN);
63
+ this.reserve(1);
64
+ this.words[this.position++] = value ? 1 : 0;
65
+ }
66
+ /** Encodes the first `length` values. */
67
+ array(values, length) {
68
+ this.tag(this.runtime.NativeDataStream.VALIDATION_FLOAT_32_ARRAY);
69
+ this.reserve(1 + length);
70
+ this.words[this.position++] = length;
71
+ for (let index = 0; index < length; index++) {
72
+ this.floats[this.position++] = values[index];
73
+ }
74
+ }
75
+ handle(value) {
76
+ this.tag(this.runtime.NativeDataStream.VALIDATION_NATIVE_DATA);
77
+ this.reserve(value.length);
78
+ this.words.set(value, this.position);
79
+ this.position += value.length;
80
+ this.retained.push(value);
81
+ }
82
+ finish() {
83
+ if (!this.frameActive) {
84
+ this.submit();
85
+ }
86
+ }
87
+ /** Claims `staging` for an encodable float array of `length` values. */
88
+ beginStaging(length) {
89
+ if (!Number.isInteger(length) || length < 0) {
90
+ throw new RangeError(`Native Lite GL array length must be a nonnegative integer, got ${length}.`);
91
+ }
92
+ if (length + 1 > this.words.length) {
93
+ throw capacityError();
94
+ }
95
+ return ++this.stagingGeneration;
96
+ }
97
+ /** Rejects an upload whose `staging` claim was taken by a nested upload. */
98
+ endStaging(generation) {
99
+ if (generation !== this.stagingGeneration) {
100
+ throw new Error("Native Lite GL array upload was interrupted by a nested array upload while reading its values.");
101
+ }
102
+ }
103
+ /** Deletes host texture storage once every command recorded before this call has been submitted. */
104
+ deleteTexture(texture) {
105
+ this.deletedTextures.push(texture);
106
+ this.finish();
107
+ }
108
+ tag(value) {
109
+ if (this.runtime.NativeDataStream.VALIDATION_ENABLED) {
110
+ this.reserve(1);
111
+ this.words[this.position++] = value;
112
+ }
113
+ }
114
+ reserve(length) {
115
+ if (length > this.words.length) {
116
+ throw capacityError();
117
+ }
118
+ if (this.position + length > this.words.length) {
119
+ this.flush();
120
+ }
121
+ }
122
+ flush() {
123
+ this._nativeDataStream.writeBuffer(this.words.buffer, this.position);
124
+ this.position = 0;
125
+ }
126
+ submit() {
127
+ this.engine.submitCommands();
128
+ this.retained.length = 0;
129
+ for (let index = 0; index < this.deletedTextures.length; index++) {
130
+ this.engine.deleteTexture(this.deletedTextures[index]);
131
+ }
132
+ this.deletedTextures.length = 0;
133
+ }
134
+ }
135
+ //# sourceMappingURL=command-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command-stream.js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C;QACrE,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;IACtB,CAAC;IAEO,MAAM;QACV,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC;QAC7B,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;QACzB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;YAC/D,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,eAAe,CAAC,KAAK,CAAiB,CAAC,CAAC;QAC3E,CAAC;QACD,IAAI,CAAC,eAAe,CAAC,MAAM,GAAG,CAAC,CAAC;IACpC,CAAC;CACJ","sourcesContent":["import type { NativeEngine, NativeHandle, NativeRuntime } from \"./runtime.js\";\n\n/** @internal Capacity of the reusable command buffer, in 32-bit words. */\nexport const COMMAND_STREAM_WORDS = 16384;\n\nfunction capacityError(): Error {\n return new Error(`Native Lite GL command exceeds its ${(COMMAND_STREAM_WORDS * 4) / 1024} KiB buffer.`);\n}\n\n/** @internal */\nexport class CommandStream {\n /** Reusable copy of an array upload, read completely before any of its command is recorded. */\n readonly staging: number[] = [];\n /** Changes whenever an array upload claims `staging`, so an interrupted upload can detect the overwrite. */\n private stagingGeneration = 0;\n private readonly words = new Uint32Array(COMMAND_STREAM_WORDS);\n private readonly integers = new Int32Array(this.words.buffer);\n private readonly floats = new Float32Array(this.words.buffer);\n private position = 0;\n private frameActive = false;\n private readonly retained: NativeHandle[] = [];\n /** Host textures whose deletion waits until the commands recorded before it are submitted. */\n private readonly deletedTextures: NativeHandle[] = [];\n /** @internal */\n readonly _nativeDataStream;\n\n constructor(\n private readonly runtime: NativeRuntime,\n private readonly engine: NativeEngine\n ) {\n this._nativeDataStream = new runtime.NativeDataStream(() => this.flush());\n engine.setCommandDataStream(this);\n }\n\n begin(): void {\n if (this.frameActive) {\n throw new Error(\"Native Lite GL frame is already active.\");\n }\n this.frameActive = true;\n }\n\n end(): void {\n if (!this.frameActive) {\n throw new Error(\"Native Lite GL frame is not active.\");\n }\n this.frameActive = false;\n this.submit();\n }\n\n start(command: NativeHandle): void {\n this.handle(command);\n }\n\n uint(value: number): void {\n this.tag(this.runtime.NativeDataStream.VALIDATION_UINT_32);\n this.reserve(1);\n this.words[this.position++] = value;\n }\n\n int(value: number): void {\n this.tag(this.runtime.NativeDataStream.VALIDATION_INT_32);\n this.reserve(1);\n this.integers[this.position++] = value;\n }\n\n float(value: number): void {\n this.tag(this.runtime.NativeDataStream.VALIDATION_FLOAT_32);\n this.reserve(1);\n this.floats[this.position++] = value;\n }\n\n bool(value: boolean): void {\n this.tag(this.runtime.NativeDataStream.VALIDATION_BOOLEAN);\n this.reserve(1);\n this.words[this.position++] = value ? 1 : 0;\n }\n\n /** Encodes the first `length` values. */\n array(values: readonly number[], length: number): void {\n this.tag(this.runtime.NativeDataStream.VALIDATION_FLOAT_32_ARRAY);\n this.reserve(1 + length);\n this.words[this.position++] = length;\n for (let index = 0; index < length; index++) {\n this.floats[this.position++] = values[index] as number;\n }\n }\n\n handle(value: NativeHandle): void {\n this.tag(this.runtime.NativeDataStream.VALIDATION_NATIVE_DATA);\n this.reserve(value.length);\n this.words.set(value, this.position);\n this.position += value.length;\n this.retained.push(value);\n }\n\n finish(): void {\n if (!this.frameActive) {\n this.submit();\n }\n }\n\n /** Claims `staging` for an encodable float array of `length` values. */\n beginStaging(length: number): number {\n if (!Number.isInteger(length) || length < 0) {\n throw new RangeError(`Native Lite GL array length must be a nonnegative integer, got ${length}.`);\n }\n if (length + 1 > this.words.length) {\n throw capacityError();\n }\n return ++this.stagingGeneration;\n }\n\n /** Rejects an upload whose `staging` claim was taken by a nested upload. */\n endStaging(generation: number): void {\n if (generation !== this.stagingGeneration) {\n throw new Error(\"Native Lite GL array upload was interrupted by a nested array upload while reading its values.\");\n }\n }\n\n /** Deletes host texture storage once every command recorded before this call has been submitted. */\n deleteTexture(texture: NativeHandle): void {\n this.deletedTextures.push(texture);\n this.finish();\n }\n\n private tag(value: number): void {\n if (this.runtime.NativeDataStream.VALIDATION_ENABLED) {\n this.reserve(1);\n this.words[this.position++] = value;\n }\n }\n\n private reserve(length: number): void {\n if (length > this.words.length) {\n throw capacityError();\n }\n if (this.position + length > this.words.length) {\n this.flush();\n }\n }\n\n private flush(): void {\n this._nativeDataStream.writeBuffer(this.words.buffer, this.position);\n this.position = 0;\n }\n\n private submit(): void {\n this.engine.submitCommands();\n this.retained.length = 0;\n for (let index = 0; index < this.deletedTextures.length; index++) {\n this.engine.deleteTexture(this.deletedTextures[index] as NativeHandle);\n }\n this.deletedTextures.length = 0;\n }\n}\n"]}
@@ -0,0 +1,26 @@
1
+ import type { NativeResource } from "./identity.js";
2
+ import type { GLRenderTarget } from "./render-target.js";
3
+ /** Device limits used by composition producers. */
4
+ export interface GLEngineCaps {
5
+ /** Largest supported texture dimension, or 0 when the host has not reported one. */
6
+ readonly maxTextureSize: number;
7
+ }
8
+ /** Composition state owned by one Babylon Native engine. */
9
+ export interface GLEngineContext extends NativeResource<"engine"> {
10
+ /** Device limits. */
11
+ readonly caps: GLEngineCaps;
12
+ }
13
+ /** Creates the native composition engine without a scene graph. */
14
+ export declare function createNativeEngine(): GLEngineContext;
15
+ /** Begins a native command submission scope. */
16
+ export declare function beginFrame(engine: GLEngineContext): void;
17
+ /** Submits a native frame, including commands recorded before a caller error. */
18
+ export declare function endFrame(engine: GLEngineContext): void;
19
+ /** Releases the engine after its renderer resources have been disposed. */
20
+ export declare function disposeNativeEngine(engine: GLEngineContext): void;
21
+ /** Default output width in physical pixels, the native counterpart of the canvas width. */
22
+ export declare function getRenderWidth(engine: GLEngineContext): number;
23
+ /** Default output height in physical pixels, the native counterpart of the canvas height. */
24
+ export declare function getRenderHeight(engine: GLEngineContext): number;
25
+ /** Current drawing target, or null for the default output. */
26
+ export declare function getCurrentRenderTarget(engine: GLEngineContext): GLRenderTarget | null;
@@ -0,0 +1,89 @@
1
+ import { CommandStream } from "./command-stream.js";
2
+ import { getRuntime } from "./runtime.js";
3
+ /** Creates the native composition engine without a scene graph. */
4
+ export function createNativeEngine() {
5
+ const runtime = getRuntime();
6
+ const nativeEngine = new runtime.Engine({ version: "@babylonjs/lite-gl/native", nonFloatVertexBuffers: true });
7
+ const fields = {
8
+ caps: { maxTextureSize: runtime.Engine.CAPS_LIMITS_MAX_TEXTURE_SIZE },
9
+ _runtime: runtime,
10
+ _native: nativeEngine,
11
+ _commands: new CommandStream(runtime, nativeEngine),
12
+ _target: null,
13
+ _program: null,
14
+ _vertexArray: null,
15
+ _blend: -1,
16
+ _viewport: [NaN, NaN, NaN, NaN],
17
+ _textureGeneration: 0,
18
+ _scissorEmpty: false,
19
+ _disposed: false,
20
+ };
21
+ const engine = fields;
22
+ nativeEngine.setHardwareScalingLevel(1);
23
+ nativeEngine.setRenderResetCallback(() => {
24
+ engine._target = null;
25
+ engine._program = null;
26
+ engine._vertexArray = null;
27
+ engine._blend = -1;
28
+ engine._viewport.fill(NaN);
29
+ engine._textureGeneration++;
30
+ engine._scissorEmpty = false;
31
+ initializeCompositionState(engine);
32
+ });
33
+ initializeCompositionState(engine);
34
+ return engine;
35
+ }
36
+ /** Composition draws without depth testing, culling or a scissor rectangle. */
37
+ function initializeCompositionState(engine) {
38
+ const runtime = engine._runtime;
39
+ const commands = engine._commands;
40
+ commands.start(runtime.Engine.COMMAND_SETDEPTHTEST);
41
+ commands.uint(runtime.Engine.DEPTH_TEST_ALWAYS);
42
+ commands.finish();
43
+ commands.start(runtime.Engine.COMMAND_SETDEPTHWRITE);
44
+ commands.uint(0);
45
+ commands.finish();
46
+ commands.start(runtime.Engine.COMMAND_SETSTATE);
47
+ commands.uint(0);
48
+ commands.float(0);
49
+ commands.float(0);
50
+ commands.uint(1);
51
+ commands.uint(0);
52
+ commands.finish();
53
+ // The host keeps the default output's scissor across a device reset; an all-zero rectangle removes it.
54
+ commands.start(runtime.Engine.COMMAND_SETSCISSOR);
55
+ commands.float(0);
56
+ commands.float(0);
57
+ commands.float(0);
58
+ commands.float(0);
59
+ commands.finish();
60
+ }
61
+ /** Begins a native command submission scope. */
62
+ export function beginFrame(engine) {
63
+ engine._commands.begin();
64
+ engine._textureGeneration++;
65
+ }
66
+ /** Submits a native frame, including commands recorded before a caller error. */
67
+ export function endFrame(engine) {
68
+ engine._commands.end();
69
+ }
70
+ /** Releases the engine after its renderer resources have been disposed. */
71
+ export function disposeNativeEngine(engine) {
72
+ if (!engine._disposed) {
73
+ engine._disposed = true;
74
+ engine._native.dispose();
75
+ }
76
+ }
77
+ /** Default output width in physical pixels, the native counterpart of the canvas width. */
78
+ export function getRenderWidth(engine) {
79
+ return engine._native.getRenderWidth();
80
+ }
81
+ /** Default output height in physical pixels, the native counterpart of the canvas height. */
82
+ export function getRenderHeight(engine) {
83
+ return engine._native.getRenderHeight();
84
+ }
85
+ /** Current drawing target, or null for the default output. */
86
+ export function getCurrentRenderTarget(engine) {
87
+ return engine._target;
88
+ }
89
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/native/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAsC1C,mEAAmE;AACnE,MAAM,UAAU,kBAAkB;IAC9B,MAAM,OAAO,GAAG,UAAU,EAAE,CAAC;IAC7B,MAAM,YAAY,GAAG,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,2BAA2B,EAAE,qBAAqB,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/G,MAAM,MAAM,GAA0C;QAClD,IAAI,EAAE,EAAE,cAAc,EAAE,OAAO,CAAC,MAAM,CAAC,4BAA4B,EAAE;QACrE,QAAQ,EAAE,OAAO;QACjB,OAAO,EAAE,YAAY;QACrB,SAAS,EAAE,IAAI,aAAa,CAAC,OAAO,EA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AC;QACxB,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;IAC7B,CAAC;AACL,CAAC;AAED,2FAA2F;AAC3F,MAAM,UAAU,cAAc,CAAC,MAAuB;IAClD,OAAO,MAAM,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;AAC3C,CAAC;AAED,6FAA6F;AAC7F,MAAM,UAAU,eAAe,CAAC,MAAuB;IACnD,OAAO,MAAM,CAAC,OAAO,CAAC,eAAe,EAAE,CAAC;AAC5C,CAAC;AAED,8DAA8D;AAC9D,MAAM,UAAU,sBAAsB,CAAC,MAAuB;IAC1D,OAAO,MAAM,CAAC,OAAO,CAAC;AAC1B,CAAC","sourcesContent":["import { CommandStream } from \"./command-stream.js\";\nimport type { NativeResource, NativeResourceFields } from \"./identity.js\";\nimport { getRuntime } from \"./runtime.js\";\nimport type { NativeEngine, NativeHandle, NativeRuntime } from \"./runtime.js\";\nimport type { GLRenderTarget } from \"./render-target.js\";\n\n/** Device limits used by composition producers. */\nexport interface GLEngineCaps {\n /** Largest supported texture dimension, or 0 when the host has not reported one. */\n readonly maxTextureSize: number;\n}\n\n/** Composition state owned by one Babylon Native engine. */\nexport interface GLEngineContext extends NativeResource<\"engine\"> {\n /** Device limits. */\n readonly caps: GLEngineCaps;\n /** @internal */\n readonly _runtime: NativeRuntime;\n /** @internal */\n readonly _native: NativeEngine;\n /** @internal */\n readonly _commands: CommandStream;\n /** @internal */\n _target: GLRenderTarget | null;\n /** @internal */\n _program: NativeHandle | null;\n /** @internal */\n _vertexArray: NativeHandle | null;\n /** @internal */\n _blend: number;\n /** @internal Normalized viewport last sent to the native engine. */\n readonly _viewport: number[];\n /** @internal */\n _textureGeneration: number;\n /** @internal Whether the scissor rectangle has no area, so drawing and clearing do nothing. */\n _scissorEmpty: boolean;\n /** @internal */\n _disposed: boolean;\n}\n\n/** Creates the native composition engine without a scene graph. */\nexport function createNativeEngine(): GLEngineContext {\n const runtime = getRuntime();\n const nativeEngine = new runtime.Engine({ version: \"@babylonjs/lite-gl/native\", nonFloatVertexBuffers: true });\n const fields: NativeResourceFields<GLEngineContext> = {\n caps: { maxTextureSize: runtime.Engine.CAPS_LIMITS_MAX_TEXTURE_SIZE },\n _runtime: runtime,\n _native: nativeEngine,\n _commands: new CommandStream(runtime, nativeEngine),\n _target: null,\n _program: null,\n _vertexArray: null,\n _blend: -1,\n _viewport: [NaN, NaN, NaN, NaN],\n _textureGeneration: 0,\n _scissorEmpty: false,\n _disposed: false,\n };\n const engine = fields as GLEngineContext;\n nativeEngine.setHardwareScalingLevel(1);\n nativeEngine.setRenderResetCallback(() => {\n engine._target = null;\n engine._program = null;\n engine._vertexArray = null;\n engine._blend = -1;\n engine._viewport.fill(NaN);\n engine._textureGeneration++;\n engine._scissorEmpty = false;\n initializeCompositionState(engine);\n });\n initializeCompositionState(engine);\n return engine;\n}\n\n/** Composition draws without depth testing, culling or a scissor rectangle. */\nfunction initializeCompositionState(engine: GLEngineContext): void {\n const runtime = engine._runtime;\n const commands = engine._commands;\n commands.start(runtime.Engine.COMMAND_SETDEPTHTEST);\n commands.uint(runtime.Engine.DEPTH_TEST_ALWAYS);\n commands.finish();\n commands.start(runtime.Engine.COMMAND_SETDEPTHWRITE);\n commands.uint(0);\n commands.finish();\n commands.start(runtime.Engine.COMMAND_SETSTATE);\n commands.uint(0);\n commands.float(0);\n commands.float(0);\n commands.uint(1);\n commands.uint(0);\n commands.finish();\n // The host keeps the default output's scissor across a device reset; an all-zero rectangle removes it.\n commands.start(runtime.Engine.COMMAND_SETSCISSOR);\n commands.float(0);\n commands.float(0);\n commands.float(0);\n commands.float(0);\n commands.finish();\n}\n\n/** Begins a native command submission scope. */\nexport function beginFrame(engine: GLEngineContext): void {\n engine._commands.begin();\n engine._textureGeneration++;\n}\n\n/** Submits a native frame, including commands recorded before a caller error. */\nexport function endFrame(engine: GLEngineContext): void {\n engine._commands.end();\n}\n\n/** Releases the engine after its renderer resources have been disposed. */\nexport function disposeNativeEngine(engine: GLEngineContext): void {\n if (!engine._disposed) {\n engine._disposed = true;\n engine._native.dispose();\n }\n}\n\n/** Default output width in physical pixels, the native counterpart of the canvas width. */\nexport function getRenderWidth(engine: GLEngineContext): number {\n return engine._native.getRenderWidth();\n}\n\n/** Default output height in physical pixels, the native counterpart of the canvas height. */\nexport function getRenderHeight(engine: GLEngineContext): number {\n return engine._native.getRenderHeight();\n}\n\n/** Current drawing target, or null for the default output. */\nexport function getCurrentRenderTarget(engine: GLEngineContext): GLRenderTarget | null {\n return engine._target;\n}\n"]}
@@ -0,0 +1,44 @@
1
+ import type { GLEffectOptions } from "../family.js";
2
+ import type { GLEngineContext } from "./context.js";
3
+ import type { NativeResource } from "./identity.js";
4
+ import type { GLTexture } from "./texture.js";
5
+ /** Native shader program and its retained upload state. */
6
+ export interface GLEffect extends NativeResource<"effect"> {
7
+ readonly name: string;
8
+ }
9
+ /** Compiles a native effect from host-prepared GLSL. */
10
+ export declare function createEffect(engine: GLEngineContext, options: GLEffectOptions): GLEffect;
11
+ /** Selects a program without reissuing an unchanged native binding. No-op for a disposed effect or engine. */
12
+ export declare function useEffect(effect: GLEffect): void;
13
+ /** Reports whether the effect's synchronous compilation succeeded and it is still owned. */
14
+ export declare function isEffectReady(effect: GLEffect): boolean;
15
+ /** Native compilation failures throw from {@link createEffect}, so there is never a deferred error. */
16
+ export declare function getEffectCompilationError(_effect: GLEffect): string | null;
17
+ /** Releases the owned shader program; after engine disposal it only marks the effect disposed. */
18
+ export declare function disposeEffect(effect: GLEffect): void;
19
+ /** Uploads a changed scalar float. */
20
+ export declare function setEffectFloat(effect: GLEffect, name: string, x: number): void;
21
+ /** Uploads a changed two-component float. */
22
+ export declare function setEffectFloat2(effect: GLEffect, name: string, x: number, y: number): void;
23
+ /** Uploads a changed three-component float. */
24
+ export declare function setEffectFloat3(effect: GLEffect, name: string, x: number, y: number, z: number): void;
25
+ /** Uploads a changed four-component float. */
26
+ export declare function setEffectFloat4(effect: GLEffect, name: string, x: number, y: number, z: number, w: number): void;
27
+ /** Uploads a changed scalar integer or Boolean uniform. */
28
+ export declare function setEffectInt(effect: GLEffect, name: string, value: number): void;
29
+ /** Uploads a changed matrix in column-major order. */
30
+ export declare function setEffectMatrix(effect: GLEffect, name: string, values: ArrayLike<number>): void;
31
+ /** Uploads a changed scalar float array. */
32
+ export declare function setEffectFloatArray(effect: GLEffect, name: string, values: ArrayLike<number>): void;
33
+ /** Uploads a changed array of two-component floats. */
34
+ export declare function setEffectFloatArray2(effect: GLEffect, name: string, values: ArrayLike<number>): void;
35
+ /** Uploads a changed array of three-component floats. */
36
+ export declare function setEffectFloatArray3(effect: GLEffect, name: string, values: ArrayLike<number>): void;
37
+ /** Uploads a changed array of four-component floats. */
38
+ export declare function setEffectFloatArray4(effect: GLEffect, name: string, values: ArrayLike<number>): void;
39
+ /**
40
+ * Binds a texture to a declared sampler without repeating an unchanged binding.
41
+ * No-op for an undeclared sampler, a texture of another engine, or a disposed
42
+ * effect, texture or engine.
43
+ */
44
+ export declare function setEffectTexture(effect: GLEffect, name: string, texture: GLTexture): void;
@@ -0,0 +1,276 @@
1
+ /** Compiles a native effect from host-prepared GLSL. */
2
+ export function createEffect(engine, options) {
3
+ const defines = options.defines ?? "";
4
+ const program = engine._native.createProgram(withDefines(options.vertexSource, defines), withDefines(options.fragmentSource, defines));
5
+ const names = [...options.uniformNames, ...options.samplerNames];
6
+ const handles = engine._native.getUniforms(program, names);
7
+ const uniforms = Object.create(null);
8
+ const values = Object.create(null);
9
+ for (let index = 0; index < names.length; index++) {
10
+ const name = names[index];
11
+ uniforms[name] = handles[index] ?? null;
12
+ values[name] = [];
13
+ }
14
+ const attributeNames = [...(options.attributeNames ?? ["position"])];
15
+ const locations = engine._native.getAttributes(program, attributeNames);
16
+ const attributes = Object.create(null);
17
+ for (let index = 0; index < attributeNames.length; index++) {
18
+ attributes[attributeNames[index]] = locations[index] ?? -1;
19
+ }
20
+ const textures = Object.create(null);
21
+ for (const name of options.samplerNames) {
22
+ textures[name] = undefined;
23
+ }
24
+ const fields = {
25
+ name: options.name,
26
+ _program: program,
27
+ _uniforms: uniforms,
28
+ _values: values,
29
+ _textures: textures,
30
+ _samplers: [...options.samplerNames],
31
+ _textureGeneration: -1,
32
+ _attributes: attributes,
33
+ _engine: engine,
34
+ _disposed: false,
35
+ };
36
+ return fields;
37
+ }
38
+ /** Native compiles unversioned GLSL, so the WebGL2 `#version` directive is dropped and defines lead the source. */
39
+ function withDefines(source, defines) {
40
+ const version = findVersionDirective(source);
41
+ let body = source;
42
+ if (version >= 0) {
43
+ const end = source.indexOf("\n", version);
44
+ body = source.slice(0, version) + (end < 0 ? "" : source.slice(end + 1));
45
+ }
46
+ return defines ? `${defines}\n${body}` : body;
47
+ }
48
+ /** Offset of a `#version` directive preceded only by whitespace and comments, or -1. */
49
+ function findVersionDirective(source) {
50
+ let index = 0;
51
+ while (index < source.length) {
52
+ const char = source[index];
53
+ if (char === " " || char === "\t" || char === "\r" || char === "\n") {
54
+ index++;
55
+ }
56
+ else if (source.startsWith("//", index)) {
57
+ const end = source.indexOf("\n", index);
58
+ index = end < 0 ? source.length : end + 1;
59
+ }
60
+ else if (source.startsWith("/*", index)) {
61
+ const end = source.indexOf("*/", index + 2);
62
+ index = end < 0 ? source.length : end + 2;
63
+ }
64
+ else {
65
+ const directive = /#[ \t]*version\b/y;
66
+ directive.lastIndex = index;
67
+ return directive.test(source) ? index : -1;
68
+ }
69
+ }
70
+ return -1;
71
+ }
72
+ /** Selects a program without reissuing an unchanged native binding. No-op for a disposed effect or engine. */
73
+ export function useEffect(effect) {
74
+ const engine = effect._engine;
75
+ if (effect._disposed || engine._disposed) {
76
+ return;
77
+ }
78
+ if (engine._program !== effect._program) {
79
+ const commands = engine._commands;
80
+ commands.start(engine._runtime.Engine.COMMAND_SETPROGRAM);
81
+ commands.handle(effect._program);
82
+ commands.finish();
83
+ engine._program = effect._program;
84
+ engine._textureGeneration++;
85
+ }
86
+ }
87
+ /** Reports whether the effect's synchronous compilation succeeded and it is still owned. */
88
+ export function isEffectReady(effect) {
89
+ return !effect._disposed;
90
+ }
91
+ /** Native compilation failures throw from {@link createEffect}, so there is never a deferred error. */
92
+ export function getEffectCompilationError(_effect) {
93
+ return null;
94
+ }
95
+ /** Releases the owned shader program; after engine disposal it only marks the effect disposed. */
96
+ export function disposeEffect(effect) {
97
+ if (effect._disposed) {
98
+ return;
99
+ }
100
+ effect._disposed = true;
101
+ const engine = effect._engine;
102
+ if (engine._disposed) {
103
+ return;
104
+ }
105
+ const commands = engine._commands;
106
+ commands.start(engine._runtime.Engine.COMMAND_DELETEPROGRAM);
107
+ commands.handle(effect._program);
108
+ commands.finish();
109
+ if (engine._program === effect._program) {
110
+ engine._program = null;
111
+ }
112
+ }
113
+ /** Records a changed declared uniform of a live effect and reports whether to upload it. */
114
+ function changed(effect, name, count, x, y = 0, z = 0, w = 0) {
115
+ const values = effect._values[name];
116
+ if (!values || !effect._uniforms[name] || effect._disposed || effect._engine._disposed) {
117
+ return false;
118
+ }
119
+ if (values.length === count && values[0] === x && (count < 2 || values[1] === y) && (count < 3 || values[2] === z) && (count < 4 || values[3] === w)) {
120
+ return false;
121
+ }
122
+ values.length = count;
123
+ values[0] = x;
124
+ if (count > 1) {
125
+ values[1] = y;
126
+ }
127
+ if (count > 2) {
128
+ values[2] = z;
129
+ }
130
+ if (count > 3) {
131
+ values[3] = w;
132
+ }
133
+ return true;
134
+ }
135
+ function uniformCommand(effect, name, command) {
136
+ useEffect(effect);
137
+ const commands = effect._engine._commands;
138
+ commands.start(command);
139
+ commands.handle(effect._uniforms[name]);
140
+ }
141
+ /** Uploads a changed scalar float. */
142
+ export function setEffectFloat(effect, name, x) {
143
+ if (changed(effect, name, 1, x)) {
144
+ const engine = effect._engine;
145
+ uniformCommand(effect, name, engine._runtime.Engine.COMMAND_SETFLOAT);
146
+ engine._commands.float(x);
147
+ engine._commands.finish();
148
+ }
149
+ }
150
+ /** Uploads a changed two-component float. */
151
+ export function setEffectFloat2(effect, name, x, y) {
152
+ if (changed(effect, name, 2, x, y)) {
153
+ const engine = effect._engine;
154
+ uniformCommand(effect, name, engine._runtime.Engine.COMMAND_SETFLOAT2);
155
+ engine._commands.float(x);
156
+ engine._commands.float(y);
157
+ engine._commands.finish();
158
+ }
159
+ }
160
+ /** Uploads a changed three-component float. */
161
+ export function setEffectFloat3(effect, name, x, y, z) {
162
+ if (changed(effect, name, 3, x, y, z)) {
163
+ const engine = effect._engine;
164
+ uniformCommand(effect, name, engine._runtime.Engine.COMMAND_SETFLOAT3);
165
+ engine._commands.float(x);
166
+ engine._commands.float(y);
167
+ engine._commands.float(z);
168
+ engine._commands.finish();
169
+ }
170
+ }
171
+ /** Uploads a changed four-component float. */
172
+ export function setEffectFloat4(effect, name, x, y, z, w) {
173
+ if (changed(effect, name, 4, x, y, z, w)) {
174
+ const engine = effect._engine;
175
+ uniformCommand(effect, name, engine._runtime.Engine.COMMAND_SETFLOAT4);
176
+ engine._commands.float(x);
177
+ engine._commands.float(y);
178
+ engine._commands.float(z);
179
+ engine._commands.float(w);
180
+ engine._commands.finish();
181
+ }
182
+ }
183
+ /** Uploads a changed scalar integer or Boolean uniform. */
184
+ export function setEffectInt(effect, name, value) {
185
+ if (changed(effect, name, 1, value)) {
186
+ const engine = effect._engine;
187
+ uniformCommand(effect, name, engine._runtime.Engine.COMMAND_SETINT);
188
+ engine._commands.int(value);
189
+ engine._commands.finish();
190
+ }
191
+ }
192
+ /**
193
+ * @internal Uploads a changed declared array uniform; a rejected upload records nothing and leaves its own cached value unchanged.
194
+ * When reading the values makes a nested array upload, the nested upload keeps its result and this upload is rejected.
195
+ */
196
+ export function setUniformArray(effect, name, values, command) {
197
+ const cached = effect._values[name];
198
+ if (!cached || !effect._uniforms[name] || effect._disposed || effect._engine._disposed) {
199
+ return;
200
+ }
201
+ const commands = effect._engine._commands;
202
+ const length = values.length;
203
+ const generation = commands.beginStaging(length);
204
+ // Every element is read once, before anything is recorded or cached, so a throwing read leaves both unchanged.
205
+ const staging = commands.staging;
206
+ let differs = cached.length !== length;
207
+ for (let index = 0; index < length; index++) {
208
+ const value = values[index];
209
+ staging[index] = value;
210
+ if (cached[index] !== value) {
211
+ differs = true;
212
+ }
213
+ }
214
+ commands.endStaging(generation);
215
+ // A value read may have disposed the effect or its engine.
216
+ if (!differs || effect._disposed || effect._engine._disposed) {
217
+ return;
218
+ }
219
+ uniformCommand(effect, name, command);
220
+ commands.array(staging, length);
221
+ commands.finish();
222
+ cached.length = length;
223
+ for (let index = 0; index < length; index++) {
224
+ cached[index] = staging[index];
225
+ }
226
+ }
227
+ /** Uploads a changed matrix in column-major order. */
228
+ export function setEffectMatrix(effect, name, values) {
229
+ setUniformArray(effect, name, values, effect._engine._runtime.Engine.COMMAND_SETMATRICES);
230
+ }
231
+ /** Uploads a changed scalar float array. */
232
+ export function setEffectFloatArray(effect, name, values) {
233
+ setUniformArray(effect, name, values, effect._engine._runtime.Engine.COMMAND_SETFLOATARRAY);
234
+ }
235
+ /** Uploads a changed array of two-component floats. */
236
+ export function setEffectFloatArray2(effect, name, values) {
237
+ setUniformArray(effect, name, values, effect._engine._runtime.Engine.COMMAND_SETFLOATARRAY2);
238
+ }
239
+ /** Uploads a changed array of three-component floats. */
240
+ export function setEffectFloatArray3(effect, name, values) {
241
+ setUniformArray(effect, name, values, effect._engine._runtime.Engine.COMMAND_SETFLOATARRAY3);
242
+ }
243
+ /** Uploads a changed array of four-component floats. */
244
+ export function setEffectFloatArray4(effect, name, values) {
245
+ setUniformArray(effect, name, values, effect._engine._runtime.Engine.COMMAND_SETFLOATARRAY4);
246
+ }
247
+ /**
248
+ * Binds a texture to a declared sampler without repeating an unchanged binding.
249
+ * No-op for an undeclared sampler, a texture of another engine, or a disposed
250
+ * effect, texture or engine.
251
+ */
252
+ export function setEffectTexture(effect, name, texture) {
253
+ const engine = effect._engine;
254
+ const uniform = effect._uniforms[name];
255
+ if (!uniform || !(name in effect._textures) || effect._disposed || texture._disposed || texture._engine !== engine || engine._disposed) {
256
+ return;
257
+ }
258
+ useEffect(effect);
259
+ // Native retains program uniforms, but sampler slots belong to the current frame's encoder.
260
+ if (effect._textureGeneration !== engine._textureGeneration) {
261
+ for (let index = 0; index < effect._samplers.length; index++) {
262
+ effect._textures[effect._samplers[index]] = undefined;
263
+ }
264
+ effect._textureGeneration = engine._textureGeneration;
265
+ }
266
+ if (effect._textures[name] === texture._handle) {
267
+ return;
268
+ }
269
+ const commands = engine._commands;
270
+ commands.start(engine._runtime.Engine.COMMAND_SETTEXTURE);
271
+ commands.handle(uniform);
272
+ commands.handle(texture._handle);
273
+ commands.finish();
274
+ effect._textures[name] = texture._handle;
275
+ }
276
+ //# sourceMappingURL=effect.js.map