DebugHud.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "../Precompiled.h"
  23. #include "../Core/CoreEvents.h"
  24. #include "../Core/Profiler.h"
  25. #include "../Engine/DebugHud.h"
  26. #include "../Engine/Engine.h"
  27. #include "../Graphics/Graphics.h"
  28. #include "../Graphics/Renderer.h"
  29. #include "../Resource/ResourceCache.h"
  30. #include "../IO/Log.h"
  31. #include "../UI/Font.h"
  32. #include "../UI/Text.h"
  33. #include "../UI/UI.h"
  34. #include "../DebugNew.h"
  35. namespace Urho3D
  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. DebugHud::DebugHud(Context* context) :
  51. Object(context),
  52. profilerMaxDepth_(M_MAX_UNSIGNED),
  53. profilerInterval_(1000),
  54. useRendererStats_(false),
  55. mode_(DEBUGHUD_SHOW_NONE)
  56. {
  57. UI* ui = GetSubsystem<UI>();
  58. UIElement* uiRoot = ui->GetRoot();
  59. statsText_ = new Text(context_);
  60. statsText_->SetAlignment(HA_LEFT, VA_TOP);
  61. statsText_->SetPriority(100);
  62. statsText_->SetVisible(false);
  63. uiRoot->AddChild(statsText_);
  64. modeText_ = new Text(context_);
  65. modeText_->SetAlignment(HA_LEFT, VA_BOTTOM);
  66. modeText_->SetPriority(100);
  67. modeText_->SetVisible(false);
  68. uiRoot->AddChild(modeText_);
  69. profilerText_ = new Text(context_);
  70. profilerText_->SetAlignment(HA_RIGHT, VA_TOP);
  71. profilerText_->SetPriority(100);
  72. profilerText_->SetVisible(false);
  73. uiRoot->AddChild(profilerText_);
  74. memoryText_ = new Text(context_);
  75. memoryText_->SetAlignment(HA_RIGHT, VA_TOP);
  76. memoryText_->SetPriority(100);
  77. memoryText_->SetVisible(false);
  78. uiRoot->AddChild(memoryText_);
  79. SubscribeToEvent(E_POSTUPDATE, URHO3D_HANDLER(DebugHud, HandlePostUpdate));
  80. }
  81. DebugHud::~DebugHud()
  82. {
  83. statsText_->Remove();
  84. modeText_->Remove();
  85. profilerText_->Remove();
  86. memoryText_->Remove();
  87. }
  88. void DebugHud::Update()
  89. {
  90. Graphics* graphics = GetSubsystem<Graphics>();
  91. Renderer* renderer = GetSubsystem<Renderer>();
  92. if (!renderer || !graphics)
  93. return;
  94. // Ensure UI-elements are not detached
  95. if (!statsText_->GetParent())
  96. {
  97. UI* ui = GetSubsystem<UI>();
  98. UIElement* uiRoot = ui->GetRoot();
  99. uiRoot->AddChild(statsText_);
  100. uiRoot->AddChild(modeText_);
  101. uiRoot->AddChild(profilerText_);
  102. }
  103. if (statsText_->IsVisible())
  104. {
  105. unsigned primitives, batches;
  106. if (!useRendererStats_)
  107. {
  108. primitives = graphics->GetNumPrimitives();
  109. batches = graphics->GetNumBatches();
  110. }
  111. else
  112. {
  113. primitives = renderer->GetNumPrimitives();
  114. batches = renderer->GetNumBatches();
  115. }
  116. String stats;
  117. stats.AppendWithFormat("Triangles %u\nBatches %u\nViews %u\nLights %u\nShadowmaps %u\nOccluders %u",
  118. primitives,
  119. batches,
  120. renderer->GetNumViews(),
  121. renderer->GetNumLights(true),
  122. renderer->GetNumShadowMaps(true),
  123. renderer->GetNumOccluders(true));
  124. if (!appStats_.Empty())
  125. {
  126. stats.Append("\n");
  127. for (HashMap<String, String>::ConstIterator i = appStats_.Begin(); i != appStats_.End(); ++i)
  128. stats.AppendWithFormat("\n%s %s", i->first_.CString(), i->second_.CString());
  129. }
  130. statsText_->SetText(stats);
  131. }
  132. if (modeText_->IsVisible())
  133. {
  134. String mode;
  135. mode.AppendWithFormat("Tex:%s Mat:%s Spec:%s Shadows:%s Size:%i Quality:%s Occlusion:%s Instancing:%s API:%s",
  136. qualityTexts[renderer->GetTextureQuality()],
  137. qualityTexts[renderer->GetMaterialQuality()],
  138. renderer->GetSpecularLighting() ? "On" : "Off",
  139. renderer->GetDrawShadows() ? "On" : "Off",
  140. renderer->GetShadowMapSize(),
  141. shadowQualityTexts[renderer->GetShadowQuality()],
  142. renderer->GetMaxOccluderTriangles() > 0 ? "On" : "Off",
  143. renderer->GetDynamicInstancing() ? "On" : "Off",
  144. graphics->GetApiName().CString());
  145. modeText_->SetText(mode);
  146. }
  147. Profiler* profiler = GetSubsystem<Profiler>();
  148. if (profiler)
  149. {
  150. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  151. {
  152. profilerTimer_.Reset();
  153. if (profilerText_->IsVisible())
  154. {
  155. String profilerOutput = profiler->PrintData(false, false, profilerMaxDepth_);
  156. profilerText_->SetText(profilerOutput);
  157. }
  158. profiler->BeginInterval();
  159. }
  160. }
  161. if (memoryText_->IsVisible())
  162. {
  163. ResourceCache* cache = GetSubsystem<ResourceCache>();
  164. memoryText_->SetText(cache->PrintMemoryUsage());
  165. }
  166. }
  167. void DebugHud::SetDefaultStyle(XMLFile* style)
  168. {
  169. if (!style)
  170. return;
  171. statsText_->SetDefaultStyle(style);
  172. statsText_->SetStyle("DebugHudText");
  173. modeText_->SetDefaultStyle(style);
  174. modeText_->SetStyle("DebugHudText");
  175. profilerText_->SetDefaultStyle(style);
  176. profilerText_->SetStyle("DebugHudText");
  177. memoryText_->SetDefaultStyle(style);
  178. memoryText_->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. memoryText_->SetVisible((mode & DEBUGHUD_SHOW_MEMORY) != 0);
  186. mode_ = mode;
  187. }
  188. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  189. {
  190. profilerMaxDepth_ = depth;
  191. }
  192. void DebugHud::SetProfilerInterval(float interval)
  193. {
  194. profilerInterval_ = (unsigned)Max((int)(interval * 1000.0f), 0);
  195. }
  196. void DebugHud::SetUseRendererStats(bool enable)
  197. {
  198. useRendererStats_ = enable;
  199. }
  200. void DebugHud::Toggle(unsigned mode)
  201. {
  202. SetMode(GetMode() ^ mode);
  203. }
  204. void DebugHud::ToggleAll()
  205. {
  206. Toggle(DEBUGHUD_SHOW_ALL);
  207. }
  208. XMLFile* DebugHud::GetDefaultStyle() const
  209. {
  210. return statsText_->GetDefaultStyle(false);
  211. }
  212. float DebugHud::GetProfilerInterval() const
  213. {
  214. return (float)profilerInterval_ / 1000.0f;
  215. }
  216. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  217. {
  218. SetAppStats(label, stats.ToString());
  219. }
  220. void DebugHud::SetAppStats(const String& label, const String& stats)
  221. {
  222. bool newLabel = !appStats_.Contains(label);
  223. appStats_[label] = stats;
  224. if (newLabel)
  225. appStats_.Sort();
  226. }
  227. bool DebugHud::ResetAppStats(const String& label)
  228. {
  229. return appStats_.Erase(label);
  230. }
  231. void DebugHud::ClearAppStats()
  232. {
  233. appStats_.Clear();
  234. }
  235. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  236. {
  237. using namespace PostUpdate;
  238. Update();
  239. }
  240. }