DebugHud.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #ifdef __disabled
  2. //
  3. // Copyright (c) 2008-2014 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 "../Engine/DebugHud.h"
  26. #include "../Engine/Engine.h"
  27. #include "../Graphics/Graphics.h"
  28. #include "../IO/Log.h"
  29. #include "../Core/Profiler.h"
  30. #include "../Graphics/Renderer.h"
  31. #include "../UI/UI.h"
  32. #include "../DebugNew.h"
  33. namespace Atomic
  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 Mode:%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. #ifdef ATOMIC_OPENGL
  137. "OGL");
  138. #else
  139. graphics->GetSM3Support() ? "SM3" : "SM2");
  140. #endif
  141. modeText_->SetText(mode);
  142. }
  143. Profiler* profiler = GetSubsystem<Profiler>();
  144. if (profiler)
  145. {
  146. if (profilerTimer_.GetMSec(false) >= profilerInterval_)
  147. {
  148. profilerTimer_.Reset();
  149. if (profilerText_->IsVisible())
  150. {
  151. String profilerOutput = profiler->GetData(false, false, profilerMaxDepth_);
  152. profilerText_->SetText(profilerOutput);
  153. }
  154. profiler->BeginInterval();
  155. }
  156. }
  157. }
  158. void DebugHud::SetDefaultStyle(XMLFile* style)
  159. {
  160. if (!style)
  161. return;
  162. statsText_->SetDefaultStyle(style);
  163. statsText_->SetStyle("DebugHudText");
  164. modeText_->SetDefaultStyle(style);
  165. modeText_->SetStyle("DebugHudText");
  166. profilerText_->SetDefaultStyle(style);
  167. profilerText_->SetStyle("DebugHudText");
  168. }
  169. void DebugHud::SetMode(unsigned mode)
  170. {
  171. statsText_->SetVisible((mode & DEBUGHUD_SHOW_STATS) != 0);
  172. modeText_->SetVisible((mode & DEBUGHUD_SHOW_MODE) != 0);
  173. profilerText_->SetVisible((mode & DEBUGHUD_SHOW_PROFILER) != 0);
  174. mode_ = mode;
  175. }
  176. void DebugHud::SetProfilerMaxDepth(unsigned depth)
  177. {
  178. profilerMaxDepth_ = depth;
  179. }
  180. void DebugHud::SetProfilerInterval(float interval)
  181. {
  182. profilerInterval_ = Max((int)(interval * 1000.0f), 0);
  183. }
  184. void DebugHud::SetUseRendererStats(bool enable)
  185. {
  186. useRendererStats_ = enable;
  187. }
  188. void DebugHud::Toggle(unsigned mode)
  189. {
  190. SetMode(GetMode() ^ mode);
  191. }
  192. void DebugHud::ToggleAll()
  193. {
  194. Toggle(DEBUGHUD_SHOW_ALL);
  195. }
  196. XMLFile* DebugHud::GetDefaultStyle() const
  197. {
  198. return statsText_->GetDefaultStyle(false);
  199. }
  200. float DebugHud::GetProfilerInterval() const
  201. {
  202. return (float)profilerInterval_ / 1000.0f;
  203. }
  204. void DebugHud::SetAppStats(const String& label, const Variant& stats)
  205. {
  206. SetAppStats(label, stats.ToString());
  207. }
  208. void DebugHud::SetAppStats(const String& label, const String& stats)
  209. {
  210. bool newLabel = !appStats_.Contains(label);
  211. appStats_[label] = stats;
  212. if (newLabel)
  213. appStats_.Sort();
  214. }
  215. bool DebugHud::ResetAppStats(const String& label)
  216. {
  217. return appStats_.Erase(label);
  218. }
  219. void DebugHud::ClearAppStats()
  220. {
  221. appStats_.Clear();
  222. }
  223. void DebugHud::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  224. {
  225. using namespace PostUpdate;
  226. Update();
  227. }
  228. }
  229. #endif