1use std::{cell::RefCell, sync::Arc};
2
3use crate::{
4 bind_group::{BindGroup, BindGroupLayout},
5 compute_pipeline::ComputePipelineCacheKey,
6 keyed_cache::KeyedCache,
7 pipeline_layout::PipelineLayout,
8 render_pipeline::RenderPipelineCacheKey,
9 sampler::Sampler,
10 texture::Texture,
11};
12
13pub(crate) struct Caches {
14 pub bind_group_layout_cache: RefCell<KeyedCache<BindGroupLayout, wgpu::BindGroupLayout>>,
15 pub bind_group_cache: RefCell<KeyedCache<BindGroup, wgpu::BindGroup>>,
16 pub texture_view_cache: RefCell<KeyedCache<Texture, wgpu::TextureView>>,
17 pub sampler_cache: RefCell<KeyedCache<Sampler, wgpu::Sampler>>,
18 pub pipeline_layout_cache: RefCell<KeyedCache<PipelineLayout, wgpu::PipelineLayout>>,
19 pub render_pipeline_cache: RefCell<KeyedCache<RenderPipelineCacheKey, wgpu::RenderPipeline>>,
20 pub compute_pipeline_cache: RefCell<KeyedCache<ComputePipelineCacheKey, wgpu::ComputePipeline>>,
21}
22
23impl Caches {
24 pub(crate) fn age(&self) {
25 self.bind_group_layout_cache.borrow_mut().age();
26 self.bind_group_cache.borrow_mut().age();
27 self.texture_view_cache.borrow_mut().age();
28 self.sampler_cache.borrow_mut().age();
29 self.pipeline_layout_cache.borrow_mut().age();
30 self.render_pipeline_cache.borrow_mut().age();
31 self.compute_pipeline_cache.borrow_mut().age();
32 }
33}
34
35#[derive(Clone)]
37pub struct Context {
38 pub(crate) device: wgpu::Device,
39 pub(crate) queue: wgpu::Queue,
40 pub(crate) caches: Arc<Caches>,
41}
42
43impl Context {
44 pub fn new(device: wgpu::Device, queue: wgpu::Queue) -> Self {
46 let caches = Caches {
47 bind_group_layout_cache: RefCell::new(KeyedCache::new()),
48 bind_group_cache: RefCell::new(KeyedCache::new()),
49 texture_view_cache: RefCell::new(KeyedCache::new()),
50 sampler_cache: RefCell::new(KeyedCache::new()),
51 pipeline_layout_cache: RefCell::new(KeyedCache::new()),
52 render_pipeline_cache: RefCell::new(KeyedCache::new()),
53 compute_pipeline_cache: RefCell::new(KeyedCache::new()),
54 };
55
56 Self {
57 device,
58 queue,
59 caches: Arc::new(caches),
60 }
61 }
62
63 pub fn device(&self) -> &wgpu::Device {
64 &self.device
65 }
66
67 pub fn queue(&self) -> &wgpu::Queue {
68 &self.queue
69 }
70
71 pub(crate) fn caches(&self) -> &Caches {
72 &self.caches
73 }
74}