DebugHud.cpp 8.3 KB

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