Decode LocalCast native render points

This commit is contained in:
Meta Vens 2026-05-19 12:28:59 +01:00
commit f4c5988e3d
4 changed files with 164 additions and 0 deletions

View file

@ -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
{

View file

@ -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<LocalCastVisualPoint> points)
{
points = [];
var pointSize = Marshal.SizeOf<LocalCastNativeRenderPoint>();
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<LocalCastNativeRenderPoint>(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

View file

@ -89,6 +89,31 @@ public sealed class LocalCastNativeReservoirTests
Assert.Equal(77u, descriptor.MetadataHandle);
}
[Fact]
public void RenderPointLayoutMatchesNativeAbi()
{
Assert.Equal(56, Marshal.SizeOf<LocalCastNativeRenderPoint>());
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()
{

View file

@ -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<LocalCastNativeRenderPoint>();
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<LocalCastNativeRenderPoint>(),
};
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