1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use super::{format::*, vao_args::*, *};

#[derive(Default, Debug)]
pub struct Vao<I> {
	o: Object<VertArrObj>,
	d: Dummy<I>,
}
impl<I: IdxType> Vao<I> {
	pub fn new() -> Self {
		Self { o: Def(), d: Dummy }
	}
	pub fn obj(&self) -> u32 {
		self.o.obj
	}
	pub fn Bind(&self) -> VaoBinding<I> {
		VaoBinding::new(self)
	}
	pub fn BindIdxs(&mut self, o: &IdxArr<I>) {
		GL!(glVaoElementBuffer(self.obj(), o.obj));
		debug_assert!({
			*Index::crossbindcheck_map().entry(self.obj()).or_default() = vec![o.obj];
			true
		});
	}
	pub fn AttribFmt<A: AttrType>(&mut self, o: &AttrArr<A>, args: impl AttrFmtArgs) {
		let (idx, size, norm, stride, offset) = args.get();
		ASSERT!(size > 0 && size < 5, "Attribute size({size}) isn't valid");
		let t_size = u32(type_size::<A>());
		GL!(glVertexAttribFormat(self.obj(), o.obj, idx, size, A::TYPE, to_glbool(norm), stride, offset, t_size));
		debug_assert!({
			let attrs = Attribute::crossbindcheck_map().entry(self.obj()).or_default();
			if attrs.len() < usize(idx + 1) {
				attrs.resize(usize(idx + 1), u32::MAX);
			}
			attrs[usize(idx)] = o.obj;
			true
		});
	}
}
impl<I> Drop for Vao<I> {
	fn drop(&mut self) {
		debug_assert!({
			Index::crossbindcheck_map().remove(&self.o.obj);
			Attribute::crossbindcheck_map().remove(&self.o.obj);
			true
		})
	}
}

pub struct VaoBinding<'l, I> {
	_b: Binding<'l, VertArrObj>,
	d: Dummy<I>,
}
impl<I: IdxType> VaoBinding<'_, I> {
	pub fn new(o: &Vao<I>) -> Self {
		let _b = Binding::new(&o.o);
		Self { _b, d: Dummy }
	}
	pub fn Draw(&self, args: impl DrawArgs) {
		Index::checkcrossbinds(VertArrObj::bound_obj());
		Attribute::checkcrossbinds(VertArrObj::bound_obj());
		let (num, offset, mode) = args.get();
		GL!(gl::DrawElements(mode, num, I::TYPE, (offset * type_size::<I>()) as *const GLvoid));
	}
	pub fn DrawUnindexed(&self, args: impl DrawArgs) {
		Attribute::checkcrossbinds(VertArrObj::bound_obj());
		let (num, offset, mode) = args.get();
		GL!(gl::DrawArrays(mode, i32(offset), num));
	}
	pub fn DrawInstanced<T>(&self, n: T, args: impl DrawArgs)
	where
		i32: Cast<T>,
	{
		Index::checkcrossbinds(VertArrObj::bound_obj());
		Attribute::checkcrossbinds(VertArrObj::bound_obj());
		let (num, offset, mode) = args.get();
		let offset = (offset * type_size::<I>()) as *const GLvoid;
		GL!(gl::DrawElementsInstanced(mode, num, I::TYPE, offset, i32(n)));
	}
}