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)
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.type==GL_TEXTURE_2D_ARRAY)
132 glFramebufferTextureLayer(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
133 else if(attch.type==GL_TEXTURE_3D)
134 glFramebufferTexture3D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level, attch.layer);
135 else if(attch.type==GL_TEXTURE_CUBE_MAP)
136 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
138 else if(ARB_direct_state_access)
139 glNamedFramebufferRenderbuffer(id, attch.attachment, 0, 0);
141 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
144 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
145 color_bufs.push_back(attch.attachment);
148 if(color_bufs.size()>1)
149 static Require _req(ARB_draw_buffers);
151 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
152 if(ARB_direct_state_access)
154 /* ARB_direct_state_access ties the availability of these functions to
155 framebuffers themselves, so no further checks are needed. */
156 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
157 glNamedFramebufferReadBuffer(id, first_buffer);
162 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
163 else if(MSP_buffer_control)
164 glDrawBuffer(first_buffer);
166 if(MSP_buffer_control)
167 glReadBuffer(first_buffer);
171 void Framebuffer::check_size()
173 bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
174 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
177 if(i->type==GL_RENDERBUFFER)
179 width = i->rbuf->get_width();
180 height = i->rbuf->get_height();
182 else if(i->type==GL_TEXTURE_2D)
184 Texture2D *tex = static_cast<Texture2D *>(i->tex);
185 width = max(tex->get_width()>>i->level, 1U);
186 height = max(tex->get_height()>>i->level, 1U);
188 else if(i->type==GL_TEXTURE_3D || i->type==GL_TEXTURE_2D_ARRAY)
190 Texture3D *tex = static_cast<Texture3D *>(i->tex);
191 width = max(tex->get_width()>>i->level, 1U);
192 height = max(tex->get_height()>>i->level, 1U);
194 else if(i->type==GL_TEXTURE_CUBE_MAP)
196 width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
205 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
207 for(unsigned i=0; i<attachments.size(); ++i)
208 if(attachments[i].attachment==attch)
210 attachments.push_back(Attachment(attch));
211 return attachments.size()-1;
214 void Framebuffer::set_texture_attachment(FramebufferAttachment attch, Texture &tex, unsigned level, int layer)
217 throw invalid_operation("Framebuffer::attach");
219 unsigned i = get_attachment_index(attch);
220 attachments[i].set(tex, level, layer);
221 update_attachment(1<<i);
225 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
228 throw invalid_operation("Framebuffer::attach");
230 unsigned i = get_attachment_index(attch);
231 attachments[i].set(rbuf);
232 update_attachment(1<<i);
236 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
239 set_texture_attachment(attch, tex, level, 0);
242 void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
245 set_texture_attachment(attch, tex, level, layer);
248 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
251 set_texture_attachment(attch, tex, level, TextureCube::get_face_index(face));
254 void Framebuffer::detach(FramebufferAttachment attch)
257 throw invalid_operation("Framebuffer::detach");
259 unsigned i = get_attachment_index(attch);
260 attachments[i].clear();
261 update_attachment(1<<i);
265 FramebufferStatus Framebuffer::check_status() const
267 if(ARB_direct_state_access)
268 return static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
271 BindRestore _bind(this);
272 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
276 void Framebuffer::require_complete() const
278 FramebufferStatus status = check_status();
279 if(status!=FRAMEBUFFER_COMPLETE)
280 throw framebuffer_incomplete(status);
283 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
291 glViewport(view.left, view.bottom, view.width, view.height);
294 void Framebuffer::reset_viewport()
296 viewport(0, 0, width, height);
299 void Framebuffer::clear()
301 clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT|STENCIL_BUFFER_BIT);
304 void Framebuffer::clear(BufferBits bits)
306 BindRestore _bind(this);
310 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)
312 static Require _req(EXT_framebuffer_blit);
314 if(ARB_direct_state_access)
316 glBlitNamedFramebuffer(other.id, id, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
320 const Framebuffer *old = current();
321 if(set_current(this))
323 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
326 update_attachment(dirty);
331 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
333 glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
336 glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
339 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
341 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
344 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
346 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
349 void Framebuffer::bind() const
351 if(id && attachments.empty())
352 throw invalid_operation("Framebuffer::bind");
354 if(set_current(this))
356 glBindFramebuffer(GL_FRAMEBUFFER, id);
359 update_attachment(dirty);
364 glViewport(view.left, view.bottom, view.width, view.height);
368 const Framebuffer *Framebuffer::current()
375 void Framebuffer::unbind()
380 Framebuffer &Framebuffer::system()
382 static Framebuffer sys_framebuf(0);
387 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
394 void Framebuffer::Attachment::set(Renderbuffer &r)
396 type = GL_RENDERBUFFER;
402 void Framebuffer::Attachment::set(Texture &t, unsigned l, unsigned z)
404 type = t.get_target();
410 void Framebuffer::Attachment::clear()
416 Framebuffer::Viewport::Viewport():