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
use gl::types::*;
use std::mem;
use std::os::raw::c_void;
#[cfg(not(feature="webgl"))]
use ::Result;
use std::ops;
#[cfg(not(feature="webgl"))]
use super::map::*;
use std::fmt::Debug;
#[cfg(not(feature="webgl"))]
use std::ops::Range;

/// Buffer object with a type
pub trait TypedBuffer<T>{
    /// OpenGL id
    fn id(&self) -> GLuint;

    /// Number of elements on the last update
    fn len(&self) -> usize;

    /// Allocated capacity of the buffer in number of elements
    fn capacity(&self) -> usize;

    fn is_empty(&self) -> bool{
        self.len() == 0
    }

    /// Total bytes on the last update
    fn bytes(&self) -> usize{
        self.len() * self.stride()
    }

    /// Total capcacity of the buffer in bytes
    fn capacity_bytes(&self) -> usize{
        self.capacity() * self.stride()
    }

    /// Stride of the buffer type
    fn stride(&self) -> usize{
        mem::size_of::<T>()
    }

    /// Maps a buffer object's data store
    ///
    /// Pass a closure that receives the mapped buffer to access it
    ///
    /// see glMapBuffer
    #[cfg(not(feature="webgl"))]
    fn with_map_read<F: FnMut(&[T])>(&self, flags: MapReadFlags, f: F) -> Result<()> where Self: Sized;

    #[cfg(not(feature="webgl"))]
    unsafe fn unmap(&self);

    /// Copy one buffer into another
    fn copy_to<U,B:BufferRange<U> + WithBackendMut>(&self, dst: &mut B) where Self: Sized;
}

/// Buffer object with a type
pub trait TypedBufferMut<T>: TypedBuffer<T>{

    /// Maps a buffer object's data store
    ///
    /// Pass a closure that receives the mapped buffer to access it
    ///
    /// This method is unsafe cause reading from mapped slice would have undefined behaviour
    ///
    /// see glMapBuffer
    #[cfg(not(feature="webgl"))]
    unsafe fn with_map_write<F: Fn(&mut [T])>(&mut self, flags: MapWriteFlags, f: F) -> Result<()> where Self: Sized;

    /// Maps a buffer object's data store
    ///
    /// Pass a closure that receives the mapped buffer to access it
    ///
    /// see glMapBuffer
    #[cfg(not(feature="webgl"))]
    fn with_map_read_write<F: Fn(&mut [T])>(&mut self, flags: MapReadWriteFlags, f: F) -> Result<()> where Self: Sized;
}

pub trait Cast<T>{
    type CastTo;
    fn cast(self) -> Self::CastTo;
}

/// A view into a portion of a buffer object
pub trait BufferRange<T>: TypedBuffer<T>{
    fn start(&self) -> usize;
    fn end(&self) -> usize;
    fn into_range<R: InputRange>(self, range: R) -> super::Range<T, Self, Self> where Self: Sized;
}

/// A writable view into a portion of a buffer
pub trait BufferRangeMut<T>: BufferRange<T>{
    fn update(&mut self, data: &[T]);
}


/// Trait to convert any partial range + a buffer into an ops::Range
pub trait InputRange{
    fn to_range<T, B: BufferRange<T>>(self, buffer: &B) -> ops::Range<usize>;
}

impl InputRange for ops::Range<usize>{
    fn to_range<T, B: BufferRange<T>>(self, buffer: &B) -> ops::Range<usize>{
        assert!(buffer.start() + self.start <= buffer.start() + self.end);
        assert!(buffer.start() + self.end <= buffer.end());
        buffer.start() + self.start .. buffer.start() + self.end
    }
}

impl InputRange for ops::RangeFrom<usize>{
    fn to_range<T, B: BufferRange<T>>(self, buffer: &B) -> ops::Range<usize>{
        assert!(buffer.start() + self.start <= buffer.end());
        buffer.start() + self.start .. buffer.end()
    }
}

impl InputRange for ops::RangeTo<usize>{
    fn to_range<T, B: BufferRange<T>>(self, buffer: &B) -> ops::Range<usize>{
        assert!(buffer.start() + self.end <= buffer.end());
        buffer.start() .. buffer.start() + self.end
    }
}

impl InputRange for ops::RangeFull{
    fn to_range<T, B: BufferRange<T>>(self, buffer: &B) -> ops::Range<usize>{
        buffer.start() .. buffer.end()
    }
}

pub trait Backend: Debug {
    fn id(&self) -> GLuint;
    fn bind(&self, target: GLenum);
    fn unbind(&self, target: GLenum);

    // fn bind_base(&self, target: GLenum, index: GLuint);

    // fn unbind_base(&self, target: GLenum, index: GLuint);

    unsafe fn load(&mut self, data: *const c_void, len: usize, usage: GLenum);

    unsafe fn load_target(&mut self, data: *const c_void, len: usize, usage: GLenum, target: GLenum);

    unsafe fn update(&mut self, data: *const c_void, len: usize, offset: usize);

    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // unsafe fn map(&self, flags: GLenum) -> *const c_void;

    #[cfg(not(feature="webgl"))]
    unsafe fn unmap(&self);

    #[cfg(not(feature="webgl"))]
    unsafe fn map_range(&self, offset: GLintptr, length: GLsizeiptr, flags: GLenum) -> *const c_void;

    fn copy_to(&self, dst: &mut dyn Backend, read_offset: usize, write_offset: usize, size: usize);

    unsafe fn get_parameter_iv(&self, parameter: GLenum, len: usize) -> Vec<GLint>;
}

pub trait WithBackend {
    fn with_backend<F:FnMut(&dyn Backend)->R, R>(&self, f:F) -> R;
}

pub trait WithBackendMut {
    fn with_backend_mut<F:FnMut(&mut dyn Backend)->R, R>(&mut self, f:F) -> R;
}

#[cfg(not(feature="webgl"))]
pub trait WithMapRange<T>{
    fn with_map_range_read<F: FnMut(&[T])>(&self, offset: usize, length: usize, flags: MapReadFlags, f: F) -> Result<()>;
}

#[cfg(not(feature="webgl"))]
pub trait WithMapRangeMut<T>: WithMapRange<T>{
    unsafe fn with_map_range_write<F: FnMut(&mut [T])>(&mut self, offset: usize, length: usize, flags: MapWriteFlags, f: F) -> Result<()>;
    fn with_map_range_read_write<F: FnMut(&mut [T])>(&mut self, offset: usize, length: usize, flags: MapReadWriteFlags, f: F) -> Result<()>;
}

#[cfg(not(feature="webgl"))]
pub trait MapRange<T>: WithBackend + Sized{
    fn map_range_read(&mut self, offset: usize, length: usize, flags: MapReadFlags) -> Result<MapRead<T,Self>>;
}

#[cfg(not(feature="webgl"))]
pub trait MapRangeMut<T>: MapRange<T>{
    fn map_range_write(&mut self, offset: usize, length: usize, flags: MapWriteFlags) -> Result<MapWrite<T,Self>>;
    fn map_range_read_write(&mut self, offset: usize, length: usize, flags: MapReadWriteFlags) -> Result<MapReadWrite<T, Self>>;
}

#[cfg(not(feature="webgl"))]
pub trait MapPersistent<T>
where
    Self: TypedBuffer<T>,
    for<'a> &'a Self: TypedBuffer<T>,
{
    fn map_persistent_read(&self, flags: MapReadFlags) -> Result<MapPersistentRead<T,&Self>>;
    fn into_map_persistent_read(self, flags: MapReadFlags) -> Result<MapPersistentRead<T,Self>> where Self: Sized;
}

#[cfg(not(feature="webgl"))]
pub trait MapPersistentMut<T>: MapPersistent<T>
where
    Self: TypedBuffer<T>,
    for<'a> &'a Self: TypedBuffer<T>,
{
    fn map_persistent_write(&self, flags: MapWriteFlags) -> Result<MapPersistentWrite<T,&Self>>;
    fn map_persistent_read_write(&self, flags: MapReadWriteFlags) -> Result<MapPersistentReadWrite<T,&Self>>;
    fn into_map_persistent_write(self, flags: MapWriteFlags) -> Result<MapPersistentWrite<T,Self>> where Self: Sized;
    fn into_map_persistent_read_write(self, flags: MapReadWriteFlags) -> Result<MapPersistentReadWrite<T,Self>> where Self: Sized;
}

#[cfg(not(feature="webgl"))]
pub trait MapPersistentRange<T>
where
    Self: TypedBuffer<T>,
    for<'a> &'a Self: TypedBuffer<T>,
{
    unsafe fn unsafe_map_range_persistent_read(&self, range: Option<Range<usize>>, flags: MapReadFlags) -> Result<&'static [T]>;
    fn map_range_persistent_read(&self, offset: usize, length: usize, flags: MapReadFlags) -> Result<MapPersistentRead<T,&Self>>;
    fn into_map_range_persistent_read(self, offset: usize, length: usize, flags: MapReadFlags) -> Result<MapPersistentRead<T,Self>> where Self: Sized;
}

#[cfg(not(feature="webgl"))]
pub trait MapPersistentRangeMut<T>: MapPersistentRange<T>
where
    Self: TypedBuffer<T>,
    for<'a> &'a Self: TypedBuffer<T>,
{
    unsafe fn unsafe_map_range_persistent_write(&self, range: Option<Range<usize>>, flags: MapWriteFlags) -> Result<&'static mut [T]>;
    unsafe fn unsafe_map_range_persistent_read_write(&self, range: Option<Range<usize>>, flags: MapReadWriteFlags) -> Result<&'static mut [T]>;
    fn map_range_persistent_write(&self, offset: usize, length: usize, flags: MapWriteFlags) -> Result<MapPersistentWrite<T,&Self>>;
    fn map_range_persistent_read_write(&self, offset: usize, length: usize, flags: MapReadWriteFlags) -> Result<MapPersistentReadWrite<T,&Self>>;
    fn into_map_range_persistent_write(self, offset: usize, length: usize, flags: MapWriteFlags) -> Result<MapPersistentWrite<T,Self>> where Self: Sized;
    fn into_map_range_persistent_read_write(self, offset: usize, length: usize, flags: MapReadWriteFlags) -> Result<MapPersistentReadWrite<T,Self>> where Self: Sized;
}