DebugHud.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/CoreEvents.h"
  5. #include "../Core/Profiler.h"
  6. #include "../Core/EventProfiler.h"
  7. #include "../Core/Context.h"
  8. #include "../Engine/DebugHud.h"
  9. #include "../Engine/Engine.h"
  10. #include "../Graphics/Graphics.h"
  11. #include "../Graphics/Renderer.h"
  12. #include "../Resource/ResourceCache.h"
  13. #include "../IO/Log.h"
  14. #include "../UI/Font.h"
  15. #include "../UI/Text.h"
  16. #include "../UI/UI.h"
  17. #include "../DebugNew.h"
  18. namespace Urho3D
  19. {
  20. static const char* qualityTexts[] =
  21. {
  22. "Low",
  23. "Med",
  24. "High",
  25. "High+"
  26. };
  27. static const char* shadowQualityTexts[] =
  28. {
  29. "16bit Simple",
  30. "24bit Simple",
  31. "16bit PCF",
  32. "24bit PCF",
  33. "VSM",
  34. "Blurred VSM"
  35. };
  36. DebugHud::DebugHud(Context* context) :
  37. Object(context),
  38. profilerMaxDepth_(M_MAX_UNSIGNED),
  39. profilerInterval_(1000),
  40. useRendererStats_(false),
  41. mode_(DebugHudElements::None)
  42. {
  43. auto* ui = GetSubsystem<UI>();
  44. UIElement* uiRoot = ui->GetRoot();
  45. statsText_ = new Text(context_);
  46. statsText_->SetAlignment(HA_LEFT, VA_TOP);
  47. statsText_->SetPriority(100);
  48. statsText_->SetVisible(false);
  49. uiRoot->AddChild(statsText_);
  50. modeText_ = new Text(context_);
  51. modeText_->SetAlignment(HA_LEFT, VA_BOTTOM);
  52. modeText_->SetPriority(100);
  53. modeText_->SetVisible(false);
  54. uiRoot->AddChild(modeText_);
  55. profilerText_ = new Text(context_);
  56. profilerText_->SetAlignment(HA_RIGHT, VA_TOP);
  57. profilerText_->SetPriority(100);
  58. profilerText_->SetVisible(false);
  59. uiRoot->AddChild(profilerText_);
  60. memoryText_ = new Text(context_);
  61. memoryText_->SetAlignment(HA_LEFT, VA_BOTTOM);
  62. memoryText_->SetPriority(100);
  63. memoryText_->SetVisible(false);
  64. uiRoot->AddChild(memoryText_);
  65. eventProfilerText_ = new Text(context_);
  66. eventProfilerText_->SetAlignment(HA_RIGHT, VA_TOP);
  67. eventProfilerText_->SetPriority(100);
  68. eventProfilerText_->SetVisible(false);
  69. uiRoot->AddChild(eventProfilerText_);
  70. SubscribeToEvent(E_POSTUPDATE, URHO3D_HANDLER(DebugHud, HandlePostUpdate));
  71. }
  72. DebugHud::~DebugHud()
  73. {
  74. statsText_->Remove();
  75. modeText_->Remove();
  76. profilerText_->Remove();
  77. memoryText_->Remove();
  78. eventProfilerText_->Remove();
  79. }
  80. void DebugHud::Update()
  81. {
  82. auto* graphics = GetSubsystem<Graphics>();
  83. auto* renderer = GetSubsystem<Renderer>();
  84. if (!renderer || !graphics)
  85. return;
  86. // Ensure UI-elements are not detached
  87. if (!statsText_->GetParent())
  88. {
  89. auto* ui = GetSubsystem<UI>();
  90. UIElement* uiRoot = ui->GetRoot();
  91. uiRoot->AddChild(statsText_);
  92. uiRoot->AddChild(modeText_);
  93. uiRoot->AddChild(profilerText_);
  94. }
  95. if (statsText_->IsVisible())
  96. {
  97. unsigned primitives, batches;
  98. if (!useRendererStats_)
  99. {
  100. primitives = graphics->GetNumPrimitives();
  101. batches = graphics->GetNumBatches();
  102. }
  103. else
  104. {
  105. primitives = renderer->GetNumPrimitives();
  106. batches = renderer->GetNumBatches();
  107. }
  108. String stats;
  109. stats.AppendWithFormat("Triangles %u\nBatches %u\nViews %u\nLights %u\nShadowmaps %u\nOccluders %u",
  110. primitives,
  111. batches,
  112. renderer->GetNumViews(),
  113. renderer->GetNumLights(true),
  114. renderer->GetNumShadowMaps(true),
  115. renderer->GetNumOccluders(true));
  116. if (!appStats_.Empty())
  117. {
  118. stats.Append("\n");
  119. for (HashMap<String, String>::ConstIterator i = appStats_.Begin(); i != appStats_.End(); ++i)
  120. stats.AppendWithFormat("\n%s %s", i->first_.CString(), i->second_.CString());
  121. }
  122. statsText_->SetText(stats);
  123. }
  124. if (modeText_->IsVisible())
  125. {
  126. String mode;
  127. mode.AppendWithFormat("Tex:%s Mat:%s Spec:%s Shadows:%s Size:%i Quality:%s Occlusion:%s Instancing:%s API:%s",
  128. qualityTexts[renderer->GetTextureQuality()],
  129. qualityTexts[Min((i32)renderer->GetMaterialQuality(), 3)],
  130. renderer->GetSpecularLighting() ? "On" : "Off",
  131. renderer->GetDrawShadows() ? "On" : "Off",
  132. renderer->GetShadowMapSize(),
  133. shadowQualityTexts[renderer->GetShadowQuality()],
  134. renderer->GetMaxOccluderTriangles() > 0 ? "On" : "Off",
  135. renderer->GetDynamicInstancing() ? "On" : "Off",
  136. graphics->GetApiName().CString());
  137. #ifdef URHO3D_OPENGL
  138. mode.AppendWithFormat(" Renderer:%s Version:%s", graphics->GetRendererName().CString(),
  139. graphics->GetVersionString().CString());
  140. #endif
  141. modeText_->SetText(mode);
  142. }
  143. auto* profiler = GetSubsystem<Profiler>();
  144. auto* eventProfiler = GetSubsystem<EventProfiler>();
  145. if (profiler)
  146. {
  147. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  148. {
  149. profilerTimer_.Reset();
  150. if (profilerText_->IsVisible())
  151. profilerText_->SetText(profiler->PrintData(false, false, profilerMaxDepth_));
  152. profiler->BeginInterval();
  153. if (eventProfiler)
  154. {
  155. if (eventProfilerText_->IsVisible())
  156. eventProfilerText_->SetText(eventProfiler->PrintData(false, false, profilerMaxDepth_));
  157. eventProfiler->BeginInterval();
  158. }
  159. }
  160. }
  161. if (memoryText_->IsVisible())
  162. memoryText_->SetText(GetSubsystem<ResourceCache>()->PrintMemoryUsage());
  163. }
  164. void DebugHud::SetDefaultStyle(XMLFile* style)
  165. {
  166. if (!style)
  167. return;
  168. statsText_->SetDefaultStyle(style);
  169. statsText_->SetStyle("DebugHudText");
  170. modeText_->SetDefaultStyle(style);
  171. modeText_->SetStyle("DebugHudText");
  172. profilerText_->SetDefaultStyle(style);
  173. profilerText_->SetStyle("DebugHudText");
  174. memoryText_->SetDefaultStyle(style);
  175. memoryText_->SetStyle("DebugHudText");
  176. eventProfilerText_->SetDefaultStyle(style);
  177. eventProfilerText_->SetStyle("DebugHudText");
  178. }
  179. void DebugHud::SetMode(DebugHudElements mode)
  180. {
  181. statsText_->SetVisible(!!(mode & DebugHudElements::Stats));
  182. modeText_->SetVisible(!!(mode & DebugHudElements::Mode));
  183. profilerText_->SetVisible(!!(mode & DebugHudElements::Profiler));
  184. memoryText_->SetVisible(!!(mode & DebugHudElements::Memory));
  185. eventProfilerText_->SetVisible(!!(mode & DebugHudElements::EventProfiler));
  186. memoryText_->SetPosition(0, modeText_->IsVisible() ? modeText_->GetHeight() * -2 : 0);
  187. #ifdef URHO3D_PROFILING
  188. // Event profiler is created on engine initialization if "EventProfiler" parameter is set
  189. auto* eventProfiler = GetSubsystem<EventProfiler>();
  190. if (eventProfiler)
  191. EventProfiler::SetActive(!!(mode & DebugHudElements::EventProfiler));
  192. #endif
  193. mode_ = mode;
  194. }
  195. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  196. {
  197. profilerMaxDepth_ = depth;
  198. }
  199. void DebugHud::SetProfilerInterval(float interval)
  200. {
  201. profilerInterval_ = Max((unsigned)(interval * 1000.0f), 0U);
  202. }
  203. void DebugHud::SetUseRendererStats(bool enable)
  204. {
  205. useRendererStats_ = enable;
  206. }
  207. void DebugHud::Toggle(DebugHudElements mode)
  208. {
  209. SetMode(GetMode() ^ mode);
  210. }
  211. void DebugHud::ToggleAll()
  212. {
  213. Toggle(DebugHudElements::All);
  214. }
  215. XMLFile* DebugHud::GetDefaultStyle() const
  216. {
  217. return statsText_->GetDefaultStyle(false);
  218. }
  219. float DebugHud::GetProfilerInterval() const
  220. {
  221. return (float)profilerInterval_ / 1000.0f;
  222. }
  223. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  224. {
  225. SetAppStats(label, stats.ToString());
  226. }
  227. void DebugHud::SetAppStats(const String& label, const String& stats)
  228. {
  229. bool newLabel = !appStats_.Contains(label);
  230. appStats_[label] = stats;
  231. if (newLabel)
  232. appStats_.Sort();
  233. }
  234. bool DebugHud::ResetAppStats(const String& label)
  235. {
  236. return appStats_.Erase(label);
  237. }
  238. void DebugHud::ClearAppStats()
  239. {
  240. appStats_.Clear();
  241. }
  242. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  243. {
  244. using namespace PostUpdate;
  245. Update();
  246. }
  247. }