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

Open
opened 2026-11-26 21:19:46 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2021
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/rt-title-image-upload-fast-scan


📝 Commits (1)

  • d26bfaf d3d12: speed up TitleImageUpload nonzero scan and harden bounds/release state

📊 Changes

3 files changed (+380 additions, -11 deletions)

View changed files

📝 runtime/CMakeLists.txt (+11 -0)
📝 runtime/shims/d3d12_title_image_upload.h (+56 -11)
➕ runtime/test/title_image_upload_test.cpp (+313 -0)

📄 Description

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2021 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-title-image-upload-fast-scan` --- ### 📝 Commits (1) - [`d26bfaf`](https://github.com/ubiquitarianism/leonida/commit/d26bfafff9d58c802c7a51bdb4615606b5b5e036) d3d12: speed up TitleImageUpload nonzero scan and harden bounds/release state ### 📊 Changes **3 files changed** (+380 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+11 -0) 📝 `runtime/shims/d3d12_title_image_upload.h` (+56 -11) ➕ `runtime/test/title_image_upload_test.cpp` (+313 -0) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2026
No description provided.