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

3 participants