DebugHud.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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", fps_, primitives, singlePassPrimitives, editorPrimitives);
  144. else
  145. stats.AppendWithFormat("FPS %d\nTriangles %u\n", fps_, primitives);
  146. stats.AppendWithFormat("Batches %u\nViews %u\nLights %u\nShadowmaps %u\nOccluders %u",
  147. fps_,
  148. primitives,
  149. batches,
  150. renderer->GetNumViews(),
  151. renderer->GetNumLights(true),
  152. renderer->GetNumShadowMaps(true),
  153. renderer->GetNumOccluders(true));
  154. if (!appStats_.Empty())
  155. {
  156. stats.Append("\n");
  157. for (HashMap<String, String>::ConstIterator i = appStats_.Begin(); i != appStats_.End(); ++i)
  158. stats.AppendWithFormat("\n%s %s", i->first_.CString(), i->second_.CString());
  159. }
  160. statsText_->SetText(stats);
  161. }
  162. if (modeText_->IsVisible())
  163. {
  164. String mode;
  165. mode.AppendWithFormat("Tex:%s Mat:%s Spec:%s Shadows:%s Size:%i Quality:%s Occlusion:%s Instancing:%s API:%s",
  166. qualityTexts[renderer->GetTextureQuality()],
  167. qualityTexts[renderer->GetMaterialQuality()],
  168. renderer->GetSpecularLighting() ? "On" : "Off",
  169. renderer->GetDrawShadows() ? "On" : "Off",
  170. renderer->GetShadowMapSize(),
  171. shadowQualityTexts[renderer->GetShadowQuality()],
  172. renderer->GetMaxOccluderTriangles() > 0 ? "On" : "Off",
  173. renderer->GetDynamicInstancing() ? "On" : "Off",
  174. graphics->GetApiName().CString());
  175. modeText_->SetText(mode);
  176. }
  177. Profiler* profiler = GetSubsystem<Profiler>();
  178. if (profiler)
  179. {
  180. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  181. {
  182. profilerTimer_.Reset();
  183. if (profilerText_->IsVisible())
  184. {
  185. String profilerOutput = profiler->PrintData(false, false, profilerMaxDepth_);
  186. profilerText_->SetText(profilerOutput);
  187. }
  188. profiler->BeginInterval();
  189. }
  190. }
  191. }
  192. void DebugHud::SetDefaultStyle(XMLFile* style)
  193. {
  194. if (!style)
  195. return;
  196. statsText_->SetDefaultStyle(style);
  197. statsText_->SetStyle("DebugHudText");
  198. modeText_->SetDefaultStyle(style);
  199. modeText_->SetStyle("DebugHudText");
  200. profilerText_->SetDefaultStyle(style);
  201. profilerText_->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. mode_ = mode;
  209. }
  210. void DebugHud::CycleMode()
  211. {
  212. switch (mode_)
  213. {
  214. case DEBUGHUD_SHOW_NONE:
  215. SetMode(DEBUGHUD_SHOW_STATS);
  216. break;
  217. case DEBUGHUD_SHOW_STATS:
  218. SetMode(DEBUGHUD_SHOW_MODE);
  219. break;
  220. case DEBUGHUD_SHOW_MODE:
  221. SetMode(DEBUGHUD_SHOW_PROFILER);
  222. break;
  223. case DEBUGHUD_SHOW_PROFILER:
  224. SetMode(DEBUGHUD_SHOW_ALL);
  225. break;
  226. case DEBUGHUD_SHOW_ALL:
  227. default:
  228. SetMode(DEBUGHUD_SHOW_NONE);
  229. break;
  230. }
  231. }
  232. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  233. {
  234. profilerMaxDepth_ = depth;
  235. }
  236. void DebugHud::SetProfilerInterval(float interval)
  237. {
  238. profilerInterval_ = (unsigned)Max((int)(interval * 1000.0f), 0);
  239. }
  240. void DebugHud::SetUseRendererStats(bool enable)
  241. {
  242. useRendererStats_ = enable;
  243. }
  244. void DebugHud::Toggle(unsigned mode)
  245. {
  246. SetMode(GetMode() ^ mode);
  247. }
  248. void DebugHud::ToggleAll()
  249. {
  250. Toggle(DEBUGHUD_SHOW_ALL);
  251. }
  252. XMLFile* DebugHud::GetDefaultStyle() const
  253. {
  254. return statsText_->GetDefaultStyle(false);
  255. }
  256. float DebugHud::GetProfilerInterval() const
  257. {
  258. return (float)profilerInterval_ / 1000.0f;
  259. }
  260. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  261. {
  262. SetAppStats(label, stats.ToString());
  263. }
  264. void DebugHud::SetAppStats(const String& label, const String& stats)
  265. {
  266. bool newLabel = !appStats_.Contains(label);
  267. appStats_[label] = stats;
  268. if (newLabel)
  269. appStats_.Sort();
  270. }
  271. bool DebugHud::ResetAppStats(const String& label)
  272. {
  273. return appStats_.Erase(label);
  274. }
  275. void DebugHud::ClearAppStats()
  276. {
  277. appStats_.Clear();
  278. }
  279. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  280. {
  281. using namespace PostUpdate;
  282. Update(eventData[P_TIMESTEP].GetFloat());
  283. }
  284. }
  285. }