3 #include "ext_framebuffer_blit.h"
4 #include "ext_framebuffer_object.h"
5 #include "framebuffer.h"
7 #include "renderbuffer.h"
15 Framebuffer::Framebuffer(unsigned i):
20 throw invalid_argument("System framebuffer must have id 0");
23 glGetIntegerv(GL_VIEWPORT, viewport);
28 Framebuffer::Framebuffer():
33 static RequireExtension _ext("GL_EXT_framebuffer_object");
35 glGenFramebuffersEXT(1, &id);
38 Framebuffer::~Framebuffer()
41 glDeleteFramebuffersEXT(1, &id);
46 void Framebuffer::update_attachment(unsigned mask) const
50 GLenum color_buf = GL_NONE;
51 for(unsigned i=0; i<attachments.size(); ++i)
53 const Attachment &attch = attachments[i];
56 if(attch.type==GL_RENDERBUFFER_EXT)
57 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, GL_RENDERBUFFER_EXT, attch.rbuf->get_id());
58 else if(attch.type==GL_TEXTURE_2D)
60 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
61 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
63 else if(attch.type==GL_TEXTURE_CUBE_MAP)
65 static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
66 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level);
69 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, 0, 0);
72 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
73 color_buf = attch.attachment;
76 glDrawBuffer(color_buf);
82 void Framebuffer::check_size()
84 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
87 if(i->type==GL_RENDERBUFFER_EXT)
89 width = i->rbuf->get_width();
90 height = i->rbuf->get_height();
92 else if(i->type==GL_TEXTURE_2D)
94 Texture2D *tex = static_cast<Texture2D *>(i->tex);
95 width = tex->get_width();
96 height = tex->get_height();
98 else if(i->type==GL_TEXTURE_CUBE_MAP)
100 width = static_cast<TextureCube *>(i->tex)->get_size();
104 glViewport(0, 0, width, height);
109 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
112 throw invalid_operation("Framebuffer::attach");
114 unsigned i = get_attachment_index(attch);
115 attachments[i].set(rbuf);
116 update_attachment(1<<i);
120 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
123 throw invalid_operation("Framebuffer::attach");
125 unsigned i = get_attachment_index(attch);
126 attachments[i].set(tex, 0, level);
127 update_attachment(1<<i);
131 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
134 throw invalid_operation("Framebuffer::attach");
136 unsigned i = get_attachment_index(attch);
137 attachments[i].set(tex, face, level);
138 update_attachment(1<<i);
142 void Framebuffer::detach(FramebufferAttachment attch)
145 throw invalid_operation("Framebuffer::detach");
147 unsigned i = get_attachment_index(attch);
148 attachments[i].clear();
149 update_attachment(1<<i);
153 FramebufferStatus Framebuffer::check_status() const
155 Bind _bind(this, true);
156 return static_cast<FramebufferStatus>(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
159 void Framebuffer::clear(BufferBits bits)
161 Bind _bind(this, true);
165 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)
167 static RequireExtension _ext("GL_EXT_framebuffer_blit");
169 const Framebuffer *old = current();
170 if(set_current(this))
172 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, id);
175 update_attachment(dirty);
180 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, other.id);
182 glBlitFramebufferEXT(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
185 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (old ? old->id : 0));
188 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
190 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
193 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
195 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
198 void Framebuffer::bind() const
200 if(set_current(this))
202 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
205 update_attachment(dirty);
209 glViewport(0, 0, width, height);
213 const Framebuffer *Framebuffer::current()
220 void Framebuffer::unbind()
225 Framebuffer &Framebuffer::system()
227 static Framebuffer sys_framebuf(0);
231 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
233 for(unsigned i=0; i<attachments.size(); ++i)
234 if(attachments[i].attachment==attch)
236 attachments.push_back(Attachment(attch));
237 return attachments.size()-1;
241 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
247 void Framebuffer::Attachment::set(Renderbuffer &r)
249 type = GL_RENDERBUFFER_EXT;
254 void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l)
256 type = t.get_target();
262 void Framebuffer::Attachment::clear()