Skip to main content

grafix_toolbox/kit/opengl/
shader.rs

1pub use shader_ext::*;
2
3#[macro_use]
4pub mod uniform;
5
6#[macro_export]
7macro_rules! SHADER {
8	($n: ident, $($body: expr),+) => {
9		#[allow(non_upper_case_globals)]
10		pub const $n: $crate::GL::macro_uses::InlineShader = $crate::GL::macro_uses::InlineShader(stringify!($n), &[$($body,)+]);
11	};
12}
13
14pub struct ShaderManager {
15	sn: Sender<ShaderTask>,
16	rx: Offhand<ShdResult>,
17	mailbox: HashMap<ShdName, Res<ShdProg>>,
18}
19impl ShaderManager {
20	pub fn Initialize<'s, W: Window>(args: impl InitArgs<'s, W>) {
21		let (window, i) = args.get();
22		Some(window).pipe(Self::get_or_init).sn.send(Includes(i)).valid();
23	}
24	pub fn Load(filenames: impl LoadArgs) {
25		for n in filenames.get() {
26			let file = load(n.clone(), FS::Lazy::Text(n));
27			ShaderManager::get().sn.send(Load(file)).valid();
28		}
29	}
30	pub fn Watch(filenames: impl LoadArgs) {
31		for n in filenames.get() {
32			let file = load(n.clone(), FS::Watch::Text(n));
33			ShaderManager::get().sn.send(Load(file)).valid();
34		}
35	}
36	pub fn CleanCache() {
37		ShaderManager::get().sn.send(Clean).valid();
38	}
39	pub fn inline_source(name: &str, source: &[&str]) {
40		ShaderManager::get().sn.send(Inline((name.into(), source.concat()))).valid();
41	}
42	fn get() -> &'static mut Self {
43		Self::get_or_init::<WindowImpl>(None)
44	}
45	fn get_or_init<W: Window>(w: Option<&mut W>) -> &'static mut Self {
46		LeakyStatic!(ShaderManager, {
47			let w = w.unwrap_or_else(|| ERROR!("Must Initialize ShaderManager before first use"));
48			let (sn, rx) = Offhand::from_fn(w, 64, compiler);
49			Self { sn, rx, mailbox: Def() }
50		})
51	}
52}
53
54mod args;
55mod compiler;
56mod object;
57mod parsing;
58mod shader_ext;
59
60pub struct InlineShader(pub STR, pub &'static [STR]);
61impl From<I> for Str {
62	fn from(v: I) -> Self {
63		let InlineShader(v, v_t) = v;
64		ShaderManager::inline_source(v, v_t);
65		v.into()
66	}
67}
68type I = InlineShader;
69
70use crate::{GL::offhand::*, GL::window::*, lazy::*, lib::*, slicing::*, sync::*};
71use {super::internal::*, args::*, compiler::*, parsing::*, std::ffi::CString};