StatsUi.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Core/Common.h>
  7. #include <AnKi/Ui/UiImmediateModeBuilder.h>
  8. #include <AnKi/Util/BuddyAllocatorBuilder.h>
  9. #include <AnKi/Gr/GrManager.h>
  10. namespace anki {
  11. /// @addtogroup core
  12. /// @{
  13. /// UI for displaying on-screen stats.
  14. class StatsUi : public UiImmediateModeBuilder
  15. {
  16. public:
  17. StatsUi(UiManager* ui)
  18. : UiImmediateModeBuilder(ui)
  19. {
  20. }
  21. ~StatsUi();
  22. Error init();
  23. void build(CanvasPtr ctx) override;
  24. void setFrameTime(Second v)
  25. {
  26. m_frameTime.set(v);
  27. }
  28. void setRenderTime(Second v)
  29. {
  30. m_renderTime.set(v);
  31. }
  32. void setSceneUpdateTime(Second v)
  33. {
  34. m_sceneUpdateTime.set(v);
  35. }
  36. void setVisibilityTestsTime(Second v)
  37. {
  38. m_visTestsTime.set(v);
  39. }
  40. void setPhysicsTime(Second v)
  41. {
  42. m_physicsTime.set(v);
  43. }
  44. void setGpuTime(Second v)
  45. {
  46. m_gpuTime.set(v);
  47. }
  48. void setGpuActiveCycles(U64 v)
  49. {
  50. m_gpuActive.set(v);
  51. }
  52. void setGpuReadBandwidth(PtrSize v)
  53. {
  54. m_gpuReadBandwidth.set(v);
  55. }
  56. void setGpuWriteBandwidth(PtrSize v)
  57. {
  58. m_gpuWriteBandwidth.set(v);
  59. }
  60. void setAllocatedCpuMemory(PtrSize v)
  61. {
  62. m_allocatedCpuMem = v;
  63. }
  64. void setCpuAllocationCount(U64 v)
  65. {
  66. m_allocCount = v;
  67. }
  68. void setCpuFreeCount(U64 v)
  69. {
  70. m_freeCount = v;
  71. }
  72. void setGrStats(const GrManagerStats& stats)
  73. {
  74. m_grStats = stats;
  75. }
  76. void setDrawableCount(U64 v)
  77. {
  78. m_drawableCount = v;
  79. }
  80. void setGlobalVertexMemoryPoolStats(const BuddyAllocatorBuilderStats& stats)
  81. {
  82. m_globalVertexPoolStats = stats;
  83. }
  84. private:
  85. static constexpr U32 BUFFERED_FRAMES = 16;
  86. template<typename T>
  87. class BufferedValue
  88. {
  89. public:
  90. void set(T x)
  91. {
  92. m_rollongAvg += x / T(BUFFERED_FRAMES);
  93. }
  94. T get(Bool flush)
  95. {
  96. if(flush)
  97. {
  98. m_avg = m_rollongAvg;
  99. m_rollongAvg = T(0);
  100. }
  101. return m_avg;
  102. }
  103. private:
  104. T m_rollongAvg = T(0);
  105. T m_avg = T(0);
  106. };
  107. FontPtr m_font;
  108. U32 m_bufferedFrames = 0;
  109. // CPU
  110. BufferedValue<Second> m_frameTime;
  111. BufferedValue<Second> m_renderTime;
  112. BufferedValue<Second> m_sceneUpdateTime;
  113. BufferedValue<Second> m_visTestsTime;
  114. BufferedValue<Second> m_physicsTime;
  115. // GPU
  116. BufferedValue<Second> m_gpuTime;
  117. BufferedValue<U64> m_gpuActive;
  118. BufferedValue<PtrSize> m_gpuReadBandwidth;
  119. BufferedValue<PtrSize> m_gpuWriteBandwidth;
  120. // Memory
  121. PtrSize m_allocatedCpuMem = 0;
  122. U64 m_allocCount = 0;
  123. U64 m_freeCount = 0;
  124. BuddyAllocatorBuilderStats m_globalVertexPoolStats = {};
  125. // GR
  126. GrManagerStats m_grStats = {};
  127. // Other
  128. PtrSize m_drawableCount = 0;
  129. static void labelTime(Second val, CString name)
  130. {
  131. ImGui::Text("%s: %fms", name.cstr(), val * 1000.0);
  132. }
  133. static void labelUint(U64 val, CString name)
  134. {
  135. ImGui::Text("%s: %lu", name.cstr(), val);
  136. }
  137. void labelBytes(PtrSize val, CString name) const;
  138. };
  139. /// @}
  140. } // end namespace anki