]> git.tdb.fi Git - libs/gl.git/blob - source/framebuffer.cpp
Add a function to require the completeness of a framebuffer
[libs/gl.git] / source / framebuffer.cpp
1 #include "arb_draw_buffers.h"
2 #include "error.h"
3 #include "ext_framebuffer_blit.h"
4 #include "ext_framebuffer_object.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         int viewport[4];
59         glGetIntegerv(GL_VIEWPORT, viewport);
60         width = viewport[2];
61         height = viewport[3];
62 }
63
64 Framebuffer::Framebuffer():
65         width(0),
66         height(0),
67         dirty(0)
68 {
69         static Require _req(EXT_framebuffer_object);
70
71         glGenFramebuffers(1, &id);
72 }
73
74 Framebuffer::~Framebuffer()
75 {
76         if(id)
77                 glDeleteFramebuffers(1, &id);
78         if(current()==this)
79                 unbind();
80 }
81
82 void Framebuffer::update_attachment(unsigned mask) const
83 {
84         if(current()==this)
85         {
86                 std::vector<GLenum> color_bufs;
87                 color_bufs.reserve(attachments.size());
88                 for(unsigned i=0; i<attachments.size(); ++i)
89                 {
90                         const Attachment &attch = attachments[i];
91                         if(mask&(1<<i))
92                         {
93                                 if(attch.type==GL_RENDERBUFFER)
94                                         glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
95                                 else if(attch.type==GL_TEXTURE_2D)
96                                 {
97                                         static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
98                                         glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
99                                 }
100                                 else if(attch.type==GL_TEXTURE_CUBE_MAP)
101                                 {
102                                         static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
103                                         glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level);
104                                 }
105                                 else
106                                         glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
107                         }
108
109                         if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
110                                 color_bufs.push_back(attch.attachment);
111                 }
112
113                 if(color_bufs.empty())
114                         glDrawBuffer(GL_NONE);
115                 else if(color_bufs.size()==1)
116                         glDrawBuffer(color_bufs.front());
117                 else
118                 {
119                         static Require _req(ARB_draw_buffers);
120                         glDrawBuffers(color_bufs.size(), &color_bufs[0]);
121                 }
122         }
123         else
124                 dirty |= mask;
125 }
126
127 void Framebuffer::check_size()
128 {
129         for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
130                 if(i->type)
131                 {
132                         if(i->type==GL_RENDERBUFFER)
133                         {
134                                 width = i->rbuf->get_width();
135                                 height = i->rbuf->get_height();
136                         }
137                         else if(i->type==GL_TEXTURE_2D)
138                         {
139                                 Texture2D *tex = static_cast<Texture2D *>(i->tex);
140                                 width = tex->get_width();
141                                 height = tex->get_height();
142                         }
143                         else if(i->type==GL_TEXTURE_CUBE_MAP)
144                         {
145                                 width = static_cast<TextureCube *>(i->tex)->get_size();
146                                 height = width;
147                         }
148                         if(current()==this)
149                                 glViewport(0, 0, width, height);
150                         break;
151                 }
152 }
153
154 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
155 {
156         for(unsigned i=0; i<attachments.size(); ++i)
157                 if(attachments[i].attachment==attch)
158                         return i;
159         attachments.push_back(Attachment(attch));
160         return attachments.size()-1;
161 }
162
163 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
164 {
165         if(!id)
166                 throw invalid_operation("Framebuffer::attach");
167
168         unsigned i = get_attachment_index(attch);
169         attachments[i].set(rbuf);
170         update_attachment(1<<i);
171         check_size();
172 }
173
174 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
175 {
176         if(!id)
177                 throw invalid_operation("Framebuffer::attach");
178
179         unsigned i = get_attachment_index(attch);
180         attachments[i].set(tex, 0, level);
181         update_attachment(1<<i);
182         check_size();
183 }
184
185 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
186 {
187         if(!id)
188                 throw invalid_operation("Framebuffer::attach");
189
190         unsigned i = get_attachment_index(attch);
191         attachments[i].set(tex, face, level);
192         update_attachment(1<<i);
193         check_size();
194 }
195
196 void Framebuffer::detach(FramebufferAttachment attch)
197 {
198         if(!id)
199                 throw invalid_operation("Framebuffer::detach");
200
201         unsigned i = get_attachment_index(attch);
202         attachments[i].clear();
203         update_attachment(1<<i);
204         check_size();
205 }
206
207 FramebufferStatus Framebuffer::check_status() const
208 {
209         Bind _bind(this, true);
210         return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
211 }
212
213 void Framebuffer::require_complete() const
214 {
215         FramebufferStatus status = check_status();
216         if(status!=FRAMEBUFFER_COMPLETE)
217                 throw framebuffer_incomplete(status);
218 }
219
220 void Framebuffer::clear(BufferBits bits)
221 {
222         Bind _bind(this, true);
223         glClear(bits);
224 }
225
226 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)
227 {
228         static Require _req(EXT_framebuffer_blit);
229
230         const Framebuffer *old = current();
231         if(set_current(this))
232         {
233                 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
234                 if(dirty)
235                 {
236                         update_attachment(dirty);
237                         dirty = 0;
238                 }
239         }
240         if(old!=&other)
241                 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
242
243         glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
244
245         set_current(old);
246         glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
247 }
248
249 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
250 {
251         blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
252 }
253
254 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
255 {
256         blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
257 }
258
259 void Framebuffer::bind() const
260 {
261         if(set_current(this))
262         {
263                 glBindFramebuffer(GL_FRAMEBUFFER, id);
264                 if(dirty)
265                 {
266                         update_attachment(dirty);
267                         dirty = 0;
268                 }
269                 if(width && height)
270                         glViewport(0, 0, width, height);
271         }
272 }
273
274 const Framebuffer *Framebuffer::current()
275 {       
276         if(!cur_obj)
277                 cur_obj = &system();
278         return cur_obj;
279 }
280
281 void Framebuffer::unbind()
282 {
283         system().bind();
284 }
285
286 Framebuffer &Framebuffer::system()
287 {
288         static Framebuffer sys_framebuf(0);
289         return sys_framebuf;
290 }
291
292
293 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
294         attachment(a),
295         type(0),
296         level(0)
297 { }
298
299 void Framebuffer::Attachment::set(Renderbuffer &r)
300 {
301         type = GL_RENDERBUFFER;
302         rbuf = &r;
303         level = 0;
304 }
305
306 void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l)
307 {
308         type = t.get_target();
309         tex = &t;
310         cube_face = f;
311         level = l;
312 }
313
314 void Framebuffer::Attachment::clear()
315 {
316         type = 0;
317 }
318
319 } // namespace GL
320 } // namespace Msp