wgpu/util/
encoder.rs

1use std::ops::Range;
2
3use wgt::{BufferAddress, DynamicOffset, IndexFormat};
4
5use crate::{BindGroup, Buffer, BufferSlice, RenderBundleEncoder, RenderPass, RenderPipeline};
6
7/// Methods shared by [`RenderPass`] and [`RenderBundleEncoder`].
8pub trait RenderEncoder<'a> {
9    /// Sets the active bind group for a given bind group index. The bind group layout
10    /// in the active pipeline when any `draw()` function is called must match the layout of this bind group.
11    ///
12    /// If the bind group have dynamic offsets, provide them in order of their declaration.
13    fn set_bind_group(
14        &mut self,
15        index: u32,
16        bind_group: Option<&'a BindGroup>,
17        offsets: &[DynamicOffset],
18    );
19
20    /// Sets the active render pipeline.
21    ///
22    /// Subsequent draw calls will exhibit the behavior defined by `pipeline`.
23    fn set_pipeline(&mut self, pipeline: &'a RenderPipeline);
24
25    /// Sets the active index buffer.
26    ///
27    /// Subsequent calls to [`draw_indexed`](RenderEncoder::draw_indexed) on this [`RenderEncoder`] will
28    /// use `buffer` as the source index buffer.
29    fn set_index_buffer(&mut self, buffer_slice: BufferSlice<'a>, index_format: IndexFormat);
30
31    /// Assign a vertex buffer to a slot.
32    ///
33    /// Subsequent calls to [`draw`] and [`draw_indexed`] on this
34    /// [`RenderEncoder`] will use `buffer` as one of the source vertex buffers.
35    ///
36    /// The `slot` refers to the index of the matching descriptor in
37    /// [VertexState::buffers](crate::VertexState::buffers).
38    ///
39    /// [`draw`]: RenderEncoder::draw
40    /// [`draw_indexed`]: RenderEncoder::draw_indexed
41    fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: BufferSlice<'a>);
42
43    /// Draws primitives from the active vertex buffer(s).
44    ///
45    /// The active vertex buffers can be set with [`RenderEncoder::set_vertex_buffer`].
46    fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>);
47
48    /// Draws indexed primitives using the active index buffer and the active vertex buffers.
49    ///
50    /// The active index buffer can be set with [`RenderEncoder::set_index_buffer`], while the active
51    /// vertex buffers can be set with [`RenderEncoder::set_vertex_buffer`].
52    fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>);
53
54    /// Draws primitives from the active vertex buffer(s) based on the contents of the `indirect_buffer`.
55    ///
56    /// The active vertex buffers can be set with [`RenderEncoder::set_vertex_buffer`].
57    ///
58    /// The structure expected in `indirect_buffer` must conform to [`DrawIndirectArgs`](crate::util::DrawIndirectArgs).
59    fn draw_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: BufferAddress);
60
61    /// Draws indexed primitives using the active index buffer and the active vertex buffers,
62    /// based on the contents of the `indirect_buffer`.
63    ///
64    /// The active index buffer can be set with [`RenderEncoder::set_index_buffer`], while the active
65    /// vertex buffers can be set with [`RenderEncoder::set_vertex_buffer`].
66    ///
67    /// The structure expected in `indirect_buffer` must conform to [`DrawIndexedIndirectArgs`](crate::util::DrawIndexedIndirectArgs).
68    fn draw_indexed_indirect(
69        &mut self,
70        indirect_buffer: &'a Buffer,
71        indirect_offset: BufferAddress,
72    );
73
74    /// [`wgt::Features::PUSH_CONSTANTS`] must be enabled on the device in order to call this function.
75    ///
76    /// Set push constant data.
77    ///
78    /// Offset is measured in bytes, but must be a multiple of [`wgt::PUSH_CONSTANT_ALIGNMENT`].
79    ///
80    /// Data size must be a multiple of 4 and must be aligned to the 4s, so we take an array of u32.
81    /// For example, with an offset of 4 and an array of `[u32; 3]`, that will write to the range
82    /// of 4..16.
83    ///
84    /// For each byte in the range of push constant data written, the union of the stages of all push constant
85    /// ranges that covers that byte must be exactly `stages`. There's no good way of explaining this simply,
86    /// so here are some examples:
87    ///
88    /// ```text
89    /// For the given ranges:
90    /// - 0..4 Vertex
91    /// - 4..8 Fragment
92    /// ```
93    ///
94    /// You would need to upload this in two set_push_constants calls. First for the `Vertex` range, second for the `Fragment` range.
95    ///
96    /// ```text
97    /// For the given ranges:
98    /// - 0..8  Vertex
99    /// - 4..12 Fragment
100    /// ```
101    ///
102    /// You would need to upload this in three set_push_constants calls. First for the `Vertex` only range 0..4, second
103    /// for the `Vertex | Fragment` range 4..8, third for the `Fragment` range 8..12.
104    fn set_push_constants(&mut self, stages: wgt::ShaderStages, offset: u32, data: &[u8]);
105}
106
107impl<'a> RenderEncoder<'a> for RenderPass<'a> {
108    #[inline(always)]
109    fn set_bind_group(
110        &mut self,
111        index: u32,
112        bind_group: Option<&'a BindGroup>,
113        offsets: &[DynamicOffset],
114    ) {
115        Self::set_bind_group(self, index, bind_group, offsets);
116    }
117
118    #[inline(always)]
119    fn set_pipeline(&mut self, pipeline: &'a RenderPipeline) {
120        Self::set_pipeline(self, pipeline);
121    }
122
123    #[inline(always)]
124    fn set_index_buffer(&mut self, buffer_slice: BufferSlice<'a>, index_format: IndexFormat) {
125        Self::set_index_buffer(self, buffer_slice, index_format);
126    }
127
128    #[inline(always)]
129    fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: BufferSlice<'a>) {
130        Self::set_vertex_buffer(self, slot, buffer_slice);
131    }
132
133    #[inline(always)]
134    fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>) {
135        Self::draw(self, vertices, instances);
136    }
137
138    #[inline(always)]
139    fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>) {
140        Self::draw_indexed(self, indices, base_vertex, instances);
141    }
142
143    #[inline(always)]
144    fn draw_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: BufferAddress) {
145        Self::draw_indirect(self, indirect_buffer, indirect_offset);
146    }
147
148    #[inline(always)]
149    fn draw_indexed_indirect(
150        &mut self,
151        indirect_buffer: &'a Buffer,
152        indirect_offset: BufferAddress,
153    ) {
154        Self::draw_indexed_indirect(self, indirect_buffer, indirect_offset);
155    }
156
157    #[inline(always)]
158    fn set_push_constants(&mut self, stages: wgt::ShaderStages, offset: u32, data: &[u8]) {
159        Self::set_push_constants(self, stages, offset, data);
160    }
161}
162
163impl<'a> RenderEncoder<'a> for RenderBundleEncoder<'a> {
164    #[inline(always)]
165    fn set_bind_group(
166        &mut self,
167        index: u32,
168        bind_group: Option<&'a BindGroup>,
169        offsets: &[DynamicOffset],
170    ) {
171        Self::set_bind_group(self, index, bind_group, offsets);
172    }
173
174    #[inline(always)]
175    fn set_pipeline(&mut self, pipeline: &'a RenderPipeline) {
176        Self::set_pipeline(self, pipeline);
177    }
178
179    #[inline(always)]
180    fn set_index_buffer(&mut self, buffer_slice: BufferSlice<'a>, index_format: IndexFormat) {
181        Self::set_index_buffer(self, buffer_slice, index_format);
182    }
183
184    #[inline(always)]
185    fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: BufferSlice<'a>) {
186        Self::set_vertex_buffer(self, slot, buffer_slice);
187    }
188
189    #[inline(always)]
190    fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>) {
191        Self::draw(self, vertices, instances);
192    }
193
194    #[inline(always)]
195    fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>) {
196        Self::draw_indexed(self, indices, base_vertex, instances);
197    }
198
199    #[inline(always)]
200    fn draw_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: BufferAddress) {
201        Self::draw_indirect(self, indirect_buffer, indirect_offset);
202    }
203
204    #[inline(always)]
205    fn draw_indexed_indirect(
206        &mut self,
207        indirect_buffer: &'a Buffer,
208        indirect_offset: BufferAddress,
209    ) {
210        Self::draw_indexed_indirect(self, indirect_buffer, indirect_offset);
211    }
212
213    #[inline(always)]
214    fn set_push_constants(&mut self, stages: wgt::ShaderStages, offset: u32, data: &[u8]) {
215        Self::set_push_constants(self, stages, offset, data);
216    }
217}