3 This file is part of libmspgl
4 Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include "ext_framebuffer_blit.h"
10 #include "ext_framebuffer_object.h"
11 #include "framebuffer.h"
13 #include "renderbuffer.h"
14 #include "texture2d.h"
21 Framebuffer::Framebuffer(unsigned i):
26 throw InvalidParameterValue("System framebuffer must have id 0");
29 glGetIntegerv(GL_VIEWPORT, viewport);
34 Framebuffer::Framebuffer():
39 static RequireExtension _ext("GL_EXT_framebuffer_object");
41 glGenFramebuffersEXT(1, &id);
44 Framebuffer::~Framebuffer()
47 glDeleteFramebuffersEXT(1, &id);
52 void Framebuffer::update_attachment(unsigned mask) const
56 GLenum color_buf = GL_NONE;
57 for(unsigned i=0; i<attachments.size(); ++i)
59 const Attachment &attch = attachments[i];
62 if(attch.type==GL_RENDERBUFFER_EXT)
63 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, GL_RENDERBUFFER_EXT, attch.rbuf->get_id());
64 else if(attch.type==GL_TEXTURE_2D)
66 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
67 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
70 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, 0, 0);
73 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
74 color_buf = attch.attachment;
77 glDrawBuffer(color_buf);
83 void Framebuffer::check_size()
85 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
88 if(i->type==GL_RENDERBUFFER_EXT)
90 width = i->rbuf->get_width();
91 height = i->rbuf->get_height();
93 else if(i->type==GL_TEXTURE_2D)
95 Texture2D *tex = static_cast<Texture2D *>(i->tex);
96 width = tex->get_width();
97 height = tex->get_height();
100 glViewport(0, 0, width, height);
105 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
108 throw InvalidState("Can't attach to system framebuffer");
110 unsigned i = get_attachment_index(attch);
111 attachments[i].set(rbuf);
112 update_attachment(1<<i);
116 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
119 throw InvalidState("Can't attach to system framebuffer");
121 unsigned i = get_attachment_index(attch);
122 attachments[i].set(tex, level);
123 update_attachment(1<<i);
127 void Framebuffer::detach(FramebufferAttachment attch)
130 throw InvalidState("Can't detach from system framebuffer");
132 unsigned i = get_attachment_index(attch);
133 attachments[i].clear();
134 update_attachment(1<<i);
138 FramebufferStatus Framebuffer::check_status() const
140 Bind _bind(this, true);
141 return static_cast<FramebufferStatus>(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
144 void Framebuffer::clear(BufferBits bits)
146 Bind _bind(this, true);
150 void Framebuffer::blit_from(const Framebuffer &other, int sx0, int sy0, int sx1, int sy1, int dx0, int dy0, int dx1, int dy1, BufferBits bits, bool filter)
152 static RequireExtension _ext("GL_EXT_framebuffer_blit");
154 const Framebuffer *old = current();
155 if(set_current(this))
157 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, id);
160 update_attachment(dirty);
165 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, other.id);
167 glBlitFramebufferEXT(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
170 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (old ? old->id : 0));
173 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
175 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
178 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
180 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
183 void Framebuffer::bind() const
185 if(set_current(this))
187 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
190 update_attachment(dirty);
194 glViewport(0, 0, width, height);
198 const Framebuffer *Framebuffer::current()
205 void Framebuffer::unbind()
210 Framebuffer &Framebuffer::system()
212 static Framebuffer sys_framebuf(0);
216 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
218 for(unsigned i=0; i<attachments.size(); ++i)
219 if(attachments[i].attachment==attch)
221 attachments.push_back(Attachment(attch));
222 return attachments.size()-1;
226 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
232 void Framebuffer::Attachment::set(Renderbuffer &r)
234 type = GL_RENDERBUFFER_EXT;
239 void Framebuffer::Attachment::set(Texture &t, unsigned l)
241 type = t.get_target();
246 void Framebuffer::Attachment::clear()