Skip to main content

webgpu_traits/
render_commands.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Render pass commands
6
7use serde::{Deserialize, Serialize};
8use wgpu_core::command::PassStateError;
9use wgpu_core::global::Global;
10use wgpu_core::id::{
11    BindGroupId, BufferId, RenderBundleEncoderId, RenderBundleId, RenderPassEncoderId,
12    RenderPipelineId,
13};
14
15/// <https://github.com/gfx-rs/wgpu/blob/f25e07b984ab391628d9568296d5970981d79d8b/wgpu-core/src/command/render_command.rs#L17>
16#[derive(Debug, Deserialize, Serialize)]
17pub enum RenderCommand {
18    SetPipeline(RenderPipelineId),
19    SetBindGroup {
20        index: u32,
21        bind_group_id: BindGroupId,
22        offsets: Vec<u32>,
23    },
24    SetViewport {
25        x: f32,
26        y: f32,
27        width: f32,
28        height: f32,
29        min_depth: f32,
30        max_depth: f32,
31    },
32    SetScissorRect {
33        x: u32,
34        y: u32,
35        width: u32,
36        height: u32,
37    },
38    SetBlendConstant(wgpu_types::Color),
39    SetStencilReference(u32),
40    SetIndexBuffer {
41        buffer_id: BufferId,
42        index_format: wgpu_types::IndexFormat,
43        offset: u64,
44        size: Option<wgpu_types::BufferSize>,
45    },
46    SetVertexBuffer {
47        slot: u32,
48        buffer_id: Option<BufferId>,
49        offset: u64,
50        size: Option<wgpu_types::BufferSize>,
51    },
52    Draw {
53        vertex_count: u32,
54        instance_count: u32,
55        first_vertex: u32,
56        first_instance: u32,
57    },
58    DrawIndexed {
59        index_count: u32,
60        instance_count: u32,
61        first_index: u32,
62        base_vertex: i32,
63        first_instance: u32,
64    },
65    DrawIndirect {
66        buffer_id: BufferId,
67        offset: u64,
68    },
69    DrawIndexedIndirect {
70        buffer_id: BufferId,
71        offset: u64,
72    },
73    ExecuteBundles(Vec<RenderBundleId>),
74    PushDebugGroup(String),
75    PopDebugGroup,
76    InsertDebugMarker(String),
77}
78
79pub fn apply_render_command(
80    global: &Global,
81    render_pass_id: RenderPassEncoderId,
82    command: RenderCommand,
83) -> Result<(), PassStateError> {
84    match command {
85        RenderCommand::SetPipeline(pipeline_id) => {
86            global.render_pass_set_pipeline_with_id(render_pass_id, pipeline_id)
87        },
88        RenderCommand::SetBindGroup {
89            index,
90            bind_group_id,
91            offsets,
92        } => global.render_pass_set_bind_group_with_id(
93            render_pass_id,
94            index,
95            Some(bind_group_id),
96            &offsets,
97        ),
98        RenderCommand::SetViewport {
99            x,
100            y,
101            width,
102            height,
103            min_depth,
104            max_depth,
105        } => global.render_pass_set_viewport_with_id(
106            render_pass_id,
107            x,
108            y,
109            width,
110            height,
111            min_depth,
112            max_depth,
113        ),
114        RenderCommand::SetScissorRect {
115            x,
116            y,
117            width,
118            height,
119        } => global.render_pass_set_scissor_rect_with_id(render_pass_id, x, y, width, height),
120        RenderCommand::SetBlendConstant(color) => {
121            global.render_pass_set_blend_constant_with_id(render_pass_id, color)
122        },
123        RenderCommand::SetStencilReference(reference) => {
124            global.render_pass_set_stencil_reference_with_id(render_pass_id, reference)
125        },
126        RenderCommand::SetIndexBuffer {
127            buffer_id,
128            index_format,
129            offset,
130            size,
131        } => global.render_pass_set_index_buffer_with_id(
132            render_pass_id,
133            buffer_id,
134            index_format,
135            offset,
136            size,
137        ),
138        RenderCommand::SetVertexBuffer {
139            slot,
140            buffer_id,
141            offset,
142            size,
143        } => global.render_pass_set_vertex_buffer_with_id(
144            render_pass_id,
145            slot,
146            buffer_id,
147            offset,
148            size,
149        ),
150        RenderCommand::Draw {
151            vertex_count,
152            instance_count,
153            first_vertex,
154            first_instance,
155        } => global.render_pass_draw_with_id(
156            render_pass_id,
157            vertex_count,
158            instance_count,
159            first_vertex,
160            first_instance,
161        ),
162        RenderCommand::DrawIndexed {
163            index_count,
164            instance_count,
165            first_index,
166            base_vertex,
167            first_instance,
168        } => global.render_pass_draw_indexed_with_id(
169            render_pass_id,
170            index_count,
171            instance_count,
172            first_index,
173            base_vertex,
174            first_instance,
175        ),
176        RenderCommand::DrawIndirect { buffer_id, offset } => {
177            global.render_pass_draw_indirect_with_id(render_pass_id, buffer_id, offset)
178        },
179        RenderCommand::DrawIndexedIndirect { buffer_id, offset } => {
180            global.render_pass_draw_indexed_indirect_with_id(render_pass_id, buffer_id, offset)
181        },
182        RenderCommand::ExecuteBundles(bundles) => {
183            global.render_pass_execute_bundles_with_id(render_pass_id, &bundles)
184        },
185        RenderCommand::PushDebugGroup(label) => {
186            global.render_pass_push_debug_group_with_id(render_pass_id, &label, 0)
187        },
188        RenderCommand::PopDebugGroup => global.render_pass_pop_debug_group_with_id(render_pass_id),
189        RenderCommand::InsertDebugMarker(label) => {
190            global.render_pass_insert_debug_marker_with_id(render_pass_id, &label, 0)
191        },
192    }
193}
194
195/// <https://github.com/gfx-rs/wgpu/blob/f25e07b984ab391628d9568296d5970981d79d8b/wgpu-core/src/command/render_command.rs#L17>
196#[derive(Debug, Deserialize, Serialize)]
197pub enum RenderBundleCommand {
198    SetPipeline(RenderPipelineId),
199    SetBindGroup {
200        index: u32,
201        bind_group_id: BindGroupId,
202        offsets: Vec<u32>,
203    },
204    SetIndexBuffer {
205        buffer_id: BufferId,
206        index_format: wgpu_types::IndexFormat,
207        offset: u64,
208        size: Option<wgpu_types::BufferSize>,
209    },
210    SetVertexBuffer {
211        slot: u32,
212        buffer_id: Option<BufferId>,
213        offset: u64,
214        size: Option<wgpu_types::BufferSize>,
215    },
216    Draw {
217        vertex_count: u32,
218        instance_count: u32,
219        first_vertex: u32,
220        first_instance: u32,
221    },
222    DrawIndexed {
223        index_count: u32,
224        instance_count: u32,
225        first_index: u32,
226        base_vertex: i32,
227        first_instance: u32,
228    },
229    DrawIndirect {
230        buffer_id: BufferId,
231        offset: u64,
232    },
233    DrawIndexedIndirect {
234        buffer_id: BufferId,
235        offset: u64,
236    },
237    PushDebugGroup(String),
238    PopDebugGroup,
239    InsertDebugMarker(String),
240}
241
242pub fn apply_render_bundle_command(
243    global: &Global,
244    render_bundle_encoder_id: RenderBundleEncoderId,
245    command: RenderBundleCommand,
246) -> Result<(), PassStateError> {
247    match command {
248        RenderBundleCommand::SetPipeline(id) => {
249            global.render_bundle_encoder_set_pipeline_with_id(render_bundle_encoder_id, id)
250        },
251        RenderBundleCommand::SetBindGroup {
252            index,
253            bind_group_id,
254            offsets,
255        } => global.render_bundle_encoder_set_bind_group_with_id(
256            render_bundle_encoder_id,
257            index,
258            Some(bind_group_id),
259            &offsets,
260        ),
261        RenderBundleCommand::SetIndexBuffer {
262            buffer_id,
263            index_format,
264            offset,
265            size,
266        } => global.render_bundle_encoder_set_index_buffer_with_id(
267            render_bundle_encoder_id,
268            buffer_id,
269            index_format,
270            offset,
271            size,
272        ),
273        RenderBundleCommand::SetVertexBuffer {
274            slot,
275            buffer_id,
276            offset,
277            size,
278        } => global.render_bundle_encoder_set_vertex_buffer_with_id(
279            render_bundle_encoder_id,
280            slot,
281            buffer_id,
282            offset,
283            size,
284        ),
285        RenderBundleCommand::Draw {
286            vertex_count,
287            instance_count,
288            first_vertex,
289            first_instance,
290        } => global.render_bundle_encoder_draw_with_id(
291            render_bundle_encoder_id,
292            vertex_count,
293            instance_count,
294            first_vertex,
295            first_instance,
296        ),
297        RenderBundleCommand::DrawIndexed {
298            index_count,
299            instance_count,
300            first_index,
301            base_vertex,
302            first_instance,
303        } => global.render_bundle_encoder_draw_indexed_with_id(
304            render_bundle_encoder_id,
305            index_count,
306            instance_count,
307            first_index,
308            base_vertex,
309            first_instance,
310        ),
311        RenderBundleCommand::DrawIndirect { buffer_id, offset } => global
312            .render_bundle_encoder_draw_indirect_with_id(
313                render_bundle_encoder_id,
314                buffer_id,
315                offset,
316            ),
317        RenderBundleCommand::DrawIndexedIndirect { buffer_id, offset } => global
318            .render_bundle_encoder_draw_indexed_indirect_with_id(
319                render_bundle_encoder_id,
320                buffer_id,
321                offset,
322            ),
323        RenderBundleCommand::PushDebugGroup(label) => {
324            global.render_bundle_encoder_push_debug_group_with_id(render_bundle_encoder_id, &label)
325        },
326        RenderBundleCommand::PopDebugGroup => {
327            global.render_bundle_encoder_pop_debug_group_with_id(render_bundle_encoder_id)
328        },
329        RenderBundleCommand::InsertDebugMarker(label) => global
330            .render_bundle_encoder_insert_debug_marker_with_id(render_bundle_encoder_id, &label),
331    }
332}