Skip to content

fix(tools): fix ps_interp_modes linear/system pair classification, chunk bounds, and packed ISG1 matching - #4838

Open
Persudes wants to merge 2 commits into
ItsNotRexed:mainfrom
Persudes:tools/ps-interp-modes-fixes
Open

Persudes wants to merge 2 commits into
ItsNotRexed:mainfrom
Persudes:tools/ps-interp-modes-fixes

Conversation

@Persudes

Copy link
Copy Markdown
Contributor

Summary

Fixes five bugs in tools/ps_interp_modes.py and adds regression tests in tests/test_ps_interp_modes.py:

  • DXBC header-overlap bounds check (chunk_table()): Compute header_end = 32 + 4 * count and skip chunk table offsets with off < header_end instead of accepting bogus chunk headers overlapping the DXBC header or chunk offset table.
  • Zero bind-info stride validation (psv0_inputs()): Reject PSV0 chunks that declare resources > 0 with bind == 0 via ValueError.
  • // comment stripping (disasm_facts()): Strip // comments in addition to ; comments before matching v_interp_* instructions.
  • Linear & system-value pair classification (_linear_pair(), pair_class()):
    • Classify live noperspective generic/clip/cull inputs (kind in (0, 6, 7) with linear_* interpolation mode) as linear_* instead of falling through to flat_only.
    • Respect 7 > 5 > 4 precedence (linear_sample > linear_centroid > linear_center) for both noperspective inputs and SV_Position.
    • Exclude SYSTEM_KINDS from others so SV_Position alongside system-VGPR inputs stays linear_*.
  • Strict component mask matching (shader_row()): Set matches = col_matches (removing the or reg_matches fallback) so disjoint component-packed rows missing from ISG1 do not inherit a sibling element's semantic name.

Verification

  • python -I -B tests/test_ps_interp_modes.py -v: 37/37 passed (7 newly added regression tests confirmed failing before the fix and passing after).
  • ast.parse check on tools/ps_interp_modes.py and tests/test_ps_interp_modes.py: passed.
  • python -I -B tests/test_ctest_registration.py: 4/4 passed.
  • python -I -B scripts/check_no_game_data.py: passed.
  • git merge-tree --write-tree upstream/main HEAD, git diff --check, CRLF=0 check: all clean.

AI-assisted (Gemini, Antigravity); no copyrighted game files or dumps used.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

The new description of raw_bary at tools/ps_interp_modes.py:282 describes a result that shader_row() cannot produce.

Run this from the repository root at af1decfc on Windows:

py -3.14 -I -B -c "import sys;sys.path.insert(0,'tools');import ps_interp_modes as p;u,ij=p.disasm_facts('v_add_f32 v8, v0, v1');print(u,ij,p.pair_class([],[],u,ij))"

The result is {} set() none. The listing alone does not identify which values the hardware supplies in v0 and v1. There is also a general reason the result cannot be raw_bary. The only call to ij.add(src) records a non-mov interpolation in usage on the same path. Every such input returns from pair_class() before the if ij check. The existing unit test supplies an empty usage and a nonempty ij directly. The parser cannot return that combination.

The merge base has the same behavior. Please qualify the new description to state this limit. If the tool must detect direct barycentric reads, add a test through shader_row() with input metadata that identifies the I/J registers.

I ran py -3.14 -I -B tests/test_ps_interp_modes.py on Windows with CPython 3.14.7. All 37 tests pass on the head. All seven new tests fail against merge base d0835dd9. A separate check of all 27 mode combinations for generic, clip, and cull inputs passes on the head.

AI-assisted.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted independent CPU comparison of d0835dd and af1decf across all 12,101 cached retail pixel shaders. All classifier fields matched per shader. Both matched #2778: 10,260 perspective-centre, 1,841 no pair, and 77,245 reads of 101,024 declared inputs. No retail shader selected a linear pair, so synthetic controls remain necessary for positive linear-pair coverage. No GPU or archive used.

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Reviewed af1decf merged onto main de67135 on Linux (CPU only): questions

What I ran: tests/test_ps_interp_modes.py 37 passed, 11 subtests on the merged tree; Neighbours run on the merged tree (python3.13, pytest 9.2.0, RunPod Linux): tests/test_ci_registration.py 11 passed (4 subtests), tests/test_docs_links.py 6 passed, tests/test_text_encoding.py 2 passed, tests/test_docs_list_items.py 4 passed.
Negative control: replacing tools/ps_interp_modes.py with the main version while keeping the PR's tests gives 7 failed, 30 passed (test_chunk_offset_inside_header_is_ignored, test_zero_bind_size_with_nonzero_resources_is_refused, test_slash_comments_are_ignored, test_mode7_position_is_linear_sample, test_noperspective_generic_and_clip_inputs_classify_linear, test_position_plus_system_value_stays_linear, test_disjoint_component_packed_row_does_not_steal_sibling_name); restored, 37 pass. So each of the six behaviour changes has a test that is red before.
Extra probes on pair_class / disasm_facts (calling them directly): persp row + linear row gives persp_center (unchanged); noperspective center + centroid gives linear_centroid (was flat_only on main); center + sample gives linear_sample; position + constant arbitrary row stays flat_only; "attr0.x ;c" and "attr1.x//x" comments are dropped as intended. No wrong result found.

Findings:

  1. docs/design.md:572-574 (not touched by the PR) still says the pair is "linear only in the 4 PS that interpolate SV_Position through the parameter cache ... flat otherwise". After this PR pair_class also returns linear_center/centroid/sample for any read noperspective arbitrary/clip/cull row (tools/ps_interp_modes.py _linear_pair, "interpolates and linear" branch), so that sentence and the census totals next to it ("v0/v1 in 10,259 PS ...") may now be wrong. Please either update the paragraph or say that the corpus census is unchanged. The description gives no before/after census, and I cannot run it without the game's DXBC.
  2. tools/ps_interp_modes.py _linear_pair: when a shader reads noperspective rows of different modes (4 and 5, or 4 and 7), it silently picks the highest. The pair_class docstring says no shader reads a second pair, but nothing checks it; a "mixed" class or a refusal would be safer than guessing. Not a regression (main returned flat_only for those inputs), so low priority.
  3. shader_row now uses only col_matches (the fallback to register-only matches is gone). That is the intended fix and the new test covers disjoint components. There is no test for a PSV0 element whose cols are 0 (rmask 0), which takes the "all matches" path; I checked by reading that it still works, but it is unpinned.

Everything else I tried (chunk_table with offset exactly at header_end, count larger than the file, bind size 0 with resources 0) behaves correctly.

No game files.
AI-assisted (Claude Code).

@Persudes

Copy link
Copy Markdown
Contributor Author

Updated in 29419764 to address @shdann and @tedin7's review feedback:

  • raw_bary docstring (@shdann): Qualified pair_class()'s docstring in tools/ps_interp_modes.py to note that raw_bary applies when a caller supplies nonempty ij without non-mov v_interp usage (disasm_facts() itself only populates ij on non-mov v_interp_* lines).
  • Zero-cols ISG1 matching (@tedin7): Added ShaderRowTest.test_zero_cols_element_matches_register_isg1_entries in tests/test_ps_interp_modes.py pinning the cols=0 (rmask == 0) register-level ISG1 fallback path.
  • docs/design.md census note (@tedin7): Per @kacper-serewis's independent run across all 12,101 cached retail pixel shaders, all classifier counts are unchanged on the corpus (no retail shader selected a linear_* pair under the updated rule), so docs/design.md's corpus totals remain accurate.

Re-verified: python -I -B tests/test_ps_interp_modes.py -v passes 38/38 tests (38 subtests/checks across the module).

AI-assisted (Gemini, Antigravity); no copyrighted game files or dumps used.

@DenRakEiw

Copy link
Copy Markdown
Contributor

Tested on Windows 11 / Python 3.12.10 at head 29419764e87540b7ce76b46bbb48322e079ddba1.

  • Trial-merge onto origin/main: Clean (git merge --no-commit --no-ff and git diff --cached --check exited 0).
  • Tests:
    • python -I tests/test_ps_interp_modes.py: 38/38 passed

Code read: tools/ps_interp_modes.py tightens DXBC/PSV0 bounds (off < header_end, bind == 0), strips // comments in disasm_facts, handles noperspective inputs, mode-7 sample, and SYSTEM_KINDS in pair_class/_linear_pair, and drops the reg_matches fallback in shader_row so disjoint packed components do not inherit sibling ISG1 names or rw bits.

AI-assisted review (Gemini 4 Neon).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 2941976 merged with main 3faac55 on Linux (WSL), Python 3.12.16: pass

tests/test_ps_interp_modes.py: 38/38 pass.
Negative control: with tools/ps_interp_modes.py restored to main, 7 of the 8 new tests fail (slash comments, noperspective generic/clip, mode-7 position, position plus system value, chunk offset inside header, zero bind size, disjoint component-packed row). test_zero_cols_element_matches_register_isg1_entries already passes on main, so it is a regression guard and does not demonstrate a fix.
No issues found in the diff.

@memorypressure

Copy link
Copy Markdown

Verified against main 3faac55 (clean merge): compileall clean, all 38 tests in tests/test_ps_interp_modes.py pass under python -I (re-run). The chunk-table offset guard, zero-bind-size rejection, comment stripping and the linear pair classification changes behave as described. One flag for a maintainer: the ISG1 name matching change from col_matches or reg_matches to col_matches only is a semantic narrowing beyond crash-hardening — the two new tests pin the new intent, but register-only matches now stop matching. No game data; stdlib only. This branch has no CI checks reported. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 2941976 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_ps_interp_modes.py -> OK
  • Negative control (tools/ps_interp_modes.py swapped to upstream/main): FAILED (failures=7)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested 29419764 on Linux, CPython 3.14, CPU only, synthetic inputs, merged onto main dde82f27: pass

  • python -I tests/test_ps_interp_modes.py: Ran 38 tests, OK. With only the non-test files reverted to main: FAILED (failures=7), so the new tests exercise the change.
  • compileall and scripts/check_no_game_data.py on the changed files: clean.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting; claimed in #8047.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants