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.

221 lines
6.2 KiB

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