1 #include "arb_draw_buffers.h"
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 Require _req(EXT_framebuffer_object);
35 glGenFramebuffers(1, &id);
38 Framebuffer::~Framebuffer()
41 glDeleteFramebuffers(1, &id);
46 void Framebuffer::update_attachment(unsigned mask) const
50 std::vector<GLenum> color_bufs;
51 color_bufs.reserve(attachments.size());
52 for(unsigned i=0; i<attachments.size(); ++i)
54 const Attachment &attch = attachments[i];
57 if(attch.type==GL_RENDERBUFFER)
58 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
59 else if(attch.type==GL_TEXTURE_2D)
61 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
62 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
64 else if(attch.type==GL_TEXTURE_CUBE_MAP)
66 static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
67 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level);
70 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
73 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
74 color_bufs.push_back(attch.attachment);
77 if(color_bufs.empty())
78 glDrawBuffer(GL_NONE);
79 else if(color_bufs.size()==1)
80 glDrawBuffer(color_bufs.front());
83 static Require _req(ARB_draw_buffers);
84 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
91 void Framebuffer::check_size()
93 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
96 if(i->type==GL_RENDERBUFFER)
98 width = i->rbuf->get_width();
99 height = i->rbuf->get_height();
101 else if(i->type==GL_TEXTURE_2D)
103 Texture2D *tex = static_cast<Texture2D *>(i->tex);
104 width = tex->get_width();
105 height = tex->get_height();
107 else if(i->type==GL_TEXTURE_CUBE_MAP)
109 width = static_cast<TextureCube *>(i->tex)->get_size();
113 glViewport(0, 0, width, height);
118 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
120 for(unsigned i=0; i<attachments.size(); ++i)
121 if(attachments[i].attachment==attch)
123 attachments.push_back(Attachment(attch));
124 return attachments.size()-1;
127 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
130 throw invalid_operation("Framebuffer::attach");
132 unsigned i = get_attachment_index(attch);
133 attachments[i].set(rbuf);
134 update_attachment(1<<i);
138 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
141 throw invalid_operation("Framebuffer::attach");
143 unsigned i = get_attachment_index(attch);
144 attachments[i].set(tex, 0, level);
145 update_attachment(1<<i);
149 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
152 throw invalid_operation("Framebuffer::attach");
154 unsigned i = get_attachment_index(attch);
155 attachments[i].set(tex, face, level);
156 update_attachment(1<<i);
160 void Framebuffer::detach(FramebufferAttachment attch)
163 throw invalid_operation("Framebuffer::detach");
165 unsigned i = get_attachment_index(attch);
166 attachments[i].clear();
167 update_attachment(1<<i);
171 FramebufferStatus Framebuffer::check_status() const
173 Bind _bind(this, true);
174 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
177 void Framebuffer::clear(BufferBits bits)
179 Bind _bind(this, true);
183 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)
185 static Require _req(EXT_framebuffer_blit);
187 const Framebuffer *old = current();
188 if(set_current(this))
190 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
193 update_attachment(dirty);
198 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
200 glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
203 glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
206 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
208 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
211 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
213 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
216 void Framebuffer::bind() const
218 if(set_current(this))
220 glBindFramebuffer(GL_FRAMEBUFFER, id);
223 update_attachment(dirty);
227 glViewport(0, 0, width, height);
231 const Framebuffer *Framebuffer::current()
238 void Framebuffer::unbind()
243 Framebuffer &Framebuffer::system()
245 static Framebuffer sys_framebuf(0);
250 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
256 void Framebuffer::Attachment::set(Renderbuffer &r)
258 type = GL_RENDERBUFFER;
263 void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l)
265 type = t.get_target();
271 void Framebuffer::Attachment::clear()