Skip to content

d3d12: preserve offsets in sub-page host buffer imports (RT-HOST-IMPORT-PREFIX) - #1192

Merged
aslan2142 merged 7 commits into
ProjectLeonida:mainfrom
GGandCo:fix/host-import-prefix
Nov 25, 2026
Merged

aslan2142 merged 7 commits into
ProjectLeonida:mainfrom
GGandCo:fix/host-import-prefix

Conversation

@GGandCo

@GGandCo GGandCo commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-HOST-IMPORT-PREFIX

Claim: #14 (comment) (posted before this PR).

Depends on: none.

Overlaps: #1190 and #1100 touch the same existing-heap fixture/backend file. This PR only changes per-resource imported-buffer prefix alignment; it does not implement existing-heap placed resources or deferred image backing. I will rebase and preserve both sets of tests if either lands first.

What and why

A buffer 16 or 32 bytes into an importable host page can be valid for the logical resource while failing the Vulkan memory-binding alignment. Binding the logical VkBuffer at that prefix currently falls back to an unrelated allocation, so the caller's writes do not share its GPU memory.

Bind a VkBuffer covering the page prefix plus logical range at memory offset zero. Carry the logical prefix in ResourceBinding::buffer_offset and in a valid returned device address. Preserve logical VA, size, initial contents and the existing ordinary-buffer fallback for byte-unaligned addresses.

Extend the existing synthetic fixture with bidirectional copies and surrounding-byte sentinels. Add d3d12_host_prefix / --host-prefix-only so this behavior can be tested independently of OpenExistingHeapFromAddress. The full existing-heap suite remains enabled and includes the new cases.

Review regression fixed

The +4-byte host-prefix RGBA16 texture-copy regression reported in review is fixed with aligned transfer scratch, preserving the host alias. The isolated test now covers 0/4/8/16/32/64 prefixes, buffer copies and two-row RGBA16 texture copies in both directions, including every padding byte. Normal and Vulkan-validation runs pass locally; the unchanged previous head fails the new CPU replay fixture. Wine/Proton revalidation is pending.

Tested

Rebased onto main 98c2f77 (current PR head 002c720). Conflict resolution keeps main's import-failure cleanup and page-pin release, BDA word padding, low-address import guards, shadow-fallback behavior and additional heap/placed-texture tests, plus this PR's logical prefix and aligned texture-copy staging. The RelWithDebInfo target build, isolated d3d12_host_prefix test normally and with Vulkan validation, and changed-file game-data/whitespace checks pass again on the rebased head. The negative control below was measured against main6293c1c4 / main58be9cdf8 before this rebase.

Local Windows / Intel UHD Graphics (101.6314):

  • Isolated candidate d3d12_host_prefix passes normally and with Vulkan validation enabled: 0, 16, 32 and 64-byte offsets, CPU-to-GPU and GPU-to-CPU coherence, initial contents, and unchanged prefix/tail.
  • Exact unchanged-main backend plus the identical isolated test fails at the 16- and 32-byte offsets because the resource VA differs from the caller VA. Aligned and 64-byte cases succeed. Restoring the candidate passes normally and with validation again.
  • Our separate local PR984 integration also passes this isolated test in both modes and its TLS/xmem/proxy regressions. That integration is not the clean-main build used above.

Independent reports on 02a2717 (before adding the isolated test):

Known limitation: the monolithic existing-heap executable on the local Intel machine still intermittently loses the device in its earlier aligned copy after the existing-heap checks, on both unchanged main and candidate. The isolated test above avoids those preceding operations; it does not remove their coverage. PR #1259 separately addresses that earlier-import lifetime issue: applied privately on top of this PR before the latest rebase, the full test passes normally, with validation and in three further repeats on this Intel machine (#1259 (comment)). That fix is not included here. A private queue-barrier experiment was not reproducible and is not included. No game boot, gameplay or performance claim is made by this PR.

GPUs tested: Intel UHD Graphics / Windows, 101.6314 (local); NVIDIA RTX3060Ti / Windows,621.24 and Arch Linux+Wine11.22,595.58.03 (independent reports linked above).

AI-assisted: yes.

Checklist

  • Based on current main and rebased cleanly.
  • One topic; independent GPU/heap issues remain separate.
  • No game data, keys, decryption, generated shaders, captures or private runtime logs included.
  • No translator rule changed.

@madhyn

madhyn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Tested 02a2717b8 on Arch Linux + Wine 11.22 + NVIDIA RTX 3060 Ti (595.58.03 / Vulkan):

  • ctest --test-dir runtime/build/win -R "^d3d12_existing_heap$" --output-on-failure: Passed (5.49 s)

AI-assisted: yes

@Deedlyy

Deedlyy commented Nov 24, 2026

Copy link
Copy Markdown

Recommendation: [WAIT]

Reasoning: Draft: binds sub-page host buffer imports from the page start and keeps the prefix in buffer_offset, instead of falling back to an unrelated allocation when the offset is misaligned. The approach looks right and the existing-heap test is extended. But the PR is still marked draft, has no description template or claim, and overlaps #1190/#1100 in the same test file. Mark it ready, fill in the claim/testing sections, and coordinate merge order. CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with main.

Impact level: 4/10

@hubeton

hubeton commented Nov 24, 2026

Copy link
Copy Markdown

Independent A/B result on native Windows 11 build 26300.9974 with an Intel Core i9-13900K (24 cores, 32 threads), 64 GB RAM and an RTX 3060 Ti 8 GB (NVIDIA 621.24, Vulkan). Both executables were built with clang-cl/xwin in RelWithDebInfo.

I used the PR's identical d3d12_existing_heap_test.cpp in both builds. The control had the runtime from 6293c1c4 and the candidate had the runtime from 02a2717b.

  • Control (exit 1) reached both new cases. At offsets +16 and +32 bytes, the buffer's GPU VA differed from the caller address. The aligned and +64-byte cases passed.
  • PR (exit 0) passed both new cases, including preservation of the caller VA and initial data, copies in both directions, and unchanged bytes before and after the logical buffer. The existing heap cases matched native D3D12 on both the RTX 3060 Ti and WARP.

This is a synthetic memory-coherence test. I did not run the game. AI-assisted testing and report.

@GGandCo

GGandCo commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the independent A/B and Wine tests. The claim predates this PR and is linked at the top of the description: #14 (comment). I filled the repository template, rebased onto current main58be9cdf8, and added an isolated host-prefix CTest without removing the full existing-heap suite.

Our Intel A/B now reaches the behavior directly: unchanged backend fails only the 16/32-byte prefix cases; candidate passes normally and with Vulkan validation. The separate Intel failure after existing-heap operations is still disclosed. No experimental barrier change is included.

For merge order, #1190 owns existing-heap placed buffers and #1100 owns deferred attachments; this PR changes neither feature. They share files/test context, so I will rebase and retain their coverage if either merges first. CI workflow approval remains with maintainers.

@qhukz13

qhukz13 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

A second hardware point, where main fails without this PR: GeForce GTX 1050 Ti (Pascal, 4 GB), NVIDIA 582.66, Windows 10 Pro 19045, MSVC RelWithDebInfo.

  • main at 6261ca916: d3d12_existing_heap fails. The case that breaks is the existing "64 bytes into a page" one (check_title_placed, test line 271): the layer logs a title-placed buffer (device slot 133) could not import its pages ... HRESULT -2147467263 (E_NOTIMPL) and the check at line 196, GetGPUVirtualAddress() == pages, fails. The other slot-133 cases (VirtualAlloc range, section view, the two cache modifiers, the partly committed range) pass, and the 8 OpenExistingHeapFromAddress cases match the host.
  • This PR at 5ea48706b, built the same way: d3d12_existing_heap passes.

So on Pascal it is not only the 16- and 32-byte offsets: a 64-byte offset into a page is refused on main too, presumably because this driver's memory-binding alignment for the imported buffer is larger than 64, which the old offset % requirements.alignment check turned into the fallback. Binding at zero and carrying the offset fixes it here as well. That matters for the boot: slot 133 is how the title places its buffers on its own pages, and on this card main silently gives such a buffer memory the title's writes never reach.

The same failure is one of the five that #1217 lists as reproduced natively on a Pascal card.

No game files. AI-assisted (Claude Code).

@moffington

Copy link
Copy Markdown
Contributor

Reviewed 1e525c53 on Linux with an RTX 3060 (615.78.08), under Wine 11.20 and Proton Experimental.

The isolated host-prefix fixture passes at 0, 16, 32 and 64 bytes, including bidirectional coherence and surrounding sentinels. Both the published base and merge base fail the +16/+32 VA checks as expected.

I found one regression with a +4-byte host prefix:

  • 256-byte imported buffer.
  • CopyTextureRegion into a 1×1 R16G16B16A16_UNORM texture.
  • Resource-relative footprint offset 0, row pitch 256.

Both bases complete the copy through their ordinary-buffer fallback. HEAD successfully imports the buffer but returns ReplayResult::unsupported during texture-copy replay. The same test passes at +8 and +64 on HEAD, and ordinary buffer copies at +4 also pass.

The prefix added through ResourceBinding::buffer_offset triggers the 8-byte alignment guard in vulkan_replay.h:1869.

Reproduced under both runners, with and without validation. Could the existing aligned staging approach be extended to handle this case while preserving the host alias?

Native Xbox parity is unverified, but this is a reproducible regression relative to both base revisions.

@GGandCo

GGandCo commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the reproducible +4 report. Fixed in 5e6d404 (code in 3df5d0c).

The imported host alias is retained. Texture-copy replay now uses the existing aligned transfer scratch when a word-aligned host prefix violates the image's texel/block alignment, in both directions. Only occupied texels are copied; prefix, row padding and tail stay untouched. The direct path remains for already aligned copies, including byte-aligned R8; invalid logical offsets and overflowing spans remain rejected.

Tests on Windows 11 / Intel UHD Graphics 101.6314:

  • Expanded isolated d3d12_host_prefix: offsets 0/4/8/16/32/64, buffer coherence plus two-row R16G16B16A16_UNORM texture upload/readback with 256-byte row pitch and every untouched byte checked: passes normally and with Vulkan validation (no validation errors).
  • CPU replay_copy_offset fixture checks the exact scratch regions both ways, missing-scratch refusal and the R8 direct path: passes.
  • Negative control: identical new RGBA16 replay fixture against unchanged 1e525c5 fails both +4 directions; restoring the fix passes.

Could you repeat your Wine/Proton reproduction on this head? That environment has not been tested locally. No game boot or gameplay claim. AI-assisted.

@GGandCo

GGandCo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 42537e7; new head 7e467c6. The conflicts are resolved while preserving main's BDA word padding, low-address import guards, shadow fallback, additional placed-texture/shadow tests and texture-refresh test setup. This PR still adds only the host-prefix handling, isolated fixture and review regression fix.

Rebuilt RelWithDebInfo on Windows / Intel UHD Graphics 101.6314. d3d12_host_prefix and replay_copy_offset pass; the isolated GPU test also passes with Vulkan validation. Coverage remains offsets 0/4/8/16/32/64, buffer and two-row RGBA16 transfers in both directions, prefix/padding/tail preservation and safe missing-scratch refusal. Changed-file data and whitespace checks pass. The full monolithic heap suite was not rerun for this rebase. AI-assisted. Wine/Proton retest of the +4 review case remains welcome.

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@GGandCo
GGandCo force-pushed the fix/host-import-prefix branch from 7e467c6 to 4bee7a3 Compare November 25, 2026 10:56
@GGandCo

GGandCo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased again onto main bcaa13d; head 4bee7a3. The new conflict was in create_host_buffer's import-refusal cleanup. Resolution retains main's decline() cleanup (including import memory and page-pin release) while keeping the prefix inside the VkBuffer and binding at zero.

RelWithDebInfo build, d3d12_host_prefix and replay_copy_offset pass on Windows / Intel UHD Graphics 101.6314. The isolated buffer/texture-prefix fixture also passes with Vulkan validation. Changed-file game-data/whitespace checks pass. Full monolithic heap suite not rerun. AI-assisted.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@GGandCo
GGandCo force-pushed the fix/host-import-prefix branch from 4bee7a3 to 002c720 Compare November 25, 2026 13:07
@GGandCo

GGandCo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 98c2f77; head 002c720. The conflict was in imported-buffer ResourceBinding initialization: both main's indirect_arguments flag and this PR's logical buffer_offset are retained. RelWithDebInfo build, isolated d3d12_host_prefix/replay_copy_offset tests and the isolated GPU prefix fixture with Vulkan validation pass on Windows / Intel UHD Graphics 101.6314. Changed-file checks pass. Full heap suite not rerun. AI-assisted.

aslan2142 and others added 2 commits November 25, 2026 09:40
On a driver that refuses importing a section view (RADV: amdgpu userptr takes
anonymous memory only), the layer answers with a shadow at the same title VA,
so the `may_refuse` early return never fires. The forward readback still
passes through the shadow, but a shadow is never written back, so the new
reverse-copy check failed on an AMD iGPU under Wine. Check the caller's pages
only when the buffer is a real import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aslan2142
aslan2142 merged commit 3d4f661 into ProjectLeonida:main Nov 25, 2026
@aslan2142

Copy link
Copy Markdown
Collaborator

Merged. Thank you, @GGandCo, and thanks to @moffington for the +4 RGBA16 regression that the texture-copy staging now covers.

  • What I pushed to the branch: a merge of main and one test fix, no force-push.
    • The test fix: on a driver that refuses importing a section view (RADV, where amdgpu userptr takes anonymous memory only), the layer answers with a shadow at the same title VA, so the may_refuse early return never fired.
    • The new reverse-copy check then read a one-way shadow. It now checks the caller's pages only when title_shadow_buffer says the buffer is a real import.
  • Tested: Windows suite under Wine (llvmpipe and an RDNA2 iGPU) on current main + this branch. Nothing goes from pass to fail. The new d3d12_host_prefix passes on both devices, and so do the three d3d12_existing_heap variants that failed on the iGPU before.
  • d3d12: import a slot-133 buffer at any offset in its page (RT-SLOT133-IMPORT-OFFSET) #1768 makes the same change in create_host_buffer without the staging, so it will be closed as a duplicate.
  • Checked on main 446574e76 + 45219a640: it merges cleanly; the semantic suite passes (489 files, 6,288 tests, 0 files failed); the 14 docs and writer tests pass; check_no_game_data.py is clean on the 5 changed files.

AI-assisted (Claude Code, Claude Opus 5.5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants