wgpu/api/
compute_pipeline.rs

1use std::{sync::Arc, thread};
2
3use crate::*;
4
5/// Handle to a compute pipeline.
6///
7/// A `ComputePipeline` object represents a compute pipeline and its single shader stage.
8/// It can be created with [`Device::create_compute_pipeline`].
9///
10/// Corresponds to [WebGPU `GPUComputePipeline`](https://gpuweb.github.io/gpuweb/#compute-pipeline).
11#[derive(Debug)]
12pub struct ComputePipeline {
13    pub(crate) context: Arc<C>,
14    pub(crate) data: Box<Data>,
15}
16#[cfg(send_sync)]
17static_assertions::assert_impl_all!(ComputePipeline: Send, Sync);
18
19super::impl_partialeq_eq_hash!(ComputePipeline);
20
21impl ComputePipeline {
22    /// Get an object representing the bind group layout at a given index.
23    ///
24    /// If this pipeline was created with a [default layout][ComputePipelineDescriptor::layout],
25    /// then bind groups created with the returned `BindGroupLayout` can only be used with this
26    /// pipeline.
27    ///
28    /// This method will raise a validation error if there is no bind group layout at `index`.
29    pub fn get_bind_group_layout(&self, index: u32) -> BindGroupLayout {
30        let context = Arc::clone(&self.context);
31        let data = self
32            .context
33            .compute_pipeline_get_bind_group_layout(self.data.as_ref(), index);
34        BindGroupLayout { context, data }
35    }
36}
37
38impl Drop for ComputePipeline {
39    fn drop(&mut self) {
40        if !thread::panicking() {
41            self.context.compute_pipeline_drop(self.data.as_ref());
42        }
43    }
44}
45
46/// Describes a compute pipeline.
47///
48/// For use with [`Device::create_compute_pipeline`].
49///
50/// Corresponds to [WebGPU `GPUComputePipelineDescriptor`](
51/// https://gpuweb.github.io/gpuweb/#dictdef-gpucomputepipelinedescriptor).
52#[derive(Clone, Debug)]
53pub struct ComputePipelineDescriptor<'a> {
54    /// Debug label of the pipeline. This will show up in graphics debuggers for easy identification.
55    pub label: Label<'a>,
56    /// The layout of bind groups for this pipeline.
57    ///
58    /// If this is set, then [`Device::create_compute_pipeline`] will raise a validation error if
59    /// the layout doesn't match what the shader module(s) expect.
60    ///
61    /// Using the same [`PipelineLayout`] for many [`RenderPipeline`] or [`ComputePipeline`]
62    /// pipelines guarantees that you don't have to rebind any resources when switching between
63    /// those pipelines.
64    ///
65    /// ## Default pipeline layout
66    ///
67    /// If `layout` is `None`, then the pipeline has a [default layout] created and used instead.
68    /// The default layout is deduced from the shader modules.
69    ///
70    /// You can use [`ComputePipeline::get_bind_group_layout`] to create bind groups for use with
71    /// the default layout. However, these bind groups cannot be used with any other pipelines. This
72    /// is convenient for simple pipelines, but using an explicit layout is recommended in most
73    /// cases.
74    ///
75    /// [default layout]: https://www.w3.org/TR/webgpu/#default-pipeline-layout
76    pub layout: Option<&'a PipelineLayout>,
77    /// The compiled shader module for this stage.
78    pub module: &'a ShaderModule,
79    /// The name of the entry point in the compiled shader to use.
80    ///
81    /// If [`Some`], there must be a compute shader entry point with this name in `module`.
82    /// Otherwise, expect exactly one compute shader entry point in `module`, which will be
83    /// selected.
84    // NOTE: keep phrasing in sync. with `FragmentState::entry_point`
85    // NOTE: keep phrasing in sync. with `VertexState::entry_point`
86    pub entry_point: Option<&'a str>,
87    /// Advanced options for when this pipeline is compiled
88    ///
89    /// This implements `Default`, and for most users can be set to `Default::default()`
90    pub compilation_options: PipelineCompilationOptions<'a>,
91    /// The pipeline cache to use when creating this pipeline.
92    pub cache: Option<&'a PipelineCache>,
93}
94#[cfg(send_sync)]
95static_assertions::assert_impl_all!(ComputePipelineDescriptor<'_>: Send, Sync);