DebugHud.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #ifdef __DISABLED
  2. //
  3. // Copyright (c) 2008-2015 the Urho3D project.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "../Precompiled.h"
  24. #include "../Core/CoreEvents.h"
  25. #include "../Core/Profiler.h"
  26. #include "../Engine/DebugHud.h"
  27. #include "../Engine/Engine.h"
  28. #include "../Graphics/Graphics.h"
  29. #include "../Graphics/Renderer.h"
  30. #include "../IO/Log.h"
  31. #include "../DebugNew.h"
  32. namespace Atomic
  33. {
  34. static const char* qualityTexts[] =
  35. {
  36. "Low",
  37. "Med",
  38. "High"
  39. };
  40. static const char* shadowQualityTexts[] =
  41. {
  42. "16bit Low",
  43. "24bit Low",
  44. "16bit High",
  45. "24bit High"
  46. };
  47. DebugHud::DebugHud(Context* context) :
  48. Object(context),
  49. profilerMaxDepth_(M_MAX_UNSIGNED),
  50. profilerInterval_(1000),
  51. useRendererStats_(false),
  52. mode_(DEBUGHUD_SHOW_NONE)
  53. {
  54. UI* ui = GetSubsystem<UI>();
  55. UIElement* uiRoot = ui->GetRoot();
  56. statsText_ = new Text(context_);
  57. statsText_->SetAlignment(HA_LEFT, VA_TOP);
  58. statsText_->SetPriority(100);
  59. statsText_->SetVisible(false);
  60. uiRoot->AddChild(statsText_);
  61. modeText_ = new Text(context_);
  62. modeText_->SetAlignment(HA_LEFT, VA_BOTTOM);
  63. modeText_->SetPriority(100);
  64. modeText_->SetVisible(false);
  65. uiRoot->AddChild(modeText_);
  66. profilerText_ = new Text(context_);
  67. profilerText_->SetAlignment(HA_RIGHT, VA_TOP);
  68. profilerText_->SetPriority(100);
  69. profilerText_->SetVisible(false);
  70. uiRoot->AddChild(profilerText_);
  71. SubscribeToEvent(E_POSTUPDATE, HANDLER(DebugHud, HandlePostUpdate));
  72. }
  73. DebugHud::~DebugHud()
  74. {
  75. statsText_->Remove();
  76. modeText_->Remove();
  77. profilerText_->Remove();
  78. }
  79. void DebugHud::Update()
  80. {
  81. Graphics* graphics = GetSubsystem<Graphics>();
  82. Renderer* renderer = GetSubsystem<Renderer>();
  83. if (!renderer || !graphics)
  84. return;
  85. // Ensure UI-elements are not detached
  86. if (!statsText_->GetParent())
  87. {
  88. UI* ui = GetSubsystem<UI>();
  89. UIElement* uiRoot = ui->GetRoot();
  90. uiRoot->AddChild(statsText_);
  91. uiRoot->AddChild(modeText_);
  92. uiRoot->AddChild(profilerText_);
  93. }
  94. if (statsText_->IsVisible())
  95. {
  96. unsigned primitives, batches;
  97. if (!useRendererStats_)
  98. {
  99. primitives = graphics->GetNumPrimitives();
  100. batches = graphics->GetNumBatches();
  101. }
  102. else
  103. {
  104. primitives = renderer->GetNumPrimitives();
  105. batches = renderer->GetNumBatches();
  106. }
  107. String stats;
  108. stats.AppendWithFormat("Triangles %u\nBatches %u\nViews %u\nLights %u\nShadowmaps %u\nOccluders %u",
  109. primitives,
  110. batches,
  111. renderer->GetNumViews(),
  112. renderer->GetNumLights(true),
  113. renderer->GetNumShadowMaps(true),
  114. renderer->GetNumOccluders(true));
  115. if (!appStats_.Empty())
  116. {
  117. stats.Append("\n");
  118. for (HashMap<String, String>::ConstIterator i = appStats_.Begin(); i != appStats_.End(); ++i)
  119. stats.AppendWithFormat("\n%s %s", i->first_.CString(), i->second_.CString());
  120. }
  121. statsText_->SetText(stats);
  122. }
  123. if (modeText_->IsVisible())
  124. {
  125. String mode;
  126. mode.AppendWithFormat("Tex:%s Mat:%s Spec:%s Shadows:%s Size:%i Quality:%s Occlusion:%s Instancing:%s API:%s",
  127. qualityTexts[renderer->GetTextureQuality()],
  128. qualityTexts[renderer->GetMaterialQuality()],
  129. renderer->GetSpecularLighting() ? "On" : "Off",
  130. renderer->GetDrawShadows() ? "On" : "Off",
  131. renderer->GetShadowMapSize(),
  132. shadowQualityTexts[renderer->GetShadowQuality()],
  133. renderer->GetMaxOccluderTriangles() > 0 ? "On" : "Off",
  134. renderer->GetDynamicInstancing() ? "On" : "Off",
  135. graphics->GetApiName().CString());
  136. modeText_->SetText(mode);
  137. }
  138. Profiler* profiler = GetSubsystem<Profiler>();
  139. if (profiler)
  140. {
  141. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  142. {
  143. profilerTimer_.Reset();
  144. if (profilerText_->IsVisible())
  145. {
  146. String profilerOutput = profiler->GetData(false, false, profilerMaxDepth_);
  147. profilerText_->SetText(profilerOutput);
  148. }
  149. profiler->BeginInterval();
  150. }
  151. }
  152. }
  153. void DebugHud::SetDefaultStyle(XMLFile* style)
  154. {
  155. if (!style)
  156. return;
  157. statsText_->SetDefaultStyle(style);
  158. statsText_->SetStyle("DebugHudText");
  159. modeText_->SetDefaultStyle(style);
  160. modeText_->SetStyle("DebugHudText");
  161. profilerText_->SetDefaultStyle(style);
  162. profilerText_->SetStyle("DebugHudText");
  163. }
  164. void DebugHud::SetMode(unsigned mode)
  165. {
  166. statsText_->SetVisible((mode & DEBUGHUD_SHOW_STATS) != 0);
  167. modeText_->SetVisible((mode & DEBUGHUD_SHOW_MODE) != 0);
  168. profilerText_->SetVisible((mode & DEBUGHUD_SHOW_PROFILER) != 0);
  169. mode_ = mode;
  170. }
  171. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  172. {
  173. profilerMaxDepth_ = depth;
  174. }
  175. void DebugHud::SetProfilerInterval(float interval)
  176. {
  177. profilerInterval_ = (unsigned)Max((int)(interval * 1000.0f), 0);
  178. }
  179. void DebugHud::SetUseRendererStats(bool enable)
  180. {
  181. useRendererStats_ = enable;
  182. }
  183. void DebugHud::Toggle(unsigned mode)
  184. {
  185. SetMode(GetMode() ^ mode);
  186. }
  187. void DebugHud::ToggleAll()
  188. {
  189. Toggle(DEBUGHUD_SHOW_ALL);
  190. }
  191. XMLFile* DebugHud::GetDefaultStyle() const
  192. {
  193. return statsText_->GetDefaultStyle(false);
  194. }
  195. float DebugHud::GetProfilerInterval() const
  196. {
  197. return (float)profilerInterval_ / 1000.0f;
  198. }
  199. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  200. {
  201. SetAppStats(label, stats.ToString());
  202. }
  203. void DebugHud::SetAppStats(const String& label, const String& stats)
  204. {
  205. bool newLabel = !appStats_.Contains(label);
  206. appStats_[label] = stats;
  207. if (newLabel)
  208. appStats_.Sort();
  209. }
  210. bool DebugHud::ResetAppStats(const String& label)
  211. {
  212. return appStats_.Erase(label);
  213. }
  214. void DebugHud::ClearAppStats()
  215. {
  216. appStats_.Clear();
  217. }
  218. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  219. {
  220. using namespace PostUpdate;
  221. Update();
  222. }
  223. }
  224. #endif