wgpu/api/render_bundle_encoder.rs
1use std::{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 };
61
62 RenderBundle { inner: bundle }
63 }
64
65 /// Sets the active bind group for a given bind group index. The bind group layout
66 /// in the active pipeline when any `draw()` function is called must match the layout of this bind group.
67 ///
68 /// If the bind group have dynamic offsets, provide them in the binding order.
69 pub fn set_bind_group<'b, BG>(&mut self, index: u32, bind_group: BG, offsets: &[DynamicOffset])
70 where
71 Option<&'b BindGroup>: From<BG>,
72 {
73 let bg: Option<&'b BindGroup> = bind_group.into();
74 let bg = bg.map(|x| &x.inner);
75 self.inner.set_bind_group(index, bg, offsets);
76 }
77
78 /// Sets the active render pipeline.
79 ///
80 /// Subsequent draw calls will exhibit the behavior defined by `pipeline`.
81 pub fn set_pipeline(&mut self, pipeline: &'a RenderPipeline) {
82 self.inner.set_pipeline(&pipeline.inner);
83 }
84
85 /// Sets the active index buffer.
86 ///
87 /// Subsequent calls to [`draw_indexed`](RenderBundleEncoder::draw_indexed) on this [`RenderBundleEncoder`] will
88 /// use `buffer` as the source index buffer.
89 pub fn set_index_buffer(&mut self, buffer_slice: BufferSlice<'a>, index_format: IndexFormat) {
90 self.inner.set_index_buffer(
91 &buffer_slice.buffer.inner,
92 index_format,
93 buffer_slice.offset,
94 buffer_slice.size,
95 );
96 }
97
98 /// Assign a vertex buffer to a slot.
99 ///
100 /// Subsequent calls to [`draw`] and [`draw_indexed`] on this
101 /// [`RenderBundleEncoder`] will use `buffer` as one of the source vertex buffers.
102 ///
103 /// The `slot` refers to the index of the matching descriptor in
104 /// [`VertexState::buffers`].
105 ///
106 /// [`draw`]: RenderBundleEncoder::draw
107 /// [`draw_indexed`]: RenderBundleEncoder::draw_indexed
108 pub fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: BufferSlice<'a>) {
109 self.inner.set_vertex_buffer(
110 slot,
111 &buffer_slice.buffer.inner,
112 buffer_slice.offset,
113 buffer_slice.size,
114 );
115 }
116
117 /// Draws primitives from the active vertex buffer(s).
118 ///
119 /// The active vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
120 /// Does not use an Index Buffer. If you need this see [`RenderBundleEncoder::draw_indexed`]
121 ///
122 /// Panics if vertices Range is outside of the range of the vertices range of any set vertex buffer.
123 ///
124 /// vertices: The range of vertices to draw.
125 /// instances: Range of Instances to draw. Use 0..1 if instance buffers are not used.
126 /// E.g.of how its used internally
127 /// ```rust ignore
128 /// for instance_id in instance_range {
129 /// for vertex_id in vertex_range {
130 /// let vertex = vertex[vertex_id];
131 /// vertex_shader(vertex, vertex_id, instance_id);
132 /// }
133 /// }
134 /// ```
135 pub fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>) {
136 self.inner.draw(vertices, instances);
137 }
138
139 /// Draws indexed primitives using the active index buffer and the active vertex buffer(s).
140 ///
141 /// The active index buffer can be set with [`RenderBundleEncoder::set_index_buffer`].
142 /// The active vertex buffer(s) can be set with [`RenderBundleEncoder::set_vertex_buffer`].
143 ///
144 /// Panics if indices Range is outside of the range of the indices range of any set index buffer.
145 ///
146 /// indices: The range of indices to draw.
147 /// base_vertex: value added to each index value before indexing into the vertex buffers.
148 /// instances: Range of Instances to draw. Use 0..1 if instance buffers are not used.
149 /// E.g.of how its used internally
150 /// ```rust ignore
151 /// for instance_id in instance_range {
152 /// for index_index in index_range {
153 /// let vertex_id = index_buffer[index_index];
154 /// let adjusted_vertex_id = vertex_id + base_vertex;
155 /// let vertex = vertex[adjusted_vertex_id];
156 /// vertex_shader(vertex, adjusted_vertex_id, instance_id);
157 /// }
158 /// }
159 /// ```
160 pub fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>) {
161 self.inner.draw_indexed(indices, base_vertex, instances);
162 }
163
164 /// Draws primitives from the active vertex buffer(s) based on the contents of the `indirect_buffer`.
165 ///
166 /// The active vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
167 ///
168 /// The structure expected in `indirect_buffer` must conform to [`DrawIndirectArgs`](crate::util::DrawIndirectArgs).
169 pub fn draw_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: BufferAddress) {
170 self.inner
171 .draw_indirect(&indirect_buffer.inner, indirect_offset);
172 }
173
174 /// Draws indexed primitives using the active index buffer and the active vertex buffers,
175 /// based on the contents of the `indirect_buffer`.
176 ///
177 /// The active index buffer can be set with [`RenderBundleEncoder::set_index_buffer`], while the active
178 /// vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
179 ///
180 /// The structure expected in `indirect_buffer` must conform to [`DrawIndexedIndirectArgs`](crate::util::DrawIndexedIndirectArgs).
181 pub fn draw_indexed_indirect(
182 &mut self,
183 indirect_buffer: &'a Buffer,
184 indirect_offset: BufferAddress,
185 ) {
186 self.inner
187 .draw_indexed_indirect(&indirect_buffer.inner, indirect_offset);
188 }
189}
190
191/// [`Features::PUSH_CONSTANTS`] must be enabled on the device in order to call these functions.
192impl RenderBundleEncoder<'_> {
193 /// Set push constant data.
194 ///
195 /// Offset is measured in bytes, but must be a multiple of [`PUSH_CONSTANT_ALIGNMENT`].
196 ///
197 /// Data size must be a multiple of 4 and must have an alignment of 4.
198 /// For example, with an offset of 4 and an array of `[u8; 8]`, that will write to the range
199 /// of 4..12.
200 ///
201 /// For each byte in the range of push constant data written, the union of the stages of all push constant
202 /// ranges that covers that byte must be exactly `stages`. There's no good way of explaining this simply,
203 /// so here are some examples:
204 ///
205 /// ```text
206 /// For the given ranges:
207 /// - 0..4 Vertex
208 /// - 4..8 Fragment
209 /// ```
210 ///
211 /// You would need to upload this in two set_push_constants calls. First for the `Vertex` range, second for the `Fragment` range.
212 ///
213 /// ```text
214 /// For the given ranges:
215 /// - 0..8 Vertex
216 /// - 4..12 Fragment
217 /// ```
218 ///
219 /// You would need to upload this in three set_push_constants calls. First for the `Vertex` only range 0..4, second
220 /// for the `Vertex | Fragment` range 4..8, third for the `Fragment` range 8..12.
221 pub fn set_push_constants(&mut self, stages: ShaderStages, offset: u32, data: &[u8]) {
222 self.inner.set_push_constants(stages, offset, data);
223 }
224}