]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.cpp
Refactor texture attachment management in Framebuffer
[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 "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         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)
121                         {
122                                 if(ARB_direct_state_access)
123                                 {
124                                         if(attch.type==GL_TEXTURE_2D)
125                                                 glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level);
126                                         else
127                                                 glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
128                                 }
129                                 else if(attch.type==GL_TEXTURE_2D)
130                                         glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
131                                 else if(attch.type==GL_TEXTURE_2D_ARRAY)
132                                         glFramebufferTextureLayer(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
133                                 else if(attch.type==GL_TEXTURE_3D)
134                                         glFramebufferTexture3D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level, attch.layer);
135                                 else if(attch.type==GL_TEXTURE_CUBE_MAP)
136                                         glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
137                         }
138                         else if(ARB_direct_state_access)
139                                 glNamedFramebufferRenderbuffer(id, attch.attachment, 0, 0);
140                         else
141                                 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
142                 }
143
144                 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
145                         color_bufs.push_back(attch.attachment);
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
171 void Framebuffer::check_size()
172 {
173         bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
174         for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
175                 if(i->type)
176                 {
177                         if(i->type==GL_RENDERBUFFER)
178                         {
179                                 width = i->rbuf->get_width();
180                                 height = i->rbuf->get_height();
181                         }
182                         else if(i->type==GL_TEXTURE_2D)
183                         {
184                                 Texture2D *tex = static_cast<Texture2D *>(i->tex);
185                                 width = max(tex->get_width()>>i->level, 1U);
186                                 height = max(tex->get_height()>>i->level, 1U);
187                         }
188                         else if(i->type==GL_TEXTURE_3D || i->type==GL_TEXTURE_2D_ARRAY)
189                         {
190                                 Texture3D *tex = static_cast<Texture3D *>(i->tex);
191                                 width = max(tex->get_width()>>i->level, 1U);
192                                 height = max(tex->get_height()>>i->level, 1U);
193                         }
194                         else if(i->type==GL_TEXTURE_CUBE_MAP)
195                         {
196                                 width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
197                                 height = width;
198                         }
199                         if(full_viewport)
200                                 reset_viewport();
201                         break;
202                 }
203 }
204
205 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
206 {
207         for(unsigned i=0; i<attachments.size(); ++i)
208                 if(attachments[i].attachment==attch)
209                         return i;
210         attachments.push_back(Attachment(attch));
211         return attachments.size()-1;
212 }
213
214 void Framebuffer::set_texture_attachment(FramebufferAttachment attch, Texture &tex, unsigned level, int layer)
215 {
216         if(!id)
217                 throw invalid_operation("Framebuffer::attach");
218
219         unsigned i = get_attachment_index(attch);
220         attachments[i].set(tex, level, layer);
221         update_attachment(1<<i);
222         check_size();
223 }
224
225 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
226 {
227         if(!id)
228                 throw invalid_operation("Framebuffer::attach");
229
230         unsigned i = get_attachment_index(attch);
231         attachments[i].set(rbuf);
232         update_attachment(1<<i);
233         check_size();
234 }
235
236 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
237 {
238         tex.allocate(level);
239         set_texture_attachment(attch, tex, level, 0);
240 }
241
242 void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
243 {
244         tex.allocate(level);
245         set_texture_attachment(attch, tex, level, layer);
246 }
247
248 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
249 {
250         tex.allocate(level);
251         set_texture_attachment(attch, tex, level, TextureCube::get_face_index(face));
252 }
253
254 void Framebuffer::detach(FramebufferAttachment attch)
255 {
256         if(!id)
257                 throw invalid_operation("Framebuffer::detach");
258
259         unsigned i = get_attachment_index(attch);
260         attachments[i].clear();
261         update_attachment(1<<i);
262         check_size();
263 }
264
265 FramebufferStatus Framebuffer::check_status() const
266 {
267         if(ARB_direct_state_access)
268                 return static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
269         else
270         {
271                 BindRestore _bind(this);
272                 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
273         }
274 }
275
276 void Framebuffer::require_complete() const
277 {
278         FramebufferStatus status = check_status();
279         if(status!=FRAMEBUFFER_COMPLETE)
280                 throw framebuffer_incomplete(status);
281 }
282
283 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
284 {
285         view.left = l;
286         view.bottom = b;
287         view.width = w;
288         view.height = h;
289
290         if(current()==this)
291                 glViewport(view.left, view.bottom, view.width, view.height);
292 }
293
294 void Framebuffer::reset_viewport()
295 {
296         viewport(0, 0, width, height);
297 }
298
299 void Framebuffer::clear()
300 {
301         clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT|STENCIL_BUFFER_BIT);
302 }
303
304 void Framebuffer::clear(BufferBits bits)
305 {
306         BindRestore _bind(this);
307         glClear(bits);
308 }
309
310 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)
311 {
312         static Require _req(EXT_framebuffer_blit);
313
314         if(ARB_direct_state_access)
315         {
316                 glBlitNamedFramebuffer(other.id, id, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
317                 return;
318         }
319
320         const Framebuffer *old = current();
321         if(set_current(this))
322         {
323                 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
324                 if(dirty)
325                 {
326                         update_attachment(dirty);
327                         dirty = 0;
328                 }
329         }
330         if(old!=&other)
331                 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
332
333         glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
334
335         set_current(old);
336         glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
337 }
338
339 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
340 {
341         blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
342 }
343
344 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
345 {
346         blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
347 }
348
349 void Framebuffer::bind() const
350 {
351         if(id && attachments.empty())
352                 throw invalid_operation("Framebuffer::bind");
353
354         if(set_current(this))
355         {
356                 glBindFramebuffer(GL_FRAMEBUFFER, id);
357                 if(dirty)
358                 {
359                         update_attachment(dirty);
360                         dirty = 0;
361                 }
362
363                 if(width && height)
364                         glViewport(view.left, view.bottom, view.width, view.height);
365         }
366 }
367
368 const Framebuffer *Framebuffer::current()
369 {
370         if(!cur_obj)
371                 cur_obj = &system();
372         return cur_obj;
373 }
374
375 void Framebuffer::unbind()
376 {
377         system().bind();
378 }
379
380 Framebuffer &Framebuffer::system()
381 {
382         static Framebuffer sys_framebuf(0);
383         return sys_framebuf;
384 }
385
386
387 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
388         attachment(a),
389         type(0),
390         level(0),
391         layer(0)
392 { }
393
394 void Framebuffer::Attachment::set(Renderbuffer &r)
395 {
396         type = GL_RENDERBUFFER;
397         rbuf = &r;
398         level = 0;
399         layer = 0;
400 }
401
402 void Framebuffer::Attachment::set(Texture &t, unsigned l, unsigned z)
403 {
404         type = t.get_target();
405         tex = &t;
406         level = l;
407         layer = z;
408 }
409
410 void Framebuffer::Attachment::clear()
411 {
412         type = 0;
413 }
414
415
416 Framebuffer::Viewport::Viewport():
417         left(0),
418         bottom(0),
419         width(0),
420         height(0)
421 { }
422
423 } // namespace GL
424 } // namespace Msp