Skip to main content

grafix_toolbox/kit/opengl/frame/
fbo.rs

1use super::*;
2
3pub struct Fbo<S, F> {
4	pub fb: Framebuffer,
5	pub tex: Tex2d<S, F>,
6}
7impl<S: TexSize, F: TexFmt> Fbo<S, F> {
8	pub fn new(args: impl FboArgs) -> Self {
9		let tex = Tex2d::<S, F>::new_empty(args.get(), 1);
10		let fb = Framebuffer::new().attach((&tex, gl::COLOR_ATTACHMENT0));
11		Self { fb, tex }
12	}
13}
14impl<S: TexSize, F: TexFmt> Frame for Fbo<S, F> {
15	fn ClearColor(&self, args: impl ClearArgs) {
16		self.fb.Clear(gl::COLOR, args);
17	}
18	fn size(&self) -> uVec2 {
19		vec2(self.tex.whdl().xy())
20	}
21	fn bind(&self) -> Bind<FramebuffT> {
22		self.fb.Bind(&self.tex)
23	}
24}
25
26pub struct Slab<S, F> {
27	pub src: Fbo<S, F>,
28	pub tgt: Fbo<S, F>,
29}
30impl<S: TexSize, F: TexFmt> Slab<S, F> {
31	pub fn new(args: impl FboArgs) -> Self {
32		Self { src: Fbo::new(args), tgt: Fbo::new(args) }
33	}
34	pub fn swap(&mut self) {
35		mem::swap(&mut self.src, &mut self.tgt);
36	}
37}
38
39pub trait FboArgs: Copy {
40	fn get(self) -> iVec2;
41}
42impl<W: Copy, H: Copy> FboArgs for (W, H)
43where
44	i32: Cast<W> + Cast<H>,
45{
46	fn get(self) -> iVec2 {
47		vec2(self)
48	}
49}