From 5af997a02f21e7f6bd16d3849e921a643b963304 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 20 Mar 2026 13:58:29 +0100 Subject: feat(opengl-bindings): add Buffer init fn --- opengl-bindings/src/buffer.rs | 68 +++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 18 deletions(-) (limited to 'opengl-bindings') diff --git a/opengl-bindings/src/buffer.rs b/opengl-bindings/src/buffer.rs index 4c91649..3bb6c48 100644 --- a/opengl-bindings/src/buffer.rs +++ b/opengl-bindings/src/buffer.rs @@ -27,6 +27,35 @@ impl Buffer Self { buf: buffer, _pd: PhantomData } } + /// Initializes this buffer with a size and a usage. + /// + /// # Errors + /// Returns `Err` if the size (in bytes) is too large. + pub fn init( + &self, + current_context: &CurrentContextWithFns<'_>, + size: usize, + usage: Usage, + ) -> Result<(), Error> + { + let size: crate::sys::types::GLsizeiptr = + size.try_into().map_err(|_| Error::SizeTooLarge { + size, + max_size: crate::sys::types::GLsizeiptr::MAX as usize, + })?; + + unsafe { + current_context.fns().NamedBufferData( + self.buf, + size, + null(), + usage.into_gl(), + ); + } + + Ok(()) + } + /// Stores items in this buffer. /// /// # Errors @@ -41,12 +70,10 @@ impl Buffer let total_size = size_of_val(items); let total_size: crate::sys::types::GLsizeiptr = - total_size - .try_into() - .map_err(|_| Error::TotalItemsSizeTooLarge { - total_size, - max_total_size: crate::sys::types::GLsizeiptr::MAX as usize, - })?; + total_size.try_into().map_err(|_| Error::SizeTooLarge { + size: total_size, + max_size: crate::sys::types::GLsizeiptr::MAX as usize, + })?; unsafe { current_context.fns().NamedBufferData( @@ -82,12 +109,10 @@ impl Buffer let total_size = size_of::() * values.len(); let total_size: crate::sys::types::GLsizeiptr = - total_size - .try_into() - .map_err(|_| Error::TotalItemsSizeTooLarge { - total_size, - max_total_size: crate::sys::types::GLsizeiptr::MAX as usize, - })?; + total_size.try_into().map_err(|_| Error::SizeTooLarge { + size: total_size, + max_size: crate::sys::types::GLsizeiptr::MAX as usize, + })?; unsafe { current_context.fns().NamedBufferData( @@ -160,15 +185,22 @@ impl Usage } } +#[derive(Debug)] +#[repr(u32)] +pub enum BindingTarget +{ + AtomicCounterBuffer = crate::sys::ATOMIC_COUNTER_BUFFER, + TransformFeedbackBuffer = crate::sys::TRANSFORM_FEEDBACK_BUFFER, + UniformBuffer = crate::sys::UNIFORM_BUFFER, + ShaderStorageBuffer = crate::sys::SHADER_STORAGE_BUFFER, +} + #[derive(Debug, thiserror::Error)] pub enum Error { - #[error( - "Total size of items ({total_size}) is too large. Must be < {max_total_size}" - )] - TotalItemsSizeTooLarge + #[error("Size ({size}) is too large. Must be < {max_size}")] + SizeTooLarge { - total_size: usize, - max_total_size: usize, + size: usize, max_size: usize }, } -- cgit v1.2.3-18-g5258