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
use fbo::{self, ValidatedAttachments};

use context::Context;
use ContextExt;
use Rect;

use QueryExt;
use draw_parameters::TimeElapsedQuery;

use Api;
use version::Version;
use gl;


pub fn clear(context: &Context, framebuffer: Option<&ValidatedAttachments>,
             rect: Option<&Rect>, color: Option<(f32, f32, f32, f32)>, color_srgb: bool,
             depth: Option<f32>, stencil: Option<i32>)
{
    unsafe {
        let mut ctxt = context.make_current();

        let fbo_id = fbo::FramebuffersContainer::get_framebuffer_for_drawing(&mut ctxt, framebuffer);
        fbo::bind_framebuffer(&mut ctxt, fbo_id, true, false);

        if ctxt.state.enabled_rasterizer_discard {
            ctxt.gl.Disable(gl::RASTERIZER_DISCARD);
            ctxt.state.enabled_rasterizer_discard = false;
        }

        if ctxt.state.color_mask != (1, 1, 1, 1) {
            ctxt.state.color_mask = (1, 1, 1, 1);
            ctxt.gl.ColorMask(1, 1, 1, 1);
        }

        if ctxt.version >= &Version(Api::Gl, 3, 0) || ctxt.extensions.gl_arb_framebuffer_srgb ||
           ctxt.extensions.gl_ext_framebuffer_srgb || ctxt.extensions.gl_ext_srgb_write_control
        {
            if !color_srgb && !ctxt.state.enabled_framebuffer_srgb {
                ctxt.gl.Enable(gl::FRAMEBUFFER_SRGB);
                ctxt.state.enabled_framebuffer_srgb = true;

            } else if color_srgb && ctxt.state.enabled_framebuffer_srgb {
                ctxt.gl.Disable(gl::FRAMEBUFFER_SRGB);
                ctxt.state.enabled_framebuffer_srgb = false;
            }
        }

        TimeElapsedQuery::end_conditional_render(&mut ctxt);

        if let Some(rect) = rect {
            let rect = (rect.left as gl::types::GLint, rect.bottom as gl::types::GLint,
                        rect.width as gl::types::GLsizei, rect.height as gl::types::GLsizei);

            if ctxt.state.scissor != Some(rect) {
                ctxt.gl.Scissor(rect.0, rect.1, rect.2, rect.3);
                ctxt.state.scissor = Some(rect);
            }

            if !ctxt.state.enabled_scissor_test {
                ctxt.gl.Enable(gl::SCISSOR_TEST);
                ctxt.state.enabled_scissor_test = true;
            }

        } else {
            if ctxt.state.enabled_scissor_test {
                ctxt.gl.Disable(gl::SCISSOR_TEST);
                ctxt.state.enabled_scissor_test = false;
            }
        }

        let mut flags = 0;

        if let Some(color) = color {
            let color = (color.0 as gl::types::GLclampf, color.1 as gl::types::GLclampf,
                         color.2 as gl::types::GLclampf, color.3 as gl::types::GLclampf);

            flags |= gl::COLOR_BUFFER_BIT;

            if ctxt.state.clear_color != color {
                ctxt.gl.ClearColor(color.0, color.1, color.2, color.3);
                ctxt.state.clear_color = color;
            }
        }

        if let Some(depth) = depth {
            let depth = depth as gl::types::GLclampf;

            flags |= gl::DEPTH_BUFFER_BIT;

            if ctxt.state.clear_depth != depth {
                if ctxt.version >= &Version(Api::Gl, 1, 0) {
                    ctxt.gl.ClearDepth(depth as gl::types::GLclampd);
                } else if ctxt.version >= &Version(Api::GlEs, 2, 0) {
                    ctxt.gl.ClearDepthf(depth);
                } else {
                    unreachable!();
                }

                ctxt.state.clear_depth = depth;
            }

            if !ctxt.state.depth_mask {
                ctxt.gl.DepthMask(gl::TRUE);
                ctxt.state.depth_mask = true;
            }
        }

        if let Some(stencil) = stencil {
            let stencil = stencil as gl::types::GLint;

            flags |= gl::STENCIL_BUFFER_BIT;

            if ctxt.state.clear_stencil != stencil {
                ctxt.gl.ClearStencil(stencil);
                ctxt.state.clear_stencil = stencil;
            }
        }

        ctxt.gl.Clear(flags);
    }
}