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/ext_texture_array.h>
6 #include <msp/gl/extensions/ext_texture3d.h>
7 #include <msp/gl/extensions/msp_buffer_control.h>
9 #include "framebuffer.h"
11 #include "renderbuffer.h"
12 #include "texture2d.h"
13 #include "texture3d.h"
20 void operator<<(LexicalConverter &conv, FramebufferStatus status)
24 case FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
25 conv.result("incomplete attachment");
27 case FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
28 conv.result("missing attachment");
30 case FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
31 conv.result("mismatched attachment dimensions");
33 case FRAMEBUFFER_INCOMPLETE_FORMATS:
34 conv.result("mismatched attachment formats");
36 case FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
37 conv.result("missing draw buffer attachment");
39 case FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
40 conv.result("missing read buffer attachment");
42 case FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
43 conv.result("mismatched attachment sample counts");
45 case FRAMEBUFFER_INCOMPLETE_LAYER_COUNT:
46 conv.result("mismatched attachment layer counts");
48 case FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
49 conv.result("mismatched attachment layering");
51 case FRAMEBUFFER_UNSUPPORTED:
52 conv.result("unsupported");
55 conv.result(lexical_cast<string, unsigned>(status, "%#x"));
60 framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status):
61 runtime_error(lexical_cast<string>(status))
65 Framebuffer::Framebuffer(unsigned i):
70 throw invalid_argument("System framebuffer must have id 0");
72 glGetIntegerv(GL_VIEWPORT, &view.left);
77 Framebuffer::Framebuffer():
82 static Require _req(EXT_framebuffer_object);
84 if(ARB_direct_state_access)
85 glCreateFramebuffers(1, &id);
87 glGenFramebuffers(1, &id);
90 Framebuffer::~Framebuffer()
93 glDeleteFramebuffers(1, &id);
98 void Framebuffer::update_attachment(unsigned mask) const
100 if(!ARB_direct_state_access && current()!=this)
106 vector<GLenum> color_bufs;
107 color_bufs.reserve(attachments.size());
108 for(unsigned i=0; i<attachments.size(); ++i)
110 const Attachment &attch = attachments[i];
113 if(attch.type==GL_RENDERBUFFER)
115 if(ARB_direct_state_access)
116 glNamedFramebufferRenderbuffer(id, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
118 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
122 if(ARB_direct_state_access)
124 if(attch.type==GL_TEXTURE_2D || attch.layer<0)
125 glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level);
127 glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
129 else if(attch.type==GL_TEXTURE_2D)
130 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
131 else if(attch.layer<0)
132 glFramebufferTexture(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level);
133 else if(attch.type==GL_TEXTURE_2D_ARRAY)
134 glFramebufferTextureLayer(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
135 else if(attch.type==GL_TEXTURE_3D)
136 glFramebufferTexture3D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level, attch.layer);
137 else if(attch.type==GL_TEXTURE_CUBE_MAP)
138 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
140 else if(ARB_direct_state_access)
141 glNamedFramebufferRenderbuffer(id, attch.attachment, 0, 0);
143 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
146 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
147 color_bufs.push_back(attch.attachment);
150 if(color_bufs.size()>1)
151 static Require _req(ARB_draw_buffers);
153 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
154 if(ARB_direct_state_access)
156 /* ARB_direct_state_access ties the availability of these functions to
157 framebuffers themselves, so no further checks are needed. */
158 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
159 glNamedFramebufferReadBuffer(id, first_buffer);
164 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
165 else if(MSP_buffer_control)
166 glDrawBuffer(first_buffer);
168 if(MSP_buffer_control)
169 glReadBuffer(first_buffer);
173 void Framebuffer::check_size()
175 bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
176 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
179 if(i->type==GL_RENDERBUFFER)
181 width = i->rbuf->get_width();
182 height = i->rbuf->get_height();
184 else if(i->type==GL_TEXTURE_2D)
186 Texture2D *tex = static_cast<Texture2D *>(i->tex);
187 width = max(tex->get_width()>>i->level, 1U);
188 height = max(tex->get_height()>>i->level, 1U);
190 else if(i->type==GL_TEXTURE_3D || i->type==GL_TEXTURE_2D_ARRAY)
192 Texture3D *tex = static_cast<Texture3D *>(i->tex);
193 width = max(tex->get_width()>>i->level, 1U);
194 height = max(tex->get_height()>>i->level, 1U);
196 else if(i->type==GL_TEXTURE_CUBE_MAP)
198 width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
207 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
209 for(unsigned i=0; i<attachments.size(); ++i)
210 if(attachments[i].attachment==attch)
212 attachments.push_back(Attachment(attch));
213 return attachments.size()-1;
216 void Framebuffer::set_texture_attachment(FramebufferAttachment attch, Texture &tex, unsigned level, int layer)
219 throw invalid_operation("Framebuffer::attach");
221 unsigned i = get_attachment_index(attch);
222 attachments[i].set(tex, level, layer);
223 update_attachment(1<<i);
227 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
230 throw invalid_operation("Framebuffer::attach");
232 unsigned i = get_attachment_index(attch);
233 attachments[i].set(rbuf);
234 update_attachment(1<<i);
238 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
241 set_texture_attachment(attch, tex, level, 0);
244 void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
247 set_texture_attachment(attch, tex, level, layer);
250 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
253 set_texture_attachment(attch, tex, level, TextureCube::get_face_index(face));
256 void Framebuffer::attach_layered(FramebufferAttachment attch, Texture3D &tex, unsigned level)
258 static Require _req(ARB_geometry_shader4);
260 set_texture_attachment(attch, tex, level, -1);
263 void Framebuffer::attach_layered(FramebufferAttachment attch, TextureCube &tex, unsigned level)
265 static Require _req(ARB_geometry_shader4);
267 set_texture_attachment(attch, tex, level, -1);
270 void Framebuffer::detach(FramebufferAttachment attch)
273 throw invalid_operation("Framebuffer::detach");
275 unsigned i = get_attachment_index(attch);
276 attachments[i].clear();
277 update_attachment(1<<i);
281 FramebufferStatus Framebuffer::check_status() const
283 if(ARB_direct_state_access)
284 return static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
287 BindRestore _bind(this);
288 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
292 void Framebuffer::require_complete() const
294 FramebufferStatus status = check_status();
295 if(status!=FRAMEBUFFER_COMPLETE)
296 throw framebuffer_incomplete(status);
299 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
307 glViewport(view.left, view.bottom, view.width, view.height);
310 void Framebuffer::reset_viewport()
312 viewport(0, 0, width, height);
315 void Framebuffer::clear()
317 clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT|STENCIL_BUFFER_BIT);
320 void Framebuffer::clear(BufferBits bits)
322 BindRestore _bind(this);
326 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)
328 static Require _req(EXT_framebuffer_blit);
330 if(ARB_direct_state_access)
332 glBlitNamedFramebuffer(other.id, id, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
336 const Framebuffer *old = current();
337 if(set_current(this))
339 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
342 update_attachment(dirty);
347 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
349 glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
352 glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
355 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
357 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
360 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
362 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
365 void Framebuffer::bind() const
367 if(id && attachments.empty())
368 throw invalid_operation("Framebuffer::bind");
370 if(set_current(this))
372 glBindFramebuffer(GL_FRAMEBUFFER, id);
375 update_attachment(dirty);
380 glViewport(view.left, view.bottom, view.width, view.height);
384 const Framebuffer *Framebuffer::current()
391 void Framebuffer::unbind()
396 Framebuffer &Framebuffer::system()
398 static Framebuffer sys_framebuf(0);
403 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
410 void Framebuffer::Attachment::set(Renderbuffer &r)
412 type = GL_RENDERBUFFER;
418 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
420 type = t.get_target();
426 void Framebuffer::Attachment::clear()
432 Framebuffer::Viewport::Viewport():