mirror of
https://github.com/spice2x/spice2x.github.io.git
synced 2026-12-05 19:16:42 -08:00
## 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
115 lines
4.1 KiB
C++
115 lines
4.1 KiB
C++
#include "dxva2.h"
|
|
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <cstring>
|
|
|
|
#include <windows.h>
|
|
#include <d3d9.h>
|
|
#include <dxva2api.h>
|
|
|
|
#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<const char *>(module);
|
|
const auto *dos_header = reinterpret_cast<const IMAGE_DOS_HEADER *>(image);
|
|
const auto *nt_headers =
|
|
reinterpret_cast<const IMAGE_NT_HEADERS *>(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");
|
|
}
|
|
}
|
|
}
|