Skip to content

rust: Port graphics/plane.c to obs-graphics - #338

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

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

Conversation

@nathan-v

Copy link
Copy Markdown
Contributor

Fixes #335.

Ports libobs/graphics/plane.c to obs-graphics. ENABLE_RUST_LIBOBS drops that C file. Header-inline helpers in plane.h stay C. No intentional differences.

Commits

  1. Characterization a72cb379a: cmocka test_plane pins the seven exports, including a normal transformed as a point, a parallel ray miss that writes *t = 0, and a line miss that leaves *t unchanged.
  2. RED e21514a8f: Tier 1 mirrors those cases against an empty plane. test_plane_from_tri_xy fails because Plane::from_tri returns a zero plane: left (0, 0, 0, 0, 0), right (0, 0, 1, 0, 0). 15 of 17 tests fail.
  3. GREEN 8199c40ab: the safe core, C shims, layout test, and proptest parity.

GREEN result

  • Tier 1: 17 plane tests pass. The rest of obs-graphics passes.
  • Tier 3: plane_matches_c_oracle (256 cases, any bit pattern) and plane_from_tri_v1_alias_matches_c are bit-exact with the C oracle, including the w lane. vec3_cross and vec3_norm follow the header SSE.
  • Layout of struct plane matches the header.
  • Mutation: flipping the sign of plane_transform's dist makes plane_matches_c_oracle fail (transform shim dist 724179300000000.0 vs C -724179300000000.0). The break was reverted.
  • export_list accepts libobs/cmake/rust-exports.d/obs-graphics-plane.txt.
  • soldr cargo clippy -p obs-graphics --all-targets -- -D warnings is clean.

plane_from_tri reads vertices by value, then, when v1 is &dst->dir, sets dist to dir·dir, which is what C does after overwriting that slot.

Not run here

Docker is not installed, so Tier 2 cmocka (test_plane OFF and ON) and the local-gate stamp were not run. ./lint's clang-format, gersemi, rustfmt, and clippy steps ran; the lane's Python unittest step fails on this machine because bash and cmake are not on PATH. Please rebase onto master and stamp if the branch is behind.

Pins the seven exports in graphics/plane.c, including a normal transformed as a point, a parallel ray miss that writes *t = 0, and a line miss that leaves *t unchanged.

Refs obs-rust#335
test_plane_from_tri_xy fails because Plane::from_tri returns a zero plane: left (0, 0, 0, 0, 0), right (0, 0, 1, 0, 0). 15 of 17 Tier 1 tests fail. The two that pass are the degenerate triangle and the line miss, which are also the empty result.

Refs obs-rust#335
@nathan-v
nathan-v requested a review from zackees as a code owner October 12, 2026 07:42
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped.

  • Characterization (a72cb379a): Tier 2 passed 109 of 109 with ENABLE_RUST_LIBOBS OFF and 109 of 109 with it ON. test_plane matches the C.
  • RED (e21514a8f): tests/plane.rs passed 2 of 17 against the empty stubs.
  • GREEN (8199c40ab): plane 17 of 17, plane_parity 2 of 2, plane_layout 1 of 1 and export_list 3 of 3 passed. The local gate passed (plane.c is now swapped out under ON).

Merging once CI is green.

The 17 Tier 1 plane tests pass. Tier 3 plane_parity is bit-exact against the C oracle, including v1 aliased with dst->dir. The layout matches plane.h. Flipping the sign of plane_transform's dist makes plane_matches_c_oracle fail on the shim dist; that break was reverted.

ENABLE_RUST_LIBOBS drops graphics/plane.c. Exports are in obs-graphics-plane.txt. Header-inline helpers stay C.

Fixes obs-rust#335

Local-Gate: v1 tree=92b294cb9463f61dfde540370a9c3b9769673b57 secs=151 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791791815,"gate":"rust/all/fmt","host":"linux-x86_64","key":"0d56f66fcee0416b2b9db06484a615c7da0f6650cb94f6fd72505d76860ca03c","lane":"lint","parents":["e21514a8f1b7a7d218289b775e370f26e93f21c3"],"secs":53,"stamp":"7083b9bb3085244b851192fe35cfaa24","tree":"92b294cb9463f61dfde540370a9c3b9769673b57","v":1,"via":"run"}
Ci-Attestation: {"at":1791791815,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"0d56f66fcee0416b2b9db06484a615c7da0f6650cb94f6fd72505d76860ca03c","lane":"lint","parents":["e21514a8f1b7a7d218289b775e370f26e93f21c3"],"secs":53,"stamp":"aa502dabb1f5f896d98fe180bccb960f","tree":"92b294cb9463f61dfde540370a9c3b9769673b57","v":1,"via":"run"}
Ci-Attestation: {"at":1791791815,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"66924d5af7c21fafe7dcbc83ef0c7ccbb8fea7ce312ee6cc8c44945fbe78788c","lane":"rust-test","parents":["e21514a8f1b7a7d218289b775e370f26e93f21c3"],"secs":47,"stamp":"56963e497044759ea98e3fcaf335898e","tree":"92b294cb9463f61dfde540370a9c3b9769673b57","v":1,"via":"run"}
Ci-Attestation: {"at":1791791815,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"8c402338d0d763c0a94173e23e59b586bb3834ec187aaa42e7738d2d4a0cff1d","lane":"tier2-linux","parents":["e21514a8f1b7a7d218289b775e370f26e93f21c3"],"secs":51,"stamp":"50ab82b6c2e3dbd0a536a6f2183b7d61","tree":"92b294cb9463f61dfde540370a9c3b9769673b57","v":1,"via":"run"}
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Correction to my comment above: the gate did not pass on your head. The plane.c guard in libobs/CMakeLists.txt was missing its closing >:

-    $<$<NOT:$<BOOL:${ENABLE_RUST_LIBOBS}>>:graphics/plane.c
+    $<$<NOT:$<BOOL:${ENABLE_RUST_LIBOBS}>>:graphics/plane.c>

so CMake configure failed in both builds: Cannot find source file: $<1:graphics/plane.c. CI would have failed the same way. I folded the fix into your GREEN commit, so the new head is dc7573f00. With it, the local gate passed: lint, rust-test, and tier2-linux 109 of 109 with ENABLE_RUST_LIBOBS OFF and 109 of 109 with it ON, with 37 ported objects checked under ON, plane.c now included. The RED and GREEN Rust evidence above still stands.

@zackees
zackees merged commit 7fb82e0 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.
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 12, 2026
…REEN)

bounds_move, bounds_scale, bounds_merge_point and bounds_get_center read
their vector operand, or b->min, again after writing dst->min, so a caller
that passes a destination it also reads from gets a value that depends on
the overlap. plane_from_tri reads v1 after writing dst->dir, so a v1
sharing storage with the destination yields a distance taken from the
normalized direction instead of the vertex.

Each fix is a snapshot of the input that the later read would otherwise
re-read: the operand for the four bounds helpers, b->min for the center,
and v1 for the plane. No expression, operand or order of operations
changes, so non-overlapping results are bit-for-bit unchanged.

The Rust port is aligned in the same commit. The bounds shims already read
their inputs before writing, so they needed no change; plane_from_tri had
copied the C bug on purpose - it detected `v1 == &dst->dir` and set the
distance to dir·dir (obs-rust#338) - and that emulation is dropped, so the shim
reads all three vertices like every other shim in the crate. The plane
characterization test that pinned dir·dir ("Characterized, not endorsed")
now asserts the distance of the original vertex and that the aliased call
equals a call with a separate destination.

GREEN: test_graphics_alias_ordinary 5 passed, 0 failed (exit 0), and the
existing test_plane (17) and test_bounds (16) suites pass, in the same
Release build as the RED commit's 0-passed/5-failed run.
zackees pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 13, 2026
…REEN)

bounds_move, bounds_scale, bounds_merge_point and bounds_get_center read
their vector operand, or b->min, again after writing dst->min, so a caller
that passes a destination it also reads from gets a value that depends on
the overlap. plane_from_tri reads v1 after writing dst->dir, so a v1
sharing storage with the destination yields a distance taken from the
normalized direction instead of the vertex.

Each fix is a snapshot of the input that the later read would otherwise
re-read: the operand for the four bounds helpers, b->min for the center,
and v1 for the plane. No expression, operand or order of operations
changes, so non-overlapping results are bit-for-bit unchanged.

The Rust port is aligned in the same commit. The bounds shims already read
their inputs before writing, so they needed no change; plane_from_tri had
copied the C bug on purpose - it detected `v1 == &dst->dir` and set the
distance to dir·dir (obs-rust#338) - and that emulation is dropped, so the shim
reads all three vertices like every other shim in the crate. The plane
characterization test that pinned dir·dir ("Characterized, not endorsed")
now asserts the distance of the original vertex and that the aliased call
equals a call with a separate destination.

GREEN: test_graphics_alias_ordinary 5 passed, 0 failed (exit 0), and the
existing test_plane (17) and test_bounds (16) suites pass, in the same
Release build as the RED commit's 0-passed/5-failed run.

Local-Gate: v1 tree=e491b7e0aad43f87b98f9f54ca8b8692c5dbc748 secs=176 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791854742,"gate":"rust/all/fmt","host":"linux-x86_64","key":"80aee80b302f28ff1fe2588d45fee172121d392fe232b8cd6d19ea0a88a01237","lane":"lint","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":39,"stamp":"c13a375d0f58e6abcbb9dd0201d059f0","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"}
Ci-Attestation: {"at":1791854742,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"80aee80b302f28ff1fe2588d45fee172121d392fe232b8cd6d19ea0a88a01237","lane":"lint","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":39,"stamp":"5b1f1dbec523079acd2b0129aa059db5","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"}
Ci-Attestation: {"at":1791854742,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"e0fc8277f27f5efe797856b0956c6e8cac844384103903d31f8fb9d4a48a4819","lane":"rust-test","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":64,"stamp":"f2fdd9558f3efd1fa3330dd7ce1fe7a9","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"}
Ci-Attestation: {"at":1791854742,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"2baf904178cd0711f9792e8a2e6792204bdf510ba3bf5ec4ce186d8a5f120758","lane":"tier2-linux","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":73,"stamp":"1ec76fb4a267a1895436e7a81eaeb3bc","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"}
zackees pushed a commit that referenced this pull request Oct 13, 2026
)

* test: Fail the graphics math functions on overlapping dst/input (RED)

The five libobs/graphics math functions this branch repairs re-read an
input after storing an output lane, so a dst overlapping an input depends
on the overlap. Three commitments, all failing on master:

- test/cmocka/test_graphics_alias_ordinary.c: runs bounds_move,
  bounds_scale, bounds_merge_point, bounds_get_center and plane_from_tri
  twice, once on separate storage and once on the arrangement, asserting
  the results are identical. 0 of 5 pass on master.
- rust/obs-graphics/tests/bounds_parity.rs: the same four arrangements on
  the Rust shims, each comparing the aliased result with the non-aliased
  one on the Rust side, the C side and between the two.
- rust/obs-graphics/tests/plane_parity.rs: the plane arrangement now also
  compares the aliased result against a call with a separate destination,
  which the C oracle on master does not satisfy (it returns dir·dir for
  the distance).

The plane comparison is on dir's lanes and dist, not the struct, because
struct plane has 12 bytes of tail padding.

* graphics: Make five math functions safe when dst overlaps an input (GREEN)

bounds_move, bounds_scale, bounds_merge_point and bounds_get_center read
their vector operand, or b->min, again after writing dst->min, so a caller
that passes a destination it also reads from gets a value that depends on
the overlap. plane_from_tri reads v1 after writing dst->dir, so a v1
sharing storage with the destination yields a distance taken from the
normalized direction instead of the vertex.

Each fix is a snapshot of the input that the later read would otherwise
re-read: the operand for the four bounds helpers, b->min for the center,
and v1 for the plane. No expression, operand or order of operations
changes, so non-overlapping results are bit-for-bit unchanged.

The Rust port is aligned in the same commit. The bounds shims already read
their inputs before writing, so they needed no change; plane_from_tri had
copied the C bug on purpose - it detected `v1 == &dst->dir` and set the
distance to dir·dir (#338) - and that emulation is dropped, so the shim
reads all three vertices like every other shim in the crate. The plane
characterization test that pinned dir·dir ("Characterized, not endorsed")
now asserts the distance of the original vertex and that the aliased call
equals a call with a separate destination.

GREEN: test_graphics_alias_ordinary 5 passed, 0 failed (exit 0), and the
existing test_plane (17) and test_bounds (16) suites pass, in the same
Release build as the RED commit's 0-passed/5-failed run.

Local-Gate: v1 tree=e491b7e0aad43f87b98f9f54ca8b8692c5dbc748 secs=176 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791854742,"gate":"rust/all/fmt","host":"linux-x86_64","key":"80aee80b302f28ff1fe2588d45fee172121d392fe232b8cd6d19ea0a88a01237","lane":"lint","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":39,"stamp":"c13a375d0f58e6abcbb9dd0201d059f0","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"}
Ci-Attestation: {"at":1791854742,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"80aee80b302f28ff1fe2588d45fee172121d392fe232b8cd6d19ea0a88a01237","lane":"lint","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":39,"stamp":"5b1f1dbec523079acd2b0129aa059db5","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"}
Ci-Attestation: {"at":1791854742,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"e0fc8277f27f5efe797856b0956c6e8cac844384103903d31f8fb9d4a48a4819","lane":"rust-test","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":64,"stamp":"f2fdd9558f3efd1fa3330dd7ce1fe7a9","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"}
Ci-Attestation: {"at":1791854742,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"2baf904178cd0711f9792e8a2e6792204bdf510ba3bf5ec4ce186d8a5f120758","lane":"tier2-linux","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":73,"stamp":"1ec76fb4a267a1895436e7a81eaeb3bc","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"}

---------

Co-authored-by: chriscoveries <[email protected]>
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/plane.c to obs-graphics

2 participants