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
90 std::vector<GLenum> color_bufs;
91 color_bufs.reserve(attachments.size());
92 for(unsigned i=0; i<attachments.size(); ++i)
94 const Attachment &attch = attachments[i];
97 if(attch.type==GL_RENDERBUFFER)
98 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
99 else if(attch.type==GL_TEXTURE_2D)
101 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
102 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
104 else if(attch.type==GL_TEXTURE_CUBE_MAP)
106 static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
107 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level);
110 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
113 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
114 color_bufs.push_back(attch.attachment);
117 if(color_bufs.size()>1)
118 static Require _req(ARB_draw_buffers);
120 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
122 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
123 else if(MSP_draw_buffer)
124 glDrawBuffer(first_buffer);
127 glReadBuffer(first_buffer);
130 void Framebuffer::check_size()
132 bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
133 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
136 if(i->type==GL_RENDERBUFFER)
138 width = i->rbuf->get_width();
139 height = i->rbuf->get_height();
141 else if(i->type==GL_TEXTURE_2D)
143 Texture2D *tex = static_cast<Texture2D *>(i->tex);
144 width = tex->get_width();
145 height = tex->get_height();
147 else if(i->type==GL_TEXTURE_CUBE_MAP)
149 width = static_cast<TextureCube *>(i->tex)->get_size();
158 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
160 for(unsigned i=0; i<attachments.size(); ++i)
161 if(attachments[i].attachment==attch)
163 attachments.push_back(Attachment(attch));
164 return attachments.size()-1;
167 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
170 throw invalid_operation("Framebuffer::attach");
172 unsigned i = get_attachment_index(attch);
173 attachments[i].set(rbuf);
174 update_attachment(1<<i);
178 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
181 throw invalid_operation("Framebuffer::attach");
183 unsigned i = get_attachment_index(attch);
184 attachments[i].set(tex, 0, level);
185 update_attachment(1<<i);
189 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
192 throw invalid_operation("Framebuffer::attach");
194 unsigned i = get_attachment_index(attch);
195 attachments[i].set(tex, face, level);
196 update_attachment(1<<i);
200 void Framebuffer::detach(FramebufferAttachment attch)
203 throw invalid_operation("Framebuffer::detach");
205 unsigned i = get_attachment_index(attch);
206 attachments[i].clear();
207 update_attachment(1<<i);
211 FramebufferStatus Framebuffer::check_status() const
213 BindRestore _bind(this);
214 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
217 void Framebuffer::require_complete() const
219 FramebufferStatus status = check_status();
220 if(status!=FRAMEBUFFER_COMPLETE)
221 throw framebuffer_incomplete(status);
224 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
232 glViewport(view.left, view.bottom, view.width, view.height);
235 void Framebuffer::reset_viewport()
237 viewport(0, 0, width, height);
240 void Framebuffer::clear(BufferBits bits)
242 BindRestore _bind(this);
246 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)
248 static Require _req(EXT_framebuffer_blit);
250 const Framebuffer *old = current();
251 if(set_current(this))
253 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
256 update_attachment(dirty);
261 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
263 glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
266 glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
269 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
271 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
274 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
276 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
279 void Framebuffer::bind() const
281 if(set_current(this))
283 glBindFramebuffer(GL_FRAMEBUFFER, id);
286 update_attachment(dirty);
291 glViewport(view.left, view.bottom, view.width, view.height);
295 const Framebuffer *Framebuffer::current()
302 void Framebuffer::unbind()
307 Framebuffer &Framebuffer::system()
309 static Framebuffer sys_framebuf(0);
314 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
320 void Framebuffer::Attachment::set(Renderbuffer &r)
322 type = GL_RENDERBUFFER;
327 void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l)
329 type = t.get_target();
335 void Framebuffer::Attachment::clear()
341 Framebuffer::Viewport::Viewport():