]> git.tdb.fi Git - libs/gl.git/blob - source/framebuffer.cpp
Prefer glDrawBuffers over glDrawBuffer
[libs/gl.git] / source / framebuffer.cpp
1 #include <msp/gl/extensions/arb_draw_buffers.h>
2 #include <msp/gl/extensions/ext_framebuffer_blit.h>
3 #include <msp/gl/extensions/ext_framebuffer_object.h>
4 #include "error.h"
5 #include "framebuffer.h"
6 #include "misc.h"
7 #include "renderbuffer.h"
8 #include "texture2d.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 void operator<<(LexicalConverter &conv, FramebufferStatus status)
16 {
17         switch(status)
18         {
19         case FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
20                 conv.result("incomplete attachment");
21                 break;
22         case FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
23                 conv.result("missing attachment");
24                 break;
25         case FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
26                 conv.result("mismatched attachment dimensions");
27                 break;
28         case FRAMEBUFFER_INCOMPLETE_FORMATS:
29                 conv.result("mismatched attachment formats");
30                 break;
31         case FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
32                 conv.result("missing draw buffer attachment");
33                 break;
34         case FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
35                 conv.result("missing read buffer attachment");
36                 break;
37         case FRAMEBUFFER_UNSUPPORTED:
38                 conv.result("unsupported");
39                 break;
40         default:
41                 conv.result(lexical_cast<string, unsigned>(status, "%#x"));
42                 break;
43         }
44 }
45
46 framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status):
47         runtime_error(lexical_cast<string>(status))
48 { }
49
50
51 Framebuffer::Framebuffer(unsigned i):
52         id(i),
53         dirty(0)
54 {
55         if(id)
56                 throw invalid_argument("System framebuffer must have id 0");
57
58         glGetIntegerv(GL_VIEWPORT, &view.left);
59         width = view.width;
60         height = view.height;
61 }
62
63 Framebuffer::Framebuffer():
64         width(0),
65         height(0),
66         dirty(0)
67 {
68         static Require _req(EXT_framebuffer_object);
69
70         glGenFramebuffers(1, &id);
71 }
72
73 Framebuffer::~Framebuffer()
74 {
75         if(id)
76                 glDeleteFramebuffers(1, &id);
77         if(current()==this)
78                 unbind();
79 }
80
81 void Framebuffer::update_attachment(unsigned mask) const
82 {
83         if(current()==this)
84         {
85                 std::vector<GLenum> color_bufs;
86                 color_bufs.reserve(attachments.size());
87                 for(unsigned i=0; i<attachments.size(); ++i)
88                 {
89                         const Attachment &attch = attachments[i];
90                         if(mask&(1<<i))
91                         {
92                                 if(attch.type==GL_RENDERBUFFER)
93                                         glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
94                                 else if(attch.type==GL_TEXTURE_2D)
95                                 {
96                                         static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
97                                         glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
98                                 }
99                                 else if(attch.type==GL_TEXTURE_CUBE_MAP)
100                                 {
101                                         static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
102                                         glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level);
103                                 }
104                                 else
105                                         glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
106                         }
107
108                         if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
109                                 color_bufs.push_back(attch.attachment);
110                 }
111
112                 if(color_bufs.size()>1)
113                         static Require _req(ARB_draw_buffers);
114
115                 GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
116                 if(ARB_draw_buffers)
117                         glDrawBuffers(color_bufs.size(), &color_bufs[0]);
118                 else
119                         glDrawBuffer(first_buffer);
120
121                 glReadBuffer(first_buffer);
122         }
123         else
124                 dirty |= mask;
125 }
126
127 void Framebuffer::check_size()
128 {
129         bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
130         for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
131                 if(i->type)
132                 {
133                         if(i->type==GL_RENDERBUFFER)
134                         {
135                                 width = i->rbuf->get_width();
136                                 height = i->rbuf->get_height();
137                         }
138                         else if(i->type==GL_TEXTURE_2D)
139                         {
140                                 Texture2D *tex = static_cast<Texture2D *>(i->tex);
141                                 width = tex->get_width();
142                                 height = tex->get_height();
143                         }
144                         else if(i->type==GL_TEXTURE_CUBE_MAP)
145                         {
146                                 width = static_cast<TextureCube *>(i->tex)->get_size();
147                                 height = width;
148                         }
149                         if(full_viewport)
150                                 reset_viewport();
151                         break;
152                 }
153 }
154
155 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
156 {
157         for(unsigned i=0; i<attachments.size(); ++i)
158                 if(attachments[i].attachment==attch)
159                         return i;
160         attachments.push_back(Attachment(attch));
161         return attachments.size()-1;
162 }
163
164 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
165 {
166         if(!id)
167                 throw invalid_operation("Framebuffer::attach");
168
169         unsigned i = get_attachment_index(attch);
170         attachments[i].set(rbuf);
171         update_attachment(1<<i);
172         check_size();
173 }
174
175 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
176 {
177         if(!id)
178                 throw invalid_operation("Framebuffer::attach");
179
180         unsigned i = get_attachment_index(attch);
181         attachments[i].set(tex, 0, level);
182         update_attachment(1<<i);
183         check_size();
184 }
185
186 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
187 {
188         if(!id)
189                 throw invalid_operation("Framebuffer::attach");
190
191         unsigned i = get_attachment_index(attch);
192         attachments[i].set(tex, face, level);
193         update_attachment(1<<i);
194         check_size();
195 }
196
197 void Framebuffer::detach(FramebufferAttachment attch)
198 {
199         if(!id)
200                 throw invalid_operation("Framebuffer::detach");
201
202         unsigned i = get_attachment_index(attch);
203         attachments[i].clear();
204         update_attachment(1<<i);
205         check_size();
206 }
207
208 FramebufferStatus Framebuffer::check_status() const
209 {
210         BindRestore _bind(this);
211         return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
212 }
213
214 void Framebuffer::require_complete() const
215 {
216         FramebufferStatus status = check_status();
217         if(status!=FRAMEBUFFER_COMPLETE)
218                 throw framebuffer_incomplete(status);
219 }
220
221 void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
222 {
223         view.left = l;
224         view.bottom = b;
225         view.width = w;
226         view.height = h;
227
228         if(current()==this)
229                 glViewport(view.left, view.bottom, view.width, view.height);
230 }
231
232 void Framebuffer::reset_viewport()
233 {
234         viewport(0, 0, width, height);
235 }
236
237 void Framebuffer::clear(BufferBits bits)
238 {
239         BindRestore _bind(this);
240         glClear(bits);
241 }
242
243 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)
244 {
245         static Require _req(EXT_framebuffer_blit);
246
247         const Framebuffer *old = current();
248         if(set_current(this))
249         {
250                 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
251                 if(dirty)
252                 {
253                         update_attachment(dirty);
254                         dirty = 0;
255                 }
256         }
257         if(old!=&other)
258                 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
259
260         glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
261
262         set_current(old);
263         glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
264 }
265
266 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
267 {
268         blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
269 }
270
271 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
272 {
273         blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
274 }
275
276 void Framebuffer::bind() const
277 {
278         if(set_current(this))
279         {
280                 glBindFramebuffer(GL_FRAMEBUFFER, id);
281                 if(dirty)
282                 {
283                         update_attachment(dirty);
284                         dirty = 0;
285                 }
286
287                 if(width && height)
288                         glViewport(view.left, view.bottom, view.width, view.height);
289         }
290 }
291
292 const Framebuffer *Framebuffer::current()
293 {
294         if(!cur_obj)
295                 cur_obj = &system();
296         return cur_obj;
297 }
298
299 void Framebuffer::unbind()
300 {
301         system().bind();
302 }
303
304 Framebuffer &Framebuffer::system()
305 {
306         static Framebuffer sys_framebuf(0);
307         return sys_framebuf;
308 }
309
310
311 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
312         attachment(a),
313         type(0),
314         level(0)
315 { }
316
317 void Framebuffer::Attachment::set(Renderbuffer &r)
318 {
319         type = GL_RENDERBUFFER;
320         rbuf = &r;
321         level = 0;
322 }
323
324 void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l)
325 {
326         type = t.get_target();
327         tex = &t;
328         cube_face = f;
329         level = l;
330 }
331
332 void Framebuffer::Attachment::clear()
333 {
334         type = 0;
335 }
336
337
338 Framebuffer::Viewport::Viewport():
339         left(0),
340         bottom(0),
341         width(0),
342         height(0)
343 { }
344
345 } // namespace GL
346 } // namespace Msp