DebugHud.cpp 7.8 KB

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