Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ cargo build --profile diag -p victory --features diagnostics # target\diag\vic
| `VICTORY_PIPELINE_WORKERS=16` | use this many pipeline worker threads (1-64) instead of half the logical processors, 2-8 by default |
| `VICTORY_SKIP_PIPELINES=<hash>,...` | omit draws and dispatches, including indirect ones, whose pipeline contains a listed 16-digit shader hash |
| `VICTORY_GPU_TIMING=1` | with the above, each list of a submission run and timed on its own; slow lists logged with their slowest commands |
| `VICTORY_TEXTURE_MIP_DROP=0..=3` | override texture mip dropping (default: 1 on adapters with at most 8.5 GiB dedicated VRAM, then raised after allocation out-of-memory) |
| `VICTORY_TEXTURE_MIP_DROP_MIN_SIZE=1024` | smallest post-drop texture dimension eligible for mip dropping |
| `VICTORY_D3D12_WARP=1` | host device on the software adapter (slow; cannot reset the driver) |
| `VICTORY_SHADER_LOOP_LIMIT=<n>` | translated shader loops stop after `n` iterations per lane instead of 2^14 (own shader cache entries); if a lower `n` makes world frames much faster, loops hitting the limit are what makes them slow |
| `VICTORY_AUDIO_CAPTURE=<file.wav>` | records what the title plays (our process only) |
Expand Down
137 changes: 137 additions & 0 deletions crates/victory-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,127 @@ fn fits(texture: &ID3D12Resource, mut format: Option<&mut DXGI_FORMAT>, unordere
true
}

/// Re-bases a console SRV mip window onto a host texture whose top `drop`
/// mips were omitted (`host_mips` surviving levels, console levels
/// `drop..drop + host_mips`).
fn rebase_mip_window(
most_detailed: u32,
mip_levels: u32,
min_lod: f32,
drop: u32,
host_mips: u32,
) -> (u32, u32, f32) {
if drop == 0 {
return (most_detailed, mip_levels, min_lod);
}
let host_mips = host_mips.max(1);
let title_mips = host_mips.saturating_add(drop);
let min_lod = (min_lod - drop as f32).max(0.0);
if mip_levels == u32::MAX {
let start = most_detailed.saturating_sub(drop).min(host_mips - 1);
return (start, u32::MAX, min_lod);
}
let end = most_detailed.saturating_add(mip_levels).min(title_mips);
let start = most_detailed.max(drop);
if start < end {
(start - drop, end - start, min_lod)
} else {
// A view naming only dropped mips stays valid at the first surviving
// mip; one past the chain uses the smallest survivor.
let start = if most_detailed < drop {
0
} else {
host_mips - 1
};
(start, 1, min_lod)
}
}

/// Adjusts an explicit SRV description for a texture whose top `drop` mips
/// were omitted on the host.
fn adjust_srv_for_mip_drop(srv: &mut D3D12_SHADER_RESOURCE_VIEW_DESC, drop: u32, host_mips: u32) {
if drop == 0 {
return;
}
// SAFETY: each branch reads and writes only the union arm named by
// `srv.ViewDimension`.
unsafe {
match srv.ViewDimension {
D3D12_SRV_DIMENSION_TEXTURE1D => {
let t = &mut srv.Anonymous.Texture1D;
(t.MostDetailedMip, t.MipLevels, t.ResourceMinLODClamp) = rebase_mip_window(
t.MostDetailedMip,
t.MipLevels,
t.ResourceMinLODClamp,
drop,
host_mips,
);
}
D3D12_SRV_DIMENSION_TEXTURE1DARRAY => {
let t = &mut srv.Anonymous.Texture1DArray;
(t.MostDetailedMip, t.MipLevels, t.ResourceMinLODClamp) = rebase_mip_window(
t.MostDetailedMip,
t.MipLevels,
t.ResourceMinLODClamp,
drop,
host_mips,
);
}
D3D12_SRV_DIMENSION_TEXTURE2D => {
let t = &mut srv.Anonymous.Texture2D;
(t.MostDetailedMip, t.MipLevels, t.ResourceMinLODClamp) = rebase_mip_window(
t.MostDetailedMip,
t.MipLevels,
t.ResourceMinLODClamp,
drop,
host_mips,
);
}
D3D12_SRV_DIMENSION_TEXTURE2DARRAY => {
let t = &mut srv.Anonymous.Texture2DArray;
(t.MostDetailedMip, t.MipLevels, t.ResourceMinLODClamp) = rebase_mip_window(
t.MostDetailedMip,
t.MipLevels,
t.ResourceMinLODClamp,
drop,
host_mips,
);
}
D3D12_SRV_DIMENSION_TEXTURE3D => {
let t = &mut srv.Anonymous.Texture3D;
(t.MostDetailedMip, t.MipLevels, t.ResourceMinLODClamp) = rebase_mip_window(
t.MostDetailedMip,
t.MipLevels,
t.ResourceMinLODClamp,
drop,
host_mips,
);
}
D3D12_SRV_DIMENSION_TEXTURECUBE => {
let t = &mut srv.Anonymous.TextureCube;
(t.MostDetailedMip, t.MipLevels, t.ResourceMinLODClamp) = rebase_mip_window(
t.MostDetailedMip,
t.MipLevels,
t.ResourceMinLODClamp,
drop,
host_mips,
);
}
D3D12_SRV_DIMENSION_TEXTURECUBEARRAY => {
let t = &mut srv.Anonymous.TextureCubeArray;
(t.MostDetailedMip, t.MipLevels, t.ResourceMinLODClamp) = rebase_mip_window(
t.MostDetailedMip,
t.MipLevels,
t.ResourceMinLODClamp,
drop,
host_mips,
);
}
_ => {}
}
}
}

/// Makes the host descriptor for `view` at `at`: the host resource it
/// views, or `None` if it cannot.
fn make(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> Option<Kept> {
Expand Down Expand Up @@ -1071,6 +1192,10 @@ fn make(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> Option<Kept> {
if !fits(texture, desc.as_mut().map(|d| &mut d.Format), false) {
return None;
}
let (drop, host_mips) = crate::resource::host_mip_info(resource);
if let Some(srv) = desc.as_mut() {
adjust_srv_for_mip_drop(srv, drop, host_mips);
}
// SAFETY: a live texture and a slot of our heap.
unsafe {
device.CreateShaderResourceView(
Expand Down Expand Up @@ -1433,4 +1558,16 @@ mod tests {

crate::shadow::unregister(resource_id, start);
}

#[test]
fn srv_mip_window_rebasing_handles_partial_full_and_dropped_ranges() {
assert_eq!(rebase_mip_window(1, 3, 3.5, 2, 5), (0, 2, 1.5));
assert_eq!(rebase_mip_window(3, 2, 1.0, 2, 5), (1, 2, 0.0));
assert_eq!(
rebase_mip_window(0, u32::MAX, 0.0, 2, 5),
(0, u32::MAX, 0.0)
);
assert_eq!(rebase_mip_window(0, 1, 0.0, 2, 5), (0, 1, 0.0));
assert_eq!(rebase_mip_window(1, 3, 1.5, 0, 7), (1, 3, 1.5));
}
}
85 changes: 85 additions & 0 deletions crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const RTV_RING: u32 = 1024;

pub(crate) struct Host {
pub(crate) device: ID3D12Device,
/// Dedicated video memory of the adapter the device is on, in bytes.
memory: u64,
queue: ID3D12CommandQueue,
fence: ID3D12Fence,
/// The last value signalled on `fence`, behind the submission lock.
Expand Down Expand Up @@ -187,10 +189,23 @@ fn create() -> windows::core::Result<Host> {
let adapter = warp_adapter()?;
D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_12_0, &mut device)?;
tracing::info!("host device on the software adapter (WARP)");
} else if let Some((adapter, memory, name)) = hardware_adapter() {
match D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_12_0, &mut device) {
Ok(()) => tracing::info!(
adapter = name,
memory_gb = format_args!("{:.1}", memory as f64 / f64::powi(1024.0, 3)),
"host device on the largest-memory hardware adapter"
),
Err(e) => {
tracing::warn!(error = %e, adapter = name, "hardware adapter refused D3D12; using the default adapter");
D3D12CreateDevice(None, D3D_FEATURE_LEVEL_12_0, &mut device)?;
}
}
} else {
D3D12CreateDevice(None, D3D_FEATURE_LEVEL_12_0, &mut device)?;
}
let device = device.ok_or_else(windows::core::Error::empty)?;
let memory = adapter_memory(&device);
let messages = debug
.then(|| device.cast::<ID3D12InfoQueue>().ok())
.flatten();
Expand All @@ -217,6 +232,7 @@ fn create() -> windows::core::Result<Host> {
tracing::info!("host D3D12 device ready");
Ok(Host {
device,
memory,
queue,
fence,
submitted: Mutex::new(0),
Expand All @@ -233,6 +249,75 @@ fn create() -> windows::core::Result<Host> {
}
}

/// The hardware adapter with the most dedicated video memory, if DXGI knows
/// one: a Ryzen APU plus a discrete card must not default to the APU's small
/// shared-memory device.
fn hardware_adapter() -> Option<(IDXGIAdapter, u64, String)> {
// SAFETY: plain DXGI factory and adapter queries.
unsafe {
let factory: IDXGIFactory4 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0)).ok()?;
let mut best = None;
for i in 0.. {
let Ok(adapter1) = factory.EnumAdapters1(i) else {
break;
};
let Ok(desc) = adapter1.GetDesc1() else {
continue;
};
if desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE.0 as u32 != 0 {
continue;
}
let memory = desc.DedicatedVideoMemory as u64;
let end = desc
.Description
.iter()
.position(|&c| c == 0)
.unwrap_or(desc.Description.len());
let name = String::from_utf16_lossy(&desc.Description[..end]);
let better = best
.as_ref()
.is_none_or(|(_, old, _): &(IDXGIAdapter, u64, String)| memory > *old);
if better {
best = adapter1
.cast::<IDXGIAdapter>()
.ok()
.map(|a| (a, memory, name));
}
}
best
}
}

/// Dedicated video memory of `device`'s adapter in bytes, zero if DXGI does
/// not say. Matching by LUID also covers a device made on the default adapter.
fn adapter_memory(device: &ID3D12Device) -> u64 {
// SAFETY: plain device and DXGI queries.
unsafe {
let luid = device.GetAdapterLuid();
let Ok(factory) = CreateDXGIFactory2::<IDXGIFactory4>(DXGI_CREATE_FACTORY_FLAGS(0)) else {
return 0;
};
for i in 0.. {
let Ok(adapter) = factory.EnumAdapters1(i) else {
break;
};
let Ok(desc) = adapter.GetDesc1() else {
continue;
};
if desc.AdapterLuid == luid {
return desc.DedicatedVideoMemory as u64;
}
}
0
}
}

/// Dedicated video memory of the host adapter in bytes (`None` without a
/// host device).
pub(crate) fn device_memory() -> Option<u64> {
host().map(|h| h.memory)
}

/// DXGI may return hardware for `EnumWarpAdapter` (notably under Proton).
/// Reject it before creating a device in diagnostics' software-only mode.
fn warp_adapter() -> windows::core::Result<IDXGIAdapter> {
Expand Down
Loading
Loading