Repository navigation
obs-scene: Port the scene audio mix kernels (#3079) - #3092
Merged
Merged
Conversation
Behaviour-neutral extraction before the Rust port (#3079). The two mix kernels of scene_audio_render() in obs-scene.c move to libobs/obs-scene-audio-mix.c.h, declared in the new libobs/obs-scene-audio-mix.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - mix_audio_with_buf() is obs_scene_audio_mix_with_buf(out, in, buf, pos, count): out[pos + i] += in[i] * buf[i]; - scene_mix_audio() is obs_scene_audio_mix_add(out, in, pos, count): out[pos + i] += in[i]. The loops are the C's, unchanged; the inputs are const now. scene_audio_render() keeps the item loop, audio_lock, apply_scene_item_volume(), obs_source_get_audio_mix(), the window and mix decisions and the mix and channel loops, in their order. The file joins libobs.core-source, before obs-scene.c. Checked in a scratch harness (not committed): master's two static functions against the moved ones on 20,000 random calls (count 0 to 1024, pos anywhere in the buffer, samples random, random bit patterns and the specials 0, -0, 1, -1, 0.5, inf, -inf, NaN, a denormal and 3.4e38; gain buffers of 0, 1 and random values), outputs compared bit for bit: clang (arm64) -O0 and -O2, with and without ASan/UBSan, -O2 with -ffp-contract=off, and gcc 14 (x86_64) -O0 with ASan/UBSan, -O2 and -O2 -march=x86-64-v3: 0 differences in 7 builds. Refs #3079. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Characterization before the Rust port, against the C moved to libobs/obs-scene-audio-mix.c: test_scene_audio_mix_steps (every platform, the moved file on its own, ASan and UBSan), 6 cases: - the plain add at the start of the tick, inside it and at its end, for counts 0, 1, 3, 724 and a whole tick: only out[pos .. pos + count) changes, 16 guard samples on each side keep their values; - the add through the gain buffer: gains 1, 0, 0.5, 2 and -1 at pos 0 and 300; - a hide mid-tick (gain 1 then 0 from the item's frame 512) at pos 0 and 301: the gain is indexed like the input, not like the output; - NaN, infinities and -0.0 in every operand of both kernels (inf * 0 is NaN, so a hidden item with an infinite sample still poisons the mix); - a count of 0 at pos 0, 500 and 1024 writes nothing; - a second add of the same item adds it again. Every product is exact, so the expectations hold whether or not the compiler fuses the multiply and the add (clang does on aarch64); checked on macOS arm64 at -O1 with ASan/UBSan, -O2, and -O2 -ffp-contract=off. Deliberate breaks of the moved C (10: each kernel's pos offset, the gain dropped, the add made a store, the gain not advanced, each loop end one short and one long, the gain indexed from pos, the add made a subtraction, and an extra multiply-add by 1 and 0): all 10 caught. The gain-from-pos break first passed (period-2 gains at an even pos); the step case at pos 301 now catches it. Refs #3079. Co-Authored-By: Claude Opus 5.5 <[email protected]>
RED for #3079. obs_scene::audio_mix has its API and docs, with todo!() bodies: mix_with_buf() (out[i] += input[i] * gain[i]) and mix_add() (out[i] += input[i]), on slices the shim cuts to the window, the shortest slice deciding. tests/scene/audio_mix.rs: - steps_*: test_scene_audio_mix_steps.c 1:1 (6 cases, same names, values and expectations), through the cut the shim makes (out[pos .. pos + count], input and gain [..count]); - edge_*: proptests of both kernels against the C's expression, bit for bit with any NaN matching any NaN, on random windows of a tick with any f32 (random bit patterns, NaN payloads, both zeros, infinities, extremes), samples outside the window unchanged, and slices of different lengths. All 9 fail on the stubs (obs-tests-core, scene::audio_mix). Refs #3079. Co-Authored-By: Claude Opus 5.5 <[email protected]>
GREEN for #3079. obs_scene::audio_mix implements the two mix kernels of scene_audio_render(): - mix_with_buf(): out[i] += input[i] * gain[i], a separate multiply and add per sample (never fused, as the C oracle); - mix_add(): out[i] += input[i]. Both run over slices, the shortest deciding, so the compiler vectorizes them without std::arch (the C's pointers may alias, and GCC at -O2, as libobs ships, leaves its loops scalar). The loop form of mix_add() depends on the target, chosen by platform::GROUPED_AUDIO_LOOPS (cfg!(target_arch = "x86_64")), because LLVM unrolls the two forms differently there: - x86_64: 4-sample groups. The plain zip loop, unrolled to two vectors with both loads before both stores, was 18 % slower than GCC's vectorized C at -O3 (on a Ryzen 9 5950X); the grouped loop is 2 %. - aarch64: a plain zip loop, unrolled to four vectors. The grouped loop is one vector per iteration and was 46 % slower than clang's C (Apple M1 Max); the zip loop is level with it. Both forms are #[doc(hidden)] pub (mix_add_grouped, mix_add_zipped), so Tier 1 checks on every host that they agree bit for bit. mix_with_buf() is a zip loop on every target: grouped, LLVM vectorizes across the groups with gathers, 4 times slower on x86_64. The module denies clippy::todo now too. No intentional differences. C ABI (ffi::scene_audio_mix): two shims that cut the scene's channel to pos .. pos + count and the item's channel and gain buffer to count, and return at once for a count of 0. libobs: obs-scene-audio-mix.c.h moves from libobs.core-source to its own group libobs.ported-scene-audio-mix (registered in build/ported-source-sets/scene-audio-mix.json), whose precompiled header includes only the unit's header. The hybrid libobs leaves the group out and links the shims; the two symbols are listed in rust-exports.map, rust-hidden.txt and rust-unexports-macos.txt and stay hidden. ci/test_ported_sources.py lists the object. Tier 1: 9/9 (obs-tests-core, scene::audio_mix). Refs #3079. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 3 for #3079. obs-c-oracle builds the original libobs/obs-scene-audio-mix.c.h as oracle_obs_scene_audio_mix_* (oracle/scene_audio_mix.c, without contraction like every oracle). tests/scene/audio_mix_parity.rs runs both kernels on the shim, the C and the core, on copies of one output buffer, and compares every sample of the whole buffer bit for bit, any NaN matching any NaN: - every triple of 14 specials (both zeros, 1, -1, 0.5, 2, the smallest normal, a subnormal, both largest floats, both infinities and two NaN payloads) as output, input and gain; - windows at every 64th frame with counts 0 to 70 and to the tick's end, and the last 8 frames, on random bit patterns (over 1,000 windows); - 32 whole ticks of audio in [-1, 1), with a 0/1 gain and a random one; - proptests over any window of a tick with any f32. NaN payloads are not compared: where two NaNs meet (a NaN sample and a NaN product, or the NaN of inf * 0), IEEE 754 leaves the payload open, the add commutes and each compiler orders its operands as it likes, and x86 returns the first operand's NaN. The first gate run on x86_64 found exactly that (shim 0xffc00000, C 0x7fc00000 for NaN + inf * 0); the Tier 1 proptests treat NaN the same way. No intentional differences. 4 tests. Deliberate breaks of the core (9: the gain dropped, the add made a store, a fused multiply-add twice, the operands swapped with a + 0.0, the add made a subtraction and a + 0.0, and each loop's input or gain shifted by one): all 9 caught here and by Tier 1. The fused ones show that Tier 3 also guards the unfused multiply and add. port_comparison (audio_mix/scene_mix_*: one channel of an item into one channel of a scene mix, a whole tick and the 724 frames from frame 300), Apple M1 Max: with_buf 93.6 / 93.0 ns and 65.5 / 71.8 ns (Rust / C), add 67.0 / 67.1 ns and 48.8 / 48.7 ns. Apple clang vectorizes the C as well; the Rust compiles to the same fmul.4s and fadd.4s per 4 samples (no fmla) as the oracle. Refs #3079. Co-Authored-By: Claude Opus 5.5 <[email protected]>
rust/README.md: the obs-scene crate row lists obs-scene-audio-mix.c.h, and its own row says what moved, what obs-scene.c keeps, how the core vectorizes, why the multiply and add stay unfused (the oracle) and where the shipped aarch64 C differs (clang fuses them by default), and what Tier 3 covers. docs/rust-port/naming.md already maps obs-scene*.c.h to rust/obs-scene. No docs/features.md line: no user-visible change. Refs #3079. Co-Authored-By: Claude Opus 5.5 <[email protected]> Local-Gate: v1 tree=a24f59391e59bc039788653e8b357c436d85938a secs=422 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a Ci-Attestation: {"at":1793383473,"gate":"rust/all/fmt","host":"linux-x86_64","key":"cd937b5d8e00a369eaeaf8ccdab64dacaa55e7e7b6c16e3d26915025a1b7b55f","lane":"lint","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":80,"stamp":"59547544296e86a76adb0a6f0e24ef14","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"} Ci-Attestation: {"at":1793383473,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"cd937b5d8e00a369eaeaf8ccdab64dacaa55e7e7b6c16e3d26915025a1b7b55f","lane":"lint","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":80,"stamp":"8c9dee4e2358f2b800baebecd4535ef8","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"} Ci-Attestation: {"at":1793383473,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"7d0de70b52f5ecec2b58bab2616c967dcaa175c925e1f0f174e12aaf54b7e1e5","lane":"rust-test","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":202,"stamp":"91647d0acb0760557c55abd21ea64086","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"} Ci-Attestation: {"at":1793383473,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"7e6e1de1c5292a690596f65110a657c81244b73bf12835f73d8bd5dcd83955e9","lane":"cross","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":140,"stamp":"42a85ad55f31dfceb04715cbaea271f9","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"} Ci-Attestation: {"at":1793383473,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"7e6e1de1c5292a690596f65110a657c81244b73bf12835f73d8bd5dcd83955e9","lane":"cross","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":140,"stamp":"241c32ba4d42ba8c15f14a8566608cc3","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"} Ci-Attestation: {"at":1793383473,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"7e6e1de1c5292a690596f65110a657c81244b73bf12835f73d8bd5dcd83955e9","lane":"cross","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":140,"stamp":"628ac865da7d476727fc8f65ec2ee568","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"} Ci-Attestation: {"at":1793383473,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"7e6e1de1c5292a690596f65110a657c81244b73bf12835f73d8bd5dcd83955e9","lane":"cross","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":140,"stamp":"bb3ba8081f99536c522706b13aaebcd7","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"} Ci-Attestation: {"at":1793383473,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"18f5e408a881971b6146005dea0b08ff8f43f92450812863a981693f2c67d34e","lane":"tier2-linux","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":247,"stamp":"daa04781f776ebd92068b0a9e570da1a","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"}
zackees
approved these changes
Oct 30, 2026
zackees
left a comment
Member
There was a problem hiding this comment.
Coordinator review: approved. The kernels keep the C's elementwise order with no FMA, and parity is bit-exact apart from NaN payloads, which the tests deliberately treat as equal, as the PR body records. The commits show RED (276c9d2) then GREEN (46698f1). There's Tier 2 cmocka coverage (6 cases) and Tier 3 parity against the C moved unchanged into the oracle. No C tests or public headers were edited. The target_arch cfg stays in platform/mod.rs, and the tests are in the existing scene binary. The Test cost line is present, and it's a behaviour-neutral port, so no ledger line is needed.
zackees
enabled auto-merge
October 30, 2026 18:41
7 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3079
What
The two mix kernels of
scene_audio_render()inlibobs/obs-scene.c.hmove tolibobs/obs-scene-audio-mix.c.hand are ported torust/obs-scene(moduleaudio_mix). The hybrid libobs links the Rust shims in place of the C (grouplibobs.ported-scene-audio-mix). The kernels are two hidden functions on plain pointers and counts:obs_scene_audio_mix_with_buf()(mix_audio_with_buf()):out[pos + i] += in[i] * buf[i], for an item whose visibility change falls in the tick (bufis its 0/1 gain);obs_scene_audio_mix_add()(scene_mix_audio()):out[pos + i] += in[i].scene_audio_render()keeps the item loop,audio_lock,apply_scene_item_volume(),obs_source_get_audio_mix(), the window and mix decisions and the mix and channel loops.This is the scene half of #3007 (#3009). The source volume kernels are #3080.
Vectorization: the safe core runs loops on slices, which cannot overlap, so LLVM vectorizes it with no
std::arch. On aarch64 it compiles tofmul.4sandfadd.4sper 4 samples, the same as the oracle. GCC 14 at-O2leaves the C loops scalar, because the C pointers may alias.mix_adduses a different loop form on x86_64 and aarch64; see Numbers.Fused multiply-add: the multiply and the add stay separate, as in the C oracle, which is built without contraction like every oracle. The shipped C on aarch64 is built with clang's default
-ffp-contract=on, which fuses them intofmla, so there it can differ from the port in the last bit. Tier 3 fails if the core fuses them.No intentional differences.
Commits
Extraction 1607ce0: behaviour-neutral. A scratch harness (not committed) compared master's static functions with the moved ones on 20,000 random calls (any
posandcount, random bit patterns and specials), bit for bit, with 0 differences in 7 builds:-ffp-contract=off;-march=x86-64-v3.Characterization ce775ce:
test_scene_audio_mix_steps, 6 cases, the moved file on its own, every platform, ASan/UBSan. Every product is exact, so it holds whether or not the compiler fuses. 10 of 10 planted mutants of the C are caught. The gain-from-posmutant first passed; a step case atpos301 now catches it.RED 276c9d2: the core with
todo!()bodies, and 9 Tier 1 tests:steps_*: the cmocka cases 1:1;edge_*: proptests against the C's expression, bit for bit.All 9 fail. GREEN adds a 10th: both loop forms of
mix_addagree on every host.GREEN 46698f1: the core (with the per-target loop form of
mix_add), two C ABI shims that cut the window and return at once for a count of 0, and the ported group. The symbols are inrust-exports.map,rust-hidden.txtandrust-unexports-macos.txtand stay hidden.Tier 3 and bench 6b9fb27: the oracle build of the original C, and 4 parity tests that compare the whole output buffer bit for bit, any NaN matching any NaN:
All 9 planted mutants of the Rust core are caught by Tier 1 and by Tier 3, including two that fuse the multiply-add. The commit also adds the
port_comparisoncasesaudio_mix/scene_mix_*.Docs 9eb6f90: the
rust/README.mdrows.naming.mdalready mapsobs-scene*.c.h. Nodocs/features.mdline: no user-visible change.Numbers
port_comparisonaudio_mix/scene_mix_*: one channel of an item into one channel of a scene mix, for a whole tick and for the 724 frames from frame 300. Rust/C below 1: the Rust is faster.Against the C as it ships (libobs
RelWithDebInfo; the Rust staticlib is Cargorelease,opt-level = 3):-O2(scalar)with_buf/whole_tickwith_buf/from_frame_300add/whole_tickadd/from_frame_300-O2(vectorized)with_buf/whole_tickwith_buf/from_frame_300add/whole_tickadd/from_frame_300/O2(vectorized)with_buf/whole_tickwith_buf/from_frame_300add/whole_tickadd/from_frame_300Against GCC's vectorized C at
-O3(the oracle's level, not shipped), x86:with_buf/whole_tickwith_buf/from_frame_300add/whole_tickadd/from_frame_300x86:
taskset -c 27, functions and loops aligned on both sides (64 and 32 bytes), two runs within 2 % (one run of the C at 76.5 ns foradd/whole_tickis an outlier). macOS and Windows: not pinned; Windows is the mean of two runs in a VM, which vary by up to 7 %.Why
mix_addhas a different loop on x86_64 and aarch64. LLVM vectorizes both loop forms of the safe core, but unrolls them differently, and the faster form differs per target:add/whole_tick-O3)So
mix_addwalks 4-sample groups on x86_64 and is a zip loop on aarch64, chosen by onecfg!(target_arch = "x86_64")constant insrc/platform/mod.rs(GROUPED_AUDIO_LOOPS, the crate's platform boundary). Both forms are#[doc(hidden)] pub, and Tier 1 checks on every host that they give the same bits.mix_with_bufis a zip loop everywhere: in 4-sample groups LLVM vectorizes across the groups with gathers (385 ns), and the other forms tried (destructured groups, groups of 8, other zip orders, products first) were slower still; it stays 11–13 % behind GCC's-O3C on x86 and level with clang's C on aarch64. Buffer placement (4K aliasing), the bench harness's order and code alignment were ruled out first.Tests
scene::audio_mixTier 1 and Tier 3, 14/14. The cmocka test passes 6/6 at -O1 with ASan/UBSan, at -O2, and at -O2-ffp-contract=off.0xffc00000, C0x7fc00000); Tier 1 and Tier 3 now compare any NaN as equal there (see the Tier 3 commit).test_scene_audio_mix_steps6/6,scene::audio_mixTier 1 and Tier 3 14/14 against the MSVC oracle.test_scene_audio_actionsruns the real scene mix on the hybrid libobs in Tier 2.Checklist
master🤖 Generated with Claude Code