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.

225 lines
6.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "vgui_BudgetFPSPanel.H"
  7. #include "vgui_BudgetPanel.h"
  8. #include <vgui/ISurface.h>
  9. #include "tier0/vprof.h"
  10. #include "materialsystem/imaterialsystem.h"
  11. // NOTE: This has to be the last file included!
  12. #include "tier0/memdbgon.h"
  13. static ConVar budget_peaks_window( "budget_peaks_window", "30", FCVAR_ARCHIVE, "number of frames to look at when figuring out peak frametimes" );
  14. static ConVar budget_averages_window( "budget_averages_window", "30", FCVAR_ARCHIVE, "number of frames to look at when figuring out average frametimes" );
  15. static ConVar budget_show_peaks( "budget_show_peaks", "1", FCVAR_ARCHIVE, "enable/disable peaks in the budget panel" );
  16. static ConVar budget_show_averages( "budget_show_averages", "0", FCVAR_ARCHIVE, "enable/disable averages in the budget panel" );
  17. CBudgetFPSPanel::CBudgetFPSPanel( vgui::Panel *pParent, const char *pPanelName ) : BaseClass( pParent, pPanelName )
  18. {
  19. SetProportional( false );
  20. SetKeyBoardInputEnabled( false );
  21. SetMouseInputEnabled( false );
  22. SetSizeable( false );
  23. SetVisible( true );
  24. SetPaintBackgroundEnabled( false );
  25. // hide the system buttons
  26. SetTitleBarVisible( false );
  27. }
  28. CBudgetFPSPanel::~CBudgetFPSPanel()
  29. {
  30. }
  31. #define PIXELS_BETWEEN_BARS 4
  32. void CBudgetFPSPanel::DrawBarAtIndex( int id, float percent )
  33. {
  34. int panelWidth, panelHeight;
  35. GetSize( panelWidth, panelHeight );
  36. int nGroups = g_VProfCurrentProfile.GetNumBudgetGroups();
  37. int top = 2 + ( id * panelHeight ) / nGroups;
  38. int bottom = ( ( ( id + 1 ) * panelHeight ) / nGroups ) - PIXELS_BETWEEN_BARS;
  39. int left = 0;
  40. int right = panelWidth * percent;
  41. int red, green, blue, alpha;
  42. g_VProfCurrentProfile.GetBudgetGroupColor( id, red, green, blue, alpha );
  43. // DrawFilledRect is panel relative
  44. vgui::surface()->DrawSetColor( 0, 0, 0, alpha );
  45. vgui::surface()->DrawFilledRect( left, top-2, right+2, bottom+2 );
  46. vgui::surface()->DrawSetColor( 255, 255, 255, alpha );
  47. vgui::surface()->DrawFilledRect( left, top-1, right+1, bottom+1 );
  48. vgui::surface()->DrawSetColor( red, green, blue, alpha );
  49. vgui::surface()->DrawFilledRect( left, top, right, bottom );
  50. }
  51. void CBudgetFPSPanel::DrawTickAtIndex( int id, float percent, int red, int green, int blue, int alpha )
  52. {
  53. if( percent > 1.0f )
  54. {
  55. percent = 1.0f;
  56. }
  57. int panelWidth, panelHeight;
  58. GetSize( panelWidth, panelHeight );
  59. int nGroups = g_VProfCurrentProfile.GetNumBudgetGroups();
  60. int top = 2 + ( id * panelHeight ) / nGroups;
  61. int bottom = ( ( ( id + 1 ) * panelHeight ) / nGroups ) - PIXELS_BETWEEN_BARS;
  62. int right = ( int )( panelWidth * percent + 1.0f );
  63. int left = right - 2;
  64. // DrawFilledRect is panel relative
  65. vgui::surface()->DrawSetColor( 0, 0, 0, alpha );
  66. vgui::surface()->DrawFilledRect( left-2, top-2, right+2, bottom+2 );
  67. vgui::surface()->DrawSetColor( 255, 255, 255, alpha );
  68. vgui::surface()->DrawFilledRect( left-1, top-1, right+1, bottom+1 );
  69. vgui::surface()->DrawSetColor( red, green, blue, alpha );
  70. vgui::surface()->DrawFilledRect( left, top, right, bottom );
  71. }
  72. void CBudgetFPSPanel::DrawTimeLines( void )
  73. {
  74. int panelWidth, panelHeight;
  75. GetSize( panelWidth, panelHeight );
  76. int i;
  77. int left, right, top, bottom;
  78. top = 0;
  79. bottom = panelHeight;
  80. for( i = ( int )m_fRangeMin; i < ( int )( m_fRangeMax + 0.5f ); i++ )
  81. {
  82. int alpha;
  83. if( i % 10 == 0 )
  84. {
  85. alpha = 150;
  86. }
  87. else if( i % 5 == 0 )
  88. {
  89. alpha = 100;
  90. }
  91. else
  92. {
  93. alpha = 50;
  94. }
  95. left = -0.5f + panelWidth * ( ( float )i - m_fRangeMin ) / ( m_fRangeMax - m_fRangeMin );
  96. right = left + 1;
  97. vgui::surface()->DrawSetColor( 0, 0, 0, alpha );
  98. vgui::surface()->DrawFilledRect( left-1, top, right+1, bottom );
  99. vgui::surface()->DrawSetColor( 255, 255, 255, alpha );
  100. vgui::surface()->DrawFilledRect( left, top+1, right, bottom-1 );
  101. }
  102. }
  103. void CBudgetFPSPanel::DrawInstantaneous()
  104. {
  105. int nGroups, nSamplesPerGroup, nSampleOffset;
  106. const double *pBudgetGroupTimes = GetBudgetPanel()->GetBudgetGroupData( nGroups, nSamplesPerGroup, nSampleOffset );
  107. if( !pBudgetGroupTimes )
  108. {
  109. return;
  110. }
  111. int i;
  112. for( i = 0; i < nGroups; i++ )
  113. {
  114. DrawBarAtIndex( i, ( pBudgetGroupTimes[nSamplesPerGroup * i + nSampleOffset] - m_fRangeMin ) / ( m_fRangeMax - m_fRangeMin ) );
  115. }
  116. }
  117. void CBudgetFPSPanel::DrawPeaks()
  118. {
  119. int nGroups, nSamplesPerGroup, nSampleOffset;
  120. const double *pBudgetGroupTimes = GetBudgetPanel()->GetBudgetGroupData( nGroups, nSamplesPerGroup, nSampleOffset );
  121. if( !pBudgetGroupTimes )
  122. {
  123. return;
  124. }
  125. int numSamples = budget_peaks_window.GetInt();
  126. int i;
  127. for( i = 0; i < nGroups; i++ )
  128. {
  129. double max = 0;
  130. int j;
  131. for( j = 0; j < numSamples; j++ )
  132. {
  133. double tmp;
  134. int offset = ( nSampleOffset - j + VPROF_HISTORY_COUNT ) % VPROF_HISTORY_COUNT;
  135. tmp = pBudgetGroupTimes[i * nSamplesPerGroup + offset];
  136. if( tmp > max )
  137. {
  138. max = tmp;
  139. }
  140. }
  141. float percent = ( max - m_fRangeMin ) / ( m_fRangeMax - m_fRangeMin );
  142. DrawTickAtIndex( i, percent, 255, 0, 0, 255 );
  143. }
  144. }
  145. void CBudgetFPSPanel::DrawAverages()
  146. {
  147. int nGroups, nSamplesPerGroup, nSampleOffset;
  148. const double *pBudgetGroupTimes = GetBudgetPanel()->GetBudgetGroupData( nGroups, nSamplesPerGroup, nSampleOffset );
  149. if( !pBudgetGroupTimes )
  150. {
  151. return;
  152. }
  153. int numSamples = budget_averages_window.GetInt();
  154. int i;
  155. for( i = 0; i < nGroups; i++ )
  156. {
  157. int red, green, blue, alpha;
  158. g_VProfCurrentProfile.GetBudgetGroupColor( i, red, green, blue, alpha );
  159. double sum = 0;
  160. int j;
  161. for( j = 0; j < numSamples; j++ )
  162. {
  163. int offset = ( nSampleOffset - j + VPROF_HISTORY_COUNT ) % VPROF_HISTORY_COUNT;
  164. sum += pBudgetGroupTimes[i * nSamplesPerGroup + offset];
  165. }
  166. sum *= ( 1.0f / numSamples );
  167. float percent = ( sum - m_fRangeMin ) / ( m_fRangeMax - m_fRangeMin );
  168. DrawTickAtIndex( i, percent, red, green, blue, alpha );
  169. }
  170. }
  171. void CBudgetFPSPanel::Paint( void )
  172. {
  173. materials->Flush();
  174. g_VProfCurrentProfile.Pause();
  175. int width, height;
  176. GetSize( width, height );
  177. DrawTimeLines();
  178. DrawInstantaneous();
  179. if( budget_show_peaks.GetBool() )
  180. {
  181. DrawPeaks();
  182. }
  183. if( budget_show_averages.GetBool() )
  184. {
  185. DrawAverages();
  186. }
  187. materials->Flush();
  188. g_VProfCurrentProfile.Resume();
  189. }
  190. void CBudgetFPSPanel::SetRange( float fMin, float fMax )
  191. {
  192. m_fRangeMin = fMin;
  193. m_fRangeMax = fMax;
  194. }