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
use super::*;

#[macro_export]
macro_rules! Sampler {
($(($n: expr, $v: expr)),+) => {{
	use $crate::GL::{macro_uses::sampler_use::id, gl::*};
	let id = const { id(&[$($n, $v),+]) };
	Sampler::pooled(id, &[$(($n, $v)),+])
}};
}

impl<S: AsRef<[(GLenum, GLenum)]>> From<S> for Sampler {
	fn from(args: S) -> Self {
		let mut s = Self::new();
		args.as_ref().iter().for_each(|&(p, v)| s.Parameter(p, v));
		s
	}
}
impl Sampler {
	pub fn pooled(id: u32, args: &[(GLenum, GLenum)]) -> Rc<Self> {
		let p = LocalStatic!(HashMap<u32, Weak<Sampler>>);

		if let Some(w) = p.get(&id) {
			if let Some(s) = w.upgrade() {
				ASSERT!(
					LocalStatic!(HashMap<u32, Vec<(u32, u32)>>).entry(id).or_insert(args.to_vec()).iter().eq(args.iter()),
					"Sampler param collision"
				);
				return s;
			}
		}

		let s = Rc::new(args.into());
		p.insert(id, Rc::downgrade(&s));
		s
	}
	pub fn linear() -> Rc<Self> {
		Sampler!(
			(TEXTURE_MIN_FILTER, LINEAR),
			(TEXTURE_WRAP_R, CLAMP_TO_EDGE),
			(TEXTURE_WRAP_S, CLAMP_TO_EDGE),
			(TEXTURE_WRAP_T, CLAMP_TO_EDGE)
		)
	}
}

pub type Sampler = Object<SamplObj>;
impl Sampler {
	#![allow(private_bounds)]
	pub fn Parameter(&mut self, name: GLenum, args: impl SamplerArg) {
		args.apply(self.obj, name);
	}
}

trait SamplerArg {
	fn apply(&self, _: u32, _: GLenum);
}
impl SamplerArg for GLenum {
	fn apply(&self, obj: u32, name: GLenum) {
		GL!(gl::SamplerParameteri(obj, name, i32(*self)));
	}
}
impl SamplerArg for f32 {
	fn apply(&self, obj: u32, name: GLenum) {
		GL!(gl::SamplerParameterf(obj, name, *self));
	}
}
impl SamplerArg for Vec4 {
	fn apply(&self, obj: u32, name: GLenum) {
		let s = [*self];
		GL!(gl::SamplerParameterfv(obj, name, s.as_ptr() as *const f32));
	}
}

pub mod sampler_use {
	pub use super::chksum::const_fnv1_u32 as id;
}