d3d12: speed up TitleImageUpload nonzero scan and harden bounds/release state (RT-TITLE-IMAGE-UPLOAD-FAST-SCAN-AND-GUARDS) #2021

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/2021 into main
IlyassRS3 commented 2026-11-25 18:16:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T18:16:19Z.


Claim: RT-TITLE-IMAGE-UPLOAD-FAST-SCAN-AND-GUARDS (claim comment #7170576832 on #1608).

Problem

In runtime/shims/d3d12_title_image_upload.h:

  1. Byte-by-byte readback of mapped staging memory: when inspect && !nonzero, TitleImageUpload::write() scanned slot.mapped + copy.bufferOffset one byte at a time via std::any_of. On discrete GPUs, HOST_VISIBLE | HOST_COHERENT staging buffers are typically write-combined (WC), making byte-by-byte reads across multi-megabyte zero-filled textures extremely slow.
  2. Missing input/overflow guards: write() did not guard !tiled or !block.block, used wrapping (extent + block - 1) / block, did not check bytes + pad for VkDeviceSize wrap, and multiplied row * mip.height_elements before verifying slice <= UINT32_MAX (allowing 64-bit wrap past the check).
  3. Null vkWaitSemaphores in wait_slot(): checked vk.vkGetSemaphoreCounterValue for null, then called vk.vkWaitSemaphores unconditionally.
  4. Incomplete state reset in release(): left slot.value, next_, value_, consumers_, and regions_ populated across release().

Fix

  • Add detail::has_nonzero_bytes() scanning mapped staging memory in 64-byte unrolled uint64_t OR-reduction chunks (then 8-byte and byte tails).
  • Guard !tiled and !block.block, use overflow-free 1u + (extent - 1u) / block.block, guard alignment padding against UINT64_MAX - bytes, and check row > UINT32_MAX / mip.height_elements before multiplying slice.
  • Guard if (!vk.vkWaitSemaphores) return false; in wait_slot().
  • Reset slot.value, next_, value_, consumers_, and regions_ in release().
  • Add CPU-only CTest title_image_upload (runtime/test/title_image_upload_test.cpp) driving TitleImageUpload with mock Vulkan functions.

Benchmark (clang++ -std=c++20 -O2, 16 MiB all-zero texture x 50 scans)

  • std::any_of byte scan: 219.74 ms
  • detail::has_nonzero_bytes (64-byte unrolled uint64_t OR-reduction): 8.67 ms (25.35x faster on cached RAM, and avoids byte-by-byte WC read transactions on discrete GPU staging buffers)

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Ran runtime/test/title_image_upload_test.cpp (with minimal dxgiformat.h stub + Vulkan-Headers) cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Ran python3 -I scripts/check_no_game_data.py runtime/shims/d3d12_title_image_upload.h runtime/test/title_image_upload_test.cpp runtime/CMakeLists.txt and python3 -I tests/test_text_write_line_endings.py: all passed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5496730082 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2021). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T18:16:19Z. --- Claim: `RT-TITLE-IMAGE-UPLOAD-FAST-SCAN-AND-GUARDS` (claim comment `#7170576832` on #1608). ### Problem In `runtime/shims/d3d12_title_image_upload.h`: 1. **Byte-by-byte readback of mapped staging memory**: when `inspect && !nonzero`, `TitleImageUpload::write()` scanned `slot.mapped + copy.bufferOffset` one byte at a time via `std::any_of`. On discrete GPUs, `HOST_VISIBLE | HOST_COHERENT` staging buffers are typically write-combined (WC), making byte-by-byte reads across multi-megabyte zero-filled textures extremely slow. 2. **Missing input/overflow guards**: `write()` did not guard `!tiled` or `!block.block`, used wrapping `(extent + block - 1) / block`, did not check `bytes + pad` for `VkDeviceSize` wrap, and multiplied `row * mip.height_elements` before verifying `slice <= UINT32_MAX` (allowing 64-bit wrap past the check). 3. **Null `vkWaitSemaphores` in `wait_slot()`**: checked `vk.vkGetSemaphoreCounterValue` for null, then called `vk.vkWaitSemaphores` unconditionally. 4. **Incomplete state reset in `release()`**: left `slot.value`, `next_`, `value_`, `consumers_`, and `regions_` populated across `release()`. ### Fix - Add `detail::has_nonzero_bytes()` scanning mapped staging memory in 64-byte unrolled `uint64_t` OR-reduction chunks (then 8-byte and byte tails). - Guard `!tiled` and `!block.block`, use overflow-free `1u + (extent - 1u) / block.block`, guard alignment padding against `UINT64_MAX - bytes`, and check `row > UINT32_MAX / mip.height_elements` before multiplying `slice`. - Guard `if (!vk.vkWaitSemaphores) return false;` in `wait_slot()`. - Reset `slot.value`, `next_`, `value_`, `consumers_`, and `regions_` in `release()`. - Add CPU-only CTest `title_image_upload` (`runtime/test/title_image_upload_test.cpp`) driving `TitleImageUpload` with mock Vulkan functions. ### Benchmark (`clang++ -std=c++20 -O2`, 16 MiB all-zero texture x 50 scans) - `std::any_of` byte scan: `219.74 ms` - `detail::has_nonzero_bytes` (64-byte unrolled `uint64_t` OR-reduction): `8.67 ms` (**25.35x faster** on cached RAM, and avoids byte-by-byte WC read transactions on discrete GPU staging buffers) ### Verification - **AI-assisted**: yes (Antigravity agent by Google DeepMind). - **GPUs tested**: none (host is macOS arm64; no retail files). - Ran `runtime/test/title_image_upload_test.cpp` (with minimal `dxgiformat.h` stub + Vulkan-Headers) cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Ran `python3 -I scripts/check_no_game_data.py runtime/shims/d3d12_title_image_upload.h runtime/test/title_image_upload_test.cpp runtime/CMakeLists.txt` and `python3 -I tests/test_text_write_line_endings.py`: all passed.
copilot-pull-request-reviewer[bot] commented 2026-11-25 18:16:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T18:16:34Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6062735582 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2021#pullrequestreview-6062735582). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T18:16:34Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
OreoFR commented 2026-11-29 12:42:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T12:42:32Z.


@IlyassRS3 this PR now conflicts with main (runtime/CMakeLists.txt, runtime/shims/d3d12_title_image_upload.h). Could you merge main into your branch (no force-push needed)? If there's no update within 24 h, OreoFR may carry it in a new PR that keeps you as co-author (per the maintainers' plan). — OreoFR's agent (AI-assisted)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7256006507 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2021#issuecomment-7256006507). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T12:42:32Z. --- @IlyassRS3 this PR now conflicts with `main` (`runtime/CMakeLists.txt`, `runtime/shims/d3d12_title_image_upload.h`). Could you merge `main` into your branch (no force-push needed)? If there's no update within 24 h, OreoFR may carry it in a new PR that keeps you as co-author (per the maintainers' plan). — OreoFR's agent (AI-assisted)
aslan2142 commented 2026-12-01 16:41:15 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:41:15Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7318036081 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2021#issuecomment-7318036081). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:41:15Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2021
No description provided.