From bd2a838a42fcc1e168fe495f6e36f380aaf760b4 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 27 May 2026 17:13:08 +0100 Subject: [PATCH] Reuse shared texture imports across frame versions --- .../Render/D3D12FieldResourceRegistry.cs | 10 +++++++++- state/evidence.jsonl | 1 + state/map.yaml | 4 +++- .../D3D12SharedTextureInteropTests.cs | 8 ++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs b/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs index 276902c..2196337 100644 --- a/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs +++ b/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs @@ -565,12 +565,20 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable return false; } + if (!D3D12FieldTextureFormat.TryFormat(declaration.Format, out var format)) + { + return false; + } + if (textures.TryGetValue(declaration.ResourceKey, out var existing) && existing.Kind == AquariumFieldResourceKind.Texture2D && - existing.Version == declaration.Version && existing.NativeHandle == declaration.NativeHandle && + existing.Texture.Width == declaration.Width && + existing.Texture.Height == declaration.Height && + existing.Texture.Format == format && string.Equals(existing.NativeHandleKind, declaration.NativeHandleKind, StringComparison.Ordinal)) { + textures[declaration.ResourceKey] = existing with { Version = declaration.Version }; return true; } diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 5921c28..7b3028e 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -259,3 +259,4 @@ {"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"} +{"lesson":"Shared Texture2D declaration Version is frame/content freshness, not resource identity. D3D12FieldResourceRegistry now reuses an already opened shared Texture2D when the handle, handle kind, dimensions, and DXGI format are unchanged, updating the stored version instead of disposing and reopening the same live camera texture every frame. Aquarium.Engine.Tests now proves second-version D3D11 NT shared BGRA imports reuse the same resolved texture object. Fensalir build and Aquarium.Engine.Tests passed.","timestamp":"2026-05-27T17:12:42+01:00","files":["src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs","tests/Aquarium.Engine.Tests/D3D12SharedTextureInteropTests.cs","state/map.yaml"],"source":"direct MF GPU texture import freshness follow-up","topic":"shared-texture2d-version-is-freshness"} diff --git a/state/map.yaml b/state/map.yaml index e021cde..d59d9c4 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -96,7 +96,9 @@ next_actions: 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 + shader-readable Texture2D. Shared Texture2D version is content freshness, + not import identity; the registry reuses an opened shared texture when + handle, handle kind, dimensions, and format are unchanged. 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 index dd301af..3095c3f 100644 --- a/tests/Aquarium.Engine.Tests/D3D12SharedTextureInteropTests.cs +++ b/tests/Aquarium.Engine.Tests/D3D12SharedTextureInteropTests.cs @@ -68,6 +68,14 @@ public sealed class D3D12SharedTextureInteropTests Assert.Equal(16, resolved.Width); Assert.Equal(8, resolved.Height); Assert.Equal(Format.B8G8R8A8_UNorm, resolved.Format); + + var nextFrameStats = registry.Resolve(d3d12, resources, [declaration with { Version = 2 }]); + + Assert.Equal(1, nextFrameStats.Resolved); + Assert.Equal(0, nextFrameStats.Unsupported); + Assert.Equal(0, nextFrameStats.StaleRemoved); + Assert.True(registry.TryGetTexture2D(declaration.ResourceKey, out var nextFrameResolved)); + Assert.Same(resolved, nextFrameResolved); } finally {