DebugHud.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // Copyright (c) 2008-2014 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 "CoreEvents.h"
  24. #include "DebugHud.h"
  25. #include "Engine.h"
  26. #include "Font.h"
  27. #include "Graphics.h"
  28. #include "Log.h"
  29. #include "Profiler.h"
  30. #include "Renderer.h"
  31. #include "Text.h"
  32. #include "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_UPDATE, HANDLER(DebugHud, HandleUpdate));
  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. if (statsText_->IsVisible())
  88. {
  89. unsigned primitives, batches;
  90. if (!useRendererStats_)
  91. {
  92. primitives = graphics->GetNumPrimitives();
  93. batches = graphics->GetNumBatches();
  94. }
  95. else
  96. {
  97. primitives = renderer->GetNumPrimitives();
  98. batches = renderer->GetNumBatches();
  99. }
  100. String stats;
  101. stats.AppendWithFormat("Triangles %u\nBatches %u\nViews %u\nLights %u\nShadowmaps %u\nOccluders %u",
  102. primitives,
  103. batches,
  104. renderer->GetNumViews(),
  105. renderer->GetNumLights(true),
  106. renderer->GetNumShadowMaps(true),
  107. renderer->GetNumOccluders(true));
  108. if (!appStats_.Empty())
  109. {
  110. stats.Append("\n");
  111. for (HashMap<String, String>::ConstIterator i = appStats_.Begin(); i != appStats_.End(); ++i)
  112. stats.AppendWithFormat("\n%s %s", i->first_.CString(), i->second_.CString());
  113. }
  114. statsText_->SetText(stats);
  115. }
  116. if (modeText_->IsVisible())
  117. {
  118. String mode;
  119. mode.AppendWithFormat("Tex:%s Mat:%s Spec:%s Shadows:%s Size:%i Quality:%s Occlusion:%s Instancing:%s Mode:%s",
  120. qualityTexts[renderer->GetTextureQuality()],
  121. qualityTexts[renderer->GetMaterialQuality()],
  122. renderer->GetSpecularLighting() ? "On" : "Off",
  123. renderer->GetDrawShadows() ? "On" : "Off",
  124. renderer->GetShadowMapSize(),
  125. shadowQualityTexts[renderer->GetShadowQuality()],
  126. renderer->GetMaxOccluderTriangles() > 0 ? "On" : "Off",
  127. renderer->GetDynamicInstancing() ? "On" : "Off",
  128. #ifdef USE_OPENGL
  129. "OGL");
  130. #else
  131. graphics->GetSM3Support() ? "SM3" : "SM2");
  132. #endif
  133. modeText_->SetText(mode);
  134. }
  135. Profiler* profiler = GetSubsystem<Profiler>();
  136. if (profiler)
  137. {
  138. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  139. {
  140. profilerTimer_.Reset();
  141. if (profilerText_->IsVisible())
  142. {
  143. String profilerOutput = profiler->GetData(false, false, profilerMaxDepth_);
  144. profilerText_->SetText(profilerOutput);
  145. }
  146. profiler->BeginInterval();
  147. }
  148. }
  149. }
  150. void DebugHud::SetDefaultStyle(XMLFile* style)
  151. {
  152. if (!style)
  153. return;
  154. statsText_->SetDefaultStyle(style);
  155. statsText_->SetStyle("DebugHudText");
  156. modeText_->SetDefaultStyle(style);
  157. modeText_->SetStyle("DebugHudText");
  158. profilerText_->SetDefaultStyle(style);
  159. profilerText_->SetStyle("DebugHudText");
  160. }
  161. void DebugHud::SetMode(unsigned mode)
  162. {
  163. statsText_->SetVisible((mode & DEBUGHUD_SHOW_STATS) != 0);
  164. modeText_->SetVisible((mode & DEBUGHUD_SHOW_MODE) != 0);
  165. profilerText_->SetVisible((mode & DEBUGHUD_SHOW_PROFILER) != 0);
  166. mode_ = mode;
  167. }
  168. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  169. {
  170. profilerMaxDepth_ = depth;
  171. }
  172. void DebugHud::SetProfilerInterval(float interval)
  173. {
  174. profilerInterval_ = Max((int)(interval * 1000.0f), 0);
  175. }
  176. void DebugHud::SetUseRendererStats(bool enable)
  177. {
  178. useRendererStats_ = enable;
  179. }
  180. void DebugHud::Toggle(unsigned mode)
  181. {
  182. SetMode(GetMode() ^ mode);
  183. }
  184. void DebugHud::ToggleAll()
  185. {
  186. Toggle(DEBUGHUD_SHOW_ALL);
  187. }
  188. XMLFile* DebugHud::GetDefaultStyle() const
  189. {
  190. return statsText_->GetDefaultStyle(false);
  191. }
  192. float DebugHud::GetProfilerInterval() const
  193. {
  194. return (float)profilerInterval_ / 1000.0f;
  195. }
  196. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  197. {
  198. SetAppStats(label, stats.ToString());
  199. }
  200. void DebugHud::SetAppStats(const String& label, const String& stats)
  201. {
  202. bool newLabel = !appStats_.Contains(label);
  203. appStats_[label] = stats;
  204. if (newLabel)
  205. appStats_.Sort();
  206. }
  207. bool DebugHud::ResetAppStats(const String& label)
  208. {
  209. return appStats_.Erase(label);
  210. }
  211. void DebugHud::ClearAppStats()
  212. {
  213. appStats_.Clear();
  214. }
  215. void DebugHud::HandleUpdate(StringHash eventType, VariantMap& eventData)
  216. {
  217. using namespace Update;
  218. Update();
  219. }
  220. }