wgpu/util/
indirect.rs

1/// The structure expected in `indirect_buffer` for [`RenderEncoder::draw_indirect`](crate::util::RenderEncoder::draw_indirect).
2#[repr(C)]
3#[derive(Copy, Clone, Debug, Default)]
4pub struct DrawIndirect {
5    /// The number of vertices to draw.
6    pub vertex_count: u32,
7    /// The number of instances to draw.
8    pub instance_count: u32,
9    /// The Index of the first vertex to draw.
10    pub base_vertex: u32,
11    /// The instance ID of the first instance to draw.
12    /// Has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`](crate::Features::INDIRECT_FIRST_INSTANCE) is enabled.
13    pub base_instance: u32,
14}
15
16impl DrawIndirect {
17    /// Returns the bytes representation of the struct, ready to be written in a [`Buffer`](crate::Buffer).
18    pub fn as_bytes(&self) -> &[u8] {
19        unsafe {
20            std::mem::transmute(std::slice::from_raw_parts(
21                self as *const _ as *const u8,
22                std::mem::size_of::<Self>(),
23            ))
24        }
25    }
26}
27
28/// The structure expected in `indirect_buffer` for [`RenderEncoder::draw_indexed_indirect`](crate::util::RenderEncoder::draw_indexed_indirect).
29#[repr(C)]
30#[derive(Copy, Clone, Debug, Default)]
31pub struct DrawIndexedIndirect {
32    /// The number of vertices to draw.
33    pub vertex_count: u32,
34    /// The number of instances to draw.
35    pub instance_count: u32,
36    /// The base index within the index buffer.
37    pub base_index: u32,
38    /// The value added to the vertex index before indexing into the vertex buffer.
39    pub vertex_offset: i32,
40    /// The instance ID of the first instance to draw.
41    /// Has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`](crate::Features::INDIRECT_FIRST_INSTANCE) is enabled.
42    pub base_instance: u32,
43}
44
45impl DrawIndexedIndirect {
46    /// Returns the bytes representation of the struct, ready to be written in a [`Buffer`](crate::Buffer).
47    pub fn as_bytes(&self) -> &[u8] {
48        unsafe {
49            std::mem::transmute(std::slice::from_raw_parts(
50                self as *const _ as *const u8,
51                std::mem::size_of::<Self>(),
52            ))
53        }
54    }
55}
56
57/// The structure expected in `indirect_buffer` for [`ComputePass::dispatch_workgroups_indirect`](crate::ComputePass::dispatch_workgroups_indirect).
58///
59/// x, y and z denote the number of work groups to dispatch in each dimension.
60#[repr(C)]
61#[derive(Copy, Clone, Debug, Default)]
62pub struct DispatchIndirect {
63    /// The number of work groups in X dimension.
64    pub x: u32,
65    /// The number of work groups in Y dimension.
66    pub y: u32,
67    /// The number of work groups in Z dimension.
68    pub z: u32,
69}
70
71impl DispatchIndirect {
72    /// Returns the bytes representation of the struct, ready to be written in a [`Buffer`](crate::Buffer).
73    pub fn as_bytes(&self) -> &[u8] {
74        unsafe {
75            std::mem::transmute(std::slice::from_raw_parts(
76                self as *const _ as *const u8,
77                std::mem::size_of::<Self>(),
78            ))
79        }
80    }
81}