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 pub fn set_index_buffer(&mut self, buffer_slice: BufferSlice<'a>, index_format: IndexFormat) {
92 self.inner.set_index_buffer(
93 &buffer_slice.buffer.inner,
94 index_format,
95 buffer_slice.offset,
96 Some(buffer_slice.size),
97 );
98 }
99
100 /// Assign a vertex buffer to a slot.
101 ///
102 /// Subsequent calls to [`draw`] and [`draw_indexed`] on this
103 /// [`RenderBundleEncoder`] will use `buffer` as one of the source vertex buffers.
104 ///
105 /// The `slot` refers to the index of the matching descriptor in
106 /// [`VertexState::buffers`].
107 ///
108 /// [`draw`]: RenderBundleEncoder::draw
109 /// [`draw_indexed`]: RenderBundleEncoder::draw_indexed
110 pub fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: BufferSlice<'a>) {
111 self.inner.set_vertex_buffer(
112 slot,
113 &buffer_slice.buffer.inner,
114 buffer_slice.offset,
115 Some(buffer_slice.size),
116 );
117 }
118
119 /// Draws primitives from the active vertex buffer(s).
120 ///
121 /// The active vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
122 /// Does not use an Index Buffer. If you need this see [`RenderBundleEncoder::draw_indexed`]
123 ///
124 /// Panics if vertices Range is outside of the range of the vertices range of any set vertex buffer.
125 ///
126 /// vertices: The range of vertices to draw.
127 /// instances: Range of Instances to draw. Use 0..1 if instance buffers are not used.
128 /// E.g.of how its used internally
129 /// ```rust ignore
130 /// for instance_id in instance_range {
131 /// for vertex_id in vertex_range {
132 /// let vertex = vertex[vertex_id];
133 /// vertex_shader(vertex, vertex_id, instance_id);
134 /// }
135 /// }
136 /// ```
137 pub fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>) {
138 self.inner.draw(vertices, instances);
139 }
140
141 /// Draws indexed primitives using the active index buffer and the active vertex buffer(s).
142 ///
143 /// The active index buffer can be set with [`RenderBundleEncoder::set_index_buffer`].
144 /// The active vertex buffer(s) can be set with [`RenderBundleEncoder::set_vertex_buffer`].
145 ///
146 /// Panics if indices Range is outside of the range of the indices range of any set index buffer.
147 ///
148 /// indices: The range of indices to draw.
149 /// base_vertex: value added to each index value before indexing into the vertex buffers.
150 /// instances: Range of Instances to draw. Use 0..1 if instance buffers are not used.
151 /// E.g.of how its used internally
152 /// ```rust ignore
153 /// for instance_id in instance_range {
154 /// for index_index in index_range {
155 /// let vertex_id = index_buffer[index_index];
156 /// let adjusted_vertex_id = vertex_id + base_vertex;
157 /// let vertex = vertex[adjusted_vertex_id];
158 /// vertex_shader(vertex, adjusted_vertex_id, instance_id);
159 /// }
160 /// }
161 /// ```
162 pub fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>) {
163 self.inner.draw_indexed(indices, base_vertex, instances);
164 }
165
166 /// Draws primitives from the active vertex buffer(s) based on the contents of the `indirect_buffer`.
167 ///
168 /// The active vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
169 ///
170 /// The structure expected in `indirect_buffer` must conform to [`DrawIndirectArgs`](crate::util::DrawIndirectArgs).
171 pub fn draw_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: BufferAddress) {
172 self.inner
173 .draw_indirect(&indirect_buffer.inner, indirect_offset);
174 }
175
176 /// Draws indexed primitives using the active index buffer and the active vertex buffers,
177 /// based on the contents of the `indirect_buffer`.
178 ///
179 /// The active index buffer can be set with [`RenderBundleEncoder::set_index_buffer`], while the active
180 /// vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
181 ///
182 /// The structure expected in `indirect_buffer` must conform to [`DrawIndexedIndirectArgs`](crate::util::DrawIndexedIndirectArgs).
183 pub fn draw_indexed_indirect(
184 &mut self,
185 indirect_buffer: &'a Buffer,
186 indirect_offset: BufferAddress,
187 ) {
188 self.inner
189 .draw_indexed_indirect(&indirect_buffer.inner, indirect_offset);
190 }
191
192 #[cfg(custom)]
193 /// Returns custom implementation of RenderBundleEncoder (if custom backend and is internally T)
194 pub fn as_custom<T: custom::RenderBundleEncoderInterface>(&self) -> Option<&T> {
195 self.inner.as_custom()
196 }
197}
198
199/// [`Features::IMMEDIATES`] must be enabled on the device in order to call these functions.
200impl RenderBundleEncoder<'_> {
201 /// Set immediate data for subsequent draw calls within the render bundle.
202 ///
203 /// Write the bytes in `data` at offset `offset` within immediate data
204 /// storage. Both `offset` and the length of `data` must be
205 /// multiples of [`crate::IMMEDIATE_DATA_ALIGNMENT`], which is always 4.
206 ///
207 /// For example, if `offset` is `4` and `data` is eight bytes long, this
208 /// call will write `data` to bytes `4..12` of immediate data storage.
209 pub fn set_immediates(&mut self, offset: u32, data: &[u8]) {
210 self.inner.set_immediates(offset, data);
211 }
212}