Skip to content

obs-graphics: Port the effect parameter values (#2409) - #2411

Merged
zackees merged 5 commits into
obs-rust:masterfrom
vpacher:port/effect-params
Oct 25, 2026
Merged

zackees merged 5 commits into
obs-rust:masterfrom
vpacher:port/effect-params

Conversation

@vpacher

@vpacher vpacher commented Oct 25, 2026

Copy link
Copy Markdown
Member

Fixes #2409

Slice 1 of graphics/effect.c: the parameter values of an effect move to libobs/graphics/effect-params.c.h and are ported to obs_graphics::effect_params. These are the 17 exported functions every filter and scene item calls on every frame.

What changed

  • libobs/graphics/effect-params.c.h: gs_effect_set_bool, _float, _int, _matrix4, _vec2, _vec3, _vec4, _color, _texture, _texture_srgb, _val, _default, gs_effect_set_next_sampler, and gs_effect_get_val, _val_size, _default_val, _default_val_size, moved unchanged from effect.c.h. The technique and pass functions stay in effect.c.h (slice 2).
  • rust/obs-graphics/src/effect_params.rs (safe core) and src/ffi/effect_params.rs (the 17 #[no_mangle] shims, listed in libobs/cmake/rust-exports.d/obs-graphics-effect_params.txt). The file moves into libobs.ported-graphics.
  • struct gs_effect_param is the existing mirror in ffi::effect_lookups (obs-graphics: Port effect name and index lookups #1843). The value stays the C DARRAY(uint8_t), kept in place by DarrayStore, which grows as darray_resize does (capacity doubling, new bytes zeroed). The getters return bmem copies.
  • Allocation and logging go through the C adapters in obs-graphics-adapter.c.h, as in image_file, so the module names no obs-util symbol. A new adapter, obs_graphics_effect_param_error(), logs the five error lines with the C's literal formats: a log handler sees the same format string as before (test_effect_set_default_null matches on it).
  • set_val() and the shim's setval() are #[inline(always)], as the C's effect_setval_inline is static inline. Values up to 64 bytes are compared by ORing the XOR of 8-byte chunks, which unrolls for the constant length of each setter, as GCC expands the C's memcmp. With a plain slice comparison, LLVM called bcmp and gs_effect_set_matrix4 took twice the C's time on x86_64.

Kept, characterized, not endorsed

  • gs_effect_set_vec3 stores 12 bytes (sizeof(float) * 3), not sizeof(struct vec3).
  • gs_effect_set_bool stores an int.
  • gs_effect_set_default on a parameter without a default passes the NULL array on, logs "invalid data" and keeps the value.

Intentional difference

gs_effect_set_texture and _texture_srgb store struct gs_shader_texture with its padding bytes zero. The C builds it on the stack and copies and compares its 7 padding bytes as it finds them (x86_64), so setting the same texture again can count as a change, and the parameter is uploaded again. Here it never counts as a change.

RED → GREEN

  1. Characterization ff2c24d29: test_effect_param_values (8 cases), on the exported functions with a hand-built struct gs_effect_param; no graphics context, so it runs on every platform. Passes on libobs and libobs-c-reference.
  2. Extraction 8bd4b1715: behaviour-neutral.
  3. RED 0bd225a86: Tier 1 shader::effect_params, the core's rules from the cmocka cases 1:1 plus edge cases and proptests: 0 of 10 pass (todo!()).
  4. GREEN 0c8e96243: 10 of 10.
  5. Tier 3 e600cdcf0: the C oracle (the file unmodified, exports renamed) against the shims: the layout of struct gs_shader_texture, every function on 6 chosen parameters with a valid and a NULL parameter, and 20 000 random calls. Compared: cur_val's num, capacity and bytes, default_val, changed, next_sampler, the returned size or copy, and the LOG_ERROR lines (count and text). For the texture setters only tex and srgb are compared (see above).

Mutation check (done once, not committed): 17 of 19 mutants fail Tier 1 or Tier 3. The two survivors are equivalent: new bytes not zeroed on growth (the only resize is followed by a full copy) and the 64-byte threshold of the chunked compare (exact at any length).

Tier 2 (Linux x86_64, Debian 13, Xvfb)

test_effect_param_values, test_effect_set_default_null, test_color_correction_16f, test_scroll_filter_wide_source, test_sprite_flip_render and test_scene_rotated_scale_filter pass on the hybrid libobs and on libobs-c-reference (6/6 each). In the hybrid, gs_effect_set_float is the Rust shim (checked in the disassembly) and the C helpers are gone; exported symbols: 1766 on both. shader::effect* 32/32 on Linux x86_64; shader::effect_params* 13/13 on macOS arm64.

Lint lane (ci/local_gate.py --lane lint) on the same host: clang-format, rustfmt, clippy -D warnings for the workspace and every feature set, the Dylints and the ratchets pass; its self-test step skips test_hybrid_packaging there (no zstd on that host, the same on master).

port_comparison (Linux x86_64, one pinned core, criterion median, Rust / C)

effect_params/set_float_same      3.21 ns / 2.54 ns
effect_params/set_float_new       3.89 ns / 2.75 ns
effect_params/set_vec4_same       3.27 ns / 2.89 ns
effect_params/set_vec4_new        3.48 ns / 3.20 ns
effect_params/set_matrix4_same    3.78 ns / 4.32 ns
effect_params/set_matrix4_new     4.73 ns / 4.07 ns
effect_params/set_texture_same    2.76 ns / 7.53 ns
effect_params/set_texture_new     3.21 ns / 7.58 ns

_same sets the stored value again (the common case per frame), _new a different one. The texture setters are faster mostly because the C compares its padding; set_float and set_vec4 are 0.3 to 1.1 ns slower than the C, set_matrix4 is faster with the same value and 0.7 ns slower with a new one.

Checklist

  • Base is master
  • 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
  • User-visible change: none
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): none
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

AI note: Claude Code (Claude Opus 5.5) wrote the code, the tests, the commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

@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. DarrayStore::resize and ensure_capacity reproduce darray_resize and darray_ensure_capacity (util/darray.h) exactly. Same-size returns early, size 0 only sets num, capacity grows as cap ? cap*2 : size and then max(size), it copies the old capacity bytes into a new bmalloc block and bfrees the old one, and growth is zero-filled. The memory stays bmalloc-owned, so C's da_free of cur_val keeps working. The three invalid-param and invalid-data blog paths stay in C through obs_graphics_effect_param_error. The Tier 2 test_effect_param_values and Tier 3 parity with the oracle are in place. LGTM.

@zackees

zackees commented Oct 25, 2026

Copy link
Copy Markdown
Member

Coordinator: the Windows Rust Tests failure here (test-diff: FAILED (3221225621), which is STATUS_INTEGER_OVERFLOW) is the audio_min_ts_parity crash on MSVC from #2406, not this PR. #2420 fixes it and is queued. Rebase after it merges.

vpacher and others added 5 commits October 25, 2026 21:35
test_effect_param_values pins the parameter values of
libobs/graphics/effect.c before their Rust port (obs-rust#2409). The functions
are EXPORT and none of them touches the graphics subsystem, so the test
calls them through the real libobs on a hand-built struct
gs_effect_param, with no graphics context, on every platform:
- each setter's stored size and bytes: bool and int as an int, vec3 as
  12 bytes (sizeof(float) * 3), vec2, vec4 and matrix4 as the whole
  struct, a texture as struct gs_shader_texture with srgb false or true,
  a color through vec4_from_bgra (0xAARRGGBB to {r, g, b, a} / 255),
  arbitrary bytes through gs_effect_set_val;
- changed: set when the bytes differ or the size changes (also to 0),
  not when the same bytes are set again;
- the getters: NULL for a size of 0, a bmem copy of the stored bytes;
- gs_effect_set_default: the default bytes back; without a default it
  passes the NULL array on, logs "invalid data" and keeps the value;
- gs_effect_set_next_sampler: only on a texture parameter;
- NULL parameter or data: exactly one LOG_ERROR line with the C's text,
  nothing changed; the size getters return 0 without an error.

Refs obs-rust#2409

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The parameter values of an effect move from graphics/effect.c to
graphics/effect-params.c, unchanged: effect_setval_inline and the two
getter copies, the gs_effect_set_* setters, gs_effect_get_val,
gs_effect_get_val_size, gs_effect_get_default_val,
gs_effect_get_default_val_size, gs_effect_set_default and
gs_effect_set_next_sampler. None of them touches the graphics
subsystem; the technique and pass functions stay in effect.c.

The file joins libobs.graphics (sorted before effect-parser) and keeps
effect.c's min() with its #undef at the end. Behaviour-neutral: the
same code, the same exported symbols.

Refs obs-rust#2409

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RED for the port of libobs/graphics/effect-params.c.h, the parameter
values of an effect (obs-rust#2409), to a new module, effect_params, of
rust/obs-graphics. The safe core is a stub (todo!()): ByteStore (the
parameter's DARRAY(uint8_t)), set_val() (effect_setval_inline with a
valid parameter and data), copy_out() (the getter copies),
bgra_to_vec4() (vec4_from_bgra) and takes_next_sampler().

tests/shader/effect_params.rs ports the rules of
test_effect_param_values that live in the core, under the same case
names and with the same inputs and expectations: changed on new bytes
and on a size change (also to 0), the getter copies, the color, the
next sampler. Each setter's size and bytes, gs_effect_set_default
without a default and the NULL log lines are the shim's; the cmocka
test runs against the shim in the hybrid libobs.

New edge cases (edge_*): a copy into a smaller buffer and the color
extremes, one channel at a time. Proptests: set_val stores the data and
sets changed exactly when the size or bytes differ, never clearing it;
copy_out copies min(src, dst) and leaves the rest; every color channel
is its byte / 255.

cargo test -p obs-graphics --test obs-tests-graphics
shader::effect_params fails 10 of 10 with "not yet implemented".

Refs obs-rust#2409

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Ports libobs/graphics/effect-params.c.h, the parameter values of an
effect (obs-rust#2409), to the effect_params module of rust/obs-graphics: the 17
exported functions gs_effect_set_bool, _float, _int, _matrix4, _vec2,
_vec3, _vec4, _color, _texture, _texture_srgb, _val, _default and
gs_effect_set_next_sampler, and gs_effect_get_val, _val_size,
_default_val and _default_val_size. The hybrid libobs drops the C file
and exports the Rust shims in its place.

Safe core (src/effect_params.rs): set_val() (store the bytes, resize on
a size change, set changed when the size or the bytes differ),
copy_out() (at most the stored size), bgra_to_vec4() (vec4_from_bgra)
and takes_next_sampler(). No state, no lock, no graphics calls; the
module denies clippy's panic, indexing and arithmetic lints.

As the C's effect_setval_inline is static inline, set_val() and the
shim's setval() are #[inline(always)], so every length is a constant in
each setter. Values up to 64 bytes (every typed setter) are compared by
ORing the XOR of 8-byte chunks: for a constant length that unrolls into
a few loads, as GCC expands the C's memcmp, where LLVM would call bcmp
for a 64-byte slice comparison (gs_effect_set_matrix4 took twice the C's
time on x86_64 with the slice comparison).

Shim (src/ffi/effect_params.rs): struct gs_effect_param is the mirror in
ffi::effect_lookups. The value stays the C DARRAY(uint8_t), kept in
place by DarrayStore, which grows as darray_resize does (capacity
doubling, new bytes zeroed, bmalloc + copy + bfree). The getters return
bmem copies, as bzalloc. Allocation and logging go through the C
adapters in obs-graphics-adapter.c.h, so the module names no obs-util
symbol; the new obs_graphics_effect_param_error() logs the five error
lines with the C's literal formats, so a log handler sees the same
format string (test_effect_set_default_null matches on it).

Kept, characterized, not endorsed: gs_effect_set_vec3 stores 12 bytes;
gs_effect_set_bool stores an int; gs_effect_set_default without a
default passes the NULL array on and logs "invalid data".

Intentional difference: gs_effect_set_texture and _texture_srgb store
struct gs_shader_texture with its padding zero. The C builds it on the
stack and copies and compares the padding as it finds it, so the same
texture set again can count as a change (and be uploaded again); here
it never does.

C side: effect-params.c.h leaves libobs.graphics for
libobs.ported-graphics (its PCH has graphics/effect.h, vec4.h and
util/base.h). The functions are EXPORT: the shims are listed in
libobs/cmake/rust-exports.d/obs-graphics-effect_params.txt.

Tier 1 shader::effect_params passes 10/10.

Refs obs-rust#2409

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Oracle (oracle/effect_params.c): the original effect-params.c.h,
unmodified, with its 17 exports renamed to oracle_*, plus the layout of
struct gs_shader_texture. blog, bzalloc and the darray inlines are the C
ones of the test binary; the shim logs and allocates through the
adapters of obs-graphics-adapter.c.h, which oracle/image_file.c builds
into the same binary.

Tier 3 (tests/shader/effect_params_parity.rs): both sides get a
parameter built the same way (type, cur_val and default_val with the
same bytes and spare capacity, changed, next_sampler) and the same call
through their C signatures, and are compared: cur_val's num, capacity
and bytes, default_val, changed, next_sampler, the returned size or copy,
and the LOG_ERROR lines (count and text, through the process log
handler both sides log to):
- the layout of struct gs_shader_texture;
- every function on 6 chosen parameters, with a valid and a NULL
  parameter (sizes up and down, the same bytes, NULL data for each
  pointer setter, no default and an empty default), and the next
  sampler on every parameter type;
- 20 000 random calls on random parameters.

Excluded, the intentional difference: the padding of struct
gs_shader_texture. For the two texture setters only tex and srgb are
compared, the shim's padding must be zero, and the shim's changed must
follow its bytes exactly; the C's must be set whenever the fields
changed (it can also be set by its stack padding).

port_comparison (benches/port_comparison/effect_params.rs): set_float,
set_vec4, set_matrix4 and set_texture with the same value (nothing
changes) and with a new value each call, changed cleared after each
call; the fixtures free their value buffers.

shader::effect_params and effect_params_parity pass 13/13 on macOS
arm64.

Refs obs-rust#2409

Co-Authored-By: Claude Opus 5.5 <[email protected]>
vpacher added a commit to vpacher/obs-studio that referenced this pull request Oct 25, 2026
…MSVC (obs-rust#2420)

audio_min_ts_parity::insufficient_matches draws the sample rate from all
of usize. On MSVC x64 the C's util_mul_div64 is exact 128-bit math
(_umul128 / _udiv128), which faults with STATUS_INTEGER_OVERFLOW when
the quotient does not fit in 64 bits, so the C oracle crashed the whole
obs-tests-core binary on windows-2025 (seen on obs-rust#2411, whose oracle
change pulls obs-tests-core into the changed-crates run). The shim
saturates there, so only the oracle faults.

On MSVC x64 the rate now stays below 1e9, so t * rate / 1e9 fits for any
t, as audio_mix_parity does since obs-rust#1909. Other hosts keep the full range
(util_mul_div64 wraps there, and both sides wrap the same way).
audio_discard_parity keeps its rates below 384 kHz and is not affected.

Refs obs-rust#2405

Co-authored-by: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher force-pushed the port/effect-params branch from e600cdc to ccf9873 Compare October 25, 2026 20:37
@zackees
zackees added this pull request to the merge queue Oct 25, 2026
Merged via the queue into obs-rust:master with commit 6a46b10 Oct 25, 2026
38 checks passed
@vpacher
vpacher deleted the port/effect-params branch October 25, 2026 21:21
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 effect parameter values in graphics/effect.c to obs-graphics (effect.c slice 1)

2 participants