Lease camera textures from Fensalir

This commit is contained in:
Meta Vens 2026-05-27 13:29:48 +01:00
commit daf1d4ef8a
10 changed files with 284 additions and 23 deletions

View file

@ -48,7 +48,8 @@ public readonly record struct MimirBridgePayloadView(
ulong NativeHandle,
string NativeHandleKind,
int ByteLength,
string ResourceKey = "")
string ResourceKey = "",
ulong ProducerFenceValue = 0)
{
public bool HasResource => NativeHandle != 0 || !string.IsNullOrWhiteSpace(ResourceKey);
}
@ -361,11 +362,14 @@ public static class MimirFensalirBridgeMapper
{
var nativeHandle = sample.VideoFrame?.NativeHandle ?? sample.AudioBlock?.NativeHandle ?? sample.PayloadHandle;
var nativeHandleKind = sample.VideoFrame?.NativeHandleKind ?? sample.AudioBlock?.NativeHandleKind ?? "";
var resourceKey = sample.VideoFrame?.ResourceKey ?? "";
var producerFenceValue = sample.VideoFrame?.ProducerFenceValue ?? 0;
return new MimirBridgePayloadView(
nativeHandle,
nativeHandleKind,
sample.ByteLength,
ResourceKeyForPayload(nativeHandle, nativeHandleKind));
string.IsNullOrWhiteSpace(resourceKey) ? ResourceKeyForPayload(nativeHandle, nativeHandleKind) : resourceKey,
producerFenceValue);
}
public static string ResourceKeyForPayload(MimirBridgePayloadView payload) =>

View file

@ -0,0 +1,86 @@
using Aquarium.Engine.Render;
namespace Mimir.Runtime.Synchronization;
public readonly record struct MimirFensalirTextureLeaseRequest(
string SourceId,
int Width,
int Height,
MimirVideoPixelFormat PixelFormat,
int StrideBytes,
long DeviceTimestampNs,
ulong Version,
AquariumFieldShaderAccess ProducerAccess = AquariumFieldShaderAccess.ShaderResource);
public readonly record struct MimirFensalirTextureLease(
MimirVideoFrameDescriptor Frame,
IntPtr NativeHandle,
IntPtr ProducerFenceHandle,
AquariumFieldResourceDeclaration Declaration)
{
public bool IsValid => NativeHandle != IntPtr.Zero && Frame.NativeHandle != 0 && !string.IsNullOrWhiteSpace(Frame.ResourceKey);
}
public sealed class MimirFensalirTextureLeaseClient(IAquariumFieldResourceBroker broker)
{
public bool TryLeaseTexture2D(MimirFensalirTextureLeaseRequest request, out MimirFensalirTextureLease lease)
{
lease = default;
if (string.IsNullOrWhiteSpace(request.SourceId) ||
request.Width <= 0 ||
request.Height <= 0 ||
request.StrideBytes <= 0)
{
return false;
}
var resourceKey = ResourceKeyForSource(request.SourceId);
var engineLease = broker.LeaseTexture2D(new AquariumTexture2DLeaseRequest(
resourceKey,
request.Width,
request.Height,
FormatForPixelFormat(request.PixelFormat),
request.ProducerAccess,
request.Version));
if (!engineLease.IsValid || engineLease.NativeHandle == IntPtr.Zero)
{
return false;
}
var frame = new MimirVideoFrameDescriptor(
request.Width,
request.Height,
request.PixelFormat,
request.StrideBytes,
request.DeviceTimestampNs,
NativeHandle: unchecked((ulong)engineLease.NativeHandle.ToInt64()),
NativeHandleKind: engineLease.NativeHandleKind,
ResourceKey: resourceKey,
ProducerFenceValue: 0);
lease = new MimirFensalirTextureLease(
frame,
engineLease.NativeHandle,
engineLease.ProducerFenceHandle,
engineLease.Declaration);
return lease.IsValid;
}
public bool Commit(string resourceKey, ulong version, ulong producerFenceValue) =>
broker.CommitLeaseVersion(resourceKey, version, producerFenceValue);
public static string ResourceKeyForSource(string sourceId)
{
var normalized = sourceId.Trim().ToLowerInvariant().Replace('\\', '/');
return $"mimir:resource:camera:{normalized}:texture2d";
}
private static string FormatForPixelFormat(MimirVideoPixelFormat pixelFormat) =>
pixelFormat switch
{
MimirVideoPixelFormat.Gray8 or MimirVideoPixelFormat.R8 => "R8Unorm",
MimirVideoPixelFormat.Rg8 or MimirVideoPixelFormat.LeapStereoIr => "R8G8_UNorm",
MimirVideoPixelFormat.Bgra8 => "Bgra8",
MimirVideoPixelFormat.Nv12 => "Nv12",
_ => "Rgba8Unorm",
};
}

View file

@ -22,4 +22,6 @@ public sealed record MimirVideoFrameDescriptor(
int StrideBytes,
long DeviceTimestampNs,
ulong NativeHandle = 0,
string NativeHandleKind = "");
string NativeHandleKind = "",
string ResourceKey = "",
ulong ProducerFenceValue = 0);