1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! Allows one to draw multiple geometry located in the same buffer.
//!
use std::ops::Deref;
use std::ops::DerefMut;
use std::os::raw;

use backend::Facade;
use buffer::{BufferCreationError, BufferType, BufferMode, Buffer};
use buffer::{BufferSlice, BufferMutSlice};
use index::{IndicesSource, PrimitiveType, IndexBuffer, Index};

/// Represents an element in a list of draw commands.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DrawCommandNoIndices {
    /// Number of vertices to draw.
    pub count: raw::c_uint,
    /// Number of instances to draw. If it's `0`, nothing will be drawn.
    pub instance_count: raw::c_uint,
    /// First vertex to draw in the vertices source.
    pub first_index: raw::c_uint,
    /// Numero of the first instance to draw.
    pub base_instance: raw::c_uint,
}

implement_uniform_block!(DrawCommandNoIndices, count, instance_count,
                         first_index, base_instance);

/// Represents an element in a list of draw commands.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DrawCommandIndices {
    /// Number of indices to use in the index buffer.
    pub count: raw::c_uint,
    /// Number of instances to draw. If it's `0`, nothing will be drawn.
    pub instance_count: raw::c_uint,
    /// First index to draw in the index buffer.
    pub first_index: raw::c_uint,
    /// Value to add to each index.
    pub base_vertex: raw::c_uint,
    /// Numero of the first instance to draw.
    pub base_instance: raw::c_uint,
}

implement_uniform_block!(DrawCommandIndices, count, instance_count, first_index,
                         base_vertex, base_instance);

/// A buffer containing a list of draw commands.
pub struct DrawCommandsNoIndicesBuffer {
    buffer: Buffer<[DrawCommandNoIndices]>,
}

impl DrawCommandsNoIndicesBuffer {
    /// Builds an empty buffer.
    ///
    /// The parameter indicates the number of elements.
    #[inline]
    pub fn empty<F: ?Sized>(facade: &F, elements: usize)
                    -> Result<DrawCommandsNoIndicesBuffer, BufferCreationError>
                    where F: Facade
    {
        let buf = try!(Buffer::empty_array(facade, BufferType::DrawIndirectBuffer,
                                               elements, BufferMode::Default));
        Ok(DrawCommandsNoIndicesBuffer { buffer: buf })
    }

    /// Builds an empty buffer.
    ///
    /// The parameter indicates the number of elements.
    #[inline]
    pub fn empty_dynamic<F: ?Sized>(facade: &F, elements: usize)
                            -> Result<DrawCommandsNoIndicesBuffer, BufferCreationError>
                            where F: Facade
    {
        let buf = try!(Buffer::empty_array(facade, BufferType::DrawIndirectBuffer,
                                               elements, BufferMode::Dynamic));
        Ok(DrawCommandsNoIndicesBuffer { buffer: buf })
    }

    /// Builds an empty buffer.
    ///
    /// The parameter indicates the number of elements.
    #[inline]
    pub fn empty_persistent<F: ?Sized>(facade: &F, elements: usize)
                               -> Result<DrawCommandsNoIndicesBuffer, BufferCreationError>
                               where F: Facade
    {
        let buf = try!(Buffer::empty_array(facade, BufferType::DrawIndirectBuffer,
                                               elements, BufferMode::Persistent));
        Ok(DrawCommandsNoIndicesBuffer { buffer: buf })
    }

    /// Builds an empty buffer.
    ///
    /// The parameter indicates the number of elements.
    #[inline]
    pub fn empty_immutable<F: ?Sized>(facade: &F, elements: usize)
                              -> Result<DrawCommandsNoIndicesBuffer, BufferCreationError>
                              where F: Facade
    {
        let buf = try!(Buffer::empty_array(facade, BufferType::DrawIndirectBuffer,
                                               elements, BufferMode::Immutable));
        Ok(DrawCommandsNoIndicesBuffer { buffer: buf })
    }

    /// Builds an indices source from this buffer and a primitives type. This indices source can
    /// be passed to the `draw()` function.
    #[inline]
    pub fn with_primitive_type(&self, primitives: PrimitiveType) -> IndicesSource {
        IndicesSource::MultidrawArray {
            buffer: self.buffer.as_slice_any(),
            primitives: primitives,
        }
    }
}

impl Deref for DrawCommandsNoIndicesBuffer {
    type Target = Buffer<[DrawCommandNoIndices]>;

    #[inline]
    fn deref(&self) -> &Buffer<[DrawCommandNoIndices]> {
        &self.buffer
    }
}

impl DerefMut for DrawCommandsNoIndicesBuffer {
    #[inline]
    fn deref_mut(&mut self) -> &mut Buffer<[DrawCommandNoIndices]> {
        &mut self.buffer
    }
}

impl<'a> From<&'a DrawCommandsNoIndicesBuffer> for BufferSlice<'a, [DrawCommandNoIndices]> {
    #[inline]
    fn from(b: &'a DrawCommandsNoIndicesBuffer) -> BufferSlice<'a, [DrawCommandNoIndices]> {
        let b: &Buffer<[DrawCommandNoIndices]> = &*b;
        b.as_slice()
    }
}

impl<'a> From<&'a mut DrawCommandsNoIndicesBuffer> for BufferMutSlice<'a, [DrawCommandNoIndices]> {
    #[inline]
    fn from(b: &'a mut DrawCommandsNoIndicesBuffer) -> BufferMutSlice<'a, [DrawCommandNoIndices]> {
        let b: &mut Buffer<[DrawCommandNoIndices]> = &mut *b;
        b.as_mut_slice()
    }
}

/// A buffer containing a list of draw commands.
pub struct DrawCommandsIndicesBuffer {
    buffer: Buffer<[DrawCommandIndices]>,
}

impl DrawCommandsIndicesBuffer {
    /// Builds an empty buffer.
    ///
    /// The parameter indicates the number of elements.
    #[inline]
    pub fn empty<F: ?Sized>(facade: &F, elements: usize)
                    -> Result<DrawCommandsIndicesBuffer, BufferCreationError>
                    where F: Facade
    {
        let buf = try!(Buffer::empty_array(facade, BufferType::DrawIndirectBuffer,
                                               elements, BufferMode::Default));
        Ok(DrawCommandsIndicesBuffer { buffer: buf })
    }

    /// Builds an empty buffer.
    ///
    /// The parameter indicates the number of elements.
    #[inline]
    pub fn empty_dynamic<F: ?Sized>(facade: &F, elements: usize)
                            -> Result<DrawCommandsIndicesBuffer, BufferCreationError>
                            where F: Facade
    {
        let buf = try!(Buffer::empty_array(facade, BufferType::DrawIndirectBuffer,
                                               elements, BufferMode::Dynamic));
        Ok(DrawCommandsIndicesBuffer { buffer: buf })
    }

    /// Builds an empty buffer.
    ///
    /// The parameter indicates the number of elements.
    #[inline]
    pub fn empty_persistent<F: ?Sized>(facade: &F, elements: usize)
                               -> Result<DrawCommandsIndicesBuffer, BufferCreationError>
                               where F: Facade
    {
        let buf = try!(Buffer::empty_array(facade, BufferType::DrawIndirectBuffer,
                                               elements, BufferMode::Persistent));
        Ok(DrawCommandsIndicesBuffer { buffer: buf })
    }

    /// Builds an empty buffer.
    ///
    /// The parameter indicates the number of elements.
    #[inline]
    pub fn empty_immutable<F: ?Sized>(facade: &F, elements: usize)
                              -> Result<DrawCommandsIndicesBuffer, BufferCreationError>
                              where F: Facade
    {
        let buf = try!(Buffer::empty_array(facade, BufferType::DrawIndirectBuffer,
                                               elements, BufferMode::Immutable));
        Ok(DrawCommandsIndicesBuffer { buffer: buf })
    }

    /// Builds an indices source from this buffer and a primitives type. This indices source can
    /// be passed to the `draw()` function.
    #[inline]
    pub fn with_index_buffer<'a, T>(&'a self, index_buffer: &'a IndexBuffer<T>)
                                    -> IndicesSource<'a> where T: Index
    {
        IndicesSource::MultidrawElement {
            commands: self.buffer.as_slice_any(),
            indices: index_buffer.as_slice_any(),
            data_type: index_buffer.get_indices_type(),
            primitives: index_buffer.get_primitives_type(),
        }
    }
}

impl Deref for DrawCommandsIndicesBuffer {
    type Target = Buffer<[DrawCommandIndices]>;

    #[inline]
    fn deref(&self) -> &Buffer<[DrawCommandIndices]> {
        &self.buffer
    }
}

impl DerefMut for DrawCommandsIndicesBuffer {
    #[inline]
    fn deref_mut(&mut self) -> &mut Buffer<[DrawCommandIndices]> {
        &mut self.buffer
    }
}

impl<'a> From<&'a DrawCommandsIndicesBuffer> for BufferSlice<'a, [DrawCommandIndices]> {
    #[inline]
    fn from(b: &'a DrawCommandsIndicesBuffer) -> BufferSlice<'a, [DrawCommandIndices]> {
        let b: &Buffer<[DrawCommandIndices]> = &*b;
        b.as_slice()
    }
}

impl<'a> From<&'a mut DrawCommandsIndicesBuffer> for BufferMutSlice<'a, [DrawCommandIndices]> {
    #[inline]
    fn from(b: &'a mut DrawCommandsIndicesBuffer) -> BufferMutSlice<'a, [DrawCommandIndices]> {
        let b: &mut Buffer<[DrawCommandIndices]> = &mut *b;
        b.as_mut_slice()
    }
}