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

XFE.SpaceEngineers.AgentBridge

【SpaceEngineer】AI调试插件

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

SpaceEngineersModDev/XFE.SpaceEngineers.AgentBridge

Fix cockpit glass reflections of live LCDs and emissive materials

4bc6f6b
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

13 个文件 +929 -36
Modified docs/RAYTRACING.md +6 -0
@@ -5,6 +5,7 @@
5 5 ## 本版支持
6 6
7 7 - 刚性舰体、功能方块及子部件的局部反射、环境遮蔽、太阳接触阴影;GGX 金属/粗糙度反射,最多四次路径反弹。
8 - `MESH` 材质的自发光贴图参与次级光路;扩展贴图按线性数据读取,缺失时使用无发光回退。第一人称驾驶舱采集实际内部模型,LCD 从引擎当前纹理直接在 GPU 上复制到共享纹理,屏幕内容、颜色、发光强度及可见性用于玻璃和其他表面的反射。最多 32 块屏幕、64 米范围、10 Hz 更新;当前驾驶舱优先。单块纹理不超过 2048×2048,总共享纹理预算 128 MiB。屏幕空间 HUD 不进入几何反射。
8 9 - 实际 `GLASS` 材质进入加速结构,窗框仍按不透明材质处理,不再根据模型文件名把整扇窗跳过。按顺序追踪玻璃层,计算菲涅耳反射、透射颜色和斜入射吸收;阴影能穿过玻璃。次级路径使用薄平行板折射,主视图通过折射后的真实命中位置、深度校验及原生 HDR 取样实现近场折射偏移。原有玻璃污渍和背景着色保留,不重复乘透射颜色。
9 10 - 相机周围 320 米内的小行星/星球体素等值面参与射线。使用本机引擎的线程独立等值面生成器和 storage pin,默认 LOD 0(1 米体素采样),后台最多四个生成任务,按距离顺序发布结果。材质取实际体素索引,以三个方向投影贴图并混合顶点材质。分块有边界采样及唯一三角形归属;体素内容/材质事件会作废旧快照并重新生成,未改写任何体素。主射线最远 256 米;不再用光栅深度加 10 厘米截断体素查询。接近光栅表面的体素命中提供次级射线起点和几何法线,避免独立 LOD 深度失配;前景遮挡的玻璃仍被拒绝。
10 11 - 最多 64 个附近灯具的点光源/聚光灯进入次级命中着色。复制实际位置、方向、颜色、开关、强度及范围,再以贡献权重做一次 RIS 重要性重采样,每个命中只对选中的灯计算遮挡。这是初始采样储备,不含 ReSTIR 的时空储备复用。
@@ -56,6 +57,8 @@ DX11/DX12 同适配器共享纹理,以 GPU fence 同步,临时隔离并恢
56 57
57 58 `graphics.rayTracing.status` 返回实际 DXR 层级、几何/纹理/玻璃/体素/灯具数量、缓存年龄和错误。`render.gpuTraceDenoiseMs` 由 D3D12 timestamp query 测量上一帧的射线、历史复制和去噪,**不包含**几何构建、DX11 pack/合成及整个游戏 GPU 开销;`cpuSubmitMs` 还可能包含同步等待,不能冒充 GPU 时间。
58 59
60 `screens` 区分成功复制的纹理、不可用纹理与隐藏屏幕,逐项列出材质、渲染对象和源纹理名称。`graphics.rayTracing.inspect` 的 `interiorCockpit` 可核对当前第一人称内部模型及屏幕网格。超出屏幕数量/纹理预算时保留原材质回退,不宣称已覆盖全部 LCD。
61
59 62 `graphics.rayTracing.inspect` 可按 `model` 子串查看最多 32 个玻璃放置对象、实际三角形表面中心/法线,以及最近 64 个体素根实体的范围。它是读接口,信息取自当前快照,场景未准备好时可能为空。`debug.camera` 可在授权副本中设置并恢复固定观察镜头,便于同场景对照,不移动舰船或角色,详见 [API](API.md)。
60 63
61 64 ## 公开技术依据及边界
@@ -71,6 +74,7 @@ VRAGE3 不能作为本插件的可替换 DLL 后端直接装入 SE1。[官方架
71 74 - 只处理近场刚性模型和有限范围体素。未处理蒙皮装甲、alpha cutout、完整行星远景几何、运动矢量及次级法线贴图。体素采样 LOD 和原版地形 LOD 可能有差别,默认优先精度。
72 75 - 玻璃是薄板模型;IOR 1.52、厚度 10 mm 是明确的默认假设,不是引擎提供的物理参数。没有封闭介质体积、焦散或完整双面材质模型。主视图折射需要有效的屏幕内背景命中,失配时保留原版背景;次级路径使用几何折射。原版透明着色仍保留,不能称为完整替换。
73 76 - 接触阴影是对已有光照的近似混合;环境反射在未命中时保留原版。次级光照保留小幅近似环境项,不是物理校准的全场景参考积分器。灯具候选上限 64,尚未覆盖所有粒子、武器、自发光面光源及完整反射器纹理。
77 - LCD 与静态发光网格能被射线命中,但没有对自发光表面做专门的重要性采样;小文字的间接照明仍可能噪声较大。非 LCD 材质的所有运行时颜色/开关变化、alpha cutout 发光贴花尚未完整覆盖。半分辨率射线及去噪会使玻璃反射中的细小文字偏软,主画面和原屏幕仍为原生分辨率。
74 78 - 半精度近场数据、有限样本、短历史和较少反弹仍可能产生噪声、闪烁、细边误差或运动拖影。次级高亮有限幅,不是生产级重建。
75 79 - 光追增加 CPU/GPU 工作,不解决原有大型舰船模拟瓶颈,不保证 FPS 提升。关闭开关可恢复原版渲染。
76 80
@@ -81,3 +85,5 @@ GPU 回读测试覆盖不透明遮挡、天空/远景保留、纹理和 GGX
81 85 第一版数据保留在 [初版实测记录](raytracing-2026-11-17.md);玻璃/体素版的场景、性能和未确认项见 [本版实测记录](raytracing-glass-voxels-2026-11-17.md)。
82 86
83 87 方差去噪、体素覆盖与 CPU 线程对照见 [后续实测](performance-raytracing-2026-11-17-v3.md)。
88
89 驾驶舱内部屏幕、自发光文字和玻璃反射见 [LCD 修复实测](raytracing-emissive-lcd-2026-11-17.md)。
Added docs/raytracing-emissive-lcd-2026-11-17.md +53 -0
@@ -0,0 +1,53 @@
1 # 驾驶舱真实屏幕与发光文字的玻璃反射
2
3 修复对象是实际驾驶舱屏幕与模型上的自发光材质。最终版本在用户当前的战斗驾驶舱中确认了 6 块 LCD 纹理,前窗和侧窗出现仪表图案及文字反射。反射中的细小文字仍偏软,不能称为已达到原屏幕的文字清晰度。
4
5 ## 原因与修复
6
7 此前有三处数据缺失:只按实体外部模型收集,未选择第一人称内部模型;只加载静态 DDS,没有采集 LCD 当前渲染纹理;次级命中着色没有使用扩展贴图的自发光通道。
8
9 - 第一人称使用驾驶舱定义中的内部模型,并绑定对应的内部渲染对象。切换驾驶舱/第一三人称会作废几何快照和反射历史。当前驾驶舱先于周围舰船分配屏幕槽位。
10 - 在渲染线程观察材质纹理、颜色、发光强度和可见性消息。游戏线程只发布不可变屏幕绑定,工作线程不读取可变实体。
11 - 通过现有 SRV 在 GPU 上复制实际 LCD 图像到 DX11/DX12 共享 RGBA16F 纹理,使用原有 fence 和 context state 隔离。没有屏幕截图回灌、CPU 像素回读或伪造屏幕文字。生成的 LCD alpha 不再被当作金属度。
12 - 静态扩展贴图按线性数据读取,绿色通道提供自发光,缺失时退回零发光。该辐射参与真实几何射线的命中着色,即使没有太阳或局部灯也可反射出来。
13 - 最多 32 块近场屏幕,范围 64 m,更新频率 10 Hz;单纹理最大 2048×2048,总共享纹理预算 128 MiB。状态区分隐藏屏幕与纹理不可用,方便判断是否真正接入。
14
15 资源通过视图确定读取格式,依据 [Microsoft 的 Direct3D 资源/视图说明](https://learn.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-intro)。复制阶段使用整数 texel 和 mip 0,依据 [HLSL Load 文档](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-load);线性扩展通道含义另在本机当前游戏 shader 中核对,未提交游戏代码或资产。
16
17 ## 实机核验
18
19 SE 1.211.017、RTX 5090,使用 `XFE Performance Huanyu 20261116` 副本。两次部署均正常保存、退出并备份后重启。最终部署前的备份为 `20261116T230915394-7687acc5ab6d4fbbbb9df686ade31c7a`;原存档没有参与本轮加载或写入。
20
21 此时用户所在驾驶舱为 `DBSmallBlockFighterCockpit`,实体 `107436408077512738`,内部模型 `Models\Cubes\small\CockpitFighterInterior.mwm`。这次按用户截图对应的驾驶舱核验,没有将该视角伪称为寰宇号大型舰船性能测试。
22
23 最终进程 PID 154488:
24
25 - 内部模型包含 18 个玻璃三角形及 `CockpitScreen_01` 至 `_06`;6 块屏幕全部绑定、可见并成功复制。
26 - 当前近场合计 17 块成功复制、15 块隐藏、0 块可见但纹理不可用。隐藏项不再作为采集错误报告。
27 - 原生窗口、viewport、backbuffer、主渲染和导出图都是 **3840×2160**;动态分辨率关闭。DXR/去噪仍使用 **1920×1080**,不是全分辨率光追。
28 - 暂停模拟以固定船体,保持真实 `MyCockpit` 相机、不设置 spectator override。开启/关闭/玻璃诊断/再次开启分别导出。开启段等候至少 128 个 active 渲染帧。核验后恢复光追开启、诊断关闭及原有未暂停状态。
29 - 已查看完整原生画面、前窗/左右侧窗的原比例分块,以及右窗区域 `(2400,1230)-(3500,1880)` 的开关并排图和差异图。开启时可辨认蓝色电池图案、仪表图案与文字行的反射;关闭时该区域只有原版背景。窗框和原 LCD 保持正常,HUD 提示没有被复制到玻璃。玻璃诊断与窗框边界一致。
30
31 首组最终构建截图仍有主菜单覆盖,已剔除出验收依据,保留在 `artifacts/graphics/20261117-emissive/final/`。有效截图和状态在 `final-clean/`,不是仅依据程序返回成功判定画面通过。原截图过大无法直接返回查看工具时,另保存了 RGB 像素逐字节一致的无透明通道 PNG,并结合原比例分块查看。
32
33 ## 自动验证
34
35 构建零警告、零错误,93 项通用检查、11 项引擎检查通过。RTX 5090 上运行了实际 DXR 和 D3D11/12 互操作回读:
36
37 | 场景 | 实测 | 含义 |
38 | --- | --- | --- |
39 | 关闭太阳和灯,线性自发光遮罩值 128 | 反射绿色 0.515625 | 自发光不再被丢弃或错误作 sRGB 解码 |
40 | 扩展贴图缺失 | 绿色 0.017990 | 只有既有环境项,不制造发光 |
41 | 相机后方真实 GPU 屏幕为红色 | 玻璃 RGB `(0.043321, 0, 0)` | 屏幕外几何和实时纹理可被反射 |
42 | 同一屏幕改为蓝色 | 玻璃 RGB `(0, 0, 0.043321)` | GPU 内容更新能改变反射 |
43 | 隐藏该屏幕 | 玻璃 RGB `(0, 0, 0)` | 不保留隐藏图像 |
44
45 已有玻璃、体素、灯光、时域稳定、几何回收和图形状态恢复测试同样通过。动态变色/隐藏的确定性验证来自上述 GPU 场景;没有改写用户驾驶舱的脚本、颜色配置或文字内容来制造测试结果。
46
47 ## 边界与证据
48
49 细小反射文字仍受半分辨率射线和过滤影响;运动拖影、全部模组驾驶舱、alpha cutout 发光贴花以及非 LCD 材质的所有运行时开关尚未完整验证。本轮没有进行新的整帧性能对照,不宣称解决此前约 29 FPS 的大型舰船 CPU 瓶颈。
50
51 最终插件 SHA-256:`2A1A20400E9DD2B7BE3A0830774E79C8E88BE6AC4C5557E6D11DFDEDA58F5EB0`。原生模块 SHA-256:`EFF7EC815135D884B67AC5E76DB6A970878C89D7B1C5DB61F364079ADB54BA43`。安装文件与构建输出哈希一致。
52
53 可核对的截图、状态、测试日志路径及哈希保存在 [证据清单](evidence/raytracing-emissive-lcd-2026-11-17.json)。本地视觉检查记录位于 `work/visual/20261117-emissive/final-clean/`。
Modified src/XFE.SeAgent.Plugin/Graphics/RayTracing.Materials.cs +5 -0
@@ -34,6 +34,11 @@ namespace XFE.SeAgent.Plugin.Graphics
34 34 var voxels=MyEntities.GetEntities().OfType<MyVoxelBase>().Select(v=>v.RootVoxel).Where(v=>v!=null && !v.Closed).Distinct()
35 35 .OrderBy(v=>Vector3D.DistanceSquared(camera,v.PositionComp.WorldAABB.Center)).Take(64);
36 36 return new JObject { ["camera"]=vec(camera),
37 ["interiorCockpit"]=interiorCockpit==null?null:new JObject { ["entityId"]=interiorCockpit.EntityId.ToString(),
38 ["model"]=DisplayedModel(interiorCockpit)?.AssetName,
39 ["parts"]=new JArray(DisplayedModel(interiorCockpit).GetMeshList().Select(m=>new JObject {
40 ["name"]=m.Material?.Name,["technique"]=m.Material?.DrawTechnique.ToString(),["triangles"]=m.TriCount,
41 ["textures"]=m.Material?.Textures==null?null:JObject.FromObject(m.Material.Textures) })) },
37 42 ["panes"]=new JArray(panes.Select(p=> { var surface=GlassSurface(p);return new JObject { ["entityId"]=p.Owner.EntityId.ToString(),["model"]=p.Owner.Model?.AssetName,
38 43 ["position"]=vec(p.Owner.WorldMatrix.Translation),["forward"]=vec(p.Owner.WorldMatrix.Forward),["up"]=vec(p.Owner.WorldMatrix.Up),
39 44 ["glassCenter"]=vec(surface.Translation),["glassNormal"]=vec(surface.Forward),["glassUp"]=vec(surface.Up),
Added src/XFE.SeAgent.Plugin/Graphics/RayTracing.Screens.cs +146 -0
@@ -0,0 +1,146 @@
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.Linq;
6 using System.Runtime.InteropServices;
7 using System.Threading;
8 using HarmonyLib;
9 using Newtonsoft.Json.Linq;
10 using Sandbox.Game.Components;
11 using Sandbox.Game.Entities;
12 using Sandbox.Game.World;
13 using VRage.Game.Entity;
14 using VRage.Game.Models;
15 using VRageMath;
16 using VRageRender;
17 using VRageRender.Messages;
18
19 namespace XFE.SeAgent.Plugin.Graphics
20 {
21 internal sealed partial class RayTracing
22 {
23 [StructLayout(LayoutKind.Sequential)]
24 private struct SurfaceMaterial { public Vector4 ColorEmission; public uint Texture,Flags,Reserved0,Reserved1; }
25 private sealed class ScreenBinding { public uint Slot,Texture,RenderId; public string Material,DefaultTexture; }
26 private sealed class ScreenModel { public MyModel Model;public uint RenderId;public Mesh Mesh; }
27 private sealed class ScreenState { public string Texture;public Vector3 Color=Vector3.One;public float Emission=1;public bool Visible=true,HasTexture; }
28 private readonly List<ScreenBinding> screenBindings=new List<ScreenBinding>(); // game thread
29 private readonly Dictionary<MyEntity,ScreenModel> screenModels=new Dictionary<MyEntity,ScreenModel>();
30 // Renderer messages and consumption run on the renderer, not game workers.
31 private readonly Dictionary<uint,Dictionary<string,ScreenState>> screenStates=new Dictionary<uint,Dictionary<string,ScreenState>>();
32 private readonly SurfaceMaterial[] surfaceUpload=new SurfaceMaterial[33];
33 private long lastScreenCopy;
34 private MyCockpit interiorCockpit;
35 private volatile JObject screenStatus=new JObject();
36 [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] private static extern int RtSurfaceMaterials(IntPtr context,SurfaceMaterial[] materials,uint count);
37 [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] private static extern int RtDynamicMaterial(IntPtr context,uint id,IntPtr srv);
38
39 private void InstallScreenCapture()
40 {
41 var process=AccessTools.DeclaredMethod(Renderer,"ProcessMessageInternal",new[]{typeof(MyRenderMessageBase),typeof(int)});
42 if(process==null)throw new NotSupportedException("Renderer material message hook unavailable.");
43 harmony.Patch(process,postfix:new HarmonyMethod(typeof(RayTracing),nameof(AfterMaterialMessage)));
44 }
45 private static void AfterMaterialMessage(MyRenderMessageBase message) { active?.CaptureMaterialMessage(message); }
46 private ScreenState ScreenStateFor(uint id,string name)
47 {
48 if(!screenStates.TryGetValue(id,out var row)) {
49 if(screenStates.Count>=65536)return null;
50 screenStates.Add(id,row=new Dictionary<string,ScreenState>(StringComparer.Ordinal));
51 }
52 if(!row.TryGetValue(name,out var state)) {
53 if(row.Count>=128)return null;
54 row.Add(name,state=new ScreenState());
55 }
56 return state;
57 }
58 private void CaptureMaterialMessage(MyRenderMessageBase message)
59 {
60 if(message is MyRenderMessageUpdateModelProperties p) {
61 var state=ScreenStateFor(p.ID,p.MaterialName.String);if(state==null)return;
62 if(p.DiffuseColor.HasValue)state.Color=p.DiffuseColor.Value.ToVector3();
63 if(p.Emissivity.HasValue)state.Emission=MathHelper.Clamp(p.Emissivity.Value,0,32);
64 if(p.FlagsChange.HasValue) {
65 var flags=p.FlagsChange.Value;
66 if((flags.Add&RenderFlags.Visible)!=0)state.Visible=true;
67 if((flags.Remove&RenderFlags.Visible)!=0)state.Visible=false;
68 }
69 } else if(message is MyRenderMessageChangeMaterialTexture t && t.Changes!=null) {
70 foreach(var change in t.Changes) {
71 var state=ScreenStateFor(t.RenderObjectID,change.Key.String);if(state==null)continue;
72 state.Texture=change.Value.ColorMetalFileName;state.HasTexture=true;
73 }
74 } else if(message.MessageType==MyRenderMessageEnum.RemoveRenderObject) {
75 var id=Member(message,"ID");if(id is uint value)screenStates.Remove(value);
76 }
77 }
78 private void CheckCockpitModel()
79 {
80 var cockpit=MySession.Static.CameraController as MyCockpit;
81 if(cockpit!=null && !(cockpit.IsInFirstPersonView || cockpit.ForceFirstPersonCamera))cockpit=null;
82 if(ReferenceEquals(cockpit,interiorCockpit))return;
83 interiorCockpit=cockpit;Interlocked.Increment(ref sceneGeneration);Interlocked.Exchange(ref resetHistory,1);
84 }
85 private MyModel DisplayedModel(MyEntity entity)
86 {
87 if(ReferenceEquals(entity,interiorCockpit) && !string.IsNullOrEmpty(interiorCockpit.BlockDefinition.InteriorModel))
88 return MyModels.GetModelOnlyData(interiorCockpit.BlockDefinition.InteriorModel);
89 return entity.Model;
90 }
91 private void ResetScreenCapture() { screenBindings.Clear();screenModels.Clear(); }
92 private Mesh CopyScreenModel(MyEntity entity,MyModel model,Mesh original)
93 {
94 if(original==null || !(entity.Render is MyRenderComponentScreenAreas render))return original;
95 if(Vector3D.DistanceSquared(entity.WorldMatrix.Translation,MySector.MainCamera.Position)>64*64)return original;
96 uint renderId=render.RenderObjectIDs.FirstOrDefault(id=>id!=uint.MaxValue);
97 object selected=Member(render,ReferenceEquals(entity,interiorCockpit)?"InteriorRenderId":"ExteriorRenderId");
98 if(selected is uint id && id!=uint.MaxValue)renderId=id;
99 if(screenModels.TryGetValue(entity,out var cached) && cached.Model==model && cached.RenderId==renderId) {
100 // A periodic range scan can retire a variant while the entity's
101 // binding remains cached. Reuse its slots when it returns.
102 meshesById[cached.Mesh.Id]=cached.Mesh;return cached.Mesh;
103 }
104 var areas=AccessTools.Field(typeof(MyRenderComponentScreenAreas),"m_screenAreas")?.GetValue(render) as IEnumerable;if(areas==null)return original;
105 var names=new Dictionary<string,string>(StringComparer.Ordinal);
106 foreach(var area in areas) {var name=Member(area,"Material") as string;if(!string.IsNullOrEmpty(name))names[name]=Member(area,"OffscreenTextureName") as string;}
107 var materials=(uint[])original.Materials.Clone();bool changed=false;
108 foreach(var part in original.Parts??new MeshPart[0]) {
109 if(!names.TryGetValue(part.Name??"",out var texture) || screenBindings.Count>=32)continue;
110 uint slot=(uint)screenBindings.Count+1;
111 screenBindings.Add(new ScreenBinding {Slot=slot,Texture=991+slot,RenderId=renderId,Material=part.Name,DefaultTexture=texture});
112 for(int i=part.Start;i<part.Start+part.Count;i++)materials[i]|=slot<<20;
113 changed=true;
114 }
115 var mesh=original;
116 if(changed) {mesh=new Mesh {Id=++nextMesh,Vertices=original.Vertices,Indices=original.Indices,Materials=materials,Parts=original.Parts};meshesById.Add(mesh.Id,mesh);}
117 screenModels[entity]=new ScreenModel {Model=model,RenderId=renderId,Mesh=mesh};return mesh;
118 }
119 private void UpdateScreens(ScreenBinding[] bindings)
120 {
121 long now=Stopwatch.GetTimestamp();if(now-lastScreenCopy<Stopwatch.Frequency/10)return;lastScreenCopy=now;
122 for(int i=0;i<surfaceUpload.Length;i++)surfaceUpload[i]=new SurfaceMaterial {ColorEmission=Vector4.One};
123 var files=Member(AccessTools.TypeByName("VRage.Render11.Common.MyManagers"),"FileTextures");
124 var textureType=AccessTools.TypeByName("VRage.Render11.Resources.ITexture");
125 var get=files==null || textureType==null?null:files.GetType().GetMethod("TryGetTexture",Flags,null,new[]{typeof(string),textureType.MakeByRefType()},null);
126 int copied=0,missing=0,hidden=0;var details=new JArray();
127 foreach(var binding in bindings??new ScreenBinding[0]) {
128 ScreenState state=null;if(screenStates.TryGetValue(binding.RenderId,out var row))row.TryGetValue(binding.Material,out state);
129 string name=state!=null && state.HasTexture?state.Texture:binding.DefaultTexture;
130 var data=new SurfaceMaterial {ColorEmission=new Vector4(state?.Color??Vector3.One,state?.Emission??1),Flags=1};
131 bool visible=state?.Visible??true;if(!visible)data.Flags|=8;
132 IntPtr srv=IntPtr.Zero;
133 if(visible && !string.IsNullOrEmpty(name) && get!=null) {object[] args={name,null};if((bool)get.Invoke(files,args))srv=Pointer(Member(args[1],"Srv"));}
134 int result=RtDynamicMaterial(native,binding.Texture,srv);
135 if(result<0)throw new InvalidOperationException("Live screen upload: 0x"+result.ToString("X8"));
136 if(srv!=IntPtr.Zero && result==0) {data.Texture=binding.Texture;data.Flags|=2|4;copied++;}
137 else if(!visible)hidden++;else missing++;
138 surfaceUpload[binding.Slot]=data;
139 details.Add(new JObject { ["renderId"]=binding.RenderId,["material"]=binding.Material,["texture"]=name,["copied"]=srv!=IntPtr.Zero && result==0,["visible"]=visible,["emission"]=data.ColorEmission.W });
140 }
141 int code=RtSurfaceMaterials(native,surfaceUpload,(uint)surfaceUpload.Length);
142 if(code<0)throw new InvalidOperationException("Screen material upload: 0x"+code.ToString("X8"));
143 screenStatus=new JObject {["liveTextures"]=copied,["unavailableTextures"]=missing,["hiddenScreens"]=hidden,["refreshHz"]=10,["limit"]=32,["captureRadiusMeters"]=64,["bindings"]=details};
144 }
145 }
146 }
Modified src/XFE.SeAgent.Plugin/Graphics/RayTracing.cs +33 -18
@@ -58,7 +58,7 @@ namespace XFE.SeAgent.Plugin.Graphics
58 58 private const string Library = "XFE.SeRayTracing.Native.dll";
59 59 [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtCreate(IntPtr device, out IntPtr context);
60 60 [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtMesh(IntPtr context, uint id, float[] vertices, uint count, uint[] indices, uint indexCount, uint[] materials);
61 [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtMaterial(IntPtr context, uint id, byte[] dds, uint bytes);
61 [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtMaterial(IntPtr context, uint id, byte[] dds, uint bytes, uint linear);
62 62 [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtRetainMeshes(IntPtr context, uint[] ids, uint count);
63 63 [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtFrame(IntPtr context, IntPtr depth, IntPtr normal, IntPtr light, IntPtr material, IntPtr albedo, IntPtr target, Instance[] instances, uint count, ref Parameters parameters);
64 64 [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern void RtGetStatus(IntPtr context, out NativeStatus status);
@@ -66,12 +66,13 @@ namespace XFE.SeAgent.Plugin.Graphics
66 66 [DllImport("kernel32", CharSet=CharSet.Unicode, SetLastError=true)] private static extern IntPtr LoadLibraryEx(string path, IntPtr file, uint flags);
67 67
68 68 private sealed class Options { public bool Enabled; public float Radius=3, Strength=.3f, ShadowStrength=.15f, Reflections=.8f, Indirect=.6f; public int Diagnostic, Bounces=3, VoxelLod=0; public bool Glass=true, Voxels=true; }
69 private sealed class Mesh { public uint Id; public float[] Vertices; public uint[] Indices, Materials; }
70 private sealed class Material { public uint Id; public string Path; public Task<byte[]> Bytes; }
69 private sealed class Mesh { public uint Id; public float[] Vertices; public uint[] Indices, Materials; public MeshPart[] Parts; }
70 private sealed class MeshPart { public string Name; public int Start,Count; }
71 private sealed class Material { public uint Id; public string Path; public bool Linear; public Task<byte[]> Bytes; }
71 72 private sealed class CellPart { public Mesh Mesh; public Matrix Matrix; public Vector3 Tint; public Vector2 UvOffset; }
72 73 private sealed class CellGeometry { public long Signature; public Mesh Mesh; }
73 74 private sealed class Placement { public MyEntity Owner; public Matrix Local; public uint Mesh; public MySlimBlock Block; }
74 private sealed class Snapshot { public Vector3D Origin; public Instance[] Instances; public int Count; public Mesh[] Meshes; public Material[] Materials; public GlassMaterial[] Glass; public Light[] Lights; public long Created; public int Generation; }
75 private sealed class Snapshot { public Vector3D Origin; public Instance[] Instances; public int Count; public Mesh[] Meshes; public Material[] Materials; public GlassMaterial[] Glass; public Light[] Lights; public ScreenBinding[] Screens; public long Created; public int Generation; }
75 76 private readonly Action<string> log;
76 77 private readonly Harmony harmony = new Harmony("xfestudio.se-agent.raytracing.v1");
77 78 private readonly object renderLock = new object();
@@ -123,6 +124,7 @@ namespace XFE.SeAgent.Plugin.Graphics
123 124 // After opaque/transparent command submission, before exposure, bloom,
124 125 // tone mapping and HUD. The engine's cached state is restored by native.
125 126 harmony.Patch(done, postfix:new HarmonyMethod(typeof(RayTracing),nameof(AfterScene)));
127 InstallScreenCapture();
126 128 active=this;
127 129 MyCubeGrid.OnBlockAddedGlobally+=GeometryChanged;
128 130 MyCubeGrid.OnBlockRemovedGlobally+=GeometryChanged;
@@ -176,11 +178,11 @@ namespace XFE.SeAgent.Plugin.Graphics
176 178 ["glass"]=o.Glass,["voxels"]=o.Voxels,["bounces"]=o.Bounces,["voxelLod"]=o.VoxelLod,
177 179 ["glassMaterials"]=glassMaterials.Count,["glassTriangles"]=glassTriangles,["voxelChunks"]=voxelChunks,["voxelTriangles"]=voxelTriangles,["voxelRevision"]=voxelRevision,
178 180 ["voxelCaptureRadiusMeters"]=VoxelRadius,["voxelWorkers"]=VoxelWorkers,
179 ["localLights"]=s?.Lights?.Length??0,
181 ["localLights"]=s?.Lights?.Length??0,["screens"]=screenStatus,
180 182 ["scannedBlocks"]=scannedBlocks, ["skippedModels"]=skippedModels, ["snapshotInstances"]=s?.Count??0,
181 183 ["snapshotAgeMs"]=s==null ? (JToken)null : (Stopwatch.GetTimestamp()-s.Created)*1000.0/Stopwatch.Frequency,
182 184 ["implementation"]="DXR 1.1 inline hardware rays over cached triangle models. Half-resolution rays, bilateral composite into native HDR before HUD.",
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." };
185 ["limits"]="Local hybrid DXR: 1..4 bounces, up to 64 point/spot fixtures via RIS, emissive mesh materials and up to 32 live LCD textures within 64 m at 10 Hz (current cockpit first), 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." };
184 186 }
185 187 public void Update(bool allowed)
186 188 {
@@ -192,6 +194,7 @@ namespace XFE.SeAgent.Plugin.Graphics
192 194 if (!options.Enabled || !worldReady || failure!=null) return;
193 195 try
194 196 {
197 CheckCockpitModel();
195 198 int generation=Volatile.Read(ref sceneGeneration);
196 199 if(generation!=capturedGeneration) { ResetCapture(); capturedGeneration=generation; }
197 200 var now=Stopwatch.GetTimestamp();
@@ -247,6 +250,7 @@ namespace XFE.SeAgent.Plugin.Graphics
247 250 next.Origin=origin; next.Count=count; next.Meshes=placementMeshes; next.Materials=captureMaterials;
248 251 next.Glass=glassMaterials.ToArray();
249 252 next.Lights=CaptureLights(origin);
253 next.Screens=screenBindings.ToArray();
250 254 next.Created=Stopwatch.GetTimestamp(); next.Generation=generation;
251 255 if(generation==Volatile.Read(ref sceneGeneration)) snapshots.Publish(next);
252 256 lastSnapshot=now;
@@ -263,6 +267,7 @@ namespace XFE.SeAgent.Plugin.Graphics
263 267 }
264 268 private void ResetCapture()
265 269 {
270 ResetScreenCapture();
266 271 snapshots.Clear(); captureMatrices.Clear(); placementMeshes=new Mesh[0]; captureMaterials=new Material[0];
267 272 collector?.Dispose(); collector=null; buildingPlacements=null; placements.Clear(); lastCollect=lastSnapshot=0;
268 273 foreach(var grid in watchedGrids) grid.OnBlockIntegrityChanged-=GeometryChanged;
@@ -284,6 +289,10 @@ namespace XFE.SeAgent.Plugin.Graphics
284 289 }
285 290 private IEnumerable<Placement> Collect(Vector3D camera)
286 291 {
292 // Reserve live-screen slots for the cockpit currently being viewed
293 // before walking unordered blocks from the surrounding large ship.
294 if(interiorCockpit!=null && !interiorCockpit.Closed)
295 foreach(var p in EntityModels(interiorCockpit,interiorCockpit.SlimBlock,0))yield return p;
287 296 var sphere=new BoundingSphereD(camera,250);
288 297 var grids=MyEntities.GetEntities().OfType<MyCubeGrid>().Where(g=>!g.Closed && g.PositionComp.WorldAABB.Intersects(sphere)).OrderBy(g=>Vector3D.DistanceSquared(g.WorldMatrix.Translation,camera)).ToArray();
289 298 var current=new HashSet<MyCubeGrid>(grids);
@@ -330,7 +339,7 @@ namespace XFE.SeAgent.Plugin.Graphics
330 339 scannedBlocks++;
331 340 if (grid.Closed || block.IsDestroyed) { yield return null; continue; }
332 341 if(Vector3D.DistanceSquared(Vector3D.Transform(block.Position*grid.GridSize,grid.WorldMatrix),camera)>250*250) { yield return null; continue; }
333 if(block.FatBlock!=null)
342 if(block.FatBlock!=null && !ReferenceEquals(block.FatBlock,interiorCockpit))
334 343 {
335 344 foreach(var p in EntityModels(block.FatBlock,block,0)) yield return p;
336 345 }
@@ -342,7 +351,8 @@ namespace XFE.SeAgent.Plugin.Graphics
342 351 private IEnumerable<Placement> EntityModels(MyEntity entity,MySlimBlock block,int depth)
343 352 {
344 353 if(entity==null || entity.Closed || depth>8 || entity.Render==null || !entity.Render.Visible) yield break;
345 var mesh=CopyModel(entity.Model);
354 var model=DisplayedModel(entity);
355 var mesh=CopyScreenModel(entity,model,CopyModel(model));
346 356 if(mesh!=null) yield return new Placement { Owner=entity, Local=Matrix.Identity, Mesh=mesh.Id, Block=block };
347 357 if(entity.Subparts!=null) foreach(var part in entity.Subparts.Values) foreach(var p in EntityModels(part,block,depth+1)) yield return p;
348 358 }
@@ -357,7 +367,7 @@ namespace XFE.SeAgent.Plugin.Graphics
357 367 model.LoadTexCoordData(); var uv=model.TexCoords;
358 368 var vertices=new float[count*8];
359 369 for(int i=0;i<count;i++) { var v=model.GetVertex(i); if(!Finite(v.X)||!Finite(v.Y)||!Finite(v.Z)) throw new InvalidDataException("Nonfinite model vertex."); vertices[i*8]=v.X; vertices[i*8+1]=v.Y; vertices[i*8+2]=v.Z; if(uv!=null && i<uv.Length) {var tex=uv[i].ToVector2(); vertices[i*8+3]=tex.X;vertices[i*8+4]=tex.Y;} }
360 var indices=new List<uint>(triangles.Length*3); var materials=new List<uint>(triangles.Length);
370 var indices=new List<uint>(triangles.Length*3); var materials=new List<uint>(triangles.Length);var parts=new List<MeshPart>();
361 371 foreach(var part in model.GetMeshList())
362 372 {
363 373 if(part.Material==null) continue;
@@ -367,10 +377,12 @@ namespace XFE.SeAgent.Plugin.Graphics
367 377 uint cm=MaterialId(part.Material.Textures,"ColorMetalTexture"),extras=MaterialId(part.Material.Textures,"AddMapsTexture");
368 378 if(extras==0) extras=MaterialId(part.Material.Textures,"ExtensionsTexture");
369 379 uint flags=glass ? GlassFlag | (GlassId(part.Material.GlassCW,part.Material.GlassCCW)<<20) : 0;
380 int start=materials.Count;
370 381 for(int i=part.TriStart;i<part.TriStart+part.TriCount && i<triangles.Length;i++) { var t=triangles[i]; indices.Add((uint)t.I0);indices.Add((uint)t.I1);indices.Add((uint)t.I2);materials.Add(cm | (extras<<10) | flags); if(glass) glassTriangles++; }
382 if(!glass)parts.Add(new MeshPart{Name=part.Material.Name,Start=start,Count=materials.Count-start});
371 383 }
372 384 if(indices.Count==0) { modelCache[model]=null; skippedModels++; return null; }
373 var mesh=new Mesh {Id=++nextMesh,Vertices=vertices,Indices=indices.ToArray(),Materials=materials.ToArray()}; modelCache.Add(model,mesh);meshesById.Add(mesh.Id,mesh); return mesh;
385 var mesh=new Mesh {Id=++nextMesh,Vertices=vertices,Indices=indices.ToArray(),Materials=materials.ToArray(),Parts=parts.ToArray()}; modelCache.Add(model,mesh);meshesById.Add(mesh.Id,mesh); return mesh;
374 386 }
375 387 private static Mesh MergeCell(CellPart[] parts)
376 388 {
@@ -394,19 +406,20 @@ namespace XFE.SeAgent.Plugin.Graphics
394 406 private uint MaterialId(Dictionary<string,string> textures,string key)
395 407 {
396 408 if(textures==null || !textures.TryGetValue(key,out var relative) || string.IsNullOrEmpty(relative)) return 0;
397 return MaterialPath(relative);
409 return MaterialPath(relative,key!="ColorMetalTexture");
398 410 }
399 private uint MaterialPath(string relative)
411 private uint MaterialPath(string relative,bool linear=false)
400 412 {
401 413 if(string.IsNullOrEmpty(relative)) return 0;
402 414 string path=Path.IsPathRooted(relative)?relative:Path.Combine(VRage.FileSystem.MyFileSystem.ContentPath,relative);
403 415 path=Path.GetFullPath(path);
404 if(materialCache.TryGetValue(path,out var cached)) return cached.Id;
405 if(materialCache.Count>=1023) throw new InvalidOperationException("Scene exceeded 1023 ray material textures.");
406 var material=new Material { Id=(uint)materialCache.Count+1,Path=path };
416 string key=(linear?"linear:":"color:")+path;
417 if(materialCache.TryGetValue(key,out var cached)) return cached.Id;
418 if(materialCache.Count>=991) throw new InvalidOperationException("Scene exceeded 991 static ray material textures (32 slots reserved for live screens).");
419 var material=new Material { Id=(uint)materialCache.Count+1,Path=path,Linear=linear };
407 420 // File I/O only on two bounded workers; never touch game entities there.
408 421 material.Bytes=ReadMaterial(path);
409 materialCache.Add(path,material); return material.Id;
422 materialCache.Add(key,material); return material.Id;
410 423 }
411 424 private Task<byte[]> ReadMaterial(string path) => Task.Run(async()=> {
412 425 await textureReaders.WaitAsync();
@@ -417,7 +430,8 @@ namespace XFE.SeAgent.Plugin.Graphics
417 430 {
418 431 if(target==null) return null; var type=target as Type??target.GetType(); var instance=target is Type?null:target;
419 432 string key=type.FullName+"|"+name;
420 if(!members.TryGetValue(key,out var member)) { member=(MemberInfo)type.GetField(name,Flags)??type.GetProperty(name,Flags); members[key]=member; }
433 MemberInfo member;
434 lock(members) if(!members.TryGetValue(key,out member)) { member=(MemberInfo)type.GetField(name,Flags)??type.GetProperty(name,Flags); members[key]=member; }
421 435 return member is FieldInfo field?field.GetValue(instance):(member as PropertyInfo)?.GetValue(instance,null);
422 436 }
423 437 private IntPtr Pointer(object target) => target==null?IntPtr.Zero:(IntPtr)(Member(target,"NativePointer")??IntPtr.Zero);
@@ -471,7 +485,7 @@ namespace XFE.SeAgent.Plugin.Graphics
471 485 if(texturesUploaded.Contains(material.Id)) continue;
472 486 if(material.Bytes==null) material.Bytes=ReadMaterial(material.Path);
473 487 if(!material.Bytes.IsCompleted) continue;
474 byte[] bytes=material.Bytes.Result; int result=RtMaterial(native,material.Id,bytes,(uint)bytes.Length);
488 byte[] bytes=material.Bytes.Result; int result=RtMaterial(native,material.Id,bytes,(uint)bytes.Length,material.Linear?1u:0u);
475 489 if(result<0) throw new InvalidOperationException("Native texture upload: 0x"+result.ToString("X8"));
476 490 if(result>0) textureFallbacks++;
477 491 texturesUploaded.Add(material.Id); material.Bytes=null; if(++added>=4) break;
@@ -494,6 +508,7 @@ namespace XFE.SeAgent.Plugin.Graphics
494 508 History=new Vector4(previousFrameAt!=0 && Stopwatch.GetTimestamp()-previousFrameAt<Stopwatch.Frequency/4?1:0,0,0,0),
495 509 Transport=new Vector4(o.Bounces,64,o.Glass?1:0,s.Lights.Length),ViewProjection=(Matrix)Member(matrices,"ViewProjectionAt0") };
496 510 var g=Member(GBuffer,"Main");
511 UpdateScreens(s.Screens);
497 512 int code=RtFrame(native,Pointer(Member(Member(Member(g,"ResolvedDepthStencil"),"SrvDepth"),"Srv")),
498 513 Pointer(Member(Member(g,"GBuffer1"),"Srv")),Pointer(Member(Member(g,"LBuffer"),"Srv")),
499 514 Pointer(Member(Member(g,"GBuffer2"),"Srv")),Pointer(Member(Member(g,"GBuffer0"),"Srv")),Pointer(Member(Member(g,"LBuffer"),"Rtv")),s.Instances,
Added src/XFE.SeRayTracing.Native/CopyScreen.hlsl +8 -0
@@ -0,0 +1,8 @@
1 // Copy the engine's current SRV, including its sRGB decoding, entirely on GPU.
2 Texture2D<float4> source : register(t0);
3 RWTexture2D<float4> target : register(u0);
4 [numthreads(8,8,1)]
5 void CopyScreen(uint3 id:SV_DispatchThreadID) {
6 uint w,h;target.GetDimensions(w,h);
7 if(id.x<w && id.y<h) target[id.xy]=source.Load(int3(id.xy,0));
8 }
Modified src/XFE.SeRayTracing.Native/Dds.h +9 -1
@@ -12,7 +12,7 @@ struct Material {
12 12 std::vector<uint8_t> data{144,144,144,0};
13 13 bool built=false;
14 14 };
15 inline bool ParseDds(const uint8_t* bytes,uint32_t size,Material& output) {
15 inline bool ParseDds(const uint8_t* bytes,uint32_t size,Material& output,bool linear=false) {
16 16 if(!bytes || size<128 || size>128u*1024*1024) return false;
17 17 auto u32=[&](uint32_t offset) { uint32_t v=0; if(offset+4<=size) memcpy(&v,bytes+offset,4); return v; };
18 18 if(u32(0)!=0x20534444 || u32(4)!=124 || u32(76)!=32 || (u32(112)&0x0020FE00)) return false;
@@ -49,5 +49,13 @@ inline bool ParseDds(const uint8_t* bytes,uint32_t size,Material& output) {
49 49 w=std::max(1u,w/2); h=std::max(1u,h/2);
50 50 }
51 51 if(total>size-offset || total>32u*1024*1024) return false;
52 if(linear) switch(f) {
53 case DXGI_FORMAT_BC1_UNORM_SRGB:f=DXGI_FORMAT_BC1_UNORM;break;
54 case DXGI_FORMAT_BC2_UNORM_SRGB:f=DXGI_FORMAT_BC2_UNORM;break;
55 case DXGI_FORMAT_BC3_UNORM_SRGB:f=DXGI_FORMAT_BC3_UNORM;break;
56 case DXGI_FORMAT_BC7_UNORM_SRGB:f=DXGI_FORMAT_BC7_UNORM;break;
57 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:f=DXGI_FORMAT_R8G8B8A8_UNORM;break;
58 case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:f=DXGI_FORMAT_B8G8R8A8_UNORM;break;
59 }
52 60 output.format=f; output.data.assign(bytes+offset,bytes+offset+(size_t)total); return true;
53 61 }
Modified src/XFE.SeRayTracing.Native/Native.cpp +81 -7
@@ -19,6 +19,7 @@
19 19 #include "FullscreenShader.h"
20 20 #include "RaysShader.h"
21 21 #include "DenoiseShader.h"
22 #include "CopyScreenShader.h"
22 23 using Microsoft::WRL::ComPtr;
23 24 struct Failure : std::runtime_error { HRESULT code; Failure(HRESULT c, const char* s) : runtime_error(s), code(c) {} };
24 25 static void Check(HRESULT result, const char* operation) { if (FAILED(result)) throw Failure(result, operation); }
@@ -32,6 +33,7 @@ struct Mesh {
32 33 struct Vertex { float position[3],uv[2],tint[3]; };
33 34 struct Triangle { uint32_t v[3],material; };
34 35 struct InstanceMaterial { uint32_t triangleOffset; float tint[3]; };
36 static_assert(sizeof(RtSurfaceMaterial)==32,"Surface material shader layout");
35 37 static_assert(sizeof(RtParameters)==416 && sizeof(RtInstance)==64 && sizeof(Vertex)==32 && sizeof(RtGlassMaterial)==32,"Managed/native/shader ABI mismatch");
36 38 struct SharedTexture {
37 39 ComPtr<ID3D12Resource> resource;
@@ -39,6 +41,7 @@ struct SharedTexture {
39 41 ComPtr<ID3D11ShaderResourceView> srv;
40 42 ComPtr<ID3D11UnorderedAccessView> uav;
41 43 };
44 struct DynamicTexture { SharedTexture shared; ComPtr<ID3D11ShaderResourceView> source; UINT width=0,height=0; bool dirty=false; };
42 45 class Context {
43 46 public:
44 47 std::mutex mutex;
@@ -46,7 +49,7 @@ public:
46 49 ComPtr<ID3D11Device5> device11;
47 50 ComPtr<ID3D11DeviceContext4> context11;
48 51 ComPtr<ID3DDeviceContextState> isolatedState;
49 ComPtr<ID3D11ComputeShader> pack;
52 ComPtr<ID3D11ComputeShader> pack,copyScreen;
50 53 ComPtr<ID3D11PixelShader> composite;
51 54 ComPtr<ID3D11VertexShader> fullscreen;
52 55 ComPtr<ID3D11Buffer> constants11;
@@ -63,6 +66,10 @@ public:
63 66 ComPtr<ID3D12Resource> tlas, scratch, instances, constants;
64 67 SharedTexture positions, normals, result, reflection, indirect,glassReflection,glassData,filtered,glassRefraction,moments,filteredReflection;
65 68 ComPtr<ID3D12Resource> glassBuffer,lightBuffer;
69 ComPtr<ID3D12Resource> surfaceBuffer;
70 std::vector<RtSurfaceMaterial> surfaces;
71 std::unordered_map<uint32_t,DynamicTexture> dynamicTextures;
72 uint64_t dynamicPixels=0;
66 73 std::vector<RtLight> lights;
67 74 ComPtr<ID3D12QueryHeap> timer;
68 75 ComPtr<ID3D12Resource> timerReadback;
@@ -143,7 +150,7 @@ public:
143 150 ranges[2].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; ranges[2].NumDescriptors = 1024; ranges[2].BaseShaderRegister=10;
144 151 ranges[3].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; ranges[3].NumDescriptors = 4; ranges[3].BaseShaderRegister=6;
145 152 ranges[4].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; ranges[4].NumDescriptors = 3; ranges[4].BaseShaderRegister=1036;
146 D3D12_ROOT_PARAMETER parameters[13]{};
153 D3D12_ROOT_PARAMETER parameters[14]{};
147 154 parameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;
148 155 parameters[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
149 156 for (int i = 2; i < 4; ++i) { parameters[i].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; parameters[i].DescriptorTable.NumDescriptorRanges = 1; parameters[i].DescriptorTable.pDescriptorRanges = &ranges[i-2]; }
@@ -154,8 +161,9 @@ public:
154 161 parameters[10].ParameterType=D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; parameters[10].Constants.ShaderRegister=1; parameters[10].Constants.Num32BitValues=1;
155 162 parameters[11].ParameterType=D3D12_ROOT_PARAMETER_TYPE_SRV;parameters[11].Descriptor.ShaderRegister=1035;
156 163 parameters[12].ParameterType=D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; parameters[12].DescriptorTable.NumDescriptorRanges=1; parameters[12].DescriptorTable.pDescriptorRanges=&ranges[4];
164 parameters[13].ParameterType=D3D12_ROOT_PARAMETER_TYPE_SRV;parameters[13].Descriptor.ShaderRegister=1039;
157 165 D3D12_STATIC_SAMPLER_DESC sampler{}; sampler.Filter=D3D12_FILTER_MIN_MAG_MIP_LINEAR; sampler.AddressU=sampler.AddressV=sampler.AddressW=D3D12_TEXTURE_ADDRESS_MODE_WRAP; sampler.MaxLOD=D3D12_FLOAT32_MAX;
158 D3D12_ROOT_SIGNATURE_DESC signature{}; signature.NumParameters = 13; signature.pParameters = parameters; signature.NumStaticSamplers=1; signature.pStaticSamplers=&sampler;
166 D3D12_ROOT_SIGNATURE_DESC signature{}; signature.NumParameters = 14; signature.pParameters = parameters; signature.NumStaticSamplers=1; signature.pStaticSamplers=&sampler;
159 167 ComPtr<ID3DBlob> blob, errors;
160 168 Check(D3D12SerializeRootSignature(&signature, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &errors), "Serialize root signature");
161 169 Check(device->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&root)), "Root signature");
@@ -169,6 +177,10 @@ public:
169 177 RtGlassMaterial clearGlass{{1,1,1,.04f},{1.52f,.01f,0,0}}; Upload(glassBuffer.Get(),&clearGlass,sizeof(clearGlass));
170 178 lightBuffer=Buffer(64*sizeof(RtLight),D3D12_HEAP_TYPE_UPLOAD,D3D12_RESOURCE_STATE_GENERIC_READ);
171 179 materials.emplace(0,Material{});
180 Check(device11->CreateComputeShader(CopyScreenBytes,sizeof(CopyScreenBytes),nullptr,&copyScreen),"Screen texture copier");
181 surfaceBuffer=Buffer(1024*sizeof(RtSurfaceMaterial),D3D12_HEAP_TYPE_UPLOAD,D3D12_RESOURCE_STATE_GENERIC_READ);
182 surfaces.resize(1024);for(auto& s:surfaces) for(float& v:s.colorEmission)v=1;
183 Upload(surfaceBuffer.Get(),surfaces.data(),surfaces.size()*sizeof(RtSurfaceMaterial));
172 184 ready = true; strcpy_s(status.message, "DXR 1.1 ready; waiting for scene geometry");
173 185 }
174 186 ComPtr<ID3D12Resource> Buffer(uint64_t bytes, D3D12_HEAP_TYPE heap, D3D12_RESOURCE_STATES state, D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE) {
@@ -194,10 +206,10 @@ public:
194 206 Check(fence->SetEventOnCompletion(submitted, completed), "Fence completion");
195 207 if (WaitForSingleObject(completed, 2000) != WAIT_OBJECT_0) throw Failure(DXGI_ERROR_DEVICE_HUNG, "Ray tracing GPU timeout; effect disabled");
196 208 }
197 SharedTexture Texture(DXGI_FORMAT format) {
209 SharedTexture Texture(DXGI_FORMAT format,UINT textureWidth=0,UINT textureHeight=0) {
198 210 SharedTexture texture;
199 211 D3D12_HEAP_PROPERTIES props{}; props.Type = D3D12_HEAP_TYPE_DEFAULT;
200 D3D12_RESOURCE_DESC desc{}; desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; desc.Width = width; desc.Height = height; desc.DepthOrArraySize = 1; desc.MipLevels = 1; desc.Format = format; desc.SampleDesc.Count = 1; desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS;
212 D3D12_RESOURCE_DESC desc{}; desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; desc.Width = textureWidth?textureWidth:width; desc.Height = textureHeight?textureHeight:height; desc.DepthOrArraySize = 1; desc.MipLevels = 1; desc.Format = format; desc.SampleDesc.Count = 1; desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS;
201 213 Check(device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_SHARED, &desc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&texture.resource)), "Shared ray tracing texture");
202 214 HANDLE handle = nullptr; Check(device->CreateSharedHandle(texture.resource.Get(), nullptr, GENERIC_ALL, nullptr, &handle), "Texture handle");
203 215 HRESULT hr = device11->OpenSharedResource1(handle, IID_PPV_ARGS(&texture.texture)); CloseHandle(handle); Check(hr, "Open D3D12 texture in D3D11");
@@ -403,6 +415,20 @@ public:
403 415 ComPtr<ID3DDeviceContextState> previous; context11->SwapDeviceContextState(isolatedState.Get(), &previous);
404 416 try {
405 417 context11->ClearState(); context11->UpdateSubresource(constants11.Get(), 0, nullptr, &params, 0, 0);
418 for(auto& entry:dynamicTextures) {
419 auto& d=entry.second;if(!d.source || !d.shared.resource)continue;
420 if(d.dirty) {
421 ID3D11ShaderResourceView* src=d.source.Get();ID3D11UnorderedAccessView* dst=d.shared.uav.Get();
422 context11->CSSetShaderResources(0,1,&src);context11->CSSetUnorderedAccessViews(0,1,&dst,nullptr);
423 context11->CSSetShader(copyScreen.Get(),nullptr,0);context11->Dispatch((d.width+7)/8,(d.height+7)/8,1);
424 context11->ClearState();d.dirty=false;
425 }
426 auto handle=descriptors->GetCPUDescriptorHandleForHeapStart();
427 handle.ptr+=(18+entry.first)*device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
428 D3D12_SHADER_RESOURCE_VIEW_DESC view{};view.Format=DXGI_FORMAT_R16G16B16A16_FLOAT;view.ViewDimension=D3D12_SRV_DIMENSION_TEXTURE2D;
429 view.Shader4ComponentMapping=D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;view.Texture2D.MipLevels=1;
430 device->CreateShaderResourceView(d.shared.resource.Get(),&view,handle);
431 }
406 432 ID3D11Buffer* cb = constants11.Get(); context11->CSSetConstantBuffers(0, 1, &cb);
407 433 ID3D11ShaderResourceView* packViews[] = {depth, normal, light, material}; context11->CSSetShaderResources(0, 4, packViews);
408 434 ID3D11UnorderedAccessView* writes[] = {positions.uav.Get(), normals.uav.Get()}; context11->CSSetUnorderedAccessViews(0, 2, writes, nullptr);
@@ -410,6 +436,8 @@ public:
410 436 context11->ClearState();
411 437 Check(context11->Signal(fence11.Get(), submitted + 1), "Signal D3D11 input"); context11->Flush();
412 438 Check(queue->Wait(fence.Get(), submitted + 1), "Wait for D3D11 inputs");
439 for(auto& d:dynamicTextures) if(d.second.source && d.second.shared.resource)
440 Barrier(d.second.shared.resource.Get(),D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
413 441 Barrier(positions.resource.Get(), D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
414 442 Barrier(normals.resource.Get(), D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
415 443 Barrier(result.resource.Get(), D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
@@ -428,6 +456,7 @@ public:
428 456 gpu.ptr+=3*device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); list->SetComputeRootDescriptorTable(7,gpu);
429 457 list->SetComputeRootShaderResourceView(9,glassBuffer->GetGPUVirtualAddress());
430 458 list->SetComputeRootShaderResourceView(11,lightBuffer->GetGPUVirtualAddress());
459 list->SetComputeRootShaderResourceView(13,surfaceBuffer->GetGPUVirtualAddress());
431 460 list->EndQuery(timer.Get(),D3D12_QUERY_TYPE_TIMESTAMP,0);
432 461 Barrier(moments.resource.Get(),D3D12_RESOURCE_STATE_COMMON,D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
433 462 list->Dispatch((width+7)/8, (height+7)/8, 1);
@@ -479,6 +508,8 @@ public:
479 508 Barrier(moments.resource.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS,D3D12_RESOURCE_STATE_COMMON);
480 509 }
481 510 list->EndQuery(timer.Get(),D3D12_QUERY_TYPE_TIMESTAMP,1);
511 for(auto& d:dynamicTextures) if(d.second.source && d.second.shared.resource)
512 Barrier(d.second.shared.resource.Get(),D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,D3D12_RESOURCE_STATE_COMMON);
482 513 list->ResolveQueryData(timer.Get(),D3D12_QUERY_TYPE_TIMESTAMP,0,2,timerReadback.Get(),0);timerReady=true;
483 514 historyValid=true;
484 515 Check(list->Close(), "Close ray dispatch"); ID3D12CommandList* commands[] = {list.Get()}; queue->ExecuteCommandLists(1, commands);
@@ -564,10 +595,53 @@ RT_API int RtMesh(void* context, uint32_t id, const float* xyz, uint32_t vertice
564 595 }
565 596 catch (...) { c->Fail(Failure(E_OUTOFMEMORY,"Model staging allocation")); return E_OUTOFMEMORY; }
566 597 }
567 RT_API int RtMaterial(void* context,uint32_t id,const uint8_t* dds,uint32_t bytes) {
598 RT_API int RtMaterial(void* context,uint32_t id,const uint8_t* dds,uint32_t bytes,uint32_t linear) {
568 599 if(!context || !id || id>=1024) return E_INVALIDARG; auto c=(Context*)context; std::lock_guard<std::mutex> guard(c->mutex);
569 600 if(c->materials.count(id)) return 0;
570 try { Material material; if(!ParseDds(dds,bytes,material)) { c->materials.emplace(id,Material{}); return 1; } c->materials.emplace(id,std::move(material)); return 0; } catch(...) { return E_OUTOFMEMORY; }
601 try {
602 Material material;
603 if(!ParseDds(dds,bytes,material,linear!=0)) {
604 Material fallback;
605 // A missing extension map has no emission or colorization. The
606 // gray color-texture fallback would otherwise invent glowing faces.
607 if(linear) {fallback.format=DXGI_FORMAT_R8G8B8A8_UNORM;fallback.data={0,0,0,0};}
608 c->materials.emplace(id,std::move(fallback));return 1;
609 }
610 c->materials.emplace(id,std::move(material));return 0;
611 } catch(...) {return E_OUTOFMEMORY;}
612 }
613 RT_API int RtSurfaceMaterials(void* context,const RtSurfaceMaterial* materials,uint32_t count) {
614 if(!context || !materials || !count || count>1024)return E_INVALIDARG;
615 auto c=(Context*)context;std::lock_guard<std::mutex> guard(c->mutex);
616 for(uint32_t i=0;i<count;i++) {
617 if(materials[i].texture>=1024 || (materials[i].flags&~15u))return E_INVALIDARG;
618 for(float v:materials[i].colorEmission)if(!std::isfinite(v)||v<0||v>32)return E_INVALIDARG;
619 }
620 if(memcmp(c->surfaces.data(),materials,count*sizeof(RtSurfaceMaterial))==0)return 0;
621 try {c->WaitPrevious();memcpy(c->surfaces.data(),materials,count*sizeof(RtSurfaceMaterial));
622 c->Upload(c->surfaceBuffer.Get(),c->surfaces.data(),c->surfaces.size()*sizeof(RtSurfaceMaterial));return 0;}
623 catch(const Failure& e){c->Fail(e);return e.code;}catch(...){return E_FAIL;}
624 }
625 RT_API int RtDynamicMaterial(void* context,uint32_t id,void* textureSrv) {
626 if(!context || id<992 || id>=1024)return E_INVALIDARG;
627 auto c=(Context*)context;std::lock_guard<std::mutex> guard(c->mutex);
628 try {
629 c->WaitPrevious();auto& d=c->dynamicTextures[id];
630 if(!textureSrv){d.source.Reset();return 0;}
631 auto source=(ID3D11ShaderResourceView*)textureSrv;D3D11_SHADER_RESOURCE_VIEW_DESC srv{};source->GetDesc(&srv);
632 if(srv.ViewDimension!=D3D11_SRV_DIMENSION_TEXTURE2D || srv.Texture2D.MostDetailedMip!=0)return 1;
633 ComPtr<ID3D11Resource> resource;source->GetResource(&resource);ComPtr<ID3D11Texture2D> texture;
634 if(FAILED(resource.As(&texture)))return 1;
635 D3D11_TEXTURE2D_DESC desc{};texture->GetDesc(&desc);
636 if(!desc.Width || !desc.Height || desc.Width>2048 || desc.Height>2048 || desc.ArraySize!=1 || desc.SampleDesc.Count!=1)return 1;
637 if(desc.Width!=d.width || desc.Height!=d.height) {
638 uint64_t pixels=c->dynamicPixels-uint64_t(d.width)*d.height+uint64_t(desc.Width)*desc.Height;
639 if(pixels>16u*1024*1024)return 1;
640 auto shared=c->Texture(DXGI_FORMAT_R16G16B16A16_FLOAT,desc.Width,desc.Height);
641 d.shared=std::move(shared);d.width=desc.Width;d.height=desc.Height;c->dynamicPixels=pixels;
642 }
643 d.source=source;d.dirty=true;return 0;
644 }catch(const Failure& e){c->Fail(e);return e.code;}catch(...){return E_FAIL;}
571 645 }
572 646 RT_API int RtRetainMeshes(void* context,const uint32_t* ids,uint32_t count) {
573 647 if(!context) return E_INVALIDARG; auto c=(Context*)context; std::lock_guard<std::mutex> guard(c->mutex);
Modified src/XFE.SeRayTracing.Native/Native.h +4 -1
@@ -11,6 +11,7 @@ struct RtParameters {
11 11 };
12 12 struct RtGlassMaterial { float transmission[4],optics[4]; };
13 13 struct RtLight { float positionRange[4],colorIntensity[4],directionCone[4],settings[4]; };
14 struct RtSurfaceMaterial { float colorEmission[4]; uint32_t texture, flags, reserved[2]; };
14 15 struct RtInstance { float transform[12]; uint32_t mesh; float tint[3]; };
15 16 struct RtStatus {
16 17 uint32_t version, tier, meshes, instances;
@@ -21,7 +22,9 @@ struct RtStatus {
21 22 };
22 23 RT_API int RtCreate(void* device11, void** output);
23 24 RT_API int RtMesh(void* context, uint32_t id, const float* xyzuv, uint32_t vertices, const uint32_t* indices, uint32_t indexCount, const uint32_t* materials);
24 RT_API int RtMaterial(void* context, uint32_t id, const uint8_t* dds, uint32_t bytes);
25 RT_API int RtMaterial(void* context, uint32_t id, const uint8_t* dds, uint32_t bytes, uint32_t linear = 0);
26 RT_API int RtSurfaceMaterials(void* context, const RtSurfaceMaterial* materials, uint32_t count);
27 RT_API int RtDynamicMaterial(void* context, uint32_t id, void* textureSrv);
25 28 RT_API int RtGlassMaterials(void* context, const RtGlassMaterial* materials, uint32_t count);
26 29 RT_API int RtLights(void* context, const RtLight* lights, uint32_t count);
27 30 RT_API int RtRetainMeshes(void* context, const uint32_t* ids, uint32_t count);
Modified src/XFE.SeRayTracing.Native/Rays.hlsl +21 -8
@@ -28,6 +28,8 @@ StructuredBuffer<Light> lights : register(t1035);
28 28 Texture2D<float4> previousGlassData : register(t1036);
29 29 Texture2D<float4> previousGlassReflections : register(t1037);
30 30 Texture2D<float4> previousMoments : register(t1038);
31 struct SurfaceMaterial { float4 colorEmission; uint texture,flags; uint2 reserved; };
32 StructuredBuffer<SurfaceMaterial> surfaceMaterials : register(t1039);
31 33 SamplerState linearWrap : register(s0);
32 34 cbuffer Parameters : register(b0) {
33 35 row_major float4x4 inverseViewProjection;
@@ -42,7 +44,7 @@ cbuffer Parameters : register(b0) {
42 44 cbuffer FilterParameters : register(b1) { uint filterStep; };
43 45 static const float PI=3.14159265359;
44 46 static const uint GLASS=0x40000000, VOXEL=0x80000000;
45 struct Hit { float3 p,n,albedo; float metal,rough,t; uint flags,glass; };
47 struct Hit { float3 p,n,albedo,emission; float metal,rough,t; uint flags,glass; };
46 48 float Random(inout uint state) { state=state*747796405u+2891336453u; uint v=((state>>((state>>28u)+4u))^state)*277803737u; v=(v>>22u)^v;return (v>>8)*0.000000059604644775; }
47 49 float Luma(float3 v) { return dot(v,float3(.2126,.7152,.0722)); }
48 50 void Basis(float3 n,out float3 t,out float3 b) { t=normalize(cross(abs(n.z)<.9?float3(0,0,1):float3(0,1,0),n));b=cross(n,t); }
@@ -68,10 +70,16 @@ bool Closest(float3 origin,float3 direction,float range,out Hit hit,bool shade=t
68 70 // each ordered hit and rays continue through them; never first-hit-occlude
69 71 // a pane as if it were opaque.
70 72 RayQuery<RAY_FLAG_FORCE_OPAQUE> q;
71 q.TraceRayInline(scene,RAY_FLAG_NONE,0xff,ray);while(q.Proceed()) {}
72 if(q.CommittedStatus()==COMMITTED_NOTHING) return false;
73 InstanceMaterial instance=instances[q.CommittedInstanceID()];
74 uint4 tri=triangles[instance.triangleOffset+q.CommittedPrimitiveIndex()];
73 InstanceMaterial instance;uint4 tri;SurfaceMaterial surface;bool visible=false;
74 [loop] for(uint layer=0;layer<8;layer++) {
75 q.TraceRayInline(scene,RAY_FLAG_NONE,0xff,ray);while(q.Proceed()) {}
76 if(q.CommittedStatus()==COMMITTED_NOTHING)return false;
77 instance=instances[q.CommittedInstanceID()];tri=triangles[instance.triangleOffset+q.CommittedPrimitiveIndex()];
78 surface=surfaceMaterials[(tri.w&(GLASS|VOXEL))?0:((tri.w>>20)&1023)];
79 if(!(surface.flags&8)) {visible=true;break;}
80 ray.TMin=q.CommittedRayT()+.001;if(ray.TMin>=ray.TMax)return false;
81 }
82 if(!visible)return false;
75 83 Vertex a=vertices[tri.x],b=vertices[tri.y],c=vertices[tri.z];
76 84 float2 bary=q.CommittedTriangleBarycentrics();float3 w=float3(1-bary.x-bary.y,bary);
77 85 float3 localNormal=normalize(cross(b.position-a.position,c.position-a.position));
@@ -87,9 +95,11 @@ bool Closest(float3 origin,float3 direction,float range,out Hit hit,bool shade=t
87 95 cm=VoxelTex(a,p,blend)*w.x+VoxelTex(b,p,blend)*w.y+VoxelTex(c,p,blend)*w.z;
88 96 hit.albedo=cm.rgb;
89 97 } else {
90 cm=Tex(tri.w&1023,uv);
98 cm=(surface.flags&2)?colorTextures[NonUniformResourceIndex(surface.texture)].SampleLevel(linearWrap,saturate(uv),0):Tex(tri.w&1023,uv);
99 if(surface.flags&4)cm.a=0; // generated LCD alpha is opacity, not metalness
91 100 uint extra=(tri.w>>10)&1023;
92 float mask=extra==0?0:colorTextures[NonUniformResourceIndex(extra)].SampleLevel(linearWrap,uv,1).a;
101 float4 extensions=extra==0?0:colorTextures[NonUniformResourceIndex(extra)].SampleLevel(linearWrap,uv,0);
102 float mask=(surface.flags&4)?0:extensions.a;
93 103 float brightness=max(cm.r,max(cm.g,cm.b));
94 104 float value=brightness<=.0031308?brightness*12.92:1.055*pow(brightness,1/2.4)-.055;
95 105 float3 tint=a.tint*w.x+b.tint*w.y+c.tint*w.z+instance.tint;
@@ -98,6 +108,9 @@ bool Closest(float3 origin,float3 direction,float range,out Hit hit,bool shade=t
98 108 float3 painted=hsv.z*lerp(1,hue,hsv.y);
99 109 painted=lerp(painted/12.92,pow((painted+.055)/1.055,2.4),step(.04045,painted));
100 110 hit.albedo=lerp(cm.rgb,painted,mask*(1-cm.a));
111 if(surface.flags&1)hit.albedo*=surface.colorEmission.rgb;
112 float emissivity=(surface.flags&4)?1:saturate(extensions.g-1.0/255);
113 hit.emission=hit.albedo*emissivity*((surface.flags&1)?surface.colorEmission.a:1);
101 114 }
102 115 hit.metal=cm.a;hit.rough=(hit.flags&VOXEL)?.85:.4;
103 116 return true;
@@ -187,7 +200,7 @@ float4 PathRadiance(float3 origin,float3 direction,float range,inout uint rng) {
187 200 found=1;
188 201 float3 l=normalize(sunDirection.xyz);float cosine=saturate(dot(h.n,l));
189 202 float3 lit=cosine>0?Visibility(h.p+h.n*.04,l,192):0;
190 radiance+=throughput*(Bsdf(h,-direction,l)*sunColor.rgb*cosine*lit+LocalLighting(h,-direction,rng)+h.albedo*.018);
203 radiance+=throughput*(h.emission+Bsdf(h,-direction,l)*sunColor.rgb*cosine*lit+LocalLighting(h,-direction,rng)+h.albedo*.018);
191 204 if(++bounce>=maxBounces) break;
192 205 float3 weight;direction=SampleBsdf(h,-direction,rng,weight);throughput*=weight;
193 206 if(max(throughput.x,max(throughput.y,throughput.z))<.001) break;
Modified tests/XFE.SeRayTracing.Tests/GpuTest.cpp +49 -1
@@ -260,5 +260,53 @@ int main() {
260 260 context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Foreground readback");
261 261 color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
262 262 printf("glass behind opaque foreground=%.6f\n",color[0]);valid=valid && color[0]<.001;context->Unmap(readback.Get(),0);
263 RtDestroy(ray); puts(valid?"PASS GPU occlusion, textured offscreen reflection, indirect bounce, interop, state restoration, validation":"FAIL ray output"); return valid?0:1;
263 // Authored extension emission is linear data and radiates without a sun/lamp.
264 auto ext=dds;ext[128]=0;ext[129]=128;ext[130]=ext[131]=0;
265 Check(RtMaterial(ray,2,ext.data(),(uint32_t)ext.size(),1),"Linear emission map");
266 keep=13;Check(RtRetainMeshes(ray,&keep,1),"Emissive-only scene");
267 uint32_t emissiveIds[]={1|(2<<10),1|(2<<10)};
268 Check(RtMesh(ray,13,background.data(),4,indices,6,emissiveIds),"Emissive text plane");instance.mesh=13;
269 Check(RtLights(ray,nullptr,0),"No lamps");p.sunColor[0]=p.sunColor[1]=p.sunColor[2]=0;
270 p.settings[3]=3;p.transport[2]=0;p.effects[0]=1;p.history[0]=0;
271 for(int frame=0;frame<3;frame++)Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Emission reflection frame");
272 context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Emission readback");
273 color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
274 printf("unlit reflected emission green=%.6f (linear mask expected 0.516)\n",color[1]);
275 valid=valid && color[1]>.51 && color[1]<.525 && color[0]<.001;context->Unmap(readback.Get(),0);
276 valid=valid && RtMaterial(ray,3,nullptr,0,1)==1;
277 keep=16;Check(RtRetainMeshes(ray,&keep,1),"Missing extension scene");
278 uint32_t missingIds[]={1|(3<<10),1|(3<<10)};
279 Check(RtMesh(ray,16,background.data(),4,indices,6,missingIds),"Missing extension plane");instance.mesh=16;
280 for(int frame=0;frame<3;frame++)Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Missing emission reflection");
281 context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Missing emission readback");
282 color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
283 printf("missing extension green=%.6f (ambient only)\n",color[1]);
284 valid=valid && color[1]>.017 && color[1]<.019;context->Unmap(readback.Get(),0);
285 // Real D3D11 render-target content, behind the camera, reflected by real DXR glass.
286 uint32_t liveKeep[]={14,15};Check(RtRetainMeshes(ray,liveKeep,2),"Live LCD scene");
287 Check(RtMesh(ray,14,vertices,4,indices,6,glassIds),"LCD receiver glass");
288 uint32_t screenIds[]={1u<<20,1u<<20};Check(RtMesh(ray,15,background.data(),4,indices,6,screenIds),"LCD geometry");
289 room[0]=instance;room[0].mesh=14;room[1]=instance;room[1].mesh=15;
290 RtSurfaceMaterial surfaces[2]{};for(auto& s:surfaces)for(float& v:s.colorEmission)v=1;
291 surfaces[1].texture=992;surfaces[1].flags=7;Check(RtSurfaceMaterials(ray,surfaces,2),"Live LCD material");
292 std::vector<float> screen(64*64*4,0);auto live=Make(device.Get(),DXGI_FORMAT_R32G32B32A32_FLOAT,screen.data(),16,D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET);
293 if(RtDynamicMaterial(ray,1,live.srv.Get())>=0){puts("FAIL live material accepted static slot");return 1;}
294 std::fill(depth.begin(),depth.end(),0.0f);context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);
295 p.settings[3]=0;p.transport[2]=1;p.history[0]=0;
296 for(int mode=0;mode<3;mode++) {
297 std::fill(screen.begin(),screen.end(),0.0f);for(size_t i=0;i<screen.size();i+=4)screen[i+(mode?2:0)]=1;
298 context->UpdateSubresource(live.texture.Get(),0,nullptr,screen.data(),64*16,0);
299 Check(RtDynamicMaterial(ray,992,live.srv.Get()),"Copy current LCD pixels");
300 if(mode==2){surfaces[1].flags|=8;Check(RtSurfaceMaterials(ray,surfaces,2),"Hide LCD");}
301 for(int frame=0;frame<3;frame++) {
302 context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
303 Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"Live glass reflection");
304 }
305 context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Live LCD readback");
306 color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
307 printf("live LCD mode %d glass RGB=%.6f,%.6f,%.6f\n",mode,color[0],color[1],color[2]);
308 valid=valid && (mode==0?(color[0]>.02 && color[2]<.001):mode==1?(color[2]>.02 && color[0]<.001):(color[0]<.001 && color[2]<.001));
309 context->Unmap(readback.Get(),0);
310 }
311 RtDestroy(ray); puts(valid?"PASS GPU occlusion, textured offscreen reflection, indirect bounce, live emissive LCD, interop, state restoration, validation":"FAIL ray output"); return valid?0:1;
264 312 }
Modified tools/Build-RayTracing.ps1 +2 -0
@@ -15,6 +15,8 @@ foreach ($seShader in @(@('Pack','cs_5_0','PackBytes','PackShader.h'),@('Composi
15 15 }
16 16 & (Join-Path $seSdkBin 'dxc.exe') -O3 -WX -T cs_6_5 -E Trace -Vn RaysBytes -Fh (Join-Path $seBuild 'RaysShader.h') (Join-Path $seNative 'Rays.hlsl')
17 17 if ($LASTEXITCODE) { throw 'DXC failed.' }
18 & (Join-Path $seSdkBin 'fxc.exe') /nologo /O3 /WX /T cs_5_0 /E CopyScreen /Vn CopyScreenBytes /Fh (Join-Path $seBuild 'CopyScreenShader.h') (Join-Path $seNative 'CopyScreen.hlsl')
19 if ($LASTEXITCODE) { throw 'Screen copy shader failed.' }
18 20 & (Join-Path $seSdkBin 'dxc.exe') -O3 -WX -T cs_6_5 -E Denoise -Vn DenoiseBytes -Fh (Join-Path $seBuild 'DenoiseShader.h') (Join-Path $seNative 'Rays.hlsl')
19 21 if ($LASTEXITCODE) { throw 'Denoiser DXC failed.' }
20 22 $seResponse = Join-Path $seBuild 'native.rsp'