|
|
@ -1,39 +1,64 @@ |
|
|
|
use std::marker::PhantomData; |
|
|
|
//! Configure vertex buffer objects to be sent to the GPU for rendering.
|
|
|
|
|
|
|
|
//!
|
|
|
|
|
|
|
|
//! See the [`attrib`] module for details on how to describe the shape and type
|
|
|
|
|
|
|
|
//! of the VBO data.
|
|
|
|
|
|
|
|
|
|
|
|
use std::mem::MaybeUninit; |
|
|
|
use std::mem::MaybeUninit; |
|
|
|
|
|
|
|
|
|
|
|
use crate::attrib; |
|
|
|
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)] |
|
|
|
pub struct Info(pub(crate) citro3d_sys::C3D_BufInfo); |
|
|
|
pub struct Info(pub(crate) citro3d_sys::C3D_BufInfo); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A slice of buffer data. This borrows the buffer data and can be thought of
|
|
|
|
|
|
|
|
/// as similar to `&[T]` obtained by slicing a `Vec<T>`.
|
|
|
|
#[derive(Debug, Clone, Copy)] |
|
|
|
#[derive(Debug, Clone, Copy)] |
|
|
|
pub struct Slice<'buf> { |
|
|
|
pub struct Slice<'buf> { |
|
|
|
index: libc::c_int, |
|
|
|
index: libc::c_int, |
|
|
|
size: libc::c_int, |
|
|
|
size: libc::c_int, |
|
|
|
_vbo_data: PhantomData<&'buf ()>, |
|
|
|
|
|
|
|
buf_info: &'buf Info, |
|
|
|
buf_info: &'buf Info, |
|
|
|
|
|
|
|
// TODO: should we encapsulate the primitive here too, and require it when the
|
|
|
|
|
|
|
|
// slice is registered? Could there ever be a use case to draw different primitives
|
|
|
|
|
|
|
|
// using the same backing data???
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Slice<'_> { |
|
|
|
impl Slice<'_> { |
|
|
|
pub fn as_raw(&self) -> libc::c_int { |
|
|
|
/// Get the index into the buffer for this slice.
|
|
|
|
|
|
|
|
pub fn index(&self) -> libc::c_int { |
|
|
|
self.index |
|
|
|
self.index |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn size(&self) -> libc::c_int { |
|
|
|
/// Get the length of the slice.
|
|
|
|
|
|
|
|
#[must_use] |
|
|
|
|
|
|
|
pub fn len(&self) -> libc::c_int { |
|
|
|
self.size |
|
|
|
self.size |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Return whether or not the slice has any elements.
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool { |
|
|
|
|
|
|
|
self.len() <= 0 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the buffer info this slice is associated with.
|
|
|
|
pub fn info(&self) -> &Info { |
|
|
|
pub fn info(&self) -> &Info { |
|
|
|
self.buf_info |
|
|
|
self.buf_info |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// The geometric primitive to draw (i.e. what shapes the buffer data describes).
|
|
|
|
#[repr(u32)] |
|
|
|
#[repr(u32)] |
|
|
|
#[derive(Debug, Clone, Copy)] |
|
|
|
#[derive(Debug, Clone, Copy)] |
|
|
|
pub enum Primitive { |
|
|
|
pub enum Primitive { |
|
|
|
|
|
|
|
/// Draw triangles (3 vertices per triangle).
|
|
|
|
Triangles = ctru_sys::GPU_TRIANGLES, |
|
|
|
Triangles = ctru_sys::GPU_TRIANGLES, |
|
|
|
|
|
|
|
/// Draw a triangle strip (each vertex shared by 1-3 triangles).
|
|
|
|
TriangleStrip = ctru_sys::GPU_TRIANGLE_STRIP, |
|
|
|
TriangleStrip = ctru_sys::GPU_TRIANGLE_STRIP, |
|
|
|
|
|
|
|
/// Draw a triangle fan (first vertex shared by all triangles).
|
|
|
|
TriangleFan = ctru_sys::GPU_TRIANGLE_FAN, |
|
|
|
TriangleFan = ctru_sys::GPU_TRIANGLE_FAN, |
|
|
|
|
|
|
|
/// Geometry primitive. Can be used for more complex use cases like geometry
|
|
|
|
|
|
|
|
/// shaders that output custom primitives.
|
|
|
|
GeometryPrim = ctru_sys::GPU_GEOMETRY_PRIM, |
|
|
|
GeometryPrim = ctru_sys::GPU_GEOMETRY_PRIM, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -49,6 +74,7 @@ impl Default for Info { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Info { |
|
|
|
impl Info { |
|
|
|
|
|
|
|
/// Construct buffer info without any registered data.
|
|
|
|
pub fn new() -> Self { |
|
|
|
pub fn new() -> Self { |
|
|
|
Self::default() |
|
|
|
Self::default() |
|
|
|
} |
|
|
|
} |
|
|
@ -63,6 +89,18 @@ impl Info { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Register vertex buffer object data. The resulting [`Slice`] will have its
|
|
|
|
|
|
|
|
/// lifetime tied to both this [`Info`] and the passed-in VBO. `vbo_data` is
|
|
|
|
|
|
|
|
/// assumed to use one `T` per drawn primitive, and its layout is assumed to
|
|
|
|
|
|
|
|
/// match the given `attrib_info`
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Errors
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Registering VBO data may fail:
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// * if `vbo_data` is not allocated with the [`ctru::linear`] allocator
|
|
|
|
|
|
|
|
/// * if the maximum number (12) of VBOs are already registered
|
|
|
|
|
|
|
|
///
|
|
|
|
pub fn add<'this, 'vbo, 'idx, T>( |
|
|
|
pub fn add<'this, 'vbo, 'idx, T>( |
|
|
|
&'this mut self, |
|
|
|
&'this mut self, |
|
|
|
vbo_data: &'vbo [T], |
|
|
|
vbo_data: &'vbo [T], |
|
|
@ -87,21 +125,16 @@ impl Info { |
|
|
|
) |
|
|
|
) |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
if res < 0 { |
|
|
|
// Error codes from <https://github.com/devkitPro/citro3d/blob/master/source/buffers.c#L11>
|
|
|
|
// TODO: should we convert to a more specific error if this fails?
|
|
|
|
match res { |
|
|
|
// It looks like the common cases are
|
|
|
|
-2 => Err(crate::Error::InvalidMemoryLocation), |
|
|
|
// - too many buffers already added (max 12)
|
|
|
|
-1 => Err(crate::Error::TooManyBuffers), |
|
|
|
// - physical memory address in the wrong place (this can be seen by
|
|
|
|
..=0 => Err(crate::Error::System(res)), |
|
|
|
// using default allocator instead of LinearAllocator)
|
|
|
|
_ => Ok(Slice { |
|
|
|
// <https://github.com/devkitPro/citro3d/blob/master/source/buffers.c#L13-L17>
|
|
|
|
|
|
|
|
Err(crate::Error::System(res)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
Ok(Slice { |
|
|
|
|
|
|
|
index: res, |
|
|
|
index: res, |
|
|
|
size: vbo_data.len().try_into()?, |
|
|
|
size: vbo_data.len().try_into()?, |
|
|
|
_vbo_data: PhantomData, |
|
|
|
|
|
|
|
buf_info: self, |
|
|
|
buf_info: self, |
|
|
|
}) |
|
|
|
}), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|