DebugHud.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (c) 2008-2022 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((unsigned)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. modeText_->SetText(mode);
  138. }
  139. auto* profiler = GetSubsystem<Profiler>();
  140. auto* eventProfiler = GetSubsystem<EventProfiler>();
  141. if (profiler)
  142. {
  143. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  144. {
  145. profilerTimer_.Reset();
  146. if (profilerText_->IsVisible())
  147. profilerText_->SetText(profiler->PrintData(false, false, profilerMaxDepth_));
  148. profiler->BeginInterval();
  149. if (eventProfiler)
  150. {
  151. if (eventProfilerText_->IsVisible())
  152. eventProfilerText_->SetText(eventProfiler->PrintData(false, false, profilerMaxDepth_));
  153. eventProfiler->BeginInterval();
  154. }
  155. }
  156. }
  157. if (memoryText_->IsVisible())
  158. memoryText_->SetText(GetSubsystem<ResourceCache>()->PrintMemoryUsage());
  159. }
  160. void DebugHud::SetDefaultStyle(XMLFile* style)
  161. {
  162. if (!style)
  163. return;
  164. statsText_->SetDefaultStyle(style);
  165. statsText_->SetStyle("DebugHudText");
  166. modeText_->SetDefaultStyle(style);
  167. modeText_->SetStyle("DebugHudText");
  168. profilerText_->SetDefaultStyle(style);
  169. profilerText_->SetStyle("DebugHudText");
  170. memoryText_->SetDefaultStyle(style);
  171. memoryText_->SetStyle("DebugHudText");
  172. eventProfilerText_->SetDefaultStyle(style);
  173. eventProfilerText_->SetStyle("DebugHudText");
  174. }
  175. void DebugHud::SetMode(DebugHudElements mode)
  176. {
  177. statsText_->SetVisible(!!(mode & DebugHudElements::Stats));
  178. modeText_->SetVisible(!!(mode & DebugHudElements::Mode));
  179. profilerText_->SetVisible(!!(mode & DebugHudElements::Profiler));
  180. memoryText_->SetVisible(!!(mode & DebugHudElements::Memory));
  181. eventProfilerText_->SetVisible(!!(mode & DebugHudElements::EventProfiler));
  182. memoryText_->SetPosition(0, modeText_->IsVisible() ? modeText_->GetHeight() * -2 : 0);
  183. #ifdef URHO3D_PROFILING
  184. // Event profiler is created on engine initialization if "EventProfiler" parameter is set
  185. auto* eventProfiler = GetSubsystem<EventProfiler>();
  186. if (eventProfiler)
  187. EventProfiler::SetActive(!!(mode & DebugHudElements::EventProfiler));
  188. #endif
  189. mode_ = mode;
  190. }
  191. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  192. {
  193. profilerMaxDepth_ = depth;
  194. }
  195. void DebugHud::SetProfilerInterval(float interval)
  196. {
  197. profilerInterval_ = Max((unsigned)(interval * 1000.0f), 0U);
  198. }
  199. void DebugHud::SetUseRendererStats(bool enable)
  200. {
  201. useRendererStats_ = enable;
  202. }
  203. void DebugHud::Toggle(DebugHudElements mode)
  204. {
  205. SetMode(GetMode() ^ mode);
  206. }
  207. void DebugHud::ToggleAll()
  208. {
  209. Toggle(DebugHudElements::All);
  210. }
  211. XMLFile* DebugHud::GetDefaultStyle() const
  212. {
  213. return statsText_->GetDefaultStyle(false);
  214. }
  215. float DebugHud::GetProfilerInterval() const
  216. {
  217. return (float)profilerInterval_ / 1000.0f;
  218. }
  219. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  220. {
  221. SetAppStats(label, stats.ToString());
  222. }
  223. void DebugHud::SetAppStats(const String& label, const String& stats)
  224. {
  225. bool newLabel = !appStats_.Contains(label);
  226. appStats_[label] = stats;
  227. if (newLabel)
  228. appStats_.Sort();
  229. }
  230. bool DebugHud::ResetAppStats(const String& label)
  231. {
  232. return appStats_.Erase(label);
  233. }
  234. void DebugHud::ClearAppStats()
  235. {
  236. appStats_.Clear();
  237. }
  238. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  239. {
  240. using namespace PostUpdate;
  241. Update();
  242. }
  243. }