From ae433ccb1d829fe665096fb56c26295fc725c5be Mon Sep 17 00:00:00 2001 From: bicarus <202771338+bicarus-dev@users.noreply.github.com> Date: Sat, 28 Nov 2026 20:21:33 -0800 Subject: [PATCH] iidx: movie decoder workaround for dxvk (#939) ## Link to GitHub Issue or related Pull Request, if one exists https://github.com/eamuse/sp2xpatcher/pull/12 ## Description of change Subsumes https://github.com/eamuse/sp2xpatcher/pull/12 into a hook-based fix that is automatically applied when Vulcan is detected. ## Testing --- src/spice2x/CMakeLists.txt | 1 + src/spice2x/games/iidx/dxva2.cpp | 114 ++++++++++++++++++ src/spice2x/games/iidx/dxva2.h | 7 ++ src/spice2x/games/iidx/iidx.cpp | 8 +- .../graphics/backends/d3d9/d3d9_backend.cpp | 31 +++++ .../graphics/backends/d3d9/d3d9_backend.h | 1 + src/spice2x/launcher/options.cpp | 15 +++ src/spice2x/launcher/options.h | 1 + 8 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 src/spice2x/games/iidx/dxva2.cpp create mode 100644 src/spice2x/games/iidx/dxva2.h diff --git a/src/spice2x/CMakeLists.txt b/src/spice2x/CMakeLists.txt index ab87161b..5b71fa67 100644 --- a/src/spice2x/CMakeLists.txt +++ b/src/spice2x/CMakeLists.txt @@ -453,6 +453,7 @@ set(SOURCE_FILES ${SOURCE_FILES} games/hpm/hpm.cpp games/hpm/io.cpp games/iidx/iidx.cpp + games/iidx/dxva2.cpp games/iidx/io.cpp games/iidx/poke.cpp games/iidx/bi2a.cpp diff --git a/src/spice2x/games/iidx/dxva2.cpp b/src/spice2x/games/iidx/dxva2.cpp new file mode 100644 index 00000000..c76ece3f --- /dev/null +++ b/src/spice2x/games/iidx/dxva2.cpp @@ -0,0 +1,114 @@ +#include "dxva2.h" + +#include +#include +#include + +#include +#include +#include + +#include "avs/game.h" +#include "hooks/graphics/backends/d3d9/d3d9_backend.h" +#include "util/detour.h" +#include "util/logging.h" + +namespace games::iidx { + static decltype(DXVA2CreateDirect3DDeviceManager9) *create_dxva2_manager_orig = nullptr; + static bool force_dxva2_workaround = false; + + static bool has_video_player_with_dxva2_fallback(HMODULE module) { + // iidx34+ tries AVMfVideoPlayerWithDxva and falls back to MF-based software decoder + // (can install dxva2 hooks to trigger the fallback) + // + // older iidx (003 variants of iidx29-30?) uses MfVideo::DxvaDeviceImpl and does not automatically fallback + // (requires a DLL patch to trigger the fallback) + // + // this function detects the AVMfVideoPlayerWithDxva case and returns true + + constexpr char player_type[] = ".?AVMfVideoPlayerWithDxva@Video@Graphics@@"; + const auto *image = reinterpret_cast(module); + const auto *dos_header = reinterpret_cast(image); + const auto *nt_headers = + reinterpret_cast(image + dos_header->e_lfanew); + + const auto *section = IMAGE_FIRST_SECTION(nt_headers); + for (WORD i = 0; i < nt_headers->FileHeader.NumberOfSections; i++, section++) { + if (std::memcmp(section->Name, ".data", sizeof(".data")) != 0) { + continue; + } + + const auto size = std::min(section->Misc.VirtualSize, section->SizeOfRawData); + const std::string_view data(image + section->VirtualAddress, size); + const std::string_view marker(player_type, sizeof(player_type)); + return data.find(marker) != std::string_view::npos; + } + return false; + } + + static HRESULT WINAPI create_dxva2_manager_hook(UINT *reset_token, IDirect3DDeviceManager9 **manager) { + if (!force_dxva2_workaround && !graphics_d3d9_is_dxvk()) { + return create_dxva2_manager_orig(reset_token, manager); + } + + // dxvk can create the manager without providing a usable decoder; failing creation + // lets iidx fall back to software decoding on builds that support it. + static std::atomic_bool logged = false; + if (!logged.exchange(true, std::memory_order_relaxed)) { + log_info( + "iidx", + "DXVA2 workaround active ({}): returning E_FAIL from DXVA2CreateDirect3DDeviceManager9", + force_dxva2_workaround ? "forced on by user" : "automatically for DXVK"); + } + return E_FAIL; + } + + void init_dxva2_workaround(std::string_view mode) { + // default (no selection) is auto + if (mode.empty()) { + mode = "auto"; + } + + // forced off by user + if (mode == "off") { + return; + } + + const bool forced_on_by_user = (mode == "on"); + + // invalid option value + if (!forced_on_by_user && mode != "auto") { + log_warning("iidx", "invalid DXVA2 workaround mode '{}'; expected auto, on, or off", mode); + return; + } + + const auto module = avs::game::DLL_INSTANCE; + if (!module) { + if (forced_on_by_user) { + log_warning("iidx", "DXVA2 workaround requested, but the game DLL is not loaded"); + } + return; + } + + if (!forced_on_by_user && !has_video_player_with_dxva2_fallback(module)) { + return; + } + + force_dxva2_workaround = forced_on_by_user; + + // install hook + create_dxva2_manager_orig = detour::iat_try( + "DXVA2CreateDirect3DDeviceManager9", create_dxva2_manager_hook, module, "dxva2.dll"); + + if (!create_dxva2_manager_orig) { + if (forced_on_by_user) { + log_warning("iidx", "DXVA2 workaround requested, but the DXVA2 import was not found"); + } + return; + } + + if (forced_on_by_user) { + log_info("iidx", "DXVA2 disabled by user"); + } + } +} diff --git a/src/spice2x/games/iidx/dxva2.h b/src/spice2x/games/iidx/dxva2.h new file mode 100644 index 00000000..439688c3 --- /dev/null +++ b/src/spice2x/games/iidx/dxva2.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +namespace games::iidx { + void init_dxva2_workaround(std::string_view mode); +} diff --git a/src/spice2x/games/iidx/iidx.cpp b/src/spice2x/games/iidx/iidx.cpp index d165da8d..4250a572 100644 --- a/src/spice2x/games/iidx/iidx.cpp +++ b/src/spice2x/games/iidx/iidx.cpp @@ -39,6 +39,7 @@ #include "bi2a.h" #include "bi2x_hook.h" +#include "dxva2.h" #include "ezusb.h" #include "io.h" #include "poke.h" @@ -311,8 +312,9 @@ namespace games::iidx { void IIDXGame::attach() { Game::attach(); - // IO boards auto options = games::get_options(eamuse_get_game()); + + // IO boards if (!options->at(launcher::Options::IIDXBIO2FW).value_bool()) { // reduce boot wait time @@ -452,6 +454,10 @@ namespace games::iidx { #if SPICE64 + // dxvk workaround for movie decoder + init_dxva2_workaround( + options->at(launcher::Options::IIDXNoDxva2).value_text()); + if (TDJ_MODE) { sysutils::hook_EnumDisplayDevicesA(); } diff --git a/src/spice2x/hooks/graphics/backends/d3d9/d3d9_backend.cpp b/src/spice2x/hooks/graphics/backends/d3d9/d3d9_backend.cpp index fb545c14..b9ca74ab 100644 --- a/src/spice2x/hooks/graphics/backends/d3d9/d3d9_backend.cpp +++ b/src/spice2x/hooks/graphics/backends/d3d9/d3d9_backend.cpp @@ -286,8 +286,37 @@ static bool is_dx9_on_12_enabled() { return result; } +static bool detect_dxvk(HMODULE d3d9) { + // **mostly** reliable detection for dxvk; these are used internally for debugging + constexpr WORD register_annotation = 28257; + constexpr WORD unregister_annotation = 28258; + if (!d3d9 || + !GetProcAddress(d3d9, MAKEINTRESOURCEA(register_annotation)) || + !GetProcAddress(d3d9, MAKEINTRESOURCEA(unregister_annotation))) { + return false; + } + + // log once + static std::atomic_bool logged = false; + if (!logged.exchange(true, std::memory_order_relaxed)) { + log_info("graphics::d3d9", "DXVK detected"); + deferredlogs::defer_error_messages({ + "dxvk detected", + " Vulkan-based renderer may cause graphical issues or crashes", + " it should only be used if necessary on your PC", + }); + } + + return true; +} + +bool graphics_d3d9_is_dxvk() { + return detect_dxvk(libutils::try_module("d3d9.dll")); +} + static IDirect3D9 *WINAPI Direct3DCreate9_hook(UINT SDKVersion) { log_misc("graphics::d3d9", "Direct3DCreate9 hook hit"); + graphics_d3d9_is_dxvk(); // remove hook if (D3D9_DIRECT3D_CREATE9_ADR) { @@ -330,6 +359,7 @@ static IDirect3D9 *WINAPI Direct3DCreate9_hook(UINT SDKVersion) { static HRESULT WINAPI Direct3DCreate9Ex_hook(UINT SDKVersion, IDirect3D9Ex **d3d9ex) { log_misc("graphics::d3d9", "Direct3DCreate9Ex hook hit"); + graphics_d3d9_is_dxvk(); // call original HRESULT result; @@ -366,6 +396,7 @@ void graphics_d3d9_init() { if (!d3d9) { log_info("graphics::d3d9", "skipping inline hooks"); } else { + detect_dxvk(d3d9); // 9 on 12 Direct3DCreate9On12_orig = (Direct3DCreate9On12_t) libutils::try_proc(d3d9, "Direct3DCreate9On12"); diff --git a/src/spice2x/hooks/graphics/backends/d3d9/d3d9_backend.h b/src/spice2x/hooks/graphics/backends/d3d9/d3d9_backend.h index f08b8e90..47ee3be9 100644 --- a/src/spice2x/hooks/graphics/backends/d3d9/d3d9_backend.h +++ b/src/spice2x/hooks/graphics/backends/d3d9/d3d9_backend.h @@ -12,6 +12,7 @@ static const GUID IID_WrappedIDirect3D9 = { }; void graphics_d3d9_init(); +bool graphics_d3d9_is_dxvk(); void graphics_d3d9_on_present( HWND hFocusWindow, IDirect3DDevice9 *device, diff --git a/src/spice2x/launcher/options.cpp b/src/spice2x/launcher/options.cpp index 43c874ed..433b1e17 100644 --- a/src/spice2x/launcher/options.cpp +++ b/src/spice2x/launcher/options.cpp @@ -846,6 +846,21 @@ static const std::vector OPTION_DEFINITIONS = std::invoke([]() .game_name = "Beatmania IIDX", .category = "Game Overlay", }; + c[launcher::Options::IIDXNoDxva2] = { + .title = "IIDX Disable DXVA2 Decoder", + .name = "iidxnodxva2", + .desc = + "Forcibly disables the DXVA2 decoder, causing the game to use the software decoder instead " + "on some versions of IIDX", + .type = OptionType::Enum, + .game_name = "Beatmania IIDX", + .category = "Advanced Game Options", + .elements = { + {"auto", ""}, + {"on", ""}, + {"off", ""}, + }, + }; c[launcher::Options::LoadSoundVoltexModule] = { .title = "Force Load Sound Voltex Module", .name = "sdvx", diff --git a/src/spice2x/launcher/options.h b/src/spice2x/launcher/options.h index fb84abaa..3fae1f87 100644 --- a/src/spice2x/launcher/options.h +++ b/src/spice2x/launcher/options.h @@ -83,6 +83,7 @@ namespace launcher { spice2x_IIDXLEDColor, spice2x_IIDXLEDPos, IIDXLEDBorderless, + IIDXNoDxva2, LoadSoundVoltexModule, SDVXForce720p, SDVXPrinterEmulation,