Skip to content
Draft
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
45 changes: 45 additions & 0 deletions crates/xcl/src/gpu/agility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub enum State {

static STATE: OnceLock<State> = OnceLock::new();
static DEBUG_LAYER: OnceLock<Result<(), String>> = OnceLock::new();
static DRED: OnceLock<Option<Result<(), String>>> = OnceLock::new();
/// Tests: set when any `create_device` started while the DRED step had not run yet.
#[cfg(test)]
pub(crate) static DRED_LATE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Candidate directories in search order.
pub(crate) fn candidates(env_override: Option<PathBuf>, exe_dir: Option<PathBuf>, local_app_data: Option<PathBuf>) -> Vec<PathBuf> {
Expand Down Expand Up @@ -321,7 +325,13 @@ pub fn create_device<P>(adapter: P, feature_level: D3D_FEATURE_LEVEL) -> windows
where
P: Param<IUnknown>,
{
// DRED only applies to devices created after it is set: before the process's first one
enable_dred_once();
let mut device: Option<ID3D12Device> = None;
#[cfg(test)]
if DRED.get().is_none() {
DRED_LATE.store(true, std::sync::atomic::Ordering::SeqCst);
}
match state() {
State::Loaded { factory, .. } => {
// SAFETY: `factory` is a valid `ID3D12DeviceFactory` kept alive for `'static`,
Expand Down Expand Up @@ -357,6 +367,41 @@ pub fn configuration_interface<T: Interface>(clsid: &GUID) -> windows::core::Res
}
}

/// What `XCL_DRED` asks for: `None` when it is unset, else whether the breadcrumbs are on too
/// (`XCL_DRED=pagefault`: the faulting address and what was there only; the breadcrumbs put a
/// marker around every operation of every list, which is what made a title stall).
pub(crate) fn dred_request(var: Option<std::ffi::OsString>) -> Option<bool> {
var.map(|v| v != "pagefault")
}

/// `XCL_DRED=1`: on a device removal the faulting address and the last GPU operations (logged
/// by `log_device_removed`); no debug layer needed. Opt-in: with it XDefiant stalled in its
/// first seconds twice. The settings apply only to devices created after them, so this runs
/// from `create_device`, before the first device of the process (the launch preparation's
/// runner makes one before the title's); once per process.
pub fn enable_dred_once() -> &'static Option<Result<(), String>> {
DRED.get_or_init(|| {
let breadcrumbs = dred_request(std::env::var_os("XCL_DRED"))?;
Some(match configuration_interface::<ID3D12DeviceRemovedExtendedDataSettings>(&CLSID_D3D12DeviceRemovedExtendedData) {
Ok(d) => {
// SAFETY: `d` is a valid settings interface; the calls only record the setting.
unsafe {
if breadcrumbs {
d.SetAutoBreadcrumbsEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON);
}
d.SetPageFaultEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON);
}
crate::xlog_info!("D3D12: DRED on ({})", if breadcrumbs { "page faults and the lists' last operations" } else { "page faults" });
Ok(())
}
Err(e) => {
crate::xlog_warn!("D3D12: DRED requested but unavailable: {e}");
Err(e.to_string())
}
})
})
}

/// Enable the debug layer at most once per process. The GPU test runner may create a
/// device before D3D12.X starts; enabling it again after that removes the device.
pub fn enable_debug_layer() -> &'static Result<(), String> {
Expand Down
25 changes: 25 additions & 0 deletions crates/xcl/src/gpu/agility_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,31 @@ fn rejects_malformed_export_rvas_and_cross_section_reads() {
assert!(read_sdk_version_from_bytes(&x86).is_err());
}

#[test]
fn dred_request_reads_the_variable() {
use std::ffi::OsString;

assert_eq!(dred_request(None), None);
assert_eq!(dred_request(Some(OsString::from("1"))), Some(true));
assert_eq!(dred_request(Some(OsString::from("pagefault"))), Some(false));
}

/// The DRED settings apply only to devices created after them: `create_device` (the runner's
/// and the title's) has to have set them by the time it returns the process's first device.
#[test]
fn create_device_sets_dred_first() {
// Needs no device: the record is made before the creation call, whether or not it succeeds.
let dev = create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0);
assert!(
!DRED_LATE.load(std::sync::atomic::Ordering::SeqCst),
"a create_device call started before the DRED step had run"
);
assert!(DRED.get().is_some(), "create_device did not run the DRED step");
if dev.is_err() {
crate::gpu::recomp::runner::skip_test("no D3D12 device (the ordering was checked)");
}
}

#[test]
fn env_trimming_trailing_slash_and_no_sm66_messages() {
use std::ffi::OsString;
Expand Down
24 changes: 2 additions & 22 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,28 +614,8 @@ pub fn init() -> Result<String, String> {
}
}
}
// `XCL_DRED=1`: on a device removal the faulting address and the last GPU operations
// (logged by `log_device_removed`); no debug layer needed. Opt-in: with it XDefiant
// stalled in its first seconds twice.
if let Some(dred) = std::env::var_os("XCL_DRED") {
match crate::gpu::agility::configuration_interface::<ID3D12DeviceRemovedExtendedDataSettings>(
&CLSID_D3D12DeviceRemovedExtendedData,
) {
Ok(d) => {
// (`XCL_DRED=pagefault`: the faulting address and what was there only; the
// breadcrumbs put a marker around every operation of every list, which is
// what made a title stall)
if dred != "pagefault" {
d.SetAutoBreadcrumbsEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON);
}
d.SetPageFaultEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON);
crate::xlog_info!("D3D12: DRED on ({})", if dred == "pagefault" { "page faults" } else { "page faults and the lists' last operations" });
}
Err(e) => {
crate::xlog_warn!("D3D12: DRED requested but unavailable: {e}");
}
}
}
// `XCL_DRED=1` (`create_device` does it too; this call only puts the log line before the device)
crate::gpu::agility::enable_dred_once();
let factory: IDXGIFactory6 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0)).map_err(|e| format!("DXGI: {e}"))?;
let mut device: Option<ID3D12Device> = None;
let mut name = String::new();
Expand Down
Loading