fix line endings
This commit is contained in:
@@ -1,304 +1,304 @@
|
||||
// dx11 / dxgi hook entrypoint. trampolines d3d11.dll / dxgi.dll exports
|
||||
// the moment those DLLs appear (LDR notification + poll-thread fallback),
|
||||
// then drives proactive vtable capture so we don't lose the race against
|
||||
// the execexe loader. per-vtable hook implementations live in the sibling
|
||||
// files (d3d11_swapchain / d3d11_factory / d3d11_vtable_capture /
|
||||
// d3d11_screenshot).
|
||||
//
|
||||
// note: never LoadLibrary d3d11/dxgi -- execexe pre-loads them itself and
|
||||
// fails (error 0xa) if they're already in the loader's module list.
|
||||
//
|
||||
// 64-bit only.
|
||||
|
||||
#include "d3d11_backend.h"
|
||||
|
||||
#ifndef SPICE_D3D11
|
||||
|
||||
void graphics_d3d11_init() {}
|
||||
void graphics_d3d11_shutdown() {}
|
||||
|
||||
#else
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <cwchar>
|
||||
#include <mutex>
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
#include <dxgi1_2.h>
|
||||
|
||||
#include "d3d11_internal.h"
|
||||
#include "util/nt_loader.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using D3D11CreateDeviceAndSwapChain_t = HRESULT(WINAPI *)(
|
||||
IDXGIAdapter *, D3D_DRIVER_TYPE, HMODULE, UINT,
|
||||
const D3D_FEATURE_LEVEL *, UINT, UINT,
|
||||
const DXGI_SWAP_CHAIN_DESC *, IDXGISwapChain **,
|
||||
ID3D11Device **, D3D_FEATURE_LEVEL *, ID3D11DeviceContext **);
|
||||
using CreateDXGIFactory_t = HRESULT(WINAPI *)(REFIID, void **);
|
||||
using CreateDXGIFactory1_t = HRESULT(WINAPI *)(REFIID, void **);
|
||||
using CreateDXGIFactory2_t = HRESULT(WINAPI *)(UINT, REFIID, void **);
|
||||
|
||||
D3D11CreateDeviceAndSwapChain_t D3D11CreateDeviceAndSwapChain_orig = nullptr;
|
||||
CreateDXGIFactory_t CreateDXGIFactory_orig = nullptr;
|
||||
CreateDXGIFactory1_t CreateDXGIFactory1_orig = nullptr;
|
||||
CreateDXGIFactory2_t CreateDXGIFactory2_orig = nullptr;
|
||||
|
||||
std::atomic<bool> g_d3d11_exports_hooked { false };
|
||||
std::atomic<bool> g_dxgi_exports_hooked { false };
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// top-level export hooks
|
||||
|
||||
HRESULT WINAPI D3D11CreateDeviceAndSwapChain_hook(
|
||||
IDXGIAdapter *pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags,
|
||||
const D3D_FEATURE_LEVEL *pFeatureLevels, UINT FeatureLevels, UINT SDKVersion,
|
||||
const DXGI_SWAP_CHAIN_DESC *pSwapChainDesc, IDXGISwapChain **ppSwapChain,
|
||||
ID3D11Device **ppDevice, D3D_FEATURE_LEVEL *pFeatureLevel,
|
||||
ID3D11DeviceContext **ppImmediateContext)
|
||||
{
|
||||
HRESULT res = D3D11CreateDeviceAndSwapChain_orig(
|
||||
pAdapter, DriverType, Software, Flags,
|
||||
pFeatureLevels, FeatureLevels, SDKVersion,
|
||||
pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
|
||||
if (SUCCEEDED(res) && ppSwapChain && *ppSwapChain) {
|
||||
if (pSwapChainDesc) {
|
||||
d3d11_hooks::note_main_hwnd(pSwapChainDesc->OutputWindow);
|
||||
}
|
||||
d3d11_hooks::install_swapchain_hooks(*ppSwapChain);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
#define DEFINE_FACTORY_HOOK(NAME, SIG_PARAMS, ORIG_ARGS) \
|
||||
HRESULT WINAPI NAME##_hook SIG_PARAMS { \
|
||||
HRESULT res = NAME##_orig ORIG_ARGS; \
|
||||
if (SUCCEEDED(res) && ppFactory && *ppFactory) { \
|
||||
d3d11_hooks::install_factory_hooks( \
|
||||
reinterpret_cast<IUnknown *>(*ppFactory)); \
|
||||
} \
|
||||
return res; \
|
||||
}
|
||||
|
||||
DEFINE_FACTORY_HOOK(CreateDXGIFactory,
|
||||
(REFIID riid, void **ppFactory),
|
||||
(riid, ppFactory))
|
||||
DEFINE_FACTORY_HOOK(CreateDXGIFactory1,
|
||||
(REFIID riid, void **ppFactory),
|
||||
(riid, ppFactory))
|
||||
DEFINE_FACTORY_HOOK(CreateDXGIFactory2,
|
||||
(UINT Flags, REFIID riid, void **ppFactory),
|
||||
(Flags, riid, ppFactory))
|
||||
|
||||
#undef DEFINE_FACTORY_HOOK
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// export trampoline plumbing
|
||||
|
||||
// serializes trampoline_export() so the LDR notification callback and the
|
||||
// poll thread don't race each other into MinHook against the same target.
|
||||
std::mutex g_export_mutex;
|
||||
|
||||
bool trampoline_export(const char *dll, const char *name, void *hook, void **orig) {
|
||||
std::lock_guard<std::mutex> lock(g_export_mutex);
|
||||
if (*orig) {
|
||||
return true;
|
||||
}
|
||||
HMODULE mod = GetModuleHandleA(dll);
|
||||
if (!mod) {
|
||||
return false;
|
||||
}
|
||||
void *addr = reinterpret_cast<void *>(GetProcAddress(mod, name));
|
||||
if (!addr) {
|
||||
return false;
|
||||
}
|
||||
*orig = addr; // trampoline_try reads *orig before overwriting it.
|
||||
if (!detour::trampoline_try(addr, hook, orig)) {
|
||||
*orig = nullptr;
|
||||
return false;
|
||||
}
|
||||
log_info("graphics::d3d11", "trampolined {}!{}", dll, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
void try_install_d3d11_exports() {
|
||||
if (g_d3d11_exports_hooked) {
|
||||
return;
|
||||
}
|
||||
if (trampoline_export("d3d11.dll", "D3D11CreateDeviceAndSwapChain",
|
||||
(void *) D3D11CreateDeviceAndSwapChain_hook,
|
||||
(void **) &D3D11CreateDeviceAndSwapChain_orig)) {
|
||||
g_d3d11_exports_hooked = true;
|
||||
}
|
||||
}
|
||||
|
||||
void try_install_dxgi_exports() {
|
||||
if (g_dxgi_exports_hooked) {
|
||||
return;
|
||||
}
|
||||
struct entry { const char *name; void *hook; void **orig; };
|
||||
const entry entries[] = {
|
||||
{ "CreateDXGIFactory", (void *) CreateDXGIFactory_hook,
|
||||
(void **) &CreateDXGIFactory_orig },
|
||||
{ "CreateDXGIFactory1", (void *) CreateDXGIFactory1_hook,
|
||||
(void **) &CreateDXGIFactory1_orig },
|
||||
{ "CreateDXGIFactory2", (void *) CreateDXGIFactory2_hook,
|
||||
(void **) &CreateDXGIFactory2_orig },
|
||||
};
|
||||
bool any = false;
|
||||
for (auto &e : entries) {
|
||||
any |= trampoline_export("dxgi.dll", e.name, e.hook, e.orig);
|
||||
}
|
||||
if (any) {
|
||||
g_dxgi_exports_hooked = true;
|
||||
}
|
||||
}
|
||||
|
||||
void try_capture_if_ready() {
|
||||
if (g_d3d11_exports_hooked && g_dxgi_exports_hooked) {
|
||||
d3d11_hooks::try_capture_vtables();
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// LDR notification + polling fallback
|
||||
|
||||
bool dll_name_ends_with(PCUNICODE_STRING name, const wchar_t *suffix) {
|
||||
if (!name || !name->Buffer) {
|
||||
return false;
|
||||
}
|
||||
const size_t n = name->Length / sizeof(WCHAR);
|
||||
const size_t s = wcslen(suffix);
|
||||
return n >= s && _wcsnicmp(name->Buffer + n - s, suffix, s) == 0;
|
||||
}
|
||||
|
||||
VOID CALLBACK ldr_dll_notification(
|
||||
ULONG reason, PCLDR_DLL_NOTIFICATION_DATA data, PVOID /*context*/)
|
||||
{
|
||||
if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED || !data) {
|
||||
return;
|
||||
}
|
||||
if (dll_name_ends_with(data->Loaded.BaseDllName, L"d3d11.dll")) {
|
||||
try_install_d3d11_exports();
|
||||
} else if (dll_name_ends_with(data->Loaded.BaseDllName, L"dxgi.dll")) {
|
||||
try_install_dxgi_exports();
|
||||
}
|
||||
}
|
||||
|
||||
// execexe maps d3d11/dxgi via a path that bypasses LdrLoadDll, so the
|
||||
// notification above never fires for those DLLs and we have to poll.
|
||||
std::atomic<bool> g_stop { false };
|
||||
std::thread g_poll_thread;
|
||||
std::mutex g_init_mutex;
|
||||
PVOID g_ldr_cookie = nullptr;
|
||||
|
||||
void poll_thread() {
|
||||
using namespace std::chrono_literals;
|
||||
for (int32_t i = 0; i < 120 && !g_stop.load(); ++i) {
|
||||
try_install_d3d11_exports();
|
||||
try_install_dxgi_exports();
|
||||
if (g_d3d11_exports_hooked && g_dxgi_exports_hooked) {
|
||||
d3d11_hooks::try_capture_vtables();
|
||||
return;
|
||||
}
|
||||
// sliced so shutdown doesn't have to wait a full second.
|
||||
for (int32_t s = 0; s < 10 && !g_stop.load(); ++s) {
|
||||
std::this_thread::sleep_for(100ms);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the overlay's imgui dx11 backend needs D3DCompile (d3dcompiler_XX.dll) to
|
||||
// build its shaders. _43 ships with the DX June 2010 redist on stock Win7;
|
||||
// _46/_47 come with newer Windows.
|
||||
bool d3dcompiler_available() {
|
||||
static const wchar_t *names[] = {
|
||||
L"d3dcompiler_47.dll",
|
||||
L"d3dcompiler_46.dll",
|
||||
L"d3dcompiler_43.dll",
|
||||
};
|
||||
for (auto name : names) {
|
||||
HMODULE mod = GetModuleHandleW(name);
|
||||
if (!mod) {
|
||||
mod = LoadLibraryW(name);
|
||||
}
|
||||
if (mod && GetProcAddress(mod, "D3DCompile")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void graphics_d3d11_init() {
|
||||
// dx11 titles always run under execexe. skipping on pure-dx9 games keeps
|
||||
// their startup path completely untouched (no exports patched, no poll
|
||||
// thread, no LDR callback).
|
||||
if (!GetModuleHandleW(L"execexe.dll")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// no d3dcompiler -> overlay can't build shaders; skip dx11 overlay
|
||||
if (!d3dcompiler_available()) {
|
||||
log_warning(
|
||||
"graphics::d3d11",
|
||||
"d3dcompiler not found; dx11 overlay disabled");
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(g_init_mutex);
|
||||
if (g_poll_thread.joinable()) {
|
||||
return; // already initialized
|
||||
}
|
||||
|
||||
log_info("graphics::d3d11", "initializing");
|
||||
|
||||
// trampoline now if either DLL is already in the PEB.
|
||||
try_install_d3d11_exports();
|
||||
try_install_dxgi_exports();
|
||||
try_capture_if_ready();
|
||||
|
||||
// catches standard LdrLoadDll loads.
|
||||
auto reg = reinterpret_cast<decltype(&LdrRegisterDllNotification)>(
|
||||
GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "LdrRegisterDllNotification"));
|
||||
if (reg) {
|
||||
NTSTATUS st = reg(0, ldr_dll_notification, nullptr, &g_ldr_cookie);
|
||||
if (NT_SUCCESS(st)) {
|
||||
log_info("graphics::d3d11", "registered LDR DLL notification");
|
||||
} else {
|
||||
g_ldr_cookie = nullptr;
|
||||
log_warning("graphics::d3d11",
|
||||
"LdrRegisterDllNotification failed: {:#x}", (unsigned long)st);
|
||||
}
|
||||
}
|
||||
|
||||
// catches the execexe loader path that bypasses LdrLoadDll.
|
||||
g_poll_thread = std::thread(poll_thread);
|
||||
}
|
||||
|
||||
void graphics_d3d11_shutdown() {
|
||||
std::lock_guard<std::mutex> lock(g_init_mutex);
|
||||
|
||||
// unregister first so the callback can't fire mid-teardown.
|
||||
if (g_ldr_cookie) {
|
||||
auto unreg = reinterpret_cast<decltype(&LdrUnregisterDllNotification)>(
|
||||
GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "LdrUnregisterDllNotification"));
|
||||
if (unreg) {
|
||||
unreg(g_ldr_cookie);
|
||||
}
|
||||
g_ldr_cookie = nullptr;
|
||||
}
|
||||
|
||||
g_stop.store(true);
|
||||
if (g_poll_thread.joinable()) {
|
||||
g_poll_thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
#endif // SPICE_D3D11
|
||||
// dx11 / dxgi hook entrypoint. trampolines d3d11.dll / dxgi.dll exports
|
||||
// the moment those DLLs appear (LDR notification + poll-thread fallback),
|
||||
// then drives proactive vtable capture so we don't lose the race against
|
||||
// the execexe loader. per-vtable hook implementations live in the sibling
|
||||
// files (d3d11_swapchain / d3d11_factory / d3d11_vtable_capture /
|
||||
// d3d11_screenshot).
|
||||
//
|
||||
// note: never LoadLibrary d3d11/dxgi -- execexe pre-loads them itself and
|
||||
// fails (error 0xa) if they're already in the loader's module list.
|
||||
//
|
||||
// 64-bit only.
|
||||
|
||||
#include "d3d11_backend.h"
|
||||
|
||||
#ifndef SPICE_D3D11
|
||||
|
||||
void graphics_d3d11_init() {}
|
||||
void graphics_d3d11_shutdown() {}
|
||||
|
||||
#else
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <cwchar>
|
||||
#include <mutex>
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
#include <dxgi1_2.h>
|
||||
|
||||
#include "d3d11_internal.h"
|
||||
#include "util/nt_loader.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using D3D11CreateDeviceAndSwapChain_t = HRESULT(WINAPI *)(
|
||||
IDXGIAdapter *, D3D_DRIVER_TYPE, HMODULE, UINT,
|
||||
const D3D_FEATURE_LEVEL *, UINT, UINT,
|
||||
const DXGI_SWAP_CHAIN_DESC *, IDXGISwapChain **,
|
||||
ID3D11Device **, D3D_FEATURE_LEVEL *, ID3D11DeviceContext **);
|
||||
using CreateDXGIFactory_t = HRESULT(WINAPI *)(REFIID, void **);
|
||||
using CreateDXGIFactory1_t = HRESULT(WINAPI *)(REFIID, void **);
|
||||
using CreateDXGIFactory2_t = HRESULT(WINAPI *)(UINT, REFIID, void **);
|
||||
|
||||
D3D11CreateDeviceAndSwapChain_t D3D11CreateDeviceAndSwapChain_orig = nullptr;
|
||||
CreateDXGIFactory_t CreateDXGIFactory_orig = nullptr;
|
||||
CreateDXGIFactory1_t CreateDXGIFactory1_orig = nullptr;
|
||||
CreateDXGIFactory2_t CreateDXGIFactory2_orig = nullptr;
|
||||
|
||||
std::atomic<bool> g_d3d11_exports_hooked { false };
|
||||
std::atomic<bool> g_dxgi_exports_hooked { false };
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// top-level export hooks
|
||||
|
||||
HRESULT WINAPI D3D11CreateDeviceAndSwapChain_hook(
|
||||
IDXGIAdapter *pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags,
|
||||
const D3D_FEATURE_LEVEL *pFeatureLevels, UINT FeatureLevels, UINT SDKVersion,
|
||||
const DXGI_SWAP_CHAIN_DESC *pSwapChainDesc, IDXGISwapChain **ppSwapChain,
|
||||
ID3D11Device **ppDevice, D3D_FEATURE_LEVEL *pFeatureLevel,
|
||||
ID3D11DeviceContext **ppImmediateContext)
|
||||
{
|
||||
HRESULT res = D3D11CreateDeviceAndSwapChain_orig(
|
||||
pAdapter, DriverType, Software, Flags,
|
||||
pFeatureLevels, FeatureLevels, SDKVersion,
|
||||
pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
|
||||
if (SUCCEEDED(res) && ppSwapChain && *ppSwapChain) {
|
||||
if (pSwapChainDesc) {
|
||||
d3d11_hooks::note_main_hwnd(pSwapChainDesc->OutputWindow);
|
||||
}
|
||||
d3d11_hooks::install_swapchain_hooks(*ppSwapChain);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
#define DEFINE_FACTORY_HOOK(NAME, SIG_PARAMS, ORIG_ARGS) \
|
||||
HRESULT WINAPI NAME##_hook SIG_PARAMS { \
|
||||
HRESULT res = NAME##_orig ORIG_ARGS; \
|
||||
if (SUCCEEDED(res) && ppFactory && *ppFactory) { \
|
||||
d3d11_hooks::install_factory_hooks( \
|
||||
reinterpret_cast<IUnknown *>(*ppFactory)); \
|
||||
} \
|
||||
return res; \
|
||||
}
|
||||
|
||||
DEFINE_FACTORY_HOOK(CreateDXGIFactory,
|
||||
(REFIID riid, void **ppFactory),
|
||||
(riid, ppFactory))
|
||||
DEFINE_FACTORY_HOOK(CreateDXGIFactory1,
|
||||
(REFIID riid, void **ppFactory),
|
||||
(riid, ppFactory))
|
||||
DEFINE_FACTORY_HOOK(CreateDXGIFactory2,
|
||||
(UINT Flags, REFIID riid, void **ppFactory),
|
||||
(Flags, riid, ppFactory))
|
||||
|
||||
#undef DEFINE_FACTORY_HOOK
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// export trampoline plumbing
|
||||
|
||||
// serializes trampoline_export() so the LDR notification callback and the
|
||||
// poll thread don't race each other into MinHook against the same target.
|
||||
std::mutex g_export_mutex;
|
||||
|
||||
bool trampoline_export(const char *dll, const char *name, void *hook, void **orig) {
|
||||
std::lock_guard<std::mutex> lock(g_export_mutex);
|
||||
if (*orig) {
|
||||
return true;
|
||||
}
|
||||
HMODULE mod = GetModuleHandleA(dll);
|
||||
if (!mod) {
|
||||
return false;
|
||||
}
|
||||
void *addr = reinterpret_cast<void *>(GetProcAddress(mod, name));
|
||||
if (!addr) {
|
||||
return false;
|
||||
}
|
||||
*orig = addr; // trampoline_try reads *orig before overwriting it.
|
||||
if (!detour::trampoline_try(addr, hook, orig)) {
|
||||
*orig = nullptr;
|
||||
return false;
|
||||
}
|
||||
log_info("graphics::d3d11", "trampolined {}!{}", dll, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
void try_install_d3d11_exports() {
|
||||
if (g_d3d11_exports_hooked) {
|
||||
return;
|
||||
}
|
||||
if (trampoline_export("d3d11.dll", "D3D11CreateDeviceAndSwapChain",
|
||||
(void *) D3D11CreateDeviceAndSwapChain_hook,
|
||||
(void **) &D3D11CreateDeviceAndSwapChain_orig)) {
|
||||
g_d3d11_exports_hooked = true;
|
||||
}
|
||||
}
|
||||
|
||||
void try_install_dxgi_exports() {
|
||||
if (g_dxgi_exports_hooked) {
|
||||
return;
|
||||
}
|
||||
struct entry { const char *name; void *hook; void **orig; };
|
||||
const entry entries[] = {
|
||||
{ "CreateDXGIFactory", (void *) CreateDXGIFactory_hook,
|
||||
(void **) &CreateDXGIFactory_orig },
|
||||
{ "CreateDXGIFactory1", (void *) CreateDXGIFactory1_hook,
|
||||
(void **) &CreateDXGIFactory1_orig },
|
||||
{ "CreateDXGIFactory2", (void *) CreateDXGIFactory2_hook,
|
||||
(void **) &CreateDXGIFactory2_orig },
|
||||
};
|
||||
bool any = false;
|
||||
for (auto &e : entries) {
|
||||
any |= trampoline_export("dxgi.dll", e.name, e.hook, e.orig);
|
||||
}
|
||||
if (any) {
|
||||
g_dxgi_exports_hooked = true;
|
||||
}
|
||||
}
|
||||
|
||||
void try_capture_if_ready() {
|
||||
if (g_d3d11_exports_hooked && g_dxgi_exports_hooked) {
|
||||
d3d11_hooks::try_capture_vtables();
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// LDR notification + polling fallback
|
||||
|
||||
bool dll_name_ends_with(PCUNICODE_STRING name, const wchar_t *suffix) {
|
||||
if (!name || !name->Buffer) {
|
||||
return false;
|
||||
}
|
||||
const size_t n = name->Length / sizeof(WCHAR);
|
||||
const size_t s = wcslen(suffix);
|
||||
return n >= s && _wcsnicmp(name->Buffer + n - s, suffix, s) == 0;
|
||||
}
|
||||
|
||||
VOID CALLBACK ldr_dll_notification(
|
||||
ULONG reason, PCLDR_DLL_NOTIFICATION_DATA data, PVOID /*context*/)
|
||||
{
|
||||
if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED || !data) {
|
||||
return;
|
||||
}
|
||||
if (dll_name_ends_with(data->Loaded.BaseDllName, L"d3d11.dll")) {
|
||||
try_install_d3d11_exports();
|
||||
} else if (dll_name_ends_with(data->Loaded.BaseDllName, L"dxgi.dll")) {
|
||||
try_install_dxgi_exports();
|
||||
}
|
||||
}
|
||||
|
||||
// execexe maps d3d11/dxgi via a path that bypasses LdrLoadDll, so the
|
||||
// notification above never fires for those DLLs and we have to poll.
|
||||
std::atomic<bool> g_stop { false };
|
||||
std::thread g_poll_thread;
|
||||
std::mutex g_init_mutex;
|
||||
PVOID g_ldr_cookie = nullptr;
|
||||
|
||||
void poll_thread() {
|
||||
using namespace std::chrono_literals;
|
||||
for (int32_t i = 0; i < 120 && !g_stop.load(); ++i) {
|
||||
try_install_d3d11_exports();
|
||||
try_install_dxgi_exports();
|
||||
if (g_d3d11_exports_hooked && g_dxgi_exports_hooked) {
|
||||
d3d11_hooks::try_capture_vtables();
|
||||
return;
|
||||
}
|
||||
// sliced so shutdown doesn't have to wait a full second.
|
||||
for (int32_t s = 0; s < 10 && !g_stop.load(); ++s) {
|
||||
std::this_thread::sleep_for(100ms);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the overlay's imgui dx11 backend needs D3DCompile (d3dcompiler_XX.dll) to
|
||||
// build its shaders. _43 ships with the DX June 2010 redist on stock Win7;
|
||||
// _46/_47 come with newer Windows.
|
||||
bool d3dcompiler_available() {
|
||||
static const wchar_t *names[] = {
|
||||
L"d3dcompiler_47.dll",
|
||||
L"d3dcompiler_46.dll",
|
||||
L"d3dcompiler_43.dll",
|
||||
};
|
||||
for (auto name : names) {
|
||||
HMODULE mod = GetModuleHandleW(name);
|
||||
if (!mod) {
|
||||
mod = LoadLibraryW(name);
|
||||
}
|
||||
if (mod && GetProcAddress(mod, "D3DCompile")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void graphics_d3d11_init() {
|
||||
// dx11 titles always run under execexe. skipping on pure-dx9 games keeps
|
||||
// their startup path completely untouched (no exports patched, no poll
|
||||
// thread, no LDR callback).
|
||||
if (!GetModuleHandleW(L"execexe.dll")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// no d3dcompiler -> overlay can't build shaders; skip dx11 overlay
|
||||
if (!d3dcompiler_available()) {
|
||||
log_warning(
|
||||
"graphics::d3d11",
|
||||
"d3dcompiler not found; dx11 overlay disabled");
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(g_init_mutex);
|
||||
if (g_poll_thread.joinable()) {
|
||||
return; // already initialized
|
||||
}
|
||||
|
||||
log_info("graphics::d3d11", "initializing");
|
||||
|
||||
// trampoline now if either DLL is already in the PEB.
|
||||
try_install_d3d11_exports();
|
||||
try_install_dxgi_exports();
|
||||
try_capture_if_ready();
|
||||
|
||||
// catches standard LdrLoadDll loads.
|
||||
auto reg = reinterpret_cast<decltype(&LdrRegisterDllNotification)>(
|
||||
GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "LdrRegisterDllNotification"));
|
||||
if (reg) {
|
||||
NTSTATUS st = reg(0, ldr_dll_notification, nullptr, &g_ldr_cookie);
|
||||
if (NT_SUCCESS(st)) {
|
||||
log_info("graphics::d3d11", "registered LDR DLL notification");
|
||||
} else {
|
||||
g_ldr_cookie = nullptr;
|
||||
log_warning("graphics::d3d11",
|
||||
"LdrRegisterDllNotification failed: {:#x}", (unsigned long)st);
|
||||
}
|
||||
}
|
||||
|
||||
// catches the execexe loader path that bypasses LdrLoadDll.
|
||||
g_poll_thread = std::thread(poll_thread);
|
||||
}
|
||||
|
||||
void graphics_d3d11_shutdown() {
|
||||
std::lock_guard<std::mutex> lock(g_init_mutex);
|
||||
|
||||
// unregister first so the callback can't fire mid-teardown.
|
||||
if (g_ldr_cookie) {
|
||||
auto unreg = reinterpret_cast<decltype(&LdrUnregisterDllNotification)>(
|
||||
GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "LdrUnregisterDllNotification"));
|
||||
if (unreg) {
|
||||
unreg(g_ldr_cookie);
|
||||
}
|
||||
g_ldr_cookie = nullptr;
|
||||
}
|
||||
|
||||
g_stop.store(true);
|
||||
if (g_poll_thread.joinable()) {
|
||||
g_poll_thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
#endif // SPICE_D3D11
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "overlay/overlay.h"
|
||||
|
||||
void graphics_d3d11_init();
|
||||
void graphics_d3d11_shutdown();
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
struct ID3D11Device;
|
||||
struct ID3D11DeviceContext;
|
||||
struct ID3D11RenderTargetView;
|
||||
struct IDXGISwapChain;
|
||||
|
||||
namespace overlay::d3d11 {
|
||||
|
||||
void render(ID3D11Device *device,
|
||||
ID3D11DeviceContext *context,
|
||||
IDXGISwapChain *swapchain,
|
||||
ID3D11RenderTargetView **rtv);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
#pragma once
|
||||
|
||||
#include "overlay/overlay.h"
|
||||
|
||||
void graphics_d3d11_init();
|
||||
void graphics_d3d11_shutdown();
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
struct ID3D11Device;
|
||||
struct ID3D11DeviceContext;
|
||||
struct ID3D11RenderTargetView;
|
||||
struct IDXGISwapChain;
|
||||
|
||||
namespace overlay::d3d11 {
|
||||
|
||||
void render(ID3D11Device *device,
|
||||
ID3D11DeviceContext *context,
|
||||
IDXGISwapChain *swapchain,
|
||||
ID3D11RenderTargetView **rtv);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,102 +1,102 @@
|
||||
// dx11 factory vtable hooks. patches CreateSwapChain / CreateSwapChainForHwnd
|
||||
// so we can install_swapchain_hooks against every newly-created swapchain.
|
||||
|
||||
#include "d3d11_backend.h"
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
#include <mutex>
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
#include <dxgi1_2.h>
|
||||
|
||||
#include "d3d11_internal.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using CreateSwapChain_t = HRESULT(STDMETHODCALLTYPE *)(
|
||||
IDXGIFactory *, IUnknown *, DXGI_SWAP_CHAIN_DESC *, IDXGISwapChain **);
|
||||
using CreateSwapChainForHwnd_t = HRESULT(STDMETHODCALLTYPE *)(
|
||||
IDXGIFactory2 *, IUnknown *, HWND,
|
||||
const DXGI_SWAP_CHAIN_DESC1 *,
|
||||
const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *,
|
||||
IDXGIOutput *, IDXGISwapChain1 **);
|
||||
|
||||
CreateSwapChain_t CreateSwapChain_orig = nullptr;
|
||||
CreateSwapChainForHwnd_t CreateSwapChainForHwnd_orig = nullptr;
|
||||
|
||||
bool g_factory_hooked = false;
|
||||
bool g_factory2_hooked = false;
|
||||
std::mutex g_hook_mutex;
|
||||
|
||||
HRESULT STDMETHODCALLTYPE CreateSwapChain_hook(
|
||||
IDXGIFactory *factory, IUnknown *pDevice,
|
||||
DXGI_SWAP_CHAIN_DESC *pDesc, IDXGISwapChain **ppSwapChain)
|
||||
{
|
||||
HRESULT res = CreateSwapChain_orig(factory, pDevice, pDesc, ppSwapChain);
|
||||
if (SUCCEEDED(res) && ppSwapChain && *ppSwapChain) {
|
||||
if (pDesc) {
|
||||
d3d11_hooks::note_main_hwnd(pDesc->OutputWindow);
|
||||
}
|
||||
d3d11_hooks::install_swapchain_hooks(*ppSwapChain);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE CreateSwapChainForHwnd_hook(
|
||||
IDXGIFactory2 *factory, IUnknown *pDevice, HWND hWnd,
|
||||
const DXGI_SWAP_CHAIN_DESC1 *pDesc,
|
||||
const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *pFullscreenDesc,
|
||||
IDXGIOutput *pRestrictToOutput, IDXGISwapChain1 **ppSwapChain)
|
||||
{
|
||||
HRESULT res = CreateSwapChainForHwnd_orig(
|
||||
factory, pDevice, hWnd, pDesc, pFullscreenDesc, pRestrictToOutput, ppSwapChain);
|
||||
if (SUCCEEDED(res) && ppSwapChain && *ppSwapChain) {
|
||||
d3d11_hooks::note_main_hwnd(hWnd);
|
||||
d3d11_hooks::install_swapchain_hooks(*ppSwapChain);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// QI-and-hook helper: dedupes the IDXGIFactory / IDXGIFactory2 install paths.
|
||||
template<typename Iface>
|
||||
void install_on(IUnknown *factory, bool &flag,
|
||||
size_t vtbl_index, void *hook, void **orig, const char *name)
|
||||
{
|
||||
if (flag) {
|
||||
return;
|
||||
}
|
||||
Iface *f = nullptr;
|
||||
if (FAILED(factory->QueryInterface(IID_PPV_ARGS(&f))) || !f) {
|
||||
return;
|
||||
}
|
||||
if (d3d11_hooks::hook_vtbl(f, vtbl_index, hook, orig, name)) {
|
||||
flag = true;
|
||||
}
|
||||
f->Release();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace d3d11_hooks {
|
||||
|
||||
void install_factory_hooks(IUnknown *factory) {
|
||||
if (!factory) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(g_hook_mutex);
|
||||
|
||||
install_on<IDXGIFactory>(factory, g_factory_hooked, 10,
|
||||
(void *) CreateSwapChain_hook, (void **) &CreateSwapChain_orig,
|
||||
"IDXGIFactory::CreateSwapChain");
|
||||
|
||||
install_on<IDXGIFactory2>(factory, g_factory2_hooked, 15,
|
||||
(void *) CreateSwapChainForHwnd_hook, (void **) &CreateSwapChainForHwnd_orig,
|
||||
"IDXGIFactory2::CreateSwapChainForHwnd");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // SPICE_D3D11
|
||||
// dx11 factory vtable hooks. patches CreateSwapChain / CreateSwapChainForHwnd
|
||||
// so we can install_swapchain_hooks against every newly-created swapchain.
|
||||
|
||||
#include "d3d11_backend.h"
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
#include <mutex>
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
#include <dxgi1_2.h>
|
||||
|
||||
#include "d3d11_internal.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using CreateSwapChain_t = HRESULT(STDMETHODCALLTYPE *)(
|
||||
IDXGIFactory *, IUnknown *, DXGI_SWAP_CHAIN_DESC *, IDXGISwapChain **);
|
||||
using CreateSwapChainForHwnd_t = HRESULT(STDMETHODCALLTYPE *)(
|
||||
IDXGIFactory2 *, IUnknown *, HWND,
|
||||
const DXGI_SWAP_CHAIN_DESC1 *,
|
||||
const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *,
|
||||
IDXGIOutput *, IDXGISwapChain1 **);
|
||||
|
||||
CreateSwapChain_t CreateSwapChain_orig = nullptr;
|
||||
CreateSwapChainForHwnd_t CreateSwapChainForHwnd_orig = nullptr;
|
||||
|
||||
bool g_factory_hooked = false;
|
||||
bool g_factory2_hooked = false;
|
||||
std::mutex g_hook_mutex;
|
||||
|
||||
HRESULT STDMETHODCALLTYPE CreateSwapChain_hook(
|
||||
IDXGIFactory *factory, IUnknown *pDevice,
|
||||
DXGI_SWAP_CHAIN_DESC *pDesc, IDXGISwapChain **ppSwapChain)
|
||||
{
|
||||
HRESULT res = CreateSwapChain_orig(factory, pDevice, pDesc, ppSwapChain);
|
||||
if (SUCCEEDED(res) && ppSwapChain && *ppSwapChain) {
|
||||
if (pDesc) {
|
||||
d3d11_hooks::note_main_hwnd(pDesc->OutputWindow);
|
||||
}
|
||||
d3d11_hooks::install_swapchain_hooks(*ppSwapChain);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE CreateSwapChainForHwnd_hook(
|
||||
IDXGIFactory2 *factory, IUnknown *pDevice, HWND hWnd,
|
||||
const DXGI_SWAP_CHAIN_DESC1 *pDesc,
|
||||
const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *pFullscreenDesc,
|
||||
IDXGIOutput *pRestrictToOutput, IDXGISwapChain1 **ppSwapChain)
|
||||
{
|
||||
HRESULT res = CreateSwapChainForHwnd_orig(
|
||||
factory, pDevice, hWnd, pDesc, pFullscreenDesc, pRestrictToOutput, ppSwapChain);
|
||||
if (SUCCEEDED(res) && ppSwapChain && *ppSwapChain) {
|
||||
d3d11_hooks::note_main_hwnd(hWnd);
|
||||
d3d11_hooks::install_swapchain_hooks(*ppSwapChain);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// QI-and-hook helper: dedupes the IDXGIFactory / IDXGIFactory2 install paths.
|
||||
template<typename Iface>
|
||||
void install_on(IUnknown *factory, bool &flag,
|
||||
size_t vtbl_index, void *hook, void **orig, const char *name)
|
||||
{
|
||||
if (flag) {
|
||||
return;
|
||||
}
|
||||
Iface *f = nullptr;
|
||||
if (FAILED(factory->QueryInterface(IID_PPV_ARGS(&f))) || !f) {
|
||||
return;
|
||||
}
|
||||
if (d3d11_hooks::hook_vtbl(f, vtbl_index, hook, orig, name)) {
|
||||
flag = true;
|
||||
}
|
||||
f->Release();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace d3d11_hooks {
|
||||
|
||||
void install_factory_hooks(IUnknown *factory) {
|
||||
if (!factory) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(g_hook_mutex);
|
||||
|
||||
install_on<IDXGIFactory>(factory, g_factory_hooked, 10,
|
||||
(void *) CreateSwapChain_hook, (void **) &CreateSwapChain_orig,
|
||||
"IDXGIFactory::CreateSwapChain");
|
||||
|
||||
install_on<IDXGIFactory2>(factory, g_factory2_hooked, 15,
|
||||
(void *) CreateSwapChainForHwnd_hook, (void **) &CreateSwapChainForHwnd_orig,
|
||||
"IDXGIFactory2::CreateSwapChainForHwnd");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // SPICE_D3D11
|
||||
|
||||
@@ -1,59 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
// internal glue for the dx11 backend. all symbols gated on SPICE_D3D11.
|
||||
|
||||
#include "overlay/overlay.h"
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "util/detour.h"
|
||||
#include "util/logging.h"
|
||||
|
||||
struct HWND__; typedef HWND__ *HWND;
|
||||
struct IUnknown;
|
||||
struct IDXGISwapChain;
|
||||
|
||||
namespace d3d11_hooks {
|
||||
|
||||
void install_swapchain_hooks(IDXGISwapChain *swapchain);
|
||||
void install_factory_hooks(IUnknown *factory);
|
||||
void try_capture_vtables();
|
||||
|
||||
// first non-null swapchain HWND wins; later ones (sub-screens, IME
|
||||
// helpers) are ignored. the dummy capture window is exempted via
|
||||
// ignore_hwnd.
|
||||
void note_main_hwnd(HWND hwnd);
|
||||
HWND main_hwnd();
|
||||
void ignore_hwnd(HWND hwnd);
|
||||
|
||||
// capture backbuffer to PNG if a screenshot was requested.
|
||||
void try_screenshot(IDXGISwapChain *swapchain);
|
||||
|
||||
// trampoline a virtual method by vtable index. on failure *orig is null.
|
||||
inline bool hook_vtbl(void *iface, size_t index,
|
||||
void *hook, void **orig, const char *name)
|
||||
{
|
||||
void **vtbl = *reinterpret_cast<void ***>(iface);
|
||||
void *target = vtbl[index];
|
||||
// trampoline_try reads *orig before overwriting it.
|
||||
*orig = target;
|
||||
if (!detour::trampoline_try(target, hook, orig)) {
|
||||
*orig = nullptr;
|
||||
log_warning("graphics::d3d11", "failed to hook {}", name);
|
||||
return false;
|
||||
}
|
||||
log_info("graphics::d3d11", "hooked {}", name);
|
||||
return true;
|
||||
}
|
||||
|
||||
// minimal COM RAII used by capture / screenshot paths.
|
||||
struct com_release {
|
||||
void operator()(IUnknown *p) const { if (p) p->Release(); }
|
||||
};
|
||||
template<typename T> using com_ptr = std::unique_ptr<T, com_release>;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
#pragma once
|
||||
|
||||
// internal glue for the dx11 backend. all symbols gated on SPICE_D3D11.
|
||||
|
||||
#include "overlay/overlay.h"
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "util/detour.h"
|
||||
#include "util/logging.h"
|
||||
|
||||
struct HWND__; typedef HWND__ *HWND;
|
||||
struct IUnknown;
|
||||
struct IDXGISwapChain;
|
||||
|
||||
namespace d3d11_hooks {
|
||||
|
||||
void install_swapchain_hooks(IDXGISwapChain *swapchain);
|
||||
void install_factory_hooks(IUnknown *factory);
|
||||
void try_capture_vtables();
|
||||
|
||||
// first non-null swapchain HWND wins; later ones (sub-screens, IME
|
||||
// helpers) are ignored. the dummy capture window is exempted via
|
||||
// ignore_hwnd.
|
||||
void note_main_hwnd(HWND hwnd);
|
||||
HWND main_hwnd();
|
||||
void ignore_hwnd(HWND hwnd);
|
||||
|
||||
// capture backbuffer to PNG if a screenshot was requested.
|
||||
void try_screenshot(IDXGISwapChain *swapchain);
|
||||
|
||||
// trampoline a virtual method by vtable index. on failure *orig is null.
|
||||
inline bool hook_vtbl(void *iface, size_t index,
|
||||
void *hook, void **orig, const char *name)
|
||||
{
|
||||
void **vtbl = *reinterpret_cast<void ***>(iface);
|
||||
void *target = vtbl[index];
|
||||
// trampoline_try reads *orig before overwriting it.
|
||||
*orig = target;
|
||||
if (!detour::trampoline_try(target, hook, orig)) {
|
||||
*orig = nullptr;
|
||||
log_warning("graphics::d3d11", "failed to hook {}", name);
|
||||
return false;
|
||||
}
|
||||
log_info("graphics::d3d11", "hooked {}", name);
|
||||
return true;
|
||||
}
|
||||
|
||||
// minimal COM RAII used by capture / screenshot paths.
|
||||
struct com_release {
|
||||
void operator()(IUnknown *p) const { if (p) p->Release(); }
|
||||
};
|
||||
template<typename T> using com_ptr = std::unique_ptr<T, com_release>;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,165 +1,165 @@
|
||||
// dx11 screenshot capture. mirrors the d3d9 backend: copy the current
|
||||
// backbuffer into a staging texture, force alpha=255, write PNG via
|
||||
// stb_image_write, push to clipboard and notify.
|
||||
|
||||
#include "d3d11_backend.h"
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
|
||||
#include "d3d11_internal.h"
|
||||
|
||||
#include "external/stb_image_write.h"
|
||||
#include "hooks/graphics/graphics.h"
|
||||
#include "misc/clipboard.h"
|
||||
#include "overlay/notifications.h"
|
||||
#include "util/fileutils.h"
|
||||
|
||||
using d3d11_hooks::com_ptr;
|
||||
|
||||
namespace {
|
||||
|
||||
// copy the swapchain backbuffer into a CPU-readable staging texture and
|
||||
// flatten it into an RGBA8 buffer (BGRA backbuffers are swizzled,
|
||||
// alpha is forced to 255).
|
||||
bool copy_backbuffer_to_rgba(IDXGISwapChain *swapchain,
|
||||
ID3D11Device *device,
|
||||
ID3D11DeviceContext *context,
|
||||
std::vector<uint8_t> &out,
|
||||
uint32_t &out_w, uint32_t &out_h)
|
||||
{
|
||||
ID3D11Texture2D *raw_bb = nullptr;
|
||||
if (FAILED(swapchain->GetBuffer(0, IID_PPV_ARGS(&raw_bb))) || !raw_bb) {
|
||||
return false;
|
||||
}
|
||||
com_ptr<ID3D11Texture2D> backbuffer(raw_bb);
|
||||
|
||||
D3D11_TEXTURE2D_DESC desc {};
|
||||
backbuffer->GetDesc(&desc);
|
||||
|
||||
// MSAA backbuffers can't be CopyResource'd into a non-MS staging target.
|
||||
com_ptr<ID3D11Texture2D> resolved;
|
||||
ID3D11Texture2D *source = backbuffer.get();
|
||||
if (desc.SampleDesc.Count > 1) {
|
||||
D3D11_TEXTURE2D_DESC rd = desc;
|
||||
rd.SampleDesc.Count = 1;
|
||||
rd.SampleDesc.Quality = 0;
|
||||
rd.Usage = D3D11_USAGE_DEFAULT;
|
||||
rd.BindFlags = D3D11_BIND_RENDER_TARGET;
|
||||
rd.CPUAccessFlags = 0;
|
||||
rd.MiscFlags = 0;
|
||||
ID3D11Texture2D *r = nullptr;
|
||||
if (FAILED(device->CreateTexture2D(&rd, nullptr, &r)) || !r) {
|
||||
return false;
|
||||
}
|
||||
resolved.reset(r);
|
||||
context->ResolveSubresource(resolved.get(), 0, backbuffer.get(), 0, desc.Format);
|
||||
source = resolved.get();
|
||||
}
|
||||
|
||||
D3D11_TEXTURE2D_DESC sd {};
|
||||
sd.Width = desc.Width;
|
||||
sd.Height = desc.Height;
|
||||
sd.MipLevels = 1;
|
||||
sd.ArraySize = 1;
|
||||
sd.Format = desc.Format;
|
||||
sd.SampleDesc.Count = 1;
|
||||
sd.Usage = D3D11_USAGE_STAGING;
|
||||
sd.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
|
||||
ID3D11Texture2D *raw_staging = nullptr;
|
||||
if (FAILED(device->CreateTexture2D(&sd, nullptr, &raw_staging)) || !raw_staging) {
|
||||
return false;
|
||||
}
|
||||
com_ptr<ID3D11Texture2D> staging(raw_staging);
|
||||
context->CopyResource(staging.get(), source);
|
||||
|
||||
D3D11_MAPPED_SUBRESOURCE mapped {};
|
||||
if (FAILED(context->Map(staging.get(), 0, D3D11_MAP_READ, 0, &mapped))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// backbuffers from GetDesc are always fully-typed (never _TYPELESS).
|
||||
const bool is_bgra = desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM
|
||||
|| desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
|
||||
|
||||
out.resize(static_cast<size_t>(desc.Width) * desc.Height * 4);
|
||||
const uint8_t *src_base = reinterpret_cast<const uint8_t *>(mapped.pData);
|
||||
for (uint32_t y = 0; y < desc.Height; ++y) {
|
||||
const uint8_t *row = src_base + static_cast<size_t>(y) * mapped.RowPitch;
|
||||
uint8_t *dst = out.data() + static_cast<size_t>(y) * desc.Width * 4;
|
||||
for (uint32_t x = 0; x < desc.Width; ++x) {
|
||||
dst[x * 4 + 0] = row[x * 4 + (is_bgra ? 2 : 0)];
|
||||
dst[x * 4 + 1] = row[x * 4 + 1];
|
||||
dst[x * 4 + 2] = row[x * 4 + (is_bgra ? 0 : 2)];
|
||||
dst[x * 4 + 3] = 255;
|
||||
}
|
||||
}
|
||||
|
||||
context->Unmap(staging.get(), 0);
|
||||
|
||||
out_w = desc.Width;
|
||||
out_h = desc.Height;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace d3d11_hooks {
|
||||
|
||||
void try_screenshot(IDXGISwapChain *swapchain) {
|
||||
if (!swapchain || !graphics_screenshot_consume()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto file_path = graphics_screenshot_genpath();
|
||||
if (file_path.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ID3D11Device *raw_device = nullptr;
|
||||
if (FAILED(swapchain->GetDevice(IID_PPV_ARGS(&raw_device))) || !raw_device) {
|
||||
return;
|
||||
}
|
||||
com_ptr<ID3D11Device> device(raw_device);
|
||||
ID3D11DeviceContext *raw_ctx = nullptr;
|
||||
device->GetImmediateContext(&raw_ctx);
|
||||
if (!raw_ctx) {
|
||||
return;
|
||||
}
|
||||
com_ptr<ID3D11DeviceContext> context(raw_ctx);
|
||||
|
||||
std::vector<uint8_t> pixels;
|
||||
uint32_t w = 0, h = 0;
|
||||
if (!copy_backbuffer_to_rgba(swapchain, device.get(), context.get(), pixels, w, h)) {
|
||||
log_warning("graphics::d3d11", "screenshot: failed to capture backbuffer");
|
||||
overlay::notifications::add(
|
||||
overlay::notifications::Severity::Error,
|
||||
"Screenshot failed to capture");
|
||||
return;
|
||||
}
|
||||
|
||||
log_info("graphics::d3d11", "saving screenshot to {}", file_path);
|
||||
if (stbi_write_png(file_path.c_str(), (int) w, (int) h, 4,
|
||||
pixels.data(), (int) w * 4))
|
||||
{
|
||||
clipboard::copy_image(file_path);
|
||||
overlay::notifications::add(
|
||||
overlay::notifications::Severity::Success,
|
||||
fmt::format("Screenshot saved: {}", fileutils::basename(file_path)));
|
||||
} else {
|
||||
log_warning("graphics::d3d11", "screenshot: stbi_write_png failed");
|
||||
overlay::notifications::add(
|
||||
overlay::notifications::Severity::Error,
|
||||
"Screenshot failed to save");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // SPICE_D3D11
|
||||
// dx11 screenshot capture. mirrors the d3d9 backend: copy the current
|
||||
// backbuffer into a staging texture, force alpha=255, write PNG via
|
||||
// stb_image_write, push to clipboard and notify.
|
||||
|
||||
#include "d3d11_backend.h"
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
|
||||
#include "d3d11_internal.h"
|
||||
|
||||
#include "external/stb_image_write.h"
|
||||
#include "hooks/graphics/graphics.h"
|
||||
#include "misc/clipboard.h"
|
||||
#include "overlay/notifications.h"
|
||||
#include "util/fileutils.h"
|
||||
|
||||
using d3d11_hooks::com_ptr;
|
||||
|
||||
namespace {
|
||||
|
||||
// copy the swapchain backbuffer into a CPU-readable staging texture and
|
||||
// flatten it into an RGBA8 buffer (BGRA backbuffers are swizzled,
|
||||
// alpha is forced to 255).
|
||||
bool copy_backbuffer_to_rgba(IDXGISwapChain *swapchain,
|
||||
ID3D11Device *device,
|
||||
ID3D11DeviceContext *context,
|
||||
std::vector<uint8_t> &out,
|
||||
uint32_t &out_w, uint32_t &out_h)
|
||||
{
|
||||
ID3D11Texture2D *raw_bb = nullptr;
|
||||
if (FAILED(swapchain->GetBuffer(0, IID_PPV_ARGS(&raw_bb))) || !raw_bb) {
|
||||
return false;
|
||||
}
|
||||
com_ptr<ID3D11Texture2D> backbuffer(raw_bb);
|
||||
|
||||
D3D11_TEXTURE2D_DESC desc {};
|
||||
backbuffer->GetDesc(&desc);
|
||||
|
||||
// MSAA backbuffers can't be CopyResource'd into a non-MS staging target.
|
||||
com_ptr<ID3D11Texture2D> resolved;
|
||||
ID3D11Texture2D *source = backbuffer.get();
|
||||
if (desc.SampleDesc.Count > 1) {
|
||||
D3D11_TEXTURE2D_DESC rd = desc;
|
||||
rd.SampleDesc.Count = 1;
|
||||
rd.SampleDesc.Quality = 0;
|
||||
rd.Usage = D3D11_USAGE_DEFAULT;
|
||||
rd.BindFlags = D3D11_BIND_RENDER_TARGET;
|
||||
rd.CPUAccessFlags = 0;
|
||||
rd.MiscFlags = 0;
|
||||
ID3D11Texture2D *r = nullptr;
|
||||
if (FAILED(device->CreateTexture2D(&rd, nullptr, &r)) || !r) {
|
||||
return false;
|
||||
}
|
||||
resolved.reset(r);
|
||||
context->ResolveSubresource(resolved.get(), 0, backbuffer.get(), 0, desc.Format);
|
||||
source = resolved.get();
|
||||
}
|
||||
|
||||
D3D11_TEXTURE2D_DESC sd {};
|
||||
sd.Width = desc.Width;
|
||||
sd.Height = desc.Height;
|
||||
sd.MipLevels = 1;
|
||||
sd.ArraySize = 1;
|
||||
sd.Format = desc.Format;
|
||||
sd.SampleDesc.Count = 1;
|
||||
sd.Usage = D3D11_USAGE_STAGING;
|
||||
sd.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
|
||||
ID3D11Texture2D *raw_staging = nullptr;
|
||||
if (FAILED(device->CreateTexture2D(&sd, nullptr, &raw_staging)) || !raw_staging) {
|
||||
return false;
|
||||
}
|
||||
com_ptr<ID3D11Texture2D> staging(raw_staging);
|
||||
context->CopyResource(staging.get(), source);
|
||||
|
||||
D3D11_MAPPED_SUBRESOURCE mapped {};
|
||||
if (FAILED(context->Map(staging.get(), 0, D3D11_MAP_READ, 0, &mapped))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// backbuffers from GetDesc are always fully-typed (never _TYPELESS).
|
||||
const bool is_bgra = desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM
|
||||
|| desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
|
||||
|
||||
out.resize(static_cast<size_t>(desc.Width) * desc.Height * 4);
|
||||
const uint8_t *src_base = reinterpret_cast<const uint8_t *>(mapped.pData);
|
||||
for (uint32_t y = 0; y < desc.Height; ++y) {
|
||||
const uint8_t *row = src_base + static_cast<size_t>(y) * mapped.RowPitch;
|
||||
uint8_t *dst = out.data() + static_cast<size_t>(y) * desc.Width * 4;
|
||||
for (uint32_t x = 0; x < desc.Width; ++x) {
|
||||
dst[x * 4 + 0] = row[x * 4 + (is_bgra ? 2 : 0)];
|
||||
dst[x * 4 + 1] = row[x * 4 + 1];
|
||||
dst[x * 4 + 2] = row[x * 4 + (is_bgra ? 0 : 2)];
|
||||
dst[x * 4 + 3] = 255;
|
||||
}
|
||||
}
|
||||
|
||||
context->Unmap(staging.get(), 0);
|
||||
|
||||
out_w = desc.Width;
|
||||
out_h = desc.Height;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace d3d11_hooks {
|
||||
|
||||
void try_screenshot(IDXGISwapChain *swapchain) {
|
||||
if (!swapchain || !graphics_screenshot_consume()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto file_path = graphics_screenshot_genpath();
|
||||
if (file_path.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ID3D11Device *raw_device = nullptr;
|
||||
if (FAILED(swapchain->GetDevice(IID_PPV_ARGS(&raw_device))) || !raw_device) {
|
||||
return;
|
||||
}
|
||||
com_ptr<ID3D11Device> device(raw_device);
|
||||
ID3D11DeviceContext *raw_ctx = nullptr;
|
||||
device->GetImmediateContext(&raw_ctx);
|
||||
if (!raw_ctx) {
|
||||
return;
|
||||
}
|
||||
com_ptr<ID3D11DeviceContext> context(raw_ctx);
|
||||
|
||||
std::vector<uint8_t> pixels;
|
||||
uint32_t w = 0, h = 0;
|
||||
if (!copy_backbuffer_to_rgba(swapchain, device.get(), context.get(), pixels, w, h)) {
|
||||
log_warning("graphics::d3d11", "screenshot: failed to capture backbuffer");
|
||||
overlay::notifications::add(
|
||||
overlay::notifications::Severity::Error,
|
||||
"Screenshot failed to capture");
|
||||
return;
|
||||
}
|
||||
|
||||
log_info("graphics::d3d11", "saving screenshot to {}", file_path);
|
||||
if (stbi_write_png(file_path.c_str(), (int) w, (int) h, 4,
|
||||
pixels.data(), (int) w * 4))
|
||||
{
|
||||
clipboard::copy_image(file_path);
|
||||
overlay::notifications::add(
|
||||
overlay::notifications::Severity::Success,
|
||||
fmt::format("Screenshot saved: {}", fileutils::basename(file_path)));
|
||||
} else {
|
||||
log_warning("graphics::d3d11", "screenshot: stbi_write_png failed");
|
||||
overlay::notifications::add(
|
||||
overlay::notifications::Severity::Error,
|
||||
"Screenshot failed to save");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // SPICE_D3D11
|
||||
|
||||
@@ -1,343 +1,343 @@
|
||||
// dx11 swapchain vtable hooks + per-frame overlay pump.
|
||||
//
|
||||
// dxgi shares vtables across swapchain instances, so we only need to patch
|
||||
// Present / Present1 / ResizeBuffers once on the first instance we see.
|
||||
// each frame we lazily attach the overlay to whichever swapchain is
|
||||
// presenting, then drive its imgui update / new_frame / render cycle.
|
||||
|
||||
#include "d3d11_backend.h"
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
#include <dxgi1_2.h>
|
||||
|
||||
#include "d3d11_internal.h"
|
||||
|
||||
#include "external/imgui/imgui.h"
|
||||
#include "external/imgui/backends/imgui_impl_dx11.h"
|
||||
#include "overlay/imgui/impl_spice.h"
|
||||
|
||||
#include "hooks/graphics/graphics.h"
|
||||
#include "util/utils.h"
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// overlay render bridge
|
||||
|
||||
namespace overlay::d3d11 {
|
||||
|
||||
// sRGB backbuffers need a UNORM view: ImGui vertex colors are already
|
||||
// sRGB-encoded, so an extra linear->sRGB conversion would wash the
|
||||
// overlay out white.
|
||||
static DXGI_FORMAT to_unorm_view(DXGI_FORMAT fmt) {
|
||||
switch (fmt) {
|
||||
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: return DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: return DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
default: return fmt;
|
||||
}
|
||||
}
|
||||
|
||||
static void ensure_rtv(ID3D11Device *device,
|
||||
IDXGISwapChain *swapchain,
|
||||
ID3D11RenderTargetView **rtv)
|
||||
{
|
||||
if (*rtv || !device || !swapchain) {
|
||||
return;
|
||||
}
|
||||
ID3D11Texture2D *backbuffer = nullptr;
|
||||
if (FAILED(swapchain->GetBuffer(0, IID_PPV_ARGS(&backbuffer))) || !backbuffer) {
|
||||
return;
|
||||
}
|
||||
D3D11_TEXTURE2D_DESC td {};
|
||||
backbuffer->GetDesc(&td);
|
||||
const DXGI_FORMAT view_fmt = to_unorm_view(td.Format);
|
||||
if (view_fmt != td.Format) {
|
||||
D3D11_RENDER_TARGET_VIEW_DESC rtvd {};
|
||||
rtvd.Format = view_fmt;
|
||||
rtvd.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
|
||||
device->CreateRenderTargetView(backbuffer, &rtvd, rtv);
|
||||
} else {
|
||||
device->CreateRenderTargetView(backbuffer, nullptr, rtv);
|
||||
}
|
||||
backbuffer->Release();
|
||||
}
|
||||
|
||||
// bind the backbuffer (lazily creating the RTV) and draw the imgui
|
||||
// frame on top. reset_invalidate releases *rtv on ResizeBuffers.
|
||||
void render(ID3D11Device *device,
|
||||
ID3D11DeviceContext *context,
|
||||
IDXGISwapChain *swapchain,
|
||||
ID3D11RenderTargetView **rtv)
|
||||
{
|
||||
ensure_rtv(device, swapchain, rtv);
|
||||
if (!*rtv || !context) {
|
||||
return;
|
||||
}
|
||||
// present happens immediately after, so no need to save the previous
|
||||
// RT binding (flip-model resets it anyway).
|
||||
context->OMSetRenderTargets(1, rtv, nullptr);
|
||||
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// file-local state + per-frame helpers
|
||||
|
||||
namespace {
|
||||
|
||||
using Present_t = HRESULT(STDMETHODCALLTYPE *)(
|
||||
IDXGISwapChain *, UINT, UINT);
|
||||
using ResizeBuffers_t = HRESULT(STDMETHODCALLTYPE *)(
|
||||
IDXGISwapChain *, UINT, UINT, UINT, DXGI_FORMAT, UINT);
|
||||
using Present1_t = HRESULT(STDMETHODCALLTYPE *)(
|
||||
IDXGISwapChain1 *, UINT, UINT, const DXGI_PRESENT_PARAMETERS *);
|
||||
|
||||
Present_t Present_orig = nullptr;
|
||||
ResizeBuffers_t ResizeBuffers_orig = nullptr;
|
||||
Present1_t Present1_orig = nullptr;
|
||||
|
||||
bool g_swapchain_hooked = false;
|
||||
bool g_swapchain1_hooked = false;
|
||||
|
||||
// sub-screens / IME helpers are usually child or zero-sized windows.
|
||||
// visibility isn't checked - the game may present before showing the window.
|
||||
bool looks_like_game_window(HWND hwnd) {
|
||||
RECT client {};
|
||||
return GetAncestor(hwnd, GA_ROOT) == hwnd
|
||||
&& GetClientRect(hwnd, &client)
|
||||
&& client.right > client.left
|
||||
&& client.bottom > client.top;
|
||||
}
|
||||
|
||||
// only the main game window; ignore sub-screens / IME helpers.
|
||||
bool is_main_game_swapchain(IDXGISwapChain *swapchain) {
|
||||
DXGI_SWAP_CHAIN_DESC desc {};
|
||||
if (!swapchain || FAILED(swapchain->GetDesc(&desc)) || !desc.OutputWindow) {
|
||||
return false;
|
||||
}
|
||||
|
||||
HWND main = d3d11_hooks::main_hwnd();
|
||||
if (!main) {
|
||||
// no creation hook recorded a window, so fall back to the presenting one;
|
||||
// the choice is permanent, so require a plausible game window
|
||||
if (!looks_like_game_window(desc.OutputWindow)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
log_misc(
|
||||
"graphics::d3d11",
|
||||
"try to notemain hwnd from swapchain present: 0x{:x}",
|
||||
(uintptr_t)desc.OutputWindow);
|
||||
|
||||
d3d11_hooks::note_main_hwnd(desc.OutputWindow);
|
||||
|
||||
// it may have been ignored, or another thread may have won the slot
|
||||
main = d3d11_hooks::main_hwnd();
|
||||
}
|
||||
return desc.OutputWindow == main;
|
||||
}
|
||||
|
||||
// checks are ordered cheapest first, since this runs on every present
|
||||
void try_create_overlay(IDXGISwapChain *swapchain) {
|
||||
if (!swapchain) {
|
||||
return;
|
||||
}
|
||||
|
||||
// overlay is disabled by user
|
||||
if (!overlay::ENABLED) {
|
||||
return;
|
||||
}
|
||||
|
||||
// overlay is already enabled and attached
|
||||
if (overlay::OVERLAY) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ignore sub windows
|
||||
if (!is_main_game_swapchain(swapchain)) {
|
||||
return;
|
||||
}
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC desc {};
|
||||
if (FAILED(swapchain->GetDesc(&desc)) || !desc.OutputWindow) {
|
||||
return;
|
||||
}
|
||||
|
||||
// theme the native title bar; first present is the only reliable point for
|
||||
// windows whose swapchain bypasses our factory hooks (e.g. UnityPlayer.dll)
|
||||
set_window_dark_titlebar(desc.OutputWindow);
|
||||
|
||||
ID3D11Device *device = nullptr;
|
||||
if (FAILED(swapchain->GetDevice(IID_PPV_ARGS(&device))) || !device) {
|
||||
return;
|
||||
}
|
||||
ID3D11DeviceContext *context = nullptr;
|
||||
device->GetImmediateContext(&context);
|
||||
|
||||
if (context) {
|
||||
overlay::create_d3d11(desc.OutputWindow, device, context, swapchain);
|
||||
RECT cr {};
|
||||
::GetClientRect(desc.OutputWindow, &cr);
|
||||
log_info("graphics::d3d11",
|
||||
"attached overlay to swapchain hwnd=0x{:x} backbuffer={}x{} client={}x{}",
|
||||
(uintptr_t) desc.OutputWindow,
|
||||
desc.BufferDesc.Width, desc.BufferDesc.Height,
|
||||
cr.right - cr.left, cr.bottom - cr.top);
|
||||
context->Release();
|
||||
}
|
||||
device->Release();
|
||||
}
|
||||
|
||||
// screenshots have to keep working with the overlay disabled, so they are not gated on it
|
||||
void pump_frame(IDXGISwapChain *swapchain) {
|
||||
const bool has_overlay =
|
||||
overlay::OVERLAY && overlay::OVERLAY->uses_swapchain(swapchain);
|
||||
if (!has_overlay && !is_main_game_swapchain(swapchain)) {
|
||||
return;
|
||||
}
|
||||
|
||||
graphics_poll_screenshot_hotkey();
|
||||
|
||||
// before the overlay render so the screenshot excludes it
|
||||
if (!GRAPHICS_SCREENSHOT_INCLUDE_OVERLAY) {
|
||||
d3d11_hooks::try_screenshot(swapchain);
|
||||
}
|
||||
|
||||
if (has_overlay) {
|
||||
|
||||
// size imgui to the backbuffer (not window client). dxgi may upscale
|
||||
// a small backbuffer into a larger client rect; without this override
|
||||
// imgui would draw past the RTV and the mouse mapping would be off.
|
||||
DXGI_SWAP_CHAIN_DESC desc {};
|
||||
if (SUCCEEDED(swapchain->GetDesc(&desc))) {
|
||||
ImGui_ImplSpice_SetDisplaySizeOverride(
|
||||
(float) desc.BufferDesc.Width,
|
||||
(float) desc.BufferDesc.Height);
|
||||
}
|
||||
|
||||
overlay::OVERLAY->update();
|
||||
overlay::OVERLAY->new_frame();
|
||||
overlay::OVERLAY->render();
|
||||
}
|
||||
|
||||
// after the overlay render so the screenshot includes toasts / menus
|
||||
if (GRAPHICS_SCREENSHOT_INCLUDE_OVERLAY) {
|
||||
d3d11_hooks::try_screenshot(swapchain);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// swapchain method hooks
|
||||
|
||||
HRESULT STDMETHODCALLTYPE Present_hook(
|
||||
IDXGISwapChain *swapchain, UINT SyncInterval, UINT Flags)
|
||||
{
|
||||
// a test present doesn't display anything; don't pick a window or take a screenshot off it
|
||||
if (!(Flags & DXGI_PRESENT_TEST)) {
|
||||
try_create_overlay(swapchain);
|
||||
pump_frame(swapchain);
|
||||
}
|
||||
return Present_orig(swapchain, SyncInterval, Flags);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE Present1_hook(
|
||||
IDXGISwapChain1 *swapchain, UINT SyncInterval, UINT Flags,
|
||||
const DXGI_PRESENT_PARAMETERS *pParams)
|
||||
{
|
||||
if (!(Flags & DXGI_PRESENT_TEST)) {
|
||||
try_create_overlay(swapchain);
|
||||
pump_frame(swapchain);
|
||||
}
|
||||
return Present1_orig(swapchain, SyncInterval, Flags, pParams);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE ResizeBuffers_hook(
|
||||
IDXGISwapChain *swapchain, UINT BufferCount, UINT Width, UINT Height,
|
||||
DXGI_FORMAT NewFormat, UINT SwapChainFlags)
|
||||
{
|
||||
const bool ours = overlay::OVERLAY && overlay::OVERLAY->uses_swapchain(swapchain);
|
||||
if (ours) {
|
||||
log_info("graphics::d3d11", "ResizeBuffers {}x{} fmt={}",
|
||||
Width, Height, (int32_t) NewFormat);
|
||||
overlay::OVERLAY->reset_invalidate();
|
||||
}
|
||||
HRESULT res = ResizeBuffers_orig(
|
||||
swapchain, BufferCount, Width, Height, NewFormat, SwapChainFlags);
|
||||
if (ours && SUCCEEDED(res)) {
|
||||
overlay::OVERLAY->reset_recreate();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// d3d11_hooks public surface: main-window tracking + vtable install.
|
||||
|
||||
namespace d3d11_hooks {
|
||||
|
||||
namespace {
|
||||
std::atomic<HWND> g_main_hwnd { nullptr };
|
||||
std::atomic<HWND> g_ignored_hwnd { nullptr };
|
||||
}
|
||||
|
||||
void note_main_hwnd(HWND hwnd) {
|
||||
if (!hwnd || hwnd == g_ignored_hwnd.load()) {
|
||||
return;
|
||||
}
|
||||
HWND expected = nullptr;
|
||||
if (g_main_hwnd.compare_exchange_strong(expected, hwnd)) {
|
||||
log_info("graphics::d3d11", "main hwnd recorded: 0x{:x}",
|
||||
(uintptr_t) hwnd);
|
||||
}
|
||||
}
|
||||
|
||||
HWND main_hwnd() {
|
||||
return g_main_hwnd.load();
|
||||
}
|
||||
|
||||
void ignore_hwnd(HWND hwnd) {
|
||||
g_ignored_hwnd.store(hwnd);
|
||||
}
|
||||
|
||||
// patch IDXGISwapChain::Present + ResizeBuffers and (if implemented)
|
||||
// IDXGISwapChain1::Present1. idempotent; flag is set only after success
|
||||
// so failed attempts can be retried on the next swapchain.
|
||||
void install_swapchain_hooks(IDXGISwapChain *swapchain) {
|
||||
if (!swapchain) {
|
||||
return;
|
||||
}
|
||||
static std::mutex s_hook_mutex;
|
||||
std::lock_guard<std::mutex> lock(s_hook_mutex);
|
||||
|
||||
if (!g_swapchain_hooked) {
|
||||
const bool a = hook_vtbl(swapchain, 8, (void *) Present_hook,
|
||||
(void **) &Present_orig, "IDXGISwapChain::Present");
|
||||
const bool b = hook_vtbl(swapchain, 13, (void *) ResizeBuffers_hook,
|
||||
(void **) &ResizeBuffers_orig, "IDXGISwapChain::ResizeBuffers");
|
||||
if (a && b) {
|
||||
g_swapchain_hooked = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!g_swapchain1_hooked) {
|
||||
IDXGISwapChain1 *sc1 = nullptr;
|
||||
if (SUCCEEDED(swapchain->QueryInterface(IID_PPV_ARGS(&sc1))) && sc1) {
|
||||
if (hook_vtbl(sc1, 22, (void *) Present1_hook,
|
||||
(void **) &Present1_orig, "IDXGISwapChain1::Present1")) {
|
||||
g_swapchain1_hooked = true;
|
||||
}
|
||||
sc1->Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // SPICE_D3D11
|
||||
// dx11 swapchain vtable hooks + per-frame overlay pump.
|
||||
//
|
||||
// dxgi shares vtables across swapchain instances, so we only need to patch
|
||||
// Present / Present1 / ResizeBuffers once on the first instance we see.
|
||||
// each frame we lazily attach the overlay to whichever swapchain is
|
||||
// presenting, then drive its imgui update / new_frame / render cycle.
|
||||
|
||||
#include "d3d11_backend.h"
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
#include <dxgi1_2.h>
|
||||
|
||||
#include "d3d11_internal.h"
|
||||
|
||||
#include "external/imgui/imgui.h"
|
||||
#include "external/imgui/backends/imgui_impl_dx11.h"
|
||||
#include "overlay/imgui/impl_spice.h"
|
||||
|
||||
#include "hooks/graphics/graphics.h"
|
||||
#include "util/utils.h"
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// overlay render bridge
|
||||
|
||||
namespace overlay::d3d11 {
|
||||
|
||||
// sRGB backbuffers need a UNORM view: ImGui vertex colors are already
|
||||
// sRGB-encoded, so an extra linear->sRGB conversion would wash the
|
||||
// overlay out white.
|
||||
static DXGI_FORMAT to_unorm_view(DXGI_FORMAT fmt) {
|
||||
switch (fmt) {
|
||||
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: return DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: return DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
default: return fmt;
|
||||
}
|
||||
}
|
||||
|
||||
static void ensure_rtv(ID3D11Device *device,
|
||||
IDXGISwapChain *swapchain,
|
||||
ID3D11RenderTargetView **rtv)
|
||||
{
|
||||
if (*rtv || !device || !swapchain) {
|
||||
return;
|
||||
}
|
||||
ID3D11Texture2D *backbuffer = nullptr;
|
||||
if (FAILED(swapchain->GetBuffer(0, IID_PPV_ARGS(&backbuffer))) || !backbuffer) {
|
||||
return;
|
||||
}
|
||||
D3D11_TEXTURE2D_DESC td {};
|
||||
backbuffer->GetDesc(&td);
|
||||
const DXGI_FORMAT view_fmt = to_unorm_view(td.Format);
|
||||
if (view_fmt != td.Format) {
|
||||
D3D11_RENDER_TARGET_VIEW_DESC rtvd {};
|
||||
rtvd.Format = view_fmt;
|
||||
rtvd.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
|
||||
device->CreateRenderTargetView(backbuffer, &rtvd, rtv);
|
||||
} else {
|
||||
device->CreateRenderTargetView(backbuffer, nullptr, rtv);
|
||||
}
|
||||
backbuffer->Release();
|
||||
}
|
||||
|
||||
// bind the backbuffer (lazily creating the RTV) and draw the imgui
|
||||
// frame on top. reset_invalidate releases *rtv on ResizeBuffers.
|
||||
void render(ID3D11Device *device,
|
||||
ID3D11DeviceContext *context,
|
||||
IDXGISwapChain *swapchain,
|
||||
ID3D11RenderTargetView **rtv)
|
||||
{
|
||||
ensure_rtv(device, swapchain, rtv);
|
||||
if (!*rtv || !context) {
|
||||
return;
|
||||
}
|
||||
// present happens immediately after, so no need to save the previous
|
||||
// RT binding (flip-model resets it anyway).
|
||||
context->OMSetRenderTargets(1, rtv, nullptr);
|
||||
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// file-local state + per-frame helpers
|
||||
|
||||
namespace {
|
||||
|
||||
using Present_t = HRESULT(STDMETHODCALLTYPE *)(
|
||||
IDXGISwapChain *, UINT, UINT);
|
||||
using ResizeBuffers_t = HRESULT(STDMETHODCALLTYPE *)(
|
||||
IDXGISwapChain *, UINT, UINT, UINT, DXGI_FORMAT, UINT);
|
||||
using Present1_t = HRESULT(STDMETHODCALLTYPE *)(
|
||||
IDXGISwapChain1 *, UINT, UINT, const DXGI_PRESENT_PARAMETERS *);
|
||||
|
||||
Present_t Present_orig = nullptr;
|
||||
ResizeBuffers_t ResizeBuffers_orig = nullptr;
|
||||
Present1_t Present1_orig = nullptr;
|
||||
|
||||
bool g_swapchain_hooked = false;
|
||||
bool g_swapchain1_hooked = false;
|
||||
|
||||
// sub-screens / IME helpers are usually child or zero-sized windows.
|
||||
// visibility isn't checked - the game may present before showing the window.
|
||||
bool looks_like_game_window(HWND hwnd) {
|
||||
RECT client {};
|
||||
return GetAncestor(hwnd, GA_ROOT) == hwnd
|
||||
&& GetClientRect(hwnd, &client)
|
||||
&& client.right > client.left
|
||||
&& client.bottom > client.top;
|
||||
}
|
||||
|
||||
// only the main game window; ignore sub-screens / IME helpers.
|
||||
bool is_main_game_swapchain(IDXGISwapChain *swapchain) {
|
||||
DXGI_SWAP_CHAIN_DESC desc {};
|
||||
if (!swapchain || FAILED(swapchain->GetDesc(&desc)) || !desc.OutputWindow) {
|
||||
return false;
|
||||
}
|
||||
|
||||
HWND main = d3d11_hooks::main_hwnd();
|
||||
if (!main) {
|
||||
// no creation hook recorded a window, so fall back to the presenting one;
|
||||
// the choice is permanent, so require a plausible game window
|
||||
if (!looks_like_game_window(desc.OutputWindow)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
log_misc(
|
||||
"graphics::d3d11",
|
||||
"try to notemain hwnd from swapchain present: 0x{:x}",
|
||||
(uintptr_t)desc.OutputWindow);
|
||||
|
||||
d3d11_hooks::note_main_hwnd(desc.OutputWindow);
|
||||
|
||||
// it may have been ignored, or another thread may have won the slot
|
||||
main = d3d11_hooks::main_hwnd();
|
||||
}
|
||||
return desc.OutputWindow == main;
|
||||
}
|
||||
|
||||
// checks are ordered cheapest first, since this runs on every present
|
||||
void try_create_overlay(IDXGISwapChain *swapchain) {
|
||||
if (!swapchain) {
|
||||
return;
|
||||
}
|
||||
|
||||
// overlay is disabled by user
|
||||
if (!overlay::ENABLED) {
|
||||
return;
|
||||
}
|
||||
|
||||
// overlay is already enabled and attached
|
||||
if (overlay::OVERLAY) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ignore sub windows
|
||||
if (!is_main_game_swapchain(swapchain)) {
|
||||
return;
|
||||
}
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC desc {};
|
||||
if (FAILED(swapchain->GetDesc(&desc)) || !desc.OutputWindow) {
|
||||
return;
|
||||
}
|
||||
|
||||
// theme the native title bar; first present is the only reliable point for
|
||||
// windows whose swapchain bypasses our factory hooks (e.g. UnityPlayer.dll)
|
||||
set_window_dark_titlebar(desc.OutputWindow);
|
||||
|
||||
ID3D11Device *device = nullptr;
|
||||
if (FAILED(swapchain->GetDevice(IID_PPV_ARGS(&device))) || !device) {
|
||||
return;
|
||||
}
|
||||
ID3D11DeviceContext *context = nullptr;
|
||||
device->GetImmediateContext(&context);
|
||||
|
||||
if (context) {
|
||||
overlay::create_d3d11(desc.OutputWindow, device, context, swapchain);
|
||||
RECT cr {};
|
||||
::GetClientRect(desc.OutputWindow, &cr);
|
||||
log_info("graphics::d3d11",
|
||||
"attached overlay to swapchain hwnd=0x{:x} backbuffer={}x{} client={}x{}",
|
||||
(uintptr_t) desc.OutputWindow,
|
||||
desc.BufferDesc.Width, desc.BufferDesc.Height,
|
||||
cr.right - cr.left, cr.bottom - cr.top);
|
||||
context->Release();
|
||||
}
|
||||
device->Release();
|
||||
}
|
||||
|
||||
// screenshots have to keep working with the overlay disabled, so they are not gated on it
|
||||
void pump_frame(IDXGISwapChain *swapchain) {
|
||||
const bool has_overlay =
|
||||
overlay::OVERLAY && overlay::OVERLAY->uses_swapchain(swapchain);
|
||||
if (!has_overlay && !is_main_game_swapchain(swapchain)) {
|
||||
return;
|
||||
}
|
||||
|
||||
graphics_poll_screenshot_hotkey();
|
||||
|
||||
// before the overlay render so the screenshot excludes it
|
||||
if (!GRAPHICS_SCREENSHOT_INCLUDE_OVERLAY) {
|
||||
d3d11_hooks::try_screenshot(swapchain);
|
||||
}
|
||||
|
||||
if (has_overlay) {
|
||||
|
||||
// size imgui to the backbuffer (not window client). dxgi may upscale
|
||||
// a small backbuffer into a larger client rect; without this override
|
||||
// imgui would draw past the RTV and the mouse mapping would be off.
|
||||
DXGI_SWAP_CHAIN_DESC desc {};
|
||||
if (SUCCEEDED(swapchain->GetDesc(&desc))) {
|
||||
ImGui_ImplSpice_SetDisplaySizeOverride(
|
||||
(float) desc.BufferDesc.Width,
|
||||
(float) desc.BufferDesc.Height);
|
||||
}
|
||||
|
||||
overlay::OVERLAY->update();
|
||||
overlay::OVERLAY->new_frame();
|
||||
overlay::OVERLAY->render();
|
||||
}
|
||||
|
||||
// after the overlay render so the screenshot includes toasts / menus
|
||||
if (GRAPHICS_SCREENSHOT_INCLUDE_OVERLAY) {
|
||||
d3d11_hooks::try_screenshot(swapchain);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// swapchain method hooks
|
||||
|
||||
HRESULT STDMETHODCALLTYPE Present_hook(
|
||||
IDXGISwapChain *swapchain, UINT SyncInterval, UINT Flags)
|
||||
{
|
||||
// a test present doesn't display anything; don't pick a window or take a screenshot off it
|
||||
if (!(Flags & DXGI_PRESENT_TEST)) {
|
||||
try_create_overlay(swapchain);
|
||||
pump_frame(swapchain);
|
||||
}
|
||||
return Present_orig(swapchain, SyncInterval, Flags);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE Present1_hook(
|
||||
IDXGISwapChain1 *swapchain, UINT SyncInterval, UINT Flags,
|
||||
const DXGI_PRESENT_PARAMETERS *pParams)
|
||||
{
|
||||
if (!(Flags & DXGI_PRESENT_TEST)) {
|
||||
try_create_overlay(swapchain);
|
||||
pump_frame(swapchain);
|
||||
}
|
||||
return Present1_orig(swapchain, SyncInterval, Flags, pParams);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE ResizeBuffers_hook(
|
||||
IDXGISwapChain *swapchain, UINT BufferCount, UINT Width, UINT Height,
|
||||
DXGI_FORMAT NewFormat, UINT SwapChainFlags)
|
||||
{
|
||||
const bool ours = overlay::OVERLAY && overlay::OVERLAY->uses_swapchain(swapchain);
|
||||
if (ours) {
|
||||
log_info("graphics::d3d11", "ResizeBuffers {}x{} fmt={}",
|
||||
Width, Height, (int32_t) NewFormat);
|
||||
overlay::OVERLAY->reset_invalidate();
|
||||
}
|
||||
HRESULT res = ResizeBuffers_orig(
|
||||
swapchain, BufferCount, Width, Height, NewFormat, SwapChainFlags);
|
||||
if (ours && SUCCEEDED(res)) {
|
||||
overlay::OVERLAY->reset_recreate();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// d3d11_hooks public surface: main-window tracking + vtable install.
|
||||
|
||||
namespace d3d11_hooks {
|
||||
|
||||
namespace {
|
||||
std::atomic<HWND> g_main_hwnd { nullptr };
|
||||
std::atomic<HWND> g_ignored_hwnd { nullptr };
|
||||
}
|
||||
|
||||
void note_main_hwnd(HWND hwnd) {
|
||||
if (!hwnd || hwnd == g_ignored_hwnd.load()) {
|
||||
return;
|
||||
}
|
||||
HWND expected = nullptr;
|
||||
if (g_main_hwnd.compare_exchange_strong(expected, hwnd)) {
|
||||
log_info("graphics::d3d11", "main hwnd recorded: 0x{:x}",
|
||||
(uintptr_t) hwnd);
|
||||
}
|
||||
}
|
||||
|
||||
HWND main_hwnd() {
|
||||
return g_main_hwnd.load();
|
||||
}
|
||||
|
||||
void ignore_hwnd(HWND hwnd) {
|
||||
g_ignored_hwnd.store(hwnd);
|
||||
}
|
||||
|
||||
// patch IDXGISwapChain::Present + ResizeBuffers and (if implemented)
|
||||
// IDXGISwapChain1::Present1. idempotent; flag is set only after success
|
||||
// so failed attempts can be retried on the next swapchain.
|
||||
void install_swapchain_hooks(IDXGISwapChain *swapchain) {
|
||||
if (!swapchain) {
|
||||
return;
|
||||
}
|
||||
static std::mutex s_hook_mutex;
|
||||
std::lock_guard<std::mutex> lock(s_hook_mutex);
|
||||
|
||||
if (!g_swapchain_hooked) {
|
||||
const bool a = hook_vtbl(swapchain, 8, (void *) Present_hook,
|
||||
(void **) &Present_orig, "IDXGISwapChain::Present");
|
||||
const bool b = hook_vtbl(swapchain, 13, (void *) ResizeBuffers_hook,
|
||||
(void **) &ResizeBuffers_orig, "IDXGISwapChain::ResizeBuffers");
|
||||
if (a && b) {
|
||||
g_swapchain_hooked = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!g_swapchain1_hooked) {
|
||||
IDXGISwapChain1 *sc1 = nullptr;
|
||||
if (SUCCEEDED(swapchain->QueryInterface(IID_PPV_ARGS(&sc1))) && sc1) {
|
||||
if (hook_vtbl(sc1, 22, (void *) Present1_hook,
|
||||
(void **) &Present1_orig, "IDXGISwapChain1::Present1")) {
|
||||
g_swapchain1_hooked = true;
|
||||
}
|
||||
sc1->Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // SPICE_D3D11
|
||||
|
||||
@@ -1,175 +1,175 @@
|
||||
// proactive vtable capture for the dx11 backend.
|
||||
//
|
||||
// titles under the execexe loader routinely race past our export-level
|
||||
// trampolines, so the game's first real swapchain never goes through us.
|
||||
// we sidestep that by creating a throwaway device + swapchain ourselves
|
||||
// the moment d3d11.dll + dxgi.dll appear, which patches the shared
|
||||
// IDXGISwapChain[1] / IDXGIFactory[2] vtables ahead of the game.
|
||||
|
||||
#include "d3d11_backend.h"
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
#include <dxgi1_2.h>
|
||||
|
||||
#include "d3d11_internal.h"
|
||||
|
||||
using d3d11_hooks::com_ptr;
|
||||
|
||||
namespace {
|
||||
|
||||
using D3D11CreateDevice_t = HRESULT(WINAPI *)(
|
||||
IDXGIAdapter *, D3D_DRIVER_TYPE, HMODULE, UINT,
|
||||
const D3D_FEATURE_LEVEL *, UINT, UINT,
|
||||
ID3D11Device **, D3D_FEATURE_LEVEL *, ID3D11DeviceContext **);
|
||||
using CreateDXGIFactory1_t = HRESULT(WINAPI *)(REFIID, void **);
|
||||
using CreateDXGIFactory2_t = HRESULT(WINAPI *)(UINT, REFIID, void **);
|
||||
|
||||
std::atomic<bool> g_vtables_captured { false };
|
||||
|
||||
template<typename Fn>
|
||||
Fn resolve(HMODULE mod, const char *name) {
|
||||
return reinterpret_cast<Fn>(GetProcAddress(mod, name));
|
||||
}
|
||||
|
||||
com_ptr<IDXGIFactory2> create_factory2(CreateDXGIFactory2_t f2,
|
||||
CreateDXGIFactory1_t f1)
|
||||
{
|
||||
IDXGIFactory2 *raw = nullptr;
|
||||
if (f2 && SUCCEEDED(f2(0, IID_PPV_ARGS(&raw))) && raw) {
|
||||
return com_ptr<IDXGIFactory2>(raw);
|
||||
}
|
||||
IDXGIFactory1 *factory1 = nullptr;
|
||||
if (f1 && SUCCEEDED(f1(IID_PPV_ARGS(&factory1))) && factory1) {
|
||||
factory1->QueryInterface(IID_PPV_ARGS(&raw));
|
||||
factory1->Release();
|
||||
}
|
||||
return com_ptr<IDXGIFactory2>(raw);
|
||||
}
|
||||
|
||||
bool create_dummy_device(D3D11CreateDevice_t create,
|
||||
com_ptr<ID3D11Device> &device,
|
||||
com_ptr<ID3D11DeviceContext> &context)
|
||||
{
|
||||
static constexpr D3D_FEATURE_LEVEL levels[] = {
|
||||
D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0,
|
||||
D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0,
|
||||
};
|
||||
// hardware first, then WARP so headless / unusual configs still work.
|
||||
for (auto type : { D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP }) {
|
||||
ID3D11Device *d = nullptr;
|
||||
ID3D11DeviceContext *c = nullptr;
|
||||
D3D_FEATURE_LEVEL got;
|
||||
if (SUCCEEDED(create(nullptr, type, nullptr, 0,
|
||||
levels, ARRAYSIZE(levels), D3D11_SDK_VERSION,
|
||||
&d, &got, &c)) && d) {
|
||||
device.reset(d);
|
||||
context.reset(c);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace d3d11_hooks {
|
||||
|
||||
// create a throwaway device + swapchain to patch the shared vtables before
|
||||
// the game's loader races past our export trampolines. safe to call
|
||||
// repeatedly; runs at most once.
|
||||
void try_capture_vtables() {
|
||||
if (g_vtables_captured.load()) {
|
||||
return;
|
||||
}
|
||||
|
||||
HMODULE d3d11 = GetModuleHandleW(L"d3d11.dll");
|
||||
HMODULE dxgi = GetModuleHandleW(L"dxgi.dll");
|
||||
if (!d3d11 || !dxgi) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto create_device = resolve<D3D11CreateDevice_t>(d3d11, "D3D11CreateDevice");
|
||||
auto f2 = resolve<CreateDXGIFactory2_t>(dxgi, "CreateDXGIFactory2");
|
||||
auto f1 = resolve<CreateDXGIFactory1_t>(dxgi, "CreateDXGIFactory1");
|
||||
if (!create_device || (!f1 && !f2)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// serialize concurrent calls (poll thread + LDR notification). only
|
||||
// flip g_vtables_captured after success so failed attempts remain
|
||||
// retriable on the next tick.
|
||||
static std::atomic<bool> in_progress { false };
|
||||
if (in_progress.exchange(true)) {
|
||||
return;
|
||||
}
|
||||
struct scope_clear {
|
||||
std::atomic<bool> &flag;
|
||||
~scope_clear() { flag.store(false); }
|
||||
} clear { in_progress };
|
||||
|
||||
// hidden message-only window; STATIC is always registered by user32.
|
||||
HWND dummy_hwnd = CreateWindowExW(
|
||||
0, L"STATIC", L"", 0, 0, 0, 1, 1,
|
||||
HWND_MESSAGE, nullptr, GetModuleHandleW(nullptr), nullptr);
|
||||
if (!dummy_hwnd) {
|
||||
log_warning("graphics::d3d11",
|
||||
"vtable capture: CreateWindowExW failed (gle={})", (unsigned long)GetLastError());
|
||||
return;
|
||||
}
|
||||
auto destroy_hwnd = std::unique_ptr<HWND__, decltype(&DestroyWindow)>(
|
||||
dummy_hwnd, &DestroyWindow);
|
||||
|
||||
// if the game's CreateDXGIFactory_hook already raced us, our
|
||||
// CreateSwapChainForHwnd call below would trip the hook and try to
|
||||
// record dummy_hwnd as the main window. block that.
|
||||
ignore_hwnd(dummy_hwnd);
|
||||
|
||||
auto factory2 = create_factory2(f2, f1);
|
||||
if (!factory2) {
|
||||
log_warning("graphics::d3d11", "vtable capture: CreateDXGIFactory* failed");
|
||||
return;
|
||||
}
|
||||
|
||||
com_ptr<ID3D11Device> device;
|
||||
com_ptr<ID3D11DeviceContext> context;
|
||||
if (!create_dummy_device(create_device, device, context)) {
|
||||
log_warning("graphics::d3d11", "vtable capture: D3D11CreateDevice failed");
|
||||
return;
|
||||
}
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC1 desc {};
|
||||
desc.Width = 1;
|
||||
desc.Height = 1;
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
desc.BufferCount = 2;
|
||||
desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
||||
|
||||
IDXGISwapChain1 *raw_sc = nullptr;
|
||||
HRESULT hr = factory2->CreateSwapChainForHwnd(
|
||||
device.get(), dummy_hwnd, &desc, nullptr, nullptr, &raw_sc);
|
||||
if (FAILED(hr) || !raw_sc) {
|
||||
log_warning("graphics::d3d11",
|
||||
"vtable capture: CreateSwapChainForHwnd failed (hr={:#x})", (unsigned long)hr);
|
||||
return;
|
||||
}
|
||||
com_ptr<IDXGISwapChain1> swapchain(raw_sc);
|
||||
|
||||
install_swapchain_hooks(swapchain.get());
|
||||
install_factory_hooks(factory2.get());
|
||||
|
||||
g_vtables_captured.store(true);
|
||||
log_info("graphics::d3d11", "vtable capture complete (via dummy swapchain)");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // SPICE_D3D11
|
||||
// proactive vtable capture for the dx11 backend.
|
||||
//
|
||||
// titles under the execexe loader routinely race past our export-level
|
||||
// trampolines, so the game's first real swapchain never goes through us.
|
||||
// we sidestep that by creating a throwaway device + swapchain ourselves
|
||||
// the moment d3d11.dll + dxgi.dll appear, which patches the shared
|
||||
// IDXGISwapChain[1] / IDXGIFactory[2] vtables ahead of the game.
|
||||
|
||||
#include "d3d11_backend.h"
|
||||
|
||||
#ifdef SPICE_D3D11
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
#include <dxgi1_2.h>
|
||||
|
||||
#include "d3d11_internal.h"
|
||||
|
||||
using d3d11_hooks::com_ptr;
|
||||
|
||||
namespace {
|
||||
|
||||
using D3D11CreateDevice_t = HRESULT(WINAPI *)(
|
||||
IDXGIAdapter *, D3D_DRIVER_TYPE, HMODULE, UINT,
|
||||
const D3D_FEATURE_LEVEL *, UINT, UINT,
|
||||
ID3D11Device **, D3D_FEATURE_LEVEL *, ID3D11DeviceContext **);
|
||||
using CreateDXGIFactory1_t = HRESULT(WINAPI *)(REFIID, void **);
|
||||
using CreateDXGIFactory2_t = HRESULT(WINAPI *)(UINT, REFIID, void **);
|
||||
|
||||
std::atomic<bool> g_vtables_captured { false };
|
||||
|
||||
template<typename Fn>
|
||||
Fn resolve(HMODULE mod, const char *name) {
|
||||
return reinterpret_cast<Fn>(GetProcAddress(mod, name));
|
||||
}
|
||||
|
||||
com_ptr<IDXGIFactory2> create_factory2(CreateDXGIFactory2_t f2,
|
||||
CreateDXGIFactory1_t f1)
|
||||
{
|
||||
IDXGIFactory2 *raw = nullptr;
|
||||
if (f2 && SUCCEEDED(f2(0, IID_PPV_ARGS(&raw))) && raw) {
|
||||
return com_ptr<IDXGIFactory2>(raw);
|
||||
}
|
||||
IDXGIFactory1 *factory1 = nullptr;
|
||||
if (f1 && SUCCEEDED(f1(IID_PPV_ARGS(&factory1))) && factory1) {
|
||||
factory1->QueryInterface(IID_PPV_ARGS(&raw));
|
||||
factory1->Release();
|
||||
}
|
||||
return com_ptr<IDXGIFactory2>(raw);
|
||||
}
|
||||
|
||||
bool create_dummy_device(D3D11CreateDevice_t create,
|
||||
com_ptr<ID3D11Device> &device,
|
||||
com_ptr<ID3D11DeviceContext> &context)
|
||||
{
|
||||
static constexpr D3D_FEATURE_LEVEL levels[] = {
|
||||
D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0,
|
||||
D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0,
|
||||
};
|
||||
// hardware first, then WARP so headless / unusual configs still work.
|
||||
for (auto type : { D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP }) {
|
||||
ID3D11Device *d = nullptr;
|
||||
ID3D11DeviceContext *c = nullptr;
|
||||
D3D_FEATURE_LEVEL got;
|
||||
if (SUCCEEDED(create(nullptr, type, nullptr, 0,
|
||||
levels, ARRAYSIZE(levels), D3D11_SDK_VERSION,
|
||||
&d, &got, &c)) && d) {
|
||||
device.reset(d);
|
||||
context.reset(c);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace d3d11_hooks {
|
||||
|
||||
// create a throwaway device + swapchain to patch the shared vtables before
|
||||
// the game's loader races past our export trampolines. safe to call
|
||||
// repeatedly; runs at most once.
|
||||
void try_capture_vtables() {
|
||||
if (g_vtables_captured.load()) {
|
||||
return;
|
||||
}
|
||||
|
||||
HMODULE d3d11 = GetModuleHandleW(L"d3d11.dll");
|
||||
HMODULE dxgi = GetModuleHandleW(L"dxgi.dll");
|
||||
if (!d3d11 || !dxgi) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto create_device = resolve<D3D11CreateDevice_t>(d3d11, "D3D11CreateDevice");
|
||||
auto f2 = resolve<CreateDXGIFactory2_t>(dxgi, "CreateDXGIFactory2");
|
||||
auto f1 = resolve<CreateDXGIFactory1_t>(dxgi, "CreateDXGIFactory1");
|
||||
if (!create_device || (!f1 && !f2)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// serialize concurrent calls (poll thread + LDR notification). only
|
||||
// flip g_vtables_captured after success so failed attempts remain
|
||||
// retriable on the next tick.
|
||||
static std::atomic<bool> in_progress { false };
|
||||
if (in_progress.exchange(true)) {
|
||||
return;
|
||||
}
|
||||
struct scope_clear {
|
||||
std::atomic<bool> &flag;
|
||||
~scope_clear() { flag.store(false); }
|
||||
} clear { in_progress };
|
||||
|
||||
// hidden message-only window; STATIC is always registered by user32.
|
||||
HWND dummy_hwnd = CreateWindowExW(
|
||||
0, L"STATIC", L"", 0, 0, 0, 1, 1,
|
||||
HWND_MESSAGE, nullptr, GetModuleHandleW(nullptr), nullptr);
|
||||
if (!dummy_hwnd) {
|
||||
log_warning("graphics::d3d11",
|
||||
"vtable capture: CreateWindowExW failed (gle={})", (unsigned long)GetLastError());
|
||||
return;
|
||||
}
|
||||
auto destroy_hwnd = std::unique_ptr<HWND__, decltype(&DestroyWindow)>(
|
||||
dummy_hwnd, &DestroyWindow);
|
||||
|
||||
// if the game's CreateDXGIFactory_hook already raced us, our
|
||||
// CreateSwapChainForHwnd call below would trip the hook and try to
|
||||
// record dummy_hwnd as the main window. block that.
|
||||
ignore_hwnd(dummy_hwnd);
|
||||
|
||||
auto factory2 = create_factory2(f2, f1);
|
||||
if (!factory2) {
|
||||
log_warning("graphics::d3d11", "vtable capture: CreateDXGIFactory* failed");
|
||||
return;
|
||||
}
|
||||
|
||||
com_ptr<ID3D11Device> device;
|
||||
com_ptr<ID3D11DeviceContext> context;
|
||||
if (!create_dummy_device(create_device, device, context)) {
|
||||
log_warning("graphics::d3d11", "vtable capture: D3D11CreateDevice failed");
|
||||
return;
|
||||
}
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC1 desc {};
|
||||
desc.Width = 1;
|
||||
desc.Height = 1;
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
desc.BufferCount = 2;
|
||||
desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
||||
|
||||
IDXGISwapChain1 *raw_sc = nullptr;
|
||||
HRESULT hr = factory2->CreateSwapChainForHwnd(
|
||||
device.get(), dummy_hwnd, &desc, nullptr, nullptr, &raw_sc);
|
||||
if (FAILED(hr) || !raw_sc) {
|
||||
log_warning("graphics::d3d11",
|
||||
"vtable capture: CreateSwapChainForHwnd failed (hr={:#x})", (unsigned long)hr);
|
||||
return;
|
||||
}
|
||||
com_ptr<IDXGISwapChain1> swapchain(raw_sc);
|
||||
|
||||
install_swapchain_hooks(swapchain.get());
|
||||
install_factory_hooks(factory2.get());
|
||||
|
||||
g_vtables_captured.store(true);
|
||||
log_info("graphics::d3d11", "vtable capture complete (via dummy swapchain)");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // SPICE_D3D11
|
||||
|
||||
@@ -1,144 +1,144 @@
|
||||
#include "d3d9_live2d.h"
|
||||
|
||||
// only the Live2D-capable SDVX versions are 64-bit, so the entire implementation
|
||||
// is compiled out of 32-bit builds (the header supplies inline no-op stubs there).
|
||||
#ifdef SPICE64
|
||||
|
||||
#include <cstdint>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "hooks/graphics/graphics.h"
|
||||
|
||||
// how the Live2D draw filtering works
|
||||
// ------------------------------------
|
||||
// SDVX draws its Live2D characters with a small, fixed set of pixel and
|
||||
// vertex shaders. to skip those draws (and save GPU) we have to recognise them at
|
||||
// the exact moment the game issues a draw call. the d3d9 device hooks feed three
|
||||
// kinds of events into this module:
|
||||
//
|
||||
// 1. shader creation (on_create_pixel_shader / on_create_vertex_shader)
|
||||
// the game compiles its shaders once at load. we can't trust the shader
|
||||
// *object pointer* to identify a shader (it's just a heap address that
|
||||
// varies per run and can be recycled), so instead we hash the shader's
|
||||
// D3D9 *bytecode* - that fingerprint is stable across runs because the
|
||||
// game ships the same shaders. if the hash matches a known Live2D shader
|
||||
// we remember that object pointer in g_live2d_shaders.
|
||||
//
|
||||
// 2. shader binding (on_set_pixel_shader / on_set_vertex_shader)
|
||||
// whenever the game binds a shader we look it up in that set once and cache
|
||||
// the yes/no answer in g_cur_ps_is_live2d / g_cur_vs_is_live2d. binds happen
|
||||
// far less often than draws, so this is where the lookup cost lives.
|
||||
//
|
||||
// 3. draw call (should_skip_draw, called from every Draw* hook)
|
||||
// the per-draw question "is this a Live2D draw?" is then just reading those
|
||||
// two cached bools - no hashing, no map lookups. if the skip is currently
|
||||
// active (see graphics_sdvx_live2d_should_skip) and either bound shader is
|
||||
// Live2D, the Draw* hook drops the call instead of forwarding it.
|
||||
//
|
||||
// everything is gated on the feature being enabled (mode != Off); when it's Off
|
||||
// every entry point is a single predicted-not-taken branch. d3d9 rendering for a
|
||||
// device is single-threaded, so none of this state needs locking.
|
||||
|
||||
namespace {
|
||||
|
||||
// shader state is tracked whenever the feature might act (mode != Off) so the
|
||||
// known-shader set is populated before a song starts. when Off, every entry
|
||||
// point is a single cheap branch.
|
||||
bool tracking_enabled() {
|
||||
return GRAPHICS_SDVX_LIVE2D_MODE != SdvxLive2dMode::Off;
|
||||
}
|
||||
|
||||
// the set of shader objects (pixel or vertex) whose bytecode matched a known
|
||||
// Live2D fingerprint. only matching shaders are stored, so this stays tiny.
|
||||
std::unordered_set<void *> g_live2d_shaders;
|
||||
|
||||
// whether the currently-bound shaders are known Live2D shaders. cached at set
|
||||
// time so the per-draw check is just two bool reads.
|
||||
bool g_cur_ps_is_live2d = false;
|
||||
bool g_cur_vs_is_live2d = false;
|
||||
|
||||
// FNV-1a 64 over a D3D9 shader token stream (ends with D3DSIO_END = 0x0000FFFF)
|
||||
uint64_t bytecode_hash(const DWORD *func) {
|
||||
if (func == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
const DWORD *p = func;
|
||||
const DWORD *cap = func + 65536; // safety bound
|
||||
while (p < cap && *p != 0x0000FFFF) {
|
||||
p++;
|
||||
}
|
||||
const size_t n_bytes = ((size_t)(p - func) + 1) * sizeof(DWORD);
|
||||
uint64_t h = 1469598103934665603ULL;
|
||||
const auto *bytes = reinterpret_cast<const uint8_t *>(func);
|
||||
for (size_t i = 0; i < n_bytes; i++) {
|
||||
h ^= bytes[i];
|
||||
h *= 1099511628211ULL;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
// known SDVX Live2D shader bytecode hashes (4 pixel + 3 vertex). stable
|
||||
// across runs because the game ships fixed shaders. the two sets are disjoint so
|
||||
// a single shader can be classified by its own hash alone.
|
||||
bool hash_is_live2d(uint64_t hash) {
|
||||
switch (hash) {
|
||||
case 0x75c89951817421a4ULL: // pixel: dominant model draw (~4.9M prims/120f in-song)
|
||||
case 0x2d7ce428c6b4775dULL: // pixel: masked model draw
|
||||
case 0x3ce00cc6111c10e7ULL: // pixel: mask generation
|
||||
case 0x8bb3a2f37150ac34ULL: // pixel: mask generation (variant)
|
||||
case 0xe9cf898c331e2a51ULL: // vertex
|
||||
case 0x94dc84e7b7c0f437ULL: // vertex
|
||||
case 0xc872937c5cc04309ULL: // vertex
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// classify a shader at creation time and record it if it is Live2D. erasing on a
|
||||
// miss keeps the set correct if the runtime reuses a freed shader pointer.
|
||||
void classify_shader(void *shader, const DWORD *func) {
|
||||
if (hash_is_live2d(bytecode_hash(func))) {
|
||||
g_live2d_shaders.insert(shader);
|
||||
} else {
|
||||
g_live2d_shaders.erase(shader);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace d3d9_live2d {
|
||||
|
||||
// stage 1: fingerprint each shader as the game creates it
|
||||
void on_create_vertex_shader(IDirect3DVertexShader9 *shader, const DWORD *func) {
|
||||
if (tracking_enabled() && shader != nullptr) [[unlikely]] {
|
||||
classify_shader(shader, func);
|
||||
}
|
||||
}
|
||||
|
||||
void on_create_pixel_shader(IDirect3DPixelShader9 *shader, const DWORD *func) {
|
||||
if (tracking_enabled() && shader != nullptr) [[unlikely]] {
|
||||
classify_shader(shader, func);
|
||||
}
|
||||
}
|
||||
|
||||
// stage 2: remember whether the just-bound shader is a Live2D one
|
||||
void on_set_vertex_shader(IDirect3DVertexShader9 *shader) {
|
||||
if (tracking_enabled()) [[unlikely]] {
|
||||
g_cur_vs_is_live2d = g_live2d_shaders.count(shader) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
void on_set_pixel_shader(IDirect3DPixelShader9 *shader) {
|
||||
if (tracking_enabled()) [[unlikely]] {
|
||||
g_cur_ps_is_live2d = g_live2d_shaders.count(shader) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
// stage 3: drop the draw if the skip is active and a Live2D shader is bound
|
||||
bool should_skip_draw() {
|
||||
return graphics_sdvx_live2d_should_skip() && (g_cur_ps_is_live2d || g_cur_vs_is_live2d);
|
||||
}
|
||||
|
||||
} // namespace d3d9_live2d
|
||||
|
||||
#endif // SPICE64
|
||||
#include "d3d9_live2d.h"
|
||||
|
||||
// only the Live2D-capable SDVX versions are 64-bit, so the entire implementation
|
||||
// is compiled out of 32-bit builds (the header supplies inline no-op stubs there).
|
||||
#ifdef SPICE64
|
||||
|
||||
#include <cstdint>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "hooks/graphics/graphics.h"
|
||||
|
||||
// how the Live2D draw filtering works
|
||||
// ------------------------------------
|
||||
// SDVX draws its Live2D characters with a small, fixed set of pixel and
|
||||
// vertex shaders. to skip those draws (and save GPU) we have to recognise them at
|
||||
// the exact moment the game issues a draw call. the d3d9 device hooks feed three
|
||||
// kinds of events into this module:
|
||||
//
|
||||
// 1. shader creation (on_create_pixel_shader / on_create_vertex_shader)
|
||||
// the game compiles its shaders once at load. we can't trust the shader
|
||||
// *object pointer* to identify a shader (it's just a heap address that
|
||||
// varies per run and can be recycled), so instead we hash the shader's
|
||||
// D3D9 *bytecode* - that fingerprint is stable across runs because the
|
||||
// game ships the same shaders. if the hash matches a known Live2D shader
|
||||
// we remember that object pointer in g_live2d_shaders.
|
||||
//
|
||||
// 2. shader binding (on_set_pixel_shader / on_set_vertex_shader)
|
||||
// whenever the game binds a shader we look it up in that set once and cache
|
||||
// the yes/no answer in g_cur_ps_is_live2d / g_cur_vs_is_live2d. binds happen
|
||||
// far less often than draws, so this is where the lookup cost lives.
|
||||
//
|
||||
// 3. draw call (should_skip_draw, called from every Draw* hook)
|
||||
// the per-draw question "is this a Live2D draw?" is then just reading those
|
||||
// two cached bools - no hashing, no map lookups. if the skip is currently
|
||||
// active (see graphics_sdvx_live2d_should_skip) and either bound shader is
|
||||
// Live2D, the Draw* hook drops the call instead of forwarding it.
|
||||
//
|
||||
// everything is gated on the feature being enabled (mode != Off); when it's Off
|
||||
// every entry point is a single predicted-not-taken branch. d3d9 rendering for a
|
||||
// device is single-threaded, so none of this state needs locking.
|
||||
|
||||
namespace {
|
||||
|
||||
// shader state is tracked whenever the feature might act (mode != Off) so the
|
||||
// known-shader set is populated before a song starts. when Off, every entry
|
||||
// point is a single cheap branch.
|
||||
bool tracking_enabled() {
|
||||
return GRAPHICS_SDVX_LIVE2D_MODE != SdvxLive2dMode::Off;
|
||||
}
|
||||
|
||||
// the set of shader objects (pixel or vertex) whose bytecode matched a known
|
||||
// Live2D fingerprint. only matching shaders are stored, so this stays tiny.
|
||||
std::unordered_set<void *> g_live2d_shaders;
|
||||
|
||||
// whether the currently-bound shaders are known Live2D shaders. cached at set
|
||||
// time so the per-draw check is just two bool reads.
|
||||
bool g_cur_ps_is_live2d = false;
|
||||
bool g_cur_vs_is_live2d = false;
|
||||
|
||||
// FNV-1a 64 over a D3D9 shader token stream (ends with D3DSIO_END = 0x0000FFFF)
|
||||
uint64_t bytecode_hash(const DWORD *func) {
|
||||
if (func == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
const DWORD *p = func;
|
||||
const DWORD *cap = func + 65536; // safety bound
|
||||
while (p < cap && *p != 0x0000FFFF) {
|
||||
p++;
|
||||
}
|
||||
const size_t n_bytes = ((size_t)(p - func) + 1) * sizeof(DWORD);
|
||||
uint64_t h = 1469598103934665603ULL;
|
||||
const auto *bytes = reinterpret_cast<const uint8_t *>(func);
|
||||
for (size_t i = 0; i < n_bytes; i++) {
|
||||
h ^= bytes[i];
|
||||
h *= 1099511628211ULL;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
// known SDVX Live2D shader bytecode hashes (4 pixel + 3 vertex). stable
|
||||
// across runs because the game ships fixed shaders. the two sets are disjoint so
|
||||
// a single shader can be classified by its own hash alone.
|
||||
bool hash_is_live2d(uint64_t hash) {
|
||||
switch (hash) {
|
||||
case 0x75c89951817421a4ULL: // pixel: dominant model draw (~4.9M prims/120f in-song)
|
||||
case 0x2d7ce428c6b4775dULL: // pixel: masked model draw
|
||||
case 0x3ce00cc6111c10e7ULL: // pixel: mask generation
|
||||
case 0x8bb3a2f37150ac34ULL: // pixel: mask generation (variant)
|
||||
case 0xe9cf898c331e2a51ULL: // vertex
|
||||
case 0x94dc84e7b7c0f437ULL: // vertex
|
||||
case 0xc872937c5cc04309ULL: // vertex
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// classify a shader at creation time and record it if it is Live2D. erasing on a
|
||||
// miss keeps the set correct if the runtime reuses a freed shader pointer.
|
||||
void classify_shader(void *shader, const DWORD *func) {
|
||||
if (hash_is_live2d(bytecode_hash(func))) {
|
||||
g_live2d_shaders.insert(shader);
|
||||
} else {
|
||||
g_live2d_shaders.erase(shader);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace d3d9_live2d {
|
||||
|
||||
// stage 1: fingerprint each shader as the game creates it
|
||||
void on_create_vertex_shader(IDirect3DVertexShader9 *shader, const DWORD *func) {
|
||||
if (tracking_enabled() && shader != nullptr) [[unlikely]] {
|
||||
classify_shader(shader, func);
|
||||
}
|
||||
}
|
||||
|
||||
void on_create_pixel_shader(IDirect3DPixelShader9 *shader, const DWORD *func) {
|
||||
if (tracking_enabled() && shader != nullptr) [[unlikely]] {
|
||||
classify_shader(shader, func);
|
||||
}
|
||||
}
|
||||
|
||||
// stage 2: remember whether the just-bound shader is a Live2D one
|
||||
void on_set_vertex_shader(IDirect3DVertexShader9 *shader) {
|
||||
if (tracking_enabled()) [[unlikely]] {
|
||||
g_cur_vs_is_live2d = g_live2d_shaders.count(shader) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
void on_set_pixel_shader(IDirect3DPixelShader9 *shader) {
|
||||
if (tracking_enabled()) [[unlikely]] {
|
||||
g_cur_ps_is_live2d = g_live2d_shaders.count(shader) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
// stage 3: drop the draw if the skip is active and a Live2D shader is bound
|
||||
bool should_skip_draw() {
|
||||
return graphics_sdvx_live2d_should_skip() && (g_cur_ps_is_live2d || g_cur_vs_is_live2d);
|
||||
}
|
||||
|
||||
} // namespace d3d9_live2d
|
||||
|
||||
#endif // SPICE64
|
||||
|
||||
@@ -1,44 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d9.h>
|
||||
|
||||
// SDVX Live2D draw-skip support for the D3D9 backend.
|
||||
//
|
||||
// SDVX renders its Live2D navigator / in-song character through a fixed set of
|
||||
// shaders. when the skip is active (see graphics_sdvx_live2d_should_skip)
|
||||
// the matching draw calls are dropped to save GPU. shaders are identified by a
|
||||
// stable hash of their D3D9 bytecode (object pointers vary per run, the bytecode
|
||||
// does not). the hashes were captured with the draw-call fingerprinting tool.
|
||||
//
|
||||
// every entry point is a no-op unless the feature is enabled (mode != Off), and
|
||||
// d3d9 rendering for a device is single-threaded, so none of this needs locking.
|
||||
namespace d3d9_live2d {
|
||||
|
||||
#ifdef SPICE64
|
||||
|
||||
// record a shader's bytecode fingerprint at creation time
|
||||
void on_create_vertex_shader(IDirect3DVertexShader9 *shader, const DWORD *func);
|
||||
void on_create_pixel_shader(IDirect3DPixelShader9 *shader, const DWORD *func);
|
||||
|
||||
// remember the currently-bound shaders
|
||||
void on_set_vertex_shader(IDirect3DVertexShader9 *shader);
|
||||
void on_set_pixel_shader(IDirect3DPixelShader9 *shader);
|
||||
|
||||
// true if the current draw call should be dropped (skip active AND the bound
|
||||
// shaders identify it as SDVX Live2D)
|
||||
bool should_skip_draw();
|
||||
|
||||
#else // !SPICE64
|
||||
|
||||
// only the Live2D-capable SDVX versions are 64-bit; on 32-bit every entry point
|
||||
// compiles away to nothing, so the d3d9 device hooks need no #ifdefs at their
|
||||
// call sites.
|
||||
inline void on_create_vertex_shader(IDirect3DVertexShader9 *, const DWORD *) {}
|
||||
inline void on_create_pixel_shader(IDirect3DPixelShader9 *, const DWORD *) {}
|
||||
inline void on_set_vertex_shader(IDirect3DVertexShader9 *) {}
|
||||
inline void on_set_pixel_shader(IDirect3DPixelShader9 *) {}
|
||||
inline bool should_skip_draw() { return false; }
|
||||
|
||||
#endif // SPICE64
|
||||
}
|
||||
#pragma once
|
||||
|
||||
#include <windows.h>
|
||||
#include <d3d9.h>
|
||||
|
||||
// SDVX Live2D draw-skip support for the D3D9 backend.
|
||||
//
|
||||
// SDVX renders its Live2D navigator / in-song character through a fixed set of
|
||||
// shaders. when the skip is active (see graphics_sdvx_live2d_should_skip)
|
||||
// the matching draw calls are dropped to save GPU. shaders are identified by a
|
||||
// stable hash of their D3D9 bytecode (object pointers vary per run, the bytecode
|
||||
// does not). the hashes were captured with the draw-call fingerprinting tool.
|
||||
//
|
||||
// every entry point is a no-op unless the feature is enabled (mode != Off), and
|
||||
// d3d9 rendering for a device is single-threaded, so none of this needs locking.
|
||||
namespace d3d9_live2d {
|
||||
|
||||
#ifdef SPICE64
|
||||
|
||||
// record a shader's bytecode fingerprint at creation time
|
||||
void on_create_vertex_shader(IDirect3DVertexShader9 *shader, const DWORD *func);
|
||||
void on_create_pixel_shader(IDirect3DPixelShader9 *shader, const DWORD *func);
|
||||
|
||||
// remember the currently-bound shaders
|
||||
void on_set_vertex_shader(IDirect3DVertexShader9 *shader);
|
||||
void on_set_pixel_shader(IDirect3DPixelShader9 *shader);
|
||||
|
||||
// true if the current draw call should be dropped (skip active AND the bound
|
||||
// shaders identify it as SDVX Live2D)
|
||||
bool should_skip_draw();
|
||||
|
||||
#else // !SPICE64
|
||||
|
||||
// only the Live2D-capable SDVX versions are 64-bit; on 32-bit every entry point
|
||||
// compiles away to nothing, so the d3d9 device hooks need no #ifdefs at their
|
||||
// call sites.
|
||||
inline void on_create_vertex_shader(IDirect3DVertexShader9 *, const DWORD *) {}
|
||||
inline void on_create_pixel_shader(IDirect3DPixelShader9 *, const DWORD *) {}
|
||||
inline void on_set_vertex_shader(IDirect3DVertexShader9 *) {}
|
||||
inline void on_set_pixel_shader(IDirect3DPixelShader9 *) {}
|
||||
inline bool should_skip_draw() { return false; }
|
||||
|
||||
#endif // SPICE64
|
||||
}
|
||||
|
||||
@@ -1,480 +1,480 @@
|
||||
#include "nvapi_impl.h"
|
||||
|
||||
#ifdef SPICE64
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "external/nvapi/nvapi.h"
|
||||
#include "hooks/libraryhook.h"
|
||||
#include "util/logging.h"
|
||||
#include "util/sysutils.h"
|
||||
|
||||
namespace nvapi_impl {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr unsigned int NVAPI_INITIALIZE_ID = 0x0150E828;
|
||||
constexpr unsigned int NVAPI_INITIALIZE_EX_ID = 0xAD298D3F;
|
||||
constexpr unsigned int NVAPI_UNLOAD_ID = 0xD22BDD7E;
|
||||
constexpr unsigned int NVAPI_ENUM_PHYSICAL_GPUS_ID = 0xE5AC921F;
|
||||
constexpr unsigned int NVAPI_GPU_GET_CONNECTED_DISPLAY_IDS_ID = 0x0078DBA2;
|
||||
constexpr unsigned int NVAPI_DISP_GET_GDI_PRIMARY_DISPLAY_ID = 0x1E9D8A31;
|
||||
constexpr unsigned int NVAPI_DISP_GET_DISPLAY_CONFIG_ID = 0x11ABCCF8;
|
||||
constexpr unsigned int NVAPI_DISP_SET_DISPLAY_CONFIG_ID = 0x5D8CF8DE;
|
||||
|
||||
constexpr char NVAPI_DLL_NAME_A[] = "nvapi64.dll";
|
||||
|
||||
struct SyntheticDisplay {
|
||||
NvU32 display_id;
|
||||
NvU32 width;
|
||||
NvU32 height;
|
||||
NvU32 color_depth;
|
||||
NvS32 x;
|
||||
NvS32 y;
|
||||
NvU32 refresh_rate_1k;
|
||||
NV_ROTATE rotation;
|
||||
bool primary;
|
||||
};
|
||||
|
||||
static bool provider_initialized = false;
|
||||
static bool nvapi_initialized = false;
|
||||
static int gpu_handle_storage = 0;
|
||||
// snapshot of the Win32 display state exposed through synthetic NVAPI
|
||||
static std::vector<SyntheticDisplay> displays;
|
||||
|
||||
static NvPhysicalGpuHandle get_gpu_handle() {
|
||||
return reinterpret_cast<NvPhysicalGpuHandle>(&gpu_handle_storage);
|
||||
}
|
||||
|
||||
static NV_ROTATE get_rotation(DWORD orientation) {
|
||||
switch (orientation) {
|
||||
case DMDO_90:
|
||||
return NV_ROTATE_90;
|
||||
case DMDO_180:
|
||||
return NV_ROTATE_180;
|
||||
case DMDO_270:
|
||||
return NV_ROTATE_270;
|
||||
default:
|
||||
return NV_ROTATE_0;
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<SyntheticDisplay> enumerate_displays(
|
||||
uint32_t main_refresh_hz,
|
||||
uint32_t sub_refresh_hz) {
|
||||
|
||||
std::vector<SyntheticDisplay> result;
|
||||
|
||||
// reuse the active monitor list, then read live modes after -mainmonitor changes
|
||||
for (const auto &monitor : sysutils::enumerate_monitors()) {
|
||||
DEVMODEA mode {};
|
||||
mode.dmSize = sizeof(mode);
|
||||
if (!EnumDisplaySettingsExA(
|
||||
monitor.display_name.c_str(),
|
||||
ENUM_CURRENT_SETTINGS,
|
||||
&mode,
|
||||
0)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const bool primary = mode.dmPosition.x == 0 && mode.dmPosition.y == 0;
|
||||
result.push_back({
|
||||
.display_id = 0,
|
||||
.width = mode.dmPelsWidth,
|
||||
.height = mode.dmPelsHeight,
|
||||
.color_depth = mode.dmBitsPerPel > 0 ? mode.dmBitsPerPel : 32,
|
||||
.x = mode.dmPosition.x,
|
||||
.y = mode.dmPosition.y,
|
||||
.refresh_rate_1k = 0,
|
||||
.rotation = get_rotation(mode.dmDisplayOrientation),
|
||||
.primary = primary,
|
||||
});
|
||||
}
|
||||
|
||||
std::stable_sort(result.begin(), result.end(), [](const auto &left, const auto &right) {
|
||||
return left.primary && !right.primary;
|
||||
});
|
||||
|
||||
if (result.size() > 2) {
|
||||
result.resize(2);
|
||||
}
|
||||
|
||||
if (result.empty()) {
|
||||
result.push_back({
|
||||
.display_id = 0,
|
||||
.width = 1920,
|
||||
.height = 1080,
|
||||
.color_depth = 32,
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.refresh_rate_1k = 0,
|
||||
.rotation = NV_ROTATE_0,
|
||||
.primary = true,
|
||||
});
|
||||
}
|
||||
|
||||
for (size_t index = 0; index < result.size(); index++) {
|
||||
auto &display = result[index];
|
||||
display.primary = index == 0;
|
||||
display.display_id = 0x80000000u | static_cast<NvU32>(index + 1);
|
||||
const uint32_t refresh_hz = index == 0 ? main_refresh_hz : sub_refresh_hz;
|
||||
display.refresh_rate_1k = refresh_hz * 1000;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// initializes NVAPI for the calling process.
|
||||
// marks the synthetic provider initialized without contacting a driver.
|
||||
static NvAPI_Status __cdecl NvAPI_Initialize_impl() {
|
||||
log_misc("nvapi_impl", "NvAPI_Initialize");
|
||||
nvapi_initialized = true;
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// initializes NVAPI with additional client flags.
|
||||
// accepts the flags and marks the synthetic provider initialized.
|
||||
static NvAPI_Status __cdecl NvAPI_InitializeEx_impl(NvU32 flags) {
|
||||
log_misc("nvapi_impl", "NvAPI_InitializeEx(flags={:#x})", flags);
|
||||
nvapi_initialized = true;
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// releases NVAPI state held for the calling process.
|
||||
// clears the synthetic initialization state while leaving the provider installed.
|
||||
static NvAPI_Status __cdecl NvAPI_Unload_impl() {
|
||||
log_misc("nvapi_impl", "NvAPI_Unload");
|
||||
nvapi_initialized = false;
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// enumerates physical GPU handles managed by the NVIDIA driver.
|
||||
// returns one stable synthetic GPU containing all exposed displays.
|
||||
static NvAPI_Status __cdecl NvAPI_EnumPhysicalGPUs_impl(
|
||||
NvPhysicalGpuHandle gpu_handles[NVAPI_MAX_PHYSICAL_GPUS],
|
||||
NvU32 *gpu_count) {
|
||||
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_EnumPhysicalGPUs(handles={}, count={})",
|
||||
fmt::ptr(gpu_handles),
|
||||
fmt::ptr(gpu_count));
|
||||
|
||||
if (!nvapi_initialized) {
|
||||
return NVAPI_API_NOT_INITIALIZED;
|
||||
}
|
||||
if (gpu_handles == nullptr || gpu_count == nullptr) {
|
||||
return NVAPI_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
gpu_handles[0] = get_gpu_handle();
|
||||
*gpu_count = 1;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_EnumPhysicalGPUs - gpu={}, count={}",
|
||||
fmt::ptr(gpu_handles[0]),
|
||||
*gpu_count);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// returns connected display descriptors for a physical GPU.
|
||||
// exposes the monitor snapshot as DP primary and HDMI secondary displays.
|
||||
static NvAPI_Status __cdecl NvAPI_GPU_GetConnectedDisplayIds_impl(
|
||||
NvPhysicalGpuHandle gpu_handle,
|
||||
NV_GPU_DISPLAYIDS *display_ids,
|
||||
NvU32 *display_id_count,
|
||||
NvU32 flags) {
|
||||
|
||||
const NvU32 input_count = display_id_count != nullptr ? *display_id_count : 0;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_GPU_GetConnectedDisplayIds(gpu={}, ids={}, count={}, flags={:#x})",
|
||||
fmt::ptr(gpu_handle),
|
||||
fmt::ptr(display_ids),
|
||||
input_count,
|
||||
flags);
|
||||
|
||||
if (!nvapi_initialized) {
|
||||
return NVAPI_API_NOT_INITIALIZED;
|
||||
}
|
||||
if (gpu_handle != get_gpu_handle()) {
|
||||
return NVAPI_EXPECTED_PHYSICAL_GPU_HANDLE;
|
||||
}
|
||||
if (display_id_count == nullptr) {
|
||||
return NVAPI_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
const NvU32 required_count = static_cast<NvU32>(displays.size());
|
||||
if (display_ids == nullptr) {
|
||||
*display_id_count = required_count;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_GPU_GetConnectedDisplayIds - required_count={}",
|
||||
required_count);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
const NvU32 capacity = *display_id_count;
|
||||
*display_id_count = required_count;
|
||||
if (capacity < required_count) {
|
||||
return NVAPI_INSUFFICIENT_BUFFER;
|
||||
}
|
||||
|
||||
for (NvU32 index = 0; index < required_count; index++) {
|
||||
const auto &source = displays[index];
|
||||
auto &destination = display_ids[index];
|
||||
destination = {};
|
||||
destination.version = NV_GPU_DISPLAYIDS_VER;
|
||||
destination.connectorType = source.primary ?
|
||||
NV_MONITOR_CONN_TYPE_DP : NV_MONITOR_CONN_TYPE_HDMI;
|
||||
destination.displayId = source.display_id;
|
||||
destination.isActive = 1;
|
||||
destination.isOSVisible = 1;
|
||||
destination.isConnected = 1;
|
||||
destination.isPhysicallyConnected = 1;
|
||||
}
|
||||
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_GPU_GetConnectedDisplayIds - returned_count={}",
|
||||
required_count);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// returns the NVAPI display ID associated with the Windows GDI primary.
|
||||
// returns the first synthetic display, ordered from the live desktop origin.
|
||||
static NvAPI_Status __cdecl NvAPI_DISP_GetGDIPrimaryDisplayId_impl(NvU32 *display_id) {
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_GetGDIPrimaryDisplayId(display_id={})",
|
||||
fmt::ptr(display_id));
|
||||
|
||||
if (!nvapi_initialized) {
|
||||
return NVAPI_API_NOT_INITIALIZED;
|
||||
}
|
||||
if (display_id == nullptr || displays.empty()) {
|
||||
return NVAPI_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
*display_id = displays.front().display_id;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_GetGDIPrimaryDisplayId - display_id={:#x}",
|
||||
*display_id);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
static void fill_source_mode(
|
||||
NV_DISPLAYCONFIG_SOURCE_MODE_INFO *destination,
|
||||
const SyntheticDisplay &source) {
|
||||
|
||||
if (destination == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
*destination = {};
|
||||
destination->resolution.width = source.width;
|
||||
destination->resolution.height = source.height;
|
||||
destination->resolution.colorDepth = source.color_depth;
|
||||
destination->colorFormat = NV_FORMAT_A8R8G8B8;
|
||||
destination->position.x = source.x;
|
||||
destination->position.y = source.y;
|
||||
destination->spanningOrientation = NV_DISPLAYCONFIG_SPAN_NONE;
|
||||
destination->bGDIPrimary = source.primary ? 1 : 0;
|
||||
}
|
||||
|
||||
static NvAPI_Status fill_target(
|
||||
NV_DISPLAYCONFIG_PATH_TARGET_INFO *destination,
|
||||
const SyntheticDisplay &source,
|
||||
NvU32 target_id) {
|
||||
|
||||
if (destination == nullptr) {
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
auto *details = destination->details;
|
||||
destination->displayId = source.display_id;
|
||||
destination->targetId = target_id;
|
||||
|
||||
if (details == nullptr) {
|
||||
return NVAPI_OK;
|
||||
}
|
||||
if (details->version != NV_DISPLAYCONFIG_PATH_ADVANCED_TARGET_INFO_VER) {
|
||||
return NVAPI_INCOMPATIBLE_STRUCT_VERSION;
|
||||
}
|
||||
|
||||
*details = {};
|
||||
details->version = NV_DISPLAYCONFIG_PATH_ADVANCED_TARGET_INFO_VER;
|
||||
details->rotation = source.rotation;
|
||||
details->scaling = NV_SCALING_DEFAULT;
|
||||
details->refreshRate1K = source.refresh_rate_1k;
|
||||
details->timingOverride = NV_TIMING_OVERRIDE_CURRENT;
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// retrieves the current global display topology through NVAPI's three-pass contract.
|
||||
// fills caller-owned buffers from the synthetic monitor snapshot and configured rates.
|
||||
static NvAPI_Status __cdecl NvAPI_DISP_GetDisplayConfig_impl(
|
||||
NvU32 *path_info_count,
|
||||
NV_DISPLAYCONFIG_PATH_INFO *path_info) {
|
||||
|
||||
const NvU32 input_count = path_info_count != nullptr ? *path_info_count : 0;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_GetDisplayConfig(count={}, paths={})",
|
||||
input_count,
|
||||
fmt::ptr(path_info));
|
||||
|
||||
if (!nvapi_initialized) {
|
||||
return NVAPI_API_NOT_INITIALIZED;
|
||||
}
|
||||
if (path_info_count == nullptr) {
|
||||
return NVAPI_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
const NvU32 required_count = static_cast<NvU32>(displays.size());
|
||||
if (path_info == nullptr) {
|
||||
*path_info_count = required_count;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_GetDisplayConfig - required_count={}",
|
||||
required_count);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
const NvU32 capacity = *path_info_count;
|
||||
*path_info_count = required_count;
|
||||
if (capacity < required_count) {
|
||||
return NVAPI_INSUFFICIENT_BUFFER;
|
||||
}
|
||||
|
||||
for (NvU32 index = 0; index < required_count; index++) {
|
||||
auto &path = path_info[index];
|
||||
if (path.version != NV_DISPLAYCONFIG_PATH_INFO_VER2) {
|
||||
return NVAPI_INCOMPATIBLE_STRUCT_VERSION;
|
||||
}
|
||||
if (path.targetInfo != nullptr && path.targetInfoCount < 1) {
|
||||
return NVAPI_INSUFFICIENT_BUFFER;
|
||||
}
|
||||
|
||||
const auto &display = displays[index];
|
||||
path.sourceId = index;
|
||||
path.targetInfoCount = 1;
|
||||
path.IsNonNVIDIAAdapter = 0;
|
||||
path.pOSAdapterID = nullptr;
|
||||
fill_source_mode(path.sourceModeInfo, display);
|
||||
|
||||
const NvAPI_Status status = fill_target(path.targetInfo, display, index);
|
||||
if (status != NVAPI_OK) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_GetDisplayConfig - returned_count={}",
|
||||
required_count);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// applies a supplied global display topology through the NVIDIA driver.
|
||||
// accepts the cabinet topology without making any changes to Windows.
|
||||
static NvAPI_Status __cdecl NvAPI_DISP_SetDisplayConfig_impl(
|
||||
NvU32 path_info_count,
|
||||
NV_DISPLAYCONFIG_PATH_INFO *path_info,
|
||||
NvU32 flags) {
|
||||
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_SetDisplayConfig(count={}, paths={}, flags={:#x})",
|
||||
path_info_count,
|
||||
fmt::ptr(path_info),
|
||||
flags);
|
||||
|
||||
if (!nvapi_initialized) {
|
||||
return NVAPI_API_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
log_misc("nvapi_impl", "NvAPI_DISP_SetDisplayConfig - return synthetic success");
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static uintptr_t *query_result(T function) {
|
||||
return reinterpret_cast<uintptr_t *>(function);
|
||||
}
|
||||
|
||||
// resolves an NVAPI function ID to its implementation address.
|
||||
// exposes only the synthetic entry points used by KFC and rejects all others.
|
||||
static uintptr_t *__cdecl NvAPI_QueryInterface_impl(unsigned int function_id) {
|
||||
uintptr_t *result = nullptr;
|
||||
switch (function_id) {
|
||||
case NVAPI_INITIALIZE_ID:
|
||||
result = query_result(NvAPI_Initialize_impl);
|
||||
break;
|
||||
case NVAPI_INITIALIZE_EX_ID:
|
||||
result = query_result(NvAPI_InitializeEx_impl);
|
||||
break;
|
||||
case NVAPI_UNLOAD_ID:
|
||||
result = query_result(NvAPI_Unload_impl);
|
||||
break;
|
||||
case NVAPI_ENUM_PHYSICAL_GPUS_ID:
|
||||
result = query_result(NvAPI_EnumPhysicalGPUs_impl);
|
||||
break;
|
||||
case NVAPI_GPU_GET_CONNECTED_DISPLAY_IDS_ID:
|
||||
result = query_result(NvAPI_GPU_GetConnectedDisplayIds_impl);
|
||||
break;
|
||||
case NVAPI_DISP_GET_GDI_PRIMARY_DISPLAY_ID:
|
||||
result = query_result(NvAPI_DISP_GetGDIPrimaryDisplayId_impl);
|
||||
break;
|
||||
case NVAPI_DISP_GET_DISPLAY_CONFIG_ID:
|
||||
result = query_result(NvAPI_DISP_GetDisplayConfig_impl);
|
||||
break;
|
||||
case NVAPI_DISP_SET_DISPLAY_CONFIG_ID:
|
||||
result = query_result(NvAPI_DISP_SetDisplayConfig_impl);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_QueryInterface(0x{:x}) - {}",
|
||||
function_id,
|
||||
result != nullptr ? "implemented" : "unsupported");
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool initialize(HINSTANCE dll, uint32_t main_refresh_hz, uint32_t sub_refresh_hz) {
|
||||
if (provider_initialized) {
|
||||
return true;
|
||||
}
|
||||
if (dll == nullptr) {
|
||||
log_warning("nvapi_impl", "invalid synthetic module handle");
|
||||
return false;
|
||||
}
|
||||
|
||||
displays = enumerate_displays(main_refresh_hz, sub_refresh_hz);
|
||||
libraryhook_hook_library(NVAPI_DLL_NAME_A, dll);
|
||||
libraryhook_hook_proc("nvapi_QueryInterface", NvAPI_QueryInterface_impl);
|
||||
libraryhook_enable();
|
||||
|
||||
provider_initialized = true;
|
||||
log_info(
|
||||
"nvapi_impl",
|
||||
"synthetic {} enabled with {} display(s), main={} Hz, sub={} Hz",
|
||||
NVAPI_DLL_NAME_A,
|
||||
displays.size(),
|
||||
main_refresh_hz,
|
||||
sub_refresh_hz);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
#include "nvapi_impl.h"
|
||||
|
||||
#ifdef SPICE64
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "external/nvapi/nvapi.h"
|
||||
#include "hooks/libraryhook.h"
|
||||
#include "util/logging.h"
|
||||
#include "util/sysutils.h"
|
||||
|
||||
namespace nvapi_impl {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr unsigned int NVAPI_INITIALIZE_ID = 0x0150E828;
|
||||
constexpr unsigned int NVAPI_INITIALIZE_EX_ID = 0xAD298D3F;
|
||||
constexpr unsigned int NVAPI_UNLOAD_ID = 0xD22BDD7E;
|
||||
constexpr unsigned int NVAPI_ENUM_PHYSICAL_GPUS_ID = 0xE5AC921F;
|
||||
constexpr unsigned int NVAPI_GPU_GET_CONNECTED_DISPLAY_IDS_ID = 0x0078DBA2;
|
||||
constexpr unsigned int NVAPI_DISP_GET_GDI_PRIMARY_DISPLAY_ID = 0x1E9D8A31;
|
||||
constexpr unsigned int NVAPI_DISP_GET_DISPLAY_CONFIG_ID = 0x11ABCCF8;
|
||||
constexpr unsigned int NVAPI_DISP_SET_DISPLAY_CONFIG_ID = 0x5D8CF8DE;
|
||||
|
||||
constexpr char NVAPI_DLL_NAME_A[] = "nvapi64.dll";
|
||||
|
||||
struct SyntheticDisplay {
|
||||
NvU32 display_id;
|
||||
NvU32 width;
|
||||
NvU32 height;
|
||||
NvU32 color_depth;
|
||||
NvS32 x;
|
||||
NvS32 y;
|
||||
NvU32 refresh_rate_1k;
|
||||
NV_ROTATE rotation;
|
||||
bool primary;
|
||||
};
|
||||
|
||||
static bool provider_initialized = false;
|
||||
static bool nvapi_initialized = false;
|
||||
static int gpu_handle_storage = 0;
|
||||
// snapshot of the Win32 display state exposed through synthetic NVAPI
|
||||
static std::vector<SyntheticDisplay> displays;
|
||||
|
||||
static NvPhysicalGpuHandle get_gpu_handle() {
|
||||
return reinterpret_cast<NvPhysicalGpuHandle>(&gpu_handle_storage);
|
||||
}
|
||||
|
||||
static NV_ROTATE get_rotation(DWORD orientation) {
|
||||
switch (orientation) {
|
||||
case DMDO_90:
|
||||
return NV_ROTATE_90;
|
||||
case DMDO_180:
|
||||
return NV_ROTATE_180;
|
||||
case DMDO_270:
|
||||
return NV_ROTATE_270;
|
||||
default:
|
||||
return NV_ROTATE_0;
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<SyntheticDisplay> enumerate_displays(
|
||||
uint32_t main_refresh_hz,
|
||||
uint32_t sub_refresh_hz) {
|
||||
|
||||
std::vector<SyntheticDisplay> result;
|
||||
|
||||
// reuse the active monitor list, then read live modes after -mainmonitor changes
|
||||
for (const auto &monitor : sysutils::enumerate_monitors()) {
|
||||
DEVMODEA mode {};
|
||||
mode.dmSize = sizeof(mode);
|
||||
if (!EnumDisplaySettingsExA(
|
||||
monitor.display_name.c_str(),
|
||||
ENUM_CURRENT_SETTINGS,
|
||||
&mode,
|
||||
0)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const bool primary = mode.dmPosition.x == 0 && mode.dmPosition.y == 0;
|
||||
result.push_back({
|
||||
.display_id = 0,
|
||||
.width = mode.dmPelsWidth,
|
||||
.height = mode.dmPelsHeight,
|
||||
.color_depth = mode.dmBitsPerPel > 0 ? mode.dmBitsPerPel : 32,
|
||||
.x = mode.dmPosition.x,
|
||||
.y = mode.dmPosition.y,
|
||||
.refresh_rate_1k = 0,
|
||||
.rotation = get_rotation(mode.dmDisplayOrientation),
|
||||
.primary = primary,
|
||||
});
|
||||
}
|
||||
|
||||
std::stable_sort(result.begin(), result.end(), [](const auto &left, const auto &right) {
|
||||
return left.primary && !right.primary;
|
||||
});
|
||||
|
||||
if (result.size() > 2) {
|
||||
result.resize(2);
|
||||
}
|
||||
|
||||
if (result.empty()) {
|
||||
result.push_back({
|
||||
.display_id = 0,
|
||||
.width = 1920,
|
||||
.height = 1080,
|
||||
.color_depth = 32,
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.refresh_rate_1k = 0,
|
||||
.rotation = NV_ROTATE_0,
|
||||
.primary = true,
|
||||
});
|
||||
}
|
||||
|
||||
for (size_t index = 0; index < result.size(); index++) {
|
||||
auto &display = result[index];
|
||||
display.primary = index == 0;
|
||||
display.display_id = 0x80000000u | static_cast<NvU32>(index + 1);
|
||||
const uint32_t refresh_hz = index == 0 ? main_refresh_hz : sub_refresh_hz;
|
||||
display.refresh_rate_1k = refresh_hz * 1000;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// initializes NVAPI for the calling process.
|
||||
// marks the synthetic provider initialized without contacting a driver.
|
||||
static NvAPI_Status __cdecl NvAPI_Initialize_impl() {
|
||||
log_misc("nvapi_impl", "NvAPI_Initialize");
|
||||
nvapi_initialized = true;
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// initializes NVAPI with additional client flags.
|
||||
// accepts the flags and marks the synthetic provider initialized.
|
||||
static NvAPI_Status __cdecl NvAPI_InitializeEx_impl(NvU32 flags) {
|
||||
log_misc("nvapi_impl", "NvAPI_InitializeEx(flags={:#x})", flags);
|
||||
nvapi_initialized = true;
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// releases NVAPI state held for the calling process.
|
||||
// clears the synthetic initialization state while leaving the provider installed.
|
||||
static NvAPI_Status __cdecl NvAPI_Unload_impl() {
|
||||
log_misc("nvapi_impl", "NvAPI_Unload");
|
||||
nvapi_initialized = false;
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// enumerates physical GPU handles managed by the NVIDIA driver.
|
||||
// returns one stable synthetic GPU containing all exposed displays.
|
||||
static NvAPI_Status __cdecl NvAPI_EnumPhysicalGPUs_impl(
|
||||
NvPhysicalGpuHandle gpu_handles[NVAPI_MAX_PHYSICAL_GPUS],
|
||||
NvU32 *gpu_count) {
|
||||
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_EnumPhysicalGPUs(handles={}, count={})",
|
||||
fmt::ptr(gpu_handles),
|
||||
fmt::ptr(gpu_count));
|
||||
|
||||
if (!nvapi_initialized) {
|
||||
return NVAPI_API_NOT_INITIALIZED;
|
||||
}
|
||||
if (gpu_handles == nullptr || gpu_count == nullptr) {
|
||||
return NVAPI_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
gpu_handles[0] = get_gpu_handle();
|
||||
*gpu_count = 1;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_EnumPhysicalGPUs - gpu={}, count={}",
|
||||
fmt::ptr(gpu_handles[0]),
|
||||
*gpu_count);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// returns connected display descriptors for a physical GPU.
|
||||
// exposes the monitor snapshot as DP primary and HDMI secondary displays.
|
||||
static NvAPI_Status __cdecl NvAPI_GPU_GetConnectedDisplayIds_impl(
|
||||
NvPhysicalGpuHandle gpu_handle,
|
||||
NV_GPU_DISPLAYIDS *display_ids,
|
||||
NvU32 *display_id_count,
|
||||
NvU32 flags) {
|
||||
|
||||
const NvU32 input_count = display_id_count != nullptr ? *display_id_count : 0;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_GPU_GetConnectedDisplayIds(gpu={}, ids={}, count={}, flags={:#x})",
|
||||
fmt::ptr(gpu_handle),
|
||||
fmt::ptr(display_ids),
|
||||
input_count,
|
||||
flags);
|
||||
|
||||
if (!nvapi_initialized) {
|
||||
return NVAPI_API_NOT_INITIALIZED;
|
||||
}
|
||||
if (gpu_handle != get_gpu_handle()) {
|
||||
return NVAPI_EXPECTED_PHYSICAL_GPU_HANDLE;
|
||||
}
|
||||
if (display_id_count == nullptr) {
|
||||
return NVAPI_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
const NvU32 required_count = static_cast<NvU32>(displays.size());
|
||||
if (display_ids == nullptr) {
|
||||
*display_id_count = required_count;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_GPU_GetConnectedDisplayIds - required_count={}",
|
||||
required_count);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
const NvU32 capacity = *display_id_count;
|
||||
*display_id_count = required_count;
|
||||
if (capacity < required_count) {
|
||||
return NVAPI_INSUFFICIENT_BUFFER;
|
||||
}
|
||||
|
||||
for (NvU32 index = 0; index < required_count; index++) {
|
||||
const auto &source = displays[index];
|
||||
auto &destination = display_ids[index];
|
||||
destination = {};
|
||||
destination.version = NV_GPU_DISPLAYIDS_VER;
|
||||
destination.connectorType = source.primary ?
|
||||
NV_MONITOR_CONN_TYPE_DP : NV_MONITOR_CONN_TYPE_HDMI;
|
||||
destination.displayId = source.display_id;
|
||||
destination.isActive = 1;
|
||||
destination.isOSVisible = 1;
|
||||
destination.isConnected = 1;
|
||||
destination.isPhysicallyConnected = 1;
|
||||
}
|
||||
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_GPU_GetConnectedDisplayIds - returned_count={}",
|
||||
required_count);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// returns the NVAPI display ID associated with the Windows GDI primary.
|
||||
// returns the first synthetic display, ordered from the live desktop origin.
|
||||
static NvAPI_Status __cdecl NvAPI_DISP_GetGDIPrimaryDisplayId_impl(NvU32 *display_id) {
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_GetGDIPrimaryDisplayId(display_id={})",
|
||||
fmt::ptr(display_id));
|
||||
|
||||
if (!nvapi_initialized) {
|
||||
return NVAPI_API_NOT_INITIALIZED;
|
||||
}
|
||||
if (display_id == nullptr || displays.empty()) {
|
||||
return NVAPI_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
*display_id = displays.front().display_id;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_GetGDIPrimaryDisplayId - display_id={:#x}",
|
||||
*display_id);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
static void fill_source_mode(
|
||||
NV_DISPLAYCONFIG_SOURCE_MODE_INFO *destination,
|
||||
const SyntheticDisplay &source) {
|
||||
|
||||
if (destination == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
*destination = {};
|
||||
destination->resolution.width = source.width;
|
||||
destination->resolution.height = source.height;
|
||||
destination->resolution.colorDepth = source.color_depth;
|
||||
destination->colorFormat = NV_FORMAT_A8R8G8B8;
|
||||
destination->position.x = source.x;
|
||||
destination->position.y = source.y;
|
||||
destination->spanningOrientation = NV_DISPLAYCONFIG_SPAN_NONE;
|
||||
destination->bGDIPrimary = source.primary ? 1 : 0;
|
||||
}
|
||||
|
||||
static NvAPI_Status fill_target(
|
||||
NV_DISPLAYCONFIG_PATH_TARGET_INFO *destination,
|
||||
const SyntheticDisplay &source,
|
||||
NvU32 target_id) {
|
||||
|
||||
if (destination == nullptr) {
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
auto *details = destination->details;
|
||||
destination->displayId = source.display_id;
|
||||
destination->targetId = target_id;
|
||||
|
||||
if (details == nullptr) {
|
||||
return NVAPI_OK;
|
||||
}
|
||||
if (details->version != NV_DISPLAYCONFIG_PATH_ADVANCED_TARGET_INFO_VER) {
|
||||
return NVAPI_INCOMPATIBLE_STRUCT_VERSION;
|
||||
}
|
||||
|
||||
*details = {};
|
||||
details->version = NV_DISPLAYCONFIG_PATH_ADVANCED_TARGET_INFO_VER;
|
||||
details->rotation = source.rotation;
|
||||
details->scaling = NV_SCALING_DEFAULT;
|
||||
details->refreshRate1K = source.refresh_rate_1k;
|
||||
details->timingOverride = NV_TIMING_OVERRIDE_CURRENT;
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// retrieves the current global display topology through NVAPI's three-pass contract.
|
||||
// fills caller-owned buffers from the synthetic monitor snapshot and configured rates.
|
||||
static NvAPI_Status __cdecl NvAPI_DISP_GetDisplayConfig_impl(
|
||||
NvU32 *path_info_count,
|
||||
NV_DISPLAYCONFIG_PATH_INFO *path_info) {
|
||||
|
||||
const NvU32 input_count = path_info_count != nullptr ? *path_info_count : 0;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_GetDisplayConfig(count={}, paths={})",
|
||||
input_count,
|
||||
fmt::ptr(path_info));
|
||||
|
||||
if (!nvapi_initialized) {
|
||||
return NVAPI_API_NOT_INITIALIZED;
|
||||
}
|
||||
if (path_info_count == nullptr) {
|
||||
return NVAPI_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
const NvU32 required_count = static_cast<NvU32>(displays.size());
|
||||
if (path_info == nullptr) {
|
||||
*path_info_count = required_count;
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_GetDisplayConfig - required_count={}",
|
||||
required_count);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
const NvU32 capacity = *path_info_count;
|
||||
*path_info_count = required_count;
|
||||
if (capacity < required_count) {
|
||||
return NVAPI_INSUFFICIENT_BUFFER;
|
||||
}
|
||||
|
||||
for (NvU32 index = 0; index < required_count; index++) {
|
||||
auto &path = path_info[index];
|
||||
if (path.version != NV_DISPLAYCONFIG_PATH_INFO_VER2) {
|
||||
return NVAPI_INCOMPATIBLE_STRUCT_VERSION;
|
||||
}
|
||||
if (path.targetInfo != nullptr && path.targetInfoCount < 1) {
|
||||
return NVAPI_INSUFFICIENT_BUFFER;
|
||||
}
|
||||
|
||||
const auto &display = displays[index];
|
||||
path.sourceId = index;
|
||||
path.targetInfoCount = 1;
|
||||
path.IsNonNVIDIAAdapter = 0;
|
||||
path.pOSAdapterID = nullptr;
|
||||
fill_source_mode(path.sourceModeInfo, display);
|
||||
|
||||
const NvAPI_Status status = fill_target(path.targetInfo, display, index);
|
||||
if (status != NVAPI_OK) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_GetDisplayConfig - returned_count={}",
|
||||
required_count);
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
// applies a supplied global display topology through the NVIDIA driver.
|
||||
// accepts the cabinet topology without making any changes to Windows.
|
||||
static NvAPI_Status __cdecl NvAPI_DISP_SetDisplayConfig_impl(
|
||||
NvU32 path_info_count,
|
||||
NV_DISPLAYCONFIG_PATH_INFO *path_info,
|
||||
NvU32 flags) {
|
||||
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_DISP_SetDisplayConfig(count={}, paths={}, flags={:#x})",
|
||||
path_info_count,
|
||||
fmt::ptr(path_info),
|
||||
flags);
|
||||
|
||||
if (!nvapi_initialized) {
|
||||
return NVAPI_API_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
log_misc("nvapi_impl", "NvAPI_DISP_SetDisplayConfig - return synthetic success");
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static uintptr_t *query_result(T function) {
|
||||
return reinterpret_cast<uintptr_t *>(function);
|
||||
}
|
||||
|
||||
// resolves an NVAPI function ID to its implementation address.
|
||||
// exposes only the synthetic entry points used by KFC and rejects all others.
|
||||
static uintptr_t *__cdecl NvAPI_QueryInterface_impl(unsigned int function_id) {
|
||||
uintptr_t *result = nullptr;
|
||||
switch (function_id) {
|
||||
case NVAPI_INITIALIZE_ID:
|
||||
result = query_result(NvAPI_Initialize_impl);
|
||||
break;
|
||||
case NVAPI_INITIALIZE_EX_ID:
|
||||
result = query_result(NvAPI_InitializeEx_impl);
|
||||
break;
|
||||
case NVAPI_UNLOAD_ID:
|
||||
result = query_result(NvAPI_Unload_impl);
|
||||
break;
|
||||
case NVAPI_ENUM_PHYSICAL_GPUS_ID:
|
||||
result = query_result(NvAPI_EnumPhysicalGPUs_impl);
|
||||
break;
|
||||
case NVAPI_GPU_GET_CONNECTED_DISPLAY_IDS_ID:
|
||||
result = query_result(NvAPI_GPU_GetConnectedDisplayIds_impl);
|
||||
break;
|
||||
case NVAPI_DISP_GET_GDI_PRIMARY_DISPLAY_ID:
|
||||
result = query_result(NvAPI_DISP_GetGDIPrimaryDisplayId_impl);
|
||||
break;
|
||||
case NVAPI_DISP_GET_DISPLAY_CONFIG_ID:
|
||||
result = query_result(NvAPI_DISP_GetDisplayConfig_impl);
|
||||
break;
|
||||
case NVAPI_DISP_SET_DISPLAY_CONFIG_ID:
|
||||
result = query_result(NvAPI_DISP_SetDisplayConfig_impl);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
log_misc(
|
||||
"nvapi_impl",
|
||||
"NvAPI_QueryInterface(0x{:x}) - {}",
|
||||
function_id,
|
||||
result != nullptr ? "implemented" : "unsupported");
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool initialize(HINSTANCE dll, uint32_t main_refresh_hz, uint32_t sub_refresh_hz) {
|
||||
if (provider_initialized) {
|
||||
return true;
|
||||
}
|
||||
if (dll == nullptr) {
|
||||
log_warning("nvapi_impl", "invalid synthetic module handle");
|
||||
return false;
|
||||
}
|
||||
|
||||
displays = enumerate_displays(main_refresh_hz, sub_refresh_hz);
|
||||
libraryhook_hook_library(NVAPI_DLL_NAME_A, dll);
|
||||
libraryhook_hook_proc("nvapi_QueryInterface", NvAPI_QueryInterface_impl);
|
||||
libraryhook_enable();
|
||||
|
||||
provider_initialized = true;
|
||||
log_info(
|
||||
"nvapi_impl",
|
||||
"synthetic {} enabled with {} display(s), main={} Hz, sub={} Hz",
|
||||
NVAPI_DLL_NAME_A,
|
||||
displays.size(),
|
||||
main_refresh_hz,
|
||||
sub_refresh_hz);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef SPICE64
|
||||
|
||||
#include <cstdint>
|
||||
#include <windows.h>
|
||||
|
||||
namespace nvapi_impl {
|
||||
|
||||
bool initialize(HINSTANCE dll, uint32_t main_refresh_hz, uint32_t sub_refresh_hz);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
#pragma once
|
||||
|
||||
#ifdef SPICE64
|
||||
|
||||
#include <cstdint>
|
||||
#include <windows.h>
|
||||
|
||||
namespace nvapi_impl {
|
||||
|
||||
bool initialize(HINSTANCE dll, uint32_t main_refresh_hz, uint32_t sub_refresh_hz);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user