Browse Source

Move camera render time perf counter to scene instrumentation

Gary Hsu 7 years ago
parent
commit
dd2b91c6cb
2 changed files with 54 additions and 4 deletions
  1. 54 0
      src/Instrumentation/babylon.sceneInstrumentation.ts
  2. 0 4
      src/babylon.scene.ts

+ 54 - 0
src/Instrumentation/babylon.sceneInstrumentation.ts

@@ -30,6 +30,9 @@ module BABYLON {
         private _captureAnimationsTime = false;
         private _captureAnimationsTime = false;
         private _animationsTime = new PerfCounter();
         private _animationsTime = new PerfCounter();
 
 
+        private _captureCameraRenderTime = false;
+        private _cameraRenderTime = new PerfCounter();
+
         // Observers
         // Observers
         private _onBeforeActiveMeshesEvaluationObserver: Nullable<Observer<Scene>> = null;
         private _onBeforeActiveMeshesEvaluationObserver: Nullable<Observer<Scene>> = null;
         private _onAfterActiveMeshesEvaluationObserver: Nullable<Observer<Scene>> = null;
         private _onAfterActiveMeshesEvaluationObserver: Nullable<Observer<Scene>> = null;
@@ -54,6 +57,9 @@ module BABYLON {
 
 
         private _onAfterAnimationsObserver: Nullable<Observer<Scene>> = null;
         private _onAfterAnimationsObserver: Nullable<Observer<Scene>> = null;
 
 
+        private _onBeforeCameraRenderObserver: Nullable<Observer<Camera>> = null;
+        private _onAfterCameraRenderObserver: Nullable<Observer<Camera>> = null;
+
         // Properties
         // Properties
         /**
         /**
          * Gets the perf counter used for active meshes evaluation time
          * Gets the perf counter used for active meshes evaluation time
@@ -390,6 +396,48 @@ module BABYLON {
         }
         }
 
 
         /**
         /**
+         * Gets the perf counter used for camera render time capture
+         */
+        public get cameraRenderTimeCounter(): PerfCounter {
+            return this._cameraRenderTime;
+        }
+
+        /**
+         * Gets the camera render time capture status
+         */
+        public get captureCameraRenderTime(): boolean {
+            return this._captureCameraRenderTime;
+        }
+
+        /**
+         * Enable or disable the camera render time capture
+         */
+        public set captureCameraRenderTime(value: boolean) {
+            if (value === this._captureCameraRenderTime) {
+                return;
+            }
+
+            this._captureCameraRenderTime = value;
+
+            if (value) {
+                this._onBeforeCameraRenderObserver = this.scene.onBeforeCameraRenderObservable.add(camera => {
+                    this._cameraRenderTime.beginMonitoring();
+                    Tools.StartPerformanceCounter(`Rendering camera ${camera.name}`);
+                });
+
+                this._onAfterCameraRenderObserver = this.scene.onAfterCameraRenderObservable.add(camera => {
+                    this._cameraRenderTime.endMonitoring(false);
+                    Tools.EndPerformanceCounter(`Rendering camera ${camera.name}`);
+                });
+            } else {
+                this.scene.onBeforeCameraRenderObservable.remove(this._onBeforeCameraRenderObserver);
+                this._onBeforeCameraRenderObserver = null;
+                this.scene.onAfterCameraRenderObservable.remove(this._onAfterCameraRenderObserver);
+                this._onAfterCameraRenderObserver = null;
+            }
+        }
+
+        /**
          * Gets the perf counter used for draw calls
          * Gets the perf counter used for draw calls
          */
          */
         public get drawCallsCounter(): PerfCounter {
         public get drawCallsCounter(): PerfCounter {
@@ -502,6 +550,12 @@ module BABYLON {
             this.scene.onAfterAnimationsObservable.remove(this._onAfterAnimationsObserver);
             this.scene.onAfterAnimationsObservable.remove(this._onAfterAnimationsObserver);
             this._onAfterAnimationsObserver = null;
             this._onAfterAnimationsObserver = null;
 
 
+            this.scene.onBeforeCameraRenderObservable.remove(this._onBeforeCameraRenderObserver);
+            this._onBeforeCameraRenderObserver = null;
+
+            this.scene.onAfterCameraRenderObservable.remove(this._onAfterCameraRenderObserver);
+            this._onAfterCameraRenderObserver = null;
+
             (<any>this.scene) = null;
             (<any>this.scene) = null;
         }
         }
     }
     }

+ 0 - 4
src/babylon.scene.ts

@@ -4477,8 +4477,6 @@
             if (!this.activeCamera)
             if (!this.activeCamera)
                 throw new Error("Active camera not set");
                 throw new Error("Active camera not set");
 
 
-            Tools.StartPerformanceCounter("Rendering camera " + this.activeCamera.name);
-
             // Viewport
             // Viewport
             engine.setViewport(this.activeCamera.viewport);
             engine.setViewport(this.activeCamera.viewport);
 
 
@@ -4657,8 +4655,6 @@
             this._alternateRendering = false;
             this._alternateRendering = false;
 
 
             this.onAfterCameraRenderObservable.notifyObservers(this.activeCamera);
             this.onAfterCameraRenderObservable.notifyObservers(this.activeCamera);
-
-            Tools.EndPerformanceCounter("Rendering camera " + this.activeCamera.name);
         }
         }
 
 
         private _processSubCameras(camera: Camera): void {
         private _processSubCameras(camera: Camera): void {