Skip to content

Commit b709222

Browse files
authored
Hook up Tools.PerformanceLogLevel / Tools.StartPerformanceCounter / Tools.EndPerformanceCounter to Babylon Native counterparts (#17450)
NativeTracing has been partially implemented in Babylon Native for a long time. These changes hook up the Babylon.js perf tracing related APIs in Tools to the Babylon Native tracing APIs. This extra functionality will only execute in the context of Babylon Native, and is resilient to older versions of Babylon Native. For these changes to fully work, they need BabylonJS/BabylonNative#1569. Couple additional changes related to our native interfaces: - I did a little bit of cleanup on the `INative` interface to make it more type safe - ~I added a declation for the `_native` global in `INativeInterfaces.ts`, which makes it visible globally everywhere so it doesn't have to be redeclared in every file it is used in. However, I declared it as potentially undefined (since it is), so I didn't touch a few files that already declared it as non nullable (like NativeEngine) as there would be a ton of changes and I didn't want this PR to get too overloaded.~ Never mind, not in the mood to battle the custom umd type declaration generation script today.
1 parent f56e8e7 commit b709222

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

packages/dev/core/src/Engines/Native/nativeInterfaces.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,35 @@ interface INativeDataStreamConstructor {
414414
readonly VALIDATION_BOOLEAN: number;
415415
}
416416

417+
// Note: These values need to match those in Babylon Native's NativeTracing plugin.
418+
export const enum NativeTraceLevel {
419+
Mark = 1,
420+
Log = 2,
421+
}
422+
417423
/** @internal */
418424
export interface INative {
425+
// NativeEngine plugin
419426
Engine: INativeEngineConstructor;
420-
Camera: INativeCameraConstructor;
421-
Canvas: INativeCanvasConstructor;
422-
Image: INativeImageConstructor;
423-
Path2D: INativePath2DConstructor;
424-
XMLHttpRequest: any; // TODO: how to do this?
425-
DeviceInputSystem: IDeviceInputSystemConstructor;
426427
NativeDataStream: INativeDataStreamConstructor;
428+
429+
// NativeCamera plugin
430+
Camera?: INativeCameraConstructor;
431+
432+
// NativeCanvas plugin
433+
Canvas?: INativeCanvasConstructor;
434+
Image?: INativeImageConstructor;
435+
Path2D?: INativePath2DConstructor;
436+
437+
// Native XMLHttpRequest polyfill
438+
XMLHttpRequest?: typeof XMLHttpRequest;
439+
440+
// NativeInput plugin
441+
DeviceInputSystem?: IDeviceInputSystemConstructor;
442+
443+
// NativeTracing plugin
444+
enablePerformanceLogging?(level?: NativeTraceLevel): void;
445+
disablePerformanceLogging?(): void;
446+
startPerformanceCounter?(counter: string): unknown;
447+
endPerformanceCounter?(counter: unknown): void;
427448
}

packages/dev/core/src/Engines/nativeEngine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,7 +2615,7 @@ export class NativeEngine extends Engine {
26152615
* @returns IImage interface
26162616
*/
26172617
public override createCanvasImage(): IImage {
2618-
if (!_native.Canvas) {
2618+
if (!_native.Image) {
26192619
throw new Error("Native Canvas plugin not available.");
26202620
}
26212621
const image = new _native.Image();
@@ -2628,7 +2628,7 @@ export class NativeEngine extends Engine {
26282628
* @param d SVG path string
26292629
*/
26302630
public override createCanvasPath2D(d?: string): IPath2D {
2631-
if (!_native.Canvas) {
2631+
if (!_native.Path2D) {
26322632
throw new Error("Native Canvas plugin not available.");
26332633
}
26342634
const path2d = new _native.Path2D(d);

packages/dev/core/src/Misc/fileTools.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ import { _FunctionContainer } from "../Engines/Processors/shaderProcessor";
1414
import { EngineStore } from "../Engines/engineStore";
1515
import { Logger } from "./logger";
1616
import { TimingTools } from "./timingTools";
17-
import type { INative } from "../Engines/Native/nativeInterfaces";
1817
import { EngineFunctionContext } from "core/Engines/abstractEngine.functions";
1918
import { AbstractEngine } from "../Engines/abstractEngine";
2019

2120
const Base64DataUrlRegEx = new RegExp(/^data:([^,]+\/[^,]+)?;base64,/i);
22-
declare const _native: INative;
2321

2422
/** @ignore */
2523
export class LoadFileError extends RuntimeError {

packages/dev/core/src/Misc/tools.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ import type { IColor4Like } from "../Maths/math.like";
2929
import { IsExponentOfTwo, Mix } from "./tools.functions";
3030
import type { AbstractEngine } from "../Engines/abstractEngine";
3131
import type { RenderTargetTexture } from "core/Materials/Textures/renderTargetTexture";
32+
import type { INative } from "../Engines/Native/nativeInterfaces";
33+
import { NativeTraceLevel } from "../Engines/Native/nativeInterfaces";
34+
35+
// eslint-disable-next-line @typescript-eslint/naming-convention
36+
declare const _native: INative;
3237

3338
// eslint-disable-next-line @typescript-eslint/naming-convention
3439
declare function importScripts(...urls: string[]): void;
@@ -1328,24 +1333,39 @@ export class Tools {
13281333

13291334
private static _Performance: Performance;
13301335

1336+
private static readonly _NativePerformanceCounterHandles = new Map<string, unknown>();
1337+
13311338
/**
13321339
* Sets the current performance log level
13331340
*/
13341341
public static set PerformanceLogLevel(level: number) {
13351342
if ((level & Tools.PerformanceUserMarkLogLevel) === Tools.PerformanceUserMarkLogLevel) {
1336-
Tools.StartPerformanceCounter = Tools._StartUserMark;
1337-
Tools.EndPerformanceCounter = Tools._EndUserMark;
1343+
if (_native?.enablePerformanceLogging) {
1344+
_native.enablePerformanceLogging(NativeTraceLevel.Mark);
1345+
Tools.StartPerformanceCounter = Tools._StartMarkNative;
1346+
Tools.EndPerformanceCounter = Tools._EndMarkNative;
1347+
} else {
1348+
Tools.StartPerformanceCounter = Tools._StartUserMark;
1349+
Tools.EndPerformanceCounter = Tools._EndUserMark;
1350+
}
13381351
return;
13391352
}
13401353

13411354
if ((level & Tools.PerformanceConsoleLogLevel) === Tools.PerformanceConsoleLogLevel) {
1342-
Tools.StartPerformanceCounter = Tools._StartPerformanceConsole;
1343-
Tools.EndPerformanceCounter = Tools._EndPerformanceConsole;
1355+
if (_native?.enablePerformanceLogging) {
1356+
_native.enablePerformanceLogging(NativeTraceLevel.Log);
1357+
Tools.StartPerformanceCounter = Tools._StartMarkNative;
1358+
Tools.EndPerformanceCounter = Tools._EndMarkNative;
1359+
} else {
1360+
Tools.StartPerformanceCounter = Tools._StartPerformanceConsole;
1361+
Tools.EndPerformanceCounter = Tools._EndPerformanceConsole;
1362+
}
13441363
return;
13451364
}
13461365

13471366
Tools.StartPerformanceCounter = Tools._StartPerformanceCounterDisabled;
13481367
Tools.EndPerformanceCounter = Tools._EndPerformanceCounterDisabled;
1368+
_native?.disablePerformanceLogging?.();
13491369
}
13501370

13511371
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -1398,6 +1418,29 @@ export class Tools {
13981418
console.timeEnd(counterName);
13991419
}
14001420

1421+
private static _StartMarkNative(counterName: string, condition = true): void {
1422+
if (condition && _native?.startPerformanceCounter) {
1423+
if (Tools._NativePerformanceCounterHandles.has(counterName)) {
1424+
Tools.Warn(`Performance counter with name ${counterName} is already started.`);
1425+
} else {
1426+
const handle = _native.startPerformanceCounter(counterName);
1427+
Tools._NativePerformanceCounterHandles.set(counterName, handle);
1428+
}
1429+
}
1430+
}
1431+
1432+
private static _EndMarkNative(counterName: string, condition = true): void {
1433+
if (condition && _native?.endPerformanceCounter) {
1434+
const handle = Tools._NativePerformanceCounterHandles.get(counterName);
1435+
if (handle) {
1436+
_native.endPerformanceCounter(handle);
1437+
Tools._NativePerformanceCounterHandles.delete(counterName);
1438+
} else {
1439+
Tools.Warn(`Performance counter with name ${counterName} was not started.`);
1440+
}
1441+
}
1442+
}
1443+
14011444
/**
14021445
* Starts a performance counter
14031446
*/

0 commit comments

Comments
 (0)