Feed LocalCast visual frames into Aquarium
This commit is contained in:
parent
1ac4886852
commit
fbb5c6dc8d
13 changed files with 782 additions and 11 deletions
|
|
@ -39,6 +39,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aquarium.Engine.Fractal", "
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aquarium.Engine.Fractal.Tests", "tests\Aquarium.Engine.Fractal.Tests\Aquarium.Engine.Fractal.Tests.csproj", "{A9D41C48-7E78-4D75-A814-0C1500245EFC}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aquarium.LocalCast", "src\Aquarium.LocalCast\Aquarium.LocalCast.csproj", "{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aquarium.LocalCast.Tests", "tests\Aquarium.LocalCast.Tests\Aquarium.LocalCast.Tests.csproj", "{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -241,6 +245,30 @@ Global
|
|||
{A9D41C48-7E78-4D75-A814-0C1500245EFC}.Release|x64.Build.0 = Release|Any CPU
|
||||
{A9D41C48-7E78-4D75-A814-0C1500245EFC}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{A9D41C48-7E78-4D75-A814-0C1500245EFC}.Release|x86.Build.0 = Release|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Release|x64.Build.0 = Release|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F}.Release|x86.Build.0 = Release|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Release|x64.Build.0 = Release|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -255,5 +283,7 @@ Global
|
|||
{801B1913-6AE1-4B8C-8030-4669AC4FE25E} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
|
||||
{FF232A59-4E8D-4D56-A2C3-ACC02F5371D0} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
|
||||
{A9D41C48-7E78-4D75-A814-0C1500245EFC} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
|
||||
{E8C66A46-1F56-484D-8CDD-5C0A3FD84A5F} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
|
||||
{E6560D72-B018-4E00-AEBD-C1B9419E0BC7} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
|
|||
|
|
@ -24,13 +24,20 @@ compact-kernel controls, and field id.
|
|||
- smoothed velocity predicts the presented center
|
||||
- history weight is confidence scaled by age inside the window
|
||||
|
||||
The D3D12 renderer lowers the field into `D3D12TemporalGaussianPacket`, uploads a
|
||||
4096-entry structured buffer, and renders an instanced proxy-quad pass from
|
||||
The D3D12 renderer lowers the field into `D3D12TemporalGaussianPacket`, uploads
|
||||
the active packet span into a 1,048,576-entry structured buffer, and renders an
|
||||
instanced proxy-quad pass from
|
||||
`D3D12TemporalGaussian.hlsl`. The pixel shader evaluates a compact anisotropic
|
||||
Gaussian kernel in world space, writes HDR scene color/travel, field metadata,
|
||||
normal, and temporal-control coverage so the existing resolve sees the field as
|
||||
diegetic scene content.
|
||||
|
||||
`Aquarium.LocalCast` is the first concrete live client for this contract. It
|
||||
reads `localcast.visual.render_frame` from LocalCastBridge's typed CultCache
|
||||
MessagePack document, maps every point claim into `TemporalGaussianObservation`
|
||||
rows, and lets the accumulator own smoothing, history, and presentation delay.
|
||||
It does not downsample the document in client space.
|
||||
|
||||
## Invariants
|
||||
|
||||
- Field accumulation happens in world space before pixel history. TAA is the
|
||||
|
|
@ -46,11 +53,12 @@ diegetic scene content.
|
|||
|
||||
## Cut Line
|
||||
|
||||
This cut deliberately does not claim million-splat residency yet. The live D3D12
|
||||
path draws up to 4096 temporal Gaussians through instanced proxy quads. Scaling
|
||||
past that belongs to the Aquarium renderer: selected-cut residency, tiled/bin
|
||||
dispatch, or Aquarium/Aquarium-like compute lowering. Do not push that scheduler
|
||||
into LocalCastBridge.
|
||||
This cut deliberately claims million-slot ingestion, not a finished million-splat
|
||||
renderer architecture. The live D3D12 path can draw up to 1,048,576 temporal
|
||||
Gaussians through instanced proxy quads and only uploads the active packet span.
|
||||
The next scaling cut belongs to the Aquarium renderer: selected-cut residency,
|
||||
tiled/bin dispatch, GPU accumulation, and clustered visibility. Do not push that
|
||||
scheduler into LocalCastBridge.
|
||||
|
||||
The first shader pass also uses camera-facing proxy planes to evaluate each
|
||||
kernel. True ray-integrated volume compositing, Gaussian depth sorting, and
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private const Format SceneDepthFormat = Format.D32_Float;
|
||||
private const int MaxSdfLightCount = 64;
|
||||
private const int MaxSdfObjectCount = 64;
|
||||
private const int MaxTemporalGaussianCount = 4096;
|
||||
private const int MaxTemporalGaussianCount = 1_048_576;
|
||||
private const float SurfaceTransparentMinZ = -1.85f;
|
||||
private const float SurfaceTransparentMaxZ = 0.45f;
|
||||
private const int BloomLevelCount = 3;
|
||||
|
|
@ -1386,7 +1386,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
{
|
||||
sdfLightBuffer.Upload(activeCommandList, frameResources.UploadRing, sdfLights);
|
||||
sdfObjectBuffer.Upload(activeCommandList, frameResources.UploadRing, sdfObjects);
|
||||
temporalGaussianBuffer.Upload(activeCommandList, frameResources.UploadRing, temporalGaussians);
|
||||
temporalGaussianBuffer.UploadPartial(activeCommandList, frameResources.UploadRing, temporalGaussians.AsSpan(0, temporalGaussianCount));
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
@ -1427,7 +1427,6 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
{
|
||||
Array.Clear(sdfObjects);
|
||||
Array.Clear(sdfLights);
|
||||
Array.Clear(temporalGaussians);
|
||||
temporalGaussianCount = 0;
|
||||
heightFieldBrushConstants = D3D12HeightFieldBrushConstants.FromBrushes(scene.HeightFieldBrushes);
|
||||
|
||||
|
|
@ -1922,7 +1921,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
{
|
||||
var commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);
|
||||
commandAllocator.Name = $"Aquarium D3D12 Frame {index} Command Allocator";
|
||||
var uploadRing = new D3D12UploadRing(device, 64 * 1024 * 1024, $"Aquarium D3D12 Frame {index} Upload Ring");
|
||||
var uploadRing = new D3D12UploadRing(device, 160 * 1024 * 1024, $"Aquarium D3D12 Frame {index} Upload Ring");
|
||||
var transientDescriptors = new D3D12DescriptorArena(
|
||||
device,
|
||||
DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView,
|
||||
|
|
|
|||
|
|
@ -53,6 +53,29 @@ internal sealed class D3D12StructuredBuffer : IDisposable
|
|||
Transition(commandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
|
||||
}
|
||||
|
||||
public void UploadPartial<T>(ID3D12GraphicsCommandList commandList, D3D12UploadRing uploadRing, ReadOnlySpan<T> values)
|
||||
where T : unmanaged
|
||||
{
|
||||
if (values.Length > elementCount)
|
||||
{
|
||||
throw new ArgumentException($"Structured buffer holds {elementCount} elements but received {values.Length}.", nameof(values));
|
||||
}
|
||||
|
||||
var actualStride = Unsafe.SizeOf<T>();
|
||||
if (actualStride != strideBytes)
|
||||
{
|
||||
throw new ArgumentException($"Structured buffer stride mismatch. Expected {strideBytes} bytes but received {actualStride}.", nameof(values));
|
||||
}
|
||||
|
||||
var upload = uploadRing.WriteArray(values);
|
||||
Transition(commandList, ResourceStates.CopyDest);
|
||||
if (upload.DataBytes > 0)
|
||||
{
|
||||
commandList.CopyBufferRegion(Resource, 0, uploadRing.Resource, (ulong)upload.OffsetBytes, (ulong)upload.DataBytes);
|
||||
}
|
||||
Transition(commandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
|
||||
}
|
||||
|
||||
public void CreateShaderResourceView(ID3D12Device device, D3D12DescriptorSlot descriptor)
|
||||
{
|
||||
device.CreateShaderResourceView(
|
||||
|
|
|
|||
18
src/Aquarium.LocalCast/Aquarium.LocalCast.csproj
Normal file
18
src/Aquarium.LocalCast/Aquarium.LocalCast.csproj
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<AssemblyName>Aquarium.LocalCast</AssemblyName>
|
||||
<RootNamespace>Aquarium.LocalCast</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Aquarium.Engine.Contracts\Aquarium.Engine.Contracts.csproj" />
|
||||
<ProjectReference Include="..\Aquarium.Engine.Fractal\Aquarium.Engine.Fractal.csproj" />
|
||||
<ProjectReference Include="..\..\..\CultLib\src\GameCult.Caching.MessagePack\GameCult.Caching.MessagePack.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
19
src/Aquarium.LocalCast/LocalCastRenderPlan.cs
Normal file
19
src/Aquarium.LocalCast/LocalCastRenderPlan.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
using Aquarium.Engine.Render;
|
||||
|
||||
namespace Aquarium.LocalCast;
|
||||
|
||||
public static class LocalCastRenderPlan
|
||||
{
|
||||
public static AquariumRenderPlan Create()
|
||||
{
|
||||
var app = new AquariumApp();
|
||||
var scene = app.RenderTargets.Hdr("scene");
|
||||
app.Cameras.Orbit("main");
|
||||
app.Graph.Pass("scene").Fullscreen();
|
||||
app.Features.Bloom(scene.Color);
|
||||
app.Features.Presentation(scene.Color);
|
||||
app.Features.DirectWriteOverlay();
|
||||
app.Debug.View("Scene", scene.Color);
|
||||
return app.Plan;
|
||||
}
|
||||
}
|
||||
139
src/Aquarium.LocalCast/LocalCastRuntime.cs
Normal file
139
src/Aquarium.LocalCast/LocalCastRuntime.cs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
using System.Numerics;
|
||||
using Aquarium.Engine;
|
||||
using Aquarium.Engine.Audio;
|
||||
using Aquarium.Engine.Fractal.Temporal;
|
||||
using Aquarium.Engine.Input;
|
||||
using Aquarium.Engine.Render;
|
||||
using Aquarium.Engine.Ui;
|
||||
|
||||
namespace Aquarium.LocalCast;
|
||||
|
||||
public sealed class LocalCastRuntime : IAquariumRuntime
|
||||
{
|
||||
private readonly TemporalGaussianAccumulator accumulator = new(
|
||||
accumulationWindowSeconds: 2.75f,
|
||||
presentationDelaySeconds: 0.35f,
|
||||
smoothing: 0.55f);
|
||||
private readonly LocalCastTemporalGaussianMapper mapper = new();
|
||||
private readonly LocalCastVisualStateReader reader;
|
||||
private float timeSeconds;
|
||||
private float sceneTimelineSeconds;
|
||||
private long lastFrameId = -1;
|
||||
private int latestPointCount;
|
||||
private string latestStatus = "waiting";
|
||||
|
||||
public LocalCastRuntime(AquariumRuntimeOptions options)
|
||||
{
|
||||
Options = options;
|
||||
reader = new LocalCastVisualStateReader(ResolveVisualCachePath());
|
||||
Ui = new AquariumUiDocument()
|
||||
.Panel("LocalCast", 18.0f, 82.0f, 372.0f, panel =>
|
||||
{
|
||||
panel.Section("Camera Field");
|
||||
panel.Readout("Cache", () => reader.Path);
|
||||
panel.Readout("Frame", () => lastFrameId >= 0 ? $"{lastFrameId} / {latestPointCount} splats" : latestStatus);
|
||||
panel.Readout("Timeline", () => $"{sceneTimelineSeconds:0.000}s");
|
||||
});
|
||||
}
|
||||
|
||||
public AquariumRuntimeOptions Options { get; }
|
||||
|
||||
public GraphicsSettings GraphicsSettings { get; set; } = new(0, 1.38f, 0.32f, 0.04f);
|
||||
|
||||
public AquariumRenderPlan RenderPlan { get; } = LocalCastRenderPlan.Create();
|
||||
|
||||
public AquariumUiDocument Ui { get; }
|
||||
|
||||
public AquariumAudioDocument Audio { get; } = new();
|
||||
|
||||
public AquariumSynthDocument Synth { get; } = AquariumSynthDocument.Empty;
|
||||
|
||||
public AquariumFrame Frame
|
||||
{
|
||||
get
|
||||
{
|
||||
var temporalField = accumulator.BuildField(sceneTimelineSeconds);
|
||||
var scene = new AquariumSceneState
|
||||
{
|
||||
TraceHeightFieldSurface = false,
|
||||
UseStarfieldBackground = false,
|
||||
TemporalGaussianField = temporalField,
|
||||
};
|
||||
|
||||
return new AquariumFrame(
|
||||
new ViewFrame(new Vector2(0.0f, 0.18f), 2.35f),
|
||||
new Vector3(0.0f, -2.35f, 1.35f),
|
||||
new Vector3(0.0f, 0.18f, 1.18f),
|
||||
timeSeconds,
|
||||
Vector2.Zero,
|
||||
scene);
|
||||
}
|
||||
}
|
||||
|
||||
public AquariumFrame ComposeFrame(AquariumFrame frame, AquariumFrameInput input)
|
||||
{
|
||||
return frame;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Console.WriteLine($"LocalCast Aquarium runtime reading {reader.Path}");
|
||||
}
|
||||
|
||||
public void Update(float deltaSeconds, InputState input)
|
||||
{
|
||||
timeSeconds += Math.Max(deltaSeconds, 0.0f);
|
||||
|
||||
if (!reader.TryReadLatest(out var frame))
|
||||
{
|
||||
latestStatus = "waiting for visual-state.msgpack";
|
||||
sceneTimelineSeconds = MathF.Max(sceneTimelineSeconds, timeSeconds);
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame.FrameId != lastFrameId)
|
||||
{
|
||||
accumulator.Observe(mapper.Map(frame));
|
||||
lastFrameId = frame.FrameId;
|
||||
latestPointCount = frame.Points.Count;
|
||||
latestStatus = "live";
|
||||
}
|
||||
|
||||
sceneTimelineSeconds = MathF.Max(
|
||||
sceneTimelineSeconds + Math.Max(deltaSeconds, 0.0f),
|
||||
mapper.ToTimelineSeconds(frame.PresentTimeNs));
|
||||
}
|
||||
|
||||
public void FlushState()
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
private string ResolveVisualCachePath()
|
||||
{
|
||||
var environmentPath = Environment.GetEnvironmentVariable("LOCALCAST_VISUAL_CACHE");
|
||||
if (!string.IsNullOrWhiteSpace(environmentPath))
|
||||
{
|
||||
return environmentPath;
|
||||
}
|
||||
|
||||
return Path.Combine(
|
||||
"E:",
|
||||
"Projects",
|
||||
"LocalCastBridge",
|
||||
"calibration",
|
||||
"runs",
|
||||
"visual-state.msgpack");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LocalCastRuntimeFactory : IAquariumRuntimeFactory
|
||||
{
|
||||
public IAquariumRuntime Create(AquariumRuntimeOptions options)
|
||||
{
|
||||
return new LocalCastRuntime(options);
|
||||
}
|
||||
}
|
||||
101
src/Aquarium.LocalCast/LocalCastTemporalGaussianMapper.cs
Normal file
101
src/Aquarium.LocalCast/LocalCastTemporalGaussianMapper.cs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
using System.Numerics;
|
||||
using Aquarium.Engine.Fractal.Temporal;
|
||||
|
||||
namespace Aquarium.LocalCast;
|
||||
|
||||
public sealed class LocalCastTemporalGaussianMapper
|
||||
{
|
||||
private const float NanosecondsToSeconds = 1.0e-9f;
|
||||
private long? originNs;
|
||||
|
||||
public long OriginNs => originNs ?? 0;
|
||||
|
||||
public float ToTimelineSeconds(long timestampNs)
|
||||
{
|
||||
originNs ??= timestampNs;
|
||||
return MathF.Max(0.0f, (timestampNs - originNs.Value) * NanosecondsToSeconds);
|
||||
}
|
||||
|
||||
public IReadOnlyList<TemporalGaussianObservation> Map(LocalCastVisualFrame frame)
|
||||
{
|
||||
var frameSeconds = ToTimelineSeconds(frame.SourceTimeMaxNs);
|
||||
var observations = new TemporalGaussianObservation[frame.Points.Count];
|
||||
for (var index = 0; index < observations.Length; index++)
|
||||
{
|
||||
var point = frame.Points[index];
|
||||
var observedSeconds = point.SourceTimestampNs > 0
|
||||
? ToTimelineSeconds(point.SourceTimestampNs)
|
||||
: frameSeconds;
|
||||
var radius = Math.Clamp(point.RadiusMeters, 0.008f, 0.09f);
|
||||
var jitter = StochasticJitter(point.StableKey, frame.FrameId, radius * 0.28f);
|
||||
var color = SanitizeColor(point.ColorOpacity, point.Confidence);
|
||||
|
||||
observations[index] = new TemporalGaussianObservation(
|
||||
point.StableKey,
|
||||
point.Position + jitter,
|
||||
new Vector3(radius * 2.15f, radius * 1.55f, radius * 1.85f),
|
||||
Quaternion.Identity,
|
||||
color,
|
||||
point.Confidence,
|
||||
observedSeconds,
|
||||
Falloff: 4.6f,
|
||||
ShapePower: 1.35f,
|
||||
FieldId: 0);
|
||||
}
|
||||
|
||||
return observations;
|
||||
}
|
||||
|
||||
private static Vector4 SanitizeColor(Vector4 color, float confidence)
|
||||
{
|
||||
var alpha = Math.Clamp(color.W * MathF.Sqrt(Math.Clamp(confidence, 0.0f, 1.0f)), 0.0f, 0.98f);
|
||||
return new Vector4(
|
||||
ToneUp(color.X),
|
||||
ToneUp(color.Y),
|
||||
ToneUp(color.Z),
|
||||
alpha);
|
||||
}
|
||||
|
||||
private static float ToneUp(float value)
|
||||
{
|
||||
return Math.Clamp(MathF.Pow(Math.Clamp(value, 0.0f, 1.0f), 0.72f) * 1.65f, 0.0f, 2.25f);
|
||||
}
|
||||
|
||||
private static Vector3 StochasticJitter(string stableKey, long frameId, float amplitude)
|
||||
{
|
||||
var seed = StableHash(stableKey, frameId);
|
||||
var x = Unit(seed) * 2.0f - 1.0f;
|
||||
var y = Unit(seed * 1664525u + 1013904223u) * 2.0f - 1.0f;
|
||||
var z = Unit(seed * 22695477u + 1u) * 2.0f - 1.0f;
|
||||
var vector = new Vector3(x, y, z);
|
||||
if (vector.LengthSquared() <= 0.000001f)
|
||||
{
|
||||
return Vector3.Zero;
|
||||
}
|
||||
|
||||
return Vector3.Normalize(vector) * amplitude;
|
||||
}
|
||||
|
||||
private static uint StableHash(string stableKey, long frameId)
|
||||
{
|
||||
const uint fnvOffset = 2166136261u;
|
||||
const uint fnvPrime = 16777619u;
|
||||
var hash = fnvOffset;
|
||||
foreach (var character in stableKey)
|
||||
{
|
||||
hash ^= character;
|
||||
hash *= fnvPrime;
|
||||
}
|
||||
|
||||
hash ^= (uint)frameId;
|
||||
hash *= fnvPrime;
|
||||
hash ^= (uint)(frameId >> 32);
|
||||
hash *= fnvPrime;
|
||||
return hash;
|
||||
}
|
||||
|
||||
private static float Unit(uint seed)
|
||||
{
|
||||
return (seed & 0x00ffffff) / 16777215.0f;
|
||||
}
|
||||
}
|
||||
36
src/Aquarium.LocalCast/LocalCastVisualFrame.cs
Normal file
36
src/Aquarium.LocalCast/LocalCastVisualFrame.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System.Numerics;
|
||||
|
||||
namespace Aquarium.LocalCast;
|
||||
|
||||
public sealed class LocalCastVisualFrame
|
||||
{
|
||||
public required string SchemaVersion { get; init; }
|
||||
|
||||
public required long FrameId { get; init; }
|
||||
|
||||
public required long CreatedMonotonicNs { get; init; }
|
||||
|
||||
public required long SourceTimeMinNs { get; init; }
|
||||
|
||||
public required long SourceTimeMaxNs { get; init; }
|
||||
|
||||
public required long PresentTimeNs { get; init; }
|
||||
|
||||
public required long AudioAlignmentTimeNs { get; init; }
|
||||
|
||||
public required string SpoutSenderName { get; init; }
|
||||
|
||||
public required int TargetWidth { get; init; }
|
||||
|
||||
public required int TargetHeight { get; init; }
|
||||
|
||||
public required IReadOnlyList<LocalCastVisualPoint> Points { get; init; }
|
||||
}
|
||||
|
||||
public readonly record struct LocalCastVisualPoint(
|
||||
string StableKey,
|
||||
Vector3 Position,
|
||||
float RadiusMeters,
|
||||
Vector4 ColorOpacity,
|
||||
float Confidence,
|
||||
long SourceTimestampNs);
|
||||
215
src/Aquarium.LocalCast/LocalCastVisualStateReader.cs
Normal file
215
src/Aquarium.LocalCast/LocalCastVisualStateReader.cs
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
using System.Buffers;
|
||||
using System.Numerics;
|
||||
using MessagePack;
|
||||
|
||||
namespace Aquarium.LocalCast;
|
||||
|
||||
public sealed class LocalCastVisualStateReader
|
||||
{
|
||||
public const string LiveRenderFrameKey = "localcast.visual.render-frame.live";
|
||||
public const string RenderFrameType = "localcast.visual.render_frame";
|
||||
public const string RenderFrameSchemaId = "gamecult.localcast.visual.render_frame.v1";
|
||||
|
||||
private readonly string path;
|
||||
|
||||
public LocalCastVisualStateReader(string path)
|
||||
{
|
||||
this.path = System.IO.Path.GetFullPath(path);
|
||||
}
|
||||
|
||||
public string Path => path;
|
||||
|
||||
public bool TryReadLatest(out LocalCastVisualFrame frame)
|
||||
{
|
||||
frame = null!;
|
||||
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
byte[] bytes;
|
||||
try
|
||||
{
|
||||
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||
bytes = new byte[stream.Length];
|
||||
var read = stream.Read(bytes, 0, bytes.Length);
|
||||
if (read != bytes.Length)
|
||||
{
|
||||
Array.Resize(ref bytes, read);
|
||||
}
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return TryParseStore(bytes, out frame);
|
||||
}
|
||||
catch (MessagePackSerializationException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (EndOfStreamException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryParseStore(ReadOnlyMemory<byte> payload, out LocalCastVisualFrame frame)
|
||||
{
|
||||
frame = null!;
|
||||
var reader = new MessagePackReader(payload);
|
||||
var recordCount = reader.ReadArrayHeader();
|
||||
|
||||
for (var recordIndex = 0; recordIndex < recordCount; recordIndex++)
|
||||
{
|
||||
var mapCount = reader.ReadMapHeader();
|
||||
string? key = null;
|
||||
string? type = null;
|
||||
byte[]? documentPayload = null;
|
||||
|
||||
for (var mapIndex = 0; mapIndex < mapCount; mapIndex++)
|
||||
{
|
||||
var field = reader.ReadString();
|
||||
switch (field)
|
||||
{
|
||||
case "key":
|
||||
key = reader.ReadString();
|
||||
break;
|
||||
case "type":
|
||||
type = reader.ReadString();
|
||||
break;
|
||||
case "payload":
|
||||
var bytes = reader.ReadBytes();
|
||||
documentPayload = bytes.HasValue ? bytes.Value.ToArray() : null;
|
||||
break;
|
||||
default:
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (string.Equals(key, LiveRenderFrameKey, StringComparison.Ordinal)
|
||||
&& string.Equals(type, RenderFrameType, StringComparison.Ordinal)
|
||||
&& documentPayload is not null)
|
||||
{
|
||||
frame = ParseRenderFrame(documentPayload);
|
||||
return string.Equals(frame.SchemaVersion, RenderFrameSchemaId, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static LocalCastVisualFrame ParseRenderFrame(ReadOnlyMemory<byte> payload)
|
||||
{
|
||||
var reader = new MessagePackReader(payload);
|
||||
var fieldCount = reader.ReadArrayHeader();
|
||||
if (fieldCount < 11)
|
||||
{
|
||||
throw new InvalidOperationException($"Render frame payload has {fieldCount} fields; expected at least 11.");
|
||||
}
|
||||
|
||||
var frame = new LocalCastVisualFrame
|
||||
{
|
||||
SchemaVersion = reader.ReadString() ?? string.Empty,
|
||||
FrameId = reader.ReadInt64(),
|
||||
CreatedMonotonicNs = reader.ReadInt64(),
|
||||
SourceTimeMinNs = reader.ReadInt64(),
|
||||
SourceTimeMaxNs = reader.ReadInt64(),
|
||||
PresentTimeNs = reader.ReadInt64(),
|
||||
AudioAlignmentTimeNs = reader.ReadInt64(),
|
||||
SpoutSenderName = reader.ReadString() ?? string.Empty,
|
||||
TargetWidth = reader.ReadInt32(),
|
||||
TargetHeight = reader.ReadInt32(),
|
||||
Points = ReadPoints(ref reader),
|
||||
};
|
||||
|
||||
for (var fieldIndex = 11; fieldIndex < fieldCount; fieldIndex++)
|
||||
{
|
||||
reader.Skip();
|
||||
}
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<LocalCastVisualPoint> ReadPoints(ref MessagePackReader reader)
|
||||
{
|
||||
var count = reader.ReadArrayHeader();
|
||||
var points = new LocalCastVisualPoint[count];
|
||||
for (var index = 0; index < count; index++)
|
||||
{
|
||||
var fieldCount = reader.ReadArrayHeader();
|
||||
if (fieldCount < 6)
|
||||
{
|
||||
throw new InvalidOperationException($"Render point payload has {fieldCount} fields; expected at least 6.");
|
||||
}
|
||||
|
||||
var stableKey = reader.ReadString() ?? string.Empty;
|
||||
var position = ReadVector3(ref reader);
|
||||
var radius = reader.ReadSingle();
|
||||
var color = ReadVector4(ref reader);
|
||||
var confidence = reader.ReadSingle();
|
||||
var timestampNs = reader.ReadInt64();
|
||||
for (var fieldIndex = 6; fieldIndex < fieldCount; fieldIndex++)
|
||||
{
|
||||
reader.Skip();
|
||||
}
|
||||
|
||||
points[index] = new LocalCastVisualPoint(
|
||||
stableKey,
|
||||
position,
|
||||
MathF.Max(0.001f, radius),
|
||||
color,
|
||||
Math.Clamp(confidence, 0.0f, 1.0f),
|
||||
timestampNs);
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
private static Vector3 ReadVector3(ref MessagePackReader reader)
|
||||
{
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count < 3)
|
||||
{
|
||||
throw new InvalidOperationException($"Vector3 payload has {count} fields; expected at least 3.");
|
||||
}
|
||||
|
||||
var value = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
||||
for (var index = 3; index < count; index++)
|
||||
{
|
||||
reader.Skip();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private static Vector4 ReadVector4(ref MessagePackReader reader)
|
||||
{
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count < 4)
|
||||
{
|
||||
throw new InvalidOperationException($"Vector4 payload has {count} fields; expected at least 4.");
|
||||
}
|
||||
|
||||
var value = new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
||||
for (var index = 4; index < count; index++)
|
||||
{
|
||||
reader.Skip();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Aquarium.LocalCast\Aquarium.LocalCast.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
using System.Numerics;
|
||||
using Aquarium.LocalCast;
|
||||
|
||||
namespace Aquarium.LocalCast.Tests;
|
||||
|
||||
public sealed class LocalCastTemporalGaussianMapperTests
|
||||
{
|
||||
[Fact]
|
||||
public void MapsRenderPointsToTemporalGaussianObservations()
|
||||
{
|
||||
var frame = new LocalCastVisualFrame
|
||||
{
|
||||
SchemaVersion = LocalCastVisualStateReader.RenderFrameSchemaId,
|
||||
FrameId = 42,
|
||||
CreatedMonotonicNs = 1_000_000_000,
|
||||
SourceTimeMinNs = 1_000_000_000,
|
||||
SourceTimeMaxNs = 1_050_000_000,
|
||||
PresentTimeNs = 1_350_000_000,
|
||||
AudioAlignmentTimeNs = 1_350_000_000,
|
||||
SpoutSenderName = "LocalCastBridge Point Cloud",
|
||||
TargetWidth = 1920,
|
||||
TargetHeight = 1080,
|
||||
Points =
|
||||
[
|
||||
new LocalCastVisualPoint(
|
||||
"host-rgb:0:0",
|
||||
new Vector3(0.25f, 0.1f, 1.2f),
|
||||
0.02f,
|
||||
new Vector4(0.8f, 0.4f, 0.2f, 0.9f),
|
||||
0.75f,
|
||||
1_040_000_000)
|
||||
],
|
||||
};
|
||||
|
||||
var mapper = new LocalCastTemporalGaussianMapper();
|
||||
var observations = mapper.Map(frame);
|
||||
|
||||
Assert.Single(observations);
|
||||
var observation = observations[0];
|
||||
Assert.Equal("host-rgb:0:0", observation.StableKey);
|
||||
Assert.True(observation.Center != frame.Points[0].Position);
|
||||
Assert.Equal(0.043f, observation.Radii.X, 3);
|
||||
Assert.InRange(observation.ColorOpacity.W, 0.0f, 0.98f);
|
||||
Assert.True(observation.ColorOpacity.X > frame.Points[0].ColorOpacity.X);
|
||||
Assert.Equal(0.75f, observation.Confidence, 6);
|
||||
Assert.True(observation.ShapePower > 1.0f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DoesNotDownsampleDenseCameraAndLeapClaimsInClientAdapter()
|
||||
{
|
||||
var points = new List<LocalCastVisualPoint>();
|
||||
for (var index = 0; index < 5000; index++)
|
||||
{
|
||||
points.Add(new LocalCastVisualPoint(
|
||||
index == 4500 ? "leap-motion:green:0:0" : $"room-rgb:{index}",
|
||||
new Vector3(index * 0.001f, 0.0f, 1.0f),
|
||||
0.02f,
|
||||
Vector4.One,
|
||||
0.8f,
|
||||
1_000_000_000 + index));
|
||||
}
|
||||
|
||||
var mapper = new LocalCastTemporalGaussianMapper();
|
||||
var observations = mapper.Map(new LocalCastVisualFrame
|
||||
{
|
||||
SchemaVersion = LocalCastVisualStateReader.RenderFrameSchemaId,
|
||||
FrameId = 1,
|
||||
CreatedMonotonicNs = 1_000_000_000,
|
||||
SourceTimeMinNs = 1_000_000_000,
|
||||
SourceTimeMaxNs = 1_000_010_000,
|
||||
PresentTimeNs = 1_350_000_000,
|
||||
AudioAlignmentTimeNs = 1_350_000_000,
|
||||
SpoutSenderName = "LocalCastBridge Point Cloud",
|
||||
TargetWidth = 1920,
|
||||
TargetHeight = 1080,
|
||||
Points = points,
|
||||
});
|
||||
|
||||
Assert.Equal(5000, observations.Count);
|
||||
Assert.Contains(observations, observation => observation.StableKey == "leap-motion:green:0:0");
|
||||
Assert.Contains(observations, observation => observation.StableKey.StartsWith("room-rgb:", StringComparison.Ordinal));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
using System.Buffers;
|
||||
using MessagePack;
|
||||
using Aquarium.LocalCast;
|
||||
|
||||
namespace Aquarium.LocalCast.Tests;
|
||||
|
||||
public sealed class LocalCastVisualStateReaderTests
|
||||
{
|
||||
[Fact]
|
||||
public void ParsesPythonCultCacheRenderFrameStore()
|
||||
{
|
||||
var store = WriteStore();
|
||||
|
||||
Assert.True(LocalCastVisualStateReader.TryParseStore(store, out var frame));
|
||||
Assert.Equal(7, frame.FrameId);
|
||||
Assert.Equal("LocalCastBridge Point Cloud", frame.SpoutSenderName);
|
||||
var point = Assert.Single(frame.Points);
|
||||
Assert.Equal("rgb:1", point.StableKey);
|
||||
Assert.Equal(0.03f, point.RadiusMeters, 6);
|
||||
}
|
||||
|
||||
private static byte[] WriteStore()
|
||||
{
|
||||
var payload = WriteRenderFramePayload();
|
||||
var buffer = new ArrayBufferWriter<byte>();
|
||||
var writer = new MessagePackWriter(buffer);
|
||||
writer.WriteArrayHeader(1);
|
||||
writer.WriteMapHeader(4);
|
||||
writer.Write("key");
|
||||
writer.Write(LocalCastVisualStateReader.LiveRenderFrameKey);
|
||||
writer.Write("type");
|
||||
writer.Write(LocalCastVisualStateReader.RenderFrameType);
|
||||
writer.Write("storedAt");
|
||||
writer.Write("2026-05-18T18:35:15Z");
|
||||
writer.Write("payload");
|
||||
writer.Write(payload);
|
||||
writer.Flush();
|
||||
return buffer.WrittenMemory.ToArray();
|
||||
}
|
||||
|
||||
private static byte[] WriteRenderFramePayload()
|
||||
{
|
||||
var buffer = new ArrayBufferWriter<byte>();
|
||||
var writer = new MessagePackWriter(buffer);
|
||||
writer.WriteArrayHeader(11);
|
||||
writer.Write(LocalCastVisualStateReader.RenderFrameSchemaId);
|
||||
writer.Write(7L);
|
||||
writer.Write(1_000_000L);
|
||||
writer.Write(1_000_000L);
|
||||
writer.Write(1_010_000L);
|
||||
writer.Write(1_260_000L);
|
||||
writer.Write(1_260_000L);
|
||||
writer.Write("LocalCastBridge Point Cloud");
|
||||
writer.Write(1920);
|
||||
writer.Write(1080);
|
||||
writer.WriteArrayHeader(1);
|
||||
writer.WriteArrayHeader(6);
|
||||
writer.Write("rgb:1");
|
||||
writer.WriteArrayHeader(3);
|
||||
writer.Write(0.1f);
|
||||
writer.Write(0.2f);
|
||||
writer.Write(1.2f);
|
||||
writer.Write(0.03f);
|
||||
writer.WriteArrayHeader(4);
|
||||
writer.Write(0.7f);
|
||||
writer.Write(0.6f);
|
||||
writer.Write(0.5f);
|
||||
writer.Write(0.9f);
|
||||
writer.Write(0.8f);
|
||||
writer.Write(1_010_000L);
|
||||
writer.Flush();
|
||||
return buffer.WrittenMemory.ToArray();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue