Skip to content

d3d12-gpu: a sparse read of only holes keeps the staged texels' digest (D3D12-GPU-SPARSE-ZERO-READ-DIGEST) - #2841

Open
Kal0s-design wants to merge 2 commits into
ProjectLeonida:mainfrom
Kal0s-design:fix/texture-sparse-zero-digest
Open

Kal0s-design wants to merge 2 commits into
ProjectLeonida:mainfrom
Kal0s-design:fix/texture-sparse-zero-digest

Conversation

@Kal0s-design

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-GPU-SPARSE-ZERO-READ-DIGEST (#2570 (comment))

What and why

A partly mapped texture is read through its unmapped pages, which come back as zeros (d3d12/crates/leonida-gpu/src/texture.rs, the partial read path). That path stored the read's digest before it rejected an all-zero read. So a read that caught only holes replaced the digest of the texels already detiled, and the next read that found those same texels again detiled and uploaded them a second time.

The change: the decision moves into Last::accept_partial, which rejects an all-zero read before touching the digest, then compares and stores the digest as before. The full (committed) read path is unchanged and still takes a texture that really is all zeros.

This ports a fix from the sibling VIctory tree (TaynanGT/Vice-Tay 26aeded, GPL-3.0).

Tested

  • New test texture::partial_tests::a_zero_sparse_read_keeps_the_digest_of_the_staged_texels: an all-zero first read stages nothing, a read with texels is taken, a later all-zero read keeps that digest, and the same texels again are not detiled. With main's order put back into accept_partial (a mutation of this branch), it fails (nothing staged yet: the next read with texels detiles, left: Some(Digest(..)), right: None); with the change it passes.
  • cargo test -p leonida-gpu --lib: 600 passed, 0 failed, 54 ignored (main 640b0d0df: 599).
  • cargo clippy -p leonida-gpu --lib --tests: no warning in the changed lines.
  • Windows only, unit tests; the title wasn't run.

GPUs tested: n/a (CPU unit tests)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • 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

A partly mapped texture is read through its unmapped pages, which come
back as zeros. The partial read path stored the digest of every read
before rejecting an all-zero one, so a read that caught nothing mapped
replaced the digest of the texels already detiled, and the next read
that found those same texels detiled and uploaded them again.

Last::accept_partial now rejects an all-zero read before touching the
digest. The full read path is unchanged and still takes a texture that
really is all zeros.

After the fix in TaynanGT/Vice-Tay (26aeded).

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

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

The rule is right, and the important part is what it fixes indirectly: Last.hash now describes the staging buffer rather than the last read from title memory, which is what note_held() / unchanged() already assume — so this also stops a wrong "held" digest and stops the alone shortcut marking a texture read when it has only seen holes. 600 passed / 54 ignored here, and restoring the old order fails the new test with left: Some(Digest(..)), right: None.

I checked the invariant separately rather than only the new test:

  • a changed last byte after staging still detiles;
  • a single non-zero byte among 127 hole bytes is taken, so a real texel at a hole boundary is not skipped (the all-zero test is whole-buffer);
  • repeated identical bytes do not detile;
  • no order dependence: copy_mapped zeroes every hole (host.rs:12164, :12250), with_read always hands size bytes, and digest is pure over them.

Two asks:

  1. Pin the invariant in the committed test. It proves the hole-only read no longer poisons the digest, but never that a real change after staging still detiles, nor a mixed texel/hole buffer. Two one-line assertions (I added them temporarily; they pass) would make the test describe the whole rule.
  2. Add the provenance. The body says this ports TaynanGT/Vice-Tay 26aeded (GPL-3.0) but the new doc comment cites nothing, while every other port in this file names repo/commit/author/licence (:160, :208, :1663, :1697). Same rule as the docstring PRs: the attribution belongs next to the code, not only in the PR body.

Cosmetic: accept_partial reads backwards (returning true means "changed, detile"); changed_partial would match the caller. The same texels under a different hole layout still hash differently and re-detile — pre-existing and not made worse, but the new comment could say the digest includes the hole zeros.

@lixonik

lixonik commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Reviewed and verified PR #2841 (fix/texture-sparse-zero-digest, merged onto main 7fbcd456e):

  • Diff check: moving the all-zero check ahead of self.hash = Some(hash) in Last::accept_partial prevents a hole-only read from overwriting the digest of previously staged texels, avoiding redundant detile + upload when the texels are read again.
  • cargo test --locked -p leonida-gpu --lib texture::partial_tests -- --nocapture: 2 passed, 0 failed (a_partly_mapped_texture_is_read_again_after_a_write, a_zero_sparse_read_keeps_the_digest_of_the_staged_texels).

GPUs tested: NVIDIA GeForce RTX 5070 12 GB (driver 621.40, Windows 10 Pro 22H2 build 19045, Ryzen 9 5950X 16C/32T)

AI-assisted: Antigravity

…xels

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

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

0729d56df: the doc comment names where the change comes from (TaynanGT/Vice-Tay 26aeded), and the test also checks that other texels are detiled and their digest kept. Without self.hash = Some(hash) the test fails. On main cd0c52fcf: 632 passed, 0 failed.

Pushed to your branch by MoonEyes (maintainer, the branch allows it), on top of your head, nothing rewritten. Say so if you would rather have it another way, and I take it out. The earlier verdicts are for the head before this one.

AI-assisted (Claude Code).

@merzleckeier

Copy link
Copy Markdown
Contributor

I tested 0729d56 together with #2852 c4e286f on 0ff8a53. The cleanly merged Win64 MSVC target passed the partial-texture and physical-page write selection on Radeon Raphael (RADV 26.3.1) and RTX 4070 SUPER (620.73.04), using Wine 11.22. The combined selection also passed on both GPUs with LEONIDA_PAGE_WRITES=slots and with 0 (the bake path).

A local digest-before-zero ordering control changes only when this function rejects zero bytes: hash/check/store before rejecting holes, as the old partial path did. Run alone, the stock regression test then fails on the first zero read because the previously empty digest has been populated. The fixed version also checks that a staged texture keeps its digest through holes and accepts different texels afterwards. This is coverage of the partial-read decision and its integration with the neighboring change. MEASURED, AI-assisted (Codex).

@DadaCap

DadaCap commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Independent red/green on Windows 11 (RTX 5080, NVIDIA 621.24), head 0729d56df merged into main 0ff8a532f:

  • python -I scripts/red_green.py 2841 gives "red does not build": the new test calls the new Last::accept_partial, so main plus the test alone cannot compile. Not a red by itself.
  • Red by mutation instead: with the PR merged, accept_partial given main's order back (the digest kept before the all-zero check) makes texture::partial_tests::a_zero_sparse_read_keeps_the_digest_of_the_staged_texels fail at texture.rs:6025 ("nothing staged yet: the next read with texels detiles"); with the fix restored it passes. So the test locks the change.
  • Green: all of leonida-gpu on main + PR, 691 run, 0 failed.

It touches rendering, so the in-game gate is still to do; no in-game run from me yet. AI-assisted (Claude Code), posted for DadaCap.

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.

6 participants