I=(S=Uno.Graphics.Framebuffer = function(){
}).prototype;

I.Clear = function(color)
{
    gl.clearColor(color.X, color.Y, color.Z, color.W);
    gl.bindFramebuffer(36160, this.fb);
    gl.clear(16384 | 256);
    gl.bindFramebuffer(36160, null);
};

I.GLBind = function()
{
    gl.bindFramebuffer(36160, this.fb);
    gl.viewport(0, 0, this.cb.Width(), this.cb.Height());
};

I.GLUnbind = function()
{
    gl.bindFramebuffer(36160, Uno.Application.GLBackbufferFramebuffer());
    var vp = Uno.Application.GLBackbufferViewport();
    gl.viewport(vp.X, vp.Y, vp.Z, vp.W);
};

I.ColorBuffer0 = function()
{
    return this.cb;
};

S.$0 = function(width, height, format, depth, $this) {
    if (!$this) $this = new Uno.Graphics.Framebuffer;
    $this.rb = null;
    Uno.Graphics.OpenGL.Tools.CheckGLError();
    $this.fb = gl.createFramebuffer();
    gl.bindFramebuffer(36160, $this.fb);
    $this.cb = Uno.Graphics.Texture2D.$0(width, height, format);
    gl.framebufferTexture2D(36160, 36064, 3553, $this.cb.GLHandle(), 0);
    Uno.Graphics.OpenGL.Tools.CheckGLError();

    if (depth)
    {
        $this.rb = gl.createRenderbuffer();
        gl.bindRenderbuffer(36161, $this.rb);
        gl.renderbufferStorage(36161, 33189, width, height);
        gl.bindRenderbuffer(36161, null);
        gl.framebufferRenderbuffer(36160, 36096, 36161, $this.rb);
        Uno.Graphics.OpenGL.Tools.CheckGLError();
    }

    Uno.Graphics.OpenGL.Tools.CheckGLFramebufferStatus();
    Uno.Graphics.OpenGL.Tools.CheckGLError();
    gl.bindFramebuffer(36160, null);
    return $this;
};
