phobos 0.10.0

Fast, powerful Vulkan abstraction library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! The pipeline cache stores pipelines and deletes them if appropriate.

use std::collections::HashMap;
use std::ffi::CString;
use std::sync::{Arc, RwLock};

use anyhow::Result;
use ash::vk;

use crate::{
    Allocator, ComputePipelineCreateInfo, DefaultAllocator, Device, Error, PipelineCreateInfo,
};
use crate::core::device::ExtensionID;
use crate::pipeline::{ComputePipeline, Pipeline, PipelineType, RayTracingPipeline};
use crate::pipeline::create_info::PipelineRenderingInfo;
use crate::pipeline::pipeline_layout::PipelineLayout;
use crate::pipeline::raytracing::{RayTracingPipelineCreateInfo, ShaderBindingTable, ShaderGroup};
use crate::pipeline::set_layout::DescriptorSetLayout;
use crate::pipeline::shader::Shader;
use crate::util::cache::{Cache, Resource, ResourceKey};

use super::shader_reflection::{build_pipeline_layout, reflect_shaders, ReflectionInfo};

#[derive(Debug)]
struct PipelineEntry<P>
where
    P: std::fmt::Debug, {
    pub info: P,
    #[cfg(feature = "shader-reflection")]
    #[allow(dead_code)]
    pub reflection: ReflectionInfo,
}

#[derive(Debug)]
struct PipelineCacheInner<A: Allocator> {
    allocator: A,
    shaders: Cache<Shader>,
    set_layouts: Cache<DescriptorSetLayout>,
    pipeline_layouts: Cache<PipelineLayout>,
    pipelines: Cache<Pipeline>,
    compute_pipelines: Cache<ComputePipeline>,
    raytracing_pipelines: Cache<RayTracingPipeline<A>>,
    pipeline_infos: HashMap<String, PipelineEntry<PipelineCreateInfo>>,
    compute_pipeline_infos: HashMap<String, PipelineEntry<ComputePipelineCreateInfo>>,
    raytracing_pipeline_infos: HashMap<String, PipelineEntry<RayTracingPipelineCreateInfo>>,
}

/// The main pipeline cache struct. This stores all named pipelines and shaders.
/// To create a pipeline you should obtain a pipeline create info, and then register it using
/// [`PipelineCache::create_named_pipeline`].
///
/// This struct is `Clone`, `Send` and `Sync`.
///
/// This should generally be accessed through a global [`ResourcePool`](crate::pool::ResourcePool)
/// # Example usage
/// ```
/// use phobos::prelude::*;
/// let mut cache = PipelineCache::new(device.clone(), allocator.clone())?;
/// let pci = PipelineBuilder::new("my_pipeline")
///     // ... options for pipeline creation
///     .build();
/// cache.create_named_pipeline(pci)?;
/// ```
#[derive(Debug, Clone)]
pub struct PipelineCache<A: Allocator = DefaultAllocator> {
    inner: Arc<RwLock<PipelineCacheInner<A>>>,
}

// SAFETY: Inner state is wrapped in an Arc<RwLock<T>>, and all pointers inside point to
// internal data
unsafe impl<A: Allocator> Send for PipelineCache<A> {}

// SAFETY: Inner state is wrapped in an Arc<RwLock<T>>, and all pointers inside point to
// internal data
unsafe impl<A: Allocator> Sync for PipelineCache<A> {}

macro_rules! require_extension {
    ($pci:ident, $device:ident, $state:expr, $ext:expr) => {
        if $pci.dynamic_states.contains(&$state) && !$device.is_extension_enabled($ext) {
            error!(
                "Pipeline {} requested dynamic state {:?}, but corresponding extension {:?} is not enabled. Maybe it is unsupported on the current device?",
                $pci.name, $state, $ext
            );
        }
    };
}

/// Check if dynamic states are supported by the enabled extension set
fn verify_valid_dynamic_states(device: &Device, pci: &PipelineCreateInfo) {
    require_extension!(
        pci,
        device,
        vk::DynamicState::POLYGON_MODE_EXT,
        ExtensionID::ExtendedDynamicState3
    );
}

impl ResourceKey for PipelineCreateInfo {
    /// Whether this resource is persistent.
    fn persistent(&self) -> bool {
        false
    }
}

impl Resource for Pipeline {
    type Key = PipelineCreateInfo;
    type ExtraParams<'a> = (
        &'a mut Cache<Shader>,
        &'a mut Cache<PipelineLayout>,
        &'a mut Cache<DescriptorSetLayout>,
    );
    const MAX_TIME_TO_LIVE: u32 = 8;

    fn create(device: Device, info: &Self::Key, params: Self::ExtraParams<'_>) -> Result<Self> {
        let (shaders, pipeline_layouts, set_layouts) = params;
        let layout = pipeline_layouts.get_or_create(&info.layout, set_layouts)?;
        let mut pci = info.to_vk(unsafe { layout.handle() });

        verify_valid_dynamic_states(&device, info);

        // Set shader create info
        let entry = CString::new("main")?;
        let shader_info: Vec<_> = info
            .shaders
            .iter()
            .map(|shader| -> vk::PipelineShaderStageCreateInfo {
                vk::PipelineShaderStageCreateInfo::builder()
                    .name(&entry)
                    .stage(shader.stage())
                    .module(unsafe { shaders.get_or_create(shader, ()).unwrap().handle() })
                    .build()
            })
            .collect();
        pci.stage_count = shader_info.len() as u32;
        pci.p_stages = shader_info.as_ptr();

        let handle = unsafe {
            device
                .create_graphics_pipelines(
                    vk::PipelineCache::null(),
                    std::slice::from_ref(&pci),
                    None,
                )
                .map_err(|(_, e)| Error::VkError(e))?
                .first()
                .cloned()
                .unwrap()
        };

        #[cfg(feature = "log-objects")]
        trace!("Created new VkPipeline (graphics) {handle:p}");

        unsafe {
            Ok(Self {
                device: device.clone(),
                handle,
                layout: layout.handle(),
                set_layouts: layout.set_layouts().to_vec(),
            })
        }
    }
}

impl Drop for Pipeline {
    fn drop(&mut self) {
        #[cfg(feature = "log-objects")]
        trace!("Destroying VkPipeline (graphics) {:p}", self.handle);
        unsafe {
            self.device.destroy_pipeline(self.handle, None);
        }
    }
}

impl ResourceKey for ComputePipelineCreateInfo {
    /// Whether this pipeline is persistent.
    fn persistent(&self) -> bool {
        self.persistent
    }
}

impl Resource for ComputePipeline {
    type Key = ComputePipelineCreateInfo;
    type ExtraParams<'a> = (
        &'a mut Cache<Shader>,
        &'a mut Cache<PipelineLayout>,
        &'a mut Cache<DescriptorSetLayout>,
    );
    const MAX_TIME_TO_LIVE: u32 = 8;

    fn create(device: Device, info: &Self::Key, params: Self::ExtraParams<'_>) -> Result<Self>
    where
        Self: Sized, {
        let (shaders, pipeline_layouts, set_layouts) = params;
        let layout = pipeline_layouts.get_or_create(&info.layout, set_layouts)?;
        let mut pci = info.to_vk(unsafe { layout.handle() });

        // Set shader create info
        let entry = CString::new("main")?;
        let shader = match &info.shader {
            None => Err(Error::Uncategorized("Compute pipeline lacks shader")),
            Some(shader) => Ok(vk::PipelineShaderStageCreateInfo::builder()
                .name(&entry)
                .stage(vk::ShaderStageFlags::COMPUTE)
                .module(unsafe { shaders.get_or_create(shader, ()).unwrap().handle() })
                .build()),
        }?;

        pci.stage = shader;

        let handle = unsafe {
            device
                .create_compute_pipelines(
                    vk::PipelineCache::null(),
                    std::slice::from_ref(&pci),
                    None,
                )
                .map_err(|(_, e)| Error::VkError(e))?
                .first()
                .cloned()
                .unwrap()
        };

        #[cfg(feature = "log-objects")]
        trace!("Created new VkPipeline (compute) {handle:p}");

        unsafe {
            Ok(Self {
                device: device.clone(),
                handle,
                layout: layout.handle(),
                set_layouts: layout.set_layouts().to_vec(),
            })
        }
    }
}

impl Drop for ComputePipeline {
    fn drop(&mut self) {
        unsafe {
            #[cfg(feature = "log-objects")]
            trace!("Destroying VkPipeline (compute) {:p}", self.handle);
            self.device.destroy_pipeline(self.handle, None);
        }
    }
}

impl ResourceKey for RayTracingPipelineCreateInfo {
    fn persistent(&self) -> bool {
        false
    }
}

impl<A: Allocator> Resource for RayTracingPipeline<A> {
    type Key = RayTracingPipelineCreateInfo;
    type ExtraParams<'a> = (
        A,
        &'a mut Cache<Shader>,
        &'a mut Cache<PipelineLayout>,
        &'a mut Cache<DescriptorSetLayout>,
    );
    const MAX_TIME_TO_LIVE: u32 = 8;

    fn create(device: Device, info: &Self::Key, params: Self::ExtraParams<'_>) -> Result<Self>
    where
        Self: Sized, {
        device.require_extension(ExtensionID::RayTracingPipeline)?;
        let (alloc, shaders, pipeline_layouts, set_layouts) = params;
        let layout = pipeline_layouts.get_or_create(&info.layout, set_layouts)?;
        let mut pci = info.to_vk(unsafe { layout.handle() });

        let entry = CString::new("main")?;
        let mut shader_indices = HashMap::new();
        let shader_info: Vec<_> = info
            .shaders
            .iter()
            .enumerate()
            .map(|(idx, shader)| -> vk::PipelineShaderStageCreateInfo {
                shader_indices.insert(shader.code_hash(), idx as u32);
                vk::PipelineShaderStageCreateInfo::builder()
                    .name(&entry)
                    .stage(shader.stage())
                    .module(unsafe { shaders.get_or_create(shader, ()).unwrap().handle() })
                    .build()
            })
            .collect();
        pci.stage_count = shader_info.len() as u32;
        pci.p_stages = shader_info.as_ptr();

        let groups = info
            .shader_groups
            .iter()
            .map(|group| {
                let (general, closest_hit, any_hit, intersection, ty) = match group {
                    ShaderGroup::RayGeneration {
                        shader,
                    } => (
                        shader.index,
                        vk::SHADER_UNUSED_KHR,
                        vk::SHADER_UNUSED_KHR,
                        vk::SHADER_UNUSED_KHR,
                        vk::RayTracingShaderGroupTypeKHR::GENERAL,
                    ),
                    ShaderGroup::RayMiss {
                        shader,
                    } => (
                        shader.index,
                        vk::SHADER_UNUSED_KHR,
                        vk::SHADER_UNUSED_KHR,
                        vk::SHADER_UNUSED_KHR,
                        vk::RayTracingShaderGroupTypeKHR::GENERAL,
                    ),
                    ShaderGroup::RayHit {
                        closest_hit,
                        any_hit,
                    } => (
                        vk::SHADER_UNUSED_KHR,
                        closest_hit
                            .map(|sh| sh.index)
                            .unwrap_or(vk::SHADER_UNUSED_KHR),
                        any_hit.map(|sh| sh.index).unwrap_or(vk::SHADER_UNUSED_KHR),
                        vk::SHADER_UNUSED_KHR,
                        vk::RayTracingShaderGroupTypeKHR::TRIANGLES_HIT_GROUP,
                    ),
                    ShaderGroup::Callable {
                        shader,
                    } => (
                        shader.index,
                        vk::SHADER_UNUSED_KHR,
                        vk::SHADER_UNUSED_KHR,
                        vk::SHADER_UNUSED_KHR,
                        vk::RayTracingShaderGroupTypeKHR::GENERAL,
                    ),
                };

                vk::RayTracingShaderGroupCreateInfoKHR {
                    s_type: vk::StructureType::RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
                    p_next: std::ptr::null(),
                    ty,
                    general_shader: general,
                    closest_hit_shader: closest_hit,
                    any_hit_shader: any_hit,
                    intersection_shader: intersection,
                    p_shader_group_capture_replay_handle: std::ptr::null(),
                }
            })
            .collect::<Vec<_>>();
        pci.group_count = groups.len() as u32;
        pci.p_groups = groups.as_ptr();

        let fns = device.raytracing_pipeline().unwrap();
        let handle = unsafe {
            fns.create_ray_tracing_pipelines(
                vk::DeferredOperationKHR::null(),
                vk::PipelineCache::null(),
                std::slice::from_ref(&pci),
                None,
            )?
            .first()
            .cloned()
            .unwrap()
        };

        #[cfg(feature = "log-objects")]
        trace!("Created new VkPipeline (raytracing) {handle:p}");

        let sbt = ShaderBindingTable::new(device.clone(), alloc, handle, info)?;

        Ok(Self {
            device,
            handle,
            layout: unsafe { layout.handle() },
            set_layouts: layout.set_layouts().to_vec(),
            shader_binding_table: sbt,
        })
    }
}

impl<A: Allocator> Drop for RayTracingPipeline<A> {
    fn drop(&mut self) {
        unsafe {
            #[cfg(feature = "log-objects")]
            trace!("Destroying VkPipeline (raytracing) {:p}", self.handle);
            self.device.destroy_pipeline(self.handle, None);
        }
    }
}

impl<A: Allocator> PipelineCacheInner<A> {
    pub(crate) fn get_pipeline(
        &mut self,
        name: &str,
        rendering_info: PipelineRenderingInfo,
    ) -> Result<&Pipeline> {
        let entry = self.pipeline_infos.get_mut(name);
        let Some(entry) = entry else { return Err(anyhow::Error::from(Error::PipelineNotFound(name.to_string()))); };
        entry.info.rendering_info = rendering_info;
        entry.info.build_rendering_state();
        // Also put in queries for descriptor set layouts and pipeline layout to make sure they are not destroyed.
        for layout in &entry.info.layout.set_layouts {
            self.set_layouts.get_or_create(layout, ())?;
        }
        self.pipeline_layouts
            .get_or_create(&entry.info.layout, &mut self.set_layouts)?;
        self.pipelines.get_or_create(
            &entry.info,
            (&mut self.shaders, &mut self.pipeline_layouts, &mut self.set_layouts),
        )
    }

    pub(crate) fn get_compute_pipeline(&mut self, name: &str) -> Result<&ComputePipeline> {
        let entry = self.compute_pipeline_infos.get_mut(name);
        let Some(entry) = entry else { return Err(anyhow::Error::from(Error::PipelineNotFound(name.to_string()))); };
        // Also put in queries for descriptor set layouts and pipeline layout to make sure they are not destroyed.
        for layout in &entry.info.layout.set_layouts {
            self.set_layouts.get_or_create(layout, ())?;
        }
        self.pipeline_layouts
            .get_or_create(&entry.info.layout, &mut self.set_layouts)?;
        self.compute_pipelines.get_or_create(
            &entry.info,
            (&mut self.shaders, &mut self.pipeline_layouts, &mut self.set_layouts),
        )
    }

    pub(crate) fn get_raytracing_pipeline(&mut self, name: &str) -> Result<&RayTracingPipeline<A>> {
        let entry = self.raytracing_pipeline_infos.get_mut(name);
        let Some(entry) = entry else { return Err(anyhow::Error::from(Error::PipelineNotFound(name.to_string()))); };
        // Also put in queries for descriptor set layouts and pipeline layout to make sure they are not destroyed.
        for layout in &entry.info.layout.set_layouts {
            self.set_layouts.get_or_create(layout, ())?;
        }
        self.pipeline_layouts
            .get_or_create(&entry.info.layout, &mut self.set_layouts)?;
        self.raytracing_pipelines.get_or_create(
            &entry.info,
            (
                self.allocator.clone(),
                &mut self.shaders,
                &mut self.pipeline_layouts,
                &mut self.set_layouts,
            ),
        )
    }
}

// TODO: Maybe incorporate the vulkan pipeline cache api to improve startup times?

impl<A: Allocator> PipelineCache<A> {
    /// Create a new empty pipeline cache.
    pub fn new(device: Device, allocator: A) -> Result<Self> {
        let inner = PipelineCacheInner {
            allocator,
            shaders: Cache::new(device.clone()),
            set_layouts: Cache::new(device.clone()),
            pipeline_layouts: Cache::new(device.clone()),
            pipelines: Cache::new(device.clone()),
            compute_pipelines: Cache::new(device.clone()),
            raytracing_pipelines: Cache::new(device),
            pipeline_infos: Default::default(),
            compute_pipeline_infos: Default::default(),
            raytracing_pipeline_infos: Default::default(),
        };
        Ok(Self {
            inner: Arc::new(RwLock::new(inner)),
        })
    }

    /// Create and register a new pipeline into the cache.
    #[cfg(feature = "shader-reflection")]
    pub fn create_named_pipeline(&mut self, mut info: PipelineCreateInfo) -> Result<()> {
        let refl = reflect_shaders(info.shaders.as_slice())?;
        // Using reflection, we can allow omitting the pipeline layout field.
        info.layout = build_pipeline_layout(&refl);
        let name = info.name.clone();
        let mut inner = self.inner.write().unwrap();
        inner.pipeline_infos.insert(
            name.clone(),
            PipelineEntry {
                info,
                reflection: refl,
            },
        );
        inner
            .pipeline_infos
            .get_mut(&name)
            .unwrap()
            .info
            .build_inner();
        Ok(())
    }

    /// Create and register a new pipeline into the cache
    #[cfg(not(feature = "shader-reflection"))]
    pub fn create_named_pipeline(&mut self, mut info: PipelineCreateInfo) -> Result<()> {
        info.build_inner();
        let name = info.name.clone();
        let mut inner = self.inner.write().unwrap();
        inner.pipeline_infos.insert(
            name.clone(),
            PipelineEntry {
                info,
            },
        );
        inner
            .pipeline_infos
            .get_mut(&name)
            .unwrap()
            .info
            .build_inner();
        Ok(())
    }

    /// Create and register a new compute pipeline into the cache
    #[cfg(feature = "shader-reflection")]
    pub fn create_named_compute_pipeline(
        &mut self,
        mut info: ComputePipelineCreateInfo,
    ) -> Result<()> {
        let refl = match &info.shader {
            None => reflect_shaders(&[])?,
            Some(info) => reflect_shaders(std::slice::from_ref(info))?,
        };
        // Using reflection, we can allow omitting the pipeline layout field.
        info.layout = build_pipeline_layout(&refl);
        // If this is persistent, then also make the pipeline and descriptor set layouts persistent
        if info.persistent {
            info.layout.persistent = true;
            info.layout.set_layouts.iter_mut().for_each(|set_layout| {
                set_layout.persistent = true;
            });
            match &mut info.shader {
                None => {}
                Some(shader) => {
                    shader.persistent = true;
                }
            }
        }
        let name = info.name.clone();
        let mut inner = self.inner.write().unwrap();
        inner.compute_pipeline_infos.insert(
            name,
            PipelineEntry {
                info,
                reflection: refl,
            },
        );
        Ok(())
    }

    /// Create and register a new compute pipeline into the cache
    #[cfg(not(feature = "shader-reflection"))]
    pub fn create_named_compute_pipeline(
        &mut self,
        mut info: ComputePipelineCreateInfo,
    ) -> Result<()> {
        let name = info.name.clone();
        let mut inner = self.inner.write().unwrap();
        inner.compute_pipeline_infos.insert(
            name,
            PipelineEntry {
                info,
            },
        );
        Ok(())
    }

    /// Create and register a new raytracing pipeline into the cache
    #[cfg(feature = "shader-reflection")]
    pub fn create_named_raytracing_pipeline(
        &mut self,
        mut info: RayTracingPipelineCreateInfo,
    ) -> Result<()> {
        let refl = reflect_shaders(info.shaders.as_slice())?;
        // Using reflection, we can allow omitting the pipeline layout field.
        info.layout = build_pipeline_layout(&refl);

        let name = info.name.clone();
        let mut inner = self.inner.write().unwrap();
        inner.raytracing_pipeline_infos.insert(
            name,
            PipelineEntry {
                info,
                reflection: refl,
            },
        );
        Ok(())
    }

    /// Create and register a new raytracing pipeline into the cache
    #[cfg(not(feature = "shader-reflection"))]
    pub fn create_named_raytracing_pipeline(
        &mut self,
        mut info: RayTracingPipelineCreateInfo,
    ) -> Result<()> {
        let name = info.name.clone();
        let mut inner = self.inner.write().unwrap();
        inner.raytracing_pipeline_infos.insert(
            name,
            PipelineEntry {
                info,
            },
        );
        Ok(())
    }

    /// Get the pipeline create info associated with a pipeline
    /// # Errors
    /// Returns None if the pipeline was not found in the cache.
    pub fn pipeline_info(&self, name: &str) -> Option<PipelineCreateInfo> {
        self.inner
            .read()
            .unwrap()
            .pipeline_infos
            .get(name)
            .map(|entry| entry.info.clone())
    }

    /// Get the pipeline create info associated with a compute pipeline
    /// # Errors
    /// Returns None if the pipeline was not found in the cache.
    pub fn compute_pipeline_info(&self, name: &str) -> Option<ComputePipelineCreateInfo> {
        self.inner
            .read()
            .unwrap()
            .compute_pipeline_infos
            .get(name)
            .map(|entry| entry.info.clone())
    }

    /// Get the pipeline create info associated with a raytracing pipeline
    /// # Errors
    /// Returns None if the pipeline was not found in the cache.
    pub fn raytracing_pipeline_info(&self, name: &str) -> Option<RayTracingPipelineCreateInfo> {
        self.inner
            .read()
            .unwrap()
            .raytracing_pipeline_infos
            .get(name)
            .map(|entry| entry.info.clone())
    }

    /// Returns the pipeline type of a pipeline, or None if the pipeline does not exist.
    pub fn pipeline_type(&self, name: &str) -> Option<PipelineType> {
        let inner = self.inner.read().unwrap();
        if inner.pipeline_infos.contains_key(name) {
            Some(PipelineType::Graphics)
        } else if inner.compute_pipeline_infos.contains_key(name) {
            Some(PipelineType::Compute)
        } else if inner.raytracing_pipeline_infos.contains_key(name) {
            Some(PipelineType::RayTracing)
        } else {
            None
        }
    }

    /// Obtain a pipeline from the cache and do some work with it.
    /// # Errors
    /// - This function can fail if the requested pipeline does not exist in the cache
    /// - This function can fail if allocating the pipeline fails.
    pub(crate) fn with_pipeline<F: FnOnce(&Pipeline) -> Result<()>>(
        &self,
        name: &str,
        rendering_info: PipelineRenderingInfo,
        f: F,
    ) -> Result<()> {
        let mut inner = self.inner.write().unwrap();
        let pipeline = inner.get_pipeline(name, rendering_info)?;
        f(pipeline)
    }

    /// Obtain a compute pipeline from the cache and do some work with it.
    /// # Errors
    /// - This function can fail if the requested pipeline does not exist in the cache
    /// - This function can fail if allocating the pipeline fails.
    pub(crate) fn with_compute_pipeline<F: FnOnce(&ComputePipeline) -> Result<()>>(
        &self,
        name: &str,
        f: F,
    ) -> Result<()> {
        let mut inner = self.inner.write().unwrap();
        let pipeline = inner.get_compute_pipeline(name)?;
        f(pipeline)
    }

    /// Obtain a raytracing pipeline from the cache and do some work with it.
    /// # Errors
    /// - This function can fail if the requested pipeline does not exist in the cache
    /// - This function can fail if allocating the pipeline fails.
    pub(crate) fn with_raytracing_pipeline<F: FnOnce(&RayTracingPipeline<A>) -> Result<()>>(
        &self,
        name: &str,
        f: F,
    ) -> Result<()> {
        let mut inner = self.inner.write().unwrap();
        let pipeline = inner.get_raytracing_pipeline(name)?;
        f(pipeline)
    }

    /// Advance cache resource time to live so resources that have not been used in a while can be cleaned up
    pub fn next_frame(&self) {
        let mut inner = self.inner.write().unwrap();
        inner.pipelines.next_frame();
        inner.compute_pipelines.next_frame();
        inner.raytracing_pipelines.next_frame();
        inner.pipeline_layouts.next_frame();
        inner.shaders.next_frame();
        inner.set_layouts.next_frame();
    }
}