Skip to content

Commit

Permalink
Keep using Vec for pipeline cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed May 8, 2024
1 parent dbe0b64 commit f7e46e0
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use wgpu::{
};

use std::borrow::Cow;
use std::collections::HashMap;
use std::mem;
use std::num::NonZeroU64;
use std::ops::Deref;
Expand All @@ -29,7 +28,12 @@ struct Inner {
uniforms_layout: BindGroupLayout,
pipeline_layout: PipelineLayout,
cache: RwLock<
HashMap<(TextureFormat, MultisampleState, Option<DepthStencilState>), Arc<RenderPipeline>>,
Vec<(
TextureFormat,
MultisampleState,
Option<DepthStencilState>,
Arc<RenderPipeline>,
)>,
>,
}

Expand Down Expand Up @@ -146,7 +150,7 @@ impl Pipeline {
uniforms_layout,
atlas_layout,
pipeline_layout,
cache: RwLock::new(HashMap::new()),
cache: RwLock::new(Vec::new()),
}))
}

Expand Down Expand Up @@ -202,12 +206,14 @@ impl Pipeline {
..
} = self.0.deref();

let mut cache = cache.write().expect("Write pipeline cache");

cache
.write()
.expect("Write to pipeline cache")
.entry((format, multisample, depth_stencil))
.or_insert_with_key(|(format, multisample, depth_stencil)| {
Arc::new(device.create_render_pipeline(&RenderPipelineDescriptor {
.iter()
.find(|(fmt, ms, ds, _)| fmt == &format && ms == &multisample && ds == &depth_stencil)
.map(|(_, _, _, p)| Arc::clone(p))
.unwrap_or_else(|| {
let pipeline = Arc::new(device.create_render_pipeline(&RenderPipelineDescriptor {
label: Some("glyphon pipeline"),
layout: Some(pipeline_layout),
vertex: VertexState {
Expand All @@ -220,17 +226,21 @@ impl Pipeline {
module: shader,
entry_point: "fs_main",
targets: &[Some(ColorTargetState {
format: *format,
format,
blend: Some(BlendState::ALPHA_BLENDING),
write_mask: ColorWrites::default(),
})],
compilation_options: PipelineCompilationOptions::default(),
}),
primitive: PrimitiveState::default(),
depth_stencil: depth_stencil.clone(),
multisample: *multisample,
multisample,
multiview: None,
}))
}));

cache.push((format, multisample, depth_stencil, pipeline.clone()));

pipeline
})
.clone()
}
Expand Down

0 comments on commit f7e46e0

Please sign in to comment.