From 69a6269e01a5344e8441befb9ad78cd24e38c4c3 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 27 May 2026 17:11:27 +0100 Subject: [PATCH] Prove direct shared camera texture import --- state/evidence.jsonl | 1 + state/map.yaml | 5 +- .../D3D12SharedTextureInteropTests.cs | 88 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/Aquarium.Engine.Tests/D3D12SharedTextureInteropTests.cs diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 9a6ee8d..5921c28 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -258,3 +258,4 @@ {"lesson":"Shared Texture2D field resources now resolve through the D3D12 field resource registry, not only local PNG assets. D3D12FieldTexture2D can open native shared handles as shader-readable Texture2D resources, and the texture format parser accepts Mimir video descriptor names including Bgra8, Gray8/R8, Rg8, Nv12, and LeapStereoIr. This lets camera FieldEvidence resources become real GPU resources before any selected camera/feature lowering exists. Fensalir build and Aquarium.Engine.Tests passed.","timestamp":"2026-05-27T12:55:00+01:00","files":["src/Aquarium.Engine/Render/D3D12FieldTexture2D.cs","src/Aquarium.Engine/Render/D3D12FieldTextureFormat.cs","src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs","tests/Aquarium.Engine.Tests/D3D12FieldTextureFormatTests.cs","state/map.yaml"],"source":"Mimir camera observation bridge follow-up","topic":"shared-texture2d-field-resource-resolver"} {"lesson":"Fensalir now exposes engine-owned Texture2D leases through AquariumRuntimeServices. Client runtimes that implement IAquariumRuntimeServicesReceiver receive a field resource broker, can request keyed D3D12 Texture2D leases with shared texture and producer-fence handles, and can commit producer fence values. D3D12Renderer waits on committed producer fences before FieldEvidence resource resolution and shader sampling. This makes Fensalir-owned camera image buffers the preferred hot path while imported shared handles remain an edge case. Fensalir build and Aquarium.Engine.Tests passed; Mimir's fensalir-texture-lease smoke passed.","timestamp":"2026-05-27T13:45:00+01:00","files":["src/Aquarium.Engine.Contracts/AquariumRuntimeServices.cs","src/Aquarium.Engine.Contracts/Render/AquariumFieldResourceBroker.cs","src/Aquarium.Engine/AquariumHost.cs","src/Aquarium.Engine/Render/IAquariumRenderer.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","state/map.yaml"],"source":"Mimir/Fensalir camera buffer ownership correction","topic":"fensalir-owned-texture2d-leases"} {"lesson":"Fensalir's field resource broker now has an explicit Texture2D upload lane for raw single-plane CPU-origin camera frames. D3D12Renderer copies row-aligned upload data into the leased texture on the direct queue, supports corrected Mimir raw formats including LeapStereoIr as R8G8 and YUY2, rejects CPU NV12 until a real planar copy path exists, and leaves copy accounting to the client producer. This is the fallback for KS/WinUSB-style raw frame paths; GPU/direct producers should write shared texture leases directly. Fensalir build and Aquarium.Engine.Tests passed; Mimir's fensalir-texture-lease smoke passed with copies=1 for the CPU-origin fake driver.","timestamp":"2026-05-27T14:35:00+01:00","files":["src/Aquarium.Engine.Contracts/Render/AquariumFieldResourceBroker.cs","src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs","src/Aquarium.Engine/Render/D3D12FieldTextureFormat.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","tests/Aquarium.Engine.Tests/D3D12FieldTextureFormatTests.cs","state/map.yaml"],"source":"device-direct camera ingestion follow-up","topic":"texture2d-raw-frame-upload-fallback"} +{"lesson":"The direct Mimir MF camera texture path is viable at the Fensalir resolver layer. A D3D11 BGRA Texture2D created with SharedNTHandle|SharedKeyedMutex can produce an IDXGIResource1 NT shared handle, and D3D12FieldResourceRegistry resolves that handle as a shader-readable Texture2D through the existing SharedGpu declaration path. This proves handle import; live camera correctness still needs explicit synchronization/lifetime handling around the producer's keyed mutex or a chosen fence handoff. Fensalir build and Aquarium.Engine.Tests passed.","timestamp":"2026-05-27T17:09:04+01:00","files":["tests/Aquarium.Engine.Tests/D3D12SharedTextureInteropTests.cs","state/map.yaml"],"source":"Mimir MF GPU direct shared texture proof","topic":"d3d11-d3d12-shared-texture-direct-import"} diff --git a/state/map.yaml b/state/map.yaml index f541503..e021cde 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -93,7 +93,10 @@ next_actions: D3D12 upload, SRV binding, and TubeField ramp lookup by resource key; shared GPU Texture2D declarations with native D3D12 handles resolve through the same field resource registry, using Mimir video format names such as - Bgra8, Gray8, Rg8, Nv12, and LeapStereoIr as DXGI format input. Fensalir + Bgra8, Gray8, Rg8, Nv12, and LeapStereoIr as DXGI format input. The direct + Mimir MF shape is now proven at resolver level: a D3D11 NT shared + keyed-mutex BGRA texture can be opened by Fensalir's D3D12 registry as a + shader-readable Texture2D. Fensalir also exposes the first engine-owned texture lease broker through `AquariumRuntimeServices`: client runtimes can request keyed D3D12 Texture2D leases, receive shared texture and producer-fence handles, then diff --git a/tests/Aquarium.Engine.Tests/D3D12SharedTextureInteropTests.cs b/tests/Aquarium.Engine.Tests/D3D12SharedTextureInteropTests.cs new file mode 100644 index 0000000..dd301af --- /dev/null +++ b/tests/Aquarium.Engine.Tests/D3D12SharedTextureInteropTests.cs @@ -0,0 +1,88 @@ +using System.Runtime.InteropServices; +using Aquarium.Engine.Render; +using Vortice.Direct3D; +using Vortice.Direct3D11; +using Vortice.Direct3D12; +using Vortice.DXGI; +using D3D11 = Vortice.Direct3D11.D3D11; +using D3D12 = Vortice.Direct3D12.D3D12; +using D3D11BindFlags = Vortice.Direct3D11.BindFlags; +using D3D11CpuAccessFlags = Vortice.Direct3D11.CpuAccessFlags; +using D3D11DeviceCreationFlags = Vortice.Direct3D11.DeviceCreationFlags; +using D3D11ResourceOptionFlags = Vortice.Direct3D11.ResourceOptionFlags; +using D3D11ResourceUsage = Vortice.Direct3D11.ResourceUsage; +using DxgiSharedResourceFlags = Vortice.DXGI.SharedResourceFlags; + +namespace Aquarium.Engine.Tests; + +public sealed class D3D12SharedTextureInteropTests +{ + [Fact] + public void RegistryResolvesD3D11NtSharedBgraTextureAsD3D12Texture2D() + { + using var d3d11 = CreateD3D11Device(); + using var d3d12 = D3D12.D3D12CreateDevice(IntPtr.Zero, FeatureLevel.Level_11_0); + var description = new Texture2DDescription( + Format.B8G8R8A8_UNorm, + width: 16, + height: 8, + arraySize: 1, + mipLevels: 1, + bindFlags: D3D11BindFlags.ShaderResource | D3D11BindFlags.RenderTarget, + usage: D3D11ResourceUsage.Default, + cpuAccessFlags: D3D11CpuAccessFlags.None, + sampleCount: 1, + sampleQuality: 0, + miscFlags: D3D11ResourceOptionFlags.SharedNTHandle | D3D11ResourceOptionFlags.SharedKeyedMutex); + using var texture = d3d11.CreateTexture2D(description); + using var sharedResource = texture.QueryInterface(); + var sharedHandle = sharedResource.CreateSharedHandle(null, DxgiSharedResourceFlags.Read | DxgiSharedResourceFlags.Write, null); + + try + { + var declaration = new AquariumFieldResourceDeclaration( + ResourceKey: "test:shared-d3d11-texture:bgra", + Kind: AquariumFieldResourceKind.Texture2D, + Residency: AquariumFieldResourceResidency.SharedGpu, + Access: AquariumFieldShaderAccess.ShaderResource, + Format: "Bgra8", + Width: 16, + Height: 8, + DepthOrCount: 1, + StrideBytes: 64, + ValidFromNs: 0, + ValidUntilNs: 0, + Version: 1, + NativeHandle: sharedHandle, + NativeHandleKind: "shared-d3d11-texture"); + + using var registry = new D3D12FieldResourceRegistry(); + var resources = new D3D12ResourceRegistry(); + var stats = registry.Resolve(d3d12, resources, [declaration]); + + Assert.Equal(1, stats.Declared); + Assert.Equal(1, stats.Texture2D); + Assert.Equal(1, stats.Resolved); + Assert.Equal(0, stats.Unsupported); + Assert.True(registry.TryGetTexture2D(declaration.ResourceKey, out var resolved)); + Assert.Equal(16, resolved.Width); + Assert.Equal(8, resolved.Height); + Assert.Equal(Format.B8G8R8A8_UNorm, resolved.Format); + } + finally + { + CloseHandle(sharedHandle); + } + } + + private static ID3D11Device CreateD3D11Device() + { + return D3D11.D3D11CreateDevice( + DriverType.Hardware, + D3D11DeviceCreationFlags.BgraSupport, + FeatureLevel.Level_11_0); + } + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool CloseHandle(IntPtr handle); +}