ResolutionScale.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "tr_local.h"
  23. #include "ResolutionScale.h"
  24. idResolutionScale resolutionScale;
  25. static const float MINIMUM_RESOLUTION_SCALE = 0.5f;
  26. static const float MAXIMUM_RESOLUTION_SCALE = 1.0f;
  27. idCVar rs_enable( "rs_enable", "1", CVAR_INTEGER, "Enable dynamic resolution scaling, 0 - off, 1 - horz only, 2 - vert only, 3 - both" );
  28. idCVar rs_forceFractionX( "rs_forceFractionX", "0", CVAR_FLOAT, "Force a specific 0.0 to 1.0 horizontal resolution scale" );
  29. idCVar rs_forceFractionY( "rs_forceFractionY", "0", CVAR_FLOAT, "Force a specific 0.0 to 1.0 vertical resolution scale" );
  30. idCVar rs_showResolutionChanges( "rs_showResolutionChanges", "0", CVAR_INTEGER, "1 = Print whenever the resolution scale changes, 2 = always" );
  31. idCVar rs_dropMilliseconds( "rs_dropMilliseconds", "15.0", CVAR_FLOAT, "Drop the resolution when GPU time exceeds this" );
  32. idCVar rs_raiseMilliseconds( "rs_raiseMilliseconds", "13.0", CVAR_FLOAT, "Raise the resolution when GPU time is below this for several frames" );
  33. idCVar rs_dropFraction( "rs_dropFraction", "0.11", CVAR_FLOAT, "Drop the resolution in increments of this" );
  34. idCVar rs_raiseFraction( "rs_raiseFraction", "0.06", CVAR_FLOAT, "Raise the resolution in increments of this" );
  35. idCVar rs_raiseFrames( "rs_raiseFrames", "5", CVAR_INTEGER, "Require this many frames below rs_raiseMilliseconds" );
  36. idCVar rs_display( "rs_display", "0", CVAR_INTEGER, "0 - percentages, 1 - pixels per frame" );
  37. /*
  38. ========================
  39. idResolutionScale::idResolutionScale
  40. ========================
  41. */
  42. idResolutionScale::idResolutionScale() {
  43. dropMilliseconds = 15.0f;
  44. raiseMilliseconds = 13.0f;
  45. framesAboveRaise = 0;
  46. currentResolution = 1.0f;
  47. }
  48. /*
  49. ========================
  50. idResolutionScale::InitForMap
  51. ========================
  52. */
  53. void idResolutionScale::InitForMap( const char * mapName ) {
  54. dropMilliseconds = rs_dropMilliseconds.GetFloat();
  55. raiseMilliseconds = rs_raiseMilliseconds.GetFloat();
  56. }
  57. /*
  58. ========================
  59. idResolutionScale::ResetToFullResolution
  60. ========================
  61. */
  62. void idResolutionScale::ResetToFullResolution() {
  63. currentResolution = 1.0f;
  64. }
  65. /*
  66. ========================
  67. idResolutionScale::GetCurrentResolutionScale
  68. ========================
  69. */
  70. void idResolutionScale::GetCurrentResolutionScale( float & x, float & y ) {
  71. assert( currentResolution >= MINIMUM_RESOLUTION_SCALE );
  72. assert( currentResolution <= MAXIMUM_RESOLUTION_SCALE );
  73. x = MAXIMUM_RESOLUTION_SCALE;
  74. y = MAXIMUM_RESOLUTION_SCALE;
  75. switch ( rs_enable.GetInteger() ) {
  76. case 0: return;
  77. case 1: x = currentResolution; break;
  78. case 2: y = currentResolution; break;
  79. case 3: {
  80. const float middle = ( MINIMUM_RESOLUTION_SCALE + MAXIMUM_RESOLUTION_SCALE ) * 0.5f;
  81. if ( currentResolution >= middle ) {
  82. // First scale horizontally from max to min
  83. x = MINIMUM_RESOLUTION_SCALE + ( currentResolution - middle ) * 2.0f;
  84. } else {
  85. // Then scale vertically from max to min
  86. x = MINIMUM_RESOLUTION_SCALE;
  87. y = MINIMUM_RESOLUTION_SCALE + ( currentResolution - MINIMUM_RESOLUTION_SCALE ) * 2.0f;
  88. }
  89. break;
  90. }
  91. }
  92. float forceFrac = rs_forceFractionX.GetFloat();
  93. if ( forceFrac > 0.0f && forceFrac <= MAXIMUM_RESOLUTION_SCALE ) {
  94. x = forceFrac;
  95. }
  96. forceFrac = rs_forceFractionY.GetFloat();
  97. if ( forceFrac > 0.0f && forceFrac <= MAXIMUM_RESOLUTION_SCALE ) {
  98. y = forceFrac;
  99. }
  100. }
  101. /*
  102. ========================
  103. idResolutionScale::SetCurrentGPUFrameTime
  104. ========================
  105. */
  106. void idResolutionScale::SetCurrentGPUFrameTime( int microseconds ) {
  107. float old = currentResolution;
  108. float milliseconds = microseconds * 0.001f;
  109. if ( milliseconds > dropMilliseconds ) {
  110. // We missed our target, so drop the resolution.
  111. // The target should be set conservatively so this does not
  112. // necessarily imply a missed VBL.
  113. //
  114. // we might consider making the drop in some way
  115. // proportional to how badly we missed
  116. currentResolution -= rs_dropFraction.GetFloat();
  117. if ( currentResolution < MINIMUM_RESOLUTION_SCALE ) {
  118. currentResolution = MINIMUM_RESOLUTION_SCALE;
  119. }
  120. } else if ( milliseconds < raiseMilliseconds ) {
  121. // We seem to have speed to spare, so increase the resolution
  122. // if we stay here consistantly. The raise fraction should
  123. // be smaller than the drop fraction to avoid ping-ponging
  124. // back and forth.
  125. if ( ++framesAboveRaise >= rs_raiseFrames.GetInteger() ) {
  126. framesAboveRaise = 0;
  127. currentResolution += rs_raiseFraction.GetFloat();
  128. if ( currentResolution > MAXIMUM_RESOLUTION_SCALE ) {
  129. currentResolution = MAXIMUM_RESOLUTION_SCALE;
  130. }
  131. }
  132. } else {
  133. // we are inside the target range
  134. framesAboveRaise = 0;
  135. }
  136. if ( rs_showResolutionChanges.GetInteger() > 1 ||
  137. ( rs_showResolutionChanges.GetInteger() == 1 && currentResolution != old ) ) {
  138. idLib::Printf( "GPU msec: %4.1f resolutionScale: %4.2f\n", milliseconds, currentResolution );
  139. }
  140. }
  141. /*
  142. ========================
  143. idResolutionScale::GetConsoleText
  144. ========================
  145. */
  146. void idResolutionScale::GetConsoleText( idStr &s ) {
  147. float x;
  148. float y;
  149. if ( rs_enable.GetInteger() == 0 ) {
  150. s = "rs-off";
  151. return;
  152. }
  153. GetCurrentResolutionScale( x, y );
  154. if ( rs_display.GetInteger() > 0 ) {
  155. x *= 1280.0f;
  156. y *= 720.0f;
  157. if ( rs_enable.GetInteger() == 1 ) {
  158. y = 1.0f;
  159. } else if ( rs_enable.GetInteger() == 2 ) {
  160. x = 1.0f;
  161. }
  162. s = va( "rs-pixels %i", idMath::Ftoi( x * y ) );
  163. } else {
  164. if ( rs_enable.GetInteger() == 3 ) {
  165. s = va( "%2i%%h,%2i%%v", idMath::Ftoi( 100.0f * x ), idMath::Ftoi( 100.0f * y ) );
  166. } else {
  167. s = va( "%2i%%%s", ( rs_enable.GetInteger() == 1 ) ? idMath::Ftoi( 100.0f * x ) : idMath::Ftoi( 100.0f * y ), ( rs_enable.GetInteger() == 1 ) ? "h" : "v" );
  168. }
  169. }
  170. }