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