Lease camera textures from Fensalir
This commit is contained in:
parent
f9151b091b
commit
daf1d4ef8a
10 changed files with 284 additions and 23 deletions
|
|
@ -108,6 +108,11 @@ if (args.Any(arg => string.Equals(arg, "--fensalir-camera-observation-smoke", St
|
|||
return RunFensalirCameraObservationSmoke();
|
||||
}
|
||||
|
||||
if (args.Any(arg => string.Equals(arg, "--fensalir-texture-lease-smoke", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return RunFensalirTextureLeaseSmoke();
|
||||
}
|
||||
|
||||
if (args.Any(arg => string.Equals(arg, "--mimir-spectrum-upload-smoke", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return RunMimirSpectrumUploadSmoke();
|
||||
|
|
@ -718,6 +723,78 @@ static int RunFensalirCameraObservationSmoke()
|
|||
: 1;
|
||||
}
|
||||
|
||||
static int RunFensalirTextureLeaseSmoke()
|
||||
{
|
||||
var broker = new FakeFieldResourceBroker();
|
||||
var client = new MimirFensalirTextureLeaseClient(broker);
|
||||
var requested = new MimirFensalirTextureLeaseRequest(
|
||||
"kiyo-pro-rgb",
|
||||
1920,
|
||||
1080,
|
||||
MimirVideoPixelFormat.Bgra8,
|
||||
1920 * 4,
|
||||
DeviceTimestampNs: 1_000_000_000L,
|
||||
Version: 17);
|
||||
|
||||
if (!client.TryLeaseTexture2D(requested, out var lease))
|
||||
{
|
||||
Console.Error.WriteLine("fensalir-texture-lease-smoke failed to lease texture");
|
||||
return 1;
|
||||
}
|
||||
|
||||
var source = new MimirNativeIngestStreamSource(
|
||||
new MimirStreamDescriptor("kiyo-pro-rgb", MimirStreamKind.Video, MimirStreamOrigin.LocalDevice));
|
||||
using (var runtime = new MimirRuntime(
|
||||
new AquariumRuntimeOptions(Headless: true, CultCachePath: null),
|
||||
new MimirSynchronizationSettings(),
|
||||
[source]))
|
||||
{
|
||||
runtime.AttachServices(new AquariumRuntimeServices(broker));
|
||||
runtime.OnSceneReady();
|
||||
source.PushVideoFrame(lease.Frame with { ProducerFenceValue = 41 }, 1_000_000_010L);
|
||||
runtime.Update(0.016f, new InputState());
|
||||
_ = runtime.Frame;
|
||||
}
|
||||
|
||||
var buffer = new MimirRollingStreamBuffer(
|
||||
new MimirStreamDescriptor("kiyo-pro-rgb", MimirStreamKind.Video, MimirStreamOrigin.LocalDevice),
|
||||
TimeSpan.FromSeconds(5));
|
||||
buffer.Append(new MimirStreamSample(
|
||||
"kiyo-pro-rgb",
|
||||
MimirStreamKind.Video,
|
||||
MimirStreamOrigin.LocalDevice,
|
||||
TimestampNs: 1_000_000_000L,
|
||||
ArrivalNs: 1_000_000_010L,
|
||||
Sequence: 17,
|
||||
PayloadHandle: 0,
|
||||
ByteLength: 1920 * 1080 * 4,
|
||||
Data: default,
|
||||
VideoFrame: lease.Frame with { ProducerFenceValue = 41 }));
|
||||
|
||||
var frame = new MimirFensalirFieldLowering().BuildCameraObservationFrame([buffer]);
|
||||
var validation = AquariumFieldEvidenceValidator.Validate(frame);
|
||||
var expectedResourceKey = MimirFensalirTextureLeaseClient.ResourceKeyForSource("kiyo-pro-rgb");
|
||||
|
||||
Console.WriteLine(
|
||||
$"fensalir-texture-lease-smoke leased={lease.IsValid} committed={broker.CommittedFenceValue > 0} resources={frame.Resources.Count} claims={frame.Claims.Count} errors={validation.HasErrors}");
|
||||
|
||||
return lease.IsValid &&
|
||||
broker.CommittedResourceKey == expectedResourceKey &&
|
||||
broker.CommittedVersion == 0 &&
|
||||
broker.CommittedFenceValue == 41 &&
|
||||
lease.Frame.ResourceKey == expectedResourceKey &&
|
||||
lease.Frame.NativeHandle == 0xCAFE &&
|
||||
lease.Frame.NativeHandleKind == "fensalir-owned-d3d12-texture2d" &&
|
||||
frame.Resources.Count == 1 &&
|
||||
frame.Resources[0].ResourceKey == expectedResourceKey &&
|
||||
frame.Resources[0].NativeHandle == new IntPtr(0xCAFE) &&
|
||||
frame.Resources[0].NativeHandleKind == "fensalir-owned-d3d12-texture2d" &&
|
||||
frame.Claims.Any(claim => claim.PayloadHandle == expectedResourceKey) &&
|
||||
!validation.HasErrors
|
||||
? 0
|
||||
: 1;
|
||||
}
|
||||
|
||||
static int RunMimirSpectrumUploadSmoke()
|
||||
{
|
||||
var previousSyntheticPreview = Environment.GetEnvironmentVariable("MIMIR_SYNTHETIC_SPECTRUM_PREVIEW");
|
||||
|
|
@ -5948,3 +6025,46 @@ public sealed record ComplexContourChannelModelEvaluationCase(
|
|||
double DeltaAbsolutePredictionErrorMicroseconds,
|
||||
double DeltaMeanAbsoluteErrorSamples,
|
||||
double DeltaMeanAbsolutePhaseErrorRadians);
|
||||
|
||||
sealed class FakeFieldResourceBroker : IAquariumFieldResourceBroker
|
||||
{
|
||||
public string CommittedResourceKey { get; private set; } = "";
|
||||
|
||||
public ulong CommittedVersion { get; private set; }
|
||||
|
||||
public ulong CommittedFenceValue { get; private set; }
|
||||
|
||||
public AquariumFieldResourceLease LeaseTexture2D(AquariumTexture2DLeaseRequest request)
|
||||
{
|
||||
var declaration = new AquariumFieldResourceDeclaration(
|
||||
request.ResourceKey,
|
||||
AquariumFieldResourceKind.Texture2D,
|
||||
AquariumFieldResourceResidency.SharedGpu,
|
||||
AquariumFieldShaderAccess.ShaderResource,
|
||||
request.Format,
|
||||
request.Width,
|
||||
request.Height,
|
||||
DepthOrCount: 1,
|
||||
StrideBytes: AquariumFieldResourceDeclaration.FormatStrideBytes(request.Format),
|
||||
request.ValidFromNs,
|
||||
request.ValidUntilNs,
|
||||
request.Version,
|
||||
new IntPtr(0xCAFE),
|
||||
"fensalir-owned-d3d12-texture2d");
|
||||
return new AquariumFieldResourceLease(
|
||||
declaration,
|
||||
new IntPtr(0xCAFE),
|
||||
"fensalir-owned-d3d12-texture2d",
|
||||
new IntPtr(0xF00D),
|
||||
request.Version,
|
||||
true);
|
||||
}
|
||||
|
||||
public bool CommitLeaseVersion(string resourceKey, ulong version, ulong producerFenceValue)
|
||||
{
|
||||
CommittedResourceKey = resourceKey;
|
||||
CommittedVersion = version;
|
||||
CommittedFenceValue = producerFenceValue;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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) =>
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
};
|
||||
}
|
||||
|
|
@ -22,4 +22,6 @@ public sealed record MimirVideoFrameDescriptor(
|
|||
int StrideBytes,
|
||||
long DeviceTimestampNs,
|
||||
ulong NativeHandle = 0,
|
||||
string NativeHandleKind = "");
|
||||
string NativeHandleKind = "",
|
||||
string ResourceKey = "",
|
||||
ulong ProducerFenceValue = 0);
|
||||
|
|
|
|||
Loading…
Reference in a new issue