Skip to content

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

Open
antikohza wants to merge 1 commit into
ProjectLeonida:mainfrom
antikohza:feature/texture-mip-drop-6043
Open

antikohza wants to merge 1 commit into
ProjectLeonida:mainfrom
antikohza:feature/texture-mip-drop-6043

Conversation

@antikohza

@antikohza antikohza commented Nov 24, 2026 •

Copy link
Copy Markdown

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).

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.
@Sangonapou

Copy link
Copy Markdown
Contributor

Tested 87064bf 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.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@MoonEyes

Copy link
Copy Markdown
Collaborator

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants