1 #include <msp/gl/extensions/arb_draw_buffers.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/ext_framebuffer_blit.h>
4 #include <msp/gl/extensions/ext_framebuffer_object.h>
5 #include <msp/gl/extensions/msp_draw_buffer.h>
7 #include "framebuffer.h"
9 #include "renderbuffer.h"
10 #include "texture2d.h"
17 void operator<<(LexicalConverter &conv, FramebufferStatus status)
21 case FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
22 conv.result("incomplete attachment");
24 case FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
25 conv.result("missing attachment");
27 case FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
28 conv.result("mismatched attachment dimensions");
30 case FRAMEBUFFER_INCOMPLETE_FORMATS:
31 conv.result("mismatched attachment formats");
33 case FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
34 conv.result("missing draw buffer attachment");
36 case FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
37 conv.result("missing read buffer attachment");
39 case FRAMEBUFFER_UNSUPPORTED:
40 conv.result("unsupported");
43 conv.result(lexical_cast<string, unsigned>(status, "%#x"));
48 framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status):
49 runtime_error(lexical_cast<string>(status))
53 Framebuffer::Framebuffer(unsigned i):
58 throw invalid_argument("System framebuffer must have id 0");
60 glGetIntegerv(GL_VIEWPORT, &view.left);
65 Framebuffer::Framebuffer():
70 static Require _req(EXT_framebuffer_object);
72 if(ARB_direct_state_access)
73 glCreateFramebuffers(1, &id);
75 glGenFramebuffers(1, &id);
78 Framebuffer::~Framebuffer()
81 glDeleteFramebuffers(1, &id);
86 void Framebuffer::update_attachment(unsigned mask) const
88 if(!ARB_direct_state_access && current()!=this)
94 std::vector<GLenum> color_bufs;
95 color_bufs.reserve(attachments.size());
96 for(unsigned i=0; i<attachments.size(); ++i)
98 const Attachment &attch = attachments[i];
101 if(attch.type==GL_RENDERBUFFER)
103 if(ARB_direct_state_access)
104 glNamedFramebufferRenderbuffer(id, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
106 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
108 else if(attch.type==GL_TEXTURE_2D)
110 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
111 if(ARB_direct_state_access)
112 glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level);
114 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
116 else if(attch.type==GL_TEXTURE_CUBE_MAP)
118 static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
119 if(ARB_direct_state_access)
120 glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
122 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
124 else if(ARB_direct_state_access)
125 glNamedFramebufferRenderbuffer(id, attch.attachment, 0, 0);
127 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
130 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
131 color_bufs.push_back(attch.attachment);
134 if(color_bufs.size()>1)
135 static Require _req(ARB_draw_buffers);
137 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
138 if(ARB_direct_state_access)
140 /* ARB_direct_state_access ties the availability of these functions to
141 framebuffers themselves, so no further checks are needed. */
142 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
143 glNamedFramebufferReadBuffer(id, first_buffer);
148 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
149 else if(MSP_draw_buffer)
150 glDrawBuffer(first_buffer);
153 glReadBuffer(first_buffer);
157 void Framebuffer::check_size()
159 bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
160 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
163 if(i->type==GL_RENDERBUFFER)
165 width = i->rbuf->get_width();
166 height = i->rbuf->get_height();
168 else if(i->type==GL_TEXTURE_2D)
170 Texture2D *tex = static_cast<Texture2D *>(i->tex);
171 width = tex->get_width();
172 height = tex->get_height();
174 else if(i->type==GL_TEXTURE_CUBE_MAP)
176 width = static_cast<TextureCube *>(i->tex)->get_size();
185 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
187 for(unsigned i=0; i<attachments.size(); ++i)
188 if(attachments[i].attachment==attch)
190 attachments.push_back(Attachment(attch));
191 return attachments.size()-1;
194 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
197 throw invalid_operation("Framebuffer::attach");
199 unsigned i = get_attachment_index(attch);
200 attachments[i].set(rbuf);
201 update_attachment(1<<i);
205 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
208 throw invalid_operation("Framebuffer::attach");
210 unsigned i = get_attachment_index(attch);
211 attachments[i].set(tex, level, 0);
212 update_attachment(1<<i);
216 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
219 throw invalid_operation("Framebuffer::attach");
221 unsigned i = get_attachment_index(attch);
222 attachments[i].set(tex, level, TextureCube::get_face_index(face));
223 update_attachment(1<<i);
227 void Framebuffer::detach(FramebufferAttachment attch)
230 throw invalid_operation("Framebuffer::detach");
232 unsigned i = get_attachment_index(attch);
233 attachments[i].clear();
234 update_attachment(1<<i);
238 FramebufferStatus Framebuffer::check_status() const
240 if(ARB_direct_state_access)
241 return static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
244 BindRestore _bind(this);
245 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
249 void Framebuffer::require_complete() const
251 FramebufferStatus status = check_status();
252 if(status!=FRAMEBUFFER_COMPLETE)
253 throw framebuffer_incomplete(status);
256 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
264 glViewport(view.left, view.bottom, view.width, view.height);
267 void Framebuffer::reset_viewport()
269 viewport(0, 0, width, height);
272 void Framebuffer::clear(BufferBits bits)
274 BindRestore _bind(this);
278 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)
280 static Require _req(EXT_framebuffer_blit);
282 if(ARB_direct_state_access)
284 glBlitNamedFramebuffer(other.id, id, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
288 const Framebuffer *old = current();
289 if(set_current(this))
291 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
294 update_attachment(dirty);
299 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
301 glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
304 glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
307 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
309 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
312 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
314 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
317 void Framebuffer::bind() const
319 if(set_current(this))
321 glBindFramebuffer(GL_FRAMEBUFFER, id);
324 update_attachment(dirty);
329 glViewport(view.left, view.bottom, view.width, view.height);
333 const Framebuffer *Framebuffer::current()
340 void Framebuffer::unbind()
345 Framebuffer &Framebuffer::system()
347 static Framebuffer sys_framebuf(0);
352 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
359 void Framebuffer::Attachment::set(Renderbuffer &r)
361 type = GL_RENDERBUFFER;
367 void Framebuffer::Attachment::set(Texture &t, unsigned l, unsigned z)
369 type = t.get_target();
375 void Framebuffer::Attachment::clear()
381 Framebuffer::Viewport::Viewport():