Skip to content

obs-scene: Port the scene item transform steps - #2392

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/item-transform
Oct 25, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/item-transform

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2385

What and why

Ports the scene item transform composition of compute_item_transform() in libobs/obs-scene.c.h to rust/obs-scene, as a new module item_transform. Every scene item runs it whenever its transform is updated (a setter, a source that changes size, a nested scene that resizes), and its outputs place every source on every canvas: draw_transform, box_transform, output_scale, box_scale, the item's crop, bounds crop and last size.

The composition moves to libobs/obs-scene-item-transform.c.h as four hidden steps that obs-scene.c.h calls in order:

  • obs_scene_item_transform_prepare(): the bounds crop reset; a nested scene's crop rescaled by its size change (update_nested_scene_crop(): both last sizes non-zero, (float)width / (float)last_width, obs_scene_scale_crop()); the cropped size (obs_scene_cropped_size(), as calc_cx()/calc_cy() take it); the last size;
  • obs_scene_item_transform_bounded(): bounds_type != OBS_BOUNDS_NONE;
  • obs_scene_item_transform_matrix(): the draw and the box transform, the same matrix4_identity(), matrix4_scale3f(), matrix4_translate3f(), matrix4_rotate_aa4f(RAD(rot)) and matrix4_translate3f() in the same order;
  • obs_scene_item_transform_box_scale(): scale.x *= (float)width, in float, for an item without bounds.

The plan sketched separate draw() and box() matrices; the C builds both with the same five calls, so there is one matrix(), called twice.

What stays C, where it was.

  • Every get_scene_dimensions() read (the main canvas mix without a lock, a group's canvas, or the parent scene's size): item_canvas_scale() and pos_to_absolute() after prepare, size_to_absolute() in calculate_bounds_data(), and size_to_absolute() again for the box scale, after the draw matrix. None is hoisted or merged, so a concurrent obs_reset_video() sees the same reads as before (the obs-source: Port the deinterlace draw decisions #2153 pattern).
  • All six float-to-integer casts: the two (uint32_t)((float)cx * scale.x) of an item without bounds and the four (int) passed to add_alignment(). A negative float cast to uint32_t (a flipped item, R7-3) wraps on x86-64 and saturates on arm64, and a float past INT_MAX cast to int differs the same way; Rust's as saturates. So no float becomes an integer in the Rust, and each architecture keeps the C's result. R7-3 itself is unchanged and can be decided later as a C change in the same place.
  • calculate_bounds_data() (the bounds read, then the already-ported obs_bounds_place()), add_alignment(), the DEBUG_TRANSFORM log, calc_cx()/calc_cy() for render_item(), and update_item_transform() (the defer check, the source size read, the parent's video lock, the item_transform signal).

#2281 / #2307: a bounds type outside the enum is OBS_BOUNDS_NONE before this code runs. The characterization pins that bounded() treats any non-zero int as bounded (characterized, not endorsed) and points at test_scene_bounds_type_range, which pins the clamp through every way a bounds type comes in.

Floats are bit-equal to the C. The crop factors are one f32 division of the sizes converted as (float)uint32_t is; the box scale is one f32 multiply per axis; RAD is the C's float constant and one multiply. The matrices use the C's operations in the C's order: matrix4_mul and vec4_dot are group_transform's (the SSE sum order, as obs-graphics' and the hybrid libobs's matrix4_*), and quat_from_axisang() and matrix4_from_quat() are written out with their operations in order, sinf/cosf called as obs-graphics calls them on each host (separate non-inlined calls on macOS, where LLVM would pair them). There is no f64 intermediate and Rust never fuses a multiply and an add. Checked on x86_64 Linux, and the Windows MSVC clippy and cross lanes (and the aarch64-apple-darwin cross lane) build it.

Why the matrix math is written out, not called from obs-graphics. The first GREEN called obs-graphics' Matrix4 directly. That put obs-graphics' texture render code, with its gs_* references, into the obs-scene archive member, and test_scene_audio_window_steps_rust and test_scene_render_plan_steps_rust, which link libobs_rust-static alone, stopped linking. group_transform and extent write their math out for the same reason; this port reuses group_transform's matrix_mul and dot (now pub(crate)). Tier 1 checks matrix() against obs-graphics' Matrix4 chain bit for bit (a proptest), and Tier 3 against the C.

Six commits:

  1. Move (behaviour-neutral, ff968551e): the steps go to libobs/obs-scene-item-transform.c.h, declared in the new obs-scene-item-transform.h without EXPORT, so they are hidden. The file joins libobs.core-source with libobs.pch.h.

    The move was checked against the original code. A scratch harness (not committed) compiles origin/master's compute_item_transform() and its helpers verbatim next to this branch's and the moved file, on a stand-in item:

    • a script rewrites every item field dereference into a logging accessor, and get_scene_dimensions() into a stub that hands out a fresh value from a seeded stream on every read, so a hoisted or merged read shows;
    • --wrap logs every call out of the composition with its arguments and results as bits: matrix4_scale(), matrix4_translate3v(), matrix4_rotate_aa(), add_alignment(), obs_bounds_place(), obs_scene_cropped_size(), obs_scene_scale_crop() and the three coordinate conversions, with the set of item fields dereferenced between two calls;
    • random items: crops, sizes and last sizes at the int and uint32_t ends and anywhere, any float bits or item-sized values for the position, scale, rotation and bounds, every bounds type, any alignment, all flags, garbage in the outputs.

    150,000,000 random items: 50,000,000 each with GCC and Clang at -O2, 20,000,000 each at -O0, and 5,000,000 each under ASan and UBSan (GCC -O0, Clang -O2). All 6,060,775,041 events match, as do the final item states, after two normalizations:

    • the moved prepare reads last_width even when last_height is 0 (the old short-circuit did not);
    • item->rot is passed to the matrix step, so it is read before matrix4_scale() instead of just before matrix4_rotate_aa(); the matrix calls in between do not touch the item.

    15 deliberately broken moves were tried; 14 fail the harness (the guard on last_width dropped, the x factor from the height, the bounds crop not reset, the last size set before the nested crop, the nested flag ignored, bounded for STRETCH only, the box scale in double or by the height, RAD() in double, the rotation before the origin, the box scale's dimension read hoisted above the draw matrix, a saturating cast as Rust's as would give, the base origin or the box scale from the cast size). The one that passes is equivalent: bounds_type > OBS_BOUNDS_NONE.

  2. Characterization (e8268338f): test_scene_item_transform_steps, 13 cases. It compiles the moved file (ASan and UBSan) with the hidden crop, coordinate and bounds files it is composed with, and calls the four steps directly and through compose(), which runs them in compute_item_transform()'s order with what obs-scene.c keeps between them. It pins:

    • the layout, the bounds type and alignment values and RAD(1);
    • prepare: the bounds crop zeroed from any value; the cropped size with no crop, a crop on each side, all of it (0), more (2), a negative sum (2) and two INT_MAX crops of UINT32_MAX; the last size;
    • the nested crop: kept with last_width 0 and with last_height 0; doubled, the same size, two thirds (truncated), a factor per axis, to 0 x 0; a huge factor (INT_MAX) and a negative crop scaled negative (characterized, not endorsed); sizes past 2^24;
    • bounded: false for NONE only; 7, -1, INT_MAX and INT_MIN bounded (characterized, not endorsed; bug: libobs: a scene item bounds type outside the enum is kept and draws as an unknown box #2281 makes them NONE first);
    • the box scale as float bits (one rounding, sizes past 2^24, signed zeros, infinities, a NaN payload);
    • the matrix at rotation 0 and -0 bit for bit; RAD() of 0, 90, 45, -30, 720, 180, 360 and -90; the matrix against the same matrix4 calls in the C's order, and unequal to the rotation done before the origin;
    • the composition for every alignment (9 values and two with extra bits), each scale sign combination with each alignment, each bounds type with crop to bounds off and on, upright and flipped, relative coordinates on two canvases and a group's child, and the nested scene crop (last_width 0, a size change, no update_tex, not a scene, the bounds crop reset between two compositions);
    • a hash of every output over all alignments x bounds types x crop x absolute/relative x scale signs (1,232 compositions) at 0 and -0.

    Flipped items without bounds are pinned through x86-64's float-to-uint32_t cast, done in defined C ((uint32_t)(int64_t)f), so the pins are the same on every architecture; obs-scene.c keeps the cast. Only sinf()/cosf() are library calls, exact at 0; the rotated matrix bits and the sweeps at 90, 45, -30 and 720 are pinned only with glibc on x86-64. It is in the test graph, sorted, with its headers as watch_inputs.

  3. RED (b8200e272): Tier 1 against todo!(). It ports the 13 C cases 1:1 and adds 8 edge tests: the guard with each last size 0 or not, the bounds crop always zeroed, the box scale's signed zeros, NaN and overflow, the rotation-0 lanes, and four proptests (4,000 cases each): prepare() against a model, matrix() against obs-graphics' Matrix4 chain, box_scale() as one f32 multiply over any bits, bounded() over any i32.

  4. GREEN (8a94021e7): the safe core and four #[no_mangle] shims, in a new libobs.ported-scene-item-transform group whose PCH is string.h, obs.h, the graphics headers, obs-scene-crop.h and the new header.

    • The commit updates source-sets.json, the unity lists, the three export lists and ci/test_ported_sources.py, each in sorted place.
    • It adds test_scene_item_transform_steps_rust, which links OBS::libobs (for matrix4_*) and libobs_rust-static.
  5. Tier 3 (6a151854e): a C oracle of the unchanged file (its matrix4_*, quat and crop calls are the C graphics and crop oracles), the parity tests and a mutation check.

  6. Docs (42c88cec9): the README rows, the crate's description, lib.rs and a port_comparison bench file. docs/rust-port/naming.md already maps libobs/obs-scene*.c.h to rust/obs-scene. The port changes nothing a user can see, so docs/features.md has no line.

RED → GREEN

  • RED commit: b8200e272. soldr cargo test -p obs-data --test obs-tests-core scene::item_transform fails 20 of 21 with "not yet implemented"; steps_layout, which calls no stub, passes. uv run ci/local_gate.py --test-diff origin/master on it: 4,066 tests, 4,046 passed, 20 failed (exactly those), 6 skipped; --lane lint passes.
  • GREEN: 8a94021e7. Tier 1 21/21. test_scene_item_transform_steps 13/13 on the C backend, the hybrid and libobs-c-reference; test_scene_item_transform_steps_rust 13/13 against the shims (hybrid and libobs-c-reference).
  • Tier 3 (6a151854e): 9/9, every float by its bits (a NaN by its payload and sign, -0 apart from +0):
    • the layout against the C header;
    • exhaustive over the flags: prepare over nested and 13 x 4 last sizes (0 and not), 8 special crops and 5 sizes; bounded over -2..=8 and the int ends;
    • the matrix over 16 special values in every pair at 10 rotations (0, -0, 90, 45, -30, 720, 180, 1e30, NaN, inf); the box scale over 9 special values in every pair and 4 sizes;
    • proptests (20,000 cases each): prepare over any crops and sizes, the matrix over finite floats (any finite bit pattern, item-sized and usual values), the box scale over any bits; and (2,000 cases, 448 compositions each) compute_item_transform()'s sequence with the shims on one side and the oracle on the other, exhaustive over the six flags (absolute, update_tex, nested scene, group, parent group, crop to bounds) and the seven bounds types, over random items, alignments, sizes and four canvases.
  • Mutation check (once, not committed): 37 mutants, 31 in the core and 6 in the shim. 31 are killed, all by Tier 3; the table is in the Tier 3 commit message. The six that survive are equivalent: the guard's two tests in either order; bounded through BoundsType; the two factors in either order; (1 - yy) - zz (equal when yy is +0, as for the z axis, the only one the scene rotates about); a zero axis lane left unmultiplied (its sign reaches only lanes the next multiply adds to +0); and an f32 product computed in f64 (one rounding of an exact product is the f32 product).
  • Real app: not applicable (no UI, protocol or log change). Every item's placement goes through this code; the real-libobs test_scene_group_transform, test_scene_bounds_place(_rust), test_scene_bounds_fit, test_scene_bounds_type_range, test_scene_coords, test_transform_clipboard, test_scene_rotated_scale_filter, test_scene_item_crop(_load), test_scene_duplicate_scale_ref, test_group_child_drift, test_group_extent and test_group_empty_bounds see the swap through the hybrid.

Checks on 42c88cec9 (rebased on origin/master d08b04fb0; the libobs builds are of the same library sources):

  • uv run ci/local_gate.py --lane lint, --test-diff origin/master (workspace scope: 4,075 tests passed, 6 skipped) and --lane cross (x86_64-pc-windows-msvc and aarch64-apple-darwin clippy, dylint and test links): all pass.
  • soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets --locked -- -D warnings: clean.
  • soldr cargo test -p obs-c-oracle --locked --bench port_comparison: passes, including scene_item_transform/update_16_items (fixtures equal before timing). On x86_64 Linux, pinned to one core: 2.1 µs Rust against 3.3 µs C per 16-item update.
  • Exported symbols: identical sets on the C backend, the hybrid and libobs-c-reference (1,766); the four functions are local in all three, and the ported group's object is built only into libobs-c-reference in the hybrid tree.
  • Full ctest under a private Xvfb and D-Bus session: C backend 511/511, hybrid 538/538, libobs-c-reference 538/538 (the same three host-dependent tests skip in each: test_pulse_default_follow, test_v4l2_vivid_replug, test_v4l2_vivid_stall). No flakes; nothing was re-run.
  • No suite size is pinned in rust/obs-build (the partitions must cover the graph); the new test is in the core suite.

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
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): none

Behaviour-neutral extraction before the Rust port. The composition of
compute_item_transform() in obs-scene.c.h moves to
libobs/obs-scene-item-transform.c.h, as four steps on plain values
declared in the new libobs/obs-scene-item-transform.h without EXPORT
(hidden, as -fvisibility=hidden keeps them):

- obs_scene_item_transform_prepare(): the bounds crop reset, a nested
  scene's crop rescaled by its size change (update_nested_scene_crop(),
  moved verbatim: the guard on both last sizes, (float)width /
  (float)last_width, obs_scene_scale_crop()), the cropped size
  (obs_scene_cropped_size(), as calc_cx() and calc_cy() take it) and the
  last size;
- obs_scene_item_transform_bounded(): bounds_type != OBS_BOUNDS_NONE;
- obs_scene_item_transform_matrix(): draw_transform and box_transform,
  the same matrix4_identity(), matrix4_scale3f(), matrix4_translate3f(),
  matrix4_rotate_aa4f(RAD(rot)) and matrix4_translate3f() in the same
  order;
- obs_scene_item_transform_box_scale(): scale.x *= (float)width, in
  float, for an item without bounds.

obs-scene.c.h keeps everything that reads or casts, between the steps:
- every get_scene_dimensions() read where it was: item_canvas_scale()
  and pos_to_absolute() after prepare, size_to_absolute() in
  calculate_bounds_data(), and size_to_absolute() again for the box
  scale, after the draw matrix. A concurrent obs_reset_video() sees the
  same reads as before;
- calculate_bounds_data(): the bounds read, then the ported
  obs_bounds_place();
- add_alignment() and all six float-to-integer casts: the two
  (uint32_t)((float)cx * scale.x) of an item without bounds and the four
  (int) passed to add_alignment(). A negative float cast to uint32_t
  (a flipped item, R7-3) wraps on x86-64 and saturates on arm64, so the
  casts stay where they were and each architecture keeps its result;
- the DEBUG_TRANSFORM logging; update_item_transform() (the defer
  check, the source size read, the parent's video lock, the signal).
calc_cx() and calc_cy() stay for render_item().

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header; source-sets.json lists it. docs/rust-port/naming.md
already maps libobs/obs-scene*.c.h to rust/obs-scene. The exported
symbol sets are unchanged (1766 on the C backend, the hybrid and
libobs-c-reference).

Checked in a scratch harness (not committed) that compiles
origin/master's compute_item_transform() and its helpers verbatim next
to this branch's and the moved file, on a stand-in item: a script
rewrites every item field dereference into a logging accessor and
get_scene_dimensions() into a stub that hands out a fresh value from a
seeded stream on every read (so a hoisted or merged read shows), and
--wrap logs every call out of the composition with its arguments and
results as bits (matrix4_scale(), matrix4_translate3v(),
matrix4_rotate_aa(), add_alignment(), obs_bounds_place(),
obs_scene_cropped_size(), obs_scene_scale_crop() and the three
coordinate conversions), with the set of item fields dereferenced
between two calls. Random items: crops, sizes and last sizes at the
int and uint32_t ends and anywhere, any float bits or item-sized
values for the position, scale, rotation and bounds, every bounds
type, any alignment, all flags, garbage in the outputs.
150,000,000 random items (50,000,000 each with GCC and Clang at -O2,
20,000,000 each at -O0, and 5,000,000 each under ASan and UBSan, GCC
-O0 and Clang -O2): all 6,060,775,041 events match, as do the final
item states.

Two normalizations: the moved prepare reads last_width even when
last_height is 0 (the old short-circuit did not), and item->rot is
passed to the matrix step, so it is read before matrix4_scale() rather
than just before matrix4_rotate_aa(); the matrix calls in between do
not touch the item.

15 deliberately broken moves were tried; 14 fail the harness: the
guard on last_width dropped, the x factor from the height, the bounds
crop not reset, the last size set before the nested crop, the nested
flag ignored, bounded for STRETCH only, the box scale in double or by
the height, RAD() in double, the rotation before the origin, the box
scale read hoisted above the draw matrix, a saturating cast (as Rust's
as), the base origin from the cast size, the box scale from the cast
size. The one that passes is equivalent: bounded as bounds_type >
OBS_BOUNDS_NONE.

Refs obs-rust#2385
test_scene_item_transform_steps compiles
libobs/obs-scene-item-transform.c.h into itself (ASan and UBSan), with
the hidden crop, coordinate and bounds files it is composed with
(obs-scene-crop.c.h, obs-scene-coords.c.h, obs-scene-bounds.c.h,
obs-scene-bounds-place.c.h); matrix4_* come from libobs. It calls the
four steps directly, and through compose(), which runs them in
compute_item_transform()'s order with what obs-scene.c keeps between
them: the canvas size (given), the coordinate conversions,
obs_bounds_place(), add_alignment() and the casts. 13 cases pin:

- the struct layout, the bounds type and alignment values and RAD(1);
- prepare: the bounds crop zeroed from any value; the cropped size
  with no crop, a crop on each side, all of it (0), more (2), a
  negative sum (2) and two INT_MAX crops of UINT32_MAX (64-bit sum);
  the last size; a crop kept when not nested;
- the nested crop: kept with last_width 0 and with last_height 0;
  doubled, the same size, two thirds (truncated toward 0), a factor
  per axis, to 0 x 0; a huge factor (INT_MAX) and a negative crop
  scaled negative (characterized, not endorsed); sizes past 2^24
  rounding to the same float;
- bounded: false for NONE only; 7, -1, INT_MAX and INT_MIN are bounded
  (characterized, not endorsed: obs-rust#2281 makes them NONE before this
  runs, test_scene_bounds_type_range pins that);
- the box scale as float bits: one rounding, sizes past 2^24, signed
  zeros, infinities and a NaN payload;
- the matrix at rotation 0 and -0 bit for bit (identity, scaled,
  flipped); RAD() of 0, 90, 45, -30, 720, 180, 360 and -90; the matrix
  against the same matrix4 calls in the C's order, and unequal to the
  rotation done before the origin;
- the composition for every alignment (9 values and two with extra
  bits), each scale sign combination with each alignment, each bounds
  type with crop to bounds off and on, upright and flipped, relative
  coordinates on two canvases and a group's child, and the nested
  scene crop: last_width 0, a size change, no update_tex, not a scene,
  and the bounds crop reset between two compositions;
- a hash of every output over all alignments x bounds types x crop x
  absolute/relative x scale signs (1232 compositions), at 0 and -0.

Flipped items without bounds are pinned through x86-64's
float-to-uint32_t cast, done in defined C ((uint32_t)(int64_t)f), so
the pins are the same on every architecture; obs-scene.c keeps the cast
itself (on arm64 such an item sits elsewhere, R7-3). Only sinf() and
cosf() are library calls, exact at 0: the bits of rotated matrices and
the sweeps at 90, 45, -30 and 720 are pinned only with glibc on x86-64,
where they were taken.

It is in the native test graph in name order, with libm except on MSVC
and the headers it reads as watch_inputs, in the core suite (no suite
size is pinned). It reports failures with fprintf(stderr, ...) and
fail(), not fail_msg(), and uses no assert_in_range. It passes 13/13 on
the C backend, and on the hybrid and libobs-c-reference (where
matrix4_* is the Rust graphics port). The real-libobs scene, group,
bounds, crop and clipboard tests are unchanged.

Refs obs-rust#2385
Failing tests for the port of libobs/obs-scene-item-transform.c.h to a
new item_transform module of rust/obs-scene. The safe core is a stub
(todo!()), one function per hidden C function: prepare() giving a
Prepared (the crop, the zeroed bounds crop, the last size and the
cropped size), bounded() on the C's int, matrix() giving an
obs-graphics Matrix4, and box_scale(); RAD is the C's float constant.

The plan sketched one draw() and one box() matrix; the C makes both
with the same five calls, so there is one matrix(), called twice, as
obs-scene.c.h calls obs_scene_item_transform_matrix() twice.

tests/scene/item_transform.rs ports test_scene_item_transform_steps
1:1: its 13 cases under the same names (steps_*), in the same order,
with the same inputs and pins. compose() runs the steps in
compute_item_transform()'s order with the ported coordinate, bounds
placement and alignment functions between them, and x86-64's cast
through i64, as the C test does; the glibc x86-64 pins are under the
same cfg.

Edge cases (edge_*): the guard with each last size 0 or not, nested or
not; the bounds crop zeroed in every case; the box scale's signed zeros,
NaN and overflow; the rotation-0 matrix lanes; and four proptests (4000
cases each): prepare() against a model of update_nested_scene_crop()
and calc_cx()/calc_cy() over any crops and sizes, matrix() against the
same Matrix4 calls in the C's order over item-sized floats, box_scale()
as one f32 multiply over any bits, and bounded() over any i32.

The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs),
as the other obs-scene units' are.

soldr cargo test -p obs-data --test obs-tests-core scene::item_transform
fails 20 of 21 with "not yet implemented"; steps_layout, which calls no
stub, passes.

Refs obs-rust#2385
Ports libobs/obs-scene-item-transform.c.h, the composition of
compute_item_transform(), to the item_transform module of
rust/obs-scene. compute_item_transform() stays C in obs-scene.c.h:
every get_scene_dimensions() read where it was (none hoisted),
calculate_bounds_data() and the ported obs_bounds_place(),
add_alignment(), all six float-to-integer casts, the DEBUG_TRANSFORM
log and update_item_transform() (the defer check, the source size
read, the parent's video lock, the signal). The hybrid libobs drops
the C file and links the Rust shims in its place.

Safe core (src/item_transform.rs), one function per hidden C function:
- prepare(): the bounds crop zeroed; when nested and neither last size
  is 0, each crop times (width as f32) / (last_width as f32) through
  crop::scale_crop(); the cropped size through crop::cropped_size()
  with the zeroed bounds crop; the last size set to the source size;
- bounded(): bounds_type != 0 (OBS_BOUNDS_NONE), on the C's int;
- matrix(): the identity, scaled by (x, y, 1), translated by minus the
  origin, rotated by rot * RAD about (0, 0, 1), translated by the
  position, each a post-multiply as matrix4_mul(dst, m, &temp) does;
- box_scale(): scale * (size as f32), one f32 multiply per axis.

The matrices are bit-equal to the C's: matrix4_mul and vec4_dot are
group_transform's (the SSE sum order, as obs-graphics' and the hybrid
libobs's matrix4_*), now pub(crate), and quat_from_axisang() and
matrix4_from_quat() are written out in item_transform with the C's
operations in their order (halfa = w * 0.5, each axis lane times the
sine, the norm as quat_dot, 2 / norm, 1 - (yy + zz), ...). The plan
was to call obs-graphics' Matrix4 directly; that put obs-graphics'
texture render code, with its gs_* references, into the obs-scene
member, and test_scene_audio_window_steps_rust and
test_scene_render_plan_steps_rust, which link libobs_rust-static alone,
stopped linking. group_transform and extent write their math out for
the same reason. sinf() and cosf() go through a new platform::sin_cos,
as obs-graphics calls them on each host (separate non-inlined calls on
macOS, where LLVM would pair them into __sincosf_stret). There is no
f64 intermediate and Rust never fuses a multiply and an add, so this
holds on x86-64, arm64 and the MSVC targets. No float becomes an
integer anywhere in the module. It denies clippy's arithmetic,
indexing, unwrap, expect, panic, unimplemented and unreachable lints,
allocates nothing, keeps no state and takes no lock.

Shim (src/ffi/scene_item_transform.rs): the four functions with the C
signatures. The crops cross as struct obs_sceneitem_crop
(bounds_place's SceneItemCrop), the size as struct
obs_scene_item_transform_size by value, the matrix and vectors as
obs-graphics' matrix4 and vec2. Each shim reads all its inputs before
it writes.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-scene-item-transform (LIBOBS_PORTED_SOURCES), whose PCH
has only string.h, obs.h, the graphics headers it uses,
obs-scene-crop.h and obs-scene-item-transform.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); the four are local in all three, and the
group's object is built only for libobs-c-reference in the hybrid
tree.

test_scene_item_transform_steps still compiles the C file into itself,
unchanged. The new test_scene_item_transform_steps_rust builds the same
test source against OBS::libobs (for matrix4_*) and libobs_rust-static
(the four shims and the hidden crop, coordinate, bounds and alignment
shims), with libm except on MSVC. It passes 13/13 against the shims on
the hybrid and on libobs-c-reference. The real-libobs scene, group,
bounds, crop and clipboard tests see the swap through the hybrid.

Tier 1 scene::item_transform passes 21/21.

Refs obs-rust#2385
Oracle (oracle/scene_item_transform.c): the original
obs-scene-item-transform.c.h, unmodified, with its four functions
renamed to oracle_*; its matrix4_*, quat and axisang calls resolve to
the graphics oracle (graphics_math_names.h) and its crop calls to the
crop oracle, so the C side is all C. It also reports the size,
alignment and offsets of struct obs_scene_item_transform_size,
OBS_BOUNDS_NONE and the enum's size (6 values).

Tier 3 (tests/scene/item_transform_parity.rs, 9 tests), every float by
its bits (a NaN by its payload and sign, -0 apart from +0):
- the layout against the C header;
- exhaustive over the flags: prepare() over nested and 13 last widths x
  4 last heights (0 and not), 8 special crops and 5 sizes; bounded()
  over -2..=8 and the int ends;
- the matrix over 16 special values (signed zeros, the float ends, a
  subnormal, infinities, NaNs) in every pair, at 10 rotations (0, -0,
  90, 45, -30, 720, 180, 1e30, NaN, inf); the box scale over 9 special
  values in every pair and 4 sizes;
- proptests (20,000 cases each): prepare() over any crops and sizes;
  the matrix over finite floats (any finite bit pattern, item-sized
  values and the usual ones) for every input; the box scale over any
  bits; and (2,000 cases, 448 compositions each)
  compute_item_transform()'s sequence with the shims on one side and
  the oracle on the other, exhaustive over the six flags (absolute,
  update_tex, nested scene, group, parent group, crop to bounds) and the
  seven bounds types, over random items, alignments, bounds alignments,
  sizes and four canvases. Both sides get the same glue (the ported
  coordinate, bounds placement and alignment functions, x86-64's cast in
  i64), so only the four steps differ.

Tier 1 and Tier 3 pass (21/21 and 9/9); Tier 3 takes 5 s.

Mutation check (done once, not committed): 37 mutants, 31 in the core
(three in the written-out rotation, one in group_transform's dot) and 6
in the shim; failures per mutant (Tier 1 has 21 tests, Tier 3 9).

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| prepare: bounds crop kept from the crop | 3 | 3 |
| nested: guard drops last_width | 3 | 3 |
| nested: guard needs both 0 | 3 | 3 |
| prepare: nested flag ignored | 4 | 3 |
| prepare: never nested | 4 | 3 |
| nested: x factor from the height | 2 | 3 |
| nested: factor inverted | 4 | 3 |
| nested: factor in f64 | 1 | 2 |
| prepare: last size kept | 6 | 3 |
| prepare: cx from top and bottom | 5 | 3 |
| bounded: > 0 | 2 | 1 |
| bounded: always | 7 | 2 |
| matrix: z scale 0 | 5 | 3 |
| matrix: origin added | 9 | 3 |
| matrix: rotation before the origin | 3 | 3 |
| matrix: pre-multiplied position | 9 | 3 |
| rad: in f64 | 1 | 2 |
| axis: no half angle | 3 | 3 |
| axis: sine and cosine swapped | 9 | 3 |
| axis: sin_cos paired in f64 | 1 | 2 |
| axis: 2 / norm skipped | 3 | 3 |
| axis: 1 - yy - zz | 0 | 0 |
| axis: 0 axis lanes not multiplied | 0 | 0 |
| axis: rotation transposed | 3 | 3 |
| translation: w 0 | 9 | 3 |
| dot: x + y + z + w | 0 | 1 |
| box scale: height on x | 8 | 3 |
| box scale: in f64 | 0 | 0 |
| shim: bounds crop not written | 0 | 3 |
| shim: last sizes swapped | 0 | 3 |
| shim: cx and cy swapped | 0 | 3 |
| shim: origin and position swapped | 0 | 3 |
| shim: box scale size swapped | 0 | 3 |
| shim: bounded inverted | 0 | 2 |
| equivalent: guard order swapped | 0 | 0 |
| equivalent: bounded through BoundsType | 0 | 0 |
| equivalent: y factor computed first | 0 | 0 |

31 are killed, all by Tier 3. The six that survive are equivalent: the
guard's two tests in either order; any non-zero int is a BoundsType
other than None; the two factors are independent; (1 - yy) - zz is
1 - (yy + zz) when yy is +0, as it is for the z axis, the only one the
scene rotates about; a zero axis lane's sign reaches only lanes the
next multiply adds to +0; and an f32 product computed in f64 and
rounded once is the f32 product (the size is already a float). Tier 1
does not reach the shims; test_scene_item_transform_steps_rust and
Tier 3 do. The proptest regression files keep the seeds the mutants
failed on.

Refs obs-rust#2385
The rust/README.md crate table now lists
libobs/obs-scene-item-transform.c.h among the rust/obs-scene units, and
its port table has a row for it, after the item names: the four steps,
what stays C (every get_scene_dimensions() read where it was,
calculate_bounds_data(), add_alignment(), the six float-to-integer
casts and why, update_item_transform()), how values cross, why the
matrix math is written out in the crate and not called from
obs-graphics (the _rust tests that link the staticlib alone), 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_item_transform.rs, in the sorted modules!
list: one transform update of 16 items (half of them bounded, rotations
from 0 to 337.5 degrees), each running prepare, the bounds test, both
matrices and the box scale as compute_item_transform() does, folded into
a checksum both sides must match (checked before timing). It allocates
nothing, so the leak check of obs-rust#2159 holds. On x86_64 Linux (GCC, pinned
to one core; medians of two runs): 2.1 us Rust against 3.3 us C per
16-item update.

Fixes obs-rust#2385
@rdoupe
rdoupe requested a review from zackees as a code owner October 25, 2026 12:35

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed: the four hidden steps are listed the same way in rust-exports.map, rust-hidden.txt and rust-unexports-macos.txt (not EXPORT, as with #2153). The macOS sin/cos split sits in src/platform/ behind a single cfg_select! and copies obs-graphics' existing fix for the __sincosf_stret ULP difference. The scene-dimension reads stay in C. The PR has the Tier 2 cmocka test and the Tier 3 oracle parity tests. LGTM.

@zackees
zackees enabled auto-merge October 25, 2026 12:42
@zackees
zackees added this pull request to the merge queue Oct 25, 2026
Merged via the queue into obs-rust:master with commit 38deb4d Oct 25, 2026
37 checks passed
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In review to Done in obs-rust Multiview Oct 25, 2026
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 transform composition in obs-scene.c to obs-scene

2 participants