Skip to main content

webgpu/
encoders.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
5use webgpu_traits::id::{
6    CommandEncoderId, ComputePassEncoderId, RenderBundleEncoderId, RenderPassEncoderId,
7};
8use webgpu_traits::{
9    BindingCommand, CommandEncoderCommand, ComputePassEncoderCommand, DebugCommand,
10    RenderBundleEncoderCommand, RenderCommand, RenderPassEncoderCommand,
11};
12use wgpu_core::command::{EncoderStateError, PassStateError};
13use wgpu_core::global::Global;
14
15pub(crate) fn handle_render_pass_command(
16    global: &Global,
17    render_pass_id: RenderPassEncoderId,
18    command: RenderPassEncoderCommand,
19) -> Result<(), PassStateError> {
20    match command {
21        RenderPassEncoderCommand::SetViewport {
22            x,
23            y,
24            width,
25            height,
26            min_depth,
27            max_depth,
28        } => global.render_pass_set_viewport_with_id(
29            render_pass_id,
30            x,
31            y,
32            width,
33            height,
34            min_depth,
35            max_depth,
36        ),
37        RenderPassEncoderCommand::SetScissorRect {
38            x,
39            y,
40            width,
41            height,
42        } => global.render_pass_set_scissor_rect_with_id(render_pass_id, x, y, width, height),
43        RenderPassEncoderCommand::SetBlendConstant(color) => {
44            global.render_pass_set_blend_constant_with_id(render_pass_id, color)
45        },
46        RenderPassEncoderCommand::SetStencilReference(value) => {
47            global.render_pass_set_stencil_reference_with_id(render_pass_id, value)
48        },
49        RenderPassEncoderCommand::BeginOcclusionQuery(query_index) => {
50            global.render_pass_begin_occlusion_query_with_id(render_pass_id, query_index)
51        },
52        RenderPassEncoderCommand::EndOcclusionQuery => {
53            global.render_pass_end_occlusion_query_with_id(render_pass_id)
54        },
55        RenderPassEncoderCommand::ExecuteBundles(ids) => {
56            global.render_pass_execute_bundles_with_id(render_pass_id, &ids)
57        },
58        RenderPassEncoderCommand::BindingCommand(binding_command) => match binding_command {
59            BindingCommand::SetBindGroup {
60                index,
61                bind_group,
62                dynamic_offsets,
63            } => global.render_pass_set_bind_group_with_id(
64                render_pass_id,
65                index,
66                bind_group,
67                &dynamic_offsets,
68            ),
69            BindingCommand::SetImmediates { range_offset, data } => {
70                global.render_pass_set_immediates_with_id(render_pass_id, range_offset, &data)
71            },
72        },
73        RenderPassEncoderCommand::RenderCommand(render_command) => match render_command {
74            RenderCommand::SetPipeline(id) => {
75                global.render_pass_set_pipeline_with_id(render_pass_id, id)
76            },
77            RenderCommand::SetIndexBuffer {
78                buffer,
79                index_format,
80                offset,
81                size,
82            } => global.render_pass_set_index_buffer_with_id(
83                render_pass_id,
84                buffer,
85                index_format,
86                offset,
87                size,
88            ),
89            RenderCommand::SetVertexBuffer {
90                slot,
91                buffer,
92                offset,
93                size,
94            } => global.render_pass_set_vertex_buffer_with_id(
95                render_pass_id,
96                slot,
97                buffer,
98                offset,
99                size,
100            ),
101            RenderCommand::Draw {
102                vertex_count,
103                instance_count,
104                first_vertex,
105                first_instance,
106            } => global.render_pass_draw_with_id(
107                render_pass_id,
108                vertex_count,
109                instance_count,
110                first_vertex,
111                first_instance,
112            ),
113            RenderCommand::DrawIndexed {
114                index_count,
115                instance_count,
116                first_index,
117                base_vertex,
118                first_instance,
119            } => global.render_pass_draw_indexed_with_id(
120                render_pass_id,
121                index_count,
122                instance_count,
123                first_index,
124                base_vertex,
125                first_instance,
126            ),
127            RenderCommand::DrawIndirect {
128                indirect_buffer,
129                indirect_offset,
130            } => global.render_pass_draw_indirect_with_id(
131                render_pass_id,
132                indirect_buffer,
133                indirect_offset,
134            ),
135            RenderCommand::DrawIndexedIndirect {
136                indirect_buffer,
137                indirect_offset,
138            } => global.render_pass_draw_indexed_indirect_with_id(
139                render_pass_id,
140                indirect_buffer,
141                indirect_offset,
142            ),
143        },
144        RenderPassEncoderCommand::DebugCommand(debug_command) => match debug_command {
145            DebugCommand::PushDebugGroup(label) => {
146                global.render_pass_push_debug_group_with_id(render_pass_id, &label, 0)
147            },
148            DebugCommand::PopDebugGroup => {
149                global.render_pass_pop_debug_group_with_id(render_pass_id)
150            },
151            DebugCommand::InsertDebugMarker(label) => {
152                global.render_pass_insert_debug_marker_with_id(render_pass_id, &label, 0)
153            },
154        },
155    }
156}
157
158pub(crate) fn handle_render_bundle_command(
159    global: &Global,
160    render_bundle_encoder_id: RenderBundleEncoderId,
161    command: RenderBundleEncoderCommand,
162) -> Result<(), PassStateError> {
163    match command {
164        RenderBundleEncoderCommand::BindingCommand(binding_command) => match binding_command {
165            BindingCommand::SetBindGroup {
166                index,
167                bind_group,
168                dynamic_offsets,
169            } => global.render_bundle_encoder_set_bind_group_with_id(
170                render_bundle_encoder_id,
171                index,
172                bind_group,
173                &dynamic_offsets,
174            ),
175            BindingCommand::SetImmediates { range_offset, data } => global
176                .render_bundle_encoder_set_immediates_with_id(
177                    render_bundle_encoder_id,
178                    range_offset,
179                    &data,
180                ),
181        },
182        RenderBundleEncoderCommand::RenderCommand(render_command) => match render_command {
183            RenderCommand::SetPipeline(id) => {
184                global.render_bundle_encoder_set_pipeline_with_id(render_bundle_encoder_id, id)
185            },
186            RenderCommand::SetIndexBuffer {
187                buffer,
188                index_format,
189                offset,
190                size,
191            } => global.render_bundle_encoder_set_index_buffer_with_id(
192                render_bundle_encoder_id,
193                buffer,
194                index_format,
195                offset,
196                size,
197            ),
198            RenderCommand::SetVertexBuffer {
199                slot,
200                buffer,
201                offset,
202                size,
203            } => global.render_bundle_encoder_set_vertex_buffer_with_id(
204                render_bundle_encoder_id,
205                slot,
206                buffer,
207                offset,
208                size,
209            ),
210            RenderCommand::Draw {
211                vertex_count,
212                instance_count,
213                first_vertex,
214                first_instance,
215            } => global.render_bundle_encoder_draw_with_id(
216                render_bundle_encoder_id,
217                vertex_count,
218                instance_count,
219                first_vertex,
220                first_instance,
221            ),
222            RenderCommand::DrawIndexed {
223                index_count,
224                instance_count,
225                first_index,
226                base_vertex,
227                first_instance,
228            } => global.render_bundle_encoder_draw_indexed_with_id(
229                render_bundle_encoder_id,
230                index_count,
231                instance_count,
232                first_index,
233                base_vertex,
234                first_instance,
235            ),
236            RenderCommand::DrawIndirect {
237                indirect_buffer,
238                indirect_offset,
239            } => global.render_bundle_encoder_draw_indirect_with_id(
240                render_bundle_encoder_id,
241                indirect_buffer,
242                indirect_offset,
243            ),
244            RenderCommand::DrawIndexedIndirect {
245                indirect_buffer,
246                indirect_offset,
247            } => global.render_bundle_encoder_draw_indexed_indirect_with_id(
248                render_bundle_encoder_id,
249                indirect_buffer,
250                indirect_offset,
251            ),
252        },
253        RenderBundleEncoderCommand::DebugCommand(debug_command) => match debug_command {
254            DebugCommand::PushDebugGroup(label) => global
255                .render_bundle_encoder_push_debug_group_with_id(render_bundle_encoder_id, &label),
256            DebugCommand::PopDebugGroup => {
257                global.render_bundle_encoder_pop_debug_group_with_id(render_bundle_encoder_id)
258            },
259            DebugCommand::InsertDebugMarker(label) => global
260                .render_bundle_encoder_insert_debug_marker_with_id(
261                    render_bundle_encoder_id,
262                    &label,
263                ),
264        },
265    }
266}
267
268pub(crate) fn handle_compute_pass_command(
269    global: &Global,
270    compute_pass_id: ComputePassEncoderId,
271    command: ComputePassEncoderCommand,
272) -> Result<(), PassStateError> {
273    match command {
274        ComputePassEncoderCommand::BindingCommand(binding_command) => match binding_command {
275            BindingCommand::SetBindGroup {
276                index,
277                bind_group,
278                dynamic_offsets,
279            } => global.compute_pass_set_bind_group_with_id(
280                compute_pass_id,
281                index,
282                bind_group,
283                &dynamic_offsets,
284            ),
285            BindingCommand::SetImmediates { range_offset, data } => {
286                global.compute_pass_set_immediates_with_id(compute_pass_id, range_offset, &data)
287            },
288        },
289        ComputePassEncoderCommand::SetPipeline(pipeline) => {
290            global.compute_pass_set_pipeline_with_id(compute_pass_id, pipeline)
291        },
292        ComputePassEncoderCommand::DispatchWorkgroups {
293            workgroup_count_x,
294            workgroup_count_y,
295            workgroup_count_z,
296        } => global.compute_pass_dispatch_workgroups_with_id(
297            compute_pass_id,
298            workgroup_count_x,
299            workgroup_count_y,
300            workgroup_count_z,
301        ),
302        ComputePassEncoderCommand::DispatchWorkgroupsIndirect {
303            indirect_buffer,
304            indirect_offset,
305        } => global.compute_pass_dispatch_workgroups_indirect_with_id(
306            compute_pass_id,
307            indirect_buffer,
308            indirect_offset,
309        ),
310        ComputePassEncoderCommand::DebugCommand(debug_command) => match debug_command {
311            DebugCommand::PushDebugGroup(label) => {
312                global.compute_pass_push_debug_group_with_id(compute_pass_id, &label, 0)
313            },
314            DebugCommand::PopDebugGroup => {
315                global.compute_pass_pop_debug_group_with_id(compute_pass_id)
316            },
317            DebugCommand::InsertDebugMarker(label) => {
318                global.compute_pass_insert_debug_marker_with_id(compute_pass_id, &label, 0)
319            },
320        },
321    }
322}
323
324pub(crate) fn handle_command_encoder_command(
325    global: &Global,
326    command_encoder_id: CommandEncoderId,
327    command: CommandEncoderCommand,
328) -> Result<(), EncoderStateError> {
329    match command {
330        CommandEncoderCommand::CopyBufferToBuffer {
331            source,
332            source_offset,
333            destination,
334            destination_offset,
335            size,
336        } => global.command_encoder_copy_buffer_to_buffer(
337            command_encoder_id,
338            source,
339            source_offset,
340            destination,
341            destination_offset,
342            size,
343        ),
344        CommandEncoderCommand::CopyBufferToTexture {
345            source,
346            destination,
347            copy_size,
348        } => global.command_encoder_copy_buffer_to_texture(
349            command_encoder_id,
350            &source,
351            &destination,
352            &copy_size,
353        ),
354        CommandEncoderCommand::CopyTextureToBuffer {
355            source,
356            destination,
357            copy_size,
358        } => global.command_encoder_copy_texture_to_buffer(
359            command_encoder_id,
360            &source,
361            &destination,
362            &copy_size,
363        ),
364        CommandEncoderCommand::CopyTextureToTexture {
365            source,
366            destination,
367            copy_size,
368        } => global.command_encoder_copy_texture_to_texture(
369            command_encoder_id,
370            &source,
371            &destination,
372            &copy_size,
373        ),
374        CommandEncoderCommand::ClearBuffer {
375            buffer,
376            offset,
377            size,
378        } => global.command_encoder_clear_buffer(command_encoder_id, buffer, offset, size),
379        CommandEncoderCommand::ResolveQuerySet {
380            query_set,
381            first_query,
382            query_count,
383            destination,
384            destination_offset,
385        } => global.command_encoder_resolve_query_set(
386            command_encoder_id,
387            query_set,
388            first_query,
389            query_count,
390            destination,
391            destination_offset,
392        ),
393        CommandEncoderCommand::DebugCommand(debug_command) => match debug_command {
394            DebugCommand::PushDebugGroup(label) => {
395                global.command_encoder_push_debug_group(command_encoder_id, &label)
396            },
397            DebugCommand::PopDebugGroup => {
398                global.command_encoder_pop_debug_group(command_encoder_id)
399            },
400            DebugCommand::InsertDebugMarker(label) => {
401                global.command_encoder_insert_debug_marker(command_encoder_id, &label)
402            },
403        },
404    }
405}