Skip to content

libobs: Port the scene blend mode table to obs-scene-blend - #1543

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-scene-blend
Oct 20, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-scene-blend

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1520 (PR 2 of 2; PR 1, #1521, moved the table into its own file and added its characterization test)

What and why

This PR ports libobs/obs-scene-blend.c.h to a new crate, rust/obs-scene-blend. The file holds a single table, obs_blend_mode_params. render_item_texture() in obs-scene.c reads it to look up the blend factors it passes to gs_blend_function_separate() and the operation it passes to gs_blend_op() for each scene item blend mode. libobs-with-rust drops the C file and links the Rust table instead.

  • Crate: obs-scene-blend, one crate per file like obs-scene-bounds (meta: Phase 6 libobs core migration through opaque C seams #444). It is listed in naming.md, in both rust/README.md tables and in the testing-policy obs-tests-core row. It adds no new dependency.
  • Safe core (src/blend.rs):
    • BlendType, BlendOp and BlendMode, with the values from graphics.h and obs.h.
    • BlendParams, the row type.
    • blend_mode_params() returns the whole table and blend_params(mode) returns one row. Both are const fn over a const table.
    • No host cfg, no unsafe.
  • Shim (src/ffi/scene_blend.rs): the C file contains only a table, so the shim is also a table. obs_blend_mode_params is a #[no_mangle] static of #[repr(C)] obs_blend_mode_param rows (five C ints), built from the safe core at compile time. It contains no code that runs.
  • C side, the smallest change that lets the table live in another translation unit:
    • Before this PR the table was static and shared libobs.core-source with obs-scene.c, its only user. A ported group is a separate translation unit, so the table now has external linkage in both builds.
    • Its row type (struct obs_blend_mode_param, the same five fields that were in the anonymous struct) and an extern declaration move to a new private header, libobs/obs-scene-blend.h. It is in source-sets.json but not installed, like obs-scene-bounds.h. Both libobs.pch.h (so obs-scene.c sees it) and the group's own PCH include it.
    • obs-scene.c.h is unchanged and still indexes the table directly.
  • Build group:
    • obs-scene-blend.c.h leaves libobs.core-source for its own group, libobs.ported-scene-blend, which is listed in LIBOBS_PORTED_SOURCES.
    • The new PCH libobs-ported-scene-blend.pch.h includes obs.h (for the OBS_BLEND_* values the C test uses) and obs-scene-blend.h, and nothing from the core. That lets the C test and the oracle compile the file on its own.
    • The group is not host-specific, so sources.cmake, os-*.cmake, obs-build and ported_sources.py need no change; they all read source-sets.json.
  • Symbol: the C file has no EXPORT. In the C build -fvisibility=hidden hides the table. In the Rust build rust-hidden.txt, rust-exports.map and rust-unexports-macos.txt hide it, and there is no rust-exports.d fragment. nm shows r obs_blend_mode_params (local, read-only) in both libobs.so builds.
  • export_list check: before this PR it only collected #[no_mangle] functions. A #[no_mangle] static would have gone unnoticed, or the scan would have picked the next fn in the file. It now also collects statics. To check this, I removed obs_blend_mode_params from rust-hidden.txt: the check then fails with "shims missing from rust-exports.d and rust-hidden.txt: ["obs_blend_mode_params"]".
  • Oracle (oracle/scene_blend.c): renames the table to oracle_obs_blend_mode_params with a #define and includes the original file. It adds accessors for the row count (from the table's own sizeof), the row struct's size, alignment and field offsets, and every value of enum obs_blending_type, enum gs_blend_type and enum gs_blend_op_type.
  • Intentional differences: none.
  • Lead (not changed here): the lookup is still an unchecked index. obs_sceneitem_set_blending_mode() stores any value it is given, so a mode past OBS_BLEND_DARKEN reads past the end of the table. That is undefined behaviour in C, and the Rust static behaves the same way. Scene loading and obs-websocket only produce the seven valid modes. I added no clamp; it would be a separate behaviour fix (test_scene_blend marks this case "characterized, not endorsed"). This is documented in the shim, the parity test header and the rust/README.md row.

test_scene_blend (6 cases) compiles the C file into itself and is unchanged. It passes in every lane but cannot see the swap. Tier 3 is the proof of the swap, and so is test_darken_transparent, which draws Darken through the real libobs and therefore through libobs-with-rust in mode ON.

RED → GREEN

  • RED commit: d19567b92 libobs: Add Tier 1 tests for the scene blend mode table port. The table and the row lookup are todo!(). soldr cargo test -p obs-data --test obs-tests-core scene_blend fails 8 of 11 with "not yet implemented"; the three enum value checks pass.
    • Tier 1 (tests/scene_blend/main.rs) mirrors test_scene_blend 1:1: the same six test names (without test_) and the same cases.
    • New edge cases: the row lookup against the table, BlendMode::ALL in row order, the factor and operation values, values no row uses (GS_BLEND_OP_SUBTRACT, GS_BLEND_ZERO, GS_BLEND_SRCALPHASAT), and the check that only Screen and Multiply use different factors for color and alpha.
    • The tests are a module of the existing core category binary, obs-tests-core. No new test binary: ci/test_binary_cap.py passes (8 of 8).
  • GREEN: 5902c53fe libobs: Port the scene blend mode table to obs-scene-blend. scene_blend passes 16/16 (11 Tier 1, 5 Tier 3), and the whole obs-tests-core binary passes 216/216.
    • Tier 3 (tests/scene_blend/parity.rs). The domain is finite, so everything is compared and nothing is sampled:
      • the row count against the C table's sizeof;
      • the size, alignment and every field offset of obs_blend_mode_param against the C struct (the Tier 2 layout test);
      • every value of the three enums;
      • every row of the static, field by field and then byte for byte against the C table;
      • blend_params(mode) for every mode against the C row.
  • Deliberate-break check: in the Rust core I changed Darken's operation from Min to Max, then restored it.
    • Tier 1 and 3: 4 tests failed, including the two parity tests parity_every_row_matches_c and parity_blend_params_matches_c.
    • With libobs-with-rust rebuilt: test_darken_transparent (darken_keeps_background_where_transparent) failed in mode ON and passed in mode OFF.
    • test_scene_blend passed in both modes, because it compiles the C table itself and never sees the Rust one.
    • After the restore, all of these pass again.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON, ENABLE_WAYLAND=ON). Each tree runs the full cmocka suite against libobs (OFF) and against libobs-with-rust (ON):
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 333/333 333/333 IDENTICAL (1766, same as master)
Rust (ENABLE_RUST_LIBOBS=ON) 333/333 333/333 IDENTICAL (1766)
  • No test was skipped or not run in any of the four runs. The export lists are also identical across the two lanes, and none of them contains obs_blend_mode_params.

  • test_scene_blend and test_darken_transparent pass in all four runs.

  • libobs.ported-scene-blend+.h.c is linked into libobs and not into libobs-with-rust. In libobs-with-rust the table comes from the Rust static.

  • Environment: headless (private Xvfb, private D-Bus, fresh HOME).

  • ci/local_gate.py:

    • --lane lint (clang-format, gersemi, rustfmt, clippy -D warnings, Dylints, ifdef ratchet, test-binary cap): passed.
    • --lane cross (clippy and Dylints for x86_64-pc-windows-msvc and aarch64-apple-darwin, plus a link of every test binary, the oracle included): passed.
    • --lane cross-release: passed.
    • --lane rust-test (soldr cargo test --workspace --locked, plus a --no-run link for x86_64-pc-windows-gnu): passed. The obs-util harness passes 668/668, including the extended export_list check.
  • ci/test_build_groups.py, ci/test_naming.py, ci/test_ported_sources.py, ci/test_rust_scope.py, ci/test_binary_cap.py, ci/test_cmocka_includes.py: OK.

  • Real app: not applicable (no behaviour change).

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (characterization test_scene_blend landed in libobs: Move the scene blend mode table into obs-scene-blend.c #1521)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

The blend mode table in libobs/obs-scene-blend.c.h (obs-rust#1521) gets its own
crate, obs-scene-blend, as obs-scene-bounds did. The safe core names the
blend factors, operations and modes with the values of graphics.h and
obs.h, and has the table and the row lookup as todo!() stubs.

The Tier 1 tests mirror test_scene_blend 1:1 (same six names and cases),
plus new edge cases: the row lookup against the table, the factor and
operation values, values no row uses, and which modes split color from
alpha. They are a module of obs-tests-core, so no new test binary.
Eight of eleven fail with "not yet implemented"; the three enum value
checks pass.

Refs obs-rust#1520
obs_blend_mode_params, the table render_item_texture() reads to set
each scene item's blend factors and operation, moves to the
obs-scene-blend crate. The file is one table, so the shim is the table:
a #[no_mangle] static with the same rows, built at compile time from the
safe core. libobs-with-rust drops the C file and links that static.

The table could stay static only while it shared a translation unit with
obs-scene.c. Now that its file is its own ported group,
libobs.ported-scene-blend, the C table has external linkage in both
builds, with its row type, struct obs_blend_mode_param, and an extern
declaration in the new private header obs-scene-blend.h (in the core PCH
and the group's own). obs-scene.c is unchanged and still indexes the
table unchecked, so a mode past OBS_BLEND_DARKEN reads past its end, as
before. The symbol is not EXPORT: it is hidden in the C build by
-fvisibility=hidden and in the Rust one through rust-hidden.txt,
rust-exports.map and rust-unexports-macos.txt. The export_list check now
also finds #[no_mangle] statics.

Tier 3 (oracle/scene_blend.c) compares the whole table with the C one,
row by row and byte for byte, plus the row count, the row struct layout
and the enum values. test_scene_blend compiles the C file into itself
and is unchanged.

Refs obs-rust#1520
@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 17:40
@zackees
zackees enabled auto-merge October 20, 2026 17:42
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
Merged via the queue into obs-rust:master with commit 3d1a172 Oct 20, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

rust: Port the scene blend lookups in obs-scene.c to obs-scene-blend

2 participants