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++

fix: retain system NVAPI across NR teardown for issue 67

Veyra Goal Agent619921298136
diff --git a/AGENTS.md b/AGENTS.md
index fe241dd3..ae725ac4 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -1,5 +1,7 @@
# Veyra 项目 Agent 执行规则
+> **2026-10-21 #67 修复授权(Codex)**:用户暂停Lecram,要求修复issue67。按docs/ISSUE67_NVAPI_SHUTDOWN_PLAN_2026-10-21.md在codex/issue67-nvapi-shutdown-20261021隔离修复NR/NVAPI模块生命周期与关闭诊断,允许必要ngx/graph/CMake/测试接点,覆盖这些旧冻结;保留正常资源释放/UI频率和其他任务。产物全E盘,构建-j2≤900秒/单测≤300秒;不合main/push/发布/评论/关机,不派Agent,无3090实卡不称已验。
+
> **2026-10-19 2.1.0 正式发布(Codex)**:用户已实测并批准 `test-packages/bilibili-2.1.0-20261019` 两包原样发布,禁止重新构建/打包。按 docs/RELEASE_2.1.0_PLAN_2026-10-19.md 整理2.0.9以来全部合入内容、替换用户新6群二维码、更新README/Release,主Release只放两个GPU包,源码另页;允许main普通快进/push/新标签及发布。Lecram两个实验分支暂停保留,不入本版。公告只拟稿,不代发、不关机、不派Agent、不改运行文件/驱动/真实配置;产物全E盘。
> **2026-10-19 2.1.0 整合与测试包(Claude)**:用户要求“之前的所有修复,包括 Codex 那边的都一起合并到 Main,然后打包 2.1.0 测试包”,并审查 Codex 的修复。在 `claude/release-2.1.0-20261019`(main 1dc8c18)合并 fullscreen-input(含 AMD NR 0.44)、#48、美颜(含 E0/AA)与赞助页,版本 2.1.0;先存档 `checkpoint/pre-release-2.1.0-20261019` 与 `archives/claude-release-2.1.0-20261019`。允许本地 main 快进、以 2.0.9-test 两包为底打 NVIDIA/AMD 2.1.0-test 包(美颜两模型按名称+哈希放行、AMD 换 0.44 运行库)。记录见 docs/BUILD_2.1.0_TEST.md 与 docs/RELEASE_2.1.0_TEST_2026-10-19.md。不改运行库/模型字节、驱动或用户配置;无 push/Release/上传/子 Agent。
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d49711d9..a93b3043 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -151,6 +151,7 @@ add_library(veyra_ngx STATIC
src/ngx/NgxCoreHost.cpp
src/ngx/NgxCoreCache.cpp
src/ngx/DlssNrRuntimeAdapter.cpp
+ src/ngx/NvapiLifetime.cpp
src/ngx/DlssSrBackend.cpp
src/ngx/VideoSrBackend.cpp
src/ngx/TrueHdrBackend.cpp
@@ -230,6 +231,10 @@ if(VEYRA_ENABLE_EXPERIMENTAL_DLSSNR)
target_compile_features(veyra_nr_ampere_tests PRIVATE cxx_std_20)
target_compile_definitions(veyra_nr_ampere_tests PRIVATE UNICODE _UNICODE WIN32_LEAN_AND_MEAN NOMINMAX)
target_link_libraries(veyra_nr_ampere_tests PRIVATE veyra_gfx veyra_ngx)
+ add_executable(veyra_nvapi_lifetime_tests tests/integration/NvapiLifetimeTests.cpp)
+ target_compile_features(veyra_nvapi_lifetime_tests PRIVATE cxx_std_20)
+ target_compile_definitions(veyra_nvapi_lifetime_tests PRIVATE UNICODE _UNICODE WIN32_LEAN_AND_MEAN NOMINMAX)
+ target_link_libraries(veyra_nvapi_lifetime_tests PRIVATE veyra_ngx)
add_executable(veyra_nr_harness
tools/nr_harness/main.cpp
tools/nr_harness/frame_loop.cpp
diff --git a/docs/ISSUE67_NVAPI_SHUTDOWN_PLAN_2026-10-21.md b/docs/ISSUE67_NVAPI_SHUTDOWN_PLAN_2026-10-21.md
new file mode 100644
index 00000000..cb5ec524
--- /dev/null
+++ b/docs/ISSUE67_NVAPI_SHUTDOWN_PLAN_2026-10-21.md
@@ -0,0 +1,43 @@
+# #67 NR 退出卡住修复
+
+用户停止 Lecram,改为修复 Likely7/Veyra-NRVideo#67。从 nrvideo/main 4ae285b 在 codex/issue67-nvapi-shutdown-20261021 隔离实施,存档 checkpoint/pre-issue67-nvapi-shutdown-20261021;Lecram、全屏及其他任务保持。
+
+## 证据与边界
+
+报告为 RTX3090、616.92、SF310.9.16,关闭后等待20秒被 watchdog终止。日志最后为 snippet module freed,但同份报告写明 architecture rewrite not installed。该版本策略早于 NVAPI 加载即返回,archLookupSlot_为空,因此 hadAmpere=false,不执行 ClearAmpereContext。作者认定的阻塞调用和跨进程遗留状态都没有堆栈证明,不能作为已确认根因。
+
+旧兼容路径确实存在逐图 FreeLibrary(nvapi64.dll);SF 的 NR/NGX 也可能持有该驱动模块。修复加载的生命周期隐患,并将原先未区分的 adapter结束、core保留检查、Shutdown1外调分开记录。仍复现时需对应阶段的线程堆栈,不能仅根据最后日志猜测。
+
+## 实施
+
+1. NR DLL 加载前,通过绝对 System32 路径一次性加载并 PIN 系统 NVAPI。并发首次调用只执行一次;后续返回借用句柄。不在析构中卸载,也不调用全局 NvAPI_Unload,不改驱动/文件/模型。加载或固定失败明确拒绝 NR,不伪报成功。
+2. 旧 Ampere 上下文只借用该句柄,失败回滚/正常卸载清理本图指针及IAT,仍正常释放 NR features、参数、NR模块、NGX核心和GPU纹理。SF版本仍不安装架构改写。不会按图累积模块引用/显存。
+3. 增加关闭阶段成对日志,保留20秒保护,不改变UI刷新、补帧/颜色/采集/调度。
+4. 实际系统 NVAPI 并发/消费者释放/PIN回归,旧NR兼容安装/恢复循环,SF/原版实际视频与运行库切换、关闭后重新启动。单测≤300秒,构建≤900秒、并发2;本机5070不能代表3090验收。
+
+Windows PIN契约:https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandleexw (固定模块保留到进程结束)。只保留驱动模块映射,不保留NR feature/视频资源,也不保证第三方驱动内部Shutdown不阻塞。
+
+所有构建、测试、日志、临时文件和存档:E:/项目/Veyra/{build,tests,logs,tmp,reports,archives}/issue67-nvapi-shutdown-20261021。仅本地修复/验证,不合main/push/发布/评论/关机,不派Agent。结果补充 WORKLOG。
+
+## 实际验收
+
+2026-10-21,本机 RTX5070 / 616.56,独立构建7目标通过(241.2秒、Ninja并发2)。命令:`py -3.11 -X utf8 -B E:/项目/Veyra/tmp/issue67-nvapi-shutdown-20261021/build.py`;其调用本树 `scripts/acceptance/claude-beauty-build.py --task issue67-nvapi-shutdown-20261021 --label 2.1.0-issue67 --template-cache E:/项目/Veyra/build/bilibili-2.1.0-20261019/CMakeCache.txt --parallel 2`,逐项传入下表目标及 veyra_qml_ui。
+
+私有运行目录 `E:/项目/Veyra/tests/issue67-nvapi-shutdown-20261021/app`,只借用已发布包的运行组件,配置独立复制,不复制真实用户偏好。单次测试命令及回执在 `logs/issue67-nvapi-shutdown-20261021/checks-20261021-223100/receipt.json`(所有路径相对 E:/项目/Veyra)。执行脚本为同任务 tmp/checks.py;全部单测≤14秒。
+
+| 测试目标 | 结果 |
+| --- | --- |
+| veyra_nvapi_lifetime_tests | 8线程首次竞态、512次借用、64次配对加载释放、额外释放后PIN仍有效,系统路径正确,退出0 |
+| veyra_nr_ampere_tests | 实际旧310.8.0模块,兼容IAT安装/恢复3次、重复unload、系统函数不变,退出0;未在30系执行NR |
+| veyra_nr_runtime_assertion_tests | 版本/架构策略断言通过,SF310.9.16仍走真实架构 |
+| veyra_nr_model_settings_tests | 模型开关/预设/会话与AMD设置回归通过,failures=0 |
+| veyra_nr_core_cache_contract_tests | 真实NGX参数容量失败/借用拒绝/重建/最终关闭通过,参数归零,deviceRemoved=0;Shutdown1成功、48.400ms |
+| veyra_nr_runtime_switch_tests ×2独立进程 | GTA6 4K输入,SF性能/旧模型/原版切换均推理并保存实际输出;NR直接开启状态下停止233.588ms、201.799ms;同进程重新打开视频并再次NR停止成功,进程正常退出0 |
+
+两个真实视频进程各只加载/固定NVAPI一次,分别5次NR卸载完成、4对NGX Shutdown1完整返回(35.943–43.705ms),各4次SF绕过架构兼容;无engine-hang。8张实际输出均3840×2160且非黑帧(只验证输出有效,不宣称画质提升)。供体及私有两种NR DLL SHA不变,未改任何运行库字节。
+
+结果索引:`E:/项目/Veyra/reports/issue67-nvapi-shutdown-20261021/final.json`;构建日志 `logs/issue67-nvapi-shutdown-20261021/build-20261021-222653/`。源改动仅NR驱动模块生命周期与关闭日志,不增加逐帧处理/等待,不改变UI频率、GPU优先级或20秒watchdog。PIN保留的是固定一份NVAPI模块映射;NR模型、特征、参数、纹理和NGX核心仍释放,不是保留显存资源规避问题。
+
+验收过程中一次汇总脚本因混合编码日志按严格UTF8读取失败(UnicodeDecodeError);改为保留原文件、容错读取ASCII阶段字段后汇总通过,没有重跑或修改原始结果。第一次停止视频时出现FFmpeg packet corrupt/Invalid NAL size文字,第二轮未出现;两轮重开均正常,未触发引擎失败。另有既存swscaler d3d12输入警告,本轮不改解码路径,也不宣称修复这些文字。
+
+**限制**:没有RTX3090或616.92,未在本机复现原20秒阻塞,也没有该用户dump。本改动是针对可确认的卸载风险的修复与SF路径防护,不能据此宣称已经证实原用户根因或彻底修好3090。若原机仍复现,用新增成对日志区分NR卸载、core设备健康检查、Shutdown1,并取对应线程dump,避免继续猜驱动内部行为。未发布测试包、未合main、未push、未关闭issue。
diff --git a/docs/WORKLOG.md b/docs/WORKLOG.md
index a2f87b55..910d7b5a 100644
--- a/docs/WORKLOG.md
+++ b/docs/WORKLOG.md
@@ -9846,3 +9846,14 @@ Branch claude/aa-53-20261017 from main 26c0d61. Archive E:/项目/Veyra/archives
用户提供“群聊:Veyra Dlss5 交流 7.png”并要求替换 GitHub 已满群二维码。从最新 nrvideo/main 7ec143c 在 codex/community-qr7-20261020 修改;原图原样复制 docs/images/community/group-7-20261020.png,SHA256 1cf67beb48815bd53999bf17252e74de4bb617c6c47846252ff89a0e417ade84。中英 README、RELEASE_SUPPORT 和 2.1.0 更新说明同步新图、7群标记及图片注明的 2026-10-27 前有效期。微信赞助图、width220、Discord、Ko-fi、知识库和其他内容保留。
工具命令:git fetch nrvideo main;bundled Python -X utf8 -B E:/项目/Veyra/tmp/community-qr7-20261020/update.py;git diff --check。Release v2.1.0 / v2.1.0-sources 修改前完整 JSON 与精确替换后的正文保存在 E:/项目/Veyra/reports/community-qr7-20261020;提交后普通 push HEAD:main,再 gh release edit --notes-file 更新两页正文。只改群信息,不重建/打包、不移动发布标签、不修改资产;远端正文、图片实际字节、赞助链接及资产身份的核验结果记该目录 final.json。
+
+
+## 2026-10-21 #67 NR退出卡住:生命周期修复与定向回归(Codex)
+
+用户暂停Lecram,要求修issue67。从nrvideo/main 4ae285b新建codex/issue67-nvapi-shutdown-20261021;checkpoint/pre-issue67-nvapi-shutdown-20261021与E:/项目/Veyra/archives/issue67-nvapi-shutdown-20261021存档,其他任务未改。计划/证据/边界见docs/ISSUE67_NVAPI_SHUTDOWN_PLAN_2026-10-21.md。
+
+关键纠正:issue同时写SF310.9.16未装架构兼容,因此按当前源码hadAmpere=false,不能从最后snippet freed一行推出ClearAmpereContext的FreeLibrary阻塞。旧兼容路径确有逐图卸载NVAPI风险;新增NvapiLifetime.h/.cpp,绝对System32加载、一次性PIN、只借用进程句柄,SF也在加载NR前覆盖。Adapter不再卸载NVAPI,仍释放NR模块;NgxCoreHost与EnhanceGraph只补关闭阶段/耗时日志,不改逐帧工作、UI频率、GPU优先级或watchdog。
+
+实际命令:py -3.11 -X utf8 -B E:/项目/Veyra/tmp/issue67-nvapi-shutdown-20261021/build.py(调用本树参数化claude-beauty-build.py,取bilibili-2.1.0缓存、Ninja并发2);7目标241.2秒通过,日志logs/issue67-nvapi-shutdown-20261021/build-20261021-222653。py -3.11 -X utf8 -B E:/项目/Veyra/tmp/issue67-nvapi-shutdown-20261021/checks.py:NVAPI并发/PIN、架构策略、模型预设、旧兼容IAT三次恢复、真实NGX core生命周期、两次独立进程SF/原版视频切换全部通过,单测<14秒。真实GTA6 4K、NR开启状态直接stop为233.588/201.799ms,两轮都能同进程重开并正常再次停止,退出0。各进程NVAPI只固定一次、5次NR卸载、4次Shutdown1全部返回,8张输出3840×2160非黑;无engine-hang,两NR原件SHA未变。完整命令/输出/索引reports/issue67-nvapi-shutdown-20261021/final.json;所有产物全E盘。
+
+汇总脚本一次纯UTF8读混合编码日志失败,容错读ASCII字段后通过,原日志未改;一次停止时FFmpeg输出截断packet/NAL警告,另有既存d3d12 swscaler警告,两轮重开均正常,本轮不宣称修复这些解码文字。仅RTX5070/616.56通过,没有3090/616.92或用户dump,不称原用户根因已证实或硬件问题已彻底修好。只保留驱动模块映射,不保留NR feature/模型/GPU资源。不合main/push/发布/评论/关机,不派Agent。
diff --git a/include/veyra/ngx/NvapiLifetime.h b/include/veyra/ngx/NvapiLifetime.h
new file mode 100644
index 00000000..5090227a
--- /dev/null
+++ b/include/veyra/ngx/NvapiLifetime.h
@@ -0,0 +1,8 @@
+#pragma once
+#include <windows.h>
+
+namespace veyra::ngx {
+// Borrowed, process-pinned system NVAPI module. Never FreeLibrary this handle.
+// No NvAPI_Initialize/Unload or architecture override is performed here.
+HMODULE processNvapi();
+}
diff --git a/src/ngx/DlssNrRuntimeAdapter.cpp b/src/ngx/DlssNrRuntimeAdapter.cpp
index 35ae487a..e4509816 100644
--- a/src/ngx/DlssNrRuntimeAdapter.cpp
+++ b/src/ngx/DlssNrRuntimeAdapter.cpp
@@ -9,6 +9,7 @@
#include "veyra/Log.h"
#include "veyra/NgxResult.h"
#include "veyra/ngx/NrArchitecturePolicy.h"
+#include "veyra/ngx/NvapiLifetime.h"
namespace veyra::ngx {
@@ -197,7 +198,7 @@ bool ExchangeImport(void** slot, void* replacement, void*& previous)
void ClearAmpereContext()
{
- if(g_ampere.nvapi)FreeLibrary(g_ampere.nvapi);
+ // Borrowed from processNvapi(); only the compatibility state is per graph.
g_ampere.nvapi=nullptr;g_ampere.query=nullptr;g_ampere.getArch=nullptr;
g_ampere.getProc=nullptr;g_ampere.count=0;
}
@@ -423,6 +424,12 @@ bool DlssNrRuntimeAdapter::load(const std::wstring& runtimeDir, Status& status)
return false;
}
+ // Also covers SF builds that bypass installAmpereCompatibility. A retained
+ // module prevents a runtime's own FreeLibrary from causing driver detach.
+ if (!processNvapi()) {
+ status = Status::LoadLibraryFailure;
+ return false;
+ }
module_ = LoadLibraryExW(dllPath_.c_str(), nullptr,
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
if (module_ == nullptr) {
@@ -568,10 +575,7 @@ bool DlssNrRuntimeAdapter::installAmpereCompatibility(ID3D12Device* device, Stat
return true;
}
auto fail=[&](const char* reason){veyra::log::error("nr-ampere",reason);ClearAmpereContext();status=Status::DeviceFailure;return false;};
- wchar_t system[MAX_PATH]{};
- if(!GetSystemDirectoryW(system,MAX_PATH))return fail("system directory lookup failed");
- const std::wstring path=std::wstring(system)+L"\\nvapi64.dll";
- g_ampere.nvapi=LoadLibraryExW(path.c_str(),nullptr,LOAD_LIBRARY_SEARCH_SYSTEM32);
+ g_ampere.nvapi=processNvapi();
if(!g_ampere.nvapi)return fail("system NVAPI unavailable");
g_ampere.query=reinterpret_cast<QueryNvApi>(GetProcAddress(g_ampere.nvapi,"nvapi_QueryInterface"));
if(!g_ampere.query)return fail("NVAPI query entry missing");
@@ -633,6 +637,8 @@ bool DlssNrRuntimeAdapter::verifyAmpereCompatibilityForTest()
void DlssNrRuntimeAdapter::unload()
{
+ const bool wasLoaded = module_ != nullptr;
+ if (wasLoaded) veyra::log::info("ngx", "nr-adapter: unload begin");
const bool hadAmpere=archLookupSlot_!=nullptr;
if(hadAmpere){
void* previous=nullptr;
@@ -651,6 +657,7 @@ void DlssNrRuntimeAdapter::unload()
}
if(hadAmpere)ClearAmpereContext();
exports_ = Exports{};
+ if (wasLoaded) veyra::log::info("ngx", "nr-adapter: unload complete (system NVAPI retained)");
}
} // namespace veyra::ngx
diff --git a/src/ngx/NgxCoreHost.cpp b/src/ngx/NgxCoreHost.cpp
index d5c2d820..8cc99cef 100644
--- a/src/ngx/NgxCoreHost.cpp
+++ b/src/ngx/NgxCoreHost.cpp
@@ -3,6 +3,7 @@
#include <windows.h>
#include <format>
+#include <chrono>
#include "veyra/Log.h"
#include "veyra/NgxResult.h"
@@ -155,10 +156,13 @@ void NgxCoreHost::shutdown()
{ uint32_t seh = 0; (void)CallDestroyParameters(block, seh); }
}
+ veyra::log::info("ngx", "core-host: Shutdown1 begin");
+ const auto started = std::chrono::steady_clock::now();
uint32_t sehCode = 0;
const NVSDK_NGX_Result result = CallShutdown1(device_, sehCode);
- veyra::log::info("ngx", std::format("core-host: Shutdown1 result={} seh={}",
- veyra::ngxResultString(static_cast<uint64_t>(result)), sehCode));
+ veyra::log::info("ngx", std::format("core-host: Shutdown1 result={} seh={} elapsedMs={:.3f}",
+ veyra::ngxResultString(static_cast<uint64_t>(result)), sehCode,
+ std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - started).count()));
device_ = nullptr;
initialized_ = false;
diff --git a/src/ngx/NvapiLifetime.cpp b/src/ngx/NvapiLifetime.cpp
new file mode 100644
index 00000000..5aa05eeb
--- /dev/null
+++ b/src/ngx/NvapiLifetime.cpp
@@ -0,0 +1,38 @@
+#include "veyra/ngx/NvapiLifetime.h"
+#include "veyra/Log.h"
+#include <format>
+#include <string>
+
+namespace veyra::ngx {
+HMODULE processNvapi()
+{
+ // One load/pin per process, including concurrent callers and graph rebuilds.
+ // Intentionally no module-owning destructor: driver detach under the loader
+ // lock must not run on the render thread after NR has been torn down (#67).
+ static const HMODULE module = []() -> HMODULE {
+ wchar_t directory[MAX_PATH]{};
+ const UINT length = GetSystemDirectoryW(directory, MAX_PATH);
+ if (!length || length >= MAX_PATH) {
+ log::error("nvapi-lifetime", std::format("system directory lookup failed error={} length={}", GetLastError(), length));
+ return nullptr;
+ }
+ const std::wstring path = std::wstring(directory) + L"\\nvapi64.dll";
+ HMODULE loaded = LoadLibraryExW(path.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
+ if (!loaded) {
+ log::error("nvapi-lifetime", std::format("system NVAPI load failed error={}", GetLastError()));
+ return nullptr;
+ }
+ HMODULE pinned = nullptr;
+ if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
+ reinterpret_cast<LPCWSTR>(loaded), &pinned)) {
+ // Fail NR initialization; retain this one reference rather than risk
+ // triggering the very driver detach this policy is meant to avoid.
+ log::error("nvapi-lifetime", std::format("system NVAPI pin failed error={}; NR disabled", GetLastError()));
+ return nullptr;
+ }
+ log::info("nvapi-lifetime", "system NVAPI pinned until process exit; no per-graph unload");
+ return pinned;
+ }();
+ return module;
+}
+}
diff --git a/src/pipeline/EnhanceGraph.cpp b/src/pipeline/EnhanceGraph.cpp
index b3bd1942..9c5fcdde 100644
--- a/src/pipeline/EnhanceGraph.cpp
+++ b/src/pipeline/EnhanceGraph.cpp
@@ -3611,9 +3611,11 @@ void EnhanceGraph::shutdown()
nrAdapter_->restoreCallerCompatibility();
nrAdapter_->unload();
}
+ log::info("graph", "shutdown: core retention/device health check begin");
const bool retainCore=coreCache_&&coreCache_->owns(coreHost_)&&initialized_&&featuresReleased&&coreHost_->healthy()&&
failedBackend_==engine::FailedBackend::None&&SUCCEEDED(context_.device()->GetDeviceRemovedReason())&&
coreHost_->liveParameterBlockCount()==0;
+ log::info("graph", std::format("shutdown: core retention/device health check complete retain={}", retainCore));
if(retainCore){
log::info("ngx-core-cache","event=retain liveParameters=0 featuresReleased=1");
}else if(coreHost_){
diff --git a/tests/integration/NrRuntimeSwitchTests.cpp b/tests/integration/NrRuntimeSwitchTests.cpp
index 7d80b44f..7e8607cf 100644
--- a/tests/integration/NrRuntimeSwitchTests.cpp
+++ b/tests/integration/NrRuntimeSwitchTests.cpp
@@ -53,8 +53,22 @@ int wmain(int argc,wchar_t** argv) {
const auto revision=engine.snapshot().desired.revision;
check(wait([&](const auto& s){return !s.applying&&s.applied.revision==revision&&!s.nrActive&&!s.srActive&&!s.fgActive;}),"plain playback restored in same process");
}
+ auto closeSettings=engine.snapshot().desired;
+ closeSettings.nr=true;
+ if(!failures) {
+ check(engine.requestSettings(closeSettings),"re-enable NR before close regression");
+ check(wait([](const auto& s){return !s.applying&&s.nrActive&&s.nrEvaluated>10;}),"NR active at close (no disable workaround)");
+ }
+ const auto stopping=std::chrono::steady_clock::now();
engine.stop();
check(wait([&](const auto&){return engine.idle();}),"GPU drained and engine stopped");
+ std::cout<<"NR_ACTIVE_STOP_MS="<<std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-stopping).count()<<std::endl;
+ if(!failures) {
+ engine.open(window,argv[1],veyra::engine::PlayerOptions::from(closeSettings));
+ check(wait([](const auto& s){return s.frames>30&&s.nrActive&&s.nrEvaluated>10;},25),"next video produces NR frames in same process");
+ engine.stop();
+ check(wait([&](const auto&){return engine.idle();}),"second NR-active session stops cleanly");
+ }
}
DestroyWindow(window);CoUninitialize();
std::cout<<"RTX30 hardware performance and visual quality NOT TESTED\n";
diff --git a/tests/integration/NvapiLifetimeTests.cpp b/tests/integration/NvapiLifetimeTests.cpp
new file mode 100644
index 00000000..e10d5a28
--- /dev/null
+++ b/tests/integration/NvapiLifetimeTests.cpp
@@ -0,0 +1,41 @@
+#include "veyra/ngx/NvapiLifetime.h"
+#include <array>
+#include <atomic>
+#include <iostream>
+#include <string>
+#include <thread>
+
+int main()
+{
+ std::array<HMODULE, 8> handles{};
+ std::array<std::thread, 8> threads;
+ std::atomic_bool ok{true};
+ // Race the first acquisition as well as subsequent graph-style borrows.
+ for (size_t i = 0; i < threads.size(); ++i) threads[i] = std::thread([&, i] {
+ for (unsigned cycle = 0; cycle < 64; ++cycle) {
+ const auto module = veyra::ngx::processNvapi();
+ if (!module || (handles[i] && handles[i] != module)) ok = false;
+ handles[i] = module;
+ }
+ });
+ for (auto& thread : threads) thread.join();
+ if (!ok) return 1;
+ for (const auto handle : handles) if (handle != handles[0]) return 2;
+ wchar_t directory[MAX_PATH]{}, actual[MAX_PATH]{};
+ if (!GetSystemDirectoryW(directory, MAX_PATH) || !GetModuleFileNameW(handles[0], actual, MAX_PATH)) return 3;
+ const std::wstring expected = std::wstring(directory) + L"\\nvapi64.dll";
+ if (_wcsicmp(actual, expected.c_str())) return 4;
+ for (unsigned cycle = 0; cycle < 64; ++cycle) {
+ HMODULE temporary = LoadLibraryExW(expected.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
+ if (temporary != handles[0] || !FreeLibrary(temporary)) return 5;
+ if (GetModuleHandleW(expected.c_str()) != handles[0] ||
+ !GetProcAddress(handles[0], "nvapi_QueryInterface")) return 6;
+ }
+ // Isolated process: exercise the documented PIN guarantee even when a
+ // consumer incorrectly drops extra references. Never do this in playback.
+ for (unsigned i = 0; i < 3; ++i) if (!FreeLibrary(handles[0])) return 7;
+ if (GetModuleHandleW(expected.c_str()) != handles[0] ||
+ !GetProcAddress(handles[0], "nvapi_QueryInterface")) return 8;
+ std::cout << "PASS concurrent system-only acquisition, 512 stable borrows, 64 balanced consumer load/unloads, process pin survives final-reference drops\n";
+ return 0;
+}