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