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(const UInt16 *j=format.begin(); j!=format.end(); ++j, ++i)
136 GLenum gl_attach_point = get_gl_attachment(static_cast<FrameAttachment>(*j));
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);
171 if(color_bufs.size()>1)
172 static Require _req(ARB_draw_buffers);
174 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
175 if(ARB_direct_state_access)
177 /* ARB_direct_state_access ties the availability of these functions to
178 framebuffers themselves, so no further checks are needed. */
179 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
180 glNamedFramebufferReadBuffer(id, first_buffer);
185 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
186 else if(MSP_buffer_control)
187 glDrawBuffer(first_buffer);
189 if(MSP_buffer_control)
190 glReadBuffer(first_buffer);
193 if(ARB_direct_state_access)
194 status = static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
196 status = static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
201 void Framebuffer::check_size()
204 for(Attachment &a: attachments)
207 GLenum type = a.tex->get_target();
210 if(type==GL_TEXTURE_2D)
212 Texture2D *tex = static_cast<Texture2D *>(a.tex);
213 w = max(tex->get_width()>>a.level, 1U);
214 h = max(tex->get_height()>>a.level, 1U);
216 else if(type==GL_TEXTURE_2D_MULTISAMPLE)
218 Texture2DMultisample *tex = static_cast<Texture2DMultisample *>(a.tex);
219 w = tex->get_width();
220 h = tex->get_height();
222 else if(type==GL_TEXTURE_3D || type==GL_TEXTURE_2D_ARRAY)
224 Texture3D *tex = static_cast<Texture3D *>(a.tex);
225 w = max(tex->get_width()>>a.level, 1U);
226 h = max(tex->get_height()>>a.level, 1U);
228 else if(type==GL_TEXTURE_CUBE_MAP)
230 w = max(static_cast<TextureCube *>(a.tex)->get_size()>>a.level, 1U);
242 width = min(width, w);
243 height = min(height, h);
248 void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, unsigned level, int layer, unsigned samples)
250 if(format.empty() || !id)
251 throw invalid_operation("Framebuffer::attach");
253 if((format.get_samples()>1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples))
254 throw incompatible_data("Framebuffer::attach");
257 for(const UInt16 *j=format.begin(); j!=format.end(); ++j, ++i)
260 attachments[i].set(tex, level, layer);
266 throw incompatible_data("Framebuffer::attach");
269 void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
272 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, 0, 0);
275 void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex)
277 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, 0, tex.get_samples());
280 void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
283 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, layer, 0);
286 void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
289 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, TextureCube::get_face_index(face), 0);
292 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
294 static Require _req(ARB_geometry_shader4);
296 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
299 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
301 static Require _req(ARB_geometry_shader4);
303 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
306 void Framebuffer::detach(FrameAttachment attch)
309 throw invalid_operation("Framebuffer::detach");
311 int i = format.index(attch);
314 attachments[i].clear();
320 void Framebuffer::resize(const WindowView &view)
323 throw invalid_operation("Framebuffer::resize");
325 width = view.get_width();
326 height = view.get_height();
329 void Framebuffer::require_complete() const
331 if(status!=FRAMEBUFFER_COMPLETE)
332 throw framebuffer_incomplete(status);
335 void Framebuffer::set_debug_name(const string &name)
339 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
345 Framebuffer &Framebuffer::system()
347 static Framebuffer sys_framebuf(0);
352 Framebuffer::Attachment::Attachment():
358 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
365 void Framebuffer::Attachment::clear()