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_object.h>
4 #include <msp/gl/extensions/ext_texture_array.h>
5 #include <msp/gl/extensions/ext_texture3d.h>
6 #include <msp/gl/extensions/msp_buffer_control.h>
7 #include <msp/gl/extensions/khr_debug.h>
9 #include "framebuffer.h"
11 #include "texture2d.h"
12 #include "texture2dmultisample.h"
13 #include "texture3d.h"
14 #include "windowview.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):
68 status(FRAMEBUFFER_COMPLETE),
72 throw invalid_argument("System framebuffer must have id 0");
75 glGetIntegerv(GL_VIEWPORT, view);
80 Framebuffer::Framebuffer()
85 Framebuffer::Framebuffer(FrameAttachment fa)
91 Framebuffer::Framebuffer(const FrameFormat &f)
97 void Framebuffer::init()
99 static Require _req(EXT_framebuffer_object);
103 status = FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
106 if(ARB_direct_state_access)
107 glCreateFramebuffers(1, &id);
109 glGenFramebuffers(1, &id);
112 Framebuffer::~Framebuffer()
115 glDeleteFramebuffers(1, &id);
118 void Framebuffer::set_format(const FrameFormat &fmt)
121 throw invalid_operation("Framebuffer::set_format");
123 throw invalid_argument("Framebuffer::set_format");
126 attachments.resize(format.size());
129 void Framebuffer::update() const
131 vector<GLenum> color_bufs;
132 color_bufs.reserve(format.size());
134 for(FrameAttachment a: format)
136 GLenum gl_attach_point = get_gl_attachment(a);
139 const Attachment &attch = attachments[i];
142 GLenum type = attch.tex->get_target();
143 if(ARB_direct_state_access)
145 if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0)
146 glNamedFramebufferTexture(id, gl_attach_point, attch.tex->get_id(), attch.level);
148 glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->get_id(), attch.level, attch.layer);
150 else if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE)
151 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, type, attch.tex->get_id(), attch.level);
152 else if(attch.layer<0)
153 glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->get_id(), attch.level);
154 else if(type==GL_TEXTURE_2D_ARRAY)
155 glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->get_id(), attch.level, attch.layer);
156 else if(type==GL_TEXTURE_3D)
157 glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, type, attch.tex->get_id(), attch.level, attch.layer);
158 else if(type==GL_TEXTURE_CUBE_MAP)
159 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
161 else if(ARB_direct_state_access)
162 glNamedFramebufferTexture(id, gl_attach_point, 0, 0);
164 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0);
167 if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
168 color_bufs.push_back(gl_attach_point);
173 if(color_bufs.size()>1)
174 static Require _req(ARB_draw_buffers);
176 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
177 if(ARB_direct_state_access)
179 /* ARB_direct_state_access ties the availability of these functions to
180 framebuffers themselves, so no further checks are needed. */
181 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
182 glNamedFramebufferReadBuffer(id, first_buffer);
187 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
188 else if(MSP_buffer_control)
189 glDrawBuffer(first_buffer);
191 if(MSP_buffer_control)
192 glReadBuffer(first_buffer);
195 if(ARB_direct_state_access)
196 status = static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
198 status = static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
203 void Framebuffer::check_size()
206 for(Attachment &a: attachments)
209 GLenum type = a.tex->get_target();
212 if(type==GL_TEXTURE_2D)
214 Texture2D *tex = static_cast<Texture2D *>(a.tex);
215 w = max(tex->get_width()>>a.level, 1U);
216 h = max(tex->get_height()>>a.level, 1U);
218 else if(type==GL_TEXTURE_2D_MULTISAMPLE)
220 Texture2DMultisample *tex = static_cast<Texture2DMultisample *>(a.tex);
221 w = tex->get_width();
222 h = tex->get_height();
224 else if(type==GL_TEXTURE_3D || type==GL_TEXTURE_2D_ARRAY)
226 Texture3D *tex = static_cast<Texture3D *>(a.tex);
227 w = max(tex->get_width()>>a.level, 1U);
228 h = max(tex->get_height()>>a.level, 1U);
230 else if(type==GL_TEXTURE_CUBE_MAP)
232 w = max(static_cast<TextureCube *>(a.tex)->get_size()>>a.level, 1U);
244 width = min(width, w);
245 height = min(height, h);
250 void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, unsigned level, int layer, unsigned samples)
252 if(format.empty() || !id)
253 throw invalid_operation("Framebuffer::attach");
255 if((format.get_samples()>1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples))
256 throw incompatible_data("Framebuffer::attach");
259 for(FrameAttachment a: format)
263 attachments[i].set(tex, level, layer);
271 throw incompatible_data("Framebuffer::attach");
274 void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
277 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, 0, 0);
280 void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex)
282 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, 0, tex.get_samples());
285 void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
288 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, layer, 0);
291 void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
294 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, TextureCube::get_face_index(face), 0);
297 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
299 static Require _req(ARB_geometry_shader4);
301 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
304 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
306 static Require _req(ARB_geometry_shader4);
308 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
311 void Framebuffer::detach(FrameAttachment attch)
314 throw invalid_operation("Framebuffer::detach");
316 int i = format.index(attch);
319 attachments[i].clear();
325 void Framebuffer::resize(const WindowView &view)
328 throw invalid_operation("Framebuffer::resize");
330 width = view.get_width();
331 height = view.get_height();
334 void Framebuffer::require_complete() const
336 if(status!=FRAMEBUFFER_COMPLETE)
337 throw framebuffer_incomplete(status);
340 void Framebuffer::set_debug_name(const string &name)
344 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
350 Framebuffer &Framebuffer::system()
352 static Framebuffer sys_framebuf(0);
357 Framebuffer::Attachment::Attachment():
363 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
370 void Framebuffer::Attachment::clear()