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