DebugHud.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. 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. SystemUI* ui = GetSubsystem<SystemUI>();
  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. SubscribeToEvent(E_POSTUPDATE, HANDLER(DebugHud, HandlePostUpdate));
  75. }
  76. DebugHud::~DebugHud()
  77. {
  78. statsText_->Remove();
  79. modeText_->Remove();
  80. profilerText_->Remove();
  81. }
  82. void DebugHud::Update()
  83. {
  84. Graphics* graphics = GetSubsystem<Graphics>();
  85. Renderer* renderer = GetSubsystem<Renderer>();
  86. if (!renderer || !graphics)
  87. return;
  88. // Ensure UI-elements are not detached
  89. if (!statsText_->GetParent())
  90. {
  91. SystemUI* ui = GetSubsystem<SystemUI>();
  92. UIElement* uiRoot = ui->GetRoot();
  93. uiRoot->AddChild(statsText_);
  94. uiRoot->AddChild(modeText_);
  95. uiRoot->AddChild(profilerText_);
  96. }
  97. if (statsText_->IsVisible())
  98. {
  99. unsigned primitives, batches;
  100. if (!useRendererStats_)
  101. {
  102. primitives = graphics->GetNumPrimitives();
  103. batches = graphics->GetNumBatches();
  104. }
  105. else
  106. {
  107. primitives = renderer->GetNumPrimitives();
  108. batches = renderer->GetNumBatches();
  109. }
  110. String stats;
  111. stats.AppendWithFormat("Triangles %u\nBatches %u\nViews %u\nLights %u\nShadowmaps %u\nOccluders %u",
  112. primitives,
  113. batches,
  114. renderer->GetNumViews(),
  115. renderer->GetNumLights(true),
  116. renderer->GetNumShadowMaps(true),
  117. renderer->GetNumOccluders(true));
  118. if (!appStats_.Empty())
  119. {
  120. stats.Append("\n");
  121. for (HashMap<String, String>::ConstIterator i = appStats_.Begin(); i != appStats_.End(); ++i)
  122. stats.AppendWithFormat("\n%s %s", i->first_.CString(), i->second_.CString());
  123. }
  124. statsText_->SetText(stats);
  125. }
  126. if (modeText_->IsVisible())
  127. {
  128. String mode;
  129. mode.AppendWithFormat("Tex:%s Mat:%s Spec:%s Shadows:%s Size:%i Quality:%s Occlusion:%s Instancing:%s API:%s",
  130. qualityTexts[renderer->GetTextureQuality()],
  131. qualityTexts[renderer->GetMaterialQuality()],
  132. renderer->GetSpecularLighting() ? "On" : "Off",
  133. renderer->GetDrawShadows() ? "On" : "Off",
  134. renderer->GetShadowMapSize(),
  135. shadowQualityTexts[renderer->GetShadowQuality()],
  136. renderer->GetMaxOccluderTriangles() > 0 ? "On" : "Off",
  137. renderer->GetDynamicInstancing() ? "On" : "Off",
  138. graphics->GetApiName().CString());
  139. modeText_->SetText(mode);
  140. }
  141. Profiler* profiler = GetSubsystem<Profiler>();
  142. if (profiler)
  143. {
  144. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  145. {
  146. profilerTimer_.Reset();
  147. if (profilerText_->IsVisible())
  148. {
  149. String profilerOutput = profiler->GetData(false, false, profilerMaxDepth_);
  150. profilerText_->SetText(profilerOutput);
  151. }
  152. profiler->BeginInterval();
  153. }
  154. }
  155. }
  156. void DebugHud::SetDefaultStyle(XMLFile* style)
  157. {
  158. if (!style)
  159. return;
  160. statsText_->SetDefaultStyle(style);
  161. statsText_->SetStyle("DebugHudText");
  162. modeText_->SetDefaultStyle(style);
  163. modeText_->SetStyle("DebugHudText");
  164. profilerText_->SetDefaultStyle(style);
  165. profilerText_->SetStyle("DebugHudText");
  166. }
  167. void DebugHud::SetMode(unsigned mode)
  168. {
  169. statsText_->SetVisible((mode & DEBUGHUD_SHOW_STATS) != 0);
  170. modeText_->SetVisible((mode & DEBUGHUD_SHOW_MODE) != 0);
  171. profilerText_->SetVisible((mode & DEBUGHUD_SHOW_PROFILER) != 0);
  172. mode_ = mode;
  173. }
  174. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  175. {
  176. profilerMaxDepth_ = depth;
  177. }
  178. void DebugHud::SetProfilerInterval(float interval)
  179. {
  180. profilerInterval_ = (unsigned)Max((int)(interval * 1000.0f), 0);
  181. }
  182. void DebugHud::SetUseRendererStats(bool enable)
  183. {
  184. useRendererStats_ = enable;
  185. }
  186. void DebugHud::Toggle(unsigned mode)
  187. {
  188. SetMode(GetMode() ^ mode);
  189. }
  190. void DebugHud::ToggleAll()
  191. {
  192. Toggle(DEBUGHUD_SHOW_ALL);
  193. }
  194. XMLFile* DebugHud::GetDefaultStyle() const
  195. {
  196. return statsText_->GetDefaultStyle(false);
  197. }
  198. float DebugHud::GetProfilerInterval() const
  199. {
  200. return (float)profilerInterval_ / 1000.0f;
  201. }
  202. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  203. {
  204. SetAppStats(label, stats.ToString());
  205. }
  206. void DebugHud::SetAppStats(const String& label, const String& stats)
  207. {
  208. bool newLabel = !appStats_.Contains(label);
  209. appStats_[label] = stats;
  210. if (newLabel)
  211. appStats_.Sort();
  212. }
  213. bool DebugHud::ResetAppStats(const String& label)
  214. {
  215. return appStats_.Erase(label);
  216. }
  217. void DebugHud::ClearAppStats()
  218. {
  219. appStats_.Clear();
  220. }
  221. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  222. {
  223. using namespace PostUpdate;
  224. Update();
  225. }
  226. }
  227. }