use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
pub trait PipelineCacheStore: Send + Sync {
fn load(&self, key: &str) -> Option<Vec<u8>>;
fn save(&self, key: &str, data: &[u8]) -> std::io::Result<()>;
}
impl PipelineCacheStore for PathBuf {
fn load(&self, key: &str) -> Option<Vec<u8>> {
std::fs::read(self.join(key)).ok()
}
fn save(&self, key: &str, data: &[u8]) -> std::io::Result<()> {
write_atomically(&self.join(key), data)
}
}
#[derive(Clone, Debug)]
pub struct FileStore(pub PathBuf);
impl PipelineCacheStore for FileStore {
fn load(&self, _key: &str) -> Option<Vec<u8>> {
std::fs::read(&self.0).ok()
}
fn save(&self, _key: &str, data: &[u8]) -> std::io::Result<()> {
write_atomically(&self.0, data)
}
}
fn write_atomically(path: &Path, data: &[u8]) -> std::io::Result<()> {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir)?;
}
let mut tmp = path.as_os_str().to_owned();
tmp.push(".tmp");
std::fs::write(&tmp, data)?;
std::fs::rename(&tmp, path)
}
#[derive(Clone, Debug, Default)]
pub struct MemoryStore(Arc<Mutex<HashMap<String, Vec<u8>>>>);
impl MemoryStore {
pub fn new() -> Self {
Self::default()
}
pub fn get(&self, key: &str) -> Option<Vec<u8>> {
self.lock().get(key).cloned()
}
pub fn set(&self, key: impl Into<String>, data: Vec<u8>) {
self.lock().insert(key.into(), data);
}
pub fn keys(&self) -> Vec<String> {
self.lock().keys().cloned().collect()
}
fn lock(&self) -> std::sync::MutexGuard<'_, HashMap<String, Vec<u8>>> {
self.0.lock().unwrap_or_else(|e| e.into_inner())
}
}
impl PipelineCacheStore for MemoryStore {
fn load(&self, key: &str) -> Option<Vec<u8>> {
self.get(key)
}
fn save(&self, key: &str, data: &[u8]) -> std::io::Result<()> {
self.set(key, data.to_vec());
Ok(())
}
}
pub struct CallbackStore<L, S> {
load: L,
save: S,
}
impl<L, S> CallbackStore<L, S>
where
L: Fn(&str) -> Option<Vec<u8>> + Send + Sync,
S: Fn(&str, &[u8]) -> std::io::Result<()> + Send + Sync,
{
pub fn new(load: L, save: S) -> Self {
Self { load, save }
}
}
impl<L, S> PipelineCacheStore for CallbackStore<L, S>
where
L: Fn(&str) -> Option<Vec<u8>> + Send + Sync,
S: Fn(&str, &[u8]) -> std::io::Result<()> + Send + Sync,
{
fn load(&self, key: &str) -> Option<Vec<u8>> {
(self.load)(key)
}
fn save(&self, key: &str, data: &[u8]) -> std::io::Result<()> {
(self.save)(key, data)
}
}
impl<T: PipelineCacheStore + ?Sized> PipelineCacheStore for Arc<T> {
fn load(&self, key: &str) -> Option<Vec<u8>> {
(**self).load(key)
}
fn save(&self, key: &str, data: &[u8]) -> std::io::Result<()> {
(**self).save(key, data)
}
}
#[derive(Clone)]
pub struct PipelineCache {
cache: wgpu::PipelineCache,
key: String,
store: Arc<dyn PipelineCacheStore>,
}
impl PipelineCache {
pub fn open(
device: &wgpu::Device,
adapter: &wgpu::AdapterInfo,
store: impl PipelineCacheStore + 'static,
) -> Option<Self> {
if !device.features().contains(wgpu::Features::PIPELINE_CACHE) {
return None;
}
let key = wgpu::util::pipeline_cache_key(adapter)?;
let data = store.load(&key);
let cache = unsafe {
device.create_pipeline_cache(&wgpu::PipelineCacheDescriptor {
label: Some("threers pipeline cache"),
data: data.as_deref(),
fallback: true,
})
};
Some(Self {
cache,
key,
store: Arc::new(store),
})
}
pub fn in_dir(
device: &wgpu::Device,
adapter: &wgpu::AdapterInfo,
dir: impl Into<PathBuf>,
) -> Option<Self> {
Self::open(device, adapter, dir.into())
}
pub fn in_memory(device: &wgpu::Device, adapter: &wgpu::AdapterInfo) -> Option<Self> {
Self::open(device, adapter, MemoryStore::new())
}
pub fn from_bytes(
device: &wgpu::Device,
adapter: &wgpu::AdapterInfo,
bytes: Option<&[u8]>,
) -> Option<Self> {
let store = MemoryStore::new();
if let (Some(bytes), Some(key)) = (bytes, wgpu::util::pipeline_cache_key(adapter)) {
store.set(key, bytes.to_vec());
}
Self::open(device, adapter, store)
}
pub fn wgpu(&self) -> &wgpu::PipelineCache {
&self.cache
}
pub fn key(&self) -> &str {
&self.key
}
pub fn data(&self) -> Option<Vec<u8>> {
self.cache.get_data()
}
pub fn save(&self) -> std::io::Result<bool> {
let Some(data) = self.cache.get_data() else {
return Ok(false);
};
self.store.save(&self.key, &data)?;
Ok(true)
}
}
impl std::fmt::Debug for PipelineCache {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PipelineCache")
.field("key", &self.key)
.finish_non_exhaustive()
}
}
pub trait IntoPipelineCache {
fn into_pipeline_cache(self) -> Option<wgpu::PipelineCache>;
}
impl IntoPipelineCache for wgpu::PipelineCache {
fn into_pipeline_cache(self) -> Option<wgpu::PipelineCache> {
Some(self)
}
}
impl IntoPipelineCache for Option<wgpu::PipelineCache> {
fn into_pipeline_cache(self) -> Option<wgpu::PipelineCache> {
self
}
}
impl IntoPipelineCache for &PipelineCache {
fn into_pipeline_cache(self) -> Option<wgpu::PipelineCache> {
Some(self.cache.clone())
}
}
impl IntoPipelineCache for PipelineCache {
fn into_pipeline_cache(self) -> Option<wgpu::PipelineCache> {
Some(self.cache)
}
}
impl IntoPipelineCache for Option<&PipelineCache> {
fn into_pipeline_cache(self) -> Option<wgpu::PipelineCache> {
self.map(|c| c.cache.clone())
}
}
impl IntoPipelineCache for &Option<PipelineCache> {
fn into_pipeline_cache(self) -> Option<wgpu::PipelineCache> {
self.as_ref().map(|c| c.cache.clone())
}
}
impl IntoPipelineCache for Option<PipelineCache> {
fn into_pipeline_cache(self) -> Option<wgpu::PipelineCache> {
self.map(|c| c.cache)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_directory_store_round_trips_and_leaves_no_temporary() {
let dir = std::env::temp_dir().join(format!("threers-store-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(dir.load("k"), None);
dir.save("k", b"one").unwrap();
dir.save("k", b"two").unwrap();
assert_eq!(dir.load("k").as_deref(), Some(&b"two"[..]));
let names: Vec<_> = std::fs::read_dir(&dir)
.unwrap()
.map(|e| e.unwrap().file_name())
.collect();
assert_eq!(names, ["k"]);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_file_store_ignores_the_key() {
let path = std::env::temp_dir().join(format!(
"threers-file-store-{}/nested/cache.bin",
std::process::id()
));
let store = FileStore(path.clone());
store.save("a", b"bytes").unwrap();
assert_eq!(store.load("b").as_deref(), Some(&b"bytes"[..]));
let _ = std::fs::remove_dir_all(path.parent().unwrap().parent().unwrap());
}
#[test]
fn memory_store_clones_share_entries() {
let a = MemoryStore::new();
let b = a.clone();
a.save("k", b"x").unwrap();
assert_eq!(b.get("k").as_deref(), Some(&b"x"[..]));
assert_eq!(b.keys(), ["k"]);
}
#[test]
fn a_callback_store_calls_through() {
let seen = Arc::new(Mutex::new(Vec::new()));
let log = seen.clone();
let store = CallbackStore::new(
|key: &str| (key == "k").then(|| b"hit".to_vec()),
move |key: &str, data: &[u8]| {
log.lock().unwrap().push((key.to_owned(), data.to_vec()));
Ok(())
},
);
assert_eq!(store.load("k").as_deref(), Some(&b"hit"[..]));
assert_eq!(store.load("other"), None);
store.save("k", b"d").unwrap();
assert_eq!(*seen.lock().unwrap(), [("k".to_owned(), b"d".to_vec())]);
}
}