babylon.engineInstrumentation.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. module BABYLON {
  2. /**
  3. * This class can be used to get instrumentation data from a Babylon engine
  4. */
  5. export class EngineInstrumentation implements IDisposable {
  6. private _captureGPUFrameTime = false;
  7. private _gpuFrameTimeToken: Nullable<_TimeToken>;
  8. private _gpuFrameTime = new PerfCounter();
  9. private _captureShaderCompilationTime = false;
  10. private _shaderCompilationTime = new PerfCounter();
  11. // Observers
  12. private _onBeginFrameObserver: Nullable<Observer<Engine>> = null;
  13. private _onEndFrameObserver: Nullable<Observer<Engine>> = null;
  14. private _onBeforeShaderCompilationObserver: Nullable<Observer<Engine>> = null;
  15. private _onAfterShaderCompilationObserver: Nullable<Observer<Engine>> = null;
  16. // Properties
  17. /**
  18. * Gets the perf counter used for GPU frame time
  19. */
  20. public get gpuFrameTimeCounter(): PerfCounter {
  21. return this._gpuFrameTime;
  22. }
  23. /**
  24. * Gets the GPU frame time capture status
  25. */
  26. public get captureGPUFrameTime(): boolean {
  27. return this._captureGPUFrameTime;
  28. }
  29. /**
  30. * Enable or disable the GPU frame time capture
  31. */
  32. public set captureGPUFrameTime(value: boolean) {
  33. if (value === this._captureGPUFrameTime) {
  34. return;
  35. }
  36. this._captureGPUFrameTime = value;
  37. if (value) {
  38. this._onBeginFrameObserver = this.engine.onBeginFrameObservable.add(()=>{
  39. if (!this._gpuFrameTimeToken) {
  40. this._gpuFrameTimeToken = this.engine.startTimeQuery();
  41. }
  42. });
  43. this._onEndFrameObserver = this.engine.onEndFrameObservable.add(()=>{
  44. if (!this._gpuFrameTimeToken) {
  45. return;
  46. }
  47. let time = this.engine.endTimeQuery(this._gpuFrameTimeToken);
  48. if (time > -1) {
  49. this._gpuFrameTimeToken = null;
  50. this._gpuFrameTime.fetchNewFrame();
  51. this._gpuFrameTime.addCount(time, true);
  52. }
  53. });
  54. } else {
  55. this.engine.onBeginFrameObservable.remove(this._onBeginFrameObserver);
  56. this._onBeginFrameObserver = null;
  57. this.engine.onEndFrameObservable.remove(this._onEndFrameObserver);
  58. this._onEndFrameObserver = null;
  59. }
  60. }
  61. /**
  62. * Gets the perf counter used for shader compilation time
  63. */
  64. public get shaderCompilationTimeCounter(): PerfCounter {
  65. return this._shaderCompilationTime;
  66. }
  67. /**
  68. * Gets the shader compilation time capture status
  69. */
  70. public get captureShaderCompilationTime(): boolean {
  71. return this._captureShaderCompilationTime;
  72. }
  73. /**
  74. * Enable or disable the shader compilation time capture
  75. */
  76. public set captureShaderCompilationTime(value: boolean) {
  77. if (value === this._captureShaderCompilationTime) {
  78. return;
  79. }
  80. this._captureShaderCompilationTime = value;
  81. if (value) {
  82. this._onBeforeShaderCompilationObserver = this.engine.onBeforeShaderCompilationObservable.add(()=>{
  83. this._shaderCompilationTime.fetchNewFrame();
  84. this._shaderCompilationTime.beginMonitoring();
  85. });
  86. this._onAfterShaderCompilationObserver = this.engine.onAfterShaderCompilationObservable.add(()=>{
  87. this._shaderCompilationTime.endMonitoring();
  88. });
  89. } else {
  90. this.engine.onBeforeShaderCompilationObservable.remove(this._onBeforeShaderCompilationObserver);
  91. this._onBeforeShaderCompilationObserver = null;
  92. this.engine.onAfterShaderCompilationObservable.remove(this._onAfterShaderCompilationObserver);
  93. this._onAfterShaderCompilationObserver = null;
  94. }
  95. }
  96. public constructor(public engine: Engine) {
  97. }
  98. public dispose() {
  99. this.engine.onBeginFrameObservable.remove(this._onBeginFrameObserver);
  100. this._onBeginFrameObserver = null;
  101. this.engine.onEndFrameObservable.remove(this._onEndFrameObserver);
  102. this._onEndFrameObserver = null;
  103. this.engine.onBeforeShaderCompilationObservable.remove(this._onBeforeShaderCompilationObserver);
  104. this._onBeforeShaderCompilationObserver = null;
  105. this.engine.onAfterShaderCompilationObservable.remove(this._onAfterShaderCompilationObserver);
  106. this._onAfterShaderCompilationObserver = null;
  107. (<any>this.engine) = null;
  108. }
  109. }
  110. }