Skip to content

startup: persist and reuse translated PSO recipes - #2919

Open
lambirou wants to merge 6 commits into
R0Y00:mainfrom
lambirou:startup/pso-warm
Open

lambirou wants to merge 6 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.
  • 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 --no-tests=error -R '^startup_pso_recipe$' -V
ctest --preset dev --no-tests=error -R '^startup_stream_draw_warp$' -V

startup_pso_recipe passed: 18 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, and the recreated blend/depth/rasterizer/sample/topology fields.

startup_stream_draw_warp now exercises 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. The same 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.

startup_stream_request_warp and the existing translated-draw fixtures still pass. d3d12_xs_shim and startup_stream_consumer build.

Limits

The replay still calls CreateGraphicsPipelineState; it does not serialize a driver PSO or use a D3D12 pipeline library. The hardware draw was not rerun in this pass; WARP covers the two-process behavior. 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.

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