DARNER_CVV=1: card memory the CPU maps on Turing via NVAPI CPU-visible vidmem #485

Open
feng wants to merge 3 commits from feng/Darner:nvapi-cpu-visible-vidmem into Main
First-time contributor

Refs #82

Contributor role (optional)

  • Fluxer ID: kirums#8956

What it changes

  • The problem on main: On NVIDIA Turing cards (such as RTX 2080 Ti) with expanded BAR1 (> 256 MiB, e.g. 16 GiB via ReBAR/VBIOS mod), the D3D12 driver does not advertise D3D12_FEATURE_DATA_D3D12_OPTIONS16::GPUUploadHeapSupported, even though NVIDIA exposes CPU-visible VRAM via NVAPI (NvAPI_D3D12_QueryCpuVisibleVidmem + NvAPI_D3D12_CreateHeap2 with NV_D3D12_HEAP_FLAG_CPUVISIBLE_VIDMEM). As a result, Host::mapped_card_buffer and heap_twin::make stay unavailable on Turing and fall back to system RAM.

This adds an opt-in switch DARNER_CVV=1 (off by default, asked_for!("DARNER_CVV"), zero behaviour change when unset):

  • crates/darner-gpu/src/nvapi.rs: loads nvapi64.dll from System32 (LOAD_LIBRARY_SEARCH_SYSTEM32), queries NvAPI_D3D12_QueryCpuVisibleVidmem dynamically per discrete NVIDIA adapter (nothing is hardcoded to 16 GiB), requires BAR1 > 256 MiB, and creates a D3D12_HEAP_TYPE_CUSTOM + WRITE_COMBINE + L0 CPU-visible VRAM heap via NvAPI_D3D12_CreateHeap2. Using CUSTOM + WRITE_COMBINE + L0 (instead of UPLOAD) both lands in CPU-visible VRAM and permits D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS + COMMON state needed by DARNER_WINDOWS_BAR_MB, DARNER_HEAP_TWIN, DARNER_SCRATCH_CARD, and host::bus. Any CreateHeap2 failure falls back cleanly to system RAM.
  • crates/darner-gpu/src/host.rs & heap_twin.rs: wires the NVAPI CVV fallback into Host::mapped_card_buffer and heap_twin::make, attaching the ID3D12Heap lifetime to the placed ID3D12Resource via SetPrivateDataInterface and skipping redundant MakeResident on the already-resident CVV heap.
  • crates/darner-gpu/src/cardledger.rs, residency.rs, resource.rs: adds residency::placed_as(MappedVram) and cardledger::made_mapped so CVV placed buffers are accounted as mapped card VRAM rather than Shared/Other, and restricts the DARNER_ALIAS_SHARE=1 upload-heap guard to GpuUpload.
  • README.md & docs/memory-separation.md: documents DARNER_CVV.

How it was tested

  • Tested revision: 4c00698 (rebased cleanly onto origin/main d91d8e4)
  • Commands and results:
    • cargo xtask policy -> PASS (566 files ok)
    • cargo clippy --workspace --all-targets --locked -- -D warnings -> PASS (0 warnings)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings -> PASS (0 warnings)
    • cargo test -p darner-gpu --lib --locked nvapi -> PASS (3/3)
    • cargo test -p darner-gpu --lib --locked cardledger -> PASS (25/25)
    • cargo test -p darner-gpu --lib --locked residency -> PASS (13/13)
    • DARNER_CVV=1 cargo test -p darner-gpu --lib --locked mapped_card_memory_is_written_by_the_cpu_and_read_by_the_card -> PASS (1/1 on live RTX 2080 Ti)
    • host::bus 256 MiB benchmark (measured earlier on the same commit before rebase onto d91d8e4, identical code) on RTX 2080 Ti 22 GB with DARNER_CVV=1: CPU write 10.4 GB/s, CPU read 0.05 GB/s, compute shader read 492.3 GB/s, UAV store 139.1 GB/s (matching DEFAULT VRAM 495.0 / 141.9 GB/s, vs system RAM 6.2 / 0.6 GB/s).
  • Changes a default: no (opt-in behind DARNER_CVV=1, off when unset).
  • A/B result: Off by default (DARNER_CVV unset creates no CVV heap, verified in live boot log on RTX 2080 Ti). Checked with DARNER_CVV=1 via the host::bus 256 MiB transfer benchmark above and a 114-minute live gameplay run.
  • Gates run on that revision (the three in AGENTS.md; there is no CI):
    • Gate 1 (cargo clippy --workspace --all-targets --locked -- -D warnings): PASS on 4c00698
    • Gate 2 (cargo clippy --workspace --all-targets --all-features --locked -- -D warnings): PASS on 4c00698
    • Gate 3 (cargo test --workspace --locked): targeted unit & live GPU tests above passed on 4c00698; full workspace test run was not completed on this machine because pre-existing unrelated darner-shader oracle64::legacy_mul_forms_are_exact hangs >20 min in debug mode and darner-gpu watch::tests has intermittent parallel flakes (related to #262).
  • Gameplay, when relevant: Intel Core i5-13490F, 31.8 GB RAM, NVIDIA GeForce RTX 2080 Ti 22 GB modded (Turing TU102, 16 GiB BAR1), Windows 11 build 26300, driver 621.1; Series X profile, 1080to1440, DLSS Performance, 3x DLSS FG (via dlssg_for_sm86), RT off. Verified on pre-rebase equivalent commit (7b1e836, identical diff) across a 114-minute continuous session (222,000+ frames): DARNER_HEAP_TWIN=1 placed the 174 MB graphics heap twin in CPU-visible VRAM; cardledger tracked window-mapped (4,062 MB early -> 2,754 MB at 114 min, other-mapped 216 -> 169 MB, scratch 134 -> 142 MB); 0 crashes, 0 device losses, 0 no buffer in card memory, 0 no twin.
  • Shader wave sizes tested, when relevant: N/A (no shader translator changes).
  • Hardware dependence: NVIDIA Turing (and other discrete NVIDIA GPUs with > 256 MiB BAR1 lacking D3D12 GPU upload heaps); untested on stock 256 MiB BAR1 Turing cards (guard rejects BAR1 <= 256 MiB), AMD, or Intel.
  • Coverage gaps: Not tested in-game on AMD/Intel or on a card where GPUUploadHeapSupported is already true (code path is skipped when GPUUploadHeapSupported is true or adapter is not discrete NVIDIA). Live 114-min session was run on 7b1e836 prior to clean rebase onto d91d8e4.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #284, #218
  • Replaces or overlaps: none found
Refs #82 ## Contributor role (optional) - Fluxer ID: kirums#8956 ## What it changes - The problem on main: On NVIDIA Turing cards (such as RTX 2080 Ti) with expanded BAR1 (> 256 MiB, e.g. 16 GiB via ReBAR/VBIOS mod), the D3D12 driver does not advertise `D3D12_FEATURE_DATA_D3D12_OPTIONS16::GPUUploadHeapSupported`, even though NVIDIA exposes CPU-visible VRAM via NVAPI (`NvAPI_D3D12_QueryCpuVisibleVidmem` + `NvAPI_D3D12_CreateHeap2` with `NV_D3D12_HEAP_FLAG_CPUVISIBLE_VIDMEM`). As a result, `Host::mapped_card_buffer` and `heap_twin::make` stay unavailable on Turing and fall back to system RAM. This adds an opt-in switch `DARNER_CVV=1` (off by default, `asked_for!("DARNER_CVV")`, zero behaviour change when unset): - `crates/darner-gpu/src/nvapi.rs`: loads `nvapi64.dll` from `System32` (`LOAD_LIBRARY_SEARCH_SYSTEM32`), queries `NvAPI_D3D12_QueryCpuVisibleVidmem` dynamically per discrete NVIDIA adapter (nothing is hardcoded to 16 GiB), requires BAR1 > 256 MiB, and creates a `D3D12_HEAP_TYPE_CUSTOM` + `WRITE_COMBINE` + `L0` CPU-visible VRAM heap via `NvAPI_D3D12_CreateHeap2`. Using `CUSTOM + WRITE_COMBINE + L0` (instead of `UPLOAD`) both lands in CPU-visible VRAM and permits `D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS` + `COMMON` state needed by `DARNER_WINDOWS_BAR_MB`, `DARNER_HEAP_TWIN`, `DARNER_SCRATCH_CARD`, and `host::bus`. Any `CreateHeap2` failure falls back cleanly to system RAM. - `crates/darner-gpu/src/host.rs` & `heap_twin.rs`: wires the NVAPI CVV fallback into `Host::mapped_card_buffer` and `heap_twin::make`, attaching the `ID3D12Heap` lifetime to the placed `ID3D12Resource` via `SetPrivateDataInterface` and skipping redundant `MakeResident` on the already-resident CVV heap. - `crates/darner-gpu/src/cardledger.rs`, `residency.rs`, `resource.rs`: adds `residency::placed_as(MappedVram)` and `cardledger::made_mapped` so CVV placed buffers are accounted as mapped card VRAM rather than `Shared`/`Other`, and restricts the `DARNER_ALIAS_SHARE=1` upload-heap guard to `GpuUpload`. - `README.md` & `docs/memory-separation.md`: documents `DARNER_CVV`. ## How it was tested - Tested revision: `4c00698` (rebased cleanly onto `origin/main` `d91d8e4`) - Commands and results: - `cargo xtask policy` -> PASS (`566 files ok`) - `cargo clippy --workspace --all-targets --locked -- -D warnings` -> PASS (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` -> PASS (0 warnings) - `cargo test -p darner-gpu --lib --locked nvapi` -> PASS (3/3) - `cargo test -p darner-gpu --lib --locked cardledger` -> PASS (25/25) - `cargo test -p darner-gpu --lib --locked residency` -> PASS (13/13) - `DARNER_CVV=1 cargo test -p darner-gpu --lib --locked mapped_card_memory_is_written_by_the_cpu_and_read_by_the_card` -> PASS (1/1 on live RTX 2080 Ti) - `host::bus` 256 MiB benchmark (measured earlier on the same commit before rebase onto `d91d8e4`, identical code) on RTX 2080 Ti 22 GB with `DARNER_CVV=1`: CPU write `10.4 GB/s`, CPU read `0.05 GB/s`, compute shader read `492.3 GB/s`, UAV store `139.1 GB/s` (matching `DEFAULT` VRAM `495.0 / 141.9 GB/s`, vs system RAM `6.2 / 0.6 GB/s`). - Changes a default: no (opt-in behind `DARNER_CVV=1`, off when unset). - A/B result: Off by default (`DARNER_CVV` unset creates no CVV heap, verified in live boot log on RTX 2080 Ti). Checked with `DARNER_CVV=1` via the `host::bus` 256 MiB transfer benchmark above and a 114-minute live gameplay run. - Gates run on that revision (the three in AGENTS.md; there is no CI): - Gate 1 (`cargo clippy --workspace --all-targets --locked -- -D warnings`): PASS on `4c00698` - Gate 2 (`cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`): PASS on `4c00698` - Gate 3 (`cargo test --workspace --locked`): targeted unit & live GPU tests above passed on `4c00698`; full workspace test run was not completed on this machine because pre-existing unrelated `darner-shader` `oracle64::legacy_mul_forms_are_exact` hangs >20 min in debug mode and `darner-gpu` `watch::tests` has intermittent parallel flakes (related to #262). - Gameplay, when relevant: Intel Core i5-13490F, 31.8 GB RAM, NVIDIA GeForce RTX 2080 Ti 22 GB modded (Turing TU102, 16 GiB BAR1), Windows 11 build 26300, driver 621.1; Series X profile, `1080to1440`, DLSS Performance, 3x DLSS FG (via `dlssg_for_sm86`), RT off. Verified on pre-rebase equivalent commit (`7b1e836`, identical diff) across a 114-minute continuous session (222,000+ frames): `DARNER_HEAP_TWIN=1` placed the 174 MB graphics heap twin in CPU-visible VRAM; `cardledger` tracked `window-mapped` (`4,062 MB` early -> `2,754 MB` at 114 min, `other-mapped 216 -> 169 MB`, `scratch 134 -> 142 MB`); 0 crashes, 0 device losses, 0 `no buffer in card memory`, 0 `no twin`. - Shader wave sizes tested, when relevant: N/A (no shader translator changes). - Hardware dependence: NVIDIA Turing (and other discrete NVIDIA GPUs with > 256 MiB BAR1 lacking D3D12 GPU upload heaps); untested on stock 256 MiB BAR1 Turing cards (guard rejects BAR1 <= 256 MiB), AMD, or Intel. - Coverage gaps: Not tested in-game on AMD/Intel or on a card where `GPUUploadHeapSupported` is already true (code path is skipped when `GPUUploadHeapSupported` is true or adapter is not discrete NVIDIA). Live 114-min session was run on `7b1e836` prior to clean rebase onto `d91d8e4`. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #284, #218 - Replaces or overlaps: none found
Contributor

One diagnostic still identifies every graphics heap twin as GPU_UPLOAD. On 4c00698, heap_twin::make can also create an NVAPI CPU-visible video-memory heap. Both paths return the same HeapTwin placement tag.

I rendered the PR's Rust log format strings with synthetic twin sizes of 17 and 5 MiB. Both produced graphics heap twin (CPU copies to GPU_UPLOAD). The placement-success and placement-refusal messages in port.rs use the same backend name. A CVV test log can therefore look like it used the D3D12 GPU upload heap. This was a CPU-only formatting check.

A shared label such as CPU copies to mapped card memory would describe both paths. Logging the selected backend once at heap creation would let readers distinguish them. Heap selection · Memory summary · Placement messages

One diagnostic still identifies every graphics heap twin as `GPU_UPLOAD`. On `4c00698`, `heap_twin::make` can also create an NVAPI CPU-visible video-memory heap. Both paths return the same `HeapTwin` placement tag. I rendered the PR's Rust log format strings with synthetic twin sizes of 17 and 5 MiB. Both produced `graphics heap twin (CPU copies to GPU_UPLOAD)`. The placement-success and placement-refusal messages in `port.rs` use the same backend name. A CVV test log can therefore look like it used the D3D12 GPU upload heap. This was a CPU-only formatting check. A shared label such as `CPU copies to mapped card memory` would describe both paths. Logging the selected backend once at heap creation would let readers distinguish them. [Heap selection](https://git.gtahub.st/TrustNo1/Darner/src/commit/4c00698a1216adb83b4111af6f7aec45d02f6685/crates/darner-gpu/src/heap_twin.rs#L187) · [Memory summary](https://git.gtahub.st/TrustNo1/Darner/src/commit/4c00698a1216adb83b4111af6f7aec45d02f6685/crates/darner-gpu/src/residency.rs#L810) · [Placement messages](https://git.gtahub.st/TrustNo1/Darner/src/commit/4c00698a1216adb83b4111af6f7aec45d02f6685/crates/darner-gpu/src/port.rs#L165)
Author
First-time contributor

@superhero-520 Good catch, thanks for checking those log strings — addressed in 4b57408:

  • host.rs: added MappedCardKind::label() ("GPU_UPLOAD" / "NVAPI CVV") and reused it in mapped_card_kind().
  • heap_twin.rs: logs way = kind.label() once when creating the graphics heap twin so logs clearly identify which backend backed it.
  • residency.rs & port.rs: changed the shared twin diagnostics from GPU_UPLOAD to mapped card memory (both the memory placement summary and the port 1 placement/refusal messages).

Re-verified on 4b57408:

  • cargo xtask policy --staged (4 files ok)
  • cargo clippy --workspace --all-targets --locked -- -D warnings (pass, 0 warnings)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (pass, 0 warnings)
  • cargo test -p darner-gpu --lib --locked for nvapi (3/3), cardledger (25/25), residency (13/13), heap_twin (9/9), port:: (4/4), and DARNER_CVV=1 mapped_card_memory_is_written_by_the_cpu_and_read_by_the_card (1/1)
  • Live run on RTX 2080 Ti 22 GB with DARNER_CVV=1:
    • the title's graphics heap: a twin in the card's memory, written by the CPU before submissions (DARNER_HEAP_TWIN) start=0xf10000 mb=174 gpu=0x9e00000 way="NVAPI CVV"
    • renderer port 1: buffer placed in the mapped card memory graphics heap twin, updated by CPU copies start=0xf10000 len=182452224 window=false
@superhero-520 Good catch, thanks for checking those log strings — addressed in `4b57408`: - `host.rs`: added `MappedCardKind::label()` (`"GPU_UPLOAD"` / `"NVAPI CVV"`) and reused it in `mapped_card_kind()`. - `heap_twin.rs`: logs `way = kind.label()` once when creating the graphics heap twin so logs clearly identify which backend backed it. - `residency.rs` & `port.rs`: changed the shared twin diagnostics from `GPU_UPLOAD` to `mapped card memory` (both the memory placement summary and the port 1 placement/refusal messages). Re-verified on `4b57408`: - `cargo xtask policy --staged` (`4 files ok`) - `cargo clippy --workspace --all-targets --locked -- -D warnings` (pass, 0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (pass, 0 warnings) - `cargo test -p darner-gpu --lib --locked` for `nvapi` (3/3), `cardledger` (25/25), `residency` (13/13), `heap_twin` (9/9), `port::` (4/4), and `DARNER_CVV=1` `mapped_card_memory_is_written_by_the_cpu_and_read_by_the_card` (1/1) - Live run on RTX 2080 Ti 22 GB with `DARNER_CVV=1`: - `the title's graphics heap: a twin in the card's memory, written by the CPU before submissions (DARNER_HEAP_TWIN) start=0xf10000 mb=174 gpu=0x9e00000 way="NVAPI CVV"` - `renderer port 1: buffer placed in the mapped card memory graphics heap twin, updated by CPU copies start=0xf10000 len=182452224 window=false`
TrustNo1 changed title from [pr-feature][memory][NVIDIA] DARNER_CVV=1: card memory the CPU maps on Turing via NVAPI CPU-visible vidmem to DARNER_CVV=1: card memory the CPU maps on Turing via NVAPI CPU-visible vidmem 2026-12-06 03:28:06 +00:00
Owner

Test needed — commit 1fded26d; what to check:

  • The problem on main: On NVIDIA Turing cards (such as RTX 2080 Ti) with expanded BAR1 (> 256 MiB, e.g. 16 GiB via ReBAR/VBIOS mod), the D3D12 driver does not advertise D3D12_FEATURE_DATA_D3D12_OPTIONS16::GPUUploadHeapSupported, even though NVIDIA exposes CPU-visible VRAM via NVAPI (NvAPI_D3D12_QueryCpuVisibleVidmem + NvAPI_D3D12_CreateHeap2 with NV_D3D12_HEAP_FLAG_CPUVISIBLE_VIDMEM). As a result, Host::mapped_card_buffer and heap_twin::make stay unavailable on Turing and fall back to system RAM.

This adds an opt-in switch DARNER_CVV=1 (off by default, asked_for!("DARNER_CVV"), zero behaviour change when unset):

  • crates/darner-gpu/src/nvapi.rs: loads nvapi64.dll from System32 (LOAD_LIBRARY_SEARCH_SYSTEM32), queries NvAPI_D3D12_QueryCpuVisibleVidmem dynamically per discrete NVIDIA adapter (nothing is hardcoded to 16 GiB), requires BAR1 > 256 MiB, and creates a D3D12_HEAP_TYPE_CUSTOM + WRITE_COMBINE + L0 CPU-visible VRAM heap via NvAPI_D3D12_CreateHeap2. Using CUSTOM + WRITE_COMBINE + L0 (instead of UPLOAD) both lands in CPU-visible VRAM and permits D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS + COMMON state needed by DARNER_WINDOWS_BAR_MB,

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `1fded26d`; what to check: - The problem on main: On NVIDIA Turing cards (such as RTX 2080 Ti) with expanded BAR1 (> 256 MiB, e.g. 16 GiB via ReBAR/VBIOS mod), the D3D12 driver does not advertise `D3D12_FEATURE_DATA_D3D12_OPTIONS16::GPUUploadHeapSupported`, even though NVIDIA exposes CPU-visible VRAM via NVAPI (`NvAPI_D3D12_QueryCpuVisibleVidmem` + `NvAPI_D3D12_CreateHeap2` with `NV_D3D12_HEAP_FLAG_CPUVISIBLE_VIDMEM`). As a result, `Host::mapped_card_buffer` and `heap_twin::make` stay unavailable on Turing and fall back to system RAM. This adds an opt-in switch `DARNER_CVV=1` (off by default, `asked_for!("DARNER_CVV")`, zero behaviour change when unset): - `crates/darner-gpu/src/nvapi.rs`: loads `nvapi64.dll` from `System32` (`LOAD_LIBRARY_SEARCH_SYSTEM32`), queries `NvAPI_D3D12_QueryCpuVisibleVidmem` dynamically per discrete NVIDIA adapter (nothing is hardcoded to 16 GiB), requires BAR1 > 256 MiB, and creates a `D3D12_HEAP_TYPE_CUSTOM` + `WRITE_COMBINE` + `L0` CPU-visible VRAM heap via `NvAPI_D3D12_CreateHeap2`. Using `CUSTOM + WRITE_COMBINE + L0` (instead of `UPLOAD`) both lands in CPU-visible VRAM and permits `D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS` + `COMMON` state needed by `DARNER_WINDOWS_BAR_MB`, Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'nvapi-cpu-visible-vidmem:feng-nvapi-cpu-visible-vidmem'
git switch 'feng-nvapi-cpu-visible-vidmem'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'Main'
git merge --no-ff 'feng-nvapi-cpu-visible-vidmem'
git switch 'feng-nvapi-cpu-visible-vidmem'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'feng-nvapi-cpu-visible-vidmem'
git switch 'feng-nvapi-cpu-visible-vidmem'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'feng-nvapi-cpu-visible-vidmem'
git switch 'Main'
git merge --squash 'feng-nvapi-cpu-visible-vidmem'
git switch 'Main'
git merge --ff-only 'feng-nvapi-cpu-visible-vidmem'
git switch 'Main'
git merge 'feng-nvapi-cpu-visible-vidmem'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
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
TrustNo1/Darner!485
No description provided.