Skip to content

Bring PS5 GTA6 to 120FPS with DLSS 5. An advanced Windows video player and capture enhancement tool featuring real-time preview for videos, images, and capture card sources. Combine AI-powered Super Resolution, DLSS5 image enhancement, and Frame Generation to deliver a next-generation visual experience.

C++

feat(base): add verified x64 thunk detour primitive for the XeSS pacing port

Veyra Goal Agent50e4c9c0b153
diff --git a/CMakeLists.txt b/CMakeLists.txt
index df55bb88..7d4788dd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -60,6 +60,7 @@ add_library(veyra_base STATIC
src/base/NgxResult.cpp
src/base/FileIdentity.cpp
src/base/RuntimePaths.cpp
+ src/base/ThunkHook.cpp
)
veyra_apply_warnings(veyra_base)
target_compile_options(veyra_base PUBLIC /utf-8)
diff --git a/docs/FRAMEGEN_FSR_DOLBY_STATUS_2026-09-16.md b/docs/FRAMEGEN_FSR_DOLBY_STATUS_2026-09-16.md
index ff029718..119ce8c9 100644
--- a/docs/FRAMEGEN_FSR_DOLBY_STATUS_2026-09-16.md
+++ b/docs/FRAMEGEN_FSR_DOLBY_STATUS_2026-09-16.md
@@ -52,6 +52,29 @@ $env:VEYRA_TEST_FG_FORCE_MULTIPLIER='1'
| FSR 超分(F) | **未开始** | AMD 卡 4.1 / N 卡 2、3.1 的分档 UI 与后端接入 |
| 杜比直通 / 解码(G-2) | **未开始** | 依赖支持位流的采集设备;当前设备已证实不提供 |
+### A-2 节奏 hook:已完成的准备与勘察结论
+
+本轮已落地 detour 基础设施并用单测验证:
+
+- 新增 `include/veyra/ThunkHook.h` + `src/base/ThunkHook.cpp`:面向"五字节 `E9 rel32` + 0xCC 填充"的跳转 thunk 做函数级 hook。它会校验 thunk 形状(非 thunk 直接拒绝,不猜)、在目标 ±2GB 内分配一页放两个绝对跳转桩(trampoline 指回原目标、entry 指向替换函数)、打补丁后回读校验、移除时按原字节恢复。替换函数可在任意地址(不受 rel32 限制)。
+- 单测(`veyra_repair_contract_tests`,现 154 项 0 失败)覆盖:thunk 到达原函数、安装 hook、替换函数执行并经 trampoline 转发、移除后恢复、非 thunk 目标被拒绝。
+
+上游 `XeFGPacing.h`(OptiScaler 固定提交 `70676c5f`)的施工坐标已勘察完毕,供下一轮直接移植:
+
+| 项 | 值 |
+| --- | --- |
+| Present thunk | RVA `0x25C0`(`jmp 0x21F730`,后接 11 字节 0xCC 填充,正好可被 `ThunkHook` 接管) |
+| 原生 present 目标 | RVA `0x21F730` |
+| 调度 thunk | RVA `0x3100`(→ `0x21EE30`) |
+| 每帧间隔计算点 | RVA `0x220254`(`count=[r8+8]`、`div rcx`、结果存 `r12`) |
+| burst 循环 / 最后一帧调用 | RVA `0x2202E8`(循环内);`0x220462`(最后一帧,arg7=0,入口 `0x220467`) |
+| 限流块触发条件 | RVA `0x220317`:`[rsi+0x340]!=0`、`[r8+8]>2`、`[r8+0x28]==0` |
+| 上下文偏移 | `LimiterEnabled=0x340`、`SchedEnable=0x341`、`BurstGate=0xC0`、`Ring=0x168`、`BurstLimiterField=0x28` |
+| ring 快照函数 | RVA `0x224CF0`(经 `0x4DA0` 调用;调用点 `0x22023D..0x22024B`) |
+| 上游实现要点 | 需要重建 present 调用的参数(上游用 1500 字节 helper `0x3F570`);全局时序变量需按上下文管理;hook 回调不得抛异常或做大分配/同步日志 |
+
+结论:基础设施已就绪,剩余工作是 hook 体本身(参数重建 + 逐帧节奏),属高风险改动,需要一轮可快速迭代的实机调试;在完成并验证前,4X 的生成帧间距仍标为"未验证"。
+
## 三、明早验收清单(建议顺序)
1. `git log --oneline -4` 确认三个提交都在 `codex/framegen-fsr-dolby-20260916`,`main` 未动。
diff --git a/include/veyra/ThunkHook.h b/include/veyra/ThunkHook.h
new file mode 100644
index 00000000..a88f94bd
--- /dev/null
+++ b/include/veyra/ThunkHook.h
@@ -0,0 +1,57 @@
+#pragma once
+
+#include <windows.h>
+
+#include <cstdint>
+#include <mutex>
+#include <string>
+
+namespace veyra {
+
+// Minimal x64 detour for jump thunks.
+//
+// Several provider DLLs expose their internal entry points as five-byte
+// relative jump thunks (`E9 rel32`) followed by padding. Redirecting such a
+// thunk is the safest way to hook provider internals: the whole hookable region
+// is one instruction, so no instruction boundary can be split and no
+// disassembler is required. Anything that is not the canonical thunk shape is
+// rejected instead of guessed at.
+//
+// The hook keeps a trampoline that continues to the original jump target, so
+// the replacement can forward calls. Every patch is verified by read-back and
+// restored byte-exactly on removal.
+class ThunkHook {
+public:
+ struct Status {
+ bool installed = false;
+ std::string error;
+ void* originalTarget = nullptr;
+ void* trampoline = nullptr;
+ };
+
+ ThunkHook() = default;
+ ~ThunkHook();
+ ThunkHook(const ThunkHook&) = delete;
+ ThunkHook& operator=(const ThunkHook&) = delete;
+
+ // Requires target[0] == 0xE9 and `paddingBytes` 0xCC bytes after the jump.
+ // The replacement may live anywhere: the hook builds a nearby page holding
+ // an absolute-jump stub to the replacement plus the trampoline that
+ // continues to the original target.
+ Status install(void* target, void* replacement, size_t paddingBytes = 11);
+ bool remove();
+ bool installed() const { return installed_; }
+ void* trampoline() const { return trampoline_; }
+
+private:
+ void releaseTrampoline();
+
+ std::mutex mutex_;
+ void* target_ = nullptr;
+ void* trampoline_ = nullptr;
+ uint8_t original_[8]{};
+ size_t patchBytes_ = 0;
+ bool installed_ = false;
+};
+
+} // namespace veyra
diff --git a/src/base/ThunkHook.cpp b/src/base/ThunkHook.cpp
new file mode 100644
index 00000000..f8e71696
--- /dev/null
+++ b/src/base/ThunkHook.cpp
@@ -0,0 +1,152 @@
+#include "veyra/ThunkHook.h"
+
+#include <format>
+
+namespace veyra {
+
+namespace {
+
+constexpr size_t kJumpBytes = 5;
+// jmp qword ptr [rip+0] ; <abs64> -> continues to the original target.
+constexpr size_t kTrampolineBytes = 14;
+
+bool writeVerified(void* address, const uint8_t* bytes, size_t size) {
+ DWORD previous = 0;
+ if (!VirtualProtect(address, size, PAGE_EXECUTE_READWRITE, &previous)) return false;
+ memcpy(address, bytes, size);
+ FlushInstructionCache(GetCurrentProcess(), address, size);
+ VirtualProtect(address, size, previous, &previous);
+ return memcmp(address, bytes, size) == 0;
+}
+
+// Allocates an executable page within +/-2 GB of `near` so the five-byte
+// relative jump can reach it. Scans outwards in 64 KB steps; returns nullptr
+// when no address is free (the caller then refuses to install).
+void* allocateNear(void* anchor, size_t size) {
+ SYSTEM_INFO info{};
+ GetSystemInfo(&info);
+ const uintptr_t granularity = info.dwAllocationGranularity ? info.dwAllocationGranularity : 0x10000;
+ const uintptr_t base = reinterpret_cast<uintptr_t>(anchor);
+ const uintptr_t minAddress = base > 0x80000000ull ? base - 0x70000000ull : granularity;
+ for (uintptr_t step = granularity; step < 0x70000000ull; step += granularity) {
+ for (int direction = 0; direction < 2; ++direction) {
+ const uintptr_t candidate = direction == 0 ? base + step : base - step;
+ if (candidate < minAddress || candidate > base + 0x70000000ull) continue;
+ void* memory = VirtualAlloc(reinterpret_cast<void*>(candidate), size,
+ MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
+ if (memory != nullptr) return memory;
+ if (direction == 1 && candidate <= granularity) break;
+ }
+ }
+ return nullptr;
+}
+
+} // namespace
+
+ThunkHook::~ThunkHook() {
+ remove();
+}
+
+ThunkHook::Status ThunkHook::install(void* target, void* replacement, size_t paddingBytes) {
+ std::lock_guard lock(mutex_);
+ Status status{};
+ if (target == nullptr || replacement == nullptr) {
+ status.error = "null target or replacement";
+ return status;
+ }
+ if (installed_) {
+ status.installed = true;
+ status.originalTarget = trampoline_ != nullptr ? reinterpret_cast<void*>(*reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(trampoline_) + 6)) : nullptr;
+ status.trampoline = trampoline_;
+ return status;
+ }
+
+ auto* bytes = reinterpret_cast<uint8_t*>(target);
+ if (bytes[0] != 0xE9) {
+ status.error = std::format("target {:#x} is not a relative jump thunk (opcode {:#x})",
+ reinterpret_cast<uintptr_t>(target), unsigned(bytes[0]));
+ return status;
+ }
+ for (size_t i = 0; i < paddingBytes; ++i) {
+ if (bytes[kJumpBytes + i] != 0xCC) {
+ status.error = std::format("target {:#x} padding byte {} is {:#x}, expected 0xCC",
+ reinterpret_cast<uintptr_t>(target), i, unsigned(bytes[kJumpBytes + i]));
+ return status;
+ }
+ }
+
+ const int32_t relative = *reinterpret_cast<int32_t*>(bytes + 1);
+ auto* originalTarget = bytes + kJumpBytes + relative;
+ status.originalTarget = originalTarget;
+
+ // One nearby page holds two absolute-jump stubs: one continues to the
+ // original target (the trampoline the replacement calls) and one enters the
+ // replacement. The replacement itself may live anywhere; only this page has
+ // to be inside the five-byte relative jump range.
+ auto* page = reinterpret_cast<uint8_t*>(allocateNear(target, 0x1000));
+ if (page == nullptr) {
+ status.error = std::format("no executable page within 2 GB of {:#x}", reinterpret_cast<uintptr_t>(target));
+ return status;
+ }
+ auto writeAbsoluteJump = [](uint8_t* at, void* destination) {
+ at[0] = 0xFF;
+ at[1] = 0x25;
+ at[2] = 0x00;
+ at[3] = 0x00;
+ at[4] = 0x00;
+ at[5] = 0x00;
+ *reinterpret_cast<uint64_t*>(at + 6) = reinterpret_cast<uint64_t>(destination);
+ };
+ auto* trampoline = page;
+ auto* entry = page + 64;
+ writeAbsoluteJump(trampoline, originalTarget);
+ writeAbsoluteJump(entry, replacement);
+ FlushInstructionCache(GetCurrentProcess(), page, 0x1000);
+
+ const int64_t distance = reinterpret_cast<uint8_t*>(entry) - (bytes + kJumpBytes);
+ if (distance > 0x7FFFFFFFll || distance < -0x80000000ll) {
+ VirtualFree(page, 0, MEM_RELEASE);
+ status.error = std::format("stub page {:#x} is out of relative jump range from {:#x}",
+ reinterpret_cast<uintptr_t>(entry), reinterpret_cast<uintptr_t>(target));
+ return status;
+ }
+
+ uint8_t patch[kJumpBytes]{};
+ patch[0] = 0xE9;
+ *reinterpret_cast<int32_t*>(patch + 1) = static_cast<int32_t>(distance);
+ memcpy(original_, bytes, kJumpBytes);
+ if (!writeVerified(target, patch, kJumpBytes)) {
+ VirtualFree(page, 0, MEM_RELEASE);
+ status.error = std::format("patch verification failed at {:#x}", reinterpret_cast<uintptr_t>(target));
+ return status;
+ }
+
+ target_ = target;
+ trampoline_ = trampoline;
+ patchBytes_ = kJumpBytes;
+ installed_ = true;
+ status.installed = true;
+ status.trampoline = trampoline;
+ return status;
+}
+
+bool ThunkHook::remove() {
+ std::lock_guard lock(mutex_);
+ bool restored = true;
+ if (installed_ && target_ != nullptr) {
+ restored = writeVerified(target_, original_, patchBytes_);
+ }
+ installed_ = false;
+ target_ = nullptr;
+ releaseTrampoline();
+ return restored;
+}
+
+void ThunkHook::releaseTrampoline() {
+ if (trampoline_ != nullptr) {
+ VirtualFree(trampoline_, 0, MEM_RELEASE);
+ trampoline_ = nullptr;
+ }
+}
+
+} // namespace veyra
diff --git a/tests/unit/RepairContractTests.cpp b/tests/unit/RepairContractTests.cpp
index 718b14d0..838db9d3 100644
--- a/tests/unit/RepairContractTests.cpp
+++ b/tests/unit/RepairContractTests.cpp
@@ -7,9 +7,17 @@
#include "veyra/engine/LiveFgAdmission.h"
#include "veyra/engine/FrameFlowWindow.h"
#include "veyra/engine/EnhancementDelayEstimate.h"
+#include "veyra/ThunkHook.h"
#include <iostream>
#include <filesystem>
#include <fstream>
+
+namespace {
+int thunkHookCalls = 0;
+int (*thunkOriginal)(int) = nullptr;
+int thunkTargetFunction(int value) { return value + 1; }
+int thunkReplacementFunction(int value) { ++thunkHookCalls; return thunkOriginal ? thunkOriginal(value) + 100 : -1; }
+}
#include <windows.h>
#include "veyra/engine/ContentCadence.h"
#include "veyra/diagnostics/Redaction.h"
@@ -358,5 +366,41 @@ int main(){
check(report.find("event=Ready host=12345 session=7 revision=9 epoch=3 source=55 batch=6 fence=8")!=std::string::npos,
"existing diagnostic preview exports frame identity and actual completion events");
check(report.find("NVOF=0x5")!=std::string::npos&&report.find("NVOF=0x7")!=std::string::npos&&report.find("NGX=0xBAD00005")!=std::string::npos&&report.find("SEH=0xC0000005")!=std::string::npos,"diagnostic report retains NVOF aliases and NGX/SEH codes (synthetic errors)");
+ {
+ // ThunkHook is the detour primitive the XeSS pacing port needs: build a
+ // canonical provider-style thunk, hook it, forward through the
+ // trampoline, then verify byte-exact restoration.
+ auto* page=static_cast<uint8_t*>(VirtualAlloc(nullptr,0x1000,MEM_RESERVE|MEM_COMMIT,PAGE_EXECUTE_READWRITE));
+ check(page!=nullptr,"thunk test page allocated");
+ if(page){
+ // Layout: [thunk E9 rel32 -> target stub][target stub: jmp [rip+0]; abs64]
+ // The stub keeps the whole test inside one page, so the relative jump
+ // is always in range regardless of where VirtualAlloc lands.
+ auto* thunk=page;
+ auto* targetStub=page+64;
+ targetStub[0]=0xFF;targetStub[1]=0x25;targetStub[2]=0x00;targetStub[3]=0x00;targetStub[4]=0x00;targetStub[5]=0x00;
+ *reinterpret_cast<uint64_t*>(targetStub+6)=reinterpret_cast<uint64_t>(&thunkTargetFunction);
+ const int64_t relative=targetStub-(thunk+5);
+ check(relative>=-0x7FFFFFFFll&&relative<=0x7FFFFFFFll,"test thunk target is in relative range");
+ thunk[0]=0xE9;
+ *reinterpret_cast<int32_t*>(thunk+1)=static_cast<int32_t>(relative);
+ for(int i=0;i<11;++i)thunk[5+i]=0xCC;
+ FlushInstructionCache(GetCurrentProcess(),page,0x1000);
+ auto callThunk=reinterpret_cast<int(*)(int)>(thunk);
+ thunkHookCalls=0;
+ check(callThunk(5)==6,"thunk reaches the original function");
+ veyra::ThunkHook hook;
+ const auto status=hook.install(thunk,reinterpret_cast<void*>(&thunkReplacementFunction));
+ check(status.installed,"thunk hook installs on a canonical thunk");
+ thunkOriginal=reinterpret_cast<int(*)(int)>(status.trampoline);
+ check(callThunk(5)==106&&thunkHookCalls==1,"replacement runs and forwards through the trampoline");
+ check(hook.remove(),"thunk hook restores the original bytes");
+ check(callThunk(5)==6&&thunkHookCalls==1,"original thunk works after removal");
+ veyra::ThunkHook reject;
+ const auto rejected=reject.install(page+512,reinterpret_cast<void*>(&thunkReplacementFunction));
+ check(!rejected.installed&&!rejected.error.empty(),"non-thunk target is rejected instead of guessed");
+ VirtualFree(page,0,MEM_RELEASE);
+ }
+ }
std::cout<<checks<<" checks "<<failures<<" failures\n";return failures?1:0;
}