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

Select the HIP adapter before initializing the RDNA3 NR worker

Veyra Goal Agent19a8680f3171
diff --git a/AGENTS.md b/AGENTS.md
index 477814a8..a81fd52a 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -1,5 +1,7 @@
# Veyra Agent 工作规则
+> **2026-11-03 RX7000 NR多GPU修复(Claude,未提交;2026-11-04用户转述现场7900XTX已可开启NR)**:现场7900XTX+gfx1036核显,ZLUDA多处默认HIP 0号(NVAPI LUID已由Codex修;Context::new在hipSetDevice前建alloc_stream,cuMemAlloc经核显流清零独显显存,对应108.6MB权重处worker退出)。分支claude/rdna3-hip-visible-20261103(基于Codex 77fcd89,独立工作树worktrees/rdna3-hip-visible-20261103):worker加载HIP7前用自身--hip-probe子进程按LUID找序号,多设备时设HIP_VISIBLE_DEVICES只留选中卡;外部已设可见性变量则保留,探测失败维持原行为。构建43.7s;HIP设备单元15/0、本机真实HIP6 19/0、Codex诊断14/0;无RX7000推理。更新包test-packages/claude-rdna3-hip-visible-20261103/Veyra-2.1.2-RX7000-NR-multiGPU-fix2.zip(worker eb012a1c…,NVAPI沿用a7adb043…),回执reports/claude-rdna3-hip-visible-20261103/FINAL.json,记录docs/RDNA3_MULTI_GPU_HIP_2026-11-03.md。共用施工树/构建目录未触碰;不合main、不push/Release。
+
> **2026-11-03 RX7000后续崩溃(诊断进行中)**:用户veyra-qml(4).log和cache(1).zip确认上次NVAPI修复已生效:m_gpuArch=0x1b0,NR进入Small网络创建,最后记录108.6MiB权重heap请求后worker管道断开。不能称为已查明OOM或再次修好。当前同分支新增worker diagnostics=2:阶段、CRT/原生stderr、未处理SEH记录;不改变算法/参数/fence/主EXE,提供只读Windows崩溃事件收集工具。CPU诊断14、GPU协议54通过,收集脚本5.1夹具通过;本机无RX7000推理。记录docs/RDNA3_WORKER_CRASH_2026-11-03.md,回执reports/rdna3-crash-diagnostics-20261103/FINAL.json,诊断包test-packages/codex-rdna3-crash-diagnostics-20261103。优先让用户收集已有三次崩溃事件,证据不足再试新worker一次。原组件/配置/发布包不动,不合main、不push/发布。
> **2026-11-03 RX7000多GPU身份映射修复(本地待实卡)**:用户cache.zip显示7900XTX有gfx1036核显/gfx1100独显,NGX因GPU身份匹配失败报Unsupported GPU architecture 0x0。codex/rdna3-nvapi-luid-20261103基线ecfbb0f,修固定ZLUDA b135e233的nvapi/src/lib.rs:当前CUDA上下文设备LUID代替写死ordinal0,歧义/失败明确报错。只重新编译开源zluda_nvapi.dll,不改NVIDIA模型/驱动/原翻译器或缓存。原正式组件13组8失败,新组件13组全通过(含双线程64次),不等于RX7000推理已验。源码patch、MIT/Apache许可证、构建和夹具测试已记录;更新包test-packages/codex-rdna3-nvapi-luid-20261103,FINAL位于reports/rdna3-nvapi-luid-20261103。未来组RX7000包必须带此更新后的NVAPI并重做manifest,勿从旧供体回退b86425dd…;新DLL身份a7adb043…/215040字节。原app/依赖/正式包保留,不合main、不push/Release。详见docs/RDNA3_NVAPI_LUID_2026-11-03.md。测试用nvcuda.dll绝不能进入产品包。
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2d69e7dc..a4dd3a23 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -418,6 +418,11 @@ add_executable(veyra_rdna3_worker_diagnostics_tests tests/integration/Rdna3Worke
target_include_directories(veyra_rdna3_worker_diagnostics_tests PRIVATE include)
target_compile_features(veyra_rdna3_worker_diagnostics_tests PRIVATE cxx_std_20)
target_compile_definitions(veyra_rdna3_worker_diagnostics_tests PRIVATE NOMINMAX UNICODE _UNICODE WIN32_LEAN_AND_MEAN)
+add_executable(veyra_rdna3_hip_devices_tests tests/integration/Rdna3HipDevicesTests.cpp)
+target_include_directories(veyra_rdna3_hip_devices_tests PRIVATE include)
+target_compile_features(veyra_rdna3_hip_devices_tests PRIVATE cxx_std_20)
+target_compile_definitions(veyra_rdna3_hip_devices_tests PRIVATE NOMINMAX UNICODE _UNICODE WIN32_LEAN_AND_MEAN)
+target_link_libraries(veyra_rdna3_hip_devices_tests PRIVATE dxgi)
target_include_directories(veyra_amd_nr_test_worker PRIVATE include)
target_compile_features(veyra_amd_nr_test_worker PRIVATE cxx_std_20)
target_compile_definitions(veyra_amd_nr_test_worker PRIVATE NOMINMAX UNICODE _UNICODE WIN32_LEAN_AND_MEAN VEYRA_RDNA3_CONTRACT_WORKER=1)
diff --git a/docs/RDNA3_MULTI_GPU_HIP_2026-11-03.md b/docs/RDNA3_MULTI_GPU_HIP_2026-11-03.md
new file mode 100644
index 00000000..6f2965b2
--- /dev/null
+++ b/docs/RDNA3_MULTI_GPU_HIP_2026-11-03.md
@@ -0,0 +1,54 @@
+# RX7000 NR:多 GPU 时工作进程只看选中的显卡(2026-11-03,Claude)
+
+用户:“直接修复吧”。排查见 `E:/项目/Veyra/reports/claude-rdna3-nr-field-20261103/FINDINGS.md`。
+
+## 原因
+
+现场机器:RX 7900 XTX(gfx1100),另有 Ryzen 核显(gfx1036)。HIP 的设备顺序是核显 0 号、独显 1 号。
+
+RedDukeDev/ZLUDA `b135e23` 至少有两处默认使用 HIP 0 号设备:
+1. **NVAPI 报的 LUID**:报的是 0 号设备的 LUID,NGX 因此拒绝显卡架构,返回 0xBAD00001。Codex 已在 `b27c0df` 修复。
+2. **清零用的流建错了设备**:`Context::new` 先建 `alloc_stream`,后执行 `hipSetDevice`,所以这条流建在核显上。每次 `cuMemAlloc` 都通过这条核显流去给独显的显存清零。现场工作进程正是在 NGX 申请 108.6 MB 权重之后退出的。
+
+## 改动
+
+- **`include/veyra/pipeline/Rdna3HipDevices.h`(新)**:
+ - 读取 `hipDeviceProp_tR0600`:name 在偏移 0,LUID 在偏移 272,gcnArchName 按前缀查找。
+ - 按 LUID 匹配设备,没有或不唯一都视为匹配失败。
+ - 定义探测子进程的输出格式和解析方法。
+ - 启动探测子进程:只继承它自己的输出管道,超时 15 秒。
+ - 用 `SetEnvironmentVariableW` 加 `_wputenv_s` 设置 `HIP_VISIBLE_DEVICES`。
+- **`src/pipeline/Rdna3NrWorker.cpp`**:
+ - 在加载 `amdhip64_7.dll` 之前调用 `selectHipDevice`。它用同一个 exe 的 `--hip-probe` 模式列出设备。只有 HIP 设备多于一块、且选中适配器的 LUID 唯一匹配时,才把其余设备隐藏。
+ - 外面已经设置了 `HIP_VISIBLE_DEVICES`、`CUDA_VISIBLE_DEVICES` 或 `GPU_DEVICE_ORDINAL` 时,保留原设置。
+ - 探测失败时所有设备保持可见,和原来行为一致,不会因此报错退出。
+ - 日志新增设备列表和选择结果;诊断阶段新增 `hip-device-selection`。
+- **测试**:新增 `tests/integration/Rdna3HipDevicesTests.cpp`,并在 `CMakeLists.txt` 中加入目标 `veyra_rdna3_hip_devices_tests`。
+- **不变的部分**:主程序、协议、模型、ZLUDA、翻译缓存、驱动和配置都没有改。合同测试用的 worker(`VEYRA_RDNA3_CONTRACT_WORKER`)不包含这些改动。
+
+## 验证(本机 RTX 5070 + AMD 核显 gfx1036;没有 HIP 7,也没有 RX7000)
+
+- **构建**:`build-recording-handoff.py --task claude-rdna3-hip-visible-20261103 … --option VEYRA_RDNA3_NETWORK_SOURCE_DIR=…/prepared`,构建了 4 个目标,用时 43.7 秒。
+ - `C4005 WIN32_LEAN_AND_MEAN` 警告原来就有,不是这次引入的。
+- **`veyra_rdna3_hip_devices_tests`(单元)**:15 项通过,0 失败。
+- **`veyra_rdna3_hip_devices_tests --hip C:\Windows\System32\amdhip64_6.dll`**:19 项通过,0 失败。
+ - 真实探测得到 `index=0 luid=0x16583 arch=gfx1036`,这个 LUID 与 DXGI 报告的一致,说明偏移 272 正确。
+ - 在进程内、加载 HIP 之前设置序号后,只剩所选设备。
+ - 设置一个超出范围的序号后,设备数变为 0。说明 Windows 上的 HIP 确实读取这个变量。
+- **`veyra_amd_nr_worker.exe --hip-probe`**:
+ - 对 HIP 6:列出 1 块设备,退出码 0。
+ - 本机没有 HIP 7:输出 `load-failed-win32-126`,退出码 2。
+ - 参数个数不对时仍返回 2。
+- **`veyra_rdna3_worker_diagnostics_tests`**:14 项通过,0 失败。
+- **未运行**:`veyra_rdna3_nr_contract_tests`。它依赖 `veyra_pipeline`,而这次的改动不进入合同测试 worker。
+- **没有在 RX7000 实卡上推理过**,需要现场用户确认。
+
+## 交付
+
+`E:/项目/Veyra/test-packages/claude-rdna3-hip-visible-20261103/Veyra-2.1.2-RX7000-NR-multiGPU-fix2.zip`
+
+- 包内 worker 的 SHA256 是 `eb012a1c…`;NVAPI 仍用 Codex 的 `a7adb043…`。另含崩溃收集脚本、许可证和源码补丁。
+- 源码补丁以公开的 v2.1.2 源码 ZIP 为基准,已重放并逐文件比对。
+- 回执:`E:/项目/Veyra/reports/claude-rdna3-hip-visible-20261103/FINAL.json`。
+
+分支 `claude/rdna3-hip-visible-20261103` 基于 Codex 的 `77fcd89`,独立工作树为 `worktrees/rdna3-hip-visible-20261103`。改动未提交、未合并、未 push。
diff --git a/docs/WORKLOG.md b/docs/WORKLOG.md
index cf66b2ad..f10499c9 100644
--- a/docs/WORKLOG.md
+++ b/docs/WORKLOG.md
@@ -509,3 +509,42 @@ PID61528,用户直接运行2.1.2 NVIDIA包;不激活窗口、不启动额外
用户授权移植,分支codex/vfg-ampere-20261102基于4895549。来源Xchen991/RTX30-Broadcast-FrameGen固定07536ab32a508b7daa714a4a9bda5b56f031bbc3/MIT,移植CUDA查询和内核元数据重定向,替换固定IAT地址/轮询为按名称发现和生命周期租约;私有副本不写原kernel,选中设备和当前CUDA上下文隔离,退场逐项读回指针与页保护;40/50原生无补丁。菜单、节点、硬件能力和四语提示同步,包含上一轮全屏修复。主程序增量和覆盖补丁按FINAL.json,VFG原DLL/模型不新增不改动。
构建最终4.2s通过;合同180、真实模块/62个fatbin/3轮共享租约342、能力75、VFG设置683、repair300、i18n0失败;QtQuick140/0/1。RTX5070原生GPU各质量/倍率/8与10位回归2425项0失败,最终结果和独立GUI 2X60FPS功能检查见FINAL.json。首次repair因子进程库路径缺失启动失败,补充子进程PATH后通过,失败日志保留。无30系实卡推理/画质/速度结论;不以本机50系代替。临时GUI Loader已去除,原7配置文件和5个VFG运行DLL逐摘要保护,未复制完整包。实际命令、日志、来源与局限见docs/VFG_AMPERE_PLAN_2026-11-02.md,E:/项目/Veyra/reports及logs/vfg-ampere-20261102。本轮不合main、不push/发布。
+
+## 2026-11-03 RX7000 NR:多 GPU 时工作进程只看选中的显卡(Claude)
+
+用户:“直接修复吧”。
+
+**依据**:现场 7900 XTX 加 gfx1036 核显,HIP 0 号是核显。ZLUDA b135e23 有两处默认用 0 号设备:
+- NVAPI 报的 LUID(Codex 已修);
+- `Context::new` 在 `hipSetDevice` 之前就建了 `alloc_stream`,导致 `cuMemAlloc` 通过核显的流去给独显显存清零。
+
+**工作树**:独立工作树 `worktrees/rdna3-hip-visible-20261103`,分支 `claude/rdna3-hip-visible-20261103`,基于 Codex 的 `77fcd89`。共用施工树(Codex 分支 `codex/rdna3-nvapi-luid-20261103`,Codex 进程在运行)未触碰。
+
+**改动**:
+- 新增 `Rdna3HipDevices.h`:探测、按 LUID 匹配、设置 `HIP_VISIBLE_DEVICES`。
+- 工作进程在加载 HIP 7 之前做设备选择,并新增 `--hip-probe` 模式。
+- 新增 `tests/integration/Rdna3HipDevicesTests.cpp`,以及 CMake 目标。
+
+**命令与结果**:
+- `py -3.11 -X utf8 -B -I scripts/acceptance/build-recording-handoff.py --task claude-rdna3-hip-visible-20261103 --root <工作树> --label 2.1.2 --option VEYRA_RDNA3_NETWORK_SOURCE_DIR=E:/项目/Veyra/deps/amd-rdna3-nr-20261029/prepared veyra_amd_nr_worker veyra_amd_nr_test_worker veyra_rdna3_worker_diagnostics_tests veyra_rdna3_hip_devices_tests`
+ - 新构建目录,43.7 秒通过。
+- `veyra_rdna3_hip_devices_tests`:单元测试 15 项通过,0 失败。
+- 加 `--hip C:\Windows\System32\amdhip64_6.dll`:19 项通过,0 失败。
+ - 本机 gfx1036 的 LUID 0x16583 与 DXGI 一致。
+ - 在进程内设置后,HIP 只剩所选设备。
+ - 设置超出范围的序号后,设备数为 0。
+- `veyra_amd_nr_worker.exe --hip-probe`:
+ - 对 HIP 6:退出码 0,列出 1 块设备。
+ - 对缺失的 HIP 7:输出 `load-failed-win32-126`,退出码 2。
+- `veyra_rdna3_worker_diagnostics_tests`:14 项通过,0 失败。
+- 新旧 worker 的导入 DLL 相同:d3d12、dxgi、kernel32、version。
+
+**打包**:`tmp/claude-rdna3-hip-visible-20261103/package.py`。
+- 输出:`test-packages/claude-rdna3-hip-visible-20261103/Veyra-2.1.2-RX7000-NR-multiGPU-fix2.zip`。
+- 第一次生成的源码补丁带进了 Codex 的 RTX30 VFG CMake 段落,公开源码缺这些文件,会导致配置失败。改为只保留 RX7000 段落后,重放比对通过。
+- 第二次运行报 `PermissionError`:shell 的当前目录停在 verify 目录里。切换目录后重跑成功。
+
+**未做**:
+- 没有在 RX7000 实卡上推理;本机没有 HIP 7。
+- 没有跑 RDNA3 合同测试:改动不进入合同测试 worker。
+- 未提交、未合并、未 push。
diff --git a/include/veyra/pipeline/Rdna3HipDevices.h b/include/veyra/pipeline/Rdna3HipDevices.h
new file mode 100644
index 00000000..26896b7a
--- /dev/null
+++ b/include/veyra/pipeline/Rdna3HipDevices.h
@@ -0,0 +1,163 @@
+// SPDX-License-Identifier: GPL-3.0-only
+#pragma once
+// HIP device selection for the RX7000 worker.
+//
+// ZLUDA and its NVAPI stand-in take HIP device 0 in several places: the adapter
+// LUID NGX checks, and the stream a context zero-fills every new allocation on
+// (created before the context switches HIP to its own device). On a machine
+// with a Ryzen iGPU, HIP device 0 is often the iGPU, not the selected card.
+// Field 2026-11-03 (RX 7900 XTX + gfx1036): NGX first refused the architecture,
+// then, with the LUID repaired, the worker died at the 108.6 MB weight heap.
+//
+// The worker therefore hides every HIP device but the selected adapter with
+// HIP_VISIBLE_DEVICES before it loads HIP. The HIP library reads its
+// environment when it loads, so the index comes from a separate probe process
+// (this executable, --hip-probe) that lists the devices and exits.
+#include <windows.h>
+#include <algorithm>
+#include <cctype>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <iterator>
+#include <optional>
+#include <string>
+#include <vector>
+
+namespace veyra::pipeline::rdna3::hip {
+struct Device { int index=-1; uint64_t luid=0; std::string arch; std::string name; };
+
+// hipDeviceProp_tR0600 begins with char name[256], hipUUID uuid (16 bytes) and
+// char luid[8] (Windows only). gcnArchName lies further in and moves between
+// releases, so it is found by its prefix, as the network layer does.
+inline constexpr size_t kPropertiesBytes=16384,kNameBytes=256,kLuidOffset=272;
+
+inline Device parseProperties(int index,const char* props,size_t size){
+ Device d;d.index=index;
+ if(!props||size<kLuidOffset+8)return d;
+ d.name.assign(props,strnlen(props,kNameBytes));
+ memcpy(&d.luid,props+kLuidOffset,sizeof(d.luid));
+ for(size_t k=kLuidOffset+8;k+4<size;++k){
+ if(memcmp(props+k,"gfx",3)!=0||props[k+3]<'0'||props[k+3]>'9')continue;
+ size_t end=k;
+ while(end<size&&(isalnum(static_cast<unsigned char>(props[end]))||props[end]=='-'||props[end]==':'||props[end]=='+'))++end;
+ d.arch.assign(props+k,end-k);break;
+ }
+ return d;
+}
+
+// The index of the one device with this LUID; none when absent or ambiguous.
+inline std::optional<int> indexForLuid(const std::vector<Device>& devices,uint64_t luid){
+ std::optional<int> found;
+ for(const auto& d:devices){
+ if(d.luid!=luid)continue;
+ if(found)return std::nullopt;
+ found=d.index;
+ }
+ return found;
+}
+
+// The probe's output: one line per device, then a summary line.
+inline std::string formatDevice(const Device& d){
+ char line[512]{};
+ _snprintf_s(line,sizeof(line),_TRUNCATE,"hip-device index=%d luid=0x%016llX arch=%s name=%s\n",
+ d.index,static_cast<unsigned long long>(d.luid),d.arch.empty()?"unknown":d.arch.c_str(),d.name.c_str());
+ return line;
+}
+inline bool parseDevice(const std::string& line,Device& d){
+ int index=-1,consumed=0;unsigned long long luid=0;char arch[64]{};
+ if(sscanf_s(line.c_str(),"hip-device index=%d luid=0x%llX arch=%63s name=%n",&index,&luid,arch,unsigned(sizeof(arch)),&consumed)!=3||
+ index<0||consumed<=0)return false;
+ d.index=index;d.luid=luid;d.arch=strcmp(arch,"unknown")==0?"":arch;d.name=line.substr(size_t(consumed));
+ while(!d.name.empty()&&(d.name.back()=='\n'||d.name.back()=='\r'))d.name.pop_back();
+ return true;
+}
+struct ProbeResult { std::vector<Device> devices; int count=-1; std::string status; };
+// False unless the summary is present, says ok and matches the device lines.
+inline bool parseProbe(const std::string& output,ProbeResult& result){
+ result={};size_t start=0;
+ while(start<output.size()){
+ const size_t end=std::min(output.find('\n',start),output.size());
+ const std::string line=output.substr(start,end-start);start=end+1;
+ Device d;
+ if(parseDevice(line,d)){result.devices.push_back(d);continue;}
+ int count=-1;char status[64]{};
+ if(sscanf_s(line.c_str(),"hip-devices count=%d status=%63s",&count,status,unsigned(sizeof(status)))==2){result.count=count;result.status=status;}
+ }
+ return result.status=="ok"&&result.count>=0&&size_t(result.count)==result.devices.size();
+}
+
+// Probe mode: load this HIP library, list its devices, nothing else.
+inline int probeMain(const wchar_t* hipPath){
+ const HMODULE library=LoadLibraryExW(hipPath,nullptr,LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR|LOAD_LIBRARY_SEARCH_SYSTEM32);
+ if(!library){printf("hip-devices count=0 status=load-failed-win32-%lu\n",GetLastError());return 2;}
+ const auto count=reinterpret_cast<int(*)(int*)>(GetProcAddress(library,"hipGetDeviceCount"));
+ const auto properties=reinterpret_cast<int(*)(void*,int)>(GetProcAddress(library,"hipGetDevicePropertiesR0600"));
+ if(!count||!properties){printf("hip-devices count=0 status=missing-export\n");return 3;}
+ int devices=0;const int status=count(&devices);
+ if(status!=0){printf("hip-devices count=0 status=hip-error-%d\n",status);return 4;}
+ std::vector<char> buffer(kPropertiesBytes);
+ for(int i=0;i<devices;++i){
+ std::fill(buffer.begin(),buffer.end(),0);
+ const int result=properties(buffer.data(),i);
+ if(result!=0){printf("hip-devices count=%d status=properties-error-%d-device-%d\n",devices,result,i);return 5;}
+ fputs(formatDevice(parseProperties(i,buffer.data(),buffer.size())).c_str(),stdout);
+ }
+ printf("hip-devices count=%d status=ok\n",devices);fflush(stdout);
+ return 0;
+}
+
+// Runs `executable --hip-probe hipPath` with only its own output pipe inherited
+// (never the caller's protocol pipes) and reads the device list back.
+inline bool runProbe(const std::wstring& executable,const std::wstring& hipPath,ProbeResult& result,std::string& detail,DWORD timeoutMs=15000){
+ SECURITY_ATTRIBUTES sa{sizeof(sa),nullptr,TRUE};HANDLE read=nullptr,write=nullptr;
+ if(!CreatePipe(&read,&write,&sa,65536)){detail="probe pipe failed win32="+std::to_string(GetLastError());return false;}
+ SetHandleInformation(read,HANDLE_FLAG_INHERIT,0);
+ SIZE_T bytes=0;InitializeProcThreadAttributeList(nullptr,1,0,&bytes);std::vector<unsigned char> storage(bytes);
+ STARTUPINFOEXW startup{};startup.StartupInfo.cb=sizeof(startup);
+ startup.StartupInfo.dwFlags=STARTF_USESTDHANDLES;startup.StartupInfo.hStdOutput=write;startup.StartupInfo.hStdError=write;
+ startup.lpAttributeList=reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(storage.data());
+ if(!InitializeProcThreadAttributeList(startup.lpAttributeList,1,0,&bytes)||
+ !UpdateProcThreadAttribute(startup.lpAttributeList,0,PROC_THREAD_ATTRIBUTE_HANDLE_LIST,&write,sizeof(write),nullptr,nullptr)){
+ CloseHandle(read);CloseHandle(write);detail="probe handle allowlist failed";return false;
+ }
+ std::wstring command=L"\""+executable+L"\" --hip-probe \""+hipPath+L"\"";
+ PROCESS_INFORMATION process{};
+ const bool created=CreateProcessW(executable.c_str(),command.data(),nullptr,nullptr,TRUE,CREATE_NO_WINDOW|EXTENDED_STARTUPINFO_PRESENT,
+ nullptr,nullptr,&startup.StartupInfo,&process)!=FALSE;
+ const DWORD launchError=GetLastError();
+ DeleteProcThreadAttributeList(startup.lpAttributeList);CloseHandle(write);
+ if(!created){CloseHandle(read);detail="probe launch failed win32="+std::to_string(launchError);return false;}
+ // The output is a few hundred bytes: it fits the pipe, so waiting first cannot block the probe.
+ const DWORD waited=WaitForSingleObject(process.hProcess,timeoutMs);
+ if(waited!=WAIT_OBJECT_0)TerminateProcess(process.hProcess,1);
+ DWORD exitCode=0;GetExitCodeProcess(process.hProcess,&exitCode);
+ std::string output;char buffer[4096];DWORD n=0;
+ while(waited==WAIT_OBJECT_0&&ReadFile(read,buffer,sizeof(buffer),&n,nullptr)&&n)output.append(buffer,n);
+ CloseHandle(read);CloseHandle(process.hThread);CloseHandle(process.hProcess);
+ if(waited!=WAIT_OBJECT_0){detail="probe did not finish within "+std::to_string(timeoutMs)+" ms";return false;}
+ const bool parsed=parseProbe(output,result);
+ if(!parsed||exitCode){
+ detail="probe exit="+std::to_string(exitCode)+" status="+(result.status.empty()?"none":result.status);
+ return false;
+ }
+ return true;
+}
+
+// A visibility setting made outside Veyra wins: the probe already ran under it.
+inline std::wstring outsideVisibility(){
+ for(const wchar_t* name:{L"HIP_VISIBLE_DEVICES",L"CUDA_VISIBLE_DEVICES",L"GPU_DEVICE_ORDINAL"}){
+ wchar_t value[128]{};
+ if(GetEnvironmentVariableW(name,value,DWORD(std::size(value))))return std::wstring(name)+L"="+value;
+ }
+ return {};
+}
+
+// Both the process block (read by a statically linked CRT when the HIP library
+// loads) and this module's CRT copy. Must run before HIP is loaded.
+inline bool showOnly(int index){
+ const std::wstring value=std::to_wstring(index);
+ return SetEnvironmentVariableW(L"HIP_VISIBLE_DEVICES",value.c_str())&&_wputenv_s(L"HIP_VISIBLE_DEVICES",value.c_str())==0;
+}
+} // namespace veyra::pipeline::rdna3::hip
diff --git a/src/pipeline/Rdna3NrWorker.cpp b/src/pipeline/Rdna3NrWorker.cpp
index 6b8eaa53..bb75fd6d 100644
--- a/src/pipeline/Rdna3NrWorker.cpp
+++ b/src/pipeline/Rdna3NrWorker.cpp
@@ -9,6 +9,7 @@
#include "veyra/pipeline/AmdNrProvider.h"
#include "veyra/pipeline/Rdna3WorkerDiagnostics.h"
#ifndef VEYRA_RDNA3_CONTRACT_WORKER
+#include "veyra/pipeline/Rdna3HipDevices.h"
#include "runtime/core/network.h"
#endif
#include <cstdio>
@@ -33,6 +34,36 @@ bool waitCpu(ID3D12Fence* fence,uint64_t value,DWORD timeout){
if(fence->GetCompletedValue()>=value)return true;HANDLE event=CreateEventW(nullptr,FALSE,FALSE,nullptr);if(!event)return false;
const bool ok=SUCCEEDED(fence->SetEventOnCompletion(value,event))&&WaitForSingleObject(event,timeout)==WAIT_OBJECT_0;CloseHandle(event);return ok;
}
+#ifndef VEYRA_RDNA3_CONTRACT_WORKER
+// Before HIP loads: leave only the selected adapter visible to HIP, so ZLUDA's
+// device-0 assumptions name that card (Rdna3HipDevices.h). Never fatal: when
+// the probe cannot tell, every device stays visible, as before.
+void selectHipDevice(const std::filesystem::path& hipPath,uint64_t adapterLuid){
+ char line[512]{};
+ if(const auto outside=hip::outsideVisibility();!outside.empty()){
+ _snprintf_s(line,sizeof(line),_TRUNCATE,"HIP device visibility set outside Veyra (%ls); kept",outside.c_str());logLine(1,line);return;
+ }
+ wchar_t self[MAX_PATH]{};
+ const DWORD length=GetModuleFileNameW(nullptr,self,MAX_PATH);
+ hip::ProbeResult probe;std::string detail;
+ if(!length||length>=MAX_PATH||!hip::runProbe(self,hipPath.wstring(),probe,detail)){
+ _snprintf_s(line,sizeof(line),_TRUNCATE,"HIP device probe failed (%s); every HIP device stays visible",detail.c_str());logLine(1,line);return;
+ }
+ for(const auto& d:probe.devices){auto text=hip::formatDevice(d);text.pop_back();logLine(2,text.c_str());}
+ if(probe.devices.size()<=1){logLine(2,"one HIP device: nothing to hide");return;}
+ const auto index=hip::indexForLuid(probe.devices,adapterLuid);
+ if(!index){
+ _snprintf_s(line,sizeof(line),_TRUNCATE,"selected adapter LUID 0x%016llX is not exactly one HIP device; every HIP device stays visible",
+ static_cast<unsigned long long>(adapterLuid));
+ logLine(1,line);return;
+ }
+ if(!hip::showOnly(*index)){logLine(1,"HIP_VISIBLE_DEVICES could not be set; every HIP device stays visible");return;}
+ const auto chosen=std::find_if(probe.devices.begin(),probe.devices.end(),[&](const hip::Device& d){return d.index==*index;});
+ _snprintf_s(line,sizeof(line),_TRUNCATE,"HIP_VISIBLE_DEVICES=%d: this worker sees only %s (%s); the other %zu HIP device(s) are hidden",
+ *index,chosen->name.c_str(),chosen->arch.c_str(),probe.devices.size()-1);
+ logLine(2,line);
+}
+#endif
struct Worker {
Init init;
ComPtr<ID3D12Device> device;
@@ -78,6 +109,9 @@ struct Worker {
if(!n||n>=std::size(sdk)||!std::filesystem::path(sdk).is_absolute()){error="AMD HIP7 is unavailable in the driver or HIP_PATH";return false;}
hip=std::filesystem::path(sdk)/L"bin"/L"amdhip64_7.dll";
}
+ diagnostics::phase("hip-device-selection");
+ selectHipDevice(hip,init.adapterLuid);
+ diagnostics::phase("device-and-shared-resource-setup");
if(!LoadLibraryExW(hip.c_str(),nullptr,LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR|LOAD_LIBRARY_SEARCH_SYSTEM32)){error="AMD HIP7 load failed win32="+std::to_string(GetLastError());return false;}
const auto snippet=std::filesystem::path(init.runtime.data())/L"nvngx_dlssnr.dll";
DWORD ignored=0;const DWORD size=GetFileVersionInfoSizeW(snippet.c_str(),&ignored);std::vector<uint8_t> version(size);VS_FIXEDFILEINFO* info=nullptr;UINT count=0;
@@ -201,6 +235,10 @@ struct Worker {
};
}
int wmain(int argc,wchar_t** argv){
+#ifndef VEYRA_RDNA3_CONTRACT_WORKER
+ // Child of selectHipDevice: list the HIP devices and exit before any Init.
+ if(argc==3&&wcscmp(argv[1],L"--hip-probe")==0)return hip::probeMain(argv[2]);
+#endif
if(argc!=3)return 2;
HANDLE input=reinterpret_cast<HANDLE>(uintptr_t(_wcstoui64(argv[1],nullptr,10)));
HANDLE output=reinterpret_cast<HANDLE>(uintptr_t(_wcstoui64(argv[2],nullptr,10)));
diff --git a/tests/integration/Rdna3HipDevicesTests.cpp b/tests/integration/Rdna3HipDevicesTests.cpp
new file mode 100644
index 00000000..e7833938
--- /dev/null
+++ b/tests/integration/Rdna3HipDevicesTests.cpp
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-3.0-only
+// RX7000 worker HIP device selection (Rdna3HipDevices.h). Unit checks always;
+// with --hip <amdhip64 dll> also the real HIP library: the probe, its LUIDs
+// against DXGI, and HIP_VISIBLE_DEVICES set in-process before the library loads.
+// No NR inference: this machine needs no RX7000 for these.
+#include "veyra/pipeline/Rdna3HipDevices.h"
+#include <dxgi1_6.h>
+#include <wrl/client.h>
+#include <iostream>
+#include <set>
+
+namespace h=veyra::pipeline::rdna3::hip;
+using Microsoft::WRL::ComPtr;
+
+// Runs a child of this executable and returns what it printed.
+std::string capture(std::wstring command){
+ SECURITY_ATTRIBUTES sa{sizeof(sa),nullptr,TRUE};HANDLE read=nullptr,write=nullptr;
+ if(!CreatePipe(&read,&write,&sa,65536))return {};
+ SetHandleInformation(read,HANDLE_FLAG_INHERIT,0);
+ STARTUPINFOW startup{};startup.cb=sizeof(startup);startup.dwFlags=STARTF_USESTDHANDLES;startup.hStdOutput=write;startup.hStdError=write;
+ PROCESS_INFORMATION process{};
+ const bool created=CreateProcessW(nullptr,command.data(),nullptr,nullptr,TRUE,CREATE_NO_WINDOW,nullptr,nullptr,&startup,&process)!=FALSE;
+ CloseHandle(write);
+ std::string output;
+ if(created){
+ if(WaitForSingleObject(process.hProcess,20000)!=WAIT_OBJECT_0)TerminateProcess(process.hProcess,1);
+ char buffer[4096];DWORD n=0;
+ while(ReadFile(read,buffer,sizeof(buffer),&n,nullptr)&&n)output.append(buffer,n);
+ CloseHandle(process.hThread);CloseHandle(process.hProcess);
+ }
+ CloseHandle(read);return output;
+}
+
+int wmain(int argc,wchar_t** argv){
+ // Children: the probe itself, and the probe after the worker's in-process visibility step.
+ if(argc==3&&std::wstring(argv[1])==L"--hip-probe")return h::probeMain(argv[2]);
+ if(argc==4&&std::wstring(argv[1])==L"--visible-probe")return h::showOnly(_wtoi(argv[2]))?h::probeMain(argv[3]):9;
+ const bool realHip=argc==3&&std::wstring(argv[1])==L"--hip";
+ if(argc!=1&&!realHip)return 2;
+ wchar_t self[MAX_PATH]{};GetModuleFileNameW(nullptr,self,MAX_PATH);
+ unsigned checks=0,failures=0;
+ auto check=[&](bool ok,const char* name){++checks;if(!ok)++failures;std::cout<<(ok?"PASS ":"FAIL ")<<name<<"\n";};
+
+ {
+ std::vector<char> props(h::kPropertiesBytes,0);
+ strcpy_s(props.data(),h::kNameBytes,"AMD Radeon RX 7900 XTX");
+ memcpy(props.data()+256,"gfx9999-in-uuid",15); // UUID bytes that look like an arch are not the arch
+ const unsigned char luid[8]={0xE2,0x00,0x01,0x00,0,0,0,0};memcpy(props.data()+h::kLuidOffset,luid,sizeof(luid));
+ strcpy_s(props.data()+1200,64,"gfx1100");
+ const auto d=h::parseProperties(1,props.data(),props.size());
+ check(d.index==1&&d.name=="AMD Radeon RX 7900 XTX"&&d.luid==0x100E2ull&&d.arch=="gfx1100","properties: name, LUID at offset 272, arch after the LUID");
+ const auto shortBuffer=h::parseProperties(0,props.data(),100);
+ check(shortBuffer.luid==0&&shortBuffer.name.empty(),"properties: a short buffer yields nothing");
+ }
+ {
+ std::vector<h::Device> devices{{0,0x12089,"gfx1036","AMD Radeon(TM) Graphics"},{1,0x100E2,"gfx1100","AMD Radeon RX 7900 XTX"}};
+ check(h::indexForLuid(devices,0x100E2)==1,"selected LUID finds the dGPU behind an iGPU at HIP index 0");
+ check(!h::indexForLuid(devices,0xDEAD),"an unknown LUID selects nothing");
+ devices.push_back({2,0x100E2,"gfx1100","duplicate"});
+ check(!h::indexForLuid(devices,0x100E2),"an ambiguous LUID selects nothing");
+ }
+ {
+ const h::Device d{3,0x00000001000100E2ull,"","AMD Radeon(TM) Graphics (iGPU)"};h::Device back;
+ check(h::parseDevice(h::formatDevice(d),back)&&back.index==3&&back.luid==d.luid&&back.arch.empty()&&back.name==d.name,
+ "device line round trip with spaces in the name and an unknown arch");
+ check(!h::parseDevice("hip-device index=x",back),"a malformed device line is refused");
+ h::ProbeResult r;
+ const std::string two=h::formatDevice({0,0x12089,"gfx1036","iGPU"})+h::formatDevice({1,0x100E2,"gfx1100","dGPU"})+"noise\nhip-devices count=2 status=ok\n";
+ check(h::parseProbe(two,r)&&r.devices.size()==2&&r.devices[1].arch=="gfx1100","probe output is read back");
+ check(!h::parseProbe(h::formatDevice({0,1,"gfx1100","a"})+"hip-devices count=2 status=ok\n",r),"a count that disagrees with the device lines is refused");
+ check(!h::parseProbe("hip-devices count=0 status=hip-error-100\n",r)&&r.status=="hip-error-100","a HIP error is reported, not accepted");
+ check(!h::parseProbe(h::formatDevice({0,1,"gfx1100","a"}),r),"output without the summary line is refused");
+ }
+ {
+ for(const wchar_t* name:{L"HIP_VISIBLE_DEVICES",L"CUDA_VISIBLE_DEVICES",L"GPU_DEVICE_ORDINAL"}){SetEnvironmentVariableW(name,nullptr);_wputenv_s(name,L"");}
+ check(h::outsideVisibility().empty(),"no visibility variable from outside");
+ SetEnvironmentVariableW(L"CUDA_VISIBLE_DEVICES",L"0");
+ check(h::outsideVisibility()==L"CUDA_VISIBLE_DEVICES=0","a visibility variable from outside is reported and kept");
+ SetEnvironmentVariableW(L"CUDA_VISIBLE_DEVICES",nullptr);
+ wchar_t block[16]{},crt[16]{};size_t length=0;
+ check(h::showOnly(1)&&GetEnvironmentVariableW(L"HIP_VISIBLE_DEVICES",block,16)==1&&std::wstring(block)==L"1"&&
+ _wgetenv_s(&length,crt,16,L"HIP_VISIBLE_DEVICES")==0&&std::wstring(crt)==L"1","showOnly sets the process block and the CRT copy");
+ SetEnvironmentVariableW(L"HIP_VISIBLE_DEVICES",nullptr);_wputenv_s(L"HIP_VISIBLE_DEVICES",L"");
+ }
+ {
+ h::ProbeResult r;std::string detail;
+ check(!h::runProbe(self,L"C:\\nonexistent-veyra-test\\amdhip64_7.dll",r,detail)&&detail.find("load-failed")!=std::string::npos,
+ "a probe that cannot load HIP fails with its reason (the worker then keeps every device visible)");
+ }
+ if(realHip){
+ h::ProbeResult r;std::string detail;
+ const bool probed=h::runProbe(self,argv[2],r,detail);
+ check(probed&&!r.devices.empty(),"the real HIP library is probed in a child process");
+ std::set<uint64_t> dxgi;ComPtr<IDXGIFactory1> factory;
+ if(SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory))))
+ for(UINT i=0;;++i){
+ ComPtr<IDXGIAdapter1> adapter;if(factory->EnumAdapters1(i,&adapter)==DXGI_ERROR_NOT_FOUND)break;
+ DXGI_ADAPTER_DESC1 desc{};adapter->GetDesc1(&desc);dxgi.insert(uint64_t(uint32_t(desc.AdapterLuid.HighPart))<<32|desc.AdapterLuid.LowPart);
+ }
+ bool matched=probed&&!r.devices.empty();
+ for(const auto& d:r.devices){std::cout<<" "<<h::formatDevice(d);matched=matched&&dxgi.count(d.luid)&&d.arch.rfind("gfx",0)==0;}
+ check(matched,"every HIP LUID is a DXGI adapter LUID and every arch is gfx*");
+ if(probed&&!r.devices.empty()){
+ const auto& first=r.devices.front();h::ProbeResult one,none;
+ check(h::parseProbe(capture(L"\""+std::wstring(self)+L"\" --visible-probe "+std::to_wstring(first.index)+L" \""+argv[2]+L"\""),one)&&
+ one.devices.size()==1&&one.devices[0].luid==first.luid,"HIP_VISIBLE_DEVICES set in-process before the load keeps exactly the chosen device");
+ h::parseProbe(capture(L"\""+std::wstring(self)+L"\" --visible-probe "+std::to_wstring(int(r.devices.size())+3)+L" \""+argv[2]+L"\""),none);
+ check(none.devices.empty()&&none.count==0,"an index past the last device leaves HIP none (the variable is honoured)");
+ }
+ }
+ std::cout<<"checks="<<checks<<" failures="<<failures<<(realHip?" (real HIP library: listing and visibility only, no NR inference)":" (unit only)")<<"\n";
+ return failures?1:0;
+}