Skip to content
Draft
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper.
Changes to translation, pipeline layout, settings or cache files trigger validation. During
validation, exact recipes that the driver accepts are imported from the previous pipeline
library; incompatible or missing recipes are rebuilt normally.
Loading a native pipeline library retains its file buffer, avoiding a second library-sized
allocation while sharing it between preparation and runtime.
- To prepare known graphics, two-pass mesh and compute recipes offline, use a local
[archive layout profile](scripts/pso_corpus/archive-profile.md) with the following commands.
The staging command accepts several journals, deduplicates their recipes and atomically publishes
Expand Down
31 changes: 27 additions & 4 deletions crates/xcl/src/shaders/pipeline_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ pub(crate) static BUILD_TEST: std::sync::Mutex<()> = std::sync::Mutex::new(());

pub(crate) struct NativeLibrary {
library: ID3D12PipelineLibrary1,
_serialized: std::sync::Arc<[u8]>,
// Keep the read buffer: converting a Vec to Arc<[u8]> copies the whole library.
_serialized: std::sync::Arc<Vec<u8>>,
}

impl NativeLibrary {
Expand All @@ -177,14 +178,14 @@ impl NativeLibrary {
}

pub(crate) fn empty(device: &ID3D12Device) -> Result<Self> {
Self::open_shared(device, std::sync::Arc::from([]))
Self::open_shared(device, std::sync::Arc::new(Vec::new()))
}

pub(crate) fn open(device: &ID3D12Device, serialized: Vec<u8>) -> Result<Self> {
Self::open_shared(device, serialized.into())
Self::open_shared(device, std::sync::Arc::new(serialized))
}

pub(crate) fn open_shared(device: &ID3D12Device, serialized: std::sync::Arc<[u8]>) -> Result<Self> {
pub(crate) fn open_shared(device: &ID3D12Device, serialized: std::sync::Arc<Vec<u8>>) -> Result<Self> {
let device: ID3D12Device1 = device.cast()?;
let library = if serialized.is_empty() {
let mut library = std::ptr::null_mut();
Expand Down Expand Up @@ -368,6 +369,28 @@ void main() {{ output.Store(0, {n}); }}
);
}

#[test]
fn reopened_library_retains_the_original_file_buffer() {
let Some((pipelines, dxil, root)) = fixture() else { return };
let desc = compute_desc(&dxil, &root);
let pso = unsafe { pipelines.dev.CreateComputePipelineState::<ID3D12PipelineState>(&desc) }
.expect("compute pipeline");
let library = NativeLibrary::empty(&pipelines.dev).expect("empty pipeline library");
library.store("shared", &pso).expect("store pipeline");
let bytes = library.serialize().expect("serialize pipeline library");
let original = bytes.as_ptr();
let reopened = NativeLibrary::open(&pipelines.dev, bytes).expect("reopen pipeline library");
assert_eq!(reopened._serialized.as_ptr(), original, "retain the original allocation");
let shared = reopened._serialized.clone();
let second = NativeLibrary::open_shared(&pipelines.dev, shared.clone()).expect("second owner");
assert_eq!(second._serialized.as_ptr(), original);
drop(shared);
drop(reopened);
drop(library);
second.load_compute("shared", &desc).expect("bytes survive the other owners");
drop_desc(desc);
}

#[test]
fn truncated_pipeline_library_is_rejected() {
let Some((pipelines, dxil, root)) = fixture() else { return };
Expand Down
18 changes: 9 additions & 9 deletions crates/xcl/src/shaders/warm_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ struct Receipt {

#[allow(dead_code)] // Consumed by the stacked lazy runtime integration.
pub(crate) struct Ready {
pub(crate) graphics: Option<Arc<[u8]>>,
pub(crate) compute: Option<Arc<[u8]>>,
pub(crate) graphics: Option<Arc<Vec<u8>>>,
pub(crate) compute: Option<Arc<Vec<u8>>>,
}

static READY: Mutex<Option<Ready>> = Mutex::new(None);
Expand Down Expand Up @@ -66,10 +66,10 @@ fn paths(dir: &Path) -> (PathBuf, PathBuf, PathBuf) {

fn pending(path: &Path) -> PathBuf { path.with_extension("plib.pending") }

fn validate(path: &Path, expected: u64, device: &ID3D12Device) -> Option<Arc<[u8]>> {
fn validate(path: &Path, expected: u64, device: &ID3D12Device) -> Option<Arc<Vec<u8>>> {
let bytes = std::fs::read(path).ok()?;
if bytes.len() as u64 != expected { return None }
let bytes: Arc<[u8]> = bytes.into();
let bytes = Arc::new(bytes);
NativeLibrary::open_shared(device, bytes.clone()).ok()?;
Some(bytes)
}
Expand Down Expand Up @@ -112,11 +112,11 @@ pub(crate) fn publish(corpus: &Corpus, device: &ID3D12Device) -> Result<(), Stri
let counts = corpus.counts();
let graphics_pending = pending(&graphics);
let compute_pending = pending(&compute);
let graphics_ready: Option<Arc<[u8]>> = (counts.0 != 0).then(|| std::fs::read(&graphics_pending)).transpose()
.map_err(|e| format!("Reading prepared graphics library: {e}"))?.map(Into::into);
let compute_ready: Option<Arc<[u8]>> = (counts.1 != 0).then(|| std::fs::read(&compute_pending)).transpose()
.map_err(|e| format!("Reading prepared compute library: {e}"))?.map(Into::into);
let open = |bytes: &Arc<[u8]>| NativeLibrary::open_shared(device, bytes.clone());
let graphics_ready: Option<Arc<Vec<u8>>> = (counts.0 != 0).then(|| std::fs::read(&graphics_pending)).transpose()
.map_err(|e| format!("Reading prepared graphics library: {e}"))?.map(Arc::new);
let compute_ready: Option<Arc<Vec<u8>>> = (counts.1 != 0).then(|| std::fs::read(&compute_pending)).transpose()
.map_err(|e| format!("Reading prepared compute library: {e}"))?.map(Arc::new);
let open = |bytes: &Arc<Vec<u8>>| NativeLibrary::open_shared(device, bytes.clone());
graphics_ready.as_ref().map(open).transpose()
.map_err(|e| format!("Validating prepared graphics library: {e}"))?;
compute_ready.as_ref().map(open).transpose()
Expand Down
Loading