From aab5cc1eb5569a897880033dd45f151dc9094047 Mon Sep 17 00:00:00 2001 From: MoonEyes <14891700+MoonEyes@users.noreply.github.com> Date: Sat, 28 Nov 2026 20:38:19 +0100 Subject: [PATCH 1/2] GPU: pipeline workers get a 64 MB stack for the driver's compiler Item: PORT-BR-PIPELINE-WORKER-STACK (first half of PORT-BR-PIPELINE-WORKER-STACK-AND-WAIT). The host driver compiles pipelines on our worker threads. On an RTX 5080 NVIDIA's compiler (nvgpucomp64.dll) recursed past the default 2 MB stack while creating a world compute pipeline: the thread overflowed its stack (0xc00000fd), then the pipeline was refused with E_OUTOFMEMORY and its dispatches dropped. A 64 MB stack is reserved address space, committed only as the compiler uses it. Ported from edsuuu/VI-BR-PORT d09c31b4 (Peppacodes, the original) and 72903b29 (edsuuu, "Port of d09c31b4"), GPL-3.0. Not a cherry-pick: the file is d3d12/crates/leonida-gpu/src/pipeline/make.rs here and the worker spawn is shared through worker_builder() so a test can check it. Test pipeline_workers_reserve_a_64_mb_stack reads the worker thread's stack limits (GetCurrentThreadStackLimits): 2097152 bytes without stack_size (fails), at least 64 MB with it (passes). No game file needed. Co-authored-by: Peppacodes <360593343+Peppacodes@users.noreply.github.com> Co-authored-by: edsuuu <111332935+edsuuu@users.noreply.github.com> Co-Authored-By: Claude Sonnet 5.5 --- d3d12/README.md | 1 + d3d12/crates/leonida-gpu/src/pipeline/make.rs | 36 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/d3d12/README.md b/d3d12/README.md index e4886a5bec..f00f15499c 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -21,6 +21,7 @@ project name changed where it named the program. | White glitter on floors, glowing windows and eyes | `LEONIDA_REFLECT_MOST` (off unless set since the renderer merged at `00698e1e8`, where it is unproven; 3 was the default before. It is not what posterizes some runs: those come out so with it off too): after `CS_UpdateHistory`, reflections are capped at a multiple of the room's light and colours far from grey pulled towards it; without rays they hold the sky of every miss | `crates/leonida-gpu/src/ambient.rs` | Leonida's own pass; the program and its view from lepotane/VIctory-leonida | | 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 (reserved address space, committed only as used): 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") | ## Shaders and pipelines are made before the title starts diff --git a/d3d12/crates/leonida-gpu/src/pipeline/make.rs b/d3d12/crates/leonida-gpu/src/pipeline/make.rs index b5626b09a7..7796bcfd41 100644 --- a/d3d12/crates/leonida-gpu/src/pipeline/make.rs +++ b/d3d12/crates/leonida-gpu/src/pipeline/make.rs @@ -648,6 +648,20 @@ fn next(screens_only: bool) -> Arc { } } +/// Stack of each pipeline worker: room for the driver's shader compiler, +/// which runs on it (reserved address space; committed only as used). +const PIPELINE_STACK: usize = 64 << 20; + +/// The thread builder of pipeline worker `i`. +fn worker_builder(i: usize) -> std::thread::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. + .stack_size(PIPELINE_STACK) +} + /// 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. @@ -679,8 +693,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 @@ -739,6 +752,25 @@ 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"); + assert!(reserved >= PIPELINE_STACK, "stack of {reserved} bytes"); + assert_eq!(PIPELINE_STACK, 64 << 20); + } + #[test] fn worker_count_follows_the_processors_and_the_switch() { assert_eq!(worker_count(None, 1), 2); From 6b4a44e1821fed7945f9b433bb01fa3bf599ac87 Mon Sep 17 00:00:00 2001 From: MoonEyes <14891700+MoonEyes@users.noreply.github.com> Date: Tue, 1 Dec 2026 08:16:14 +0100 Subject: [PATCH 2/2] gpu: pipeline workers keep the default stack under Wine, which commits the whole of it Found by timbot103 and measured by merzleckeier on #2588. Co-Authored-By: Claude Opus 5.5 --- d3d12/README.md | 2 +- d3d12/crates/leonida-gpu/src/pipeline/make.rs | 49 ++++++++++++++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/d3d12/README.md b/d3d12/README.md index 0261af4620..c5dd2203a1 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -62,7 +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 (reserved address space, committed only as used): 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") | +| 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 fa0b8a1a18..0c81f31264 100644 --- a/d3d12/crates/leonida-gpu/src/pipeline/make.rs +++ b/d3d12/crates/leonida-gpu/src/pipeline/make.rs @@ -700,17 +700,41 @@ fn next(screens_only: bool) -> Arc { } /// Stack of each pipeline worker: room for the driver's shader compiler, -/// which runs on it (reserved address space; committed only as used). +/// 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 { - 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. - .stack_size(PIPELINE_STACK) + 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 @@ -818,10 +842,21 @@ mod tests { .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);