Skip to main content

grafix_toolbox/kit/opengl/geom/
vao.rs

1use super::{super::internal::*, *};
2
3#[derive(Default, Debug)]
4pub struct Vao<I> {
5	i: Dummy<I>,
6	o: Obj<VertArrT>,
7}
8impl<I: IdxType> Vao<I> {
9	pub fn obj(&self) -> u32 {
10		self.o.obj
11	}
12	pub fn Bind(&self) -> VaoBind<I> {
13		VaoBind::new(self)
14	}
15	pub fn BindIdxs(&mut self, o: &IdxArr<I>) {
16		GL!(glVaoElementBuffer(self.obj(), o.obj));
17		debug_assert!({
18			*Index::crossbindcheck_map().entry(self.obj()).or_default() = vec![o.obj];
19			true
20		});
21	}
22	pub fn AttribFmt<A: AttrType>(&mut self, o: &AttrArr<A>, args: impl AttrFmtArgs) {
23		let (idx, size, norm, stride, offset) = args.get();
24		ASSERT!(size > 0 && size < 5, "Attribute size({size}) isn't valid");
25		let t_size = u32(type_size::<A>());
26		GL!(glVertexAttribFormat(self.obj(), o.obj, idx, size, A::TYPE, to_glbool(norm), stride, offset, t_size));
27		debug_assert!({
28			let attrs = Attribute::crossbindcheck_map().entry(self.obj()).or_default();
29			if attrs.len() < usize(idx + 1) {
30				attrs.resize(usize(idx + 1), u32::MAX);
31			}
32			attrs[usize(idx)] = o.obj;
33			true
34		});
35	}
36}
37impl<I> Drop for Vao<I> {
38	fn drop(&mut self) {
39		debug_assert!({
40			Index::crossbindcheck_map().remove(&self.o.obj);
41			Attribute::crossbindcheck_map().remove(&self.o.obj);
42			true
43		})
44	}
45}
46
47pub struct VaoBind<'l, I> {
48	i: Dummy<I>,
49	_b: Bind<'l, VertArrT>,
50}
51impl<I: IdxType> VaoBind<'_, I> {
52	fn new(o: &Vao<I>) -> Self {
53		let _b = Bind::new(&o.o);
54		Self { i: Dummy, _b }
55	}
56	pub fn Draw(&self, args: impl DrawArgs) {
57		VertArrT::bound_obj().pipe_as(Index::checkcrossbinds);
58		VertArrT::bound_obj().pipe_as(Attribute::checkcrossbinds);
59		let (num, offset, mode) = args.get();
60		GL!(gl::DrawElements(mode, num, I::TYPE, (offset * type_size::<I>()) as *const GLvoid));
61	}
62	pub fn DrawUnindexed(&self, args: impl DrawArgs) {
63		VertArrT::bound_obj().pipe_as(Attribute::checkcrossbinds);
64		let (num, offset, mode) = args.get();
65		GL!(gl::DrawArrays(mode, i32(offset), num));
66	}
67	pub fn DrawInstanced<A>(&self, n: A, args: impl DrawArgs)
68	where
69		i32: Cast<A>,
70	{
71		VertArrT::bound_obj().pipe_as(Index::checkcrossbinds);
72		VertArrT::bound_obj().pipe_as(Attribute::checkcrossbinds);
73		let (num, offset, mode) = args.get();
74		let offset = (offset * type_size::<I>()) as *const GLvoid;
75		GL!(gl::DrawElementsInstanced(mode, num, I::TYPE, offset, i32(n)));
76	}
77}