Skip to content

obs-graphics: Port the effect parser writer - #3769

Merged
rdoupe merged 9 commits into
masterfrom
fix/rust-port-port-the-effect-parser-643
Nov 7, 2026
Merged

rdoupe merged 9 commits into
masterfrom
fix/rust-port-port-the-effect-parser-643

Conversation

@rdoupe

@rdoupe rdoupe commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #3765, #3768
Fixes #3765
Fixes #3768

What and why

Ports the effect parser's shader writer, libobs/graphics/effect-parser-writer.c.h (moved out of effect-parser.c.h in #3766), to obs-graphics's effect_parser module, and fixes the generated main() of a shader function with two or more mapped params (#3768) as the port's one intentional difference.

The compile step calls ep_makeshaderstring() on each pass's vertex and pixel call. It writes the declarations of the params the call names, then the called function after every param, sampler, struct and function it depends on (each once), and a main() that returns the call; each uniform it declares goes into used_params, and the written flags are cleared afterwards.

  • obs-graphics: Test the effect parser writer (RED): Tier 1 against todo!() stubs. params, blocks, funcs, mains, calls and preset port test_effect_parser_writer one to one: the same 15 cases and expected text, starting from the parser ep_parse() leaves for each of its texts (bodies, dependency lists and call tokens as the C keeps them), less the parse-error and leak lines, which are ep_parse()'s. Edge tests add calls that write nothing, leading whitespace and the byte tested for it, the NULL compare, missing dependency names, the four var modifiers, negative and large array counts, a body starting with a NUL byte, the first-match lookups and main()'s struct params.
  • obs-graphics: Port the effect parser writer (GREEN): the safe core, one to one, every piece appended in the C's order with dstr_cat's and dstr_cat_dstr's rules. ep_getannotation has no caller and isn't ported; ep_write_main_params' name string and ep_write_main's copy of the call never reach the output and are gone.
  • obs-graphics: Swap in the effect parser writer: the hidden shim ep_makeshaderstring(). It reads the parser's four arrays in place, appends to the caller's struct dstr as the C grows it, on libobs's allocator through the existing private adapters, pushes each used name as a dstr_init_copy, and writes the flags back once the writer returns. The group becomes libobs.ported-effect-parser-writer (ported-source-sets/effect-parser-writer.json), and the shim is listed in rust-hidden.d/obs-graphics-effect_parser.txt. A new test_effect_parser_shader_call_rust runs the bug: effect parser: an unknown pass function leaks and breaks the next shader #3625 test with ep_parse() and the compile step compiled in and the writer from the production staticlib, so its shaders are the Rust writer's. The blocks, params and passes _rust tests stop compiling the writer's C.
  • obs-graphics: Compare the effect parser writer: Tier 2 layouts (ep_func, ep_param, ep_var, ep_struct, ep_sampler, dstr) and Tier 3 parity against an oracle_ build of the unmodified writer, with the funcs file's lookups and the C strref_cmp and dstr. The proptests build random parsers twice and compare the shader string (bytes, length, capacity), every used_params entry and every written flag. Their inputs: random names from a shared pool, bodies with NUL bytes, every ep_var_type and one past it, dependency graphs with cycles, random flags, prefilled shader strings, and calls with whitespace, NULL, empty and NUL-holding tokens. A port_comparison bench is included.
  • test: Expect commas between main()'s params (RED for bug: effect parser: main() gets two mapped params with no comma between them #3768) and obs-graphics: Put commas between main()'s params (GREEN): main() gets ", " between its mapped params. Every shader with at most one mapped param, as every stock effect has, comes out byte for byte the same. The parity test compares exactly those calls with the C's main() rewritten with the commas, and everything else unchanged. A docs/features.md line is included.
  • docs: Record the effect parser writer port: the rust/README.md row, the crate row, the Cargo.toml description and the lib.rs docs.
  • obs-graphics: Read the writer's parser in place: the core runs over two traits, WriterParser and CallTokens, read one field at a time where the C reads it. The shim implements them over the C items in place, so each read happens under the C's condition and each written flag is set and cleared where the C does. A call that returns early (no token array, no tokens, whitespace only or NULL) reads nothing of the parser. Before this, the shim copied the whole parser and call first and wrote the flags back at the end. edge_calls_read_nothing and edge_reads_in_c_order (every read, flag write and append of one call, in the C's order) check it; a planted early read and a moved flag write each fail them.
  • obs-graphics: Grow the writer's strings with brealloc: the shim's dstr_ensure_capacity() grows the shader string and each used name with brealloc, as the C does, through a new private adapter obs_graphics_brealloc() beside obs_graphics_bmalloc and obs_graphics_bfree. used_params grows through darray_ensure_capacity(), which itself uses a new bmalloc buffer, so its push is unchanged.

Decision: the four lookups the writer shares with the function parser (ep_getstruct, ep_getsampler, ep_getfunc_strref, ep_getparam_strref) are ported into the core with the same first-match rules, rather than called as C symbols. A Rust staticlib object calling back into hidden C would leave the Rust test binaries with nothing to link against. effect-parser-funcs.c keeps its C copies until its own port (#3623). Parity covers the lookups against the C ones.

Characterized, not endorsed, and kept: a const param is declared without its value; a sampler named twice adds a blank line each time; a uniform the call names that is already marked written isn't pushed; main() drops a param whose struct's first member is unmapped. A dependency name with no item, which the C would crash on and the parser never records, is skipped.

Ported code: Lain Bailey's (upstream obs-studio), credited on the GREEN commit. No upstream issue.

RED → GREEN

  • RED commit: 958d638. The 15 Tier 1 tests in shader::effect_parser_writer fail on the todo!() stubs (not yet implemented); --test-diff showed exactly those 15 failing, 6340 passing. RED for bug: effect parser: main() gets two mapped params with no comma between them #3768: 1961e17, where shader::effect_parser_writer::mains and edge_main_params_get_commas fail against the C-faithful writer (POSITIONfloat2).
  • GREEN: runs were on 0f8238d (the stamped head; the local gate attested lint, the Rust tests, cross and Tier 2 Linux there). local_gate.py --test-diff: 6721 of 6721 passed, 6 skipped. --lane lint ok, --lane cross ok, and soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets --locked -- -D warnings clean. The effect and shader cmocka tests pass 26 of 26 against hybrid libobs and libobs-c-reference, and 20 of 20 on the C build. The full ctest runs (before the two review commits) are 695 tests on C and 752 on hybrid and C reference. The 6 or 7 failures each had (display and load tests run without a display at -j8) pass alone under Xvfb on all three builds. The C, hybrid and C reference builds export the same 1767 symbols as master, with ep_makeshaderstring and the new obs_graphics_brealloc adapter local (t) in each. Mutation: the parity tests catch all 16 mutants that reach the shim (10 in the core, 3 in the shim, 3 of the comma). Of the other two, one is in Tier 1's plain ShaderString, which edge_body_bytes_go_in_unchanged catches, and one is equivalent. port_comparison passes its allocation check.
  • Real app (UI, protocol output or logging changes): N/A.

Test cost: new tests: longest 0.01 s at OBS_PARITY_CASES=reduced (all 21 in 0.09 s at full), peak RSS 9 MB over 25 runs, no own threads.

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): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

rdoupe and others added 9 commits November 6, 2026 15:37
Tier 1 for the port of the effect parser's shader writer
(libobs/graphics/effect-parser-writer.c.h) to obs_graphics::
effect_parser::writer, against todo!() stubs.

The core writes from a ShaderSource, the parser's params, structs,
functions and samplers as the writer reads them, each with its written
flag, into a ShaderOut, one dstr_cat or dstr_cat_dstr piece at a time,
with the uniform params it declares handed to push_used. A pass call is
its tokens before the end token, each with its text and the byte the C
tests for whitespace.

params, blocks, funcs, mains, calls and preset port
test_effect_parser_writer one to one: the same cases and expected text,
from the parser ep_parse() leaves for each of its texts (the function
bodies, dependency lists and call tokens as the C keeps them), less the
parse errors and leaks, which are ep_parse()'s. The edge tests add the
calls that write nothing, leading whitespace, the byte tested for it,
the NULL compare, missing dependency names (the C would crash; the port
skips them), the in, inout, out and uniform modifiers, a negative and a
large array count, a body that starts with a NUL byte, the first-match
lookups and main()'s struct params.

Refs #3765.
The safe core of the effect parser's shader writer
(libobs/graphics/effect-parser-writer.c.h) in obs_graphics::
effect_parser::writer: ep_makeshaderstring() and its helpers, one to
one, each piece appended in the C's order with the C's dstr rule
(dstr_cat skips an empty piece or one that starts with a NUL byte,
dstr_cat_dstr appends any non-empty one), so a shim's C string grows
as the C's does.

The lookups are the C's first-match rules: ep_getfunc and ep_getparam
of this file, and the function parser's ep_getstruct, ep_getsampler,
ep_getfunc_strref and ep_getparam_strref (strcmp, or strref_cmp for a
call token), which effect-parser-funcs.c keeps in C for its own use
until its port. A dependency name with no item, which the parser never
records and the C would crash on, is skipped. ep_getannotation has no
caller and is not ported; ep_write_main_params' name list and
ep_write_main's copy of the call never reach the output and are gone.

Nothing calls the core yet; the C ep_makeshaderstring() still writes
every shader.

Refs #3765.

Co-authored-by: Lain <[email protected]>
The hidden C ABI shim ep_makeshaderstring() in ffi::effect_parser,
which the compile step in effect-parser.c calls on each pass's vertex
and pixel call, over the safe writer. The hybrid libobs drops the C.

The shim reads ep->params, structs, funcs and samplers in place: their
strings, function bodies (len bytes of the dstr) and the call's tokens
up to the end token are borrowed, each token with the byte the C tests
for whitespace (*str.array, even when len is 0). Text goes onto the
caller's struct dstr as dstr_cat and dstr_cat_dstr put it, the buffer
grown as dstr_ensure_capacity grows it, on libobs's allocator through
the existing private adapters; each used param is a dstr_init_copy of
its name (an empty name the empty dstr) pushed as da_push_back pushes
it. The written flags go back into the items once the writer returns,
so a call that writes nothing leaves them as they were, and every other
one clears them. The lookups the writer shares with the function parser
are the core's ports, so the shim names no C symbol beyond the
allocator.

The writer's unity group becomes libobs.ported-effect-parser-writer
(its own ported-source-sets/effect-parser-writer.json, out of
LIBOBS_SOURCES and the graphics module), and the shim is hidden by
rust-hidden.d/obs-graphics-effect_parser.txt. The C, hybrid and C
reference builds export the same 1767 symbols as master, with
ep_makeshaderstring local (t) in each.

test_effect_parser_writer and test_effect_parser_shader_call compile
the C writer into themselves and keep checking it.
test_effect_parser_shader_call_rust runs the shader_call file with
ep_parse() and the compile step compiled in and the writer from the
production staticlib, so its shaders are the Rust writer's, on the
hybrid build only. The blocks, params and passes _rust tests no longer
compile the writer's C, which the staticlib now defines.

Refs #3765.
Tier 2 layouts and Tier 3 parity for the shader writer against the
original C.

The oracle (oracle/effect_parser_writer.c) compiles
effect-parser-writer.c.h unmodified under oracle_* names, with the
lookups it takes from effect-parser-funcs.c.h (that file compiled
whole, renamed), on the C strref_cmp, dstr and cf_adderror.

shader::effect_parser_writer_parity compares the layouts of struct
ep_func, ep_param (the fields the writer reads), ep_var (with enum
ep_var_type), ep_struct, ep_sampler and dstr, then runs both writers
on the same random parser, built twice: params, structs, samplers and
functions with random names from a shared pool, bodies that may hold
NUL bytes, param vars with every ep_var_type value and one past it,
dependency lists naming items of their kind (the C crashes on others),
random written flags, and a call that is no token array or tokens with
whitespace, NULL, item names, punctuation, empty tokens with whitespace
or not after them and tokens holding a NUL, before the end token; the
shader string may already hold text. The shader string (bytes, length,
capacity), each used_params entry and the list's count and capacity,
and every written flag afterwards must match. A second property calls
the parser's first function, so the whole writer runs more often.

Mutation: of 15 mutants planted in the core and the shim (a dropped
line break after a sampler or a struct, const and uniform swapped, no
reset, the param-deps break pair kept to two or more, main()'s mapping
rule inverted, the whitespace test on the text's byte instead of the
lead byte, %u printed signed, out written as inout, a function not
marked written before its dependencies, the shim's dstr_cat_dstr
dropping a NUL-first body, an empty used param given a buffer, an empty
token's lead read as 0), the parity tests catch all 13 that reach the
shim. The other two are Tier 1's: the plain ShaderString's cat_dstr,
which the shim does not use (edge_body_bytes_go_in_unchanged catches
it), and an empty-token test that is equivalent (a NUL-first token
compares as empty either way).

The port_comparison bench writes a typical pixel shader on each side.

No intentional differences.

Refs #3765.
A shader function with two or more mapped params gets a main() with
nothing between them: ep_write_main_params() writes each straight after
the one before, and puts ", " only between the names it collects in a
string ep_write_main() never uses. So an effect whose vertex or pixel
function takes float4 pos : POSITION and float2 uv : TEXCOORD0 gets
float4 main(float4 pos : POSITIONfloat2 uv : TEXCOORD0) and fails to
compile, with the error in code the effect file doesn't contain.

The writer port fixes it as its intentional difference: Tier 1's
mains_cases[0] and [1], which test_effect_parser_writer pins as the C
writes them (characterized, not endorsed), now expect the comma, and
edge_main_params_get_commas checks three mapped params after a dropped
one. Both fail against the port as the C writes it.

Refs #3768.
The writer port writes ", " between the mapped params of the main() it
writes for a pass call, so an effect whose vertex or pixel function
takes two or more separately mapped params (float4 pos : POSITION,
float2 uv : TEXCOORD0) now compiles. The C wrote each straight after
the one before it. A shader with at most one mapped param, as every
stock effect has, comes out byte for byte the same.

It is the port's one intentional difference, listed in the core's and
the parity test's docs: for exactly the calls whose function has two or
more mapped params, shader::effect_parser_writer_parity compares the
C's shader string with the end of its main() rewritten with the commas
(and not its capacity, which follows the length). Three planted
mutants of the comma (none, "; ", one before the first param) fail it.
test_effect_parser_writer keeps pinning the C writer's output.

Refs #3765.
Fixes #3768.
The rust/README.md row for libobs/graphics/effect-parser-writer.c.h
(swapped in hybrid libobs, what the shim reads and writes, what is
characterized and kept, the intentional difference of #3768, and the
tests), and the writer in obs-graphics' crate row, Cargo.toml
description and lib.rs module docs. docs/rust-port/naming.md lists the
file under obs-graphics since the extraction.

Refs #3768.
Fixes #3765.
The shim copied the parser's four arrays and the call's tokens into a
model before the writer looked at the call, and wrote the written
flags back once it returned. So a call with no token array, no tokens,
only whitespace or NULL, for which the C returns before it reads
anything of the parser, still read all of it, and every other call read
fields the C reads only on some paths, and set its flags later than the
C does.

The core now runs over two traits, WriterParser and CallTokens, read one
field at a time where the C reads it: each loop bound on each pass, a
flag before its item, a var's mapping before its type, a token's lead
byte only while skipping whitespace. Each written flag is set and
cleared where the C sets and clears it. The shim implements them over
the C items in place, and ShaderSource and a slice of CallToken over
plain data for Tier 1.

edge_calls_read_nothing checks that the calls that return early read
nothing of the parser, and edge_reads_in_c_order pins every read, flag
write and append of one call, as the C makes them; a planted read of
the function count before the call is checked, and a flag set before
its declaration's ";\n", each fail them. The shader strings, used
params and flags are unchanged: the parity tests pass as before.

Refs #3765.
dstr_ensure_capacity() grows a string with brealloc(). The shim grew
the shader string and each used param's copy with a new bmalloc buffer,
a copy of the whole old capacity and a bfree instead: the same bytes
and counts once it returned, but another allocator call and a second
buffer each time, even where brealloc grows in place.

A private adapter, obs_graphics_brealloc(), beside obs_graphics_bmalloc
and obs_graphics_bfree in obs-graphics-adapter.c.h, so obs-graphics
still names no allocator symbol of obs-util's, and the shim's
dstr_ensure_capacity() calls it as the C does. used_params grows
through darray_ensure_capacity(), which itself copies into a new
bmalloc buffer, so its push is unchanged.

Refs #3765.

Local-Gate: v1 tree=f80a042002995c7fb468213326ad88c1bd45f985 secs=2204 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1794002420,"gate":"rust/all/fmt","host":"linux-x86_64","key":"a6e0169ee2e5ddebc9d40fac938cf3317ebdf125de8966d110a65ab740d321e0","lane":"lint","parents":["ed9b36d4cf0614e27d833b1d73ce1e72867935b4"],"secs":48,"stamp":"a2892f19afa63812cdacf958f23557ae","tree":"f80a042002995c7fb468213326ad88c1bd45f985","v":1,"via":"run"}
Ci-Attestation: {"at":1794002420,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"a6e0169ee2e5ddebc9d40fac938cf3317ebdf125de8966d110a65ab740d321e0","lane":"lint","parents":["ed9b36d4cf0614e27d833b1d73ce1e72867935b4"],"secs":48,"stamp":"62ed93fc70ce6c98650882524cc7f980","tree":"f80a042002995c7fb468213326ad88c1bd45f985","v":1,"via":"run"}
Ci-Attestation: {"at":1794002420,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"ca75af2cd2a7703483564e66a2b2f88fabaaaa5daadac89b49699c37a02aa8c1","lane":"rust-test","parents":["ed9b36d4cf0614e27d833b1d73ce1e72867935b4"],"secs":111,"stamp":"ccf8c7cba5804803201ba96480e088ec","tree":"f80a042002995c7fb468213326ad88c1bd45f985","v":1,"via":"run"}
Ci-Attestation: {"at":1794002420,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"d8b826a16c1704d07bfe60869479f3984a1a234309ec63d6af7805f3aeabfaf5","lane":"cross","parents":["ed9b36d4cf0614e27d833b1d73ce1e72867935b4"],"secs":60,"stamp":"c380d6c0acf4535ab3b1afd727ece7f6","tree":"f80a042002995c7fb468213326ad88c1bd45f985","v":1,"via":"run"}
Ci-Attestation: {"at":1794002420,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"d8b826a16c1704d07bfe60869479f3984a1a234309ec63d6af7805f3aeabfaf5","lane":"cross","parents":["ed9b36d4cf0614e27d833b1d73ce1e72867935b4"],"secs":60,"stamp":"9470cbe5e43665f1312457105c71642e","tree":"f80a042002995c7fb468213326ad88c1bd45f985","v":1,"via":"run"}
Ci-Attestation: {"at":1794002420,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"d8b826a16c1704d07bfe60869479f3984a1a234309ec63d6af7805f3aeabfaf5","lane":"cross","parents":["ed9b36d4cf0614e27d833b1d73ce1e72867935b4"],"secs":60,"stamp":"772a138fd322d9363d150d3d53b93c8b","tree":"f80a042002995c7fb468213326ad88c1bd45f985","v":1,"via":"run"}
Ci-Attestation: {"at":1794002420,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"d8b826a16c1704d07bfe60869479f3984a1a234309ec63d6af7805f3aeabfaf5","lane":"cross","parents":["ed9b36d4cf0614e27d833b1d73ce1e72867935b4"],"secs":60,"stamp":"ca50de70fbe52ebdce9136f04ba84809","tree":"f80a042002995c7fb468213326ad88c1bd45f985","v":1,"via":"run"}
Ci-Attestation: {"at":1794002420,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"80866a97cbb8f9fc3a12f5de4bf88a95bdbf63f1fb67fad5a60aca64854dd01e","lane":"tier2-linux","parents":["ed9b36d4cf0614e27d833b1d73ce1e72867935b4"],"secs":2204,"stamp":"a7b72bb02ed6eaf5328144dcfc68b851","tree":"f80a042002995c7fb468213326ad88c1bd45f985","v":1,"via":"run"}
@rdoupe
rdoupe force-pushed the fix/rust-port-port-the-effect-parser-643 branch from ca6db6d to 0f8238d Compare November 6, 2026 22:01
@obs-rust-project-manager obs-rust-project-manager Bot moved this to In progress in obs-rust Multiview Nov 6, 2026
@rdoupe
rdoupe marked this pull request as ready for review November 7, 2026 02:34
@rdoupe
rdoupe requested a review from zackees as a code owner November 7, 2026 02:34
@rdoupe
rdoupe added this pull request to the merge queue Nov 7, 2026
@rdoupe

rdoupe commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author

Handed off to the coordinator

Merged via the queue into master with commit 6a3c7b3 Nov 7, 2026
40 checks passed
@rdoupe
rdoupe deleted the fix/rust-port-port-the-effect-parser-643 branch November 7, 2026 02:45
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In progress to Done in obs-rust Multiview Nov 7, 2026
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.

bug: effect parser: main() gets two mapped params with no comma between them rust: Port the effect parser's shader writer

1 participant