Repository navigation
Translator: tests, expected WGSL and stricter errors - #142
Merged
Merged
Conversation
Snippet tests for each rule of the supported Metal subset (validated with naga), golden WGSL for every module (DEEPFIELD_BLESS=1 cargo test --test golden rewrites it), and errors with file and line for what was silently accepted: unknown sampler properties or combinations, a mip level on read/get_width, builtins and struct constructors with the wrong argument count, kernels without a workgroup size, renames that match nothing. round now takes halves away from zero as in Metal. Local array-size constants are scoped to their block. CI runs the tests.
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.
Item T2 of the Rust review (#92). Until now the translator had no tests. It also accepted some Metal it couldn't translate faithfully, so the output could pass naga and still mean something else.
Tests (
shaders/msl2wgsl/tests)snippets.rs(46 tests): a small Metal snippet for each rule inrender-wgpu/wgsl/README.md, compared against the WGSL lines it should produce, with each translation validated by naga. Covered: literal spellings and conversions, splats, C's usual arithmetic conversions, compound assignment converting back, truthiness, casts and constructors, shifts, precedence and the parentheses WGSL needs,%andfmod, the ternary asselectand as a hoistedif, swizzle assignment,x++inside an expression, float++, copied parameters, switch case grouping, for/while/do-while/else-if,powguards,round/rint, renamed builtins,mix/clamp/selectsplats, macros andM_PI_F, reserved words, struct defaults, constant tables,threadreferences and array pointers, resource parameters (one copy per global), renames, the three samplers pluscoord::pixelandlevel(), texture read/write/size, and kernel, vertex and fragment entry points.test.metal:LINEand a clear message: matrices, templates, the comma operator, assignment inside an expression, switch fallthrough, several for steps,++in a loop condition or after&&, a vector component passed by reference, pointer arithmetic, a resource that isn't a plain name,isnan/isinf, vector ternary conditions, unknown names and functions, preprocessor and lexer errors, and an unclosed block.golden.rs: translates every module fromshaders/metaland compares it byte for byte withtests/golden/<module>.wgsl. It also validates each module with its bindings, as--checkdoes. Rewrite the expected files withDEEPFIELD_BLESS=1 cargo test --test golden(run fromshaders/msl2wgsl), the same variable the CLI golden test uses. The files are LF on every platform (.gitattributes).Stricter errors and fixes
linearClamp. That covers an unknown property, a missingfilter::linear(Metal's default is nearest),address::repeaton both axes, andmip_filter::linearwithout the repeating sampler. Properties with parentheses (max_anisotropy(4)) now parse, so the error names them.read(p, lod),get_width(lod)andwrite(v, p, lod)are errors; the translator used to drop the level.level()with no value is an error, where it used to panic.dot(a),mix(a, b),tex.read()).round: Metal rounds halves away from zero, WGSL'sroundrounds them to even. It now goes through ametal_round_*helper,sign(x) * floor(abs(x) + 0.5). This affects Neutron Star Merger (neutronMerger.metal:134) and Black Hole Merger (blackHoleMerger.metal:343); those are the only diffs in the generated WGSL.rintstill maps to WGSLround.int_consts: a localconst int Nused to size later arrays in every function parsed after it. It is now scoped to its block.MODULES.kernelsis an error; it used to get a silent 8x8 with no bounds check. The three bloom kernels are now listed explicitly with the same 8x8 and no bounds check, so their output is unchanged. A rename that matches no resource parameter of an entry point is also an error.file.metal:N:like the rest. An unclosed/*is reported instead of ignored. The parser errors that had no location now have one, and ausingline with no;no longer loops forever at the end of the file.BINDINGSmoved frommain.rstolib.rsso the tests can use it. The README lists the new rules and the test command.Verification
cargo testinshaders/msl2wgsl: 48 passed (46 snippet tests, 2 golden).cargo run --features check -- --check: all 7 modules ok.--outcomparison of every module: the only change is theroundhelper and its 2 call sites inframe.Notes
shaders/metal. Whichever of the two merges second needsDEEPFIELD_BLESS=1 cargo test --test goldeninshaders/msl2wgsland a commit of the newtests/golden/*.wgsl. After this PR, any.metalchange that alters the WGSL needs the same step..github/workflows/ci.yml: adds one step,cargo test --release --locked, to theshadersjob. B3 and T3 also edit this file; the change is a single step, so it should merge without trouble.render-wgpu/build.rswatches all ofshaders/, so a change to the golden files also triggers a render-wgpu rebuild (T3 narrows that watch).no-changelog.