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_geometry_shader4.h>
4 #include <msp/gl/extensions/arb_internalformat_query.h>
5 #include <msp/gl/extensions/arb_internalformat_query2.h>
6 #include <msp/gl/extensions/ext_framebuffer_object.h>
7 #include <msp/gl/extensions/ext_texture_array.h>
8 #include <msp/gl/extensions/ext_texture3d.h>
9 #include <msp/gl/extensions/msp_buffer_control.h>
10 #include <msp/gl/extensions/khr_debug.h>
11 #include <msp/strings/format.h>
12 #include "framebuffer.h"
13 #include "framebuffer_backend.h"
21 OpenGLFramebuffer::OpenGLFramebuffer(bool is_system):
22 status(is_system ? GL_FRAMEBUFFER_COMPLETE : GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT)
26 static Require _req(EXT_framebuffer_object);
28 if(ARB_direct_state_access)
29 glCreateFramebuffers(1, &id);
31 glGenFramebuffers(1, &id);
35 OpenGLFramebuffer::OpenGLFramebuffer(OpenGLFramebuffer &&other):
42 OpenGLFramebuffer::~OpenGLFramebuffer()
45 glDeleteFramebuffers(1, &id);
48 void OpenGLFramebuffer::set_system_format(const FrameFormat &fmt)
50 static_cast<Framebuffer *>(this)->format = fmt;
53 bool OpenGLFramebuffer::is_format_supported(const FrameFormat &fmt)
55 // Pretend everything is supported if we can't check
56 if(!ARB_internalformat_query || !ARB_internalformat_query2)
59 unsigned target = (fmt.get_samples()>1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D);
60 for(FrameAttachment a: fmt)
62 unsigned pf = get_gl_pixelformat(get_attachment_pixelformat(a));
64 glGetInternalformativ(target, pf, GL_FRAMEBUFFER_RENDERABLE, 1, &supported);
65 if(supported!=GL_FULL_SUPPORT)
72 void OpenGLFramebuffer::require_layered()
74 static Require _req(ARB_geometry_shader4);
77 void OpenGLFramebuffer::resize_system(unsigned w, unsigned h)
79 Framebuffer *self = static_cast<Framebuffer *>(this);
84 void OpenGLFramebuffer::update(unsigned mask) const
86 const FrameFormat &format = static_cast<const Framebuffer *>(this)->format;
87 vector<GLenum> color_bufs;
88 color_bufs.reserve(format.size());
90 for(FrameAttachment a: format)
92 GLenum gl_attach_point = get_gl_attachment(a);
95 const Framebuffer::Attachment &attch = static_cast<const Framebuffer *>(this)->attachments[i];
98 if(ARB_direct_state_access)
100 if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0)
101 glNamedFramebufferTexture(id, gl_attach_point, attch.tex->id, attch.level);
103 glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->id, attch.level, attch.layer);
105 else if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
106 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level);
107 else if(attch.layer<0)
108 glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level);
109 else if(attch.tex->target==GL_TEXTURE_2D_ARRAY)
110 glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level, attch.layer);
111 else if(attch.tex->target==GL_TEXTURE_3D)
112 glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level, attch.layer);
113 else if(attch.tex->target==GL_TEXTURE_CUBE_MAP)
114 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast<TextureCubeFace>(attch.layer)), attch.tex->id, attch.level);
116 else if(ARB_direct_state_access)
117 glNamedFramebufferTexture(id, gl_attach_point, 0, 0);
119 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0);
122 if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
123 color_bufs.push_back(gl_attach_point);
128 if(color_bufs.size()>1)
129 static Require _req(ARB_draw_buffers);
131 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
132 if(ARB_direct_state_access)
134 /* ARB_direct_state_access ties the availability of these functions to
135 framebuffers themselves, so no further checks are needed. */
136 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
137 glNamedFramebufferReadBuffer(id, first_buffer);
142 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
143 else if(MSP_buffer_control)
144 glDrawBuffer(first_buffer);
146 if(MSP_buffer_control)
147 glReadBuffer(first_buffer);
150 if(ARB_direct_state_access)
151 status = glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER);
153 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
156 void OpenGLFramebuffer::require_complete() const
158 if(status==GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
159 throw framebuffer_incomplete("incomplete or unsupported attachment");
160 if(status==GL_FRAMEBUFFER_UNSUPPORTED)
161 throw framebuffer_incomplete("unsupported configuration");
162 if(status!=GL_FRAMEBUFFER_COMPLETE)
163 throw framebuffer_incomplete(Msp::format("incomplete (%#x)", status));
166 void OpenGLFramebuffer::set_debug_name(const string &name)
170 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());