Skip to content

Commit

Permalink
DRS improvements (#2154)
Browse files Browse the repository at this point in the history
* Added 30-frame history and 15-frame cooldown period after resolution change.

* Simplified and tweaked downscale/upscale threshold parameters.
  • Loading branch information
rfomin authored Jan 26, 2025
1 parent a8e1749 commit a6b0c1e
Showing 1 changed file with 55 additions and 28 deletions.
83 changes: 55 additions & 28 deletions src/i_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,57 +709,84 @@ void I_DynamicResolution(void)
return;
}

static int frame_counter;
static double averagepercent;
#define DRS_COOLDOWN_FRAMES 15
static int cooldown_counter;

if (cooldown_counter > 0)
{
--cooldown_counter;
return;
}

// 1.25 milliseconds for SDL render present
double target = (1.0 / targetrefresh) - 0.00125;
double actual = frametime_withoutpresent / 1000000.0;

double actualpercent = actual / target;
#define DRS_FRAME_HISTORY 30
static double frame_history[DRS_FRAME_HISTORY];
static int frame_index;

#define DRS_DELTA 0.1
#define DRS_GREATER (1 + DRS_DELTA)
#define DRS_LESS (1 - DRS_DELTA / 10.0)
#define DRS_STEP (SCREENHEIGHT / 2)
frame_history[frame_index] = actual;
frame_index = (frame_index + 1) % DRS_FRAME_HISTORY;
double total = 0;
for (int i = 0; i < DRS_FRAME_HISTORY; ++i)
{
total += frame_history[i];
}
const double avg_frame_time = total / DRS_FRAME_HISTORY;
const double performance_ratio = avg_frame_time / target;

int newheight = 0;
int oldheight = video.height;
static boolean needs_upscale;

#define DRS_STEP (SCREENHEIGHT / 2)
#define DRS_DOWNSCALE_T1 1.08
#define DRS_DOWNSCALE_T2 1.2 // 20% over target -> 2x step
#define DRS_DOWNSCALE_T3 1.5 // 50% over target -> 3x step
#define DRS_UPSCALE_T1 0.6
#define DRS_UPSCALE_T2 0.4

frame_counter++;
averagepercent = (averagepercent + actualpercent) / frame_counter;
int oldheight = video.height;
int newheight = 0;

if (actualpercent > DRS_GREATER)
if (performance_ratio > DRS_DOWNSCALE_T1)
{
double reduction = (actualpercent - DRS_GREATER) * 0.4;
newheight = (int)MAX(DRS_MIN_HEIGHT, oldheight - oldheight * reduction);
int step_multipler = 1;
if (performance_ratio > DRS_DOWNSCALE_T3)
{
step_multipler = 3;
}
else if (performance_ratio > DRS_DOWNSCALE_T2)
{
step_multipler = 2;
}

newheight = MAX(DRS_MIN_HEIGHT, oldheight - DRS_STEP * step_multipler);
}
else if (averagepercent < DRS_LESS && frame_counter > targetrefresh)
else if (performance_ratio < DRS_UPSCALE_T1 && needs_upscale)
{
double addition = (DRS_LESS - averagepercent) * 0.25;
newheight = (int)MIN(current_video_height, oldheight + oldheight * addition);
int step_multiplier = 1;
if (performance_ratio < DRS_UPSCALE_T2)
{
step_multiplier = 2;
}

newheight =
MIN(current_video_height, oldheight + DRS_STEP * step_multiplier);
}
else
{
return;
}

frame_counter = 0;

int mul = (newheight + (DRS_STEP - 1)) / DRS_STEP; // integer round

newheight = mul * DRS_STEP;

if (newheight > current_video_height)
{
newheight = current_video_height;
}

if (newheight == oldheight)
{
return;
}

needs_upscale = newheight < current_video_height;

cooldown_counter = DRS_COOLDOWN_FRAMES;

if (newheight < oldheight)
{
VX_DecreaseMaxDist();
Expand Down

0 comments on commit a6b0c1e

Please sign in to comment.