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

+32
View File
@@ -1,5 +1,6 @@
#include "screen_resize.h"
#include <algorithm>
#include "avs/game.h"
#include "external/rapidjson/document.h"
#include "external/rapidjson/pointer.h"
@@ -97,7 +98,24 @@ namespace cfg {
int duplicate = 0;
load_int_value(doc, root + prefix + "duplicate", duplicate);
scene.duplicate = static_cast<cfg::ScreenDuplicateMode>(duplicate);
load_bool_value(doc, root + prefix + "split_mode", scene.split_mode);
if (load_float_value(doc, root + prefix + "split_ratio", scene.split_ratio)) {
scene.split_ratio = std::clamp(scene.split_ratio, 0.1f, 0.9f);
}
load_bool_value(doc, root + prefix + "split_swap_sides", scene.split_swap_sides);
auto load_split_region = [&](const std::string& sub, split_region_setting& region) {
load_int_value(doc, root + prefix + sub + "/offset_x", region.offset_x);
load_int_value(doc, root + prefix + sub + "/offset_y", region.offset_y);
load_float_value(doc, root + prefix + sub + "/scale_x", region.scale_x);
load_float_value(doc, root + prefix + sub + "/scale_y", region.scale_y);
load_bool_value(doc, root + prefix + sub + "/keep_aspect_ratio", region.keep_aspect_ratio);
};
load_split_region("split_left", scene.split_left);
load_split_region("split_right", scene.split_right);
}
this->need_surface_clean = true;
// windowed settings are always under game settings
root = "/sp2x_games/" + eamuse_get_game() + "/";
@@ -212,6 +230,20 @@ namespace cfg {
rapidjson::Pointer(root + prefix + "scale_y").Set(doc, scene.scale_y);
rapidjson::Pointer(root + prefix + "keep_aspect_ratio").Set(doc, scene.keep_aspect_ratio);
rapidjson::Pointer(root + prefix + "duplicate").Set(doc, scene.duplicate);
rapidjson::Pointer(root + prefix + "split_mode").Set(doc, scene.split_mode);
rapidjson::Pointer(root + prefix + "split_ratio").Set(doc, scene.split_ratio);
rapidjson::Pointer(root + prefix + "split_swap_sides").Set(doc, scene.split_swap_sides);
auto save_split_region = [&](const std::string& sub, const split_region_setting& region) {
rapidjson::Pointer(root + prefix + sub + "/offset_x").Set(doc, region.offset_x);
rapidjson::Pointer(root + prefix + sub + "/offset_y").Set(doc, region.offset_y);
rapidjson::Pointer(root + prefix + sub + "/scale_x").Set(doc, region.scale_x);
rapidjson::Pointer(root + prefix + sub + "/scale_y").Set(doc, region.scale_y);
rapidjson::Pointer(root + prefix + sub + "/keep_aspect_ratio").Set(doc, region.keep_aspect_ratio);
};
save_split_region("split_left", scene.split_left);
save_split_region("split_right", scene.split_right);
}
// windowed mode settings
+15
View File
@@ -21,6 +21,14 @@ namespace cfg {
CopyRight = 2,
};
struct split_region_setting {
int offset_x = 0;
int offset_y = 0;
float scale_x = 1.0f;
float scale_y = 1.0f;
bool keep_aspect_ratio = true;
};
struct fullscreen_setting {
int offset_x = 0;
int offset_y = 0;
@@ -28,6 +36,13 @@ namespace cfg {
float scale_y = 1.0;
bool keep_aspect_ratio = true;
ScreenDuplicateMode duplicate = ScreenDuplicateMode::None;
// dual-playfield / split-screen zoom settings (e.g. battle mode)
bool split_mode = false;
float split_ratio = 0.5f;
bool split_swap_sides = false;
split_region_setting split_left;
split_region_setting split_right;
};
extern std::optional<std::string> SCREEN_RESIZE_CFG_PATH_OVERRIDE;
@@ -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",
+145 -43
View File
@@ -63,7 +63,14 @@ namespace overlay::windows {
scene.scale_x = 1.f;
scene.scale_y = 1.f;
scene.duplicate = cfg::ScreenDuplicateMode::None;
scene.split_mode = false;
scene.split_ratio = 0.5f;
scene.split_swap_sides = false;
scene.split_left = {};
scene.split_right = {};
}
cfg::SCREENRESIZE->need_surface_clean = true;
cfg::SCREENRESIZE->enable_window_resize = false;
cfg::SCREENRESIZE->window_always_on_top = false;
@@ -101,6 +108,45 @@ namespace overlay::windows {
build_footer();
}
void ScreenResize::build_split_region_config(
const char *id, const char *label, cfg::split_region_setting &region) {
if (!ImGui::TreeNodeEx(label, ImGuiTreeNodeFlags_DefaultOpen)) {
return;
}
ImGui::PushID(id);
ImGui::InputInt("X Offset", &region.offset_x);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on +/- buttons to move quickly.");
ImGui::InputInt("Y Offset", &region.offset_y);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on +/- buttons to move quickly.");
if (ImGui::Checkbox("Keep Aspect Ratio", &region.keep_aspect_ratio)) {
if (region.keep_aspect_ratio) {
region.scale_y = region.scale_x;
}
}
if (region.keep_aspect_ratio) {
if (ImGui::SliderFloat("Scale", &region.scale_x, 0.6f, 3.0f)) {
region.scale_y = region.scale_x;
}
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on the slider to type in a numeric value.");
} else {
ImGui::SliderFloat("Width Scale", &region.scale_x, 0.6f, 3.0f);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on the slider to type in a numeric value.");
ImGui::SliderFloat("Height Scale", &region.scale_y, 0.6f, 3.0f);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on the slider to type in a numeric value.");
}
ImGui::PopID();
ImGui::TreePop();
}
void ScreenResize::build_fullscreen_config() {
ImGui::TextColored(ImVec4(1, 0.5f, 0.5f, 1.f),
@@ -138,53 +184,109 @@ namespace overlay::windows {
auto& scene = cfg::SCREENRESIZE->scene_settings[cfg::SCREENRESIZE->screen_resize_current_scene];
// general settings
ImGui::InputInt("X Offset", &scene.offset_x);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on +/- buttons to move quickly.");
ImGui::InputInt("Y Offset", &scene.offset_y);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on +/- buttons to move quickly.");
// aspect ratio
ImGui::Checkbox("Keep Aspect Ratio", &scene.keep_aspect_ratio);
if (scene.keep_aspect_ratio) {
// we want to avoid zooming out below 0.6 as it will cause StretchRect calls to
// go on a slow sync path with the GPU, leading to frame drops in some situations
// (e.g., SDVX Live2D)
if (ImGui::SliderFloat("Scale", &scene.scale_x, 0.6f, 2.5f)) {
scene.scale_y = scene.scale_x;
}
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on the slider to type in a numeric value.");
} else {
ImGui::SliderFloat("Width Scale", &scene.scale_x, 0.6f, 2.5f);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on the slider to type in a numeric value.");
ImGui::SliderFloat("Height Scale", &scene.scale_y, 0.6f, 2.5f);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on the slider to type in a numeric value.");
}
static const char* dupe_items[] = { "None", "Copy Left", "Copy Right" };
const bool duplicate_changed = ImGui::Combo(
"Duplicate",
reinterpret_cast<int *>(&scene.duplicate),
dupe_items,
ARRAYSIZE(dupe_items));
const bool split_changed = ImGui::Checkbox("Split Screen (Dual Playfield)", &scene.split_mode);
ImGui::SameLine();
ImGui::HelpMarker(
"Show an identical copy of the image on the left or right, allowing you to achieve "
"wrap-around effect when an offset is set.");
ImGui::EndDisabled();
// tell d3d9 device to clear surface to get rid of ghost image
if (duplicate_changed) {
"Split the screen into independent Left (1P) and Right (2P) zoom viewports, "
"allowing separate zoom and offset per playfield (e.g. Battle mode).");
if (split_changed) {
cfg::SCREENRESIZE->need_surface_clean = true;
}
if (scene.split_mode) {
if (ImGui::SliderFloat("Split Position", &scene.split_ratio, 0.1f, 0.9f, "%.2f")) {
scene.split_ratio = CLAMP(scene.split_ratio, 0.1f, 0.9f);
}
ImGui::SameLine();
ImGui::HelpMarker(
"Horizontal divider position between the left and right viewports (0.50 = 50/50). "
"Ctrl + click to type in a numeric value.");
ImGui::Checkbox("Swap Sides", &scene.split_swap_sides);
ImGui::SameLine();
ImGui::HelpMarker(
"Visually swap 1P (left) and 2P (right) playfields on screen "
"while keeping each playfield's individual zoom and offset.");
if (ImGui::Button("Mirror 1P -> 2P")) {
scene.split_right = scene.split_left;
scene.split_right.offset_x = -scene.split_left.offset_x;
}
ImGui::SameLine();
if (ImGui::Button("Mirror 2P -> 1P")) {
scene.split_left = scene.split_right;
scene.split_left.offset_x = -scene.split_right.offset_x;
}
ImGui::SameLine();
if (ImGui::Button("Reset Split")) {
scene.split_ratio = 0.5f;
scene.split_swap_sides = false;
scene.split_left = {};
scene.split_right = {};
}
ImGui::SameLine();
ImGui::HelpMarker(
"Mirror copies zoom and offset to the other playfield with an inverted X offset "
"for symmetric Battle layouts.");
const char *left_label = scene.split_swap_sides
? "1P Playfield (Displayed Right)###split_left"
: "1P Playfield (Left)###split_left";
const char *right_label = scene.split_swap_sides
? "2P Playfield (Displayed Left)###split_right"
: "2P Playfield (Right)###split_right";
this->build_split_region_config("split_left", left_label, scene.split_left);
this->build_split_region_config("split_right", right_label, scene.split_right);
} else {
// general settings
ImGui::InputInt("X Offset", &scene.offset_x);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on +/- buttons to move quickly.");
ImGui::InputInt("Y Offset", &scene.offset_y);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on +/- buttons to move quickly.");
// aspect ratio
ImGui::Checkbox("Keep Aspect Ratio", &scene.keep_aspect_ratio);
if (scene.keep_aspect_ratio) {
// we want to avoid zooming out below 0.6 as it will cause StretchRect calls to
// go on a slow sync path with the GPU, leading to frame drops in some situations
// (e.g., SDVX Live2D)
if (ImGui::SliderFloat("Scale", &scene.scale_x, 0.6f, 2.5f)) {
scene.scale_y = scene.scale_x;
}
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on the slider to type in a numeric value.");
} else {
ImGui::SliderFloat("Width Scale", &scene.scale_x, 0.6f, 2.5f);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on the slider to type in a numeric value.");
ImGui::SliderFloat("Height Scale", &scene.scale_y, 0.6f, 2.5f);
ImGui::SameLine();
ImGui::HelpMarker("Hint: ctrl + click on the slider to type in a numeric value.");
}
static const char* dupe_items[] = { "None", "Copy Left", "Copy Right" };
const bool duplicate_changed = ImGui::Combo(
"Duplicate",
reinterpret_cast<int *>(&scene.duplicate),
dupe_items,
ARRAYSIZE(dupe_items));
ImGui::SameLine();
ImGui::HelpMarker(
"Show an identical copy of the image on the left or right, allowing you to achieve "
"wrap-around effect when an offset is set.");
// tell d3d9 device to clear surface to get rid of ghost image
if (duplicate_changed) {
cfg::SCREENRESIZE->need_surface_clean = true;
}
}
ImGui::EndDisabled();
}
void ScreenResize::build_windowed_config() {
@@ -2,6 +2,10 @@
#include "overlay/window.h"
namespace cfg {
struct split_region_setting;
}
namespace overlay::windows {
class ScreenResize : public Window {
@@ -20,6 +24,8 @@ namespace overlay::windows {
uint32_t toggle_scene_state = ~0u;
void build_fullscreen_config();
void build_split_region_config(
const char *id, const char *label, cfg::split_region_setting &region);
void build_windowed_config();
void build_footer();
std::string hwnd_preview(int index, HWND hwnd);