clean up screen resize

This commit is contained in:
bicarus-dev committed 2026-12-05 18:31:07 -08:00
1 parent 3d530bfee0
commit 15b2c04f0f
5 files changed
+204 -219

No files matched your search

@@ -1091,27 +1091,18 @@ static UINT backbuffer_height = 0;
static RECT compute_split_src_rect(
const RECT &origin_rect,
LONG dest_w,
LONG dest_h,
LONG player_width,
LONG player_height,
bool is_right_source,
const cfg::split_region_setting &region) {
const float safe_scale_x = std::max(region.scale_x, 0.01f);
const float safe_scale_y = std::max(region.scale_y, 0.01f);
float w_new = static_cast<float>(dest_w) / safe_scale_x;
float h_new = static_cast<float>(dest_h) / safe_scale_y;
float w_new = static_cast<float>(player_width) / region.scale_x;
float h_new = static_cast<float>(player_height) / region.scale_y;
const float max_w = static_cast<float>(topSurface_width);
const float max_h = static_cast<float>(topSurface_height);
if (region.keep_aspect_ratio) {
float limit = 1.0f;
if (w_new > max_w) {
limit = std::min(limit, max_w / w_new);
}
if (h_new > max_h) {
limit = std::min(limit, max_h / h_new);
}
const float limit = std::min({1.0f, max_w / w_new, max_h / h_new});
w_new *= limit;
h_new *= limit;
} else {
@@ -1125,34 +1116,47 @@ static RECT compute_split_src_rect(
static_cast<LONG>(std::lround(h_new)), 1, static_cast<LONG>(topSurface_height));
const float anchor_x = is_right_source
? (static_cast<float>(origin_rect.right) - static_cast<float>(dest_w) * 0.5f)
: (static_cast<float>(origin_rect.left) + static_cast<float>(dest_w) * 0.5f);
? (static_cast<float>(origin_rect.right) - static_cast<float>(player_width) * 0.5f)
: (static_cast<float>(origin_rect.left) + static_cast<float>(player_width) * 0.5f);
const float anchor_y =
(static_cast<float>(origin_rect.top) + static_cast<float>(origin_rect.bottom)) * 0.5f;
RECT src_rect {};
src_rect.left = static_cast<LONG>(std::lround(anchor_x - static_cast<float>(src_w) * 0.5f)) - region.offset_x;
src_rect.top = static_cast<LONG>(std::lround(anchor_y - static_cast<float>(src_h) * 0.5f)) - region.offset_y;
src_rect.right = src_rect.left + src_w;
src_rect.bottom = src_rect.top + src_h;
// widen before applying offsets so even extreme settings stay within the surface
const auto left = static_cast<int64_t>(std::lround(anchor_x - src_w * 0.5f)) - region.offset_x;
const auto top = static_cast<int64_t>(std::lround(anchor_y - src_h * 0.5f)) - region.offset_y;
const auto x = static_cast<LONG>(std::clamp<int64_t>(left, 0, topSurface_width - src_w));
const auto y = static_cast<LONG>(std::clamp<int64_t>(top, 0, topSurface_height - src_h));
return {x, y, x + src_w, y + src_h};
}
// boundary check to prevent StretchRect failures
if (src_rect.left < 0) {
src_rect.left = 0;
src_rect.right = src_w;
} else if (src_rect.right > static_cast<LONG>(topSurface_width)) {
src_rect.left = static_cast<LONG>(topSurface_width) - src_w;
src_rect.right = static_cast<LONG>(topSurface_width);
}
if (src_rect.top < 0) {
src_rect.top = 0;
src_rect.bottom = src_h;
} else if (src_rect.bottom > static_cast<LONG>(topSurface_height)) {
src_rect.top = static_cast<LONG>(topSurface_height) - src_h;
src_rect.bottom = static_cast<LONG>(topSurface_height);
static void draw_split_region(
IDirect3DDevice9 *device, const RECT &src, const RECT &dest,
D3DTEXTUREFILTERTYPE filter, const char *player) {
if (device->StretchRect(topSurface, &src, backbuffer, &dest, filter) != D3D_OK) {
log_warning(
"graphics::d3d9",
"SurfaceHook - StretchRect split {} failed, src: {} {} {} {}, dst: {} {} {} {}",
player, src.left, src.top, src.right, src.bottom,
dest.left, dest.top, dest.right, dest.bottom);
}
}
return src_rect;
static void draw_split_screen(
IDirect3DDevice9 *device, const RECT &origin, LONG width, LONG height,
const cfg::split_screen_setting &split, D3DTEXTUREFILTERTYPE filter) {
const LONG left_width = std::clamp<LONG>(
std::lround(static_cast<float>(width) * split.ratio), 1, width - 1);
const LONG right_width = width - left_width;
const RECT left_src = compute_split_src_rect(origin, left_width, height, false, split.left);
const RECT right_src = compute_split_src_rect(origin, right_width, height, true, split.right);
// move each viewport with its original width, without changing its source crop
const LONG left_x = split.swap_sides ? right_width : 0;
const LONG right_x = split.swap_sides ? 0 : left_width;
const RECT left_dest {left_x, 0, left_x + left_width, height};
const RECT right_dest {right_x, 0, right_x + right_width, height};
draw_split_region(device, left_src, left_dest, filter, "1P");
draw_split_region(device, right_src, right_dest, filter, "2P");
}
void SurfaceHook(IDirect3DDevice9 *pReal) {
@@ -1211,14 +1215,14 @@ void SurfaceHook(IDirect3DDevice9 *pReal) {
const int w = backbuffer_width;
const int h = backbuffer_height;
const bool use_split_mode =
cfg::SCREENRESIZE->enable_screen_resize
&& cfg::SCREENRESIZE->scene_settings[cfg::SCREENRESIZE->screen_resize_current_scene].split_mode
&& w >= 2 && h >= 1;
const auto &resize = *cfg::SCREENRESIZE;
const auto &scene = resize.scene_settings[resize.screen_resize_current_scene];
const bool use_split_mode = resize.enable_screen_resize && scene.split.enabled &&
w >= 2 && h >= 1;
const auto effective_duplicate =
(cfg::SCREENRESIZE->enable_screen_resize && !use_split_mode)
? cfg::SCREENRESIZE->scene_settings[cfg::SCREENRESIZE->screen_resize_current_scene].duplicate
(resize.enable_screen_resize && !use_split_mode)
? scene.duplicate
: cfg::ScreenDuplicateMode::None;
// this code used to clear the surface using ColorFill on every call, but
@@ -1292,58 +1296,11 @@ void SurfaceHook(IDirect3DDevice9 *pReal) {
}
}
bool use_linear_filter = true;
if (cfg::SCREENRESIZE->enable_screen_resize) {
use_linear_filter = cfg::SCREENRESIZE->enable_linear_filter;
}
const bool use_linear_filter = !resize.enable_screen_resize || resize.enable_linear_filter;
const auto filter = use_linear_filter ? D3DTEXF_LINEAR : D3DTEXF_NONE;
// phase 2 & 3 (split mode) - independently zoom/offset left and right playfields onto
// the left and right portions of the back buffer
if (use_split_mode) {
const auto& scene = cfg::SCREENRESIZE->scene_settings[cfg::SCREENRESIZE->screen_resize_current_scene];
const float ratio = std::clamp(scene.split_ratio, 0.1f, 0.9f);
const LONG split_x = std::clamp<LONG>(
static_cast<LONG>(std::lround(static_cast<float>(w) * ratio)), 1, static_cast<LONG>(w) - 1);
const RECT destLeft { 0, 0, split_x, static_cast<LONG>(h) };
const RECT destRight { split_x, 0, static_cast<LONG>(w), static_cast<LONG>(h) };
const bool left_uses_right_source = scene.split_swap_sides;
const bool right_uses_right_source = !scene.split_swap_sides;
const auto& left_region = scene.split_swap_sides ? scene.split_right : scene.split_left;
const auto& right_region = scene.split_swap_sides ? scene.split_left : scene.split_right;
const RECT srcLeft = compute_split_src_rect(
originRect,
destLeft.right - destLeft.left,
destLeft.bottom - destLeft.top,
left_uses_right_source,
left_region);
const RECT srcRight = compute_split_src_rect(
originRect,
destRight.right - destRight.left,
destRight.bottom - destRight.top,
right_uses_right_source,
right_region);
hr = pReal->StretchRect(topSurface, &srcLeft, backbuffer, &destLeft, filter);
if (hr != D3D_OK) {
log_warning(
"graphics::d3d9",
"SurfaceHook - StretchRect split left failed, src: {} {} {} {}, dst: {} {} {} {}",
srcLeft.left, srcLeft.top, srcLeft.right, srcLeft.bottom,
destLeft.left, destLeft.top, destLeft.right, destLeft.bottom);
}
hr = pReal->StretchRect(topSurface, &srcRight, backbuffer, &destRight, filter);
if (hr != D3D_OK) {
log_warning(
"graphics::d3d9",
"SurfaceHook - StretchRect split right failed, src: {} {} {} {}, dst: {} {} {} {}",
srcRight.left, srcRight.top, srcRight.right, srcRight.bottom,
destRight.left, destRight.top, destRight.right, destRight.bottom);
}
draw_split_screen(pReal, originRect, w, h, scene.split, filter);
return;
}
@@ -1355,8 +1312,7 @@ void SurfaceHook(IDirect3DDevice9 *pReal) {
(LONG)(rectLeft + w),
(LONG)(rectTop + h),
};
if (cfg::SCREENRESIZE->enable_screen_resize) {
auto& scene = cfg::SCREENRESIZE->scene_settings[cfg::SCREENRESIZE->screen_resize_current_scene];
if (resize.enable_screen_resize) {
auto w_new = w / scene.scale_x;
auto h_new = h / scene.scale_y;