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