Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
8.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "perfvisualbenchmark.h"
  9. #include "KeyValues.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. #define VAR 0
  13. #define ON 1
  14. #define OFF 2
  15. #define DETAILS 3
  16. static CPerfVisualBenchmark s_PerfVisualBenchmark; // singleton
  17. IGameSystem* PerfVisualBenchmark() { return &s_PerfVisualBenchmark; }
  18. #ifndef _XBOX
  19. extern ConVar cl_mouseenable;
  20. #endif
  21. void usrCmd_Start()
  22. {
  23. s_PerfVisualBenchmark.Start();
  24. }
  25. void usrCmd_Abort()
  26. {
  27. s_PerfVisualBenchmark.Stop();
  28. }
  29. static ConCommand perfvisualbenchmark("perfvisualbenchmark", usrCmd_Start);
  30. static ConCommand perfvisualbenchmark_abort("perfvisualbenchmark_abort", usrCmd_Abort);
  31. CPerfVisualBenchmark::CPerfVisualBenchmark()
  32. {
  33. }
  34. CPerfVisualBenchmark::~CPerfVisualBenchmark()
  35. {
  36. }
  37. bool CPerfVisualBenchmark::Init()
  38. {
  39. RunInfo_t runInfo;
  40. runInfo.m_pVarName = "";
  41. runInfo.m_pOnVal = "";
  42. runInfo.m_pOffVal = "";
  43. runInfo.m_pDescription = "Default";
  44. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  45. m_RunInfo.AddToTail( runInfo );
  46. runInfo.m_pVarName = "r_drawdetailprops";
  47. runInfo.m_pOnVal = "0";
  48. runInfo.m_pOffVal = "1";
  49. runInfo.m_pDescription = "detail props";
  50. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  51. m_RunInfo.AddToTail( runInfo );
  52. runInfo.m_pVarName = "r_drawworld";
  53. runInfo.m_pOnVal = "0";
  54. runInfo.m_pOffVal = "1";
  55. runInfo.m_pDescription = "world geometry";
  56. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  57. m_RunInfo.AddToTail( runInfo );
  58. runInfo.m_pVarName = "r_drawentities";
  59. runInfo.m_pOnVal = "0";
  60. runInfo.m_pOffVal = "1";
  61. runInfo.m_pDescription = "entities";
  62. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  63. m_RunInfo.AddToTail( runInfo );
  64. runInfo.m_pVarName = "r_3dsky";
  65. runInfo.m_pOnVal = "0";
  66. runInfo.m_pOffVal = "1";
  67. runInfo.m_pDescription = "3D skybox";
  68. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  69. m_RunInfo.AddToTail( runInfo );
  70. runInfo.m_pVarName = "r_drawdecals";
  71. runInfo.m_pOnVal = "0";
  72. runInfo.m_pOffVal = "1";
  73. runInfo.m_pDescription = "decals";
  74. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  75. m_RunInfo.AddToTail( runInfo );
  76. runInfo.m_pVarName = "mat_stub";
  77. runInfo.m_pOnVal = "1";
  78. runInfo.m_pOffVal = "0";
  79. runInfo.m_pDescription = "material system and below";
  80. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  81. m_RunInfo.AddToTail( runInfo );
  82. runInfo.m_pVarName = "mat_viewportscale";
  83. runInfo.m_pOnVal = ".2";
  84. runInfo.m_pOffVal = "1.0";
  85. runInfo.m_pDescription = "fillrate";
  86. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  87. m_RunInfo.AddToTail( runInfo );
  88. runInfo.m_pVarName = "r_drawstaticprops";
  89. runInfo.m_pOnVal = "0";
  90. runInfo.m_pOffVal = "1";
  91. runInfo.m_pDescription = "fillrate";
  92. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  93. m_RunInfo.AddToTail( runInfo );
  94. runInfo.m_pVarName = "r_drawbrushmodels";
  95. runInfo.m_pOnVal = "0";
  96. runInfo.m_pOffVal = "1";
  97. runInfo.m_pDescription = "brush models";
  98. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  99. m_RunInfo.AddToTail( runInfo );
  100. runInfo.m_pVarName = "r_renderoverlayfragment";
  101. runInfo.m_pOnVal = "0";
  102. runInfo.m_pOffVal = "1";
  103. runInfo.m_pDescription = "overlays";
  104. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  105. m_RunInfo.AddToTail( runInfo );
  106. runInfo.m_pVarName = "r_drawdisp";
  107. runInfo.m_pOnVal = "0";
  108. runInfo.m_pOffVal = "1";
  109. runInfo.m_pDescription = "displacements";
  110. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  111. m_RunInfo.AddToTail( runInfo );
  112. runInfo.m_pVarName = "r_drawviewmodel";
  113. runInfo.m_pOnVal = "0";
  114. runInfo.m_pOffVal = "1";
  115. runInfo.m_pDescription = "viewmodel";
  116. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  117. m_RunInfo.AddToTail( runInfo );
  118. runInfo.m_pVarName = "cl_drawhud";
  119. runInfo.m_pOnVal = "0";
  120. runInfo.m_pOffVal = "1";
  121. runInfo.m_pDescription = "hud";
  122. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  123. m_RunInfo.AddToTail( runInfo );
  124. runInfo.m_pVarName = "r_drawparticles";
  125. runInfo.m_pOnVal = "0";
  126. runInfo.m_pOffVal = "1";
  127. runInfo.m_pDescription = "particles";
  128. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  129. m_RunInfo.AddToTail( runInfo );
  130. runInfo.m_pVarName = "r_drawsprites";
  131. runInfo.m_pOnVal = "0";
  132. runInfo.m_pOffVal = "1";
  133. runInfo.m_pDescription = "sprites";
  134. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  135. m_RunInfo.AddToTail( runInfo );
  136. runInfo.m_pVarName = "mat_bumpmap";
  137. runInfo.m_pOnVal = "0";
  138. runInfo.m_pOffVal = "1";
  139. runInfo.m_pDescription = "bump mapping";
  140. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME_RELOAD_MATERIALS;
  141. m_RunInfo.AddToTail( runInfo );
  142. runInfo.m_pVarName = "mat_specular";
  143. runInfo.m_pOnVal = "0";
  144. runInfo.m_pOffVal = "1";
  145. runInfo.m_pDescription = "specularity";
  146. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME_RELOAD_MATERIALS;
  147. m_RunInfo.AddToTail( runInfo );
  148. runInfo.m_pVarName = "mat_drawwater";
  149. runInfo.m_pOnVal = "0";
  150. runInfo.m_pOffVal = "1";
  151. runInfo.m_pDescription = "water";
  152. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  153. m_RunInfo.AddToTail( runInfo );
  154. runInfo.m_pVarName = "r_dynamic";
  155. runInfo.m_pOnVal = "0";
  156. runInfo.m_pOffVal = "1";
  157. runInfo.m_pDescription = "dynamic lighting";
  158. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  159. m_RunInfo.AddToTail( runInfo );
  160. runInfo.m_pVarName = "r_shadows";
  161. runInfo.m_pOnVal = "0";
  162. runInfo.m_pOffVal = "1";
  163. runInfo.m_pDescription = "shadows";
  164. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  165. m_RunInfo.AddToTail( runInfo );
  166. runInfo.m_pVarName = "r_drawropes";
  167. runInfo.m_pOnVal = "0";
  168. runInfo.m_pOffVal = "1";
  169. runInfo.m_pDescription = "ropes";
  170. runInfo.m_flStabilizeTime = FPS_STABILIZE_TIME;
  171. m_RunInfo.AddToTail( runInfo );
  172. m_bIsOn = false; //is this thing on?
  173. return true;
  174. }
  175. void CPerfVisualBenchmark::Start()
  176. {
  177. #ifndef _XBOX
  178. m_bSaveMouseEnable = cl_mouseenable.GetBool();
  179. cl_mouseenable.SetValue( 0 );
  180. #endif
  181. m_iCurVar = 0;
  182. m_flTimer = gpGlobals->realtime + FPS_STABILIZE_TIME;
  183. m_bWaiting = true;
  184. m_bIsOn = true; // showtime!
  185. engine->ClientCmd_Unrestricted("cancelselect"); // exit menu and console
  186. // engine->ClientCmd_Unrestricted("wait");
  187. // engine->ClientCmd_Unrestricted("setpause"); // pause the mofo
  188. engine->ClientCmd_Unrestricted("host_timescale 0.0001"); // pause the mofo
  189. }
  190. void CPerfVisualBenchmark::Stop()
  191. {
  192. #ifndef _XBOX
  193. cl_mouseenable.SetValue( m_bSaveMouseEnable );
  194. #endif
  195. m_bIsOn = false;
  196. Print();
  197. engine->ClientCmd_Unrestricted("host_timescale 0"); // pause the mofo
  198. // engine->ClientCmd_Unrestricted("unpause"); // unpause the mofo
  199. // engine->ClientCmd_Unrestricted("wait");
  200. engine->ClientCmd_Unrestricted("toggleconsole");
  201. }
  202. void CPerfVisualBenchmark::PreRender( )
  203. {
  204. if (!m_bIsOn)
  205. return;
  206. // Wait for the timer
  207. if ( m_flTimer > gpGlobals->realtime )
  208. return;
  209. if ( m_bWaiting )
  210. {
  211. m_flTimer = gpGlobals->realtime + FPS_MEASURE_TIME;
  212. m_flStartMeasureTime = gpGlobals->realtime;
  213. m_nStartFrameCount = gpGlobals->framecount;
  214. m_bWaiting = false;
  215. return;
  216. }
  217. // Ok, we were measuring, lets calculate the results
  218. float flDenom = gpGlobals->realtime - m_flStartMeasureTime;
  219. if (flDenom == 0)
  220. {
  221. flDenom = 1.0f;
  222. }
  223. // note the current avged fps;
  224. float flAveFPS = (gpGlobals->framecount - m_nStartFrameCount) / flDenom;
  225. m_RunInfo[m_iCurVar].m_flFPS = flAveFPS;
  226. m_flTimer = gpGlobals->realtime + FPS_STABILIZE_TIME;
  227. m_bWaiting = true;
  228. char combuffer[255];
  229. // Turn off any previous value
  230. if ( m_RunInfo[m_iCurVar].m_pVarName )
  231. {
  232. Q_snprintf(combuffer, sizeof(combuffer), "%s %s\n", m_RunInfo[m_iCurVar].m_pVarName, m_RunInfo[m_iCurVar].m_pOffVal ); //turn off current var
  233. engine->ClientCmd_Unrestricted(combuffer);
  234. }
  235. // next var
  236. m_iCurVar++;
  237. if (m_iCurVar == m_RunInfo.Count())
  238. {
  239. Stop();
  240. return;
  241. }
  242. Q_snprintf(combuffer, sizeof(combuffer), "%s %s\n",m_RunInfo[m_iCurVar].m_pVarName, m_RunInfo[m_iCurVar].m_pOnVal); //turn on next var
  243. engine->ClientCmd_Unrestricted(combuffer);
  244. }
  245. void CPerfVisualBenchmark::Print() // sort and print into console
  246. {
  247. for (int i = 0; i<m_RunInfo.Count(); i++)
  248. {
  249. int curMax = 0;
  250. for (int j = 0; j<m_RunInfo.Count(); j++)
  251. {
  252. if (m_RunInfo[j].m_flFPS > m_RunInfo[curMax].m_flFPS)
  253. {
  254. curMax = j;
  255. }
  256. }
  257. Msg("%.0f fps - %s\n",m_RunInfo[curMax].m_flFPS, m_RunInfo[curMax].m_pDescription);
  258. m_RunInfo[curMax].m_flFPS=-1;
  259. }
  260. }