From f1794125d58af729b4eba8aa5bcb5ec030b3610a Mon Sep 17 00:00:00 2001 From: Kuzey Cimen Date: Sun, 29 Nov 2026 22:39:34 +0000 Subject: [PATCH 1/3] d3d12: give an empty pipeline library a null blob (D3D12-VKD3D-EMPTY-PIPELINE-LIBRARY) The empty host pipeline library was made with CreatePipelineLibrary(&[]), which the windows crate passes as an empty slice's dangling, non-null pointer with a length of 0. Microsoft's D3D12 takes that; VKD3D-Proton answers E_INVALIDARG (0x80070057), and make(&[]).ok()? dropped the error, so under Wine and Proton the run kept no library and every start made every pipeline again. The blob now goes through blob_pointer(), null when it is empty, and a refused empty library is logged instead of dropped. Co-Authored-By: Claude Opus 5.5 --- .../leonida-gpu/src/pipeline/library.rs | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/pipeline/library.rs b/d3d12/crates/leonida-gpu/src/pipeline/library.rs index c446aabcb3..48e5d6b412 100644 --- a/d3d12/crates/leonida-gpu/src/pipeline/library.rs +++ b/d3d12/crates/leonida-gpu/src/pipeline/library.rs @@ -505,9 +505,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() { @@ -536,7 +547,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, @@ -545,6 +562,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 { @@ -873,6 +898,16 @@ 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()); + assert!(!empty.as_ptr().is_null(), "an empty slice's pointer is dangling, which VKD3D-Proton refuses"); + 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); From 55ac5d337b25320803edc6e363871a2064ac5ada Mon Sep 17 00:00:00 2001 From: Kuzey Cimen Date: Sun, 29 Nov 2026 22:53:19 +0000 Subject: [PATCH 2/3] d3d12: drop the null check clippy calls useless from the empty-blob test An empty slice's pointer is never null, so the assertion could not fail (useless_ptr_null_checks); the converted pointer is checked just above. Co-Authored-By: Claude Opus 5.5 --- d3d12/crates/leonida-gpu/src/pipeline/library.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/d3d12/crates/leonida-gpu/src/pipeline/library.rs b/d3d12/crates/leonida-gpu/src/pipeline/library.rs index 48e5d6b412..a20b3414d2 100644 --- a/d3d12/crates/leonida-gpu/src/pipeline/library.rs +++ b/d3d12/crates/leonida-gpu/src/pipeline/library.rs @@ -903,7 +903,6 @@ mod tests { assert!(blob_pointer(&[]).is_null()); let empty: Vec = Vec::new(); assert!(blob_pointer(&empty).is_null()); - assert!(!empty.as_ptr().is_null(), "an empty slice's pointer is dangling, which VKD3D-Proton refuses"); let bytes = [1u8, 2, 3]; assert_eq!(blob_pointer(&bytes), bytes.as_ptr().cast()); } From f4ae7b922bc591918b43c4761e69f77572a4c8ac Mon Sep 17 00:00:00 2001 From: Kuzey Cimen Date: Tue, 1 Dec 2026 21:19:20 +0000 Subject: [PATCH 3/3] d3d12: give the replay test's empty pipeline library a null blob too tests/replay.rs made its library with CreatePipelineLibrary(&[]), the same dangling non-null pointer with a length of 0 that VKD3D-Proton refuses with E_INVALIDARG. It now passes null and 0 through the vtable, as pipeline::library does. Co-Authored-By: Claude Opus 5.5 --- d3d12/crates/leonida-gpu/tests/replay.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) 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::()