DebugHud.cpp 9.3 KB

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