summaryrefslogtreecommitdiff
path: root/engine/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-11-05 12:32:42 +0100
committerHampusM <hampus@hampusmat.com>2023-11-05 12:32:42 +0100
commit7001b4955c8c1b337ce26bf75305417f6963a836 (patch)
tree3ba9e7e2293074ad96a8d333a8603722ddff196d /engine/src
parentd5b84d968b570fbaeb6a1832a93875116001c504 (diff)
refactor(engine): add shader program currently bound safety
Diffstat (limited to 'engine/src')
-rw-r--r--engine/src/opengl/shader.rs15
-rw-r--r--engine/src/renderer/mod.rs93
2 files changed, 66 insertions, 42 deletions
diff --git a/engine/src/opengl/shader.rs b/engine/src/opengl/shader.rs
index 603c790..3dc308e 100644
--- a/engine/src/opengl/shader.rs
+++ b/engine/src/opengl/shader.rs
@@ -2,6 +2,7 @@ use std::ffi::CStr;
use std::ptr::null_mut;
use crate::matrix::Matrix;
+use crate::opengl::currently_bound::CurrentlyBound;
#[derive(Debug)]
pub struct Shader
@@ -151,14 +152,24 @@ impl Program
Ok(())
}
- pub fn activate(&self)
+ pub fn activate(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>))
{
unsafe {
gl::UseProgram(self.program);
}
+
+ // SAFETY: The shader program object is bound above
+ let currently_bound = unsafe { CurrentlyBound::new() };
+
+ cb(currently_bound);
}
- pub fn set_uniform_matrix_4fv(&self, name: &CStr, matrix: &Matrix<f32, 4, 4>)
+ pub fn set_uniform_matrix_4fv(
+ &self,
+ _: &CurrentlyBound<Self>,
+ name: &CStr,
+ matrix: &Matrix<f32, 4, 4>,
+ )
{
let uniform_location =
unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) };
diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs
index cad4deb..7d5e104 100644
--- a/engine/src/renderer/mod.rs
+++ b/engine/src/renderer/mod.rs
@@ -12,6 +12,7 @@ use crate::opengl::buffer::{
ElementArrayKind as ElementArrayBufferKind,
Usage as BufferUsage,
};
+use crate::opengl::currently_bound::CurrentlyBound;
#[cfg(feature = "debug")]
use crate::opengl::debug::{MessageSeverity, MessageSource, MessageType};
use crate::opengl::shader::Program as ShaderProgram;
@@ -70,37 +71,24 @@ impl Renderer
clear_buffers(BufferClearMask::COLOR | BufferClearMask::DEPTH);
for obj in objects {
- obj.renderable().shader_program.activate();
-
- apply_transformation_matrices(obj, &self.camera, window_size);
-
- let draw = || {
- obj.renderable().vertex_arr.bind(|vert_arr_curr_bound| {
- if let Some(index_info) = &obj.renderable().index_info {
- VertexArray::draw_elements(
- &vert_arr_curr_bound,
- PrimitiveKind::Triangles,
- 0,
- index_info.cnt,
- );
+ obj.renderable()
+ .shader_program
+ .activate(|shader_program_curr_bound| {
+ apply_transformation_matrices(
+ obj,
+ &self.camera,
+ window_size,
+ &shader_program_curr_bound,
+ );
+
+ if let Some(texture) = obj.texture() {
+ texture.inner().bind(|_| {
+ Self::draw_object(obj);
+ });
} else {
- VertexArray::draw_arrays(
- &vert_arr_curr_bound,
- PrimitiveKind::Triangles,
- 0,
- 3,
- );
+ Self::draw_object(obj);
}
});
- };
-
- if let Some(texture) = obj.texture() {
- texture.inner().bind(|_| {
- draw();
- });
- } else {
- draw();
- }
}
}
@@ -136,6 +124,27 @@ impl Renderer
set_debug_message_control(None, None, None, &[], MessageIdsAction::Disable);
}
+
+ fn draw_object(obj: &Object)
+ {
+ obj.renderable().vertex_arr.bind(|vert_arr_curr_bound| {
+ if let Some(index_info) = &obj.renderable().index_info {
+ VertexArray::draw_elements(
+ &vert_arr_curr_bound,
+ PrimitiveKind::Triangles,
+ 0,
+ index_info.cnt,
+ );
+ } else {
+ VertexArray::draw_arrays(
+ &vert_arr_curr_bound,
+ PrimitiveKind::Triangles,
+ 0,
+ 3,
+ );
+ }
+ });
+ }
}
#[derive(Debug)]
@@ -204,19 +213,22 @@ fn apply_transformation_matrices(
object: &Object,
camera: &Camera,
window_size: &WindowSize,
+ shader_program_curr_bound: &CurrentlyBound<ShaderProgram>,
)
{
- object
- .renderable()
- .shader_program
- .set_uniform_matrix_4fv(cstr!("model"), &object.transform().as_matrix());
+ object.renderable().shader_program.set_uniform_matrix_4fv(
+ shader_program_curr_bound,
+ cstr!("model"),
+ &object.transform().as_matrix(),
+ );
let view = camera.as_matrix();
- object
- .renderable()
- .shader_program
- .set_uniform_matrix_4fv(cstr!("view"), &view);
+ object.renderable().shader_program.set_uniform_matrix_4fv(
+ shader_program_curr_bound,
+ cstr!("view"),
+ &view,
+ );
#[allow(clippy::cast_precision_loss)]
let projection = new_perspective(
@@ -226,10 +238,11 @@ fn apply_transformation_matrices(
0.1,
);
- object
- .renderable()
- .shader_program
- .set_uniform_matrix_4fv(cstr!("projection"), &projection);
+ object.renderable().shader_program.set_uniform_matrix_4fv(
+ shader_program_curr_bound,
+ cstr!("projection"),
+ &projection,
+ );
}
#[cfg(feature = "debug")]