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 std::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());
120 else if(attch.type==GL_TEXTURE_2D)
122 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
123 if(ARB_direct_state_access)
124 glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level);
126 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
128 else if(attch.type==GL_TEXTURE_3D || attch.type==GL_TEXTURE_2D_ARRAY)
130 static_cast<Texture3D *>(attch.tex)->allocate(attch.level);
131 if(ARB_direct_state_access)
132 glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
133 else if(attch.type==GL_TEXTURE_2D_ARRAY)
134 glFramebufferTextureLayer(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
136 glFramebufferTexture3D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level, attch.layer);
138 else if(attch.type==GL_TEXTURE_CUBE_MAP)
140 static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
141 if(ARB_direct_state_access)
142 glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
144 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
146 else if(ARB_direct_state_access)
147 glNamedFramebufferRenderbuffer(id, attch.attachment, 0, 0);
149 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
152 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
153 color_bufs.push_back(attch.attachment);
156 if(color_bufs.size()>1)
157 static Require _req(ARB_draw_buffers);
159 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
160 if(ARB_direct_state_access)
162 /* ARB_direct_state_access ties the availability of these functions to
163 framebuffers themselves, so no further checks are needed. */
164 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
165 glNamedFramebufferReadBuffer(id, first_buffer);
170 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
171 else if(MSP_buffer_control)
172 glDrawBuffer(first_buffer);
174 if(MSP_buffer_control)
175 glReadBuffer(first_buffer);
179 void Framebuffer::check_size()
181 bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
182 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
185 if(i->type==GL_RENDERBUFFER)
187 width = i->rbuf->get_width();
188 height = i->rbuf->get_height();
190 else if(i->type==GL_TEXTURE_2D)
192 Texture2D *tex = static_cast<Texture2D *>(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_3D || i->type==GL_TEXTURE_2D_ARRAY)
198 Texture3D *tex = static_cast<Texture3D *>(i->tex);
199 width = max(tex->get_width()>>i->level, 1U);
200 height = max(tex->get_height()>>i->level, 1U);
202 else if(i->type==GL_TEXTURE_CUBE_MAP)
204 width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
213 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
215 for(unsigned i=0; i<attachments.size(); ++i)
216 if(attachments[i].attachment==attch)
218 attachments.push_back(Attachment(attch));
219 return attachments.size()-1;
222 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
225 throw invalid_operation("Framebuffer::attach");
227 unsigned i = get_attachment_index(attch);
228 attachments[i].set(rbuf);
229 update_attachment(1<<i);
233 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
236 throw invalid_operation("Framebuffer::attach");
238 unsigned i = get_attachment_index(attch);
239 attachments[i].set(tex, level, 0);
240 update_attachment(1<<i);
244 void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
247 throw invalid_operation("Framebuffer::attach");
249 unsigned i = get_attachment_index(attch);
250 attachments[i].set(tex, level, layer);
251 update_attachment(1<<i);
255 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
258 throw invalid_operation("Framebuffer::attach");
260 unsigned i = get_attachment_index(attch);
261 attachments[i].set(tex, level, TextureCube::get_face_index(face));
262 update_attachment(1<<i);
266 void Framebuffer::detach(FramebufferAttachment attch)
269 throw invalid_operation("Framebuffer::detach");
271 unsigned i = get_attachment_index(attch);
272 attachments[i].clear();
273 update_attachment(1<<i);
277 FramebufferStatus Framebuffer::check_status() const
279 if(ARB_direct_state_access)
280 return static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
283 BindRestore _bind(this);
284 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
288 void Framebuffer::require_complete() const
290 FramebufferStatus status = check_status();
291 if(status!=FRAMEBUFFER_COMPLETE)
292 throw framebuffer_incomplete(status);
295 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
303 glViewport(view.left, view.bottom, view.width, view.height);
306 void Framebuffer::reset_viewport()
308 viewport(0, 0, width, height);
311 void Framebuffer::clear()
313 clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT|STENCIL_BUFFER_BIT);
316 void Framebuffer::clear(BufferBits bits)
318 BindRestore _bind(this);
322 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)
324 static Require _req(EXT_framebuffer_blit);
326 if(ARB_direct_state_access)
328 glBlitNamedFramebuffer(other.id, id, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
332 const Framebuffer *old = current();
333 if(set_current(this))
335 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
338 update_attachment(dirty);
343 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
345 glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
348 glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
351 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
353 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
356 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
358 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
361 void Framebuffer::bind() const
363 if(id && attachments.empty())
364 throw invalid_operation("Framebuffer::bind");
366 if(set_current(this))
368 glBindFramebuffer(GL_FRAMEBUFFER, id);
371 update_attachment(dirty);
376 glViewport(view.left, view.bottom, view.width, view.height);
380 const Framebuffer *Framebuffer::current()
387 void Framebuffer::unbind()
392 Framebuffer &Framebuffer::system()
394 static Framebuffer sys_framebuf(0);
399 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
406 void Framebuffer::Attachment::set(Renderbuffer &r)
408 type = GL_RENDERBUFFER;
414 void Framebuffer::Attachment::set(Texture &t, unsigned l, unsigned z)
416 type = t.get_target();
422 void Framebuffer::Attachment::clear()
428 Framebuffer::Viewport::Viewport():