diff options
| author | HampusM <hampus@hampusmat.com> | 2026-03-24 17:16:08 +0100 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-03-24 17:16:08 +0100 |
| commit | 21af49e7bc3996ab73db08b8ec7f069ee4cb60a1 (patch) | |
| tree | fed25133a20c285bb30fc435a22280a48d88577d /opengl-bindings/src | |
| parent | 4f06675fa051e4b2f54960f76b75060c27ea8b7d (diff) | |
refactor(opengl-bindings): make VertexArray::bind_vertex_buffer take vertex size
Diffstat (limited to 'opengl-bindings/src')
| -rw-r--r-- | opengl-bindings/src/vertex_array.rs | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/opengl-bindings/src/vertex_array.rs b/opengl-bindings/src/vertex_array.rs index 5d80c0c..3dc5cb6 100644 --- a/opengl-bindings/src/vertex_array.rs +++ b/opengl-bindings/src/vertex_array.rs @@ -1,5 +1,4 @@ -use std::ffi::{c_int, c_void}; -use std::mem::size_of; +use std::ffi::c_void; use safer_ffi::layout::ReprC; @@ -107,15 +106,29 @@ impl VertexArray } } - pub fn bind_vertex_buffer<VertexT: ReprC>( + pub fn bind_vertex_buffer<VertexBufferItem: ReprC>( &self, current_context: &CurrentContextWithFns<'_>, binding_index: u32, - vertex_buffer: &Buffer<VertexT>, - offset: isize, - ) + vertex_buffer: &Buffer<VertexBufferItem>, + vertex_buffer_spec: VertexBufferSpec, + ) -> Result<(), BindVertexBufferError> { - let vertex_size = const { cast_usize_to_c_int(size_of::<VertexT>()) }; + let offset: crate::sys::types::GLintptr = vertex_buffer_spec + .offset + .try_into() + .map_err(|_| BindVertexBufferError::OffsetValueTooLarge { + value: vertex_buffer_spec.offset, + max_value: crate::sys::types::GLintptr::MAX as usize, + })?; + + let vertex_size: crate::sys::types::GLsizei = vertex_buffer_spec + .vertex_size + .try_into() + .map_err(|_| BindVertexBufferError::VertexSizeValueTooLarge { + value: vertex_buffer_spec.vertex_size, + max_value: crate::sys::types::GLsizei::MAX as usize, + })?; unsafe { current_context.fns().VertexArrayVertexBuffer( @@ -126,6 +139,8 @@ impl VertexArray vertex_size, ); } + + Ok(()) } pub fn enable_attrib( @@ -239,6 +254,13 @@ pub struct AttributeFormat pub offset: u32, } +#[derive(Debug, Clone)] +pub struct VertexBufferSpec +{ + pub offset: usize, + pub vertex_size: usize, +} + #[derive(Debug, thiserror::Error)] pub enum DrawError { @@ -255,22 +277,18 @@ pub enum DrawError }, } -const fn cast_usize_to_c_int(num: usize) -> c_int -{ - assert!(num <= c_int::MAX.cast_unsigned() as usize); - - c_int::from_ne_bytes(shorten_byte_array(num.to_ne_bytes())) -} - -const fn shorten_byte_array<const SRC_LEN: usize, const DST_LEN: usize>( - src: [u8; SRC_LEN], -) -> [u8; DST_LEN] +#[derive(Debug, thiserror::Error)] +pub enum BindVertexBufferError { - assert!(DST_LEN < SRC_LEN); - - let mut ret = [0; DST_LEN]; - - ret.copy_from_slice(src.split_at(DST_LEN).0); + #[error("Offset value {value} is too large. Must be < {max_value}")] + OffsetValueTooLarge + { + value: usize, max_value: usize + }, - ret + #[error("Vertex size value {value} is too large. Must be < {max_value}")] + VertexSizeValueTooLarge + { + value: usize, max_value: usize + }, } |
