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
use fbo::{Fbo, BorrowColorAttach, BorrowDepthAttach};
use ::Rect;
use std::rc::Rc;

pub trait OffscreenBuffer {
    type ColorAttach: BorrowColorAttach;
    type DepthAttach: BorrowDepthAttach;
    fn render_buffer(&self) -> &Fbo<Self::ColorAttach, Self::DepthAttach>;
    fn color_attachment(&self, idx: usize) -> Option<&Self::ColorAttach>;
    fn depth_attachment(&self) -> Option<&Self::DepthAttach>;
}

impl<'a, C: BorrowColorAttach, D: BorrowDepthAttach> OffscreenBuffer for Fbo<C,D>{
    type ColorAttach = C;
    type DepthAttach = D;
    fn render_buffer(&self) -> &Fbo<C,D>{
        self
    }

    fn color_attachment(&self, idx: usize) -> Option<&C>{
        self.color(idx)
    }

    fn depth_attachment(&self) -> Option<&Self::DepthAttach>{
        self.depth()
    }
}

impl<'a, C: BorrowColorAttach, D: BorrowDepthAttach> OffscreenBuffer for &'a Fbo<C,D>{
    type ColorAttach = C;
    type DepthAttach = D;
    fn render_buffer(&self) -> &Fbo<C,D>{
        self
    }

    fn color_attachment(&self, idx: usize) -> Option<&C>{
        self.color(idx)
    }

    fn depth_attachment(&self) -> Option<&Self::DepthAttach>{
        self.depth()
    }
}

impl<C: BorrowColorAttach, D: BorrowDepthAttach> OffscreenBuffer for Rc<Fbo<C,D>>{
    type ColorAttach = C;
    type DepthAttach = D;
    fn render_buffer(&self) -> &Fbo<C,D>{
        &self
    }

    fn color_attachment(&self, idx: usize) -> Option<&C>{
        self.color(idx)
    }

    fn depth_attachment(&self) -> Option<&Self::DepthAttach>{
        self.depth()
    }
}

pub unsafe trait UntypedOffscreenBuffer {
    fn id(&self) -> u32;
    fn viewport(&self) -> Rect;
    fn bind_attachments(&self);
}

unsafe impl<F: OffscreenBuffer> UntypedOffscreenBuffer for F{
    fn id(&self) -> u32{
        self.render_buffer().id()
    }

    fn viewport(&self) -> Rect{
        self.render_buffer().viewport()
    }

    fn bind_attachments(&self) {
        self.render_buffer().bind_attachments()
    }
}