]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/framebuffer_backend.cpp
18566ac3c2553fd82ea1dd71ab735dc0f3cd3168
[libs/gl.git] / source / backends / opengl / framebuffer_backend.cpp
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"
14 #include "gl.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 OpenGLFramebuffer::OpenGLFramebuffer(bool is_system):
22         status(is_system ? GL_FRAMEBUFFER_COMPLETE : GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT)
23 {
24         if(!is_system)
25         {
26                 static Require _req(EXT_framebuffer_object);
27
28                 if(ARB_direct_state_access)
29                         glCreateFramebuffers(1, &id);
30                 else
31                         glGenFramebuffers(1, &id);
32         }
33 }
34
35 OpenGLFramebuffer::OpenGLFramebuffer(OpenGLFramebuffer &&other):
36         id(other.id),
37         status(other.status)
38 {
39         other.id = 0;
40 }
41
42 OpenGLFramebuffer::~OpenGLFramebuffer()
43 {
44         if(id)
45                 glDeleteFramebuffers(1, &id);
46 }
47
48 void OpenGLFramebuffer::set_system_format(const FrameFormat &fmt)
49 {
50         static_cast<Framebuffer *>(this)->format = fmt;
51 }
52
53 bool OpenGLFramebuffer::is_format_supported(const FrameFormat &fmt)
54 {
55         // Pretend everything is supported if we can't check
56         if(!ARB_internalformat_query || !ARB_internalformat_query2)
57                 return true;
58
59         unsigned target = (fmt.get_samples()>1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D);
60         for(FrameAttachment a: fmt)
61         {
62                 unsigned pf = get_gl_pixelformat(get_attachment_pixelformat(a));
63                 int supported = 0;
64                 glGetInternalformativ(target, pf, GL_FRAMEBUFFER_RENDERABLE, 1, &supported);
65                 if(supported!=GL_FULL_SUPPORT)
66                         return false;
67         }
68
69         return true;
70 }
71
72 void OpenGLFramebuffer::require_layered()
73 {
74         static Require _req(ARB_geometry_shader4);
75 }
76
77 void OpenGLFramebuffer::resize_system(unsigned w, unsigned h)
78 {
79         Framebuffer &self = *static_cast<Framebuffer *>(this);
80         self.width = w;
81         self.height = h;
82 }
83
84 void OpenGLFramebuffer::update(unsigned mask) const
85 {
86         const Framebuffer &self = *static_cast<const Framebuffer *>(this);
87
88         vector<GLenum> color_bufs;
89         color_bufs.reserve(self.format.size());
90         unsigned i = 0;
91         for(FrameAttachment a: self.format)
92         {
93                 GLenum gl_attach_point = get_gl_attachment(a);
94                 if(mask&(1<<i))
95                 {
96                         const Framebuffer::Attachment &attch = self.attachments[i];
97                         if(attch.tex)
98                         {
99                                 if(ARB_direct_state_access)
100                                 {
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);
103                                         else
104                                                 glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->id, attch.level, attch.layer);
105                                 }
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);
116                         }
117                         else if(ARB_direct_state_access)
118                                 glNamedFramebufferTexture(id, gl_attach_point, 0, 0);
119                         else
120                                 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0);
121                 }
122
123                 if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
124                         color_bufs.push_back(gl_attach_point);
125
126                 ++i;
127         }
128
129         if(color_bufs.size()>1)
130                 static Require _req(ARB_draw_buffers);
131
132         GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
133         if(ARB_direct_state_access)
134         {
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);
139         }
140         else
141         {
142                 if(ARB_draw_buffers)
143                         glDrawBuffers(color_bufs.size(), &color_bufs[0]);
144                 else if(MSP_buffer_control)
145                         glDrawBuffer(first_buffer);
146
147                 if(MSP_buffer_control)
148                         glReadBuffer(first_buffer);
149         }
150
151         if(ARB_direct_state_access)
152                 status = glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER);
153         else
154                 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
155 }
156
157 void OpenGLFramebuffer::require_complete() const
158 {
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));
165 }
166
167 void OpenGLFramebuffer::set_debug_name(const string &name)
168 {
169 #ifdef DEBUG
170         if(KHR_debug)
171                 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
172 #else
173         (void)name;
174 #endif
175 }
176
177 } // namespace GL
178 } // namespace Msp