summaryrefslogtreecommitdiff
path: root/engine/src/opengl/buffer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/opengl/buffer.rs')
-rw-r--r--engine/src/opengl/buffer.rs37
1 files changed, 32 insertions, 5 deletions
diff --git a/engine/src/opengl/buffer.rs b/engine/src/opengl/buffer.rs
index 34514fd..3a4ecf0 100644
--- a/engine/src/opengl/buffer.rs
+++ b/engine/src/opengl/buffer.rs
@@ -4,13 +4,14 @@ use std::mem::size_of_val;
use crate::opengl::currently_bound::CurrentlyBound;
#[derive(Debug)]
-pub struct Buffer<Item>
+pub struct Buffer<Item, ModeT: Mode>
{
buffer: gl::types::GLuint,
_item_pd: PhantomData<Item>,
+ _mode_pd: PhantomData<ModeT>,
}
-impl<Item> Buffer<Item>
+impl<Item, ModeT: Mode> Buffer<Item, ModeT>
{
pub fn new() -> Self
{
@@ -23,6 +24,7 @@ impl<Item> Buffer<Item>
Self {
buffer,
_item_pd: PhantomData,
+ _mode_pd: PhantomData,
}
}
@@ -31,7 +33,7 @@ impl<Item> Buffer<Item>
pub fn bind(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>))
{
unsafe {
- gl::BindBuffer(gl::ARRAY_BUFFER, self.buffer);
+ gl::BindBuffer(ModeT::GL_ENUM, self.buffer);
}
// SAFETY: The buffer object is bound above
@@ -46,7 +48,7 @@ impl<Item> Buffer<Item>
unsafe {
#[allow(clippy::cast_possible_wrap)]
gl::BufferData(
- gl::ARRAY_BUFFER,
+ ModeT::GL_ENUM,
size_of_val(items) as gl::types::GLsizeiptr,
items.as_ptr().cast(),
usage.into_gl(),
@@ -55,7 +57,7 @@ impl<Item> Buffer<Item>
}
}
-impl<Item> Drop for Buffer<Item>
+impl<Item, ModeT: Mode> Drop for Buffer<Item, ModeT>
{
fn drop(&mut self)
{
@@ -92,3 +94,28 @@ impl Usage
}
}
}
+
+/// Buffer mode.
+pub trait Mode
+{
+ #[doc(hidden)]
+ const GL_ENUM: gl::types::GLenum;
+}
+
+/// Array buffer kind.
+#[derive(Debug)]
+pub struct ArrayKind;
+
+impl Mode for ArrayKind
+{
+ const GL_ENUM: gl::types::GLenum = gl::ARRAY_BUFFER;
+}
+
+/// Element array buffer kind.
+#[derive(Debug)]
+pub struct ElementArrayKind;
+
+impl Mode for ElementArrayKind
+{
+ const GL_ENUM: gl::types::GLenum = gl::ELEMENT_ARRAY_BUFFER;
+}