From 72738e667621db1307b90eaffb3f5dcaf30927f2 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 27 May 2026 23:56:42 +0100 Subject: [PATCH] Observe program output producer fence --- docs/eve-program-output.md | 16 ++--- docs/implementation-plan.md | 13 ++-- docs/obs-stem-plugin.md | 7 ++- .../mimir_obs_program_texture_source.cpp | 61 +++++++++++++++++++ notes/current-system-map.md | 9 ++- scripts/start-eve-dashboard-stream.ps1 | 3 + scripts/start-eve-program-output.ps1 | 3 + .../MimirEveProgramOutputConfiguration.cs | 4 +- state/evidence.jsonl | 1 + state/map.yaml | 8 ++- 10 files changed, 106 insertions(+), 19 deletions(-) diff --git a/docs/eve-program-output.md b/docs/eve-program-output.md index 3f0f05e..487a805 100644 --- a/docs/eve-program-output.md +++ b/docs/eve-program-output.md @@ -23,16 +23,18 @@ Set these before launching the Mimir app: ```powershell $env:FENSALIR_PROGRAM_OUTPUT_D3D12 = "1" $env:FENSALIR_PROGRAM_OUTPUT_NAME = "Global\MimirFensalirProgramTexture" +$env:FENSALIR_PROGRAM_OUTPUT_FENCE_NAME = "Global\MimirFensalirProgramFence" dotnet run --project .\src\Mimir.App\Mimir.App.csproj ``` -Fensalir creates a shareable D3D12 texture with that name and copies the finished -backbuffer into it during the present pass. `Mimir.EveRelay` opens that named -texture, reads the completed program frame, feeds a low-latency H.264 encoder, -and sends complete Annex-B access units to EveCanvas over the existing -`/stream` WebSocket. EveCanvas treats JPEG frames as the old VoidBot dashboard -fallback only; Mimir dashboard streaming is `h264-annexb` and displays through a -native `AVSampleBufferDisplayLayer` so the iPad owns hardware decode/composite. +Fensalir creates a shareable D3D12 texture with that name, publishes a named +producer fence, and copies the finished backbuffer into the texture during the +present pass. `Mimir.EveRelay` opens that named texture, reads the completed +program frame, feeds a low-latency H.264 encoder, and sends complete Annex-B +access units to EveCanvas over the existing `/stream` WebSocket. EveCanvas +treats JPEG frames as the old VoidBot dashboard fallback only; Mimir dashboard +streaming is `h264-annexb` and displays through a native +`AVSampleBufferDisplayLayer` so the iPad owns hardware decode/composite. `scripts/start-eve-dashboard-stream.ps1` starts the Mimir/Fensalir app with the shared-texture output enabled, starts the H.264 relay, verifies the `eve` SSH diff --git a/docs/implementation-plan.md b/docs/implementation-plan.md index abe43fd..df82327 100644 --- a/docs/implementation-plan.md +++ b/docs/implementation-plan.md @@ -163,11 +163,14 @@ a named invariant that the native runtime cannot protect yet. `Mimir Audio Stem`, which reads one named stem and submits it to OBS as an audio source, and `Mimir Program Texture`, which opens Fensalir's named D3D12 program-output texture and feeds OBS through a GPU-to-GPU D3D12-to-D3D11 - bridge texture. This avoids CPU readback and removes Spout2 as a required - program-video dependency, while preserving the explicit boundary cost that - libobs is D3D11 on Windows. `scripts/build-obs-stem-plugin.ps1` stages the - upstream OBS plugin template SDK under `artifacts/obs-sdk/` and builds the - plugin DLL locally. + bridge texture. Fensalir also publishes a named D3D12 producer fence, and the + OBS source opens it before copying so the consumer is not reading blindly. + This avoids CPU readback and removes Spout2 as a required program-video + dependency, while preserving the explicit boundary cost that libobs is D3D11 + on Windows. A later publication ring is still required to make + producer/consumer overwrite races structurally impossible. + `scripts/build-obs-stem-plugin.ps1` stages the upstream OBS plugin template + SDK under `artifacts/obs-sdk/` and builds the plugin DLL locally. - `MimirRuntime` no longer submits the legacy direct `AquariumSplineFrame` spectrum dashboard. Live spectrum visualization authority is the `AquariumFieldEvidenceFrame`: Mimir declares the rolling resource, emits a diff --git a/docs/obs-stem-plugin.md b/docs/obs-stem-plugin.md index aac1f8a..b0d8725 100644 --- a/docs/obs-stem-plugin.md +++ b/docs/obs-stem-plugin.md @@ -29,6 +29,7 @@ Texture` video source. Set: ```text Shared D3D12 texture name: Global\MimirFensalirProgramTexture +Shared D3D12 fence name: Global\MimirFensalirProgramFence ``` OBS/libobs is D3D11 internally on Windows, so this source opens the named D3D12 @@ -36,7 +37,11 @@ texture, creates an OBS-readable legacy shared D3D11 texture, opens that same bridge texture from D3D12, and performs a GPU-to-GPU copy during OBS render before drawing the source. This avoids CPU readback and does not require Spout2. It is not pure zero-copy yet; the remaining copy is the -D3D12-to-libobs-D3D11 boundary. +D3D12-to-libobs-D3D11 boundary. Fensalir also publishes a named D3D12 fence; +the OBS source opens it and observes the producer completion value before +copying. This prevents blind read-before-producer-completion behavior. A later +multi-texture publication ring is still the clean way to make overwrite races +structurally impossible when producer and consumer cadence diverge. For a visible interop witness while the real Mimir scene is sparse, launch Mimir with: diff --git a/native/obs_stem_source/mimir_obs_program_texture_source.cpp b/native/obs_stem_source/mimir_obs_program_texture_source.cpp index afc510c..1a3c575 100644 --- a/native/obs_stem_source/mimir_obs_program_texture_source.cpp +++ b/native/obs_stem_source/mimir_obs_program_texture_source.cpp @@ -19,9 +19,11 @@ namespace { struct MimirProgramTextureSource { obs_source_t *source = nullptr; std::string texture_name = "Global\\MimirFensalirProgramTexture"; + std::string fence_name = "Global\\MimirFensalirProgramFence"; uint32_t width = 0; uint32_t height = 0; uint64_t fence_value = 0; + uint64_t last_producer_fence_value = 0; uint64_t next_retry_ns = 0; uint64_t next_failure_log_ns = 0; HANDLE bridge_shared_handle = nullptr; @@ -32,6 +34,7 @@ struct MimirProgramTextureSource { ComPtr command_allocator; ComPtr command_list; ComPtr fence; + ComPtr producer_fence; ComPtr source_texture; ComPtr bridge_texture_d3d12; ComPtr d3d11_device; @@ -91,6 +94,7 @@ static void release_bridge(MimirProgramTextureSource *ctx) ctx->bridge_texture_d3d11.Reset(); ctx->source_texture.Reset(); ctx->fence.Reset(); + ctx->producer_fence.Reset(); ctx->command_list.Reset(); ctx->command_allocator.Reset(); ctx->command_queue.Reset(); @@ -100,6 +104,7 @@ static void release_bridge(MimirProgramTextureSource *ctx) ctx->width = 0; ctx->height = 0; ctx->fence_value = 0; + ctx->last_producer_fence_value = 0; if (ctx->bridge_shared_handle) { CloseHandle(ctx->bridge_shared_handle); @@ -237,6 +242,53 @@ static bool open_source_texture(MimirProgramTextureSource *ctx) return ctx->width > 0 && ctx->height > 0; } +static void open_producer_fence(MimirProgramTextureSource *ctx) +{ + const auto wide_name = utf8_to_wide(ctx->fence_name); + if (wide_name.empty()) { + return; + } + + HANDLE fence_handle = nullptr; + HRESULT hr = ctx->d3d12_device->OpenSharedHandleByName(wide_name.c_str(), GENERIC_ALL, &fence_handle); + if (FAILED(hr)) { + log_retry_failure(ctx, "OpenSharedHandleByName producer fence failed", hr); + return; + } + + hr = ctx->d3d12_device->OpenSharedHandle(fence_handle, IID_PPV_ARGS(&ctx->producer_fence)); + CloseHandle(fence_handle); + if (FAILED(hr)) { + log_retry_failure(ctx, "OpenSharedHandle producer fence failed", hr); + return; + } + + ctx->last_producer_fence_value = ctx->producer_fence->GetCompletedValue(); + blog(LOG_INFO, + "Mimir OBS program texture producer fence opened %s value=%llu", + ctx->fence_name.c_str(), + static_cast(ctx->last_producer_fence_value)); +} + +static bool wait_for_producer(MimirProgramTextureSource *ctx) +{ + if (!ctx->producer_fence) { + return true; + } + + const uint64_t completed = ctx->producer_fence->GetCompletedValue(); + if (completed == UINT64_MAX) { + blog(LOG_WARNING, "Mimir OBS program texture: producer fence reported device removal"); + return false; + } + + if (completed > ctx->last_producer_fence_value) { + ctx->last_producer_fence_value = completed; + } + + return true; +} + static bool create_obs_bridge_texture(MimirProgramTextureSource *ctx) { D3D11_TEXTURE2D_DESC desc = {}; @@ -303,6 +355,8 @@ static bool ensure_bridge(MimirProgramTextureSource *ctx) return false; } + open_producer_fence(ctx); + blog(LOG_INFO, "Mimir OBS program texture source opened %s (%ux%u)", ctx->texture_name.c_str(), ctx->width, @@ -317,6 +371,10 @@ static bool copy_source_to_bridge(MimirProgramTextureSource *ctx) return false; } + if (!wait_for_producer(ctx)) { + return false; + } + hr = ctx->command_list->Reset(ctx->command_allocator.Get(), nullptr); if (FAILED(hr)) { return false; @@ -359,6 +417,7 @@ static void mimir_program_texture_update(void *data, obs_data_t *settings) { auto *ctx = static_cast(data); ctx->texture_name = obs_string(settings, "texture_name", "Global\\MimirFensalirProgramTexture"); + ctx->fence_name = obs_string(settings, "fence_name", "Global\\MimirFensalirProgramFence"); release_bridge(ctx); } @@ -394,12 +453,14 @@ static uint32_t mimir_program_texture_height(void *data) static void mimir_program_texture_defaults(obs_data_t *settings) { obs_data_set_default_string(settings, "texture_name", "Global\\MimirFensalirProgramTexture"); + obs_data_set_default_string(settings, "fence_name", "Global\\MimirFensalirProgramFence"); } static obs_properties_t *mimir_program_texture_properties(void *) { obs_properties_t *props = obs_properties_create(); obs_properties_add_text(props, "texture_name", "Shared D3D12 texture name", OBS_TEXT_DEFAULT); + obs_properties_add_text(props, "fence_name", "Shared D3D12 fence name", OBS_TEXT_DEFAULT); return props; } diff --git a/notes/current-system-map.md b/notes/current-system-map.md index 59ca6d6..0bd12b4 100644 --- a/notes/current-system-map.md +++ b/notes/current-system-map.md @@ -325,9 +325,12 @@ and submits it through libobs audio, while `Mimir Program Texture` opens Fensalir's named shared D3D12 program-output texture and draws it through an OBS-readable shared D3D11 bridge texture. The video path is GPU-to-GPU and does not require Spout2 or CPU readback; the remaining copy is the explicit D3D12 to -libobs-D3D11 boundary. `scripts/build-obs-stem-plugin.ps1` stages the upstream -OBS plugin-template SDK under `artifacts/obs-sdk/` and builds the plugin DLL -locally. +libobs-D3D11 boundary. Fensalir publishes `Global\MimirFensalirProgramFence`, +and the OBS source opens that producer fence before copying. This prevents +blind read-before-producer-completion; a later publication ring is still needed +to make overwrite races structurally impossible. `scripts/build-obs-stem-plugin.ps1` +stages the upstream OBS plugin-template SDK under `artifacts/obs-sdk/` and +builds the plugin DLL locally. ## Visual Fusion diff --git a/scripts/start-eve-dashboard-stream.ps1 b/scripts/start-eve-dashboard-stream.ps1 index 14c32e6..139197c 100644 --- a/scripts/start-eve-dashboard-stream.ps1 +++ b/scripts/start-eve-dashboard-stream.ps1 @@ -1,6 +1,7 @@ param( [string] $Target = "eve", [string] $SharedTextureName = "Global\MimirFensalirProgramTexture", + [string] $SharedFenceName = "Global\MimirFensalirProgramFence", [string] $Configuration = "Debug", [int] $Width = 1620, [int] $Height = 2160, @@ -35,6 +36,7 @@ ssh $Target "uname -a >/tmp/mimir-eve-dashboard-check.txt && cat /tmp/mimir-eve- $env:FENSALIR_PROGRAM_OUTPUT_D3D12 = "1" $env:FENSALIR_PROGRAM_OUTPUT_NAME = $SharedTextureName +$env:FENSALIR_PROGRAM_OUTPUT_FENCE_NAME = $SharedFenceName $mimirProject = Join-Path $repoRoot "src\Mimir.App\Mimir.App.csproj" $mimirArgs = @( @@ -47,6 +49,7 @@ $mimirArgs = @( ) + $AppArguments Write-Host "Starting Mimir/Fensalir program output: $SharedTextureName" +Write-Host "Program output fence: $SharedFenceName" $mimirProcess = Start-Process -FilePath "dotnet" ` -ArgumentList $mimirArgs ` -WorkingDirectory $repoRoot ` diff --git a/scripts/start-eve-program-output.ps1 b/scripts/start-eve-program-output.ps1 index 60e1737..188dd24 100644 --- a/scripts/start-eve-program-output.ps1 +++ b/scripts/start-eve-program-output.ps1 @@ -1,6 +1,7 @@ param( [string] $Target = "eve", [string] $SharedTextureName = "Global\MimirFensalirProgramTexture", + [string] $SharedFenceName = "Global\MimirFensalirProgramFence", [string] $Configuration = "Debug", [string] $LogPath = "logs\eve-program-output-fensalir.log", [string[]] $AppArguments = @() @@ -21,6 +22,7 @@ ssh $Target "uname -a >/tmp/mimir-eve-program-output-check.txt && cat /tmp/mimir $env:FENSALIR_PROGRAM_OUTPUT_D3D12 = "1" $env:FENSALIR_PROGRAM_OUTPUT_NAME = $SharedTextureName +$env:FENSALIR_PROGRAM_OUTPUT_FENCE_NAME = $SharedFenceName $projectPath = Join-Path $repoRoot "src\Mimir.App\Mimir.App.csproj" $dotnetArgs = @( @@ -33,6 +35,7 @@ $dotnetArgs = @( ) + $AppArguments Write-Host "Starting Mimir/Fensalir with shared D3D12 output: $SharedTextureName" +Write-Host "Program output fence: $SharedFenceName" Write-Host "Log: $absoluteLogPath" $process = Start-Process -FilePath "dotnet" ` -ArgumentList $dotnetArgs ` diff --git a/src/Mimir.Runtime/Synchronization/MimirEveProgramOutputConfiguration.cs b/src/Mimir.Runtime/Synchronization/MimirEveProgramOutputConfiguration.cs index d85e43f..0daab81 100644 --- a/src/Mimir.Runtime/Synchronization/MimirEveProgramOutputConfiguration.cs +++ b/src/Mimir.Runtime/Synchronization/MimirEveProgramOutputConfiguration.cs @@ -25,6 +25,7 @@ public sealed record MimirEveProgramOutputConfiguration( public static class MimirEveProgramOutputConfigurations { public const string DefaultSharedTextureName = "Global\\MimirFensalirProgramTexture"; + public const string DefaultSharedFenceName = "Global\\MimirFensalirProgramFence"; public static MimirEveProgramOutputConfiguration NativeSharedTexture { get; } = new( "eve-native-d3d12-hardware-stream", @@ -41,7 +42,8 @@ public static class MimirEveProgramOutputConfigurations RequiredEnvironment: [ "FENSALIR_PROGRAM_OUTPUT_D3D12=1", - $"FENSALIR_PROGRAM_OUTPUT_NAME={DefaultSharedTextureName}" + $"FENSALIR_PROGRAM_OUTPUT_NAME={DefaultSharedTextureName}", + $"FENSALIR_PROGRAM_OUTPUT_FENCE_NAME={DefaultSharedFenceName}" ], "EVE owns decode/composite only. It does not own layout, DOM, timing authority, or Chromium compatibility."); diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 3f9114c..b120c71 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -196,3 +196,4 @@ {"ts": "2026-05-27T21:30:23+01:00", "type": "verification", "status": "ok", "note": "The OBS plugin install path now uses OBS's plugin package layout under C:\\\\ProgramData\\\\obs-studio\\\\plugins by default instead of requiring writes to C:\\\\Program Files. scripts/install-obs-mimir-plugin.ps1 -SkipBuild installed mimir_obs_stem_source.dll to C:\\\\ProgramData\\\\obs-studio\\\\plugins\\\\mimir_obs_stem_source\\\\bin\\\\64bit, registering Mimir Program Texture and Mimir Audio Stem for the next OBS launch."} {"ts": "2026-05-27T21:31:36+01:00", "type": "verification", "status": "ok", "note": "Installed Mimir OBS plugin was loaded by local OBS Studio 32.1.2 from C:\\\\ProgramData\\\\obs-studio\\\\plugins\\\\mimir_obs_stem_source\\\\bin\\\\64bit. A hidden launch/kill smoke produced OBS log line 'Mimir OBS sources loaded' and listed mimir_obs_stem_source.dll. OBS also warned that en-US text was missing for the module; locale packaging is cosmetic but should be cleaned before distribution. Live video rendering still needs an OBS scene source while Fensalir publishes Global\\\\MimirFensalirProgramTexture."} {"ts": "2026-05-27T23:34:13+01:00", "type": "verification", "status": "ok", "note": "Live OBS program texture proof succeeded. With Mimir/Fensalir publishing Global\\\\MimirFensalirProgramTexture at 1264x681 and MIMIR_OBS_PROOF_VISUAL=1, OBS 32.1.2 loaded mimir_obs_stem_source.dll, the Mimir Program Texture source opened the shared texture, and the user visually confirmed the Fensalir debug/proof scene in OBS. The working bridge uses a legacy shared D3D11 texture because libobs accepts that handle path; D3D12 opens the same bridge texture and copies GPU-to-GPU from the Fensalir D3D12 program texture. This removes Spout2 and CPU readback from the program-video proof path while retaining the explicit D3D12-to-D3D11 boundary copy."} +{"ts": "2026-05-27T23:55:25+01:00", "type": "verification", "status": "ok", "note": "OBS program texture bridge now opens Fensalir's named D3D12 producer fence Global\\\\MimirFensalirProgramFence and the shared program texture Global\\\\MimirFensalirProgramTexture; live OBS 32.1.2 log showed producer fence value 1061 and texture 1264x681 while the proof visual appeared in OBS."} diff --git a/state/map.yaml b/state/map.yaml index 516525c..52618c9 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -200,9 +200,13 @@ accepted_design: plugin bundle. It registers `Mimir Audio Stem`, which reads one named stem from that map and submits it as libobs audio, and `Mimir Program Texture`, which opens Fensalir's named shared D3D12 program-output texture and draws - it through an OBS-readable shared D3D11 bridge texture. The video path is + it through an OBS-readable shared D3D11 bridge texture. Fensalir also + publishes `Global\MimirFensalirProgramFence`; the OBS source opens that + producer fence and observes completion before copying. The video path is GPU-to-GPU and does not require Spout2 or CPU readback; the remaining copy - is the explicit D3D12 to libobs-D3D11 boundary. + is the explicit D3D12 to libobs-D3D11 boundary. A later publication ring is + still required to make producer/consumer overwrite races structurally + impossible. `scripts/build-obs-stem-plugin.ps1` stages the upstream OBS plugin template SDK under `artifacts/obs-sdk/` and builds the plugin DLL locally. The synthetic