From 19aec59ba2931333e4218fe16ab7851b110122bd Mon Sep 17 00:00:00 2001 From: kykifox1337-source Date: Mon, 23 Nov 2026 20:05:37 -0500 Subject: [PATCH] Windows 10: load the D3D12 Agility SDK runtime for shader model 6.6 Windows 10's in-box D3D12 stops at shader model 6.5 and refuses root signatures with the directly indexed heap flags (c01), so every translated pipeline was refused and nothing drew (#84). victory.exe now exports D3D12SDKVersion (620) and D3D12SDKPath (.\D3D12\), and `cargo xtask agility` fetches Microsoft.Direct3D.D3D12 1.620.2 from nuget.org, checks its published SHA-512, and puts D3D12Core.dll and d3d12SDKLayers.dll in target\\D3D12\. The DLLs never enter the repository. Windows 11, or a missing folder, keeps the in-box runtime. `victory config` reports whether the runtime is in place, and the SM 6.5 error names the command. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 75 ++++++++- README.md | 7 + apps/victory/build.rs | 8 + apps/victory/src/agility.rs | 34 ++++ apps/victory/src/main.rs | 11 ++ crates/victory-shader/src/emit/host.rs | 3 +- xtask/Cargo.toml | 5 +- xtask/src/agility.rs | 218 +++++++++++++++++++++++++ xtask/src/main.rs | 16 ++ 9 files changed, 374 insertions(+), 3 deletions(-) create mode 100644 apps/victory/build.rs create mode 100644 apps/victory/src/agility.rs create mode 100644 xtask/src/agility.rs diff --git a/Cargo.lock b/Cargo.lock index bab4a7e1a6..f3bc39a88a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,7 +95,16 @@ dependencies = [ "cc", "cfg-if", "constant_time_eq", - "cpufeatures", + "cpufeatures 0.3.1", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", ] [[package]] @@ -166,6 +175,15 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d52eff69cd5e647efe296129160853a42795992097e8af39800e1060caeea9b" +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "cpufeatures" version = "0.3.1" @@ -175,6 +193,26 @@ dependencies = [ "libc", ] +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "equivalent" version = "1.0.3" @@ -203,6 +241,16 @@ version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.4.3" @@ -434,6 +482,17 @@ dependencies = [ "serde_core", ] +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -625,6 +684,12 @@ dependencies = [ "tracing-log", ] +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + [[package]] name = "unicode-ident" version = "1.0.27" @@ -643,6 +708,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "victory" version = "0.1.0" @@ -910,5 +981,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "miniz_oxide", + "sha2", "walkdir", ] diff --git a/README.md b/README.md index 36b77e48b7..a718ee4c06 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,12 @@ Requirements: Windows 10/11 x64, a CPU with AVX2, a Direct3D 12 GPU with shader model 6.6 (and mesh shaders for the world), Rust (pinned in `rust-toolchain.toml`). +On Windows 10 the in-box D3D12 runtime stops at shader model 6.5, so the host +refuses every translated pipeline. `cargo xtask agility` fetches Microsoft's +D3D12 Agility SDK runtime (a pinned, hash-checked NuGet package) into +`target\\D3D12\`, where `victory.exe` makes D3D12 load it. Windows 11 +needs nothing. + The title also needs several GiB of available Windows commit at startup. Check **Committed** under **Task Manager → Performance → Memory**. If `crashes.log` reports Windows error 1455, close other applications or increase @@ -58,6 +64,7 @@ the Windows paging file size, then retry. ```powershell cargo xtask install-hooks # asset-policy check on every commit copy victory.example.toml victory.toml # then set your paths (outside the repo) +cargo xtask agility # Windows 10 only: newer D3D12 runtime cargo test --workspace # needs no game data cargo run -p victory -- config # check your paths ``` diff --git a/apps/victory/build.rs b/apps/victory/build.rs new file mode 100644 index 0000000000..912240c58c --- /dev/null +++ b/apps/victory/build.rs @@ -0,0 +1,8 @@ +fn main() { + // The D3D12 Agility SDK exports (src/agility.rs): D3D12 finds them on + // the executable. Only the MSVC linker takes /EXPORT on a binary. + if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") { + println!("cargo:rustc-link-arg-bins=/EXPORT:D3D12SDKVersion,DATA"); + println!("cargo:rustc-link-arg-bins=/EXPORT:D3D12SDKPath,DATA"); + } +} diff --git a/apps/victory/src/agility.rs b/apps/victory/src/agility.rs new file mode 100644 index 0000000000..efd58b7355 --- /dev/null +++ b/apps/victory/src/agility.rs @@ -0,0 +1,34 @@ +//! The D3D12 Agility SDK: which D3D12 runtime the host uses. +//! +//! Windows 10's in-box runtime stops at shader model 6.5 and refuses root +//! signatures that index the descriptor heaps directly (flags 0x400, 0x800), +//! which every translated pipeline uses. D3D12 looks for these two exports on +//! the process's executable; when they name a runtime newer than the +//! system's, it loads `D3D12Core.dll` from `D3D12SDKPath` (relative to the +//! executable) instead. `cargo xtask agility` puts it there. Without the +//! folder, or on a system whose runtime is newer, the in-box one is used. +//! +//! `build.rs` exports both symbols. +#![allow(non_upper_case_globals)] + +/// The runtime's SDK version: the minor number of the +/// `Microsoft.Direct3D.D3D12` package `cargo xtask agility` fetches +/// (1.620.2). Keep the two in step. +#[unsafe(no_mangle)] +pub static D3D12SDKVersion: u32 = 620; + +/// A C string pointer D3D12 reads once at device creation. +#[repr(transparent)] +pub struct SdkPath(*const std::ffi::c_char); + +// SAFETY: points at a static, immutable C string. +unsafe impl Sync for SdkPath {} + +/// Where `D3D12Core.dll` is, relative to the executable. +#[unsafe(no_mangle)] +pub static D3D12SDKPath: SdkPath = SdkPath(c".\\D3D12\\".as_ptr()); + +/// The folder `D3D12SDKPath` names, beside the running executable. +pub fn runtime_dir() -> Option { + Some(std::env::current_exe().ok()?.parent()?.join("D3D12")) +} diff --git a/apps/victory/src/main.rs b/apps/victory/src/main.rs index 31392ffb08..9ebdee0de8 100644 --- a/apps/victory/src/main.rs +++ b/apps/victory/src/main.rs @@ -3,6 +3,7 @@ //! Every command reads the user's own files and writes only to stdout or to //! the user's cache directory (outside the repository). +mod agility; mod census; mod inspect; mod run; @@ -180,6 +181,16 @@ fn show_config() -> Result<()> { ); println!("game_dir : {}", status(&cfg.game_dir)); println!("cache_dir : {}", cfg.cache_dir.display()); + match agility::runtime_dir().map(|d| d.join("D3D12Core.dll")) { + Some(dll) if dll.exists() => println!( + "d3d12 sdk : {} (version {})", + dll.display(), + agility::D3D12SDKVersion + ), + _ => println!( + "d3d12 sdk : not beside the executable (needed on Windows 10: `cargo xtask agility`)" + ), + } for t in &cfg.test_titles { println!("test_title : {}", status(&Some(t.clone()))); } diff --git a/crates/victory-shader/src/emit/host.rs b/crates/victory-shader/src/emit/host.rs index dabd4a4dc5..008fee2aa0 100644 --- a/crates/victory-shader/src/emit/host.rs +++ b/crates/victory-shader/src/emit/host.rs @@ -90,7 +90,8 @@ impl Host { .map_err(err)?; if sm.HighestShaderModel.0 < D3D_SHADER_MODEL_6_6.0 { return Err(format!( - "the device supports SM {:#x} only", + "the device supports SM {:#x} only (on Windows 10, \ + `cargo xtask agility` puts a newer D3D12 runtime beside victory.exe)", sm.HighestShaderModel.0 )); } diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index d8e74bfea8..65328ae201 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xtask" -description = "Repository maintenance: asset-policy enforcement and git hooks." +description = "Repository maintenance: asset-policy enforcement, git hooks, the D3D12 Agility SDK runtime." version.workspace = true edition.workspace = true rust-version.workspace = true @@ -11,6 +11,9 @@ publish.workspace = true anyhow.workspace = true clap.workspace = true walkdir.workspace = true +miniz_oxide.workspace = true +# SHA-512 of the Agility SDK package (`cargo xtask agility`). +sha2 = "0.10" [lints] workspace = true diff --git a/xtask/src/agility.rs b/xtask/src/agility.rs new file mode 100644 index 0000000000..4288ddb044 --- /dev/null +++ b/xtask/src/agility.rs @@ -0,0 +1,218 @@ +//! `cargo xtask agility`: the D3D12 Agility SDK runtime beside victory.exe. +//! +//! Windows 10's in-box D3D12 stops at shader model 6.5, below what every +//! translated pipeline needs (issue #84). victory.exe asks D3D12 for a newer +//! runtime (`apps/victory/src/agility.rs`); this puts Microsoft's +//! `D3D12Core.dll`, and the debug layer's `d3d12SDKLayers.dll`, where it +//! looks: `//D3D12/`. +//! +//! The DLLs are Microsoft's, so they never enter the repository (asset +//! policy): they are fetched from nuget.org, at a pinned version whose +//! SHA-512 nuget.org publishes, and kept under the target folder. + +use std::path::Path; +use std::process::Command; + +use anyhow::{Context, Result, bail, ensure}; +use sha2::{Digest, Sha512}; + +/// The package version. Its minor number is `D3D12SDKVersion` in +/// `apps/victory/src/agility.rs`: keep the two in step. +const VERSION: &str = "1.620.2"; +/// The package's SHA-512, as nuget.org's catalog gives it (base64 +/// `1oP/f1cm…MnNQ==`). +const SHA512: &str = "d683ff7f5726906c3f43d299f6cdd4b1cad22f8329ccd27b3492d0a1413d9904\ + 102b8d543419bec3b3545c86bf49f6a500ebf42d95c999aa0969e5d693032735"; +/// What we take from the package. +const FILES: &[&str] = &[ + "build/native/bin/x64/D3D12Core.dll", + "build/native/bin/x64/d3d12SDKLayers.dll", +]; +/// The profiles victory.exe is built with (README). +const PROFILES: &[&str] = &["debug", "release", "diag"]; + +pub fn run(target: &Path) -> Result<()> { + let package = fetch(target)?; + let files = FILES + .iter() + .map(|name| Ok((*name, extract(&package, name)?))) + .collect::>>()?; + for profile in PROFILES { + let dir = target.join(profile).join("D3D12"); + std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?; + for (name, bytes) in &files { + let file = dir.join(name.rsplit('/').next().unwrap_or(name)); + std::fs::write(&file, bytes).with_context(|| format!("writing {}", file.display()))?; + } + println!("D3D12 Agility SDK {VERSION}: {}", dir.display()); + } + Ok(()) +} + +/// The package's bytes, downloaded once into `/agility/`. +fn fetch(target: &Path) -> Result> { + let dir = target.join("agility"); + let file = dir.join(format!("microsoft.direct3d.d3d12.{VERSION}.nupkg")); + if let Ok(bytes) = std::fs::read(&file) + && hex(&Sha512::digest(&bytes)) == SHA512 + { + return Ok(bytes); + } + std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?; + let url = format!( + "https://api.nuget.org/v3-flatcontainer/microsoft.direct3d.d3d12/{VERSION}/\ + microsoft.direct3d.d3d12.{VERSION}.nupkg" + ); + let partial = file.with_extension("partial"); + println!("downloading {url}"); + // curl ships with Windows 10 (1803) and later. + let status = Command::new("curl") + .args(["--fail", "--location", "--silent", "--show-error"]) + .args(["--retry", "3", "--output"]) + .arg(&partial) + .arg(&url) + .status() + .context("running curl")?; + ensure!(status.success(), "curl could not download {url}"); + let bytes = std::fs::read(&partial)?; + let got = hex(&Sha512::digest(&bytes)); + if got != SHA512 { + let _ = std::fs::remove_file(&partial); + bail!("{url}: SHA-512 is {got}, expected {SHA512}"); + } + std::fs::rename(&partial, &file)?; + Ok(bytes) +} + +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|b| format!("{b:02x}")).collect() +} + +fn u16_at(b: &[u8], at: usize) -> Result { + let s = b.get(at..at + 2).context("truncated zip")?; + Ok(u16::from_le_bytes([s[0], s[1]]).into()) +} + +fn u32_at(b: &[u8], at: usize) -> Result { + let s = b.get(at..at + 4).context("truncated zip")?; + Ok(u32::from_le_bytes([s[0], s[1], s[2], s[3]]) as usize) +} + +/// One file of a zip (a .nupkg is one): found through the central +/// directory, stored or deflated. No zip64; this package is 35 MB. +fn extract(zip: &[u8], name: &str) -> Result> { + const END: u32 = 0x0605_4b50; + const CENTRAL: usize = 0x0201_4b50; + const LOCAL: usize = 0x0403_4b50; + let end = (0..zip.len().saturating_sub(21)) + .rev() + .take(0x1_0000 + 22) + .find(|&at| zip[at..].starts_with(&END.to_le_bytes())) + .context("no zip end record")?; + let entries = u16_at(zip, end + 10)?; + let mut at = u32_at(zip, end + 16)?; + for _ in 0..entries { + ensure!(u32_at(zip, at)? == CENTRAL, "bad zip central directory"); + let method = u16_at(zip, at + 10)?; + let packed = u32_at(zip, at + 20)?; + let size = u32_at(zip, at + 24)?; + let name_len = u16_at(zip, at + 28)?; + let skip = name_len + u16_at(zip, at + 30)? + u16_at(zip, at + 32)?; + let local = u32_at(zip, at + 42)?; + let this = zip + .get(at + 46..at + 46 + name_len) + .context("truncated zip")?; + at += 46 + skip; + if this != name.as_bytes() { + continue; + } + ensure!(u32_at(zip, local)? == LOCAL, "bad zip local header"); + let data = local + 30 + u16_at(zip, local + 26)? + u16_at(zip, local + 28)?; + let data = zip.get(data..data + packed).context("truncated zip")?; + let out = match method { + 0 => data.to_vec(), + 8 => miniz_oxide::inflate::decompress_to_vec(data) + .map_err(|e| anyhow::anyhow!("{name}: inflate failed: {e:?}"))?, + m => bail!("{name}: zip method {m} is not supported"), + }; + ensure!( + out.len() == size, + "{name}: {} bytes, expected {size}", + out.len() + ); + return Ok(out); + } + bail!("{name} is not in the package") +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A zip with one stored and one deflated file, written here. + fn zip(files: &[(&str, &[u8], bool)]) -> Vec { + let (mut out, mut central) = (Vec::new(), Vec::new()); + for (name, data, deflate) in files { + let packed = if *deflate { + miniz_oxide::deflate::compress_to_vec(data, 6) + } else { + data.to_vec() + }; + let method: u16 = if *deflate { 8 } else { 0 }; + let local = out.len() as u32; + out.extend(0x0403_4b50_u32.to_le_bytes()); + out.extend([0; 4]); + out.extend(method.to_le_bytes()); + out.extend([0; 8]); + out.extend((packed.len() as u32).to_le_bytes()); + out.extend((data.len() as u32).to_le_bytes()); + out.extend((name.len() as u16).to_le_bytes()); + out.extend([0; 2]); + out.extend(name.as_bytes()); + out.extend(&packed); + central.extend(0x0201_4b50_u32.to_le_bytes()); + central.extend([0; 6]); + central.extend(method.to_le_bytes()); + central.extend([0; 8]); + central.extend((packed.len() as u32).to_le_bytes()); + central.extend((data.len() as u32).to_le_bytes()); + central.extend((name.len() as u16).to_le_bytes()); + central.extend([0; 12]); + central.extend(local.to_le_bytes()); + central.extend(name.as_bytes()); + } + let start = out.len() as u32; + let count = (files.len() as u16).to_le_bytes(); + out.extend(¢ral); + out.extend(0x0605_4b50_u32.to_le_bytes()); + out.extend([0; 4]); + out.extend(count); + out.extend(count); + out.extend((central.len() as u32).to_le_bytes()); + out.extend(start.to_le_bytes()); + out.extend([0; 2]); + out + } + + #[test] + fn extracts_stored_and_deflated_files() { + let text = b"synthetic data, repeated, repeated, repeated".repeat(20); + let z = zip(&[ + ("a/stored.txt", b"plain", false), + ("b/packed.txt", &text, true), + ]); + assert_eq!(extract(&z, "a/stored.txt").unwrap(), b"plain"); + assert_eq!(extract(&z, "b/packed.txt").unwrap(), text); + assert!(extract(&z, "missing").is_err()); + } + + #[test] + fn version_matches_the_sdk_version_victory_exports() { + let source = include_str!("../../apps/victory/src/agility.rs"); + let minor = VERSION.split('.').nth(1).unwrap(); + assert!( + source.contains(&format!("D3D12SDKVersion: u32 = {minor};")), + "apps/victory/src/agility.rs must export D3D12SDKVersion = {minor}" + ); + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 911ed8087c..22bc460329 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,5 +1,6 @@ //! Repository maintenance. Run with `cargo xtask `. +mod agility; mod policy; use std::path::{Path, PathBuf}; @@ -26,6 +27,15 @@ enum Cmd { }, /// Point git at .githooks/ so every commit runs the policy check. InstallHooks, + /// Fetch the D3D12 Agility SDK runtime (Microsoft's NuGet package, + /// pinned and hash-checked) into `//D3D12/` beside + /// victory.exe. Needed on Windows 10, whose in-box D3D12 stops at shader + /// model 6.5. + Agility { + /// Cargo's target folder. Default: `CARGO_TARGET_DIR`, else `target`. + #[arg(long)] + target_dir: Option, + }, } fn main() -> Result<()> { @@ -45,6 +55,12 @@ fn main() -> Result<()> { println!("hooks installed: commits now run `cargo xtask policy --staged`"); Ok(()) } + Cmd::Agility { target_dir } => { + let target = target_dir + .or_else(|| std::env::var_os("CARGO_TARGET_DIR").map(PathBuf::from)) + .unwrap_or_else(|| root.join("target")); + agility::run(&target) + } } }