diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index c2655e74f4..fdf2b055a8 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -1030,6 +1030,31 @@ fn buffer_srv( once!(first, count, element, "buffer view empty or past its host copy"); return None; } + let stride = u64::from(buffer.StructureByteStride); + if stride > 0 && raw_structured() { + // The same bytes as a raw view: the byte offset of a scalar or + // offset-only read (`victory_shader::emit`, raw handles) then reads + // what it names; through the structured view NVIDIA took it as the + // element, so the wind simulation's loop counts were other records' + // data and its loops ran until the device was reset. Shaders reading + // by element keep their structured handle, which NVIDIA reads on a + // raw view as the console would (as `WARP` does; AMD reads element 0, + // so only hosts that read it right do this). Raw views start on 16 + // bytes; one that would not stays structured. + let (byte, dwords) = (first * stride, u64::from(count) * stride / 4); + match u32::try_from(dwords) { + Ok(dwords) if byte.is_multiple_of(16) && stride.is_multiple_of(4) => { + desc.Format = DXGI_FORMAT_R32_TYPELESS; + desc.Anonymous.Buffer = D3D12_BUFFER_SRV { + FirstElement: byte / 4, + NumElements: dwords, + StructureByteStride: 0, + Flags: D3D12_BUFFER_SRV_FLAG_RAW, + }; + } + _ => once!(byte, stride, "structured buffer view not made raw: not on 16 bytes"), + } + } let host = host()?; // SAFETY: a live buffer and a slot of our heap. unsafe { @@ -1039,6 +1064,22 @@ fn buffer_srv( Some(Some((shadow.buffer.clone(), own.then_some(shadow)))) } +/// Whether structured buffer views are made raw ([`buffer_srv`]): on NVIDIA +/// hosts, which read a structured handle on a raw view right. +/// `VICTORY_RAW_STRUCTURED=0` or `=1` decides instead. +fn raw_structured() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| { + let on = match std::env::var("VICTORY_RAW_STRUCTURED").as_deref() { + Ok("0") => false, + Ok("1") => true, + _ => crate::host::host_vendor() == 0x10DE, + }; + tracing::info!(on, "structured buffer views made raw"); + on + }) +} + /// Whether `count` elements of `element` bytes from element `first` lie in /// `buffer`. The console takes a view that is empty or runs past its /// memory; the host removes the device for one. diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 4a522031f5..95295ad7dd 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -277,6 +277,25 @@ fn create() -> windows::core::Result { } } +/// The PCI vendor id of the host device's adapter (NVIDIA 0x10DE), 0 if it +/// cannot be told. +pub(crate) fn host_vendor() -> u32 { + static VENDOR: OnceLock = OnceLock::new(); + *VENDOR.get_or_init(|| { + let Some(host) = host() else { return 0 }; + // SAFETY: plain DXGI queries for the device's own adapter. + let vendor = unsafe { + let luid = host.device.GetAdapterLuid(); + CreateDXGIFactory2::(DXGI_CREATE_FACTORY_FLAGS(0)) + .and_then(|f| f.EnumAdapterByLuid::(luid)) + .and_then(|a| a.GetDesc1()) + .map_or(0, |d| d.VendorId) + }; + tracing::info!(vendor = format_args!("{vendor:#x}"), "host adapter vendor"); + vendor + }) +} + /// The adapter the host device lands on: the one with the highest /// performance (the discrete card on hybrid systems), or the one /// `VICTORY_ADAPTER` names by a substring of its description. Without a