DebugHud.cpp 7.3 KB

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