Skip to content

Commit 8e9205d

Browse files
Use a vertex buffer slice in draw_elements args
1 parent 8f9d70a commit 8e9205d

File tree

5 files changed

+30
-31
lines changed

5 files changed

+30
-31
lines changed

citro3d/examples/cube.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ fn main() {
122122
vbo_data.push(vert);
123123
}
124124

125+
let attr_info = build_attrib_info();
126+
125127
let mut buf_info = buffer::Info::new();
126-
let (attr_info, vbo_slice) = prepare_vbos(&mut buf_info, &vbo_data);
128+
let vbo_slice = buf_info.add(&vbo_data, &attr_info).unwrap();
127129

128130
// Configure the first fragment shading substage to just pass through the vertex color
129131
// See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight
@@ -148,7 +150,7 @@ fn main() {
148150
16, 19, 17, 17, 19, 18, // back (+z)
149151
20, 21, 23, 21, 22, 23, // forward (-z)
150152
];
151-
let indices = vbo_slice.index_buffer(indices).unwrap();
153+
let index_buffer = vbo_slice.index_buffer(indices).unwrap();
152154

153155
while apt.main_loop() {
154156
hid.scan_input();
@@ -169,7 +171,7 @@ fn main() {
169171

170172
instance.set_attr_info(&attr_info);
171173
unsafe {
172-
instance.draw_elements(buffer::Primitive::Triangles, &buf_info, &indices);
174+
instance.draw_elements(buffer::Primitive::Triangles, vbo_slice, &index_buffer);
173175
}
174176
};
175177

@@ -186,10 +188,7 @@ fn main() {
186188
}
187189
}
188190

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

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

207-
let buf_idx = buf_info.add(vbo_data, &attr_info).unwrap();
208-
209-
(attr_info, buf_idx)
206+
attr_info
210207
}
211208

212209
struct Projections {

citro3d/src/buffer.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl Slice<'_> {
5757
/// Returns an error if:
5858
/// - any of the given indices are out of bounds.
5959
/// - the given slice is too long for its length to fit in a `libc::c_int`.
60-
pub fn index_buffer<I>(&self, indices: &[I]) -> Result<Vec<I, LinearAllocator>, Error>
60+
pub fn index_buffer<I>(&self, indices: &[I]) -> Result<Indices<I>, Error>
6161
where
6262
I: Index + Copy + Into<libc::c_int>,
6363
{
@@ -83,16 +83,22 @@ impl Slice<'_> {
8383
///
8484
/// If any indices are outside this buffer it can cause an invalid access by the GPU
8585
/// (this crashes citra).
86-
pub unsafe fn index_buffer_unchecked<I: Index + Clone>(
87-
&self,
88-
indices: &[I],
89-
) -> Vec<I, LinearAllocator> {
90-
let mut buf = Vec::with_capacity_in(indices.len(), LinearAllocator);
91-
buf.extend_from_slice(indices);
92-
buf
86+
pub unsafe fn index_buffer_unchecked<I: Index + Clone>(&self, indices: &[I]) -> Indices<I> {
87+
let mut buffer = Vec::with_capacity_in(indices.len(), LinearAllocator);
88+
buffer.extend_from_slice(indices);
89+
Indices {
90+
buffer,
91+
_slice: *self,
92+
}
9393
}
9494
}
9595

96+
/// An index buffer for indexed drawing. See [`Slice::index_buffer`] to obtain one.
97+
pub struct Indices<'buf, I> {
98+
pub(crate) buffer: Vec<I, LinearAllocator>,
99+
_slice: Slice<'buf>,
100+
}
101+
96102
/// A type that can be used as an index for indexed drawing.
97103
pub trait Index: crate::private::Sealed {
98104
/// The data type of the index, as used by [`citro3d_sys::C3D_DrawElements`]'s `type_` parameter.

citro3d/src/error.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! General-purpose error and result types returned by public APIs of this crate.
22
3-
use core::fmt;
43
use std::ffi::NulError;
54
use std::num::TryFromIntError;
65
use std::sync::TryLockError;

citro3d/src/lib.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ use std::cell::{OnceCell, RefMut};
3030
use std::fmt;
3131
use std::rc::Rc;
3232

33-
use ctru::linear::LinearAllocation;
3433
use ctru::services::gfx::Screen;
3534
pub use error::{Error, Result};
3635

36+
use self::buffer::{Index, Indices};
3737
use self::texenv::TexEnv;
3838
use self::uniform::Uniform;
3939

@@ -219,18 +219,15 @@ impl Instance {
219219
///
220220
/// If the given index buffer is too long to have its length converted to `i32`.
221221
#[doc(alias = "C3D_DrawElements")]
222-
pub unsafe fn draw_elements<I, Indices>(
222+
pub unsafe fn draw_elements<I: Index>(
223223
&mut self,
224224
primitive: buffer::Primitive,
225-
buf: &buffer::Info,
226-
indices: &Indices,
227-
) where
228-
I: buffer::Index,
229-
Indices: AsRef<[I]> + LinearAllocation,
230-
{
231-
self.set_buffer_info(buf);
232-
233-
let indices = indices.as_ref();
225+
vbo_data: buffer::Slice,
226+
indices: &Indices<I>,
227+
) {
228+
self.set_buffer_info(vbo_data.info());
229+
230+
let indices = &indices.buffer;
234231
let elements = indices.as_ptr().cast();
235232

236233
citro3d_sys::C3D_DrawElements(

citro3d/src/math/ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl AbsDiffEq for Matrix4 {
212212
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
213213
self.rows_wzyx()
214214
.into_iter()
215-
.zip(other.rows_wzyx().into_iter())
215+
.zip(other.rows_wzyx())
216216
.all(|(l, r)| l.abs_diff_eq(&r, epsilon))
217217
}
218218
}

0 commit comments

Comments
 (0)