From 319210778ac7435f7405816037b3c133a678138a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 28 May 2026 00:20:32 +0100 Subject: [PATCH] Publish OBS program output consumer fence --- docs/implementation-plan.md | 7 +- docs/obs-stem-plugin.md | 14 ++-- .../mimir_obs_program_texture_source.cpp | 67 ++++++++++++++++++- notes/current-system-map.md | 11 +-- state/evidence.jsonl | 1 + state/map.yaml | 7 +- 6 files changed, 88 insertions(+), 19 deletions(-) diff --git a/docs/implementation-plan.md b/docs/implementation-plan.md index 1ca5404..79468b9 100644 --- a/docs/implementation-plan.md +++ b/docs/implementation-plan.md @@ -170,9 +170,10 @@ a named invariant that the native runtime cannot protect yet. preserving the explicit boundary cost that libobs is D3D11 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 program-output fence value. A later consumer - acknowledgement fence is still required to make producer/consumer overwrite - races structurally impossible. + completed slot from the program-output fence value. OBS can also publish a + consumer fence, and when Fensalir is launched with + `FENSALIR_PROGRAM_OUTPUT_CONSUMER_FENCE_NAME`, Fensalir skips publication + instead of reusing a ring slot OBS has not acknowledged. `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 957147f..8bcd49c 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 +Consumer D3D12 fence name: Global\MimirObsProgramConsumerFence Texture ring slots: 1 ``` @@ -41,22 +42,25 @@ Spout2. It is not pure zero-copy yet; the remaining copy is the D3D12-to-libobs-D3D11 boundary. Fensalir also publishes a named D3D12 program-output fence; the OBS source opens it and observes publication completion before copying. This prevents blind read-before-producer-completion -behavior without coupling OBS to Fensalir's private frame fence. A later consumer -acknowledgement fence is still required to make overwrite races structurally -impossible when producer and consumer cadence diverge. +behavior without coupling OBS to Fensalir's private frame fence. For OBS-only stress testing, Fensalir and the OBS source can agree on a bounded texture ring: ```text FENSALIR_PROGRAM_OUTPUT_RING_COUNT=3 +FENSALIR_PROGRAM_OUTPUT_CONSUMER_FENCE_NAME=Global\MimirObsProgramConsumerFence Texture ring slots: 3 +Consumer D3D12 fence name: Global\MimirObsProgramConsumerFence ``` With a ring count above one, Fensalir publishes textures named `Global\MimirFensalirProgramTexture.0`, `.1`, and so on. The OBS source uses the -program-output 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. +program-output fence value to copy the latest completed slot and signals the +consumer fence after its GPU copy. When Fensalir is launched with the matching +consumer-fence environment variable, it will not reuse a ring slot until OBS has +acknowledged a copy at or beyond that slot's published fence value. 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 6a3a69b..d08a5c2 100644 --- a/native/obs_stem_source/mimir_obs_program_texture_source.cpp +++ b/native/obs_stem_source/mimir_obs_program_texture_source.cpp @@ -21,6 +21,7 @@ struct MimirProgramTextureSource { obs_source_t *source = nullptr; std::string texture_name = "Global\\MimirFensalirProgramTexture"; std::string fence_name = "Global\\MimirFensalirProgramFence"; + std::string consumer_fence_name = "Global\\MimirObsProgramConsumerFence"; uint32_t ring_count = 1; uint32_t width = 0; uint32_t height = 0; @@ -29,6 +30,7 @@ struct MimirProgramTextureSource { uint64_t next_retry_ns = 0; uint64_t next_failure_log_ns = 0; HANDLE bridge_shared_handle = nullptr; + HANDLE consumer_fence_shared_handle = nullptr; HANDLE fence_event = nullptr; gs_texture_t *obs_texture = nullptr; ComPtr d3d12_device; @@ -37,6 +39,7 @@ struct MimirProgramTextureSource { ComPtr command_list; ComPtr fence; ComPtr producer_fence; + ComPtr consumer_fence; std::vector> source_textures; ComPtr bridge_texture_d3d12; ComPtr d3d11_device; @@ -119,6 +122,7 @@ static void release_bridge(MimirProgramTextureSource *ctx) ctx->source_textures.clear(); ctx->fence.Reset(); ctx->producer_fence.Reset(); + ctx->consumer_fence.Reset(); ctx->command_list.Reset(); ctx->command_allocator.Reset(); ctx->command_queue.Reset(); @@ -135,14 +139,28 @@ static void release_bridge(MimirProgramTextureSource *ctx) ctx->bridge_shared_handle = nullptr; } + if (ctx->consumer_fence_shared_handle) { + CloseHandle(ctx->consumer_fence_shared_handle); + ctx->consumer_fence_shared_handle = nullptr; + } + if (ctx->fence_event) { CloseHandle(ctx->fence_event); ctx->fence_event = nullptr; } } -static bool wait_for_copy(MimirProgramTextureSource *ctx) +static bool wait_for_copy(MimirProgramTextureSource *ctx, uint64_t consumed_producer_value) { + if (ctx->consumer_fence && consumed_producer_value > 0) { + HRESULT consumer_hr = ctx->command_queue->Signal(ctx->consumer_fence.Get(), consumed_producer_value); + if (FAILED(consumer_hr)) { + blog(LOG_WARNING, + "Mimir OBS program texture: consumer fence Signal failed: 0x%08lx", + static_cast(consumer_hr)); + } + } + const uint64_t value = ++ctx->fence_value; HRESULT hr = ctx->command_queue->Signal(ctx->fence.Get(), value); if (FAILED(hr)) { @@ -233,6 +251,40 @@ static bool create_devices(MimirProgramTextureSource *ctx) return true; } +static void create_consumer_fence(MimirProgramTextureSource *ctx) +{ + if (ctx->consumer_fence_name.empty()) { + return; + } + + HRESULT hr = ctx->d3d12_device->CreateFence(0, D3D12_FENCE_FLAG_SHARED, IID_PPV_ARGS(&ctx->consumer_fence)); + if (FAILED(hr)) { + log_retry_failure(ctx, "CreateFence consumer fence failed", hr); + return; + } + + const auto wide_name = utf8_to_wide(ctx->consumer_fence_name); + if (wide_name.empty()) { + log_retry_failure(ctx, "consumer fence name is empty or invalid UTF-8"); + ctx->consumer_fence.Reset(); + return; + } + + hr = ctx->d3d12_device->CreateSharedHandle( + ctx->consumer_fence.Get(), + nullptr, + GENERIC_ALL, + wide_name.c_str(), + &ctx->consumer_fence_shared_handle); + if (FAILED(hr)) { + log_retry_failure(ctx, "CreateSharedHandle consumer fence failed", hr); + ctx->consumer_fence.Reset(); + return; + } + + blog(LOG_INFO, "Mimir OBS program texture consumer fence published %s", ctx->consumer_fence_name.c_str()); +} + static bool open_source_texture(MimirProgramTextureSource *ctx) { ctx->source_textures.clear(); @@ -396,7 +448,13 @@ static bool ensure_bridge(MimirProgramTextureSource *ctx) ctx->next_retry_ns = now + 1000000000ULL; release_bridge(ctx); - if (!create_devices(ctx) || !open_source_texture(ctx) || !create_obs_bridge_texture(ctx)) { + if (!create_devices(ctx)) { + release_bridge(ctx); + return false; + } + + create_consumer_fence(ctx); + if (!open_source_texture(ctx) || !create_obs_bridge_texture(ctx)) { release_bridge(ctx); return false; } @@ -458,7 +516,7 @@ static bool copy_source_to_bridge(MimirProgramTextureSource *ctx) ID3D12CommandList *lists[] = {ctx->command_list.Get()}; ctx->command_queue->ExecuteCommandLists(1, lists); - return wait_for_copy(ctx); + return wait_for_copy(ctx, completed_producer_value); } static const char *mimir_program_texture_get_name(void *) @@ -471,6 +529,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->consumer_fence_name = obs_string(settings, "consumer_fence_name", "Global\\MimirObsProgramConsumerFence"); ctx->ring_count = clamp_ring_count(obs_data_get_int(settings, "ring_count")); release_bridge(ctx); } @@ -508,6 +567,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_string(settings, "consumer_fence_name", "Global\\MimirObsProgramConsumerFence"); obs_data_set_default_int(settings, "ring_count", 1); } @@ -516,6 +576,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_text(props, "consumer_fence_name", "Consumer 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 6a74492..d158eb6 100644 --- a/notes/current-system-map.md +++ b/notes/current-system-map.md @@ -330,11 +330,12 @@ a program-output publication fence, and the OBS source opens that fence before copying. This prevents blind read-before-producer-completion without coupling OBS to Fensalir's private frame fence. 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 program-output 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. +the latest completed slot from the program-output fence value. OBS can publish +`Global\MimirObsProgramConsumerFence`; when Fensalir is launched with +`FENSALIR_PROGRAM_OUTPUT_CONSUMER_FENCE_NAME` pointing at that fence, it skips +publication instead of reusing a ring slot OBS has not acknowledged. +`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/state/evidence.jsonl b/state/evidence.jsonl index dbb09ab..3f518cb 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -199,3 +199,4 @@ {"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."} {"ts": "2026-05-28T00:08:37+01:00", "type": "verification", "status": "ok", "note": "Fensalir program output now uses a dedicated shared publication fence instead of the private frame fence. Ring proof reran with FENSALIR_PROGRAM_OUTPUT_RING_COUNT=3; OBS opened Global\\\\MimirFensalirProgramFence at value 1252 and Global\\\\MimirFensalirProgramTexture ring=3 at 1264x681."} +{"ts": "2026-05-28T00:16:37+01:00", "type": "verification", "status": "ok", "note": "Two-way OBS program texture ring proof passed: OBS published Global\\\\MimirObsProgramConsumerFence, Fensalir opened that consumer fence, OBS opened Global\\\\MimirFensalirProgramFence at value 903 and the 1264x681 ring=3 texture set. Fensalir can now avoid reusing busy ring slots when the consumer fence is configured."} diff --git a/state/map.yaml b/state/map.yaml index c4d61f3..7dcb795 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -208,9 +208,10 @@ accepted_design: 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 program-output fence value. A later consumer - acknowledgement fence is still required to make producer/consumer overwrite - races structurally impossible. + latest completed slot from the program-output fence value. OBS can publish + `Global\MimirObsProgramConsumerFence`; when Fensalir is launched with + `FENSALIR_PROGRAM_OUTPUT_CONSUMER_FENCE_NAME`, it skips publication instead + of reusing a ring slot OBS has not acknowledged. `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