Skip to content
Open
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
40 changes: 37 additions & 3 deletions d3d12/crates/leonida-gpu/src/pipeline/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,20 @@ fn library() -> Option<&'static Library> {
/// adapter or driver change, damage).
fn open(device: &ID3D12Device1, blob: Blob) -> Option<Library> {
let make = |bytes: &[u8]| -> windows::core::Result<ID3D12PipelineLibrary1> {
// 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::<ID3D12PipelineLibrary>(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() {
Expand Down Expand Up @@ -548,7 +559,13 @@ fn open(device: &ID3D12Device1, blob: Blob) -> Option<Library> {
// 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,
Expand All @@ -557,6 +574,14 @@ fn open(device: &ID3D12Device1, blob: Blob) -> Option<Library> {
})
}

/// 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 {
Expand Down Expand Up @@ -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<u8> = 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);
Expand Down
22 changes: 15 additions & 7 deletions d3d12/crates/leonida-gpu/tests/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,21 @@ fn mesh(device: &ID3D12Device, blend: D3D12_BLEND_DESC) -> Option<(Made, ID3D12C
SizeInBytes: size_of::<Stream>(),
pPipelineStateSubobjectStream: (&raw mut stream).cast(),
};
let library: ID3D12PipelineLibrary1 = device
.cast::<ID3D12Device1>()
.unwrap()
.CreatePipelineLibrary::<ID3D12PipelineLibrary>(&[])
.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::<ID3D12Device1>().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::<ID3D12Device2>()
Expand Down