Skip to content

obs-scene: Port the scene item crop arithmetic - #1947

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/scene-crop
Oct 23, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/scene-crop

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1937

What and why

This PR ports the scene item crop arithmetic in libobs/obs-scene.h / obs-scene.c.h to a new crop module of rust/obs-scene, next to extent (#1922). These are the four functions #1840 made defined on every input:

  • obs_scene_cropped_size(): the size a crop leaves, for calc_cx and calc_cy;
  • obs_scene_scale_crop(): a nested scene's crop, rescaled in update_nested_scene_crop;
  • obs_scene_crop_offset(): how far render_item moves a cropped source;
  • the negative-crop clamp of obs_sceneitem_set_crop().

What stays C: calc_cx/calc_cy, update_nested_scene_crop (it computes the factors from the old and new size and writes the crops), update_item_transform, render_item, the load clamp obs_scene_crop_from_int64() in scene_load_item, and crop_equal with the setter's change check. The hybrid libobs drops the C file and links the Rust shims in its place.

  • Move (libobs: Move the scene item crop arithmetic out, no behaviour change):

    • The three static inlines move verbatim from obs-scene.h into libobs/obs-scene-crop.c.h. The clamp moves with them as obs_scene_clamp_crop(), applied to each side in the setter. They are declared in obs-scene-crop.h without EXPORT. obs-scene.h includes that header, so every caller stays as it is.
    • As in obs-scene: Port the group content extent #1922, the new file needs no core header. obs-scene.h's structs use pthread_mutex_t, so a C test or the oracle could not build the file on its own if it needed that header.
    • The file gets its own small PCH (limits.h, math.h, stdint.h and its header).
    • test_scene_crop_steps (libobs: Clamp loaded scene item crops and saturate the crop and group size casts #1840) calls three of the functions, which are now hidden in libobs. Its tests.d fragment therefore compiles obs-scene-crop.c.h into it, still with UBSan. The test source is unchanged.
  • Characterization (test_scene_item_crop, 11 cases). Eight cases call the four functions directly:

    • sums equal to the size and over it, and sums past the int range (up to 4 × INT_MAX, and 2^32 against UINT32_MAX);
    • negative sums;
    • the rescale's truncation toward 0;
    • NaN of either sign or payload, ±inf, FLT_MAX, the smallest and subnormal factors, and crops at INT_MIN/INT_MAX, with the floats on either side of ±2^31;
    • the offset's rounding past 2^24;
    • the clamp.

    Three cases go through the real libobs: a scene and an item over a source of a fixed size.

    • Negative crops set with obs_sceneitem_set_crop() are clamped on each side, and the item is drawn from the clamped crop.
    • Crops whose sum equals the size draw the item 0 wide. One more pixel, or a sum past the int range, draws it 2 px wide.
    • A nested scene grows and shrinks (100 → 400×200 → 100×50 → 150 → 100 → 25 → 100, then 1 → 4000×), and its item's crop is checked each time through the exported setter and getter and the drawn size.
    • Characterized, not endorsed:
      • A negative crop that the other crops make up for is subtracted. The setter never stores one, but the bounds crop is not clamped there.
      • The crop is a float before the rescale, so past 2^24 it loses its low bits even at a factor of 1.
      • The truncation makes a rescale and its inverse lossy: a crop of 10 at × 0.25 then × 4 comes back as 8.
      • A crop saturated at INT_MAX shrinks from INT_MAX (536870 at 1/4000), not from the crop it came from.
    • The real-libobs cases need core video, so the test is Linux with OpenGL (suite graphics, as test_scene_item_crop_load), and they skip without a display. test_scene_crop_steps keeps covering the arithmetic on every platform.
    • It is registered in test/cmocka/build/test-graph.json. It uses no assert_in_range. It builds and runs with cmocka 2.0.2 and with 1.1.7.
  • Safe core (src/crop.rs): cropped_size, scale_crop, crop_offset and clamp_crop.

    • The size sums the four crops in 64 bits. It gives 2 when u32::try_from fails (a negative sum, or one past u32::MAX) or the sum is past the size.
    • The rescale is one f32 multiply of crop as f32. Then NaN is 0, 2^31 and up is i32::MAX, -2^31 and down is i32::MIN, and the rest is truncated toward 0. Rust's saturating as i32 gives the same values on every input; the branches are written out to match the C.
    • The offset is the 64-bit sum as the nearest f32 (ties to even), as C's (float)int64_t is.
    • No unsafe.
  • Shim (src/ffi/scene_crop.rs): the four functions, with the C signatures, all by value. No struct is mirrored and there are no pointers.

    • No new dependency. test_scene_bounds_place_rust still links and passes.
  • _rust variant: test_scene_item_crop_rust runs the unchanged test source against libobs_rust-static, like test_source_audio_timing_steps_rust in obs-source: Port the source audio timestamp placement #1936. It is in tests.d/test_scene_item_crop.cmake, inside if(TARGET libobs_rust-static).

    • It links OBS::libobs first, so libobs gives the exported scene API and the archive gives only the four hidden functions.
    • With the archive first, its obs-data members come in for obs_data_* and need libjansson, and the link fails.
  • C side:

    • obs-scene-crop.c.h is in its own group, libobs.ported-scene-crop, listed under LIBOBS_PORTED_SOURCES in source-sets.json. Its PCH is libobs-ported-scene-crop.pch.h.
    • ci/test_ported_sources.py checks the group on Linux, Windows and macOS.
  • Symbols: the four shims are listed in rust-hidden.txt, in the local: list of rust-exports.map and in rust-unexports-macos.txt. nm shows them as local (t) in the hybrid libobs. The exported symbol sets are unchanged: 1766 in all three libraries, identical.

  • Oracle (oracle/scene_crop.c): the original file, unmodified, with the four functions renamed to oracle_*.

  • Docs:

    • The rust/README.md rows (crate table and port table), the crate's Cargo description and the lib.rs docs are updated.
    • The docs/rust-port/naming.md row (libobs/obs-scene*.c.h) already covers the file.
    • No docs/features.md line: there is no user-visible change.
  • port_comparison (its own file, benches/port_comparison/scene_crop.rs): scene_crop/{cropped_size,scale_crop,crop_offset}/16, shim against oracle, with the calls for 16 items. Pinned to one core, the medians are:

    Bench Rust C
    cropped_size/16 64.7 ns 58.0 ns
    scale_crop/16 109.1 ns 84.0 ns
    crop_offset/16 35.5 ns 36.1 ns

    The first two are about 0.4 ns and 1.6 ns slower per call; I have not looked into why. They run a few times per item when its transform is updated or it is drawn cropped.

No intentional differences. Since #1840, every input has defined C behaviour, so the parity test excludes nothing.

Seen while characterizing, and not changed here (upstream behaviour):

  • A nested scene's crop drifts when the scene is resized and resized back, because each rescale truncates: 10 at 100 px, then 25 px, then 100 px again, is 8.
  • crop_equal compares the new crop before it is clamped with the stored one after the clamp. So setting the same negative crop again sets update_transform again each time. This is harmless churn, and I only read it in the code; there is no test for it.

RED → GREEN

  • RED commit: b4620180e obs-scene: Add the scene item crop arithmetic tests. The core is todo!(). soldr cargo test -p obs-data --test obs-tests-core scene::crop fails all 15 cases (0 passed, 15 failed, "not yet implemented").
    • Tier 1 (tests/scene/crop.rs) ports the eight direct cases of test_scene_item_crop one for one, under the same names, with the same values and expectations.
    • 7 new edge cases: every split of every small sum against every small size, the u32 edges of the size, the floats next to the rescale's edges, NaN payloads, the product's rounding, the offset's ties, and the clamp of every sign.
    • The tests are a module of the existing core category binary, obs-tests-core. No new test binary.
  • GREEN: 1069e72b9 obs-scene: Port the scene item crop arithmetic. scene::crop passes 15/15.
  • Tier 3: 06ff310df obs-scene: Check the scene item crop port against the C (tests/scene/crop_parity.rs).
    • What is compared: every output, bit for bit. The size and the crops are compared as integers and the offset as f32 bits.

    • Runs:

      • the test_scene_item_crop values;
      • every factor bit pattern within 256 of 2^31, 2^30, 1, 0.5, +0, +inf, the last NaNs and 2^-31, with both signs, for crops of ±1, ±2 and the int edges;
      • every sum from the size - 3 to the size + 3 for sizes at the u32 edges;
      • 8192-case proptests over full i32 crops, u32 sizes and any-bit f32 factors (biased to the edges), sums within 3 of the size, and factors that put the product within 4 floats of ±2^31 or of a whole number.
    • Tier 1 gains edge_scale_crop_product_is_f32 (see the mutation check).

    • proptest-regressions/crop_parity.txt keeps the seeds the mutants failed on.

    • Mutation check (done once, not committed). 20 of 24 mutants fail both Tier 1 and Tier 3:

      Mutant Tier 1 failures Tier 3 failures
      size: < for <= at the size 3 3
      size: 0 for 2 when over 5 4
      size: 32-bit wrapping sum 3 4
      size: bounds_crop2 left out 5 4
      size: negative sum read as unsigned 3 4
      scale: NaN gives 1 2 3
      scale: upper edge one float below 2^31 3 4
      scale: lower edge one float above -2^31 3 4
      scale: INT_MAX - 1 on saturation 3 4
      scale: INT_MIN + 1 on saturation 3 4
      scale: round for truncation 3 4
      scale: floor for truncation 2 4
      scale: product in f64 1 3
      scale: crop kept exact at factor 1 1 3
      offset: 32-bit wrapping sum 2 2
      offset: two roundings 1 2
      offset: bounds crop left out 2 2
      clamp: -1 kept 2 2
      clamp: 1 for negative 2 2
      clamp: saturating_abs 2 2
      scale: NaN check dropped 0 0 (equivalent)
      scale: > for >= at 2^31 0 0 (equivalent)
      scale: < for <= at -2^31 0 0 (equivalent)
      clamp: <= 0 for < 0 0 0 (equivalent)

      On the first run, "product in f64" passed Tier 1 (Tier 3 caught it). edge_scale_crop_product_is_f32 was added for it: 16777217 × 3 is 50331648, where an exact product would round to 50331652. The four survivors are equivalent: Rust's as i32 already gives 0 for NaN, i32::MAX at 2^31 and i32::MIN at -2^31, and the clamp gives 0 for 0 either way.

  • Local runs used two builds: OBS_LIBOBS_BACKEND=C, and HYBRID with OBS_BUILD_C_REFERENCE=ON. Headless: private Xvfb with -noreset, private D-Bus, fresh HOME, no core dumps, no deep TMPDIR. The libobs-c-reference target is not in all, so it was built by name before each c-ref run.
  • The final head is 5cd9049a7, on 20839ada6 (rebased after obs-source: Port the source audio timestamp placement #1936 and libobs: Port the source registration rules to obs-core #1929; only the README rows and the port_comparison bench list conflicted). The rebase also bumps the pinned graphics suite count in rust/obs-build/tests/native.rs from 13 to 14 for the new test, in the characterization commit; uv run ci/local_gate.py --test-diff (the CI changed-crates step) passes 2861/2861 with 1 skipped on the final head, and the build-group checks and the lint lane pass. The earlier head was on 0905cee3d. The runs below were on 1ede6821e, the same series on b4a58944c, unless a line says otherwise. The last rebase brought only obs-properties: Preserve parameter destructor ordering #1939 and fix(obs-util): preserve configs when opening fails #1944 (Rust obs-properties/obs-util and docs/features.md). After it, these were re-run on 5cd9049a7:
    • the hybrid build (it links obs-util) and the focused set on hybrid and c-ref;
    • scene::crop 25/25, and RED on b4620180e (still 0/15);
    • the lint lane, ci/test_build_groups.py, ci/test_ported_sources.py, ci/test_oracle_standins.py, and the export counts.
Lane Library Full ctest Focused set Exported symbols
OBS_LIBOBS_BACKEND=C libobs 393/393 7/7 (61 cases) 1766
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 396/396 8/8 (72 cases) 1766, identical
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference (focused set only) 8/8 (72 cases) 1766, identical
  • The focused set: test_scene_item_crop (11/11), test_scene_item_crop_rust (hybrid builds only, 11/11), test_transform_clipboard, test_group_child_drift, test_group_empty_bounds, test_group_extent, and the libobs: Clamp loaded scene item crops and saturate the crop and group size casts #1840 crop tests test_scene_crop_steps and test_scene_item_crop_load. No case was skipped.
  • Full ctest skips: no ctest-level skips. Three cmocka cases skipped in both lanes, none of them related to this PR: test_config_hex_float_rounding's comma-locale case (no such locale is installed) and open_crashes_without_obs_core in test_audio_io and in test_video_io.
  • The move commit on its own (same content, before the rebase) passes -R 'scene|group|transform_clipboard' 27/27 on the C lane, with no skips.
  • soldr cargo test -p obs-data --test obs-tests-core passes 1052/1052, 25 of them new (16 Tier 1, 9 Tier 3).
  • soldr cargo bench -p obs-c-oracle --bench port_comparison -- --test scene_crop passes.
  • uv run ci/local_gate.py --lane lint passes (also on 5cd9049a7). --lane cross passes.
  • ci/test_build_groups.py, ci/test_ported_sources.py and ci/test_oracle_standins.py pass.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change for libobs).

Windows/macOS: not built there. The core and the shim have no host conditional. The new file and its header need no pthread type. test_scene_item_crop is Linux-only (it needs core video), and test_scene_crop_steps still builds the moved file on every platform.

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 (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • 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): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (none)

@rdoupe
rdoupe requested a review from zackees as a code owner October 23, 2026 15:31
@zackees

zackees commented Oct 23, 2026

Copy link
Copy Markdown
Member

Coordinator: two things block this PR.

  1. Fast Gate: native_test_graph_replaces_legacy_utility_definitions (rust/obs-build/tests/native.rs:1321) fails with graphics tests left 14, right 13. This PR adds a graphics-suite cmocka test, and only the core count was unpinned (test: stop pinning the core cmocka test count in the obs-build graph contract (#1908) #1910), so ("graphics", Some(13)) needs to become Some(14).
  2. Conflict with master (DIRTY).

If you haven't pushed a fix by the next coordinator tick, I'll rebase and bump the count myself.

@rdoupe

rdoupe commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator Author

Both fixed in afa07cd5f: rebased onto master after #1936, and ("graphics", Some(14)) folded into the characterization commit that adds the test. ci/local_gate.py --test-diff passes locally (2809/2809).

@zackees
zackees added this pull request to the merge queue Oct 23, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 23, 2026
@zackees
zackees added this pull request to the merge queue Oct 23, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 23, 2026
obs_scene_cropped_size(), obs_scene_scale_crop() and
obs_scene_crop_offset(), the static inlines obs-rust#1840 made defined on
every input, move verbatim from obs-scene.h into obs-scene-crop.c.h.
The negative-crop clamp of obs_sceneitem_set_crop() moves with them as
obs_scene_clamp_crop(), applied to each side. They are declared in
obs-scene-crop.h without EXPORT, and obs-scene.h includes that header,
so calc_cx, calc_cy, update_nested_scene_crop and render_item call
them as before. Same behavior.

The new file needs no core header (not obs-scene.h, whose structs
need the pthread types), so a test or the oracle can compile it on its
own on every platform. obs_scene_crop_from_int64(), the load clamp,
stays in obs-scene.h.

test_scene_crop_steps calls the three functions, which are now hidden
in libobs, so its build compiles obs-scene-crop.c.h into it, still
with UBSan. The test source is unchanged.

Refs obs-rust#1937
The four functions are hidden in libobs, so the test compiles
obs-scene-crop.c.h and pins what it does today: sums equal to and over
the size and sums past the int range, a negative sum and negative crops
made up for by the others, the rescale's truncation toward 0 at every
edge of the int range, with NaN, the infinities, the largest and the
smallest factors, the offset's rounding past 2^24, and the clamp.

Through the real libobs: negative crops set with obs_sceneitem_set_crop()
are clamped on each side, crops summing to and past the size and past
the int range draw the item 0 or 2 px wide, and a nested scene that
grows and shrinks rescales its item's crop. Characterized, not
endorsed: the truncation is lossy (10 at x 0.25 then x 4 comes back as
8), and a crop saturated at INT_MAX shrinks from INT_MAX. These cases
need core video (Linux, under xvfb-run) and skip without a display.
No assert_in_range, so it builds with cmocka 1.x and 2.x.

Refs obs-rust#1937
Tier 1 ports the eight cases of test_scene_item_crop that call the
functions directly, and adds every small sum against every small
size, the u32 edges of the size, the floats next to the rescale's
edges, NaN payloads, the product's rounding, the offset's ties and
the clamp of every sign. The core is still todo, so all 15 cases
panic: not yet implemented.

Refs obs-rust#1937
The safe core matches obs-scene-crop.c.h: the size sums the four
crops in 64 bits and leaves 2 for a negative sum or one past the size,
the rescale is one f32 multiply of the crop as a float, then NaN is 0,
2^31 and up INT_MAX, -2^31 and down INT_MIN, and the rest truncated
toward 0; the offset is the 64-bit sum as the nearest f32, and the
clamp makes a negative crop 0. No unsafe.

The four shims take and return values, so no struct is mirrored, and
stay hidden. calc_cx, calc_cy, update_nested_scene_crop, render_item,
the load clamp and the setter's change check stay C. The C file moves
to libobs.ported-scene-crop with its own PCH. obs-scene gains no
dependency.

test_scene_item_crop_rust runs the unchanged characterization against
libobs_rust-static. It links libobs first, so the archive gives only
the four hidden functions; with the archive first its obs-data members
come in and need libjansson.

Tier 1 scene::crop is 15/15.

Refs obs-rust#1937
The oracle is obs-scene-crop.c.h, unmodified, with its four functions
renamed. Every output is compared bit for bit, the offset as f32 bits:
the test_scene_item_crop values, sweeps of the factor's bit patterns
around 2^31, 2^30, 1, 0.5, 0, the infinities and the NaNs, every sum
from the size - 3 to the size + 3 at the u32 edges, and proptests over
full i32 crops, u32 sizes and any-bit f32 factors, with crops near the
int edges and factors that put the product next to +/-2^31 or a whole
number. No input is excluded.

Breaking the core: < for <= at the size, 0 for 2, a 32-bit sum, a
crop left out, a negative sum read as unsigned, NaN as 1, either edge
one float in, INT_MAX - 1 or INT_MIN + 1 on saturation, round or floor
for the truncation, the product in f64, the crop kept exact, a 32-bit
or twice-rounded offset, the offset without the bounds crop, and three
wrong clamps each fail Tier 1 and Tier 3. The f64 product first passed
Tier 1; edge_scale_crop_product_is_f32 is added for it. Four survive
and are equivalent: dropping the NaN check, > for >= at 2^31, < for <=
at -2^31 (Rust's as i32 already gives 0, INT_MAX and INT_MIN there),
and <= 0 for < 0 in the clamp. Reverted. The seeds the mutants failed
on are kept in proptest-regressions/crop_parity.txt. No intentional
differences.

Refs obs-rust#1937
rust/README.md: the obs-scene layout row names
libobs/obs-scene-crop.c.h, and its row of the ported table gives the
swap, what stays in obs-scene.c.h, what is passed by value, the
rescale's branches and truncation, the behavior kept from the
characterization, the _rust variant and what Tier 3 compares. No
intentional differences. The obs-scene Cargo description and crate
docs name the new module. The naming.md row (libobs/obs-scene*.c.h)
already covers the file.

port_comparison: scene_crop/{cropped_size,scale_crop,crop_offset}/16,
shim against oracle, in benches/port_comparison/scene_crop.rs: the
calls for 16 items. Pinned to one core, the medians are 64.7 ns (Rust)
against 58.0 ns (C) for the sizes, 109.1 ns against 84.0 ns for the
rescales, and 35.5 ns against 36.1 ns for the offsets: about 0.4 and
1.6 ns more per call for the first two (not looked into further). They
run when an item's transform is updated and when a cropped item is
drawn, a few calls per item.

Fixes obs-rust#1937
@zackees
zackees enabled auto-merge October 23, 2026 17:11
@zackees
zackees added this pull request to the merge queue Oct 23, 2026
Merged via the queue into obs-rust:master with commit 773faa0 Oct 23, 2026
33 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
Ports libobs/obs-scene-render-plan.c.h, the per-item render decisions
of render_item() and render_item_texture(), to the render_plan module
of rust/obs-scene. render_item() and render_item_texture() stay C in
obs-scene.c.h: every gs_* call, obs_source_video_render(), the texrender
objects, obs_get_video_sdr_white_level() (passed as a value), the blend
table lookup (obs-rust#1521), calc_cx/calc_cy (obs-rust#1947), and the mapping of the
effect and technique values to obs->video.* and the technique names.
The hybrid libobs drops the C file and links the Rust shims in its
place.

Safe core (src/render_plan.rs), one function per hidden C function:
- texture_enabled(): any crop or bounds crop side non-zero, a scale
  filter other than Disable, sRGB off, a blend type other than Normal,
  or a nested scene that is not a group;
- texrender_action(): destroy when there and either not wanted or not in
  the format, then create when wanted and none is left (None, Keep,
  Destroy, Create, Recreate);
- size_empty() (obs-rust#573), clear_white() (obs-rust#1406: Darken), child() (Show for
  a visible item with a running show transition, Hide for a hidden one
  with a running hide transition, else Source) and linear_srgb();
- texcoords_centered(): over obs-graphics' Matrix4, every entry compared
  by its bits against the identity with the x and y translation
  replaced by their floor(), as the C's memcmp, so -0.0 and NaN payloads
  behave as in C;
- effect(): Disable draws as it is, Point sets the point sampler, and
  any other filter at an output scale not within EPSILON of 1 on both
  axes sets the base dimension and picks the low-resolution bilinear
  effect below 0.5 on either axis, else Bicubic, Lanczos or Area (which
  upscales at 1 and over on both axes), else the default effect.
  fabsf(f - 1) <= EPSILON is written the same way in f32, so NaN is
  never close;
- multiplier(): white / 80 for SDR or extended into scRGB, 80 / white
  for scRGB into SDR or extended (+inf for a white level of 0, as in C),
  else 1;
- technique(): the eight techniques, with values outside enum
  gs_color_space drawing as Draw or DrawUpscale.
Values from the item and the colour spaces are i32 and compared as
they are. The module denies clippy's arithmetic, indexing, as, unwrap,
expect, panic, unimplemented and unreachable lints, keeps no state and
takes no lock.

Shim (src/ffi/scene_render_plan.rs): the ten functions with the C
signatures. struct obs_scene_render_triggers and struct
obs_scene_render_effect_plan are mirrored (#[repr(C)]); the triggers and
the matrix (obs-graphics' ffi matrix4) are read through their pointers
and the effect plan written through its pointer. The enums are returned
as the C's values. No item or source pointer crosses.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-scene-render-plan (LIBOBS_PORTED_SOURCES), whose PCH has
only math.h, string.h, graphics/math-defs.h and obs-scene-render-plan.h;
libobs.pch.h keeps the header for obs-scene.c. The functions are not
EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of
rust-exports.map and rust-unexports-macos.txt. libobs-rust already
links the crate's ffi. ci/test_ported_sources.py checks the group on
every host. The exported symbol sets are unchanged and equal (1766 on
the C backend, the hybrid and libobs-c-reference).

test_scene_render_plan_steps still compiles the C file into itself,
unchanged. The new test_scene_render_plan_steps_rust builds the same
test source against libobs_rust-static instead, as
test_source_color_space_steps_rust does: the test calls the hidden
functions directly, so this variant runs the shims, the structs passed
from C included. The real-libobs scene tests see the swap through the
hybrid.

Tier 1 scene::render_plan passes 24/24.

Refs obs-rust#2003
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
* libobs: Move the scene item render decisions into obs-scene-render-plan.c

Behaviour-neutral extraction before the Rust port. The per-item render
decisions of render_item() and render_item_texture() in obs-scene.c.h
move to libobs/obs-scene-render-plan.c.h as pure functions, declared in
the new libobs/obs-scene-render-plan.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_scene_render_texture_enabled(): draw through the texrender, from
  struct obs_scene_render_triggers (the crop and bounds crop, the scale
  filter, the blend method and type, and whether the item is a nested
  scene and a group). It replaces item_texture_enabled()'s helpers.
- obs_scene_render_texrender_action(): keep, destroy, create or recreate
  the texrender.
- obs_scene_render_size_empty(): the source has no size, the gate of the
  empty-source rule (obs-rust#573).
- obs_scene_render_clear_white(): white for Darken (obs-rust#1406). The obs-rust#1406
  comment moves with it.
- obs_scene_render_child(): the show transition, the hide transition or
  the source.
- obs_scene_render_linear_srgb().
- obs_scene_texcoords_centered(): are_texcoords_centered(), with its
  memcmp, unchanged.
- obs_scene_render_effect(): the effect, the point sampler, the base
  dimension parameters and Area's upscale, from the scale filter and the
  output scale.
- obs_scene_render_multiplier(): the SDR white multiplier, from the two
  colour spaces and the white level the caller passes.
- obs_scene_render_technique(): the technique.

Each decision is made where the C made it, from the values the C read
there, so every gs_* call, obs_source_video_render(), the texrender
objects and obs_get_video_sdr_white_level() stay in obs-scene.c.h in the
same order:
- gs_texrender_get_format() is still asked only when a texrender is
  there and wanted (format_matches short-circuits as the old condition
  did), and the texrender is destroyed and created as before;
- the source's size is still read only with a texrender, and the child
  choice still asks scene_transition_active() of the show transition
  only when the item is visible and of the hide transition only when it
  is not, once per draw, at the same three places;
- render_item_texture() reads the texture's size, then makes the gs
  calls of the effect plan in the old order (the point sampler, then
  base_dimension and base_dimension_i), then takes the white level at the
  same place as before and passes it to the multiplier. The getter is
  now called for every space pair, not only the six that use it; it is a
  plain read of obs->video with no lock and no side effect.
- obs-scene.c.h maps the effect value to obs->video.* and the technique
  value to its name.

item->user_visible is read once per child choice instead of twice in
the hidden case. It is a plain field read with nothing in between, so
the compiler was already free to merge the two.

The enum values cross as int, so the header needs only
graphics/matrix4.h; obs-scene.c.h checks at compile time that the
mirrored scale filter, blend and colour space values match obs.h and
graphics.h, and that the four enums are int-sized.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header. The exported symbol sets are unchanged (1766).

Refs obs-rust#2003

* test: Characterize the scene item render decisions

test_scene_render_plan_steps compiles libobs/obs-scene-render-plan.c.h
(ASan and UBSan, no libobs core) and calls only its hidden functions,
so it runs on every platform. 16 cases:

- obs_scene_render_texture_enabled(): no trigger (a group, a scene that
  is a group, nothing set), and each trigger alone: every side of the
  crop and the bounds crop at 1, -1, INT_MAX and INT_MIN; every scale
  filter but Disable, values outside the enum included; sRGB off (and
  not another blend method outside the enum); every blend mode but
  Normal, values outside the enum included; a nested scene.
- obs_scene_render_texrender_action(): all 8 rows of has / wanted /
  format matches.
- obs_scene_render_size_empty() (obs-rust#573) and obs_scene_render_clear_white()
  (obs-rust#1406: Darken only).
- obs_scene_render_child(): visible or hidden x show active x hide
  active (8 rows), and obs_scene_render_linear_srgb().
- obs_scene_texcoords_centered(): the identity and whole translations,
  any other entry moved, fractional translations (the next float above
  3, the smallest subnormal), -0.0 and NaN.
- obs_scene_render_effect(): each scale filter at scale 1, up, down a
  little, one axis 1, one up and one down, and below 0.5 (with and
  without Area's upscale); 0.5 and the floats on either side; 1 +/-
  EPSILON (both round away from 1 as floats and are not close; the next
  floats towards 1 are); NaN of either sign and payload, and +/-inf.
- obs_scene_render_multiplier() for the 4 x 4 colour space pairs at SDR
  white levels of 80, 300 and 0, the values written out, and spaces
  outside the enum (1).
- obs_scene_render_technique() for the 4 x 4 pairs, with and without
  upscale, and spaces outside the enum (Draw or DrawUpscale).

Characterized, not endorsed:
- a white level of 0 gives 80.f / 0, +inf (and -0 gives -inf);
- a NaN output scale is never close to 1 and never below 0.5, so the
  filter's own effect is used, and Area does not upscale; a negative
  scale is below 0.5;
- memcmp: -0.0 anywhere but the translation is not centred, while a
  quiet NaN or infinite translation is (floorf gives it back as it is);
- the texrender and child rows libobs never passes (format_matches
  without a texrender, or the other transition active).

It is in the native test graph, in the core suite. It builds with
-Werror against cmocka 2.0.2 and 1.1.7 and uses no assert_in_range. The
real-libobs scene tests are unchanged.

Refs obs-rust#2003

* obs-scene: Add Tier 1 tests for the scene item render decisions port

RED for the port of libobs/obs-scene-render-plan.c.h to a new
render_plan module of rust/obs-scene. The safe core is a stub (todo!()),
one function per hidden C function: texture_enabled() over Triggers,
texrender_action() giving a Texrender (None, Keep, Destroy, Create,
Recreate), size_empty(), clear_white(), child() giving a Child (Source,
Show, Hide), linear_srgb(), texcoords_centered() over obs-graphics'
Matrix4, effect() giving an EffectPlan (an Effect, the point sampler,
the base dimension and the upscale), multiplier() and technique() giving
a Technique, with the mirrored scale filter, blend and colour space
values and EPSILON.

tests/scene/render_plan.rs ports test_scene_render_plan_steps 1:1: its
16 cases under the same names (steps_*), in the same order, with the
same inputs and expectations, the characterized-not-endorsed cases
included.

Edge cases (edge_*): the texrender action's invariants (a texrender is
there afterwards exactly when wanted; destroyed only when there,
created only when wanted, kept only when there, wanted and matching),
the child's (only a visible item draws its show transition, only a
hidden one its hide transition), and the upscaled technique as the
plain one's DrawUpscale variant for spaces -2 to 6; and five proptests
(4000 cases each), with floats drawn from bit patterns (obs-rust#1951): the
triggers against a model over full i32 crops, filters, methods and
types; the effect against a model whose close-to-1 test is written on
the bit patterns; any whole-number translation is centred and any other
entry off the identity's bits is not; a fractional translation is not
centred; and the multiplier is 1, white / 80 or 80 / white, bit for bit,
for any white level.

The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs),
as crop's are.

soldr cargo test -p obs-data --test obs-tests-core scene::render_plan
fails 24 of 24 with "not yet implemented".

Refs obs-rust#2003

* obs-scene: Port the scene item render decisions

Ports libobs/obs-scene-render-plan.c.h, the per-item render decisions
of render_item() and render_item_texture(), to the render_plan module
of rust/obs-scene. render_item() and render_item_texture() stay C in
obs-scene.c.h: every gs_* call, obs_source_video_render(), the texrender
objects, obs_get_video_sdr_white_level() (passed as a value), the blend
table lookup (obs-rust#1521), calc_cx/calc_cy (obs-rust#1947), and the mapping of the
effect and technique values to obs->video.* and the technique names.
The hybrid libobs drops the C file and links the Rust shims in its
place.

Safe core (src/render_plan.rs), one function per hidden C function:
- texture_enabled(): any crop or bounds crop side non-zero, a scale
  filter other than Disable, sRGB off, a blend type other than Normal,
  or a nested scene that is not a group;
- texrender_action(): destroy when there and either not wanted or not in
  the format, then create when wanted and none is left (None, Keep,
  Destroy, Create, Recreate);
- size_empty() (obs-rust#573), clear_white() (obs-rust#1406: Darken), child() (Show for
  a visible item with a running show transition, Hide for a hidden one
  with a running hide transition, else Source) and linear_srgb();
- texcoords_centered(): over obs-graphics' Matrix4, every entry compared
  by its bits against the identity with the x and y translation
  replaced by their floor(), as the C's memcmp, so -0.0 and NaN payloads
  behave as in C;
- effect(): Disable draws as it is, Point sets the point sampler, and
  any other filter at an output scale not within EPSILON of 1 on both
  axes sets the base dimension and picks the low-resolution bilinear
  effect below 0.5 on either axis, else Bicubic, Lanczos or Area (which
  upscales at 1 and over on both axes), else the default effect.
  fabsf(f - 1) <= EPSILON is written the same way in f32, so NaN is
  never close;
- multiplier(): white / 80 for SDR or extended into scRGB, 80 / white
  for scRGB into SDR or extended (+inf for a white level of 0, as in C),
  else 1;
- technique(): the eight techniques, with values outside enum
  gs_color_space drawing as Draw or DrawUpscale.
Values from the item and the colour spaces are i32 and compared as
they are. The module denies clippy's arithmetic, indexing, as, unwrap,
expect, panic, unimplemented and unreachable lints, keeps no state and
takes no lock.

Shim (src/ffi/scene_render_plan.rs): the ten functions with the C
signatures. struct obs_scene_render_triggers and struct
obs_scene_render_effect_plan are mirrored (#[repr(C)]); the triggers and
the matrix (obs-graphics' ffi matrix4) are read through their pointers
and the effect plan written through its pointer. The enums are returned
as the C's values. No item or source pointer crosses.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-scene-render-plan (LIBOBS_PORTED_SOURCES), whose PCH has
only math.h, string.h, graphics/math-defs.h and obs-scene-render-plan.h;
libobs.pch.h keeps the header for obs-scene.c. The functions are not
EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of
rust-exports.map and rust-unexports-macos.txt. libobs-rust already
links the crate's ffi. ci/test_ported_sources.py checks the group on
every host. The exported symbol sets are unchanged and equal (1766 on
the C backend, the hybrid and libobs-c-reference).

test_scene_render_plan_steps still compiles the C file into itself,
unchanged. The new test_scene_render_plan_steps_rust builds the same
test source against libobs_rust-static instead, as
test_source_color_space_steps_rust does: the test calls the hidden
functions directly, so this variant runs the shims, the structs passed
from C included. The real-libobs scene tests see the swap through the
hybrid.

Tier 1 scene::render_plan passes 24/24.

Refs obs-rust#2003

* obs-scene: Check the scene item render decisions port against the C

Oracle (oracle/scene_render_plan.c): the original
obs-scene-render-plan.c.h, unmodified, with its ten functions renamed to
oracle_*, plus the layouts of struct obs_scene_render_triggers, struct
obs_scene_render_effect_plan and struct matrix4, the sizes and values of
the four enums and the mirrored constants (59 values). The moved
texcoords check initializes struct matrix4 with flat lists, which
libobs builds with -Wno-missing-braces; the oracle turns that warning
off around the include, as image_file.c does for its own.

Tier 3 (tests/scene/render_plan_parity.rs, 11 tests), comparing every
output field by field, bit for bit (the bools, the enum values as
integers, the multiplier as f32 bits), with floats and matrices drawn
from bit patterns (obs-rust#1951), so -0.0 and quiet and signalling NaNs of
either sign and payload are covered:
- the layouts and constants against the C header;
- exhaustive: the texrender action and the child (8 rows each); the
  triggers over every zero/non-zero pattern of the eight crops x 11
  scale filters x 6 blend methods x 11 blend types x the two flags
  (743,424 calls); the size check over the u32 edges; the clear colour
  and linear sRGB over every value from -2 to 9 and the ends of i32; the
  technique and the multiplier over every pair of 11 spaces (-2 to 6 and
  the ends of i32) with 26 white levels; the effect over 15 filters and
  every pair of 86 scales from around 0.5, 1 - EPSILON, 1, 1 + EPSILON,
  0, the infinities and the NaNs (110,940 calls); the centred check with
  each of the 16 entries set to 30 special values, and every pair of
  them as the translation;
- proptests (10,000 cases each) over full i32 triggers, any i32 filter
  with any-bit scales biased to the edges, any spaces with any-bit white
  levels, and matrices whose entries are the identity's, whole or
  fractional, or any bits.

The module passed 10 runs in a row with fresh seeds. Rust's f32::floor
and the C's floorf agree on every NaN tried, the signalling ones
included (both quiet them).

Mutation check (done once, not committed): 48 mutants, 40 in the core
and 8 in the shim; 43 fail. 35 of the 40 core mutants fail both Tier 1
(1 to 4 of 24) and Tier 3 (1 or 2 of 11), among them obs-rust#1406 reverted
(never white), the group rule dropped, the format ignored and the
low-resolution edge moved; "round for floor" in the centred check
differs only on a signalling NaN translation and fails Tier 3 alone.
7 of the 8 shim mutants fail Tier 3; Tier 1 does not reach the shims.
Five survive and are equivalent: trunc for floor in the centred check
(both equal x exactly when x is whole), < for <= against EPSILON and the
close-to-1 test in f64 (f - 1 is exact near 1 and never exactly 1e-4f),
the 80 / white division in f64 (double rounding from 53 to 24 bits is
innocuous for division), and the two scale axes swapped in the shim (the
effect is symmetric in x and y). The proptest regression files keep the
seeds the mutants failed on.

Refs obs-rust#2003

* docs: Record the scene item render decisions port

The rust/README.md crate table now lists
libobs/obs-scene-render-plan.c.h among the rust/obs-scene units, and its
port table has a row for it: the ten decisions and where the C makes
them, what stays C (every gs_* call in its order, the renders, the
texrender objects, the white level lookup, the blend table, calc_cx and
calc_cy, and the effect and technique mapping), the mirrored structs and
the compile-time checks, what the characterization pins and the port
keeps, the _rust variant of the steps test, the real-libobs tests that
see the swap and what Tier 3 compares. The crate's description and
lib.rs say what it holds. docs/rust-port/naming.md already maps
libobs/obs-scene*.c.h to rust/obs-scene. No docs/features.md line: no
user-visible change.

port_comparison gets its own bench file,
benches/port_comparison/scene_render_plan.rs, for 16 items: the
per-item decisions of render_item (texture, texrender action, clear
colour, child, linear sRGB), the texture's effect, multiplier and
technique, and the centred check, each folded into a checksum both sides
must match. On x86_64 Linux (GCC, pinned to one core, but on a machine
with a load average near 30, so the numbers are noisy; medians per 16
items, two runs): item 1.09 us / 220 ns Rust against 419 / 408 ns C,
texture 534 / 250 ns against 522 / 145 ns, texcoords_centered 450 /
132 ns against 201 / 98 ns. The gap is at most about 40 ns per item
per frame (the first run), and for the item decisions the order flips
between runs. I have not looked into it further.

Fixes obs-rust#2003
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 item crop arithmetic in obs-scene.c to obs-scene

2 participants