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>
4 #include <msp/gl/extensions/msp_draw_buffer.h>
6 #include "framebuffer.h"
8 #include "renderbuffer.h"
16 void operator<<(LexicalConverter &conv, FramebufferStatus status)
20 case FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
21 conv.result("incomplete attachment");
23 case FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
24 conv.result("missing attachment");
26 case FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
27 conv.result("mismatched attachment dimensions");
29 case FRAMEBUFFER_INCOMPLETE_FORMATS:
30 conv.result("mismatched attachment formats");
32 case FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
33 conv.result("missing draw buffer attachment");
35 case FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
36 conv.result("missing read buffer attachment");
38 case FRAMEBUFFER_UNSUPPORTED:
39 conv.result("unsupported");
42 conv.result(lexical_cast<string, unsigned>(status, "%#x"));
47 framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status):
48 runtime_error(lexical_cast<string>(status))
52 Framebuffer::Framebuffer(unsigned i):
57 throw invalid_argument("System framebuffer must have id 0");
59 glGetIntegerv(GL_VIEWPORT, &view.left);
64 Framebuffer::Framebuffer():
69 static Require _req(EXT_framebuffer_object);
71 glGenFramebuffers(1, &id);
74 Framebuffer::~Framebuffer()
77 glDeleteFramebuffers(1, &id);
82 void Framebuffer::update_attachment(unsigned mask) const
86 std::vector<GLenum> color_bufs;
87 color_bufs.reserve(attachments.size());
88 for(unsigned i=0; i<attachments.size(); ++i)
90 const Attachment &attch = attachments[i];
93 if(attch.type==GL_RENDERBUFFER)
94 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
95 else if(attch.type==GL_TEXTURE_2D)
97 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
98 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
100 else if(attch.type==GL_TEXTURE_CUBE_MAP)
102 static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
103 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level);
106 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
109 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
110 color_bufs.push_back(attch.attachment);
113 if(color_bufs.size()>1)
114 static Require _req(ARB_draw_buffers);
116 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
118 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
119 else if(MSP_draw_buffer)
120 glDrawBuffer(first_buffer);
123 glReadBuffer(first_buffer);
129 void Framebuffer::check_size()
131 bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
132 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
135 if(i->type==GL_RENDERBUFFER)
137 width = i->rbuf->get_width();
138 height = i->rbuf->get_height();
140 else if(i->type==GL_TEXTURE_2D)
142 Texture2D *tex = static_cast<Texture2D *>(i->tex);
143 width = tex->get_width();
144 height = tex->get_height();
146 else if(i->type==GL_TEXTURE_CUBE_MAP)
148 width = static_cast<TextureCube *>(i->tex)->get_size();
157 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
159 for(unsigned i=0; i<attachments.size(); ++i)
160 if(attachments[i].attachment==attch)
162 attachments.push_back(Attachment(attch));
163 return attachments.size()-1;
166 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
169 throw invalid_operation("Framebuffer::attach");
171 unsigned i = get_attachment_index(attch);
172 attachments[i].set(rbuf);
173 update_attachment(1<<i);
177 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
180 throw invalid_operation("Framebuffer::attach");
182 unsigned i = get_attachment_index(attch);
183 attachments[i].set(tex, 0, level);
184 update_attachment(1<<i);
188 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
191 throw invalid_operation("Framebuffer::attach");
193 unsigned i = get_attachment_index(attch);
194 attachments[i].set(tex, face, level);
195 update_attachment(1<<i);
199 void Framebuffer::detach(FramebufferAttachment attch)
202 throw invalid_operation("Framebuffer::detach");
204 unsigned i = get_attachment_index(attch);
205 attachments[i].clear();
206 update_attachment(1<<i);
210 FramebufferStatus Framebuffer::check_status() const
212 BindRestore _bind(this);
213 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
216 void Framebuffer::require_complete() const
218 FramebufferStatus status = check_status();
219 if(status!=FRAMEBUFFER_COMPLETE)
220 throw framebuffer_incomplete(status);
223 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
231 glViewport(view.left, view.bottom, view.width, view.height);
234 void Framebuffer::reset_viewport()
236 viewport(0, 0, width, height);
239 void Framebuffer::clear(BufferBits bits)
241 BindRestore _bind(this);
245 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)
247 static Require _req(EXT_framebuffer_blit);
249 const Framebuffer *old = current();
250 if(set_current(this))
252 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
255 update_attachment(dirty);
260 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
262 glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
265 glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
268 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
270 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
273 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
275 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
278 void Framebuffer::bind() const
280 if(set_current(this))
282 glBindFramebuffer(GL_FRAMEBUFFER, id);
285 update_attachment(dirty);
290 glViewport(view.left, view.bottom, view.width, view.height);
294 const Framebuffer *Framebuffer::current()
301 void Framebuffer::unbind()
306 Framebuffer &Framebuffer::system()
308 static Framebuffer sys_framebuf(0);
313 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
319 void Framebuffer::Attachment::set(Renderbuffer &r)
321 type = GL_RENDERBUFFER;
326 void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l)
328 type = t.get_target();
334 void Framebuffer::Attachment::clear()
340 Framebuffer::Viewport::Viewport():