Datasets:
File size: 5,415 Bytes
987b273 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | /* x64 ReShade API 20. Uses only public callbacks and documented Win32 APIs.
With MSVC or MinGW, the Windows SDK provides the actual declarations. */
#include <stdint.h>
#include <stdbool.h>
#include "xireun.h"
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
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.";
|