DebugHud.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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 "CoreEvents.h"
  25. #include "DebugHud.h"
  26. #include "Engine.h"
  27. #include "Font.h"
  28. #include "Graphics.h"
  29. #include "Log.h"
  30. #include "Profiler.h"
  31. #include "Renderer.h"
  32. #include "StringUtils.h"
  33. #include "Text.h"
  34. #include "UI.h"
  35. #include "DebugNew.h"
  36. OBJECTTYPESTATIC(DebugHud);
  37. DebugHud::DebugHud(Context* context) :
  38. Object(context),
  39. profilerInterval_(1.0f),
  40. profilerTimer_(0.0f),
  41. useRendererStats_(false)
  42. {
  43. UI* ui = GetSubsystem<UI>();
  44. UIElement* uiRoot = ui->GetRootElement();
  45. statsText_ = new Text(context_);
  46. statsText_->SetAlignment(HA_LEFT, VA_TOP);
  47. statsText_->SetPriority(100);
  48. statsText_->SetVisible(false);
  49. uiRoot->AddChild(statsText_);
  50. modeText_ = new Text(context_);
  51. modeText_->SetAlignment(HA_LEFT, VA_BOTTOM);
  52. modeText_->SetPriority(100);
  53. modeText_->SetVisible(false);
  54. uiRoot->AddChild(modeText_);
  55. profilerText_ = new Text(context_);
  56. profilerText_->SetAlignment(HA_RIGHT, VA_TOP);
  57. profilerText_->SetPriority(100);
  58. profilerText_->SetVisible(false);
  59. uiRoot->AddChild(profilerText_);
  60. SubscribeToEvent(E_UPDATE, HANDLER(DebugHud, HandleUpdate));
  61. }
  62. DebugHud::~DebugHud()
  63. {
  64. UI* ui = GetSubsystem<UI>();
  65. if (ui)
  66. {
  67. UIElement* uiRoot = ui->GetRootElement();
  68. uiRoot->RemoveChild(statsText_);
  69. uiRoot->RemoveChild(modeText_);
  70. uiRoot->RemoveChild(profilerText_);
  71. }
  72. }
  73. void DebugHud::Update(float timeStep)
  74. {
  75. Graphics* graphics = GetSubsystem<Graphics>();
  76. Renderer* renderer = GetSubsystem<Renderer>();
  77. if ((!renderer) || (!graphics))
  78. return;
  79. unsigned primitives, batches;
  80. if (!useRendererStats_)
  81. {
  82. primitives = graphics->GetNumPrimitives();
  83. batches = graphics->GetNumBatches();
  84. }
  85. else
  86. {
  87. primitives = renderer->GetNumPrimitives();
  88. batches = renderer->GetNumBatches();
  89. }
  90. if (statsText_->IsVisible())
  91. {
  92. String stats =
  93. "Triangles " + ToString(graphics->GetNumPrimitives()) +
  94. "\nBatches " + ToString(graphics->GetNumBatches()) +
  95. "\nViews " + ToString(renderer->GetNumViews()) +
  96. "\nLights " + ToString(renderer->GetNumLights(true)) +
  97. "\nShadowmaps " + ToString(renderer->GetNumShadowMaps(true)) +
  98. "\nOccluders " + ToString(renderer->GetNumOccluders(true) + renderer->GetNumShadowOccluders(true));
  99. statsText_->SetText(stats);
  100. }
  101. if (modeText_->IsVisible())
  102. {
  103. String mode;
  104. RenderMode renderMode = graphics->GetRenderMode();
  105. if (renderMode == RENDER_FORWARD)
  106. mode = "Forward";
  107. else if (renderMode == RENDER_PREPASS)
  108. mode = "Prepass";
  109. else
  110. mode = "Deferred";
  111. int textureQuality = renderer->GetTextureQuality();
  112. mode += " Tex:";
  113. if (textureQuality == 0)
  114. mode += "Low";
  115. if (textureQuality == 1)
  116. mode += "Med";
  117. if (textureQuality == 2)
  118. mode += "High";
  119. int materialQuality = renderer->GetMaterialQuality();
  120. mode +=" Mat:";
  121. if (materialQuality == 0)
  122. mode += "Low";
  123. if (materialQuality == 1)
  124. mode += "Med";
  125. if (materialQuality == 2)
  126. mode += "High";
  127. bool specular = renderer->GetSpecularLighting();
  128. mode += " Spec:";
  129. if (specular)
  130. mode += "On";
  131. else
  132. mode += "Off";
  133. bool shadows = renderer->GetDrawShadows();
  134. mode += " Shadows:";
  135. if (shadows)
  136. mode += "On";
  137. else
  138. mode += "Off";
  139. int shadowMapSize = renderer->GetShadowMapSize();
  140. mode += " Size:" + ToString(shadowMapSize);
  141. bool shadowMapHiresDepth = renderer->GetShadowMapHiresDepth();
  142. mode += " Depth:";
  143. if (shadowMapHiresDepth)
  144. mode += "24";
  145. else
  146. mode += "16";
  147. bool occlusion = renderer->GetMaxOccluderTriangles() > 0;
  148. mode += " Occlusion:";
  149. if (occlusion)
  150. mode += "On";
  151. else
  152. mode += "Off";
  153. bool instancing = renderer->GetDynamicInstancing();
  154. mode += " Instancing:";
  155. if (instancing)
  156. mode += "On";
  157. else
  158. mode += "Off";
  159. bool sm3 = graphics->GetSM3Support();
  160. mode += " Mode:";
  161. if (sm3)
  162. mode += "SM3";
  163. else
  164. mode += "SM2";
  165. modeText_->SetText(mode);
  166. }
  167. Profiler* profiler = GetSubsystem<Profiler>();
  168. if (profiler)
  169. {
  170. profilerTimer_ += timeStep;
  171. if (profilerTimer_ >= profilerInterval_)
  172. {
  173. if (profilerText_->IsVisible())
  174. {
  175. String profilerOutput = profiler->GetData(false, true, false);
  176. profilerText_->SetText(profilerOutput);
  177. }
  178. profilerTimer_ -= profilerInterval_;
  179. profiler->ClearAccumulated();
  180. }
  181. }
  182. }
  183. void DebugHud::SetStyle(XMLFile* style)
  184. {
  185. if (!style)
  186. return;
  187. style_ = style;
  188. statsText_->SetStyle(style, "DebugHudText");
  189. modeText_->SetStyle(style, "DebugHudText");
  190. profilerText_->SetStyle(style, "DebugHudText");
  191. }
  192. void DebugHud::SetMode(unsigned mode)
  193. {
  194. statsText_->SetVisible((mode & DEBUGHUD_SHOW_STATS) != 0);
  195. modeText_->SetVisible((mode & DEBUGHUD_SHOW_MODE) != 0);
  196. profilerText_->SetVisible((mode & DEBUGHUD_SHOW_PROFILER) != 0);
  197. // If profiler text is made visible, force update
  198. if (profilerText_->IsVisible())
  199. profilerTimer_ = profilerInterval_;
  200. }
  201. void DebugHud::SetProfilerInterval(float interval)
  202. {
  203. profilerInterval_ = Max(interval, 0.0f);
  204. }
  205. void DebugHud::SetUseRendererStats(bool enable)
  206. {
  207. useRendererStats_ = enable;
  208. }
  209. void DebugHud::Toggle(unsigned mode)
  210. {
  211. SetMode(GetMode() ^ mode);
  212. }
  213. void DebugHud::ToggleAll()
  214. {
  215. Toggle(DEBUGHUD_SHOW_STATS | DEBUGHUD_SHOW_MODE | DEBUGHUD_SHOW_PROFILER);
  216. }
  217. unsigned DebugHud::GetMode() const
  218. {
  219. unsigned mode = DEBUGHUD_SHOW_NONE;
  220. if (statsText_->IsVisible())
  221. mode |= DEBUGHUD_SHOW_STATS;
  222. if (modeText_->IsVisible())
  223. mode |= DEBUGHUD_SHOW_MODE;
  224. if (profilerText_->IsVisible())
  225. mode |= DEBUGHUD_SHOW_PROFILER;
  226. return mode;
  227. }
  228. void DebugHud::HandleUpdate(StringHash eventType, VariantMap& eventData)
  229. {
  230. using namespace Update;
  231. Update(eventData[P_TIMESTEP].GetFloat());
  232. }