wgpu/api/
common_pipeline.rs

1use std::collections::HashMap;
2
3use crate::*;
4
5#[derive(Clone, Debug)]
6/// Advanced options for use when a pipeline is compiled
7///
8/// This implements `Default`, and for most users can be set to `Default::default()`
9pub struct PipelineCompilationOptions<'a> {
10    /// Specifies the values of pipeline-overridable constants in the shader module.
11    ///
12    /// If an `@id` attribute was specified on the declaration,
13    /// the key must be the pipeline constant ID as a decimal ASCII number; if not,
14    /// the key must be the constant's identifier name.
15    ///
16    /// The value may represent any of WGSL's concrete scalar types.
17    pub constants: &'a HashMap<String, f64>,
18    /// Whether workgroup scoped memory will be initialized with zero values for this stage.
19    ///
20    /// This is required by the WebGPU spec, but may have overhead which can be avoided
21    /// for cross-platform applications
22    pub zero_initialize_workgroup_memory: bool,
23}
24
25impl<'a> Default for PipelineCompilationOptions<'a> {
26    fn default() -> Self {
27        // HashMap doesn't have a const constructor, due to the use of RandomState
28        // This does introduce some synchronisation costs, but these should be minor,
29        // and might be cheaper than the alternative of getting new random state
30        static DEFAULT_CONSTANTS: std::sync::OnceLock<HashMap<String, f64>> =
31            std::sync::OnceLock::new();
32        let constants = DEFAULT_CONSTANTS.get_or_init(Default::default);
33        Self {
34            constants,
35            zero_initialize_workgroup_memory: true,
36        }
37    }
38}
39
40/// Describes a pipeline cache, which allows reusing compilation work
41/// between program runs.
42///
43/// For use with [`Device::create_pipeline_cache`]
44///
45/// This type is unique to the Rust API of `wgpu`.
46#[derive(Clone, Debug)]
47pub struct PipelineCacheDescriptor<'a> {
48    /// Debug label of the pipeline cache. This might show up in some logs from `wgpu`
49    pub label: Label<'a>,
50    /// The data used to initialise the cache initialise
51    ///
52    /// # Safety
53    ///
54    /// This data must have been provided from a previous call to
55    /// [`PipelineCache::get_data`], if not `None`
56    pub data: Option<&'a [u8]>,
57    /// Whether to create a cache without data when the provided data
58    /// is invalid.
59    ///
60    /// Recommended to set to true
61    pub fallback: bool,
62}
63#[cfg(send_sync)]
64static_assertions::assert_impl_all!(PipelineCacheDescriptor<'_>: Send, Sync);