diff --git a/d3d12/README.md b/d3d12/README.md index 5ccdb28686..c5dd2203a1 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -62,6 +62,7 @@ measurements and what is still wrong in the picture are in the [top-level README | With the 60 fps patch the scripts told 60 frames a second, from which the title halves a count it hands them | Always with the 60 fps patch: the function that tells the scripts the frame rate answers 59 (test `the_scripts_are_told_fifty_nine`) | `crates/leonida-loader/src/vipatch.rs` | trustno1-coder/VIctory-NVIDIA PR #65 and kylepelham/El1xr `sim_target.rs` | | No trainer menu | `LEONIDA_MENU` (on; 0 off): F4 (or RB + d-pad Left) opens an in-game menu beside the F1 settings and the F3 panel; arrows or numpad 8/2/4/6 move, Enter or 5 selects, Backspace or 0 goes back; `LEONIDA_MENU_OPEN=1` starts it open. DLSS sharpness, the frame-rate cap and the FPS panel (the renderer's own settings, shared with the F1 menu), screenshots, frame recording and the title's clock rate work; script features (god mode, vehicles, weather, ...) wait for the title's natives. Built and unit-tested, not yet tried in the running title (`docs/mod-menu.md`) | `crates/leonida-menu`, `crates/leonida-gpu/src/host/trainer.rs` | Leonida's own; the classic single-player trainer layout, no code or art from one | | No Lua mods | `LEONIDA_LUA` (on; 0 off, no thread): Lua 5.4 mods in `mods\\` (`mod.json` with `"script": "main.lua"`, `LEONIDA_MODS` names the folder) run in a sandbox on a thread of their own, with the `leonida` API (version 1): frame, key and menu events, an overlay, the live settings (frame cap, DLSS sharpness, panel), pages in the F4 menu under Mods (and Reload Lua mods), keys held for the title, and checked reads, writes and pattern scans of the title's memory; a failing mod is switched off alone. `mods\example_lua` ships disabled. Not the title's own script functions (god mode, vehicles, weather: none can be called). Built and unit-tested, not yet tried in the running title (`docs/lua-modding.md`) | `crates/leonida-lua`, `crates/leonida-menu/src/{overlay,mods}.rs`, `crates/leonida-gpu/src/host/trainer.rs` | Leonida's own; Lua 5.4 through the `mlua` crate (MIT) | +| A pipeline the NVIDIA driver refuses as out of memory | Each pipeline worker has a 64 MB stack (on Windows: reserved address space, committed only as used; under Wine the default stack, since Wine commits the whole of it): the driver compiles pipelines on the worker, and NVIDIA's compiler (`nvgpucomp64.dll`) recursed past the default 2 MB on a world compute program ("thread has overflowed its stack", `0xc00000fd`), after which the pipeline was refused and its dispatches dropped (test `pipeline_workers_reserve_a_64_mb_stack`) | `crates/leonida-gpu/src/pipeline/make.rs` | Peppacodes, edsuuu/VI-BR-PORT `d09c31b4` (the original, seen on an RTX 5080) and `72903b29` ("Port of d09c31b4") | | No frame generation on RTX 20 and 30 cards (DLSS's runs on RTX 40 and 50 only) | `LEONIDA_FSR_FRAMEGEN=1` (off; `-FsrFrameGen`, or FSR 3 FRAME GEN in the F1 menu from the next start, off and on again while it runs) with AMD's signed `amd_fidelityfx_loader_dx12.dll` and `amd_fidelityfx_framegeneration_dx12.dll` in `amd-sdk\FidelityFX\bin` (`LEONIDA_FIDELITYFX_DIR`): the swap chain is FidelityFX's frame interpolation swap chain, given the depth and motion vectors DLSS frame generation is given, and one frame is generated between each two rendered; on Series S the base plane is the colour without the interface. Never with DLSS frame generation or ray tracing | `crates/leonida-gpu/src/fsr.rs`, `crates/leonida-gpu/src/fg.rs`, `scripts/play-nvidia.ps1` | trustno1-coder/VIctory-NVIDIA `29d8dbf` (`crates/victory-gpu/src/streamline/fsr.rs`, GPL-3.0) | diff --git a/d3d12/crates/leonida-gpu/src/pipeline/make.rs b/d3d12/crates/leonida-gpu/src/pipeline/make.rs index 42189790a5..0c81f31264 100644 --- a/d3d12/crates/leonida-gpu/src/pipeline/make.rs +++ b/d3d12/crates/leonida-gpu/src/pipeline/make.rs @@ -699,6 +699,44 @@ fn next(screens_only: bool) -> Arc { } } +/// Stack of each pipeline worker: room for the driver's shader compiler, +/// which runs on it. On Windows this is reserved address space, committed +/// only as used; Wine commits the whole of it (see [`pipeline_stack`]). +const PIPELINE_STACK: usize = 64 << 20; + +/// The stack a pipeline worker asks for, if not the default. None under Wine: +/// it maps a thread's whole stack as committed memory. With 64 MB stacks the +/// idle pool (10 to 18 workers) took 0.6 to 1.1 GiB of Linux commit, with 0.6 +/// to 5 MiB of it resident. +fn pipeline_stack(wine: bool) -> Option { + (!wine).then_some(PIPELINE_STACK) +} + +/// Whether we run under Wine (its ntdll exports `wine_get_version`). +fn on_wine() -> bool { + static WINE: OnceLock = OnceLock::new(); + *WINE.get_or_init(|| { + use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress}; + // SAFETY: plain lookups in ntdll, always loaded. + unsafe { + let ntdll = GetModuleHandleA(c"ntdll.dll".as_ptr().cast()); + !ntdll.is_null() && GetProcAddress(ntdll, c"wine_get_version".as_ptr().cast()).is_some() + } + }) +} + +/// The thread builder of pipeline worker `i`. +fn worker_builder(i: usize) -> std::thread::Builder { + let builder = std::thread::Builder::new().name(format!("leonida-pipelines-{i}")); + // The driver compiles pipelines on this thread, recursively: NVIDIA's + // compiler (nvgpucomp64) overflowed the default 2 MB on a world compute + // program and refused it as out of memory. + match pipeline_stack(on_wine()) { + Some(bytes) => builder.stack_size(bytes), + None => builder, + } +} + /// How many workers make pipelines: `asked` (`LEONIDA_PIPELINE_WORKERS`, 1 to /// 64), else two thirds of the logical processors, 2 to 16. They run below the /// title's own threads, and a cold cache queues hundreds at once. @@ -730,8 +768,7 @@ fn workers() -> bool { let screens = if screens_first() { SCREEN_WORKERS } else { 0 }; for i in 0..workers + screens { let screens_only = i >= workers; - let spawned = std::thread::Builder::new() - .name(format!("leonida-pipelines-{i}")) + let spawned = worker_builder(i) .spawn(move || { // Below the title's own threads (loading, recording), // and the streaming one, which is below normal itself @@ -790,6 +827,36 @@ fn drained() { mod tests { use super::*; + /// The driver's compiler runs on the worker and recursed past the default + /// 2 MB on NVIDIA: a worker must reserve the whole of `PIPELINE_STACK`. + #[test] + fn pipeline_workers_reserve_a_64_mb_stack() { + use windows::Win32::System::Threading::GetCurrentThreadStackLimits; + let reserved = worker_builder(0) + .spawn(|| { + let (mut low, mut high) = (0usize, 0usize); + // SAFETY: both pointers are to locals that live through the call. + unsafe { GetCurrentThreadStackLimits(&mut low, &mut high) }; + high - low + }) + .expect("a worker thread") + .join() + .expect("the stack limits"); + if on_wine() { + assert!(reserved < PIPELINE_STACK, "stack of {reserved} bytes under Wine"); + return; + } + assert!(reserved >= PIPELINE_STACK, "stack of {reserved} bytes"); + assert_eq!(PIPELINE_STACK, 64 << 20); + } + + /// Wine commits a whole stack, so a worker there keeps the default one. + #[test] + fn wine_workers_keep_the_default_stack() { + assert_eq!(pipeline_stack(true), None); + assert_eq!(pipeline_stack(false), Some(PIPELINE_STACK)); + } + #[test] fn older_frames_cannot_reopen_a_pipeline_wait_budget() { let budget = Duration::from_millis(500);