Skip to content

Commit

Permalink
Merge pull request #32 from rust3ds/docs/alias-and-examples
Browse files Browse the repository at this point in the history
 Add a bunch of doc aliases for unsafe APIs
  • Loading branch information
ian-h-chamberlain authored Nov 25, 2023
2 parents c56c4cd + 1fe11ce commit ffb8d70
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 5 deletions.
4 changes: 4 additions & 0 deletions citro3d/src/attrib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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)]
#[doc(alias = "C3D_AttrInfo")]
pub struct Info(pub(crate) citro3d_sys::C3D_AttrInfo);

/// A shader input register, usually corresponding to a single vertex attribute
Expand Down Expand Up @@ -43,6 +44,7 @@ pub struct Index(u8);
/// The data format of an attribute.
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
#[doc(alias = "GPU_FORMATS")]
pub enum Format {
/// A signed byte, i.e. [`i8`].
Byte = ctru_sys::GPU_BYTE,
Expand All @@ -60,6 +62,7 @@ unsafe impl Sync for Info {}
unsafe impl Send for Info {}

impl Default for Info {
#[doc(alias = "AttrInfo_Init")]
fn default() -> Self {
let mut raw = MaybeUninit::zeroed();
let raw = unsafe {
Expand Down Expand Up @@ -100,6 +103,7 @@ impl Info {
///
/// * If `count > 4`
/// * If this attribute info already has the maximum number of attributes.
#[doc(alias = "AttrInfo_AddLoader")]
pub fn add_loader(
&mut self,
register: Register,
Expand Down
4 changes: 4 additions & 0 deletions citro3d/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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)]
#[doc(alias = "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
Expand Down Expand Up @@ -50,6 +51,7 @@ impl Slice<'_> {
/// The geometric primitive to draw (i.e. what shapes the buffer data describes).
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
#[doc(alias = "GPU_Primitive_t")]
pub enum Primitive {
/// Draw triangles (3 vertices per triangle).
Triangles = ctru_sys::GPU_TRIANGLES,
Expand All @@ -63,6 +65,7 @@ pub enum Primitive {
}

impl Default for Info {
#[doc(alias = "BufInfo_Init")]
fn default() -> Self {
let mut info = MaybeUninit::zeroed();
let info = unsafe {
Expand Down Expand Up @@ -100,6 +103,7 @@ impl Info {
///
/// * if `vbo_data` is not allocated with the [`ctru::linear`] allocator
/// * if the maximum number (12) of VBOs are already registered
#[doc(alias = "BufInfo_Add")]
pub fn add<'this, 'vbo, 'idx, T>(
&'this mut self,
vbo_data: &'vbo [T],
Expand Down
16 changes: 12 additions & 4 deletions citro3d/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Instance {
/// # Errors
///
/// Fails if `citro3d` cannot be initialized.
#[doc(alias = "C3D_Init")]
pub fn with_cmdbuf_size(size: usize) -> Result<Self> {
if unsafe { citro3d_sys::C3D_Init(size) } {
Ok(Self)
Expand All @@ -61,6 +62,7 @@ impl Instance {
/// # Errors
///
/// Fails if the given target cannot be used for drawing.
#[doc(alias = "C3D_FrameDrawOn")]
pub fn select_render_target(&mut self, target: &render::Target<'_>) -> Result<()> {
let _ = self;
if unsafe { citro3d_sys::C3D_FrameDrawOn(target.as_raw()) } {
Expand All @@ -72,13 +74,13 @@ impl Instance {

/// Render a frame. The passed in function/closure can mutate the instance,
/// such as to [select a render target](Self::select_render_target).
#[doc(alias = "C3D_FrameBegin")]
#[doc(alias = "C3D_FrameEnd")]
pub fn render_frame_with(&mut self, f: impl FnOnce(&mut Self)) {
unsafe {
citro3d_sys::C3D_FrameBegin(
// TODO: begin + end flags should be configurable
citro3d_sys::C3D_FRAME_SYNCDRAW
.try_into()
.expect("const is valid u8"),
citro3d_sys::C3D_FRAME_SYNCDRAW.try_into().unwrap(),
);
}

Expand All @@ -91,12 +93,14 @@ impl Instance {

/// Get the buffer info being used, if it exists. Note that the resulting
/// [`buffer::Info`] is copied from the one currently in use.
#[doc(alias = "C3D_GetBufInfo")]
pub fn buffer_info(&self) -> Option<buffer::Info> {
let raw = unsafe { citro3d_sys::C3D_GetBufInfo() };
buffer::Info::copy_from(raw)
}

/// Set the buffer info to use for any following draw calls.
#[doc(alias = "C3D_SetBufInfo")]
pub fn set_buffer_info(&mut self, buffer_info: &buffer::Info) {
let raw: *const _ = &buffer_info.0;
// SAFETY: C3D_SetBufInfo actually copies the pointee instead of mutating it.
Expand All @@ -105,19 +109,22 @@ impl Instance {

/// Get the attribute info being used, if it exists. Note that the resulting
/// [`attrib::Info`] is copied from the one currently in use.
#[doc(alias = "C3D_GetAttrInfo")]
pub fn attr_info(&self) -> Option<attrib::Info> {
let raw = unsafe { citro3d_sys::C3D_GetAttrInfo() };
attrib::Info::copy_from(raw)
}

/// Set the attribute info to use for any following draw calls.
#[doc(alias = "C3D_SetAttrInfo")]
pub fn set_attr_info(&mut self, attr_info: &attrib::Info) {
let raw: *const _ = &attr_info.0;
// SAFETY: C3D_SetAttrInfo actually copies the pointee instead of mutating it.
unsafe { citro3d_sys::C3D_SetAttrInfo(raw.cast_mut()) };
}

/// Draw the specified primitivearrays. The
/// Render primitives from the current vertex array buffer.
#[doc(alias = "C3D_DrawArrays")]
pub fn draw_arrays(&mut self, primitive: buffer::Primitive, index: buffer::Slice) {
self.set_buffer_info(index.info());

Expand Down Expand Up @@ -170,6 +177,7 @@ impl Instance {
}

impl Drop for Instance {
#[doc(alias = "C3D_Fini")]
fn drop(&mut self) {
unsafe {
citro3d_sys::C3D_Fini();
Expand Down
3 changes: 2 additions & 1 deletion citro3d/src/math/fvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt;

/// A vector of `f32`s.
#[derive(Clone, Copy)]
#[doc(alias = "C3D_FVec")]
pub struct FVec<const N: usize>(pub(crate) citro3d_sys::C3D_FVec);

/// A 3-vector of `f32`s.
Expand Down Expand Up @@ -130,7 +131,7 @@ impl FVec4 {
/// let v = FVec4::new(1.0, 2.0, 2.0, 4.0);
/// assert_abs_diff_eq!(v.normalize(), FVec4::new(0.2, 0.4, 0.4, 0.8));
/// ```
#[doc(alias = "FVec3_Normalize")]
#[doc(alias = "FVec4_Normalize")]
pub fn normalize(self) -> Self {
Self(unsafe { citro3d_sys::FVec4_Normalize(self.0) })
}
Expand Down
7 changes: 7 additions & 0 deletions citro3d/src/math/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,31 +109,37 @@ impl<const M: usize, const N: usize> Matrix<M, N> {

/// Translate a transformation matrix by the given amounts in the X, Y, and Z
/// directions.
#[doc(alias = "Mtx_Translate")]
pub fn translate(&mut self, x: f32, y: f32, z: f32) {
unsafe { citro3d_sys::Mtx_Translate(self.as_mut(), x, y, z, false) }
}

/// Scale a transformation matrix by the given amounts in the X, Y, and Z directions.
#[doc(alias = "Mtx_Scale")]
pub fn scale(&mut self, x: f32, y: f32, z: f32) {
unsafe { citro3d_sys::Mtx_Scale(self.as_mut(), x, y, z) }
}

/// Rotate a transformation matrix by the given angle around the given axis.
#[doc(alias = "Mtx_Rotate")]
pub fn rotate(&mut self, axis: FVec3, angle: f32) {
unsafe { citro3d_sys::Mtx_Rotate(self.as_mut(), axis.0, angle, false) }
}

/// Rotate a transformation matrix by the given angle around the X axis.
#[doc(alias = "Mtx_RotateX")]
pub fn rotate_x(&mut self, angle: f32) {
unsafe { citro3d_sys::Mtx_RotateX(self.as_mut(), angle, false) }
}

/// Rotate a transformation matrix by the given angle around the Y axis.
#[doc(alias = "Mtx_RotateY")]
pub fn rotate_y(&mut self, angle: f32) {
unsafe { citro3d_sys::Mtx_RotateY(self.as_mut(), angle, false) }
}

/// Rotate a transformation matrix by the given angle around the Z axis.
#[doc(alias = "Mtx_RotateZ")]
pub fn rotate_z(&mut self, angle: f32) {
unsafe { citro3d_sys::Mtx_RotateZ(self.as_mut(), angle, false) }
}
Expand Down Expand Up @@ -193,6 +199,7 @@ impl Matrix4 {

/// Construct a 3D transformation matrix for a camera, given its position,
/// target, and upward direction.
#[doc(alias = "Mtx_LookAt")]
pub fn looking_at(
camera_position: FVec3,
camera_target: FVec3,
Expand Down
6 changes: 6 additions & 0 deletions citro3d/src/math/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl<const N: usize> AbsDiffEq for FVec<N> {
impl<Rhs: Borrow<Self>, const M: usize, const N: usize> Add<Rhs> for &Matrix<M, N> {
type Output = <Self as Deref>::Target;

#[doc(alias = "Mtx_Add")]
fn add(self, rhs: Rhs) -> Self::Output {
let mut out = MaybeUninit::uninit();
unsafe {
Expand All @@ -141,6 +142,7 @@ impl<Rhs: Borrow<Self>, const M: usize, const N: usize> Add<Rhs> for &Matrix<M,
impl<Rhs: Borrow<Self>, const M: usize, const N: usize> Sub<Rhs> for &Matrix<M, N> {
type Output = <Self as Deref>::Target;

#[doc(alias = "Mtx_Subtract")]
fn sub(self, rhs: Rhs) -> Self::Output {
let mut out = MaybeUninit::uninit();
unsafe {
Expand All @@ -153,6 +155,7 @@ impl<Rhs: Borrow<Self>, const M: usize, const N: usize> Sub<Rhs> for &Matrix<M,
impl<const M: usize, const N: usize, const P: usize> Mul<&Matrix<N, P>> for &Matrix<M, N> {
type Output = Matrix<M, P>;

#[doc(alias = "Mtx_Multiply")]
fn mul(self, rhs: &Matrix<N, P>) -> Self::Output {
let mut out = MaybeUninit::uninit();
unsafe {
Expand All @@ -173,6 +176,7 @@ impl<const M: usize, const N: usize, const P: usize> Mul<Matrix<N, P>> for &Matr
impl Mul<FVec3> for &Matrix3 {
type Output = FVec3;

#[doc(alias = "Mtx_MultiplyFVec3")]
fn mul(self, rhs: FVec3) -> Self::Output {
FVec(unsafe { citro3d_sys::Mtx_MultiplyFVec3(self.as_raw(), rhs.0) })
}
Expand All @@ -181,6 +185,7 @@ impl Mul<FVec3> for &Matrix3 {
impl Mul<FVec4> for &Matrix4 {
type Output = FVec4;

#[doc(alias = "Mtx_MultiplyFVec4")]
fn mul(self, rhs: FVec4) -> Self::Output {
FVec(unsafe { citro3d_sys::Mtx_MultiplyFVec4(self.as_raw(), rhs.0) })
}
Expand All @@ -189,6 +194,7 @@ impl Mul<FVec4> for &Matrix4 {
impl Mul<FVec3> for &Matrix<4, 3> {
type Output = FVec4;

#[doc(alias = "Mtx_MultiplyFVecH")]
fn mul(self, rhs: FVec3) -> Self::Output {
FVec(unsafe { citro3d_sys::Mtx_MultiplyFVecH(self.as_raw(), rhs.0) })
}
Expand Down
2 changes: 2 additions & 0 deletions citro3d/src/math/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ pub struct ClipPlanes {
/// The aspect ratio of a projection plane.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
#[doc(alias = "C3D_AspectRatioTop")]
#[doc(alias = "C3D_AspectRatioBot")]
pub enum AspectRatio {
/// The aspect ratio of the 3DS' top screen (per-eye).
#[doc(alias = "C3D_AspectRatioTop")]
Expand Down
9 changes: 9 additions & 0 deletions citro3d/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod transfer;

/// A render target for `citro3d`. Frame data will be written to this target
/// to be rendered on the GPU and displayed on the screen.
#[doc(alias = "C3D_RenderTarget")]
pub struct Target<'screen> {
raw: *mut citro3d_sys::C3D_RenderTarget,
// This is unused after construction, but ensures unique access to the
Expand All @@ -24,6 +25,7 @@ pub struct Target<'screen> {
}

impl Drop for Target<'_> {
#[doc(alias = "C3D_RenderTargetDelete")]
fn drop(&mut self) {
unsafe {
C3D_RenderTargetDelete(self.raw);
Expand All @@ -38,6 +40,8 @@ impl<'screen> Target<'screen> {
/// # Errors
///
/// Fails if the target could not be created.
#[doc(alias = "C3D_RenderTargetCreate")]
#[doc(alias = "C3D_RenderTargetSetOutput")]
pub fn new(
width: usize,
height: usize,
Expand Down Expand Up @@ -81,6 +85,7 @@ impl<'screen> Target<'screen> {

/// Clear the render target with the given 32-bit RGBA color and depth buffer value.
/// Use `flags` to specify whether color and/or depth should be overwritten.
#[doc(alias = "C3D_RenderTargetClear")]
pub fn clear(&mut self, flags: ClearFlags, rgba_color: u32, depth: u32) {
unsafe {
citro3d_sys::C3D_RenderTargetClear(self.raw, flags.bits(), rgba_color, depth);
Expand All @@ -95,6 +100,7 @@ impl<'screen> Target<'screen> {

bitflags::bitflags! {
/// Indicate whether color, depth buffer, or both values should be cleared.
#[doc(alias = "C3D_ClearBits")]
pub struct ClearFlags: u32 {
/// Clear the color of the render target.
const COLOR = citro3d_sys::C3D_CLEAR_COLOR;
Expand All @@ -108,6 +114,7 @@ bitflags::bitflags! {
/// The color format to use when rendering on the GPU.
#[repr(u32)]
#[derive(Clone, Copy, Debug)]
#[doc(alias = "GPU_COLORBUF")]
pub enum ColorFormat {
/// 8-bit Red + 8-bit Green + 8-bit Blue + 8-bit Alpha.
RGBA8 = ctru_sys::GPU_RB_RGBA8,
Expand Down Expand Up @@ -137,6 +144,8 @@ impl From<FramebufferFormat> for ColorFormat {
/// The depth buffer format to use when rendering.
#[repr(u32)]
#[derive(Clone, Copy, Debug)]
#[doc(alias = "GPU_DEPTHBUF")]
#[doc(alias = "C3D_DEPTHTYPE")]
pub enum DepthFormat {
/// 16-bit depth.
Depth16 = ctru_sys::GPU_RB_DEPTH16,
Expand Down
1 change: 1 addition & 0 deletions citro3d/src/render/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl Flags {
/// convertible to one another. Use [`From::from`] to get the [`Format`] corresponding
/// to a given [`ColorFormat`].
#[repr(u32)]
#[doc(alias = "GX_TRANSFER_FORMAT")]
pub enum Format {
/// 8-bit Red + 8-bit Green + 8-bit Blue + 8-bit Alpha.
RGBA8 = ctru_sys::GX_TRANSFER_FMT_RGBA8,
Expand Down
Loading

0 comments on commit ffb8d70

Please sign in to comment.