Skip to content

sgd5_pass_pipelines: decode the rasterizer, depth/stencil and blend rows each pass selects - #5342

Open
Pipodi wants to merge 9 commits into
ItsNotRexed:mainfrom
Pipodi:pass-states
Open

Pipodi wants to merge 9 commits into
ItsNotRexed:mainfrom
Pipodi:pass-states

Conversation

@Pipodi

@Pipodi Pipodi commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-888 (build the graphics pipelines the effect database defines, claim). This is its render-state step.

Depends on: #5207 (stacked on it: this branch contains #5207's three commits; the new work is the commits from 8aa02e7 on).

What and why

Each stored SGD5 pass selects one rasterizer, one depth/stencil and one blend row (pass bytes 6, 7, 8; docs/sgd5_render_states.md). The rows were located but not read. To build each pass's pipeline with the game's own states, the measured fields need to be decoded, and the ones the data doesn't pin need to stay raw.

  • tools/sgd5_effects.py: Effect.tables keeps (count, first_row, width) for the four state tables. They are recorded in the existing width loop, so there is still one parser.
  • tools/sgd5_pass_pipelines.py:
    • --passes rows gain raster_state, depth_state and blend_state, with null for an empty table.
    • --states FILE writes the distinct decoded rows with the number of passes that use each.
    • A selector at or past the table's row count, a nonzero selector into an empty table, and a decoded field outside its measured range are errors (exit 1). The bytes kept raw are not range-checked.
    • Passes that select the same row share one decoded object, decoded once per effect (found by @shdann: a per-pass copy took a 10,000-pass census from 12.5 MB to 49.8 MB peak; now 14.3 MB).
    • The manifest output is unchanged.
  • docs/sgd5_render_states.md: an offset table from the byte distributions over every stored row (64 rasterizer, 124 depth/stencil, 398 blend), and the per-pass distributions.

What the data pins, and what stays raw:

  • Rasterizer: cull mode (byte 1, values 1..3, named none / front / back by the D3D11 values). Fill mode is 3 in every row, kept as a raw integer. The front-face flag is not pinned: three bytes are constant 1 and none varies. So the winding that front or back culls is not established, and those bytes stay raw.
  • Depth/stencil:
    • Stencil enable (byte 3) is pinned: it is 0 in exactly the 18 rows whose stencil pass-op bytes are 1, and 1 in the 106 with 3.
    • Depth enable and write (bytes 0, 1) follow the D3D11 order. The pairs are (1,1) 106, (1,0) 9, (0,0) 8, (0,1) 1. Both orders are legal (depth off also turns writes off), so the pairs don't pin the order: it is the D3D11 reading, and a pipeline sets the depth write only when depth is enabled (found by @King1565).
    • The compare function (byte 2, 1..8) is named in the D3D11 order, and the doc marks that as a reading. greater_equal is the most used, which fits a reversed depth range.
    • Bytes 4..23 stay raw.
  • Blend: per-target enable (byte 0 of each 8-byte entry) and write mask (byte 7, 0..15). Blend factors and operations, and the 4-byte prefix, stay raw.

Tested

Windows 11, own 1.0.2.1 copy, Python 3.13. CPU only.

  • Real run: 28,798 passes. Selected rows are non-null in 1,833 passes (rasterizer), 2,280 (depth/stencil) and 17,116 (blend); the rest select an empty table (26,965, 26,518 and 11,682 passes). In use: 3 rasterizer, 10 depth/stencil and 21 blend rows, plus the empty table each. The doc has the full value distributions. The manifest is byte-identical to tools: sgd5_pass_pipelines.py turns every stored SGD5 pass into a shader_link_check batch manifest #5207's; --passes and --states are byte-identical before and after the shared-row change.
  • python -I tests/test_sgd5_pass_pipelines.py: 11 tests OK. Six are new, on synthetic databases from tests/test_sgd5_effects.py's builders, which take a states= argument now. They cover a decoded row of each kind, an empty table giving null, an out-of-range selector on each of bytes 6/7/8, a nonzero selector into an empty table, rows outside the measured ranges for all three kinds, --states counts, and passes on one row sharing its decoded object.
  • tests/test_sgd5_effects.py (OK, 1 package test skipped), tests/test_docs_links.py, tests/test_text_write_line_endings.py, python -m compileall -q tools tests: OK.
  • No translator change, so no corpus run.

AI-assisted: yes (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. Counts and value ranges only; the test rows are synthetic; the decoded rows stay in the user's work folder.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. N/A: no translator change.

🤖 Generated with Claude Code

Pipodi and others added 7 commits November 23, 2026 09:48
…der_link_check batch manifest

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… keep only the stage sets shader_link_check takes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ch batch refuses

Reported by whattheflop in ItsNotRexed#5207: an empty --spirv-dir left no set, the tool wrote
{"pipelines": []} and exited 0, and shader_link_check.py batch then exited 2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…offset

Reported by kacper-serewis in ItsNotRexed#5207: with such entries the selected shader depended on the
index order. The same entry listed twice is still accepted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… blend rows

Locate the four state tables per effect in sgd5_effects, decode the fields the stored rows pin down (cull mode,
depth enable/write/compare, stencil enable, blend target enable and write mask; the rest stays raw), add
raster_state/depth_state/blend_state to the --passes rows and a --states file. Offsets and counts in
docs/sgd5_render_states.md. The manifest is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… and the open winding behind cull names

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mes are the D3D11 reading, range errors for depth and blend tested

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted review of 30c9e595 on Windows, CPython 3.14.7.

The default manifest path retains a separate copy of the decoded render states for each pass. The input below has 10,000 passes in one effect. All passes select the same three state rows. The input is 245,760 bytes. The caller requests neither --passes nor --states.

Implementation Rows returned Peak bytes measured by tracemalloc
Stacked parent a4a87bab 10,000 12,503,382
This head 10,000 49,767,766

The manifest needs the stage keys. It does not use the decoded state objects. Cache each decoded row by table and selector within its effect. This lets repeated passes share one decoded row and preserves range checks. The synthetic effects and pipeline tests pass (19 pass, one package test skips).

To repeat, run this script with py -3.14 -I from the repository root at this head.

import sys, tempfile, tracemalloc, subprocess, types
from pathlib import Path
sys.path[:0] = ['tests','tools']
import test_sgd5_pass_pipelines as t
import sgd5_pass_pipelines as head
prior = types.ModuleType('prior')
prior.__file__ = head.__file__
exec(subprocess.check_output(['git','show','a4a87bab:tools/sgd5_pass_pipelines.py']),prior.__dict__)
passes = [t.PASSES[0]] * 250
_, _, index = t.build(passes)
item = t.effect('Mock',{0:(0,1),1:(0,1),2:(0,),5:(0,)},[('T',passes)]*40)
data,layout = t.database(t.NAMES,[item])
with tempfile.TemporaryDirectory() as d:
    path = Path(d)/'synthetic.bin'
    path.write_bytes(data)
    for tool in (prior,head):
        tracemalloc.start()
        rows,counts = tool.census(path,index,[layout])
        print(len(data),len(rows),tracemalloc.get_traced_memory())
        tracemalloc.stop()
        del rows

…ed object

Reported by shdann in ItsNotRexed#5342: each pass kept its own copy of the decoded rows, so a census of
10,000 passes on one row peaked at 49.8 MB against 12.5 MB before the decoding. Rows are now
decoded once per effect and selector (14.3 MB there); range checks are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Pipodi

Pipodi commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @shdann, good find, and thanks for the script. Fixed in c4767f6: each effect keeps one decoded object per (table, selector), and its passes share it; range checks are unchanged. With your script: parent a4a87ba 12.4 MB peak, 30c9e59 49.8 MB, now 14.3 MB. A new test checks that two passes on one row get the same object. On the real archive, --passes, --states and the manifest are byte-identical before and after. Added to the body.

AI-assisted (Claude Code).

@Kev-Ball

Copy link
Copy Markdown

At c4767f62, 316 link-compatible shader combinations each select more than one render-state tuple on my 1.0.2.1 copy. This gives a count to check when SHADER-888 starts creating pipelines with the selected states.

I grouped graphics passes by the complete stage-to-program mapping, then counted distinct triples of decoded rasterizer, depth/stencil and blend rows within each group. Equality includes the retained raw fields and treats an empty table as null. I checked that the generated manifest matched my #5207 manifest before joining its shader_link_check batch results.

Scope Shader combinations One state tuple Two tuples Three tuples Distinct combinations including state
All graphics sets in the manifest 16,325 16,006 257 62 16,706
Link-compatible sets 16,024 15,708 254 62 16,402

Among the compatible sets, rasterizer state varies within 175, depth/stencil within 2, and blend within 197 (overlap possible). A shader-only key would therefore collapse 378 additional recorded state variants in the compatible subset. These are distinct database selections; the number of distinct Vulkan create infos will depend on how the raw fields and defaults map. CPU census on CachyOS, AI-assisted (OpenAI Codex).

@Pipodi

Pipodi commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Kev-Ball, that's the number the next step needs. When vk_pipeline_check creates pipelines with the selected states, it will key them by the program set plus the decoded state tuple: 16,706 over the whole manifest, 16,402 over the link-compatible sets, as you counted. Your 175 rasterizer / 2 depth-stencil / 197 blend split will be the check on how many variants survive the mapping into Vulkan create infos.

AI-assisted (Claude Code).

@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 review at c4767f6. Across both databases all 28,797 emitted rows matched independent parsing of selectors and selected state records, including all eight blend targets. Histograms matched across 28,798 stored passes. One selected no program. The comparison reused the effect-boundary reader. All 11 focused CPU tests passed. Runtime defaults and rendering were outside this check.

@King1565

Copy link
Copy Markdown

Tested c4767f6 on Linux, Python 3.13 (CPU only, no GPU, no game files): pass with notes

Ran

  • tests/test_sgd5_pass_pipelines.py: 11 OK on head. With tools: sgd5_pass_pipelines.py turns every stored SGD5 pass into a shader_link_check batch manifest #5207's tool (a4a87ba), the 6 new tests fail and the 5 old ones pass. test_sgd5_effects.py, test_docs_links.py and test_text_write_line_endings.py OK. compileall and git diff --check clean. Merges cleanly into main.
  • I made 8 mutations of the decoders, and the suite caught all of them: stencil enable moved to byte 4, write mask moved to entry byte 6, cull read from byte 2, front/back swapped, 7 renamed less_equal, write read from the enable byte, the stencil range check dropped, targets 3..7 aliased to target 0.
  • CULL and COMPARE use the D3D11/12 values the shim consumes. The doc's counts add up: 64/124 rows, 1,833/2,280 passes, 8 × 17,116 targets.

Read
No code defect found. Two notes:

  1. docs/sgd5_render_states.md:30 argues that with the bytes swapped, the nine mixed rows "would write depth without testing it". That can't happen: in D3D (and Vulkan), turning depth off also turns off depth writes. The repo's shim does exactly this at runtime/shims/d3d12_vulkan_device.cpp:2991. Read swapped, the data is 9 rows with depth off and 1 read-only row, which is just as legal. So the pairs don't favour either order. A pipeline step has to AND depth_write with depth_enable for the (0,1) row.
  2. Line 13 says a row outside the measured ranges is an error, but only the decoded fields are checked. Synthetic decode_blend with prefix [7,9,7,7], and decode_raster with byte 2 = 0 (one of the three front-face candidates), are both accepted.

AI-assisted (Claude Code).


Generated by Claude Code

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

Head c4767f62 — stacked on #5207 (still OPEN, head a4a87bab). The PR's own commits are 8aa02e75..c4767f62 (+218/−24): tools/sgd5_pass_pipelines.py, tools/sgd5_effects.py, docs/sgd5_render_states.md, two tests.

  • Blocked on merge order, not on content: git diff origin/main...HEAD also contains all of #5207 (the file shows as new). Merge #5207 first, then rebase this.
  • python -I -m pytest tests/test_sgd5_pass_pipelines.py -q → 11 passed, 10 subtests; tests/test_sgd5_effects.py → 9 passed, 1 skipped, 29 subtests; tests/test_docs_links.py 6 OK; tests/test_text_write_line_endings.py 3 OK.
  • Decoding cross-checked against the pre-existing doc: table order (16, 24, 68, 36) and pass bytes 6/7/8. pass_states allows selector 0 on an empty table (null), errors on nonzero-into-empty and selector ≥ count, and slices at first + selector*width from effect_records. Blend enable/write-mask offsets (4+8i / 11+8i) match the table in the doc.
  • Shared row cache is per effect keyed (kind, selector); the manifest output is unchanged and a test pins the shared object.
  • git diff --check origin/main...HEAD → clean.
  • Not reproducible here (needs the game's shaders.rpf): the 28,798-pass census and the per-value distributions in the doc.
    Verdict: new work LGTM; hold until #5207 lands.

… say which bytes are range-checked

Review by King1565 in ItsNotRexed#5342: depth off also turns writes off in D3D and Vulkan, so the swapped
reading is just as legal (nine rows with depth off, one read-only). The doc now calls the order
the D3D11 reading and says a pipeline sets the write only when depth is enabled. The docstring
says only the decoded fields are range-checked; the raw bytes are kept unchecked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Pipodi

Pipodi commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @King1565, both notes were right; fixed in dc6be36. The doc no longer argues for the order: depth off turns writes off, so either reading is legal. It calls the order the D3D11 reading and says a pipeline sets the depth write only when depth is enabled, so the (0, 1) row writes nothing. The docstring now says only the decoded fields are range-checked and the raw bytes are not. Body updated. Thanks also for the mutation run.

@Kam471 thanks; agreed on the merge order.

AI-assisted (Claude Code).

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

Head now dc6be36e — one commit on top of the c4767f62 I reviewed; doc and docstring wording only.

  • The depth row no longer claims the (1,0)/(0,1) pairs prove the enable/write order: both orders are legal because depth-off suppresses writes, and the row now says the order is the D3D11 reading and that a row's pipeline sets the depth write only when depth is enabled. That is the honest reading.
  • The docstring now says a decoded field outside its range is an error while the raw bytes are not range-checked, matching decode_raster/decode_depth/decode_blend.
  • python -I -m pytest tests/test_sgd5_pass_pipelines.py -q → 11 passed, 10 subtests.
  • git diff --check origin/main...HEAD → clean.
  • Stacking note unchanged: #5207 is still OPEN and the diff against main contains its commits, so merge #5207 first and rebase this.
    Verdict: LGTM once #5207 lands.

@kapitche

Copy link
Copy Markdown
Contributor

Tested dc6be36 merged with main 561b65a on Linux (WSL2), Python 3.12.16: pass

  • tests/test_sgd5_effects.py: 10 run, 9 passed, 1 skipped, 0 failed. tests/test_sgd5_pass_pipelines.py: 11 run, 11 passed. tests/test_docs_*.py: 11 files, 11 OK.
  • Negative control: tools/sgd5_effects.py checked out from main (the new tool file has no main version): test_sgd5_pass_pipelines.py gives 11 run, 17 errors (the new Effect.tables field is gone); test_sgd5_effects.py still passes on main's tool (its changes are to the test builder). Mutating the new tool's CULL names (front/back swapped) gives 2 failures of 11. Restored afterwards.
  • Run on the user's own dump (read only, counts only): python -I tools/sgd5_pass_pipelines.py <shaders.rpf> <carve index.json> --out <scratch> --states <scratch> took 3 min 48 s, exit 0, and reproduces the doc: 28,798 passes, 16,325 distinct graphics sets with a PS, empty tables 26,965 raster / 26,518 depth / 11,682 blend; decoded per-pass counts: fill mode 3 in 1,833; cull none 999, front 776, back 58; depth enable 1 in 2,027 and 0 in 253; depth write 1 in 1,702 and 0 in 578; compare less 249, equal 74, greater 64, greater_equal 1,891, always 2; stencil enable 1 in 1,701 and 0 in 579; blend target entries 4,716 enabled and 132,212 not, over 17,116 passes with a blend row; write masks 15: 108,236, 3: 16,415, 0: 9,139, 7: 1,528, 1: 959, 8: 490, 2: 156, 11: 5; distinct rows 3 raster, 10 depth/stencil, 21 blend plus the empty table each. All of these match the text exactly.

Not rechecked: the per-byte "Measured values" column of the new table (e.g. byte 1 of the rasterizer row is 1 in 25 rows, 2 in 35, 3 in 4), which is a different tally from the pass counts above. The doc is careful to call the D3D11 order of the names a reading rather than a measurement.

@larpinguser larpinguser left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary: Adds tools/sgd5_pass_pipelines.py to decode the rasterizer, depth/stencil, and blend rows each SGD5 pass selects, emitting a distinct graphics-program-set manifest plus pass rows for link checking. docs/sgd5_render_states.md records measured field tables; tests build synthetic passes and state rows.

Why merge: Measured state bytes become actionable pipeline sets for shader_link_check.py, with the evidence documented alongside.

Verdict: Approve

@larpinguser

Copy link
Copy Markdown

Summary: Adds tools/sgd5_pass_pipelines.py to decode the rasterizer, depth/stencil, and blend rows each SGD5 pass selects, emitting a distinct graphics-program-set manifest plus pass rows for link checking. docs/sgd5_render_states.md records measured field tables; tests build synthetic passes and state rows.

Why merge: Measured state bytes become actionable pipeline sets for shader_link_check.py, with the evidence documented alongside.

Verdict: Approve

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.

8 participants