Skip to content

Commit

Permalink
Use a vertex buffer slice in draw_elements args
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-h-chamberlain committed Jun 20, 2024
1 parent 8f9d70a commit 8e9205d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
17 changes: 7 additions & 10 deletions citro3d/examples/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ fn main() {
vbo_data.push(vert);
}

let attr_info = build_attrib_info();

let mut buf_info = buffer::Info::new();
let (attr_info, vbo_slice) = prepare_vbos(&mut buf_info, &vbo_data);
let vbo_slice = buf_info.add(&vbo_data, &attr_info).unwrap();

// 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
Expand All @@ -148,7 +150,7 @@ fn main() {
16, 19, 17, 17, 19, 18, // back (+z)
20, 21, 23, 21, 22, 23, // forward (-z)
];
let indices = vbo_slice.index_buffer(indices).unwrap();
let index_buffer = vbo_slice.index_buffer(indices).unwrap();

while apt.main_loop() {
hid.scan_input();
Expand All @@ -169,7 +171,7 @@ fn main() {

instance.set_attr_info(&attr_info);
unsafe {
instance.draw_elements(buffer::Primitive::Triangles, &buf_info, &indices);
instance.draw_elements(buffer::Primitive::Triangles, vbo_slice, &index_buffer);
}
};

Expand All @@ -186,10 +188,7 @@ fn main() {
}
}

fn prepare_vbos<'a>(
buf_info: &'a mut buffer::Info,
vbo_data: &'a [Vertex],
) -> (attrib::Info, buffer::Slice<'a>) {
fn build_attrib_info() -> attrib::Info {
// Configure attributes for use with the vertex shader
let mut attr_info = attrib::Info::new();

Expand All @@ -204,9 +203,7 @@ fn prepare_vbos<'a>(
.add_loader(reg1, attrib::Format::Float, 3)
.unwrap();

let buf_idx = buf_info.add(vbo_data, &attr_info).unwrap();

(attr_info, buf_idx)
attr_info
}

struct Projections {
Expand Down
22 changes: 14 additions & 8 deletions citro3d/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Slice<'_> {
/// Returns an error if:
/// - any of the given indices are out of bounds.
/// - the given slice is too long for its length to fit in a `libc::c_int`.
pub fn index_buffer<I>(&self, indices: &[I]) -> Result<Vec<I, LinearAllocator>, Error>
pub fn index_buffer<I>(&self, indices: &[I]) -> Result<Indices<I>, Error>
where
I: Index + Copy + Into<libc::c_int>,
{
Expand All @@ -83,16 +83,22 @@ impl Slice<'_> {
///
/// If any indices are outside this buffer it can cause an invalid access by the GPU
/// (this crashes citra).
pub unsafe fn index_buffer_unchecked<I: Index + Clone>(
&self,
indices: &[I],
) -> Vec<I, LinearAllocator> {
let mut buf = Vec::with_capacity_in(indices.len(), LinearAllocator);
buf.extend_from_slice(indices);
buf
pub unsafe fn index_buffer_unchecked<I: Index + Clone>(&self, indices: &[I]) -> Indices<I> {
let mut buffer = Vec::with_capacity_in(indices.len(), LinearAllocator);
buffer.extend_from_slice(indices);
Indices {
buffer,
_slice: *self,
}
}
}

/// An index buffer for indexed drawing. See [`Slice::index_buffer`] to obtain one.
pub struct Indices<'buf, I> {
pub(crate) buffer: Vec<I, LinearAllocator>,
_slice: Slice<'buf>,
}

/// A type that can be used as an index for indexed drawing.
pub trait Index: crate::private::Sealed {
/// The data type of the index, as used by [`citro3d_sys::C3D_DrawElements`]'s `type_` parameter.
Expand Down
1 change: 0 additions & 1 deletion citro3d/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! General-purpose error and result types returned by public APIs of this crate.

use core::fmt;
use std::ffi::NulError;
use std::num::TryFromIntError;
use std::sync::TryLockError;
Expand Down
19 changes: 8 additions & 11 deletions citro3d/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ use std::cell::{OnceCell, RefMut};
use std::fmt;
use std::rc::Rc;

use ctru::linear::LinearAllocation;
use ctru::services::gfx::Screen;
pub use error::{Error, Result};

use self::buffer::{Index, Indices};
use self::texenv::TexEnv;
use self::uniform::Uniform;

Expand Down Expand Up @@ -219,18 +219,15 @@ impl Instance {
///
/// If the given index buffer is too long to have its length converted to `i32`.
#[doc(alias = "C3D_DrawElements")]
pub unsafe fn draw_elements<I, Indices>(
pub unsafe fn draw_elements<I: Index>(
&mut self,
primitive: buffer::Primitive,
buf: &buffer::Info,
indices: &Indices,
) where
I: buffer::Index,
Indices: AsRef<[I]> + LinearAllocation,
{
self.set_buffer_info(buf);

let indices = indices.as_ref();
vbo_data: buffer::Slice,
indices: &Indices<I>,
) {
self.set_buffer_info(vbo_data.info());

let indices = &indices.buffer;
let elements = indices.as_ptr().cast();

citro3d_sys::C3D_DrawElements(
Expand Down
2 changes: 1 addition & 1 deletion citro3d/src/math/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl AbsDiffEq for Matrix4 {
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
self.rows_wzyx()
.into_iter()
.zip(other.rows_wzyx().into_iter())
.zip(other.rows_wzyx())
.all(|(l, r)| l.abs_diff_eq(&r, epsilon))
}
}
Expand Down

0 comments on commit 8e9205d

Please sign in to comment.