Skip to content

DirectStorage: bound BCPack record scratch memory - #1078

Open
TG0000 wants to merge 2 commits into
xcl-dev:mainfrom
TG0000:perf/bcpack-stream-records
Open

TG0000 wants to merge 2 commits into
xcl-dev:mainfrom
TG0000:perf/bcpack-stream-records

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

BCPack now reconstructs tiles from batches of at most 64 records. The decoder used to keep all intermediate records until the input was consumed. The bounded buffer is 2 KiB on the stack and the existing reconstruction history stays on the heap.

What is in it

  • crates/xcl/src/hle/bcpack/victory.rs keeps lane read-ahead across batches, handles zero symbols directly, and retains the error precedence for malformed streams. Regression tests check exact bytes for all six widths, batch boundaries, and invalid input.
  • Review follow-up (28760f3), same file: batch_boundaries_match_the_previous_decoder and the // shortcut: note, which still applies to RecordStream::fill_batch, back.

Tested

  • Ryzen 9 5900X, Radeon RX 6800 (driver 32.0.21045.29003), Windows 11 build 26300, Rust 1.100.0 MSVC.
  • The locked release workspace build succeeded. cargo test --release --locked --workspace --no-fail-fast -- --test-threads=4 --show-output reported 741 passed, 3 failed, and 41 ignored. All three new tests passed. Two tests reported GPU skips because the D3D12 debug layer was unavailable.
  • The run on main was the library tests only (695 passed, 3 failed, 21 ignored), not the workspace command used on the head, so the two counts are not the same command and do not compare directly; the workspace command was not run on main. The three failing tests are the same graphics tests in both runs: the 3D texture readback disagrees at subresource 2 and the other two meet a poisoned lock.
  • The standalone release benchmark compared 64 KiB and 512 KiB synthetic outputs across BC1, BC3, BC4, BC5, BC6H, and BC7. Peak decoder heap scratch fell by 5.41–77.80×. For example, BC1 at 512 KiB used 796,672 B on main and 10,240 B on head. Median elapsed times ranged from 3% slower to 16% faster. Every output byte matched, as did 3,610 truncated and junk cases; that benchmark is not part of the diff, so these cannot be rerun from the PR. In the tree the byte checks are decoded_blocks_match_the_previous_decoder (128 bytes of each format) and the test below.
  • Review follow-up (28760f3): cargo test -p xcl --lib hle::bcpack: 10 passed, 0 failed. batch_boundaries_match_the_previous_decoder compares FNV-1a hashes of bc1-700-2 (63, 64, 65, 127, 128 and 129 blocks, and 700 blocks hashed per batch of 64) and of bc1-8192-3 with the output of main (47efa2e) for the same fixtures. They are synthetic (tests/data/bcpack/generate.py), no game data. It fails when the first record of each full batch is skipped, when the last one is dropped and when the last one is repeated (three builds); the constant-data batch_boundaries_preserve_lane_read_ahead passes with the repeat. cargo clippy -p xcl --lib --tests: no warning on the changed lines. Not covered byte for byte past block 64: the formats with two symbols per block, because the only fixture over 64 blocks for them, bc7-700-2, has block errors; their lane stage is checked by fixtures_decode_to_their_lanes. Same machine as above.
  • On the previous head (8cb6ba1) the 45-second RX 6800 smoke comparison showed 954 frames on main and 922 and 1027 on head. The selected runs exited cleanly with zero crashes and device removals.

Regression run

On the head 28760f3 and on main 47efa2e (its merge base): one release build and one run each, from clean trees at those commits, the same title and 45 s. Other builds and tests ran on the PC during both runs, so the frame counts are not a timing comparison. Head: 1034 frames, main: 1027; peak commit 15131 MB against 15264 MB; no device removal and no crash.

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 1027 (PresentX 1027)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15264 MB

PR:

xcl-smoke v1
commit: 28760f362331607e9103ee5f2f0e35ef24754c8e
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 1034 (PresentX 1034)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15131 MB

Earlier runs, on 8cb6ba1 (before the review follow-up), main then PR:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 954 (PresentX 954)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15118 MB
xcl-smoke v1
commit: 8cb6ba1a508a504a5fb23166db037f8d9ee32ed1
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 1027 (PresentX 1028)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15124 MB

Not tested

Gameplay FPS, retail BCPack throughput, NVIDIA GPUs, and Wine.

AI use

Codex with GPT 6.2 Sol implemented the patch, added the tests, ran validation, and drafted this description. Claude Code with Claude Sonnet 5.5 wrote the review follow-up commit (the boundary test and the restored note) and updated this description for it.

Reconstruct blocks from fixed batches instead of staging every decoded record. Preserve lane state across batches and report the first lane error before block errors. Handle the frequent zero symbol directly to keep decode throughput.

Co-Authored-By: GPT 6.2 Sol <noreply@openai.com>
@fromthemoonx

fromthemoonx commented Nov 24, 2026 •

Copy link
Copy Markdown

Rerun on the new head 28760f362331607e9103ee5f2f0e35ef24754c8e (same machine, debug; the follow-up adds batch_boundaries_match_the_previous_decoder only):

  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4: 745 passed, 0 failed, 41 ignored
  • Merged onto current main 930e8b4 (no conflict): 755 passed, 0 failed, 41 ignored
  • With fill_batch starting each batch at pos = 1 (the first record of every batch skipped), batch_boundaries_match_the_previous_decoder fails, and so do three older tests (fixtures_decode_to_their_lanes, decoded_blocks_match_the_previous_decoder, truncated_lane_stream_takes_precedence_over_invalid_block). I did not try the dropped and repeated last record.
  • hle::bcpack + hle::dstorage tests, 10 runs: 10 of 10 passed (24 each)

The first run, on 8cb6ba1, follows unchanged.


NVIDIA run, since Not tested lists NVIDIA. The change is CPU-side; this machine also has the D3D12 debug layer, so the two GPU tests your run skipped ran here.

Tested

  • NVIDIA GeForce RTX 3060 12 GB (32-lane waves), driver 32.0.16.1088; AMD Ryzen 7 5700X; native Windows 11 Pro 26H2 (build 26300.9974), Graphics Tools (D3D12 debug layer) installed; rustc 1.100.0 stable-x86_64-pc-windows-msvc; DXC 1.9.2602.17
  • Exact head 8cb6ba1a508a504a5fb23166db037f8d9ee32ed1, which is on current main 47efa2e (so also the merged result):
    • debug, cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4: 744 passed, 0 failed, 41 ignored
    • release, cargo build --release --workspace, then cargo test --release --workspace --no-fail-fast -- --test-threads=4: 744 passed, 0 failed, 41 ignored
    • the three tests that fail in your RX 6800 runs (placed_title_textures_share_vram_and_preserve_pixels and the two on its lock) pass here, and test_title_draws_with_a_root_signature_under_the_debug_layer and uploads_start_from_each_subresources_state ran
  • The three new tests passed: streaming_tests::decoded_blocks_match_the_previous_decoder, batch_boundaries_preserve_lane_read_ahead, truncated_lane_stream_takes_precedence_over_invalid_block
  • hle::bcpack + hle::dstorage tests (the BCPack fixtures, junk / truncated streams, and the DirectStorage BCPack paths), 10 runs: 10 of 10 passed (23 tests each)
  • SKIPPED (no wave64 on this adapter): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware
  • One unrelated note: a first release run, started without the release build of the test title (so both e2e tests failed on the missing xcl_testtitle.exe), also had list_x::tests::cross_queue_cycle_and_same_queue_fifo_do_not_deadlock fail once with E_OUTOFMEMORY while the machine was busy compiling. The clean rerun above passed it, and it doesn't touch BCPack.

Not tested

  • Real BCPack data from a title (none here) and the standalone heap/elapsed benchmark from the description, which isn't in the repository as far as I can see.

AI use

  • Claude Code (Claude Opus 5.5) ran the builds and tests on the machine above and wrote this report from the output.

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

Review of 8cb6ba1 against its merge base 47efa2e (the tip of main when I read it), by reading the code; nothing of the PR was built or run here (one check below used a separate script over the tables on main). I found no defect in the decoder. What I checked, and three smaller points about the tests and the description:

  • Batching keeps the schedule (crates/xcl/src/hle/bcpack/victory.rs:293-333, :339-358). A batch ends in the round where done == full advances pos, and in that round done becomes 0 and moved makes idle 0, so restarting pos, done and idle at 0 for the next batch is the state the single loop on main had at that point. The lanes (latch, out, stalled, need, x, left) and the source iterator live in the RecordStream, so read-ahead carries over. The record index row[j * PER + pos % PER] is the old records[pos / per * stride + j * per + pos % per], at most 31 in a 32-byte row.
  • The symbol lookup rewrite gives the same values (victory.rs:221-243). With ascending starts, slot < start[1] is the old bucket 0, 1 + count(start[2..] <= slot) is the old bucket, and since the offset is below 2^(b-1) the old k equals b, so start[b] + offset * f is the old masked c (it is at most slot, so neither the mask nor the wrapping multiply did anything). I also compared the old and new (sym, f, c) in a separate script over the tables on main for all 128 models and all 65536 slots: no difference.
  • Error precedence (victory.rs:127-132). After a block error the rest of the records are drained, so a stream that runs out anywhere still reports the lane error first, as on main; a lane error ends the iterator (:350-353). dst can now be partly written when the lane error comes late; the only caller (crates/xcl/src/hle/bcpack/mod.rs:63-66) drops it.
  • No new panic on untrusted input. A record is now always 32 bytes instead of stride; every reader takes fixed indices or bounded ranges (s[8..12], s[6..14], s[20..24], s[4..12], s[8..24], s[o..o + 2]), none depends on the slice length, and the bytes past stride are never written and stay 0.
  1. CONFIRMED (tests, minor): the new byte-exact tests do not cross a batch boundary with varying data. decoded_blocks_match_the_previous_decoder (victory.rs:1061-1096) decodes 128 bytes per format, 16 or 8 blocks, so it stays inside the first batch of 64. batch_boundaries_preserve_lane_read_ahead (victory.rs:1099-1104) uses bc7-8192-3, whose symbols are all zero (crates/xcl/src/hle/bcpack/tests.rs:18-30): all 4096 records are the same, and the same as a batch row that was never written. A record dropped, repeated or left unwritten at a boundary would pass it. What does cover the boundaries is the test that was already there, fixtures_decode_to_their_lanes (tests.rs:33-55), which now goes through RecordStream (victory.rs:151-159) and compares bc1-700-2, bc7-700-2 and bc1-8192-3 with their independent .lanes files, for both the one- and two-symbol paths. So the lane stage is covered; the block stage is not checked byte for byte past block 64 on anything but a constant output. Suggestion: add bc1-700-2 (the plain formats have no block errors, so its random symbols decode) at 700 blocks against reference bytes or a hash from main's decoder, and block counts of 63, 64 and 128.

  2. CONFIRMED (description): the byte-identity evidence is mostly outside the PR. "Every output byte matched, as did 3,610 truncated and junk cases" comes from a standalone benchmark that is not in the diff, so nobody else can rerun it; in the tree the claim rests on the tests of point 1. Also for the maintainer: Tested compares a workspace run on the head (741 passed, 3 failed, 41 ignored) with a library-only run on main (695 passed, 21 ignored), so "the same three fail on main" is not the same command on both sides; the NVIDIA report in this thread has since run the whole suite at this head with 0 failed. The CI workflow shows action_required at this head (not yet run), only PR checks is green. The regression blocks name 47efa2e and the head and meet the frame and commit limits; the text mentions two head runs (922 and 1027 frames) and pastes the higher, both above 80 % of 954.

  3. CONFIRMED (scope, minor): two changes in one commit. The batching and the rewrite of the symbol lookup in Lane::step (victory.rs:221-243) are independent; the second is a speed change to the rANS step that the title does not mention (the commit body does). It is correct as far as I can tell (see above), but it would have been easier to review on its own. The commit also drops the // shortcut: note above the old lanes function (main, victory.rs:263-265) with the measured throughput and the plan for a faster split, which still applies.

Overlap: #1047 (open, adds crates/xcl/tests/dstorage_bcpack.rs) tests BCPack through the queue and does not touch this file; no other open PR changes victory.rs.

Verdict: no defects found; from the code this can be merged once CI has run on the head. Points 1 to 3 are improvements, not blockers.

Review by Claude Code (written by Claude Fable 5.2, checked and edited with Claude Opus 5.5); each point checked by a person before posting.

…t note back

Review of xcl-dev#1078: the tests did not cross a batch of 64 records with varying
output. batch_boundaries_match_the_previous_decoder hashes the output of
bc1-700-2 at 63, 64, 65, 127, 128, 129 and 700 blocks (and per batch for
700), and of bc1-8192-3, against what the two-pass decoder of main
(47efa2e) gave for the same synthetic fixtures. A record dropped or
repeated at a batch end fails it.

The `// shortcut:` note on the one-lane-step-at-a-time schedule, dropped
with the old `lanes` function, still applies to RecordStream::fill_batch
and comes back.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@TG0000

TG0000 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you for the careful review. I checked each point against the code.

Pushed 28760f3 on top of 8cb6ba1 (no force push):

  • Point 1: batch_boundaries_match_the_previous_decoder hashes the output of bc1-700-2 at 63, 64, 65, 127, 128 and 129 blocks (and at 700 blocks, per batch of 64) and of bc1-8192-3, against what main (47efa2e) decodes from the same fixtures. They are synthetic (made by generate.py), no game data. It fails when the first record of a full batch is skipped, when the last one is dropped, and when the last one is repeated; the constant-data test still passes with the repeat. Formats with two symbols per block are not covered byte for byte past block 64, because bc7-700-2 has block errors; the description says so.
  • Point 2: the description now says the run on main was the library tests only, not the same command as on the head, and that the byte-identity benchmark is not in the diff.
  • Point 3: the // shortcut: note is back on RecordStream::fill_batch, where it still applies. I left the symbol lookup change in this PR rather than rewrite history.

Tested: cargo test -p xcl --lib hle::bcpack: 10 passed, 0 failed. cargo clippy -p xcl --lib --tests: no warning on the changed lines. Update: xcl-smoke blocks for the new head 28760f3 and for main 47efa2e are now in the description (1034 and 1027 frames, no device removal or crash; other work ran on the PC, so not a timing comparison).

AI-assisted: this follow-up was written and tested by Claude Code with Claude Sonnet 5.5.

@pvfreis

pvfreis commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Follow-up at 28760f3. I read the diff only; nothing was built or run here.

  • Point 1 is fixed. batch_boundaries_match_the_previous_decoder (victory.rs:1122-1156) checks bc1-700-2 at 63-65 and 127-129 blocks, each batch of 64 over 700 blocks, and bc1-8192-3, so a record dropped or repeated at a batch end now fails it. I did not recompute the hashes. The 64-block hash and the first per-batch hash of the 700-block decode are the same value, as they should be. The two-symbol formats past block 64 are still not covered byte for byte, and the description now says so.
  • Point 2 is fixed in the description: main's run is named as library-only, and the benchmark as outside the diff.
  • Point 3: the // shortcut: note is back above fill_batch (victory.rs:293-296). The symbol-lookup change stays in the first commit, as you chose.
  • Still open before merging: PR checks now fails at 28760f3 because the Regression run block names 8cb6ba1, and the check wants an xcl-smoke block made on 28760f3. CI shows action_required at this head, so it has not run yet.

Reply by Claude Code (Claude Opus 5.5); checked by a person before posting.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…vall#1142, ivall#1150, ivall#1156, ivall#1182, ivall#1191, ivall#1195, ivall#1221

Integrate 10 high-impact open upstream PRs on top of upstream/main (930e8b4):
- ivall#1182: share video memory across render targets aliased onto one physical page (~1.85-2.1 GB VRAM saved)
- ivall#1142: declare tiled texture pools texture-only (D3D12_HEAP_FLAG_DENY_BUFFERS) for vkd3d-proton
- ivall#1097: report process memory under Wine via PagefileUsage fallback
- ivall#1078: bound BCPack record scratch memory to 2 KiB stack batches
- ivall#1150: convert console clear-value formats in CreatePlacedResource
- ivall#1156: release the interface returned by QueryInterface in class_for
- ivall#1139: check pointer alignment in is_adopted before dereferencing
- ivall#1195: skip ExecuteIndirect dispatch of untranslated console compute pipelines
- ivall#1191: VirtualProtect commits lazy pages in its range first
- ivall#1221: unmark lazy away mappings in FreeTitlePhysicalPages

Assisted-by: Antigravity (Google DeepMind)
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…ll#1221, and updated Linux PRs

Update fork README for upstream/main 930e8b4 (BCPack BC1-BC7, XCL_PSO_WAIT_MS, low-VA ASLR < 2^40, alloc_pool, sparse tile parking/pool reuse, direct page moves, write-watch merge), newly integrated PRs (ivall#1078, ivall#1097, ivall#1139, ivall#1142, ivall#1150, ivall#1156, ivall#1182, ivall#1191, ivall#1195, ivall#1221), and updated Linux PRs (ivall#867, ivall#871, ivall#877).

Assisted-by: Antigravity (Google DeepMind)
@Royalflamejlh

Copy link
Copy Markdown
Contributor

Linux (Wine + vkd3d-proton) + NVIDIA check: the tests of the current head 28760f3 pass under Wine, and in one 100 s run of a retail title with 8cb6ba1, the head before it, every BCPack request still unpacked (2,442 requests, 0 failed), as on main in the run beside it (2,379, 0 failed).

Setup: Fedora 44 (kernel 7.2.9), Wine 11.21 Staging (Wayland driver), vkd3d-proton v3.1-90-g30b0eb9b with the relaxed-vtable patch from #175 and a local dxil-spirv patch. RTX 4070 12 GB (driver 615.71.09), Ryzen 9 9950X3D, 64 GB RAM. Release, cross-compiled to x86_64-pc-windows-gnu (rustc 1.98.0), XCL_NETWORK=0, XCL_PSO_PRELOAD=0. Retail title 1.0.2.1, the first-run screens completed once and no story progress, one A press at 44.8 s through XCL_PRESS_AT. Each head merged onto main 67a3735 + #684 (e56dbc3) without conflicts; 1 run of the base and 1 of 8cb6ba1. 28760f3 adds a test and a comment to 8cb6ba1 and no decoder code, so I did not repeat the run.

Tested

  • cargo test --release -p xcl --lib bcpack under Wine, with 28760f3 merged: 14 passed, 0 failed, batch_boundaries_match_the_previous_decoder among them (13 of 13 on 8cb6ba1).
bcpack_unpacked in report.json 67a3735 + #684 the same + 8cb6ba1
BC1 2,077 requests, 148,267,008 B, 0 failed 2,126 requests, 148,738,048 B, 0 failed
BC5 107, 7,077,888 B, 0 failed 112, 7,405,568 B, 0 failed
BC6H 1, 1,048,576 B, 0 failed 1, 1,048,576 B, 0 failed
BC7 194, 25,157,632 B, 0 failed 203, 26,898,432 B, 0 failed
refused 0 0

The request counts differ by a few percent between any two runs here, because a run is cut at 100 s while the world is still loading; I did not compare the unpacked bytes of the two builds.

Not tested: the decoder's memory use or speed, which is what this change is for; whether the textures look the same (no run of ours reaches a frame of the world); the rest of the test suite; AMD and Intel.

AI use: Claude Code (Claude Fable 5.2) read the diff, built the tree, ran the tests and the two runs and wrote this note.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

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.

5 participants