Skip to content
Open
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
38 changes: 38 additions & 0 deletions crates/xcl/src/gpu/agility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,44 @@ where
device.ok_or_else(|| windows::core::Error::from(windows::Win32::Foundation::E_FAIL))
}

/// Environment variable: `0` turns the driver's background compiles off.
pub const ENV_BACKGROUND: &str = "XCL_BACKGROUND_COMPILES";

/// `XCL_BACKGROUND_COMPILES=0`: the driver's background work on `device` is turned off
/// (None: not asked for; Err: the reason it stays on, for the log).
///
/// NVIDIA's driver compiles a compute pipeline a second time on D3D12's background threads
/// once `CreateComputePipelineState` has returned (#156): as long again and with the same
/// memory (a translated CS of 98 s in the call: 198 s of CPU in all), next to the compiles a
/// title is waiting for. Off by default: what the second compile gains at run time is not
/// measured.
pub fn background_compiles_off(device: &ID3D12Device) -> Option<Result<(), String>> {
background_off_asked(std::env::var(ENV_BACKGROUND).ok().as_deref()).then(|| set_background_work(device, false))
}

/// Whether the value of `XCL_BACKGROUND_COMPILES` asks for them off: `0` only.
fn background_off_asked(value: Option<&str>) -> bool {
value.is_some_and(|v| v.trim() == "0")
}

/// D3D12 background processing of `device` allowed (the default) or not. Windows takes "not"
/// in Developer Mode only.
fn set_background_work(device: &ID3D12Device, on: bool) -> Result<(), String> {
let mode = if on { D3D12_BACKGROUND_PROCESSING_MODE_ALLOWED } else { D3D12_BACKGROUND_PROCESSING_MODE_DISABLE_BACKGROUND_WORK };
// SAFETY: `device` is a live device; no event and no out-pointer are passed.
let r = device.cast::<ID3D12Device6>().and_then(|d| unsafe { d.SetBackgroundProcessingMode(mode, D3D12_MEASUREMENTS_ACTION_KEEP_ALL, None, None) });
r.map_err(|e| background_refusal(e.code()))
}

/// Why `SetBackgroundProcessingMode` did not take a mode, for the log.
fn background_refusal(code: windows::core::HRESULT) -> String {
if code == windows::Win32::Graphics::Dxgi::DXGI_ERROR_SDK_COMPONENT_MISSING {
format!("Windows takes it in Developer Mode only ({:#x})", code.0)
} else {
format!("{:#x}", code.0)
}
}

/// Retrieve a D3D12 configuration/debug interface (`ID3D12Debug`,
/// `ID3D12DeviceRemovedExtendedDataSettings`, etc.) from the Agility SDK factory when
/// loaded, otherwise from inbox `D3D12GetDebugInterface`.
Expand Down
30 changes: 30 additions & 0 deletions crates/xcl/src/gpu/agility_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,33 @@ fn resolve_dir_handles_explicit_override_and_implicit_fallback() {

let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn background_compiles_are_turned_off_by_0_only() {
assert!(background_off_asked(Some("0")));
assert!(background_off_asked(Some(" 0 ")));
for other in [None, Some(""), Some("1"), Some("off"), Some("00")] {
assert!(!background_off_asked(other), "{other:?}");
}
}

#[test]
fn a_refusal_for_developer_mode_says_so() {
let missing = windows::Win32::Graphics::Dxgi::DXGI_ERROR_SDK_COMPONENT_MISSING;
assert_eq!(background_refusal(missing), "Windows takes it in Developer Mode only (0x887a002d)");
assert_eq!(background_refusal(windows::Win32::Foundation::E_NOINTERFACE), "0x80004002");
}

/// A device that takes "no background work" (Windows in Developer Mode) takes "allowed"
/// again; one that refuses it says why and is left as it was.
#[test]
fn gpu_background_work_is_turned_off_and_on_again() {
let Ok(dev) = create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else {
crate::gpu::recomp::runner::skip_test("no GPU");
return;
};
match set_background_work(&dev, false) {
Ok(()) => assert_eq!(set_background_work(&dev, true), Ok(())),
Err(e) => println!("not taken here: {e}"),
}
}
5 changes: 5 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,11 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result<String, String
let _ = q.RegisterMessageCallback(Some(debug_message), D3D12_MESSAGE_CALLBACK_FLAG_NONE, std::ptr::null_mut(), &mut cookie);
}
}
match crate::gpu::agility::background_compiles_off(&device) {
Some(Ok(())) => crate::xlog_info!("D3D12: the driver's background compiles are off ({}=0)", crate::gpu::agility::ENV_BACKGROUND),
Some(Err(e)) => crate::xlog_warn!("D3D12: {}=0 was not taken, the driver's background compiles stay on: {e}", crate::gpu::agility::ENV_BACKGROUND),
None => {}
}
open_uma(&device, allow_driver_remap);
super::device_x::probe_dynamic_depth_bias(&device);
crate::shaders::pipeline_library::activate(&device)?;
Expand Down
5 changes: 5 additions & 0 deletions crates/xcl/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,11 @@ impl Pipelines {
(crate::gpu::agility::create_device(&adapter, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())?, desc.VendorId)
};
crate::hle::d3d12x::real::driver_cache::note_vendor(vendor);
match crate::gpu::agility::background_compiles_off(&dev) {
Some(Ok(())) => eprintln!(" the driver's background compiles are off ({}=0)", crate::gpu::agility::ENV_BACKGROUND),
Some(Err(e)) => eprintln!(" {}=0 was not taken, the driver's background compiles stay on: {e}", crate::gpu::agility::ENV_BACKGROUND),
None => {}
}
let most = pipeline_threads();
let max_dxil = pipeline_cap_kb(std::env::var("XCL_PIPELINE_MAX_KB").ok().as_deref()).saturating_mul(1024);
Ok(Pipelines {
Expand Down
Loading