XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFE.SpaceEngineers.AgentBridge

【SpaceEngineer】AI调试插件

公开
关注 0 Fork 0 Star 0
返回提交历史

SpaceEngineersModDev/XFE.SpaceEngineers.AgentBridge

Reduce hierarchy allocations and improve DXR denoising and voxel coverage

Bound voxel meshing to four workers, delay scene rescans from completion, and add variance-guided reflection filtering. Add reversible thread-affinity experiments and native engine/GPU regression checks. Record native-4K Huanyu results and research sources; overall CPU FPS remains about 29 and stalls are unresolved.

31fdcf6
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

18 个文件 +1371 -63
Modified docs/PERFORMANCE.md +6 -1
@@ -19,6 +19,9 @@ $cli = './src/XFE.SeAgent.Cli/bin/Release/net10.0/xfe-se.exe'
19 19 | `actorWorkers` | 0 | 0 保留原并行上限,否则限制本组件阶段的并行任务数(1 到逻辑处理器数);不创建新线程池。 |
20 20 | `idleRotation` | false | 对已停止、速度增量为零、没有目标角度的旋转组件省去无效矩阵写入。活动旋转、加减速和目标角度处理保留原实现。安装时校验已实测引擎方法体指纹,不支持的版本拒绝启用。 |
21 21 | `texturePriority` | false | 在贴图批次的原子最大值调用点先读取已有优先级,只在确需提高时竞争写入。保留原有两层锁、贴图生命周期、逐帧更新及计算结果;不降低贴图质量。 |
22 | `hierarchyUpdates` | false | 子网格位置传播直接使用具体集合的结构枚举器,避免在每一级递归中装箱。保留原线程、顺序、矩阵副本及全部回调;校验原方法与局部函数的 IL 指纹。 |
23 | `mainThreadL3` | false | 实验选项,需要 `preferLargestL3`。只将游戏主线程固定到大缓存核心组,其他线程保留原进程可用核心;取消时恢复原线程掩码。本机实测只绑主线程提高了模拟更新频率,却降低了渲染 FPS,最终保持关闭。 |
24 | `pinRenderWorkers` | false | 需要同时开启上面两个选项。将渲染线程与经过组件批次的原生工作线程也固定到同一 L3 组;不创建线程池。状态报告实际固定线程数与错误;本机观测到 9 个,其他引擎工作线程仍可使用其余核心。 |
22 25 | `lowLatencyGc` | false | 活跃游戏期间申请 CLR `SustainedLowLatency`;菜单/暂停时恢复之前的模式,可用物理内存低于 `max(2GiB, 总内存10%)` 时回退。尊重其他组件后续更改,不使用禁止 GC 的区域。此模式不能消除所有 GC 停顿,需实测内存与长帧。 |
23 26 | `lockFreeParallelLoops` | false | 实验选项:启用引擎自带的无互斥锁 `ForEach` 调度分支,作用范围比 `actorBatching` 大。两者叠加未在本次实测中取得额外收益,最终配置保持此项关闭。 |
24 27 | `preferLargestL3` | false | 针对 L3 容量不同的核心组,用 Windows 拓扑信息选择缓存最大的一组作为进程亲和性。保留该组内的多核、SMT 和引擎线程池,减少跨缓存组调度;不硬编码核心编号。当前只支持 x64、单处理器组、唯一较大的 L3;不满足条件时拒绝此项。必须对照测试,不能假定所有 CPU 都受益。 |
@@ -26,7 +29,7 @@ $cli = './src/XFE.SeAgent.Cli/bin/Release/net10.0/xfe-se.exe'
26 29 | `physicsWorkers` | 0 | 0 使用游戏原值;否则为 2 到逻辑处理器数量(最多 64)。使用原生 Havok 线程池配置入口,下次载入世界才生效。实际值以 `havokWorkers` 为准;更多线程未必更快。 |
27 30 | `persist` | false | 保存到 `%LOCALAPPDATA%/XFE/SpaceEngineersAgent/performance.json`,保留上一份 `.bak`。 |
28 31
29 恢复原行为:将 `renderUncapped`、`disableVsync`、`terminalAccessCache`、`actorBatching`、`lockFreeParallelLoops`、`preferLargestL3`、`idleRotation`、`texturePriority`、`lowLatencyGc` 全部设为 false,`physicsWorkers` 设为 0,选择持久化,然后重新载入世界。正常卸载插件也会移除自身补丁并恢复原渲染等待器、GC 延迟模式和任务循环选项。CPU 亲和性恢复到插件启动前的值;如果外部程序在调优后另行改变它,则尊重外部设置。与其他修改相同引擎方法的插件组合时需重新验证。
32 恢复原行为:将 `renderUncapped`、`disableVsync`、`terminalAccessCache`、`actorBatching`、`lockFreeParallelLoops`、`preferLargestL3`、`mainThreadL3`、`pinRenderWorkers`、`idleRotation`、`texturePriority`、`hierarchyUpdates`、`lowLatencyGc` 全部设为 false,`physicsWorkers` 设为 0,选择持久化,然后重新载入世界。正常卸载插件也会移除自身补丁并恢复原渲染等待器、GC 延迟模式和任务循环选项。CPU 亲和性恢复到插件启动前的值;如果外部程序在调优后另行改变它,则尊重外部设置。与其他修改相同引擎方法的插件组合时需重新验证。
30 33
31 34 权限缓存只在原版主线程脚本执行期间工作,不接管其他插件的并行脚本调度。物理与渲染继续使用引擎自身的并行任务系统;不会把任意修改方块的脚本直接扔进线程池。
32 35
@@ -60,6 +63,8 @@ Server GC 使用多个堆和专用回收线程,可能增加内存占用,需
60 63
61 64 脚本保存完整报告和同期游戏/Blender 进程 CPU 累计时间,以及可用时的 NVIDIA GPU 负载。它不会暂停其他进程。更完整的后台负载记录由具体实验补充。
62 65
66 加 `-RayTracing` 时,另在每次外部采样中记录光追启用、场景收集、快照和原生 CPU/GPU 通道状态,便于识别预热/重建区间。采样间隔约 5 秒,这些状态不能当作逐帧占空比;光追通道 GPU 时间也不是整帧 GPU 时间。
67
63 68 `renderIntervals` 是渲染线程 `BeforeDraw` 的墙钟间隔,`updateIntervals` 是插件 Update 间隔,不能混称为同一个 FPS。另附原生模拟速度、CPU 累计时间、GC 次数、窗口 `drawEnabled` 和当前 GUI 屏幕。它不是 GPU Present/显示器扫描输出追踪;菜单记录不代表大舰船性能。没有绘制或正在加载的区间不能作为正常游玩结果。
64 69
65 70 平均 FPS 为总帧数除以总时间;1% low 为最慢 1% 帧耗时均值的倒数。P50/P95/P99 使用 nearest-rank。每个序列最多保留 131072 个间隔,超出计入 `Dropped`;出现丢弃不能声称覆盖完整采样时段。
Modified docs/RAYTRACING.md +7 -3
@@ -6,9 +6,9 @@
6 6
7 7 - 刚性舰体、功能方块及子部件的局部反射、环境遮蔽、太阳接触阴影;GGX 金属/粗糙度反射,最多四次路径反弹。
8 8 - 实际 `GLASS` 材质进入加速结构,窗框仍按不透明材质处理,不再根据模型文件名把整扇窗跳过。按顺序追踪玻璃层,计算菲涅耳反射、透射颜色和斜入射吸收;阴影能穿过玻璃。次级路径使用薄平行板折射,主视图通过折射后的真实命中位置、深度校验及原生 HDR 取样实现近场折射偏移。原有玻璃污渍和背景着色保留,不重复乘透射颜色。
9 - 相机周围 192 米内的小行星/星球体素等值面参与射线。使用本机引擎的线程独立等值面生成器和 storage pin,默认 LOD 0(1 米体素采样),后台最多一个生成任务。材质取实际体素索引,以三个方向投影贴图并混合顶点材质。分块有边界采样及唯一三角形归属;体素内容/材质事件会作废旧快照并重新生成,未改写任何体素。
9 - 相机周围 320 米内的小行星/星球体素等值面参与射线。使用本机引擎的线程独立等值面生成器和 storage pin,默认 LOD 0(1 米体素采样),后台最多四个生成任务,按距离顺序发布结果。材质取实际体素索引,以三个方向投影贴图并混合顶点材质。分块有边界采样及唯一三角形归属;体素内容/材质事件会作废旧快照并重新生成,未改写任何体素。主射线最远 256 米;不再用光栅深度加 10 厘米截断体素查询。接近光栅表面的体素命中提供次级射线起点和几何法线,避免独立 LOD 深度失配;前景遮挡的玻璃仍被拒绝。
10 10 - 最多 64 个附近灯具的点光源/聚光灯进入次级命中着色。复制实际位置、方向、颜色、开关、强度及范围,再以贡献权重做一次 RIS 重要性重采样,每个命中只对选中的灯计算遮挡。这是初始采样储备,不含 ReSTIR 的时空储备复用。
11 - 不透明表面使用位置/法线校验的时域积累,加三轮扩张步长的 à-trous 空间去噪及全分辨率双边上采样。玻璃独立保存深度/透射与反射历史,重投影校验透明接收表面(包括天空背景),以 1/32 的新帧权重积累,并做深度引导的空间过滤。空间滤波结果不反馈进时域历史,避免重复模糊。
11 - 不透明表面使用位置/法线/光滑度校验的时域积累,分别记录间接光与反射亮度的一、二阶矩,按方差指导三轮 à-trous 空间过滤,再做全分辨率双边上采样。新像素用局部统计补充方差,镜面表面限制过滤范围;随机射线命中差异不再被误当成历史失效。玻璃独立保存深度/透射与反射历史,重投影校验透明接收表面(包括天空背景),以 1/32 的新帧权重积累,并做深度引导的空间过滤。空间滤波结果不反馈进时域历史,避免重复模糊。这是参考 SVGF 的有限实现,缺少完整物体运动矢量等生产级重建输入。
12 12
13 13 ## 构建与使用
14 14
@@ -46,7 +46,9 @@ $cli='./src/XFE.SeAgent.Cli/bin/Release/net10.0/xfe-se.exe'
46 46
47 47 ## 线程、缓存与诊断
48 48
49 游戏线程按时间片收集装甲 render cell、功能方块和当前变换;后台仅合并复制的装甲数据、读取纹理或执行引擎支持的体素存储读取/等值面任务,不在工作线程遍历可变游戏实体。体素任务串行受限;捕获时的材质定义读取和数组转换仍在游戏线程。时间片不能抢占一次引擎调用。
49 游戏线程按时间片收集装甲 render cell、功能方块和当前变换;后台仅合并复制的装甲数据、读取纹理或执行引擎支持的体素存储读取/等值面任务,不在工作线程遍历可变游戏实体。体素任务最多四个并行、四格前瞻,世代变化后拒绝旧结果;捕获时的材质定义读取和数组转换仍在游戏线程。时间片不能抢占一次引擎调用。
50
51 周期性场景刷新从上次收集完成后等待 15 秒;一次大型舰队收集超时不会立即触发下一轮。拓扑失效事件仍可提前重建,移动实例的变换快照继续独立更新。
50 52
51 53 约 10 Hz 的不可变快照使用三个可复用缓冲和读取租约。模型和 BLAS 缓存复用,变化时增量上传并回收过期几何;仅累计空洞过大时压缩。相机移动超过 64 米立即重建收集范围,通常拓扑周期 15 秒。方块/体素变更通过世代号拒绝过期结果;卸载世界释放缓存和本机图形资源。
52 54
@@ -77,3 +79,5 @@ VRAGE3 不能作为本插件的可替换 DLL 后端直接装入 SE1。[官方架
77 79 GPU 回读测试覆盖不透明遮挡、天空/远景保留、纹理和 GGX 屏幕外反射、时域稳定、缓存回收/压缩、DX11 状态恢复,以及玻璃透光阴影、天空背景的玻璃、主视图折射、双灯玻璃反射方差、体素三向材质、实际点光源和一次/三次反弹的差异。它不能代替游戏画面验收。
78 80
79 81 第一版数据保留在 [初版实测记录](raytracing-2026-11-17.md);玻璃/体素版的场景、性能和未确认项见 [本版实测记录](raytracing-glass-voxels-2026-11-17.md)。
82
83 方差去噪、体素覆盖与 CPU 线程对照见 [后续实测](performance-raytracing-2026-11-17-v3.md)。
Added docs/engine-research-2026-11-17.md +28 -0
@@ -0,0 +1,28 @@
1 # 本轮引擎优化的公开技术依据
2
3 本轮针对已安装 SE 1.211.017 的 CPU 热路径及本插件的 DX11/DX12 混合光追。以下资料用于理解和选择实现方法;没有移植其他商业游戏的引擎代码,也没有把所有相关技术都称为已完成。
4
5 | 一手资料 | 对本轮的具体影响 |
6 | --- | --- |
7 | [Bungie:Multithreading the Entire Destiny Engine,GDC 2015](https://www.gdcvault.com/play/1022164/Multithreading-the-Entire-) | 先明确数据归属和依赖。体素工作只接收存储与值坐标,定义读取和场景发布留在游戏线程;位置回调、空间索引及物理世界没有被直接并行改写。 |
8 | [Christian Gyrling:Parallelizing the Naughty Dog Engine Using Fibers,GDC 2015](https://media.gdcvault.com/gdc2015/presentations/Gyrling_Christian_Parallelizing_The_Naughty.pdf) | 借鉴有界任务、依赖完成后继续执行、减少调度及临时内存开销。这里保留 VRAGE 原任务系统,不在已经运行的引擎内替换成另一套 fiber 调度器。 |
9 | [Schied 等:Spatiotemporal Variance-Guided Filtering,HPG 2017](https://research.nvidia.com/labs/rtr/publication/schied2017spatiotemporal/) | 给 GI 和反射增加一、二阶亮度矩及方差权重,保留接收表面重投影验证;三个扩张步长的过滤结果不反复反馈进历史。本实现没有完整 SVGF 的全部输入和阶段。 |
10 | [Juha Sjoholm:Effectively Integrating RTX Ray Tracing into a Real-Time Rendering Engine,2018](https://developer.nvidia.com/blog/effectively-integrating-rtx-ray-tracing-real-time-rendering-engine/) | 重点核对光栅与射线几何的 LOD/深度一致性。修复过短的主射线截断与体素收集范围不一致;在局部体素命中附近从实际射线表面发射次级射线,同时保留玻璃前景深度限制。 |
11 | [Dirk Gerrit van Antwerpen:Solving Self-Intersection Artifacts in DirectX Raytracing,2023](https://developer.nvidia.com/blog/solving-self-intersection-artifacts-in-directx-raytracing/) | 区分浮点误差与几何失配。继续使用相机相对坐标,优先修正失配表面;本轮并未实现该文完整的逐三角形保守误差界,仍有固定射线偏移的限制。 |
12 | [NVIDIA RTX:Real-Time Path Tracing in Games,2026](https://github.com/NVIDIA-RTX/Whitepapers/blob/main/pt_performance_whitepaper/README.md) | 同时记录主画面和射线分辨率、GPU 射线与去噪耗时、CPU 帧时间及时间稳定性。不能把 GPU 局部通道时间或合成分辨率当作整个游戏的性能/精度。 |
13 | [Microsoft:SetThreadAffinityMask](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadaffinitymask) 与 [Processor Groups](https://learn.microsoft.com/en-us/windows/win32/procthread/processor-groups) | 区分进程可用核心与单个线程的限制。实验让主线程/渲染线程留在大缓存核心组、其他任务可使用另一组;保存和恢复原线程掩码,实际收益以交替实测为准。 |
14
15 ## 本机验证方式
16
17 - 使用已有寰宇号测试副本;开始前再次离线备份完整活动文件。原存档不参与写入。
18 - CPU 对照固定原生 3840×2160、原图形质量、相同驾驶席。关闭 DXR 隔离 CPU 改动,交替启停新增补丁,每段 60 秒;单独带探针捕获只用于定位,不能与无探针帧率直接比较。
19 - 原版位置传播和优化版本在隔离引擎测试中比较子集合、顺序、源对象、传递矩阵和参数;独立进程验证主/渲染工作线程掩码恢复。不会以降低物理频率或停掉舰船脚本换取帧数。
20 - GPU 测试使用真实 DXR、合成的已知几何与灯光、纹理回读,覆盖随机反射方差、体素深度差和玻璃遮挡。游戏画面另用插件在后台导出原生截图,分别检查玻璃与小行星场景。
21
22 ## 未采用的尝试
23
24 渲染变换的矩阵按引用访问曾通过更新/不更新索引及相机移动分支的对照测试,但实际 `UpdateMatrices` 均值从 1.17 ms 变为 1.23 ms,整体 FPS 也没有稳定收益。该候选实现已撤销,没有作为有效优化提交。
25
26 本机驱动拒绝将新建的共享 `R32G32B32A32_FLOAT` 纹理从 D3D12 打开到 D3D11(`E_INVALIDARG`)。因此没有提交这条未经支持的格式切换,位置桥接仍使用 FP16;不能声称已消除所有精度误差。体素修复依靠几何匹配与实际命中点,并保留清晰的近场范围。
27
28 物理步进、子网格位置通知和可编程方块仍是重要 CPU 开销。更多线程只有在任务足够独立、调度和缓存成本可控时才有效;最终开关以本机对照结果为准。
Added docs/performance-raytracing-2026-11-17-v3.md +74 -0
@@ -0,0 +1,74 @@
1 # 寰宇号:CPU 热路径与光追噪声、体素覆盖复测
2
3 本轮在 SE **1.211.017**、9950X3D、RTX 5090 上继续测试。光追墙面颗粒与近场体素覆盖缺口已有实机改善;CPU 的整体帧率仍未取得稳定提升,不能把本轮称为已经解决大型多子网格舰船的低帧率或全部卡顿。
4
5 使用 `XFE Performance Huanyu 20261116` 副本,开始前新增完整离线备份 `20261116T214302498-f045fcb2880e41a0a91ea403354298de`,共 16 个活动文件。原存档未参与加载或写入,其原有备份清单中的 12 个文件再次核对一致。
6
7 ## 保留的实现
8
9 - **减少位置传播的分配。** `MyPositionComponent.UpdateChildren` 使用具体集合的结构枚举器,保留原线程、递归回调、迭代顺序、矩阵副本及所有参数。补丁安装前核对外层和局部函数 IL 指纹。隔离引擎测试中,10000 次空集合更新从分配 640000 字节降至 0;这不是整体 FPS 提升的证明。
10 - **有界并行体素构建。** 同时最多 4 个存储读取/等值面构建任务,只有 4 个单元的预取窗口。定义访问、材质打包和场景发布仍留在游戏线程。场景世代变化后拒绝旧任务结果;不是把共享物理状态搬进线程池。
11 - **场景扫描刷新间隔。** 从扫描完成时开始计算下一次 15 秒刷新间隔,避免大型舰队的一次收集超过间隔后立即重复扫描。拓扑事件的失效处理和变换快照更新保留原行为。
12 - **GI 与反射去噪。** 一、二阶亮度矩估计方差,几何/法线/光泽验证后累计最多 32 帧,再执行三个步长的边缘感知过滤。随机射线命中变化不再被误当作几何遮挡变化;空间过滤结果不反馈进历史。玻璃保留独立的表面历史验证。
13 - **体素覆盖。** 收集半径从 192 m 增至 320 m,主射线最远 256 m,取消过紧的光栅深度加 0.1 m 截断。附近体素的次级射线从真实光追命中点出发;玻璃仍受不透明前景深度约束,避免延长射线后穿过前景显示玻璃。
14
15 新增的 `mainThreadL3`/`pinRenderWorkers` 是默认关闭的实验开关,用于比较主线程、渲染工作线程与其他工作线程的缓存局部性。最终继续使用原有整进程大缓存核心组及 8 个原生 Havok 工作线程,两个新线程绑定选项关闭。
16
17 ## CPU 对照与未采用的候选
18
19 同一驾驶席、原生 4K、关闭 DXR,每段 60 秒。FPS 来自渲染 `BeforeDraw` 墙钟间隔,模拟更新另列;不是 GPU Present 追踪。世界继续正常运行,视角背景随船体轻微旋转变化,没有停止脚本、降低物理频率或删除子网格。
20
21 | 对照 | 平均 FPS | 模拟更新 Hz | 结论 |
22 | --- | ---: | ---: | --- |
23 | 位置补丁关闭,两段 | 29.19 / 28.99 | 25.39 / 25.09 | 本轮对照 |
24 | 仅位置补丁开启 | 29.21 | 25.49 | 分配减少,整体帧率没有稳定收益证据 |
25 | 增加矩阵按引用访问,两段 | 29.16 / 29.25 | 25.16 / 25.25 | 未保留该候选实现 |
26 | 主线程单独绑定,对照两段 | 28.37 / 28.26 | 25.79 / 25.06 | 原进程绑定 |
27 | 主线程单独绑定,实验两段 | 23.63 / 23.36 | 27.86 / 27.75 | 更新更快,但渲染明显变慢,未启用 |
28 | 主线程加渲染线程绑定,对照两段 | 29.31 / 28.50 | 25.50 / 24.33 | 原进程绑定,第二段存在后台负载干扰 |
29 | 主线程加渲染线程绑定,实验两段 | 28.68 / 28.65 | 25.33 / 25.14 | 实际绑定 9 个渲染线程,第二段存在后台负载干扰;没有启用依据 |
30
31 渲染线程绑定的第二轮对照/实验分别有 4/1 次外部采样检测到 Blender,不能用该轮差值作因果判断。第一轮未发现 Blender,也未测到帧率提升。每段的外部进程记录及检测次数均保留在结果清单中。
32
33 带方法探针的诊断中,物理步进约 9.85 ms/调用、活动刚体回写约 6.68 ms/调用、渲染 Actor 更新约 8.45 ms/调用。位置传播 45 秒调用约 367 万次。它们包含子调用和并行等待,不可相加为独立 CPU 预算。矩阵访问候选通过了正确性检查,但被测阶段从 1.17 ms 变成 1.23 ms,已撤销。
34
35 后续光追开/关采样期间检测到多个后台 Blender 渲染进程,其 CPU 累计时间、GPU 利用率均有变化。第一次开启还包含切回舰船后的场景重建。因此保留全部原始记录,不将这一组开/关差值解释为光追开销或优化收益;重新预热后的最终记录独立报告。
36
37 修复扫描间隔并重启到最终版本后,完成以下无方法探针采样。两段的外部进程采样均未发现 Blender,主画面保持 4K、持续绘制、未暂停:
38
39 | 最终版本 | 时长 | FPS | 1% low | P95 | 最长渲染帧 | 模拟更新 | 最长更新 |
40 | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
41 | DXR 关闭 | 60 s | 28.78 | 22.95 | 38.20 ms | 49.60 ms | 26.14 Hz | 54.46 ms |
42 | DXR 开启且预热完成 | 120 s | 28.78 | 20.52 | 38.88 ms | 78.69 ms | 24.17 Hz | 150.73 ms |
43
44 开启段 23 次原生通道采样全部为 active,GPU 射线与去噪平均约 1.53 ms;该数值不含整帧光栅渲染。13 次采样处于场景收集空闲期,说明不再持续立即重扫;5 秒采样不能转换成精确占空比。当前渲染仍受 CPU 限制,DXR 的额外场景工作降低了模拟更新频率,**没有达到更高 FPS,也不能声称已消除卡顿**。
45
46 ## 原生 4K 与画面证据
47
48 主视口、后缓冲、渲染尺寸、截图均为 **3840×2160**;进程与窗口 DPI awareness 均为 2,动态分辨率关闭,HIGH 贴图/模型/阴影及 FXAA 保持原设置。**射线采样仍为 1920×1080**,双边重建后合成到原生 HDR;没有声称是逐像素 4K 路径追踪。
49
50 玻璃场景保持同一块窗户和相对机位,正常合成、关闭、诊断、重复画面分别导出。船体在前后 CPU 实验间有轻微旋转,日照变化;不能把前后整张截图当作严格同光照质量评分。最终版本额外固定暂停机位,等待 256 个 active 渲染帧后取 A,再等待 64 帧取 B;A、B 间未切换任何光追选项。
51
52 | 检查 | 修复前 | 修复后 | 范围 |
53 | --- | ---: | ---: | --- |
54 | 小行星诊断覆盖率 | 0.4833% | 100% | 固定 `[1860,260,100,120]` 区域,原阈值 99% |
55 | GPU 夹具不透明反射时间标准差 | 0.013474 | 0.001055 | 同一真实 DXR 灯光夹具,约减少 92% |
56 | GPU 夹具玻璃反射时间标准差 | 0.001906 | 0.000108 | 同一夹具 |
57
58 最终连续 A/B 帧的墙面平均绝对像素差为 **0.7602/255**,RMS 差为 **1.3016/255**,区域仍为原图 `[3180,300,500,350]`。玻璃局部 `[1100,200,900,500]` 的对应值为 0.9597 和 1.3608。早期前后截图序列穿插了关闭/诊断模式,含曝光及历史重建变化;没有把那些差值计算成实机噪声降低百分比。约 92% 的标准差降幅仅指上表独立 GPU 夹具。
59
60 已查看窗户原图全部 12 个原比例分块、对照与差异图,以及小行星前景的正常和覆盖诊断图。墙面亮色颗粒显著减少,板缝和纹理保留;玻璃仍可看见舱内座椅、植物,局部反射存在,窗框保持不透明。小行星原先大片内部覆盖孔洞消失,所查正常表面未见矩形块缝。
61
62 画面证据在本机 `work/visual/20261117-raytracing-v3/`,最终验收记录为 `window-verdict-final.json` 和 `voxel-verdict-final.json`。稳定后的墙面噪声、近场体素、玻璃透视/遮挡与原生 4K 检查通过。**玻璃刚进入场景时仍有明显亮色噪点,收敛后才减少;运动遮挡切换与反射拖影尚未完整验证**。时间稳定性项目明确保留为 `uncertain`,没有把静止截图等同于无拖影。
63
64 ## 验证与限制
65
66 Release 构建零警告、零错误;93 项通用检查、11 项本机引擎检查通过。后者覆盖位置回调、640000→0 字节分配、主/渲染线程掩码恢复等。真实 GPU 测试覆盖离屏纹理反射、间接光、多次反弹、玻璃透射/遮挡、体素材质、资源退休和互操作状态恢复;光栅表面后 0.5 m 的体素夹具覆盖为 1.0,不透明前景后的玻璃贡献为 0.0。
67
68 寰宇号主网格完整检查 33566 个方块,前后完整度 216536724.8881836、损伤总量 147847.412109375 均一致;损伤/变形/未完成方块仍为 64/30/8。检查没有使用默认截断上限。结束时恢复驾驶席相机并解除暂停,保留正常光追显示。
69
70 最终部署与构建产物 SHA-256 一致:托管插件 `DB13A7D4F53202FBFDA7B74698E807B126FAB16A9418DE6C91FD63B0FD0E6580`;原生光追 DLL `DE1BA0664431A1DFF9574D07DB5226A2626E4FDCF46D281DACC3702C539F8521`。实机截图与最终基准均对应这两个产物;原生 GPU 测试使用相同 DLL。
71
72 体素支持仍是有限近场。位置桥接纹理仍为 FP16;本机 D3D11/D3D12 共享 FP32 格式尝试返回 `E_INVALIDARG`,未采用。没有完整的次级反射运动矢量、NRD、ReSTIR、DLSS Ray Reconstruction 或完整路径追踪;固定射线偏移、快速运动和新显露区域仍可能产生残余噪声/历史误差。
73
74 公开方法与实现取舍见 [技术参考](engine-research-2026-11-17.md)。CPU 配置及撤销见 [PERFORMANCE](PERFORMANCE.md),光追范围见 [RAYTRACING](RAYTRACING.md)。本轮指标与原始记录哈希见 [结果清单](performance-raytracing-results-2026-11-17-v3.json)。不提交游戏反编译源码、存档或完整游戏资产。
Modified src/XFE.SeAgent.Plugin/Graphics/RayTracing.Voxels.cs +31 -13
@@ -15,7 +15,11 @@ namespace XFE.SeAgent.Plugin.Graphics
15 15 {
16 16 private readonly Dictionary<string,Mesh> voxelCache=new Dictionary<string,Mesh>();
17 17 private readonly HashSet<MyVoxelBase> watchedVoxels=new HashSet<MyVoxelBase>();
18 private readonly SemaphoreSlim voxelWorker=new SemaphoreSlim(1);
18 // Bounded jobs: storage reads/meshing are independent and engine mesher
19 // instances are thread-local. Definitions and scene publication stay on
20 // the game thread, in deterministic cell order.
21 private const int VoxelWorkers=4, VoxelRadius=320;
22 private readonly SemaphoreSlim voxelWorker=new SemaphoreSlim(VoxelWorkers);
19 23 private int voxelChunks,voxelTriangles,voxelRevision;
20 24
21 25 private void ResetVoxels()
@@ -34,7 +38,7 @@ namespace XFE.SeAgent.Plugin.Graphics
34 38 private IEnumerable<Placement> CollectVoxels(Vector3D camera)
35 39 {
36 40 int lod=options.VoxelLod, scale=1<<lod, cell=32, meters=cell*scale;
37 var sphere=new BoundingSphereD(camera,192);
41 var sphere=new BoundingSphereD(camera,VoxelRadius);
38 42 var roots=MyEntities.GetEntities().OfType<MyVoxelBase>().Select(v=>v.RootVoxel).Where(v=>v!=null && !v.Closed && v.PositionComp.WorldAABB.Intersects(sphere)).Distinct().ToArray();
39 43 var live=new HashSet<string>(); voxelChunks=voxelTriangles=0;
40 44 foreach(var root in roots)
@@ -43,18 +47,36 @@ namespace XFE.SeAgent.Plugin.Graphics
43 47 var storage=root.Storage; if(storage==null) continue;
44 48 var local=Vector3D.Transform(camera,root.PositionComp.WorldMatrixNormalizedInv)+root.SizeInMetresHalf+root.StorageMin;
45 49 var size=storage.Size;
46 var low=Vector3I.Max(Vector3I.Zero,Vector3I.Floor((local-new Vector3D(192))/meters));
47 var high=Vector3I.Min((size-Vector3I.One)/meters,Vector3I.Floor((local+new Vector3D(192))/meters));
50 var low=Vector3I.Max(Vector3I.Zero,Vector3I.Floor((local-new Vector3D(VoxelRadius))/meters));
51 var high=Vector3I.Min((size-Vector3I.One)/meters,Vector3I.Floor((local+new Vector3D(VoxelRadius))/meters));
48 52 var cells=new List<Vector3I>();
49 53 for(int z=low.Z;z<=high.Z;z++) for(int y=low.Y;y<=high.Y;y++) for(int x=low.X;x<=high.X;x++) {
50 54 var c=new Vector3I(x,y,z);
51 55 var box=new BoundingBoxD(c*meters,(c+Vector3I.One)*meters);
52 if(box.Intersects(new BoundingSphereD(local,192))) cells.Add(c);
56 if(box.Intersects(new BoundingSphereD(local,VoxelRadius))) cells.Add(c);
53 57 }
54 58 cells.Sort((a,b)=>Vector3D.DistanceSquared(((Vector3D)a+new Vector3D(.5))*meters,local).CompareTo(Vector3D.DistanceSquared(((Vector3D)b+new Vector3D(.5))*meters,local)));
55 59 var offset=root.SizeInMetresHalf+root.StorageMin;
56 foreach(var cellIndex in cells)
60 var pending=new Dictionary<int,Task<MyIsoMesh>>();
61 int scheduled=0, revision=Volatile.Read(ref sceneGeneration);
62 for(int cellNumber=0;cellNumber<cells.Count;cellNumber++)
57 63 {
64 // Maintain a short look-ahead window rather than scheduling
65 // thousands of tasks or awaiting a new worker for every cell.
66 while(scheduled<Math.Min(cells.Count,cellNumber+VoxelWorkers)) {
67 int index=scheduled++;var c=cells[index];
68 if(voxelCache.ContainsKey(root.EntityId+":"+lod+":"+c)) continue;
69 var min=c*cell-Vector3I.One;var max=(c+Vector3I.One)*cell+new Vector3I(2);
70 pending.Add(index,Task.Run(async()=> {
71 await voxelWorker.WaitAsync();
72 try {
73 if(Volatile.Read(ref sceneGeneration)!=revision) return null;
74 return ((IMyIsoMesher)MyDualContouringMesher.Static).Precalc(storage,lod,min,max,MyStorageDataTypeFlags.ContentAndMaterial,0);
75 }
76 finally { voxelWorker.Release(); }
77 }));
78 }
79 var cellIndex=cells[cellNumber];
58 80 string key=root.EntityId+":"+lod+":"+cellIndex;
59 81 live.Add(key);
60 82 if(!voxelCache.TryGetValue(key,out var mesh))
@@ -63,15 +85,11 @@ namespace XFE.SeAgent.Plugin.Graphics
63 85 // are the same concurrency-safe route used by engine jobs.
64 86 // The worker receives only storage and value coordinates,
65 87 // never an entity, definition manager or render collection.
66 var min=cellIndex*cell-Vector3I.One;
67 var max=(cellIndex+Vector3I.One)*cell+new Vector3I(2);
68 var task=Task.Run(async()=> {
69 await voxelWorker.WaitAsync();
70 try { return ((IMyIsoMesher)MyDualContouringMesher.Static).Precalc(storage,lod,min,max,MyStorageDataTypeFlags.ContentAndMaterial,0); }
71 finally { voxelWorker.Release(); }
72 });
88 var task=pending[cellNumber];
73 89 while(!task.IsCompleted) yield return null;
74 90 var iso=task.GetAwaiter().GetResult();
91 pending.Remove(cellNumber);
92 if(Volatile.Read(ref sceneGeneration)!=revision) yield break;
75 93 if(iso!=null && iso.VerticesCount>0 && iso.TrianglesCount>0) {
76 94 var vertices=new float[iso.VerticesCount*8];
77 95 for(int i=0;i<iso.VerticesCount;i++) {
Modified src/XFE.SeAgent.Plugin/Graphics/RayTracing.cs +12 -3
@@ -175,11 +175,12 @@ namespace XFE.SeAgent.Plugin.Graphics
175 175 ["reflections"]=o.Reflections,["indirect"]=o.Indirect,["textureFallbacks"]=textureFallbacks,
176 176 ["glass"]=o.Glass,["voxels"]=o.Voxels,["bounces"]=o.Bounces,["voxelLod"]=o.VoxelLod,
177 177 ["glassMaterials"]=glassMaterials.Count,["glassTriangles"]=glassTriangles,["voxelChunks"]=voxelChunks,["voxelTriangles"]=voxelTriangles,["voxelRevision"]=voxelRevision,
178 ["voxelCaptureRadiusMeters"]=VoxelRadius,["voxelWorkers"]=VoxelWorkers,
178 179 ["localLights"]=s?.Lights?.Length??0,
179 180 ["scannedBlocks"]=scannedBlocks, ["skippedModels"]=skippedModels, ["snapshotInstances"]=s?.Count??0,
180 181 ["snapshotAgeMs"]=s==null ? (JToken)null : (Stopwatch.GetTimestamp()-s.Created)*1000.0/Stopwatch.Frequency,
181 182 ["implementation"]="DXR 1.1 inline hardware rays over cached triangle models. Half-resolution rays, bilateral composite into native HDR before HUD.",
182 ["limits"]="Local hybrid DXR: 1..4 bounces, up to 64 point/spot fixtures via RIS, thin dielectric glass, editable voxel isosurfaces within 192 m. Glass defaults IOR 1.52 / 10 mm; primary refraction reuses depth-validated native HDR, secondary paths refract through slabs. Native transparency and approximate ambient remain. No ReSTIR reuse, NRD, DLSS RR, full-frame path tracing, solid dielectric volumes, cutouts, deformation or secondary normal maps." };
183 ["limits"]="Local hybrid DXR: 1..4 bounces, up to 64 point/spot fixtures via RIS, thin dielectric glass, editable voxel capture within 320 m / primary rays 256 m. Variance-guided GI and reflection filtering. Glass defaults IOR 1.52 / 10 mm; primary refraction reuses depth-validated native HDR, secondary paths refract through slabs. Native transparency and approximate ambient remain. No ReSTIR reuse, NRD, DLSS RR, full-frame path tracing, solid dielectric volumes, cutouts, deformation or secondary normal maps." };
183 184 }
184 185 public void Update(bool allowed)
185 186 {
@@ -210,7 +211,15 @@ namespace XFE.SeAgent.Plugin.Graphics
210 211 int steps=0;
211 212 while (steps++<512 && Stopwatch.GetTimestamp()<until)
212 213 {
213 if (!collector.MoveNext()) { collector.Dispose(); collector=null; placements=buildingPlacements;buildingPlacements=null; RetireGeometry(); break; }
214 if (!collector.MoveNext()) {
215 collector.Dispose(); collector=null; placements=buildingPlacements;buildingPlacements=null; RetireGeometry();
216 // Large fleets can take longer than the refresh interval
217 // to collect. Start that interval after completion, so a
218 // finished scan does not immediately consume the next
219 // game's frame budget rebuilding the same static topology.
220 lastCollect=Stopwatch.GetTimestamp();
221 break;
222 }
214 223 if (collector.Current!=null) buildingPlacements.Add(collector.Current);
215 224 if (buildingPlacements.Count>=100000) throw new InvalidOperationException("Visible geometry exceeded 100000 instances; refusing an incomplete scene.");
216 225 }
@@ -480,7 +489,7 @@ namespace XFE.SeAgent.Plugin.Graphics
480 489 var p=new Parameters { InverseViewProjection=(Matrix)Member(matrices,"InvViewProjectionAt0"), InverseView=(Matrix)Member(matrices,"InvViewAt0"),
481 490 OriginDelta=new Vector4((Vector3)(camera-s.Origin),0), SunDirection=new Vector4(sunVector,0), Settings=new Vector4(o.Radius,o.Strength,o.ShadowStrength,o.Diagnostic),
482 491 Width=(uint)resolution.X,Height=(uint)resolution.Y,TraceWidth=(uint)(resolution.X+1)/2,TraceHeight=(uint)(resolution.Y+1)/2,
483 SunColor=new Vector4(sunColor,0),Effects=new Vector4(o.Reflections,o.Indirect,0,0), PreviousViewProjection=previousViewProjection,
492 SunColor=new Vector4(sunColor,0),Effects=new Vector4(o.Reflections,o.Indirect,o.Voxels?2.5f*(1<<o.VoxelLod):0,0), PreviousViewProjection=previousViewProjection,
484 493 PreviousCamera=new Vector4((Vector3)(previousCamera-s.Origin),0),OriginChange=new Vector4((Vector3)(s.Origin-previousOrigin),0),
485 494 History=new Vector4(previousFrameAt!=0 && Stopwatch.GetTimestamp()-previousFrameAt<Stopwatch.Frequency/4?1:0,0,0,0),
486 495 Transport=new Vector4(o.Bounces,64,o.Glass?1:0,s.Lights.Length),ViewProjection=(Matrix)Member(matrices,"ViewProjectionAt0") };
Modified src/XFE.SeAgent.Plugin/Performance/ActorBatching.cs +2 -0
@@ -16,6 +16,7 @@ namespace XFE.SeAgent.Plugin.Performance
16 16 [ThreadStatic] private static Batch spare;
17 17 public static long Dispatches, ItemsProcessed;
18 18 internal static volatile ActorMetrics Metrics;
19 internal static volatile CacheCoreAffinity Affinity;
19 20 private sealed class Batch
20 21 {
21 22 public readonly List<MyActorComponent> Items = new List<MyActorComponent>();
@@ -26,6 +27,7 @@ namespace XFE.SeAgent.Plugin.Performance
26 27 public Batch() { Run = RunChunk; }
27 28 private void RunChunk(int chunk)
28 29 {
30 Affinity?.MarkRenderThread();
29 31 int start = chunk * Size, end = Math.Min(start + Size, Items.Count);
30 32 if (Metrics == null) { for (int i = start; i < end; i++) Action(Items[i]); }
31 33 else { for (int i = start; i < end; i++) Metrics.Measure(Items[i], Action); }
Modified src/XFE.SeAgent.Plugin/Performance/CacheCoreAffinity.cs +77 -6
@@ -3,6 +3,7 @@ using System.Collections.Generic;
3 3 using System.ComponentModel;
4 4 using System.Diagnostics;
5 5 using System.Runtime.InteropServices;
6 using System.Threading;
6 7
7 8 namespace XFE.SeAgent.Plugin.Performance
8 9 {
@@ -13,9 +14,34 @@ namespace XFE.SeAgent.Plugin.Performance
13 14 private static extern bool GetLogicalProcessorInformation(IntPtr buffer, ref uint length);
14 15 [DllImport("kernel32.dll")]
15 16 private static extern ushort GetActiveProcessorGroupCount();
17 [DllImport("kernel32.dll")]
18 private static extern uint GetCurrentThreadId();
19 [DllImport("kernel32.dll",SetLastError=true)]
20 private static extern IntPtr OpenThread(uint access,bool inherit,uint id);
21 [DllImport("kernel32.dll",SetLastError=true)]
22 private static extern UIntPtr SetThreadAffinityMask(IntPtr thread,UIntPtr mask);
23 [DllImport("kernel32.dll",SetLastError=true)]
24 private static extern bool GetThreadGroupAffinity(IntPtr thread,out GroupAffinity affinity);
25 [DllImport("kernel32.dll")]
26 private static extern bool CloseHandle(IntPtr handle);
27 [StructLayout(LayoutKind.Sequential)]
28 private struct GroupAffinity { public UIntPtr Mask; public ushort Group,Reserved0,Reserved1,Reserved2; }
16 29 private readonly ulong original = ReadMask();
17 30 private ulong applied;
31 private IntPtr mainThread;
32 private ulong mainOriginal,mainApplied;
33 private uint mainId;
34 private sealed class ThreadMask { public IntPtr Handle; public ulong Original,Applied; }
35 private readonly Dictionary<uint,ThreadMask> renderThreads=new Dictionary<uint,ThreadMask>();
36 private volatile bool pinRender;
37 private int affinityGeneration;
38 [ThreadStatic] private static CacheCoreAffinity lastOwner;
39 [ThreadStatic] private static int lastGeneration;
18 40 public bool Enabled { get; private set; }
41 public bool MainThreadOnly { get; private set; }
42 public bool PinRenderWorkers => pinRender;
43 public int PinnedRenderThreads { get { lock(renderThreads) return renderThreads.Count; } }
44 public string ThreadError { get; private set; }
19 45 public uint SelectedCacheBytes { get; private set; }
20 46 public string CurrentMask => ReadMask().ToString("X");
21 47 private struct Cache { public ulong Mask; public uint Bytes; }
@@ -29,13 +55,48 @@ namespace XFE.SeAgent.Plugin.Performance
29 55 using (var process = Process.GetCurrentProcess()) process.ProcessorAffinity = new IntPtr(unchecked((long)mask));
30 56 }
31 57
32 public void Set(bool enabled)
58 public void MarkRenderThread()
59 {
60 if(!pinRender) return;
61 int generation=Volatile.Read(ref affinityGeneration);
62 if(ReferenceEquals(lastOwner,this) && lastGeneration==generation) return;
63 lock(renderThreads) {
64 if(!pinRender) return;
65 uint id=GetCurrentThreadId();
66 if(id!=mainId && !renderThreads.ContainsKey(id)) {
67 var handle=OpenThread(0x60,false,id);
68 if(handle==IntPtr.Zero) {ThreadError="Open render thread: "+Marshal.GetLastWin32Error();return;}
69 ulong prior=SetThreadAffinityMask(handle,new UIntPtr(mainApplied)).ToUInt64();
70 if(prior==0) {ThreadError="Pin render thread: "+Marshal.GetLastWin32Error();CloseHandle(handle);return;}
71 renderThreads.Add(id,new ThreadMask {Handle=handle,Original=prior,Applied=mainApplied});
72 }
73 lastOwner=this;lastGeneration=generation;
74 }
75 }
76
77 public void Set(bool enabled,bool mainThreadOnly=false,bool renderWorkers=false)
33 78 {
34 if (enabled == Enabled) return;
79 renderWorkers=enabled && mainThreadOnly && renderWorkers;
80 if (enabled == Enabled && (!enabled || mainThreadOnly == MainThreadOnly && renderWorkers==pinRender)) return;
81 lock(renderThreads) {
82 pinRender=false;Interlocked.Increment(ref affinityGeneration);
83 foreach(var saved in renderThreads.Values) {
84 if(GetThreadGroupAffinity(saved.Handle,out var current) && current.Mask.ToUInt64()==saved.Applied)
85 SetThreadAffinityMask(saved.Handle,new UIntPtr(saved.Original));
86 CloseHandle(saved.Handle);
87 }
88 renderThreads.Clear();ThreadError=null;
89 }
90 if(mainThread!=IntPtr.Zero) {
91 if(GetThreadGroupAffinity(mainThread,out var affinity) && affinity.Mask.ToUInt64()==mainApplied)
92 SetThreadAffinityMask(mainThread,new UIntPtr(mainOriginal));
93 CloseHandle(mainThread);mainThread=IntPtr.Zero;
94 }
95 if(Enabled && applied!=original && ReadMask()==applied) WriteMask(original);
96 Enabled=false;MainThreadOnly=false;
35 97 if (!enabled)
36 98 {
37 // Do not undo an affinity change made externally after our own setting.
38 if (ReadMask() == applied) WriteMask(original);
99 // Restoration above respects externally changed process/thread masks.
39 100 Enabled = false; SelectedCacheBytes = 0; return;
40 101 }
41 102 if (IntPtr.Size != 8 || GetActiveProcessorGroupCount() != 1)
@@ -63,8 +124,18 @@ namespace XFE.SeAgent.Plugin.Performance
63 124 caches.Sort((a, b) => b.Bytes.CompareTo(a.Bytes));
64 125 if (caches.Count < 2 || caches[0].Bytes <= caches[1].Bytes)
65 126 throw new NotSupportedException("No uniquely larger L3 cache group is available; keep normal CPU scheduling.");
66 WriteMask(caches[0].Mask);
67 applied = caches[0].Mask; SelectedCacheBytes = caches[0].Bytes; Enabled = true;
127 if(mainThreadOnly) {
128 // Retain cache locality for the simulation thread while allowing
129 // existing render/Havok/jobs to run on the other CPU cores too.
130 // This method is invoked on the game thread by the command queue.
131 mainId=GetCurrentThreadId();mainThread=OpenThread(0x60,false,mainId);
132 if(mainThread==IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
133 mainApplied=caches[0].Mask;
134 mainOriginal=SetThreadAffinityMask(mainThread,new UIntPtr(mainApplied)).ToUInt64();
135 if(mainOriginal==0) {int error=Marshal.GetLastWin32Error();CloseHandle(mainThread);mainThread=IntPtr.Zero;throw new Win32Exception(error);}
136 applied=ReadMask();MainThreadOnly=true;pinRender=renderWorkers;
137 } else {WriteMask(caches[0].Mask);applied=caches[0].Mask;}
138 SelectedCacheBytes = caches[0].Bytes; Enabled = true;
68 139 }
69 140 public void Dispose() { Set(false); }
70 141 }
Added src/XFE.SeAgent.Plugin/Performance/HierarchyUpdates.cs +48 -0
@@ -0,0 +1,48 @@
1 using System;
2 using System.Security.Cryptography;
3 using HarmonyLib;
4 using VRage.Game.Components;
5 using VRageMath;
6
7 namespace XFE.SeAgent.Plugin.Performance
8 {
9 // Keep recursive notifications and iteration order on the engine's calling
10 // thread. Concrete reader enumerators avoid two interface boxes at every
11 // level of a moving ship's hierarchy. No pose, callback or update is skipped.
12 internal static class HierarchyUpdates
13 {
14 private static readonly AccessTools.FieldRef<MyPositionComponent, MyHierarchyComponentBase> Hierarchy =
15 AccessTools.FieldRefAccess<MyPositionComponent, MyHierarchyComponentBase>("m_hierarchy");
16
17 public static void ValidateEngine()
18 {
19 Verify("UpdateChildren", "6D88A7405C665579E18E3E5220E9CA78553A8F3C75C33B69D97F74BA8F73B77D");
20 Verify("<UpdateChildren>g__UpdateChildren|14_0", "DA494F182A63E46D34E41C422A1CFD0EB0BCB9693AF8406B08A40B287EFA0FAB");
21 }
22 private static void Verify(string name,string expected)
23 {
24 var body=AccessTools.DeclaredMethod(typeof(MyPositionComponent),name)?.GetMethodBody()?.GetILAsByteArray();
25 if(body==null) throw new NotSupportedException("Missing hierarchy method: "+name);
26 using(var hash=SHA256.Create())
27 if(BitConverter.ToString(hash.ComputeHash(body)).Replace("-","")!=expected)
28 throw new NotSupportedException("Unverified hierarchy engine version: "+name);
29 }
30
31 public static bool Prefix(MyPositionComponent __instance, object source, bool forceUpdateAllChildren)
32 {
33 MatrixD parent = __instance.WorldMatrixRef;
34 var hierarchy = Hierarchy(__instance);
35 if (forceUpdateAllChildren)
36 {
37 foreach (var child in hierarchy.Children)
38 child.Entity.PositionComp.UpdateWorldMatrix(ref parent, source, true, true);
39 }
40 else
41 {
42 foreach (var child in hierarchy.ChildrenNeedingWorldMatrix)
43 child.Entity.PositionComp.UpdateWorldMatrix(ref parent, source, true, false);
44 }
45 return false;
46 }
47 }
48 }
Modified src/XFE.SeAgent.Plugin/Performance/PerformanceTuning.cs +32 -2
@@ -35,13 +35,15 @@ namespace XFE.SeAgent.Plugin.Performance
35 35 private ConditionalWeakTable<MyGridTerminalSystem, AccessStamp> stamps = new ConditionalWeakTable<MyGridTerminalSystem, AccessStamp>();
36 36 private readonly List<MethodBase> cacheMethods = new List<MethodBase>();
37 37 private WaitForTargetFrameRate renderWaiter;
38 private MyRenderThread affinityRenderThread;
38 39 private bool originalRenderMaxSpeed;
39 private bool uncapped, cacheEnabled, validate, disableVsync, actorBatching, lockFreeLoops, idleRotation, texturePriority;
40 private bool uncapped, cacheEnabled, validate, disableVsync, actorBatching, lockFreeLoops, idleRotation, texturePriority, hierarchyUpdates;
40 41 private readonly bool originalLockFreeLoops = ParallelTasks.Parallel.IsMutexSlow;
41 42 private MethodBase presentMethod;
42 43 private MethodBase actorUpdateMethod;
43 44 private MethodBase rotationMethod;
44 45 private MethodBase texturePriorityMethod;
46 private MethodBase hierarchyMethod;
45 47 private int physicsWorkers;
46 48 private long generation, lookups, hits, validations, mismatches;
47 49 private string patchError;
@@ -50,6 +52,7 @@ namespace XFE.SeAgent.Plugin.Performance
50 52 public PerformanceTuning(Action<string> logger)
51 53 {
52 54 log = logger; gameThread = Thread.CurrentThread.ManagedThreadId; current = this;
55 ActorBatching.Affinity=cacheCoreAffinity;
53 56 path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "XFE", "SpaceEngineersAgent", "performance.json");
54 57 // This is the engine's supported pool sizing input; never replace a live native pool.
55 58 try
@@ -78,8 +81,11 @@ namespace XFE.SeAgent.Plugin.Performance
78 81 bool nextActorBatching = Boolean(args, "actorBatching", actorBatching);
79 82 bool nextLockFreeLoops = Boolean(args, "lockFreeParallelLoops", lockFreeLoops);
80 83 bool nextCacheCores = Boolean(args, "preferLargestL3", cacheCoreAffinity.Enabled);
84 bool nextMainThreadOnly = Boolean(args, "mainThreadL3", cacheCoreAffinity.MainThreadOnly);
85 bool nextRenderAffinity = Boolean(args, "pinRenderWorkers", cacheCoreAffinity.PinRenderWorkers);
81 86 bool nextIdleRotation = Boolean(args, "idleRotation", idleRotation);
82 87 bool nextTexturePriority = Boolean(args, "texturePriority", texturePriority);
88 bool nextHierarchy = Boolean(args, "hierarchyUpdates", hierarchyUpdates);
83 89 bool nextLowLatency = Boolean(args, "lowLatencyGc", gcLatency.Enabled);
84 90 int nextChunk = Integer(args, "actorBatchSize", ActorBatching.ChunkSize, 16, 1024);
85 91 int nextActorWorkers = Integer(args, "actorWorkers", ActorBatching.Workers, 0, Environment.ProcessorCount);
@@ -97,9 +103,10 @@ namespace XFE.SeAgent.Plugin.Performance
97 103 if (nextActorBatching != actorBatching) SetActorBatching(nextActorBatching);
98 104 if (nextIdleRotation != idleRotation) SetIdleRotation(nextIdleRotation);
99 105 if (nextTexturePriority != texturePriority) SetTexturePriority(nextTexturePriority);
106 if (nextHierarchy != hierarchyUpdates) SetHierarchyUpdates(nextHierarchy);
100 107 ActorBatching.ChunkSize = nextChunk; ActorBatching.Workers = nextActorWorkers;
101 108 gcLatency.Set(nextLowLatency);
102 cacheCoreAffinity.Set(nextCacheCores);
109 cacheCoreAffinity.Set(nextCacheCores,nextMainThreadOnly,nextRenderAffinity);
103 110 lockFreeLoops = nextLockFreeLoops;
104 111 ParallelTasks.Parallel.IsMutexSlow = lockFreeLoops || originalLockFreeLoops;
105 112 uncapped = nextUncapped; physicsWorkers = nextWorkers; validate = nextValidate;
@@ -110,8 +117,11 @@ namespace XFE.SeAgent.Plugin.Performance
110 117 ["actorBatching"] = actorBatching, ["lockFreeParallelLoops"] = lockFreeLoops,
111 118 ["idleRotation"] = idleRotation, ["actorBatchSize"] = ActorBatching.ChunkSize, ["actorWorkers"] = ActorBatching.Workers,
112 119 ["texturePriority"] = texturePriority,
120 ["hierarchyUpdates"] = hierarchyUpdates,
113 121 ["lowLatencyGc"] = gcLatency.Enabled,
114 122 ["preferLargestL3"] = cacheCoreAffinity.Enabled, ["physicsWorkers"] = physicsWorkers };
123 settings["mainThreadL3"]=cacheCoreAffinity.MainThreadOnly;
124 settings["pinRenderWorkers"]=cacheCoreAffinity.PinRenderWorkers;
115 125 Directory.CreateDirectory(Path.GetDirectoryName(path));
116 126 File.WriteAllText(path + ".tmp", settings.ToString(Formatting.Indented));
117 127 if (File.Exists(path)) File.Replace(path + ".tmp", path, path + ".bak"); else File.Move(path + ".tmp", path);
@@ -138,6 +148,11 @@ namespace XFE.SeAgent.Plugin.Performance
138 148 {
139 149 gcLatency.Update(MySession.Static != null && MySandboxGame.IsGameReady && !MySandboxGame.IsPaused);
140 150 var thread = MySandboxGame.Static?.GameRenderComponent?.RenderThread;
151 if(!ReferenceEquals(thread,affinityRenderThread)) {
152 if(affinityRenderThread!=null) affinityRenderThread.BeforeDraw-=cacheCoreAffinity.MarkRenderThread;
153 affinityRenderThread=thread;
154 if(thread!=null) thread.BeforeDraw+=cacheCoreAffinity.MarkRenderThread;
155 }
141 156 var waiter = thread == null ? null : RenderWaiter?.GetValue(thread) as WaitForTargetFrameRate;
142 157 if (!ReferenceEquals(waiter, renderWaiter))
143 158 {
@@ -155,10 +170,13 @@ namespace XFE.SeAgent.Plugin.Performance
155 170 ["actorBatching"] = actorBatching, ["lockFreeParallelLoops"] = lockFreeLoops,
156 171 ["idleRotation"] = idleRotation, ["idleRotationsSkipped"] = IdleRotation.Skipped,
157 172 ["texturePriority"] = texturePriority,
173 ["hierarchyUpdates"] = hierarchyUpdates,
158 174 ["lowLatencyGc"] = gcLatency.Enabled, ["lowLatencyGcApplied"] = gcLatency.Applied,
159 175 ["lowLatencyGcReason"] = gcLatency.Reason, ["availablePhysicalBytes"] = gcLatency.AvailableBytes,
160 176 ["actorBatchSize"] = ActorBatching.ChunkSize, ["actorWorkers"] = ActorBatching.Workers,
161 177 ["preferLargestL3"] = cacheCoreAffinity.Enabled, ["processorAffinityHex"] = cacheCoreAffinity.CurrentMask,
178 ["mainThreadL3"] = cacheCoreAffinity.MainThreadOnly,
179 ["pinRenderWorkers"] = cacheCoreAffinity.PinRenderWorkers,["pinnedRenderThreads"] = cacheCoreAffinity.PinnedRenderThreads,["threadAffinityError"] = cacheCoreAffinity.ThreadError,
162 180 ["selectedL3Bytes"] = cacheCoreAffinity.SelectedCacheBytes,
163 181 ["actorBatchesDispatched"] = Interlocked.Read(ref ActorBatching.Dispatches), ["actorComponentsProcessed"] = Interlocked.Read(ref ActorBatching.ItemsProcessed),
164 182 ["terminalAccessCache"] = cacheEnabled, ["validateCache"] = validate,
@@ -215,6 +233,16 @@ namespace XFE.SeAgent.Plugin.Performance
215 233 harmony.Patch(method, transpiler: new HarmonyMethod(typeof(TexturePriorityUpdates), nameof(TexturePriorityUpdates.ReplaceMaximum)));
216 234 texturePriorityMethod = method; texturePriority = true;
217 235 }
236 private void SetHierarchyUpdates(bool enabled)
237 {
238 if (hierarchyMethod != null) harmony.Unpatch(hierarchyMethod, HarmonyPatchType.Prefix, PatchId);
239 hierarchyMethod = null; hierarchyUpdates = false;
240 if (!enabled) return;
241 HierarchyUpdates.ValidateEngine();
242 var method = AccessTools.DeclaredMethod(typeof(VRage.Game.Components.MyPositionComponent), "UpdateChildren");
243 harmony.Patch(method, prefix: new HarmonyMethod(typeof(HierarchyUpdates), nameof(HierarchyUpdates.Prefix)));
244 hierarchyMethod = method; hierarchyUpdates = true;
245 }
218 246 private static IEnumerable<CodeInstruction> PresentWithoutVsync(IEnumerable<CodeInstruction> instructions)
219 247 {
220 248 var result = new List<CodeInstruction>(); int matches = 0;
@@ -309,6 +337,8 @@ namespace XFE.SeAgent.Plugin.Performance
309 337 }
310 338 public void Dispose()
311 339 {
340 if(affinityRenderThread!=null) affinityRenderThread.BeforeDraw-=cacheCoreAffinity.MarkRenderThread;
341 if(ReferenceEquals(ActorBatching.Affinity,cacheCoreAffinity)) ActorBatching.Affinity=null;
312 342 try { gcLatency.Dispose(); cacheCoreAffinity.Dispose(); }
313 343 finally
314 344 {
Modified src/XFE.SeRayTracing.Native/Native.cpp +27 -12
@@ -61,7 +61,7 @@ public:
61 61 ComPtr<ID3D12PipelineState> pipeline,denoisePipeline;
62 62 ComPtr<ID3D12DescriptorHeap> descriptors;
63 63 ComPtr<ID3D12Resource> tlas, scratch, instances, constants;
64 SharedTexture positions, normals, result, reflection, indirect,glassReflection,glassData,filtered,glassRefraction;
64 SharedTexture positions, normals, result, reflection, indirect,glassReflection,glassData,filtered,glassRefraction,moments,filteredReflection;
65 65 ComPtr<ID3D12Resource> glassBuffer,lightBuffer;
66 66 std::vector<RtLight> lights;
67 67 ComPtr<ID3D12QueryHeap> timer;
@@ -77,7 +77,7 @@ public:
77 77 std::vector<InstanceMaterial> instanceMaterials;
78 78 std::vector<ComPtr<ID3D12Resource>> textureResources;
79 79 ComPtr<ID3D12Resource> vertexBuffer,triangleBuffer,instanceMaterialBuffer;
80 ComPtr<ID3D12Resource> historyTextures[6];
80 ComPtr<ID3D12Resource> historyTextures[7];
81 81 bool historyValid=false;
82 82 bool geometryDirty=true;
83 83 size_t uploadedVertices=0,uploadedTriangles=0;
@@ -139,10 +139,10 @@ public:
139 139 Check(device11->CreateDeviceContextState(0, &feature, 1, D3D11_SDK_VERSION, __uuidof(ID3D11Device), nullptr, &isolatedState), "Isolated D3D11 state");
140 140 D3D12_DESCRIPTOR_RANGE ranges[5]{};
141 141 ranges[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; ranges[0].NumDescriptors = 2; ranges[0].BaseShaderRegister = 1;
142 ranges[1].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV; ranges[1].NumDescriptors = 7;
142 ranges[1].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV; ranges[1].NumDescriptors = 9;
143 143 ranges[2].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; ranges[2].NumDescriptors = 1024; ranges[2].BaseShaderRegister=10;
144 144 ranges[3].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; ranges[3].NumDescriptors = 4; ranges[3].BaseShaderRegister=6;
145 ranges[4].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; ranges[4].NumDescriptors = 2; ranges[4].BaseShaderRegister=1036;
145 ranges[4].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; ranges[4].NumDescriptors = 3; ranges[4].BaseShaderRegister=1036;
146 146 D3D12_ROOT_PARAMETER parameters[13]{};
147 147 parameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;
148 148 parameters[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
@@ -162,7 +162,7 @@ public:
162 162 D3D12_COMPUTE_PIPELINE_STATE_DESC ps{}; ps.pRootSignature = root.Get(); ps.CS = { RaysBytes, sizeof(RaysBytes) };
163 163 Check(device->CreateComputePipelineState(&ps, IID_PPV_ARGS(&pipeline)), "DXR inline ray query pipeline");
164 164 ps.CS={DenoiseBytes,sizeof(DenoiseBytes)}; Check(device->CreateComputePipelineState(&ps,IID_PPV_ARGS(&denoisePipeline)),"Edge-aware atrous denoiser");
165 D3D12_DESCRIPTOR_HEAP_DESC heap{}; heap.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; heap.NumDescriptors = 1039; heap.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
165 D3D12_DESCRIPTOR_HEAP_DESC heap{}; heap.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; heap.NumDescriptors = 1042; heap.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
166 166 Check(device->CreateDescriptorHeap(&heap, IID_PPV_ARGS(&descriptors)), "Descriptors");
167 167 constants = Buffer(Align(sizeof(RtParameters)), D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ);
168 168 glassBuffer=Buffer(1024*sizeof(RtGlassMaterial),D3D12_HEAP_TYPE_UPLOAD,D3D12_RESOURCE_STATE_GENERIC_READ);
@@ -216,6 +216,8 @@ public:
216 216 indirect = Texture(DXGI_FORMAT_R16G16B16A16_FLOAT);
217 217 glassReflection=Texture(DXGI_FORMAT_R16G16B16A16_FLOAT); glassData=Texture(DXGI_FORMAT_R16G16B16A16_FLOAT); filtered=Texture(DXGI_FORMAT_R16G16B16A16_FLOAT);
218 218 glassRefraction=Texture(DXGI_FORMAT_R16G16B16A16_FLOAT);
219 moments=Texture(DXGI_FORMAT_R16G16B16A16_FLOAT);
220 filteredReflection=Texture(DXGI_FORMAT_R16G16B16A16_FLOAT);
219 221 auto handle = descriptors->GetCPUDescriptorHandleForHeapStart();
220 222 auto step = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
221 223 D3D12_SHADER_RESOURCE_VIEW_DESC srv{}; srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv.Texture2D.MipLevels = 1;
@@ -229,8 +231,10 @@ public:
229 231 handle.ptr+=step; device->CreateUnorderedAccessView(glassData.resource.Get(),nullptr,&uav,handle);
230 232 handle.ptr+=step; device->CreateUnorderedAccessView(filtered.resource.Get(),nullptr,&uav,handle);
231 233 handle.ptr+=step; device->CreateUnorderedAccessView(glassRefraction.resource.Get(),nullptr,&uav,handle);
234 handle.ptr+=step; device->CreateUnorderedAccessView(moments.resource.Get(),nullptr,&uav,handle);
235 handle.ptr+=step; device->CreateUnorderedAccessView(filteredReflection.resource.Get(),nullptr,&uav,handle);
232 236 historyValid=false;
233 for(int i=0;i<6;i++) {
237 for(int i=0;i<7;i++) {
234 238 historyTextures[i].Reset(); D3D12_HEAP_PROPERTIES props{}; props.Type=D3D12_HEAP_TYPE_DEFAULT;
235 239 auto desc=positions.resource->GetDesc(); desc.Flags=D3D12_RESOURCE_FLAG_NONE;
236 240 Check(device->CreateCommittedResource(&props,D3D12_HEAP_FLAG_NONE,&desc,D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,nullptr,IID_PPV_ARGS(&historyTextures[i])),"Radiance history texture");
@@ -335,7 +339,7 @@ public:
335 339 upload->Unmap(0,nullptr); Barrier(texture.Get(),D3D12_RESOURCE_STATE_COPY_DEST,D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
336 340 D3D12_SHADER_RESOURCE_VIEW_DESC view{}; view.Format=d.Format; view.ViewDimension=D3D12_SRV_DIMENSION_TEXTURE2D; view.Shader4ComponentMapping=D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; view.Texture2D.MipLevels=m.mips;
337 341 auto start=descriptors->GetCPUDescriptorHandleForHeapStart(); auto step=device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
338 start.ptr+=15*step; auto handle=start; handle.ptr+=id*step; device->CreateShaderResourceView(texture.Get(),&view,handle);
342 start.ptr+=18*step; auto handle=start; handle.ptr+=id*step; device->CreateShaderResourceView(texture.Get(),&view,handle);
339 343 if(id==0) for(UINT i=1;i<1024;i++) { auto dst=start; dst.ptr+=i*step; device->CopyDescriptorsSimple(1,dst,start,D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); }
340 344 materialBytes+=m.data.size(); textureResources.push_back(texture); transient.push_back(upload); m.data.clear(); m.data.shrink_to_fit(); m.built=true;
341 345 }
@@ -419,12 +423,13 @@ public:
419 423 auto gpu = descriptors->GetGPUDescriptorHandleForHeapStart(); list->SetComputeRootDescriptorTable(2, gpu);
420 424 gpu.ptr += 2*device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); list->SetComputeRootDescriptorTable(3, gpu);
421 425 list->SetComputeRootShaderResourceView(4,vertexBuffer->GetGPUVirtualAddress()); list->SetComputeRootShaderResourceView(5,triangleBuffer->GetGPUVirtualAddress()); list->SetComputeRootShaderResourceView(6,instanceMaterialBuffer->GetGPUVirtualAddress());
422 gpu.ptr+=7*device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); list->SetComputeRootDescriptorTable(8,gpu);
426 gpu.ptr+=9*device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); list->SetComputeRootDescriptorTable(8,gpu);
423 427 gpu.ptr+=4*device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); list->SetComputeRootDescriptorTable(12,gpu);
424 gpu.ptr+=2*device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); list->SetComputeRootDescriptorTable(7,gpu);
428 gpu.ptr+=3*device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); list->SetComputeRootDescriptorTable(7,gpu);
425 429 list->SetComputeRootShaderResourceView(9,glassBuffer->GetGPUVirtualAddress());
426 430 list->SetComputeRootShaderResourceView(11,lightBuffer->GetGPUVirtualAddress());
427 431 list->EndQuery(timer.Get(),D3D12_QUERY_TYPE_TIMESTAMP,0);
432 Barrier(moments.resource.Get(),D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
428 433 list->Dispatch((width+7)/8, (height+7)/8, 1);
429 434 Barrier(result.resource.Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON);
430 435 Barrier(reflection.resource.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS,D3D12_RESOURCE_STATE_COMMON);
@@ -432,10 +437,11 @@ public:
432 437 Barrier(glassReflection.resource.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS,D3D12_RESOURCE_STATE_COMMON);
433 438 Barrier(glassData.resource.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS,D3D12_RESOURCE_STATE_COMMON);
434 439 Barrier(glassRefraction.resource.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS,D3D12_RESOURCE_STATE_COMMON);
440 Barrier(moments.resource.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS,D3D12_RESOURCE_STATE_COMMON);
435 441 Barrier(positions.resource.Get(), D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COMMON);
436 442 Barrier(normals.resource.Get(), D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COMMON);
437 ID3D12Resource* sources[]={positions.resource.Get(),normals.resource.Get(),reflection.resource.Get(),indirect.resource.Get(),glassData.resource.Get(),glassReflection.resource.Get()};
438 for(int i=0;i<6;i++) {
443 ID3D12Resource* sources[]={positions.resource.Get(),normals.resource.Get(),reflection.resource.Get(),indirect.resource.Get(),glassData.resource.Get(),glassReflection.resource.Get(),moments.resource.Get()};
444 for(int i=0;i<7;i++) {
439 445 Barrier(sources[i],D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_COPY_SOURCE);
440 446 Barrier(historyTextures[i].Get(),D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,D3D12_RESOURCE_STATE_COPY_DEST);
441 447 list->CopyResource(historyTextures[i].Get(),sources[i]);
@@ -445,13 +451,16 @@ public:
445 451 // Keep temporal history unfiltered: feeding a spatial blur back into
446 452 // accumulation progressively destroys detail. Three expanding passes
447 453 // operate on this frame only, with geometry and radiance edge weights.
448 if(params.effects[1]>0) {
454 if(params.effects[0]>0 || params.effects[1]>0) {
449 455 list->SetPipelineState(denoisePipeline.Get());
450 456 Barrier(positions.resource.Get(),D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
451 457 Barrier(normals.resource.Get(),D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
458 Barrier(moments.resource.Get(),D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
452 459 for(UINT step=1;step<=4;step*=2) {
453 460 Barrier(indirect.resource.Get(),D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
454 461 Barrier(filtered.resource.Get(),D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
462 Barrier(reflection.resource.Get(),D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
463 Barrier(filteredReflection.resource.Get(),D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
455 464 list->SetComputeRoot32BitConstant(10,step,0);
456 465 list->Dispatch((width+7)/8,(height+7)/8,1);
457 466 Barrier(filtered.resource.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS,D3D12_RESOURCE_STATE_COPY_SOURCE);
@@ -459,9 +468,15 @@ public:
459 468 list->CopyResource(indirect.resource.Get(),filtered.resource.Get());
460 469 Barrier(filtered.resource.Get(),D3D12_RESOURCE_STATE_COPY_SOURCE,D3D12_RESOURCE_STATE_COMMON);
461 470 Barrier(indirect.resource.Get(),D3D12_RESOURCE_STATE_COPY_DEST,D3D12_RESOURCE_STATE_COMMON);
471 Barrier(filteredReflection.resource.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS,D3D12_RESOURCE_STATE_COPY_SOURCE);
472 Barrier(reflection.resource.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS,D3D12_RESOURCE_STATE_COPY_DEST);
473 list->CopyResource(reflection.resource.Get(),filteredReflection.resource.Get());
474 Barrier(filteredReflection.resource.Get(),D3D12_RESOURCE_STATE_COPY_SOURCE,D3D12_RESOURCE_STATE_COMMON);
475 Barrier(reflection.resource.Get(),D3D12_RESOURCE_STATE_COPY_DEST,D3D12_RESOURCE_STATE_COMMON);
462 476 }
463 477 Barrier(positions.resource.Get(),D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,D3D12_RESOURCE_STATE_COMMON);
464 478 Barrier(normals.resource.Get(),D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,D3D12_RESOURCE_STATE_COMMON);
479 Barrier(moments.resource.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS,D3D12_RESOURCE_STATE_COMMON);
465 480 }
466 481 list->EndQuery(timer.Get(),D3D12_QUERY_TYPE_TIMESTAMP,1);
467 482 list->ResolveQueryData(timer.Get(),D3D12_QUERY_TYPE_TIMESTAMP,0,2,timerReadback.Get(),0);timerReady=true;
Modified src/XFE.SeRayTracing.Native/Rays.hlsl +57 -22
@@ -9,6 +9,8 @@ RWTexture2D<float4> glassReflected : register(u3);
9 9 RWTexture2D<float4> glassData : register(u4);
10 10 RWTexture2D<float4> filtered : register(u5);
11 11 RWTexture2D<float4> glassRefracted : register(u6);
12 RWTexture2D<float4> moments : register(u7);
13 RWTexture2D<float4> filteredReflection : register(u8);
12 14 struct Vertex { float3 position; float2 uv; float3 tint; };
13 15 struct InstanceMaterial { uint triangleOffset; float3 tint; };
14 16 struct GlassMaterial { float4 transmission, optics; };
@@ -25,6 +27,7 @@ struct Light { float4 positionRange,colorIntensity,directionCone,settings; };
25 27 StructuredBuffer<Light> lights : register(t1035);
26 28 Texture2D<float4> previousGlassData : register(t1036);
27 29 Texture2D<float4> previousGlassReflections : register(t1037);
30 Texture2D<float4> previousMoments : register(t1038);
28 31 SamplerState linearWrap : register(s0);
29 32 cbuffer Parameters : register(b0) {
30 33 row_major float4x4 inverseViewProjection;
@@ -201,19 +204,28 @@ void Trace(uint3 id:SV_DispatchThreadID) {
201 204 if(any(id.xy>=sizes.zw)) return;
202 205 float4 p=positions[id.xy];
203 206 uint rng=id.x*1973+id.y*9277+(uint)history.y*26699+89173;
204 result[id.xy]=1;reflected[id.xy]=0;indirect[id.xy]=0;glassReflected[id.xy]=0;glassData[id.xy]=0;glassRefracted[id.xy]=0;
207 result[id.xy]=1;reflected[id.xy]=0;indirect[id.xy]=0;glassReflected[id.xy]=0;glassData[id.xy]=0;glassRefracted[id.xy]=0;moments[id.xy]=0;
208 float3 surface=p.xyz, surfaceNormal=normalize(normals[id.xy].xyz);
205 209 // Transparent surfaces have no opaque G-buffer entry. Trace camera rays
206 210 // independently, including windows backed by sky or distant terrain.
207 if(transport.z>0 || settings.w==6) {
211 if(transport.z>0 || settings.w==6 || effects.z>0) {
208 212 float2 uv=(float2(id.xy*2+1)+.5)/sizes.xy;
209 213 float4 nearPoint=mul(float4(uv.x*2-1,1-uv.y*2,1,1),inverseViewProjection);
210 214 float3 viewRay=p.w>0?normalize(p.xyz-originDelta.xyz):normalize(nearPoint.xyz/nearPoint.w);
211 float range=p.w>0?min(length(p.xyz-originDelta.xyz)+.1,250):250;
215 // Raster voxels and the independently meshed RT surface need not have
216 // identical depth. A G-buffer depth cutoff produced holes in coverage.
217 float opaqueDistance=p.w>0?length(p.xyz-originDelta.xyz):1e20;
218 float range=256;
212 219 Hit front;
213 220 if(Closest(originDelta.xyz,viewRay,range,front,false)) {
214 221 if(front.flags&VOXEL) result[id.xy].z=1;
215 222 else result[id.xy].z=0;
216 if((front.flags&GLASS) && transport.z>0) {
223 if((front.flags&VOXEL) && p.w>0 && abs(front.t-opaqueDistance)<effects.z) {
224 surface=front.p;surfaceNormal=front.n;
225 }
226 // Extending a camera ray must not composite glass behind opaque
227 // foreground geometry. Only voxel secondary-ray origins are repaired.
228 if((front.flags&GLASS) && transport.z>0 && front.t<=opaqueDistance+.02) {
217 229 GlassMaterial g=glassMaterials[front.glass];
218 230 uint glassRng=rng;
219 231 float3 reflectedDirection=normalize(reflect(viewRay,front.n)+Cosine(front.n,glassRng)*g.transmission.w*g.transmission.w);
@@ -259,56 +271,79 @@ void Trace(uint3 id:SV_DispatchThreadID) {
259 271 } else result[id.xy].z=0;
260 272 }
261 273 if(p.w==0) return;
262 float3 n=normalize(normals[id.xy].xyz),t,b;Basis(n,t,b);
274 float3 n=surfaceNormal,t,b;Basis(n,t,b);
263 275 uint stable=id.x*1973+id.y*9277+89173;float ao=0;
264 [unroll] for(uint i=0;i<4;i++) ao+=Luma(Visibility(p.xyz+n*.04,Cosine(n,stable),settings.x));
265 float shadow=dot(n,sunDirection.xyz)>0?Luma(Visibility(p.xyz+n*.04,normalize(sunDirection.xyz),192)):1;
276 [unroll] for(uint i=0;i<4;i++) ao+=Luma(Visibility(surface+n*.04,Cosine(n,stable),settings.x));
277 float shadow=dot(n,sunDirection.xyz)>0?Luma(Visibility(surface+n*.04,normalize(sunDirection.xyz),192)):1;
266 278 float voxel=result[id.xy].z;result[id.xy]=float4(ao*.25,shadow,voxel,1);
267 279 float3 incoming=normalize(p.xyz-originDelta.xyz);
268 280 float roughness=pow(1-normals[id.xy].w,2);
269 281 float3 rd=normalize(reflect(incoming,n)+Cosine(n,rng)*roughness*.35);
270 float4 reflection=effects.x>0 && dot(rd,n)>0?PathRadiance(p.xyz+n*.04,rd,192,rng):0;
282 float4 reflection=effects.x>0 && dot(rd,n)>0?PathRadiance(surface+n*.04,rd,192,rng):0;
271 283 float4 bounce=0;
272 284 if(effects.y>0) {
273 [unroll] for(uint j=0;j<2;j++) bounce.rgb+=PathRadiance(p.xyz+n*.04,Cosine(n,rng),max(transport.y,12),rng).rgb*.5;
285 [unroll] for(uint j=0;j<2;j++) bounce.rgb+=PathRadiance(surface+n*.04,Cosine(n,rng),max(transport.y,12),rng).rgb*.5;
274 286 }
275 287 bounce.a=1;
288 float giLuma=Luma(bounce.rgb),specLuma=Luma(reflection.rgb);
289 float4 sampleMoments=float4(giLuma,giLuma*giLuma,specLuma,specLuma*specLuma);
276 290 if(history.x>0) {
277 291 float4 clip=mul(float4(p.xyz-previousCamera.xyz,1),previousViewProjection);
278 292 float2 uv=float2(clip.x/clip.w*.5+.5,.5-clip.y/clip.w*.5);
279 293 if(clip.w>0 && all(uv>=0) && all(uv<1)) {
280 294 uint2 q=min(uint2(uv*sizes.zw),sizes.zw-1);
281 295 float4 oldPosition=previousPositions[q];float error=length(oldPosition.xyz-(p.xyz+originChange.xyz));
282 if(oldPosition.w>0 && error<.12 && dot(n,previousNormals[q].xyz)>.98) {
296 if(oldPosition.w>0 && error<.12 && dot(normals[id.xy].xyz,previousNormals[q].xyz)>.98 && abs(normals[id.xy].w-previousNormals[q].w)<.05) {
283 297 float4 oldBounce=previousIndirect[q];
284 // React faster to changed illumination; preserve stationary low
285 // sample radiance without clipping physically bright surfaces.
286 float change=abs(Luma(oldBounce.rgb)-Luma(bounce.rgb));
287 bounce.a=min(oldBounce.a+1,change>.15?4:32);
298 // A different Monte Carlo hit is not a disocclusion. Validate
299 // receiver geometry, then accumulate radiance and its moments.
300 bounce.a=min(oldBounce.a+1,32);
288 301 bounce.rgb=lerp(bounce.rgb,oldBounce.rgb,1-1/bounce.a);
289 302 float4 oldReflection=previousReflections[q];
290 if(abs(reflection.a-oldReflection.a)<.1) reflection=lerp(reflection,oldReflection,.75);
303 reflection=lerp(reflection,oldReflection,1-1/bounce.a);
304 sampleMoments=lerp(sampleMoments,previousMoments[q],1-1/bounce.a);
291 305 }
292 306 }
293 307 }
294 reflected[id.xy]=reflection;indirect[id.xy]=bounce;
308 reflected[id.xy]=reflection;indirect[id.xy]=bounce;moments[id.xy]=sampleMoments;
295 309 }
296 310 [numthreads(8,8,1)]
297 311 void Denoise(uint3 id:SV_DispatchThreadID) {
298 312 if(any(id.xy>=sizes.zw)) return;
299 313 float4 center=indirect[id.xy],p=positions[id.xy];
300 if(p.w==0) {filtered[id.xy]=center;return;}
301 float3 n=normals[id.xy].xyz;float3 sum=0;float total=0;
314 float4 spec=reflected[id.xy];
315 if(p.w==0) {filtered[id.xy]=center;filteredReflection[id.xy]=spec;return;}
316 float3 n=normals[id.xy].xyz;float3 sum=0;float total=0;float4 specSum=0;float specTotal=0;
317 float4 m=moments[id.xy];
318 float2 variance=max(float2(m.y-m.x*m.x,m.w-m.z*m.z),0);
319 // A fresh receiver has no temporal variance yet. Bootstrap from the local
320 // radiance distribution; subsequent frames use accumulated first/second moments.
321 if(center.a<4) {
322 float4 local=0;float count=0;
323 [unroll] for(int yy=-1;yy<=1;yy++) [unroll] for(int xx=-1;xx<=1;xx++) {
324 int2 q=clamp(int2(id.xy)+int2(xx,yy),0,int2(sizes.zw)-1);
325 if(positions[q].w==0 || dot(n,normals[q].xyz)<.98) continue;
326 float a=Luma(indirect[q].rgb),b=Luma(reflected[q].rgb);
327 local+=float4(a,a*a,b,b*b);count++;
328 }
329 local/=max(count,1);variance=max(variance,float2(local.y-local.x*local.x,local.w-local.z*local.z));
330 }
302 331 [unroll] for(int y=-1;y<=1;y++) [unroll] for(int x=-1;x<=1;x++) {
303 332 int2 q=clamp(int2(id.xy)+int2(x,y)*(int)filterStep,0,int2(sizes.zw)-1);
304 333 float4 other=positions[q],value=indirect[q];
305 334 float distance=abs(dot(other.xyz-p.xyz,n));
306 335 float geometry=other.w*exp(-distance*20)*pow(saturate(dot(n,normals[q].xyz)),32);
307 // A noisy radiance outlier is not a geometry edge. Wide luminance weights
308 // let the position/normal guide remove fireflies instead of preserving them.
309 float radiance=exp(-abs(Luma(value.rgb)-Luma(center.rgb))/(.08+.6*max(Luma(value.rgb),Luma(center.rgb))));
310 float weight=geometry*radiance*(x==0?2:1)*(y==0?2:1);
336 float kernel=(x==0?2:1)*(y==0?2:1);
337 float radiance=exp(-abs(Luma(value.rgb)-Luma(center.rgb))/(.02+4*sqrt(variance.x)));
338 float weight=geometry*radiance*kernel;
311 339 sum+=value.rgb*weight;total+=weight;
340 float4 otherSpec=reflected[q];
341 float specWeight=geometry*kernel*exp(-abs(Luma(otherSpec.rgb)-Luma(spec.rgb))/(.02+4*sqrt(variance.y)))
342 *exp(-abs(normals[q].w-normals[id.xy].w)*32);
343 // Tight specular lobes keep the finest spatial footprint.
344 if(normals[id.xy].w>.9 && filterStep>1 && (x!=0 || y!=0)) specWeight=0;
345 specSum+=otherSpec*specWeight;specTotal+=specWeight;
312 346 }
313 347 filtered[id.xy]=float4(sum/max(total,1e-5),center.a);
348 filteredReflection[id.xy]=specSum/max(specTotal,1e-5);
314 349 }
Modified tests/XFE.SeAgent.EngineTests/Program.cs +101 -0
Modified tests/XFE.SeAgent.EngineTests/README.md +4 -0
Modified tests/XFE.SeAgent.EngineTests/XFE.SeAgent.EngineTests.csproj +2 -0
Modified tests/XFE.SeRayTracing.Tests/GpuTest.cpp +37 -0
Modified tools/Measure-Performance.ps1 +3 -1