DebugHud.cpp 7.3 KB

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