Skip to content

rust: Port graphics/math-extra.c to obs-graphics - #352

Merged
zackees merged 3 commits into
obs-rust:masterfrom
nathan-v:port/math-extra
Oct 12, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
nathan-v:port/math-extra

Conversation

@nathan-v

Copy link
Copy Markdown
Contributor

Fixes #351. Refs #45.

Ports libobs/graphics/math-extra.c to obs-graphics. Header-inline get_percentage and get_percentagei stay C. ENABLE_RUST_LIBOBS drops graphics/math-extra.c. Exports are in libobs/cmake/rust-exports.d/obs-graphics-math_extra.txt. No new public struct, so there is no layout test. vec2 and vec3 layouts are already tested.

Commits

  1. 2c32c8e9a characterization: test/cmocka/test_math_extra.c. It does not read w after polar_to_cart or a non-zero cart_to_polar (that lane is uninitialized in C).
  2. e99656444 RED. test_polar_to_cart_plus_z fails because polar_to_cart returns a zero vector: left (0, 0, 0), right (0, 0, 2). 10 of 12 Tier 1 tests fail. test_polar_to_cart_zero_radius and test_cart_to_polar_within_epsilon pass because the empty result is also the right one.
  3. c15b11e7f GREEN.

What passed here

  • Tier 1: 12 math_extra tests.
  • Tier 3: 8 math_extra_parity tests, 1024 cases, bit-exact (NaN equals NaN). xyz only for polar_to_cart and cart_to_polar.
  • export_list (3 tests). vec3 rand_matches_c_oracle still passes now that rand_float is the Rust shim.
  • soldr cargo test --workspace --locked passed.
  • soldr cargo clippy -p obs-graphics -p obs-c-oracle --all-targets --locked -- -D warnings passed.
  • Mutation: making vec3_close an absolute comparison made test_calc_torque_one_sided_close_copies_target fail (left (2, 1, 4, 0), right (4, 2, 8, 5)). That break was reverted.

Intentional differences

  • polar_to_cart and cart_to_polar set w to 0. C leaves that lane uninitialized except when cart_to_polar zeros a length within EPSILON.
  • polar_to_norm does not write w.
  • vec3_close is one-sided (v1 - v2 < EPSILON on each of xyz), so calc_torque copies v2 for some far-apart pairs. The shim stores the scaled step and then re-reads v1, so dst == v1 matches C ((10, 0, 0) stepped halfway becomes (-10, 0, 0)).
  • rand_float calls the C library rand / RAND_MAX. Any non-zero positive_only takes the [0, 1] branch.

Not run

Docker is not installed, so Tier 2 cmocka (./unittest) and the local-gate stamp were not run. This does not claim CI is green. Please add the claimed label on #351. If this branch is behind master at review, rebase onto master and stamp.

Pins the current exports of graphics/math-extra.c before the Rust port.
Header-inline helpers stay C. Tests do not read the uninitialized w lane
from polar_to_cart or a non-zero cart_to_polar.

Refs obs-rust#351
test_polar_to_cart_plus_z fails because polar_to_cart returns a zero
vector: left (0, 0, 0), right (0, 0, 2). 10 of 12 Tier 1 tests fail.
test_polar_to_cart_zero_radius and test_cart_to_polar_within_epsilon
pass because the empty result is also the right one.

Refs obs-rust#351
@nathan-v
nathan-v requested a review from zackees as a code owner October 12, 2026 09:15
Safe core, C shims, Tier 3 parity, and the ENABLE_RUST_LIBOBS guard.
Header-inline helpers stay C. Exports are in
libobs/cmake/rust-exports.d/obs-graphics-math_extra.txt.
The oracle no longer defines the unrenamed rand_float; the shim does.

Refs obs-rust#351

Local-Gate: v1 tree=5fe221c580d30315c45db536a88de063a2880649 secs=107 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791797032,"gate":"rust/all/fmt","host":"linux-x86_64","key":"55e953d2fba6a439a344d276a7a0c040af6d2347aeb328dd3f38ad2c143eed9f","lane":"lint","parents":["e996564448f2ceb991f0e633d3ff193297d1ba42"],"secs":16,"stamp":"0fddddeadd811f0a58d78c33ba12ebc0","tree":"5fe221c580d30315c45db536a88de063a2880649","v":1,"via":"run"}
Ci-Attestation: {"at":1791797032,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"55e953d2fba6a439a344d276a7a0c040af6d2347aeb328dd3f38ad2c143eed9f","lane":"lint","parents":["e996564448f2ceb991f0e633d3ff193297d1ba42"],"secs":16,"stamp":"c2453111d2364ca577eeebf8e570e900","tree":"5fe221c580d30315c45db536a88de063a2880649","v":1,"via":"run"}
Ci-Attestation: {"at":1791797032,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"5abaf406b47699f9aa8d3f767cc7bdee63998e0cee4fb38b96ae7c8832566631","lane":"rust-test","parents":["e996564448f2ceb991f0e633d3ff193297d1ba42"],"secs":47,"stamp":"7ae5a90bbfc6ba6bc177ae47917e75c8","tree":"5fe221c580d30315c45db536a88de063a2880649","v":1,"via":"run"}
Ci-Attestation: {"at":1791797032,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"4f27af4194ed261416a9812baca8846fe9136c1889d6fa9604afe8003e799508","lane":"tier2-linux","parents":["e996564448f2ceb991f0e633d3ff193297d1ba42"],"secs":44,"stamp":"feb537950e7720b242360b51ff2b70e0","tree":"5fe221c580d30315c45db536a88de063a2880649","v":1,"via":"run"}
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified locally (zccache bypassed):

  • RED e996564: math_extra 10 failed / 2 passed
  • GREEN c15b11e: math_extra 12/12, math_extra_parity 8/8
  • Tier 2 (OFF and ON): 115/115 each, including test_math_extra
  • Local gate passed and stamped; pushed as 76c1333 (already on top of master).

@zackees
zackees merged commit 6159e29 into obs-rust:master Oct 12, 2026
23 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 12, 2026
…-extra ports copied

Upstream obs-rust#338 (plane) and obs-rust#352 (math-extra) landed after this branch's
base, and both ports deliberately copied the C aliasing bugs so they
would match it: plane_from_tri detected `v1 == &dst->dir` and set the
distance to `dir·dir`, and calc_torque stored the scaled direction and
then re-read `v1` through the pointer to add it. Their characterization
tests pinned those results, marked "Characterized, not endorsed".

With the C fixed, neither emulation is needed. plane_from_tri now reads
all three vertices and builds the plane from the copies, like every
other shim in the crate, and calc_torque uses the safe core's
calc_torque, which already reads v1 before writing.

The two characterization tests are updated to the fixed values, and each
now also runs the non-aliased call and asserts the aliased result equals
it, so the property (not just a constant) is checked. The Rust parity
tests do the same against the oracle.

Tested by reverting the C and shim fixes to the versions in this merge
base and rebuilding: test_graphics_math_alias fails 19/19,
test_math_extra fails test_calc_torque_dst_aliases_v1, test_plane fails
test_plane_from_tri_v1_aliases_dir, and calc_torque_dst_aliases_v1_matches_c
fails against the unfixed oracle. With the fixes in place all pass, in
debug and release.
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 libobs/graphics/math-extra.c to obs-graphics

2 participants