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 Framebuffer &self = *static_cast<const Framebuffer *>(this);
88 vector<GLenum> color_bufs;
89 color_bufs.reserve(self.format.size());
91 for(FrameAttachment a: self.format)
93 GLenum gl_attach_point = get_gl_attachment(a);
96 const Framebuffer::Attachment &attch = self.attachments[i];
99 if(ARB_direct_state_access)
101 if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0)
102 glNamedFramebufferTexture(id, gl_attach_point, attch.tex->id, attch.level);
104 glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->id, attch.level, attch.layer);
106 else if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
107 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level);
108 else if(attch.layer<0)
109 glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level);
110 else if(attch.tex->target==GL_TEXTURE_2D_ARRAY)
111 glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level, attch.layer);
112 else if(attch.tex->target==GL_TEXTURE_3D)
113 glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level, attch.layer);
114 else if(attch.tex->target==GL_TEXTURE_CUBE_MAP)
115 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast<TextureCubeFace>(attch.layer)), attch.tex->id, attch.level);
117 else if(ARB_direct_state_access)
118 glNamedFramebufferTexture(id, gl_attach_point, 0, 0);
120 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0);
123 if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
124 color_bufs.push_back(gl_attach_point);
129 if(color_bufs.size()>1)
130 static Require _req(ARB_draw_buffers);
132 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
133 if(ARB_direct_state_access)
135 /* ARB_direct_state_access ties the availability of these functions to
136 framebuffers themselves, so no further checks are needed. */
137 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
138 glNamedFramebufferReadBuffer(id, first_buffer);
143 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
144 else if(MSP_buffer_control)
145 glDrawBuffer(first_buffer);
147 if(MSP_buffer_control)
148 glReadBuffer(first_buffer);
151 if(ARB_direct_state_access)
152 status = glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER);
154 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
157 void OpenGLFramebuffer::require_complete() const
159 if(status==GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
160 throw framebuffer_incomplete("incomplete or unsupported attachment");
161 if(status==GL_FRAMEBUFFER_UNSUPPORTED)
162 throw framebuffer_incomplete("unsupported configuration");
163 if(status!=GL_FRAMEBUFFER_COMPLETE)
164 throw framebuffer_incomplete(Msp::format("incomplete (%#x)", status));
167 void OpenGLFramebuffer::set_debug_name(const string &name)
171 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());