From 01a949cc0dadf5d9aa0a96dad09b95409b195d99 Mon Sep 17 00:00:00 2001 From: openhands Date: Wed, 25 Nov 2026 17:31:36 +0000 Subject: [PATCH] GPU: on NVIDIA, structured buffer views are made raw Port of upstream PR #2464 (claude-GTA6, AI-assisted, design from fxceless/victory-fork's raw_window). FIND-11 / upstream #1744: a scalar or offset-only buffer read is translated as a rawBufferLoad at a byte offset; when the slot holds the title's structured view, D3D12 leaves that read undefined and NVIDIA took the byte offset as the element, so WindNew's loop counts came from other records' data and its loops ran until the device was reset (opening-credits hang on NVIDIA). buffer_srv: on an NVIDIA host a structured SRV becomes a raw view (R32_TYPELESS + RAW) over the same bytes when they start on 16 bytes; otherwise it stays structured and logs once. VICTORY_RAW_STRUCTURED overrides the vendor check. host_vendor() reads the adapter vendor once by LUID. Shaders are unchanged: reads by element keep their structured handle, which NVIDIA and WARP read right on a raw view (AMD reads element 0, so other hosts are unchanged). No shader-cache or pipeline-library rebuild is needed. Co-authored-by: openhands --- crates/victory-gpu/src/descriptor/host.rs | 41 +++++++++++++++++++++++ crates/victory-gpu/src/host.rs | 19 +++++++++++ 2 files changed, 60 insertions(+) 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