diff --git a/docs/eve-program-output.md b/docs/eve-program-output.md index 487a805..9c30413 100644 --- a/docs/eve-program-output.md +++ b/docs/eve-program-output.md @@ -24,6 +24,7 @@ Set these before launching the Mimir app: $env:FENSALIR_PROGRAM_OUTPUT_D3D12 = "1" $env:FENSALIR_PROGRAM_OUTPUT_NAME = "Global\MimirFensalirProgramTexture" $env:FENSALIR_PROGRAM_OUTPUT_FENCE_NAME = "Global\MimirFensalirProgramFence" +$env:FENSALIR_PROGRAM_OUTPUT_RING_COUNT = "1" dotnet run --project .\src\Mimir.App\Mimir.App.csproj ``` @@ -36,6 +37,10 @@ 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. +The EVE relay currently consumes the single-texture contract. Keep +`FENSALIR_PROGRAM_OUTPUT_RING_COUNT=1` for EVE until the relay has the same +slot-selection contract as the OBS program texture source. + `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 target, opens a reverse SSH tunnel from Eve's `127.0.0.1:8792` back to the diff --git a/docs/implementation-plan.md b/docs/implementation-plan.md index df82327..1054821 100644 --- a/docs/implementation-plan.md +++ b/docs/implementation-plan.md @@ -167,8 +167,11 @@ a named invariant that the native runtime cannot protect yet. 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. + on Windows. Fensalir and the OBS source can optionally agree on a bounded + publication ring using `FENSALIR_PROGRAM_OUTPUT_RING_COUNT`; OBS selects the + latest completed slot from the producer fence value. A later consumer + acknowledgement fence 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` diff --git a/docs/obs-stem-plugin.md b/docs/obs-stem-plugin.md index b0d8725..d3edc99 100644 --- a/docs/obs-stem-plugin.md +++ b/docs/obs-stem-plugin.md @@ -30,6 +30,7 @@ Texture` video source. Set: ```text Shared D3D12 texture name: Global\MimirFensalirProgramTexture Shared D3D12 fence name: Global\MimirFensalirProgramFence +Texture ring slots: 1 ``` OBS/libobs is D3D11 internally on Windows, so this source opens the named D3D12 @@ -40,9 +41,22 @@ Spout2. It is not pure zero-copy yet; the remaining copy is the 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 +consumer acknowledgement fence is still required to make overwrite races structurally impossible when producer and consumer cadence diverge. +For OBS-only stress testing, Fensalir and the OBS source can agree on a bounded +texture ring: + +```text +FENSALIR_PROGRAM_OUTPUT_RING_COUNT=3 +Texture ring slots: 3 +``` + +With a ring count above one, Fensalir publishes textures named +`Global\MimirFensalirProgramTexture.0`, `.1`, and so on. The OBS source uses the +producer fence value to copy the latest completed slot. Leave the ring count at +`1` for EVE until the EVE relay speaks the same ring contract. + 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 1a3c575..6a3a69b 100644 --- a/native/obs_stem_source/mimir_obs_program_texture_source.cpp +++ b/native/obs_stem_source/mimir_obs_program_texture_source.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -20,6 +21,7 @@ struct MimirProgramTextureSource { obs_source_t *source = nullptr; std::string texture_name = "Global\\MimirFensalirProgramTexture"; std::string fence_name = "Global\\MimirFensalirProgramFence"; + uint32_t ring_count = 1; uint32_t width = 0; uint32_t height = 0; uint64_t fence_value = 0; @@ -35,7 +37,7 @@ struct MimirProgramTextureSource { ComPtr command_list; ComPtr fence; ComPtr producer_fence; - ComPtr source_texture; + std::vector> source_textures; ComPtr bridge_texture_d3d12; ComPtr d3d11_device; ComPtr d3d11_context; @@ -83,6 +85,28 @@ static std::string obs_string(obs_data_t *settings, const char *key, const char return value && *value ? std::string(value) : std::string(fallback); } +static uint32_t clamp_ring_count(long long value) +{ + if (value < 1) { + return 1; + } + + if (value > 4) { + return 4; + } + + return static_cast(value); +} + +static std::string texture_name_for_slot(const MimirProgramTextureSource *ctx, uint32_t slot) +{ + if (ctx->ring_count <= 1) { + return ctx->texture_name; + } + + return ctx->texture_name + "." + std::to_string(slot); +} + static void release_bridge(MimirProgramTextureSource *ctx) { if (ctx->obs_texture) { @@ -92,7 +116,7 @@ static void release_bridge(MimirProgramTextureSource *ctx) ctx->bridge_texture_d3d12.Reset(); ctx->bridge_texture_d3d11.Reset(); - ctx->source_texture.Reset(); + ctx->source_textures.clear(); ctx->fence.Reset(); ctx->producer_fence.Reset(); ctx->command_list.Reset(); @@ -211,35 +235,56 @@ static bool create_devices(MimirProgramTextureSource *ctx) static bool open_source_texture(MimirProgramTextureSource *ctx) { - const auto wide_name = utf8_to_wide(ctx->texture_name); - if (wide_name.empty()) { - log_retry_failure(ctx, "shared texture name is empty or invalid UTF-8"); - return false; + ctx->source_textures.clear(); + ctx->source_textures.reserve(ctx->ring_count); + for (uint32_t slot = 0; slot < ctx->ring_count; ++slot) { + const auto slot_name = texture_name_for_slot(ctx, slot); + const auto wide_name = utf8_to_wide(slot_name); + if (wide_name.empty()) { + log_retry_failure(ctx, "shared texture name is empty or invalid UTF-8"); + return false; + } + + HANDLE source_handle = nullptr; + HRESULT hr = ctx->d3d12_device->OpenSharedHandleByName(wide_name.c_str(), GENERIC_ALL, &source_handle); + if (FAILED(hr)) { + log_retry_failure(ctx, "OpenSharedHandleByName failed", hr); + return false; + } + + ComPtr source_texture; + hr = ctx->d3d12_device->OpenSharedHandle(source_handle, IID_PPV_ARGS(&source_texture)); + CloseHandle(source_handle); + if (FAILED(hr)) { + log_retry_failure(ctx, "OpenSharedHandle failed", hr); + return false; + } + + const D3D12_RESOURCE_DESC desc = source_texture->GetDesc(); + if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || desc.Format != DXGI_FORMAT_B8G8R8A8_UNORM) { + log_retry_failure(ctx, "expected BGRA8 D3D12 Texture2D"); + return false; + } + + const uint32_t slot_width = static_cast(desc.Width); + const uint32_t slot_height = desc.Height; + if (slot_width == 0 || slot_height == 0) { + log_retry_failure(ctx, "shared texture dimensions are empty"); + return false; + } + + if (slot == 0) { + ctx->width = slot_width; + ctx->height = slot_height; + } else if (slot_width != ctx->width || slot_height != ctx->height) { + log_retry_failure(ctx, "program output ring texture dimensions do not match"); + return false; + } + + ctx->source_textures.push_back(std::move(source_texture)); } - HANDLE source_handle = nullptr; - HRESULT hr = ctx->d3d12_device->OpenSharedHandleByName(wide_name.c_str(), GENERIC_ALL, &source_handle); - if (FAILED(hr)) { - log_retry_failure(ctx, "OpenSharedHandleByName failed", hr); - return false; - } - - hr = ctx->d3d12_device->OpenSharedHandle(source_handle, IID_PPV_ARGS(&ctx->source_texture)); - CloseHandle(source_handle); - if (FAILED(hr)) { - log_retry_failure(ctx, "OpenSharedHandle failed", hr); - return false; - } - - const D3D12_RESOURCE_DESC desc = ctx->source_texture->GetDesc(); - if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || desc.Format != DXGI_FORMAT_B8G8R8A8_UNORM) { - log_retry_failure(ctx, "expected BGRA8 D3D12 Texture2D"); - return false; - } - - ctx->width = static_cast(desc.Width); - ctx->height = desc.Height; - return ctx->width > 0 && ctx->height > 0; + return ctx->source_textures.size() == ctx->ring_count; } static void open_producer_fence(MimirProgramTextureSource *ctx) @@ -270,20 +315,21 @@ static void open_producer_fence(MimirProgramTextureSource *ctx) static_cast(ctx->last_producer_fence_value)); } -static bool wait_for_producer(MimirProgramTextureSource *ctx) +static bool read_producer_fence(MimirProgramTextureSource *ctx, uint64_t *completed) { + *completed = 0; if (!ctx->producer_fence) { return true; } - const uint64_t completed = ctx->producer_fence->GetCompletedValue(); - if (completed == UINT64_MAX) { + *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; + if (*completed > ctx->last_producer_fence_value) { + ctx->last_producer_fence_value = *completed; } return true; @@ -357,10 +403,11 @@ static bool ensure_bridge(MimirProgramTextureSource *ctx) open_producer_fence(ctx); - blog(LOG_INFO, "Mimir OBS program texture source opened %s (%ux%u)", + blog(LOG_INFO, "Mimir OBS program texture source opened %s (%ux%u ring=%u)", ctx->texture_name.c_str(), ctx->width, - ctx->height); + ctx->height, + ctx->ring_count); return true; } @@ -371,10 +418,16 @@ static bool copy_source_to_bridge(MimirProgramTextureSource *ctx) return false; } - if (!wait_for_producer(ctx)) { + uint64_t completed_producer_value = 0; + if (!read_producer_fence(ctx, &completed_producer_value)) { return false; } + const uint32_t source_slot = ctx->source_textures.empty() + ? 0 + : static_cast(completed_producer_value % ctx->source_textures.size()); + ID3D12Resource *source_texture = ctx->source_textures[source_slot].Get(); + hr = ctx->command_list->Reset(ctx->command_allocator.Get(), nullptr); if (FAILED(hr)) { return false; @@ -382,7 +435,7 @@ static bool copy_source_to_bridge(MimirProgramTextureSource *ctx) D3D12_RESOURCE_BARRIER barriers[2] = {}; barriers[0].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; - barriers[0].Transition.pResource = ctx->source_texture.Get(); + barriers[0].Transition.pResource = source_texture; barriers[0].Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON; barriers[0].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE; barriers[0].Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; @@ -392,7 +445,7 @@ static bool copy_source_to_bridge(MimirProgramTextureSource *ctx) barriers[1].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST; barriers[1].Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; ctx->command_list->ResourceBarrier(2, barriers); - ctx->command_list->CopyResource(ctx->bridge_texture_d3d12.Get(), ctx->source_texture.Get()); + ctx->command_list->CopyResource(ctx->bridge_texture_d3d12.Get(), source_texture); std::swap(barriers[0].Transition.StateBefore, barriers[0].Transition.StateAfter); std::swap(barriers[1].Transition.StateBefore, barriers[1].Transition.StateAfter); @@ -418,6 +471,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"); + ctx->ring_count = clamp_ring_count(obs_data_get_int(settings, "ring_count")); release_bridge(ctx); } @@ -454,6 +508,7 @@ 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"); + obs_data_set_default_int(settings, "ring_count", 1); } static obs_properties_t *mimir_program_texture_properties(void *) @@ -461,6 +516,7 @@ 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); + obs_properties_add_int(props, "ring_count", "Texture ring slots", 1, 4, 1); return props; } diff --git a/notes/current-system-map.md b/notes/current-system-map.md index 0bd12b4..f46fca9 100644 --- a/notes/current-system-map.md +++ b/notes/current-system-map.md @@ -327,10 +327,13 @@ 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. 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. +blind read-before-producer-completion. An optional texture ring is available +when Fensalir and OBS agree on `FENSALIR_PROGRAM_OUTPUT_RING_COUNT`; OBS selects +the latest completed slot from the producer fence value. A later consumer +acknowledgement fence 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 139197c..3bc0174 100644 --- a/scripts/start-eve-dashboard-stream.ps1 +++ b/scripts/start-eve-dashboard-stream.ps1 @@ -2,6 +2,7 @@ param( [string] $Target = "eve", [string] $SharedTextureName = "Global\MimirFensalirProgramTexture", [string] $SharedFenceName = "Global\MimirFensalirProgramFence", + [int] $SharedTextureRingCount = 1, [string] $Configuration = "Debug", [int] $Width = 1620, [int] $Height = 2160, @@ -37,6 +38,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 +$env:FENSALIR_PROGRAM_OUTPUT_RING_COUNT = [string] $SharedTextureRingCount $mimirProject = Join-Path $repoRoot "src\Mimir.App\Mimir.App.csproj" $mimirArgs = @( @@ -50,6 +52,7 @@ $mimirArgs = @( Write-Host "Starting Mimir/Fensalir program output: $SharedTextureName" Write-Host "Program output fence: $SharedFenceName" +Write-Host "Program output ring slots: $SharedTextureRingCount" $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 188dd24..699354a 100644 --- a/scripts/start-eve-program-output.ps1 +++ b/scripts/start-eve-program-output.ps1 @@ -2,6 +2,7 @@ param( [string] $Target = "eve", [string] $SharedTextureName = "Global\MimirFensalirProgramTexture", [string] $SharedFenceName = "Global\MimirFensalirProgramFence", + [int] $SharedTextureRingCount = 1, [string] $Configuration = "Debug", [string] $LogPath = "logs\eve-program-output-fensalir.log", [string[]] $AppArguments = @() @@ -23,6 +24,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 +$env:FENSALIR_PROGRAM_OUTPUT_RING_COUNT = [string] $SharedTextureRingCount $projectPath = Join-Path $repoRoot "src\Mimir.App\Mimir.App.csproj" $dotnetArgs = @( @@ -36,6 +38,7 @@ $dotnetArgs = @( Write-Host "Starting Mimir/Fensalir with shared D3D12 output: $SharedTextureName" Write-Host "Program output fence: $SharedFenceName" +Write-Host "Program output ring slots: $SharedTextureRingCount" 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 0daab81..7bf464c 100644 --- a/src/Mimir.Runtime/Synchronization/MimirEveProgramOutputConfiguration.cs +++ b/src/Mimir.Runtime/Synchronization/MimirEveProgramOutputConfiguration.cs @@ -26,6 +26,7 @@ public static class MimirEveProgramOutputConfigurations { public const string DefaultSharedTextureName = "Global\\MimirFensalirProgramTexture"; public const string DefaultSharedFenceName = "Global\\MimirFensalirProgramFence"; + public const int DefaultSharedTextureRingCount = 1; public static MimirEveProgramOutputConfiguration NativeSharedTexture { get; } = new( "eve-native-d3d12-hardware-stream", @@ -43,7 +44,8 @@ public static class MimirEveProgramOutputConfigurations [ "FENSALIR_PROGRAM_OUTPUT_D3D12=1", $"FENSALIR_PROGRAM_OUTPUT_NAME={DefaultSharedTextureName}", - $"FENSALIR_PROGRAM_OUTPUT_FENCE_NAME={DefaultSharedFenceName}" + $"FENSALIR_PROGRAM_OUTPUT_FENCE_NAME={DefaultSharedFenceName}", + $"FENSALIR_PROGRAM_OUTPUT_RING_COUNT={DefaultSharedTextureRingCount}" ], "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 b120c71..a8ab1ed 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -197,3 +197,4 @@ {"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."} +{"ts": "2026-05-28T00:03:43+01:00", "type": "verification", "status": "ok", "note": "Optional OBS program texture ring proof passed: with FENSALIR_PROGRAM_OUTPUT_RING_COUNT=3, Fensalir published Global\\\\MimirFensalirProgramTexture.0/.1/.2 plus Global\\\\MimirFensalirProgramFence, and OBS 32.1.2 opened Mimir Program Texture at 1264x681 ring=3 with producer fence value 1149."} diff --git a/state/map.yaml b/state/map.yaml index 52618c9..12dba79 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -204,9 +204,12 @@ accepted_design: 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. A later publication ring is - still required to make producer/consumer overwrite races structurally - impossible. + is the explicit D3D12 to libobs-D3D11 boundary. Fensalir and the OBS source + can optionally agree on `FENSALIR_PROGRAM_OUTPUT_RING_COUNT>1`; Fensalir + publishes `Global\MimirFensalirProgramTexture.0`, `.1`, and so on, and OBS + chooses the latest completed slot from the producer fence value. A later + consumer acknowledgement fence 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