DebugHud.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Core/CoreEvents.h"
  23. #include "../../Core/Profiler.h"
  24. #include "../../Engine/Engine.h"
  25. #include "../../Graphics/Graphics.h"
  26. #include "../../Graphics/Renderer.h"
  27. #include "../../IO/Log.h"
  28. #include "Font.h"
  29. #include "Text.h"
  30. #include "SystemUI.h"
  31. #include "UIElement.h"
  32. #include "DebugHud.h"
  33. #include "../../DebugNew.h"
  34. #include "../../Metrics/Metrics.h"
  35. namespace Atomic
  36. {
  37. namespace SystemUI
  38. {
  39. static const char* qualityTexts[] =
  40. {
  41. "Low",
  42. "Med",
  43. "High"
  44. };
  45. static const char* shadowQualityTexts[] =
  46. {
  47. "16bit Low",
  48. "24bit Low",
  49. "16bit High",
  50. "24bit High"
  51. };
  52. static const float FPS_UPDATE_INTERVAL = 0.5f;
  53. DebugHud::DebugHud(Context* context) :
  54. Object(context),
  55. profilerMaxDepth_(M_MAX_UNSIGNED),
  56. profilerMode_(DEBUG_HUD_PROFILE_PERFORMANCE),
  57. profilerInterval_(1000),
  58. useRendererStats_(true),
  59. mode_(DEBUGHUD_SHOW_NONE),
  60. fpsTimeSinceUpdate_(FPS_UPDATE_INTERVAL),
  61. fpsFramesSinceUpdate_(0),
  62. fps_(0)
  63. {
  64. SystemUI* ui = GetSubsystem<SystemUI>();
  65. UIElement* uiRoot = ui->GetRoot();
  66. layout_ = new UIElement(context_);
  67. uiRoot->AddChild(layout_);
  68. layout_->SetSize(uiRoot->GetSize());
  69. statsText_ = new Text(context_);
  70. statsText_->SetAlignment(HA_LEFT, VA_TOP);
  71. statsText_->SetPriority(100);
  72. statsText_->SetVisible(false);
  73. layout_->AddChild(statsText_);
  74. modeText_ = new Text(context_);
  75. modeText_->SetAlignment(HA_LEFT, VA_BOTTOM);
  76. modeText_->SetPriority(100);
  77. modeText_->SetVisible(false);
  78. layout_->AddChild(modeText_);
  79. profilerText_ = new Text(context_);
  80. profilerText_->SetAlignment(HA_RIGHT, VA_TOP);
  81. profilerText_->SetPriority(100);
  82. profilerText_->SetVisible(false);
  83. layout_->AddChild(profilerText_);
  84. SubscribeToEvent(E_POSTUPDATE, ATOMIC_HANDLER(DebugHud, HandlePostUpdate));
  85. statsText_->SetTextEffect(TE_SHADOW);
  86. modeText_->SetTextEffect(TE_SHADOW);
  87. profilerText_->SetTextEffect(TE_SHADOW);
  88. }
  89. DebugHud::~DebugHud()
  90. {
  91. statsText_->Remove();
  92. modeText_->Remove();
  93. profilerText_->Remove();
  94. }
  95. void DebugHud::SetExtents(bool useRootExtents, const IntVector2& position, const IntVector2& size)
  96. {
  97. if (useRootExtents)
  98. {
  99. SystemUI* ui = GetSubsystem<SystemUI>();
  100. UIElement* uiRoot = ui->GetRoot();
  101. layout_->SetPosition(IntVector2::ZERO);
  102. layout_->SetSize(uiRoot->GetSize());
  103. }
  104. else
  105. {
  106. layout_->SetPosition(position);
  107. layout_->SetSize(size);
  108. }
  109. }
  110. void DebugHud::Update(float timeStep)
  111. {
  112. Graphics* graphics = GetSubsystem<Graphics>();
  113. Renderer* renderer = GetSubsystem<Renderer>();
  114. if (!renderer || !graphics)
  115. return;
  116. // Ensure UI-elements are not detached
  117. if (!layout_->GetParent())
  118. {
  119. SystemUI* ui = GetSubsystem<SystemUI>();
  120. UIElement* uiRoot = ui->GetRoot();
  121. uiRoot->AddChild(layout_);
  122. }
  123. if (statsText_->IsVisible())
  124. {
  125. fpsTimeSinceUpdate_ += timeStep;
  126. ++fpsFramesSinceUpdate_;
  127. if (fpsTimeSinceUpdate_ > FPS_UPDATE_INTERVAL)
  128. {
  129. fps_ = (int)(fpsFramesSinceUpdate_ / fpsTimeSinceUpdate_);
  130. fpsFramesSinceUpdate_ = 0;
  131. fpsTimeSinceUpdate_ = 0;
  132. }
  133. unsigned primitives, batches;
  134. if (!useRendererStats_)
  135. {
  136. primitives = graphics->GetNumPrimitives();
  137. batches = graphics->GetNumBatches();
  138. }
  139. else
  140. {
  141. primitives = renderer->GetNumPrimitives();
  142. batches = renderer->GetNumBatches();
  143. }
  144. String stats;
  145. unsigned singlePassPrimitives = graphics->GetSinglePassPrimitives();
  146. unsigned editorPrimitives = graphics->GetNumPrimitives() - renderer->GetNumPrimitives();
  147. if (singlePassPrimitives)
  148. stats.AppendWithFormat("FPS %d\nTriangles (All passes) %u\nTriangles (Single pass) %u\nTriangles (Editor) %u\n",
  149. fps_,
  150. primitives,
  151. singlePassPrimitives,
  152. editorPrimitives);
  153. else
  154. stats.AppendWithFormat("FPS %d\nTriangles %u\n", fps_, primitives);
  155. stats.AppendWithFormat("Batches %u\nViews %u\nLights %u\nShadowmaps %u\nOccluders %u",
  156. batches,
  157. renderer->GetNumViews(),
  158. renderer->GetNumLights(true),
  159. renderer->GetNumShadowMaps(true),
  160. renderer->GetNumOccluders(true));
  161. if (!appStats_.Empty())
  162. {
  163. stats.Append("\n");
  164. for (HashMap<String, String>::ConstIterator i = appStats_.Begin(); i != appStats_.End(); ++i)
  165. stats.AppendWithFormat("\n%s %s", i->first_.CString(), i->second_.CString());
  166. }
  167. statsText_->SetText(stats);
  168. }
  169. if (modeText_->IsVisible())
  170. {
  171. String mode;
  172. mode.AppendWithFormat("Tex:%s Mat:%s Spec:%s Shadows:%s Size:%i Quality:%s Occlusion:%s Instancing:%s API:%s",
  173. qualityTexts[renderer->GetTextureQuality()],
  174. qualityTexts[renderer->GetMaterialQuality()],
  175. renderer->GetSpecularLighting() ? "On" : "Off",
  176. renderer->GetDrawShadows() ? "On" : "Off",
  177. renderer->GetShadowMapSize(),
  178. shadowQualityTexts[renderer->GetShadowQuality()],
  179. renderer->GetMaxOccluderTriangles() > 0 ? "On" : "Off",
  180. renderer->GetDynamicInstancing() ? "On" : "Off",
  181. graphics->GetApiName().CString());
  182. modeText_->SetText(mode);
  183. }
  184. Profiler* profiler = GetSubsystem<Profiler>();
  185. if (profiler)
  186. {
  187. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  188. {
  189. profilerTimer_.Reset();
  190. if (profilerText_->IsVisible())
  191. {
  192. String profilerOutput;
  193. if (profilerMode_ == DEBUG_HUD_PROFILE_PERFORMANCE)
  194. {
  195. profilerOutput = profiler->PrintData(false, false, profilerMaxDepth_);
  196. }
  197. else
  198. {
  199. Metrics* metrics = GetSubsystem<Metrics>();
  200. if (metrics)
  201. {
  202. if (!metrics->GetEnabled())
  203. metrics->Enable();
  204. SharedPtr<MetricsSnapshot> snapshot(new MetricsSnapshot());
  205. metrics->Capture(snapshot);
  206. profilerOutput = snapshot->PrintData(2);
  207. }
  208. else
  209. {
  210. profilerOutput = "Metrics subsystem not found";
  211. }
  212. }
  213. profilerText_->SetText(profilerOutput);
  214. }
  215. profiler->BeginInterval();
  216. }
  217. }
  218. }
  219. void DebugHud::SetProfilerMode(DebugHudProfileMode mode)
  220. {
  221. profilerMode_ = mode;
  222. if (profilerMode_ == DEBUG_HUD_PROFILE_PERFORMANCE)
  223. {
  224. if (profilerText_.NotNull())
  225. {
  226. profilerText_->SetText("");
  227. profilerText_->SetFont(profilerText_->GetFont(), 11);
  228. }
  229. }
  230. else
  231. {
  232. int size = 8;
  233. Metrics* metrics = GetSubsystem<Metrics>();
  234. if (!metrics)
  235. size = 32;
  236. if (profilerText_.NotNull())
  237. {
  238. profilerText_->SetText("");
  239. profilerText_->SetFont(profilerText_->GetFont(), size);
  240. }
  241. }
  242. }
  243. void DebugHud::SetDefaultStyle(XMLFile* style)
  244. {
  245. if (!style)
  246. return;
  247. statsText_->SetDefaultStyle(style);
  248. statsText_->SetStyle("DebugHudText");
  249. modeText_->SetDefaultStyle(style);
  250. modeText_->SetStyle("DebugHudText");
  251. profilerText_->SetDefaultStyle(style);
  252. profilerText_->SetStyle("DebugHudText");
  253. }
  254. void DebugHud::SetMode(unsigned mode)
  255. {
  256. statsText_->SetVisible((mode & DEBUGHUD_SHOW_STATS) != 0);
  257. modeText_->SetVisible((mode & DEBUGHUD_SHOW_MODE) != 0);
  258. profilerText_->SetVisible((mode & DEBUGHUD_SHOW_PROFILER) != 0);
  259. mode_ = mode;
  260. }
  261. void DebugHud::CycleMode()
  262. {
  263. switch (mode_)
  264. {
  265. case DEBUGHUD_SHOW_NONE:
  266. SetMode(DEBUGHUD_SHOW_STATS);
  267. break;
  268. case DEBUGHUD_SHOW_STATS:
  269. SetMode(DEBUGHUD_SHOW_MODE);
  270. break;
  271. case DEBUGHUD_SHOW_MODE:
  272. SetMode(DEBUGHUD_SHOW_PROFILER);
  273. break;
  274. case DEBUGHUD_SHOW_PROFILER:
  275. SetMode(DEBUGHUD_SHOW_ALL);
  276. break;
  277. case DEBUGHUD_SHOW_ALL:
  278. default:
  279. SetMode(DEBUGHUD_SHOW_NONE);
  280. break;
  281. }
  282. }
  283. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  284. {
  285. profilerMaxDepth_ = depth;
  286. }
  287. void DebugHud::SetProfilerInterval(float interval)
  288. {
  289. profilerInterval_ = (unsigned)Max((int)(interval * 1000.0f), 0);
  290. }
  291. void DebugHud::SetUseRendererStats(bool enable)
  292. {
  293. useRendererStats_ = enable;
  294. }
  295. void DebugHud::Toggle(unsigned mode)
  296. {
  297. SetMode(GetMode() ^ mode);
  298. }
  299. void DebugHud::ToggleAll()
  300. {
  301. Toggle(DEBUGHUD_SHOW_ALL);
  302. }
  303. XMLFile* DebugHud::GetDefaultStyle() const
  304. {
  305. return statsText_->GetDefaultStyle(false);
  306. }
  307. float DebugHud::GetProfilerInterval() const
  308. {
  309. return (float)profilerInterval_ / 1000.0f;
  310. }
  311. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  312. {
  313. SetAppStats(label, stats.ToString());
  314. }
  315. void DebugHud::SetAppStats(const String& label, const String& stats)
  316. {
  317. bool newLabel = !appStats_.Contains(label);
  318. appStats_[label] = stats;
  319. if (newLabel)
  320. appStats_.Sort();
  321. }
  322. bool DebugHud::ResetAppStats(const String& label)
  323. {
  324. return appStats_.Erase(label);
  325. }
  326. void DebugHud::ClearAppStats()
  327. {
  328. appStats_.Clear();
  329. }
  330. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  331. {
  332. using namespace PostUpdate;
  333. Update(eventData[P_TIMESTEP].GetFloat());
  334. }
  335. }
  336. }