Skip to content

Link sparse mesh exports to fragment inputs using FSSM stage records - #4868

Open
jeremygold02 wants to merge 4 commits into
ItsNotRexed:mainfrom
jeremygold02:codex/mesh-ps-link
Open

jeremygold02 wants to merge 4 commits into
ItsNotRexed:mainfrom
jeremygold02:codex/mesh-ps-link

Conversation

@jeremygold02

@jeremygold02 jeremygold02 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

An original fragment input ordinal and its paired mesh export can have different locations. The existing structural checker accepts a wrong connection when the intervening mesh output has the same type. The new opt-in shader_link_check.py link-mesh command matches interface keys from the two native headers and checks the fragment's independently stored one-based slot. It writes a separate fragment module, changing only the affected input Location literals.

python -I tools/shader_link_check.py link-mesh --ms mesh.spv --ms-fssm mesh.fssm --ps pixel.spv --ps-fssm pixel.fssm --out paired-pixel.spv

Scope. The command accepts the ordinary single-entry fragment translation, its native MeshEXT producer and their corresponding FSSM sidecars. It checks the reflected float4 declarations before and after linking. Ambiguous native keys, conflicting slot fields, mismatched tables and nonidentity dense permutations produce an error. Runtime selection of a graphics pipeline remains outside this command.

Verification on commit f65c85953df073036ef6b9aceb74f568c55b46bd

  • CPU tests at the PR head on Intel Core Ultra 7 265K, Windows 11 Home 25H2 build 26200.9974. All 130 tests passed across test_shader_link_check.py, test_fssm_header.py and test_spirv_interface.py. Hand-built fixtures cover a type-compatible skipped output, identity, both byte orders, conflict rejection, render-target preservation and protecting inputs and the last good output.
  • Three private stage pairs checked on the CPU using main af70fc78 for the new pixel translations and rdna2spirv: wave64 emulation clears and frees %x_buf with one writer per word (SHADER-708, data race under GPU-assisted validation) #4247 13c732c3 for the two new mesh translations. The skipped-export cases each changed a single byte. The identity control stayed byte-identical. All three fragment modules passed spirv-val --target-env vulkan1.2.
  • An independent rendering check exercised the same one-location correction on an NVIDIA GeForce RTX 5070, driver 621.01, subgroup 32, Intel Core Ultra 7 265K, Windows 11 Home 25H2 build 26200.9974. Across two draws with a synthetic 16×16 ramp, all 2,352 covered pixels matched a CPU prediction from the selected mesh export within 0.002 (maximum error below 0.000615). Zero matched the skipped export at that tolerance. With the original dense assignment, all 2,352 matched the skipped export and zero matched the selected one. This measures one compiled pair under diagnostic inputs. The FSSM interpretation has been checked on three pairs.
  • The coordinating tester independently ran the published linker at f65c8595 and passed its output directly to the same RTX 5070. The generated module matched the SHA-256 of the independently corrected reference module. In both draw orders, all three complete 384×384 attachment readbacks matched the reference byte for byte. The run reported 84,847 covered pixels.

Bounded sidecar reads at 9644c10a50dfbf64f2122bd3b0addb60f08c9e69

I reproduced the allocation finding on Windows 11 Home 25H2 build 26200.9974, Intel Core Ultra 7 265K, CPython 3.12.10. With a 4 MiB synthetic file for each FSSM sidecar, peak traced Python allocations fell from 8,456,391 bytes to 68,965 bytes. The synthetic fragment output was byte-identical at every tested file size. The command reads at most 672 bytes from the mesh sidecar and 576 bytes from the pixel sidecar.

All 131 targeted CPU tests passed. The new control accepts 32 complete pixel inputs, rejects both a partial and a complete 33rd input, rejects a truncated mesh count, and checks that a failed link preserves the last good output. Relinking the two private sparse pairs reproduced both previously validated modules byte for byte. This revision was tested on the CPU.

The existing VS-only link comparison in #4119 uses DXIL signatures. Coordination claim

AI-assisted by GPT-6.2 Astra.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

At f65c8595, link_mesh reads both FSSM sidecars in full and keeps them in memory together. It only needs header records. With the PR fixture and a zero-filled tail on each sidecar, increasing each file from 1 MiB to 4 MiB raises peak traced Python allocations from 2,163,287 to 8,452,264 bytes. Every run emits the same fragment module.

I repeated the command with bounded reads. The mesh read ends at 0x2a0. The pixel read ends at 0x138 + 33 * 8. The extra record lets the existing check reject an input with more than 32 records. Peak allocation for the 4 MiB case drops to 183,970 bytes. Please limit the two sidecar reads in this command.

All 130 CPU tests in test_shader_link_check.py, test_fssm_header.py, and test_spirv_interface.py pass on Windows with CPython 3.14.7. The new command is absent from merge base af70fc78. The PR link test file reports six errors there. The measurement uses synthetic headers and covers CPU linking.

Run the following script from the checkout root with py -3.14 -I -B probe.py.

import gc, sys, tempfile, tracemalloc
from pathlib import Path
from unittest.mock import patch
sys.path.insert(0,str(Path("tests").resolve()))
from test_shader_link_check import NativeMeshLinkTest, link
with tempfile.TemporaryDirectory() as d:
    paths=[Path(d)/name for name in ("m.spv","p.spv","m.fssm","p.fssm")]
    args=["link-mesh"]
    for flag,p,src in zip(("--ms","--ps","--ms-fssm","--ps-fssm"),paths,NativeMeshLinkTest().fixture()):
        p.write_bytes(src)
        args.extend((flag,str(p)))
    out=Path(d)/"out.spv"
    args.extend(("--out",str(out)))
    original=Path.read_bytes
    def bounded(p):
        if p in paths[2:]:
            with p.open("rb") as f:
                return f.read(0x2a0 if p==paths[2] else 0x138+33*8)
        return original(p)
    for size in (0,1048576,4194304):
        for p in paths[2:]:
            with p.open("r+b") as f: f.truncate(max(size,p.stat().st_size))
        for read in (original,bounded):
            with patch.object(Path,"read_bytes",read):
                gc.collect()
                tracemalloc.start()
                assert link.main(args)==0
                peak=tracemalloc.get_traced_memory()[1]
                tracemalloc.stop()
            if size==0: expected=out.read_bytes()
            assert out.read_bytes()==expected
            print(size,read.__name__,peak)

AI-assisted.

@whattheflop whattheflop 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.

Independent Windows CPU verification, AI-assisted, on uv CPython 3.12.13. I reused a synthetic fragment with four dense float4 inputs. A seven-export producer with skipped semantic keys at locations 1, 3 and 5 passed the old type-only check, even though three bindings were wrong. This head linked the inputs to 0, 4, 6, 2. A genuinely sparse producer linked them to 0, 2, 4, 6, and the old checker accepted both linked outputs.

Each linked binary changed exactly three bytes in input Location literals. Color outputs at 1 and 3, FragCoord, FragDepth, input modules and sidecars all stayed unchanged. Six CLI runs succeeded, including repeat writes with byte-identical results. All seven validator invocations passed for Vulkan 1.2 (five unique binaries). Synthetic inputs only.

@DenRakEiw

Copy link
Copy Markdown
Contributor

Tested head 9644c10a50dfbf64f2122bd3b0addb60f08c9e69 on Windows 11 / Python 3.12.10.

  • Trial-merge onto origin/main: clean (git diff --cached --check passed).
  • Tests:
    • python -I tests/test_shader_link_check.py: 51 passed (OK).
    • python -I tests/test_docs_design_rows.py: 4 passed (OK).
  • Code read: mesh_pixel_slots and link_mesh_fragment cleanly match native FSSM mesh export and pixel input tables, reject ambiguous dense permutations or truncated/conflicting records, and rewrite only direct Location literals on standalone float vec4 fragment inputs while atomically publishing --out.

AI-assisted review (Gemini 4 Neon).

@kacper-serewis

Copy link
Copy Markdown
Contributor

BUILD-886 independent CPU check at f65c859 using stored pass selectors. The two databases yielded 17,145 two-stage mesh/pixel pass rows, 12,221 distinct pairs. We selected ten header-eligible pairs and independently checked their 20 cached inputs against the stored program records. In the resulting 10 by 10 matrix all 16 authored combinations linked (7 unchanged, 9 remapped). Of 84 combinations absent from the pass records, 45 also linked (43 unchanged, 2 remapped), and 39 failed. All 20 stage modules and all 61 emitted fragments passed Vulkan 1.2 spirv-val. Link success establishes interface compatibility; the independent selectors establish authored membership. The matrix was selected for eligibility. Runtime draw selection remains unmeasured. AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 9644c10 merged with main 3faac55 on Linux (WSL2) / CPython 3.12.16 (-I): pass

Counts: tests/test_shader_link_check.py 51 run / 51 pass / 0 fail; test_fssm_header.py 42 pass; test_spirv_interface.py 38 pass; all tests/test_docs_*.py pass (docs/design.md is touched). CPU only, synthetic fixtures.

Negative control: with tools/shader_link_check.py and tools/fssm_header.py taken from main, test_shader_link_check.py gives 51 run, 7 errors (the NativeMeshLinkTest cases, including both byte orders); with the PR's files all pass.

By hand: link-mesh on garbage inputs exits 2 with expected both native FSSM headers, writes no output file, and --out equal to an input is refused (linked module would overwrite an input file). MAX_MESH_PARAMS = (0x29c - 0x1dc) // 6 = 32, which matches the table size in the new docs paragraph.

@memorypressure

Copy link
Copy Markdown

Verified head 9644c10 against main 3faac55 at check time: merges cleanly (branch base is current main tip), compileall clean, tests/test_shader_link_check.py passes 51 tests under python -I (new link-mesh coverage: skipped export, identity, both byte orders, truncated-table and conflict cases), and test_fssm_header/test_spirv_interface plus the docs guards show no regressions. Fixtures are synthetic; text-write and repo-path rules check out. One observation: the design.md entry is prose under Pixel-shader inputs rather than a contract-table row — fine for a linker tool, worth a maintainer confirmation. CI was pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 9644c10 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_shader_link_check.py -> OK
  • Negative control (tools/shader_link_check.py swapped to upstream/main): FAILED (errors=7)

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

@NJ1912

NJ1912 commented Nov 23, 2026

Copy link
Copy Markdown

Code read of 64fe29d1 (merges cleanly onto main 167feec5): the matching follows the translator's conventions and stops with an error on every inconsistency I traced; notes on test coverage and format evidence.

  • Conventions: the fragment input %in_attr{n} (FSSM row n) is a v4float with a direct OpDecorate … Location {n} (tools/rdna2spirv.py:13149-13150, :14363-14364, :14691-14692), and the mesh output %ms_out_param{n} gets Location {n} from exp param{n} (:14346). So the in-place patch of word offset + 3, keyed by input ID (tools/shader_link_check.py:319-320), only moves the intended inputs; a render target with the same Location number is left alone. The head is a merge of main and all test reports above are on f65c8595/9644c10; its only conflict hunk (tools/shader_link_check.py:236-254) keeps main's os.path.samestat alias check and adds the label argument.
  • Untested guards: every fixture declares float4 inputs and mesh outputs equal to the export table (tests/test_shader_link_check.py:128-139), so no test reaches the parameter_ids rejections (tools/shader_link_check.py:288-294), the mesh-side half of the set check (:307) or the non-direct-Location error (:321-322). parameter_ids is, for example, what refuses a --bary-fallback fragment, whose vec3 %bary takes a free Location (tools/rdna2spirv.py:14660-14662). One negative case each (a vec3 or Component-decorated input, an extra mesh output) would pin them.
  • Format evidence: docs/design.md:598-599 states the 0x1dc/0x29c mesh table from three inspected pairs. The code also relies on byte 1 being unique in the mesh table (tools/fssm_header.py:147) and on pixel byte 3 equalling the row position (tools/fssm_header.py:158), which the new paragraph doesn't state; tools/fssm_header.py:9 describes byte 3 as "running index (same semantic id, next index)". Both checks fail closed rather than mislink, so this limits what the command accepts, not its correctness. Does byte 3 == row position, with byte 1 unique, hold across the corpus? AGENTS.md asks for corpus counts; a count over the MS and PS headers (docs/design.md:569, :630) would show how many pairs the command can take and could go into the paragraph.

Not run: code read only, no tests run. AI-assisted review.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 64fe29d merged with main e0816d6 on Linux WSL2 (Python 3.12.16, -I): pass
Real merge was clean (docs/design.md, tests/test_shader_link_check.py and tools/shader_link_check.py auto-merged). tests/test_shader_link_check.py: 56 tests OK; tests/test_fssm_header.py: 42 tests OK.
Negative control: with tools/shader_link_check.py and tools/fssm_header.py reverted to main, test_shader_link_check.py fails with 7 errors, so the new tests discriminate.

@jeremygold02

Copy link
Copy Markdown
Contributor Author

At 8bb648b5b, three synthetic tests cover the guards raised in the review. The linker rejects a plain vec3 and a vec3 packed at Component 1, a translated mesh with an extra export, and a fragment input whose Location comes from a decoration group. In the last case reflection recovers Location 0, but moving it to native slot 2 requires a direct decoration. The existing identity and sparse success cases also pass.

Both field checks held throughout their supported table shapes in the Xbox 1.0.2.1 shader package.

Check across both SGD5 databases Result
Pixel byte 3 equals the zero-based row position All 10,307 complete tables with 1–32 kind-6 inputs, out of 12,101 pixel programs
Mesh byte 1 is unique within each table All 5,637 tables with 1–32 entirely kind-6 exports, out of 9,282 mesh programs

I resolved the group-0 and group-1 selectors of each stored technique pass, checked their DXIL stages, and counted each distinct MS/PS ordinal pair once per database. The 17,145 authored pass occurrences selected 12,221 distinct pairs. Using the default native payload for each program, mesh_pixel_slots returned 5,385 identity mappings, 1,423 sparse mappings and 5,413 rejections. An independent check of the header bytes agreed on every decision and slot list. These counts measure native table compatibility. Linking a translated pair also requires its SPIR-V interface to pass the declaration and Location checks.

To reproduce the synthetic checks, run python -I -m pytest -q -ra tests/test_shader_link_check.py tests/test_fssm_header.py tests/test_spirv_interface.py. The PR head passed 141 tests and 121 subtests. A clean trial merge with main dde82f27e passed 144 tests and 123 subtests. Tested on Windows with Python 3.12.10 and an Intel Core Ultra 7 265K. Both GPUs were idle (NVIDIA GeForce RTX 5070, driver 32.0.16.2101, and Intel Graphics, driver 32.0.101.9150).

AI-assisted by GPT-6.2 Astra.

@skorex

skorex commented Nov 23, 2026

Copy link
Copy Markdown

Tested 8bb648b on Linux (WSL), Python 3.13: pass, 1 issue (docs wording).

Ran on the head: python3 -I tests/test_shader_link_check.py (56 tests OK), tests/test_fssm_header.py (42 OK), tests/test_spirv_interface.py (OK), all tests/test_docs_*.py (OK), python3 -m compileall -q on the changed files (clean), git diff --check (clean).
On the tree from git merge-tree with main dde82f27 (clean merge): test_shader_link_check.py 59 OK, test_fssm_header.py 42 OK, test_spirv_interface.py 43 OK. Negative control on that tree: with tools/shader_link_check.py and tools/fssm_header.py from main, test_shader_link_check.py gives 11 errors.
Not run: the corpus counts added to docs/design.md (they need a game copy), anything on a GPU.

Delta since 64fe29d1, the head checked before: three new rejection tests (vec3 / Component 1 input, an extra mesh export, a Location from a decoration group), the corpus paragraph in docs/design.md, and the byte-3 wording in tools/fssm_header.py. The three tests pin guards that were already in 64fe29d1, so they pass there too; they are regression pins, not fixes.

  1. tools/fssm_header.py:9 now calls byte 3 the "row index (equals the zero-based table position ...)", but docs/design.md:631 (the tools/fssm_header.py paragraph under the PS rows) still says "byte 3 the running index". The two now describe the same field differently. Suggest changing that sentence to the new wording (or a short "row index; equals the table position in the measured tables") so the docstring and design.md agree.

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.

10 participants