Skip to content

d3d12: drop UNIFORM_BUFFER_BIT on sub-page host imports refused by 256B UBO alignment (D3D12-HOST-BUFFER-SUBPAGE-UBO) - #1458

Open
robot00f wants to merge 1 commit into
ProjectLeonida:mainfrom
robot00f:fix/host-buffer-subpage-ubo
Open

robot00f wants to merge 1 commit into
ProjectLeonida:mainfrom
robot00f:fix/host-buffer-subpage-ubo

Conversation

@robot00f

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-HOST-BUFFER-SUBPAGE-UBO (#14 (comment))

What and why

On main (b4b7756e), d3d12_existing_heap fails deterministically (3/3) on NVIDIA Pascal at runtime/test/d3d12_existing_heap_test.cpp:196 during check_title_placed(device, "64 bytes into a page", pages + 64, 0x1000 - 64) (d3d12_existing_heap_test.cpp:271), where Resource::create_host_buffer returns E_NOTIMPL (0x80004001) and create_at_host_address falls back to a non-aliasing buffer.

Measured cause on NVIDIA GeForce GTX 1050 Ti (driver 582.28):

  • host_pointer_alignment = 4096, minUniformBufferOffsetAlignment = 256, minStorageBufferOffsetAlignment = 16.
  • Resource::create_host_buffer (runtime/shims/d3d12_vulkan_device.cpp:2077) unconditionally requests VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT.
  • On Turing+ NVIDIA GPUs (minUniformBufferOffsetAlignment = 64), a 4032-byte buffer at sub-page offset = 64 reports requirements.alignment = 64 and requirements.size = 4032, which fits in bytes = 4096.
  • On Pascal (minUniformBufferOffsetAlignment = 256), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT raises requirements.alignment from 16 to 256 and rounds requirements.size up to a multiple of 256 (4032 -> 4096, 16 -> 256). At offset = 64, both guards at d3d12_vulkan_device.cpp:2092 fail (64 % 256 != 0 and 4096 < 64 + 4096); a 16-byte UAV placed 64 bytes before the end of a page (offset = 4032, size = 16) likewise fails both checks (4032 % 256 != 0 and 4096 < 4032 + 256). Without VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, the same buffers report requirements.alignment = 16 and requirements.size = 4032 / 16.

D3D12 constant-buffer views require 256-byte address and size alignment (D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT) anyway, and the layer encodes CBVs as 16-byte GFX10 raw V# descriptors (CreateConstantBufferView, d3d12_vulkan_device.cpp:6560) rather than VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER.

Changes:

  • In Resource::create_host_buffer (runtime/shims/d3d12_vulkan_device.cpp), when (offset % requirements.alignment) || requirements.size > bytes - offset, recreate buffer_ without VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT and re-query VkMemoryRequirements before checking the final alignment and room guards (written as requirements.size > bytes - offset to avoid unsigned addition wrap). Page-aligned and 256-byte-aligned slices keep the existing usage flags unchanged, and genuinely unaligned addresses (bytes + 1) still take the committed-buffer fallback.
  • In runtime/test/d3d12_existing_heap_test.cpp, add a 16-byte sub-page case 64 bytes from the end of the committed range ("16 bytes near the end of the range") so both the sub-256-byte offset and the 256-byte UBO size-rounding overflow past the page boundary are covered.

Tested

Built with MSVC 19.44 (Visual Studio 2022 Professional), Ninja, RelWithDebInfo, VK_LOADER_LAYERS_DISABLE=~implicit~.

Test Before (main b4b7756e) After (fix/host-buffer-subpage-ubo)
d3d12_existing_heap (3 consecutive runs) 3/3 Failed (1.18 s, 0.73 s, 0.73 s) at line 196 ("64 bytes into a page", E_NOTIMPL) 3/3 Passed (0.89 s, 0.67 s, 0.67 s), including "64 bytes into a page", "16 bytes near the end of the range", and all 8 OpenExistingHeapFromAddress parity cases against GTX 1050 Ti and WARP
Neighbor runtime tests (device_proxy, xmem, vk_host_region_import, d3d12_xmem_placed, d3d12_descriptor, d3d12_console_descriptor_heap) — 6/6 Passed (2.29 s total)

Additional checks:

  • python -I scripts/check_no_game_data.py runtime/shims/d3d12_vulkan_device.cpp runtime/test/d3d12_existing_heap_test.cpp (exit 0)
  • python -I tests/test_docs_links.py (16 tests, OK) and python -I tests/test_docs_table_rows.py (2 tests, OK)
  • git diff --check (exit 0)
  • Not tested: LEONIDA_VK_VALIDATION=1 (VK_LAYER_KHRONOS_validation is not installed on this machine), the full ctest suite, Windows 11, Turing+/AMD/Intel GPUs, or Linux/Wine.

GPUs tested: NVIDIA GeForce GTX 1050 Ti (Pascal, 4 GB), driver 582.28, Vulkan 1.4.312, Windows 10 Pro 10.0.19045

AI-assisted: yes (Antigravity)

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)

@Okachamie

Copy link
Copy Markdown
Contributor

I checked 12862a6fd against #1192 at 5e6d4048. git merge-tree --write-tree reports conflicts in runtime/shims/d3d12_vulkan_device.cpp and runtime/test/d3d12_existing_heap_test.cpp.

#1192 includes the page prefix in the Vulkan buffer size and binds at offset zero. Based on the reported 256-byte rounding, both Pascal cases here should fit in a 4 KiB import with that approach. The two aliasing cases in this PR would help verify whether #1192 covers them on Pascal. This is a source comparison and merge check. I did not run either head on Pascal. AI-assisted.

@whoalin1

Copy link
Copy Markdown
Contributor

Independent verification on both of our GPUs (native Windows 11, validation on):

  • NVIDIA RTX 4070 SUPER (driver 616.64-class, Vulkan): 12/12 pass — d3d12_existing_heap (incl. the new "16 bytes near the end of the range" case), device_proxy, device_proxy_trace, xmem, vk_host_region_import(+_validation), d3d12_xmem_placed, d3d12_descriptor, d3d12_predicated_copy, d3d12_console_descriptor_heap, predicated_copy, vk_predicated_copy.
  • AMD Radeon iGPU (Ryzen 5 7600, gfx1036, driver 24.10.24, LEONIDA_PC_TAIL_UBO=1): identical 12/12, d3d12_existing_heap reports "GPU VA is the address" for both new slice cases plus 8/8 OpenExistingHeapFromAddress parity cases vs AMD host and WARP.
  • Clean origin/main c7e82e26e baseline: 27/27 on both GPUs — no pre-existing failures, no PR-vs-main delta.

One honest caveat: neither of our GPUs has minUniformBufferOffsetAlignment = 256, so the new drop-UNIFORM_BUFFER_BIT retry branch itself is not exercised here — what this run proves is no regression plus the new test case executing correctly on both vendors. The fix confirmation rests on the author's Pascal repro, which is the right evidence for that path.

AI-assisted verification.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@robot00f
robot00f force-pushed the fix/host-buffer-subpage-ubo branch from 12862a6 to 96b22d7 Compare November 25, 2026 15:04
…6B UBO alignment

Resource::create_host_buffer adds VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT to every
imported host buffer. On GPUs whose minUniformBufferOffsetAlignment is 256
bytes (measured on NVIDIA GeForce GTX 1050 Ti, Pascal), that raises
VkMemoryRequirements::alignment from 16 to 256 and rounds requirements.size up
to a multiple of 256, refusing 64-byte-aligned sub-page slot-133 placements
such as "64 bytes into a page" in d3d12_existing_heap_test with E_NOTIMPL.

D3D12 constant-buffer views require 256-byte address and size alignment anyway,
and the layer encodes CBVs as raw GFX10 V# descriptors rather than Vulkan
uniform buffers. Recreate the VkBuffer without VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
when the UBO requirement rejects a sub-page offset or size, compare remaining
bytes without addition wrap, and add a 16-byte end-of-range sub-page case to
d3d12_existing_heap_test.
@robot00f
robot00f force-pushed the fix/host-buffer-subpage-ubo branch from 96b22d7 to 9f85ac6 Compare November 25, 2026 15:15
@robot00f

Copy link
Copy Markdown
Contributor Author

Rebased (9f85ac6) onto current main (�0f3d399, after #1192 merged) and verified on NVIDIA GeForce GTX 1050 Ti (Pascal, 4 GB, driver 582.28, Vulkan 1.4.312, Windows 10 Pro 10.0.19045):

  • Confirming @Okachamie's note and @whoalin1's run: with d3d12: preserve offsets in sub-page host buffer imports (RT-HOST-IMPORT-PREFIX) #1192 binding at offset zero and sizing the buffer to offset + backing_size, Pascal's 256-byte UNIFORM_BUFFER_BIT rounding fits inside the 4 KiB minImportedHostPointerAlignment window (host prefix buffers: 0/4/8/16/32/64-byte offsets ... PASS, 64 bytes into a page, and 16 bytes near the end of the range all succeed on Pascal, along with �k_host_region_import and d3d12_xmem_placed 3/3).
  • Updated the retry condition in Resource::create_host_buffer to
    equirements.size > bytes (using decline(E_OUTOFMEMORY) on buffer recreation failure) so UNIFORM_BUFFER_BIT is still dropped if a driver/layer rounds
    equirements.size past �ytes (minImportedHostPointerAlignment), while keeping the 16 bytes near the end of the range regression check alongside check_host_prefix_buffers.

AI-assisted: yes (Antigravity)

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 26, 2026
@zmasi

zmasi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Independently verified rebased head 9f85ac6de2fda8feb0895015e2a46b7a4359a4c1 merged cleanly onto origin/main (59e65849b0927ee8c435baf1c74aecd08104fd60) on Windows 11 (10.0.26200), MSVC 19.44.35207 (RelWithDebInfo), NVIDIA GeForce RTX 3090 (driver 595.79), with VK_LAYER_KHRONOS_validation 1.4.363.1 (LEONIDA_VK_VALIDATION=1 and VK_VALIDATE_SYNC=1). No game files used.

Verification Results (#1458 merged with origin/main 59e65849b0 on RTX 3090)

  • d3d12_existing_heap: Passed (8.9 s, including "64 bytes into a page", check_host_prefix_buffers, "16 bytes near the end of the range" slot-133 import #9 (16 bytes), and 8/8 OpenExistingHeapFromAddress parity cases against both host RTX 3090 D3D12 and WARP; 0 runtime validation errors).
  • d3d12_existing_heap_no_write_watch: Passed (7.16 s, 8/8 parity cases vs host RTX 3090 and WARP).
  • vk_host_region_import & vk_host_region_import_validation (VK_VALIDATE_SYNC=1): Passed (0.41 s / 0.42 s, 0 Khronos validation errors).
  • d3d12_xmem_placed & d3d12_descriptor: Passed (0 Khronos validation errors).
  • scripts/check_no_game_data.py: Passed.

Code Audit Note (Post-#1192 Arithmetic & Comment Accuracy)

  • Now that #1192 (main) binds imported host buffers at offset = 0 and sizes the VkBuffer to info.size = offset + backing_size, bytes is align_up(offset + backing_size, host_alignment). Because minImportedHostPointerAlignment on Windows/Vulkan is a power of two >= 4096 (a multiple of 256), align_up(info.size, 256) <= bytes holds for every sub-page slice — including "16 bytes near the end of the range" (offset = 4032, size = 16 -> info.size = 4048 -> align_up(4048, 256) = 4096 == bytes).
  • Confirmed empirically on RTX 3090 under VK_LAYER_KHRONOS_validation 1.4.363.1 by forcing requirements.size = align_up(requirements.size, 256) whenever VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT is set: d3d12_existing_heap passes both with and without the retry branch, whereas the requirements.size > bytes retry remains a clean defensive fallback (vkDestroyBuffer -> strip VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT -> vkCreateBuffer / decline(E_OUTOFMEMORY)) if any driver/layer rounds UNIFORM_BUFFER_BIT buffers to a page multiple larger than minImportedHostPointerAlignment (e.g., 64 KiB on a 4 KiB host import). Updating the two-line comment at d3d12_existing_heap_test.cpp:610-611 to reflect post-#1192 offset + size sizing is optional cleanup.

AI-assisted: yes

@enlight3d

Copy link
Copy Markdown
Contributor

RUN #1458 | head 9f85ac6, merged cleanly with main 54b2a81 (local merge, not pushed)

Commands (from the repository root, in a worktree):

git fetch origin pull/1458/head:pr-1458
git checkout --detach origin/main && git merge --no-edit --no-ff pr-1458
cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON
cmake --build runtime/build -j 8 --target gta6rt d3d12_existing_heap_test
ctest -C RelWithDebInfo -R "^d3d12_existing_heap$" --output-on-failure -j1

Green: d3d12_existing_heap passes on the merged tree. The neighbours the description lists also pass (device_proxy, xmem, vk_host_region_import, d3d12_xmem_placed, d3d12_descriptor, d3d12_console_descriptor_heap, 6/6).

Red: not reproduced on this GPU, so no red to report. With the change in runtime/shims/d3d12_vulkan_device.cpp reverted and the PR's test kept (new case included), d3d12_existing_heap still passes. This matches the description: the failure needs minUniformBufferOffsetAlignment 256 (Pascal), and vulkaninfo reports 64 (0x40) here, so the sub-page import is never refused. This run confirms no regression on Ampere, not the fix itself.

Link: no new imports vs main (gta6rt.exe, 723 DLL!symbol entries, same list as the baseline built from main).

Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44, Vulkan SDK 1.4.363.1.

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