Counter Strike : Global Offensive Source Code
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.

308 lines
8.2 KiB

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