/* x64 ReShade API 20. Uses only public callbacks and documented Win32 APIs. With MSVC or MinGW, the Windows SDK provides the actual declarations. */ #include #include #include "xireun.h" #if defined(_WIN32) #define WIN32_LEAN_AND_MEAN #include typedef LARGE_INTEGER counter_t; #define counter_value(q) ((q).QuadPart) #define XC_EXPORT __declspec(dllexport) #else /* Cross-build and deterministic harness declarations for the same x64 ABI. */ typedef void *HANDLE; typedef struct { void *ptr; } SRWLOCK; typedef int64_t counter_t; #define counter_value(q) (q) #define XC_EXPORT extern void *GetProcAddress(void *,const char *); extern int QueryPerformanceCounter(counter_t *), QueryPerformanceFrequency(counter_t *); extern uint32_t GetCurrentThreadId(void); extern unsigned char TryAcquireSRWLockExclusive(SRWLOCK *); extern void AcquireSRWLockExclusive(SRWLOCK *),ReleaseSRWLockExclusive(SRWLOCK *); extern HANDLE CreateWaitableTimerExW(void *,const void *,uint32_t,uint32_t); extern int SetWaitableTimer(HANDLE,const counter_t *,int32_t,void *,void *,int); extern uint32_t WaitForSingleObject(HANDLE,uint32_t); extern int CloseHandle(HANDLE); #endif typedef bool (*register_fn)(void *,uint32_t); typedef void (*remove_fn)(void *); typedef void (*event_fn)(uint32_t,void *); typedef void (*log_fn)(void *,int,const char *); enum { ABI=20, DESTROY_CHAIN=8, BEFORE=74, AFTER=100 }; static remove_fn unregister_addon; static log_fn host_log; static void *self; static HANDLE timer; static SRWLOCK lock; static int64_t frequency; static struct { xr_gate gate; void *chain; uint32_t owner; int64_t begin, waited; bool pending; } context; static int64_t clock_us(void) { counter_t q; if (!QueryPerformanceCounter(&q)) return 0; int64_t v=counter_value(q); return (v/frequency)*1000000+(v%frequency)*1000000/frequency; } static void clear_context(void) { xr_reset(&context.gate); context.chain=NULL; context.owner=0; context.begin=0; context.waited=0; context.pending=false; } static void on_before(void *queue,void *chain,const void *source,const void *dest, uint32_t count,const void *dirty) { (void)queue;(void)source;(void)dest;(void)count;(void)dirty; if (!chain || !TryAcquireSRWLockExclusive(&lock)) return; uint32_t owner=GetCurrentThreadId(); if (context.chain!=chain || context.owner!=owner || context.pending) clear_context(); context.chain=chain; context.owner=owner; context.begin=clock_us(); context.pending=context.begin>0; ReleaseSRWLockExclusive(&lock); } static void on_after(void *queue,void *chain) { (void)queue; if (!TryAcquireSRWLockExclusive(&lock)) return; if (!context.pending || context.chain!=chain || context.owner!=GetCurrentThreadId()) { clear_context(); ReleaseSRWLockExclusive(&lock); return; } context.pending=false; int64_t finish=clock_us(); enum xr_mode old=context.gate.mode; int64_t delay=xr_after(&context.gate, context.begin, finish, context.waited); context.waited=0; if (delay>0 && timer) { counter_t due; counter_value(due)=-10*delay; int64_t start=clock_us(); bool ok=SetWaitableTimer(timer,&due,0,NULL,NULL,0) && WaitForSingleObject(timer,(uint32_t)(delay/1000+10))==0; context.waited=clock_us()-start; if (!ok) xr_cool(&context.gate); } enum xr_mode current=context.gate.mode; if (host_log && old!=current) { if (current==XR_ACTIVE) host_log(self,3,"Xireun: wait exchange accepted."); if (current==XR_COOLDOWN) host_log(self,3,"Xireun: timing changed or trial failed. Observing."); } ReleaseSRWLockExclusive(&lock); } static void on_destroy(void *chain,bool resize) { (void)resize; AcquireSRWLockExclusive(&lock); if (context.chain==chain) clear_context(); ReleaseSRWLockExclusive(&lock); } XC_EXPORT bool AddonInit(void *addon,void *reshade) { if (!addon || !reshade) return false; register_fn add=(register_fn)GetProcAddress(reshade,"ReShadeRegisterAddon"); remove_fn remove=(remove_fn)GetProcAddress(reshade,"ReShadeUnregisterAddon"); event_fn event=(event_fn)GetProcAddress(reshade,"ReShadeRegisterEvent"); counter_t q; if (!add || !remove || !event || !QueryPerformanceFrequency(&q) || counter_value(q)<=0 || counter_value(q)>10000000000LL) return false; HANDLE t=CreateWaitableTimerExW(NULL,NULL,2,0x00100002); if (!t) return false; if (!add(addon,ABI)) {CloseHandle(t);return false;} frequency=counter_value(q);timer=t;self=addon;unregister_addon=remove; host_log=(log_fn)GetProcAddress(reshade,"ReShadeLogMessage"); clear_context(); event(BEFORE,(void *)on_before);event(AFTER,(void *)on_after);event(DESTROY_CHAIN,(void *)on_destroy); if (host_log) host_log(self,3,"Xireun: observing Present calls. Calibration is automatic."); return true; } XC_EXPORT void AddonUninit(void *addon,void *reshade) { (void)reshade; if (unregister_addon) unregister_addon(addon); AcquireSRWLockExclusive(&lock); if (timer) CloseHandle(timer); timer=NULL;unregister_addon=NULL;host_log=NULL;self=NULL; clear_context(); ReleaseSRWLockExclusive(&lock); } XC_EXPORT const char *NAME="Xireun III"; XC_EXPORT const char *DESCRIPTION="Tests whether a small post-Present wait replaces existing waiting.";