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

@ -11,7 +11,7 @@ using System.Text;
namespace Mimir.Runtime;
public sealed class MimirRuntime : IAquariumRuntime
public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesReceiver
{
private const float DefaultAudioSyncUpdateIntervalSeconds = 0.1f;
private const double HybridPassiveConfidenceThreshold = 0.12;
@ -73,6 +73,7 @@ public sealed class MimirRuntime : IAquariumRuntime
private IReadOnlyList<MimirAudioSpectrumSnapshot> lastAudioSpectra = [];
private long spectrumHistorySequence;
private readonly Queue<MimirSpectrumHistoryFrame> spectrumHistory = new();
private AquariumRuntimeServices runtimeServices = AquariumRuntimeServices.Empty;
public MimirRuntime(AquariumRuntimeOptions options)
: this(options, MimirRuntimeConfiguration.Load())
@ -145,6 +146,11 @@ public sealed class MimirRuntime : IAquariumRuntime
public AquariumAudioDocument Audio => audio;
public void AttachServices(AquariumRuntimeServices services)
{
runtimeServices = services;
}
private AquariumFrame CreateFrame()
{
var channelCount = Math.Max(1, lastAudioSpectra.Count);
@ -186,6 +192,7 @@ public sealed class MimirRuntime : IAquariumRuntime
var observations = new List<MimirObservation>(buffers.Count);
MimirFensalirBridgeMapper.MapWindows(buffers, windows);
MimirFensalirBridgeMapper.MapLatestObservations(buffers, observations);
CommitProducerLeases(windows);
var constraints = new List<MimirCalibrationConstraint>(synchronization.AudioSynchronizationStates.Count);
MimirFensalirBridgeMapper.MapCalibrationConstraints(synchronization.AudioSynchronizationStates, constraints);
@ -216,6 +223,24 @@ public sealed class MimirRuntime : IAquariumRuntime
return AddSpectrumFieldEvidence(frame);
}
private void CommitProducerLeases(IReadOnlyList<MimirRollingStreamWindow> windows)
{
foreach (var window in windows)
{
if (window.SourceKind != MimirStreamKind.Video ||
string.IsNullOrWhiteSpace(window.Payload.ResourceKey) ||
window.Payload.ProducerFenceValue == 0)
{
continue;
}
runtimeServices.FieldResources.CommitLeaseVersion(
window.Payload.ResourceKey,
window.SequenceId,
window.Payload.ProducerFenceValue);
}
}
private static MimirSurfaceIntent BuildSurfaceIntent(MimirRollingStreamWindow window, MimirObservation observation)
{
if (window.SourceKind == MimirStreamKind.Audio)

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);