wgpu/api/render_bundle_encoder.rs
1use core::{marker::PhantomData, num::NonZeroU32, ops::Range};
2
3use crate::dispatch::RenderBundleEncoderInterface;
4use crate::*;
5
6/// Encodes a series of GPU operations into a reusable "render bundle".
7///
8/// It only supports a handful of render commands, but it makes them reusable.
9/// It can be created with [`Device::create_render_bundle_encoder`].
10/// It can be executed onto a [`CommandEncoder`] using [`RenderPass::execute_bundles`].
11///
12/// Executing a [`RenderBundle`] is often more efficient than issuing the underlying commands
13/// manually.
14///
15/// Corresponds to [WebGPU `GPURenderBundleEncoder`](
16/// https://gpuweb.github.io/gpuweb/#gpurenderbundleencoder).
17#[derive(Debug)]
18pub struct RenderBundleEncoder<'a> {
19 pub(crate) inner: dispatch::DispatchRenderBundleEncoder,
20 /// This type should be !Send !Sync, because it represents an allocation on this thread's
21 /// command buffer.
22 pub(crate) _p: PhantomData<(*const u8, &'a ())>,
23}
24static_assertions::assert_not_impl_any!(RenderBundleEncoder<'_>: Send, Sync);
25
26crate::cmp::impl_eq_ord_hash_proxy!(RenderBundleEncoder<'_> => .inner);
27
28/// Describes a [`RenderBundleEncoder`].
29///
30/// For use with [`Device::create_render_bundle_encoder`].
31///
32/// Corresponds to [WebGPU `GPURenderBundleEncoderDescriptor`](
33/// https://gpuweb.github.io/gpuweb/#dictdef-gpurenderbundleencoderdescriptor).
34#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
35pub struct RenderBundleEncoderDescriptor<'a> {
36 /// Debug label of the render bundle encoder. This will show up in graphics debuggers for easy identification.
37 pub label: Label<'a>,
38 /// The formats of the color attachments that this render bundle is capable to rendering to. This
39 /// must match the formats of the color attachments in the render pass this render bundle is executed in.
40 pub color_formats: &'a [Option<TextureFormat>],
41 /// Information about the depth attachment that this render bundle is capable to rendering to. This
42 /// must match the format of the depth attachments in the render pass this render bundle is executed in.
43 pub depth_stencil: Option<RenderBundleDepthStencil>,
44 /// Sample count this render bundle is capable of rendering to. This must match the pipelines and
45 /// the render passes it is used in.
46 pub sample_count: u32,
47 /// If this render bundle will rendering to multiple array layers in the attachments at the same time.
48 pub multiview: Option<NonZeroU32>,
49}
50static_assertions::assert_impl_all!(RenderBundleEncoderDescriptor<'_>: Send, Sync);
51
52impl<'a> RenderBundleEncoder<'a> {
53 /// Finishes recording and returns a [`RenderBundle`] that can be executed in other render passes.
54 pub fn finish(self, desc: &RenderBundleDescriptor<'_>) -> RenderBundle {
55 let bundle = match self.inner {
56 #[cfg(wgpu_core)]
57 dispatch::DispatchRenderBundleEncoder::Core(b) => b.finish(desc),
58 #[cfg(webgpu)]
59 dispatch::DispatchRenderBundleEncoder::WebGPU(b) => b.finish(desc),
60 #[cfg(custom)]
61 dispatch::DispatchRenderBundleEncoder::Custom(_) => unimplemented!(),
62 };
63
64 RenderBundle { inner: bundle }
65 }
66
67 /// Sets the active bind group for a given bind group index. The bind group layout
68 /// in the active pipeline when any `draw()` function is called must match the layout of this bind group.
69 ///
70 /// If the bind group have dynamic offsets, provide them in the binding order.
71 pub fn set_bind_group<'b, BG>(&mut self, index: u32, bind_group: BG, offsets: &[DynamicOffset])
72 where
73 Option<&'b BindGroup>: From<BG>,
74 {
75 let bg: Option<&'b BindGroup> = bind_group.into();
76 let bg = bg.map(|x| &x.inner);
77 self.inner.set_bind_group(index, bg, offsets);
78 }
79
80 /// Sets the active render pipeline.
81 ///
82 /// Subsequent draw calls will exhibit the behavior defined by `pipeline`.
83 pub fn set_pipeline(&mut self, pipeline: &'a RenderPipeline) {
84 self.inner.set_pipeline(&pipeline.inner);
85 }
86
87 /// Sets the active index buffer.
88 ///
89 /// Subsequent calls to [`draw_indexed`](RenderBundleEncoder::draw_indexed) on this [`RenderBundleEncoder`] will
90 /// use `buffer` as the source index buffer.
91 ///
92 /// # Panics
93 ///
94 /// - If the buffer slice length is 0.
95 pub fn set_index_buffer(&mut self, buffer_slice: BufferSlice<'a>, index_format: IndexFormat) {
96 self.inner.set_index_buffer(
97 &buffer_slice.buffer.inner,
98 index_format,
99 buffer_slice.offset,
100 // TODO(https://github.com/gfx-rs/wgpu/issues/3170): Empty slices should be supported here
101 Some(buffer_slice.size_expect_nonzero()),
102 );
103 }
104
105 /// Assign a vertex buffer to a slot.
106 ///
107 /// Subsequent calls to [`draw`] and [`draw_indexed`] on this
108 /// [`RenderBundleEncoder`] will use `buffer` as one of the source vertex buffers.
109 ///
110 /// The `slot` refers to the index of the matching descriptor in
111 /// [`VertexState::buffers`].
112 ///
113 /// [`draw`]: RenderBundleEncoder::draw
114 /// [`draw_indexed`]: RenderBundleEncoder::draw_indexed
115 ///
116 /// # Panics
117 ///
118 /// - If the buffer slice length is 0.
119 pub fn set_vertex_buffer<'b, B>(&mut self, slot: u32, buffer_slice: B)
120 where
121 Option<BufferSlice<'b>>: From<B>,
122 {
123 let buffer_slice: Option<BufferSlice<'b>> = buffer_slice.into();
124 if let Some(buffer_slice) = buffer_slice {
125 self.inner.set_vertex_buffer(
126 slot,
127 Some(&buffer_slice.buffer.inner),
128 buffer_slice.offset,
129 // TODO(https://github.com/gfx-rs/wgpu/issues/3170): Empty slices should be supported here
130 Some(buffer_slice.size_expect_nonzero()),
131 );
132 } else {
133 self.inner.set_vertex_buffer(slot, None, 0, None);
134 }
135 }
136
137 /// Draws primitives from the active vertex buffer(s).
138 ///
139 /// The active vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
140 /// Does not use an Index Buffer. If you need this see [`RenderBundleEncoder::draw_indexed`]
141 ///
142 /// Panics if vertices Range is outside of the range of the vertices range of any set vertex buffer.
143 ///
144 /// vertices: The range of vertices to draw.
145 /// instances: Range of Instances to draw. Use 0..1 if instance buffers are not used.
146 /// E.g.of how its used internally
147 /// ```rust ignore
148 /// for instance_id in instance_range {
149 /// for vertex_id in vertex_range {
150 /// let vertex = vertex[vertex_id];
151 /// vertex_shader(vertex, vertex_id, instance_id);
152 /// }
153 /// }
154 /// ```
155 pub fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>) {
156 self.inner.draw(vertices, instances);
157 }
158
159 /// Draws indexed primitives using the active index buffer and the active vertex buffer(s).
160 ///
161 /// The active index buffer can be set with [`RenderBundleEncoder::set_index_buffer`].
162 /// The active vertex buffer(s) can be set with [`RenderBundleEncoder::set_vertex_buffer`].
163 ///
164 /// Panics if indices Range is outside of the range of the indices range of any set index buffer.
165 ///
166 /// indices: The range of indices to draw.
167 /// base_vertex: value added to each index value before indexing into the vertex buffers.
168 /// instances: Range of Instances to draw. Use 0..1 if instance buffers are not used.
169 /// E.g.of how its used internally
170 /// ```rust ignore
171 /// for instance_id in instance_range {
172 /// for index_index in index_range {
173 /// let vertex_id = index_buffer[index_index];
174 /// let adjusted_vertex_id = vertex_id + base_vertex;
175 /// let vertex = vertex[adjusted_vertex_id];
176 /// vertex_shader(vertex, adjusted_vertex_id, instance_id);
177 /// }
178 /// }
179 /// ```
180 pub fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>) {
181 self.inner.draw_indexed(indices, base_vertex, instances);
182 }
183
184 /// Draws primitives from the active vertex buffer(s) based on the contents of the `indirect_buffer`.
185 ///
186 /// The active vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
187 ///
188 /// The structure expected in `indirect_buffer` must conform to [`DrawIndirectArgs`](crate::util::DrawIndirectArgs).
189 pub fn draw_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: BufferAddress) {
190 self.inner
191 .draw_indirect(&indirect_buffer.inner, indirect_offset);
192 }
193
194 /// Draws indexed primitives using the active index buffer and the active vertex buffers,
195 /// based on the contents of the `indirect_buffer`.
196 ///
197 /// The active index buffer can be set with [`RenderBundleEncoder::set_index_buffer`], while the active
198 /// vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
199 ///
200 /// The structure expected in `indirect_buffer` must conform to [`DrawIndexedIndirectArgs`](crate::util::DrawIndexedIndirectArgs).
201 pub fn draw_indexed_indirect(
202 &mut self,
203 indirect_buffer: &'a Buffer,
204 indirect_offset: BufferAddress,
205 ) {
206 self.inner
207 .draw_indexed_indirect(&indirect_buffer.inner, indirect_offset);
208 }
209
210 #[cfg(custom)]
211 /// Returns custom implementation of RenderBundleEncoder (if custom backend and is internally T)
212 pub fn as_custom<T: custom::RenderBundleEncoderInterface>(&self) -> Option<&T> {
213 self.inner.as_custom()
214 }
215}
216
217/// [`Features::IMMEDIATES`] must be enabled on the device in order to call these functions.
218impl RenderBundleEncoder<'_> {
219 /// Set immediate data for subsequent draw calls within the render bundle.
220 ///
221 /// Write the bytes in `data` at offset `offset` within immediate data
222 /// storage. Both `offset` and the length of `data` must be
223 /// multiples of [`crate::IMMEDIATE_DATA_ALIGNMENT`], which is always 4.
224 ///
225 /// For example, if `offset` is `4` and `data` is eight bytes long, this
226 /// call will write `data` to bytes `4..12` of immediate data storage.
227 pub fn set_immediates(&mut self, offset: u32, data: &[u8]) {
228 self.inner.set_immediates(offset, data);
229 }
230}