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++
test(fg): J0 motion-input experiment for character-edge jelly
Veyra Goal Agent66e240748f46
diff --git a/AGENTS.md b/AGENTS.mdindex 453c444e..30e7d35f 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -1,5 +1,7 @@# Veyra 项目 Agent 执行规则+> **2026-10-12 补帧人物边缘果冻 J0(Claude)**:用户要求先排查果冻(延迟暂缓),按 reports/jelly-latency-20261012/方案选项.md 先做 J0 判定,只测 DLSS(XeSS 在送显端无法直接读回)。存档 `checkpoint/pre-claude-jelly-20261012`(main 76f41b3)与 E 盘 `archives/claude-jelly-20261012`;分支 `claude/jelly-20261012` 只新增诊断测试 `veyra_fg_motion_boundary_tests` 及文档,不改产品、默认设置、运行库/模型/驱动/用户配置;无 merge/push/Release。结论见 docs/JELLY_J0_2026-10-12.md。+> **2026-10-11 当前修复已合入本地main**:Codex4557ab6(含e8300d9)与Claude1d4c33f经隔离整合、42依赖闭包/13EXE构建、完整CMake、16组回归、12真实GUI状态/6布局及SR+VFG组合持久化/重启通过;实测产品4904b73、记录tip adcf509通过功能merge d2e1396进入main,完整Git树相同。VFG默认中档、DLSS K、33ms统计/250ms状态及负优化撤回保持。59开工树中唯main按授权更新,其余58树/504脏文件、来源分支及原包1626文件保持;final.json见reports/merge-current-fixes-20261011。首次组合GUI夹具漏FG节点失败,修夹具后通过,原证据保留;没有新的性能A/B或全硬件/长期显存通过声明。无push/打包/Release/关机。> **2026-10-11 当前修复本地合并授权**:用户明确要求将 Codex 与 Claude 当前修复合入 main。按 docs/MERGE_CURRENT_FIXES_PLAN_2026-10-11.md,在 codex/merge-current-fixes-20261011 整合已验4557ab6(含e8300d9)与Claude1d4c33f,必要冲突适配后构建/定向回归,再本地main合并;覆盖这些接点历史no-merge。59旧工作树/504脏文件及来源tip由独立start/bundle保护,main仅按本次授权更新。默认档位、33ms统计/250ms状态节奏保持,不派Agent,不改运行库/模型/驱动/真实配置。产物全E盘,每测试≤300秒、构建≤900秒;不push/Release/打包/关机,不恢复已撤回负优化或旧未完成实验。diff --git a/CMakeLists.txt b/CMakeLists.txtindex c74a6c8d..fd273020 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -912,6 +912,11 @@ if(VEYRA_FFMPEG_ROOT AND EXISTS "${VEYRA_FFMPEG_ROOT}/include/libavformat/avformtarget_include_directories(veyra_srfg_depth_tests PRIVATE ${FFMPEG_INCLUDE_DIRS})target_compile_definitions(veyra_srfg_depth_tests PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN VEYRA_PROJECT_ROOT="${CMAKE_CURRENT_SOURCE_DIR}")target_link_libraries(veyra_srfg_depth_tests PRIVATE veyra_engine)+ add_executable(veyra_fg_motion_boundary_tests tests/integration/FgMotionBoundaryTests.cpp)+ target_compile_features(veyra_fg_motion_boundary_tests PRIVATE cxx_std_20)+ target_include_directories(veyra_fg_motion_boundary_tests PRIVATE ${FFMPEG_INCLUDE_DIRS})+ target_compile_definitions(veyra_fg_motion_boundary_tests PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN VEYRA_PROJECT_ROOT="${CMAKE_CURRENT_SOURCE_DIR}")+ target_link_libraries(veyra_fg_motion_boundary_tests PRIVATE veyra_engine)# Qt 6.8.3 is staged under deps/ (the user allowed the latest Qt). The probe is# opt-in so a normal product build never depends on Qt being present.option(VEYRA_BUILD_QT_PROBE "Build the Qt + D3D12 host probe" OFF)diff --git a/docs/JELLY_J0_2026-10-12.md b/docs/JELLY_J0_2026-10-12.mdnew file mode 100644index 00000000..31fef981--- /dev/null+++ b/docs/JELLY_J0_2026-10-12.md@@ -0,0 +1,101 @@+# 补帧人物边缘“果冻”:J0 判定实验(2026-10-12,Claude)++## 授权与范围++- 用户先拿到排查方案 `E:/项目/Veyra/reports/jelly-latency-20261012/方案选项.md`,回复:+ - “1.光流来源 2.几倍都一样,一般都用NV12 或者yuy 3.延迟这块先放着吧,先搞果冻的问题,看看能不能想办法解决一下,推荐VFG不是解决办法,你先做J0判定吧”;+ - 随后补充“测试xess即可,不要浪费时间”“那就测dlss”。+- J0 只加诊断测试程序,不改产品代码、默认设置、运行库、模型、驱动或用户配置。+- 存档:+ - 标签 `checkpoint/pre-claude-jelly-20261012`,指向 main 76f41b3;+ - `archives/claude-jelly-20261012/` 下有 `source-before.bundle`(已 verify)和 `start.json`(61 个工作树、504 个脏文件)。+- 分支:`claude/jelly-20261012`,工作树 `worktrees/jelly-latency-20261012`。不 merge、不 push、不 Release。++## 方法++- 程序:`tests/integration/FgMotionBoundaryTests.cpp` → `veyra_fg_motion_boundary_tests <输出目录> <dlss|fsr> <orbit|walk> <变体>`。+- 构建:`py -3.11 -I tmp/claude-jelly-20261012/build.py`。+ - 先逐个核对本工作树的产品源码与 Codex 合并构建回执(`logs/merge-current-fixes-20261011/build-20261011-221225`)一致;+ - 然后只编译这一个测试源文件,用该回执的测试链接文件链接。+- 运行:在私有 app 副本 `tests/claude-jelly-latency-20261012/app` 中运行,着色器、DLSS 310.9.1 和 FSR 3.1.6 运行库都取自其中。D3D12 调试层开启。+- 场景:4K 合成画面,按 30fps 内容的位移设计:每帧 40 px,A/B 原帧之间有精确的中间帧真值。+ - **orbit**:镜头转动,背景平移,人物在屏幕上静止;+ - **walk**:镜头不动,人物移动。+- 产品链路:+ - 光流在 1920×1080 上计算,与 4K 采集加实时 NR 相同;+ - 补帧 2X,不开 NR / SR;+ - 每组 10 张原帧,前 2 张作为预热,统计后 8 张生成帧。+- 补帧的运动输入用测试钩子 `fgMotionProbe` 替换:++| 变体 | 补帧收到的运动矢量 |+| --- | --- |+| zero | 全 0(只靠补帧自己的运动估计;XeSS 在“自动”下就是这样) |+| product | 产品光流:NVOF → NvofDensify → FlowAdapt |+| ideal | 逐像素精确矢量,露出区按背景运动给 |+| blocky | ideal 按 8×8 块中心取样(只模拟网格量化) |+| zeroband | ideal,但遮挡露出带和轮廓两侧各一块清零(只模拟清零) |+| inverted / garbage | 对照:ideal 取反 / 全屏 +120 px |+| nrflow / nrzero | 开 NR(1080p 实时),NR 运动分别取产品光流 / 0 |++- 指标:+ - 轮廓带:距人物轮廓 64 px 以内,统计生成帧与真值的 RGB 平均误差、单通道误差 >32 的像素比例和 P95;+ - 远处背景平均误差;+ - 抖动:只在 orbit 场景,统计静止人物内侧 64 px 以内相邻上屏帧的变化。+- 结果文件:`logs/claude-jelly-20261012/j0/j0-results.json`,每组日志、裁切图、误差图、`sheet-*.png` 在同一目录。++## 结果(RTX 5070)++### DLSS 补帧:完全不受运动矢量影响++| 变体 | orbit 轮廓带误差 / 坏点 | walk 轮廓带误差 / 坏点 |+| --- | --- | --- |+| zero | 2.774 / 2.59% | 2.579 / 2.29% |+| product | 2.773 / 2.59% | 2.580 / 2.29% |+| ideal | 2.774 / 2.59% | 2.579 / 2.29% |+| blocky / zeroband | 2.775 / 2.59% | 2.579 / 2.29% |+| inverted | 2.775 / 2.59% | 2.579 / 2.29% |+| garbage(全屏 +120 px) | 2.775 / 2.59% | 2.583 / 2.29% |++- 连明显错误的矢量都不改变结果,误差图也完全相同。+- 在产品现在的 DLSS-G 参数下(静止合成相机、常数深度、`cameraMotionIncluded=1`),DLSS 补帧基本只用自己的运动估计。+- 加上 `SR_FG_DEPTH_RESPONSE_2026-09-08.md`(深度几乎无响应),对 DLSS 补帧来说,我们能给的矢量和深度都改变不了轮廓结果。用户反馈“开启光流也没用”,与此一致。++### FSR 补帧:强烈依赖运动矢量,产品光流已接近理想++| 变体 | orbit 轮廓带 / 坏点 / 远处背景 | walk 轮廓带 / 坏点 |+| --- | --- | --- |+| zero | 6.586 / 8.12% / **16.26** | 10.521 / 17.21% |+| product | **1.542 / 1.83% / 0.10** | **2.041 / 3.13%** |+| ideal | 1.711 / 3.74% / 0 | 2.050 / 3.52% |+| blocky | 1.917 / 4.12% / 0 | 2.099 / 3.64% |+| zeroband | 1.710 / 2.09% / 0 | 2.842 / 5.82% |+| garbage | 24.08 / 47.13% / 17.91 | 25.27 / 49.63% |++- **没有矢量时**,镜头转动下整片背景不跟着动,远处背景误差 16。+- **产品光流**与理想矢量相当,orbit 下甚至更好。因此在干净画面上,“光流精修”(方案 J1)没有可测收益。+- **顺带发现一个缺陷**:`EnhanceGraph::initNvof` 只在补帧后端是 DLSS 时才为补帧创建光流(`frameGenerationBackend==Dlss`)。只开 FSR 补帧时,“光流/自动”实际给的是 0,必须同时开 NR 或 SR 才有光流。第一次跑 product 组时 NVOF 执行 0 次,结果与 zero 完全相同,就是这个原因;测试里给 product 组打开了独立光流,与开 NR 的用户一致。+- XeSS 走送显端补帧,创建光流的条件不同;它在“自动”下按设计给 0(`motionUsesFlow`)。++### NR 的影响(DLSS 2X)++| 组 | orbit 真实帧轮廓带误差 | 生成帧轮廓带 / 坏点 | 静止人物轮廓内侧抖动 |+| --- | --- | --- | --- |+| 不开 NR | 0 | 2.77 / 2.59% | 0.241 |+| NR,运动=0 | 8.98 | 10.77 / 20.63% | 0.452 |+| NR,运动=产品光流 | 11.47 | 13.00 / 35.89% | 0.376 |++- walk 场景:NR 光流 12.34 / 35.75%,NR 零运动 11.23 / 28.88%。+- NR 会改变画面,所以“与真值的误差”里包含正常的增强效果。+- 用产品光流时,轮廓带偏离原图更多,但静止人物内侧的逐帧抖动没有增加(3 组都在 0.5 级以下)。合成画面里没有复现出由 NR 引起的轮廓晃动。++## 判定++1. DLSS 用户的果冻不是我们的光流造成的,也不能靠改光流来修:DLSS 补帧不读这些矢量。+2. 吃矢量的补帧(以 FSR 代表)在干净的合成画面上,产品光流已经与理想矢量相当。方案 J1(光流精修)在 J0 里没有收益,**不建议实施**。+3. 合成场景(刚体、无模糊、无压缩噪声、每帧 40 px)里,DLSS 和 FSR 的轮廓误差都只是一条窄边,**没能复现**用户看到的明显果冻。真正的诱因还在真实画面的特性里:位移更大、运动模糊、压缩噪声、人物肢体动画、背景视差,或者游戏自身的 TAA 拖影。这些需要真实素材才能确认。+4. XeSS 没有测:它在送显端由提供方自行生成和显示,处理图读不回生成帧。SDK 3.0.2 有 `XEFG_SWAPCHAIN_DEBUG_FEATURE_SHOW_ONLY_INTERPOLATION`,可以配合抓屏单独实施。++## 边界++- 只测了合成画面和本机 RTX 5070、DLSS 310.9.1、FSR 3.1.6。没有真实游戏画面,没有 XeSS,没有其他显卡。+- 首次重跑 dlss-*-zero 时裁切图文件已存在、保存失败,所以那两条记为 pass=false;指标与之后的有效重跑一致,后来已改为覆盖保存。FSR 首轮因为测试程序按错误状态上传矢量,触发了调试层报错,修正后整组重跑,报错为 0。diff --git a/docs/WORKLOG.md b/docs/WORKLOG.mdindex a5eb17e3..b268f79c 100644--- a/docs/WORKLOG.md+++ b/docs/WORKLOG.md@@ -9149,3 +9149,10 @@ seal.py validate逐项核对源码/输入/EXE/QML/原包1626文件及旧树;2026-10-11 本地合并收尾:实测产品4904b73,补报告/WORKLOG后tip adcf509;main功能merge d2e1396无冲突,完整Git树与adcf509相同。后续仅四份收尾文档。seal.py final核对833源码/配置、13EXE、所有回执及来源祖先、main clean、其它58旧树/504脏文件和原包1626文件,最终main提交/增量bundle/标签见reports/merge-current-fixes-20261011/final.json。本轮无推送/打包/发布/关机,来源分支均保留,不把原正式/测试包说成已经更新。首次final raw-SHA核对停止于旧main ColorDetailPass.h;逐项排查只有9个旧文件LF/CRLF差异,去除CRLF后字节完全相同,Git树仍仅四份文档差异。保留final-raw-hash-attempt.json,核对器改为对这类差异额外验证唯一换行转换及完整Git内容,并记录两份checkout SHA;未改任何产品/旧源文件。后续final.json记录最终通过结果。++## 2026-10-12 补帧人物边缘果冻:J0 判定(Claude)++- 授权:用户看完 reports/jelly-latency-20261012/方案选项.md 后回复“延迟这块先放着吧,先搞果冻的问题……你先做J0判定吧”,随后“测试xess即可,不要浪费时间”“那就测dlss”。存档:标签 checkpoint/pre-claude-jelly-20261012(main 76f41b3),archives/claude-jelly-20261012(bundle 已 verify,start.json 61 工作树/504 脏文件);分支 claude/jelly-20261012。只加诊断测试,不改产品。+- 构建:py -3.11 -I E:/项目/Veyra/tmp/claude-jelly-20261012/build.py(产品源码逐个对照 Codex 合并构建回执,只编译新测试并用其链接文件链接,6–8 秒)。运行目录为私有副本 tests/claude-jelly-latency-20261012/app,D3D12 调试层开启。+- 结果(4K 合成场景,30fps 位移,FG 2X,光流 1080p):DLSS 补帧对零、产品光流、理想、取反、全屏 +120 px 的矢量结果相同(轮廓带误差 2.77/2.58),即不读矢量。FSR 依赖矢量:零 6.59/10.52(镜头转动时背景整体 16.26),产品光流 1.54/2.04,与理想 1.71/2.05 相当。开 NR 后轮廓带偏离原图更多(NR 光流 13.00 对 NR 零 10.77),但静止人物内侧抖动没有增加。判定:光流精修(J1)没有收益,不实施。附带发现 initNvof 只给 DLSS 补帧建光流,只开 FSR 补帧时“光流”实际为 0。+- 证据:logs/claude-jelly-20261012/j0/(j0-results.json、每组日志、裁切图/误差图/sheet)。详细结论与边界见 docs/JELLY_J0_2026-10-12.md。合成场景没有复现明显果冻,XeSS 未测;不 merge/push/Release。diff --git a/tests/integration/FgMotionBoundaryTests.cpp b/tests/integration/FgMotionBoundaryTests.cppnew file mode 100644index 00000000..7f2eb6f5--- /dev/null+++ b/tests/integration/FgMotionBoundaryTests.cpp@@ -0,0 +1,316 @@+// J0 for the "jelly" field report (2026-10-12): how much of the wobble around a+// foreground silhouette comes from the motion vectors frame generation receives?+// Synthetic 4K third-person scenes with exact mid-frame ground truth run through the+// product EnhanceGraph (FG 2X, flow at 1920x1080 like 4K capture with realtime NR).+// Only the FG motion input changes between runs:+// zero - all zero (the provider's own optical flow only)+// product - no probe: product NVOF -> NvofDensify -> FlowAdapt, what users get+// ideal - exact current->previous motion per pixel, background motion in the+// disoccluded strip (fgMotionProbe)+// blocky - ideal sampled at the centre of each 8x8 block (the grid alone)+// zeroband - ideal, zeroed where the product checks zero it: the disoccluded+// strip and one block either side of the silhouette (zeroing alone)+// inverted - ideal with the opposite sign, and garbage - +120 px everywhere: controls+// showing whether the provider uses this input at all+// nrflow / nrzero - NR on (1920x1080 realtime, like 4K capture), NR motion from the+// product flow or zero; FG motion zero. Real frames are then NR output.+// Scenes: orbit (camera turns: background pans, character static on screen) and+// walk (static camera, character moves). Diagnostic readback only.+// usage: veyra_fg_motion_boundary_tests <outDir> <dlss|fsr> <orbit|walk> <variant>+#include "veyra/pipeline/EnhanceGraph.h"+#include "veyra/pipeline/GpuPassUtils.h"+#include "veyra/gfx/D3D12DeviceContext.h"+#include "veyra/gfx/CommandSlotRing.h"+#include "veyra/sink/ImageExportSink.h"+#include "veyra/RuntimePaths.h"+#include <d3d12sdklayers.h>+#include <DirectXPackedVector.h>+#include <algorithm>+#include <array>+#include <cmath>+#include <cstring>+#include <filesystem>+#include <format>+#include <iostream>+#include <string>+#include <vector>+extern "C" {+#include <libavutil/frame.h>+#include <libavutil/pixfmt.h>+}+using namespace veyra;+namespace {+constexpr int W=3840,H=2160,FlowW=1920,FlowH=1080,N=10,D=40,Band=64,Warmup=2,Block=8;+constexpr int BW=W+(N+2)*D; // background strip wide enough for the orbit pan++float hash01(int x,int y,uint32_t seed){+ uint32_t v=uint32_t(x)*0x9e3779b9u^uint32_t(y)*0x85ebca6bu^seed*0xc2b2ae35u;+ v^=v>>16;v*=0x7feb352du;v^=v>>15;v*=0x846ca68bu;v^=v>>16;return float(v&0xffffu)/65535.f;+}+float noise(float x,float y,float cell,uint32_t seed){+ const float gx=x/cell,gy=y/cell;const int ix=int(std::floor(gx)),iy=int(std::floor(gy));+ float tx=gx-ix,ty=gy-iy;tx=tx*tx*(3-2*tx);ty=ty*ty*(3-2*ty);+ const float a=hash01(ix,iy,seed),b=hash01(ix+1,iy,seed),c=hash01(ix,iy+1,seed),d=hash01(ix+1,iy+1,seed);+ return (a*(1-tx)+b*tx)*(1-ty)+(c*(1-tx)+d*tx)*ty;+}+// Character = union of capsules; part 0 head, 1 torso, 2 legs, 3 arms.+struct Capsule{float ax,ay,bx,by,r;int part;};+constexpr Capsule kShape[]={{1920,560,1920,560,120,0},{1920,790,1920,1290,230,1},{1820,1360,1800,1900,85,2},+ {2020,1360,2040,1900,85,2},{1660,830,1600,1360,70,3},{2180,830,2240,1360,70,3}};+float sdf(float x,float y,int* part=nullptr){+ float best=1e9f;int which=-1;+ for(const auto& c:kShape){+ const float px=x-c.ax,py=y-c.ay,bx=c.bx-c.ax,by=c.by-c.ay,len=bx*bx+by*by;+ const float h=len>0?std::clamp((px*bx+py*by)/len,0.f,1.f):0.f;+ const float d=std::hypot(px-bx*h,py-by*h)-c.r;+ if(d<best){best=d;which=c.part;}+ }+ if(part)*part=which;return best;+}+struct Scene {+ bool orbit=true;+ std::vector<std::array<uint8_t,3>> background; // BW x H+ std::vector<std::array<uint8_t,4>> character; // W x H, alpha = coverage+ std::vector<float> distance; // W x H signed distance (character at offset 0)+ int bgOffset(double frame)const{return orbit?int(std::lround(frame*D)):0;}+ int chOffset(double frame)const{return orbit?0:int(std::lround(frame*D));}+ // Character layer and distance sampled with the character shifted right by `o`.+ const std::array<uint8_t,4>* ch(int x,int y,int o)const{const int sx=x-o;return sx>=0&&sx<W?&character[size_t(y)*W+sx]:nullptr;}+ float dist(int x,int y,int o)const{const int sx=x-o;return sx>=0&&sx<W?distance[size_t(y)*W+sx]:1e9f;}+ bool covered(int x,int y,int o)const{auto* c=ch(x,y,o);return c&&(*c)[3]>=128;}+ std::array<uint8_t,3> pixel(int x,int y,double frame)const{+ const auto& b=background[size_t(y)*BW+x+bgOffset(frame)];+ const auto* c=ch(x,y,chOffset(frame));if(!c||!(*c)[3])return b;+ const float a=(*c)[3]/255.f;+ return {uint8_t(std::lround(b[0]*(1-a)+(*c)[0]*a)),uint8_t(std::lround(b[1]*(1-a)+(*c)[1]*a)),uint8_t(std::lround(b[2]*(1-a)+(*c)[2]*a))};+ }+ // Exact current(frame)->previous(frame-1) motion in pixels (product flow convention).+ std::array<float,2> ideal(int x,int y,int frame)const{+ if(covered(x,y,chOffset(frame)))return {orbit?0.f:-float(D),0.f};+ return {orbit?float(D):0.f,0.f};+ }+ bool disoccluded(int x,int y,int frame)const{+ if(covered(x,y,chOffset(frame)))return false;+ // Background visible now, hidden by the character in the previous frame.+ return orbit?covered(std::min(x+D,W-1),y,chOffset(frame-1)):covered(x,y,chOffset(frame-1));+ }+};+Scene makeScene(bool orbit){+ Scene s;s.orbit=orbit;s.background.resize(size_t(BW)*H);s.character.resize(size_t(W)*H);s.distance.resize(size_t(W)*H);+ for(int y=0;y<H;++y)for(int x=0;x<BW;++x){+ const float n=.5f*noise(float(x),float(y),96,1)+.3f*noise(float(x),float(y),24,2)+.2f*noise(float(x),float(y),6,3);+ const float t=float(y)/H;+ float r=110*(1-t)+86*t,g=138*(1-t)+112*t,b=168*(1-t)+70*t; // sky -> ground+ const float m=.55f+.9f*n;r*=m;g*=m;b*=m;+ if(x%283<18){r*=.35f;g*=.32f;b*=.3f;} // poles/trunks: strong vertical edges+ if((y/180)%3==1&&x%97<5){r=std::min(255.f,r+60);g=std::min(255.f,g+55);b=std::min(255.f,b+40);}+ s.background[size_t(y)*BW+x]={uint8_t(std::clamp(r,0.f,255.f)),uint8_t(std::clamp(g,0.f,255.f)),uint8_t(std::clamp(b,0.f,255.f))};+ }+ constexpr std::array<std::array<float,3>,4> base{{{205,160,130},{48,66,118},{42,42,48},{52,70,124}}};+ for(int y=0;y<H;++y)for(int x=0;x<W;++x){+ int part=0;const float d=sdf(x+.5f,y+.5f,&part);s.distance[size_t(y)*W+x]=d;+ const float a=std::clamp(.5f-d,0.f,1.f);+ if(a<=0){s.character[size_t(y)*W+x]={0,0,0,0};continue;}+ const float n=.7f+.6f*noise(float(x),float(y),8,11+part);+ const float stripe=part==1&&((x/16+y/16)%2)?18.f:0.f;+ const auto& c=base[size_t(part)];+ s.character[size_t(y)*W+x]={uint8_t(std::clamp(c[0]*n+stripe,0.f,255.f)),uint8_t(std::clamp(c[1]*n+stripe,0.f,255.f)),+ uint8_t(std::clamp(c[2]*n+stripe,0.f,255.f)),uint8_t(std::lround(a*255))};+ }+ return s;+}+bool uploadMotion(gfx::D3D12DeviceContext& ctx,gfx::CommandSlotRing& ring,ID3D12Resource* motion,ID3D12Resource* upload,+ const std::vector<uint16_t>& halves,bool first,bool common){+ // The FSR path hands registered textures back in COMMON; DLSS reads the probe as SRV.+ const auto rest=common?D3D12_RESOURCE_STATE_COMMON:D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;+ void* data=nullptr;if(FAILED(upload->Map(0,nullptr,&data)))return false;+ std::memcpy(data,halves.data(),halves.size()*2);upload->Unmap(0,nullptr);+ Status st=Status::Ok;unsigned slot=0;auto* list=ring.acquireNext(slot,st);if(!list)return false;+ D3D12_RESOURCE_BARRIER b{};b.Type=D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;+ b.Transition={motion,D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,first?D3D12_RESOURCE_STATE_COMMON:rest,D3D12_RESOURCE_STATE_COPY_DEST};+ list->ResourceBarrier(1,&b);+ D3D12_TEXTURE_COPY_LOCATION dst{},src{};dst.pResource=motion;dst.Type=D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;+ src.pResource=upload;src.Type=D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;src.PlacedFootprint.Footprint={DXGI_FORMAT_R16G16_FLOAT,UINT(W),UINT(H),1,UINT(W*4)};+ list->CopyTextureRegion(&dst,0,0,0,&src,nullptr);+ b.Transition.StateBefore=D3D12_RESOURCE_STATE_COPY_DEST;b.Transition.StateAfter=rest;+ list->ResourceBarrier(1,&b);+ return ring.submitAndSignal(slot)&&ring.waitIdle();+}+// R16G16_FLOAT readback of a graph-owned texture from its tracked state and back.+bool readFlow(gfx::D3D12DeviceContext& ctx,gfx::CommandSlotRing& ring,ID3D12Resource* tex,D3D12_RESOURCE_STATES state,std::vector<float>& out){+ auto d=tex->GetDesc();if(d.Format!=DXGI_FORMAT_R16G16_FLOAT)return false;+ D3D12_PLACED_SUBRESOURCE_FOOTPRINT fp{};UINT rows=0;UINT64 rowBytes=0,total=0;+ ctx.device()->GetCopyableFootprints(&d,0,1,0,&fp,&rows,&rowBytes,&total);+ D3D12_HEAP_PROPERTIES hp{};hp.Type=D3D12_HEAP_TYPE_READBACK;+ D3D12_RESOURCE_DESC bd{};bd.Dimension=D3D12_RESOURCE_DIMENSION_BUFFER;bd.Width=total;bd.Height=1;bd.DepthOrArraySize=1;bd.MipLevels=1;bd.SampleDesc.Count=1;bd.Layout=D3D12_TEXTURE_LAYOUT_ROW_MAJOR;+ pipeline::ComPtr<ID3D12Resource> rb;+ if(FAILED(ctx.device()->CreateCommittedResource(&hp,D3D12_HEAP_FLAG_NONE,&bd,D3D12_RESOURCE_STATE_COPY_DEST,nullptr,IID_PPV_ARGS(&rb))))return false;+ Status st=Status::Ok;unsigned slot=0;auto* list=ring.acquireNext(slot,st);if(!list)return false;+ D3D12_RESOURCE_BARRIER b{};b.Type=D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;+ b.Transition={tex,D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,state,D3D12_RESOURCE_STATE_COPY_SOURCE};+ if(state!=D3D12_RESOURCE_STATE_COPY_SOURCE)list->ResourceBarrier(1,&b);+ D3D12_TEXTURE_COPY_LOCATION src{},dst{};src.pResource=tex;src.Type=D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;+ dst.pResource=rb.Get();dst.Type=D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;dst.PlacedFootprint=fp;+ list->CopyTextureRegion(&dst,0,0,0,&src,nullptr);+ std::swap(b.Transition.StateBefore,b.Transition.StateAfter);if(state!=D3D12_RESOURCE_STATE_COPY_SOURCE)list->ResourceBarrier(1,&b);+ if(!ring.submitAndSignal(slot)||!ring.waitIdle())return false;+ uint8_t* data=nullptr;D3D12_RANGE range{0,SIZE_T(total)};if(FAILED(rb->Map(0,&range,reinterpret_cast<void**>(&data))))return false;+ out.resize(size_t(d.Width)*d.Height*2);+ for(UINT y=0;y<d.Height;++y){+ const auto* row=reinterpret_cast<const uint16_t*>(data+fp.Offset+size_t(y)*fp.Footprint.RowPitch);+ for(UINT64 i=0;i<d.Width*2;++i)out[size_t(y)*d.Width*2+i]=DirectX::PackedVector::XMConvertHalfToFloat(row[i]);+ }+ D3D12_RANGE empty{0,0};rb->Unmap(0,&empty);return true;+}+bool save(const std::filesystem::path& path,const sink::RgbaImage& image){std::error_code ec;std::filesystem::remove(path,ec);return sink::saveImage(path.wstring(),image);}+sink::RgbaImage crop(const std::vector<std::array<uint8_t,3>>& rgb,int cx,int cy,int size){+ sink::RgbaImage im;im.width=im.height=uint32_t(size);im.pixels.resize(size_t(size)*size*4);+ const int x0=std::clamp(cx-size/2,0,W-size),y0=std::clamp(cy-size/2,0,H-size);+ for(int y=0;y<size;++y)for(int x=0;x<size;++x){const auto& p=rgb[size_t(y0+y)*W+x0+x];auto* q=&im.pixels[(size_t(y)*size+x)*4];q[0]=p[0];q[1]=p[1];q[2]=p[2];q[3]=255;}+ return im;+}+struct Accumulator {+ double sum=0;uint64_t samples=0,bad=0;std::array<uint64_t,256> histogram{};+ void add(int r,int g,int b){const int m=std::max({r,g,b});sum+=r+g+b;samples+=3;bad+=m>32;++histogram[size_t(std::min(m,255))];}+ double mae()const{return samples?sum/samples:-1;}+ double badPercent()const{return samples?100.0*bad/(samples/3):-1;}+ int p95()const{uint64_t total=samples/3,seen=0;for(int i=0;i<256;++i){seen+=histogram[size_t(i)];if(total&&seen*100>=total*95)return i;}return -1;}+};+} // namespace++int wmain(int argc,wchar_t** argv){+ if(argc!=5)return 2;+ const std::filesystem::path dir=argv[1];const std::wstring backend=argv[2],sceneName=argv[3],variant=argv[4];+ const bool fsr=backend==L"fsr";if(!fsr&&backend!=L"dlss")return 2;+ const bool orbit=sceneName==L"orbit";if(!orbit&&sceneName!=L"walk")return 2;+ const int mode=variant==L"zero"?0:variant==L"product"?1:variant==L"ideal"?2:variant==L"blocky"?3:variant==L"zeroband"?4:variant==L"inverted"?5:variant==L"garbage"?6:variant==L"nrflow"?7:variant==L"nrzero"?8:-1;if(mode<0)return 2;+ std::filesystem::create_directories(dir);CoInitializeEx(nullptr,COINIT_MULTITHREADED);+ const std::string label=std::string(fsr?"fsr":"dlss")+"-"+(orbit?"orbit":"walk")+"-"+std::filesystem::path(variant).string();+ const Scene scene=makeScene(orbit);+ gfx::D3D12DeviceContext ctx;gfx::CommandSlotRing ring;Status st=Status::Ok;gfx::DeviceContextDesc dd;dd.enableDebugLayer=true;+ if(!ctx.initialize(dd,st)||!ring.initialize(ctx.device(),ctx.directQueue(),ctx.fence(),ctx.fenceEvent(),6,st))return 2;+ pipeline::EnhanceGraph graph(ctx,ring);pipeline::EnhanceGraphDesc gd;+ gd.sourceWidth=gd.workWidth=W;gd.sourceHeight=gd.workHeight=H;gd.flowWidth=FlowW;gd.flowHeight=FlowH;gd.rgbInput=true;+ gd.enableNr=false;gd.enableSr=false;gd.enableFg=true;gd.fgMultiplier=2;+ gd.frameGenerationBackend=fsr?engine::FrameGenerationBackend::Fsr:engine::FrameGenerationBackend::Dlss;+ gd.fgMotion=mode==1?engine::MotionSource::OpticalFlow:engine::MotionSource::Zero;+ if(mode>=7){gd.enableNr=true;gd.nrWidth=FlowW;gd.nrHeight=FlowH;gd.nrMotion=mode==7?engine::MotionSource::OpticalFlow:engine::MotionSource::Zero;gd.enableNvofStandalone=mode==7;}+ const bool probe=mode!=1&&mode<7;+ // EnhanceGraph::initNvof only creates flow for in-graph FG when the backend is DLSS;+ // FSR receives flow only when NR/SR request it. Run it here as NR users do.+ if(mode==1)gd.enableNvofStandalone=true;+ gd.runtimeAbsPath=runtime::localRuntimeDirectory().wstring();+ pipeline::ComPtr<ID3D12Resource> motion,upload;+ if(probe){+ motion=pipeline::makeTexture(ctx.device(),W,H,DXGI_FORMAT_R16G16_FLOAT,false);upload=pipeline::makeUploadBuffer(ctx.device(),uint64_t(W)*H*4);+ if(!motion||!upload)return 2;gd.fgMotionProbe=motion.Get();+ }+ if(!graph.initialize(gd)||!graph.createViews()){std::cerr<<"graph initialize failed"<<std::endl;return 2;}+ AVFrame* f=av_frame_alloc();if(!f)return 2;+ f->width=W;f->height=H;f->format=AV_PIX_FMT_RGBA;f->color_range=AVCOL_RANGE_JPEG;f->color_trc=AVCOL_TRC_IEC61966_2_1;f->colorspace=AVCOL_SPC_RGB;+ if(av_frame_get_buffer(f,32)<0)return 2;+ std::vector<uint16_t> halves(size_t(W)*H*2);+ Accumulator band,outside,inner,full,real,realBand,realOutside,wobbleEdge,wobbleDeep;+ sink::RgbaImage previousReal;+ // Orbit only: the character is static on screen, so any change between consecutive+ // displayed frames inside its silhouette is wobble. Edge = up to 64 px inside.+ auto wobble=[&](const sink::RgbaImage& a,const sink::RgbaImage& b){+ if(!orbit||a.pixels.size()!=b.pixels.size())return;+ for(int y=0;y<H;y+=2)for(int x=0;x<W;x+=2){const float d=scene.dist(x,y,0);if(d>-2)continue;+ const auto* p=&a.pixels[(size_t(y)*W+x)*4];const auto* q=&b.pixels[(size_t(y)*W+x)*4];+ (d>=-Band?wobbleEdge:wobbleDeep).add(std::abs(p[0]-q[0]),std::abs(p[1]-q[1]),std::abs(p[2]-q[2]));}+ };unsigned generated=0,evaluated=0;+ // Product flow diagnostics at flow resolution, inside the band.+ uint64_t flowBand=0,flowZeroed=0,flowWrong=0,flowFar=0,flowFarWrong=0;double flowBandEpe=0;+ bool ok=true;+ for(int k=0;k<N&&ok;++k){+ if(probe){+ for(int y=0;y<H;++y)for(int x=0;x<W;++x){+ std::array<float,2> v{0,0};+ if(k>0&&mode>=2){+ if(mode==3){const int bx=x/Block*Block+Block/2,by=y/Block*Block+Block/2;v=scene.ideal(std::min(bx,W-1),std::min(by,H-1),k);}+ else v=scene.ideal(x,y,k);+ if(mode==4&&(std::abs(scene.dist(x,y,scene.chOffset(k)))<=Block||scene.disoccluded(x,y,k)))v={0,0};+ if(mode==5)v={-v[0],-v[1]};+ if(mode==6)v={120,0};+ }+ halves[(size_t(y)*W+x)*2]=DirectX::PackedVector::XMConvertFloatToHalf(v[0]);+ halves[(size_t(y)*W+x)*2+1]=DirectX::PackedVector::XMConvertFloatToHalf(v[1]);+ }+ if(!uploadMotion(ctx,ring,motion.Get(),upload.Get(),halves,k==0,fsr)){ok=false;break;}+ }+ for(int y=0;y<H;++y){auto* row=f->data[0]+size_t(y)*f->linesize[0];for(int x=0;x<W;++x){const auto p=scene.pixel(x,y,k);row[x*4]=p[0];row[x*4+1]=p[1];row[x*4+2]=p[2];row[x*4+3]=255;}}+ pipeline::EnhanceGraph::FrameOutputs out;sink::RgbaImage shown;+ ok=graph.process(f,k*(1000.0/30),k==0,out,uint64_t(k+1))&&ring.waitIdle()&&graph.resolveGeneration(out)&&+ sink::readRgba8(ctx,ring,graph.videoFrameResource(out.videoSlot),shown);+ if(!ok)break;+ if(k>=Warmup)for(int y=0;y<H;y+=2)for(int x=0;x<W;x+=2){const auto p=scene.pixel(x,y,k);const auto* q=&shown.pixels[(size_t(y)*W+x)*4];+ const int r=std::abs(q[0]-p[0]),g=std::abs(q[1]-p[1]),b=std::abs(q[2]-p[2]);real.add(r,g,b);+ if(std::min(std::abs(scene.dist(x,y,scene.chOffset(k))),std::abs(scene.dist(x,y,scene.chOffset(k-1))))<=Band)realBand.add(r,g,b);else realOutside.add(r,g,b);}+ if(k==N-1&&(mode==0||mode>=7)){+ std::vector<std::array<uint8_t,3>> rgb(size_t(W)*H);for(size_t i=0;i<rgb.size();++i)rgb[i]={shown.pixels[i*4],shown.pixels[i*4+1],shown.pixels[i*4+2]};+ const int o=scene.chOffset(k);ok=save(dir/(label+"-left-real.png"),crop(rgb,1920+o-250,1050,512))&&save(dir/(label+"-right-real.png"),crop(rgb,1920+o+250,1050,512));+ if(!ok)break;}+ if(mode==1&&k>=Warmup){+ std::vector<float> flow;auto* tex=graph.flowResource();+ if(!tex||!readFlow(ctx,ring,tex,graph.diagnosticResourceState(tex),flow)){ok=false;break;}+ const auto fd=tex->GetDesc();const float scale=float(W)/float(fd.Width);+ for(UINT y=0;y<fd.Height;++y)for(UINT x=0;x<fd.Width;++x){+ const int sx=std::min(int((x+.5f)*scale),W-1),sy=std::min(int((y+.5f)*scale),H-1);+ const auto ideal=scene.ideal(sx,sy,k);const float ix=ideal[0]/scale,iy=ideal[1]/scale;+ const float vx=flow[(size_t(y)*fd.Width+x)*2],vy=flow[(size_t(y)*fd.Width+x)*2+1];+ const float epe=std::hypot(vx-ix,vy-iy);+ const bool inBand=std::min(std::abs(scene.dist(sx,sy,scene.chOffset(k))),std::abs(scene.dist(sx,sy,scene.chOffset(k-1))))<=Band;+ if(inBand){++flowBand;flowBandEpe+=epe;flowWrong+=epe>2;flowZeroed+=std::hypot(vx,vy)<1&&std::hypot(ix,iy)>=5;}+ else{++flowFar;flowFarWrong+=epe>2;}+ }+ }+ if(k==0){previousReal=shown;continue;}+ for(unsigned i=0;i<out.batch.count;++i){+ const auto& item=out.batch.frames[i];+ if(item.kind!=pipeline::FrameKind::Generated)continue;+ ++generated;+ if(item.validity!=pipeline::GenerationValidity::Valid||k<Warmup)continue;+ sink::RgbaImage gen;if(!sink::readRgba8(ctx,ring,graph.generatedFrameResource(item.lease->slot),gen)){ok=false;break;}+ if(k>Warmup){wobble(previousReal,gen);wobble(gen,shown);}+ ++evaluated;const double mid=k-.5;const int oG=scene.chOffset(mid),oA=scene.chOffset(k-1),oB=scene.chOffset(k);+ const bool keep=k==N-1;std::vector<std::array<uint8_t,3>> gtImage,genImage,errImage;+ if(keep){gtImage.resize(size_t(W)*H);genImage.resize(size_t(W)*H);errImage.resize(size_t(W)*H);}+ for(int y=0;y<H;++y)for(int x=0;x<W;++x){+ const auto t=scene.pixel(x,y,mid);const auto* q=&gen.pixels[(size_t(y)*W+x)*4];+ const int r=std::abs(q[0]-t[0]),g=std::abs(q[1]-t[1]),b=std::abs(q[2]-t[2]);+ const float nearest=std::min({std::abs(scene.dist(x,y,oG)),std::abs(scene.dist(x,y,oA)),std::abs(scene.dist(x,y,oB))});+ full.add(r,g,b);+ if(nearest<=Band)band.add(r,g,b);else if(scene.covered(x,y,oG))inner.add(r,g,b);else outside.add(r,g,b);+ if(keep){gtImage[size_t(y)*W+x]=t;genImage[size_t(y)*W+x]={q[0],q[1],q[2]};const uint8_t e=uint8_t(std::min(255,(r+g+b)*4/3));errImage[size_t(y)*W+x]={e,e,e};}+ }+ if(keep){+ // Left/right torso edges and the head, around the ground-truth silhouette.+ const std::array<std::array<int,2>,3> centers{{{1920+oG-250,1050},{1920+oG+250,1050},{1920+oG,560}}};+ const char* names[]={"left","right","head"};+ for(size_t c=0;c<centers.size()&&ok;++c){+ const std::string stem=label+"-"+names[c];+ ok=save(dir/(stem+"-gen.png"),crop(genImage,centers[c][0],centers[c][1],512))&&+ save(dir/(stem+"-err.png"),crop(errImage,centers[c][0],centers[c][1],512));+ if(ok&&mode==0)ok=save(dir/(std::string(fsr?"fsr":"dlss")+"-"+(orbit?"orbit":"walk")+"-"+names[c]+"-gt.png"),crop(gtImage,centers[c][0],centers[c][1],512));+ }+ }+ }+ previousReal=std::move(shown);+ }+ av_frame_free(&f);+ const auto m=graph.metrics();+ ok=ring.drainQueue()&&ok;graph.shutdown();+ pipeline::ComPtr<ID3D12InfoQueue> iq;unsigned errors=0;+ if(SUCCEEDED(ctx.device()->QueryInterface(IID_PPV_ARGS(&iq))))+ for(UINT64 i=0;i<iq->GetNumStoredMessages();++i){SIZE_T bytes=0;if(FAILED(iq->GetMessage(i,nullptr,&bytes)))continue;std::vector<uint8_t>b(bytes);auto* msg=reinterpret_cast<D3D12_MESSAGE*>(b.data());+ if(SUCCEEDED(iq->GetMessage(i,msg,&bytes))&&msg->Severity<=D3D12_MESSAGE_SEVERITY_ERROR){++errors;std::cerr<<msg->pDescription<<std::endl;}}+ const bool complete=evaluated==unsigned(N-Warmup);+ std::cout<<std::format("FG_MOTION_BOUNDARY {{\"label\":\"{}\",\"generated\":{},\"evaluated\":{},\"disabled\":{},\"nvof\":{},\"bandMae\":{:.4f},\"bandBadPct\":{:.4f},\"bandP95\":{},"+ "\"farMae\":{:.4f},\"farBadPct\":{:.4f},\"innerMae\":{:.4f},\"fullMae\":{:.4f},\"realMae\":{:.4f},\"realBandMae\":{:.4f},\"realOutsideMae\":{:.4f},\"wobbleEdge\":{:.4f},\"wobbleEdgeBadPct\":{:.4f},\"wobbleDeep\":{:.4f},\"flowBandEpe\":{:.4f},\"flowBandWrongPct\":{:.3f},\"flowBandZeroedPct\":{:.3f},\"flowFarWrongPct\":{:.3f},\"errors\":{},\"pass\":{}}}",+ label,generated,evaluated,m.fgDisabledFrames,m.nvofExecuteCount,band.mae(),band.badPercent(),band.p95(),outside.mae(),outside.badPercent(),inner.mae(),full.mae(),real.mae(),realBand.mae(),realOutside.mae(),wobbleEdge.mae(),wobbleEdge.badPercent(),wobbleDeep.mae(),+ flowBand?flowBandEpe/flowBand:-1.0,flowBand?100.0*flowWrong/flowBand:-1.0,flowBand?100.0*flowZeroed/flowBand:-1.0,flowFar?100.0*flowFarWrong/flowFar:-1.0,+ errors,ok&&complete&&!errors?"true":"false")<<std::endl;+ ring.shutdown();ctx.shutdown();CoUninitialize();+ return ok&&complete&&!errors?0:1;+}