Update to spice2x-25-04-25 (pre-apply)
> broken commit
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#endif
|
||||
|
||||
#include "avs/game.h"
|
||||
#include "cfg/screen_resize.h"
|
||||
#include "games/iidx/iidx.h"
|
||||
#include "games/sdvx/sdvx.h"
|
||||
#include "games/io.h"
|
||||
@@ -200,6 +201,34 @@ static std::string presentation_interval2s(UINT presentation_interval) {
|
||||
FLAGS_END(presentation_interval);
|
||||
}
|
||||
|
||||
static void update_backbuffer_dimensions(D3DPRESENT_PARAMETERS *params);
|
||||
|
||||
static bool is_dx9_on_12_enabled() {
|
||||
switch (GRAPHICS_9_ON_12_STATE) {
|
||||
case DX9ON12_FORCE_OFF:
|
||||
log_info("graphics::d3d9", "DirectX 9on12: forced OFF by user (-dx9on12)");
|
||||
return false;
|
||||
|
||||
case DX9ON12_FORCE_ON:
|
||||
log_info("graphics::d3d9", "DirectX 9on12: forced ON by user (-9on12 or -dx9on12)");
|
||||
return true;
|
||||
|
||||
case DX9ON12_AUTO:
|
||||
default:
|
||||
if (GRAPHICS_9_ON_12_REQUESTED_BY_GAME) {
|
||||
log_info(
|
||||
"graphics::d3d9",
|
||||
"DirectX 9on12: enabled automatically for current game (tip: use -dx9on12 to force on or off)");
|
||||
return true;
|
||||
} else {
|
||||
log_info(
|
||||
"graphics::d3d9",
|
||||
"DirectX 9on12: disabled by default, using DX9 (tip: use -dx9on12 to force on or off)");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static IDirect3D9 *WINAPI Direct3DCreate9_hook(UINT SDKVersion) {
|
||||
log_misc("graphics::d3d9", "Direct3DCreate9 hook hit");
|
||||
|
||||
@@ -214,7 +243,7 @@ static IDirect3D9 *WINAPI Direct3DCreate9_hook(UINT SDKVersion) {
|
||||
|
||||
// create interface
|
||||
IDirect3D9 *value;
|
||||
if (GRAPHICS_9_ON_12) {
|
||||
if (is_dx9_on_12_enabled()) {
|
||||
if (!Direct3DCreate9On12_orig) {
|
||||
log_fatal("graphics::d3d9", "unable to find Direct3DCreate9On12");
|
||||
} else {
|
||||
@@ -247,7 +276,7 @@ static HRESULT WINAPI Direct3DCreate9Ex_hook(UINT SDKVersion, IDirect3D9Ex **d3d
|
||||
|
||||
// call original
|
||||
HRESULT result;
|
||||
if (GRAPHICS_9_ON_12) {
|
||||
if (is_dx9_on_12_enabled()) {
|
||||
if (!Direct3DCreate9On12Ex_orig) {
|
||||
log_fatal("graphics::d3d9", "unable to find Direct3DCreate9On12Ex");
|
||||
} else {
|
||||
@@ -389,7 +418,7 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::EnumAdapterModes(
|
||||
{
|
||||
HRESULT ret = pReal->EnumAdapterModes(Adapter, Format, Mode, pMode);
|
||||
|
||||
if (SUCCEEDED(ret) && pMode && avs::game::is_model("LDJ")) {
|
||||
if (SUCCEEDED(ret) && pMode) {
|
||||
/*
|
||||
log_misc("graphics::d3d9", "IDirect3D9::EnumAdapterMode({}, {}, {}) => {}x{} @ {} Hz ({})",
|
||||
Adapter,
|
||||
@@ -406,52 +435,66 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::EnumAdapterModes(
|
||||
auto height = pMode->Height;
|
||||
auto refresh = pMode->RefreshRate;
|
||||
|
||||
if (Mode == 0 && games::iidx::TDJ_MODE) {
|
||||
if (games::iidx::is_tdj_fhd()) {
|
||||
log_misc("graphics::d3d9", "overriding mode 0 to 1920x1080 @ 120 Hz (for TDJ FHD)");
|
||||
pMode->Width = 1920;
|
||||
pMode->Height = 1080;
|
||||
pMode->RefreshRate = 120;
|
||||
modified = true;
|
||||
} else {
|
||||
log_misc("graphics::d3d9", "overriding mode 0 to 1280x720 @ 120 Hz (for TDJ HD)");
|
||||
pMode->Width = 1280;
|
||||
pMode->Height = 720;
|
||||
pMode->RefreshRate = 120;
|
||||
if (avs::game::is_model("LDJ")) {
|
||||
if (Mode == 0 && games::iidx::TDJ_MODE) {
|
||||
if (games::iidx::is_tdj_fhd()) {
|
||||
log_misc("graphics::d3d9", "overriding mode 0 to 1920x1080 @ 120 Hz (for TDJ FHD)");
|
||||
pMode->Width = 1920;
|
||||
pMode->Height = 1080;
|
||||
pMode->RefreshRate = 120;
|
||||
modified = true;
|
||||
} else {
|
||||
log_misc("graphics::d3d9", "overriding mode 0 to 1280x720 @ 120 Hz (for TDJ HD)");
|
||||
pMode->Width = 1280;
|
||||
pMode->Height = 720;
|
||||
pMode->RefreshRate = 120;
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
// For whatever reason, TDJ FHD mode prefers to pick lower (~60Hz) resolutions instead
|
||||
// of 1080p@120Hz. Remove them here and try to force 120+ Hz.
|
||||
if (!modified && games::iidx::is_tdj_fhd() && refresh < 110) {
|
||||
if ((width == 1920 && height == 1080) ||
|
||||
(width == 1280 && height == 720)){
|
||||
log_misc(
|
||||
"graphics::d3d9", "removing mode {}, {}x{} @ {}Hz (for TDJ FHD)",
|
||||
Mode, width, height, refresh);
|
||||
memset(pMode, 0, sizeof(*pMode));
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
// zero out display mode for bad entries
|
||||
// - skip non-native resolutions
|
||||
// - skip 75 and 90 Hz entries because LDJ game timing is messed up with it
|
||||
// (TDJ should be fine as it assumes a fixed 60 Hz timing)
|
||||
if (!modified && (width == 1360 || width == 1366 || refresh == 75 || refresh == 90)) {
|
||||
log_misc(
|
||||
"graphics::d3d9", "removing mode {}, {}x{} @ {}Hz (for LDJ/TDJ)",
|
||||
Mode, width, height, refresh);
|
||||
memset(pMode, 0, sizeof(*pMode));
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
// For whatever reason, TDJ FHD mode prefers to pick these resolutions over 1080p@120Hz.
|
||||
// Remove them entirely.
|
||||
if (!modified && games::iidx::is_tdj_fhd() && refresh == 60) {
|
||||
if ((width == 1920 && height == 1080) ||
|
||||
(width == 1280 && height == 720)){
|
||||
if (!modified && !games::iidx::TDJ_MODE && games::iidx::FORCE_720P && (height > 720)) {
|
||||
log_misc(
|
||||
"graphics::d3d9", "removing mode {}, {}x{} @ {}Hz (for TDJ FHD)",
|
||||
"graphics::d3d9", "removing mode {}, {}x{} @ {}Hz (-iidxforce720p)",
|
||||
Mode, width, height, refresh);
|
||||
memset(pMode, 0, sizeof(*pMode));
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
// zero out display mode for bad entries
|
||||
// - skip non-native resolutions
|
||||
// - skip 75 and 90 Hz entries because LDJ game timing is messed up with it
|
||||
// (TDJ should be fine as it assumes a fixed 60 Hz timing)
|
||||
if (!modified && (width == 1360 || width == 1366 || refresh == 75 || refresh == 90)) {
|
||||
log_misc(
|
||||
"graphics::d3d9", "removing mode {}, {}x{} @ {}Hz (for LDJ/TDJ)",
|
||||
Mode, width, height, refresh);
|
||||
memset(pMode, 0, sizeof(*pMode));
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!modified && !games::iidx::TDJ_MODE && games::iidx::FORCE_720P && (height > 720)) {
|
||||
if (!modified &&
|
||||
(width > height) &&
|
||||
(GRAPHICS_ADJUST_ORIENTATION == ORIENTATION_CW ||
|
||||
GRAPHICS_ADJUST_ORIENTATION == ORIENTATION_CCW)) {
|
||||
log_misc(
|
||||
"graphics::d3d9", "removing mode {}, {}x{} @ {}Hz (-sp2x-iidxforce720p)",
|
||||
"graphics::d3d9", "swapping width and height for mode {}, {}x{} @ {}Hz (-autoorientation)",
|
||||
Mode, width, height, refresh);
|
||||
memset(pMode, 0, sizeof(*pMode));
|
||||
pMode->Height = width;
|
||||
pMode->Width = height;
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
@@ -551,15 +594,31 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::GetDeviceCaps(UINT Adapter, D3DDEVT
|
||||
}
|
||||
|
||||
HRESULT ret = this->pReal->GetDeviceCaps(Adapter, DeviceType, pCaps);
|
||||
|
||||
// IIDX Lightning Cabinet & SDVX Valkyrie Cabinet
|
||||
if (SUCCEEDED(ret) && !GRAPHICS_WINDOWED && avs::game::is_model({"LDJ", "KFC"})) {
|
||||
pCaps->NumberOfAdaptersInGroup = 2;
|
||||
if (FAILED(ret)) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (SUCCEEDED(ret)) {
|
||||
if (GRAPHICS_WINDOWED && (avs::game::is_model("NBT") || avs::game::is_model("PAN"))) {
|
||||
if (avs::game::is_model("LDJ")) {
|
||||
if (!GRAPHICS_WINDOWED) {
|
||||
// use 2 so that the subscreen overlay can be drawn in full screen with single monitor
|
||||
pCaps->NumberOfAdaptersInGroup = 2;
|
||||
}
|
||||
// in windowed mode, LDJ will always launch two windows, no special handling needed here
|
||||
} else if (avs::game::is_model("KFC")) {
|
||||
if (GRAPHICS_WINDOWED & GRAPHICS_PREVENT_SECONDARY_WINDOW) {
|
||||
// user wants windowed mode but does not want subscreen at all
|
||||
pCaps->NumberOfAdaptersInGroup = 1;
|
||||
} else {
|
||||
// in both full screen and windowed mode, use 2 so that the game draws the subscreen
|
||||
// (if this is 1, the game won't even draw the second window, causing subscreen overlay to not work)
|
||||
pCaps->NumberOfAdaptersInGroup = 2;
|
||||
}
|
||||
} else if (avs::game::is_model({"NBT", "PAN"})) {
|
||||
// beatstream, nostalgia
|
||||
if (GRAPHICS_WINDOWED) {
|
||||
pCaps->NumberOfAdaptersInGroup = std::min(pCaps->NumberOfAdaptersInGroup, 1u);
|
||||
} else if (GRAPHICS_FORCE_SINGLE_ADAPTER) {
|
||||
pCaps->NumberOfAdaptersInGroup = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -621,7 +680,29 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::CreateDevice(
|
||||
|
||||
// dump presentation parameters
|
||||
for (size_t i = 0; i < num_adapters; i++) {
|
||||
const auto *params = &pPresentationParameters[i];
|
||||
auto *params = &pPresentationParameters[i];
|
||||
if (!GRAPHICS_WINDOWED && i == 0) {
|
||||
GRAPHICS_FS_ORIGINAL_WIDTH = params->BackBufferWidth;
|
||||
GRAPHICS_FS_ORIGINAL_HEIGHT = params->BackBufferHeight;
|
||||
log_misc("graphics::d3d9", "original resolution: {}x{}", GRAPHICS_FS_ORIGINAL_WIDTH, GRAPHICS_FS_ORIGINAL_HEIGHT);
|
||||
if (GRAPHICS_FS_CUSTOM_RESOLUTION.has_value()) {
|
||||
log_misc(
|
||||
"graphics::d3d9",
|
||||
"use custom resolution {}x{} => {}x{}",
|
||||
params->BackBufferWidth, params->BackBufferHeight,
|
||||
GRAPHICS_FS_CUSTOM_RESOLUTION.value().first,
|
||||
GRAPHICS_FS_CUSTOM_RESOLUTION.value().second);
|
||||
params->BackBufferWidth = GRAPHICS_FS_CUSTOM_RESOLUTION.value().first;
|
||||
params->BackBufferHeight = GRAPHICS_FS_CUSTOM_RESOLUTION.value().second;
|
||||
} else if (GRAPHICS_FS_ORIENTATION_SWAP) {
|
||||
log_misc(
|
||||
"graphics::d3d9",
|
||||
"swap full orientation {}x{} => {}x{}",
|
||||
params->BackBufferWidth, params->BackBufferHeight,
|
||||
params->BackBufferHeight, params->BackBufferWidth);
|
||||
std::swap(params->BackBufferWidth, params->BackBufferHeight);
|
||||
}
|
||||
}
|
||||
|
||||
log_info("graphics::d3d9",
|
||||
"D3D9 presentation parameters for adapter {}: BackBufferWidth: {}, BackBufferHeight: {}, "
|
||||
@@ -648,6 +729,7 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::CreateDevice(
|
||||
if (GRAPHICS_WINDOWED) {
|
||||
pPresentationParameters->Windowed = true;
|
||||
pPresentationParameters->FullScreen_RefreshRateInHz = 0;
|
||||
update_backbuffer_dimensions(pPresentationParameters);
|
||||
|
||||
} else if (GRAPHICS_FORCE_REFRESH > 0) {
|
||||
log_info("graphics::d3d9", "force refresh rate: {} => {} Hz (-graphics-force-refresh option)",
|
||||
@@ -672,6 +754,13 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::CreateDevice(
|
||||
BehaviorFlags &= ~D3DCREATE_ADAPTERGROUP_DEVICE;
|
||||
}
|
||||
|
||||
if (GRAPHICS_FORCE_VSYNC_BUFFER.has_value()) {
|
||||
log_info("graphics::d3d9", "force BackBufferCount: {} => {}",
|
||||
pPresentationParameters->BackBufferCount,
|
||||
GRAPHICS_FORCE_VSYNC_BUFFER.value());
|
||||
pPresentationParameters->BackBufferCount = GRAPHICS_FORCE_VSYNC_BUFFER.value();
|
||||
}
|
||||
|
||||
// call original
|
||||
HRESULT ret = this->pReal->CreateDevice(
|
||||
Adapter,
|
||||
@@ -686,7 +775,7 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::CreateDevice(
|
||||
|
||||
// log error
|
||||
log_info("graphics::d3d9", "IDirect3D9::CreateDevice failed, hr={}", FMT_HRESULT(ret));
|
||||
} else {
|
||||
} else if (!D3D9_DEVICE_HOOK_DISABLE) {
|
||||
graphics_hook_window(hFocusWindow, pPresentationParameters);
|
||||
|
||||
*ppReturnedDeviceInterface = new WrappedIDirect3DDevice9(hFocusWindow, *ppReturnedDeviceInterface);
|
||||
@@ -736,7 +825,13 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::CreateDeviceEx(
|
||||
{
|
||||
assert(is_d3d9ex);
|
||||
|
||||
log_misc("graphics::d3d9", "IDirect3D9Ex::CreateDeviceEx hook hit");
|
||||
log_misc("graphics::d3d9", "IDirect3D9Ex::CreateDeviceEx hook hit ({}, {}, {}, {}, {}, {})",
|
||||
Adapter,
|
||||
DeviceType,
|
||||
fmt::ptr(hFocusWindow),
|
||||
behavior2s(BehaviorFlags),
|
||||
fmt::ptr(pPresentationParameters),
|
||||
fmt::ptr(ppReturnedDeviceInterface));
|
||||
|
||||
// check parameters
|
||||
if (!pPresentationParameters || !ppReturnedDeviceInterface) {
|
||||
@@ -773,10 +868,32 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::CreateDeviceEx(
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < num_adapters; i++) {
|
||||
const auto *params = &pPresentationParameters[i];
|
||||
auto *params = &pPresentationParameters[i];
|
||||
if (!GRAPHICS_WINDOWED && i == 0) {
|
||||
GRAPHICS_FS_ORIGINAL_WIDTH = params->BackBufferWidth;
|
||||
GRAPHICS_FS_ORIGINAL_HEIGHT = params->BackBufferHeight;
|
||||
log_misc("graphics::d3d9", "original resolution: {}x{}", GRAPHICS_FS_ORIGINAL_WIDTH, GRAPHICS_FS_ORIGINAL_HEIGHT);
|
||||
if (GRAPHICS_FS_CUSTOM_RESOLUTION.has_value()) {
|
||||
log_misc(
|
||||
"graphics::d3d9",
|
||||
"use custom resolution {}x{} => {}x{}",
|
||||
params->BackBufferWidth, params->BackBufferHeight,
|
||||
GRAPHICS_FS_CUSTOM_RESOLUTION.value().first,
|
||||
GRAPHICS_FS_CUSTOM_RESOLUTION.value().second);
|
||||
params->BackBufferWidth = GRAPHICS_FS_CUSTOM_RESOLUTION.value().first;
|
||||
params->BackBufferHeight = GRAPHICS_FS_CUSTOM_RESOLUTION.value().second;
|
||||
} else if (GRAPHICS_FS_ORIENTATION_SWAP) {
|
||||
log_misc(
|
||||
"graphics::d3d9",
|
||||
"swap full orientation {}x{} => {}x{}",
|
||||
params->BackBufferWidth, params->BackBufferHeight,
|
||||
params->BackBufferHeight, params->BackBufferWidth);
|
||||
std::swap(params->BackBufferWidth, params->BackBufferHeight);
|
||||
}
|
||||
}
|
||||
|
||||
log_info("graphics::d3d9",
|
||||
"D3D9 presentation parameters for adapter {}: BackBufferWidth: {}, BackBufferHeight: {}, "
|
||||
"D3D9Ex presentation parameters for adapter {}: BackBufferWidth: {}, BackBufferHeight: {}, "
|
||||
"Format: {}, BackBufferCount: {}, MultiSampleType: {}, MultiSampleQuality: {}, "
|
||||
"SwapEffect: {}, Windowed: {}, EnableAutoDepthStencil: {}, AutoDepthStencilFormat: {}, "
|
||||
"Flags: {}, FullScreen_RefreshRateInHz: {}, PresentationInterval: {}",
|
||||
@@ -797,7 +914,16 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::CreateDeviceEx(
|
||||
}
|
||||
if (pFullscreenDisplayMode) {
|
||||
for (size_t i = 0; i < num_adapters; i++) {
|
||||
const auto *fullscreen_display_mode = &pFullscreenDisplayMode[i];
|
||||
auto *fullscreen_display_mode = &pFullscreenDisplayMode[i];
|
||||
|
||||
if (!GRAPHICS_WINDOWED && i == 0) {
|
||||
if (GRAPHICS_FS_CUSTOM_RESOLUTION.has_value()) {
|
||||
fullscreen_display_mode->Width = GRAPHICS_FS_CUSTOM_RESOLUTION.value().first;
|
||||
fullscreen_display_mode->Height = GRAPHICS_FS_CUSTOM_RESOLUTION.value().second;
|
||||
} else if (GRAPHICS_FS_ORIENTATION_SWAP) {
|
||||
std::swap(fullscreen_display_mode->Width, fullscreen_display_mode->Height);
|
||||
}
|
||||
}
|
||||
|
||||
log_info("graphics::d3d9",
|
||||
"D3D9Ex fullscreen display mode for adapter {}: Width: {}, Height: {}, RefreshRate: {}, "
|
||||
@@ -824,6 +950,7 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::CreateDeviceEx(
|
||||
|
||||
pPresentationParameters->Windowed = true;
|
||||
pPresentationParameters->FullScreen_RefreshRateInHz = 0;
|
||||
update_backbuffer_dimensions(pPresentationParameters);
|
||||
pFullscreenDisplayMode = nullptr;
|
||||
} else if (GRAPHICS_FORCE_REFRESH > 0) {
|
||||
log_info("graphics::d3d9", "force refresh rate: {} => {} Hz",
|
||||
@@ -845,6 +972,13 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::CreateDeviceEx(
|
||||
BehaviorFlags &= ~D3DCREATE_ADAPTERGROUP_DEVICE;
|
||||
}
|
||||
|
||||
if (GRAPHICS_FORCE_VSYNC_BUFFER.has_value()) {
|
||||
log_info("graphics::d3d9", "force BackBufferCount: {} => {}",
|
||||
pPresentationParameters->BackBufferCount,
|
||||
GRAPHICS_FORCE_VSYNC_BUFFER.value());
|
||||
pPresentationParameters->BackBufferCount = GRAPHICS_FORCE_VSYNC_BUFFER.value();
|
||||
}
|
||||
|
||||
// call original
|
||||
HRESULT result = static_cast<IDirect3D9Ex *>(this->pReal)->CreateDeviceEx(
|
||||
Adapter,
|
||||
@@ -860,7 +994,7 @@ HRESULT STDMETHODCALLTYPE WrappedIDirect3D9::CreateDeviceEx(
|
||||
|
||||
// log error
|
||||
log_warning("graphics::d3d9", "CreateDeviceEx failed, hr={}", FMT_HRESULT(result));
|
||||
} else {
|
||||
} else if (!D3D9_DEVICE_HOOK_DISABLE) {
|
||||
graphics_hook_window(hFocusWindow, pPresentationParameters);
|
||||
|
||||
*ppReturnedDeviceInterface = new WrappedIDirect3DDevice9(hFocusWindow, *ppReturnedDeviceInterface);
|
||||
@@ -950,13 +1084,11 @@ IDirect3DSurface9 *graphics_d3d9_ldj_get_sub_screen() {
|
||||
static void graphics_d3d9_ldj_on_present(IDirect3DDevice9 *wrapped_device) {
|
||||
if (!ATTEMPTED_SUB_SWAP_CHAIN_ACQUIRE && SUB_SWAP_CHAIN == nullptr) {
|
||||
ATTEMPTED_SUB_SWAP_CHAIN_ACQUIRE = true;
|
||||
|
||||
HRESULT hr = wrapped_device->GetSwapChain(1, &SUB_SWAP_CHAIN);
|
||||
if (FAILED(hr)) {
|
||||
log_warning(
|
||||
"graphics::d3d9",
|
||||
"failed to acquire sub screeen swap chain; "
|
||||
"this can be ignored if game does not have sub screen, hr={}",
|
||||
"failed to acquire sub screeen swap chain! hr={}",
|
||||
FMT_HRESULT(hr));
|
||||
return;
|
||||
}
|
||||
@@ -1160,7 +1292,12 @@ void graphics_d3d9_on_present(
|
||||
device->EndScene();
|
||||
}
|
||||
|
||||
if (avs::game::is_model({"LDJ", "KFC"})) {
|
||||
// for IIDX TDJ / SDVX UFC, handle subscreen
|
||||
const bool is_vm =
|
||||
avs::game::is_model("KFC") &&
|
||||
(avs::game::SPEC[0] == 'G' || avs::game::SPEC[0] == 'H');
|
||||
const bool is_tdj = avs::game::is_model("LDJ") && games::iidx::TDJ_MODE;
|
||||
if (is_vm || is_tdj) {
|
||||
graphics_d3d9_ldj_on_present(wrapped_device);
|
||||
}
|
||||
|
||||
@@ -1282,3 +1419,39 @@ void graphics_d3d9_on_present(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void update_backbuffer_dimensions(D3DPRESENT_PARAMETERS *params) {
|
||||
if (!GRAPHICS_WINDOW_BACKBUFFER_SCALE) {
|
||||
return;
|
||||
}
|
||||
|
||||
// only do this for primary adapter
|
||||
static bool first_adapter_hooked = false;
|
||||
if (first_adapter_hooked) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (GRAPHICS_WINDOW_SIZE.has_value()) {
|
||||
first_adapter_hooked = true;
|
||||
params->BackBufferWidth = GRAPHICS_WINDOW_SIZE.value().first;
|
||||
params->BackBufferHeight = GRAPHICS_WINDOW_SIZE.value().second;
|
||||
log_info(
|
||||
"graphics::d3d9",
|
||||
"override BackBufferWidth / BackBufferHeight with {}x{} (from -windowresize)",
|
||||
params->BackBufferWidth,
|
||||
params->BackBufferHeight);
|
||||
return;
|
||||
}
|
||||
|
||||
if (cfg::SCREENRESIZE->client_width != 0 && cfg::SCREENRESIZE->client_height != 0) {
|
||||
first_adapter_hooked = true;
|
||||
params->BackBufferWidth = cfg::SCREENRESIZE->client_width;
|
||||
params->BackBufferHeight = cfg::SCREENRESIZE->client_height;
|
||||
log_info(
|
||||
"graphics::d3d9",
|
||||
"override BackBufferWidth / BackBufferHeight with {}x{} (from screen resize config file)",
|
||||
params->BackBufferWidth,
|
||||
params->BackBufferHeight);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "hooks/graphics/graphics.h"
|
||||
#include "overlay/overlay.h"
|
||||
#include "util/flags_helper.h"
|
||||
#include "util/utils.h"
|
||||
#include "cfg/screen_resize.h"
|
||||
|
||||
#include "d3d9_backend.h"
|
||||
@@ -692,88 +693,181 @@ static IDirect3DSurface9 *backbuffer = nullptr;
|
||||
static LPDIRECT3DSWAPCHAIN9 mSwapChain = nullptr;
|
||||
static IDirect3DTexture9* tex;
|
||||
|
||||
static UINT topSurface_width = 0;
|
||||
static UINT topSurface_height = 0;
|
||||
static float topSurface_aspect_ratio = 1.f;
|
||||
|
||||
void SurfaceHook(IDirect3DDevice9 *pReal) {
|
||||
// log_misc("graphics::d3d9", "SurfaceHook called");
|
||||
D3DPRESENT_PARAMETERS param {};
|
||||
|
||||
pReal->GetSwapChain(0, &mSwapChain);
|
||||
mSwapChain->GetPresentParameters(¶m);
|
||||
|
||||
// phase 0 - create a surface that has the same aspect ratio as the original back buffer
|
||||
// but larger in size. this is needed to ensure that when the user zooms out, we have
|
||||
// sufficient buffer space (black border) around the original image
|
||||
//
|
||||
// (only done once)
|
||||
if (!topSurface) {
|
||||
if (pReal->CreateTexture(4096, 4096, 1,
|
||||
topSurface_width = param.BackBufferWidth * 3;
|
||||
topSurface_height = param.BackBufferHeight * 3;
|
||||
topSurface_aspect_ratio = (float)topSurface_width / (float)topSurface_height;
|
||||
if (pReal->CreateTexture(topSurface_width, topSurface_height, 1,
|
||||
D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,
|
||||
D3DPOOL_DEFAULT, &tex, NULL) != D3D_OK) {
|
||||
log_misc("graphics::d3d9", "create texture failed");
|
||||
|
||||
log_warning("graphics::d3d9", "SurfaceHook - create texture failed");
|
||||
}
|
||||
log_misc("graphics::d3d9", "Backbuffer: {} {} {}",
|
||||
param.BackBufferWidth, param.BackBufferHeight, param.BackBufferCount);
|
||||
tex->GetSurfaceLevel(0, &topSurface);
|
||||
|
||||
log_misc(
|
||||
"graphics::d3d9", "SurfaceHook - Backbuffer: {} {} {}",
|
||||
param.BackBufferWidth, param.BackBufferHeight, param.BackBufferCount);
|
||||
|
||||
tex->GetSurfaceLevel(0, &topSurface);
|
||||
if (mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &backbuffer) != D3D_OK) {
|
||||
log_misc("graphics::d3d9", "GetBackBuffer failed");
|
||||
log_warning("graphics::d3d9", "SurfaceHook - GetBackBuffer failed");
|
||||
}
|
||||
}
|
||||
|
||||
const int rectLeft = 1024;
|
||||
const int rectTop = 576;
|
||||
// pre-calculate dimensions used for phase 1
|
||||
const int rectLeft = param.BackBufferWidth;
|
||||
const int rectTop = param.BackBufferHeight;
|
||||
const int w = param.BackBufferWidth;
|
||||
const int h = param.BackBufferHeight;
|
||||
|
||||
D3DLOCKED_RECT rect;
|
||||
HRESULT hr;
|
||||
topSurface->LockRect(&rect, NULL, D3DLOCK_DONOTWAIT);
|
||||
|
||||
// phase 1 - copy the original back buffer onto the new surface.
|
||||
// we draw it 1:1 in the center of the surface.
|
||||
// if orientation is swapped, fix up the rect and draw it in the center.
|
||||
//
|
||||
// for this phase we always render with the same rect so that it's always overwritten
|
||||
// by a new frame on the same spot.
|
||||
RECT originRect {
|
||||
rectLeft,
|
||||
rectTop,
|
||||
(LONG)(rectLeft + w),
|
||||
(LONG)(rectTop + h),
|
||||
};
|
||||
if (GRAPHICS_FS_ORIENTATION_SWAP) {
|
||||
originRect.left = (topSurface_width / 2) - (GRAPHICS_FS_ORIGINAL_WIDTH / 2);
|
||||
originRect.top = (topSurface_height / 2) - (GRAPHICS_FS_ORIGINAL_HEIGHT / 2);
|
||||
originRect.right = originRect.left + GRAPHICS_FS_ORIGINAL_WIDTH;
|
||||
originRect.bottom = originRect.top + GRAPHICS_FS_ORIGINAL_HEIGHT;
|
||||
}
|
||||
|
||||
// log_misc(
|
||||
// "graphics::d3d9", "originRect: {} {} {} {}",
|
||||
// originRect.left, originRect.top, originRect.right, originRect.bottom);
|
||||
|
||||
hr = pReal->StretchRect(
|
||||
backbuffer, nullptr,
|
||||
topSurface, &originRect,
|
||||
D3DTEXF_LINEAR);
|
||||
if (hr != D3D_OK) {
|
||||
log_warning(
|
||||
"graphics::d3d9",
|
||||
"SurfaceHook - StretchRect backbuffer failed, rect: {} {} {} {}",
|
||||
originRect.left, originRect.top, originRect.right, originRect.bottom);
|
||||
}
|
||||
topSurface->UnlockRect();
|
||||
|
||||
// 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 {
|
||||
rectLeft,
|
||||
rectTop,
|
||||
(LONG)(rectLeft + w),
|
||||
(LONG)(rectTop + h),
|
||||
};
|
||||
if (cfg::SCREENRESIZE->enable_screen_resize) {
|
||||
auto& scene = cfg::SCREENRESIZE->scene_settings[cfg::SCREENRESIZE->screen_resize_current_scene];
|
||||
auto w_new = w / scene.scale_x;
|
||||
auto h_new = h / scene.scale_y;
|
||||
|
||||
// stretch to add top/left offset to avoid going negative
|
||||
topSurface->LockRect(&rect, NULL, D3DLOCK_DONOTWAIT);
|
||||
auto hr = pReal->StretchRect(
|
||||
backbuffer, nullptr,
|
||||
topSurface, &targetRect,
|
||||
D3DTEXF_LINEAR);
|
||||
// make sure the scaling is within bounds
|
||||
if (cfg::SCREENRESIZE->client_keep_aspect_ratio) {
|
||||
if (w_new > topSurface_width || h_new > topSurface_height) {
|
||||
w_new = topSurface_width;
|
||||
h_new = topSurface_height;
|
||||
}
|
||||
} else {
|
||||
w_new = MIN(w_new, topSurface_width);
|
||||
h_new = MIN(h_new, topSurface_height);
|
||||
}
|
||||
|
||||
if (hr != D3D_OK) {
|
||||
log_misc("graphics::d3d9", "StretchRect backbuffer failed");
|
||||
}
|
||||
topSurface->UnlockRect();
|
||||
targetRect.left = (topSurface_width / 2) - (w_new / 2) - scene.offset_x;
|
||||
targetRect.top = (topSurface_height / 2) - (h_new / 2) - scene.offset_y;
|
||||
targetRect.right = targetRect.left + w_new;
|
||||
targetRect.bottom = targetRect.top + h_new;
|
||||
|
||||
// do the actual zoom / offset math
|
||||
if (cfg::SCREENRESIZE->centered) {
|
||||
targetRect.right = (w + rectLeft) / cfg::SCREENRESIZE->scale_x;
|
||||
targetRect.bottom = (h + rectTop) / cfg::SCREENRESIZE->scale_y;
|
||||
const LONG deltaH = ((targetRect.bottom - targetRect.top) - h) / 2;
|
||||
const LONG deltaW = ((targetRect.right - targetRect.left) - w) / 2;
|
||||
targetRect.top -= deltaH;
|
||||
targetRect.bottom -= deltaH;
|
||||
targetRect.left -= deltaW;
|
||||
targetRect.right -= deltaW;
|
||||
} else {
|
||||
targetRect.left -= cfg::SCREENRESIZE->offset_x;
|
||||
targetRect.top += cfg::SCREENRESIZE->offset_y;
|
||||
targetRect.right = -cfg::SCREENRESIZE->offset_x;
|
||||
targetRect.right += (w + rectLeft) / cfg::SCREENRESIZE->scale_x;
|
||||
targetRect.bottom = cfg::SCREENRESIZE->offset_y;
|
||||
targetRect.bottom += (h + rectTop) / cfg::SCREENRESIZE->scale_y;
|
||||
// boundary check to prevent StretchRect failures
|
||||
if (targetRect.left < 0) {
|
||||
targetRect.left = 0;
|
||||
targetRect.right = w_new;
|
||||
} else if (targetRect.right > (LONG)topSurface_width) {
|
||||
targetRect.left = topSurface_width - w_new;
|
||||
targetRect.right = topSurface_width;
|
||||
}
|
||||
if (targetRect.top < 0) {
|
||||
targetRect.top = 0;
|
||||
targetRect.bottom = h_new;
|
||||
} else if (targetRect.bottom > (LONG)topSurface_height) {
|
||||
targetRect.top = topSurface_height - h_new;
|
||||
targetRect.bottom = topSurface_height;
|
||||
}
|
||||
|
||||
} else if (GRAPHICS_FS_ORIENTATION_SWAP) {
|
||||
// increase the viewport to fit the image on screen (effectively, zoom out a little)
|
||||
if (GRAPHICS_FS_ORIGINAL_WIDTH < GRAPHICS_FS_ORIGINAL_HEIGHT) {
|
||||
// portrait game on landscape display
|
||||
// height should match the original image
|
||||
targetRect.top = originRect.top;
|
||||
targetRect.bottom = originRect.bottom;
|
||||
|
||||
// width of viewport should be wider, effectively shrinking the image on x-axis when rendered
|
||||
const auto w_new = GRAPHICS_FS_ORIGINAL_HEIGHT * topSurface_aspect_ratio;
|
||||
targetRect.left = (topSurface_width / 2) - (w_new / 2);
|
||||
targetRect.right = targetRect.left + w_new;
|
||||
} else {
|
||||
// landscape game on portrait display
|
||||
targetRect.left = originRect.left;
|
||||
targetRect.right = originRect.right;
|
||||
|
||||
// height of viewport should be taller, effectively shrinking the image on y-axis when rendered
|
||||
const auto h_new = GRAPHICS_FS_ORIGINAL_WIDTH / topSurface_aspect_ratio;
|
||||
targetRect.top = (topSurface_height / 2) - (h_new / 2);
|
||||
targetRect.bottom = targetRect.top + h_new;
|
||||
}
|
||||
}
|
||||
|
||||
// draw to back buffer
|
||||
// log_misc("graphics::d3d9", "targetRect: {} {} {} {}",
|
||||
// targetRect.left, targetRect.top, targetRect.right, targetRect.bottom);
|
||||
|
||||
// phase 3 - draw the surface to back buffer
|
||||
backbuffer->LockRect(&rect, NULL, D3DLOCK_DONOTWAIT);
|
||||
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,
|
||||
cfg::SCREENRESIZE->enable_linear_filter ? D3DTEXF_LINEAR : D3DTEXF_NONE);
|
||||
use_linear_filter ? D3DTEXF_LINEAR : D3DTEXF_NONE);
|
||||
backbuffer->UnlockRect();
|
||||
if (hr != D3D_OK) {
|
||||
log_misc("graphics::d3d9", "StretchRect targetRect failed");
|
||||
log_warning(
|
||||
"graphics::d3d9",
|
||||
"SurfaceHook - StretchRect targetRect failed, you must reset image scaling to default values! rect: {} {} {} {}",
|
||||
targetRect.left, targetRect.top, targetRect.right, targetRect.bottom);
|
||||
}
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE WrappedIDirect3DDevice9::EndScene() {
|
||||
WRAP_DEBUG;
|
||||
|
||||
if (cfg::SCREENRESIZE->enable_screen_resize) {
|
||||
if (cfg::SCREENRESIZE->enable_screen_resize || GRAPHICS_FS_ORIENTATION_SWAP) {
|
||||
SurfaceHook(pReal);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user