wgpu/api/compute_pass.rs
1use crate::*;
2
3/// In-progress recording of a compute pass.
4///
5/// It can be created with [`CommandEncoder::begin_compute_pass`].
6///
7/// Corresponds to [WebGPU `GPUComputePassEncoder`](
8/// https://gpuweb.github.io/gpuweb/#compute-pass-encoder).
9#[derive(Debug)]
10pub struct ComputePass<'encoder> {
11 pub(crate) inner: dispatch::DispatchComputePass,
12
13 /// This lifetime is used to protect the [`CommandEncoder`] from being used
14 /// while the pass is alive. This needs to be PhantomDrop to prevent the lifetime
15 /// from being shortened.
16 pub(crate) _encoder_guard: crate::api::PhantomDrop<&'encoder ()>,
17}
18
19#[cfg(send_sync)]
20static_assertions::assert_impl_all!(ComputePass<'_>: Send, Sync);
21
22crate::cmp::impl_eq_ord_hash_proxy!(ComputePass<'_> => .inner);
23
24impl ComputePass<'_> {
25 /// Drops the lifetime relationship to the parent command encoder, making usage of
26 /// the encoder while this pass is recorded a run-time error instead.
27 ///
28 /// Attention: As long as the compute pass has not been ended, any mutating operation on the parent
29 /// command encoder will cause a run-time error and invalidate it!
30 /// By default, the lifetime constraint prevents this, but it can be useful
31 /// to handle this at run time, such as when storing the pass and encoder in the same
32 /// data structure.
33 ///
34 /// This operation has no effect on pass recording.
35 /// It's a safe operation, since [`CommandEncoder`] is in a locked state as long as the pass is active
36 /// regardless of the lifetime constraint or its absence.
37 pub fn forget_lifetime(self) -> ComputePass<'static> {
38 ComputePass {
39 inner: self.inner,
40 _encoder_guard: crate::api::PhantomDrop::default(),
41 }
42 }
43
44 /// Sets the active bind group for a given bind group index. The bind group layout
45 /// in the active pipeline when the `dispatch()` function is called must match the layout of this bind group.
46 ///
47 /// If the bind group have dynamic offsets, provide them in the binding order.
48 /// These offsets have to be aligned to [`Limits::min_uniform_buffer_offset_alignment`]
49 /// or [`Limits::min_storage_buffer_offset_alignment`] appropriately.
50 pub fn set_bind_group<'a, BG>(&mut self, index: u32, bind_group: BG, offsets: &[DynamicOffset])
51 where
52 Option<&'a BindGroup>: From<BG>,
53 {
54 let bg: Option<&BindGroup> = bind_group.into();
55 let bg = bg.map(|bg| &bg.inner);
56 self.inner.set_bind_group(index, bg, offsets);
57 }
58
59 /// Sets the active compute pipeline.
60 pub fn set_pipeline(&mut self, pipeline: &ComputePipeline) {
61 self.inner.set_pipeline(&pipeline.inner);
62 }
63
64 /// Inserts debug marker.
65 pub fn insert_debug_marker(&mut self, label: &str) {
66 self.inner.insert_debug_marker(label);
67 }
68
69 /// Start record commands and group it into debug marker group.
70 pub fn push_debug_group(&mut self, label: &str) {
71 self.inner.push_debug_group(label);
72 }
73
74 /// Stops command recording and creates debug group.
75 pub fn pop_debug_group(&mut self) {
76 self.inner.pop_debug_group();
77 }
78
79 /// Dispatches compute work operations.
80 ///
81 /// `x`, `y` and `z` denote the number of work groups to dispatch in each dimension.
82 pub fn dispatch_workgroups(&mut self, x: u32, y: u32, z: u32) {
83 self.inner.dispatch_workgroups(x, y, z);
84 }
85
86 /// Dispatches compute work operations, based on the contents of the `indirect_buffer`.
87 ///
88 /// The structure expected in `indirect_buffer` must conform to [`DispatchIndirectArgs`](crate::util::DispatchIndirectArgs).
89 pub fn dispatch_workgroups_indirect(
90 &mut self,
91 indirect_buffer: &Buffer,
92 indirect_offset: BufferAddress,
93 ) {
94 self.inner
95 .dispatch_workgroups_indirect(&indirect_buffer.inner, indirect_offset);
96 }
97}
98
99/// [`Features::PUSH_CONSTANTS`] must be enabled on the device in order to call these functions.
100impl ComputePass<'_> {
101 /// Set push constant data for subsequent dispatch calls.
102 ///
103 /// Write the bytes in `data` at offset `offset` within push constant
104 /// storage. Both `offset` and the length of `data` must be
105 /// multiples of [`PUSH_CONSTANT_ALIGNMENT`], which is always 4.
106 ///
107 /// For example, if `offset` is `4` and `data` is eight bytes long, this
108 /// call will write `data` to bytes `4..12` of push constant storage.
109 pub fn set_push_constants(&mut self, offset: u32, data: &[u8]) {
110 self.inner.set_push_constants(offset, data);
111 }
112}
113
114/// [`Features::TIMESTAMP_QUERY_INSIDE_PASSES`] must be enabled on the device in order to call these functions.
115impl ComputePass<'_> {
116 /// Issue a timestamp command at this point in the queue. The timestamp will be written to the specified query set, at the specified index.
117 ///
118 /// Must be multiplied by [`Queue::get_timestamp_period`] to get
119 /// the value in nanoseconds. Absolute values have no meaning,
120 /// but timestamps can be subtracted to get the time it takes
121 /// for a string of operations to complete.
122 pub fn write_timestamp(&mut self, query_set: &QuerySet, query_index: u32) {
123 self.inner.write_timestamp(&query_set.inner, query_index);
124 }
125}
126
127/// [`Features::PIPELINE_STATISTICS_QUERY`] must be enabled on the device in order to call these functions.
128impl ComputePass<'_> {
129 /// Start a pipeline statistics query on this compute pass. It can be ended with
130 /// `end_pipeline_statistics_query`. Pipeline statistics queries may not be nested.
131 pub fn begin_pipeline_statistics_query(&mut self, query_set: &QuerySet, query_index: u32) {
132 self.inner
133 .begin_pipeline_statistics_query(&query_set.inner, query_index);
134 }
135
136 /// End the pipeline statistics query on this compute pass. It can be started with
137 /// `begin_pipeline_statistics_query`. Pipeline statistics queries may not be nested.
138 pub fn end_pipeline_statistics_query(&mut self) {
139 self.inner.end_pipeline_statistics_query();
140 }
141}
142
143/// Describes the timestamp writes of a compute pass.
144///
145/// For use with [`ComputePassDescriptor`].
146/// At least one of `beginning_of_pass_write_index` and `end_of_pass_write_index` must be `Some`.
147///
148/// Corresponds to [WebGPU `GPUComputePassTimestampWrites`](
149/// https://gpuweb.github.io/gpuweb/#dictdef-gpucomputepasstimestampwrites).
150#[derive(Clone, Debug)]
151pub struct ComputePassTimestampWrites<'a> {
152 /// The query set to write to.
153 pub query_set: &'a QuerySet,
154 /// The index of the query set at which a start timestamp of this pass is written, if any.
155 pub beginning_of_pass_write_index: Option<u32>,
156 /// The index of the query set at which an end timestamp of this pass is written, if any.
157 pub end_of_pass_write_index: Option<u32>,
158}
159#[cfg(send_sync)]
160static_assertions::assert_impl_all!(ComputePassTimestampWrites<'_>: Send, Sync);
161
162/// Describes the attachments of a compute pass.
163///
164/// For use with [`CommandEncoder::begin_compute_pass`].
165///
166/// Corresponds to [WebGPU `GPUComputePassDescriptor`](
167/// https://gpuweb.github.io/gpuweb/#dictdef-gpucomputepassdescriptor).
168#[derive(Clone, Default, Debug)]
169pub struct ComputePassDescriptor<'a> {
170 /// Debug label of the compute pass. This will show up in graphics debuggers for easy identification.
171 pub label: Label<'a>,
172 /// Defines which timestamp values will be written for this pass, and where to write them to.
173 ///
174 /// Requires [`Features::TIMESTAMP_QUERY`] to be enabled.
175 pub timestamp_writes: Option<ComputePassTimestampWrites<'a>>,
176}
177#[cfg(send_sync)]
178static_assertions::assert_impl_all!(ComputePassDescriptor<'_>: Send, Sync);