From bd427836bfa6f7228951c18e43058d3e35577702 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 23 Oct 2023 19:21:42 +0200 Subject: refactor(engine): rename vertex buffer to buffer & make generic --- engine/src/opengl/buffer.rs | 94 ++++++++++++++++++++++++++++++++++++++ engine/src/opengl/mod.rs | 2 +- engine/src/opengl/vertex_array.rs | 4 +- engine/src/opengl/vertex_buffer.rs | 93 ------------------------------------- engine/src/renderer/mod.rs | 8 ++-- 5 files changed, 101 insertions(+), 100 deletions(-) create mode 100644 engine/src/opengl/buffer.rs delete mode 100644 engine/src/opengl/vertex_buffer.rs (limited to 'engine') diff --git a/engine/src/opengl/buffer.rs b/engine/src/opengl/buffer.rs new file mode 100644 index 0000000..34514fd --- /dev/null +++ b/engine/src/opengl/buffer.rs @@ -0,0 +1,94 @@ +use std::marker::PhantomData; +use std::mem::size_of_val; + +use crate::opengl::currently_bound::CurrentlyBound; + +#[derive(Debug)] +pub struct Buffer +{ + buffer: gl::types::GLuint, + _item_pd: PhantomData, +} + +impl Buffer +{ + pub fn new() -> Self + { + let mut buffer = gl::types::GLuint::default(); + + unsafe { + gl::GenBuffers(1, &mut buffer); + }; + + Self { + buffer, + _item_pd: PhantomData, + } + } + + #[allow(clippy::inline_always)] + #[inline(always)] + pub fn bind(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>)) + { + unsafe { + gl::BindBuffer(gl::ARRAY_BUFFER, self.buffer); + } + + // SAFETY: The buffer object is bound above + let currently_bound = unsafe { CurrentlyBound::new() }; + + cb(currently_bound); + } + + /// Stores items in the currently bound buffer. + pub fn store(_currently_bound: &CurrentlyBound, items: &[Item], usage: Usage) + { + unsafe { + #[allow(clippy::cast_possible_wrap)] + gl::BufferData( + gl::ARRAY_BUFFER, + size_of_val(items) as gl::types::GLsizeiptr, + items.as_ptr().cast(), + usage.into_gl(), + ); + } + } +} + +impl Drop for Buffer +{ + fn drop(&mut self) + { + #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)] + unsafe { + gl::DeleteBuffers(1, &self.buffer); + } + } +} + +/// Buffer usage. +#[derive(Debug)] +#[allow(dead_code)] +pub enum Usage +{ + /// 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 Usage +{ + fn into_gl(self) -> gl::types::GLenum + { + match self { + Self::Stream => gl::STREAM_DRAW, + Self::Static => gl::STATIC_DRAW, + Self::Dynamic => gl::DYNAMIC_DRAW, + } + } +} diff --git a/engine/src/opengl/mod.rs b/engine/src/opengl/mod.rs index ce375f5..6f95a1f 100644 --- a/engine/src/opengl/mod.rs +++ b/engine/src/opengl/mod.rs @@ -3,10 +3,10 @@ use glfw::WindowSize; use crate::vector::Vec2; +pub mod buffer; pub mod currently_bound; pub mod shader; pub mod vertex_array; -pub mod vertex_buffer; mod util; diff --git a/engine/src/opengl/vertex_array.rs b/engine/src/opengl/vertex_array.rs index f255b8a..ba2ded2 100644 --- a/engine/src/opengl/vertex_array.rs +++ b/engine/src/opengl/vertex_array.rs @@ -1,7 +1,7 @@ use std::mem::size_of; +use crate::opengl::buffer::Buffer; use crate::opengl::currently_bound::CurrentlyBound; -use crate::opengl::vertex_buffer::VertexBuffer; use crate::vertex::{Attribute, AttributeComponentType, Vertex}; const VERTEX_STRIDE: usize = size_of::(); @@ -45,7 +45,7 @@ impl VertexArray pub fn configure_attrs( _currently_bound: &CurrentlyBound, - _vert_buf_curr_bound: &CurrentlyBound, + _vert_buf_curr_bound: &CurrentlyBound>, ) { let mut offset = 0; diff --git a/engine/src/opengl/vertex_buffer.rs b/engine/src/opengl/vertex_buffer.rs deleted file mode 100644 index 0050a9f..0000000 --- a/engine/src/opengl/vertex_buffer.rs +++ /dev/null @@ -1,93 +0,0 @@ -use std::mem::size_of_val; - -use crate::opengl::currently_bound::CurrentlyBound; -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 } - } - - #[allow(clippy::inline_always)] - #[inline(always)] - pub fn bind(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>)) - { - unsafe { - gl::BindBuffer(gl::ARRAY_BUFFER, self.buffer); - } - - // SAFETY: A vertex array object is currently bound - let currently_bound = unsafe { CurrentlyBound::new() }; - - cb(currently_bound); - } - - /// Stores vertices in the currently bound vertex bound. - pub fn store( - _currently_bound: &CurrentlyBound, - vertices: &[Vertex], - usage: BufferUsage, - ) - { - 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, - } - } -} diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 068b8f0..f31196d 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -5,11 +5,11 @@ use cstr::cstr; use glfw::WindowSize; use crate::object::Object; +use crate::opengl::buffer::{Buffer, Usage}; #[cfg(feature = "debug")] use crate::opengl::debug::{MessageSeverity, MessageSource, MessageType}; use crate::opengl::shader::Program as ShaderProgram; use crate::opengl::vertex_array::{PrimitiveKind, VertexArray}; -use crate::opengl::vertex_buffer::{BufferUsage, VertexBuffer}; use crate::opengl::{clear_buffers, BufferClearMask}; use crate::vector::Vec2; use crate::vertex::Vertex; @@ -123,7 +123,7 @@ pub struct Renderable vertex_arr: VertexArray, /// Vertex buffer has to live as long as the vertex array - _vertex_buffer: VertexBuffer, + _vertex_buffer: Buffer, } impl Renderable @@ -131,11 +131,11 @@ impl Renderable pub fn new(shader_program: ShaderProgram, vertices: &[Vertex]) -> Self { let vertex_arr = VertexArray::new(); - let vertex_buffer = VertexBuffer::new(); + let vertex_buffer = Buffer::new(); vertex_arr.bind(|vert_arr_curr_bound| { vertex_buffer.bind(|vert_buf_curr_bound| { - VertexBuffer::store(&vert_buf_curr_bound, vertices, BufferUsage::Static); + Buffer::store(&vert_buf_curr_bound, vertices, Usage::Static); VertexArray::configure_attrs(&vert_arr_curr_bound, &vert_buf_curr_bound); }); -- cgit v1.2.3-18-g5258