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/// Describes a compute pipeline.
39///
40/// For use with [`Device::create_compute_pipeline`].
41///
42/// Corresponds to [WebGPU `GPUComputePipelineDescriptor`](
43/// https://gpuweb.github.io/gpuweb/#dictdef-gpucomputepipelinedescriptor).
44#[derive(Clone, Debug)]
45pub struct ComputePipelineDescriptor<'a> {
46 /// Debug label of the pipeline. This will show up in graphics debuggers for easy identification.
47 pub label: Label<'a>,
48 /// The layout of bind groups for this pipeline.
49 ///
50 /// If this is set, then [`Device::create_compute_pipeline`] will raise a validation error if
51 /// the layout doesn't match what the shader module(s) expect.
52 ///
53 /// Using the same [`PipelineLayout`] for many [`RenderPipeline`] or [`ComputePipeline`]
54 /// pipelines guarantees that you don't have to rebind any resources when switching between
55 /// those pipelines.
56 ///
57 /// ## Default pipeline layout
58 ///
59 /// If `layout` is `None`, then the pipeline has a [default layout] created and used instead.
60 /// The default layout is deduced from the shader modules.
61 ///
62 /// You can use [`ComputePipeline::get_bind_group_layout`] to create bind groups for use with
63 /// the default layout. However, these bind groups cannot be used with any other pipelines. This
64 /// is convenient for simple pipelines, but using an explicit layout is recommended in most
65 /// cases.
66 ///
67 /// [default layout]: https://www.w3.org/TR/webgpu/#default-pipeline-layout
68 pub layout: Option<&'a PipelineLayout>,
69 /// The compiled shader module for this stage.
70 pub module: &'a ShaderModule,
71 /// The name of the entry point in the compiled shader to use.
72 ///
73 /// If [`Some`], there must be a compute shader entry point with this name in `module`.
74 /// Otherwise, expect exactly one compute shader entry point in `module`, which will be
75 /// selected.
76 // NOTE: keep phrasing in sync. with `FragmentState::entry_point`
77 // NOTE: keep phrasing in sync. with `VertexState::entry_point`
78 pub entry_point: Option<&'a str>,
79 /// Advanced options for when this pipeline is compiled
80 ///
81 /// This implements `Default`, and for most users can be set to `Default::default()`
82 pub compilation_options: PipelineCompilationOptions<'a>,
83 /// The pipeline cache to use when creating this pipeline.
84 pub cache: Option<&'a PipelineCache>,
85}
86#[cfg(send_sync)]
87static_assertions::assert_impl_all!(ComputePipelineDescriptor<'_>: Send, Sync);