Publish OBS program output consumer fence

This commit is contained in:
Meta Vens 2026-05-28 00:20:32 +01:00
commit 319210778a
6 changed files with 88 additions and 19 deletions

View file

@ -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`

View file

@ -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:

View file

@ -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<ID3D12Device> d3d12_device;
@ -37,6 +39,7 @@ struct MimirProgramTextureSource {
ComPtr<ID3D12GraphicsCommandList> command_list;
ComPtr<ID3D12Fence> fence;
ComPtr<ID3D12Fence> producer_fence;
ComPtr<ID3D12Fence> consumer_fence;
std::vector<ComPtr<ID3D12Resource>> source_textures;
ComPtr<ID3D12Resource> bridge_texture_d3d12;
ComPtr<ID3D11Device> 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<unsigned long>(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<MimirProgramTextureSource *>(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;
}

View file

@ -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

View file

@ -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."}

View file

@ -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