Skip to content

rust: Port graphics/vec3.c to obs-graphics - #75

Merged
zackees merged 5 commits into
obs-rust:masterfrom
b7216309-jpg:port/vec3
Oct 10, 2026
Merged

zackees merged 5 commits into
obs-rust:masterfrom
b7216309-jpg:port/vec3

Conversation

@b7216309-jpg

@b7216309-jpg b7216309-jpg commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Ports libobs/graphics/vec3.c to obs-graphics: vec3_from_vec4, vec3_plane_dist, vec3_rotate, vec3_transform, vec3_transform3x4, vec3_mirror, vec3_mirrorv and vec3_rand. With ENABLE_RUST_LIBOBS=ON libobs builds without graphics/vec3.c. The header-inline helpers in vec3.h stay C.

The w lane. struct vec3 is a union with __m128, so it has a fourth float. The C code clears w in its results, but vec3_dot multiplies all four lanes, so a nonzero or NaN w from a caller changes plane distances, rotations and mirrors. Vec3 therefore keeps w, and Vec3::dot adds the products as (w + y) + (z + x), the SSE shuffle order. No intentional differences.

vec3_rand. It calls libobs rand_float, which stays C in math-extra.c, so the Rust shim calls it too. Test binaries have no libobs, so the oracle hosts an unrenamed rand_float backed by its math-extra.c copy, in the same way as test_bmem.c and base-variadic.c. Remove it when math-extra.c is ported.

Also adds struct plane and struct matrix3 mirrors (ffi::plane, ffi::matrix3), with only what this port needs. libc becomes a dev-dependency of obs-graphics, for srand in the parity test.

Motivation and Context

The next item from the graphics math list in #45, after vec4 (#73).

Refs #45

How Has This Been Tested?

Windows 11, MSVC 14.44, soldr 0.9.31.

  • Commits: test/cmocka/test_vec3.c (C characterization), then RED (todo!() core: Tier 1 fails 10/10, the parity binary aborts on the first shim call, layout passes 3/3), then GREEN.
  • Tier 1, Tier 2 layout (vec3 incl. the ptr/m union members, plane, matrix3), and Tier 3 proptest parity:
    • inputs use arbitrary bits and finite values;
    • w is mostly 0 but sometimes any value;
    • each operation also runs in place, and vec3_mirrorv with dst aliasing the direction;
    • vec3_rand is compared after the same srand seed for both sides.
  • The test_vec3.c expectations also run against the oracle (cmocka_cases_match_c_oracle). There is no cmocka on Windows, so the cmocka test itself first runs in Tier 2 CI.
  • Mutation check: 12 mutants, 10 caught. The caught ones: dot ignores w, naive sum order, sub keeps w, rotate uses t, 3x4 skips the translation, mirror factor 1, rand order z/y/x, from_vec4 keeps w, plane distance sign, and transform as a direction (w = 0). Two equivalent mutants survive. Swapping the halves of the final add changes NaN payloads only. mulf clearing w makes no difference, because the following sub always clears it.
  • python ci/local_gate.py --lane lint, and cargo clippy -p obs-graphics -p libobs-rust --lib -- -D warnings for x86_64/aarch64 Linux and aarch64 macOS.

Updated from master after #73, #74 and #85 landed. soldr cargo test --workspace (38 binaries) and lint pass with no local changes. The CI run before that update was all green on Linux, macOS (arm64, first use of the scalar SSE stand-in) and Windows, Tier 2 included.

Types of changes

  • Code cleanup (non-breaking change which makes code smaller or more readable)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

🤖 Generated with Claude Code

b7216309-jpg and others added 4 commits October 10, 2026 03:12
Pins the exported functions of graphics/vec3.c before the Rust port:
plane distance, rotation by a matrix3's axes, the 3x4 transform that
subtracts the translation first, the matrix4 point transform, both
mirrors, and vec3_rand repeating after the same srand seed. Every
result has w cleared.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 1, Tier 2 layout (vec3, plane, matrix3) and Tier 3 parity tests
for libobs/graphics/vec3.c against a todo!() core.

vec3_rand calls libobs rand_float, which stays C in math-extra.c, so
the Rust shim calls it too. Test binaries have no libobs, so the oracle
hosts an unrenamed rand_float backed by its math-extra copy; the parity
test seeds the C library's rand the same way for both sides.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
vec3_from_vec4, vec3_plane_dist, vec3_rotate, vec3_transform,
vec3_transform3x4, vec3_mirror, vec3_mirrorv and vec3_rand move to
Rust. With ENABLE_RUST_LIBOBS=ON libobs builds without graphics/vec3.c.

struct vec3 has an SSE w lane, and vec3_dot multiplies it, so a
caller's nonzero or NaN w changes the result. Vec3 keeps w, and dot
adds the products as (w + y) + (z + x), the SSE order. No intentional
differences.

Co-Authored-By: Claude Opus 5.5 <[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.

2 participants