pub struct PipelineCache { /* private fields */ }Expand description
Single-owner cache of compiled wgpu::ComputePipelines.
Pipelines are stored behind Arcs so that callers can hold them
independently of the cache lifetime.
PipelineCache is not Sync on its own. For concurrent access,
use the SharedPipelineCache type alias together with
new_shared_pipeline_cache.
§Device-lost recovery
After a GPU device-lost event, call PipelineCache::clear before
compiling new pipelines; reusing stale pipelines from a previous device
causes undefined behaviour in WGPU.
Implementations§
Source§impl PipelineCache
impl PipelineCache
Sourcepub fn get_or_insert_with<F, E>(
&mut self,
key: PipelineCacheKey,
factory: F,
) -> Result<Arc<ComputePipeline>, E>
pub fn get_or_insert_with<F, E>( &mut self, key: PipelineCacheKey, factory: F, ) -> Result<Arc<ComputePipeline>, E>
Return the cached pipeline for key, or compile it via factory and
cache the result.
factory is invoked only on a cache miss. If factory returns
Err(e), the error is propagated and nothing is stored in the cache.
§Errors
Propagates any error returned by factory.
§Examples
use oxigdal_gpu::pipeline_cache::{PipelineCache, PipelineCacheKey};
use oxigdal_gpu::error::GpuResult;
fn build_pipeline(
cache: &mut PipelineCache,
device: &wgpu::Device,
shader_source: &str,
entry: &str,
) -> GpuResult<std::sync::Arc<wgpu::ComputePipeline>> {
let key = PipelineCacheKey::new(shader_source, entry, "r-w");
cache.get_or_insert_with(key, || {
// expensive compile — called only on miss
todo!("compile shader and create pipeline")
})
}Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Evict all cached pipelines.
Must be called after a GPU device-lost event and before building new pipelines on the replacement device.
Sourcepub fn evict(&mut self, key: &PipelineCacheKey) -> bool
pub fn evict(&mut self, key: &PipelineCacheKey) -> bool
Evict a single pipeline by key.
Returns true if the key was present (and thus removed), false if it
was already absent.
Sourcepub fn keys(&self) -> impl Iterator<Item = &PipelineCacheKey>
pub fn keys(&self) -> impl Iterator<Item = &PipelineCacheKey>
Returns an iterator over all cached keys in arbitrary order.
Useful for diagnostics or implementing external LRU eviction policies.
Sourcepub fn retain<F>(&mut self, predicate: F)
pub fn retain<F>(&mut self, predicate: F)
Retain only the entries for which predicate returns true.
This allows bulk conditional eviction, for example to remove all pipelines belonging to a specific shader entry point.
use oxigdal_gpu::pipeline_cache::{PipelineCache, PipelineCacheKey};
let mut cache = PipelineCache::new();
// … populate cache …
// Evict every "hillshade" pipeline regardless of layout tag.
cache.retain(|key| key.entry_point != "hillshade");