diff --git a/README.md b/README.md index d55abf4300..fac9ca75aa 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/crates/xcl/src/shaders/pipeline_library.rs b/crates/xcl/src/shaders/pipeline_library.rs index 470de7fde0..0aa04b81d7 100644 --- a/crates/xcl/src/shaders/pipeline_library.rs +++ b/crates/xcl/src/shaders/pipeline_library.rs @@ -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>, } impl NativeLibrary { @@ -177,14 +178,14 @@ impl NativeLibrary { } pub(crate) fn empty(device: &ID3D12Device) -> Result { - 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) -> Result { - 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 { + pub(crate) fn open_shared(device: &ID3D12Device, serialized: std::sync::Arc>) -> Result { let device: ID3D12Device1 = device.cast()?; let library = if serialized.is_empty() { let mut library = std::ptr::null_mut(); @@ -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::(&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 }; diff --git a/crates/xcl/src/shaders/warm_library.rs b/crates/xcl/src/shaders/warm_library.rs index 56c5993de8..f70bc4d39e 100644 --- a/crates/xcl/src/shaders/warm_library.rs +++ b/crates/xcl/src/shaders/warm_library.rs @@ -31,8 +31,8 @@ struct Receipt { #[allow(dead_code)] // Consumed by the stacked lazy runtime integration. pub(crate) struct Ready { - pub(crate) graphics: Option>, - pub(crate) compute: Option>, + pub(crate) graphics: Option>>, + pub(crate) compute: Option>>, } static READY: Mutex> = Mutex::new(None); @@ -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> { +fn validate(path: &Path, expected: u64, device: &ID3D12Device) -> Option>> { 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) } @@ -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> = (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> = (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>> = (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>> = (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>| 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()