]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.cpp
Check supported attachment formats in Framebuffer::set_format
[libs/gl.git] / source / core / framebuffer.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_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>
11 #include "error.h"
12 #include "framebuffer.h"
13 #include "misc.h"
14 #include "texture2d.h"
15 #include "texture2dmultisample.h"
16 #include "texture3d.h"
17 #include "windowview.h"
18
19 using namespace std;
20
21 namespace Msp {
22 namespace GL {
23
24 framebuffer_incomplete::framebuffer_incomplete(const std::string &reason):
25         runtime_error(reason)
26 { }
27
28
29 Framebuffer::Framebuffer(unsigned i):
30         id(i),
31         status(GL_FRAMEBUFFER_COMPLETE),
32         dirty(0)
33 {
34         if(id)
35                 throw invalid_argument("System framebuffer must have id 0");
36
37         int view[4];
38         glGetIntegerv(GL_VIEWPORT, view);
39         width = view[2];
40         height = view[3];
41 }
42
43 Framebuffer::Framebuffer()
44 {
45         init();
46 }
47
48 Framebuffer::Framebuffer(FrameAttachment fa)
49 {
50         init();
51         set_format(fa);
52 }
53
54 Framebuffer::Framebuffer(const FrameFormat &f)
55 {
56         init();
57         set_format(f);
58 }
59
60 void Framebuffer::init()
61 {
62         static Require _req(EXT_framebuffer_object);
63
64         width = 0;
65         height = 0;
66         status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
67         dirty = 0;
68
69         if(ARB_direct_state_access)
70                 glCreateFramebuffers(1, &id);
71         else
72                 glGenFramebuffers(1, &id);
73 }
74
75 Framebuffer::~Framebuffer()
76 {
77         if(id)
78                 glDeleteFramebuffers(1, &id);
79 }
80
81 void Framebuffer::set_format(const FrameFormat &fmt)
82 {
83         if(!format.empty())
84                 throw invalid_operation("Framebuffer::set_format");
85         if(fmt.empty())
86                 throw invalid_argument("Framebuffer::set_format");
87
88         if(ARB_internalformat_query && ARB_internalformat_query2)
89         {
90                 unsigned target = (fmt.get_samples()>1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D);
91                 for(FrameAttachment a: fmt)
92                 {
93                         unsigned pf = get_gl_pixelformat(get_attachment_pixelformat(a));
94                         int supported = 0;
95                         glGetInternalformativ(target, pf, GL_FRAMEBUFFER_RENDERABLE, 1, &supported);
96                         if(supported!=GL_FULL_SUPPORT)
97                                 throw invalid_argument("Framebuffer::set_format");
98                 }
99         }
100
101         format = fmt;
102         attachments.resize(format.size());
103 }
104
105 void Framebuffer::update() const
106 {
107         vector<GLenum> color_bufs;
108         color_bufs.reserve(format.size());
109         unsigned i = 0;
110         for(FrameAttachment a: format)
111         {
112                 GLenum gl_attach_point = get_gl_attachment(a);
113                 if(dirty&(1<<i))
114                 {
115                         const Attachment &attch = attachments[i];
116                         if(attch.tex)
117                         {
118                                 if(ARB_direct_state_access)
119                                 {
120                                         if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0)
121                                                 glNamedFramebufferTexture(id, gl_attach_point, attch.tex->id, attch.level);
122                                         else
123                                                 glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->id, attch.level, attch.layer);
124                                 }
125                                 else if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
126                                         glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level);
127                                 else if(attch.layer<0)
128                                         glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level);
129                                 else if(attch.tex->target==GL_TEXTURE_2D_ARRAY)
130                                         glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level, attch.layer);
131                                 else if(attch.tex->target==GL_TEXTURE_3D)
132                                         glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level, attch.layer);
133                                 else if(attch.tex->target==GL_TEXTURE_CUBE_MAP)
134                                         glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast<TextureCubeFace>(attch.layer)), attch.tex->id, attch.level);
135                         }
136                         else if(ARB_direct_state_access)
137                                 glNamedFramebufferTexture(id, gl_attach_point, 0, 0);
138                         else
139                                 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0);
140                 }
141
142                 if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
143                         color_bufs.push_back(gl_attach_point);
144
145                 ++i;
146         }
147
148         if(color_bufs.size()>1)
149                 static Require _req(ARB_draw_buffers);
150
151         GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
152         if(ARB_direct_state_access)
153         {
154                 /* ARB_direct_state_access ties the availability of these functions to
155                 framebuffers themselves, so no further checks are needed. */
156                 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
157                 glNamedFramebufferReadBuffer(id, first_buffer);
158         }
159         else
160         {
161                 if(ARB_draw_buffers)
162                         glDrawBuffers(color_bufs.size(), &color_bufs[0]);
163                 else if(MSP_buffer_control)
164                         glDrawBuffer(first_buffer);
165
166                 if(MSP_buffer_control)
167                         glReadBuffer(first_buffer);
168         }
169
170         if(ARB_direct_state_access)
171                 status = glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER);
172         else
173                 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
174
175         dirty = 0;
176 }
177
178 void Framebuffer::check_size()
179 {
180         bool first = true;
181         for(Attachment &a: attachments)
182                 if(a.tex)
183                 {
184                         unsigned w = 0;
185                         unsigned h = 0;
186                         if(a.tex->target==GL_TEXTURE_2D)
187                         {
188                                 Texture2D *tex = static_cast<Texture2D *>(a.tex);
189                                 w = max(tex->get_width()>>a.level, 1U);
190                                 h = max(tex->get_height()>>a.level, 1U);
191                         }
192                         else if(a.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
193                         {
194                                 Texture2DMultisample *tex = static_cast<Texture2DMultisample *>(a.tex);
195                                 w = tex->get_width();
196                                 h = tex->get_height();
197                         }
198                         else if(a.tex->target==GL_TEXTURE_3D || a.tex->target==GL_TEXTURE_2D_ARRAY)
199                         {
200                                 Texture3D *tex = static_cast<Texture3D *>(a.tex);
201                                 w = max(tex->get_width()>>a.level, 1U);
202                                 h = max(tex->get_height()>>a.level, 1U);
203                         }
204                         else if(a.tex->target==GL_TEXTURE_CUBE_MAP)
205                         {
206                                 w = max(static_cast<TextureCube *>(a.tex)->get_size()>>a.level, 1U);
207                                 h = w;
208                         }
209
210                         if(first)
211                         {
212                                 width = w;
213                                 height = h;
214                                 first = false;
215                         }
216                         else
217                         {
218                                 width = min(width, w);
219                                 height = min(height, h);
220                         }
221                 }
222 }
223
224 void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, unsigned level, int layer, unsigned samples)
225 {
226         if(format.empty() || !id)
227                 throw invalid_operation("Framebuffer::attach");
228
229         if((format.get_samples()>1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples))
230                 throw incompatible_data("Framebuffer::attach");
231
232         unsigned i = 0;
233         for(FrameAttachment a: format)
234         {
235                 if(a==attch)
236                 {
237                         attachments[i].set(tex, level, layer);
238                         dirty |= 1<<i;
239                         check_size();
240                         return;
241                 }
242                 ++i;
243         }
244
245         throw incompatible_data("Framebuffer::attach");
246 }
247
248 void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
249 {
250         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, 0, 0);
251 }
252
253 void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex)
254 {
255         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, 0, tex.get_samples());
256 }
257
258 void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
259 {
260         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, layer, 0);
261 }
262
263 void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
264 {
265         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, face, 0);
266 }
267
268 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
269 {
270         static Require _req(ARB_geometry_shader4);
271         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
272 }
273
274 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
275 {
276         static Require _req(ARB_geometry_shader4);
277         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
278 }
279
280 void Framebuffer::detach(FrameAttachment attch)
281 {
282         if(!id)
283                 throw invalid_operation("Framebuffer::detach");
284
285         int i = format.index(attch);
286         if(i>=0)
287         {
288                 attachments[i].clear();
289                 dirty |= 1<<i;
290                 check_size();
291         }
292 }
293
294 void Framebuffer::resize(const WindowView &view)
295 {
296         if(id)
297                 throw invalid_operation("Framebuffer::resize");
298
299         width = view.get_width();
300         height = view.get_height();
301 }
302
303 void Framebuffer::require_complete() const
304 {
305         if(!id)
306                 return;
307
308         bool layered = (!attachments.empty() && attachments.front().layer<0);
309         for(const Attachment &a: attachments)
310         {
311                 if(!a.tex)
312                         throw framebuffer_incomplete("missing attachment");
313                 if(layered!=(a.layer<0))
314                         throw framebuffer_incomplete("inconsistent layering");
315         }
316
317         if(status==GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
318                 throw framebuffer_incomplete("incomplete or unsupported attachment");
319         if(status==GL_FRAMEBUFFER_UNSUPPORTED)
320                 throw framebuffer_incomplete("unsupported configuration");
321         if(status!=GL_FRAMEBUFFER_COMPLETE)
322                 throw framebuffer_incomplete(Msp::format("incomplete (%#x)", status));
323 }
324
325 void Framebuffer::set_debug_name(const string &name)
326 {
327 #ifdef DEBUG
328         if(KHR_debug)
329                 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
330 #else
331         (void)name;
332 #endif
333 }
334
335 Framebuffer &Framebuffer::system()
336 {
337         static Framebuffer sys_framebuf(0);
338         return sys_framebuf;
339 }
340
341
342 Framebuffer::Attachment::Attachment():
343         tex(0),
344         level(0),
345         layer(0)
346 { }
347
348 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
349 {
350         tex = &t;
351         level = l;
352         layer = z;
353 }
354
355 void Framebuffer::Attachment::clear()
356 {
357         tex = 0;
358 }
359
360 } // namespace GL
361 } // namespace Msp