using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; using HarmonyLib; using Newtonsoft.Json.Linq; using Sandbox.Game.Entities; using Sandbox.Game.Entities.Cube; using Sandbox.Game.World; using VRage.Game.Entity; using VRage.Game.Models; using VRage.Render.Scene; using VRageMath; namespace XFE.SeAgent.Plugin.Graphics { /// Opt-in hardware DXR experiment. World data is copied only on the game /// thread; immutable snapshots cross to the render thread. No world mutations. internal sealed partial class RayTracing : IDisposable { [StructLayout(LayoutKind.Sequential)] internal struct Instance { public float M11, M21, M31, M41, M12, M22, M32, M42, M13, M23, M33, M43; public uint Mesh; public Vector3 Tint; public ulong Key; public Instance(MatrixD m, Vector3D origin, uint mesh, Vector3 tint, ulong key=0) { M11=(float)m.M11; M21=(float)m.M21; M31=(float)m.M31; M41=(float)(m.M41-origin.X); M12=(float)m.M12; M22=(float)m.M22; M32=(float)m.M32; M42=(float)(m.M42-origin.Y); M13=(float)m.M13; M23=(float)m.M23; M33=(float)m.M33; M43=(float)(m.M43-origin.Z); Mesh=mesh; Tint=tint; Key=key; } } [StructLayout(LayoutKind.Sequential)] private struct Parameters { public Matrix InverseViewProjection, InverseView; public Vector4 OriginDelta, SunDirection, Settings; public uint Width, Height, TraceWidth, TraceHeight; public Vector4 SunColor, Effects; public Matrix PreviousViewProjection; public Vector4 PreviousCamera, OriginChange, History, Transport; public Matrix ViewProjection; public Vector4 PathSettings, EnvironmentSettings; } [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] private struct NativeStatus { public uint Version, Tier, Meshes, Instances; public ulong Triangles, Frames; public double CpuMilliseconds, GpuMilliseconds; public int LastHresult; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=384)] public string Message; } private const string Library = "XFE.SeRayTracing.Native.dll"; [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtCreate(IntPtr device, out IntPtr context); [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtMesh(IntPtr context, uint id, float[] vertices, uint count, uint[] indices, uint indexCount, uint[] materials); [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtMaterial(IntPtr context, uint id, byte[] dds, uint bytes, uint linear); [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtRetainMeshes(IntPtr context, uint[] ids, uint count); [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); [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern void RtGetStatus(IntPtr context, out NativeStatus status); [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtEnvironment(IntPtr context,IntPtr cubeSrv); [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern int RtFinishTransparency(IntPtr context,IntPtr depth,IntPtr light,IntPtr target); [DllImport(Library, CallingConvention=CallingConvention.Cdecl)] private static extern void RtDestroy(IntPtr context); [DllImport("kernel32", CharSet=CharSet.Unicode, SetLastError=true)] private static extern IntPtr LoadLibraryEx(string path, IntPtr file, uint flags); private sealed class Options { public bool Enabled, PathTracing, FullResolution; public float Radius=3, Strength=.3f, ShadowStrength=.15f, Reflections=.8f, Indirect=.6f; public int Diagnostic, Bounces=3, VoxelLod=0, Samples=1; public bool Glass=true, Voxels=true, Denoise=true; } private sealed class Mesh { public uint Id; public float[] Vertices; public uint[] Indices, Materials; public MeshPart[] Parts; } private sealed class MeshPart { public string Name; public int Start,Count; } private sealed class Material { public uint Id; public string Path; public bool Linear; public Task Bytes; } private sealed class CellPart { public Mesh Mesh; public Matrix Matrix; public Vector3 Tint; public Vector2 UvOffset; } private sealed class CellGeometry { public long Signature; public Mesh Mesh; } private sealed class Placement { public MyEntity Owner; public Matrix Local; public uint Mesh; public MySlimBlock Block; } private sealed class Snapshot { public Vector3D Origin; public Instance[] Instances; public uint[] RenderIds; public Matrix[] LocalTransforms; public int Count; public Mesh[] Meshes; public Material[] Materials; public GlassMaterial[] Glass; public Light[] Lights; public ScreenBinding[] Screens; public long Created; public int Generation; } private readonly Action log; private readonly Harmony harmony = new Harmony("xfestudio.se-agent.raytracing.v1"); private readonly object renderLock = new object(); private readonly Dictionary modelCache = new Dictionary(); private readonly Dictionary meshesById = new Dictionary(); private readonly Dictionary cellCache = new Dictionary(); private readonly HashSet watchedGrids = new HashSet(); private volatile HashSet watchedIds = new HashSet(); private readonly HashSet uploaded = new HashSet(); private readonly HashSet texturesUploaded = new HashSet(); private readonly Dictionary materialCache = new Dictionary(StringComparer.OrdinalIgnoreCase); private readonly SemaphoreSlim textureReaders = new SemaphoreSlim(2); private readonly Dictionary members = new Dictionary(); private List placements = new List(); private List buildingPlacements; private IEnumerator collector; private volatile Options options = new Options(); private readonly SnapshotExchange snapshots = new SnapshotExchange(); private readonly Dictionary captureMatrices = new Dictionary(); private readonly Dictionary renderMatrices = new Dictionary(); private Instance[] renderInstances=new Instance[0]; private Mesh[] placementMeshes = new Mesh[0]; private Material[] captureMaterials = new Material[0]; private volatile JObject renderStatus = new JObject { ["state"]="disabled" }; private IntPtr native, nativeLibrary; private Mesh[] retainedMeshes; private volatile string failure; private Matrix previousViewProjection; private Vector3D previousCamera,previousOrigin; private long previousFrameAt; private bool opaqueRendered; private uint nextMesh; private long lastCollect, lastSnapshot; private Vector3D lastCollectionOrigin; private int scannedBlocks, skippedModels; private volatile int textureFallbacks; private volatile bool worldReady; private int sceneGeneration, capturedGeneration, resetNative, resetHistory; private readonly string settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "XFE", "SpaceEngineersAgent", "raytracing.json"); private MySession session; private static RayTracing active; private static readonly Type Renderer = AccessTools.TypeByName("VRageRender.MyRender11"); private static readonly Type GBuffer = AccessTools.TypeByName("VRage.Render11.Resources.MyGBuffer"); // TypeByName can scan every loaded assembly. Resolve these immutable // renderer contracts once, not on every frame / screen refresh. private static readonly Type Managers = AccessTools.TypeByName("VRage.Render11.Common.MyManagers"); private static readonly Type TextureInterface = AccessTools.TypeByName("VRage.Render11.Resources.ITexture"); private const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; public RayTracing(Action logger) { log=logger; var scheduler = AccessTools.TypeByName("VRage.Render11.Render.MyRenderScheduler"); var done = scheduler?.GetMethod("Done",Flags,null,Type.EmptyTypes,null); if (done==null || done.ReturnType!=typeof(void)) throw new NotSupportedException("Installed renderer end-of-scene hook not found."); // After opaque/transparent command submission, before exposure, bloom, // tone mapping and HUD. The engine's cached state is restored by native. harmony.Patch(done, postfix:new HarmonyMethod(typeof(RayTracing),nameof(AfterScene))); var resolve=AccessTools.TypeByName("VRage.Render11.GBufferResolve.MyGBufferResolver"); harmony.Patch(AccessTools.DeclaredMethod(resolve,"ConsumeWork"),postfix:new HarmonyMethod(typeof(RayTracing),nameof(AfterOpaqueLighting))); InstallScreenCapture(); InstallSettingsMenu(); active=this; MyCubeGrid.OnBlockAddedGlobally+=GeometryChanged; MyCubeGrid.OnBlockRemovedGlobally+=GeometryChanged; if(File.Exists(settingsPath)) try { if(new FileInfo(settingsPath).Length>16384) throw new InvalidDataException("Ray tracing settings exceed 16 KiB."); Configure(JObject.Parse(File.ReadAllText(settingsPath)),false); } catch(Exception error) { log("Ray tracing preferences ignored: "+error.Message); } } public JObject Configure(JObject args, bool mayPersist=true) { foreach (var property in args.Properties()) { if (!new[]{"enabled","radius","strength","shadowStrength","reflections","indirect","diagnostic","persist","glass","voxels","bounces","voxelLod","pathTracing","fullResolution","samples","denoise"}.Contains(property.Name)) throw new ArgumentException("Unknown ray tracing option: "+property.Name); bool boolean=new[]{"enabled","persist","glass","voxels","pathTracing","fullResolution","denoise"}.Contains(property.Name); if(boolean ? property.Value.Type!=JTokenType.Boolean : property.Value.Type!=JTokenType.Integer && property.Value.Type!=JTokenType.Float) throw new ArgumentException("Invalid ray tracing option type: "+property.Name); if((property.Name=="diagnostic" || property.Name=="bounces" || property.Name=="voxelLod" || property.Name=="samples") && property.Value.Type!=JTokenType.Integer) throw new ArgumentException(property.Name+" must be an integer."); } var old=options; var next=new Options { Enabled=(bool?)args["enabled"]??old.Enabled, Radius=(float?)args["radius"]??old.Radius, Strength=(float?)args["strength"]??old.Strength, ShadowStrength=(float?)args["shadowStrength"]??old.ShadowStrength, Reflections=(float?)args["reflections"]??old.Reflections, Indirect=(float?)args["indirect"]??old.Indirect, Diagnostic=(int?)args["diagnostic"]??old.Diagnostic, Bounces=(int?)args["bounces"]??old.Bounces, VoxelLod=(int?)args["voxelLod"]??old.VoxelLod, Glass=(bool?)args["glass"]??old.Glass, Voxels=(bool?)args["voxels"]??old.Voxels, PathTracing=(bool?)args["pathTracing"]??old.PathTracing, FullResolution=(bool?)args["fullResolution"]??old.FullResolution, Samples=(int?)args["samples"]??old.Samples,Denoise=(bool?)args["denoise"]??old.Denoise }; if (!Finite(next.Radius) || next.Radius<.25f || next.Radius>12 || !Finite(next.Strength) || next.Strength<0 || next.Strength>1 || !Finite(next.ShadowStrength) || next.ShadowStrength<0 || next.ShadowStrength>.5f || !Finite(next.Reflections) || next.Reflections<0 || next.Reflections>1 || !Finite(next.Indirect) || next.Indirect<0 || next.Indirect>1 || next.Diagnostic<0 || next.Diagnostic>10 || next.Bounces<1 || next.Bounces>8 || next.VoxelLod<0 || next.VoxelLod>2 || next.Samples<1 || next.Samples>8) throw new ArgumentException("radius .25..12 m; strength/reflections/indirect 0..1; shadowStrength 0...5; diagnostic 0..10; bounces/samples 1..8; voxelLod 0..2."); if (next.Enabled && failure!=null) throw new InvalidOperationException("Ray tracing failed closed: "+failure+". Restart after correcting the cause."); if(mayPersist && (bool?)args["persist"]==true) { var settings=new JObject { ["enabled"]=next.Enabled,["radius"]=next.Radius,["strength"]=next.Strength, ["shadowStrength"]=next.ShadowStrength,["reflections"]=next.Reflections,["indirect"]=next.Indirect, ["glass"]=next.Glass,["voxels"]=next.Voxels,["bounces"]=next.Bounces,["voxelLod"]=next.VoxelLod, ["pathTracing"]=next.PathTracing,["fullResolution"]=next.FullResolution,["samples"]=next.Samples,["denoise"]=next.Denoise }; Directory.CreateDirectory(Path.GetDirectoryName(settingsPath)); File.WriteAllText(settingsPath+".tmp",settings.ToString()); if(File.Exists(settingsPath)) File.Replace(settingsPath+".tmp",settingsPath,settingsPath+".bak"); else File.Move(settingsPath+".tmp",settingsPath); } options=next; if(next.Voxels!=old.Voxels || next.VoxelLod!=old.VoxelLod) Interlocked.Increment(ref sceneGeneration); Interlocked.Exchange(ref resetHistory,1); return Status(); } private static bool Finite(float value) => !float.IsNaN(value) && !float.IsInfinity(value); public JObject Status() { var o=options; var s=snapshots.Current; var rendered=(JObject)renderStatus.DeepClone(); if(!o.Enabled) rendered["state"]="disabled"; else if(!worldReady) rendered["state"]="no-world"; return new JObject { ["enabled"]=o.Enabled, ["radius"]=o.Radius, ["strength"]=o.Strength, ["shadowStrength"]=o.ShadowStrength, ["pathTracing"]=o.PathTracing,["fullResolution"]=o.FullResolution,["samples"]=o.Samples,["denoise"]=o.Denoise,["mode"]=!o.Enabled?"disabled":o.PathTracing?"path-traced-near-field":"hybrid-ray-tracing", ["diagnostic"]=o.Diagnostic, ["failure"]=failure, ["render"]=rendered, ["collecting"]=collector!=null, ["reflections"]=o.Reflections,["indirect"]=o.Indirect,["textureFallbacks"]=textureFallbacks, ["glass"]=o.Glass,["voxels"]=o.Voxels,["bounces"]=o.Bounces,["voxelLod"]=o.VoxelLod, ["glassMaterials"]=glassMaterials.Count,["glassTriangles"]=glassTriangles,["voxelChunks"]=voxelChunks,["voxelTriangles"]=voxelTriangles,["voxelRevision"]=voxelRevision, ["voxelCaptureRadiusMeters"]=VoxelRadius,["voxelWorkers"]=VoxelWorkers, ["localLights"]=s?.Lights?.Length??0,["screens"]=screenStatus, ["scannedBlocks"]=scannedBlocks, ["skippedModels"]=skippedModels, ["snapshotInstances"]=s?.Count??0, ["snapshotAgeMs"]=s==null ? (JToken)null : (Stopwatch.GetTimestamp()-s.Created)*1000.0/Stopwatch.Frequency, ["implementation"]="DXR 1.1 hardware geometry rays. Near-field opaque paths run in the engine lighting resolve before transparency, using native G-buffer materials, HDR environment MIS and rigid-instance history. Glass resolves after transparency; HDR output precedes tone mapping and HUD.", ["limits"]="Near-field path tracing replaces covered opaque lighting with emission, direct-light sampling and BSDF paths; raster depth validates primary visibility. Uncovered geometry, sky and distant scenes keep native shading. Glass retains hybrid primary transparency and thin-slab secondary transport. Up to 64 fixtures, 32 live LCDs within 64 m at 10 Hz; voxel capture 320 m, primary range 256 m; HDR environment sampled from the engine far probe. No emissive-area importance sampling, ReSTIR, NRD, DLSS RR, full-scene path tracing, solid dielectric volumes, cutouts, deformation motion or secondary normal maps." }; } public void Update(bool allowed) { worldReady=allowed && MySession.Static!=null && MySession.Static.Ready; if(!ReferenceEquals(session,MySession.Static)) { ResetCapture(); session=MySession.Static; modelCache.Clear(); meshesById.Clear(); cellCache.Clear(); materialCache.Clear(); glassMaterials.Clear(); glassIds.Clear(); glassTriangles=0; nextMesh=0; Interlocked.Exchange(ref resetNative,1); } if (!options.Enabled || !worldReady || failure!=null) return; try { CheckCockpitModel(); int generation=Volatile.Read(ref sceneGeneration); if(generation!=capturedGeneration) { ResetCapture(); capturedGeneration=generation; } var now=Stopwatch.GetTimestamp(); if(lastCollect!=0 && Vector3D.DistanceSquared(lastCollectionOrigin,MySector.MainCamera.Position)>64*64) { Interlocked.Increment(ref sceneGeneration);ResetCapture();capturedGeneration=Volatile.Read(ref sceneGeneration);generation=capturedGeneration; Interlocked.Exchange(ref resetHistory,1); } if (collector==null && (lastCollect==0 || now-lastCollect>Stopwatch.Frequency*15)) { lastCollectionOrigin=MySector.MainCamera.Position; collector=Collect(MySector.MainCamera.Position).GetEnumerator(); buildingPlacements=new List(); scannedBlocks=0; skippedModels=0; lastCollect=now; } long until=now+Stopwatch.Frequency*3/1000; if (collector!=null) { // Work is sliced on the game thread; do not enumerate mutable grids on workers. int steps=0; while (steps++<512 && Stopwatch.GetTimestamp()ReferenceEquals(b.Owner,p.Owner)))buildingPlacements.Add(p); } if (buildingPlacements.Count>=100000) throw new InvalidOperationException("Visible geometry exceeded 100000 instances; refusing an incomplete scene."); } } if (now-lastSnapshot>Stopwatch.Frequency/10 && placements.Count>0) { var origin=MySector.MainCamera.Position; var next=snapshots.AcquireWritable(); if(next.Instances==null || next.Instances.Length250*250) continue; next.RenderIds[count]=RenderIdFor(p.Owner);next.LocalTransforms[count]=p.Local; next.Instances[count++]=new Instance(world,origin,p.Mesh,p.Block?.ColorMaskHSV??Vector3.Zero,unchecked((ulong)(uint)System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(p.Owner)<<32) ^ ((ulong)p.Mesh<<16) ^ (uint)p.Local.GetHashCode()); } if(captureMaterials.Length!=materialCache.Count) captureMaterials=materialCache.Values.ToArray(); next.Origin=origin; next.Count=count; next.Meshes=placementMeshes; next.Materials=captureMaterials; next.Glass=glassMaterials.ToArray(); next.Lights=CaptureLights(origin); next.Screens=screenBindings.ToArray(); next.Created=Stopwatch.GetTimestamp(); next.Generation=generation; if(generation==Volatile.Read(ref sceneGeneration)) snapshots.Publish(next); lastSnapshot=now; } } catch(Exception error) { Fail("Scene capture: "+error.Message); } } private void GeometryChanged(MySlimBlock block) { if(block?.CubeGrid!=null && watchedIds.Contains(block.CubeGrid.EntityId)) { Interlocked.Increment(ref sceneGeneration); snapshots.Clear(); Interlocked.Exchange(ref resetHistory,1); } } private void ResetCapture() { ResetScreenCapture(); snapshots.Clear(); captureMatrices.Clear(); placementMeshes=new Mesh[0]; captureMaterials=new Material[0]; collector?.Dispose(); collector=null; buildingPlacements=null; placements.Clear(); lastCollect=lastSnapshot=0; foreach(var grid in watchedGrids) grid.OnBlockIntegrityChanged-=GeometryChanged; watchedGrids.Clear(); watchedIds=new HashSet(); lightingBlocks=new Sandbox.Game.Entities.Blocks.MyLightingBlock[0]; ResetVoxels(); } private void RetireGeometry() { var live=new HashSet(placements.Select(p=>p.Mesh)); foreach(var entry in cellCache.Where(p=>!live.Contains(p.Value.Mesh.Id)).ToArray()) cellCache.Remove(entry.Key); foreach(var mesh in modelCache.Values) if(mesh!=null) live.Add(mesh.Id); foreach(var variants in screenModels.Values)foreach(var variant in variants)live.Add(variant.Mesh.Id); var obsolete=meshesById.Keys.Where(id=>!live.Contains(id)).ToArray(); foreach(var id in obsolete) meshesById.Remove(id); RefreshPlacementMeshes(); lightingBlocks=placements.Select(p=>p.Block?.FatBlock as Sandbox.Game.Entities.Blocks.MyLightingBlock).Where(b=>b!=null).Distinct().ToArray(); // Native retirement follows the next published snapshot. It frees only // obsolete BLAS and preserves active geometry, textures and frame history. } private void RefreshPlacementMeshes() { // Keep the bounded LCD variants resident across view toggles. An // inactive variant has no TLAS instance and cannot appear in rays. placementMeshes=placements.Select(p=>p.Mesh).Concat(screenModels.Values.SelectMany(v=>v.Select(m=>m.Mesh.Id))) .Distinct().Select(id=>meshesById[id]).ToArray(); } private IEnumerable Collect(Vector3D camera) { // Reserve live-screen slots for the cockpit currently being viewed // before walking unordered blocks from the surrounding large ship. var viewCockpit=MySession.Static.CameraController as MyCockpit; if(viewCockpit!=null && !viewCockpit.Closed) foreach(var p in EntityModels(viewCockpit,viewCockpit.SlimBlock,0))yield return p; var sphere=new BoundingSphereD(camera,250); var grids=MyEntities.GetEntities().OfType().Where(g=>!g.Closed && g.PositionComp.WorldAABB.Intersects(sphere)).OrderBy(g=>Vector3D.DistanceSquared(g.WorldMatrix.Translation,camera)).ToArray(); var current=new HashSet(grids); foreach(var grid in watchedGrids.Where(g=>!current.Contains(g)).ToArray()) { grid.OnBlockIntegrityChanged-=GeometryChanged; watchedGrids.Remove(grid); } foreach(var grid in grids) if(watchedGrids.Add(grid)) grid.OnBlockIntegrityChanged+=GeometryChanged; watchedIds=new HashSet(grids.Select(g=>g.EntityId)); foreach(var grid in grids) { // The game maintains only exposed armor faces in render cells. // Traversing every cube part includes tens of thousands of hidden // internal faces and can incorrectly exhaust the scene budget. foreach(var cell in grid.RenderData.Cells.ToArray()) { var parts=new List(); long signature=0; foreach(var entry in cell.Value.CubeParts.ToArray()) { var part=entry.Key; var local=part.InstanceData.LocalMatrix; if(Vector3D.DistanceSquared(Vector3D.Transform(local.Translation,grid.WorldMatrix),camera)>250*250) { yield return null; continue; } if(part.InstanceData.EnableSkinning) { skippedModels++; yield return null; continue; } var mesh=CopyModel(part.Model); if(mesh!=null) { var tint=part.InstanceData.ColorMaskHSV; var uvOffset=new Vector2(part.InstanceData.GetTextureOffset(0),part.InstanceData.GetTextureOffset(1)); parts.Add(new CellPart { Mesh=mesh,Matrix=local,Tint=new Vector3(tint.X,tint.Y,tint.Z),UvOffset=uvOffset }); unchecked { signature += (long)part.GetHashCode()*397 ^ local.GetHashCode() ^ tint.GetHashCode() ^ uvOffset.GetHashCode() ^ (long)mesh.Id*7919; } } yield return null; } if(parts.Count==0) continue; if(!cellCache.TryGetValue(cell.Value,out var geometry) || geometry.Signature!=signature) { // The worker sees copied matrices and immutable model arrays. // It never reads a grid/entity/model from the live engine. var input=parts.ToArray(); var task=Task.Run(()=>MergeCell(input)); while(!task.IsCompleted) yield return null; var mesh=task.GetAwaiter().GetResult(); mesh.Id=++nextMesh; meshesById.Add(mesh.Id,mesh); geometry=new CellGeometry { Signature=signature,Mesh=mesh }; cellCache[cell.Value]=geometry; } yield return new Placement { Owner=grid,Local=Matrix.Identity,Mesh=geometry.Mesh.Id }; } var blocks=grid.GetBlocks().ToArray(); foreach(var block in blocks) { scannedBlocks++; if (grid.Closed || block.IsDestroyed) { yield return null; continue; } if(Vector3D.DistanceSquared(Vector3D.Transform(block.Position*grid.GridSize,grid.WorldMatrix),camera)>250*250) { yield return null; continue; } if(block.FatBlock!=null && !ReferenceEquals(block.FatBlock,viewCockpit)) { foreach(var p in EntityModels(block.FatBlock,block,0)) yield return p; } yield return null; } } if(options.Voxels) foreach(var p in CollectVoxels(camera)) yield return p; } private IEnumerable EntityModels(MyEntity entity,MySlimBlock block,int depth) { if(entity==null || entity.Closed || depth>8 || entity.Render==null || !entity.Render.Visible) yield break; var model=DisplayedModel(entity); var mesh=CopyScreenModel(entity,model,CopyModel(model)); if(mesh!=null) yield return new Placement { Owner=entity, Local=Matrix.Identity, Mesh=mesh.Id, Block=block }; if(entity.Subparts!=null) foreach(var part in entity.Subparts.Values) foreach(var p in EntityModels(part,block,depth+1)) yield return p; } private Mesh CopyModel(MyModel model) { if(model==null) return null; if(modelCache.TryGetValue(model,out var cached)) return cached; if(modelCache.Count>=4096) throw new InvalidOperationException("Scene exceeded the 4096 model limit."); var triangles=model.Triangles; int count=model.GetVerticesCount(); if(count<=0 || count>1000000 || triangles==null || triangles.Length==0 || triangles.Length>2000000) { modelCache[model]=null; skippedModels++; return null; } model.LoadTexCoordData(); var uv=model.TexCoords; var vertices=new float[count*8]; for(int i=0;i(triangles.Length*3); var materials=new List(triangles.Length);var parts=new List(); foreach(var part in model.GetMeshList()) { if(part.Material==null) continue; var technique=part.Material.DrawTechnique.ToString(); bool glass=technique=="GLASS"; if(technique!="MESH" && !glass) continue; uint cm=MaterialId(part.Material.Textures,"ColorMetalTexture"),extras=MaterialId(part.Material.Textures,"AddMapsTexture"); if(extras==0) extras=MaterialId(part.Material.Textures,"ExtensionsTexture"); uint flags=glass ? GlassFlag | (GlassId(part.Material.Name,part.Material.GlassCW,part.Material.GlassCCW)<<20) : 0; int start=materials.Count; for(int i=part.TriStart;ip.Mesh.Vertices.Length),indexCount=parts.Sum(p=>p.Mesh.Indices.Length); if(floatCount>8000000 || indexCount>6000000) throw new InvalidOperationException("An armor render cell exceeds the geometry budget."); var vertices=new float[floatCount];var indices=new uint[indexCount];var materials=new uint[indexCount/3]; int vertexOffset=0,indexOffset=0; foreach(var p in parts) { for(int i=0;i textures,string key) { if(textures==null || !textures.TryGetValue(key,out var relative) || string.IsNullOrEmpty(relative)) return 0; return MaterialPath(relative,key!="ColorMetalTexture"); } private uint MaterialPath(string relative,bool linear=false) { if(string.IsNullOrEmpty(relative)) return 0; string path=Path.IsPathRooted(relative)?relative:Path.Combine(VRage.FileSystem.MyFileSystem.ContentPath,relative); path=Path.GetFullPath(path); string key=(linear?"linear:":"color:")+path; if(materialCache.TryGetValue(key,out var cached)) return cached.Id; if(materialCache.Count>=991) throw new InvalidOperationException("Scene exceeded 991 static ray material textures (32 slots reserved for live screens)."); var material=new Material { Id=(uint)materialCache.Count+1,Path=path,Linear=linear }; // File I/O only on two bounded workers; never touch game entities there. material.Bytes=ReadMaterial(path); materialCache.Add(key,material); return material.Id; } private Task ReadMaterial(string path) => Task.Run(async()=> { await textureReaders.WaitAsync(); try { if(!File.Exists(path) || new FileInfo(path).Length>128*1024*1024) return new byte[0]; return File.ReadAllBytes(path); } catch { return new byte[0]; } finally { textureReaders.Release(); } }); private object Member(object target,string name) { if(target==null) return null; var type=target as Type??target.GetType(); var instance=target is Type?null:target; string key=type.FullName+"|"+name; MemberInfo member; lock(members) if(!members.TryGetValue(key,out member)) { member=(MemberInfo)type.GetField(name,Flags)??type.GetProperty(name,Flags); members[key]=member; } return member is FieldInfo field?field.GetValue(instance):(member as PropertyInfo)?.GetValue(instance,null); } private IntPtr Pointer(object target) => target==null?IntPtr.Zero:(IntPtr)(Member(target,"NativePointer")??IntPtr.Zero); private static void AfterOpaqueLighting() { if(active!=null){active.opaqueRendered=false;if(active.options.PathTracing)active.Render();} } private static void AfterScene() { var a=active;if(a==null)return; if(!a.options.PathTracing){a.Render();return;} lock(a.renderLock) { if(!a.opaqueRendered || !a.options.Enabled || a.native==IntPtr.Zero || a.failure!=null)return; a.opaqueRendered=false; try { var g=a.Member(GBuffer,"Main"); int code=RtFinishTransparency(a.native,a.Pointer(a.Member(a.Member(a.Member(g,"ResolvedDepthStencil"),"SrvDepth"),"Srv")), a.Pointer(a.Member(a.Member(g,"LBuffer"),"Srv")),a.Pointer(a.Member(a.Member(g,"LBuffer"),"Rtv"))); if(code<0)a.Fail("Transparent resolve: 0x"+code.ToString("X8")); }catch(Exception e){a.Fail("Transparent resolve: "+e.Message);} } } private void Render() { lock(renderLock) { if(Interlocked.Exchange(ref resetNative,0)!=0) { if(native!=IntPtr.Zero) { RtDestroy(native); native=IntPtr.Zero; } uploaded.Clear(); texturesUploaded.Clear(); textureFallbacks=0; previousFrameAt=0; retainedMeshes=null; } if(Interlocked.Exchange(ref resetHistory,0)!=0) previousFrameAt=0; if(!options.Enabled || !worldReady || failure!=null) return; Snapshot leased=null; try { var s=leased=snapshots.AcquireRead(); if(s==null || s.Count==0 || s.Generation!=Volatile.Read(ref sceneGeneration)) { renderStatus=new JObject { ["state"]="waiting-for-scene" }; return; } if(Stopwatch.GetTimestamp()-s.Created>Stopwatch.Frequency) { renderStatus=new JObject { ["state"]="waiting-for-fresh-scene" }; previousFrameAt=0; return; } if(native==IntPtr.Zero) { LoadNative(); int error=RtCreate(Pointer(Member(Renderer,"DeviceInstance")),out native); if(error<0) { RtGetStatus(native,out var init); throw new InvalidOperationException(init.Message+" (0x"+error.ToString("X8")+")"); } } int glassResult=RtGlassMaterials(native,s.Glass,(uint)s.Glass.Length); if(glassResult<0) throw new InvalidOperationException("Glass material upload failed: 0x"+glassResult.ToString("X8")); int lightResult=RtLights(native,s.Lights,(uint)s.Lights.Length); if(lightResult<0) throw new InvalidOperationException("Local light upload failed: 0x"+lightResult.ToString("X8")); if(!ReferenceEquals(retainedMeshes,s.Meshes)) { var ids=s.Meshes.Select(m=>m.Id).ToArray(); int error=RtRetainMeshes(native,ids,(uint)ids.Length); if(error<0) throw new InvalidOperationException("Native scene retirement: 0x"+error.ToString("X8")); uploaded.IntersectWith(ids); retainedMeshes=s.Meshes; } int added=0; foreach(var mesh in s.Meshes) { if(uploaded.Contains(mesh.Id)) continue; int result=RtMesh(native,mesh.Id,mesh.Vertices,(uint)(mesh.Vertices.Length/8),mesh.Indices,(uint)mesh.Indices.Length,mesh.Materials); if(result<0) throw new InvalidOperationException("Native geometry upload: 0x"+result.ToString("X8")); uploaded.Add(mesh.Id); if(++added>=6) break; } added=0; foreach(var material in s.Materials) { if(texturesUploaded.Contains(material.Id)) continue; if(material.Bytes==null) material.Bytes=ReadMaterial(material.Path); if(!material.Bytes.IsCompleted) continue; byte[] bytes=material.Bytes.Result; int result=RtMaterial(native,material.Id,bytes,(uint)bytes.Length,material.Linear?1u:0u); if(result<0) throw new InvalidOperationException("Native texture upload: 0x"+result.ToString("X8")); if(result>0) textureFallbacks++; texturesUploaded.Add(material.Id); material.Bytes=null; if(++added>=4) break; } // Native builds bounded BLAS batches; original frame remains intact // until all snapshot meshes have been submitted. var environment=Member(Renderer,"Environment"); var matrices=Member(environment,"Matrices"); var camera=(Vector3D)Member(matrices,"CameraPosition"); var resolution=(Vector2I)Member(Renderer,"m_resolution"); var lighting=Member(Member(environment,"Data"),"EnvironmentLight"); var sun=Member(lighting,"SunLightDirection"); Vector3 sunVector=sun is Vector3 direction?-direction:Vector3.Up; var o=options; var probe=Member(Member(Managers,"EnvironmentProbe"),"FarCubemapOriginal"); var environmentSrv=Pointer(Member(probe,"Srv")); int env=RtEnvironment(native,environmentSrv); if(env<0)throw new InvalidOperationException("Environment probe: 0x"+env.ToString("X8")); var post=Member(Member(Renderer,"Postprocess"),"Data"); var emissive=Member(post,"BloomEmissiveness"); var sunColor=(Vector3)Member(lighting,"SunColorRaw")*(float)Member(lighting,"SunDiffuseFactor"); var p=new Parameters { InverseViewProjection=(Matrix)Member(matrices,"InvViewProjectionAt0"), InverseView=(Matrix)Member(matrices,"InvViewAt0"), OriginDelta=new Vector4((Vector3)(camera-s.Origin),0), SunDirection=new Vector4(sunVector,0), Settings=new Vector4(o.Radius,o.Strength,o.ShadowStrength,o.Diagnostic), Width=(uint)resolution.X,Height=(uint)resolution.Y,TraceWidth=(uint)(o.FullResolution?resolution.X:(resolution.X+1)/2),TraceHeight=(uint)(o.FullResolution?resolution.Y:(resolution.Y+1)/2), SunColor=new Vector4(sunColor,0),Effects=new Vector4(o.Reflections,o.Indirect,o.Voxels?2.5f*(1<.FindByID(id); if(actor!=null && !actor.IsDestroyed)renderMatrices[id]=matrix=actor.WorldMatrix; else {renderInstances[i]=instance;continue;} } instance=new Instance(s.LocalTransforms[i]*matrix,s.Origin,instance.Mesh,instance.Tint,instance.Key);refreshed++; } renderInstances[i]=instance; } int code=RtFrame(native,Pointer(Member(Member(Member(g,"ResolvedDepthStencil"),"SrvDepth"),"Srv")), Pointer(Member(Member(g,"GBuffer1"),"Srv")),Pointer(Member(Member(g,"LBuffer"),"Srv")), Pointer(Member(Member(g,"GBuffer2"),"Srv")),Pointer(Member(Member(g,"GBuffer0"),"Srv")),Pointer(Member(Member(g,"LBuffer"),"Rtv")),renderInstances, s.Meshes.All(mesh=>uploaded.Contains(mesh.Id)) && texturesUploaded.Count>=s.Materials.Length?(uint)s.Count:0,ref p); RtGetStatus(native,out var status); renderStatus=new JObject { ["state"]=code>0?"active":"building", ["hardwareTier"]=status.Tier, ["meshes"]=status.Meshes, ["instances"]=status.Instances,["triangles"]=status.Triangles,["frames"]=status.Frames,["cpuSubmitMs"]=status.CpuMilliseconds,["gpuTraceDenoiseMs"]=status.GpuMilliseconds,["currentRenderPoseInstances"]=refreshed, ["hresult"]=status.LastHresult,["message"]=status.Message,["nativeResolution"]=resolution.X+"x"+resolution.Y,["traceResolution"]=p.TraceWidth+"x"+p.TraceHeight,["textures"]=texturesUploaded.Count,["environmentProbe"]=environmentSrv!=IntPtr.Zero,["lightingStage"]=o.PathTracing?"opaque resolve before transparency":"after scene" }; if(code>0) { opaqueRendered=o.PathTracing;previousViewProjection=(Matrix)Member(matrices,"ViewProjectionAt0");previousCamera=camera;previousOrigin=s.Origin;previousFrameAt=Stopwatch.GetTimestamp(); } if(code<0) throw new InvalidOperationException(status.Message+" (0x"+code.ToString("X8")+")"); } catch(Exception error) { Fail("Render: "+error.Message); } finally { snapshots.ReleaseRead(leased); } } } private void LoadNative() { if(nativeLibrary!=IntPtr.Zero) return; using(var stream=typeof(RayTracing).Assembly.GetManifestResourceStream("XFE.SeRayTracing.Native.dll")) { if(stream==null) throw new InvalidOperationException("Build native module with tools/Build-RayTracing.ps1 before building the plugin."); byte[] bytes=new byte[stream.Length]; int offset=0; while(offset