1 #include <msp/gl/extensions/arb_draw_buffers.h>
2 #include <msp/gl/extensions/ext_framebuffer_blit.h>
3 #include <msp/gl/extensions/ext_framebuffer_object.h>
5 #include "framebuffer.h"
7 #include "renderbuffer.h"
15 void operator<<(LexicalConverter &conv, FramebufferStatus status)
19 case FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
20 conv.result("incomplete attachment");
22 case FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
23 conv.result("missing attachment");
25 case FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
26 conv.result("mismatched attachment dimensions");
28 case FRAMEBUFFER_INCOMPLETE_FORMATS:
29 conv.result("mismatched attachment formats");
31 case FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
32 conv.result("missing draw buffer attachment");
34 case FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
35 conv.result("missing read buffer attachment");
37 case FRAMEBUFFER_UNSUPPORTED:
38 conv.result("unsupported");
41 conv.result(lexical_cast<string, unsigned>(status, "%#x"));
46 framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status):
47 runtime_error(lexical_cast<string>(status))
51 Framebuffer::Framebuffer(unsigned i):
56 throw invalid_argument("System framebuffer must have id 0");
58 glGetIntegerv(GL_VIEWPORT, &view.left);
63 Framebuffer::Framebuffer():
68 static Require _req(EXT_framebuffer_object);
70 glGenFramebuffers(1, &id);
73 Framebuffer::~Framebuffer()
76 glDeleteFramebuffers(1, &id);
81 void Framebuffer::update_attachment(unsigned mask) const
85 std::vector<GLenum> color_bufs;
86 color_bufs.reserve(attachments.size());
87 for(unsigned i=0; i<attachments.size(); ++i)
89 const Attachment &attch = attachments[i];
92 if(attch.type==GL_RENDERBUFFER)
93 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
94 else if(attch.type==GL_TEXTURE_2D)
96 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
97 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
99 else if(attch.type==GL_TEXTURE_CUBE_MAP)
101 static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
102 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level);
105 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
108 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
109 color_bufs.push_back(attch.attachment);
112 if(color_bufs.empty())
114 glDrawBuffer(GL_NONE);
115 glReadBuffer(GL_NONE);
117 else if(color_bufs.size()==1)
119 glDrawBuffer(color_bufs.front());
120 glReadBuffer(color_bufs.front());
124 static Require _req(ARB_draw_buffers);
125 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
126 glReadBuffer(color_bufs.front());
133 void Framebuffer::check_size()
135 bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
136 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
139 if(i->type==GL_RENDERBUFFER)
141 width = i->rbuf->get_width();
142 height = i->rbuf->get_height();
144 else if(i->type==GL_TEXTURE_2D)
146 Texture2D *tex = static_cast<Texture2D *>(i->tex);
147 width = tex->get_width();
148 height = tex->get_height();
150 else if(i->type==GL_TEXTURE_CUBE_MAP)
152 width = static_cast<TextureCube *>(i->tex)->get_size();
161 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
163 for(unsigned i=0; i<attachments.size(); ++i)
164 if(attachments[i].attachment==attch)
166 attachments.push_back(Attachment(attch));
167 return attachments.size()-1;
170 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
173 throw invalid_operation("Framebuffer::attach");
175 unsigned i = get_attachment_index(attch);
176 attachments[i].set(rbuf);
177 update_attachment(1<<i);
181 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
184 throw invalid_operation("Framebuffer::attach");
186 unsigned i = get_attachment_index(attch);
187 attachments[i].set(tex, 0, level);
188 update_attachment(1<<i);
192 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
195 throw invalid_operation("Framebuffer::attach");
197 unsigned i = get_attachment_index(attch);
198 attachments[i].set(tex, face, level);
199 update_attachment(1<<i);
203 void Framebuffer::detach(FramebufferAttachment attch)
206 throw invalid_operation("Framebuffer::detach");
208 unsigned i = get_attachment_index(attch);
209 attachments[i].clear();
210 update_attachment(1<<i);
214 FramebufferStatus Framebuffer::check_status() const
216 BindRestore _bind(this);
217 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
220 void Framebuffer::require_complete() const
222 FramebufferStatus status = check_status();
223 if(status!=FRAMEBUFFER_COMPLETE)
224 throw framebuffer_incomplete(status);
227 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
235 glViewport(view.left, view.bottom, view.width, view.height);
238 void Framebuffer::reset_viewport()
240 viewport(0, 0, width, height);
243 void Framebuffer::clear(BufferBits bits)
245 BindRestore _bind(this);
249 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)
251 static Require _req(EXT_framebuffer_blit);
253 const Framebuffer *old = current();
254 if(set_current(this))
256 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
259 update_attachment(dirty);
264 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
266 glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
269 glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
272 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
274 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
277 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
279 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
282 void Framebuffer::bind() const
284 if(set_current(this))
286 glBindFramebuffer(GL_FRAMEBUFFER, id);
289 update_attachment(dirty);
294 glViewport(view.left, view.bottom, view.width, view.height);
298 const Framebuffer *Framebuffer::current()
305 void Framebuffer::unbind()
310 Framebuffer &Framebuffer::system()
312 static Framebuffer sys_framebuf(0);
317 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
323 void Framebuffer::Attachment::set(Renderbuffer &r)
325 type = GL_RENDERBUFFER;
330 void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l)
332 type = t.get_target();
338 void Framebuffer::Attachment::clear()
344 Framebuffer::Viewport::Viewport():