Skip to content

obs-graphics: Port the effect parser blocks - #3595

Merged
rdoupe merged 4 commits into
masterfrom
fix/rust-port-port-the-effect-parser-582
Nov 4, 2026
Merged

rdoupe merged 4 commits into
masterfrom
fix/rust-port-port-the-effect-parser-582

Conversation

@rdoupe

@rdoupe rdoupe commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3550

What and why

Second step of #3550: the Rust port of the effect parser's struct and sampler-state blocks (libobs/graphics/effect-parser-blocks.c.h, moved out of effect-parser.c.h with its characterization in #3561) to a new obs-graphics module, effect_parser. The hybrid libobs drops the C. No upstream issue; upstream code by Lain, credited on the port commit.

The struct block's C is the shader parser's line for line (ported in #3541), so the effect core reuses it; only the sampler-state item is new.

  • obs-graphics: Test the effect parser blocks (RED): Tier 1 in shader::effect_parser, against todo!(). The struct and sampler cases of test_effect_parser_blocks.c, one to one at the block (the struct or sampler kept, the errors the block adds, and the token it stops at), with the second blocks of its two-block and mixed texts, plus edge cases: values of any token kind, a string holding a ;, newlines and tabs inside a value, a { or = inside a value, several empty items, a value or state name cut short, a bad name cut short inside the braces it skips, and the shader parser's struct edges; and a proptest that the walk never steps past the terminator and keeps no empty value. It shares the shader parser's Tier 1 cursor and dump helpers.
  • obs-graphics: Port the effect parser blocks (GREEN): the safe core over the shader parser's Tokens trait and its one-to-one ports of the util/cf-parser.h inlines, each called where the C calls it. parse_struct is shader_parser::parse_struct; the sampler-state item keeps its own loop (every token up to the one starting with ; joined into the value, as dstr_ncat does, and the item pushed only when the value is not empty). The hidden shims ep_parse_struct() and ep_parse_sampler_state() walk ep->cfp.cur_token itself, so ep_parse() (still C) goes on from the token where a block stopped; errors and brace skips go to the exported cf_adderror() and cf_pass_pair() through the existing private adapters, at the same tokens. A kept block is written as the C builds it (bmalloc copies, darrays grown as util/darray.h grows them, on libobs's allocator; nothing marked written), so ep_free() frees it. The unity group becomes libobs.ported-effect-parser-blocks (its own ported-source-sets/effect-parser-blocks.json), and rust-hidden.d/obs-graphics-effect_parser.txt hides the shims. test_effect_parser_blocks compiles ep_parse() and the blocks' C into itself, since ep_parse() is hidden, so a new test_effect_parser_blocks_rust runs the same unchanged file with ep_parse() compiled in and the blocks, ep_free() and the parameter type decisions taken from the production staticlib (hybrid build only, as test_effect_param_types_steps_rust does).
  • obs-graphics: Compare the effect parser blocks (Tier 2 layouts, Tier 3): an oracle that compiles the moved file unmodified under oracle_* names on the C cf-parser inlines and the C cf_adderror, cf_pass_pair, dstr_ncat, lexer and preprocessor, with helpers that lex a text into a struct effect_parser with that C, so both sides get the same tokens. shader::effect_parser_parity checks the layouts of struct ep_var (with enum ep_var_type), ep_struct, ep_sampler and effect_parser, then everything either side leaves in the parser (every field of each struct, variable and sampler, darray counts and capacities, the other collections, every error with its file, row, column and level, and where the cursor stopped) on 61 fixed texts, every prefix of each, each from every token as either block, and random token soups. Plus a port_comparison bench.
  • docs: Record the effect parser blocks port: the rust/README.md row, the crate row, Cargo.toml description and lib.rs docs.

No intentional differences: the sampler-state item leak the round plan carried was fixed in the C by #3561, so the port follows the fixed C. Characterized, not endorsed, and kept as the C does it (pinned by test_effect_parser_blocks and Tier 1): a missing ; in a sampler state joins the next item into the value; an empty value drops the item with no error; a stray token before a struct variable's ; drops the variable with no error; a bad block name skips to a ; past the braces.

RED → GREEN

  • RED commit: obs-graphics: Test the effect parser blocks. All 11 Tier 1 tests in shader::effect_parser fail, panicking at the stubs (not yet implemented: ep_parse_struct / ep_parse_sampler_state); the 11 shader::shader_parser tests whose helpers it shares still pass. --test-diff origin/master there runs 6159 tests and fails on those 11 and nothing else (6 skipped by the test filter), and the lint lane passes every step up to Dylint. Runs were on that commit.
  • GREEN (runs were on docs: Record the effect parser blocks port):
    • Tier 1 shader::effect_parser 11/11, Tier 3 shader::effect_parser_parity 7/7.
    • Mutation check: five planted mutants (an empty value kept, a space pushed after each value token, end of text inside a value taken as a skipped item, a pushed struct marked written, end of text at the peek keeping the sampler) each fail one to three of the seven parity tests. A sixth (the } check after the peek loop never failing) was equivalent: that check cannot fail after the peek.
    • Tier 2: test_effect_parser_blocks passes on the C build, hybrid libobs and libobs-c-reference, and test_effect_parser_blocks_rust (the same file on the shims) passes on the hybrid build. The C, hybrid and C reference builds export the same 1767 symbols as master, the two shims local (t) in each; the hybrid compiles none of the group's C.
    • The graphics, effect and shader C tests (ctest -R 'shader|effect|graphics|texture|image|gif|sampler', under Xvfb): 57/57 on hybrid libobs, 57/57 on libobs-c-reference, 48/48 on the C build, none skipped.
    • port_comparison bench passes its allocation check.
    • Local gate: --test-diff 6450 passed, 6 skipped by the test filter; --lane lint and --lane cross pass every step up to Dylint (formatting, the ci/ tests, every clippy feature set), which this host can't run (no Dylint driver for the pinned nightly); soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets --locked -- -D warnings is clean. PR CI runs the Dylint step. For the same reason the head is not stamped (the stamp run stops at that lint step), so PR CI runs the full suite.
    • ./unittest: the Rust tiers pass (6450 and 912), and Tier 2 in Docker passes 717/717 against libobs-c-reference and 717/717 against hybrid libobs, test_effect_parser_blocks_rust among them (3 skipped on each, the pulse default-follow and two v4l2 vivid tests, which need hardware or a sound server); its export-symbol comparison of C and Rust libobs passes.

Test cost: new tests: longest module 0.11 s in the debug build (parity, 7 tests; Tier 1 0.01 s), the same at reduced and full; peak RSS 7.5 MB over 25 runs; no own threads.

  • Real app (UI, protocol output or logging changes): not applicable (no change in behaviour).

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • 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 (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): none

rdoupe and others added 4 commits November 3, 2026 17:53
Tier 1 for the port of the effect parser's struct and sampler-state
blocks (libobs/graphics/effect-parser-blocks.c.h) to a new
obs_graphics::effect_parser module, against todo!() stubs.

The core walks the C parser's tokens in place through the shader
parser's Tokens trait (cur_token, its type and text, cf_adderror() and
cf_pass_pair()), so ep_parse() goes on from the token where a block
stopped, and returns the struct or sampler the C would push, or None
where the C frees it. The struct block's C is the shader parser's line
for line; the sampler-state item differs (its value is every token up
to the next ';', joined, and an empty value drops the item).

struct_* and sampler_* port test_effect_parser_blocks.c one to one
(same texts, in the same groups) at the block: the struct or sampler
kept, the errors the block adds, and the token it stops at, with the
second blocks of its two-block and mixed texts. edge_* add what the C
test leaves out: values of any token kind, a string holding a ';',
newlines and tabs inside a value, a '{' or '=' inside a value, several
empty items, a value or state name cut short, a bad name cut short
inside the braces it skips, and the shader parser's struct edges. A
proptest walks random token soups over the blocks' vocabulary and
checks the cursor never steps past the terminator and no kept value is
empty. The shader parser's Tier 1 cursor and dump helpers are shared,
and its cf-parser steps are made visible to the crate.

Refs #3550.
The safe core of the effect parser's struct and sampler-state blocks
(libobs/graphics/effect-parser-blocks.c.h) in
obs_graphics::effect_parser, and the hidden C ABI shims
ep_parse_struct() and ep_parse_sampler_state() in ffi::effect_parser
that the C ep_parse() calls on the keyword token of each block. The
hybrid libobs drops the C.

The core is ep_parse_struct(), ep_parse_sampler_state_item() and
ep_parse_sampler_state() over the shader parser's Tokens trait and its
one-to-one ports of the util/cf-parser.h inlines (cf_next_name,
cf_next_token_should_be, cf_next_valid_token, cf_peek_valid_token),
each called where the C calls it. The struct block's C is the shader
parser's line for line (struct ep_var and ep_struct in place of
shader_var and shader_struct), so parse_struct() is
shader_parser::parse_struct(). The sampler-state item keeps its own
loop: every token up to the one starting with ';' joined into the value
(dstr_ncat), and the item pushed only when the value is not empty. Each
block returns the struct or sampler the C would push, or None where the
C frees it.

The shim's Tokens is ep->cfp.cur_token itself (the shader parser shim's
RawTokens), so ep_parse() goes on from the token where a block stopped;
its errors and brace skips go to the exported cf_adderror() and
cf_pass_pair() through the existing private adapters, at the same
tokens. A kept block is written as the C builds it: bmalloc copies and
darrays grown as util/darray.h grows them, on libobs's allocator, so
ep_free()'s inline destructors free them; a struct variable is
ep_var_init() (all zero) with its type, name and mapping, and a struct
or sampler is not written.

The blocks' unity group becomes libobs.ported-effect-parser-blocks (its
own ported-source-sets/effect-parser-blocks.json, out of LIBOBS_SOURCES
and the graphics module), and the shims are hidden by
rust-hidden.d/obs-graphics-effect_parser.txt. The C, hybrid and C
reference builds export the same 1767 symbols as master, the two shims
local (t) in each. test_effect_parser_blocks compiles ep_parse() and
the blocks' C into itself, so it keeps checking the C;
test_effect_parser_blocks_rust runs the same file with ep_parse()
compiled in and the blocks, ep_free() and the parameter type decisions
from the production staticlib, on the hybrid build only.

No intentional differences: the C already frees a dropped item's state
name and value on every path.

Refs #3550.

Co-authored-by: Lain <[email protected]>
Tier 2 layouts and Tier 3 parity for the struct and sampler-state
blocks against the original C.

The oracle (oracle/effect_parser_blocks.c) compiles
effect-parser-blocks.c.h unmodified under oracle_* names, on the C
cf-parser.h inlines with the C cf_adderror, cf_pass_pair, dstr_ncat,
lexer, preprocessor and strref_cmp, with helpers that lex and
preprocess a text into a struct effect_parser with that C, at a chosen
token, and free it as ep_free() does. Both sides get the same tokens;
the shim reaches libobs's cf_adderror and cf_pass_pair, obs-util's Rust
here as in the hybrid libobs.

shader::effect_parser_parity compares the layouts of struct ep_var
(with enum ep_var_type), ep_struct, ep_sampler and effect_parser, then
everything either side leaves in the parser: each struct, variable and
sampler (every field, the strings, darray counts and capacities), the
other collections, every error (text, file, row, column, level) and
where the cursor stopped. A kept value's own allocation size is not
compared (the C grows a dstr, the shim copies once; ep_free() frees
either). Inputs: 61 fixed texts (the C test's, Tier 1's and libobs's
own effects' blocks, with comments), every prefix of each, each from
every token as either block, and random token soups from the keyword
or from a random token. No intentional differences.

Mutation check: five planted mutants (an empty value kept, a space
pushed after each value token, end of text inside a value taken as a
skipped item, a pushed struct marked written, end of text at the peek
keeping the sampler) each fail one to three of the seven tests.

The port_comparison bench runs a vertex struct and a four-item sampler
state through both sides on the same tokens, freeing what each
iteration pushed.

Refs #3550.
A rust/README.md row for libobs/graphics/effect-parser-blocks.c.h
(obs-graphics::effect_parser: what the shims take over from the C, how
the core walks the C parser's cursor and reuses the shader parser's
struct block, what is characterized and kept, no intentional
differences, and its tests), and the blocks in the obs-graphics crate
row, its Cargo.toml description and its lib.rs module docs.
docs/rust-port/naming.md already lists the file since the move.

Fixes #3550.
@rdoupe
rdoupe force-pushed the fix/rust-port-port-the-effect-parser-582 branch from 39a07d8 to 79497b2 Compare November 4, 2026 00:17
@rdoupe
rdoupe marked this pull request as ready for review November 4, 2026 00:30
@rdoupe
rdoupe requested a review from zackees as a code owner November 4, 2026 00:30
@rdoupe
rdoupe added this pull request to the merge queue Nov 4, 2026
Merged via the queue into master with commit 60b38b1 Nov 4, 2026
44 checks passed
@rdoupe
rdoupe deleted the fix/rust-port-port-the-effect-parser-582 branch November 4, 2026 00:47
@rdoupe rdoupe mentioned this pull request Nov 4, 2026
6 of 7 tasks
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to Done in obs-rust Multiview Nov 4, 2026
@rdoupe rdoupe mentioned this pull request Nov 4, 2026
6 of 7 tasks
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 parser's struct and sampler-state blocks

1 participant