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, RenderPass};
9use wgpu_core::global::Global;
10use wgpu_core::id::{BindGroupId, BufferId, RenderBundleId, RenderPipelineId};
11
12/// <https://github.com/gfx-rs/wgpu/blob/f25e07b984ab391628d9568296d5970981d79d8b/wgpu-core/src/command/render_command.rs#L17>
13#[derive(Debug, Deserialize, Serialize)]
14pub enum RenderCommand {
15    SetPipeline(RenderPipelineId),
16    SetBindGroup {
17        index: u32,
18        bind_group_id: BindGroupId,
19        offsets: Vec<u32>,
20    },
21    SetViewport {
22        x: f32,
23        y: f32,
24        width: f32,
25        height: f32,
26        min_depth: f32,
27        max_depth: f32,
28    },
29    SetScissorRect {
30        x: u32,
31        y: u32,
32        width: u32,
33        height: u32,
34    },
35    SetBlendConstant(wgpu_types::Color),
36    SetStencilReference(u32),
37    SetIndexBuffer {
38        buffer_id: BufferId,
39        index_format: wgpu_types::IndexFormat,
40        offset: u64,
41        size: Option<wgpu_types::BufferSize>,
42    },
43    SetVertexBuffer {
44        slot: u32,
45        buffer_id: BufferId,
46        offset: u64,
47        size: Option<wgpu_types::BufferSize>,
48    },
49    Draw {
50        vertex_count: u32,
51        instance_count: u32,
52        first_vertex: u32,
53        first_instance: u32,
54    },
55    DrawIndexed {
56        index_count: u32,
57        instance_count: u32,
58        first_index: u32,
59        base_vertex: i32,
60        first_instance: u32,
61    },
62    DrawIndirect {
63        buffer_id: BufferId,
64        offset: u64,
65    },
66    DrawIndexedIndirect {
67        buffer_id: BufferId,
68        offset: u64,
69    },
70    ExecuteBundles(Vec<RenderBundleId>),
71    PushDebugGroup(String),
72    PopDebugGroup,
73    InsertDebugMarker(String),
74}
75
76pub fn apply_render_command(
77    global: &Global,
78    pass: &mut RenderPass,
79    command: RenderCommand,
80) -> Result<(), PassStateError> {
81    match command {
82        RenderCommand::SetPipeline(pipeline_id) => {
83            global.render_pass_set_pipeline(pass, pipeline_id)
84        },
85        RenderCommand::SetBindGroup {
86            index,
87            bind_group_id,
88            offsets,
89        } => global.render_pass_set_bind_group(pass, index, Some(bind_group_id), &offsets),
90        RenderCommand::SetViewport {
91            x,
92            y,
93            width,
94            height,
95            min_depth,
96            max_depth,
97        } => global.render_pass_set_viewport(pass, x, y, width, height, min_depth, max_depth),
98        RenderCommand::SetScissorRect {
99            x,
100            y,
101            width,
102            height,
103        } => global.render_pass_set_scissor_rect(pass, x, y, width, height),
104        RenderCommand::SetBlendConstant(color) => {
105            global.render_pass_set_blend_constant(pass, color)
106        },
107        RenderCommand::SetStencilReference(reference) => {
108            global.render_pass_set_stencil_reference(pass, reference)
109        },
110        RenderCommand::SetIndexBuffer {
111            buffer_id,
112            index_format,
113            offset,
114            size,
115        } => global.render_pass_set_index_buffer(pass, buffer_id, index_format, offset, size),
116        RenderCommand::SetVertexBuffer {
117            slot,
118            buffer_id,
119            offset,
120            size,
121        } => global.render_pass_set_vertex_buffer(pass, slot, buffer_id, offset, size),
122        RenderCommand::Draw {
123            vertex_count,
124            instance_count,
125            first_vertex,
126            first_instance,
127        } => global.render_pass_draw(
128            pass,
129            vertex_count,
130            instance_count,
131            first_vertex,
132            first_instance,
133        ),
134        RenderCommand::DrawIndexed {
135            index_count,
136            instance_count,
137            first_index,
138            base_vertex,
139            first_instance,
140        } => global.render_pass_draw_indexed(
141            pass,
142            index_count,
143            instance_count,
144            first_index,
145            base_vertex,
146            first_instance,
147        ),
148        RenderCommand::DrawIndirect { buffer_id, offset } => {
149            global.render_pass_draw_indirect(pass, buffer_id, offset)
150        },
151        RenderCommand::DrawIndexedIndirect { buffer_id, offset } => {
152            global.render_pass_draw_indexed_indirect(pass, buffer_id, offset)
153        },
154        RenderCommand::ExecuteBundles(bundles) => {
155            global.render_pass_execute_bundles(pass, &bundles)
156        },
157        RenderCommand::PushDebugGroup(label) => {
158            global.render_pass_push_debug_group(pass, &label, 0)
159        },
160        RenderCommand::PopDebugGroup => global.render_pass_pop_debug_group(pass),
161        RenderCommand::InsertDebugMarker(label) => {
162            global.render_pass_insert_debug_marker(pass, &label, 0)
163        },
164    }
165}