DebugHud.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // Copyright (c) 2008-2016 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. };
  45. static const char* shadowQualityTexts[] =
  46. {
  47. "16bit Simple",
  48. "24bit Simple",
  49. "16bit PCF",
  50. "24bit PCF",
  51. "VSM",
  52. "Blurred VSM"
  53. };
  54. DebugHud::DebugHud(Context* context) :
  55. Object(context),
  56. profilerMaxDepth_(M_MAX_UNSIGNED),
  57. profilerInterval_(1000),
  58. useRendererStats_(false),
  59. mode_(DEBUGHUD_SHOW_NONE)
  60. {
  61. UI* ui = GetSubsystem<UI>();
  62. UIElement* uiRoot = ui->GetRoot();
  63. statsText_ = new Text(context_);
  64. statsText_->SetAlignment(HA_LEFT, VA_TOP);
  65. statsText_->SetPriority(100);
  66. statsText_->SetVisible(false);
  67. uiRoot->AddChild(statsText_);
  68. modeText_ = new Text(context_);
  69. modeText_->SetAlignment(HA_LEFT, VA_BOTTOM);
  70. modeText_->SetPriority(100);
  71. modeText_->SetVisible(false);
  72. uiRoot->AddChild(modeText_);
  73. profilerText_ = new Text(context_);
  74. profilerText_->SetAlignment(HA_RIGHT, VA_TOP);
  75. profilerText_->SetPriority(100);
  76. profilerText_->SetVisible(false);
  77. uiRoot->AddChild(profilerText_);
  78. memoryText_ = new Text(context_);
  79. memoryText_->SetAlignment(HA_RIGHT, VA_TOP);
  80. memoryText_->SetPriority(100);
  81. memoryText_->SetVisible(false);
  82. uiRoot->AddChild(memoryText_);
  83. eventProfilerText_ = new Text(context_);
  84. eventProfilerText_->SetAlignment(HA_RIGHT, VA_TOP);
  85. eventProfilerText_->SetPriority(100);
  86. eventProfilerText_->SetVisible(false);
  87. uiRoot->AddChild(eventProfilerText_);
  88. SubscribeToEvent(E_POSTUPDATE, URHO3D_HANDLER(DebugHud, HandlePostUpdate));
  89. }
  90. DebugHud::~DebugHud()
  91. {
  92. statsText_->Remove();
  93. modeText_->Remove();
  94. profilerText_->Remove();
  95. memoryText_->Remove();
  96. eventProfilerText_->Remove();
  97. }
  98. void DebugHud::Update()
  99. {
  100. Graphics* graphics = GetSubsystem<Graphics>();
  101. Renderer* renderer = GetSubsystem<Renderer>();
  102. if (!renderer || !graphics)
  103. return;
  104. // Ensure UI-elements are not detached
  105. if (!statsText_->GetParent())
  106. {
  107. UI* ui = GetSubsystem<UI>();
  108. UIElement* uiRoot = ui->GetRoot();
  109. uiRoot->AddChild(statsText_);
  110. uiRoot->AddChild(modeText_);
  111. uiRoot->AddChild(profilerText_);
  112. }
  113. if (statsText_->IsVisible())
  114. {
  115. unsigned primitives, batches;
  116. if (!useRendererStats_)
  117. {
  118. primitives = graphics->GetNumPrimitives();
  119. batches = graphics->GetNumBatches();
  120. }
  121. else
  122. {
  123. primitives = renderer->GetNumPrimitives();
  124. batches = renderer->GetNumBatches();
  125. }
  126. String stats;
  127. stats.AppendWithFormat("Triangles %u\nBatches %u\nViews %u\nLights %u\nShadowmaps %u\nOccluders %u",
  128. primitives,
  129. batches,
  130. renderer->GetNumViews(),
  131. renderer->GetNumLights(true),
  132. renderer->GetNumShadowMaps(true),
  133. renderer->GetNumOccluders(true));
  134. if (!appStats_.Empty())
  135. {
  136. stats.Append("\n");
  137. for (HashMap<String, String>::ConstIterator i = appStats_.Begin(); i != appStats_.End(); ++i)
  138. stats.AppendWithFormat("\n%s %s", i->first_.CString(), i->second_.CString());
  139. }
  140. statsText_->SetText(stats);
  141. }
  142. if (modeText_->IsVisible())
  143. {
  144. String mode;
  145. mode.AppendWithFormat("Tex:%s Mat:%s Spec:%s Shadows:%s Size:%i Quality:%s Occlusion:%s Instancing:%s API:%s",
  146. qualityTexts[renderer->GetTextureQuality()],
  147. qualityTexts[renderer->GetMaterialQuality()],
  148. renderer->GetSpecularLighting() ? "On" : "Off",
  149. renderer->GetDrawShadows() ? "On" : "Off",
  150. renderer->GetShadowMapSize(),
  151. shadowQualityTexts[renderer->GetShadowQuality()],
  152. renderer->GetMaxOccluderTriangles() > 0 ? "On" : "Off",
  153. renderer->GetDynamicInstancing() ? "On" : "Off",
  154. graphics->GetApiName().CString());
  155. modeText_->SetText(mode);
  156. }
  157. Profiler* profiler = GetSubsystem<Profiler>();
  158. EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
  159. if (profiler)
  160. {
  161. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  162. {
  163. profilerTimer_.Reset();
  164. if (profilerText_->IsVisible())
  165. {
  166. String profilerOutput = profiler->PrintData(false, false, profilerMaxDepth_);
  167. profilerText_->SetText(profilerOutput);
  168. }
  169. profiler->BeginInterval();
  170. if (eventProfiler)
  171. {
  172. if (eventProfilerText_->IsVisible())
  173. {
  174. String profilerOutput = eventProfiler->PrintData(false, false, profilerMaxDepth_);
  175. eventProfilerText_->SetText(profilerOutput);
  176. }
  177. eventProfiler->BeginInterval();
  178. }
  179. }
  180. }
  181. if (memoryText_->IsVisible())
  182. {
  183. ResourceCache* cache = GetSubsystem<ResourceCache>();
  184. memoryText_->SetText(cache->PrintMemoryUsage());
  185. }
  186. }
  187. void DebugHud::SetDefaultStyle(XMLFile* style)
  188. {
  189. if (!style)
  190. return;
  191. statsText_->SetDefaultStyle(style);
  192. statsText_->SetStyle("DebugHudText");
  193. modeText_->SetDefaultStyle(style);
  194. modeText_->SetStyle("DebugHudText");
  195. profilerText_->SetDefaultStyle(style);
  196. profilerText_->SetStyle("DebugHudText");
  197. memoryText_->SetDefaultStyle(style);
  198. memoryText_->SetStyle("DebugHudText");
  199. eventProfilerText_->SetDefaultStyle(style);
  200. eventProfilerText_->SetStyle("DebugHudText");
  201. }
  202. void DebugHud::SetMode(unsigned mode)
  203. {
  204. statsText_->SetVisible((mode & DEBUGHUD_SHOW_STATS) != 0);
  205. modeText_->SetVisible((mode & DEBUGHUD_SHOW_MODE) != 0);
  206. profilerText_->SetVisible((mode & DEBUGHUD_SHOW_PROFILER) != 0);
  207. memoryText_->SetVisible((mode & DEBUGHUD_SHOW_MEMORY) != 0);
  208. eventProfilerText_->SetVisible((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0);
  209. #ifdef URHO3D_PROFILING
  210. // Event profiler is created on engine initialization if "EventProfiler" parameter is set
  211. EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
  212. if (eventProfiler)
  213. EventProfiler::SetActive((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0);
  214. #endif
  215. mode_ = mode;
  216. }
  217. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  218. {
  219. profilerMaxDepth_ = depth;
  220. }
  221. void DebugHud::SetProfilerInterval(float interval)
  222. {
  223. profilerInterval_ = Max((unsigned)(interval * 1000.0f), 0U);
  224. }
  225. void DebugHud::SetUseRendererStats(bool enable)
  226. {
  227. useRendererStats_ = enable;
  228. }
  229. void DebugHud::Toggle(unsigned mode)
  230. {
  231. SetMode(GetMode() ^ mode);
  232. }
  233. void DebugHud::ToggleAll()
  234. {
  235. Toggle(DEBUGHUD_SHOW_ALL);
  236. }
  237. XMLFile* DebugHud::GetDefaultStyle() const
  238. {
  239. return statsText_->GetDefaultStyle(false);
  240. }
  241. float DebugHud::GetProfilerInterval() const
  242. {
  243. return (float)profilerInterval_ / 1000.0f;
  244. }
  245. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  246. {
  247. SetAppStats(label, stats.ToString());
  248. }
  249. void DebugHud::SetAppStats(const String& label, const String& stats)
  250. {
  251. bool newLabel = !appStats_.Contains(label);
  252. appStats_[label] = stats;
  253. if (newLabel)
  254. appStats_.Sort();
  255. }
  256. bool DebugHud::ResetAppStats(const String& label)
  257. {
  258. return appStats_.Erase(label);
  259. }
  260. void DebugHud::ClearAppStats()
  261. {
  262. appStats_.Clear();
  263. }
  264. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  265. {
  266. using namespace PostUpdate;
  267. Update();
  268. }
  269. }