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

Open
github-bot wants to merge 1 commit from github-bot/gh-1458 into main AGit
Owner

@⁠robot00f on GitHub · 2026-11-25 00:37 UTC · original
robot00f:fix/host-buffer-subpage-ubo → main · +13 −1 · 2 files · 1 commits · open

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

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)
**@⁠robot00f** on GitHub · 2026-11-25 00:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1458) `robot00f:fix/host-buffer-subpage-ubo` → `main` · +13 −1 · 2 files · 1 commits · open Claimed in the coordination issue as: D3D12-HOST-BUFFER-SUBPAGE-UBO (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change)
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:52:07 +00:00
Author
Owner

@⁠Okachamie on GitHub · 2026-11-25 02:26 UTC · original

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.

**@⁠Okachamie** on GitHub · 2026-11-25 02:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1458#issuecomment-7150733011) 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.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 05:40 UTC · original

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.

**@⁠whoalin1** on GitHub · 2026-11-25 05:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1458#issuecomment-7154026904) 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.
Author
Owner

@⁠robot00f on GitHub · 2026-11-25 15:16 UTC · original

Rebased (9f85ac6d) 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 #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)

**@⁠robot00f** on GitHub · 2026-11-25 15:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1458#issuecomment-7166584823) Rebased (9f85ac6d) 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 #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)
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1458/head:github-bot/gh-1458'
git switch 'github-bot/gh-1458'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1458'
git switch 'github-bot/gh-1458'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1458'
git switch 'github-bot/gh-1458'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1458'
git switch 'main'
git merge --squash 'github-bot/gh-1458'
git switch 'main'
git merge --ff-only 'github-bot/gh-1458'
git switch 'main'
git merge 'github-bot/gh-1458'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.