summaryrefslogtreecommitdiff
path: root/engine/src/renderer/vertex_buffer.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-13 22:34:23 +0200
committerHampusM <hampus@hampusmat.com>2023-10-13 22:34:23 +0200
commit25b5ca97c5e5597570360c37d7452662e0118a00 (patch)
treebe724d8a454faf955b6cdc59203b94a4a6ddeee0 /engine/src/renderer/vertex_buffer.rs
parent4da06868266c12622207e2e61f92aee93c24aaf8 (diff)
refactor(engine): make vertex buffer wrapper hold single buffer
Diffstat (limited to 'engine/src/renderer/vertex_buffer.rs')
-rw-r--r--engine/src/renderer/vertex_buffer.rs77
1 files changed, 77 insertions, 0 deletions
diff --git a/engine/src/renderer/vertex_buffer.rs b/engine/src/renderer/vertex_buffer.rs
new file mode 100644
index 0000000..27ed705
--- /dev/null
+++ b/engine/src/renderer/vertex_buffer.rs
@@ -0,0 +1,77 @@
+use std::mem::size_of_val;
+
+use crate::vertex::Vertex;
+
+#[derive(Debug)]
+pub struct VertexBuffer
+{
+ buffer: gl::types::GLuint,
+}
+
+impl VertexBuffer
+{
+ pub fn new() -> Self
+ {
+ let mut buffer = gl::types::GLuint::default();
+
+ unsafe {
+ gl::GenBuffers(1, &mut buffer);
+ };
+
+ Self { buffer }
+ }
+
+ pub fn store(&self, vertices: &[Vertex], usage: BufferUsage)
+ {
+ unsafe {
+ gl::BindBuffer(gl::ARRAY_BUFFER, self.buffer);
+ }
+
+ unsafe {
+ #[allow(clippy::cast_possible_wrap)]
+ gl::BufferData(
+ gl::ARRAY_BUFFER,
+ size_of_val(vertices) as gl::types::GLsizeiptr,
+ vertices.as_ptr().cast(),
+ usage.into_gl(),
+ );
+ }
+ }
+}
+
+impl Drop for VertexBuffer
+{
+ fn drop(&mut self)
+ {
+ #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
+ unsafe {
+ gl::DeleteBuffers(1, &self.buffer);
+ }
+ }
+}
+
+#[derive(Debug)]
+#[allow(dead_code)]
+pub enum BufferUsage
+{
+ /// The buffer data is set only once and used by the GPU at most a few times.
+ Stream,
+
+ /// The buffer data is set only once and used many times.
+ Static,
+
+ /// The buffer data is changed a lot and used many times.
+ Dynamic,
+}
+
+impl BufferUsage
+{
+ fn into_gl(self) -> gl::types::GLenum
+ {
+ match self {
+ Self::Stream => gl::STREAM_DRAW,
+ Self::Static => gl::STATIC_DRAW,
+ Self::Dynamic => gl::DYNAMIC_DRAW,
+ }
+ }
+}