[go: up one dir, main page]

glium 0.3.3

High-level and safe OpenGL wrapper.
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use std::borrow::Borrow;
use std::cell::RefCell;
use std::collections::HashMap;
use std::mem;

use Handle;
use program::Program;
use vertex::AttributeType;
use vertex::VertexBufferAny;
use vertex::VertexFormat;
use GlObject;

use {libc, gl};
use context::CommandContext;
use version::Api;
use version::Version;

/// Stores and handles vertex attributes.
pub struct VertexAttributesSystem {
    // we maintain a list of VAOs for each vertexbuffer-indexbuffer-program association
    // the key is a (buffers-list-with-offset, program) ; the buffers list must be sorted
    vaos: RefCell<HashMap<(Vec<(gl::types::GLuint, usize)>, Handle), VertexArrayObject>>,
}

/// Object allowing one to bind vertex attributes to the current context.
pub struct Binder<'a, 'c, 'd: 'c> {
    context: &'c mut CommandContext<'d, 'd>,
    program: &'a Program,
    system: &'a VertexAttributesSystem,
    element_array_buffer: gl::types::GLuint,
    vertex_buffers: Vec<(gl::types::GLuint, VertexFormat, usize, usize, Option<u32>)>,
}

impl VertexAttributesSystem {
    /// Builds a new `VertexAttributesSystem`.
    pub fn new() -> VertexAttributesSystem {
        VertexAttributesSystem {
            vaos: RefCell::new(HashMap::new()),
        }
    }

    /// Starts the process of binding vertex attributes.
    pub fn start<'a, 'c, 'd>(&'a self, ctxt: &'c mut CommandContext<'d, 'd>, program: &'a Program,
                             indices: gl::types::GLuint) -> Binder<'a, 'c, 'd>
    {
        Binder {
            context: ctxt,
            program: program,
            system: self,
            element_array_buffer: indices,
            vertex_buffers: Vec::with_capacity(1),
        }
    }

    /// This function *must* be called whenever you destroy a buffer so that the system can
    /// purge its VAOs cache.
    pub fn purge_buffer(&self, ctxt: &mut CommandContext, id: gl::types::GLuint) {
        self.purge_if(ctxt, |&(ref buffers, _)| {
            buffers.iter().find(|&&(b, _)| b == id).is_some()
        })
    }

    /// This function *must* be called whenever you destroy a program so that the system can
    /// purge its VAOs cache.
    pub fn purge_program(&self, ctxt: &mut CommandContext, program: Handle) {
        self.purge_if(ctxt, |&(_, p)| p == program)
    }

    /// Purges the VAOs cache.
    pub fn purge_all(&self, ctxt: &mut CommandContext) {
        let vaos = mem::replace(&mut *self.vaos.borrow_mut(),
                                HashMap::new());

        for (_, vao) in vaos {
            vao.destroy(ctxt);
        }
    }

    /// Purges the VAOs cache. Contrary to `purge_all`, this function expects the system to be
    /// destroyed soon.
    pub fn cleanup(&mut self, ctxt: &mut CommandContext) {
        let vaos = mem::replace(&mut *self.vaos.borrow_mut(),
                                HashMap::with_capacity(0));

        for (_, vao) in vaos {
            vao.destroy(ctxt);
        }
    }

    /// Purges VAOs that match a certain condition.
    fn purge_if<F>(&self, ctxt: &mut CommandContext, mut condition: F)
                   where F: FnMut(&(Vec<(gl::types::GLuint, usize)>, Handle)) -> bool
    {
        let mut vaos = self.vaos.borrow_mut();

        let mut keys = Vec::with_capacity(4);
        for (key, _) in &*vaos {
            if condition(key) {
                keys.push(key.clone());
            }
        }

        for key in keys {
            vaos.remove(&key).unwrap().destroy(ctxt);
        }
    }
}

impl<'a, 'c, 'd: 'c> Binder<'a, 'c, 'd> {
    /// Adds a buffer to bind as a source of vertices.
    ///
    /// # Parameters
    ///
    /// - `buffer`: The buffer to bind.
    /// - `first`: Offset of the first element of the buffer in number of elements.
    /// - `divisor`: If `Some`, use this value for `glVertexAttribDivisor` (instancing-related).
    pub fn add(mut self, buffer: &VertexBufferAny, first: usize, divisor: Option<u32>)
               -> Binder<'a, 'c, 'd>
    {
        let (buffer, format, stride) = (buffer.get_id(), buffer.get_bindings().clone(),
                                        buffer.get_elements_size());

        self.vertex_buffers.push((buffer, format, first * stride, stride, divisor));
        self
    }

    /// Finish binding the vertex attributes.
    pub fn bind(self) {
        let ctxt = self.context;

        if ctxt.version >= &Version(Api::Gl, 3, 0) || ctxt.version >= &Version(Api::GlEs, 3, 0) ||
           ctxt.extensions.gl_arb_vertex_array_object || ctxt.extensions.gl_oes_vertex_array_object
           || ctxt.extensions.gl_apple_vertex_array_object
        {
            // VAOs are supported
            let mut buffers_list: Vec<_> = self.vertex_buffers.iter()
                                                              .map(|&(v, _, o, _, _)| (v, o))
                                                              .collect();
            buffers_list.push((self.element_array_buffer, 0));
            buffers_list.sort();

            let program_id = self.program.get_id();

            // trying to find an existing VAO in the cache
            if let Some(value) = self.system.vaos.borrow_mut()
                                     .get(&(buffers_list.clone(), program_id))
            {
                bind_vao(ctxt, value.id);
                return;
            }

            // if not found, building a new one
            let new_vao = unsafe {
                VertexArrayObject::new(ctxt, &self.vertex_buffers,
                                       self.element_array_buffer, self.program)
            };

            bind_vao(ctxt, new_vao.id);
            self.system.vaos.borrow_mut().insert((buffers_list, program_id), new_vao);

        } else {
            // VAOs are not supported

            // just in case
            if ctxt.state.vertex_array != 0 {
                bind_vao(ctxt, 0);
                ctxt.state.vertex_array = 0;
            }

            unsafe {
                if ctxt.version >= &Version(Api::Gl, 1, 5) ||
                    ctxt.version >= &Version(Api::GlEs, 2, 0)
                {
                    ctxt.gl.BindBuffer(gl::ELEMENT_ARRAY_BUFFER, self.element_array_buffer);
                } else if ctxt.extensions.gl_arb_vertex_buffer_object {
                    ctxt.gl.BindBufferARB(gl::ELEMENT_ARRAY_BUFFER_ARB, self.element_array_buffer);
                } else {
                    unreachable!();
                }
            }

            for (vertex_buffer, bindings, offset, stride, divisor) in self.vertex_buffers {
                unsafe {
                    bind_attribute(ctxt, self.program, vertex_buffer, &bindings, offset, stride,
                                   divisor);
                }
            }
        }
    }
}

/// Stores informations about how to bind a vertex buffer, an index buffer and a program.
struct VertexArrayObject {
    id: gl::types::GLuint,
    destroyed: bool,
}

impl VertexArrayObject {
    /// Builds a new `VertexArrayObject`.
    ///
    /// The vertex buffer, index buffer and program must not outlive the
    /// VAO, and the VB & program attributes must not change.
    unsafe fn new(mut ctxt: &mut CommandContext,
                  vertex_buffers: &[(gl::types::GLuint, VertexFormat, usize, usize, Option<u32>)],
                  ib_id: gl::types::GLuint, program: &Program) -> VertexArrayObject
    {
        // checking the attributes types
        for &(_, ref bindings, _, _, _) in vertex_buffers {
            for &(ref name, _, ty) in bindings {
                let attribute = match program.get_attribute(Borrow::<str>::borrow(name)) {
                    Some(a) => a,
                    None => continue
                };

                if ty.get_num_components() != attribute.ty.get_num_components() ||
                    attribute.size != 1
                {
                    panic!("The program attribute `{}` does not match the vertex format. \
                            Program expected {:?}, got {:?}.", name, attribute.ty, ty);
                }
            }
        }

        // checking for missing attributes
        for (&ref name, _) in program.attributes() {
            let mut found = false;
            for &(_, ref bindings, _, _, _) in vertex_buffers {
                if bindings.iter().find(|&&(ref n, _, _)| n == name).is_some() {
                    found = true;
                    break;
                }
            }
            if !found {
                panic!("The program attribute `{}` is missing in the vertex bindings", name);
            }
        };

        // TODO: check for collisions between the vertices sources

        // building the VAO
        let id = {
            let mut id = mem::uninitialized();
            if ctxt.version >= &Version(Api::Gl, 3, 0) ||
                ctxt.version >= &Version(Api::GlEs, 3, 0) ||
                ctxt.extensions.gl_arb_vertex_array_object
            {
                ctxt.gl.GenVertexArrays(1, &mut id);
            } else if ctxt.extensions.gl_oes_vertex_array_object {
                ctxt.gl.GenVertexArraysOES(1, &mut id);
            } else if ctxt.extensions.gl_apple_vertex_array_object {
                ctxt.gl.GenVertexArraysAPPLE(1, &mut id);
            } else {
                unreachable!();
            };
            id
        };

        // we don't use DSA as we're going to make multiple calls for this VAO
        // and we're likely going to use the VAO right after it's been created
        bind_vao(&mut ctxt, id);

        // binding index buffer
        // the ELEMENT_ARRAY_BUFFER is part of the state of the VAO
        // TODO: use a proper function
        if ctxt.version >= &Version(Api::Gl, 1, 5) ||
            ctxt.version >= &Version(Api::GlEs, 2, 0)
        {
            ctxt.gl.BindBuffer(gl::ELEMENT_ARRAY_BUFFER, ib_id);
        } else if ctxt.extensions.gl_arb_vertex_buffer_object {
            ctxt.gl.BindBufferARB(gl::ELEMENT_ARRAY_BUFFER_ARB, ib_id);
        } else {
            unreachable!();
        }

        for &(vertex_buffer, ref bindings, offset, stride, divisor) in vertex_buffers {
            bind_attribute(ctxt, program, vertex_buffer, bindings, offset, stride, divisor);
        }

        VertexArrayObject {
            id: id,
            destroyed: false,
        }
    }

    /// Must be called to destroy the VAO (otherwise its destructor will panic as a safety
    /// measure).
    fn destroy(mut self, mut ctxt: &mut CommandContext) {
        self.destroyed = true;

        unsafe {
            // unbinding
            if ctxt.state.vertex_array == self.id {
                bind_vao(ctxt, 0);
                ctxt.state.vertex_array = 0;
            }

            // deleting
            if ctxt.version >= &Version(Api::Gl, 3, 0) ||
                ctxt.version >= &Version(Api::GlEs, 3, 0) ||
                ctxt.extensions.gl_arb_vertex_array_object
            {
                ctxt.gl.DeleteVertexArrays(1, [ self.id ].as_ptr());
            } else if ctxt.extensions.gl_oes_vertex_array_object {
                ctxt.gl.DeleteVertexArraysOES(1, [ self.id ].as_ptr());
            } else if ctxt.extensions.gl_apple_vertex_array_object {
                ctxt.gl.DeleteVertexArraysAPPLE(1, [ self.id ].as_ptr());
            } else {
                unreachable!();
            }
        }
    }
}

impl Drop for VertexArrayObject {
    fn drop(&mut self) {
        assert!(self.destroyed);
    }
}

impl GlObject for VertexArrayObject {
    type Id = gl::types::GLuint;

    fn get_id(&self) -> gl::types::GLuint {
        self.id
    }
}

fn vertex_binding_type_to_gl(ty: AttributeType) -> (gl::types::GLenum, gl::types::GLint) {
    match ty {
        AttributeType::I8 => (gl::BYTE, 1),
        AttributeType::I8I8 => (gl::BYTE, 2),
        AttributeType::I8I8I8 => (gl::BYTE, 3),
        AttributeType::I8I8I8I8 => (gl::BYTE, 4),
        AttributeType::U8 => (gl::UNSIGNED_BYTE, 1),
        AttributeType::U8U8 => (gl::UNSIGNED_BYTE, 2),
        AttributeType::U8U8U8 => (gl::UNSIGNED_BYTE, 3),
        AttributeType::U8U8U8U8 => (gl::UNSIGNED_BYTE, 4),
        AttributeType::I16 => (gl::SHORT, 1),
        AttributeType::I16I16 => (gl::SHORT, 2),
        AttributeType::I16I16I16 => (gl::SHORT, 3),
        AttributeType::I16I16I16I16 => (gl::SHORT, 4),
        AttributeType::U16 => (gl::UNSIGNED_SHORT, 1),
        AttributeType::U16U16 => (gl::UNSIGNED_SHORT, 2),
        AttributeType::U16U16U16 => (gl::UNSIGNED_SHORT, 3),
        AttributeType::U16U16U16U16 => (gl::UNSIGNED_SHORT, 4),
        AttributeType::I32 => (gl::INT, 1),
        AttributeType::I32I32 => (gl::INT, 2),
        AttributeType::I32I32I32 => (gl::INT, 3),
        AttributeType::I32I32I32I32 => (gl::INT, 4),
        AttributeType::U32 => (gl::UNSIGNED_INT, 1),
        AttributeType::U32U32 => (gl::UNSIGNED_INT, 2),
        AttributeType::U32U32U32 => (gl::UNSIGNED_INT, 3),
        AttributeType::U32U32U32U32 => (gl::UNSIGNED_INT, 4),
        AttributeType::F32 => (gl::FLOAT, 1),
        AttributeType::F32F32 => (gl::FLOAT, 2),
        AttributeType::F32F32F32 => (gl::FLOAT, 3),
        AttributeType::F32F32F32F32 => (gl::FLOAT, 4),
        AttributeType::F32x2x2 => (gl::FLOAT_MAT2, 1),
        AttributeType::F32x2x3 => (gl::FLOAT_MAT2x3, 1),
        AttributeType::F32x2x4 => (gl::FLOAT_MAT2x4, 1),
        AttributeType::F32x3x2 => (gl::FLOAT_MAT3x2, 1),
        AttributeType::F32x3x3 => (gl::FLOAT_MAT3, 1),
        AttributeType::F32x3x4 => (gl::FLOAT_MAT3x4, 1),
        AttributeType::F32x4x2 => (gl::FLOAT_MAT4x2, 1),
        AttributeType::F32x4x3 => (gl::FLOAT_MAT4x3, 1),
        AttributeType::F32x4x4 => (gl::FLOAT_MAT4, 1),
        AttributeType::F64 => (gl::DOUBLE, 1),
        AttributeType::F64F64 => (gl::DOUBLE, 2),
        AttributeType::F64F64F64 => (gl::DOUBLE, 3),
        AttributeType::F64F64F64F64 => (gl::DOUBLE, 4),
        AttributeType::F64x2x2 => (gl::DOUBLE_MAT2, 1),
        AttributeType::F64x2x3 => (gl::DOUBLE_MAT2x3, 1),
        AttributeType::F64x2x4 => (gl::DOUBLE_MAT2x4, 1),
        AttributeType::F64x3x2 => (gl::DOUBLE_MAT3x2, 1),
        AttributeType::F64x3x3 => (gl::DOUBLE_MAT3, 1),
        AttributeType::F64x3x4 => (gl::DOUBLE_MAT3x4, 1),
        AttributeType::F64x4x2 => (gl::DOUBLE_MAT4x2, 1),
        AttributeType::F64x4x3 => (gl::DOUBLE_MAT4x3, 1),
        AttributeType::F64x4x4 => (gl::DOUBLE_MAT4, 1),
    }
}

/// Binds the vertex array object as the current one. Unbinds if `0` is passed.
///
/// ## Panic
///
/// Panics if the backend doesn't support vertex array objects.
fn bind_vao(ctxt: &mut CommandContext, vao_id: gl::types::GLuint) {
    if ctxt.state.vertex_array != vao_id {
        if ctxt.version >= &Version(Api::Gl, 3, 0) ||
            ctxt.version >= &Version(Api::GlEs, 3, 0) ||
            ctxt.extensions.gl_arb_vertex_array_object
        {
            unsafe { ctxt.gl.BindVertexArray(vao_id) };
        } else if ctxt.extensions.gl_oes_vertex_array_object {
            unsafe { ctxt.gl.BindVertexArrayOES(vao_id) };
        } else if ctxt.extensions.gl_apple_vertex_array_object {
            unsafe { ctxt.gl.BindVertexArrayAPPLE(vao_id) };
        } else {
            unreachable!();
        }

        ctxt.state.vertex_array = vao_id;
    }
}

/// Binds an individual attribute to the current VAO.
unsafe fn bind_attribute(ctxt: &mut CommandContext, program: &Program,
                         vertex_buffer: gl::types::GLuint, bindings: &VertexFormat,
                         buffer_offset: usize, stride: usize, divisor: Option<u32>)
{
    // glVertexAttribPointer uses the current array buffer
    // TODO: use a proper function
    if ctxt.state.array_buffer_binding != vertex_buffer {
        if ctxt.version >= &Version(Api::Gl, 1, 5) ||
            ctxt.version >= &Version(Api::GlEs, 2, 0)
        {
            ctxt.gl.BindBuffer(gl::ARRAY_BUFFER, vertex_buffer);
        } else if ctxt.extensions.gl_arb_vertex_buffer_object {
            ctxt.gl.BindBufferARB(gl::ARRAY_BUFFER_ARB, vertex_buffer);
        } else {
            unreachable!();
        }
        ctxt.state.array_buffer_binding = vertex_buffer;
    }

    // binding attributes
    for &(ref name, offset, ty) in bindings {
        let (data_type, elements_count) = vertex_binding_type_to_gl(ty);

        let attribute = match program.get_attribute(Borrow::<str>::borrow(name)) {
            Some(a) => a,
            None => continue
        };

        let (attribute_ty, _) = vertex_binding_type_to_gl(attribute.ty);

        if attribute.location != -1 {
            match attribute_ty {
                gl::BYTE | gl::UNSIGNED_BYTE | gl::SHORT | gl::UNSIGNED_SHORT |
                gl::INT | gl::UNSIGNED_INT =>
                    ctxt.gl.VertexAttribIPointer(attribute.location as u32,
                                                 elements_count as gl::types::GLint, data_type,
                                                 stride as i32,
                                                 (buffer_offset + offset) as *const libc::c_void),

                gl::DOUBLE | gl::DOUBLE_VEC2 | gl::DOUBLE_VEC3 | gl::DOUBLE_VEC4 |
                gl::DOUBLE_MAT2 | gl::DOUBLE_MAT3 | gl::DOUBLE_MAT4 |
                gl::DOUBLE_MAT2x3 | gl::DOUBLE_MAT2x4 | gl::DOUBLE_MAT3x2 |
                gl::DOUBLE_MAT3x4 | gl::DOUBLE_MAT4x2 | gl::DOUBLE_MAT4x3 =>
                    ctxt.gl.VertexAttribLPointer(attribute.location as u32,
                                                 elements_count as gl::types::GLint, data_type,
                                                 stride as i32,
                                                 (buffer_offset + offset) as *const libc::c_void),

                _ => ctxt.gl.VertexAttribPointer(attribute.location as u32,
                                                 elements_count as gl::types::GLint, data_type, 0,
                                                 stride as i32,
                                                 (buffer_offset + offset) as *const libc::c_void)
            }

            if let Some(divisor) = divisor {
                ctxt.gl.VertexAttribDivisor(attribute.location as u32, divisor);
            }

            ctxt.gl.EnableVertexAttribArray(attribute.location as u32);
        }
    }
}