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

Closed
leonida/mandy
antikohza wants to merge 0 commits from feature/texture-mip-drop-6043 into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

antikohza commented 2026-11-24 05:55:48 +00:00

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).
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).
Sangonapou commented 2026-11-24 19:04:03 +00:00

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.

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.
MoonEyes commented 2026-11-27 23:19:50 +00:00

Hi @antikohza, thanks for this pull request. It currently conflicts with main (conflicting file(s): docs/boot.md, runtime/shims/d3d12_vulkan_device.cpp, runtime/src/vulkan_replay.h), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

Hi @antikohza, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `docs/boot.md`, `runtime/shims/d3d12_vulkan_device.cpp`, `runtime/src/vulkan_replay.h`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 17:20:49 +00:00

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.

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.
Sign in to join this conversation.
No description provided.