Accumulate LocalCast GPU fusion history
This commit is contained in:
parent
c7d64a00aa
commit
2b30d67b43
4 changed files with 207 additions and 4 deletions
|
|
@ -41,6 +41,14 @@ the D3D12 backend, `D3D12LocalCastFusion.hlsl` dispatches over them, and the
|
|||
compute shader writes the temporal Gaussian buffer consumed by the SDF Gaussian
|
||||
draw. It does not downsample the document in client space.
|
||||
|
||||
LocalCast GPU fusion also has its own live-history accumulator. New frames
|
||||
update stable-key tracks, stale tracks expire after a bounded horizon, and the
|
||||
whole retained set is sent to the GPU seed buffer every frame. The default
|
||||
history horizon is 18 seconds and may be overridden with
|
||||
`LOCALCAST_GPU_HISTORY_SECONDS` from 1 to 120 seconds. That is the reconstruction
|
||||
buffer: long enough to harvest samples, bounded enough to remain a machine
|
||||
instead of a scrapbook with a power cord.
|
||||
|
||||
## Invariants
|
||||
|
||||
- Field accumulation happens in world space before pixel history. TAA is the
|
||||
|
|
@ -82,6 +90,8 @@ Ownership:
|
|||
- `AquariumGpuFusionField` is the renderer contract for GPU-owned fusion input.
|
||||
- `LocalCastGpuFusionMapper` converts typed LocalCast point claims into compact
|
||||
seeds without client downsampling.
|
||||
- `LocalCastGpuFusionAccumulator` owns the bounded live history buffer for
|
||||
stable seeds before GPU lowering.
|
||||
- `D3D12LocalCastFusion.hlsl` owns the first compute lowering pass.
|
||||
- `D3D12Renderer` owns UAV-capable temporal Gaussian storage, dispatch, and the
|
||||
transition back to shader-resource state for the draw.
|
||||
|
|
|
|||
127
src/Aquarium.LocalCast/LocalCastGpuFusionAccumulator.cs
Normal file
127
src/Aquarium.LocalCast/LocalCastGpuFusionAccumulator.cs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
using System.Numerics;
|
||||
using Aquarium.Engine.Render;
|
||||
|
||||
namespace Aquarium.LocalCast;
|
||||
|
||||
public sealed class LocalCastGpuFusionAccumulator
|
||||
{
|
||||
private readonly Dictionary<string, Track> tracks = new(StringComparer.Ordinal);
|
||||
private readonly LocalCastGpuFusionMapper mapper = new();
|
||||
private readonly float historySeconds;
|
||||
private readonly float smoothing;
|
||||
private readonly int maxSeedCount;
|
||||
|
||||
public LocalCastGpuFusionAccumulator(float historySeconds, float smoothing = 0.45f, int maxSeedCount = 1_048_576)
|
||||
{
|
||||
if (!float.IsFinite(historySeconds) || historySeconds <= 0.0f)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(historySeconds), historySeconds, "History window must be positive.");
|
||||
}
|
||||
|
||||
if (!float.IsFinite(smoothing) || smoothing <= 0.0f || smoothing > 1.0f)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(smoothing), smoothing, "Smoothing must be in (0, 1].");
|
||||
}
|
||||
|
||||
this.historySeconds = historySeconds;
|
||||
this.smoothing = smoothing;
|
||||
this.maxSeedCount = Math.Max(1, maxSeedCount);
|
||||
}
|
||||
|
||||
public int TrackCount => tracks.Count;
|
||||
|
||||
public void Observe(LocalCastVisualFrame frame)
|
||||
{
|
||||
var field = mapper.Map(frame);
|
||||
var frameTimeSeconds = mapper.ToTimelineSeconds(frame.SourceTimeMaxNs);
|
||||
foreach (var seed in field.Seeds)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(seed.StableKey))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tracks.TryGetValue(seed.StableKey, out var current))
|
||||
{
|
||||
tracks[seed.StableKey] = current.Update(seed, frameTimeSeconds, smoothing);
|
||||
}
|
||||
else
|
||||
{
|
||||
tracks[seed.StableKey] = Track.Create(seed, frameTimeSeconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public AquariumGpuFusionField BuildField(float renderTimeSeconds)
|
||||
{
|
||||
var cutoff = renderTimeSeconds - historySeconds;
|
||||
var expired = new List<string>();
|
||||
var seeds = new List<AquariumGpuFusionSeed>(Math.Min(tracks.Count, maxSeedCount));
|
||||
|
||||
foreach (var (key, track) in tracks)
|
||||
{
|
||||
if (track.LastObservedTimeSeconds < cutoff)
|
||||
{
|
||||
expired.Add(key);
|
||||
continue;
|
||||
}
|
||||
|
||||
var age = MathF.Max(0.0f, renderTimeSeconds - track.LastObservedTimeSeconds);
|
||||
var retained = Math.Clamp(1.0f - (age / historySeconds), 0.0f, 1.0f);
|
||||
var predictedCenter = track.Seed.Center + track.Seed.Velocity * age;
|
||||
var previousCenter = predictedCenter - track.Seed.Velocity * (1.0f / 60.0f);
|
||||
seeds.Add(track.Seed with
|
||||
{
|
||||
Center = predictedCenter,
|
||||
PreviousCenter = previousCenter,
|
||||
HistoryWeight = Math.Clamp(track.Seed.HistoryWeight * retained, 0.0f, 1.0f),
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var key in expired)
|
||||
{
|
||||
tracks.Remove(key);
|
||||
}
|
||||
|
||||
return new AquariumGpuFusionField
|
||||
{
|
||||
Seeds = seeds
|
||||
.OrderByDescending(seed => seed.HistoryWeight)
|
||||
.Take(maxSeedCount)
|
||||
.ToArray(),
|
||||
AccumulationWindowSeconds = historySeconds,
|
||||
PresentationDelaySeconds = 0.35f,
|
||||
};
|
||||
}
|
||||
|
||||
private readonly record struct Track(AquariumGpuFusionSeed Seed, float LastObservedTimeSeconds)
|
||||
{
|
||||
public static Track Create(AquariumGpuFusionSeed seed, float observedTimeSeconds)
|
||||
{
|
||||
return new Track(seed, observedTimeSeconds);
|
||||
}
|
||||
|
||||
public Track Update(AquariumGpuFusionSeed seed, float observedTimeSeconds, float smoothing)
|
||||
{
|
||||
var dt = MathF.Max(0.0001f, observedTimeSeconds - LastObservedTimeSeconds);
|
||||
var measuredVelocity = (seed.Center - Seed.Center) / dt;
|
||||
var confidence = Math.Clamp(seed.Confidence, 0.0f, 1.0f);
|
||||
var alpha = smoothing * confidence;
|
||||
var updatedCenter = Vector3.Lerp(Seed.Center, seed.Center, alpha);
|
||||
return new Track(
|
||||
Seed with
|
||||
{
|
||||
Center = updatedCenter,
|
||||
PreviousCenter = Seed.Center,
|
||||
Velocity = Vector3.Lerp(Seed.Velocity, measuredVelocity, alpha),
|
||||
Radii = Vector3.Lerp(Seed.Radii, seed.Radii, alpha),
|
||||
ColorOpacity = Vector4.Lerp(Seed.ColorOpacity, seed.ColorOpacity, alpha),
|
||||
Confidence = Math.Clamp((Seed.Confidence * 0.85f) + (confidence * 0.15f), 0.0f, 1.0f),
|
||||
HistoryWeight = Math.Clamp((Seed.HistoryWeight * 0.75f) + (confidence * 0.25f), 0.0f, 1.0f),
|
||||
Falloff = MathF.Max(0.0001f, seed.Falloff),
|
||||
ShapePower = MathF.Max(0.0001f, seed.ShapePower),
|
||||
},
|
||||
observedTimeSeconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,8 +16,10 @@ public sealed class LocalCastRuntime : IAquariumRuntime
|
|||
smoothing: 0.55f);
|
||||
private readonly LocalCastTemporalGaussianMapper mapper = new();
|
||||
private readonly LocalCastGpuFusionMapper gpuFusionMapper = new();
|
||||
private readonly LocalCastGpuFusionAccumulator gpuFusionAccumulator = new(
|
||||
ResolveGpuHistorySeconds(),
|
||||
smoothing: 0.45f);
|
||||
private readonly LocalCastVisualStateReader reader;
|
||||
private AquariumGpuFusionField latestGpuFusionField = AquariumGpuFusionField.Empty;
|
||||
private float timeSeconds;
|
||||
private float sceneTimelineSeconds;
|
||||
private long lastFrameId = -1;
|
||||
|
|
@ -34,6 +36,7 @@ public sealed class LocalCastRuntime : IAquariumRuntime
|
|||
panel.Section("Camera Field");
|
||||
panel.Readout("Cache", () => reader.Path);
|
||||
panel.Readout("Frame", () => lastFrameId >= 0 ? $"{lastFrameId} / {latestPointCount} splats" : latestStatus);
|
||||
panel.Readout("History", () => $"{gpuFusionAccumulator.TrackCount} tracked seeds");
|
||||
panel.Readout("Timeline", () => $"{sceneTimelineSeconds:0.000}s");
|
||||
});
|
||||
}
|
||||
|
|
@ -55,14 +58,15 @@ public sealed class LocalCastRuntime : IAquariumRuntime
|
|||
get
|
||||
{
|
||||
var temporalField = accumulator.BuildField(sceneTimelineSeconds);
|
||||
var gpuFusionField = gpuFusionAccumulator.BuildField(sceneTimelineSeconds);
|
||||
var scene = new AquariumSceneState
|
||||
{
|
||||
TraceHeightFieldSurface = false,
|
||||
UseStarfieldBackground = false,
|
||||
TemporalGaussianField = ReferenceEquals(latestGpuFusionField, AquariumGpuFusionField.Empty)
|
||||
TemporalGaussianField = gpuFusionField.Seeds.Count == 0
|
||||
? temporalField
|
||||
: AquariumTemporalGaussianField.Empty,
|
||||
GpuFusionField = latestGpuFusionField,
|
||||
GpuFusionField = gpuFusionField,
|
||||
};
|
||||
|
||||
return new AquariumFrame(
|
||||
|
|
@ -99,7 +103,7 @@ public sealed class LocalCastRuntime : IAquariumRuntime
|
|||
if (frame.FrameId != lastFrameId)
|
||||
{
|
||||
accumulator.Observe(mapper.Map(frame));
|
||||
latestGpuFusionField = gpuFusionMapper.Map(frame);
|
||||
gpuFusionAccumulator.Observe(frame);
|
||||
lastFrameId = frame.FrameId;
|
||||
latestPointCount = frame.Points.Count;
|
||||
latestStatus = "live";
|
||||
|
|
@ -134,6 +138,14 @@ public sealed class LocalCastRuntime : IAquariumRuntime
|
|||
"runs",
|
||||
"visual-state.msgpack");
|
||||
}
|
||||
|
||||
private static float ResolveGpuHistorySeconds()
|
||||
{
|
||||
var raw = Environment.GetEnvironmentVariable("LOCALCAST_GPU_HISTORY_SECONDS");
|
||||
return float.TryParse(raw, out var seconds) && float.IsFinite(seconds) && seconds > 0.0f
|
||||
? Math.Clamp(seconds, 1.0f, 120.0f)
|
||||
: 18.0f;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LocalCastRuntimeFactory : IAquariumRuntimeFactory
|
||||
|
|
|
|||
|
|
@ -116,4 +116,58 @@ public sealed class LocalCastTemporalGaussianMapperTests
|
|||
Assert.Contains(field.Seeds, seed => seed.StableKey.StartsWith("dense-rgb:", StringComparison.Ordinal));
|
||||
Assert.Contains(field.Seeds, seed => seed.ShapePower > 2.0f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GpuFusionAccumulatorRetainsStableSamplesAcrossHistoryWindow()
|
||||
{
|
||||
var accumulator = new LocalCastGpuFusionAccumulator(historySeconds: 10.0f, smoothing: 1.0f, maxSeedCount: 16);
|
||||
accumulator.Observe(FrameWithPoint(1, "room-rgb:old", new Vector3(-0.2f, 0.0f, 1.0f), 1_000_000_000));
|
||||
accumulator.Observe(FrameWithPoint(2, "room-rgb:new", new Vector3(0.2f, 0.0f, 1.0f), 5_000_000_000));
|
||||
|
||||
var field = accumulator.BuildField(6.0f);
|
||||
|
||||
Assert.Equal(2, field.Seeds.Count);
|
||||
Assert.Contains(field.Seeds, seed => seed.StableKey == "room-rgb:old");
|
||||
Assert.Contains(field.Seeds, seed => seed.StableKey == "room-rgb:new");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GpuFusionAccumulatorExpiresSamplesOutsideHistoryWindow()
|
||||
{
|
||||
var accumulator = new LocalCastGpuFusionAccumulator(historySeconds: 2.0f, smoothing: 1.0f, maxSeedCount: 16);
|
||||
accumulator.Observe(FrameWithPoint(1, "room-rgb:old", new Vector3(-0.2f, 0.0f, 1.0f), 1_000_000_000));
|
||||
accumulator.Observe(FrameWithPoint(2, "room-rgb:new", new Vector3(0.2f, 0.0f, 1.0f), 4_000_000_000));
|
||||
|
||||
var field = accumulator.BuildField(4.2f);
|
||||
|
||||
Assert.Single(field.Seeds);
|
||||
Assert.Equal("room-rgb:new", field.Seeds[0].StableKey);
|
||||
}
|
||||
|
||||
private static LocalCastVisualFrame FrameWithPoint(long frameId, string key, Vector3 position, long sourceTimeNs)
|
||||
{
|
||||
return new LocalCastVisualFrame
|
||||
{
|
||||
SchemaVersion = LocalCastVisualStateReader.RenderFrameSchemaId,
|
||||
FrameId = frameId,
|
||||
CreatedMonotonicNs = sourceTimeNs,
|
||||
SourceTimeMinNs = sourceTimeNs,
|
||||
SourceTimeMaxNs = sourceTimeNs,
|
||||
PresentTimeNs = sourceTimeNs + 350_000_000,
|
||||
AudioAlignmentTimeNs = sourceTimeNs + 350_000_000,
|
||||
SpoutSenderName = "LocalCastBridge Point Cloud",
|
||||
TargetWidth = 1920,
|
||||
TargetHeight = 1080,
|
||||
Points =
|
||||
[
|
||||
new LocalCastVisualPoint(
|
||||
key,
|
||||
position,
|
||||
0.012f,
|
||||
new Vector4(0.5f, 0.7f, 0.9f, 0.85f),
|
||||
0.9f,
|
||||
sourceTimeNs)
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue