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()
38 glDeleteFramebuffers(1, &id);
41 void OpenGLFramebuffer::set_system_format(const FrameFormat &fmt)
43 static_cast<Framebuffer *>(this)->format = fmt;
46 bool OpenGLFramebuffer::is_format_supported(const FrameFormat &fmt)
48 // Pretend everything is supported if we can't check
49 if(!ARB_internalformat_query || !ARB_internalformat_query2)
52 unsigned target = (fmt.get_samples()>1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D);
53 for(FrameAttachment a: fmt)
55 unsigned pf = get_gl_pixelformat(get_attachment_pixelformat(a));
57 glGetInternalformativ(target, pf, GL_FRAMEBUFFER_RENDERABLE, 1, &supported);
58 if(supported!=GL_FULL_SUPPORT)
65 void OpenGLFramebuffer::require_layered()
67 static Require _req(ARB_geometry_shader4);
70 void OpenGLFramebuffer::resize_system(unsigned w, unsigned h)
72 Framebuffer *self = static_cast<Framebuffer *>(this);
77 void OpenGLFramebuffer::update(unsigned mask) const
79 const FrameFormat &format = static_cast<const Framebuffer *>(this)->format;
80 vector<GLenum> color_bufs;
81 color_bufs.reserve(format.size());
83 for(FrameAttachment a: format)
85 GLenum gl_attach_point = get_gl_attachment(a);
88 const Framebuffer::Attachment &attch = static_cast<const Framebuffer *>(this)->attachments[i];
91 if(ARB_direct_state_access)
93 if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0)
94 glNamedFramebufferTexture(id, gl_attach_point, attch.tex->id, attch.level);
96 glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->id, attch.level, attch.layer);
98 else if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
99 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level);
100 else if(attch.layer<0)
101 glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level);
102 else if(attch.tex->target==GL_TEXTURE_2D_ARRAY)
103 glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level, attch.layer);
104 else if(attch.tex->target==GL_TEXTURE_3D)
105 glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level, attch.layer);
106 else if(attch.tex->target==GL_TEXTURE_CUBE_MAP)
107 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast<TextureCubeFace>(attch.layer)), attch.tex->id, attch.level);
109 else if(ARB_direct_state_access)
110 glNamedFramebufferTexture(id, gl_attach_point, 0, 0);
112 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0);
115 if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
116 color_bufs.push_back(gl_attach_point);
121 if(color_bufs.size()>1)
122 static Require _req(ARB_draw_buffers);
124 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
125 if(ARB_direct_state_access)
127 /* ARB_direct_state_access ties the availability of these functions to
128 framebuffers themselves, so no further checks are needed. */
129 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
130 glNamedFramebufferReadBuffer(id, first_buffer);
135 glDrawBuffers(color_bufs.size(), &color_bufs[0]);
136 else if(MSP_buffer_control)
137 glDrawBuffer(first_buffer);
139 if(MSP_buffer_control)
140 glReadBuffer(first_buffer);
143 if(ARB_direct_state_access)
144 status = glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER);
146 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
149 void OpenGLFramebuffer::require_complete() const
151 if(status==GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
152 throw framebuffer_incomplete("incomplete or unsupported attachment");
153 if(status==GL_FRAMEBUFFER_UNSUPPORTED)
154 throw framebuffer_incomplete("unsupported configuration");
155 if(status!=GL_FRAMEBUFFER_COMPLETE)
156 throw framebuffer_incomplete(Msp::format("incomplete (%#x)", status));
159 void OpenGLFramebuffer::set_debug_name(const string &name)
163 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());