From cfa73b1ea42fa491ff9e00bb5efb5e5a5d860578 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 13 Oct 2023 22:55:56 +0200 Subject: refactor(engine): add OpenGL object currently bound guards --- engine/src/renderer/mod.rs | 15 +++++++++------ engine/src/renderer/vertex_array.rs | 26 +++++++++++++++++++++----- engine/src/renderer/vertex_buffer.rs | 18 +++++++++++++++++- 3 files changed, 47 insertions(+), 12 deletions(-) (limited to 'engine/src/renderer') diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 364b7f4..a23d5a0 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -45,7 +45,9 @@ pub fn render<'renderable>(renderables: impl IntoIterator Self { let vertex_arr = VertexArray::new(); - - vertex_arr.bind(); - let vertex_buffer = VertexBuffer::new(); - vertex_buffer.store(vertices, BufferUsage::Static); + vertex_arr.bind(|vert_arr_curr_bound| { + vertex_buffer.bind(|vert_buf_curr_bound| { + VertexBuffer::store(&vert_buf_curr_bound, vertices, BufferUsage::Static); - VertexArray::configure_attrs(); + VertexArray::configure_attrs(&vert_arr_curr_bound, &vert_buf_curr_bound); + }); + }); Self { shader_program, diff --git a/engine/src/renderer/vertex_array.rs b/engine/src/renderer/vertex_array.rs index b0aac81..2bd6a5b 100644 --- a/engine/src/renderer/vertex_array.rs +++ b/engine/src/renderer/vertex_array.rs @@ -1,5 +1,7 @@ use std::mem::size_of; +use crate::currently_bound::CurrentlyBound; +use crate::renderer::vertex_buffer::VertexBuffer; use crate::vertex::{Attribute, AttributeComponentType, Vertex}; const VERTEX_STRIDE: usize = size_of::(); @@ -23,10 +25,14 @@ impl VertexArray Self { array } } - pub fn draw(&self, primitive_kind: PrimitiveKind, start_index: u32, index_cnt: u32) + /// Draws the currently bound vertex array. + pub fn draw( + _currently_bound: &CurrentlyBound, + primitive_kind: PrimitiveKind, + start_index: u32, + index_cnt: u32, + ) { - self.bind(); - unsafe { #[allow(clippy::cast_possible_wrap)] gl::DrawArrays( @@ -37,7 +43,10 @@ impl VertexArray } } - pub fn configure_attrs() + pub fn configure_attrs( + _currently_bound: &CurrentlyBound, + _vert_buf_curr_bound: &CurrentlyBound, + ) { let mut offset = 0; @@ -50,9 +59,16 @@ impl VertexArray } } - pub fn bind(&self) + #[allow(clippy::inline_always)] + #[inline(always)] + pub fn bind(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>)) { unsafe { gl::BindVertexArray(self.array) } + + // SAFETY: A vertex array object is currently bound + let currently_bound = unsafe { CurrentlyBound::new() }; + + cb(currently_bound); } fn vertex_attrib_ptr(vertex_attr: &Attribute, offset: usize) diff --git a/engine/src/renderer/vertex_buffer.rs b/engine/src/renderer/vertex_buffer.rs index 27ed705..57debc3 100644 --- a/engine/src/renderer/vertex_buffer.rs +++ b/engine/src/renderer/vertex_buffer.rs @@ -1,5 +1,6 @@ use std::mem::size_of_val; +use crate::currently_bound::CurrentlyBound; use crate::vertex::Vertex; #[derive(Debug)] @@ -21,12 +22,27 @@ impl VertexBuffer Self { buffer } } - pub fn store(&self, vertices: &[Vertex], usage: BufferUsage) + #[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( -- cgit v1.2.3-18-g5258