]> git.tdb.fi Git - libs/gl.git/blob - source/framebuffer.cpp
Add another constructor to RenderTarget to resolve an ambiguity
[libs/gl.git] / source / 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/ext_framebuffer_blit.h>
4 #include <msp/gl/extensions/ext_framebuffer_object.h>
5 #include <msp/gl/extensions/ext_texture_array.h>
6 #include <msp/gl/extensions/ext_texture3d.h>
7 #include <msp/gl/extensions/msp_buffer_control.h>
8 #include "error.h"
9 #include "framebuffer.h"
10 #include "misc.h"
11 #include "renderbuffer.h"
12 #include "texture2d.h"
13 #include "texture3d.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19
20 void operator<<(LexicalConverter &conv, FramebufferStatus status)
21 {
22         switch(status)
23         {
24         case FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
25                 conv.result("incomplete attachment");
26                 break;
27         case FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
28                 conv.result("missing attachment");
29                 break;
30         case FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
31                 conv.result("mismatched attachment dimensions");
32                 break;
33         case FRAMEBUFFER_INCOMPLETE_FORMATS:
34                 conv.result("mismatched attachment formats");
35                 break;
36         case FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
37                 conv.result("missing draw buffer attachment");
38                 break;
39         case FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
40                 conv.result("missing read buffer attachment");
41                 break;
42         case FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
43                 conv.result("mismatched attachment sample counts");
44                 break;
45         case FRAMEBUFFER_INCOMPLETE_LAYER_COUNT:
46                 conv.result("mismatched attachment layer counts");
47                 break;
48         case FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
49                 conv.result("mismatched attachment layering");
50                 break;
51         case FRAMEBUFFER_UNSUPPORTED:
52                 conv.result("unsupported");
53                 break;
54         default:
55                 conv.result(lexical_cast<string, unsigned>(status, "%#x"));
56                 break;
57         }
58 }
59
60 framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status):
61         runtime_error(lexical_cast<string>(status))
62 { }
63
64
65 Framebuffer::Framebuffer(unsigned i):
66         id(i),
67         dirty(0)
68 {
69         if(id)
70                 throw invalid_argument("System framebuffer must have id 0");
71
72         glGetIntegerv(GL_VIEWPORT, &view.left);
73         width = view.width;
74         height = view.height;
75 }
76
77 Framebuffer::Framebuffer():
78         width(0),
79         height(0),
80         dirty(0)
81 {
82         static Require _req(EXT_framebuffer_object);
83
84         if(ARB_direct_state_access)
85                 glCreateFramebuffers(1, &id);
86         else
87                 glGenFramebuffers(1, &id);
88 }
89
90 Framebuffer::~Framebuffer()
91 {
92         if(id)
93                 glDeleteFramebuffers(1, &id);
94         if(current()==this)
95                 unbind();
96 }
97
98 void Framebuffer::update_attachment(unsigned mask) const
99 {
100         if(!ARB_direct_state_access && current()!=this)
101         {
102                 dirty |= mask;
103                 return;
104         }
105
106         std::vector<GLenum> color_bufs;
107         color_bufs.reserve(attachments.size());
108         for(unsigned i=0; i<attachments.size(); ++i)
109         {
110                 const Attachment &attch = attachments[i];
111                 if(mask&(1<<i))
112                 {
113                         if(attch.type==GL_RENDERBUFFER)
114                         {
115                                 if(ARB_direct_state_access)
116                                         glNamedFramebufferRenderbuffer(id, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
117                                 else
118                                         glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
119                         }
120                         else if(attch.type==GL_TEXTURE_2D)
121                         {
122                                 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
123                                 if(ARB_direct_state_access)
124                                         glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level);
125                                 else
126                                         glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
127                         }
128                         else if(attch.type==GL_TEXTURE_3D || attch.type==GL_TEXTURE_2D_ARRAY)
129                         {
130                                 static_cast<Texture3D *>(attch.tex)->allocate(attch.level);
131                                 if(ARB_direct_state_access)
132                                         glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
133                                 else if(attch.type==GL_TEXTURE_2D_ARRAY)
134                                         glFramebufferTextureLayer(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
135                                 else
136                                         glFramebufferTexture3D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level, attch.layer);
137                         }
138                         else if(attch.type==GL_TEXTURE_CUBE_MAP)
139                         {
140                                 static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
141                                 if(ARB_direct_state_access)
142                                         glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
143                                 else
144                                         glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
145                         }
146                         else if(ARB_direct_state_access)
147                                 glNamedFramebufferRenderbuffer(id, attch.attachment, 0, 0);
148                         else
149                                 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
150                 }
151
152                 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
153                         color_bufs.push_back(attch.attachment);
154         }
155
156         if(color_bufs.size()>1)
157                 static Require _req(ARB_draw_buffers);
158
159         GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
160         if(ARB_direct_state_access)
161         {
162                 /* ARB_direct_state_access ties the availability of these functions to
163                 framebuffers themselves, so no further checks are needed. */
164                 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
165                 glNamedFramebufferReadBuffer(id, first_buffer);
166         }
167         else
168         {
169                 if(ARB_draw_buffers)
170                         glDrawBuffers(color_bufs.size(), &color_bufs[0]);
171                 else if(MSP_buffer_control)
172                         glDrawBuffer(first_buffer);
173
174                 if(MSP_buffer_control)
175                         glReadBuffer(first_buffer);
176         }
177 }
178
179 void Framebuffer::check_size()
180 {
181         bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
182         for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
183                 if(i->type)
184                 {
185                         if(i->type==GL_RENDERBUFFER)
186                         {
187                                 width = i->rbuf->get_width();
188                                 height = i->rbuf->get_height();
189                         }
190                         else if(i->type==GL_TEXTURE_2D)
191                         {
192                                 Texture2D *tex = static_cast<Texture2D *>(i->tex);
193                                 width = max(tex->get_width()>>i->level, 1U);
194                                 height = max(tex->get_height()>>i->level, 1U);
195                         }
196                         else if(i->type==GL_TEXTURE_3D || i->type==GL_TEXTURE_2D_ARRAY)
197                         {
198                                 Texture3D *tex = static_cast<Texture3D *>(i->tex);
199                                 width = max(tex->get_width()>>i->level, 1U);
200                                 height = max(tex->get_height()>>i->level, 1U);
201                         }
202                         else if(i->type==GL_TEXTURE_CUBE_MAP)
203                         {
204                                 width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
205                                 height = width;
206                         }
207                         if(full_viewport)
208                                 reset_viewport();
209                         break;
210                 }
211 }
212
213 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
214 {
215         for(unsigned i=0; i<attachments.size(); ++i)
216                 if(attachments[i].attachment==attch)
217                         return i;
218         attachments.push_back(Attachment(attch));
219         return attachments.size()-1;
220 }
221
222 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
223 {
224         if(!id)
225                 throw invalid_operation("Framebuffer::attach");
226
227         unsigned i = get_attachment_index(attch);
228         attachments[i].set(rbuf);
229         update_attachment(1<<i);
230         check_size();
231 }
232
233 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
234 {
235         if(!id)
236                 throw invalid_operation("Framebuffer::attach");
237
238         unsigned i = get_attachment_index(attch);
239         attachments[i].set(tex, level, 0);
240         update_attachment(1<<i);
241         check_size();
242 }
243
244 void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
245 {
246         if(!id)
247                 throw invalid_operation("Framebuffer::attach");
248
249         unsigned i = get_attachment_index(attch);
250         attachments[i].set(tex, level, layer);
251         update_attachment(1<<i);
252         check_size();
253 }
254
255 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
256 {
257         if(!id)
258                 throw invalid_operation("Framebuffer::attach");
259
260         unsigned i = get_attachment_index(attch);
261         attachments[i].set(tex, level, TextureCube::get_face_index(face));
262         update_attachment(1<<i);
263         check_size();
264 }
265
266 void Framebuffer::detach(FramebufferAttachment attch)
267 {
268         if(!id)
269                 throw invalid_operation("Framebuffer::detach");
270
271         unsigned i = get_attachment_index(attch);
272         attachments[i].clear();
273         update_attachment(1<<i);
274         check_size();
275 }
276
277 FramebufferStatus Framebuffer::check_status() const
278 {
279         if(ARB_direct_state_access)
280                 return static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
281         else
282         {
283                 BindRestore _bind(this);
284                 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
285         }
286 }
287
288 void Framebuffer::require_complete() const
289 {
290         FramebufferStatus status = check_status();
291         if(status!=FRAMEBUFFER_COMPLETE)
292                 throw framebuffer_incomplete(status);
293 }
294
295 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
296 {
297         view.left = l;
298         view.bottom = b;
299         view.width = w;
300         view.height = h;
301
302         if(current()==this)
303                 glViewport(view.left, view.bottom, view.width, view.height);
304 }
305
306 void Framebuffer::reset_viewport()
307 {
308         viewport(0, 0, width, height);
309 }
310
311 void Framebuffer::clear()
312 {
313         clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT|STENCIL_BUFFER_BIT);
314 }
315
316 void Framebuffer::clear(BufferBits bits)
317 {
318         BindRestore _bind(this);
319         glClear(bits);
320 }
321
322 void Framebuffer::blit_from(const Framebuffer &other, int sx0, int sy0, int sx1, int sy1, int dx0, int dy0, int dx1, int dy1, BufferBits bits, bool filter)
323 {
324         static Require _req(EXT_framebuffer_blit);
325
326         if(ARB_direct_state_access)
327         {
328                 glBlitNamedFramebuffer(other.id, id, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
329                 return;
330         }
331
332         const Framebuffer *old = current();
333         if(set_current(this))
334         {
335                 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
336                 if(dirty)
337                 {
338                         update_attachment(dirty);
339                         dirty = 0;
340                 }
341         }
342         if(old!=&other)
343                 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
344
345         glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
346
347         set_current(old);
348         glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
349 }
350
351 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
352 {
353         blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
354 }
355
356 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
357 {
358         blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
359 }
360
361 void Framebuffer::bind() const
362 {
363         if(id && attachments.empty())
364                 throw invalid_operation("Framebuffer::bind");
365
366         if(set_current(this))
367         {
368                 glBindFramebuffer(GL_FRAMEBUFFER, id);
369                 if(dirty)
370                 {
371                         update_attachment(dirty);
372                         dirty = 0;
373                 }
374
375                 if(width && height)
376                         glViewport(view.left, view.bottom, view.width, view.height);
377         }
378 }
379
380 const Framebuffer *Framebuffer::current()
381 {
382         if(!cur_obj)
383                 cur_obj = &system();
384         return cur_obj;
385 }
386
387 void Framebuffer::unbind()
388 {
389         system().bind();
390 }
391
392 Framebuffer &Framebuffer::system()
393 {
394         static Framebuffer sys_framebuf(0);
395         return sys_framebuf;
396 }
397
398
399 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
400         attachment(a),
401         type(0),
402         level(0),
403         layer(0)
404 { }
405
406 void Framebuffer::Attachment::set(Renderbuffer &r)
407 {
408         type = GL_RENDERBUFFER;
409         rbuf = &r;
410         level = 0;
411         layer = 0;
412 }
413
414 void Framebuffer::Attachment::set(Texture &t, unsigned l, unsigned z)
415 {
416         type = t.get_target();
417         tex = &t;
418         level = l;
419         layer = z;
420 }
421
422 void Framebuffer::Attachment::clear()
423 {
424         type = 0;
425 }
426
427
428 Framebuffer::Viewport::Viewport():
429         left(0),
430         bottom(0),
431         width(0),
432         height(0)
433 { }
434
435 } // namespace GL
436 } // namespace Msp