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>
8 #include <msp/gl/extensions/khr_debug.h>
10 #include "framebuffer.h"
12 #include "renderbuffer.h"
13 #include "texture2d.h"
14 #include "texture3d.h"
21 void operator<<(LexicalConverter &conv, FramebufferStatus status)
25 case FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
26 conv.result("incomplete attachment");
28 case FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
29 conv.result("missing attachment");
31 case FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
32 conv.result("mismatched attachment dimensions");
34 case FRAMEBUFFER_INCOMPLETE_FORMATS:
35 conv.result("mismatched attachment formats");
37 case FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
38 conv.result("missing draw buffer attachment");
40 case FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
41 conv.result("missing read buffer attachment");
43 case FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
44 conv.result("mismatched attachment sample counts");
46 case FRAMEBUFFER_INCOMPLETE_LAYER_COUNT:
47 conv.result("mismatched attachment layer counts");
49 case FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
50 conv.result("mismatched attachment layering");
52 case FRAMEBUFFER_UNSUPPORTED:
53 conv.result("unsupported");
56 conv.result(lexical_cast<string, unsigned>(status, "%#x"));
61 framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status):
62 runtime_error(lexical_cast<string>(status))
66 Framebuffer::Framebuffer(unsigned i):
71 throw invalid_argument("System framebuffer must have id 0");
73 glGetIntegerv(GL_VIEWPORT, &view.left);
78 Framebuffer::Framebuffer():
83 static Require _req(EXT_framebuffer_object);
85 if(ARB_direct_state_access)
86 glCreateFramebuffers(1, &id);
88 glGenFramebuffers(1, &id);
91 Framebuffer::~Framebuffer()
94 glDeleteFramebuffers(1, &id);
99 void Framebuffer::update_attachment(unsigned mask) const
101 if(!ARB_direct_state_access && current()!=this)
107 vector<GLenum> color_bufs;
108 color_bufs.reserve(attachments.size());
109 for(unsigned i=0; i<attachments.size(); ++i)
111 const Attachment &attch = attachments[i];
114 if(attch.type==GL_RENDERBUFFER)
116 if(ARB_direct_state_access)
117 glNamedFramebufferRenderbuffer(id, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
119 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
123 if(ARB_direct_state_access)
125 if(attch.type==GL_TEXTURE_2D || attch.layer<0)
126 glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level);
128 glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
130 else if(attch.type==GL_TEXTURE_2D)
131 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
132 else if(attch.layer<0)
133 glFramebufferTexture(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level);
134 else if(attch.type==GL_TEXTURE_2D_ARRAY)
135 glFramebufferTextureLayer(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
136 else if(attch.type==GL_TEXTURE_3D)
137 glFramebufferTexture3D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level, attch.layer);
138 else if(attch.type==GL_TEXTURE_CUBE_MAP)
139 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
141 else if(ARB_direct_state_access)
142 glNamedFramebufferRenderbuffer(id, attch.attachment, 0, 0);
144 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
147 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
148 color_bufs.push_back(attch.attachment);
151 if(color_bufs.size()>1)
152 static Require _req(ARB_draw_buffers);
154 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
155 if(ARB_direct_state_access)
157 /* ARB_direct_state_access ties the availability of these functions to
158 framebuffers themselves, so no further checks are needed. */
159 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
160 glNamedFramebufferReadBuffer(id, first_buffer);
165 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
166 else if(MSP_buffer_control)
167 glDrawBuffer(first_buffer);
169 if(MSP_buffer_control)
170 glReadBuffer(first_buffer);
174 void Framebuffer::check_size()
176 bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
177 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
180 if(i->type==GL_RENDERBUFFER)
182 width = i->rbuf->get_width();
183 height = i->rbuf->get_height();
185 else if(i->type==GL_TEXTURE_2D)
187 Texture2D *tex = static_cast<Texture2D *>(i->tex);
188 width = max(tex->get_width()>>i->level, 1U);
189 height = max(tex->get_height()>>i->level, 1U);
191 else if(i->type==GL_TEXTURE_3D || i->type==GL_TEXTURE_2D_ARRAY)
193 Texture3D *tex = static_cast<Texture3D *>(i->tex);
194 width = max(tex->get_width()>>i->level, 1U);
195 height = max(tex->get_height()>>i->level, 1U);
197 else if(i->type==GL_TEXTURE_CUBE_MAP)
199 width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
208 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
210 for(unsigned i=0; i<attachments.size(); ++i)
211 if(attachments[i].attachment==attch)
213 attachments.push_back(Attachment(attch));
214 return attachments.size()-1;
217 void Framebuffer::set_texture_attachment(FramebufferAttachment attch, Texture &tex, unsigned level, int layer)
220 throw invalid_operation("Framebuffer::attach");
222 unsigned i = get_attachment_index(attch);
223 attachments[i].set(tex, level, layer);
224 update_attachment(1<<i);
228 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
231 throw invalid_operation("Framebuffer::attach");
233 unsigned i = get_attachment_index(attch);
234 attachments[i].set(rbuf);
235 update_attachment(1<<i);
239 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
242 set_texture_attachment(attch, tex, level, 0);
245 void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
248 set_texture_attachment(attch, tex, level, layer);
251 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
254 set_texture_attachment(attch, tex, level, TextureCube::get_face_index(face));
257 void Framebuffer::attach_layered(FramebufferAttachment attch, Texture3D &tex, unsigned level)
259 static Require _req(ARB_geometry_shader4);
261 set_texture_attachment(attch, tex, level, -1);
264 void Framebuffer::attach_layered(FramebufferAttachment attch, TextureCube &tex, unsigned level)
266 static Require _req(ARB_geometry_shader4);
268 set_texture_attachment(attch, tex, level, -1);
271 void Framebuffer::detach(FramebufferAttachment attch)
274 throw invalid_operation("Framebuffer::detach");
276 unsigned i = get_attachment_index(attch);
277 attachments[i].clear();
278 update_attachment(1<<i);
282 FramebufferStatus Framebuffer::check_status() const
284 if(ARB_direct_state_access)
285 return static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
288 BindRestore _bind(this);
289 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
293 void Framebuffer::require_complete() const
295 FramebufferStatus status = check_status();
296 if(status!=FRAMEBUFFER_COMPLETE)
297 throw framebuffer_incomplete(status);
300 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
308 glViewport(view.left, view.bottom, view.width, view.height);
311 void Framebuffer::reset_viewport()
313 viewport(0, 0, width, height);
316 void Framebuffer::clear()
318 clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT|STENCIL_BUFFER_BIT);
321 void Framebuffer::clear(BufferBits bits)
323 BindRestore _bind(this);
327 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)
329 static Require _req(EXT_framebuffer_blit);
331 if(ARB_direct_state_access)
333 glBlitNamedFramebuffer(other.id, id, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
337 const Framebuffer *old = current();
338 if(set_current(this))
340 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
343 update_attachment(dirty);
348 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
350 glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
353 glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
356 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
358 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
361 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
363 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
366 void Framebuffer::bind() const
368 if(id && attachments.empty())
369 throw invalid_operation("Framebuffer::bind");
371 if(set_current(this))
373 glBindFramebuffer(GL_FRAMEBUFFER, id);
376 update_attachment(dirty);
381 glViewport(view.left, view.bottom, view.width, view.height);
385 const Framebuffer *Framebuffer::current()
392 void Framebuffer::unbind()
397 void Framebuffer::set_debug_name(const string &name)
401 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
407 Framebuffer &Framebuffer::system()
409 static Framebuffer sys_framebuf(0);
414 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
421 void Framebuffer::Attachment::set(Renderbuffer &r)
423 type = GL_RENDERBUFFER;
429 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
431 type = t.get_target();
437 void Framebuffer::Attachment::clear()
443 Framebuffer::Viewport::Viewport():