From 97bb50d42d7c1f475bf63861449a2162f665be26 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 14 Apr 2024 14:33:44 +0200 Subject: refactor(engine): use OpenGL DSA functions --- engine/src/opengl/texture.rs | 105 +++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 53 deletions(-) (limited to 'engine/src/opengl/texture.rs') diff --git a/engine/src/opengl/texture.rs b/engine/src/opengl/texture.rs index 5d12729..074ade7 100644 --- a/engine/src/opengl/texture.rs +++ b/engine/src/opengl/texture.rs @@ -1,8 +1,5 @@ -use std::ptr::null; - -use crate::opengl::currently_bound::CurrentlyBound; +use crate::data_types::dimens::Dimens; use crate::texture::{Id, Properties}; -use crate::vector::Vec2; #[derive(Debug)] pub struct Texture @@ -17,88 +14,73 @@ impl Texture let mut texture = gl::types::GLuint::default(); unsafe { - gl::GenTextures(1, &mut texture); + gl::CreateTextures(gl::TEXTURE_2D, 1, &mut texture); }; Self { texture } } - pub fn bind(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>)) + pub fn bind(&self) { unsafe { gl::BindTexture(gl::TEXTURE_2D, self.texture); } - - // SAFETY: The buffer object is bound above - let currently_bound = unsafe { CurrentlyBound::new() }; - - cb(currently_bound); } pub fn generate( - curr_bound: &CurrentlyBound, - dimens: &Vec2, + &mut self, + dimens: Dimens, data: &[u8], pixel_data_format: PixelDataFormat, ) { - Self::alloc_image(curr_bound, pixel_data_format, dimens, Some(data)); + self.alloc_image(pixel_data_format, dimens, data); unsafe { - gl::GenerateMipmap(gl::TEXTURE_2D); + gl::GenerateTextureMipmap(self.texture); } } - pub fn apply_properties(&self, properties: &Properties) + pub fn apply_properties(&mut self, properties: &Properties) { - self.bind(|texture_curr_bound| { - Texture::set_wrap(&texture_curr_bound, properties.wrap); - - Texture::set_magnifying_filter( - &texture_curr_bound, - properties.magnifying_filter, - ); - - Texture::set_minifying_filter( - &texture_curr_bound, - properties.minifying_filter, - ); - }); + self.set_wrap(properties.wrap); + self.set_magnifying_filter(properties.magnifying_filter); + self.set_minifying_filter(properties.minifying_filter); } - pub fn set_wrap(_: &CurrentlyBound, wrapping: Wrapping) + pub fn set_wrap(&mut self, wrapping: Wrapping) { let wrapping_gl = wrapping.to_gl(); #[allow(clippy::cast_possible_wrap)] unsafe { - gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, wrapping_gl as i32); - gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, wrapping_gl as i32); + gl::TextureParameteri(self.texture, gl::TEXTURE_WRAP_S, wrapping_gl as i32); + gl::TextureParameteri(self.texture, gl::TEXTURE_WRAP_T, wrapping_gl as i32); } } - pub fn set_magnifying_filter(_: &CurrentlyBound, filtering: Filtering) + pub fn set_magnifying_filter(&mut self, filtering: Filtering) { let filtering_gl = filtering.to_gl(); #[allow(clippy::cast_possible_wrap)] unsafe { - gl::TexParameteri( - gl::TEXTURE_2D, + gl::TextureParameteri( + self.texture, gl::TEXTURE_MAG_FILTER, filtering_gl as i32, ); } } - pub fn set_minifying_filter(_: &CurrentlyBound, filtering: Filtering) + pub fn set_minifying_filter(&mut self, filtering: Filtering) { let filtering_gl = filtering.to_gl(); #[allow(clippy::cast_possible_wrap)] unsafe { - gl::TexParameteri( - gl::TEXTURE_2D, + gl::TextureParameteri( + self.texture, gl::TEXTURE_MIN_FILTER, filtering_gl as i32, ); @@ -106,24 +88,33 @@ impl Texture } fn alloc_image( - _: &CurrentlyBound, + &mut self, pixel_data_format: PixelDataFormat, - dimens: &Vec2, - data: Option<&[u8]>, + dimens: Dimens, + data: &[u8], ) { unsafe { #[allow(clippy::cast_possible_wrap)] - gl::TexImage2D( - gl::TEXTURE_2D, + gl::TextureStorage2D( + self.texture, + 1, + pixel_data_format.to_sized_internal_format(), + dimens.width as i32, + dimens.height as i32, + ); + + #[allow(clippy::cast_possible_wrap)] + gl::TextureSubImage2D( + self.texture, + 0, 0, - pixel_data_format.to_gl() as i32, - dimens.x as i32, - dimens.y as i32, 0, - pixel_data_format.to_gl(), + dimens.width as i32, + dimens.height as i32, + pixel_data_format.to_format(), gl::UNSIGNED_BYTE, - data.map_or_else(null, |data| data.as_ptr().cast()), + data.as_ptr().cast(), ); } } @@ -184,17 +175,25 @@ impl Filtering #[derive(Debug, Clone, Copy)] pub enum PixelDataFormat { - Rgb, - Rgba, + Rgb8, + Rgba8, } impl PixelDataFormat { - fn to_gl(self) -> gl::types::GLenum + fn to_sized_internal_format(self) -> gl::types::GLenum + { + match self { + Self::Rgb8 => gl::RGB8, + Self::Rgba8 => gl::RGBA8, + } + } + + fn to_format(self) -> gl::types::GLenum { match self { - Self::Rgb => gl::RGB, - Self::Rgba => gl::RGBA, + Self::Rgb8 => gl::RGB, + Self::Rgba8 => gl::RGBA, } } } -- cgit v1.2.3-18-g5258