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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 18:16 UTC · original
IlyassRS3:perf/rt-title-image-upload-fast-scan → main · +380 −11 · 3 files · 1 commits · open

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.
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2021) `IlyassRS3:perf/rt-title-image-upload-fast-scan` → `main` · +380 −11 · 3 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:15:00 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:16 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 18:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2021#pullrequestreview-6062735582) · **reviewed** 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.
This pull request has changes conflicting with the target branch.
  • runtime/CMakeLists.txt
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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