From f4c5988e3d22dfa6748b6d24caabe173037371a1 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 19 May 2026 12:28:59 +0100 Subject: [PATCH] Decode LocalCast native render points --- .../LocalCastNativeReservoir.cs | 16 ++++ .../LocalCastNativeVisualFrameSource.cs | 37 ++++++++ .../LocalCastNativeReservoirTests.cs | 25 ++++++ .../LocalCastNativeVisualFrameSourceTests.cs | 86 +++++++++++++++++++ 4 files changed, 164 insertions(+) diff --git a/src/Aquarium.LocalCast/LocalCastNativeReservoir.cs b/src/Aquarium.LocalCast/LocalCastNativeReservoir.cs index 5c436d5..99f5823 100644 --- a/src/Aquarium.LocalCast/LocalCastNativeReservoir.cs +++ b/src/Aquarium.LocalCast/LocalCastNativeReservoir.cs @@ -68,6 +68,22 @@ public struct LocalCastNativeRenderPacketDescriptor public ulong MetadataHandle; } +[StructLayout(LayoutKind.Sequential)] +public struct LocalCastNativeRenderPoint +{ + public ulong StableKeyHash; + public ulong SourceTimestampNs; + public float X; + public float Y; + public float Z; + public float RadiusMeters; + public float Red; + public float Green; + public float Blue; + public float Alpha; + public float Confidence; +} + [StructLayout(LayoutKind.Sequential)] public struct LocalCastNativeRuntimeStatus { diff --git a/src/Aquarium.LocalCast/LocalCastNativeVisualFrameSource.cs b/src/Aquarium.LocalCast/LocalCastNativeVisualFrameSource.cs index aa37c26..81629ee 100644 --- a/src/Aquarium.LocalCast/LocalCastNativeVisualFrameSource.cs +++ b/src/Aquarium.LocalCast/LocalCastNativeVisualFrameSource.cs @@ -1,4 +1,5 @@ using System.Runtime.InteropServices; +using System.Numerics; namespace Aquarium.LocalCast; @@ -55,6 +56,42 @@ public sealed class LocalCastNativeRenderDescriptorDecoder return true; } + public static bool DecodeNativePointBuffer( + in LocalCastNativeRenderPacketDescriptor descriptor, + LocalCastNativeSampleHandle _, + out IReadOnlyList points) + { + points = []; + var pointSize = Marshal.SizeOf(); + if (descriptor.PointCount == 0) + { + return true; + } + + if (descriptor.PointBufferHandle == 0 || descriptor.PointStrideBytes < pointSize) + { + return false; + } + + var decoded = new LocalCastVisualPoint[checked((int)descriptor.PointCount)]; + var cursor = (IntPtr)descriptor.PointBufferHandle; + var stride = checked((int)descriptor.PointStrideBytes); + for (var index = 0; index < decoded.Length; index++) + { + var native = Marshal.PtrToStructure(IntPtr.Add(cursor, index * stride)); + decoded[index] = new LocalCastVisualPoint( + $"native:{native.StableKeyHash:x16}", + new Vector3(native.X, native.Y, native.Z), + native.RadiusMeters, + new Vector4(native.Red, native.Green, native.Blue, native.Alpha), + native.Confidence, + checked((long)native.SourceTimestampNs)); + } + + points = decoded; + return true; + } + private static LocalCastVisualFrame EmptyFrame() { return new LocalCastVisualFrame diff --git a/tests/Aquarium.LocalCast.Tests/LocalCastNativeReservoirTests.cs b/tests/Aquarium.LocalCast.Tests/LocalCastNativeReservoirTests.cs index 9d886c1..d82113d 100644 --- a/tests/Aquarium.LocalCast.Tests/LocalCastNativeReservoirTests.cs +++ b/tests/Aquarium.LocalCast.Tests/LocalCastNativeReservoirTests.cs @@ -89,6 +89,31 @@ public sealed class LocalCastNativeReservoirTests Assert.Equal(77u, descriptor.MetadataHandle); } + [Fact] + public void RenderPointLayoutMatchesNativeAbi() + { + Assert.Equal(56, Marshal.SizeOf()); + + var point = new LocalCastNativeRenderPoint + { + StableKeyHash = 0xCAFE, + SourceTimestampNs = 1_900, + X = 0.1f, + Y = 0.2f, + Z = 1.2f, + RadiusMeters = 0.03f, + Red = 0.7f, + Green = 0.6f, + Blue = 0.5f, + Alpha = 0.9f, + Confidence = 0.8f, + }; + + Assert.Equal(0xCAFEu, point.StableKeyHash); + Assert.Equal(1_900u, point.SourceTimestampNs); + Assert.Equal(0.03f, point.RadiusMeters); + } + [Fact] public void RuntimeStatusLayoutUsesNativeSizeCounts() { diff --git a/tests/Aquarium.LocalCast.Tests/LocalCastNativeVisualFrameSourceTests.cs b/tests/Aquarium.LocalCast.Tests/LocalCastNativeVisualFrameSourceTests.cs index 91a28cc..b68fb86 100644 --- a/tests/Aquarium.LocalCast.Tests/LocalCastNativeVisualFrameSourceTests.cs +++ b/tests/Aquarium.LocalCast.Tests/LocalCastNativeVisualFrameSourceTests.cs @@ -161,6 +161,92 @@ public sealed class LocalCastNativeVisualFrameSourceTests } } + [Fact] + public void NativePointBufferDecoderReadsStridedPoints() + { + var points = new[] + { + new LocalCastNativeRenderPoint + { + StableKeyHash = 0xCAFE, + SourceTimestampNs = 1_900, + X = 0.1f, + Y = 0.2f, + Z = 1.2f, + RadiusMeters = 0.03f, + Red = 0.7f, + Green = 0.6f, + Blue = 0.5f, + Alpha = 0.9f, + Confidence = 0.8f, + }, + new LocalCastNativeRenderPoint + { + StableKeyHash = 0xBEEF, + SourceTimestampNs = 1_950, + X = -0.1f, + Y = 0.4f, + Z = 1.4f, + RadiusMeters = 0.04f, + Red = 0.2f, + Green = 0.3f, + Blue = 0.9f, + Alpha = 0.6f, + Confidence = 0.5f, + }, + }; + var pointSize = Marshal.SizeOf(); + var pointer = Marshal.AllocHGlobal(pointSize * points.Length); + try + { + for (var index = 0; index < points.Length; index++) + { + Marshal.StructureToPtr(points[index], IntPtr.Add(pointer, index * pointSize), false); + } + + var descriptor = new LocalCastNativeRenderPacketDescriptor + { + PointBufferHandle = (ulong)pointer, + PointCount = (uint)points.Length, + PointStrideBytes = (uint)pointSize, + }; + + Assert.True(LocalCastNativeRenderDescriptorDecoder.DecodeNativePointBuffer(in descriptor, default, out var decoded)); + Assert.Equal(2, decoded.Count); + Assert.Equal("native:000000000000cafe", decoded[0].StableKey); + Assert.Equal(new Vector3(0.1f, 0.2f, 1.2f), decoded[0].Position); + Assert.Equal(new Vector4(0.7f, 0.6f, 0.5f, 0.9f), decoded[0].ColorOpacity); + Assert.Equal(1_950, decoded[1].SourceTimestampNs); + } + finally + { + Marshal.FreeHGlobal(pointer); + } + } + + [Fact] + public void NativePointBufferDecoderRejectsMissingBufferAndShortStride() + { + var missing = new LocalCastNativeRenderPacketDescriptor + { + PointCount = 1, + PointStrideBytes = (uint)Marshal.SizeOf(), + }; + Assert.False(LocalCastNativeRenderDescriptorDecoder.DecodeNativePointBuffer(in missing, default, out _)); + + var empty = new LocalCastNativeRenderPacketDescriptor(); + Assert.True(LocalCastNativeRenderDescriptorDecoder.DecodeNativePointBuffer(in empty, default, out var points)); + Assert.Empty(points); + + var shortStride = new LocalCastNativeRenderPacketDescriptor + { + PointBufferHandle = 1, + PointCount = 1, + PointStrideBytes = 8, + }; + Assert.False(LocalCastNativeRenderDescriptorDecoder.DecodeNativePointBuffer(in shortStride, default, out _)); + } + private static LocalCastVisualFrame FrameFromPayload(ulong payloadHandle, ulong timestampNs) { return new LocalCastVisualFrame