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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::core::*;

pub struct Screen {}

impl Screen {
    pub fn write(gl: &Gl, x: i32, y: i32, width: usize, height: usize,
                          clear_color: Option<&Vec4>, clear_depth: Option<f32>, render: &dyn Fn()) -> Result<(), Error>
    {
        gl.viewport(x, y, width, height);
        gl.bind_framebuffer(consts::DRAW_FRAMEBUFFER, None);
        RenderTarget::clear(gl, clear_color, clear_depth);
        render();
        Ok(())
    }

    // TODO: Possible to change format
    #[cfg(not(target_arch = "wasm32"))]
    pub fn read_color(gl: &Gl, x: i32, y: i32, width: usize, height: usize) -> Result<Vec<u8>, Error>
    {
        gl.viewport(x, y, width, height);
        let mut pixels = vec![0u8; width * height * 3];
        gl.bind_framebuffer(consts::READ_FRAMEBUFFER, None);
        gl.read_pixels_with_u8_data(x as u32, y as u32, width as u32, height as u32, consts::RGB,
                            consts::UNSIGNED_BYTE, &mut pixels);
        Ok(pixels)
    }

    // TODO: Possible to change format
    #[cfg(not(target_arch = "wasm32"))]
    pub fn read_depth(gl: &Gl, x: i32, y: i32, width: usize, height: usize) -> Result<Vec<f32>, Error>
    {
        gl.viewport(x, y, width, height);
        let mut pixels = vec![0f32; width * height];
        gl.bind_framebuffer(consts::READ_FRAMEBUFFER, None);
        gl.read_pixels_with_f32_data(x as u32, y as u32, width as u32, height as u32,
                        consts::DEPTH_COMPONENT, consts::FLOAT, &mut pixels);
        Ok(pixels)
    }

    #[cfg(all(not(target_arch = "wasm32"), feature = "image-io"))]
    pub fn save_color(path: &str, gl: &Gl, x: i32, y: i32, width: usize, height: usize) -> Result<(), Error>
    {
        let pixels = Self::read_color(gl, x, y, width, height)?;
        let mut pixels_out = vec![0u8; width * height * 3];
        for row in 0..height {
            for col in 0..width {
                for i in 0..3 {
                    pixels_out[3 * width * (height - row - 1) + 3 * col + i] =
                        pixels[3 * width * row + 3 * col + i];
                }
            }
        }

        image::save_buffer(&std::path::Path::new(path), &pixels_out, width as u32, height as u32, image::RGB(8))?;
        Ok(())
    }
}

pub struct RenderTarget {}

impl RenderTarget
{
    pub fn write_to_color(gl: &Gl, x: i32, y: i32, width: usize, height: usize,
                          clear_color: Option<&Vec4>, color_texture: Option<&Texture2D>, render: &dyn Fn()) -> Result<(), Error>
    {
        Self::write(gl, x, y, width, height, clear_color, None, color_texture, None, render)
    }

    pub fn write_to_depth(gl: &Gl, x: i32, y: i32, width: usize, height: usize,
                          clear_depth: Option<f32>, depth_texture: Option<&Texture2D>, render: &dyn Fn()) -> Result<(), Error>
    {
        Self::write(gl, x, y, width, height, None, clear_depth, None, depth_texture, render)
    }

    pub fn write(gl: &Gl, x: i32, y: i32, width: usize, height: usize,
                 clear_color: Option<&Vec4>, clear_depth: Option<f32>,
                 color_texture: Option<&Texture2D>, depth_texture: Option<&Texture2D>,
                 render: &dyn Fn()) -> Result<(), Error>
    {
        gl.viewport(x, y, width, height);
        let id = RenderTarget::new_framebuffer(gl, if color_texture.is_some() {1} else {0})?;

        if let Some(color_texture) = color_texture {
            color_texture.bind_as_color_target(0);
        }

        if let Some(depth_texture) = depth_texture {
            depth_texture.bind_as_depth_target();
        }

        #[cfg(feature = "debug")]
        {
            gl.check_framebuffer_status().or_else(|message| Err(Error::FailedToCreateFramebuffer {message}))?;
        }
        RenderTarget::clear(gl, clear_color, clear_depth);

        render();

        gl.delete_framebuffer(Some(&id));

        if let Some(color_texture) = color_texture {
            color_texture.generate_mip_maps();
        }

        if let Some(depth_texture) = depth_texture {
            depth_texture.generate_mip_maps();
        }
        Ok(())
    }

    pub fn write_to_color_array(gl: &Gl, x: i32, y: i32, width: usize, height: usize,
                                clear_color: Option<&Vec4>,
                       color_texture_array: Option<&Texture2DArray>,
                                color_channel_count: usize, color_channel_to_texture_layer: &dyn Fn(usize) -> usize,
                                render: &dyn Fn()) -> Result<(), Error>
    {
        Self::write_array(gl, x, y, width, height, clear_color, None, color_texture_array, None, color_channel_count, color_channel_to_texture_layer, 0, render)
    }

    pub fn write_to_depth_array(gl: &Gl, x: i32, y: i32, width: usize, height: usize, clear_depth: Option<f32>,
                       depth_texture_array: Option<&Texture2DArray>, depth_layer: usize,
                                render: &dyn Fn()) -> Result<(), Error>
    {
        Self::write_array(gl, x, y, width, height,None, clear_depth, None, depth_texture_array, 0, &|i| {i}, depth_layer, render)
    }

    pub fn write_array(gl: &Gl, x: i32, y: i32, width: usize, height: usize,
                       clear_color: Option<&Vec4>, clear_depth: Option<f32>,
                       color_texture_array: Option<&Texture2DArray>,
                       depth_texture_array: Option<&Texture2DArray>,
                       color_channel_count: usize, color_channel_to_texture_layer: &dyn Fn(usize) -> usize,
                       depth_layer: usize, render: &dyn Fn()) -> Result<(), Error>
    {
        gl.viewport(x, y, width, height);
        let id = RenderTarget::new_framebuffer(gl, color_channel_count)?;

        if let Some(color_texture) = color_texture_array {
            for channel in 0..color_channel_count {
                color_texture.bind_as_color_target(color_channel_to_texture_layer(channel), channel);
            }
        }

        if let Some(depth_texture) = depth_texture_array {
            depth_texture.bind_as_depth_target(depth_layer);
        }

        #[cfg(feature = "debug")]
        {
            gl.check_framebuffer_status().or_else(|message| Err(Error::FailedToCreateFramebuffer {message}))?;
        }
        RenderTarget::clear(gl, clear_color, clear_depth);

        render();

        gl.delete_framebuffer(Some(&id));
        if let Some(color_texture) = color_texture_array {
            color_texture.generate_mip_maps();
        }

        if let Some(depth_texture) = depth_texture_array {
            depth_texture.generate_mip_maps();
        }
        Ok(())
    }

    // TODO: Read color and depth from rendertarget to cpu
    /*#[cfg(not(target_arch = "wasm32"))]
    pub fn read_color(&self, x: i32, y: i32, width: usize, height: usize) -> Result<Vec<u8>, Error>
    {
        let color_texture = self.color_texture.as_ref().unwrap();
        self.gl.viewport(x, y, width, height);
        let mut pixels = vec![0u8; width * height * 3];
        let id = RenderTarget::new_framebuffer(&self.gl, 1)?;
        color_texture.bind_as_color_target(0);
        self.gl.check_framebuffer_status().or_else(|message| Err(Error::FailedToCreateFramebuffer {message}))?;

        self.gl.bind_framebuffer(consts::READ_FRAMEBUFFER, Some(&id));
        self.gl.read_pixels(x as u32, y as u32, width as u32, height as u32, consts::RGB,
                            consts::UNSIGNED_BYTE, &mut pixels);
        self.gl.delete_framebuffer(Some(&id));
        Ok(pixels)
    }*/

    /*#[cfg(not(target_arch = "wasm32"))]
    pub fn read_depth(&self, x: i32, y: i32, width: usize, height: usize) -> Result<Vec<f32>, Error>
    {
        let depth_texture = self.depth_texture.as_ref().unwrap();
        self.gl.viewport(x, y, width, height);
        let mut pixels = vec![0f32; width * height];
        let id = RenderTarget::new_framebuffer(&self.gl, 0)?;
        depth_texture.bind_as_depth_target();
        self.gl.check_framebuffer_status().or_else(|message| Err(Error::FailedToCreateFramebuffer {message}))?;

        self.gl.bind_framebuffer(consts::READ_FRAMEBUFFER, Some(&id));
        self.gl.read_depths(x as u32, y as u32, width as u32, height as u32,
                        consts::DEPTH_COMPONENT, consts::FLOAT, &mut pixels);
        self.gl.delete_framebuffer(Some(&id));
        Ok(pixels)
    }*/

    fn new_framebuffer(gl: &Gl, no_color_channels: usize) -> Result<crate::gl::Framebuffer, Error>
    {
        let id = gl.create_framebuffer()
            .ok_or_else(|| Error::FailedToCreateFramebuffer {message: "Failed to create framebuffer".to_string()} )?;
        gl.bind_framebuffer(consts::DRAW_FRAMEBUFFER, Some(&id));

        if no_color_channels > 0 {
            let mut draw_buffers = Vec::new();
            for i in 0..no_color_channels {
                draw_buffers.push(consts::COLOR_ATTACHMENT0 + i as u32);
            }
            gl.draw_buffers(&draw_buffers);
        }
        Ok(id)
    }

    fn clear(gl: &Gl, clear_color: Option<&Vec4>, clear_depth: Option<f32>) {
        if let Some(color) = clear_color {
            if let Some(depth) = clear_depth {
                gl.clear_color(color.x, color.y, color.z, color.w);
                depth_write(gl,true);
                gl.clear_depth(depth);
                gl.clear(consts::COLOR_BUFFER_BIT | consts::DEPTH_BUFFER_BIT);
            }
            else {
                gl.clear_color(color.x, color.y, color.z, color.w);
                gl.clear(consts::COLOR_BUFFER_BIT);
            }
        } else if let Some(depth) = clear_depth {
            gl.clear_depth(depth);
            depth_write(gl, true);
            gl.clear(consts::DEPTH_BUFFER_BIT);
        }
    }

    /*pub fn blit_color_and_depth_to(&self, target: &RenderTarget, target_color_texture: &Texture2D, target_depth_texture: &Texture2D)
    {
        self.read();
        target.write_to_color_and_depth(target_color_texture, target_depth_texture).unwrap();
        depth_write(&self.gl, true);
        self.gl.blit_framebuffer(0, 0, target_color_texture.width as u32, target_color_texture.height as u32,
                                 0, 0, target_color_texture.width as u32, target_color_texture.height as u32,
                                 consts::COLOR_BUFFER_BIT | consts::DEPTH_BUFFER_BIT, consts::NEAREST);
    }

    pub fn blit_color_to(&self, target: &RenderTarget, target_texture: &Texture2D)
    {
        self.read();
        target.write_to_color(target_texture).unwrap();
        self.gl.blit_framebuffer(0, 0, target_texture.width as u32, target_texture.height as u32,
                                 0, 0, target_texture.width as u32, target_texture.height as u32,
                                 consts::COLOR_BUFFER_BIT, consts::NEAREST);
    }

    pub fn blit_depth_to(&self, target: &RenderTarget, target_texture: &Texture2D)
    {
        self.read();
        target.write_to_depth(target_texture).unwrap();
        depth_write(&self.gl, true);
        self.gl.blit_framebuffer(0, 0, target_texture.width as u32, target_texture.height as u32,
                                 0, 0, target_texture.width as u32, target_texture.height as u32,
                                 consts::DEPTH_BUFFER_BIT, consts::NEAREST);
    }*/
}