2 #include "ext_framebuffer_blit.h"
3 #include "ext_framebuffer_object.h"
4 #include "framebuffer.h"
6 #include "renderbuffer.h"
14 Framebuffer::Framebuffer(unsigned i):
19 throw InvalidParameterValue("System framebuffer must have id 0");
22 glGetIntegerv(GL_VIEWPORT, viewport);
27 Framebuffer::Framebuffer():
32 static RequireExtension _ext("GL_EXT_framebuffer_object");
34 glGenFramebuffersEXT(1, &id);
37 Framebuffer::~Framebuffer()
40 glDeleteFramebuffersEXT(1, &id);
45 void Framebuffer::update_attachment(unsigned mask) const
49 GLenum color_buf = GL_NONE;
50 for(unsigned i=0; i<attachments.size(); ++i)
52 const Attachment &attch = attachments[i];
55 if(attch.type==GL_RENDERBUFFER_EXT)
56 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, GL_RENDERBUFFER_EXT, attch.rbuf->get_id());
57 else if(attch.type==GL_TEXTURE_2D)
59 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
60 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
63 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, 0, 0);
66 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
67 color_buf = attch.attachment;
70 glDrawBuffer(color_buf);
76 void Framebuffer::check_size()
78 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
81 if(i->type==GL_RENDERBUFFER_EXT)
83 width = i->rbuf->get_width();
84 height = i->rbuf->get_height();
86 else if(i->type==GL_TEXTURE_2D)
88 Texture2D *tex = static_cast<Texture2D *>(i->tex);
89 width = tex->get_width();
90 height = tex->get_height();
93 glViewport(0, 0, width, height);
98 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
101 throw InvalidState("Can't attach to system framebuffer");
103 unsigned i = get_attachment_index(attch);
104 attachments[i].set(rbuf);
105 update_attachment(1<<i);
109 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
112 throw InvalidState("Can't attach to system framebuffer");
114 unsigned i = get_attachment_index(attch);
115 attachments[i].set(tex, level);
116 update_attachment(1<<i);
120 void Framebuffer::detach(FramebufferAttachment attch)
123 throw InvalidState("Can't detach from system framebuffer");
125 unsigned i = get_attachment_index(attch);
126 attachments[i].clear();
127 update_attachment(1<<i);
131 FramebufferStatus Framebuffer::check_status() const
133 Bind _bind(this, true);
134 return static_cast<FramebufferStatus>(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
137 void Framebuffer::clear(BufferBits bits)
139 Bind _bind(this, true);
143 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)
145 static RequireExtension _ext("GL_EXT_framebuffer_blit");
147 const Framebuffer *old = current();
148 if(set_current(this))
150 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, id);
153 update_attachment(dirty);
158 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, other.id);
160 glBlitFramebufferEXT(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
163 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (old ? old->id : 0));
166 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
168 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
171 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
173 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
176 void Framebuffer::bind() const
178 if(set_current(this))
180 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
183 update_attachment(dirty);
187 glViewport(0, 0, width, height);
191 const Framebuffer *Framebuffer::current()
198 void Framebuffer::unbind()
203 Framebuffer &Framebuffer::system()
205 static Framebuffer sys_framebuf(0);
209 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
211 for(unsigned i=0; i<attachments.size(); ++i)
212 if(attachments[i].attachment==attch)
214 attachments.push_back(Attachment(attch));
215 return attachments.size()-1;
219 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
225 void Framebuffer::Attachment::set(Renderbuffer &r)
227 type = GL_RENDERBUFFER_EXT;
232 void Framebuffer::Attachment::set(Texture &t, unsigned l)
234 type = t.get_target();
239 void Framebuffer::Attachment::clear()