1 #include <msp/gl/extensions/arb_draw_buffers.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_internalformat_query.h>
4 #include <msp/gl/extensions/arb_internalformat_query2.h>
5 #include <msp/gl/extensions/ext_framebuffer_object.h>
6 #include <msp/gl/extensions/ext_texture_array.h>
7 #include <msp/gl/extensions/ext_texture3d.h>
8 #include <msp/gl/extensions/msp_buffer_control.h>
9 #include <msp/gl/extensions/khr_debug.h>
10 #include <msp/strings/format.h>
12 #include "framebuffer.h"
14 #include "texture2d.h"
15 #include "texture2dmultisample.h"
16 #include "texture3d.h"
17 #include "windowview.h"
24 framebuffer_incomplete::framebuffer_incomplete(const std::string &reason):
29 Framebuffer::Framebuffer(unsigned i):
31 status(GL_FRAMEBUFFER_COMPLETE),
35 throw invalid_argument("System framebuffer must have id 0");
37 if(EXT_framebuffer_object)
40 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_BACK, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
42 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_FRONT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
44 format = (format,COLOR_ATTACHMENT);
46 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
48 format = (format,DEPTH_ATTACHMENT);
50 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_STENCIL, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
52 format = (format,STENCIL_ATTACHMENT);
56 glGetIntegerv(GL_VIEWPORT, view);
61 Framebuffer::Framebuffer()
66 Framebuffer::Framebuffer(FrameAttachment fa)
72 Framebuffer::Framebuffer(const FrameFormat &f)
78 void Framebuffer::init()
80 static Require _req(EXT_framebuffer_object);
84 status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
87 if(ARB_direct_state_access)
88 glCreateFramebuffers(1, &id);
90 glGenFramebuffers(1, &id);
93 Framebuffer::~Framebuffer()
96 glDeleteFramebuffers(1, &id);
99 void Framebuffer::set_format(const FrameFormat &fmt)
101 if(!format.empty() || !id)
102 throw invalid_operation("Framebuffer::set_format");
104 throw invalid_argument("Framebuffer::set_format");
106 if(ARB_internalformat_query && ARB_internalformat_query2)
108 unsigned target = (fmt.get_samples()>1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D);
109 for(FrameAttachment a: fmt)
111 unsigned pf = get_gl_pixelformat(get_attachment_pixelformat(a));
113 glGetInternalformativ(target, pf, GL_FRAMEBUFFER_RENDERABLE, 1, &supported);
114 if(supported!=GL_FULL_SUPPORT)
115 throw invalid_argument("Framebuffer::set_format");
120 attachments.resize(format.size());
123 void Framebuffer::update() const
125 vector<GLenum> color_bufs;
126 color_bufs.reserve(format.size());
128 for(FrameAttachment a: format)
130 GLenum gl_attach_point = get_gl_attachment(a);
133 const Attachment &attch = attachments[i];
136 if(ARB_direct_state_access)
138 if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0)
139 glNamedFramebufferTexture(id, gl_attach_point, attch.tex->id, attch.level);
141 glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->id, attch.level, attch.layer);
143 else if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
144 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level);
145 else if(attch.layer<0)
146 glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level);
147 else if(attch.tex->target==GL_TEXTURE_2D_ARRAY)
148 glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level, attch.layer);
149 else if(attch.tex->target==GL_TEXTURE_3D)
150 glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level, attch.layer);
151 else if(attch.tex->target==GL_TEXTURE_CUBE_MAP)
152 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast<TextureCubeFace>(attch.layer)), attch.tex->id, attch.level);
154 else if(ARB_direct_state_access)
155 glNamedFramebufferTexture(id, gl_attach_point, 0, 0);
157 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0);
160 if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
161 color_bufs.push_back(gl_attach_point);
166 if(color_bufs.size()>1)
167 static Require _req(ARB_draw_buffers);
169 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
170 if(ARB_direct_state_access)
172 /* ARB_direct_state_access ties the availability of these functions to
173 framebuffers themselves, so no further checks are needed. */
174 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
175 glNamedFramebufferReadBuffer(id, first_buffer);
180 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
181 else if(MSP_buffer_control)
182 glDrawBuffer(first_buffer);
184 if(MSP_buffer_control)
185 glReadBuffer(first_buffer);
188 if(ARB_direct_state_access)
189 status = glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER);
191 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
196 void Framebuffer::check_size()
199 for(Attachment &a: attachments)
204 if(a.tex->target==GL_TEXTURE_2D)
206 Texture2D *tex = static_cast<Texture2D *>(a.tex);
207 w = max(tex->get_width()>>a.level, 1U);
208 h = max(tex->get_height()>>a.level, 1U);
210 else if(a.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
212 Texture2DMultisample *tex = static_cast<Texture2DMultisample *>(a.tex);
213 w = tex->get_width();
214 h = tex->get_height();
216 else if(a.tex->target==GL_TEXTURE_3D || a.tex->target==GL_TEXTURE_2D_ARRAY)
218 Texture3D *tex = static_cast<Texture3D *>(a.tex);
219 w = max(tex->get_width()>>a.level, 1U);
220 h = max(tex->get_height()>>a.level, 1U);
222 else if(a.tex->target==GL_TEXTURE_CUBE_MAP)
224 w = max(static_cast<TextureCube *>(a.tex)->get_size()>>a.level, 1U);
236 width = min(width, w);
237 height = min(height, h);
242 void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, unsigned level, int layer, unsigned samples)
244 if(format.empty() || !id)
245 throw invalid_operation("Framebuffer::attach");
247 if((format.get_samples()>1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples))
248 throw incompatible_data("Framebuffer::attach");
251 for(FrameAttachment a: format)
255 attachments[i].set(tex, level, layer);
263 throw incompatible_data("Framebuffer::attach");
266 void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
268 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, 0, 0);
271 void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex)
273 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, 0, tex.get_samples());
276 void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
278 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, layer, 0);
281 void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
283 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, face, 0);
286 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
288 static Require _req(ARB_geometry_shader4);
289 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
292 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
294 static Require _req(ARB_geometry_shader4);
295 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
298 void Framebuffer::detach(FrameAttachment attch)
301 throw invalid_operation("Framebuffer::detach");
303 int i = format.index(attch);
306 attachments[i].clear();
312 void Framebuffer::resize(const WindowView &view)
315 throw invalid_operation("Framebuffer::resize");
317 width = view.get_width();
318 height = view.get_height();
321 void Framebuffer::require_complete() const
326 bool layered = (!attachments.empty() && attachments.front().layer<0);
327 for(const Attachment &a: attachments)
330 throw framebuffer_incomplete("missing attachment");
331 if(layered!=(a.layer<0))
332 throw framebuffer_incomplete("inconsistent layering");
335 if(status==GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
336 throw framebuffer_incomplete("incomplete or unsupported attachment");
337 if(status==GL_FRAMEBUFFER_UNSUPPORTED)
338 throw framebuffer_incomplete("unsupported configuration");
339 if(status!=GL_FRAMEBUFFER_COMPLETE)
340 throw framebuffer_incomplete(Msp::format("incomplete (%#x)", status));
343 void Framebuffer::set_debug_name(const string &name)
347 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
353 Framebuffer &Framebuffer::system()
355 static Framebuffer sys_framebuf(0);
360 Framebuffer::Attachment::Attachment():
366 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
373 void Framebuffer::Attachment::clear()