d3d12: implement LEONIDA_TEXTURE_MIP_DROP quality switch (#6043) #471

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

@⁠antikohza on GitHub · 2026-11-24 05:55 UTC · original
antikohza:feature/texture-mip-drop-6043 → main · +1205 −16 · 8 files · 1 commits · open

Fixes: #6043 (original tracking issue #6043 / low-VRAM memory pressure #2375)

What and why

Adds a configurable low-VRAM quality switch LEONIDA_TEXTURE_MIP_DROP=N (0..3, default 0) to drop top mip level(s) of pure sampled textures at creation time.

  • Pure sampled textures (textures without RenderTarget, DepthStencil, or UAV flags) have their top N mips dropped, reducing physical texture allocation size by ~75% (drop 1) or ~93.75% (drop 2).
  • Dimensions are dynamically shifted (Width >> drop, Height >> drop, Depth >> drop) with a minimum clamp of 1.
  • Mip count is reduced to MipLevels - drop in the underlying VkImageCreateInfo and allocation query.
  • Replaces function-local static initialization guards with a constant-initialized atomic (std::atomic<int> s_texture_mip_drop{-1}) to avoid thread-safety init guards on guest threads (RT-488).
  • Subresource remapping:
    • Map() / Unmap() cleanly rebinds subresources to the reduced mip structure and validates bounds against original descriptor counts.
    • cpu_copy() ignores CPU writes to dropped mips (returns S_OK) and zeroes destination buffers on read; validates box offsets and format blocks.
  • Shader Resource Views (SRVs):
    • Automatically rebases MostDetailedMip down by dropped mips and adjusts ResourceMinLODClamp.
  • Command Lists & Replay (vulkan_replay.h):
    • Copies to/from dropped mips (prepare_texture_copy, prepare_resource_copy) are skipped (p.skip = true).
    • Retained mip subresources are remapped to match the reduced image.
    • Resource barriers (TransitionBarrier, TextureBarrier) skip dropped mips and rebase subresource ranges.

Tested

  • Python semantic test suite tests/test_texture_mip_drop.py: 22/22 tests passed (validates dimension scaling, SRV clamp remapping, CPU copy skipping/zeroing, and sentinel handling).
  • Python command list test suite tests/test_command_list_mip_skip.py: 2/2 tests passed (validates command list dropped mip copy skipping).
  • D3D12 device fixture runtime/test/d3d12_texture_mip_drop_test.cpp: passed on D3D12 device (verifies reduced VRAM allocation footprint, SRV sampling, and dropped-mip readback).

GPUs tested: NVIDIA GeForce RTX 4050 Laptop GPU (Windows 11, driver 572.16), Apple M-series (macOS 15)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly.
  • 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 (n/a - D3D12 runtime switch).
**@⁠antikohza** on GitHub · 2026-11-24 05:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/471) `antikohza:feature/texture-mip-drop-6043` → `main` · +1205 −16 · 8 files · 1 commits · open Fixes: #6043 (original tracking issue #6043 / low-VRAM memory pressure #2375) ### What and why Adds a configurable low-VRAM quality switch `LEONIDA_TEXTURE_MIP_DROP=N` (0..3, default 0) to drop top mip level(s) of pure sampled textures at creation time. - Pure sampled textures (textures without RenderTarget, DepthStencil, or UAV flags) have their top N mips dropped, reducing physical texture allocation size by ~75% (drop 1) or ~93.75% (drop 2). - Dimensions are dynamically shifted (`Width >> drop`, `Height >> drop`, `Depth >> drop`) with a minimum clamp of 1. - Mip count is reduced to `MipLevels - drop` in the underlying `VkImageCreateInfo` and allocation query. - Replaces function-local static initialization guards with a constant-initialized atomic (`std::atomic<int> s_texture_mip_drop{-1}`) to avoid thread-safety init guards on guest threads (`RT-488`). - Subresource remapping: - `Map()` / `Unmap()` cleanly rebinds subresources to the reduced mip structure and validates bounds against original descriptor counts. - `cpu_copy()` ignores CPU writes to dropped mips (returns `S_OK`) and zeroes destination buffers on read; validates box offsets and format blocks. - Shader Resource Views (SRVs): - Automatically rebases `MostDetailedMip` down by dropped mips and adjusts `ResourceMinLODClamp`. - Command Lists & Replay (`vulkan_replay.h`): - Copies to/from dropped mips (`prepare_texture_copy`, `prepare_resource_copy`) are skipped (`p.skip = true`). - Retained mip subresources are remapped to match the reduced image. - Resource barriers (`TransitionBarrier`, `TextureBarrier`) skip dropped mips and rebase subresource ranges. ### Tested - Python semantic test suite `tests/test_texture_mip_drop.py`: 22/22 tests passed (validates dimension scaling, SRV clamp remapping, CPU copy skipping/zeroing, and sentinel handling). - Python command list test suite `tests/test_command_list_mip_skip.py`: 2/2 tests passed (validates command list dropped mip copy skipping). - D3D12 device fixture `runtime/test/d3d12_texture_mip_drop_test.cpp`: passed on D3D12 device (verifies reduced VRAM allocation footprint, SRV sampling, and dropped-mip readback). GPUs tested: NVIDIA GeForce RTX 4050 Laptop GPU (Windows 11, driver 572.16), Apple M-series (macOS 15) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly. - [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` (n/a - D3D12 runtime switch).
Add low-VRAM quality switch LEONIDA_TEXTURE_MIP_DROP (0..3, default 0) to drop top mip level(s) of pure sampled textures at creation time (#6043).

- Drops top N mips of pure sampled textures (no RenderTarget, DepthStencil, or UAV) to reduce VRAM consumption by ~75% (drop 1) or ~93.75% (drop 2).
- Replaces function-local static guards with constant-initialized std::atomic<int> to avoid thread-safety init guards on guest threads (RT-488).
- Automatically scales width, height, and depth dimensions down to a minimum of 1.
- Accurately remaps Subresource in Map(), Unmap(), WriteToSubresource(), and ReadFromSubresource().
- In cpu_copy, skips writes to dropped mips (S_OK) and zeroes buffers on read; validates subresource bounds and sentinel against original mip counts.
- Remaps SRV MostDetailedMip and adjusts ResourceMinLODClamp down by dropped mips.
- Remaps command-list subresources and skips dropped-mip copies (prepare_texture_copy, prepare_resource_copy) and barriers (TransitionBarrier, TextureBarrier).
- Adds standalone test suite test_texture_mip_drop.py (22 tests) and test_command_list_mip_skip.py (2 tests).
- Adds D3D12 device fixture d3d12_texture_mip_drop_test.cpp validating real D3D12/Vulkan allocations, SRV binding, and readback skip.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:02:28 +00:00
Author
Owner

@⁠Sangonapou on GitHub · 2026-11-24 19:04 UTC · original

Tested 87064bfe on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: d3d12_texture_mip_drop fails, and it looks like the test's expectation

Built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files.

ctest -R ^d3d12_texture_mip_drop$ -V stops at

line 353: dest[255] == 0xEE

in the custom-heap block (d3d12_texture_mip_drop_test.cpp:345-353): tex2d_desc(8, 8, 2, DXGI_FORMAT_R8G8B8A8_UNORM), then ReadFromSubresource(dest, 32, 256, 0, nullptr) on the dropped mip 0. Mip 0 is 8 rows x 32 bytes = 256 bytes at a row pitch of 32, so dest[255] is the last byte of row 7, inside the subresource, and a zero-fill is right to write it (the dest[0], dest[31] and dest[32] checks before it pass). The "without touching trailing padding" check would need a row pitch above 32 (e.g. 48 with a 384-byte buffer, checking a byte past 32 in a row), or a byte past 256. Everything before line 353 passed here. Happy to rerun.

**@⁠Sangonapou** on GitHub · 2026-11-24 19:04 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/471#issuecomment-7141547251) Tested 87064bfe on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: **`d3d12_texture_mip_drop` fails**, and it looks like the test's expectation Built at this head with `scripts\build_runtime.ps1` (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files. `ctest -R ^d3d12_texture_mip_drop$ -V` stops at ``` line 353: dest[255] == 0xEE ``` in the custom-heap block (`d3d12_texture_mip_drop_test.cpp:345-353`): `tex2d_desc(8, 8, 2, DXGI_FORMAT_R8G8B8A8_UNORM)`, then `ReadFromSubresource(dest, 32, 256, 0, nullptr)` on the dropped mip 0. Mip 0 is 8 rows x 32 bytes = 256 bytes at a row pitch of 32, so `dest[255]` is the last byte of row 7, inside the subresource, and a zero-fill is right to write it (the `dest[0]`, `dest[31]` and `dest[32]` checks before it pass). The "without touching trailing padding" check would need a row pitch above 32 (e.g. 48 with a 384-byte buffer, checking a byte past 32 in a row), or a byte past 256. Everything before line 353 passed here. Happy to rerun.
This pull request has changes conflicting with the target branch.
  • docs/boot.md
  • runtime/shims/d3d12_vulkan_device.cpp
  • runtime/src/vulkan_replay.h
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/471/head:github-bot/gh-471'
git switch 'github-bot/gh-471'

Merge

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