@babylonjs/core 8.45.4 → 8.45.5

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 (56) hide show
  1. package/AudioV2/webAudio/components/webAudioParameterComponent.js +5 -1
  2. package/AudioV2/webAudio/components/webAudioParameterComponent.js.map +1 -1
  3. package/Engines/AbstractEngine/abstractEngine.loadFile.d.ts +11 -0
  4. package/Engines/AbstractEngine/abstractEngine.loadFile.js +12 -0
  5. package/Engines/AbstractEngine/abstractEngine.loadFile.js.map +1 -0
  6. package/Engines/AbstractEngine/abstractEngine.textureLoaders.d.ts +1 -0
  7. package/Engines/AbstractEngine/abstractEngine.textureLoaders.js +4 -0
  8. package/Engines/AbstractEngine/abstractEngine.textureLoaders.js.map +1 -0
  9. package/Engines/AbstractEngine/index.d.ts +2 -0
  10. package/Engines/AbstractEngine/index.js +2 -0
  11. package/Engines/AbstractEngine/index.js.map +1 -1
  12. package/Engines/Native/Extensions/index.d.ts +1 -0
  13. package/Engines/Native/Extensions/index.js +2 -0
  14. package/Engines/Native/Extensions/index.js.map +1 -0
  15. package/Engines/Native/Extensions/nativeEngine.cubeTexture.d.ts +27 -0
  16. package/Engines/Native/Extensions/nativeEngine.cubeTexture.js +96 -0
  17. package/Engines/Native/Extensions/nativeEngine.cubeTexture.js.map +1 -0
  18. package/Engines/Native/nativePipelineContext.d.ts +2 -2
  19. package/Engines/Native/nativePipelineContext.js.map +1 -1
  20. package/Engines/Native/nativeRenderTargetWrapper.d.ts +3 -3
  21. package/Engines/Native/nativeRenderTargetWrapper.js.map +1 -1
  22. package/Engines/Native/validatedNativeDataStream.js +2 -2
  23. package/Engines/Native/validatedNativeDataStream.js.map +1 -1
  24. package/Engines/abstractEngine.d.ts +9 -0
  25. package/Engines/abstractEngine.js +13 -4
  26. package/Engines/abstractEngine.js.map +1 -1
  27. package/Engines/engine.d.ts +2 -6
  28. package/Engines/engine.js +2 -13
  29. package/Engines/engine.js.map +1 -1
  30. package/Engines/index.d.ts +1 -0
  31. package/Engines/index.js +1 -0
  32. package/Engines/index.js.map +1 -1
  33. package/Engines/nativeEngine.d.ts +19 -536
  34. package/Engines/nativeEngine.js +27 -2127
  35. package/Engines/nativeEngine.js.map +1 -1
  36. package/Engines/nullEngine.d.ts +2 -0
  37. package/Engines/nullEngine.js +2 -0
  38. package/Engines/nullEngine.js.map +1 -1
  39. package/Engines/thinNativeEngine.d.ts +537 -0
  40. package/Engines/thinNativeEngine.js +2033 -0
  41. package/Engines/thinNativeEngine.js.map +1 -0
  42. package/Engines/webgpuEngine.d.ts +2 -0
  43. package/Engines/webgpuEngine.js +2 -0
  44. package/Engines/webgpuEngine.js.map +1 -1
  45. package/Misc/tools.d.ts +3 -1
  46. package/Misc/tools.js +76 -59
  47. package/Misc/tools.js.map +1 -1
  48. package/Particles/Node/Blocks/index.d.ts +2 -0
  49. package/Particles/Node/Blocks/index.js +2 -0
  50. package/Particles/Node/Blocks/index.js.map +1 -1
  51. package/Particles/Node/Blocks/particleClampBlock.d.ts +42 -0
  52. package/Particles/Node/Blocks/particleClampBlock.js +114 -0
  53. package/Particles/Node/Blocks/particleClampBlock.js.map +1 -0
  54. package/XR/native/nativeXRFrame.js +1 -1
  55. package/XR/native/nativeXRFrame.js.map +1 -1
  56. package/package.json +1 -1
@@ -0,0 +1,2033 @@
1
+ import { InternalTexture } from "../Materials/Textures/internalTexture.js";
2
+ import { DataBuffer } from "../Buffers/dataBuffer.js";
3
+ import { Observable } from "../Misc/observable.js";
4
+ import { Logger } from "../Misc/logger.js";
5
+
6
+ import { AbstractEngine } from "./abstractEngine.js";
7
+ import { ThinEngine } from "./thinEngine.js";
8
+ import { EngineStore } from "./engineStore.js";
9
+ import { ShaderCodeInliner } from "./Processors/shaderCodeInliner.js";
10
+ import { NativeShaderProcessor } from "./Native/nativeShaderProcessors.js";
11
+ import { NativeDataStream } from "./Native/nativeDataStream.js";
12
+ import { NativePipelineContext } from "./Native/nativePipelineContext.js";
13
+ import { NativeRenderTargetWrapper } from "./Native/nativeRenderTargetWrapper.js";
14
+ import { NativeHardwareTexture } from "./Native/nativeHardwareTexture.js";
15
+ import { getNativeAlphaMode, getNativeAttribType, getNativeSamplingMode, getNativeTextureFormat, getNativeStencilDepthFail, getNativeStencilDepthPass, getNativeStencilFunc, getNativeStencilOpFail, getNativeAddressMode, } from "./Native/nativeHelpers.js";
16
+ import { checkNonFloatVertexBuffers } from "../Buffers/buffer.nonFloatVertexBuffers.js";
17
+ import { NativeShaderProcessingContext } from "./Native/nativeShaderProcessingContext.js";
18
+ import "../Buffers/buffer.align.js";
19
+ import { _TimeToken } from "../Instrumentation/timeToken.js";
20
+ import { PerfCounter } from "../Misc/perfCounter.js";
21
+ import { DecodeBase64UrlToBinary } from "../Misc/fileTools.js";
22
+ const onNativeObjectInitialized = new Observable();
23
+ if (typeof self !== "undefined" && !Object.prototype.hasOwnProperty.call(self, "_native")) {
24
+ let __native;
25
+ Object.defineProperty(self, "_native", {
26
+ get: () => __native,
27
+ set: (value) => {
28
+ __native = value;
29
+ if (__native) {
30
+ onNativeObjectInitialized.notifyObservers(__native);
31
+ }
32
+ },
33
+ });
34
+ }
35
+ /**
36
+ * Returns _native only after it has been defined by BabylonNative.
37
+ * @internal
38
+ */
39
+ export async function AcquireNativeObjectAsync() {
40
+ return await new Promise((resolve) => {
41
+ if (typeof _native === "undefined") {
42
+ onNativeObjectInitialized.addOnce((nativeObject) => resolve(nativeObject));
43
+ }
44
+ else {
45
+ resolve(_native);
46
+ }
47
+ });
48
+ }
49
+ /**
50
+ * Registers a constructor on the _native object. See NativeXRFrame for an example.
51
+ * @internal
52
+ */
53
+ export async function RegisterNativeTypeAsync(typeName, constructor) {
54
+ (await AcquireNativeObjectAsync())[typeName] = constructor;
55
+ }
56
+ /**
57
+ * Container for accessors for natively-stored mesh data buffers.
58
+ */
59
+ class NativeDataBuffer extends DataBuffer {
60
+ }
61
+ /** @internal */
62
+ class CommandBufferEncoder {
63
+ constructor(_engine) {
64
+ this._engine = _engine;
65
+ this._pending = new Array();
66
+ this._isCommandBufferScopeActive = false;
67
+ this._commandStream = ThinNativeEngine._createNativeDataStream();
68
+ this._engine.setCommandDataStream(this._commandStream);
69
+ }
70
+ beginCommandScope() {
71
+ if (this._isCommandBufferScopeActive) {
72
+ throw new Error("Command scope already active.");
73
+ }
74
+ this._isCommandBufferScopeActive = true;
75
+ }
76
+ endCommandScope() {
77
+ if (!this._isCommandBufferScopeActive) {
78
+ throw new Error("Command scope is not active.");
79
+ }
80
+ this._isCommandBufferScopeActive = false;
81
+ this._submit();
82
+ }
83
+ startEncodingCommand(command) {
84
+ this._commandStream.writeNativeData(command);
85
+ }
86
+ encodeCommandArgAsUInt32(commandArg) {
87
+ this._commandStream.writeUint32(commandArg);
88
+ }
89
+ encodeCommandArgAsUInt32s(commandArg) {
90
+ this._commandStream.writeUint32Array(commandArg);
91
+ }
92
+ encodeCommandArgAsInt32(commandArg) {
93
+ this._commandStream.writeInt32(commandArg);
94
+ }
95
+ encodeCommandArgAsInt32s(commandArg) {
96
+ this._commandStream.writeInt32Array(commandArg);
97
+ }
98
+ encodeCommandArgAsFloat32(commandArg) {
99
+ this._commandStream.writeFloat32(commandArg);
100
+ }
101
+ encodeCommandArgAsFloat32s(commandArg) {
102
+ this._commandStream.writeFloat32Array(commandArg);
103
+ }
104
+ encodeCommandArgAsNativeData(commandArg) {
105
+ this._commandStream.writeNativeData(commandArg);
106
+ this._pending.push(commandArg);
107
+ }
108
+ finishEncodingCommand() {
109
+ if (!this._isCommandBufferScopeActive) {
110
+ this._submit();
111
+ }
112
+ }
113
+ _submit() {
114
+ this._engine.submitCommands();
115
+ this._pending.length = 0;
116
+ }
117
+ }
118
+ const remappedAttributesNames = [];
119
+ /** @internal */
120
+ export class ThinNativeEngine extends ThinEngine {
121
+ /** @internal */
122
+ static _createNativeDataStream() {
123
+ return new NativeDataStream();
124
+ }
125
+ constructor(options = {}) {
126
+ super(null, false, undefined, options.adaptToDeviceRatio);
127
+ this._initializeNativeEngine(options.adaptToDeviceRatio ?? false);
128
+ }
129
+ //////////////////////////////////////////////////////////////////////
130
+ /**
131
+ * Keeps as a separate function to use in NativeEngine
132
+ * @internal
133
+ */
134
+ _initializeNativeEngine(adaptToDeviceRatio) {
135
+ this._engine = new _native.Engine({
136
+ version: AbstractEngine.Version,
137
+ nonFloatVertexBuffers: true,
138
+ });
139
+ this._camera = _native.Camera ? new _native.Camera() : null;
140
+ this._commandBufferEncoder = new CommandBufferEncoder(this._engine);
141
+ this._frameStats = { gpuTimeNs: Number.NaN };
142
+ this._boundBuffersVertexArray = null;
143
+ this._currentDepthTest = _native.Engine.DEPTH_TEST_LEQUAL;
144
+ this._stencilTest;
145
+ this._stencilMask = 255;
146
+ this._stencilFunc = 519;
147
+ this._stencilFuncRef = 0;
148
+ this._stencilFuncMask = 255;
149
+ this._stencilOpStencilFail = 7680;
150
+ this._stencilOpDepthFail = 7680;
151
+ this._stencilOpStencilDepthPass = 7681;
152
+ this._zOffset = 0;
153
+ this._zOffsetUnits = 0;
154
+ this._depthWrite = true;
155
+ // warning for non supported fill mode has already been displayed
156
+ this._fillModeWarningDisplayed = false;
157
+ this._drawCalls = new PerfCounter();
158
+ if (_native.Engine.PROTOCOL_VERSION !== ThinNativeEngine.PROTOCOL_VERSION) {
159
+ throw new Error(`Protocol version mismatch: ${_native.Engine.PROTOCOL_VERSION} (Native) !== ${ThinNativeEngine.PROTOCOL_VERSION} (JS)`);
160
+ }
161
+ if (this._engine.setDeviceLostCallback) {
162
+ this._engine.setDeviceLostCallback(() => {
163
+ this.onContextLostObservable.notifyObservers(this);
164
+ this._contextWasLost = true;
165
+ this._restoreEngineAfterContextLost();
166
+ });
167
+ }
168
+ this._webGLVersion = 2;
169
+ this.disableUniformBuffers = true;
170
+ this._shaderPlatformName = "NATIVE";
171
+ // TODO: Initialize this more correctly based on the hardware capabilities.
172
+ // Init caps
173
+ this._caps = {
174
+ maxTexturesImageUnits: 16,
175
+ maxVertexTextureImageUnits: 16,
176
+ maxCombinedTexturesImageUnits: 32,
177
+ maxTextureSize: _native.Engine.CAPS_LIMITS_MAX_TEXTURE_SIZE,
178
+ maxCubemapTextureSize: 512,
179
+ maxRenderTextureSize: 512,
180
+ maxVertexAttribs: 16,
181
+ maxVaryingVectors: 16,
182
+ maxDrawBuffers: 8,
183
+ maxFragmentUniformVectors: 16,
184
+ maxVertexUniformVectors: 16,
185
+ shaderFloatPrecision: 23, // TODO: is this correct?
186
+ standardDerivatives: true,
187
+ astc: null,
188
+ pvrtc: null,
189
+ etc1: null,
190
+ etc2: null,
191
+ bptc: null,
192
+ maxAnisotropy: 16, // TODO: Retrieve this smartly. Currently set to D3D11 maximum allowable value.
193
+ uintIndices: true,
194
+ fragmentDepthSupported: false,
195
+ highPrecisionShaderSupported: true,
196
+ colorBufferFloat: false,
197
+ blendFloat: false,
198
+ supportFloatTexturesResolve: false,
199
+ rg11b10ufColorRenderable: false,
200
+ textureFloat: true,
201
+ textureFloatLinearFiltering: true,
202
+ textureFloatRender: true,
203
+ textureHalfFloat: true,
204
+ textureHalfFloatLinearFiltering: true,
205
+ textureHalfFloatRender: true,
206
+ textureLOD: true,
207
+ texelFetch: false,
208
+ drawBuffersExtension: false,
209
+ depthTextureExtension: false,
210
+ vertexArrayObject: true,
211
+ instancedArrays: true,
212
+ supportOcclusionQuery: false,
213
+ canUseTimestampForTimerQuery: false,
214
+ blendMinMax: false,
215
+ maxMSAASamples: 16,
216
+ canUseGLInstanceID: true,
217
+ canUseGLVertexID: true,
218
+ supportComputeShaders: false,
219
+ supportSRGBBuffers: true,
220
+ supportTransformFeedbacks: false,
221
+ textureMaxLevel: false,
222
+ texture2DArrayMaxLayerCount: _native.Engine.CAPS_LIMITS_MAX_TEXTURE_LAYERS,
223
+ disableMorphTargetTexture: false,
224
+ parallelShaderCompile: { COMPLETION_STATUS_KHR: 0 },
225
+ textureNorm16: false,
226
+ blendParametersPerTarget: false,
227
+ dualSourceBlending: false,
228
+ };
229
+ this._features = {
230
+ forceBitmapOverHTMLImageElement: true,
231
+ supportRenderAndCopyToLodForFloatTextures: false,
232
+ supportDepthStencilTexture: false,
233
+ supportShadowSamplers: false,
234
+ uniformBufferHardCheckMatrix: false,
235
+ allowTexturePrefiltering: false,
236
+ trackUbosInFrame: false,
237
+ checkUbosContentBeforeUpload: false,
238
+ supportCSM: false,
239
+ basisNeedsPOT: false,
240
+ support3DTextures: false,
241
+ needTypeSuffixInShaderConstants: false,
242
+ supportMSAA: true,
243
+ supportSSAO2: false,
244
+ supportIBLShadows: false,
245
+ supportExtendedTextureFormats: false,
246
+ supportSwitchCaseInShader: false,
247
+ supportSyncTextureRead: false,
248
+ needsInvertingBitmap: true,
249
+ useUBOBindingCache: true,
250
+ needShaderCodeInlining: true,
251
+ needToAlwaysBindUniformBuffers: false,
252
+ supportRenderPasses: true,
253
+ supportSpriteInstancing: false,
254
+ forceVertexBufferStrideAndOffsetMultiple4Bytes: true,
255
+ _checkNonFloatVertexBuffersDontRecreatePipelineContext: false,
256
+ _collectUbosUpdatedInFrame: false,
257
+ };
258
+ Logger.Log("Babylon Native (v" + AbstractEngine.Version + ") launched");
259
+ // Wrappers
260
+ if (typeof URL === "undefined") {
261
+ window.URL = {
262
+ createObjectURL: function () { },
263
+ revokeObjectURL: function () { },
264
+ };
265
+ }
266
+ // TODO: Remove in next protocol version update
267
+ if (typeof Blob === "undefined") {
268
+ window.Blob = function (v) {
269
+ return v;
270
+ };
271
+ }
272
+ // polyfill for Chakra
273
+ if (!Array.prototype.flat) {
274
+ Object.defineProperty(Array.prototype, "flat", {
275
+ configurable: true,
276
+ value: function flat() {
277
+ const depth = isNaN(arguments[0]) ? 1 : Number(arguments[0]);
278
+ return depth
279
+ ? Array.prototype.reduce.call(this, function (acc, cur) {
280
+ if (Array.isArray(cur)) {
281
+ // eslint-disable-next-line prefer-spread
282
+ acc.push.apply(acc, flat.call(cur, depth - 1));
283
+ }
284
+ else {
285
+ acc.push(cur);
286
+ }
287
+ return acc;
288
+ }, [])
289
+ : Array.prototype.slice.call(this);
290
+ },
291
+ writable: true,
292
+ });
293
+ }
294
+ // Currently we do not fully configure the ThinEngine on construction of NativeEngine.
295
+ // Setup resolution scaling based on display settings.
296
+ const devicePixelRatio = window ? window.devicePixelRatio || 1.0 : 1.0;
297
+ this._hardwareScalingLevel = adaptToDeviceRatio ? 1.0 / devicePixelRatio : 1.0;
298
+ this._engine.setHardwareScalingLevel(this._hardwareScalingLevel);
299
+ this._lastDevicePixelRatio = devicePixelRatio;
300
+ this.resize();
301
+ const currentDepthFunction = this.getDepthFunction();
302
+ if (currentDepthFunction) {
303
+ this.setDepthFunction(currentDepthFunction);
304
+ }
305
+ // Shader processor
306
+ this._shaderProcessor = new NativeShaderProcessor();
307
+ this.onNewSceneAddedObservable.add((scene) => {
308
+ const originalRender = scene.render;
309
+ scene.render = (...args) => {
310
+ this._commandBufferEncoder.beginCommandScope();
311
+ originalRender.apply(scene, args);
312
+ this._commandBufferEncoder.endCommandScope();
313
+ };
314
+ });
315
+ }
316
+ setHardwareScalingLevel(level) {
317
+ super.setHardwareScalingLevel(level);
318
+ this._engine.setHardwareScalingLevel(level);
319
+ }
320
+ dispose() {
321
+ super.dispose();
322
+ if (this._boundBuffersVertexArray) {
323
+ this._deleteVertexArray(this._boundBuffersVertexArray);
324
+ }
325
+ this._engine.dispose();
326
+ }
327
+ /**
328
+ * Enable scissor test on a specific rectangle (ie. render will only be executed on a specific portion of the screen)
329
+ * @param x defines the x-coordinate of the bottom left corner of the clear rectangle
330
+ * @param y defines the y-coordinate of the corner of the clear rectangle
331
+ * @param width defines the width of the clear rectangle
332
+ * @param height defines the height of the clear rectangle
333
+ */
334
+ enableScissor(x, y, width, height) {
335
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETSCISSOR);
336
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(x);
337
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(y);
338
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(width);
339
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(height);
340
+ this._commandBufferEncoder.finishEncodingCommand();
341
+ }
342
+ /**
343
+ * Disable previously set scissor test rectangle
344
+ */
345
+ disableScissor() {
346
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETSCISSOR);
347
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(0);
348
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(0);
349
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(0);
350
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(0);
351
+ this._commandBufferEncoder.finishEncodingCommand();
352
+ }
353
+ /**
354
+ * Can be used to override the current requestAnimationFrame requester.
355
+ * @internal
356
+ */
357
+ _queueNewFrame(bindedRenderFunction, requester) {
358
+ // Use the provided requestAnimationFrame, unless the requester is the window. In that case, we will default to the Babylon Native version of requestAnimationFrame.
359
+ if (requester.requestAnimationFrame && requester !== window) {
360
+ requester.requestAnimationFrame(bindedRenderFunction);
361
+ }
362
+ else {
363
+ this._engine.requestAnimationFrame(bindedRenderFunction);
364
+ }
365
+ return 0;
366
+ }
367
+ _restoreEngineAfterContextLost() {
368
+ this._clearEmptyResources();
369
+ const depthTest = this._depthCullingState.depthTest; // backup those values because the call to initEngine / wipeCaches will reset them
370
+ const depthFunc = this._depthCullingState.depthFunc;
371
+ const depthMask = this._depthCullingState.depthMask;
372
+ const stencilTest = this._stencilState.stencilTest;
373
+ this._rebuildGraphicsResources();
374
+ this._depthCullingState.depthTest = depthTest;
375
+ this._depthCullingState.depthFunc = depthFunc;
376
+ this._depthCullingState.depthMask = depthMask;
377
+ this._stencilState.stencilTest = stencilTest;
378
+ this._flagContextRestored();
379
+ }
380
+ /**
381
+ * Override default engine behavior.
382
+ * @param framebuffer
383
+ */
384
+ _bindUnboundFramebuffer(framebuffer) {
385
+ if (this._currentFramebuffer !== framebuffer) {
386
+ if (this._currentFramebuffer) {
387
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_UNBINDFRAMEBUFFER);
388
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(this._currentFramebuffer);
389
+ this._commandBufferEncoder.finishEncodingCommand();
390
+ }
391
+ if (framebuffer) {
392
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_BINDFRAMEBUFFER);
393
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(framebuffer);
394
+ this._commandBufferEncoder.finishEncodingCommand();
395
+ }
396
+ this._currentFramebuffer = framebuffer;
397
+ }
398
+ }
399
+ /**
400
+ * Gets host document
401
+ * @returns the host document object
402
+ */
403
+ getHostDocument() {
404
+ return null;
405
+ }
406
+ clear(color, backBuffer, depth, stencil = false, stencilClearValue = 0) {
407
+ if (this.useReverseDepthBuffer) {
408
+ throw new Error("reverse depth buffer is not currently implemented");
409
+ }
410
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_CLEAR);
411
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(backBuffer && color ? 1 : 0);
412
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(color ? color.r : 0);
413
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(color ? color.g : 0);
414
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(color ? color.b : 0);
415
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(color ? color.a : 1);
416
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(depth ? 1 : 0);
417
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(1);
418
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(stencil ? 1 : 0);
419
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(stencilClearValue);
420
+ this._commandBufferEncoder.finishEncodingCommand();
421
+ }
422
+ createIndexBuffer(indices, updateable, _label) {
423
+ const data = this._normalizeIndexData(indices);
424
+ const buffer = new NativeDataBuffer();
425
+ buffer.references = 1;
426
+ buffer.is32Bits = data.BYTES_PER_ELEMENT === 4;
427
+ if (data.byteLength) {
428
+ buffer.nativeIndexBuffer = this._engine.createIndexBuffer(data.buffer, data.byteOffset, data.byteLength, buffer.is32Bits, updateable ?? false);
429
+ }
430
+ return buffer;
431
+ }
432
+ createVertexBuffer(vertices, updateable, _label) {
433
+ const data = ArrayBuffer.isView(vertices) ? vertices : new Float32Array(vertices);
434
+ const buffer = new NativeDataBuffer();
435
+ buffer.references = 1;
436
+ if (data.byteLength) {
437
+ buffer.nativeVertexBuffer = this._engine.createVertexBuffer(data.buffer, data.byteOffset, data.byteLength, updateable ?? false);
438
+ }
439
+ return buffer;
440
+ }
441
+ _recordVertexArrayObject(vertexArray, vertexBuffers, indexBuffer, effect, overrideVertexBuffers) {
442
+ if (!effect._checkedNonFloatVertexBuffers) {
443
+ checkNonFloatVertexBuffers(vertexBuffers, effect);
444
+ effect._checkedNonFloatVertexBuffers = true;
445
+ }
446
+ if (indexBuffer) {
447
+ this._engine.recordIndexBuffer(vertexArray, indexBuffer.nativeIndexBuffer);
448
+ }
449
+ const attributes = effect.getAttributesNames();
450
+ for (let index = 0; index < attributes.length; index++) {
451
+ const location = effect.getAttributeLocation(index);
452
+ if (location >= 0) {
453
+ const kind = attributes[index];
454
+ let vertexBuffer = null;
455
+ if (overrideVertexBuffers) {
456
+ vertexBuffer = overrideVertexBuffers[kind];
457
+ }
458
+ if (!vertexBuffer) {
459
+ vertexBuffer = vertexBuffers[kind];
460
+ }
461
+ if (vertexBuffer) {
462
+ const buffer = vertexBuffer.effectiveBuffer;
463
+ if (buffer && buffer.nativeVertexBuffer) {
464
+ this._engine.recordVertexBuffer(vertexArray, buffer.nativeVertexBuffer, location, vertexBuffer.effectiveByteOffset, vertexBuffer.effectiveByteStride, vertexBuffer.getSize(), getNativeAttribType(vertexBuffer.type), vertexBuffer.normalized, vertexBuffer.getInstanceDivisor());
465
+ }
466
+ }
467
+ }
468
+ }
469
+ }
470
+ bindBuffers(vertexBuffers, indexBuffer, effect) {
471
+ if (this._boundBuffersVertexArray) {
472
+ this._deleteVertexArray(this._boundBuffersVertexArray);
473
+ }
474
+ this._boundBuffersVertexArray = this._engine.createVertexArray();
475
+ this._recordVertexArrayObject(this._boundBuffersVertexArray, vertexBuffers, indexBuffer, effect);
476
+ this.bindVertexArrayObject(this._boundBuffersVertexArray);
477
+ }
478
+ recordVertexArrayObject(vertexBuffers, indexBuffer, effect, overrideVertexBuffers) {
479
+ const vertexArray = this._engine.createVertexArray();
480
+ this._recordVertexArrayObject(vertexArray, vertexBuffers, indexBuffer, effect, overrideVertexBuffers);
481
+ return vertexArray;
482
+ }
483
+ _deleteVertexArray(vertexArray) {
484
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_DELETEVERTEXARRAY);
485
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(vertexArray);
486
+ this._commandBufferEncoder.finishEncodingCommand();
487
+ }
488
+ bindVertexArrayObject(vertexArray) {
489
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_BINDVERTEXARRAY);
490
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(vertexArray);
491
+ this._commandBufferEncoder.finishEncodingCommand();
492
+ }
493
+ releaseVertexArrayObject(vertexArray) {
494
+ this._deleteVertexArray(vertexArray);
495
+ }
496
+ getAttributes(pipelineContext, attributesNames) {
497
+ const nativePipelineContext = pipelineContext;
498
+ const nativeShaderProcessingContext = nativePipelineContext.shaderProcessingContext;
499
+ remappedAttributesNames.length = 0;
500
+ for (let index = 0; index < attributesNames.length; index++) {
501
+ const origAttributeName = attributesNames[index];
502
+ const attributeName = nativeShaderProcessingContext.remappedAttributeNames[origAttributeName] ?? origAttributeName;
503
+ remappedAttributesNames[index] = attributeName;
504
+ }
505
+ return this._engine.getAttributes(nativePipelineContext.program, remappedAttributesNames);
506
+ }
507
+ /**
508
+ * Triangle Fan and Line Loop are not supported by modern rendering API
509
+ * @param fillMode defines the primitive to use
510
+ * @returns true if supported
511
+ */
512
+ _checkSupportedFillMode(fillMode) {
513
+ if (fillMode == 5 || fillMode == 8) {
514
+ if (!this._fillModeWarningDisplayed) {
515
+ Logger.Warn("Line Loop and Triangle Fan are not supported fill modes with Babylon Native. Elements with these fill mode will not be visible.");
516
+ this._fillModeWarningDisplayed = true;
517
+ }
518
+ return false;
519
+ }
520
+ return true;
521
+ }
522
+ /**
523
+ * Draw a list of indexed primitives
524
+ * @param fillMode defines the primitive to use
525
+ * @param indexStart defines the starting index
526
+ * @param indexCount defines the number of index to draw
527
+ * @param instancesCount defines the number of instances to draw (if instantiation is enabled)
528
+ */
529
+ drawElementsType(fillMode, indexStart, indexCount, instancesCount) {
530
+ if (!this._checkSupportedFillMode(fillMode)) {
531
+ return;
532
+ }
533
+ // Apply states
534
+ this._drawCalls.addCount(1, false);
535
+ if (instancesCount) {
536
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_DRAWINDEXEDINSTANCED);
537
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(fillMode);
538
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(indexStart);
539
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(indexCount);
540
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(instancesCount);
541
+ }
542
+ else {
543
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_DRAWINDEXED);
544
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(fillMode);
545
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(indexStart);
546
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(indexCount);
547
+ }
548
+ this._commandBufferEncoder.finishEncodingCommand();
549
+ }
550
+ /**
551
+ * Draw a list of unindexed primitives
552
+ * @param fillMode defines the primitive to use
553
+ * @param verticesStart defines the index of first vertex to draw
554
+ * @param verticesCount defines the count of vertices to draw
555
+ * @param instancesCount defines the number of instances to draw (if instantiation is enabled)
556
+ */
557
+ drawArraysType(fillMode, verticesStart, verticesCount, instancesCount) {
558
+ if (!this._checkSupportedFillMode(fillMode)) {
559
+ return;
560
+ }
561
+ // Apply states
562
+ this._drawCalls.addCount(1, false);
563
+ if (instancesCount) {
564
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_DRAWINSTANCED);
565
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(fillMode);
566
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(verticesStart);
567
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(verticesCount);
568
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(instancesCount);
569
+ }
570
+ else {
571
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_DRAW);
572
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(fillMode);
573
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(verticesStart);
574
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(verticesCount);
575
+ }
576
+ this._commandBufferEncoder.finishEncodingCommand();
577
+ }
578
+ createPipelineContext(shaderProcessingContext) {
579
+ const isAsync = !!this._caps.parallelShaderCompile;
580
+ return new NativePipelineContext(this, isAsync, shaderProcessingContext);
581
+ }
582
+ createMaterialContext() {
583
+ return undefined;
584
+ }
585
+ createDrawContext() {
586
+ return undefined;
587
+ }
588
+ /**
589
+ * Function is not technically Async
590
+ * @internal
591
+ */
592
+ // eslint-disable-next-line no-restricted-syntax
593
+ _preparePipelineContextAsync(pipelineContext, vertexSourceCode, fragmentSourceCode, createAsRaw, _rawVertexSourceCode, _rawFragmentSourceCode, _rebuildRebind, defines, _transformFeedbackVaryings, _key, onReady) {
594
+ if (createAsRaw) {
595
+ this.createRawShaderProgram();
596
+ }
597
+ else {
598
+ this.createShaderProgram(pipelineContext, vertexSourceCode, fragmentSourceCode, defines);
599
+ }
600
+ onReady();
601
+ }
602
+ /**
603
+ * @internal
604
+ */
605
+ _getShaderProcessingContext(_shaderLanguage) {
606
+ return new NativeShaderProcessingContext();
607
+ }
608
+ /**
609
+ * @internal
610
+ */
611
+ _executeWhenRenderingStateIsCompiled(pipelineContext, action) {
612
+ const nativePipelineContext = pipelineContext;
613
+ if (nativePipelineContext.isAsync) {
614
+ if (nativePipelineContext.onCompiled) {
615
+ const oldHandler = nativePipelineContext.onCompiled;
616
+ nativePipelineContext.onCompiled = () => {
617
+ oldHandler();
618
+ action();
619
+ };
620
+ }
621
+ else {
622
+ nativePipelineContext.onCompiled = action;
623
+ }
624
+ }
625
+ else {
626
+ action();
627
+ }
628
+ }
629
+ createRawShaderProgram() {
630
+ throw new Error("Not Supported");
631
+ }
632
+ createShaderProgram(pipelineContext, vertexCode, fragmentCode, defines) {
633
+ const nativePipelineContext = pipelineContext;
634
+ this.onBeforeShaderCompilationObservable.notifyObservers(this);
635
+ const vertexInliner = new ShaderCodeInliner(vertexCode);
636
+ vertexInliner.processCode();
637
+ vertexCode = vertexInliner.code;
638
+ const fragmentInliner = new ShaderCodeInliner(fragmentCode);
639
+ fragmentInliner.processCode();
640
+ fragmentCode = fragmentInliner.code;
641
+ vertexCode = ThinEngine._ConcatenateShader(vertexCode, defines);
642
+ fragmentCode = ThinEngine._ConcatenateShader(fragmentCode, defines);
643
+ const onSuccess = () => {
644
+ nativePipelineContext.isCompiled = true;
645
+ nativePipelineContext.onCompiled?.();
646
+ this.onAfterShaderCompilationObservable.notifyObservers(this);
647
+ };
648
+ if (pipelineContext.isAsync) {
649
+ nativePipelineContext.program = this._engine.createProgramAsync(vertexCode, fragmentCode, onSuccess, (error) => {
650
+ nativePipelineContext.compilationError = error;
651
+ });
652
+ }
653
+ else {
654
+ try {
655
+ nativePipelineContext.program = this._engine.createProgram(vertexCode, fragmentCode);
656
+ onSuccess();
657
+ }
658
+ catch (e) {
659
+ const message = e?.message;
660
+ throw new Error("SHADER ERROR" + (typeof message === "string" ? "\n" + message : ""));
661
+ }
662
+ }
663
+ return nativePipelineContext.program;
664
+ }
665
+ /**
666
+ * Inline functions in shader code that are marked to be inlined
667
+ * @param code code to inline
668
+ * @returns inlined code
669
+ */
670
+ inlineShaderCode(code) {
671
+ const sci = new ShaderCodeInliner(code);
672
+ sci.debug = false;
673
+ sci.processCode();
674
+ return sci.code;
675
+ }
676
+ _setProgram(program) {
677
+ if (this._currentProgram !== program) {
678
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETPROGRAM);
679
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(program);
680
+ this._commandBufferEncoder.finishEncodingCommand();
681
+ this._currentProgram = program;
682
+ }
683
+ }
684
+ _deletePipelineContext(pipelineContext) {
685
+ const nativePipelineContext = pipelineContext;
686
+ if (nativePipelineContext && nativePipelineContext.program) {
687
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_DELETEPROGRAM);
688
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(nativePipelineContext.program);
689
+ this._commandBufferEncoder.finishEncodingCommand();
690
+ }
691
+ }
692
+ getUniforms(pipelineContext, uniformsNames) {
693
+ const nativePipelineContext = pipelineContext;
694
+ return this._engine.getUniforms(nativePipelineContext.program, uniformsNames);
695
+ }
696
+ bindUniformBlock(pipelineContext, blockName, index) {
697
+ // TODO
698
+ throw new Error("Not Implemented");
699
+ }
700
+ bindSamplers(effect) {
701
+ const nativePipelineContext = effect.getPipelineContext();
702
+ this._setProgram(nativePipelineContext.program);
703
+ // TODO: share this with engine?
704
+ const samplers = effect.getSamplers();
705
+ for (let index = 0; index < samplers.length; index++) {
706
+ const uniform = effect.getUniform(samplers[index]);
707
+ if (uniform) {
708
+ this._boundUniforms[index] = uniform;
709
+ }
710
+ }
711
+ this._currentEffect = null;
712
+ }
713
+ getRenderWidth(useScreen = false) {
714
+ if (!useScreen && this._currentRenderTarget) {
715
+ return this._currentRenderTarget.width;
716
+ }
717
+ return this._engine.getRenderWidth();
718
+ }
719
+ getRenderHeight(useScreen = false) {
720
+ if (!useScreen && this._currentRenderTarget) {
721
+ return this._currentRenderTarget.height;
722
+ }
723
+ return this._engine.getRenderHeight();
724
+ }
725
+ setViewport(viewport, requiredWidth, requiredHeight) {
726
+ this._cachedViewport = viewport;
727
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETVIEWPORT);
728
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(viewport.x);
729
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(viewport.y);
730
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(viewport.width);
731
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(viewport.height);
732
+ this._commandBufferEncoder.finishEncodingCommand();
733
+ }
734
+ setStateCullFaceType(_cullBackFaces, _force) {
735
+ throw new Error("setStateCullFaceType: Not Implemented");
736
+ }
737
+ setState(culling, zOffset = 0, force, reverseSide = false, cullBackFaces, stencil, zOffsetUnits = 0) {
738
+ this._zOffset = zOffset;
739
+ this._zOffsetUnits = zOffsetUnits;
740
+ if (this._zOffset !== 0) {
741
+ Logger.Warn("zOffset is not supported in Native engine.");
742
+ }
743
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETSTATE);
744
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(culling ? 1 : 0);
745
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(zOffset);
746
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(zOffsetUnits);
747
+ this._commandBufferEncoder.encodeCommandArgAsUInt32((this.cullBackFaces ?? cullBackFaces ?? true) ? 1 : 0);
748
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(reverseSide ? 1 : 0);
749
+ this._commandBufferEncoder.finishEncodingCommand();
750
+ }
751
+ /**
752
+ * Gets the client rect of native canvas. Needed for InputManager.
753
+ * @returns a client rectangle
754
+ */
755
+ getInputElementClientRect() {
756
+ const rect = {
757
+ bottom: this.getRenderHeight(),
758
+ height: this.getRenderHeight(),
759
+ left: 0,
760
+ right: this.getRenderWidth(),
761
+ top: 0,
762
+ width: this.getRenderWidth(),
763
+ x: 0,
764
+ y: 0,
765
+ toJSON: () => { },
766
+ };
767
+ return rect;
768
+ }
769
+ /**
770
+ * Set the z offset Factor to apply to current rendering
771
+ * @param value defines the offset to apply
772
+ */
773
+ setZOffset(value) {
774
+ if (value !== this._zOffset) {
775
+ this._zOffset = value;
776
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETZOFFSET);
777
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(this.useReverseDepthBuffer ? -value : value);
778
+ this._commandBufferEncoder.finishEncodingCommand();
779
+ }
780
+ }
781
+ /**
782
+ * Gets the current value of the zOffset Factor
783
+ * @returns the current zOffset Factor state
784
+ */
785
+ getZOffset() {
786
+ return this._zOffset;
787
+ }
788
+ /**
789
+ * Set the z offset Units to apply to current rendering
790
+ * @param value defines the offset to apply
791
+ */
792
+ setZOffsetUnits(value) {
793
+ if (value !== this._zOffsetUnits) {
794
+ this._zOffsetUnits = value;
795
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETZOFFSETUNITS);
796
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(this.useReverseDepthBuffer ? -value : value);
797
+ this._commandBufferEncoder.finishEncodingCommand();
798
+ }
799
+ }
800
+ /**
801
+ * Gets the current value of the zOffset Units
802
+ * @returns the current zOffset Units state
803
+ */
804
+ getZOffsetUnits() {
805
+ return this._zOffsetUnits;
806
+ }
807
+ /**
808
+ * Enable or disable depth buffering
809
+ * @param enable defines the state to set
810
+ */
811
+ setDepthBuffer(enable) {
812
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETDEPTHTEST);
813
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(enable ? this._currentDepthTest : _native.Engine.DEPTH_TEST_ALWAYS);
814
+ this._commandBufferEncoder.finishEncodingCommand();
815
+ }
816
+ /**
817
+ * Gets a boolean indicating if depth writing is enabled
818
+ * @returns the current depth writing state
819
+ */
820
+ getDepthWrite() {
821
+ return this._depthWrite;
822
+ }
823
+ getDepthFunction() {
824
+ switch (this._currentDepthTest) {
825
+ case _native.Engine.DEPTH_TEST_NEVER:
826
+ return 512;
827
+ case _native.Engine.DEPTH_TEST_ALWAYS:
828
+ return 519;
829
+ case _native.Engine.DEPTH_TEST_GREATER:
830
+ return 516;
831
+ case _native.Engine.DEPTH_TEST_GEQUAL:
832
+ return 518;
833
+ case _native.Engine.DEPTH_TEST_NOTEQUAL:
834
+ return 517;
835
+ case _native.Engine.DEPTH_TEST_EQUAL:
836
+ return 514;
837
+ case _native.Engine.DEPTH_TEST_LESS:
838
+ return 513;
839
+ case _native.Engine.DEPTH_TEST_LEQUAL:
840
+ return 515;
841
+ }
842
+ return null;
843
+ }
844
+ setDepthFunction(depthFunc) {
845
+ let nativeDepthFunc = 0;
846
+ switch (depthFunc) {
847
+ case 512:
848
+ nativeDepthFunc = _native.Engine.DEPTH_TEST_NEVER;
849
+ break;
850
+ case 519:
851
+ nativeDepthFunc = _native.Engine.DEPTH_TEST_ALWAYS;
852
+ break;
853
+ case 516:
854
+ nativeDepthFunc = _native.Engine.DEPTH_TEST_GREATER;
855
+ break;
856
+ case 518:
857
+ nativeDepthFunc = _native.Engine.DEPTH_TEST_GEQUAL;
858
+ break;
859
+ case 517:
860
+ nativeDepthFunc = _native.Engine.DEPTH_TEST_NOTEQUAL;
861
+ break;
862
+ case 514:
863
+ nativeDepthFunc = _native.Engine.DEPTH_TEST_EQUAL;
864
+ break;
865
+ case 513:
866
+ nativeDepthFunc = _native.Engine.DEPTH_TEST_LESS;
867
+ break;
868
+ case 515:
869
+ nativeDepthFunc = _native.Engine.DEPTH_TEST_LEQUAL;
870
+ break;
871
+ }
872
+ this._currentDepthTest = nativeDepthFunc;
873
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETDEPTHTEST);
874
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(this._currentDepthTest);
875
+ this._commandBufferEncoder.finishEncodingCommand();
876
+ }
877
+ /**
878
+ * Enable or disable depth writing
879
+ * @param enable defines the state to set
880
+ */
881
+ setDepthWrite(enable) {
882
+ this._depthWrite = enable;
883
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETDEPTHWRITE);
884
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(Number(enable));
885
+ this._commandBufferEncoder.finishEncodingCommand();
886
+ }
887
+ /**
888
+ * Enable or disable color writing
889
+ * @param enable defines the state to set
890
+ */
891
+ setColorWrite(enable) {
892
+ this._colorWrite = enable;
893
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETCOLORWRITE);
894
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(Number(enable));
895
+ this._commandBufferEncoder.finishEncodingCommand();
896
+ }
897
+ /**
898
+ * Gets a boolean indicating if color writing is enabled
899
+ * @returns the current color writing state
900
+ */
901
+ getColorWrite() {
902
+ return this._colorWrite;
903
+ }
904
+ applyStencil() {
905
+ this._setStencil(this._stencilMask, getNativeStencilOpFail(this._stencilOpStencilFail), getNativeStencilDepthFail(this._stencilOpDepthFail), getNativeStencilDepthPass(this._stencilOpStencilDepthPass), getNativeStencilFunc(this._stencilFunc), this._stencilFuncRef);
906
+ }
907
+ _setStencil(mask, stencilOpFail, depthOpFail, depthOpPass, func, ref) {
908
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETSTENCIL);
909
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(mask);
910
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(stencilOpFail);
911
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(depthOpFail);
912
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(depthOpPass);
913
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(func);
914
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(ref);
915
+ this._commandBufferEncoder.finishEncodingCommand();
916
+ }
917
+ /**
918
+ * Enable or disable the stencil buffer
919
+ * @param enable defines if the stencil buffer must be enabled or disabled
920
+ */
921
+ setStencilBuffer(enable) {
922
+ this._stencilTest = enable;
923
+ if (enable) {
924
+ this.applyStencil();
925
+ }
926
+ else {
927
+ this._setStencil(255, _native.Engine.STENCIL_OP_FAIL_S_KEEP, _native.Engine.STENCIL_OP_FAIL_Z_KEEP, _native.Engine.STENCIL_OP_PASS_Z_KEEP, _native.Engine.STENCIL_TEST_ALWAYS, 0);
928
+ }
929
+ }
930
+ /**
931
+ * Gets a boolean indicating if stencil buffer is enabled
932
+ * @returns the current stencil buffer state
933
+ */
934
+ getStencilBuffer() {
935
+ return this._stencilTest;
936
+ }
937
+ /**
938
+ * Gets the current stencil operation when stencil passes
939
+ * @returns a number defining stencil operation to use when stencil passes
940
+ */
941
+ getStencilOperationPass() {
942
+ return this._stencilOpStencilDepthPass;
943
+ }
944
+ /**
945
+ * Sets the stencil operation to use when stencil passes
946
+ * @param operation defines the stencil operation to use when stencil passes
947
+ */
948
+ setStencilOperationPass(operation) {
949
+ this._stencilOpStencilDepthPass = operation;
950
+ this.applyStencil();
951
+ }
952
+ /**
953
+ * Sets the current stencil mask
954
+ * @param mask defines the new stencil mask to use
955
+ */
956
+ setStencilMask(mask) {
957
+ this._stencilMask = mask;
958
+ this.applyStencil();
959
+ }
960
+ /**
961
+ * Sets the current stencil function
962
+ * @param stencilFunc defines the new stencil function to use
963
+ */
964
+ setStencilFunction(stencilFunc) {
965
+ this._stencilFunc = stencilFunc;
966
+ this.applyStencil();
967
+ }
968
+ /**
969
+ * Sets the current stencil reference
970
+ * @param reference defines the new stencil reference to use
971
+ */
972
+ setStencilFunctionReference(reference) {
973
+ this._stencilFuncRef = reference;
974
+ this.applyStencil();
975
+ }
976
+ /**
977
+ * Sets the current stencil mask
978
+ * @param mask defines the new stencil mask to use
979
+ */
980
+ setStencilFunctionMask(mask) {
981
+ this._stencilFuncMask = mask;
982
+ }
983
+ /**
984
+ * Sets the stencil operation to use when stencil fails
985
+ * @param operation defines the stencil operation to use when stencil fails
986
+ */
987
+ setStencilOperationFail(operation) {
988
+ this._stencilOpStencilFail = operation;
989
+ this.applyStencil();
990
+ }
991
+ /**
992
+ * Sets the stencil operation to use when depth fails
993
+ * @param operation defines the stencil operation to use when depth fails
994
+ */
995
+ setStencilOperationDepthFail(operation) {
996
+ this._stencilOpDepthFail = operation;
997
+ this.applyStencil();
998
+ }
999
+ /**
1000
+ * Gets the current stencil mask
1001
+ * @returns a number defining the new stencil mask to use
1002
+ */
1003
+ getStencilMask() {
1004
+ return this._stencilMask;
1005
+ }
1006
+ /**
1007
+ * Gets the current stencil function
1008
+ * @returns a number defining the stencil function to use
1009
+ */
1010
+ getStencilFunction() {
1011
+ return this._stencilFunc;
1012
+ }
1013
+ /**
1014
+ * Gets the current stencil reference value
1015
+ * @returns a number defining the stencil reference value to use
1016
+ */
1017
+ getStencilFunctionReference() {
1018
+ return this._stencilFuncRef;
1019
+ }
1020
+ /**
1021
+ * Gets the current stencil mask
1022
+ * @returns a number defining the stencil mask to use
1023
+ */
1024
+ getStencilFunctionMask() {
1025
+ return this._stencilFuncMask;
1026
+ }
1027
+ /**
1028
+ * Gets the current stencil operation when stencil fails
1029
+ * @returns a number defining stencil operation to use when stencil fails
1030
+ */
1031
+ getStencilOperationFail() {
1032
+ return this._stencilOpStencilFail;
1033
+ }
1034
+ /**
1035
+ * Gets the current stencil operation when depth fails
1036
+ * @returns a number defining stencil operation to use when depth fails
1037
+ */
1038
+ getStencilOperationDepthFail() {
1039
+ return this._stencilOpDepthFail;
1040
+ }
1041
+ /**
1042
+ * Sets alpha constants used by some alpha blending modes
1043
+ * @param r defines the red component
1044
+ * @param g defines the green component
1045
+ * @param b defines the blue component
1046
+ * @param a defines the alpha component
1047
+ */
1048
+ setAlphaConstants(r, g, b, a) {
1049
+ throw new Error("Setting alpha blend constant color not yet implemented.");
1050
+ }
1051
+ /**
1052
+ * Sets the current alpha mode
1053
+ * @param mode defines the mode to use (one of the BABYLON.undefined)
1054
+ * @param noDepthWriteChange defines if depth writing state should remains unchanged (false by default)
1055
+ * @param targetIndex defines the index of the target to set the alpha mode for (default is 0)
1056
+ * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/advanced/transparent_rendering
1057
+ */
1058
+ setAlphaMode(mode, noDepthWriteChange = false, targetIndex = 0) {
1059
+ if (this._alphaMode[targetIndex] === mode) {
1060
+ return;
1061
+ }
1062
+ const nativeMode = getNativeAlphaMode(mode);
1063
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETBLENDMODE);
1064
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(nativeMode);
1065
+ this._commandBufferEncoder.finishEncodingCommand();
1066
+ if (!noDepthWriteChange) {
1067
+ this.setDepthWrite(mode === 0);
1068
+ }
1069
+ this._alphaMode[targetIndex] = mode;
1070
+ }
1071
+ setInt(uniform, int) {
1072
+ if (!uniform) {
1073
+ return false;
1074
+ }
1075
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETINT);
1076
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1077
+ this._commandBufferEncoder.encodeCommandArgAsInt32(int);
1078
+ this._commandBufferEncoder.finishEncodingCommand();
1079
+ return true;
1080
+ }
1081
+ setIntArray(uniform, array) {
1082
+ if (!uniform) {
1083
+ return false;
1084
+ }
1085
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETINTARRAY);
1086
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1087
+ this._commandBufferEncoder.encodeCommandArgAsInt32s(array);
1088
+ this._commandBufferEncoder.finishEncodingCommand();
1089
+ return true;
1090
+ }
1091
+ setIntArray2(uniform, array) {
1092
+ if (!uniform) {
1093
+ return false;
1094
+ }
1095
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETINTARRAY2);
1096
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1097
+ this._commandBufferEncoder.encodeCommandArgAsInt32s(array);
1098
+ this._commandBufferEncoder.finishEncodingCommand();
1099
+ return true;
1100
+ }
1101
+ setIntArray3(uniform, array) {
1102
+ if (!uniform) {
1103
+ return false;
1104
+ }
1105
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETINTARRAY3);
1106
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1107
+ this._commandBufferEncoder.encodeCommandArgAsInt32s(array);
1108
+ this._commandBufferEncoder.finishEncodingCommand();
1109
+ return true;
1110
+ }
1111
+ setIntArray4(uniform, array) {
1112
+ if (!uniform) {
1113
+ return false;
1114
+ }
1115
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETINTARRAY4);
1116
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1117
+ this._commandBufferEncoder.encodeCommandArgAsInt32s(array);
1118
+ this._commandBufferEncoder.finishEncodingCommand();
1119
+ return true;
1120
+ }
1121
+ setFloatArray(uniform, array) {
1122
+ if (!uniform) {
1123
+ return false;
1124
+ }
1125
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETFLOATARRAY);
1126
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1127
+ this._commandBufferEncoder.encodeCommandArgAsFloat32s(array);
1128
+ this._commandBufferEncoder.finishEncodingCommand();
1129
+ return true;
1130
+ }
1131
+ setFloatArray2(uniform, array) {
1132
+ if (!uniform) {
1133
+ return false;
1134
+ }
1135
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETFLOATARRAY2);
1136
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1137
+ this._commandBufferEncoder.encodeCommandArgAsFloat32s(array);
1138
+ this._commandBufferEncoder.finishEncodingCommand();
1139
+ return true;
1140
+ }
1141
+ setFloatArray3(uniform, array) {
1142
+ if (!uniform) {
1143
+ return false;
1144
+ }
1145
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETFLOATARRAY3);
1146
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1147
+ this._commandBufferEncoder.encodeCommandArgAsFloat32s(array);
1148
+ this._commandBufferEncoder.finishEncodingCommand();
1149
+ return true;
1150
+ }
1151
+ setFloatArray4(uniform, array) {
1152
+ if (!uniform) {
1153
+ return false;
1154
+ }
1155
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETFLOATARRAY4);
1156
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1157
+ this._commandBufferEncoder.encodeCommandArgAsFloat32s(array);
1158
+ this._commandBufferEncoder.finishEncodingCommand();
1159
+ return true;
1160
+ }
1161
+ setArray(uniform, array) {
1162
+ if (!uniform) {
1163
+ return false;
1164
+ }
1165
+ return this.setFloatArray(uniform, new Float32Array(array));
1166
+ }
1167
+ setArray2(uniform, array) {
1168
+ if (!uniform) {
1169
+ return false;
1170
+ }
1171
+ return this.setFloatArray2(uniform, new Float32Array(array));
1172
+ }
1173
+ setArray3(uniform, array) {
1174
+ if (!uniform) {
1175
+ return false;
1176
+ }
1177
+ return this.setFloatArray3(uniform, new Float32Array(array));
1178
+ }
1179
+ setArray4(uniform, array) {
1180
+ if (!uniform) {
1181
+ return false;
1182
+ }
1183
+ return this.setFloatArray4(uniform, new Float32Array(array));
1184
+ }
1185
+ setMatrices(uniform, matrices) {
1186
+ if (!uniform) {
1187
+ return false;
1188
+ }
1189
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETMATRICES);
1190
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1191
+ this._commandBufferEncoder.encodeCommandArgAsFloat32s(matrices);
1192
+ this._commandBufferEncoder.finishEncodingCommand();
1193
+ return true;
1194
+ }
1195
+ setMatrix3x3(uniform, matrix) {
1196
+ if (!uniform) {
1197
+ return false;
1198
+ }
1199
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETMATRIX3X3);
1200
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1201
+ this._commandBufferEncoder.encodeCommandArgAsFloat32s(matrix);
1202
+ this._commandBufferEncoder.finishEncodingCommand();
1203
+ return true;
1204
+ }
1205
+ setMatrix2x2(uniform, matrix) {
1206
+ if (!uniform) {
1207
+ return false;
1208
+ }
1209
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETMATRIX2X2);
1210
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1211
+ this._commandBufferEncoder.encodeCommandArgAsFloat32s(matrix);
1212
+ this._commandBufferEncoder.finishEncodingCommand();
1213
+ return true;
1214
+ }
1215
+ setFloat(uniform, value) {
1216
+ if (!uniform) {
1217
+ return false;
1218
+ }
1219
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETFLOAT);
1220
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1221
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(value);
1222
+ this._commandBufferEncoder.finishEncodingCommand();
1223
+ return true;
1224
+ }
1225
+ setFloat2(uniform, x, y) {
1226
+ if (!uniform) {
1227
+ return false;
1228
+ }
1229
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETFLOAT2);
1230
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1231
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(x);
1232
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(y);
1233
+ this._commandBufferEncoder.finishEncodingCommand();
1234
+ return true;
1235
+ }
1236
+ setFloat3(uniform, x, y, z) {
1237
+ if (!uniform) {
1238
+ return false;
1239
+ }
1240
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETFLOAT3);
1241
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1242
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(x);
1243
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(y);
1244
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(z);
1245
+ this._commandBufferEncoder.finishEncodingCommand();
1246
+ return true;
1247
+ }
1248
+ setFloat4(uniform, x, y, z, w) {
1249
+ if (!uniform) {
1250
+ return false;
1251
+ }
1252
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETFLOAT4);
1253
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1254
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(x);
1255
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(y);
1256
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(z);
1257
+ this._commandBufferEncoder.encodeCommandArgAsFloat32(w);
1258
+ this._commandBufferEncoder.finishEncodingCommand();
1259
+ return true;
1260
+ }
1261
+ setColor3(uniform, color3) {
1262
+ if (!uniform) {
1263
+ return false;
1264
+ }
1265
+ this.setFloat3(uniform, color3.r, color3.g, color3.b);
1266
+ return true;
1267
+ }
1268
+ setColor4(uniform, color3, alpha) {
1269
+ if (!uniform) {
1270
+ return false;
1271
+ }
1272
+ this.setFloat4(uniform, color3.r, color3.g, color3.b, alpha);
1273
+ return true;
1274
+ }
1275
+ wipeCaches(bruteForce) {
1276
+ if (this.preventCacheWipeBetweenFrames) {
1277
+ return;
1278
+ }
1279
+ this.resetTextureCache();
1280
+ this._currentEffect = null;
1281
+ if (bruteForce) {
1282
+ this._currentProgram = null;
1283
+ this._stencilStateComposer.reset();
1284
+ this._depthCullingState.reset();
1285
+ this._alphaState.reset();
1286
+ }
1287
+ this._cachedVertexBuffers = null;
1288
+ this._cachedIndexBuffer = null;
1289
+ this._cachedEffectForVertexBuffers = null;
1290
+ }
1291
+ _createTexture() {
1292
+ return this._engine.createTexture();
1293
+ }
1294
+ _deleteTexture(texture) {
1295
+ if (texture) {
1296
+ this._engine.deleteTexture(texture.underlyingResource);
1297
+ }
1298
+ }
1299
+ /**
1300
+ * Update the content of a dynamic texture
1301
+ * @param texture defines the texture to update
1302
+ * @param canvas defines the canvas containing the source
1303
+ * @param invertY defines if data must be stored with Y axis inverted
1304
+ * @param premulAlpha defines if alpha is stored as premultiplied
1305
+ * @param format defines the format of the data
1306
+ */
1307
+ updateDynamicTexture(texture, canvas, invertY, premulAlpha = false, format) {
1308
+ if (premulAlpha === void 0) {
1309
+ premulAlpha = false;
1310
+ }
1311
+ if (!!texture && !!texture._hardwareTexture) {
1312
+ const destination = texture._hardwareTexture.underlyingResource;
1313
+ const context = canvas.getContext();
1314
+ // flush need to happen before getCanvasTexture: flush will create the render target synchronously (if it's not been created before)
1315
+ context.flush();
1316
+ const source = canvas.getCanvasTexture();
1317
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_COPYTEXTURE);
1318
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(source);
1319
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(destination);
1320
+ this._commandBufferEncoder.finishEncodingCommand();
1321
+ texture.isReady = true;
1322
+ }
1323
+ }
1324
+ createDynamicTexture(width, height, generateMipMaps, samplingMode) {
1325
+ // it's not possible to create 0x0 texture sized. Many bgfx methods assume texture size is at least 1x1(best case).
1326
+ // Worst case is getting a crash/assert.
1327
+ width = Math.max(width, 1);
1328
+ height = Math.max(height, 1);
1329
+ return this.createRawTexture(new Uint8Array(width * height * 4), width, height, 5, false, false, samplingMode);
1330
+ }
1331
+ createVideoElement(constraints) {
1332
+ // create native object depending on stream. Only NativeCamera is supported for now.
1333
+ if (this._camera) {
1334
+ return this._camera.createVideo(constraints);
1335
+ }
1336
+ return null;
1337
+ }
1338
+ updateVideoTexture(texture, video, invertY) {
1339
+ if (texture && texture._hardwareTexture && this._camera) {
1340
+ const webGLTexture = texture._hardwareTexture.underlyingResource;
1341
+ this._camera.updateVideoTexture(webGLTexture, video, invertY);
1342
+ }
1343
+ }
1344
+ createRawTexture(data, width, height, format, generateMipMaps, invertY, samplingMode, compression = null, type = 0, creationFlags = 0, useSRGBBuffer = false) {
1345
+ const texture = new InternalTexture(this, 3 /* InternalTextureSource.Raw */);
1346
+ texture.format = format;
1347
+ texture.generateMipMaps = generateMipMaps;
1348
+ texture.samplingMode = samplingMode;
1349
+ texture.invertY = invertY;
1350
+ texture.baseWidth = width;
1351
+ texture.baseHeight = height;
1352
+ texture.width = texture.baseWidth;
1353
+ texture.height = texture.baseHeight;
1354
+ texture._compression = compression;
1355
+ texture.type = type;
1356
+ texture._useSRGBBuffer = this._getUseSRGBBuffer(useSRGBBuffer, !generateMipMaps);
1357
+ this.updateRawTexture(texture, data, format, invertY, compression, type, texture._useSRGBBuffer);
1358
+ if (texture._hardwareTexture) {
1359
+ const webGLTexture = texture._hardwareTexture.underlyingResource;
1360
+ const filter = getNativeSamplingMode(samplingMode);
1361
+ this._setTextureSampling(webGLTexture, filter);
1362
+ }
1363
+ this._internalTexturesCache.push(texture);
1364
+ return texture;
1365
+ }
1366
+ createRawTexture2DArray(data, width, height, depth, format, generateMipMaps, invertY, samplingMode, compression = null, textureType = 0) {
1367
+ const texture = new InternalTexture(this, 11 /* InternalTextureSource.Raw2DArray */);
1368
+ texture.baseWidth = width;
1369
+ texture.baseHeight = height;
1370
+ texture.baseDepth = depth;
1371
+ texture.width = width;
1372
+ texture.height = height;
1373
+ texture.depth = depth;
1374
+ texture.format = format;
1375
+ texture.type = textureType;
1376
+ texture.generateMipMaps = generateMipMaps;
1377
+ texture.samplingMode = samplingMode;
1378
+ texture.is2DArray = true;
1379
+ if (texture._hardwareTexture) {
1380
+ const nativeTexture = texture._hardwareTexture.underlyingResource;
1381
+ this._engine.loadRawTexture2DArray(nativeTexture, data, width, height, depth, getNativeTextureFormat(format, textureType), generateMipMaps, invertY);
1382
+ const filter = getNativeSamplingMode(samplingMode);
1383
+ this._setTextureSampling(nativeTexture, filter);
1384
+ }
1385
+ texture.isReady = true;
1386
+ this._internalTexturesCache.push(texture);
1387
+ return texture;
1388
+ }
1389
+ updateRawTexture(texture, bufferView, format, invertY, compression = null, type = 0, useSRGBBuffer = false) {
1390
+ if (!texture) {
1391
+ return;
1392
+ }
1393
+ if (bufferView && texture._hardwareTexture) {
1394
+ const underlyingResource = texture._hardwareTexture.underlyingResource;
1395
+ this._engine.loadRawTexture(underlyingResource, bufferView, texture.width, texture.height, getNativeTextureFormat(format, type), texture.generateMipMaps, texture.invertY);
1396
+ }
1397
+ texture.isReady = true;
1398
+ }
1399
+ // TODO: Refactor to share more logic with babylon.engine.ts version.
1400
+ /**
1401
+ * Usually called from Texture.ts.
1402
+ * Passed information to create a NativeTexture
1403
+ * @param url defines a value which contains one of the following:
1404
+ * * A conventional http URL, e.g. 'http://...' or 'file://...'
1405
+ * * A base64 string of in-line texture data, e.g. 'data:image/jpg;base64,/...'
1406
+ * * An indicator that data being passed using the buffer parameter, e.g. 'data:mytexture.jpg'
1407
+ * @param noMipmap defines a boolean indicating that no mipmaps shall be generated. Ignored for compressed textures. They must be in the file
1408
+ * @param invertY when true, image is flipped when loaded. You probably want true. Certain compressed textures may invert this if their default is inverted (eg. ktx)
1409
+ * @param scene needed for loading to the correct scene
1410
+ * @param samplingMode mode with should be used sample / access the texture (Default: Texture.TRILINEAR_SAMPLINGMODE)
1411
+ * @param onLoad optional callback to be called upon successful completion
1412
+ * @param onError optional callback to be called upon failure
1413
+ * @param buffer a source of a file previously fetched as either a base64 string, an ArrayBuffer (compressed or image format), HTMLImageElement (image format), or a Blob
1414
+ * @param fallback an internal argument in case the function must be called again, due to etc1 not having alpha capabilities
1415
+ * @param format internal format. Default: RGB when extension is '.jpg' else RGBA. Ignored for compressed textures
1416
+ * @param forcedExtension defines the extension to use to pick the right loader
1417
+ * @param mimeType defines an optional mime type
1418
+ * @param loaderOptions options to be passed to the loader
1419
+ * @param creationFlags specific flags to use when creating the texture (1 for storage textures, for eg)
1420
+ * @param useSRGBBuffer defines if the texture must be loaded in a sRGB GPU buffer (if supported by the GPU).
1421
+ * @returns a InternalTexture for assignment back into BABYLON.Texture
1422
+ */
1423
+ createTexture(url, noMipmap, invertY, scene, samplingMode = 3, onLoad = null, onError = null, buffer = null, fallback = null, format = null, forcedExtension = null, mimeType, loaderOptions, creationFlags, useSRGBBuffer = false) {
1424
+ url = url || "";
1425
+ const fromData = url.substring(0, 5) === "data:";
1426
+ //const fromBlob = url.substring(0, 5) === "blob:";
1427
+ const isBase64 = fromData && url.indexOf(";base64,") !== -1;
1428
+ const texture = fallback ? fallback : new InternalTexture(this, 1 /* InternalTextureSource.Url */);
1429
+ const originalUrl = url;
1430
+ if (this._transformTextureUrl && !isBase64 && !fallback && !buffer) {
1431
+ url = this._transformTextureUrl(url);
1432
+ }
1433
+ // establish the file extension, if possible
1434
+ const lastDot = url.lastIndexOf(".");
1435
+ const extension = forcedExtension ? forcedExtension : lastDot > -1 ? url.substring(lastDot).toLowerCase() : "";
1436
+ // some formats are already supported by bimg, no need to try to load them with JS
1437
+ // leaving TextureLoader extension check for future use
1438
+ let loaderPromise = null;
1439
+ if (extension.endsWith(".basis") || extension.endsWith(".ktx") || extension.endsWith(".ktx2") || mimeType === "image/ktx" || mimeType === "image/ktx2") {
1440
+ loaderPromise = AbstractEngine.GetCompatibleTextureLoader(extension);
1441
+ }
1442
+ if (scene) {
1443
+ scene.addPendingData(texture);
1444
+ }
1445
+ texture.url = url;
1446
+ texture.generateMipMaps = !noMipmap;
1447
+ texture.samplingMode = samplingMode;
1448
+ texture.invertY = invertY;
1449
+ texture._useSRGBBuffer = this._getUseSRGBBuffer(useSRGBBuffer, noMipmap);
1450
+ if (!this.doNotHandleContextLost) {
1451
+ // Keep a link to the buffer only if we plan to handle context lost
1452
+ texture._buffer = buffer;
1453
+ }
1454
+ let onLoadObserver = null;
1455
+ if (onLoad && !fallback) {
1456
+ onLoadObserver = texture.onLoadedObservable.add(onLoad);
1457
+ }
1458
+ if (!fallback) {
1459
+ this._internalTexturesCache.push(texture);
1460
+ }
1461
+ const onInternalError = (message, exception) => {
1462
+ if (scene) {
1463
+ scene.removePendingData(texture);
1464
+ }
1465
+ if (url === originalUrl) {
1466
+ if (onLoadObserver) {
1467
+ texture.onLoadedObservable.remove(onLoadObserver);
1468
+ }
1469
+ if (EngineStore.UseFallbackTexture) {
1470
+ this.createTexture(EngineStore.FallbackTexture, noMipmap, texture.invertY, scene, samplingMode, null, onError, buffer, texture);
1471
+ }
1472
+ if (onError) {
1473
+ onError((message || "Unknown error") + (EngineStore.UseFallbackTexture ? " - Fallback texture was used" : ""), exception);
1474
+ }
1475
+ }
1476
+ else {
1477
+ // fall back to the original url if the transformed url fails to load
1478
+ Logger.Warn(`Failed to load ${url}, falling back to ${originalUrl}`);
1479
+ this.createTexture(originalUrl, noMipmap, texture.invertY, scene, samplingMode, onLoad, onError, buffer, texture, format, forcedExtension, mimeType, loaderOptions);
1480
+ }
1481
+ };
1482
+ // processing for non-image formats
1483
+ if (loaderPromise) {
1484
+ throw new Error("Loading textures from IInternalTextureLoader not yet implemented.");
1485
+ }
1486
+ else {
1487
+ const onload = (data) => {
1488
+ if (!texture._hardwareTexture) {
1489
+ if (scene) {
1490
+ scene.removePendingData(texture);
1491
+ }
1492
+ return;
1493
+ }
1494
+ const underlyingResource = texture._hardwareTexture.underlyingResource;
1495
+ this._engine.loadTexture(underlyingResource, data, !noMipmap, invertY, texture._useSRGBBuffer, () => {
1496
+ texture.baseWidth = this._engine.getTextureWidth(underlyingResource);
1497
+ texture.baseHeight = this._engine.getTextureHeight(underlyingResource);
1498
+ texture.width = texture.baseWidth;
1499
+ texture.height = texture.baseHeight;
1500
+ texture.isReady = true;
1501
+ const filter = getNativeSamplingMode(samplingMode);
1502
+ this._setTextureSampling(underlyingResource, filter);
1503
+ if (scene) {
1504
+ scene.removePendingData(texture);
1505
+ }
1506
+ texture.onLoadedObservable.notifyObservers(texture);
1507
+ texture.onLoadedObservable.clear();
1508
+ }, () => {
1509
+ throw new Error("Could not load a native texture.");
1510
+ });
1511
+ };
1512
+ if (fromData && buffer) {
1513
+ if (buffer instanceof ArrayBuffer) {
1514
+ onload(new Uint8Array(buffer));
1515
+ }
1516
+ else if (ArrayBuffer.isView(buffer)) {
1517
+ onload(buffer);
1518
+ }
1519
+ else if (typeof buffer === "string") {
1520
+ onload(new Uint8Array(DecodeBase64UrlToBinary(buffer)));
1521
+ }
1522
+ else {
1523
+ throw new Error("Unsupported buffer type");
1524
+ }
1525
+ }
1526
+ else {
1527
+ if (isBase64) {
1528
+ onload(new Uint8Array(DecodeBase64UrlToBinary(url)));
1529
+ }
1530
+ else {
1531
+ this._loadFile(url, (data) => onload(new Uint8Array(data)), undefined, undefined, true, (request, exception) => {
1532
+ onInternalError("Unable to load " + (request ? request.responseURL : url, exception));
1533
+ });
1534
+ }
1535
+ }
1536
+ }
1537
+ return texture;
1538
+ }
1539
+ /**
1540
+ * Wraps an external native texture in a Babylon texture.
1541
+ * @param texture defines the external texture
1542
+ * @param hasMipMaps defines whether the external texture has mip maps
1543
+ * @param samplingMode defines the sampling mode for the external texture (default: 3)
1544
+ * @returns the babylon internal texture
1545
+ */
1546
+ wrapNativeTexture(texture, hasMipMaps = false, samplingMode = 3) {
1547
+ const hardwareTexture = new NativeHardwareTexture(texture, this._engine);
1548
+ const internalTexture = new InternalTexture(this, 0 /* InternalTextureSource.Unknown */, true);
1549
+ internalTexture._hardwareTexture = hardwareTexture;
1550
+ internalTexture.baseWidth = this._engine.getTextureWidth(texture);
1551
+ internalTexture.baseHeight = this._engine.getTextureHeight(texture);
1552
+ internalTexture.width = internalTexture.baseWidth;
1553
+ internalTexture.height = internalTexture.baseHeight;
1554
+ internalTexture.isReady = true;
1555
+ internalTexture.useMipMaps = hasMipMaps;
1556
+ this.updateTextureSamplingMode(samplingMode, internalTexture);
1557
+ return internalTexture;
1558
+ }
1559
+ _createDepthStencilTexture(size, options, rtWrapper) {
1560
+ // TODO: handle other options?
1561
+ const generateStencil = options.generateStencil || false;
1562
+ const samples = options.samples || 1;
1563
+ const nativeRTWrapper = rtWrapper;
1564
+ const texture = new InternalTexture(this, 12 /* InternalTextureSource.DepthStencil */);
1565
+ const width = size.width ?? size;
1566
+ const height = size.height ?? size;
1567
+ const framebuffer = this._engine.createFrameBuffer(texture._hardwareTexture.underlyingResource, width, height, generateStencil, true, samples);
1568
+ nativeRTWrapper._framebufferDepthStencil = framebuffer;
1569
+ return texture;
1570
+ }
1571
+ /**
1572
+ * @internal
1573
+ */
1574
+ _releaseFramebufferObjects(framebuffer) {
1575
+ if (framebuffer) {
1576
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_DELETEFRAMEBUFFER);
1577
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(framebuffer);
1578
+ this._commandBufferEncoder.finishEncodingCommand();
1579
+ }
1580
+ }
1581
+ /**
1582
+ * @internal Engine abstraction for loading and creating an image bitmap from a given source string.
1583
+ * @param imageSource source to load the image from.
1584
+ * @param _options An object that sets options for the image's extraction.
1585
+ * @returns ImageBitmap
1586
+ */
1587
+ async _createImageBitmapFromSource(imageSource, _options) {
1588
+ const promise = new Promise((resolve, reject) => {
1589
+ const image = this.createCanvasImage();
1590
+ image.onload = () => {
1591
+ try {
1592
+ const imageBitmap = this._engine.createImageBitmap(image);
1593
+ resolve(imageBitmap);
1594
+ }
1595
+ catch (error) {
1596
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
1597
+ reject(`Error loading image ${image.src} with exception: ${error}`);
1598
+ }
1599
+ };
1600
+ image.onerror = (error) => {
1601
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
1602
+ reject(`Error loading image ${image.src} with exception: ${error}`);
1603
+ };
1604
+ image.src = imageSource;
1605
+ });
1606
+ return await promise;
1607
+ }
1608
+ /**
1609
+ * Engine abstraction for createImageBitmap
1610
+ * @param image source for image
1611
+ * @param options An object that sets options for the image's extraction.
1612
+ * @returns ImageBitmap
1613
+ */
1614
+ async createImageBitmap(image, options) {
1615
+ // Back-compat: Because of the previous Blob hack, this could be an array of BlobParts.
1616
+ if (Array.isArray(image)) {
1617
+ const arr = image;
1618
+ if (arr.length) {
1619
+ return this._engine.createImageBitmap(arr[0]);
1620
+ }
1621
+ }
1622
+ if (image instanceof Blob) {
1623
+ const data = await image.arrayBuffer();
1624
+ return this._engine.createImageBitmap(data);
1625
+ }
1626
+ throw new Error("Unsupported data for createImageBitmap.");
1627
+ }
1628
+ /**
1629
+ * Resize an image and returns the image data as an uint8array
1630
+ * @param image image to resize
1631
+ * @param bufferWidth destination buffer width
1632
+ * @param bufferHeight destination buffer height
1633
+ * @returns an uint8array containing RGBA values of bufferWidth * bufferHeight size
1634
+ */
1635
+ resizeImageBitmap(image, bufferWidth, bufferHeight) {
1636
+ return this._engine.resizeImageBitmap(image, bufferWidth, bufferHeight);
1637
+ }
1638
+ /** @internal */
1639
+ _createHardwareTexture() {
1640
+ return new NativeHardwareTexture(this._createTexture(), this._engine);
1641
+ }
1642
+ /** @internal */
1643
+ _createHardwareRenderTargetWrapper(isMulti, isCube, size) {
1644
+ const rtWrapper = new NativeRenderTargetWrapper(isMulti, isCube, size, this);
1645
+ this._renderTargetWrapperCache.push(rtWrapper);
1646
+ return rtWrapper;
1647
+ }
1648
+ /** @internal */
1649
+ _createInternalTexture(size, options, _delayGPUTextureCreation = true, source = 0 /* InternalTextureSource.Unknown */) {
1650
+ let generateMipMaps = false;
1651
+ let type = 0;
1652
+ let samplingMode = 3;
1653
+ let format = 5;
1654
+ let useSRGBBuffer = false;
1655
+ let samples = 1;
1656
+ let label;
1657
+ if (options !== undefined && typeof options === "object") {
1658
+ generateMipMaps = !!options.generateMipMaps;
1659
+ type = options.type === undefined ? 0 : options.type;
1660
+ samplingMode = options.samplingMode === undefined ? 3 : options.samplingMode;
1661
+ format = options.format === undefined ? 5 : options.format;
1662
+ useSRGBBuffer = options.useSRGBBuffer === undefined ? false : options.useSRGBBuffer;
1663
+ samples = options.samples ?? 1;
1664
+ label = options.label;
1665
+ }
1666
+ else {
1667
+ generateMipMaps = !!options;
1668
+ }
1669
+ useSRGBBuffer = this._getUseSRGBBuffer(useSRGBBuffer, !generateMipMaps);
1670
+ if (type === 1 && !this._caps.textureFloatLinearFiltering) {
1671
+ // if floating point linear (gl.FLOAT) then force to NEAREST_SAMPLINGMODE
1672
+ samplingMode = 1;
1673
+ }
1674
+ else if (type === 2 && !this._caps.textureHalfFloatLinearFiltering) {
1675
+ // if floating point linear (HALF_FLOAT) then force to NEAREST_SAMPLINGMODE
1676
+ samplingMode = 1;
1677
+ }
1678
+ if (type === 1 && !this._caps.textureFloat) {
1679
+ type = 0;
1680
+ Logger.Warn("Float textures are not supported. Type forced to TEXTURETYPE_UNSIGNED_BYTE");
1681
+ }
1682
+ const texture = new InternalTexture(this, source);
1683
+ const width = size.width ?? size;
1684
+ const height = size.height ?? size;
1685
+ const layers = size.layers || 0;
1686
+ if (layers !== 0) {
1687
+ throw new Error("Texture layers are not supported in Babylon Native");
1688
+ }
1689
+ const nativeTexture = texture._hardwareTexture.underlyingResource;
1690
+ const nativeTextureFormat = getNativeTextureFormat(format, type);
1691
+ // REVIEW: We are always setting the renderTarget flag as we don't know whether the texture will be used as a render target.
1692
+ this._engine.initializeTexture(nativeTexture, width, height, generateMipMaps, nativeTextureFormat, true, useSRGBBuffer, samples);
1693
+ this._setTextureSampling(nativeTexture, getNativeSamplingMode(samplingMode));
1694
+ texture._useSRGBBuffer = useSRGBBuffer;
1695
+ texture.baseWidth = width;
1696
+ texture.baseHeight = height;
1697
+ texture.width = width;
1698
+ texture.height = height;
1699
+ texture.depth = layers;
1700
+ texture.isReady = true;
1701
+ texture.samples = samples;
1702
+ texture.generateMipMaps = generateMipMaps;
1703
+ texture.samplingMode = samplingMode;
1704
+ texture.type = type;
1705
+ texture.format = format;
1706
+ texture.label = label;
1707
+ this._internalTexturesCache.push(texture);
1708
+ return texture;
1709
+ }
1710
+ createRenderTargetTexture(size, options) {
1711
+ const rtWrapper = this._createHardwareRenderTargetWrapper(false, false, size);
1712
+ let generateDepthBuffer = true;
1713
+ let generateStencilBuffer = false;
1714
+ let noColorAttachment = false;
1715
+ let colorAttachment = undefined;
1716
+ let samples = 1;
1717
+ if (options !== undefined && typeof options === "object") {
1718
+ generateDepthBuffer = options.generateDepthBuffer ?? true;
1719
+ generateStencilBuffer = !!options.generateStencilBuffer;
1720
+ noColorAttachment = !!options.noColorAttachment;
1721
+ colorAttachment = options.colorAttachment;
1722
+ samples = options.samples ?? 1;
1723
+ }
1724
+ const texture = colorAttachment || (noColorAttachment ? null : this._createInternalTexture(size, options, true, 5 /* InternalTextureSource.RenderTarget */));
1725
+ const width = size.width ?? size;
1726
+ const height = size.height ?? size;
1727
+ const framebuffer = this._engine.createFrameBuffer(texture ? texture._hardwareTexture.underlyingResource : null, width, height, generateStencilBuffer, generateDepthBuffer, samples);
1728
+ rtWrapper._framebuffer = framebuffer;
1729
+ rtWrapper._generateDepthBuffer = generateDepthBuffer;
1730
+ rtWrapper._generateStencilBuffer = generateStencilBuffer;
1731
+ rtWrapper._samples = samples;
1732
+ rtWrapper.setTextures(texture);
1733
+ return rtWrapper;
1734
+ }
1735
+ updateRenderTargetTextureSampleCount(rtWrapper, samples) {
1736
+ Logger.Warn("Updating render target sample count is not currently supported");
1737
+ return rtWrapper.samples;
1738
+ }
1739
+ updateTextureSamplingMode(samplingMode, texture) {
1740
+ if (texture._hardwareTexture) {
1741
+ const filter = getNativeSamplingMode(samplingMode);
1742
+ this._setTextureSampling(texture._hardwareTexture.underlyingResource, filter);
1743
+ }
1744
+ texture.samplingMode = samplingMode;
1745
+ }
1746
+ bindFramebuffer(texture, faceIndex, requiredWidth, requiredHeight, forceFullscreenViewport) {
1747
+ const nativeRTWrapper = texture;
1748
+ if (this._currentRenderTarget) {
1749
+ this.unBindFramebuffer(this._currentRenderTarget);
1750
+ }
1751
+ this._currentRenderTarget = texture;
1752
+ if (faceIndex) {
1753
+ throw new Error("Cuboid frame buffers are not yet supported in NativeEngine.");
1754
+ }
1755
+ if (requiredWidth || requiredHeight) {
1756
+ throw new Error("Required width/height for frame buffers not yet supported in NativeEngine.");
1757
+ }
1758
+ if (nativeRTWrapper._framebufferDepthStencil) {
1759
+ this._bindUnboundFramebuffer(nativeRTWrapper._framebufferDepthStencil);
1760
+ }
1761
+ else {
1762
+ this._bindUnboundFramebuffer(nativeRTWrapper._framebuffer);
1763
+ }
1764
+ }
1765
+ unBindFramebuffer(texture, disableGenerateMipMaps = false, onBeforeUnbind) {
1766
+ // NOTE: Disabling mipmap generation is not yet supported in NativeEngine.
1767
+ this._currentRenderTarget = null;
1768
+ if (onBeforeUnbind) {
1769
+ onBeforeUnbind();
1770
+ }
1771
+ this._bindUnboundFramebuffer(null);
1772
+ }
1773
+ createDynamicVertexBuffer(data) {
1774
+ return this.createVertexBuffer(data, true);
1775
+ }
1776
+ updateDynamicIndexBuffer(indexBuffer, indices, offset = 0) {
1777
+ const buffer = indexBuffer;
1778
+ const data = this._normalizeIndexData(indices);
1779
+ buffer.is32Bits = data.BYTES_PER_ELEMENT === 4;
1780
+ this._engine.updateDynamicIndexBuffer(buffer.nativeIndexBuffer, data.buffer, data.byteOffset, data.byteLength, offset);
1781
+ }
1782
+ updateDynamicVertexBuffer(vertexBuffer, data, byteOffset = 0, byteLength) {
1783
+ const buffer = vertexBuffer;
1784
+ const dataView = data instanceof Array ? new Float32Array(data) : data instanceof ArrayBuffer ? new Uint8Array(data) : data;
1785
+ const byteView = new Uint8Array(dataView.buffer, dataView.byteOffset, byteLength ?? dataView.byteLength);
1786
+ this._engine.updateDynamicVertexBuffer(buffer.nativeVertexBuffer, byteView.buffer, byteView.byteOffset, byteView.byteLength, byteOffset);
1787
+ }
1788
+ // TODO: Refactor to share more logic with base Engine implementation.
1789
+ /**
1790
+ * @internal
1791
+ */
1792
+ _setTexture(channel, texture, isPartOfTextureArray = false, depthStencilTexture = false) {
1793
+ const uniform = this._boundUniforms[channel];
1794
+ if (!uniform) {
1795
+ return false;
1796
+ }
1797
+ // Not ready?
1798
+ if (!texture) {
1799
+ if (this._boundTexturesCache[channel] != null) {
1800
+ this._activeChannel = channel;
1801
+ this._boundTexturesCache[channel] = null;
1802
+ this._unsetNativeTexture(uniform);
1803
+ }
1804
+ return false;
1805
+ }
1806
+ // Video
1807
+ if (texture.video) {
1808
+ this._activeChannel = channel;
1809
+ texture.update();
1810
+ }
1811
+ else if (texture.delayLoadState === 4) {
1812
+ // Delay loading
1813
+ texture.delayLoad();
1814
+ return false;
1815
+ }
1816
+ let internalTexture;
1817
+ if (depthStencilTexture) {
1818
+ internalTexture = texture.depthStencilTexture;
1819
+ }
1820
+ else if (texture.isReady()) {
1821
+ internalTexture = texture.getInternalTexture();
1822
+ }
1823
+ else if (texture.isCube) {
1824
+ internalTexture = this.emptyCubeTexture;
1825
+ }
1826
+ else if (texture.is3D) {
1827
+ internalTexture = this.emptyTexture3D;
1828
+ }
1829
+ else if (texture.is2DArray) {
1830
+ internalTexture = this.emptyTexture2DArray;
1831
+ }
1832
+ else {
1833
+ internalTexture = this.emptyTexture;
1834
+ }
1835
+ this._activeChannel = channel;
1836
+ if (!internalTexture || !internalTexture._hardwareTexture) {
1837
+ return false;
1838
+ }
1839
+ this._setTextureWrapMode(internalTexture._hardwareTexture.underlyingResource, getNativeAddressMode(texture.wrapU), getNativeAddressMode(texture.wrapV), getNativeAddressMode(texture.wrapR));
1840
+ this._updateAnisotropicLevel(texture);
1841
+ this._setNativeTexture(uniform, internalTexture._hardwareTexture.underlyingResource);
1842
+ return true;
1843
+ }
1844
+ // filter is a NativeFilter.XXXX value.
1845
+ _setTextureSampling(texture, filter) {
1846
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETTEXTURESAMPLING);
1847
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(texture);
1848
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(filter);
1849
+ this._commandBufferEncoder.finishEncodingCommand();
1850
+ }
1851
+ // addressModes are NativeAddressMode.XXXX values.
1852
+ _setTextureWrapMode(texture, addressModeU, addressModeV, addressModeW) {
1853
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETTEXTUREWRAPMODE);
1854
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(texture);
1855
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(addressModeU);
1856
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(addressModeV);
1857
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(addressModeW);
1858
+ this._commandBufferEncoder.finishEncodingCommand();
1859
+ }
1860
+ _setNativeTexture(uniform, texture) {
1861
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETTEXTURE);
1862
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1863
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(texture);
1864
+ this._commandBufferEncoder.finishEncodingCommand();
1865
+ }
1866
+ _unsetNativeTexture(uniform) {
1867
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_UNSETTEXTURE);
1868
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(uniform);
1869
+ this._commandBufferEncoder.finishEncodingCommand();
1870
+ }
1871
+ // TODO: Share more of this logic with the base implementation.
1872
+ // TODO: Rename to match naming in base implementation once refactoring allows different parameters.
1873
+ _updateAnisotropicLevel(texture) {
1874
+ const internalTexture = texture.getInternalTexture();
1875
+ const value = texture.anisotropicFilteringLevel;
1876
+ if (!internalTexture || !internalTexture._hardwareTexture) {
1877
+ return;
1878
+ }
1879
+ if (internalTexture._cachedAnisotropicFilteringLevel !== value) {
1880
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETTEXTUREANISOTROPICLEVEL);
1881
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(internalTexture._hardwareTexture.underlyingResource);
1882
+ this._commandBufferEncoder.encodeCommandArgAsUInt32(value);
1883
+ this._commandBufferEncoder.finishEncodingCommand();
1884
+ internalTexture._cachedAnisotropicFilteringLevel = value;
1885
+ }
1886
+ }
1887
+ /**
1888
+ * @internal
1889
+ */
1890
+ _bindTexture(channel, texture) {
1891
+ const uniform = this._boundUniforms[channel];
1892
+ if (!uniform) {
1893
+ return;
1894
+ }
1895
+ if (texture && texture._hardwareTexture) {
1896
+ const underlyingResource = texture._hardwareTexture.underlyingResource;
1897
+ this._setNativeTexture(uniform, underlyingResource);
1898
+ }
1899
+ else {
1900
+ this._unsetNativeTexture(uniform);
1901
+ }
1902
+ }
1903
+ /**
1904
+ * Unbind all textures
1905
+ */
1906
+ unbindAllTextures() {
1907
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_DISCARDALLTEXTURES);
1908
+ this._commandBufferEncoder.finishEncodingCommand();
1909
+ }
1910
+ _deleteBuffer(buffer) {
1911
+ if (buffer.nativeIndexBuffer) {
1912
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_DELETEINDEXBUFFER);
1913
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(buffer.nativeIndexBuffer);
1914
+ this._commandBufferEncoder.finishEncodingCommand();
1915
+ delete buffer.nativeIndexBuffer;
1916
+ }
1917
+ if (buffer.nativeVertexBuffer) {
1918
+ this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_DELETEVERTEXBUFFER);
1919
+ this._commandBufferEncoder.encodeCommandArgAsNativeData(buffer.nativeVertexBuffer);
1920
+ this._commandBufferEncoder.finishEncodingCommand();
1921
+ delete buffer.nativeVertexBuffer;
1922
+ }
1923
+ }
1924
+ /**
1925
+ * Create a canvas
1926
+ * @param width width
1927
+ * @param height height
1928
+ * @returns ICanvas interface
1929
+ */
1930
+ createCanvas(width, height) {
1931
+ if (!_native.Canvas) {
1932
+ throw new Error("Native Canvas plugin not available.");
1933
+ }
1934
+ const canvas = new _native.Canvas();
1935
+ canvas.width = width;
1936
+ canvas.height = height;
1937
+ return canvas;
1938
+ }
1939
+ /**
1940
+ * Create an image to use with canvas
1941
+ * @returns IImage interface
1942
+ */
1943
+ createCanvasImage() {
1944
+ if (!_native.Image) {
1945
+ throw new Error("Native Canvas plugin not available.");
1946
+ }
1947
+ const image = new _native.Image();
1948
+ return image;
1949
+ }
1950
+ /**
1951
+ * Create a 2D path to use with canvas
1952
+ * @returns IPath2D interface
1953
+ * @param d SVG path string
1954
+ */
1955
+ createCanvasPath2D(d) {
1956
+ if (!_native.Path2D) {
1957
+ throw new Error("Native Canvas plugin not available.");
1958
+ }
1959
+ const path2d = new _native.Path2D(d);
1960
+ return path2d;
1961
+ }
1962
+ /**
1963
+ * Update a portion of an internal texture
1964
+ * @param texture defines the texture to update
1965
+ * @param imageData defines the data to store into the texture
1966
+ * @param xOffset defines the x coordinates of the update rectangle
1967
+ * @param yOffset defines the y coordinates of the update rectangle
1968
+ * @param width defines the width of the update rectangle
1969
+ * @param height defines the height of the update rectangle
1970
+ * @param faceIndex defines the face index if texture is a cube (0 by default)
1971
+ * @param lod defines the lod level to update (0 by default)
1972
+ * @param generateMipMaps defines whether to generate mipmaps or not
1973
+ */
1974
+ updateTextureData(texture, imageData, xOffset, yOffset, width, height, faceIndex = 0, lod = 0, generateMipMaps = false) {
1975
+ throw new Error("updateTextureData not implemented.");
1976
+ }
1977
+ /**
1978
+ * @internal
1979
+ */
1980
+ _uploadCompressedDataToTextureDirectly(texture, internalFormat, width, height, data, faceIndex = 0, lod = 0) {
1981
+ throw new Error("_uploadCompressedDataToTextureDirectly not implemented.");
1982
+ }
1983
+ /**
1984
+ * @internal
1985
+ */
1986
+ _uploadDataToTextureDirectly(texture, imageData, faceIndex = 0, lod = 0) {
1987
+ throw new Error("_uploadDataToTextureDirectly not implemented.");
1988
+ }
1989
+ /**
1990
+ * @internal
1991
+ */
1992
+ _uploadArrayBufferViewToTexture(texture, imageData, faceIndex = 0, lod = 0) {
1993
+ throw new Error("_uploadArrayBufferViewToTexture not implemented.");
1994
+ }
1995
+ getFontOffset(font) {
1996
+ // TODO
1997
+ const result = { ascent: 0, height: 0, descent: 0 };
1998
+ return result;
1999
+ }
2000
+ /**
2001
+ * No equivalent for native. Do nothing.
2002
+ */
2003
+ flushFramebuffer() { }
2004
+ // eslint-disable-next-line @typescript-eslint/promise-function-async
2005
+ _readTexturePixels(texture, width, height, faceIndex, level, buffer, _flushRenderer, _noDataConversion, x, y) {
2006
+ if (faceIndex !== undefined && faceIndex !== -1) {
2007
+ throw new Error(`Reading cubemap faces is not supported, but faceIndex is ${faceIndex}.`);
2008
+ }
2009
+ return (this._engine
2010
+ .readTexture(texture._hardwareTexture?.underlyingResource, level ?? 0, x ?? 0, y ?? 0, width, height, buffer?.buffer ?? null, buffer?.byteOffset ?? 0, buffer?.byteLength ?? 0)
2011
+ // eslint-disable-next-line github/no-then
2012
+ .then((rawBuffer) => {
2013
+ if (!buffer) {
2014
+ buffer = new Uint8Array(rawBuffer);
2015
+ }
2016
+ return buffer;
2017
+ }));
2018
+ }
2019
+ startTimeQuery() {
2020
+ if (!this._gpuFrameTimeToken) {
2021
+ this._gpuFrameTimeToken = new _TimeToken();
2022
+ }
2023
+ // Always return the same time token. For native, we don't need a start marker, we just query for native frame stats.
2024
+ return this._gpuFrameTimeToken;
2025
+ }
2026
+ endTimeQuery(token) {
2027
+ this._engine.populateFrameStats(this._frameStats);
2028
+ return this._frameStats.gpuTimeNs;
2029
+ }
2030
+ }
2031
+ // This must match the protocol version in NativeEngine.cpp
2032
+ ThinNativeEngine.PROTOCOL_VERSION = 9;
2033
+ //# sourceMappingURL=thinNativeEngine.js.map