DebugHud.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #ifdef DISABLED__
  2. //
  3. // Copyright (c) 2008-2016 the Urho3D project.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "../Precompiled.h"
  24. #include "../Core/CoreEvents.h"
  25. #include "../Core/Profiler.h"
  26. #include "../Core/EventProfiler.h"
  27. #include "../Core/Context.h"
  28. #include "../Engine/DebugHud.h"
  29. #include "../Engine/Engine.h"
  30. #include "../Graphics/Graphics.h"
  31. #include "../Graphics/Renderer.h"
  32. #include "../Resource/ResourceCache.h"
  33. #include "../IO/Log.h"
  34. #include "../UI/Font.h"
  35. #include "../UI/Text.h"
  36. #include "../UI/UI.h"
  37. #include "../DebugNew.h"
  38. namespace Atomic
  39. {
  40. static const char* qualityTexts[] =
  41. {
  42. "Low",
  43. "Med",
  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. UI* 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_RIGHT, VA_TOP);
  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, ATOMIC_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. Graphics* graphics = GetSubsystem<Graphics>();
  102. Renderer* renderer = GetSubsystem<Renderer>();
  103. if (!renderer || !graphics)
  104. return;
  105. // Ensure UI-elements are not detached
  106. if (!statsText_->GetParent())
  107. {
  108. UI* 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[renderer->GetMaterialQuality()],
  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. Profiler* profiler = GetSubsystem<Profiler>();
  159. EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
  160. if (profiler)
  161. {
  162. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  163. {
  164. profilerTimer_.Reset();
  165. if (profilerText_->IsVisible())
  166. {
  167. String profilerOutput = profiler->PrintData(false, false, profilerMaxDepth_);
  168. profilerText_->SetText(profilerOutput);
  169. }
  170. profiler->BeginInterval();
  171. if (eventProfiler)
  172. {
  173. if (eventProfilerText_->IsVisible())
  174. {
  175. String profilerOutput = eventProfiler->PrintData(false, false, profilerMaxDepth_);
  176. eventProfilerText_->SetText(profilerOutput);
  177. }
  178. eventProfiler->BeginInterval();
  179. }
  180. }
  181. }
  182. if (memoryText_->IsVisible())
  183. {
  184. ResourceCache* cache = GetSubsystem<ResourceCache>();
  185. memoryText_->SetText(cache->PrintMemoryUsage());
  186. }
  187. }
  188. void DebugHud::SetDefaultStyle(XMLFile* style)
  189. {
  190. if (!style)
  191. return;
  192. statsText_->SetDefaultStyle(style);
  193. statsText_->SetStyle("DebugHudText");
  194. modeText_->SetDefaultStyle(style);
  195. modeText_->SetStyle("DebugHudText");
  196. profilerText_->SetDefaultStyle(style);
  197. profilerText_->SetStyle("DebugHudText");
  198. memoryText_->SetDefaultStyle(style);
  199. memoryText_->SetStyle("DebugHudText");
  200. eventProfilerText_->SetDefaultStyle(style);
  201. eventProfilerText_->SetStyle("DebugHudText");
  202. }
  203. void DebugHud::SetMode(unsigned mode)
  204. {
  205. statsText_->SetVisible((mode & DEBUGHUD_SHOW_STATS) != 0);
  206. modeText_->SetVisible((mode & DEBUGHUD_SHOW_MODE) != 0);
  207. profilerText_->SetVisible((mode & DEBUGHUD_SHOW_PROFILER) != 0);
  208. memoryText_->SetVisible((mode & DEBUGHUD_SHOW_MEMORY) != 0);
  209. eventProfilerText_->SetVisible((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0);
  210. #ifdef ATOMIC_PROFILING
  211. // Event profiler is created on engine initialization if "EventProfiler" parameter is set
  212. EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
  213. if (eventProfiler)
  214. EventProfiler::SetActive((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0);
  215. #endif
  216. mode_ = mode;
  217. }
  218. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  219. {
  220. profilerMaxDepth_ = depth;
  221. }
  222. void DebugHud::SetProfilerInterval(float interval)
  223. {
  224. profilerInterval_ = Max((unsigned)(interval * 1000.0f), 0U);
  225. }
  226. void DebugHud::SetUseRendererStats(bool enable)
  227. {
  228. useRendererStats_ = enable;
  229. }
  230. void DebugHud::Toggle(unsigned mode)
  231. {
  232. SetMode(GetMode() ^ mode);
  233. }
  234. void DebugHud::ToggleAll()
  235. {
  236. Toggle(DEBUGHUD_SHOW_ALL);
  237. }
  238. XMLFile* DebugHud::GetDefaultStyle() const
  239. {
  240. return statsText_->GetDefaultStyle(false);
  241. }
  242. float DebugHud::GetProfilerInterval() const
  243. {
  244. return (float)profilerInterval_ / 1000.0f;
  245. }
  246. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  247. {
  248. SetAppStats(label, stats.ToString());
  249. }
  250. void DebugHud::SetAppStats(const String& label, const String& stats)
  251. {
  252. bool newLabel = !appStats_.Contains(label);
  253. appStats_[label] = stats;
  254. if (newLabel)
  255. appStats_.Sort();
  256. }
  257. bool DebugHud::ResetAppStats(const String& label)
  258. {
  259. return appStats_.Erase(label);
  260. }
  261. void DebugHud::ClearAppStats()
  262. {
  263. appStats_.Clear();
  264. }
  265. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  266. {
  267. using namespace PostUpdate;
  268. Update();
  269. }
  270. }
  271. #endif