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
use super::*;
use super::errors::*;

use raw::{create_buffer, named_buffer_storage, delete_buffer, BufferId, BufferData,
          BufferMutability, BufferMap, named_buffer_sub_data};

pub struct MutableBuffer {
    id: BufferId,
}

impl MutableBuffer {
    /// Creates new buffer and copies `data` to its memory on GPU
    ///
    /// # Errors
    /// - GL_OUT_OF_MEMORY - if the buffer cannot be created
    ///
    pub fn new<T>(data: &[T]) -> Result<MutableBuffer> {
        let id = create_buffer();

        unsafe {
            named_buffer_storage(
                id,
                BufferData::Data(data),
                BufferMutability::Immutable,
                None,
            ).chain_err(|| ErrorKind::BufferCreationError)?;
        }

        Ok(MutableBuffer { id: id })
    }

    /// Creates new empty buffer
    ///
    /// # Errors
    /// - BufferCreationError(GL_OUT_OF_MEMORY) - if the buffer cannot be created
    ///
    pub fn new_empty(size: usize) -> Result<MutableBuffer> {
        let id = create_buffer();

        unsafe {
            named_buffer_storage::<u8, Option<BufferMap>>(
                id,
                BufferData::Empty(size),
                BufferMutability::Mutable,
                None,
            ).chain_err(|| ErrorKind::BufferCreationError)?;
        }

        Ok(MutableBuffer { id: id })
    }

    /// Replaces part of memory with `data`
    ///
    /// # Errors
    /// - BufferSetSubDataError(GL_INVALID_VALUE) - if size of `data` + offset > buffer size
    ///
    pub fn set_sub_data<T>(&mut self, data: &[T], offset: isize) -> Result<()> {
        unsafe {
            named_buffer_sub_data(self.id, offset, data).chain_err(|| {
                ErrorKind::BufferSetSubDataError
            })?;
        }

        Ok(())
    }
}

impl Buffer for MutableBuffer {
    fn get_id(&self) -> BufferId {
        self.id
    }
}

impl Drop for MutableBuffer {
    fn drop(&mut self) {
        unsafe {
            delete_buffer(self.id);
        }
    }
}