Browse Source

Merge pull request #19 from rust3ds/fix/gfx-updates-3d-example

pull/25/head
Ian Chamberlain 1 year ago committed by GitHub
parent
commit
76eb36c1c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      Cargo.toml
  2. 1
      bindgen-citro3d/Cargo.toml
  3. 287
      citro3d-sys/src/bindings.rs
  4. 2
      citro3d-sys/src/lib.rs
  5. 13
      citro3d-sys/src/os.rs
  6. 219
      citro3d/examples/triangle.rs
  7. 134
      citro3d/src/attrib.rs
  8. 140
      citro3d/src/buffer.rs
  9. 22
      citro3d/src/error.rs
  10. 48
      citro3d/src/lib.rs
  11. 11
      citro3d/src/render.rs
  12. 22
      citro3d/src/shader.rs
  13. 15
      citro3d/src/shader/macros.rs
  14. 1
      citro3d/src/texture.rs
  15. 1
      citro3d/src/vbo.rs

2
Cargo.toml

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
[workspace]
members = ["citro3d-sys", "citro3d", "bindgen-citro3d"]
default-members = ["citro3d", "citro3d-sys"]
resolver = "2"
[patch."https://github.com/rust3ds/citro3d-rs.git"]
citro3d-sys = { path = "citro3d-sys" }

1
bindgen-citro3d/Cargo.toml

@ -3,6 +3,7 @@ name = "bindgen-citro3d" @@ -3,6 +3,7 @@ name = "bindgen-citro3d"
version = "0.1.0"
edition = "2021"
description = "Helper tool to generate citro3d-sys bindings."
publish = false
[dependencies]
bindgen = "0.62.0"

287
citro3d-sys/src/bindings.rs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
/* automatically generated by rust-bindgen 0.62.0 */
/* automatically generated by rust-bindgen 0.64.0 */
use ctru_sys::*;
@ -96,10 +96,7 @@ pub type _off_t = __int64_t; @@ -96,10 +96,7 @@ pub type _off_t = __int64_t;
pub type _fpos_t = __int64_t;
pub type wint_t = ::libc::c_int;
pub type C3D_IVec = u32_;
#[doc = " @struct C3D_FVec"]
#[doc = " @brief Float vector"]
#[doc = ""]
#[doc = " Matches PICA layout"]
#[doc = " @struct C3D_FVec\n @brief Float vector\n\n Matches PICA layout"]
#[repr(C)]
#[derive(Copy, Clone)]
pub union C3D_FVec {
@ -134,13 +131,9 @@ pub struct C3D_FVec__bindgen_ty_2 { @@ -134,13 +131,9 @@ pub struct C3D_FVec__bindgen_ty_2 {
#[doc = "< I-component"]
pub i: f32,
}
#[doc = " @struct C3D_FVec"]
#[doc = " @brief Float vector"]
#[doc = ""]
#[doc = " Matches PICA layout"]
#[doc = " @struct C3D_FVec\n @brief Float vector\n\n Matches PICA layout"]
pub type C3D_FQuat = C3D_FVec;
#[doc = " @struct C3D_Mtx"]
#[doc = " @brief Row-major 4x4 matrix"]
#[doc = " @struct C3D_Mtx\n @brief Row-major 4x4 matrix"]
#[repr(C)]
#[derive(Copy, Clone)]
pub union C3D_Mtx {
@ -150,101 +143,55 @@ pub union C3D_Mtx { @@ -150,101 +143,55 @@ pub union C3D_Mtx {
pub m: [f32; 16usize],
}
extern "C" {
#[doc = "@brief Transposes the matrix. Row => Column, and vice versa."]
#[doc = "@param[in,out] out Output matrix."]
#[doc = "@brief Transposes the matrix. Row => Column, and vice versa.\n@param[in,out] out Output matrix."]
pub fn Mtx_Transpose(out: *mut C3D_Mtx);
}
extern "C" {
#[doc = " @brief Multiply two matrices"]
#[doc = " @param[out] out Output matrix"]
#[doc = " @param[in] a Multiplicand"]
#[doc = " @param[in] b Multiplier"]
#[doc = " @brief Multiply two matrices\n @param[out] out Output matrix\n @param[in] a Multiplicand\n @param[in] b Multiplier"]
pub fn Mtx_Multiply(out: *mut C3D_Mtx, a: *const C3D_Mtx, b: *const C3D_Mtx);
}
extern "C" {
#[doc = " @brief Inverse a matrix"]
#[doc = " @param[in,out] out Matrix to inverse"]
#[doc = " @retval 0.0f Degenerate matrix (no inverse)"]
#[doc = " @return determinant"]
#[doc = " @brief Inverse a matrix\n @param[in,out] out Matrix to inverse\n @retval 0.0f Degenerate matrix (no inverse)\n @return determinant"]
pub fn Mtx_Inverse(out: *mut C3D_Mtx) -> f32;
}
extern "C" {
#[doc = " @brief Multiply 3x3 matrix by a FVec3"]
#[doc = " @param[in] mtx Matrix"]
#[doc = " @param[in] v Vector"]
#[doc = " @return mtx*v (product)"]
#[doc = " @brief Multiply 3x3 matrix by a FVec3\n @param[in] mtx Matrix\n @param[in] v Vector\n @return mtx*v (product)"]
pub fn Mtx_MultiplyFVec3(mtx: *const C3D_Mtx, v: C3D_FVec) -> C3D_FVec;
}
extern "C" {
#[doc = " @brief Multiply 4x4 matrix by a FVec4"]
#[doc = " @param[in] mtx Matrix"]
#[doc = " @param[in] v Vector"]
#[doc = " @return mtx*v (product)"]
#[doc = " @brief Multiply 4x4 matrix by a FVec4\n @param[in] mtx Matrix\n @param[in] v Vector\n @return mtx*v (product)"]
pub fn Mtx_MultiplyFVec4(mtx: *const C3D_Mtx, v: C3D_FVec) -> C3D_FVec;
}
extern "C" {
#[doc = " @brief Get 4x4 matrix equivalent to Quaternion"]
#[doc = " @param[out] m Output matrix"]
#[doc = " @param[in] q Input Quaternion"]
#[doc = " @brief Get 4x4 matrix equivalent to Quaternion\n @param[out] m Output matrix\n @param[in] q Input Quaternion"]
pub fn Mtx_FromQuat(m: *mut C3D_Mtx, q: C3D_FQuat);
}
extern "C" {
#[doc = " @brief 3D translation"]
#[doc = " @param[in,out] mtx Matrix to translate"]
#[doc = " @param[in] x X component to translate"]
#[doc = " @param[in] y Y component to translate"]
#[doc = " @param[in] z Z component to translate"]
#[doc = " @param[in] bRightSide Whether to transform from the right side"]
#[doc = " @brief 3D translation\n @param[in,out] mtx Matrix to translate\n @param[in] x X component to translate\n @param[in] y Y component to translate\n @param[in] z Z component to translate\n @param[in] bRightSide Whether to transform from the right side"]
pub fn Mtx_Translate(mtx: *mut C3D_Mtx, x: f32, y: f32, z: f32, bRightSide: bool);
}
extern "C" {
#[doc = " @brief 3D Scale"]
#[doc = " @param[in,out] mtx Matrix to scale"]
#[doc = " @param[in] x X component to scale"]
#[doc = " @param[in] y Y component to scale"]
#[doc = " @param[in] z Z component to scale"]
#[doc = " @brief 3D Scale\n @param[in,out] mtx Matrix to scale\n @param[in] x X component to scale\n @param[in] y Y component to scale\n @param[in] z Z component to scale"]
pub fn Mtx_Scale(mtx: *mut C3D_Mtx, x: f32, y: f32, z: f32);
}
extern "C" {
#[doc = " @brief 3D Rotation"]
#[doc = " @param[in,out] mtx Matrix to rotate"]
#[doc = " @param[in] axis Axis about which to rotate"]
#[doc = " @param[in] angle Radians to rotate"]
#[doc = " @param[in] bRightSide Whether to transform from the right side"]
#[doc = " @brief 3D Rotation\n @param[in,out] mtx Matrix to rotate\n @param[in] axis Axis about which to rotate\n @param[in] angle Radians to rotate\n @param[in] bRightSide Whether to transform from the right side"]
pub fn Mtx_Rotate(mtx: *mut C3D_Mtx, axis: C3D_FVec, angle: f32, bRightSide: bool);
}
extern "C" {
#[doc = " @brief 3D Rotation about the X axis"]
#[doc = " @param[in,out] mtx Matrix to rotate"]
#[doc = " @param[in] angle Radians to rotate"]
#[doc = " @param[in] bRightSide Whether to transform from the right side"]
#[doc = " @brief 3D Rotation about the X axis\n @param[in,out] mtx Matrix to rotate\n @param[in] angle Radians to rotate\n @param[in] bRightSide Whether to transform from the right side"]
pub fn Mtx_RotateX(mtx: *mut C3D_Mtx, angle: f32, bRightSide: bool);
}
extern "C" {
#[doc = " @brief 3D Rotation about the Y axis"]
#[doc = " @param[in,out] mtx Matrix to rotate"]
#[doc = " @param[in] angle Radians to rotate"]
#[doc = " @param[in] bRightSide Whether to transform from the right side"]
#[doc = " @brief 3D Rotation about the Y axis\n @param[in,out] mtx Matrix to rotate\n @param[in] angle Radians to rotate\n @param[in] bRightSide Whether to transform from the right side"]
pub fn Mtx_RotateY(mtx: *mut C3D_Mtx, angle: f32, bRightSide: bool);
}
extern "C" {
#[doc = " @brief 3D Rotation about the Z axis"]
#[doc = " @param[in,out] mtx Matrix to rotate"]
#[doc = " @param[in] angle Radians to rotate"]
#[doc = " @param[in] bRightSide Whether to transform from the right side"]
#[doc = " @brief 3D Rotation about the Z axis\n @param[in,out] mtx Matrix to rotate\n @param[in] angle Radians to rotate\n @param[in] bRightSide Whether to transform from the right side"]
pub fn Mtx_RotateZ(mtx: *mut C3D_Mtx, angle: f32, bRightSide: bool);
}
extern "C" {
#[doc = " @brief Orthogonal projection"]
#[doc = " @param[out] mtx Output matrix"]
#[doc = " @param[in] left Left clip plane (X=left)"]
#[doc = " @param[in] right Right clip plane (X=right)"]
#[doc = " @param[in] bottom Bottom clip plane (Y=bottom)"]
#[doc = " @param[in] top Top clip plane (Y=top)"]
#[doc = " @param[in] near Near clip plane (Z=near)"]
#[doc = " @param[in] far Far clip plane (Z=far)"]
#[doc = " @param[in] isLeftHanded Whether to build a LH projection"]
#[doc = " @sa Mtx_OrthoTilt"]
#[doc = " @brief Orthogonal projection\n @param[out] mtx Output matrix\n @param[in] left Left clip plane (X=left)\n @param[in] right Right clip plane (X=right)\n @param[in] bottom Bottom clip plane (Y=bottom)\n @param[in] top Top clip plane (Y=top)\n @param[in] near Near clip plane (Z=near)\n @param[in] far Far clip plane (Z=far)\n @param[in] isLeftHanded Whether to build a LH projection\n @sa Mtx_OrthoTilt"]
pub fn Mtx_Ortho(
mtx: *mut C3D_Mtx,
left: f32,
@ -257,16 +204,7 @@ extern "C" { @@ -257,16 +204,7 @@ extern "C" {
);
}
extern "C" {
#[doc = " @brief Perspective projection"]
#[doc = " @param[out] mtx Output matrix"]
#[doc = " @param[in] fovy Vertical field of view in radians"]
#[doc = " @param[in] aspect Aspect ration of projection plane (width/height)"]
#[doc = " @param[in] near Near clip plane (Z=near)"]
#[doc = " @param[in] far Far clip plane (Z=far)"]
#[doc = " @param[in] isLeftHanded Whether to build a LH projection"]
#[doc = " @sa Mtx_PerspTilt"]
#[doc = " @sa Mtx_PerspStereo"]
#[doc = " @sa Mtx_PerspStereoTilt"]
#[doc = " @brief Perspective projection\n @param[out] mtx Output matrix\n @param[in] fovy Vertical field of view in radians\n @param[in] aspect Aspect ration of projection plane (width/height)\n @param[in] near Near clip plane (Z=near)\n @param[in] far Far clip plane (Z=far)\n @param[in] isLeftHanded Whether to build a LH projection\n @sa Mtx_PerspTilt\n @sa Mtx_PerspStereo\n @sa Mtx_PerspStereoTilt"]
pub fn Mtx_Persp(
mtx: *mut C3D_Mtx,
fovy: f32,
@ -277,24 +215,7 @@ extern "C" { @@ -277,24 +215,7 @@ extern "C" {
);
}
extern "C" {
#[doc = " @brief Stereo perspective projection"]
#[doc = " @note Typically you will use iod to mean the distance between the eyes. Plug"]
#[doc = " in -iod for the left eye and iod for the right eye."]
#[doc = " @note The focal length is defined by screen. If objects are further than this,"]
#[doc = " they will appear to be inside the screen. If objects are closer than this,"]
#[doc = " they will appear to pop out of the screen. Objects at this distance appear"]
#[doc = " to be at the screen."]
#[doc = " @param[out] mtx Output matrix"]
#[doc = " @param[in] fovy Vertical field of view in radians"]
#[doc = " @param[in] aspect Aspect ration of projection plane (width/height)"]
#[doc = " @param[in] near Near clip plane (Z=near)"]
#[doc = " @param[in] far Far clip plane (Z=far)"]
#[doc = " @param[in] iod Interocular distance"]
#[doc = " @param[in] screen Focal length"]
#[doc = " @param[in] isLeftHanded Whether to build a LH projection"]
#[doc = " @sa Mtx_Persp"]
#[doc = " @sa Mtx_PerspTilt"]
#[doc = " @sa Mtx_PerspStereoTilt"]
#[doc = " @brief Stereo perspective projection\n @note Typically you will use iod to mean the distance between the eyes. Plug\n in -iod for the left eye and iod for the right eye.\n @note The focal length is defined by screen. If objects are further than this,\n they will appear to be inside the screen. If objects are closer than this,\n they will appear to pop out of the screen. Objects at this distance appear\n to be at the screen.\n @param[out] mtx Output matrix\n @param[in] fovy Vertical field of view in radians\n @param[in] aspect Aspect ration of projection plane (width/height)\n @param[in] near Near clip plane (Z=near)\n @param[in] far Far clip plane (Z=far)\n @param[in] iod Interocular distance\n @param[in] screen Focal length\n @param[in] isLeftHanded Whether to build a LH projection\n @sa Mtx_Persp\n @sa Mtx_PerspTilt\n @sa Mtx_PerspStereoTilt"]
pub fn Mtx_PerspStereo(
mtx: *mut C3D_Mtx,
fovy: f32,
@ -307,16 +228,7 @@ extern "C" { @@ -307,16 +228,7 @@ extern "C" {
);
}
extern "C" {
#[doc = " @brief Orthogonal projection, tilted to account for the 3DS screen rotation"]
#[doc = " @param[out] mtx Output matrix"]
#[doc = " @param[in] left Left clip plane (X=left)"]
#[doc = " @param[in] right Right clip plane (X=right)"]
#[doc = " @param[in] bottom Bottom clip plane (Y=bottom)"]
#[doc = " @param[in] top Top clip plane (Y=top)"]
#[doc = " @param[in] near Near clip plane (Z=near)"]
#[doc = " @param[in] far Far clip plane (Z=far)"]
#[doc = " @param[in] isLeftHanded Whether to build a LH projection"]
#[doc = " @sa Mtx_Ortho"]
#[doc = " @brief Orthogonal projection, tilted to account for the 3DS screen rotation\n @param[out] mtx Output matrix\n @param[in] left Left clip plane (X=left)\n @param[in] right Right clip plane (X=right)\n @param[in] bottom Bottom clip plane (Y=bottom)\n @param[in] top Top clip plane (Y=top)\n @param[in] near Near clip plane (Z=near)\n @param[in] far Far clip plane (Z=far)\n @param[in] isLeftHanded Whether to build a LH projection\n @sa Mtx_Ortho"]
pub fn Mtx_OrthoTilt(
mtx: *mut C3D_Mtx,
left: f32,
@ -329,16 +241,7 @@ extern "C" { @@ -329,16 +241,7 @@ extern "C" {
);
}
extern "C" {
#[doc = " @brief Perspective projection, tilted to account for the 3DS screen rotation"]
#[doc = " @param[out] mtx Output matrix"]
#[doc = " @param[in] fovy Vertical field of view in radians"]
#[doc = " @param[in] aspect Aspect ration of projection plane (width/height)"]
#[doc = " @param[in] near Near clip plane (Z=near)"]
#[doc = " @param[in] far Far clip plane (Z=far)"]
#[doc = " @param[in] isLeftHanded Whether to build a LH projection"]
#[doc = " @sa Mtx_Persp"]
#[doc = " @sa Mtx_PerspStereo"]
#[doc = " @sa Mtx_PerspStereoTilt"]
#[doc = " @brief Perspective projection, tilted to account for the 3DS screen rotation\n @param[out] mtx Output matrix\n @param[in] fovy Vertical field of view in radians\n @param[in] aspect Aspect ration of projection plane (width/height)\n @param[in] near Near clip plane (Z=near)\n @param[in] far Far clip plane (Z=far)\n @param[in] isLeftHanded Whether to build a LH projection\n @sa Mtx_Persp\n @sa Mtx_PerspStereo\n @sa Mtx_PerspStereoTilt"]
pub fn Mtx_PerspTilt(
mtx: *mut C3D_Mtx,
fovy: f32,
@ -349,19 +252,7 @@ extern "C" { @@ -349,19 +252,7 @@ extern "C" {
);
}
extern "C" {
#[doc = " @brief Stereo perspective projection, tilted to account for the 3DS screen rotation"]
#[doc = " @note See the notes for @ref Mtx_PerspStereo"]
#[doc = " @param[out] mtx Output matrix"]
#[doc = " @param[in] fovy Vertical field of view in radians"]
#[doc = " @param[in] aspect Aspect ration of projection plane (width/height)"]
#[doc = " @param[in] near Near clip plane (Z=near)"]
#[doc = " @param[in] far Far clip plane (Z=far)"]
#[doc = " @param[in] iod Interocular distance"]
#[doc = " @param[in] screen Focal length"]
#[doc = " @param[in] isLeftHanded Whether to build a LH projection"]
#[doc = " @sa Mtx_Persp"]
#[doc = " @sa Mtx_PerspTilt"]
#[doc = " @sa Mtx_PerspStereo"]
#[doc = " @brief Stereo perspective projection, tilted to account for the 3DS screen rotation\n @note See the notes for @ref Mtx_PerspStereo\n @param[out] mtx Output matrix\n @param[in] fovy Vertical field of view in radians\n @param[in] aspect Aspect ration of projection plane (width/height)\n @param[in] near Near clip plane (Z=near)\n @param[in] far Far clip plane (Z=far)\n @param[in] iod Interocular distance\n @param[in] screen Focal length\n @param[in] isLeftHanded Whether to build a LH projection\n @sa Mtx_Persp\n @sa Mtx_PerspTilt\n @sa Mtx_PerspStereo"]
pub fn Mtx_PerspStereoTilt(
mtx: *mut C3D_Mtx,
fovy: f32,
@ -374,13 +265,7 @@ extern "C" { @@ -374,13 +265,7 @@ extern "C" {
);
}
extern "C" {
#[doc = " @brief Look-At matrix, based on DirectX implementation"]
#[doc = " @note See https://msdn.microsoft.com/en-us/library/windows/desktop/bb205342"]
#[doc = " @param[out] out Output matrix."]
#[doc = " @param[in] cameraPosition Position of the intended camera in 3D space."]
#[doc = " @param[in] cameraTarget Position of the intended target the camera is supposed to face in 3D space."]
#[doc = " @param[in] cameraUpVector The vector that points straight up depending on the camera's \"Up\" direction."]
#[doc = " @param[in] isLeftHanded Whether to build a LH projection"]
#[doc = " @brief Look-At matrix, based on DirectX implementation\n @note See https://msdn.microsoft.com/en-us/library/windows/desktop/bb205342\n @param[out] out Output matrix.\n @param[in] cameraPosition Position of the intended camera in 3D space.\n @param[in] cameraTarget Position of the intended target the camera is supposed to face in 3D space.\n @param[in] cameraUpVector The vector that points straight up depending on the camera's \"Up\" direction.\n @param[in] isLeftHanded Whether to build a LH projection"]
pub fn Mtx_LookAt(
out: *mut C3D_Mtx,
cameraPosition: C3D_FVec,
@ -390,84 +275,43 @@ extern "C" { @@ -390,84 +275,43 @@ extern "C" {
);
}
extern "C" {
#[doc = " @brief Multiply two Quaternions"]
#[doc = " @param[in] lhs Multiplicand"]
#[doc = " @param[in] rhs Multiplier"]
#[doc = " @return lhs*rhs"]
#[doc = " @brief Multiply two Quaternions\n @param[in] lhs Multiplicand\n @param[in] rhs Multiplier\n @return lhs*rhs"]
pub fn Quat_Multiply(lhs: C3D_FQuat, rhs: C3D_FQuat) -> C3D_FQuat;
}
extern "C" {
#[doc = " @brief Raise Quaternion to a power"]
#[doc = " @note If p is 0, this returns the identity Quaternion."]
#[doc = " If p is 1, this returns q."]
#[doc = " @param[in] q Base Quaternion"]
#[doc = " @param[in] p Power"]
#[doc = " @return q<sup>p</sup>"]
#[doc = " @brief Raise Quaternion to a power\n @note If p is 0, this returns the identity Quaternion.\n If p is 1, this returns q.\n @param[in] q Base Quaternion\n @param[in] p Power\n @return q<sup>p</sup>"]
pub fn Quat_Pow(q: C3D_FQuat, p: f32) -> C3D_FQuat;
}
extern "C" {
#[doc = " @brief Cross product of Quaternion and FVec3"]
#[doc = " @param[in] q Base Quaternion"]
#[doc = " @param[in] v Vector to cross"]
#[doc = " @return q×v"]
#[doc = " @brief Cross product of Quaternion and FVec3\n @param[in] q Base Quaternion\n @param[in] v Vector to cross\n @return q×v"]
pub fn Quat_CrossFVec3(q: C3D_FQuat, v: C3D_FVec) -> C3D_FVec;
}
extern "C" {
#[doc = " @brief 3D Rotation"]
#[doc = " @param[in] q Quaternion to rotate"]
#[doc = " @param[in] axis Axis about which to rotate"]
#[doc = " @param[in] r Radians to rotate"]
#[doc = " @param[in] bRightSide Whether to transform from the right side"]
#[doc = " @return Rotated Quaternion"]
#[doc = " @brief 3D Rotation\n @param[in] q Quaternion to rotate\n @param[in] axis Axis about which to rotate\n @param[in] r Radians to rotate\n @param[in] bRightSide Whether to transform from the right side\n @return Rotated Quaternion"]
pub fn Quat_Rotate(q: C3D_FQuat, axis: C3D_FVec, r: f32, bRightSide: bool) -> C3D_FQuat;
}
extern "C" {
#[doc = " @brief 3D Rotation about the X axis"]
#[doc = " @param[in] q Quaternion to rotate"]
#[doc = " @param[in] r Radians to rotate"]
#[doc = " @param[in] bRightSide Whether to transform from the right side"]
#[doc = " @return Rotated Quaternion"]
#[doc = " @brief 3D Rotation about the X axis\n @param[in] q Quaternion to rotate\n @param[in] r Radians to rotate\n @param[in] bRightSide Whether to transform from the right side\n @return Rotated Quaternion"]
pub fn Quat_RotateX(q: C3D_FQuat, r: f32, bRightSide: bool) -> C3D_FQuat;
}
extern "C" {
#[doc = " @brief 3D Rotation about the Y axis"]
#[doc = " @param[in] q Quaternion to rotate"]
#[doc = " @param[in] r Radians to rotate"]
#[doc = " @param[in] bRightSide Whether to transform from the right side"]
#[doc = " @return Rotated Quaternion"]
#[doc = " @brief 3D Rotation about the Y axis\n @param[in] q Quaternion to rotate\n @param[in] r Radians to rotate\n @param[in] bRightSide Whether to transform from the right side\n @return Rotated Quaternion"]
pub fn Quat_RotateY(q: C3D_FQuat, r: f32, bRightSide: bool) -> C3D_FQuat;
}
extern "C" {
#[doc = " @brief 3D Rotation about the Z axis"]
#[doc = " @param[in] q Quaternion to rotate"]
#[doc = " @param[in] r Radians to rotate"]
#[doc = " @param[in] bRightSide Whether to transform from the right side"]
#[doc = " @return Rotated Quaternion"]
#[doc = " @brief 3D Rotation about the Z axis\n @param[in] q Quaternion to rotate\n @param[in] r Radians to rotate\n @param[in] bRightSide Whether to transform from the right side\n @return Rotated Quaternion"]
pub fn Quat_RotateZ(q: C3D_FQuat, r: f32, bRightSide: bool) -> C3D_FQuat;
}
extern "C" {
#[doc = " @brief Get Quaternion equivalent to 4x4 matrix"]
#[doc = " @note If the matrix is orthogonal or special orthogonal, where determinant(matrix) = +1.0f, then the matrix can be converted."]
#[doc = " @param[in] m Input Matrix"]
#[doc = " @return Generated Quaternion"]
#[doc = " @brief Get Quaternion equivalent to 4x4 matrix\n @note If the matrix is orthogonal or special orthogonal, where determinant(matrix) = +1.0f, then the matrix can be converted.\n @param[in] m Input Matrix\n @return Generated Quaternion"]
pub fn Quat_FromMtx(m: *const C3D_Mtx) -> C3D_FQuat;
}
extern "C" {
#[doc = " @brief Converting Pitch, Yaw, and Roll to Quaternion equivalent"]
#[doc = " @param[in] pitch The pitch angle in radians."]
#[doc = " @param[in] yaw The yaw angle in radians."]
#[doc = " @param[in] roll The roll angle in radians."]
#[doc = " @param[in] bRightSide Whether to transform from the right side"]
#[doc = " @return C3D_FQuat The Quaternion equivalent with the pitch, yaw, and roll (in that order) orientations applied."]
#[doc = " @brief Converting Pitch, Yaw, and Roll to Quaternion equivalent\n @param[in] pitch The pitch angle in radians.\n @param[in] yaw The yaw angle in radians.\n @param[in] roll The roll angle in radians.\n @param[in] bRightSide Whether to transform from the right side\n @return C3D_FQuat The Quaternion equivalent with the pitch, yaw, and roll (in that order) orientations applied."]
pub fn Quat_FromPitchYawRoll(pitch: f32, yaw: f32, roll: f32, bRightSide: bool) -> C3D_FQuat;
}
extern "C" {
#[doc = " @brief Quaternion Look-At"]
#[doc = " @param[in] source C3D_FVec Starting position. Origin of rotation."]
#[doc = " @param[in] target C3D_FVec Target position to orient towards."]
#[doc = " @param[in] forwardVector C3D_FVec The Up vector."]
#[doc = " @param[in] upVector C3D_FVec The Up vector."]
#[doc = " @return Quaternion rotation."]
#[doc = " @brief Quaternion Look-At\n @param[in] source C3D_FVec Starting position. Origin of rotation.\n @param[in] target C3D_FVec Target position to orient towards.\n @param[in] forwardVector C3D_FVec The Up vector.\n @param[in] upVector C3D_FVec The Up vector.\n @return Quaternion rotation."]
pub fn Quat_LookAt(
source: C3D_FVec,
target: C3D_FVec,
@ -476,10 +320,7 @@ extern "C" { @@ -476,10 +320,7 @@ extern "C" {
) -> C3D_FQuat;
}
extern "C" {
#[doc = " @brief Quaternion, created from a given axis and angle in radians."]
#[doc = " @param[in] axis C3D_FVec The axis to rotate around at."]
#[doc = " @param[in] angle float The angle to rotate. Unit: Radians"]
#[doc = " @return Quaternion rotation based on the axis and angle. Axis doesn't have to be orthogonal."]
#[doc = " @brief Quaternion, created from a given axis and angle in radians.\n @param[in] axis C3D_FVec The axis to rotate around at.\n @param[in] angle float The angle to rotate. Unit: Radians\n @return Quaternion rotation based on the axis and angle. Axis doesn't have to be orthogonal."]
pub fn Quat_FromAxisAngle(axis: C3D_FVec, angle: f32) -> C3D_FQuat;
}
#[repr(C)]
@ -1823,8 +1664,7 @@ extern "C" { @@ -1823,8 +1664,7 @@ extern "C" {
);
}
pub type FILE = __FILE;
#[doc = " @brief Subtexture"]
#[doc = " @note If top > bottom, the subtexture is rotated 1/4 revolution counter-clockwise"]
#[doc = " @brief Subtexture\n @note If top > bottom, the subtexture is rotated 1/4 revolution counter-clockwise"]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Tex3DS_SubTexture {
@ -1849,13 +1689,7 @@ pub struct Tex3DS_Texture_s { @@ -1849,13 +1689,7 @@ pub struct Tex3DS_Texture_s {
#[doc = " @brief Texture"]
pub type Tex3DS_Texture = *mut Tex3DS_Texture_s;
extern "C" {
#[doc = " @brief Import Tex3DS texture"]
#[doc = " @param[in] input Input data"]
#[doc = " @param[in] insize Size of the input data"]
#[doc = " @param[out] tex citro3d texture"]
#[doc = " @param[out] texcube citro3d texcube"]
#[doc = " @param[in] vram Whether to store textures in VRAM"]
#[doc = " @returns Tex3DS texture"]
#[doc = " @brief Import Tex3DS texture\n @param[in] input Input data\n @param[in] insize Size of the input data\n @param[out] tex citro3d texture\n @param[out] texcube citro3d texcube\n @param[in] vram Whether to store textures in VRAM\n @returns Tex3DS texture"]
pub fn Tex3DS_TextureImport(
input: *const ::libc::c_void,
insize: usize,
@ -1865,18 +1699,7 @@ extern "C" { @@ -1865,18 +1699,7 @@ extern "C" {
) -> Tex3DS_Texture;
}
extern "C" {
#[doc = " @brief Import Tex3DS texture"]
#[doc = ""]
#[doc = " @description"]
#[doc = " For example, use this if you want to import from a large file without"]
#[doc = " pulling the entire file into memory."]
#[doc = ""]
#[doc = " @param[out] tex citro3d texture"]
#[doc = " @param[out] texcube citro3d texcube"]
#[doc = " @param[in] vram Whether to store textures in VRAM"]
#[doc = " @param[in] callback Data callback"]
#[doc = " @param[in] userdata User data passed to callback"]
#[doc = " @returns Tex3DS texture"]
#[doc = " @brief Import Tex3DS texture\n\n @description\n For example, use this if you want to import from a large file without\n pulling the entire file into memory.\n\n @param[out] tex citro3d texture\n @param[out] texcube citro3d texcube\n @param[in] vram Whether to store textures in VRAM\n @param[in] callback Data callback\n @param[in] userdata User data passed to callback\n @returns Tex3DS texture"]
pub fn Tex3DS_TextureImportCallback(
tex: *mut C3D_Tex,
texcube: *mut C3D_TexCube,
@ -1886,17 +1709,7 @@ extern "C" { @@ -1886,17 +1709,7 @@ extern "C" {
) -> Tex3DS_Texture;
}
extern "C" {
#[doc = " @brief Import Tex3DS texture"]
#[doc = ""]
#[doc = " Starts reading at the current file descriptor's offset. The file"]
#[doc = " descriptor's position is left at the end of the decoded data. On error, the"]
#[doc = " file descriptor's position is indeterminate."]
#[doc = ""]
#[doc = " @param[in] fd Open file descriptor"]
#[doc = " @param[out] tex citro3d texture"]
#[doc = " @param[out] texcube citro3d texcube"]
#[doc = " @param[in] vram Whether to store textures in VRAM"]
#[doc = " @returns Tex3DS texture"]
#[doc = " @brief Import Tex3DS texture\n\n Starts reading at the current file descriptor's offset. The file\n descriptor's position is left at the end of the decoded data. On error, the\n file descriptor's position is indeterminate.\n\n @param[in] fd Open file descriptor\n @param[out] tex citro3d texture\n @param[out] texcube citro3d texcube\n @param[in] vram Whether to store textures in VRAM\n @returns Tex3DS texture"]
pub fn Tex3DS_TextureImportFD(
fd: ::libc::c_int,
tex: *mut C3D_Tex,
@ -1905,17 +1718,7 @@ extern "C" { @@ -1905,17 +1718,7 @@ extern "C" {
) -> Tex3DS_Texture;
}
extern "C" {
#[doc = " @brief Import Tex3DS texture"]
#[doc = ""]
#[doc = " Starts reading at the current file stream's offset. The file stream's"]
#[doc = " position is left at the end of the decoded data. On error, the file"]
#[doc = " stream's position is indeterminate."]
#[doc = ""]
#[doc = " @param[in] fp Open file stream"]
#[doc = " @param[out] tex citro3d texture"]
#[doc = " @param[out] texcube citro3d texcube"]
#[doc = " @param[in] vram Whether to store textures in VRAM"]
#[doc = " @returns Tex3DS texture"]
#[doc = " @brief Import Tex3DS texture\n\n Starts reading at the current file stream's offset. The file stream's\n position is left at the end of the decoded data. On error, the file\n stream's position is indeterminate.\n\n @param[in] fp Open file stream\n @param[out] tex citro3d texture\n @param[out] texcube citro3d texcube\n @param[in] vram Whether to store textures in VRAM\n @returns Tex3DS texture"]
pub fn Tex3DS_TextureImportStdio(
fp: *mut FILE,
tex: *mut C3D_Tex,
@ -1924,20 +1727,14 @@ extern "C" { @@ -1924,20 +1727,14 @@ extern "C" {
) -> Tex3DS_Texture;
}
extern "C" {
#[doc = " @brief Get number of subtextures"]
#[doc = " @param[in] texture Tex3DS texture"]
#[doc = " @returns Number of subtextures"]
#[doc = " @brief Get number of subtextures\n @param[in] texture Tex3DS texture\n @returns Number of subtextures"]
pub fn Tex3DS_GetNumSubTextures(texture: Tex3DS_Texture) -> usize;
}
extern "C" {
#[doc = " @brief Get subtexture"]
#[doc = " @param[in] texture Tex3DS texture"]
#[doc = " @param[in] index Subtexture index"]
#[doc = " @returns Subtexture info"]
#[doc = " @brief Get subtexture\n @param[in] texture Tex3DS texture\n @param[in] index Subtexture index\n @returns Subtexture info"]
pub fn Tex3DS_GetSubTexture(texture: Tex3DS_Texture, index: usize) -> *const Tex3DS_SubTexture;
}
extern "C" {
#[doc = " @brief Free Tex3DS texture"]
#[doc = " @param[in] texture Tex3DS texture to free"]
#[doc = " @brief Free Tex3DS texture\n @param[in] texture Tex3DS texture to free"]
pub fn Tex3DS_TextureFree(texture: Tex3DS_Texture);
}

2
citro3d-sys/src/lib.rs

@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
pub mod base;
pub mod gx;
pub mod os;
pub mod renderqueue;
pub mod texenv;
pub mod uniforms;
@ -16,6 +17,7 @@ mod bindings; @@ -16,6 +17,7 @@ mod bindings;
pub use base::*;
pub use bindings::*;
pub use gx::*;
pub use os::*;
pub use renderqueue::*;
pub use texenv::*;
pub use uniforms::*;

13
citro3d-sys/src/os.rs

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
// TODO: move this to ctru-sys, maybe?
// would probably be auto-generated via https://github.com/rust3ds/ctru-rs/issues/123
use ctru_sys::{osSharedConfig_s, OS_SHAREDCFG_VADDR};
fn OS_SharedConfig() -> *mut osSharedConfig_s {
OS_SHAREDCFG_VADDR as _
}
/// Gets the state of the 3D slider as a value from 0.0 to 1.0
pub unsafe fn osGet3DSliderState() -> f32 {
(*OS_SharedConfig()).slider_3d
}

219
citro3d/examples/triangle.rs

@ -1,12 +1,15 @@ @@ -1,12 +1,15 @@
//! This example demonstrates the most basic usage of `citro3d`: rendering a simple
//! RGB triangle (sometimes called a "Hello triangle") to the 3DS screen.
#![feature(allocator_api)]
use citro3d::render::{ClearFlags, Target};
use citro3d::attrib;
use citro3d::buffer;
use citro3d::render::{self, ClearFlags};
use citro3d::{include_aligned_bytes, shader};
use citro3d_sys::C3D_Mtx;
use ctru::services::gfx::{Gfx, RawFrameBuffer, Screen};
use ctru::services::apt::Apt;
use ctru::services::hid::{Hid, KeyPad};
use ctru::services::soc::Soc;
use ctru::prelude::*;
use ctru::services::gfx::{RawFrameBuffer, Screen, TopScreen3D};
use std::ffi::CStr;
use std::mem::MaybeUninit;
@ -34,15 +37,15 @@ struct Vertex { @@ -34,15 +37,15 @@ struct Vertex {
static VERTICES: &[Vertex] = &[
Vertex {
pos: Vec3::new(0.0, 0.5, 0.5),
pos: Vec3::new(0.0, 0.5, 3.0),
color: Vec3::new(1.0, 0.0, 0.0),
},
Vertex {
pos: Vec3::new(-0.5, -0.5, 0.5),
pos: Vec3::new(-0.5, -0.5, 3.0),
color: Vec3::new(0.0, 1.0, 0.0),
},
Vertex {
pos: Vec3::new(0.5, -0.5, 0.5),
pos: Vec3::new(0.5, -0.5, 3.0),
color: Vec3::new(0.0, 0.0, 1.0),
},
];
@ -51,6 +54,8 @@ static SHADER_BYTES: &[u8] = @@ -51,6 +54,8 @@ static SHADER_BYTES: &[u8] =
include_aligned_bytes!(concat!(env!("OUT_DIR"), "/examples/assets/vshader.shbin"));
fn main() {
ctru::use_panic_handler();
let mut soc = Soc::new().expect("failed to get SOC");
drop(soc.redirect_to_3dslink(true, true));
@ -58,18 +63,24 @@ fn main() { @@ -58,18 +63,24 @@ fn main() {
let mut hid = Hid::new().expect("Couldn't obtain HID controller");
let apt = Apt::new().expect("Couldn't obtain APT controller");
let mut top_screen = gfx.top_screen.borrow_mut();
let RawFrameBuffer { width, height, .. } = top_screen.raw_framebuffer();
let mut instance = citro3d::Instance::new().expect("failed to initialize Citro3D");
let mut top_target = citro3d::render::Target::new(width.try_into().unwrap(), height.try_into().unwrap(), top_screen, None)
let top_screen = TopScreen3D::from(&gfx.top_screen);
let (mut top_left, mut top_right) = top_screen.split_mut();
let RawFrameBuffer { width, height, .. } = top_left.raw_framebuffer();
let mut top_left_target =
render::Target::new(width, height, top_left, None).expect("failed to create render target");
let RawFrameBuffer { width, height, .. } = top_right.raw_framebuffer();
let mut top_right_target = render::Target::new(width, height, top_right, None)
.expect("failed to create render target");
let mut bottom_screen = gfx.bottom_screen.borrow_mut();
let RawFrameBuffer { width, height, .. } = bottom_screen.raw_framebuffer();
let mut bottom_target = citro3d::render::Target::new(width.try_into().unwrap(), height.try_into().unwrap(), bottom_screen, None)
let mut bottom_target = render::Target::new(width, height, bottom_screen, None)
.expect("failed to create bottom screen render target");
let shader = shader::Library::from_bytes(SHADER_BYTES).unwrap();
@ -80,8 +91,10 @@ fn main() { @@ -80,8 +91,10 @@ fn main() {
let mut vbo_data = Vec::with_capacity_in(VERTICES.len(), ctru::linear::LinearAllocator);
vbo_data.extend_from_slice(VERTICES);
let (uloc_projection, projection) = scene_init(&mut program, &vbo_data);
let mut buf_info = buffer::Info::new();
let (attr_info, vbo_idx) = prepare_vbos(&mut buf_info, &vbo_data);
let projection_uniform_idx = scene_init(&mut program);
while apt.main_loop() {
hid.scan_input();
@ -89,71 +102,136 @@ fn main() { @@ -89,71 +102,136 @@ fn main() {
break;
}
let mut render_to = |target: &mut Target| {
instance.render_frame_with(|instance| {
let mut render_to = |target: &mut render::Target, projection| {
instance
.select_render_target(target)
.expect("failed to set render target");
let clear_color: u32 = 0x7F_7F_7F_FF;
target.clear(ClearFlags::ALL, clear_color, 0);
scene_render(uloc_projection.into(), &projection);
});
unsafe {
// Update the uniforms
citro3d_sys::C3D_FVUnifMtx4x4(
ctru_sys::GPU_VERTEX_SHADER,
projection_uniform_idx.into(),
projection,
);
}
instance.set_attr_info(&attr_info);
instance.draw_arrays(buffer::Primitive::Triangles, vbo_idx);
};
render_to(&mut top_target);
render_to(&mut bottom_target);
let Projections {
left,
right,
center,
} = calculate_projections();
render_to(&mut top_left_target, &left);
render_to(&mut top_right_target, &right);
render_to(&mut bottom_target, &center);
});
}
}
fn scene_init(program: &mut shader::Program, vbo_data: &[Vertex]) -> (i8, C3D_Mtx) {
// Load the vertex shader, create a shader program and bind it
unsafe {
citro3d_sys::C3D_BindProgram(program.as_raw());
// sheeeesh, this sucks to type:
fn prepare_vbos<'buf, 'info, 'vbo>(
buf_info: &'info mut buffer::Info,
vbo_data: &'vbo [Vertex],
) -> (attrib::Info, buffer::Slice<'buf>)
where
'info: 'buf,
'vbo: 'buf,
{
// Configure attributes for use with the vertex shader
let mut attr_info = attrib::Info::new();
// Get the location of the uniforms
let projection_name = CStr::from_bytes_with_nul(b"projection\0").unwrap();
let uloc_projection = ctru_sys::shaderInstanceGetUniformLocation(
(*program.as_raw()).vertexShader,
projection_name.as_ptr(),
let reg0 = attrib::Register::new(0).unwrap();
let reg1 = attrib::Register::new(1).unwrap();
attr_info
.add_loader(reg0, attrib::Format::Float, 3)
.unwrap();
attr_info
.add_loader(reg1, attrib::Format::Float, 3)
.unwrap();
let buf_idx = buf_info.add(vbo_data, &attr_info).unwrap();
(attr_info, buf_idx)
}
struct Projections {
left: C3D_Mtx,
right: C3D_Mtx,
center: C3D_Mtx,
}
fn calculate_projections() -> Projections {
let mut left_eye = MaybeUninit::uninit();
let mut right_eye = MaybeUninit::uninit();
let mut center = MaybeUninit::uninit();
// TODO: it would be cool to allow playing around with these parameters on
// the fly with D-pad, etc.
let slider_val = unsafe { citro3d_sys::osGet3DSliderState() };
let iod = slider_val / 4.0;
let near = 0.01;
let far = 100.0;
let fovy = 40.0_f32.to_radians();
let screen = 2.0;
unsafe {
citro3d_sys::Mtx_PerspStereoTilt(
left_eye.as_mut_ptr(),
fovy,
citro3d_sys::C3D_AspectRatioTop as f32,
near,
far,
-iod,
screen,
true,
);
// Configure attributes for use with the vertex shader
let attr_info = citro3d_sys::C3D_GetAttrInfo();
citro3d_sys::AttrInfo_Init(attr_info);
citro3d_sys::AttrInfo_AddLoader(attr_info, 0, ctru_sys::GPU_FLOAT, 3); // v0=position
citro3d_sys::AttrInfo_AddLoader(attr_info, 1, ctru_sys::GPU_FLOAT, 3); // v1=color
// Compute the projection matrix
let projection = {
let mut projection = MaybeUninit::uninit();
citro3d_sys::Mtx_OrthoTilt(
projection.as_mut_ptr(),
// The 3ds top screen is a 5:3 ratio
-1.66,
1.66,
-1.0,
1.0,
0.0,
1.0,
citro3d_sys::Mtx_PerspStereoTilt(
right_eye.as_mut_ptr(),
fovy,
citro3d_sys::C3D_AspectRatioTop as f32,
near,
far,
iod,
screen,
true,
);
projection.assume_init()
};
// Configure buffers
let buf_info = citro3d_sys::C3D_GetBufInfo();
citro3d_sys::BufInfo_Init(buf_info);
citro3d_sys::BufInfo_Add(
buf_info,
vbo_data.as_ptr().cast(),
std::mem::size_of::<Vertex>()
.try_into()
.expect("size of vec3 fits in u32"),
2, // Each vertex has two attributes
0x10, // v0 = position, v1 = color, in LSB->MSB nibble order
citro3d_sys::Mtx_PerspTilt(
center.as_mut_ptr(),
fovy,
citro3d_sys::C3D_AspectRatioBot as f32,
near,
far,
true,
);
Projections {
left: left_eye.assume_init(),
right: right_eye.assume_init(),
center: center.assume_init(),
}
}
}
fn scene_init(program: &mut shader::Program) -> i8 {
// Load the vertex shader, create a shader program and bind it
unsafe {
citro3d_sys::C3D_BindProgram(program.as_raw());
// Configure the first fragment shading substage to just pass through the vertex color
// See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight
let env = citro3d_sys::C3D_GetTexEnv(0);
@ -167,23 +245,12 @@ fn scene_init(program: &mut shader::Program, vbo_data: &[Vertex]) -> (i8, C3D_Mt @@ -167,23 +245,12 @@ fn scene_init(program: &mut shader::Program, vbo_data: &[Vertex]) -> (i8, C3D_Mt
);
citro3d_sys::C3D_TexEnvFunc(env, citro3d_sys::C3D_Both, ctru_sys::GPU_REPLACE);
(uloc_projection, projection)
}
}
fn scene_render(uloc_projection: i32, projection: &C3D_Mtx) {
unsafe {
// Update the uniforms
citro3d_sys::C3D_FVUnifMtx4x4(ctru_sys::GPU_VERTEX_SHADER, uloc_projection, projection);
// Get the location of the uniforms
let projection_name = CStr::from_bytes_with_nul(b"projection\0").unwrap();
// Draw the VBO
citro3d_sys::C3D_DrawArrays(
ctru_sys::GPU_TRIANGLES,
0,
VERTICES
.len()
.try_into()
.expect("VERTICES.len() fits in i32"),
);
ctru_sys::shaderInstanceGetUniformLocation(
(*program.as_raw()).vertexShader,
projection_name.as_ptr(),
)
}
}

134
citro3d/src/attrib.rs

@ -0,0 +1,134 @@ @@ -0,0 +1,134 @@
//! Configure vertex attributes.
//!
//! This module has types and helpers for describing the shape/structure of vertex
//! data to be sent to the GPU.
//!
//! See the [`buffer`](crate::buffer) module to use the vertex data itself.
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)]
pub struct Info(pub(crate) citro3d_sys::C3D_AttrInfo);
/// A shader input register, usually corresponding to a single vertex attribute
/// (e.g. position or color). These are called `v0`, `v1`, ... `v15` in the
/// [picasso](https://github.com/devkitPro/picasso/blob/master/Manual.md)
/// shader language.
#[derive(Debug, Clone, Copy)]
pub struct Register(libc::c_int);
impl Register {
/// Get a register corresponding to the given index.
///
/// # Errors
///
/// Returns an error for `n >= 16`.
pub fn new(n: u16) -> crate::Result<Self> {
if n < 16 {
Ok(Self(n.into()))
} else {
Err(crate::Error::TooManyAttributes)
}
}
}
/// An attribute index. This is the attribute's actual index in the input buffer,
/// and may correspond to any [`Register`] (or multiple) as input in the shader
/// program.
#[derive(Debug, Clone, Copy)]
pub struct Index(u8);
/// The data format of an attribute.
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum Format {
/// A signed byte, i.e. [`i8`].
Byte = ctru_sys::GPU_BYTE,
/// An unsigned byte, i.e. [`u8`].
UnsignedByte = ctru_sys::GPU_UNSIGNED_BYTE,
/// A float, i.e. [`f32`].
Float = ctru_sys::GPU_FLOAT,
/// A short integer, i.e. [`i16`].
Short = ctru_sys::GPU_SHORT,
}
// SAFETY: the RWLock ensures unique access when mutating the global struct, and
// we trust citro3d to Do The Right Thing™ and not mutate it otherwise.
unsafe impl Sync for Info {}
unsafe impl Send for Info {}
impl Default for Info {
fn default() -> Self {
let mut raw = MaybeUninit::zeroed();
let raw = unsafe {
citro3d_sys::AttrInfo_Init(raw.as_mut_ptr());
raw.assume_init()
};
Self(raw)
}
}
impl Info {
/// Construct a new attribute info structure with no attributes.
pub fn new() -> Self {
Self::default()
}
pub(crate) fn copy_from(raw: *const citro3d_sys::C3D_AttrInfo) -> Option<Self> {
if raw.is_null() {
None
} else {
// This is less efficient than returning a pointer or something, but it's
// safer since we don't know the lifetime of the pointee
Some(Self(unsafe { *raw }))
}
}
/// Add an attribute loader to the attribute info. The resulting attribute index
/// indicates the registration order of the attributes.
///
/// # Parameters
///
/// * `register`: the shader program input register for this attribute.
/// * `format`: the data format of this attribute.
/// * `count`: the number of elements in each attribute (up to 4, corresponding
/// to `xyzw` / `rgba` / `stpq`).
///
/// # Errors
///
/// * If `count > 4`
/// * If this attribute info already has the maximum number of attributes.
pub fn add_loader(
&mut self,
register: Register,
format: Format,
count: u8,
) -> crate::Result<Index> {
if count > 4 {
return Err(crate::Error::InvalidSize);
}
// SAFETY: the &mut self.0 reference is only used to access fields in
// the attribute info, not stored somewhere for later use
let ret = unsafe {
citro3d_sys::AttrInfo_AddLoader(&mut self.0, register.0, format as u32, count.into())
};
let Ok(idx) = ret.try_into() else {
return Err(crate::Error::TooManyAttributes)
};
Ok(Index(idx))
}
pub(crate) fn permutation(&self) -> u64 {
self.0.permutation
}
/// Get the number of registered attributes.
pub fn attr_count(&self) -> libc::c_int {
self.0.attrCount
}
}

140
citro3d/src/buffer.rs

@ -0,0 +1,140 @@ @@ -0,0 +1,140 @@
//! 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 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)]
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)]
pub struct Slice<'buf> {
index: libc::c_int,
size: libc::c_int,
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<'_> {
/// Get the index into the buffer for this slice.
pub fn index(&self) -> libc::c_int {
self.index
}
/// Get the length of the slice.
#[must_use]
pub fn len(&self) -> libc::c_int {
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 {
self.buf_info
}
}
/// The geometric primitive to draw (i.e. what shapes the buffer data describes).
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum Primitive {
/// Draw triangles (3 vertices per triangle).
Triangles = ctru_sys::GPU_TRIANGLES,
/// Draw a triangle strip (each vertex shared by 1-3 triangles).
TriangleStrip = ctru_sys::GPU_TRIANGLE_STRIP,
/// Draw a triangle fan (first vertex shared by all triangles).
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,
}
impl Default for Info {
fn default() -> Self {
let mut info = MaybeUninit::zeroed();
let info = unsafe {
citro3d_sys::BufInfo_Init(info.as_mut_ptr());
info.assume_init()
};
Self(info)
}
}
impl Info {
/// Construct buffer info without any registered data.
pub fn new() -> Self {
Self::default()
}
pub(crate) fn copy_from(raw: *const citro3d_sys::C3D_BufInfo) -> Option<Self> {
if raw.is_null() {
None
} else {
// This is less efficient than returning a pointer or something, but it's
// safer since we don't know the lifetime of the pointee
Some(Self(unsafe { *raw }))
}
}
/// 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>(
&'this mut self,
vbo_data: &'vbo [T],
attrib_info: &attrib::Info,
) -> crate::Result<Slice<'idx>>
where
'this: 'idx,
'vbo: 'idx,
{
let stride = std::mem::size_of::<T>().try_into()?;
// 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.
let res = unsafe {
citro3d_sys::BufInfo_Add(
&mut self.0,
vbo_data.as_ptr().cast(),
stride,
attrib_info.attr_count(),
attrib_info.permutation(),
)
};
// Error codes from <https://github.com/devkitPro/citro3d/blob/master/source/buffers.c#L11>
match res {
..=-3 => Err(crate::Error::System(res)),
-2 => Err(crate::Error::InvalidMemoryLocation),
-1 => Err(crate::Error::TooManyBuffers),
_ => Ok(Slice {
index: res,
size: vbo_data.len().try_into()?,
buf_info: self,
}),
}
}
}

22
citro3d/src/error.rs

@ -1,20 +1,36 @@ @@ -1,20 +1,36 @@
//! General-purpose error and result types returned by public APIs of this crate.
use std::num::TryFromIntError;
use std::sync::TryLockError;
/// The common result type returned by `citro3d` functions.
pub type Result<T> = std::result::Result<T, Error>;
// TODO probably want a similar type to ctru::Result to make it easier to convert
// nonzero result codes to errors.
/// The common error type that may be returned by `citro3d` functions.
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
/// C3D error code.
System(libc::c_int),
/// A C3D object or context could not be initialized.
FailedToInitialize,
/// A size parameter was specified that cannot be converted to the proper type.
InvalidSize,
/// Failed to select the given render target for drawing to.
InvalidRenderTarget,
/// Indicates that a reference could not be obtained because a lock is already
/// held on the requested object.
LockHeld,
/// Indicates that too many vertex attributes were registered (max 12 supported).
TooManyAttributes,
/// Indicates that too many vertex buffer objects were registered (max 12 supported).
TooManyBuffers,
/// The given memory could not be converted to a physical address for sharing
/// with the GPU. Data should be allocated with [`ctru::linear`].
InvalidMemoryLocation,
}
impl From<TryFromIntError> for Error {
@ -22,3 +38,9 @@ impl From<TryFromIntError> for Error { @@ -22,3 +38,9 @@ impl From<TryFromIntError> for Error {
Self::InvalidSize
}
}
impl<T> From<TryLockError<T>> for Error {
fn from(_: TryLockError<T>) -> Self {
Self::LockHeld
}
}

48
citro3d/src/lib.rs

@ -1,10 +1,10 @@ @@ -1,10 +1,10 @@
//! Safe Rust bindings to `citro3d`.
pub mod attrib;
pub mod buffer;
pub mod error;
pub mod render;
pub mod shader;
pub mod texture;
pub mod vbo;
use citro3d_sys::C3D_FrameDrawOn;
pub use error::{Error, Result};
@ -12,6 +12,7 @@ pub use error::{Error, Result}; @@ -12,6 +12,7 @@ pub use error::{Error, Result};
/// The single instance for using `citro3d`. This is the base type that an application
/// should instantiate to use this library.
#[non_exhaustive]
#[must_use]
#[derive(Debug)]
pub struct Instance;
@ -70,6 +71,49 @@ impl Instance { @@ -70,6 +71,49 @@ impl Instance {
citro3d_sys::C3D_FrameEnd(0);
}
}
/// Get the buffer info being used, if it exists. Note that the resulting
/// [`buffer::Info`] is copied from the one currently in use.
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.
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.
unsafe { citro3d_sys::C3D_SetBufInfo(raw.cast_mut()) };
}
/// Get the attribute info being used, if it exists. Note that the resulting
/// [`attrib::Info`] is copied from the one currently in use.
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.
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
pub fn draw_arrays(&mut self, primitive: buffer::Primitive, index: buffer::Slice) {
self.set_buffer_info(index.info());
// TODO: should we also require the attrib info directly here?
unsafe {
citro3d_sys::C3D_DrawArrays(
primitive as ctru_sys::GPU_Primitive_t,
index.index(),
index.len(),
);
}
}
}
impl Drop for Instance {

11
citro3d/src/render.rs

@ -39,8 +39,8 @@ impl<'screen> Target<'screen> { @@ -39,8 +39,8 @@ impl<'screen> Target<'screen> {
///
/// Fails if the target could not be created.
pub fn new(
width: u16,
height: u16,
width: usize,
height: usize,
screen: RefMut<'screen, dyn Screen>,
depth_format: Option<DepthFormat>,
) -> Result<Self> {
@ -48,8 +48,8 @@ impl<'screen> Target<'screen> { @@ -48,8 +48,8 @@ impl<'screen> Target<'screen> {
let raw = unsafe {
C3D_RenderTargetCreate(
width.into(),
height.into(),
width.try_into()?,
height.try_into()?,
color_format as GPU_COLORBUF,
depth_format.map_or(C3D_DEPTHTYPE { __i: -1 }, DepthFormat::as_raw),
)
@ -96,8 +96,11 @@ impl<'screen> Target<'screen> { @@ -96,8 +96,11 @@ impl<'screen> Target<'screen> {
bitflags::bitflags! {
/// Indicate whether color, depth buffer, or both values should be cleared.
pub struct ClearFlags: u32 {
/// Clear the color of the render target.
const COLOR = citro3d_sys::C3D_CLEAR_COLOR;
/// Clear the depth buffer value of the render target.
const DEPTH = citro3d_sys::C3D_CLEAR_DEPTH;
/// Clear both color and depth buffer values of the render target.
const ALL = citro3d_sys::C3D_CLEAR_ALL;
}
}

22
citro3d/src/shader.rs

@ -7,12 +7,15 @@ @@ -7,12 +7,15 @@
use std::error::Error;
use std::mem::MaybeUninit;
// Macros get exported at the crate root, so no reason to document this module.
// It still needs to be `pub` for the helper struct it exports.
#[doc(hidden)]
pub mod macros;
/// A PICA200 shader program. It may have one or both of:
///
/// * A vertex [shader instance](Instance)
/// * A geometry [shader instance](Instance)
/// * A vertex shader [`Library`]
/// * A geometry shader [`Library`]
///
/// The PICA200 does not support user-programmable fragment shaders.
pub struct Program {
@ -86,7 +89,7 @@ impl Drop for Program { @@ -86,7 +89,7 @@ impl Drop for Program {
/// one or more [`Entrypoint`]s, but most commonly has one vertex shader and an
/// optional geometry shader.
///
/// This is the result of parsing a shader binary (shbin), and the resulting
/// This is the result of parsing a shader binary (`.shbin`), and the resulting
/// [`Entrypoint`]s can be used as part of a [`Program`].
pub struct Library(*mut ctru_sys::DVLB_s);
@ -110,11 +113,19 @@ impl Library { @@ -110,11 +113,19 @@ impl Library {
}))
}
/// Get the number of [`Entrypoint`]s in this shader library.
#[must_use]
pub fn len(&self) -> usize {
unsafe { (*self.0).numDVLE as usize }
}
/// Whether the library has any [`Entrypoint`]s or not.
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Get the [`Entrypoint`] at the given index, if present.
#[must_use]
pub fn get(&self, index: usize) -> Option<Entrypoint> {
if index < self.len() {
@ -127,11 +138,6 @@ impl Library { @@ -127,11 +138,6 @@ impl Library {
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
fn as_raw(&mut self) -> *mut ctru_sys::DVLB_s {
self.0
}

15
citro3d/src/shader/macros.rs

@ -1,9 +1,4 @@ @@ -1,9 +1,4 @@
/// Helper struct to [`include_bytes`] aligned as a specific type.
#[repr(C)] // guarantee 'bytes' comes after '_align'
pub struct AlignedAs<Align, Bytes: ?Sized> {
pub _align: [Align; 0],
pub bytes: Bytes,
}
//! Helper macros for working with shader data.
/// Helper macro for including a file as bytes that are correctly aligned for
/// use as a [`Library`](super::Library).
@ -22,3 +17,11 @@ macro_rules! include_aligned_bytes { @@ -22,3 +17,11 @@ macro_rules! include_aligned_bytes {
&ALIGNED.bytes
}};
}
/// Helper struct to [`include_bytes`] aligned as a specific type.
#[repr(C)] // guarantee 'bytes' comes after '_align'
#[doc(hidden)]
pub struct AlignedAs<Align, Bytes: ?Sized> {
pub _align: [Align; 0],
pub bytes: Bytes,
}

1
citro3d/src/texture.rs

@ -1 +0,0 @@ @@ -1 +0,0 @@

1
citro3d/src/vbo.rs

@ -1 +0,0 @@ @@ -1 +0,0 @@
Loading…
Cancel
Save