Crash fixes: upload room reserved at the start, D3D12 folder of any version, VICTORY_HOME
- upload.rs: 32 pieces of 64 MiB at the top of [4 GiB, 2^37) are reserved first thing in main, before the process's allocations can fill that window. On players' PCs it was full by the first ray-traced frame, the first upload failed and the title threw "D3D12 XDXR Error: Failed to allocate upload buffer". Past the pool: the window as before, a failed search not repeated for a second (with PR #88's per-frame limit in memory.rs), then the heap instead of null. - main.rs: D3D12SDKVersion follows the version of D3D12\D3D12Core.dll beside the exe (a repack's 1.618 or 1.620 gave 0x887E0003 against our 614). - VICTORY_HOME: one folder for settings, saves, caches, logs and downloads (a full C: drive, issue 135). - README: both. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
c5f7528bbf
commit
e69e39c55b
7 files changed
+242
-62
No files matched your search
@@ -61,7 +61,15 @@
|
|||||||
SDK, `D3D12Core.dll`, about 22 MB from nuget.org). It goes into a `D3D12`
|
SDK, `D3D12Core.dll`, about 22 MB from nuget.org). It goes into a `D3D12`
|
||||||
folder beside `victory.exe`. `victory.exe get-d3d12` does the same by hand.
|
folder beside `victory.exe`. `victory.exe get-d3d12` does the same by hand.
|
||||||
The file is checked against Microsoft's signature and the tested SHA-256.
|
The file is checked against Microsoft's signature and the tested SHA-256.
|
||||||
Windows 11 keeps using its own runtime.
|
Windows 11 keeps using its own runtime. A `D3D12` folder that is already
|
||||||
|
there, for example from a repack or another guide (1.618, 1.620), is used
|
||||||
|
at the version of its `D3D12Core.dll`. Before v2026.11.27.4 that version
|
||||||
|
had to be 1.614, or Windows refused Direct3D 12 with `0x887E0003`.
|
||||||
|
- C: drive full? Set `VICTORY_HOME` to another folder, once, in a command
|
||||||
|
prompt: `setx VICTORY_HOME D:\victory`. Then start victory.exe from a new
|
||||||
|
window. Settings, saves, caches, logs and the downloads all go there
|
||||||
|
instead of `%LOCALAPPDATA%\victory`. To keep what you have, move that
|
||||||
|
folder's contents there first.
|
||||||
- You need Windows 10 or 11, 32 GB of RAM or more, an SSD and an NVIDIA
|
- You need Windows 10 or 11, 32 GB of RAM or more, an SSD and an NVIDIA
|
||||||
GeForce RTX card with a recent driver. This repository and its releases
|
GeForce RTX card with a recent driver. This repository and its releases
|
||||||
contain no game files.
|
contain no game files.
|
||||||
|
|||||||
@@ -26,11 +26,48 @@ use clap::{Parser, Subcommand, ValueEnum};
|
|||||||
/// older: Windows 10's stops at shader model 6.5, and the translated shaders
|
/// older: Windows 10's stops at shader model 6.5, and the translated shaders
|
||||||
/// are 6.6 (`victory play` fetches it there; 2026-11-27, an RTX 3060 on
|
/// are 6.6 (`victory play` fetches it there; 2026-11-27, an RTX 3060 on
|
||||||
/// Windows 10 22H2 had every pipeline refused). Without the folder, nothing changes.
|
/// Windows 10 22H2 had every pipeline refused). Without the folder, nothing changes.
|
||||||
|
///
|
||||||
|
/// A `D3D12Core.dll` there of another version (other forks' guides put 1.618
|
||||||
|
/// or 1.620 there) made Windows refuse Direct3D 12 altogether, "the D3D12 SDK
|
||||||
|
/// version configuration of the host exe is invalid" (0x887E0003; 2026-11-27,
|
||||||
|
/// an RTX 5080 and an RTX 3080 Ti on Windows 11): [`match_d3d12_core`] asks
|
||||||
|
/// for the version of the file that is there, before Direct3D 12 is loaded.
|
||||||
|
/// (An atomic: the same bytes as a `u32`, in memory that can be written.)
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
#[used]
|
#[used]
|
||||||
#[allow(non_upper_case_globals)]
|
#[allow(non_upper_case_globals)]
|
||||||
pub static D3D12SDKVersion: u32 = 614;
|
pub static D3D12SDKVersion: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(614);
|
||||||
|
|
||||||
|
/// The Agility SDK version of a `D3D12Core.dll` (its file version is
|
||||||
|
/// `1.<sdk>.<patch>.0`, e.g. 1.614.1.0): from the file's VS_FIXEDFILEINFO.
|
||||||
|
fn agility_version(dll: &[u8]) -> Option<u32> {
|
||||||
|
let signature = 0xFEEF_04BD_u32.to_le_bytes();
|
||||||
|
let at = dll.windows(4).position(|w| w == signature)?;
|
||||||
|
let most = u32::from_le_bytes(dll.get(at + 8..at + 12)?.try_into().ok()?);
|
||||||
|
let (major, minor) = (most >> 16, most & 0xffff);
|
||||||
|
(major == 1 && minor >= 600).then_some(minor)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Asks Windows for the Agility SDK version of the `D3D12\D3D12Core.dll`
|
||||||
|
/// beside this program, where there is one and it is not 614. First thing in
|
||||||
|
/// `main`: Windows reads `D3D12SDKVersion` when Direct3D 12 is first used.
|
||||||
|
#[cfg(windows)]
|
||||||
|
fn match_d3d12_core() {
|
||||||
|
let Some(core) = std::env::current_exe().ok().and_then(|p| p.parent().map(|d| d.join("D3D12").join("D3D12Core.dll")))
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Ok(bytes) = std::fs::read(&core) else { return };
|
||||||
|
match agility_version(&bytes) {
|
||||||
|
Some(v) if v != D3D12SDKVersion.load(std::sync::atomic::Ordering::Relaxed) => {
|
||||||
|
D3D12SDKVersion.store(v, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
println!("{} is the D3D12 Agility SDK 1.{v}: asking Windows for that version.", core.display());
|
||||||
|
}
|
||||||
|
Some(_) => {}
|
||||||
|
None => println!("{} is not a D3D12 Agility SDK runtime Windows can use; move it away if Direct3D 12 fails.", core.display()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// `D3D12SDKPath`'s value: a pointer to a C string.
|
/// `D3D12SDKPath`'s value: a pointer to a C string.
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
@@ -258,6 +295,11 @@ enum Console {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
#[cfg(windows)]
|
||||||
|
match_d3d12_core();
|
||||||
|
// Before anything else can take the address space it needs.
|
||||||
|
#[cfg(windows)]
|
||||||
|
victory_hle::reserve_upload_space();
|
||||||
// The audio recorder `run` starts for `VICTORY_AUDIO_CAPTURE`.
|
// The audio recorder `run` starts for `VICTORY_AUDIO_CAPTURE`.
|
||||||
#[cfg(all(windows, feature = "diagnostics"))]
|
#[cfg(all(windows, feature = "diagnostics"))]
|
||||||
if std::env::var_os("VICTORY_AUDIO_CAPTURE").is_some() {
|
if std::env::var_os("VICTORY_AUDIO_CAPTURE").is_some() {
|
||||||
@@ -424,6 +466,30 @@ fn show_config() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
/// A D3D12Core.dll's VS_FIXEDFILEINFO: signature, structure version, then
|
||||||
|
/// the file version's high and low words (1.620.2.0 here).
|
||||||
|
fn core_bytes(major: u32, minor: u32) -> Vec<u8> {
|
||||||
|
let mut b = vec![0u8; 40];
|
||||||
|
for w in [0xFEEF_04BD_u32, 0x0001_0000, (major << 16) | minor, 2 << 16] {
|
||||||
|
b.extend_from_slice(&w.to_le_bytes());
|
||||||
|
}
|
||||||
|
b.extend_from_slice(&[0u8; 16]);
|
||||||
|
b
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn agility_versions_are_read_from_the_file() {
|
||||||
|
assert_eq!(super::agility_version(&core_bytes(1, 620)), Some(620));
|
||||||
|
assert_eq!(super::agility_version(&core_bytes(1, 614)), Some(614));
|
||||||
|
// Windows' own D3D12Core.dll (10.0.x) and files without the structure are not one.
|
||||||
|
assert_eq!(super::agility_version(&core_bytes(10, 0)), None);
|
||||||
|
assert_eq!(super::agility_version(&[0u8; 64]), None);
|
||||||
|
assert_eq!(super::agility_version(&core_bytes(1, 620)[..48]), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Paths given on the command line, or the configured game + test titles.
|
/// Paths given on the command line, or the configured game + test titles.
|
||||||
pub(crate) fn default_paths(paths: Vec<PathBuf>) -> Result<Vec<PathBuf>> {
|
pub(crate) fn default_paths(paths: Vec<PathBuf>) -> Result<Vec<PathBuf>> {
|
||||||
if !paths.is_empty() {
|
if !paths.is_empty() {
|
||||||
|
|||||||
@@ -195,8 +195,11 @@ pub(crate) fn preload(exe: &Path) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `%LOCALAPPDATA%\victory`.
|
/// `%LOCALAPPDATA%\victory`, or the folder `VICTORY_HOME` names.
|
||||||
fn home() -> Result<PathBuf> {
|
fn home() -> Result<PathBuf> {
|
||||||
|
if let Some(home) = victory_core::config::home_override() {
|
||||||
|
return Ok(home);
|
||||||
|
}
|
||||||
let base = std::env::var_os("LOCALAPPDATA").context("no %LOCALAPPDATA% to keep settings and logs in")?;
|
let base = std::env::var_os("LOCALAPPDATA").context("no %LOCALAPPDATA% to keep settings and logs in")?;
|
||||||
Ok(PathBuf::from(base).join("victory"))
|
Ok(PathBuf::from(base).join("victory"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,8 +134,19 @@ fn subdir(root: &Path, sub: &str) -> Result<PathBuf> {
|
|||||||
Ok(dir)
|
Ok(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `%LOCALAPPDATA%/victory/<name>` (elsewhere the XDG or home cache).
|
/// The folder for everything of ours (settings, logs, caches, saves,
|
||||||
|
/// downloads) when `VICTORY_HOME` names one: for a full C: drive, e.g.
|
||||||
|
/// `setx VICTORY_HOME D:\victory`. Else `%LOCALAPPDATA%\victory`.
|
||||||
|
pub fn home_override() -> Option<PathBuf> {
|
||||||
|
std::env::var_os("VICTORY_HOME").filter(|v| !v.is_empty()).map(PathBuf::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `%LOCALAPPDATA%/victory/<name>` (elsewhere the XDG or home cache), or
|
||||||
|
/// `VICTORY_HOME/<name>`.
|
||||||
fn default_dir(name: &str) -> Result<PathBuf> {
|
fn default_dir(name: &str) -> Result<PathBuf> {
|
||||||
|
if let Some(home) = home_override() {
|
||||||
|
return Ok(home.join(name));
|
||||||
|
}
|
||||||
let base = std::env::var_os("LOCALAPPDATA")
|
let base = std::env::var_os("LOCALAPPDATA")
|
||||||
.or_else(|| std::env::var_os("XDG_CACHE_HOME"))
|
.or_else(|| std::env::var_os("XDG_CACHE_HOME"))
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
|
|||||||
@@ -107,6 +107,10 @@ mod texback;
|
|||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
pub use host::on_failure;
|
pub use host::on_failure;
|
||||||
|
/// Reserves the address space for memory shaders read by address; call it
|
||||||
|
/// first thing, before the process's other allocations can fill [4 GiB, 2^37).
|
||||||
|
#[cfg(windows)]
|
||||||
|
pub use upload::reserve_space as reserve_upload_space;
|
||||||
|
|
||||||
/// Whether `switch` is on now, if it is one of those `VICTORY_AB` turns on and
|
/// Whether `switch` is on now, if it is one of those `VICTORY_AB` turns on and
|
||||||
/// off while the title runs (`ab`).
|
/// off while the title runs (`ab`).
|
||||||
|
|||||||
@@ -54,6 +54,35 @@ fn gap(taken: &[bool], n: usize) -> Option<usize> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MEM_WRITE_WATCH: u32 = 0x20_0000;
|
||||||
|
const MEM_TOP_DOWN: u32 = 0x10_0000;
|
||||||
|
|
||||||
|
/// `VirtualAlloc2` in [4 GiB, 2^37): its address, or the error.
|
||||||
|
fn allocate_in_window(len: usize, kind: u32) -> Result<usize, u32> {
|
||||||
|
use windows_sys::Win32::Foundation::GetLastError;
|
||||||
|
use windows_sys::Win32::System::Memory::{
|
||||||
|
MEM_ADDRESS_REQUIREMENTS, MEM_EXTENDED_PARAMETER, MemExtendedParameterAddressRequirements, PAGE_READWRITE, VirtualAlloc2,
|
||||||
|
};
|
||||||
|
let mut requirements = MEM_ADDRESS_REQUIREMENTS {
|
||||||
|
LowestStartingAddress: (1usize << 32) as *mut _,
|
||||||
|
HighestEndingAddress: ((1usize << 37) - 1) as *mut _,
|
||||||
|
Alignment: 0,
|
||||||
|
};
|
||||||
|
let mut parameter = MEM_EXTENDED_PARAMETER::default();
|
||||||
|
parameter.Anonymous1._bitfield = MemExtendedParameterAddressRequirements as u64;
|
||||||
|
parameter.Anonymous2.Pointer = (&raw mut requirements).cast();
|
||||||
|
// SAFETY: a fresh allocation; the parameter points at `requirements`,
|
||||||
|
// which outlives the call.
|
||||||
|
let at = unsafe { VirtualAlloc2(std::ptr::null_mut(), std::ptr::null(), len, kind, PAGE_READWRITE, &mut parameter, 1) }
|
||||||
|
as usize;
|
||||||
|
if at == 0 {
|
||||||
|
// SAFETY: plain call.
|
||||||
|
Err(unsafe { GetLastError() })
|
||||||
|
} else {
|
||||||
|
Ok(at)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Pieces of address space in [4 GiB, 2^37), [`EARLY_PIECE`] each, reserved
|
/// Pieces of address space in [4 GiB, 2^37), [`EARLY_PIECE`] each, reserved
|
||||||
/// at the start (`VICTORY_LOW_EARLY_MB`, 3,200 with the structure
|
/// at the start (`VICTORY_LOW_EARLY_MB`, 3,200 with the structure
|
||||||
/// builder, else 0). [`reserve`] commits a whole one for a request of that
|
/// builder, else 0). [`reserve`] commits a whole one for a request of that
|
||||||
@@ -72,11 +101,7 @@ const EARLY_PIECE: usize = 64 << 20;
|
|||||||
|
|
||||||
/// Reserves [`EARLY`]; once, as early as the host is made.
|
/// Reserves [`EARLY`]; once, as early as the host is made.
|
||||||
pub(crate) fn reserve_early() {
|
pub(crate) fn reserve_early() {
|
||||||
use windows_sys::Win32::System::Memory::{
|
use windows_sys::Win32::System::Memory::MEM_RESERVE;
|
||||||
MEM_ADDRESS_REQUIREMENTS, MEM_EXTENDED_PARAMETER, MEM_RESERVE, MemExtendedParameterAddressRequirements, PAGE_READWRITE,
|
|
||||||
VirtualAlloc2,
|
|
||||||
};
|
|
||||||
const MEM_WRITE_WATCH: u32 = 0x20_0000;
|
|
||||||
static ONCE: std::sync::Once = std::sync::Once::new();
|
static ONCE: std::sync::Once = std::sync::Once::new();
|
||||||
ONCE.call_once(|| {
|
ONCE.call_once(|| {
|
||||||
let builder = std::env::var_os("VICTORY_BVH_BUILDER").is_some_and(|v| v == "1");
|
let builder = std::env::var_os("VICTORY_BVH_BUILDER").is_some_and(|v| v == "1");
|
||||||
@@ -86,30 +111,9 @@ pub(crate) fn reserve_early() {
|
|||||||
.unwrap_or(if builder { 3200 } else { 0 });
|
.unwrap_or(if builder { 3200 } else { 0 });
|
||||||
let mut early = EARLY.lock().unwrap_or_else(|e| e.into_inner());
|
let mut early = EARLY.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
for _ in 0..(mb << 20) / EARLY_PIECE {
|
for _ in 0..(mb << 20) / EARLY_PIECE {
|
||||||
let mut requirements = MEM_ADDRESS_REQUIREMENTS {
|
let Ok(at) = allocate_in_window(EARLY_PIECE, MEM_RESERVE | MEM_WRITE_WATCH) else {
|
||||||
LowestStartingAddress: (1usize << 32) as *mut _,
|
|
||||||
HighestEndingAddress: ((1usize << 37) - 1) as *mut _,
|
|
||||||
Alignment: 0,
|
|
||||||
};
|
|
||||||
let mut parameter = MEM_EXTENDED_PARAMETER::default();
|
|
||||||
parameter.Anonymous1._bitfield = MemExtendedParameterAddressRequirements as u64;
|
|
||||||
parameter.Anonymous2.Pointer = (&raw mut requirements).cast();
|
|
||||||
// SAFETY: a fresh reservation; the parameter points at
|
|
||||||
// `requirements`, which outlives the call.
|
|
||||||
let at = unsafe {
|
|
||||||
VirtualAlloc2(
|
|
||||||
std::ptr::null_mut(),
|
|
||||||
std::ptr::null(),
|
|
||||||
EARLY_PIECE,
|
|
||||||
MEM_RESERVE | MEM_WRITE_WATCH,
|
|
||||||
PAGE_READWRITE,
|
|
||||||
&mut parameter,
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
} as usize;
|
|
||||||
if at == 0 {
|
|
||||||
break;
|
break;
|
||||||
}
|
};
|
||||||
early.push(at);
|
early.push(at);
|
||||||
}
|
}
|
||||||
// The lowest first.
|
// The lowest first.
|
||||||
@@ -132,40 +136,97 @@ fn from_early(len: usize) -> Option<usize> {
|
|||||||
(done == at).then_some(at)
|
(done == at).then_some(at)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The address space every run reserves first thing ([`reserve_space`]),
|
||||||
|
/// at the top of [4 GiB, 2^37): [`POOL_PIECES`] reservations of
|
||||||
|
/// [`EARLY_PIECE`] (one each, for the write-watch walks as above), before
|
||||||
|
/// anything else can fill that window. On players' PCs it was full by the
|
||||||
|
/// first ray-traced frame (the title's 2 GiB pool went above 2^37 there):
|
||||||
|
/// the first upload asked for failed, and the title threw "D3D12 XDXR
|
||||||
|
/// Error: Failed to allocate upload buffer" and ended the game (RTX 4090,
|
||||||
|
/// 5080, 5090, 11-27).
|
||||||
|
const POOL_PIECES: usize = 32;
|
||||||
|
|
||||||
|
/// Per piece of the pool: its start and how much of it is handed out.
|
||||||
|
static POOL: Mutex<Vec<(usize, usize)>> = Mutex::new(Vec::new());
|
||||||
|
|
||||||
|
/// Reserves the pool (address space only, nothing committed). Called first
|
||||||
|
/// thing in `main`; [`reserve`] does it otherwise.
|
||||||
|
pub fn reserve_space() {
|
||||||
|
use windows_sys::Win32::System::Memory::MEM_RESERVE;
|
||||||
|
static ONCE: std::sync::Once = std::sync::Once::new();
|
||||||
|
ONCE.call_once(|| {
|
||||||
|
let mut pool = POOL.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
|
for _ in 0..POOL_PIECES {
|
||||||
|
let Ok(at) = allocate_in_window(EARLY_PIECE, MEM_RESERVE | MEM_WRITE_WATCH | MEM_TOP_DOWN) else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
pool.push((at, 0));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `len` bytes of the pool committed, from the first piece with room.
|
||||||
|
fn from_pool(len: usize) -> Option<usize> {
|
||||||
|
use windows_sys::Win32::System::Memory::{MEM_COMMIT, PAGE_READWRITE, VirtualAlloc};
|
||||||
|
static TOLD: std::sync::Once = std::sync::Once::new();
|
||||||
|
let len = len.div_ceil(BLOCK) * BLOCK;
|
||||||
|
let mut pool = POOL.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
|
TOLD.call_once(|| {
|
||||||
|
let lowest = pool.iter().map(|p| p.0).min().unwrap_or(0);
|
||||||
|
tracing::info!(
|
||||||
|
pieces = pool.len(),
|
||||||
|
lowest = format_args!("{lowest:#x}"),
|
||||||
|
"address space for memory shaders read by address, reserved at the start"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
let (start, used) = pool.iter_mut().find(|(_, used)| EARLY_PIECE - *used >= len)?;
|
||||||
|
let want = *start + *used;
|
||||||
|
// SAFETY: committing pages of our own reservation, none handed out before.
|
||||||
|
let at = unsafe { VirtualAlloc(want as *const _, len, MEM_COMMIT, PAGE_READWRITE) } as usize;
|
||||||
|
(at == want).then(|| {
|
||||||
|
*used += len;
|
||||||
|
at
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Fresh zeroed memory in [4 GiB, 2^37) (where the directory of memory by
|
/// Fresh zeroed memory in [4 GiB, 2^37) (where the directory of memory by
|
||||||
/// address reaches, `MEMORY_PAGES`), write-watched as the title's own
|
/// address reaches, `MEMORY_PAGES`), write-watched as the title's own
|
||||||
/// pools are (copies then take only written pages). From [`EARLY`] first.
|
/// pools are (copies then take only written pages). From [`EARLY`], then
|
||||||
|
/// the pool, then wherever the window has room. A search of a full window
|
||||||
|
/// takes long in the kernel; after one failed, none is made for a second
|
||||||
|
/// (PR #88: thousands of searches a frame, 30 s frames, the title's
|
||||||
|
/// watchdog).
|
||||||
pub(crate) fn reserve(len: usize) -> Option<usize> {
|
pub(crate) fn reserve(len: usize) -> Option<usize> {
|
||||||
if let Some(at) = from_early(len) {
|
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
|
||||||
|
use windows_sys::Win32::System::Memory::{MEM_COMMIT, MEM_RESERVE};
|
||||||
|
static FAILED_AT_MS: AtomicU64 = AtomicU64::new(0);
|
||||||
|
static FAILURES: AtomicU64 = AtomicU64::new(0);
|
||||||
|
static START: OnceLock<std::time::Instant> = OnceLock::new();
|
||||||
|
reserve_space();
|
||||||
|
if let Some(at) = from_early(len).or_else(|| from_pool(len)) {
|
||||||
return Some(at);
|
return Some(at);
|
||||||
}
|
}
|
||||||
use windows_sys::Win32::System::Memory::{
|
let now = START.get_or_init(std::time::Instant::now).elapsed().as_millis() as u64 + 1;
|
||||||
MEM_ADDRESS_REQUIREMENTS, MEM_COMMIT, MEM_EXTENDED_PARAMETER, MEM_RESERVE, MemExtendedParameterAddressRequirements,
|
let failed = FAILED_AT_MS.load(Relaxed);
|
||||||
PAGE_READWRITE, VirtualAlloc2,
|
if failed != 0 && now < failed + 1000 {
|
||||||
};
|
return None;
|
||||||
const MEM_WRITE_WATCH: u32 = 0x20_0000;
|
}
|
||||||
let mut requirements = MEM_ADDRESS_REQUIREMENTS {
|
match allocate_in_window(len, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH) {
|
||||||
LowestStartingAddress: (1usize << 32) as *mut _,
|
Ok(at) => Some(at),
|
||||||
HighestEndingAddress: ((1usize << 37) - 1) as *mut _,
|
Err(error) => {
|
||||||
Alignment: 0,
|
FAILED_AT_MS.store(now, Relaxed);
|
||||||
};
|
let n = FAILURES.fetch_add(1, Relaxed) + 1;
|
||||||
let mut parameter = MEM_EXTENDED_PARAMETER::default();
|
if n.is_power_of_two() {
|
||||||
parameter.Anonymous1._bitfield = MemExtendedParameterAddressRequirements as u64;
|
tracing::warn!(
|
||||||
parameter.Anonymous2.Pointer = (&raw mut requirements).cast();
|
bytes = len,
|
||||||
// SAFETY: a fresh allocation; the parameter points at `requirements`,
|
error,
|
||||||
// which outlives the call.
|
times = n,
|
||||||
let at = unsafe {
|
"no room left below 2^37 for memory shaders read by address; none looked for again for a second"
|
||||||
VirtualAlloc2(
|
);
|
||||||
std::ptr::null_mut(),
|
}
|
||||||
std::ptr::null(),
|
None
|
||||||
len,
|
}
|
||||||
MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH,
|
}
|
||||||
PAGE_READWRITE,
|
|
||||||
&mut parameter,
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
} as usize;
|
|
||||||
(at != 0).then_some(at)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Upload {
|
impl Upload {
|
||||||
@@ -180,7 +241,13 @@ impl Upload {
|
|||||||
Some(place) => place,
|
Some(place) => place,
|
||||||
None => {
|
None => {
|
||||||
let blocks = n.max(ARENA / BLOCK);
|
let blocks = n.max(ARENA / BLOCK);
|
||||||
let at = reserve(blocks * BLOCK)?;
|
let Some(at) = reserve(blocks * BLOCK) else {
|
||||||
|
// Better than null, which the title throws on, ending
|
||||||
|
// the game: its shaders may read zeros from this.
|
||||||
|
static TOLD: std::sync::Once = std::sync::Once::new();
|
||||||
|
TOLD.call_once(|| tracing::warn!(bytes = len, "an upload from the process heap instead: ray tracing may miss some of it"));
|
||||||
|
return Some(Upload(Kind::Heap(vec![0; len])));
|
||||||
|
};
|
||||||
crate::shadow::register(at, at as u64, (blocks * BLOCK) as u64);
|
crate::shadow::register(at, at as u64, (blocks * BLOCK) as u64);
|
||||||
tracing::info!(at = format_args!("{at:#x}"), bytes = blocks * BLOCK, "memory for lists' uploads, where shaders reach it by address");
|
tracing::info!(at = format_args!("{at:#x}"), bytes = blocks * BLOCK, "memory for lists' uploads, where shaders reach it by address");
|
||||||
arenas.push(Arena { at, taken: vec![false; blocks] });
|
arenas.push(Arena { at, taken: vec![false; blocks] });
|
||||||
@@ -250,6 +317,25 @@ mod tests {
|
|||||||
drop(b);
|
drop(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn the_pool_is_reserved_in_the_window_and_handed_out_once() {
|
||||||
|
reserve_space();
|
||||||
|
assert!(!POOL.lock().unwrap().is_empty(), "the pool is reserved");
|
||||||
|
let a = reserve(100).unwrap();
|
||||||
|
let b = reserve(BLOCK).unwrap();
|
||||||
|
// Larger than a piece: from the window.
|
||||||
|
let c = reserve(EARLY_PIECE + BLOCK).unwrap();
|
||||||
|
for at in [a, b, c] {
|
||||||
|
assert!(at >= 1 << 32 && at + BLOCK <= 1 << 37 && at.is_multiple_of(BLOCK));
|
||||||
|
}
|
||||||
|
assert!(a != b);
|
||||||
|
// SAFETY: committed, at least a block each.
|
||||||
|
unsafe {
|
||||||
|
assert_eq!(*(a as *const u8), 0);
|
||||||
|
(b as *mut u8).add(BLOCK - 1).write(7);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_gap_is_blocks_side_by_side() {
|
fn a_gap_is_blocks_side_by_side() {
|
||||||
assert_eq!(gap(&[true, false, true, false, false, true], 1), Some(1));
|
assert_eq!(gap(&[true, false, true, false, false, true], 1), Some(1));
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ pub use victory_gpu::console;
|
|||||||
/// DLSS and frame generation (`VICTORY_DLSS`, `VICTORY_FRAMEGEN`).
|
/// DLSS and frame generation (`VICTORY_DLSS`, `VICTORY_FRAMEGEN`).
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
pub use victory_gpu::streamline;
|
pub use victory_gpu::streamline;
|
||||||
|
#[cfg(windows)]
|
||||||
|
pub use victory_gpu::reserve_upload_space;
|
||||||
#[cfg(all(windows, feature = "diagnostics"))]
|
#[cfg(all(windows, feature = "diagnostics"))]
|
||||||
pub use victory_gpu::replay;
|
pub use victory_gpu::replay;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user