DebugHud.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/CoreEvents.h"
  24. #include "../Core/Profiler.h"
  25. #include "../Core/EventProfiler.h"
  26. #include "../Core/Context.h"
  27. #include "../Engine/DebugHud.h"
  28. #include "../Engine/Engine.h"
  29. #include "../Graphics/Graphics.h"
  30. #include "../Graphics/Renderer.h"
  31. #include "../Resource/ResourceCache.h"
  32. #include "../IO/Log.h"
  33. #include "../UI/Font.h"
  34. #include "../UI/Text.h"
  35. #include "../UI/UI.h"
  36. #include "../DebugNew.h"
  37. namespace Urho3D
  38. {
  39. static const char* qualityTexts[] =
  40. {
  41. "Low",
  42. "Med",
  43. "High",
  44. "High+"
  45. };
  46. static const char* shadowQualityTexts[] =
  47. {
  48. "16bit Simple",
  49. "24bit Simple",
  50. "16bit PCF",
  51. "24bit PCF",
  52. "VSM",
  53. "Blurred VSM"
  54. };
  55. DebugHud::DebugHud(Context* context) :
  56. Object(context),
  57. profilerMaxDepth_(M_MAX_UNSIGNED),
  58. profilerInterval_(1000),
  59. useRendererStats_(false),
  60. mode_(DEBUGHUD_SHOW_NONE)
  61. {
  62. auto* ui = GetSubsystem<UI>();
  63. UIElement* uiRoot = ui->GetRoot();
  64. statsText_ = new Text(context_);
  65. statsText_->SetAlignment(HA_LEFT, VA_TOP);
  66. statsText_->SetPriority(100);
  67. statsText_->SetVisible(false);
  68. uiRoot->AddChild(statsText_);
  69. modeText_ = new Text(context_);
  70. modeText_->SetAlignment(HA_LEFT, VA_BOTTOM);
  71. modeText_->SetPriority(100);
  72. modeText_->SetVisible(false);
  73. uiRoot->AddChild(modeText_);
  74. profilerText_ = new Text(context_);
  75. profilerText_->SetAlignment(HA_RIGHT, VA_TOP);
  76. profilerText_->SetPriority(100);
  77. profilerText_->SetVisible(false);
  78. uiRoot->AddChild(profilerText_);
  79. memoryText_ = new Text(context_);
  80. memoryText_->SetAlignment(HA_LEFT, VA_BOTTOM);
  81. memoryText_->SetPriority(100);
  82. memoryText_->SetVisible(false);
  83. uiRoot->AddChild(memoryText_);
  84. eventProfilerText_ = new Text(context_);
  85. eventProfilerText_->SetAlignment(HA_RIGHT, VA_TOP);
  86. eventProfilerText_->SetPriority(100);
  87. eventProfilerText_->SetVisible(false);
  88. uiRoot->AddChild(eventProfilerText_);
  89. SubscribeToEvent(E_POSTUPDATE, URHO3D_HANDLER(DebugHud, HandlePostUpdate));
  90. }
  91. DebugHud::~DebugHud()
  92. {
  93. statsText_->Remove();
  94. modeText_->Remove();
  95. profilerText_->Remove();
  96. memoryText_->Remove();
  97. eventProfilerText_->Remove();
  98. }
  99. void DebugHud::Update()
  100. {
  101. auto* graphics = GetSubsystem<Graphics>();
  102. auto* renderer = GetSubsystem<Renderer>();
  103. if (!renderer || !graphics)
  104. return;
  105. // Ensure UI-elements are not detached
  106. if (!statsText_->GetParent())
  107. {
  108. auto* ui = GetSubsystem<UI>();
  109. UIElement* uiRoot = ui->GetRoot();
  110. uiRoot->AddChild(statsText_);
  111. uiRoot->AddChild(modeText_);
  112. uiRoot->AddChild(profilerText_);
  113. }
  114. if (statsText_->IsVisible())
  115. {
  116. unsigned primitives, batches;
  117. if (!useRendererStats_)
  118. {
  119. primitives = graphics->GetNumPrimitives();
  120. batches = graphics->GetNumBatches();
  121. }
  122. else
  123. {
  124. primitives = renderer->GetNumPrimitives();
  125. batches = renderer->GetNumBatches();
  126. }
  127. String stats;
  128. stats.AppendWithFormat("Triangles %u\nBatches %u\nViews %u\nLights %u\nShadowmaps %u\nOccluders %u",
  129. primitives,
  130. batches,
  131. renderer->GetNumViews(),
  132. renderer->GetNumLights(true),
  133. renderer->GetNumShadowMaps(true),
  134. renderer->GetNumOccluders(true));
  135. if (!appStats_.Empty())
  136. {
  137. stats.Append("\n");
  138. for (HashMap<String, String>::ConstIterator i = appStats_.Begin(); i != appStats_.End(); ++i)
  139. stats.AppendWithFormat("\n%s %s", i->first_.CString(), i->second_.CString());
  140. }
  141. statsText_->SetText(stats);
  142. }
  143. if (modeText_->IsVisible())
  144. {
  145. String mode;
  146. mode.AppendWithFormat("Tex:%s Mat:%s Spec:%s Shadows:%s Size:%i Quality:%s Occlusion:%s Instancing:%s API:%s",
  147. qualityTexts[renderer->GetTextureQuality()],
  148. qualityTexts[Min(renderer->GetMaterialQuality(), 3)],
  149. renderer->GetSpecularLighting() ? "On" : "Off",
  150. renderer->GetDrawShadows() ? "On" : "Off",
  151. renderer->GetShadowMapSize(),
  152. shadowQualityTexts[renderer->GetShadowQuality()],
  153. renderer->GetMaxOccluderTriangles() > 0 ? "On" : "Off",
  154. renderer->GetDynamicInstancing() ? "On" : "Off",
  155. graphics->GetApiName().CString());
  156. modeText_->SetText(mode);
  157. }
  158. auto* profiler = GetSubsystem<Profiler>();
  159. auto* eventProfiler = GetSubsystem<EventProfiler>();
  160. if (profiler)
  161. {
  162. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  163. {
  164. profilerTimer_.Reset();
  165. if (profilerText_->IsVisible())
  166. profilerText_->SetText(profiler->PrintData(false, false, profilerMaxDepth_));
  167. profiler->BeginInterval();
  168. if (eventProfiler)
  169. {
  170. if (eventProfilerText_->IsVisible())
  171. eventProfilerText_->SetText(eventProfiler->PrintData(false, false, profilerMaxDepth_));
  172. eventProfiler->BeginInterval();
  173. }
  174. }
  175. }
  176. if (memoryText_->IsVisible())
  177. memoryText_->SetText(GetSubsystem<ResourceCache>()->PrintMemoryUsage());
  178. }
  179. void DebugHud::SetDefaultStyle(XMLFile* style)
  180. {
  181. if (!style)
  182. return;
  183. statsText_->SetDefaultStyle(style);
  184. statsText_->SetStyle("DebugHudText");
  185. modeText_->SetDefaultStyle(style);
  186. modeText_->SetStyle("DebugHudText");
  187. profilerText_->SetDefaultStyle(style);
  188. profilerText_->SetStyle("DebugHudText");
  189. memoryText_->SetDefaultStyle(style);
  190. memoryText_->SetStyle("DebugHudText");
  191. eventProfilerText_->SetDefaultStyle(style);
  192. eventProfilerText_->SetStyle("DebugHudText");
  193. }
  194. void DebugHud::SetMode(unsigned mode)
  195. {
  196. statsText_->SetVisible((mode & DEBUGHUD_SHOW_STATS) != 0);
  197. modeText_->SetVisible((mode & DEBUGHUD_SHOW_MODE) != 0);
  198. profilerText_->SetVisible((mode & DEBUGHUD_SHOW_PROFILER) != 0);
  199. memoryText_->SetVisible((mode & DEBUGHUD_SHOW_MEMORY) != 0);
  200. eventProfilerText_->SetVisible((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0);
  201. memoryText_->SetPosition(0, modeText_->IsVisible() ? modeText_->GetHeight() * -2 : 0);
  202. #ifdef URHO3D_PROFILING
  203. // Event profiler is created on engine initialization if "EventProfiler" parameter is set
  204. auto* eventProfiler = GetSubsystem<EventProfiler>();
  205. if (eventProfiler)
  206. EventProfiler::SetActive((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0);
  207. #endif
  208. mode_ = mode;
  209. }
  210. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  211. {
  212. profilerMaxDepth_ = depth;
  213. }
  214. void DebugHud::SetProfilerInterval(float interval)
  215. {
  216. profilerInterval_ = Max((unsigned)(interval * 1000.0f), 0U);
  217. }
  218. void DebugHud::SetUseRendererStats(bool enable)
  219. {
  220. useRendererStats_ = enable;
  221. }
  222. void DebugHud::Toggle(unsigned mode)
  223. {
  224. SetMode(GetMode() ^ mode);
  225. }
  226. void DebugHud::ToggleAll()
  227. {
  228. Toggle(DEBUGHUD_SHOW_ALL);
  229. }
  230. XMLFile* DebugHud::GetDefaultStyle() const
  231. {
  232. return statsText_->GetDefaultStyle(false);
  233. }
  234. float DebugHud::GetProfilerInterval() const
  235. {
  236. return (float)profilerInterval_ / 1000.0f;
  237. }
  238. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  239. {
  240. SetAppStats(label, stats.ToString());
  241. }
  242. void DebugHud::SetAppStats(const String& label, const String& stats)
  243. {
  244. bool newLabel = !appStats_.Contains(label);
  245. appStats_[label] = stats;
  246. if (newLabel)
  247. appStats_.Sort();
  248. }
  249. bool DebugHud::ResetAppStats(const String& label)
  250. {
  251. return appStats_.Erase(label);
  252. }
  253. void DebugHud::ClearAppStats()
  254. {
  255. appStats_.Clear();
  256. }
  257. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  258. {
  259. using namespace PostUpdate;
  260. Update();
  261. }
  262. }