From d235e1a62384641188e37aed5eb1a0498ecf0e20 Mon Sep 17 00:00:00 2001 From: Natasha England-Elbro Date: Thu, 15 Feb 2024 21:08:45 +0000 Subject: [PATCH 1/2] feat: add_bytes for buffer::Info --- citro3d/src/buffer.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/citro3d/src/buffer.rs b/citro3d/src/buffer.rs index b959f83..b735ae1 100644 --- a/citro3d/src/buffer.rs +++ b/citro3d/src/buffer.rs @@ -113,8 +113,38 @@ impl Info { 'this: 'idx, 'vbo: 'idx, { - let stride = std::mem::size_of::().try_into()?; + unsafe { + self.add_bytes( + std::slice::from_raw_parts( + vbo_data.as_ptr().cast(), + std::mem::size_of_val(vbo_data), + ), + attrib_info, + std::mem::size_of::() as u32, + ) + } + } + /// Add vbo bytes directly + /// + /// This is the same as [`Info::add`] except it requires manually specifying the + /// stride for each set of attributes, this is useful if you don't know the size + /// at compile time + /// + /// # Safety + /// `vbo_data` must have data matching `attrib_info` every `stride` bytes or strangeness + /// will occur + #[doc(alias = "BufInfo_Add")] + pub unsafe fn add_bytes<'this, 'vbo, 'idx>( + &'this mut self, + vbo_data: &'vbo [u8], + attrib_info: &attrib::Info, + stride: u32, + ) -> crate::Result> + where + 'this: 'idx, + 'vbo: 'idx, + { // SAFETY: the lifetime of the VBO data is encapsulated in the return value's // 'vbo lifetime, and the pointer to &mut self.0 is used to access values // in the BufInfo, not copied to be used later. @@ -122,7 +152,7 @@ impl Info { citro3d_sys::BufInfo_Add( &mut self.0, vbo_data.as_ptr().cast(), - stride, + stride as isize, attrib_info.attr_count(), attrib_info.permutation(), ) From cd1a9c8d581b02a66ba3c7aa74994486d9931b42 Mon Sep 17 00:00:00 2001 From: Natasha England-Elbro Date: Thu, 15 Feb 2024 21:10:51 +0000 Subject: [PATCH 2/2] feat: Clone + Copy for buffer and attrib --- citro3d/src/attrib.rs | 2 +- citro3d/src/buffer.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/citro3d/src/attrib.rs b/citro3d/src/attrib.rs index 229b9fe..3cfd548 100644 --- a/citro3d/src/attrib.rs +++ b/citro3d/src/attrib.rs @@ -9,7 +9,7 @@ use std::mem::MaybeUninit; /// Vertex attribute info. This struct describes how vertex buffers are /// layed out and used (i.e. the shape of the vertex data). -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] #[doc(alias = "C3D_AttrInfo")] pub struct Info(pub(crate) citro3d_sys::C3D_AttrInfo); diff --git a/citro3d/src/buffer.rs b/citro3d/src/buffer.rs index b735ae1..907e04c 100644 --- a/citro3d/src/buffer.rs +++ b/citro3d/src/buffer.rs @@ -9,7 +9,7 @@ use crate::attrib; /// Vertex buffer info. This struct is used to describe the shape of the buffer /// data to be sent to the GPU for rendering. -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] #[doc(alias = "C3D_BufInfo")] pub struct Info(pub(crate) citro3d_sys::C3D_BufInfo);