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