StatsUi.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Core/StatsUi.h>
  6. #include <AnKi/Ui/UiManager.h>
  7. #include <AnKi/Ui/Font.h>
  8. #include <AnKi/Ui/Canvas.h>
  9. namespace anki {
  10. StatsUi::~StatsUi()
  11. {
  12. }
  13. Error StatsUi::init()
  14. {
  15. ANKI_CHECK(UiManager::getSingleton().newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf", Array<U32, 1>{24}));
  16. return Error::kNone;
  17. }
  18. void StatsUi::labelBytes(PtrSize val, CString name) const
  19. {
  20. PtrSize gb, mb, kb, b;
  21. gb = val / 1_GB;
  22. val -= gb * 1_GB;
  23. mb = val / 1_MB;
  24. val -= mb * 1_MB;
  25. kb = val / 1_KB;
  26. val -= kb * 1_KB;
  27. b = val;
  28. UiString timestamp;
  29. if(gb)
  30. {
  31. timestamp.sprintf("%s: %zu,%04zu,%04zu,%04zu", name.cstr(), gb, mb, kb, b);
  32. }
  33. else if(mb)
  34. {
  35. timestamp.sprintf("%s: %zu,%04zu,%04zu", name.cstr(), mb, kb, b);
  36. }
  37. else if(kb)
  38. {
  39. timestamp.sprintf("%s: %zu,%04zu", name.cstr(), kb, b);
  40. }
  41. else
  42. {
  43. timestamp.sprintf("%s: %zu", name.cstr(), b);
  44. }
  45. ImGui::TextUnformatted(timestamp.cstr());
  46. }
  47. void StatsUi::setStats(const StatsUiInput& input, StatsUiDetail detail)
  48. {
  49. Bool flush = false;
  50. if(m_bufferedFrames == kBufferedFrames)
  51. {
  52. flush = true;
  53. m_bufferedFrames = 0;
  54. }
  55. ++m_bufferedFrames;
  56. #define ANKI_STATS_UI_BEGIN_GROUP(x)
  57. #define ANKI_STATS_UI_VALUE(type, name, text, flags) m_##name.update(input.m_##name, flags, flush);
  58. #include <AnKi/Core/StatsUi.defs.h>
  59. #undef ANKI_STATS_UI_BEGIN_GROUP
  60. #undef ANKI_STATS_UI_VALUE
  61. m_detail = detail;
  62. }
  63. void StatsUi::build(CanvasPtr canvas)
  64. {
  65. canvas->pushFont(m_font, 24);
  66. const Vec4 oldWindowColor = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
  67. ImGui::GetStyle().Colors[ImGuiCol_WindowBg].w = 0.3f;
  68. if(ImGui::Begin("Stats", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize))
  69. {
  70. ImGui::SetWindowPos(Vec2(5.0f, 5.0f));
  71. ImGui::SetWindowSize(Vec2(230.0f, 450.0f));
  72. auto writeText = [this](const Value& v, const Char* text, ValueFlag flags, Bool isFloat) {
  73. if(isFloat)
  74. {
  75. if(!!(flags & ValueFlag::kSeconds))
  76. {
  77. labelTime(v.m_float, text);
  78. }
  79. else
  80. {
  81. ImGui::Text("%s: %f", text, v.m_float);
  82. }
  83. }
  84. else
  85. {
  86. U64 val;
  87. if(!!(flags & ValueFlag::kAverage))
  88. {
  89. val = U64(v.m_avg);
  90. }
  91. else
  92. {
  93. val = v.m_int;
  94. }
  95. if(!!(flags & ValueFlag::kBytes))
  96. {
  97. labelBytes(val, text);
  98. }
  99. else
  100. {
  101. ImGui::Text("%s: %zu", text, val);
  102. }
  103. }
  104. };
  105. if(m_detail == StatsUiDetail::kDetailed)
  106. {
  107. #define ANKI_STATS_UI_BEGIN_GROUP(x) \
  108. ImGui::Text("----"); \
  109. ImGui::Text(x); \
  110. ImGui::Text("----");
  111. #define ANKI_STATS_UI_VALUE(type, name, text, flags) writeText(m_##name, text, flags, std::is_floating_point<type>::value);
  112. #include <AnKi/Core/StatsUi.defs.h>
  113. #undef ANKI_STATS_UI_BEGIN_GROUP
  114. #undef ANKI_STATS_UI_VALUE
  115. }
  116. else
  117. {
  118. const Second maxTime = max(m_cpuFrameTime.m_float, m_gpuFrameTime.m_float);
  119. const F32 fps = F32(1.0 / maxTime);
  120. const Bool cpuBound = m_cpuFrameTime.m_float > m_gpuFrameTime.m_float;
  121. ImGui::TextColored((cpuBound) ? Vec4(1.0f, 0.5f, 0.5f, 1.0f) : Vec4(0.5f, 1.0f, 0.5f, 1.0f), "FPS %.1f", fps);
  122. }
  123. }
  124. ImGui::End();
  125. ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = oldWindowColor;
  126. canvas->popFont();
  127. }
  128. } // end namespace anki