@babylonjs/core 7.3.2 → 7.3.3

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 (68) hide show
  1. package/Compute/computeEffect.js +4 -4
  2. package/Compute/computeEffect.js.map +1 -1
  3. package/Engines/IPipelineContext.d.ts +3 -0
  4. package/Engines/IPipelineContext.js.map +1 -1
  5. package/Engines/Native/nativePipelineContext.d.ts +2 -0
  6. package/Engines/Native/nativePipelineContext.js +3 -0
  7. package/Engines/Native/nativePipelineContext.js.map +1 -1
  8. package/Engines/Processors/iShaderProcessor.d.ts +3 -2
  9. package/Engines/Processors/iShaderProcessor.js.map +1 -1
  10. package/Engines/Processors/shaderProcessor.d.ts +15 -23
  11. package/Engines/Processors/shaderProcessor.js +332 -320
  12. package/Engines/Processors/shaderProcessor.js.map +1 -1
  13. package/Engines/WebGL/webGLPipelineContext.d.ts +2 -0
  14. package/Engines/WebGL/webGLPipelineContext.js +3 -0
  15. package/Engines/WebGL/webGLPipelineContext.js.map +1 -1
  16. package/Engines/WebGL/webGLShaderProcessors.d.ts +3 -2
  17. package/Engines/WebGL/webGLShaderProcessors.js +2 -2
  18. package/Engines/WebGL/webGLShaderProcessors.js.map +1 -1
  19. package/Engines/WebGPU/Extensions/engine.computeShader.js +1 -1
  20. package/Engines/WebGPU/Extensions/engine.computeShader.js.map +1 -1
  21. package/Engines/WebGPU/webgpuPipelineContext.d.ts +2 -0
  22. package/Engines/WebGPU/webgpuPipelineContext.js +3 -0
  23. package/Engines/WebGPU/webgpuPipelineContext.js.map +1 -1
  24. package/Engines/WebGPU/webgpuShaderProcessorsGLSL.d.ts +3 -2
  25. package/Engines/WebGPU/webgpuShaderProcessorsGLSL.js +2 -4
  26. package/Engines/WebGPU/webgpuShaderProcessorsGLSL.js.map +1 -1
  27. package/Engines/abstractEngine.d.ts +10 -5
  28. package/Engines/abstractEngine.functions.d.ts +38 -0
  29. package/Engines/abstractEngine.functions.js +75 -0
  30. package/Engines/abstractEngine.functions.js.map +1 -0
  31. package/Engines/abstractEngine.js +22 -14
  32. package/Engines/abstractEngine.js.map +1 -1
  33. package/Engines/constants.d.ts +2 -0
  34. package/Engines/constants.js +2 -0
  35. package/Engines/constants.js.map +1 -1
  36. package/Engines/thinEngine.d.ts +11 -6
  37. package/Engines/thinEngine.functions.d.ts +96 -0
  38. package/Engines/thinEngine.functions.js +249 -0
  39. package/Engines/thinEngine.functions.js.map +1 -0
  40. package/Engines/thinEngine.js +50 -129
  41. package/Engines/thinEngine.js.map +1 -1
  42. package/Engines/webgpuEngine.js +5 -1
  43. package/Engines/webgpuEngine.js.map +1 -1
  44. package/Materials/Node/Blocks/Dual/textureBlock.js +3 -3
  45. package/Materials/Node/Blocks/Dual/textureBlock.js.map +1 -1
  46. package/Materials/Textures/Procedurals/proceduralTexture.d.ts +9 -1
  47. package/Materials/Textures/Procedurals/proceduralTexture.js +16 -0
  48. package/Materials/Textures/Procedurals/proceduralTexture.js.map +1 -1
  49. package/Materials/effect.d.ts +11 -6
  50. package/Materials/effect.functions.d.ts +86 -0
  51. package/Materials/effect.functions.js +185 -0
  52. package/Materials/effect.functions.js.map +1 -0
  53. package/Materials/effect.js +94 -153
  54. package/Materials/effect.js.map +1 -1
  55. package/Materials/effect.webgl.functions.d.ts +13 -0
  56. package/Materials/effect.webgl.functions.js +83 -0
  57. package/Materials/effect.webgl.functions.js.map +1 -0
  58. package/Materials/materialPluginManager.js +2 -2
  59. package/Materials/materialPluginManager.js.map +1 -1
  60. package/Meshes/Compression/dracoCompression.js.map +1 -1
  61. package/Meshes/Compression/dracoCompressionWorker.d.ts +1 -1
  62. package/Meshes/Compression/dracoCompressionWorker.js +21 -10
  63. package/Meshes/Compression/dracoCompressionWorker.js.map +1 -1
  64. package/Meshes/Node/nodeGeometryBlock.js +1 -0
  65. package/Meshes/Node/nodeGeometryBlock.js.map +1 -1
  66. package/Misc/fileTools.js +4 -3
  67. package/Misc/fileTools.js.map +1 -1
  68. package/package.json +1 -1
@@ -0,0 +1,249 @@
1
+ import { WebGLPipelineContext } from "./WebGL/webGLPipelineContext.js";
2
+ import { _ConcatenateShader } from "./abstractEngine.functions.js";
3
+ /**
4
+ * @internal
5
+ */
6
+ const _stateObject = new WeakMap();
7
+ /**
8
+ * This will be used in cases where the engine doesn't have a context (like the nullengine)
9
+ */
10
+ const singleStateObject = {
11
+ _webGLVersion: 2,
12
+ cachedPipelines: {},
13
+ };
14
+ /**
15
+ * get or create a state object for the given context
16
+ * Note - Used in WebGL only at the moment.
17
+ * @param context The context to get the state object from
18
+ * @returns the state object
19
+ * @internal
20
+ */
21
+ export function getStateObject(context) {
22
+ let state = _stateObject.get(context);
23
+ if (!state) {
24
+ if (!context) {
25
+ return singleStateObject;
26
+ }
27
+ state = {
28
+ // use feature detection. instanceof returns false. This only exists on WebGL2 context
29
+ _webGLVersion: context.TEXTURE_BINDING_3D ? 2 : 1,
30
+ _context: context,
31
+ cachedPipelines: {},
32
+ };
33
+ _stateObject.set(context, state);
34
+ }
35
+ return state;
36
+ }
37
+ /**
38
+ * Remove the state object that belongs to the specific context
39
+ * @param context the context that is being
40
+ */
41
+ export function deleteStateObject(context) {
42
+ _stateObject.delete(context);
43
+ }
44
+ /**
45
+ * Directly creates a webGL program
46
+ * @param pipelineContext defines the pipeline context to attach to
47
+ * @param vertexCode defines the vertex shader code to use
48
+ * @param fragmentCode defines the fragment shader code to use
49
+ * @param context defines the webGL context to use (if not set, the current one will be used)
50
+ * @param transformFeedbackVaryings defines the list of transform feedback varyings to use
51
+ * @param _createShaderProgramInjection defines an optional injection to use to create the shader program
52
+ * @returns the new webGL program
53
+ */
54
+ export function createRawShaderProgram(pipelineContext, vertexCode, fragmentCode, context, transformFeedbackVaryings, _createShaderProgramInjection) {
55
+ const stateObject = getStateObject(context);
56
+ if (!_createShaderProgramInjection) {
57
+ _createShaderProgramInjection = stateObject._createShaderProgramInjection ?? _createShaderProgram;
58
+ }
59
+ const vertexShader = _compileRawShader(vertexCode, "vertex", context, stateObject._contextWasLost);
60
+ const fragmentShader = _compileRawShader(fragmentCode, "fragment", context, stateObject._contextWasLost);
61
+ return _createShaderProgramInjection(pipelineContext, vertexShader, fragmentShader, context, transformFeedbackVaryings, stateObject.validateShaderPrograms);
62
+ }
63
+ /**
64
+ * Creates a webGL program
65
+ * @param pipelineContext defines the pipeline context to attach to
66
+ * @param vertexCode defines the vertex shader code to use
67
+ * @param fragmentCode defines the fragment shader code to use
68
+ * @param defines defines the string containing the defines to use to compile the shaders
69
+ * @param context defines the webGL context to use (if not set, the current one will be used)
70
+ * @param transformFeedbackVaryings defines the list of transform feedback varyings to use
71
+ * @param _createShaderProgramInjection defines an optional injection to use to create the shader program
72
+ * @returns the new webGL program
73
+ */
74
+ export function createShaderProgram(pipelineContext, vertexCode, fragmentCode, defines, context, transformFeedbackVaryings = null, _createShaderProgramInjection) {
75
+ const stateObject = getStateObject(context);
76
+ if (!_createShaderProgramInjection) {
77
+ _createShaderProgramInjection = stateObject._createShaderProgramInjection ?? _createShaderProgram;
78
+ }
79
+ const shaderVersion = stateObject._webGLVersion > 1 ? "#version 300 es\n#define WEBGL2 \n" : "";
80
+ const vertexShader = _compileShader(vertexCode, "vertex", defines, shaderVersion, context, stateObject._contextWasLost);
81
+ const fragmentShader = _compileShader(fragmentCode, "fragment", defines, shaderVersion, context, stateObject._contextWasLost);
82
+ return _createShaderProgramInjection(pipelineContext, vertexShader, fragmentShader, context, transformFeedbackVaryings, stateObject.validateShaderPrograms);
83
+ }
84
+ /**
85
+ * Creates a new pipeline context. Note, make sure to attach an engine instance to the created context
86
+ * @param context defines the webGL context to use (if not set, the current one will be used)
87
+ * @param _shaderProcessingContext defines the shader processing context used during the processing if available
88
+ * @returns the new pipeline
89
+ */
90
+ export function createPipelineContext(context, _shaderProcessingContext) {
91
+ const pipelineContext = new WebGLPipelineContext();
92
+ const stateObject = getStateObject(context);
93
+ if (stateObject.parallelShaderCompile) {
94
+ pipelineContext.isParallelCompiled = true;
95
+ }
96
+ pipelineContext.context = stateObject._context;
97
+ return pipelineContext;
98
+ }
99
+ /**
100
+ * @internal
101
+ */
102
+ export function _createShaderProgram(pipelineContext, vertexShader, fragmentShader, context, _transformFeedbackVaryings = null, validateShaderPrograms) {
103
+ const shaderProgram = context.createProgram();
104
+ pipelineContext.program = shaderProgram;
105
+ if (!shaderProgram) {
106
+ throw new Error("Unable to create program");
107
+ }
108
+ context.attachShader(shaderProgram, vertexShader);
109
+ context.attachShader(shaderProgram, fragmentShader);
110
+ context.linkProgram(shaderProgram);
111
+ pipelineContext.context = context;
112
+ pipelineContext.vertexShader = vertexShader;
113
+ pipelineContext.fragmentShader = fragmentShader;
114
+ if (!pipelineContext.isParallelCompiled) {
115
+ _finalizePipelineContext(pipelineContext, context, validateShaderPrograms);
116
+ }
117
+ return shaderProgram;
118
+ }
119
+ /**
120
+ * @internal
121
+ */
122
+ export function _finalizePipelineContext(pipelineContext, gl, validateShaderPrograms) {
123
+ const context = pipelineContext.context;
124
+ const vertexShader = pipelineContext.vertexShader;
125
+ const fragmentShader = pipelineContext.fragmentShader;
126
+ const program = pipelineContext.program;
127
+ const linked = context.getProgramParameter(program, context.LINK_STATUS);
128
+ if (!linked) {
129
+ // Get more info
130
+ // Vertex
131
+ if (gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
132
+ const log = gl.getShaderInfoLog(vertexShader);
133
+ if (log) {
134
+ pipelineContext.vertexCompilationError = log;
135
+ throw new Error("VERTEX SHADER " + log);
136
+ }
137
+ }
138
+ // Fragment
139
+ if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
140
+ const log = gl.getShaderInfoLog(fragmentShader);
141
+ if (log) {
142
+ pipelineContext.fragmentCompilationError = log;
143
+ throw new Error("FRAGMENT SHADER " + log);
144
+ }
145
+ }
146
+ const error = context.getProgramInfoLog(program);
147
+ if (error) {
148
+ pipelineContext.programLinkError = error;
149
+ throw new Error(error);
150
+ }
151
+ }
152
+ if ( /*this.*/validateShaderPrograms) {
153
+ context.validateProgram(program);
154
+ const validated = context.getProgramParameter(program, context.VALIDATE_STATUS);
155
+ if (!validated) {
156
+ const error = context.getProgramInfoLog(program);
157
+ if (error) {
158
+ pipelineContext.programValidationError = error;
159
+ throw new Error(error);
160
+ }
161
+ }
162
+ }
163
+ context.deleteShader(vertexShader);
164
+ context.deleteShader(fragmentShader);
165
+ pipelineContext.vertexShader = undefined;
166
+ pipelineContext.fragmentShader = undefined;
167
+ if (pipelineContext.onCompiled) {
168
+ pipelineContext.onCompiled();
169
+ pipelineContext.onCompiled = undefined;
170
+ }
171
+ }
172
+ /**
173
+ * @internal
174
+ */
175
+ export function _preparePipelineContext(pipelineContext, vertexSourceCode, fragmentSourceCode, createAsRaw, _rawVertexSourceCode, _rawFragmentSourceCode, rebuildRebind, defines, transformFeedbackVaryings, _key = "", createRawShaderProgramInjection, createShaderProgramInjection) {
176
+ const stateObject = getStateObject(pipelineContext.context);
177
+ if (!createRawShaderProgramInjection) {
178
+ createRawShaderProgramInjection = stateObject.createRawShaderProgramInjection ?? createRawShaderProgram;
179
+ }
180
+ if (!createShaderProgramInjection) {
181
+ createShaderProgramInjection = stateObject.createShaderProgramInjection ?? createShaderProgram;
182
+ }
183
+ const webGLRenderingState = pipelineContext;
184
+ if (createAsRaw) {
185
+ webGLRenderingState.program = createRawShaderProgramInjection(webGLRenderingState, vertexSourceCode, fragmentSourceCode, webGLRenderingState.context, transformFeedbackVaryings);
186
+ }
187
+ else {
188
+ webGLRenderingState.program = createShaderProgramInjection(webGLRenderingState, vertexSourceCode, fragmentSourceCode, defines, webGLRenderingState.context, transformFeedbackVaryings);
189
+ }
190
+ webGLRenderingState.program.__SPECTOR_rebuildProgram = rebuildRebind;
191
+ }
192
+ function _compileShader(source, type, defines, shaderVersion, gl, _contextWasLost) {
193
+ return _compileRawShader(_ConcatenateShader(source, defines, shaderVersion), type, gl, _contextWasLost);
194
+ }
195
+ function _compileRawShader(source, type, gl, _contextWasLost) {
196
+ const shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
197
+ if (!shader) {
198
+ let error = gl.NO_ERROR;
199
+ let tempError = gl.NO_ERROR;
200
+ while ((tempError = gl.getError()) !== gl.NO_ERROR) {
201
+ error = tempError;
202
+ }
203
+ throw new Error(`Something went wrong while creating a gl ${type} shader object. gl error=${error}, gl isContextLost=${gl.isContextLost()}, _contextWasLost=${_contextWasLost}`);
204
+ }
205
+ gl.shaderSource(shader, source);
206
+ gl.compileShader(shader);
207
+ return shader;
208
+ }
209
+ /**
210
+ * Binds an effect to the webGL context
211
+ * @param pipelineContext defines the pipeline context to use
212
+ * @param samplers defines the list of webGL samplers to bind
213
+ * @param uniforms defines the list of webGL uniforms to bind
214
+ * @returns the webGL program
215
+ */
216
+ export function bindSamplers(pipelineContext, samplers, uniforms) {
217
+ const webGLPipelineContext = pipelineContext;
218
+ _setProgram(webGLPipelineContext.program, webGLPipelineContext.context);
219
+ const _boundUniforms = [];
220
+ for (let index = 0; index < samplers.length; index++) {
221
+ const uniform = uniforms[samplers[index]];
222
+ if (uniform) {
223
+ _boundUniforms[index] = uniform;
224
+ }
225
+ }
226
+ return _boundUniforms;
227
+ }
228
+ /**
229
+ * @internal
230
+ */
231
+ export function _setProgram(program, gl) {
232
+ gl.useProgram(program);
233
+ }
234
+ /**
235
+ * @internal
236
+ */
237
+ export function _executeWhenRenderingStateIsCompiled(pipelineContext, action) {
238
+ const webGLPipelineContext = pipelineContext;
239
+ if (!webGLPipelineContext.isParallelCompiled) {
240
+ action(pipelineContext);
241
+ return;
242
+ }
243
+ const oldHandler = webGLPipelineContext.onCompiled;
244
+ webGLPipelineContext.onCompiled = () => {
245
+ oldHandler?.();
246
+ action(pipelineContext);
247
+ };
248
+ }
249
+ //# sourceMappingURL=thinEngine.functions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"thinEngine.functions.js","sourceRoot":"","sources":["../../../../dev/core/src/Engines/thinEngine.functions.ts"],"names":[],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AAG,eAAuC,CAAC;IACrE,WAAW,CAAC,oBAAoB,CAAC,OAAQ,EAAE,oBAAoB,CAAC,OAAQ,CAAC,CAAC;IAC1E,MAAM,cAAc,GAAqC,EAAE,CAAC;IAC5D,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;QAClD,MAAM,OAAO,GAAG,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;QAE1C,IAAI,OAAO,EAAE;YACT,cAAc,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC;SACnC;KACJ;IACD,OAAO,cAAc,CAAC;AAC1B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,OAAqB,EAAE,EAAgB;IAC/D,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;AAC3B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oCAAoC,CAAC,eAAiC,EAAE,MAAoD;IACxI,MAAM,oBAAoB,GAAG,eAAuC,CAAC;IAErE,IAAI,CAAC,oBAAoB,CAAC,kBAAkB,EAAE;QAC1C,MAAM,CAAC,eAAe,CAAC,CAAC;QACxB,OAAO;KACV;IAED,MAAM,UAAU,GAAG,oBAAoB,CAAC,UAAU,CAAC;IAEnD,oBAAoB,CAAC,UAAU,GAAG,GAAG,EAAE;QACnC,UAAU,EAAE,EAAE,CAAC;QACf,MAAM,CAAC,eAAe,CAAC,CAAC;IAC5B,CAAC,CAAC;AACN,CAAC","sourcesContent":["import type { Nullable } from \"../types\";\r\nimport type { IPipelineContext } from \"./IPipelineContext\";\r\nimport type { ShaderProcessingContext } from \"./Processors/shaderProcessingOptions\";\r\nimport { WebGLPipelineContext } from \"./WebGL/webGLPipelineContext\";\r\nimport { _ConcatenateShader } from \"./abstractEngine.functions\";\r\n\r\n/**\r\n * @internal\r\n */\r\nexport interface IThinEngineStateObject {\r\n _contextWasLost?: boolean;\r\n validateShaderPrograms?: boolean;\r\n _webGLVersion: number;\r\n parallelShaderCompile?: { COMPLETION_STATUS_KHR: number };\r\n _context?: WebGLContext;\r\n _createShaderProgramInjection?: typeof _createShaderProgram;\r\n createRawShaderProgramInjection?: typeof createRawShaderProgram;\r\n createShaderProgramInjection?: typeof createShaderProgram;\r\n cachedPipelines: { [name: string]: IPipelineContext };\r\n}\r\n/**\r\n * @internal\r\n */\r\nconst _stateObject: WeakMap<WebGLContext, IThinEngineStateObject> = new WeakMap();\r\n\r\n/**\r\n * This will be used in cases where the engine doesn't have a context (like the nullengine)\r\n */\r\nconst singleStateObject: IThinEngineStateObject = {\r\n _webGLVersion: 2,\r\n cachedPipelines: {},\r\n};\r\n\r\n/**\r\n * get or create a state object for the given context\r\n * Note - Used in WebGL only at the moment.\r\n * @param context The context to get the state object from\r\n * @returns the state object\r\n * @internal\r\n */\r\nexport function getStateObject(context: WebGLContext): IThinEngineStateObject {\r\n let state = _stateObject.get(context);\r\n if (!state) {\r\n if (!context) {\r\n return singleStateObject;\r\n }\r\n state = {\r\n // use feature detection. instanceof returns false. This only exists on WebGL2 context\r\n _webGLVersion: (context as WebGL2RenderingContext).TEXTURE_BINDING_3D ? 2 : 1,\r\n _context: context,\r\n cachedPipelines: {},\r\n };\r\n _stateObject.set(context, state);\r\n }\r\n return state;\r\n}\r\n/**\r\n * Remove the state object that belongs to the specific context\r\n * @param context the context that is being\r\n */\r\nexport function deleteStateObject(context: WebGLContext): void {\r\n _stateObject.delete(context);\r\n}\r\n\r\nexport type WebGLContext = WebGLRenderingContext | WebGL2RenderingContext;\r\n/**\r\n * Directly creates a webGL program\r\n * @param pipelineContext defines the pipeline context to attach to\r\n * @param vertexCode defines the vertex shader code to use\r\n * @param fragmentCode defines the fragment shader code to use\r\n * @param context defines the webGL context to use (if not set, the current one will be used)\r\n * @param transformFeedbackVaryings defines the list of transform feedback varyings to use\r\n * @param _createShaderProgramInjection defines an optional injection to use to create the shader program\r\n * @returns the new webGL program\r\n */\r\nexport function createRawShaderProgram(\r\n pipelineContext: IPipelineContext,\r\n vertexCode: string,\r\n fragmentCode: string,\r\n context: WebGLContext,\r\n transformFeedbackVaryings: Nullable<string[]>,\r\n _createShaderProgramInjection?: typeof _createShaderProgram\r\n): WebGLProgram {\r\n const stateObject = getStateObject(context);\r\n if (!_createShaderProgramInjection) {\r\n _createShaderProgramInjection = stateObject._createShaderProgramInjection ?? _createShaderProgram;\r\n }\r\n\r\n const vertexShader = _compileRawShader(vertexCode, \"vertex\", context, stateObject._contextWasLost);\r\n const fragmentShader = _compileRawShader(fragmentCode, \"fragment\", context, stateObject._contextWasLost);\r\n\r\n return _createShaderProgramInjection(\r\n pipelineContext as WebGLPipelineContext,\r\n vertexShader,\r\n fragmentShader,\r\n context,\r\n transformFeedbackVaryings,\r\n stateObject.validateShaderPrograms\r\n );\r\n}\r\n\r\n/**\r\n * Creates a webGL program\r\n * @param pipelineContext defines the pipeline context to attach to\r\n * @param vertexCode defines the vertex shader code to use\r\n * @param fragmentCode defines the fragment shader code to use\r\n * @param defines defines the string containing the defines to use to compile the shaders\r\n * @param context defines the webGL context to use (if not set, the current one will be used)\r\n * @param transformFeedbackVaryings defines the list of transform feedback varyings to use\r\n * @param _createShaderProgramInjection defines an optional injection to use to create the shader program\r\n * @returns the new webGL program\r\n */\r\nexport function createShaderProgram(\r\n pipelineContext: IPipelineContext,\r\n vertexCode: string,\r\n fragmentCode: string,\r\n defines: Nullable<string>,\r\n context: WebGLContext,\r\n transformFeedbackVaryings: Nullable<string[]> = null,\r\n _createShaderProgramInjection?: typeof _createShaderProgram\r\n): WebGLProgram {\r\n const stateObject = getStateObject(context);\r\n if (!_createShaderProgramInjection) {\r\n _createShaderProgramInjection = stateObject._createShaderProgramInjection ?? _createShaderProgram;\r\n }\r\n const shaderVersion = stateObject._webGLVersion > 1 ? \"#version 300 es\\n#define WEBGL2 \\n\" : \"\";\r\n const vertexShader = _compileShader(vertexCode, \"vertex\", defines, shaderVersion, context, stateObject._contextWasLost);\r\n const fragmentShader = _compileShader(fragmentCode, \"fragment\", defines, shaderVersion, context, stateObject._contextWasLost);\r\n\r\n return _createShaderProgramInjection(\r\n pipelineContext as WebGLPipelineContext,\r\n vertexShader,\r\n fragmentShader,\r\n context,\r\n transformFeedbackVaryings,\r\n stateObject.validateShaderPrograms\r\n );\r\n}\r\n\r\n/**\r\n * Creates a new pipeline context. Note, make sure to attach an engine instance to the created context\r\n * @param context defines the webGL context to use (if not set, the current one will be used)\r\n * @param _shaderProcessingContext defines the shader processing context used during the processing if available\r\n * @returns the new pipeline\r\n */\r\nexport function createPipelineContext(context: WebGLContext, _shaderProcessingContext: Nullable<ShaderProcessingContext>): IPipelineContext {\r\n const pipelineContext = new WebGLPipelineContext();\r\n const stateObject = getStateObject(context);\r\n if (stateObject.parallelShaderCompile) {\r\n pipelineContext.isParallelCompiled = true;\r\n }\r\n pipelineContext.context = stateObject._context;\r\n return pipelineContext;\r\n}\r\n\r\n/**\r\n * @internal\r\n */\r\nexport function _createShaderProgram(\r\n pipelineContext: WebGLPipelineContext,\r\n vertexShader: WebGLShader,\r\n fragmentShader: WebGLShader,\r\n context: WebGLContext,\r\n _transformFeedbackVaryings: Nullable<string[]> = null,\r\n validateShaderPrograms?: boolean\r\n): WebGLProgram {\r\n const shaderProgram = context.createProgram();\r\n pipelineContext.program = shaderProgram;\r\n\r\n if (!shaderProgram) {\r\n throw new Error(\"Unable to create program\");\r\n }\r\n\r\n context.attachShader(shaderProgram, vertexShader);\r\n context.attachShader(shaderProgram, fragmentShader);\r\n\r\n context.linkProgram(shaderProgram);\r\n\r\n pipelineContext.context = context;\r\n pipelineContext.vertexShader = vertexShader;\r\n pipelineContext.fragmentShader = fragmentShader;\r\n\r\n if (!pipelineContext.isParallelCompiled) {\r\n _finalizePipelineContext(pipelineContext, context, validateShaderPrograms);\r\n }\r\n\r\n return shaderProgram;\r\n}\r\n\r\n/**\r\n * @internal\r\n */\r\nexport function _finalizePipelineContext(pipelineContext: WebGLPipelineContext, gl: WebGLContext, validateShaderPrograms?: boolean) {\r\n const context = pipelineContext.context!;\r\n const vertexShader = pipelineContext.vertexShader!;\r\n const fragmentShader = pipelineContext.fragmentShader!;\r\n const program = pipelineContext.program!;\r\n\r\n const linked = context.getProgramParameter(program, context.LINK_STATUS);\r\n if (!linked) {\r\n // Get more info\r\n // Vertex\r\n if (gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {\r\n const log = gl.getShaderInfoLog(vertexShader);\r\n if (log) {\r\n pipelineContext.vertexCompilationError = log;\r\n throw new Error(\"VERTEX SHADER \" + log);\r\n }\r\n }\r\n\r\n // Fragment\r\n if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {\r\n const log = gl.getShaderInfoLog(fragmentShader);\r\n if (log) {\r\n pipelineContext.fragmentCompilationError = log;\r\n throw new Error(\"FRAGMENT SHADER \" + log);\r\n }\r\n }\r\n\r\n const error = context.getProgramInfoLog(program);\r\n if (error) {\r\n pipelineContext.programLinkError = error;\r\n throw new Error(error);\r\n }\r\n }\r\n\r\n if (/*this.*/ validateShaderPrograms) {\r\n context.validateProgram(program);\r\n const validated = context.getProgramParameter(program, context.VALIDATE_STATUS);\r\n\r\n if (!validated) {\r\n const error = context.getProgramInfoLog(program);\r\n if (error) {\r\n pipelineContext.programValidationError = error;\r\n throw new Error(error);\r\n }\r\n }\r\n }\r\n\r\n context.deleteShader(vertexShader);\r\n context.deleteShader(fragmentShader);\r\n\r\n pipelineContext.vertexShader = undefined;\r\n pipelineContext.fragmentShader = undefined;\r\n\r\n if (pipelineContext.onCompiled) {\r\n pipelineContext.onCompiled();\r\n pipelineContext.onCompiled = undefined;\r\n }\r\n}\r\n\r\n/**\r\n * @internal\r\n */\r\nexport function _preparePipelineContext(\r\n pipelineContext: IPipelineContext,\r\n vertexSourceCode: string,\r\n fragmentSourceCode: string,\r\n createAsRaw: boolean,\r\n _rawVertexSourceCode: string,\r\n _rawFragmentSourceCode: string,\r\n rebuildRebind: any,\r\n defines: Nullable<string>,\r\n transformFeedbackVaryings: Nullable<string[]>,\r\n _key: string = \"\",\r\n createRawShaderProgramInjection?: typeof createRawShaderProgram,\r\n createShaderProgramInjection?: typeof createShaderProgram\r\n) {\r\n const stateObject = getStateObject((pipelineContext as WebGLPipelineContext).context!);\r\n if (!createRawShaderProgramInjection) {\r\n createRawShaderProgramInjection = stateObject.createRawShaderProgramInjection ?? createRawShaderProgram;\r\n }\r\n if (!createShaderProgramInjection) {\r\n createShaderProgramInjection = stateObject.createShaderProgramInjection ?? createShaderProgram;\r\n }\r\n const webGLRenderingState = pipelineContext as WebGLPipelineContext;\r\n\r\n if (createAsRaw) {\r\n webGLRenderingState.program = createRawShaderProgramInjection(\r\n webGLRenderingState,\r\n vertexSourceCode,\r\n fragmentSourceCode,\r\n webGLRenderingState.context!,\r\n transformFeedbackVaryings\r\n );\r\n } else {\r\n webGLRenderingState.program = createShaderProgramInjection(\r\n webGLRenderingState,\r\n vertexSourceCode,\r\n fragmentSourceCode,\r\n defines,\r\n webGLRenderingState.context!,\r\n transformFeedbackVaryings\r\n );\r\n }\r\n webGLRenderingState.program.__SPECTOR_rebuildProgram = rebuildRebind;\r\n}\r\n\r\nfunction _compileShader(source: string, type: string, defines: Nullable<string>, shaderVersion: string, gl: WebGLContext, _contextWasLost?: boolean): WebGLShader {\r\n return _compileRawShader(_ConcatenateShader(source, defines, shaderVersion), type, gl, _contextWasLost);\r\n}\r\n\r\nfunction _compileRawShader(source: string, type: string, gl: WebGLContext, _contextWasLost?: boolean): WebGLShader {\r\n const shader = gl.createShader(type === \"vertex\" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);\r\n\r\n if (!shader) {\r\n let error: GLenum = gl.NO_ERROR;\r\n let tempError: GLenum = gl.NO_ERROR;\r\n while ((tempError = gl.getError()) !== gl.NO_ERROR) {\r\n error = tempError;\r\n }\r\n\r\n throw new Error(\r\n `Something went wrong while creating a gl ${type} shader object. gl error=${error}, gl isContextLost=${gl.isContextLost()}, _contextWasLost=${_contextWasLost}`\r\n );\r\n }\r\n\r\n gl.shaderSource(shader, source);\r\n gl.compileShader(shader);\r\n\r\n return shader;\r\n}\r\n\r\n/**\r\n * Binds an effect to the webGL context\r\n * @param pipelineContext defines the pipeline context to use\r\n * @param samplers defines the list of webGL samplers to bind\r\n * @param uniforms defines the list of webGL uniforms to bind\r\n * @returns the webGL program\r\n */\r\nexport function bindSamplers(\r\n pipelineContext: IPipelineContext,\r\n samplers: string[],\r\n uniforms: {\r\n [key: string]: Nullable<WebGLUniformLocation>;\r\n }\r\n) {\r\n const webGLPipelineContext = pipelineContext as WebGLPipelineContext;\r\n _setProgram(webGLPipelineContext.program!, webGLPipelineContext.context!);\r\n const _boundUniforms: Nullable<WebGLUniformLocation>[] = [];\r\n for (let index = 0; index < samplers.length; index++) {\r\n const uniform = uniforms[samplers[index]];\r\n\r\n if (uniform) {\r\n _boundUniforms[index] = uniform;\r\n }\r\n }\r\n return _boundUniforms;\r\n}\r\n\r\n/**\r\n * @internal\r\n */\r\nexport function _setProgram(program: WebGLProgram, gl: WebGLContext): void {\r\n gl.useProgram(program);\r\n}\r\n\r\n/**\r\n * @internal\r\n */\r\nexport function _executeWhenRenderingStateIsCompiled(pipelineContext: IPipelineContext, action: (pipelineContext?: IPipelineContext) => void) {\r\n const webGLPipelineContext = pipelineContext as WebGLPipelineContext;\r\n\r\n if (!webGLPipelineContext.isParallelCompiled) {\r\n action(pipelineContext);\r\n return;\r\n }\r\n\r\n const oldHandler = webGLPipelineContext.onCompiled;\r\n\r\n webGLPipelineContext.onCompiled = () => {\r\n oldHandler?.();\r\n action(pipelineContext);\r\n };\r\n}\r\n"]}
@@ -1,3 +1,4 @@
1
+ import { createPipelineContext, createRawShaderProgram, createShaderProgram, _finalizePipelineContext, _preparePipelineContext, _setProgram, _executeWhenRenderingStateIsCompiled, getStateObject, _createShaderProgram, deleteStateObject, } from "./thinEngine.functions.js";
1
2
  import { IsWrapper } from "../Materials/drawWrapper.functions.js";
2
3
  import { Logger } from "../Misc/logger.js";
3
4
  import { IsWindowObjectExist } from "../Misc/domManagement.js";
@@ -9,10 +10,11 @@ import { AbstractEngine, QueueNewFrame } from "./abstractEngine.js";
9
10
 
10
11
  import { WebGLHardwareTexture } from "./WebGL/webGLHardwareTexture.js";
11
12
  import { ShaderLanguage } from "../Materials/shaderLanguage.js";
12
- import { WebGLPipelineContext } from "./WebGL/webGLPipelineContext.js";
13
13
  import { InternalTexture, InternalTextureSource } from "../Materials/Textures/internalTexture.js";
14
14
  import { Effect } from "../Materials/effect.js";
15
15
  import { _WarnImport } from "../Misc/devTools.js";
16
+ import { _ConcatenateShader, _getGlobalDefines } from "./abstractEngine.functions.js";
17
+ import { resetCachedPipeline } from "../Materials/effect.functions.js";
16
18
  /**
17
19
  * Keeps track of all the buffer info used in engine.
18
20
  */
@@ -270,6 +272,10 @@ export class ThinEngine extends AbstractEngine {
270
272
  if (this._renderingCanvas && this._renderingCanvas.setAttribute) {
271
273
  this._renderingCanvas.setAttribute("data-engine", versionToLog);
272
274
  }
275
+ const stateObject = getStateObject(this._gl);
276
+ // update state object with the current engine state
277
+ stateObject.validateShaderPrograms = this.validateShaderPrograms;
278
+ stateObject.parallelShaderCompile = this._caps.parallelShaderCompile;
273
279
  }
274
280
  _clearEmptyResources() {
275
281
  this._dummyFramebuffer = null;
@@ -1498,9 +1504,16 @@ export class ThinEngine extends AbstractEngine {
1498
1504
  const webGLPipelineContext = pipelineContext;
1499
1505
  if (webGLPipelineContext && webGLPipelineContext.program) {
1500
1506
  webGLPipelineContext.program.__SPECTOR_rebuildProgram = null;
1507
+ resetCachedPipeline(webGLPipelineContext);
1501
1508
  this._gl.deleteProgram(webGLPipelineContext.program);
1502
1509
  }
1503
1510
  }
1511
+ /**
1512
+ * @internal
1513
+ */
1514
+ _getGlobalDefines(defines) {
1515
+ return _getGlobalDefines(defines, this.isNDCHalfZRange, this.useReverseDepthBuffer, this.useExactSrgbConversions);
1516
+ }
1504
1517
  /**
1505
1518
  * Create a new effect (used to store vertex/fragment shaders)
1506
1519
  * @param baseName defines the base name of the effect (The name of file without .fragment.fx or .vertex.fx)
@@ -1531,32 +1544,13 @@ export class ThinEngine extends AbstractEngine {
1531
1544
  }
1532
1545
  return compiledEffect;
1533
1546
  }
1547
+ if (this._gl) {
1548
+ getStateObject(this._gl);
1549
+ }
1534
1550
  const effect = new Effect(baseName, attributesNamesOrOptions, uniformsNamesOrEngine, samplers, this, defines, fallbacks, onCompiled, onError, indexParameters, name, attributesNamesOrOptions.shaderLanguage ?? shaderLanguage);
1535
1551
  this._compiledEffects[name] = effect;
1536
1552
  return effect;
1537
1553
  }
1538
- // eslint-disable-next-line @typescript-eslint/naming-convention
1539
- static _ConcatenateShader(source, defines, shaderVersion = "") {
1540
- return shaderVersion + (defines ? defines + "\n" : "") + source;
1541
- }
1542
- _compileShader(source, type, defines, shaderVersion) {
1543
- return this._compileRawShader(ThinEngine._ConcatenateShader(source, defines, shaderVersion), type);
1544
- }
1545
- _compileRawShader(source, type) {
1546
- const gl = this._gl;
1547
- const shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
1548
- if (!shader) {
1549
- let error = gl.NO_ERROR;
1550
- let tempError = gl.NO_ERROR;
1551
- while ((tempError = gl.getError()) !== gl.NO_ERROR) {
1552
- error = tempError;
1553
- }
1554
- throw new Error(`Something went wrong while creating a gl ${type} shader object. gl error=${error}, gl isContextLost=${gl.isContextLost()}, _contextWasLost=${this._contextWasLost}`);
1555
- }
1556
- gl.shaderSource(shader, source);
1557
- gl.compileShader(shader);
1558
- return shader;
1559
- }
1560
1554
  /**
1561
1555
  * @internal
1562
1556
  */
@@ -1573,10 +1567,10 @@ export class ThinEngine extends AbstractEngine {
1573
1567
  * @returns the new webGL program
1574
1568
  */
1575
1569
  createRawShaderProgram(pipelineContext, vertexCode, fragmentCode, context, transformFeedbackVaryings = null) {
1576
- context = context || this._gl;
1577
- const vertexShader = this._compileRawShader(vertexCode, "vertex");
1578
- const fragmentShader = this._compileRawShader(fragmentCode, "fragment");
1579
- return this._createShaderProgram(pipelineContext, vertexShader, fragmentShader, context, transformFeedbackVaryings);
1570
+ const stateObject = getStateObject(this._gl);
1571
+ stateObject._contextWasLost = this._contextWasLost;
1572
+ stateObject.validateShaderPrograms = this.validateShaderPrograms;
1573
+ return createRawShaderProgram(pipelineContext, vertexCode, fragmentCode, context || this._gl, transformFeedbackVaryings);
1580
1574
  }
1581
1575
  /**
1582
1576
  * Creates a webGL program
@@ -1589,11 +1583,11 @@ export class ThinEngine extends AbstractEngine {
1589
1583
  * @returns the new webGL program
1590
1584
  */
1591
1585
  createShaderProgram(pipelineContext, vertexCode, fragmentCode, defines, context, transformFeedbackVaryings = null) {
1592
- context = context || this._gl;
1593
- const shaderVersion = this._webGLVersion > 1 ? "#version 300 es\n#define WEBGL2 \n" : "";
1594
- const vertexShader = this._compileShader(vertexCode, "vertex", defines, shaderVersion);
1595
- const fragmentShader = this._compileShader(fragmentCode, "fragment", defines, shaderVersion);
1596
- return this._createShaderProgram(pipelineContext, vertexShader, fragmentShader, context, transformFeedbackVaryings);
1586
+ const stateObject = getStateObject(this._gl);
1587
+ // assure the state object is correct
1588
+ stateObject._contextWasLost = this._contextWasLost;
1589
+ stateObject.validateShaderPrograms = this.validateShaderPrograms;
1590
+ return createShaderProgram(pipelineContext, vertexCode, fragmentCode, defines, context || this._gl, transformFeedbackVaryings);
1597
1591
  }
1598
1592
  /**
1599
1593
  * Inline functions in shader code that are marked to be inlined
@@ -1610,12 +1604,13 @@ export class ThinEngine extends AbstractEngine {
1610
1604
  * @returns the new pipeline
1611
1605
  */
1612
1606
  createPipelineContext(shaderProcessingContext) {
1613
- const pipelineContext = new WebGLPipelineContext();
1614
- pipelineContext.engine = this;
1615
- if (this._caps.parallelShaderCompile) {
1616
- pipelineContext.isParallelCompiled = true;
1607
+ if (this._gl) {
1608
+ const stateObject = getStateObject(this._gl);
1609
+ stateObject.parallelShaderCompile = this._caps.parallelShaderCompile;
1617
1610
  }
1618
- return pipelineContext;
1611
+ const context = createPipelineContext(this._gl, shaderProcessingContext);
1612
+ context.engine = this;
1613
+ return context;
1619
1614
  }
1620
1615
  /**
1621
1616
  * Creates a new material context
@@ -1631,85 +1626,23 @@ export class ThinEngine extends AbstractEngine {
1631
1626
  createDrawContext() {
1632
1627
  return undefined;
1633
1628
  }
1634
- _createShaderProgram(pipelineContext, vertexShader, fragmentShader, context, transformFeedbackVaryings = null) {
1635
- const shaderProgram = context.createProgram();
1636
- pipelineContext.program = shaderProgram;
1637
- if (!shaderProgram) {
1638
- throw new Error("Unable to create program");
1639
- }
1640
- context.attachShader(shaderProgram, vertexShader);
1641
- context.attachShader(shaderProgram, fragmentShader);
1642
- context.linkProgram(shaderProgram);
1643
- pipelineContext.context = context;
1644
- pipelineContext.vertexShader = vertexShader;
1645
- pipelineContext.fragmentShader = fragmentShader;
1646
- if (!pipelineContext.isParallelCompiled) {
1647
- this._finalizePipelineContext(pipelineContext);
1648
- }
1649
- return shaderProgram;
1650
- }
1651
1629
  _finalizePipelineContext(pipelineContext) {
1652
- const context = pipelineContext.context;
1653
- const vertexShader = pipelineContext.vertexShader;
1654
- const fragmentShader = pipelineContext.fragmentShader;
1655
- const program = pipelineContext.program;
1656
- const linked = context.getProgramParameter(program, context.LINK_STATUS);
1657
- if (!linked) {
1658
- // Get more info
1659
- // Vertex
1660
- if (!this._gl.getShaderParameter(vertexShader, this._gl.COMPILE_STATUS)) {
1661
- const log = this._gl.getShaderInfoLog(vertexShader);
1662
- if (log) {
1663
- pipelineContext.vertexCompilationError = log;
1664
- throw new Error("VERTEX SHADER " + log);
1665
- }
1666
- }
1667
- // Fragment
1668
- if (!this._gl.getShaderParameter(fragmentShader, this._gl.COMPILE_STATUS)) {
1669
- const log = this._gl.getShaderInfoLog(fragmentShader);
1670
- if (log) {
1671
- pipelineContext.fragmentCompilationError = log;
1672
- throw new Error("FRAGMENT SHADER " + log);
1673
- }
1674
- }
1675
- const error = context.getProgramInfoLog(program);
1676
- if (error) {
1677
- pipelineContext.programLinkError = error;
1678
- throw new Error(error);
1679
- }
1680
- }
1681
- if (this.validateShaderPrograms) {
1682
- context.validateProgram(program);
1683
- const validated = context.getProgramParameter(program, context.VALIDATE_STATUS);
1684
- if (!validated) {
1685
- const error = context.getProgramInfoLog(program);
1686
- if (error) {
1687
- pipelineContext.programValidationError = error;
1688
- throw new Error(error);
1689
- }
1690
- }
1691
- }
1692
- context.deleteShader(vertexShader);
1693
- context.deleteShader(fragmentShader);
1694
- pipelineContext.vertexShader = undefined;
1695
- pipelineContext.fragmentShader = undefined;
1696
- if (pipelineContext.onCompiled) {
1697
- pipelineContext.onCompiled();
1698
- pipelineContext.onCompiled = undefined;
1699
- }
1630
+ return _finalizePipelineContext(pipelineContext, this._gl, this.validateShaderPrograms);
1700
1631
  }
1701
1632
  /**
1702
1633
  * @internal
1703
1634
  */
1704
- _preparePipelineContext(pipelineContext, vertexSourceCode, fragmentSourceCode, createAsRaw, rawVertexSourceCode, rawFragmentSourceCode, rebuildRebind, defines, transformFeedbackVaryings, key) {
1705
- const webGLRenderingState = pipelineContext;
1706
- if (createAsRaw) {
1707
- webGLRenderingState.program = this.createRawShaderProgram(webGLRenderingState, vertexSourceCode, fragmentSourceCode, undefined, transformFeedbackVaryings);
1708
- }
1709
- else {
1710
- webGLRenderingState.program = this.createShaderProgram(webGLRenderingState, vertexSourceCode, fragmentSourceCode, defines, undefined, transformFeedbackVaryings);
1711
- }
1712
- webGLRenderingState.program.__SPECTOR_rebuildProgram = rebuildRebind;
1635
+ _preparePipelineContext(pipelineContext, vertexSourceCode, fragmentSourceCode, createAsRaw, _rawVertexSourceCode, _rawFragmentSourceCode, rebuildRebind, defines, transformFeedbackVaryings, _key) {
1636
+ const stateObject = getStateObject(this._gl);
1637
+ stateObject._contextWasLost = this._contextWasLost;
1638
+ stateObject.validateShaderPrograms = this.validateShaderPrograms;
1639
+ stateObject._createShaderProgramInjection = this._createShaderProgram.bind(this);
1640
+ stateObject.createRawShaderProgramInjection = this.createRawShaderProgram.bind(this);
1641
+ stateObject.createShaderProgramInjection = this.createShaderProgram.bind(this);
1642
+ return _preparePipelineContext(pipelineContext, vertexSourceCode, fragmentSourceCode, createAsRaw, _rawVertexSourceCode, _rawFragmentSourceCode, rebuildRebind, defines, transformFeedbackVaryings, _key);
1643
+ }
1644
+ _createShaderProgram(pipelineContext, vertexShader, fragmentShader, context, transformFeedbackVaryings = null) {
1645
+ return _createShaderProgram(pipelineContext, vertexShader, fragmentShader, context, transformFeedbackVaryings);
1713
1646
  }
1714
1647
  /**
1715
1648
  * @internal
@@ -1729,21 +1662,7 @@ export class ThinEngine extends AbstractEngine {
1729
1662
  * @internal
1730
1663
  */
1731
1664
  _executeWhenRenderingStateIsCompiled(pipelineContext, action) {
1732
- const webGLPipelineContext = pipelineContext;
1733
- if (!webGLPipelineContext.isParallelCompiled) {
1734
- action();
1735
- return;
1736
- }
1737
- const oldHandler = webGLPipelineContext.onCompiled;
1738
- if (oldHandler) {
1739
- webGLPipelineContext.onCompiled = () => {
1740
- oldHandler();
1741
- action();
1742
- };
1743
- }
1744
- else {
1745
- webGLPipelineContext.onCompiled = action;
1746
- }
1665
+ _executeWhenRenderingStateIsCompiled(pipelineContext, action);
1747
1666
  }
1748
1667
  /**
1749
1668
  * Gets the list of webGL uniform locations associated with a specific program based on a list of uniform names
@@ -2990,7 +2909,7 @@ export class ThinEngine extends AbstractEngine {
2990
2909
  }
2991
2910
  _setProgram(program) {
2992
2911
  if (this._currentProgram !== program) {
2993
- this._gl.useProgram(program);
2912
+ _setProgram(program, this._gl);
2994
2913
  this._currentProgram = program;
2995
2914
  }
2996
2915
  }
@@ -3292,8 +3211,6 @@ export class ThinEngine extends AbstractEngine {
3292
3211
  if (this._dummyFramebuffer) {
3293
3212
  this._gl.deleteFramebuffer(this._dummyFramebuffer);
3294
3213
  }
3295
- // Release effects
3296
- this.releaseEffects();
3297
3214
  // Unbind
3298
3215
  this.unbindAllAttributes();
3299
3216
  this._boundUniforms = {};
@@ -3313,6 +3230,8 @@ export class ThinEngine extends AbstractEngine {
3313
3230
  if (this._creationOptions.loseContextOnDispose) {
3314
3231
  this._gl.getExtension("WEBGL_lose_context")?.loseContext();
3315
3232
  }
3233
+ // clear the state object
3234
+ deleteStateObject(this._gl);
3316
3235
  }
3317
3236
  /**
3318
3237
  * Attach a new callback raised when context lost event is fired
@@ -3768,6 +3687,8 @@ ThinEngine.ExceptionList = [
3768
3687
  * Gets or sets the epsilon value used by collision engine
3769
3688
  */
3770
3689
  ThinEngine.CollisionsEpsilon = 0.001;
3690
+ // eslint-disable-next-line @typescript-eslint/naming-convention
3691
+ ThinEngine._ConcatenateShader = _ConcatenateShader;
3771
3692
  // Statics
3772
3693
  ThinEngine._IsSupported = null;
3773
3694
  ThinEngine._HasMajorPerformanceCaveat = null;