diff --git a/d3d12/crates/leonida-gpu/src/pipeline/library.rs b/d3d12/crates/leonida-gpu/src/pipeline/library.rs index 19420b83f9..304092a74f 100644 --- a/d3d12/crates/leonida-gpu/src/pipeline/library.rs +++ b/d3d12/crates/leonida-gpu/src/pipeline/library.rs @@ -517,9 +517,20 @@ fn library() -> Option<&'static Library> { /// adapter or driver change, damage). fn open(device: &ID3D12Device1, blob: Blob) -> Option { let make = |bytes: &[u8]| -> windows::core::Result { - // SAFETY: the bytes outlive the library (kept beside it). + use windows::core::Interface; + let mut raw = std::ptr::null_mut(); + // SAFETY: the bytes outlive the library (kept beside it); an empty + // blob is a null pointer ([`blob_pointer`]); `raw` is ours on success. unsafe { - device.CreatePipelineLibrary::(bytes)?.cast() + (Interface::vtable(device).CreatePipelineLibrary)( + Interface::as_raw(device), + blob_pointer(bytes), + bytes.len(), + &ID3D12PipelineLibrary::IID, + &mut raw, + ) + .ok()?; + ID3D12PipelineLibrary::from_raw(raw).cast() } }; if !blob.bytes().is_empty() { @@ -548,7 +559,13 @@ fn open(device: &ID3D12Device1, blob: Blob) -> Option { // The rejected bytes are let go (a mapped file is unmapped) before the // empty library is made: nothing reads them any more. drop(blob); - let library = make(&[]).ok()?; + let library = match make(&[]) { + Ok(library) => library, + Err(e) => { + tracing::warn!(error = %e, "the host refused an empty pipeline library"); + return None; + } + }; Some(Library { library, _blob: Blob::None, @@ -557,6 +574,14 @@ fn open(device: &ID3D12Device1, blob: Blob) -> Option { }) } +/// The pointer `CreatePipelineLibrary` is given for `bytes`: null for an +/// empty blob. An empty slice's own pointer is dangling, not null; the +/// D3D12 runtime takes that with a length of 0, but VKD3D-Proton answers +/// `E_INVALIDARG` (0x80070057) to it, and the run then kept no library. +fn blob_pointer(bytes: &[u8]) -> *const core::ffi::c_void { + if bytes.is_empty() { std::ptr::null() } else { bytes.as_ptr().cast() } +} + /// A pipeline's name in the library: the hash of what its host /// description holds. pub(crate) fn name(kind: &str, root: &[u8], code: &[&[u8]], state: &str) -> String { @@ -887,6 +912,15 @@ pub(crate) fn note_used(key: &str) { mod tests { use super::*; + #[test] + fn an_empty_blob_is_given_as_a_null_pointer() { + assert!(blob_pointer(&[]).is_null()); + let empty: Vec = Vec::new(); + assert!(blob_pointer(&empty).is_null()); + let bytes = [1u8, 2, 3]; + assert_eq!(blob_pointer(&bytes), bytes.as_ptr().cast()); + } + #[test] fn bypassing_the_library_calls_pipeline_creation() { let created = std::cell::Cell::new(false); diff --git a/d3d12/crates/leonida-gpu/tests/replay.rs b/d3d12/crates/leonida-gpu/tests/replay.rs index 675b80a336..23cc2ebcd3 100644 --- a/d3d12/crates/leonida-gpu/tests/replay.rs +++ b/d3d12/crates/leonida-gpu/tests/replay.rs @@ -187,13 +187,21 @@ fn mesh(device: &ID3D12Device, blend: D3D12_BLEND_DESC) -> Option<(Made, ID3D12C SizeInBytes: size_of::(), pPipelineStateSubobjectStream: (&raw mut stream).cast(), }; - let library: ID3D12PipelineLibrary1 = device - .cast::() - .unwrap() - .CreatePipelineLibrary::(&[]) - .unwrap() - .cast() - .unwrap(); + // An empty library as a null blob of length 0: the windows crate + // would pass an empty slice's dangling pointer, which VKD3D-Proton + // refuses with E_INVALIDARG (as `pipeline::library` does it). + let device1 = device.cast::().unwrap(); + let mut raw = std::ptr::null_mut(); + (Interface::vtable(&device1).CreatePipelineLibrary)( + device1.as_raw(), + std::ptr::null(), + 0, + &ID3D12PipelineLibrary::IID, + &mut raw, + ) + .ok() + .unwrap(); + let library: ID3D12PipelineLibrary1 = ID3D12PipelineLibrary::from_raw(raw).cast().unwrap(); { let made: ID3D12PipelineState = device .cast::()