summaryrefslogtreecommitdiff
path: root/src/framebuffer.rs
blob: db808e419c0138559128b7b70893b61254b987f9 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use glow::HasContext;

use crate::context;

pub struct Framebuffer {
    pub tex: Option<glow::Texture>,
    pub fbo: Option<glow::Framebuffer>,
    pub dims: glam::Vec2,
    pub offsets: glam::Vec2,
}

impl Framebuffer {
    pub fn screen(ctx: &context::Context) -> Self {
        #[cfg(target_arch = "wasm32")]
        let (windoww, windowh): (f32, f32) = if ctx.resize {
            ctx.window.inner_size().into()
        } else {
            (ctx.render_width, ctx.render_height)
        };
        #[cfg(not(target_arch = "wasm32"))]
        let (windoww, windowh) = {
            let (w, h) = ctx.window.borrow().get_size();
            (w as f32, h as f32)
        };
        let ratio = ctx.compute_upscale(windoww as _, windowh as _) as f32;
        let upscalew = ctx.render_width * ratio;
        let upscaleh = ctx.render_height * ratio;
        let offsetx = (windoww - upscalew) / 2.0;
        let offsety = (windowh - upscaleh) / 2.0;
        log::info!("resize window: {:?}, upscale: {:?}, offset: {:?}", (windoww, windowh), (upscalew, upscaleh), (offsetx, offsety));
        Self {
            tex: None,
            fbo: None,
            dims: glam::Vec2::new(upscalew, upscaleh),
            offsets: glam::Vec2::new(offsetx, offsety),
        }
    }

    pub fn new(ctx: &context::Context, dims: &glam::Vec2, offsets: &glam::Vec2) -> Self {
        unsafe {
            let fbo = ctx.gl.create_framebuffer()
                .expect("failed to create framebuffer");
            ctx.gl.bind_framebuffer(glow::FRAMEBUFFER, Some(fbo));

            let depth_buffer = ctx.gl.create_renderbuffer()
                .expect("failed to create depth buffer");
            ctx.gl.bind_renderbuffer(glow::RENDERBUFFER, Some(depth_buffer));
            ctx.gl.renderbuffer_storage(glow::RENDERBUFFER, glow::DEPTH_COMPONENT32F, dims.x as _, dims.y as _);
            ctx.gl.framebuffer_renderbuffer(glow::FRAMEBUFFER, glow::DEPTH_ATTACHMENT, glow::RENDERBUFFER, Some(depth_buffer));

            let stencil_buffer = ctx.gl.create_renderbuffer()
                .expect("failed to create stencil buffer");
            ctx.gl.bind_renderbuffer(glow::RENDERBUFFER, Some(stencil_buffer));
            ctx.gl.renderbuffer_storage(glow::RENDERBUFFER, glow::DEPTH_STENCIL, dims.x as _, dims.y as _);
            ctx.gl.framebuffer_renderbuffer(glow::FRAMEBUFFER, glow::DEPTH_STENCIL_ATTACHMENT, glow::RENDERBUFFER, Some(stencil_buffer));

            let tex = ctx.gl.create_texture()
                .expect("failed to create framebuffer texture");
            ctx.gl.bind_texture(glow::TEXTURE_2D, Some(tex));
            ctx.gl.tex_image_2d(
                glow::TEXTURE_2D,
                0,
                glow::RGBA as _,
                dims.x as _,
                dims.y as _,
                0,
                glow::RGBA,
                glow::UNSIGNED_BYTE,
                None,
            );
            ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::CLAMP_TO_EDGE as _);
            ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::CLAMP_TO_EDGE as _);
            ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MIN_FILTER, glow::NEAREST as _);
            ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, glow::NEAREST as _);
            ctx.gl.framebuffer_texture_2d(glow::FRAMEBUFFER, glow::COLOR_ATTACHMENT0, glow::TEXTURE_2D, Some(tex), 0);
            ctx.gl.draw_buffer(glow::COLOR_ATTACHMENT0);

            let status = ctx.gl.check_framebuffer_status(glow::FRAMEBUFFER);
            if status != glow::FRAMEBUFFER_COMPLETE {
                panic!("error initializing framebuffer:\n{}", status);
            }

            Self {
                tex: Some(tex),
                fbo: Some(fbo),
                dims: dims.clone(),
                offsets: offsets.clone(),
            }
        }
    }

    pub fn bind_texture(&self, ctx: &context::Context) {
        unsafe {
            ctx.gl.active_texture(glow::TEXTURE0);
            ctx.gl.bind_texture(glow::TEXTURE_2D, self.tex);
        }
    }

    pub fn bind(&self, ctx: &context::Context) {
        unsafe {
            ctx.gl.bind_framebuffer(glow::FRAMEBUFFER, self.fbo);
            ctx.gl.viewport(
                self.offsets.x as _,
                self.offsets.y as _,
                self.dims.x as _,
                self.dims.y as _,
            );
        }
    }

    pub fn blit(&self, ctx: &context::Context, dest: &Self, pos: &glam::Vec2, scale: &glam::Vec2) {
        unsafe {
            ctx.gl.bind_framebuffer(glow::READ_FRAMEBUFFER, self.fbo);
            ctx.gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, dest.fbo);
            ctx.gl.blit_framebuffer(
                0, 0, self.dims.x as _, self.dims.y as _,
                pos.x as _, pos.y as _, (pos.x + scale.x) as _, (pos.y + scale.y) as _,
                glow::COLOR_BUFFER_BIT, glow::NEAREST
            );
        }
    }

    pub fn get_pixels(&self, ctx: &context::Context, buf: &mut [glam::Vec3]) {
        let w = self.dims.x as usize;
        let h = self.dims.y as usize;
        let tmp = vec![glam::Vec3::default(); w * h];
        unsafe {
            ctx.gl.bind_texture(glow::TEXTURE_2D, self.tex);
            ctx.gl.get_tex_image(
                glow::TEXTURE_2D, 0, glow::RGB, glow::FLOAT,
                glow::PixelPackData::Slice(
                    std::slice::from_raw_parts_mut(
                        tmp.as_ptr() as *mut u8,
                        tmp.len() * std::mem::size_of::<glam::Vec3>()
                    )
                ),
            );
        }
        for y in 0..h {
            for x in 0..w {
                buf[x + (h - 1 - y) * w] = tmp[x + y * w];
            }
        }
    }
}