Skip to content

startup: persist and reuse translated PSO recipes - #2919

Merged
R0Y00 merged 10 commits into
R0Y00:mainfrom
lambirou:startup/pso-warm
Nov 27, 2026
Merged

R0Y00 merged 10 commits into
R0Y00:mainfrom
lambirou:startup/pso-warm

Conversation

@lambirou

@lambirou lambirou commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What changed

The translated draw path builds each desktop graphics PSO on its first draw, from state the shader cache does not store: the effective blend state after export write masks, depth-stencil state, rasterizer state, target formats, DSV format, sample mask and topology. A run therefore reconstructs that state and creates the PSO at the first draw.

This adds an opt-in persistent recipe cache:

  • X2W_PSO_RECIPE_DIR=<dir>, off by default.
  • One versioned text file per translation key, bounded to 64 KiB and 64 records.
  • pipeline_for appends the exact state after a successful CreateGraphicsPipelineState.
  • On a later run, load_translation reads the matching file and the first pipeline_for replays records whose target formats and DSV match the pass. A malformed record, an unknown version, a corrupt state key or a target-format mismatch is ignored and the unchanged creation path runs.
  • The writer applies the parser before appending: it refuses a 65th record, a file with any rejected line, a record that would cross 64 KiB, and a non-empty file whose last byte is not LF. The read, count/size check and append share one exclusive file handle.
  • A contending draw keeps the PSO it already created and skips only the cache write; a reader denied by that handle also falls back to the unchanged creation path.
  • Counters: recipes loaded, PSOs precreated, mismatches, fallbacks.

This is state reconstruction, not a D3D12 pipeline library. A replayed record still calls CreateGraphicsPipelineState; the driver may compile or find the PSO in its own cache. The change moves the state reconstruction and first-use creation earlier; it does not claim to eliminate driver compilation.

Tests

ctest --preset <dev|release> --no-tests=error -R '^startup_(pso_recipe|stream_draw_(warp|hardware))$' -V

startup_pso_recipe passed: 29 checks, 0 failed. It covers the serializer/parser round trip, two appended records, a corrupt state key, an unknown version, an oversized file, target-format and DSV mismatches, the recreated blend/depth/rasterizer/sample/topology fields, the 63/64/65 record boundary (63 accepts one more, 64 refuses a 65th, and the parser rejects a 65th), and a parseable record without its final LF (append refused, earlier record still readable).

startup_stream_draw_warp and startup_stream_draw_hardware exercise the full path: the first translated stream draw writes a recipe, a second process with the same directory loads it, reports precreated records, and produces the same 32 RGBA bytes. They also hold the recipe file exclusively during a draw and check that the draw still renders with exact pixels while the file size stays unchanged. The runtime boundary control seeds 63 valid records for another translation key, checks that the next draw appends the matching record as number 64 and the following process logs precreated 1, then seeds 64 records and checks that the draw still renders while the file stays at 64. It removes the final LF from that file, checks that the draw leaves it byte-for-byte unchanged, and checks that the next draw still reads it. The test found and fixed a real serializer/parser gap: an unused depth-stencil block was written as a zero-filled D3D12_DEPTH_STENCIL_DESC, whose zero stencil ops the parser rejects. The serializer now writes a valid default for an unused block, and the parser normalizes it back for the state key.

Passed on 360a9dff, dev and release, on WARP (1414:008c, driver 10.0.26100.10068) and on an AMD Radeon RX 9060 XT (1002:7590, driver 32.0.32029.2010). The branch is merged with current main e1ea515d.

Limits

The replay still calls CreateGraphicsPipelineState; it does not serialize a driver PSO or use a D3D12 pipeline library. No game files or captures are included.

Refs #870.

AI-assisted.

Copy link
Copy Markdown
Contributor Author

Strengthened the coverage in 560c075a: startup_stream_draw_warp now writes a recipe on the first translated draw, runs the same draw in a second process with the same directory, checks that the recipe is precreated, and compares all 32 RGBA bytes. This found a real serializer gap: an unused depth-stencil block was written as a zero-filled struct whose stencil ops are invalid; the serializer now writes a valid default and the parser normalizes it back for the state key. CI is green on the new head.

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

PR #2919 by @lambirou -- startup: persist and reuse translated PSO recipes

What works

  • Solid foundational work establishing the 0x100 linear layout in gfx10_layout.h with careful derivation from AMD addrlib
  • The linear_mode = 0 constant elegantly extends block_log2() to accept mode 0 without changing the tiled modes switch logic
  • supports() check is correct: mode == linear_mode bypasses the block_log2 >= 12 requirement since linear has no tail
  • Test infrastructure is excellent -- linear_order_control (expected failure with largest mip first), linear_addrlib (338K element addresses), and the unpadded negative control all add strong coverage
  • Documentation accurately restates addrlib's linear mode algorithm in clear, testable terms

Issues

  • supports() currently allows block-compressed formats (BC1-BC7) in linear mode via the fallthrough -- BC formats are block-based and don't have linear row semantics; consider gating them out explicitly like 1D does in #2929
  • The check script in startup_address_console_check.py builds linear backing bytes from addrlib equations. This is correct but the linear path could break if the hardcoded equations drift from addrlib; a comment linking to the Mesa commit that derives these equations would help

Recommendations

  • Good discipline in including the negative control (unpadded rows test) to verify the 256-byte pitch requirement
  • Consider adding a TODO or assert in supports() that blocks linear mode for BC formats until explicitly validated against addrlib

Overall

A well-researched, well-tested implementation. The linear layout extension is cleanly separated from tiled modes. Minor concern about BC format handling in linear mode, but the existing refusal behavior is safe. Ready for merge.

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

PR #2919 by @lambirou -- startup: persist and reuse translated PSO recipes

What works

  • Solid foundational work establishing the 0x100 linear layout in gfx10_layout.h with careful derivation from AMD addrlib
  • The linear_mode = 0 constant elegantly extends block_log2() to accept mode 0 without changing the tiled modes switch logic
  • supports() check is correct: mode == linear_mode bypasses the block_log2 >= 12 requirement since linear has no tail
  • Test infrastructure is excellent -- linear_order_control (expected failure with largest mip first), linear_addrlib (338K element addresses), and the unpadded negative control all add strong coverage
  • Documentation accurately restates addrlib's linear mode algorithm in clear, testable terms

Issues

  • supports() currently allows block-compressed formats (BC1-BC7) in linear mode via the fallthrough -- BC formats are block-based and don't have linear row semantics; consider gating them out explicitly like 1D does in #2929
  • The check script in startup_address_console_check.py builds linear backing bytes from addrlib equations. This is correct but the linear path could break if the hardcoded equations drift from addrlib; a comment linking to the Mesa commit that derives these equations would help

Recommendations

  • Good discipline in including the negative control (unpadded rows test) to verify the 256-byte pitch requirement
  • Consider adding a TODO or assert in supports() that blocks linear mode for BC formats until explicitly validated against addrlib

Overall

A well-researched, well-tested implementation. The linear layout extension is cleanly separated from tiled modes. Minor concern about BC format handling in linear mode, but the existing refusal behavior is safe. Ready for merge.

Copy link
Copy Markdown
Contributor Author

The two COMMENTED reviews on this PR describe gfx10_layout.h, linear_mode and startup_address_console_check.py, which are not part of this change. #2919 is the translated-PSO recipe cache (pso_recipe.h, d3d12_draw.inl, startup_stream_draw_check.py). Could a reviewer take another look at the actual diff? The current head a891727f is green and the two-process replay test passes.

Copy link
Copy Markdown
Contributor Author

@R0Y00 the two COMMENTED reviews here describe gfx10_layout.h and linear_mode, not this PSO-recipe change. The current head a92a0f97 is rebased on main, green, and the two-process PSO replay test passes. Could you review the actual diff?

Copy link
Copy Markdown
Contributor Author

Windows dev + release receipt on 711e7d95:

ctest --preset dev     -R "startup_pso_recipe"        -> Passed (18 checks, 0 failed)
ctest --preset dev     -R "startup_stream_draw_warp"   -> Passed (two-process recipe replay, 32 RGBA bytes)
ctest --preset release -R "startup_pso_recipe"        -> Passed
ctest --preset release -R "startup_stream_draw_warp"   -> Passed

The two-process replay is the requested end-to-end control: first process writes the recipe, second process loads and precreates it, and the 32 RGBA bytes match.

Copy link
Copy Markdown
Contributor

Could you share when you expect this PR to merge, and whether the record-limit behavior below should be addressed here?

On head 711e7d95, I ran ctest --preset dev --no-tests=error -R '^(startup_pso_recipe|startup_stream_draw_(warp|hardware))$' -V: all three passed on Windows 10 build 19045, WARP 10.0.19041.5794 and NVIDIA GTX 1080 Ti driver 32.0.15.8278; the debug layer was disabled for the draw fixtures.

I also checked the 64-record boundary with the existing serializer and parser. A source-built file containing 64 valid records and a 65th with a different state key was 30,484 bytes, below the 65,536-byte file limit. The parser accepted 64, rejected the last with recipe record limit reached, and did not return the 65th key. append_pso_recipe checks file size but not the record count, so such a record can be appended and then ignored on later loads. The regular PSO creation path still handles a miss. A 64/65-record test and a matching append policy would make this boundary explicit.

AI-assisted.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please address the writer/reader record boundary in this PR. On 711e7d95 plus main 1ec2c938 (tree 117604d5), I reproduced the effect through the ordinary draw entry point with a source-built VS/PS. At 63 valid seed records, the writer appended the live record as number 64 and the next process logged precreated 1. At 64 valid seed records, it appended number 65 at 30,810 bytes, well below 64 KiB. The next process logged recipe record limit reached, fell back and appended number 66. The pixels still matched on both WARP and RTX. This is a persistent cache miss after a successful write, so please align the append policy with the parser and cover the 64/65 boundary.

The implementation also opens the recipe file with FILE_SHARE_WRITE and seeks to the observed end. Two processes can both choose the same offset. Please make the count/size check and publication one writer transaction or document and enforce exclusive ownership, and keep a contending draw on the regular fallback path. I noted this from the code and did not fault-inject a concurrent interleaving.

Independent ctest --preset <dev|release> --no-tests=error -V -R '^(startup_pso_recipe|startup_stream_draw_(warp|hardware)|startup_compute_dispatch_native_(warp|hardware))$' gave four passes and one native-compute WARP 77 skip per preset. The normal two-process draw, manifest retry and exact red/green readbacks pass. Windows 11 26200.9974, SDK 10.0.26100, WARP 10.0.26100.9818 and RTX 4060 Laptop driver 32.0.16.1656. The standard stream test leaves the debug layer off; the boundary probe enabled it.

Refuse a 65th record instead of appending one the next process rejects, and hold the recipe file exclusively across the read, count/size check and append. A contending draw keeps its PSO and skips only the cache write. Cover 63/64/65 and an exclusive-lock draw on WARP and hardware.

Copy link
Copy Markdown
Contributor Author

Merged main at e1ea515d. Head is ab23bcf4.

Record boundary:

  • the writer now runs the same parser over the existing file before it appends;
  • 63 complete records still accept the next append (the next process loads 64 and precreates it);
  • at 64 complete records the writer refuses the 65th instead of creating a record the next process drops as recipe record limit reached;
  • a file with any rejected line is left alone rather than extended.

The device-free check now covers 63 records (append allowed), 64 records (append refused), 65 records (parser rejects the extra record), and a malformed existing file. All 26 checks pass.

Concurrency:

  • append_pso_recipe opens the file with dwShareMode = 0 and holds that handle across the read, count/size check and append;
  • read_pso_recipe_text now shares read only, so it denies a writer for the whole read;
  • a contending draw fails the append open, keeps the PSO it already created and skips only the cache write.

startup_stream_draw_check.py now takes an exclusive CreateFileW handle on the recipe file, runs another draw, and checks that the draw still produces the exact 32 RGBA pixels while the file size stays unchanged. That control passes for both the red and green recipes.

Windows receipt on ab23bcf4, dev and release, ctest --preset <dev|release> --no-tests=error -R '^startup_(pso_recipe|stream_draw_(warp|hardware))$':

  • startup_pso_recipe: pass, 26 checks.
  • startup_stream_draw_warp: pass, including the exclusive-ownership fallback.
  • startup_stream_draw_hardware: pass on AMD Radeon RX 9060 XT 1002:7590, driver 32.0.32029.2010.

Windows checks #8169 is still running on this head; I will report the result when it finishes.

Copy link
Copy Markdown
Contributor Author

Windows checks #8169 completed successfully on ab23bcf4.

Seed the recipe file with 63 valid records for another translation key, verify the next draw appends the matching record as number 64 and the following process precreates it, then seed 64 and verify the draw still renders while no 65th record is written. Passes dev and release on WARP and the RX 9060 XT.

Copy link
Copy Markdown
Contributor Author

Added the runtime boundary control on head 708416f3, so the writer path itself is covered rather than only the policy helper.

startup_stream_draw_check.py now seeds the recipe file with 63 valid records for another translation key. The next draw appends the matching record as number 64, and the following process logs precreated 1 and reproduces the exact pixels. Seeding 64 records then leaves the file at 64 after another successful draw: the 65th record is not written. Both controls pass in dev and release on WARP and on the RX 9060 XT.

Windows checks are running on the new head; the previous head ab23bcf4 was green in #8169.

Copy link
Copy Markdown
Contributor Author

Windows checks #8172 completed successfully on 708416f3.

Copy link
Copy Markdown
Contributor Author

@R0Y00 the two CHANGES_REQUESTED points are addressed on 708416f3 and Windows checks #8172 are green. The runtime control now seeds 63 records (the next draw appends number 64 and the following process logs precreated 1) and 64 records (no 65th is written), and the exclusive handle spans the read, count/size check and append. Could you re-review when you have a moment?

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The revised code holds one exclusive handle across the count and append, and the 63/64 controls cover the earlier record-limit issue. I found one remaining reader/writer boundary at 708416f3. Using the shipped C++ serializer, I removed only the last LF from one valid record. parse_pso_recipe_file still accepted that record (one accepted, zero rejected), and pso_recipe_append_allowed returned true. Appending the next serialized record produces endx2w-pso-recipe 1 on one line. The next read accepted zero records, including the previously valid one. With the separator restored, both records parse.

Please ensure a successful append preserves a line boundary. Rejecting the append without changing the file is sufficient. If the writer inserts a separator instead, include it in the 64 KiB cap. Add a control starting from a parseable record without a final LF and verify that the earlier record remains readable.

A file whose last record lacks its LF still parses, but appending would join end and the next header on one line. Refuse the append and leave the file unchanged; cover the policy and the runtime file.

Copy link
Copy Markdown
Contributor Author

Fixed on 360a9dff. pso_recipe_append_allowed now refuses a non-empty file whose last byte is not LF, so the writer leaves the file unchanged instead of joining end to the next header.

Controls:

  • the device-free check parses a valid record with its final LF removed, confirms the append is refused, and confirms the earlier record still parses;
  • the runtime check removes the final LF from the 64-record file, runs a draw, and checks that the pixels are exact, the file is byte-for-byte unchanged, and the next draw still reads it.

Passes in dev and release on WARP and the RX 9060 XT: startup_pso_recipe, startup_stream_draw_warp, startup_stream_draw_hardware. Windows checks are running on the new head.

Copy link
Copy Markdown
Contributor Author

Windows checks #8191 completed successfully on 360a9dff.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Retested 360a9dff on main a9fd1488 (tree bcfaa9376a0cd5ee21944a5a42b5a41b075f2f38). The writer now refuses the append before changing a parseable file with no final LF. Besides the submitted unit and runtime controls, I seeded 63 valid foreign records with no final LF, a count still eligible for append. On both WARP and RTX the draw produced exact pixels, the file stayed byte-for-byte unchanged, and the next run rendered again. The adjacent 63-record file with a final LF still appended record 64 and precreated it in the following process. The 64-record and exclusive-handle controls also passed.

Built startup_stream_consumer d3d12_xs_shim startup_pso_recipe_check startup_manifest_retry_check compile_dxil. Debug and Release /O2 each gave five passes and one native-compute WARP wave32 skip using ctest --test-dir <build> --no-tests=error -j 1 -V -R '^startup_(pso_recipe|stream_draw_(warp|hardware)|compute_dispatch_native_(warp|hardware)|manifest_retry)$'. Windows 11 26200.9974, WARP 10.0.26100.9818, RTX 4060 Laptop driver 32.0.16.1656 and SDK 10.0.26100. The stream draw fixture used the debug layer. This addresses both rounds of cache-write findings.

@R0Y00
R0Y00 merged commit 5df098d into R0Y00:main Nov 27, 2026
5 checks passed
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.

4 participants