feat: add per player battle/DP zoom (#943)

## Link to GitHub Issue or related Pull Request, if one exists
[#0](https://github.com/spice2x/spice2x.github.io/issues/402)

## Description of change
*what does this code do?*
Simply modifing existing zoom feature with split zoom funcionality.

## Testing
*how was the code tested?*
There were no desyncs while I was playing with a friend of mine on my
setup (3700x, rtx 3060 12gb), performance was no different to regular
zoom.
This commit is contained in:
Elthiee authored and GitHub committed 2026-12-05 18:19:01 -08:00
1 parent af66dd5b50
commit 3d530bfee0
5 files changed
+352 -61

No files matched your search

@@ -3,6 +3,7 @@
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <mutex>
#include <unordered_map>
#include <vector>
@@ -1088,6 +1089,72 @@ static float topSurface_aspect_ratio = 1.f;
static UINT backbuffer_width = 0;
static UINT backbuffer_height = 0;
static RECT compute_split_src_rect(
const RECT &origin_rect,
LONG dest_w,
LONG dest_h,
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;
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);
}
w_new *= limit;
h_new *= limit;
} else {
w_new = std::min(w_new, max_w);
h_new = std::min(h_new, max_h);
}
const LONG src_w = std::clamp<LONG>(
static_cast<LONG>(std::lround(w_new)), 1, static_cast<LONG>(topSurface_width));
const LONG src_h = std::clamp<LONG>(
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);
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;
// 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);
}
return src_rect;
}
void SurfaceHook(IDirect3DDevice9 *pReal) {
// phase 0 - create a surface that has the same aspect ratio as the original back buffer
@@ -1121,7 +1188,12 @@ void SurfaceHook(IDirect3DDevice9 *pReal) {
"graphics::d3d9", "SurfaceHook - Backbuffer: {} {} {}",
param.BackBufferWidth, param.BackBufferHeight, param.BackBufferCount);
tex->GetSurfaceLevel(0, &topSurface);
if (tex) {
tex->GetSurfaceLevel(0, &topSurface);
}
if (topSurface) {
pReal->ColorFill(topSurface, nullptr, D3DCOLOR_XRGB(0, 0, 0));
}
if (swapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &backbuffer) != D3D_OK) {
log_warning("graphics::d3d9", "SurfaceHook - GetBackBuffer failed");
}
@@ -1129,19 +1201,35 @@ void SurfaceHook(IDirect3DDevice9 *pReal) {
swapChain->Release();
}
if (!topSurface || !backbuffer) {
return;
}
// pre-calculate dimensions used for phase 1
const int rectLeft = backbuffer_width;
const int rectTop = backbuffer_height;
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 effective_duplicate =
(cfg::SCREENRESIZE->enable_screen_resize && !use_split_mode)
? cfg::SCREENRESIZE->scene_settings[cfg::SCREENRESIZE->screen_resize_current_scene].duplicate
: cfg::ScreenDuplicateMode::None;
// this code used to clear the surface using ColorFill on every call, but
// this turned out to be very expensive, leading to major frame drops in
// SDVX Live2D scenario
if (cfg::SCREENRESIZE->need_surface_clean) {
static auto last_duplicate_mode = cfg::ScreenDuplicateMode::None;
if (cfg::SCREENRESIZE->need_surface_clean || last_duplicate_mode != effective_duplicate) {
pReal->ColorFill(topSurface, nullptr, D3DCOLOR_XRGB(0, 0, 0));
cfg::SCREENRESIZE->need_surface_clean = false;
}
last_duplicate_mode = effective_duplicate;
HRESULT hr;
@@ -1167,17 +1255,14 @@ void SurfaceHook(IDirect3DDevice9 *pReal) {
bool duplicate = false;
RECT originRect2 = originRect;
if (cfg::SCREENRESIZE->enable_screen_resize) {
auto& scene = cfg::SCREENRESIZE->scene_settings[cfg::SCREENRESIZE->screen_resize_current_scene];
if (scene.duplicate == cfg::ScreenDuplicateMode::CopyLeft) {
duplicate = true;
originRect2.right = originRect.left;
originRect2.left = (LONG)(originRect2.left - (originRect.right - originRect.left));
} else if (scene.duplicate == cfg::ScreenDuplicateMode::CopyRight) {
duplicate = true;
originRect2.left = originRect.right;
originRect2.right = (LONG)(originRect2.left + (originRect.right - originRect.left));
}
if (effective_duplicate == cfg::ScreenDuplicateMode::CopyLeft) {
duplicate = true;
originRect2.right = originRect.left;
originRect2.left = (LONG)(originRect2.left - (originRect.right - originRect.left));
} else if (effective_duplicate == cfg::ScreenDuplicateMode::CopyRight) {
duplicate = true;
originRect2.left = originRect.right;
originRect2.right = (LONG)(originRect2.left + (originRect.right - originRect.left));
}
// log_misc(
@@ -1207,6 +1292,61 @@ void SurfaceHook(IDirect3DDevice9 *pReal) {
}
}
bool use_linear_filter = true;
if (cfg::SCREENRESIZE->enable_screen_resize) {
use_linear_filter = cfg::SCREENRESIZE->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);
}
return;
}
// phase 2 - viewport calculation - do the actual zoom / offset math
// figure out what region of the surface to copy back to the back buffer.
RECT targetRect {
@@ -1280,14 +1420,10 @@ void SurfaceHook(IDirect3DDevice9 *pReal) {
// targetRect.left, targetRect.top, targetRect.right, targetRect.bottom);
// phase 3 - draw the surface to back buffer
bool use_linear_filter = true;
if (cfg::SCREENRESIZE->enable_screen_resize) {
use_linear_filter = cfg::SCREENRESIZE->enable_linear_filter;
}
hr = pReal->StretchRect(
topSurface, &targetRect,
backbuffer, nullptr,
use_linear_filter ? D3DTEXF_LINEAR : D3DTEXF_NONE);
filter);
if (hr != D3D_OK) {
log_warning(
"graphics::d3d9",