1use serde::{Deserialize, Serialize};
8use wgpu_core::command::{PassStateError, RenderPass};
9use wgpu_core::global::Global;
10use wgpu_core::id::{BindGroupId, BufferId, RenderBundleId, RenderPipelineId};
11
12#[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}
72
73pub fn apply_render_command(
74 global: &Global,
75 pass: &mut RenderPass,
76 command: RenderCommand,
77) -> Result<(), PassStateError> {
78 match command {
79 RenderCommand::SetPipeline(pipeline_id) => {
80 global.render_pass_set_pipeline(pass, pipeline_id)
81 },
82 RenderCommand::SetBindGroup {
83 index,
84 bind_group_id,
85 offsets,
86 } => global.render_pass_set_bind_group(pass, index, Some(bind_group_id), &offsets),
87 RenderCommand::SetViewport {
88 x,
89 y,
90 width,
91 height,
92 min_depth,
93 max_depth,
94 } => global.render_pass_set_viewport(pass, x, y, width, height, min_depth, max_depth),
95 RenderCommand::SetScissorRect {
96 x,
97 y,
98 width,
99 height,
100 } => global.render_pass_set_scissor_rect(pass, x, y, width, height),
101 RenderCommand::SetBlendConstant(color) => {
102 global.render_pass_set_blend_constant(pass, color)
103 },
104 RenderCommand::SetStencilReference(reference) => {
105 global.render_pass_set_stencil_reference(pass, reference)
106 },
107 RenderCommand::SetIndexBuffer {
108 buffer_id,
109 index_format,
110 offset,
111 size,
112 } => global.render_pass_set_index_buffer(pass, buffer_id, index_format, offset, size),
113 RenderCommand::SetVertexBuffer {
114 slot,
115 buffer_id,
116 offset,
117 size,
118 } => global.render_pass_set_vertex_buffer(pass, slot, buffer_id, offset, size),
119 RenderCommand::Draw {
120 vertex_count,
121 instance_count,
122 first_vertex,
123 first_instance,
124 } => global.render_pass_draw(
125 pass,
126 vertex_count,
127 instance_count,
128 first_vertex,
129 first_instance,
130 ),
131 RenderCommand::DrawIndexed {
132 index_count,
133 instance_count,
134 first_index,
135 base_vertex,
136 first_instance,
137 } => global.render_pass_draw_indexed(
138 pass,
139 index_count,
140 instance_count,
141 first_index,
142 base_vertex,
143 first_instance,
144 ),
145 RenderCommand::DrawIndirect { buffer_id, offset } => {
146 global.render_pass_draw_indirect(pass, buffer_id, offset)
147 },
148 RenderCommand::DrawIndexedIndirect { buffer_id, offset } => {
149 global.render_pass_draw_indexed_indirect(pass, buffer_id, offset)
150 },
151 RenderCommand::ExecuteBundles(bundles) => {
152 global.render_pass_execute_bundles(pass, &bundles)
153 },
154 }
155}