]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.cpp
Use friend declarations to access OpenGL IDs of objects
[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_object.h>
4 #include <msp/gl/extensions/ext_texture_array.h>
5 #include <msp/gl/extensions/ext_texture3d.h>
6 #include <msp/gl/extensions/msp_buffer_control.h>
7 #include <msp/gl/extensions/khr_debug.h>
8 #include "error.h"
9 #include "framebuffer.h"
10 #include "misc.h"
11 #include "texture2d.h"
12 #include "texture2dmultisample.h"
13 #include "texture3d.h"
14 #include "windowview.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         status(FRAMEBUFFER_COMPLETE),
69         dirty(0)
70 {
71         if(id)
72                 throw invalid_argument("System framebuffer must have id 0");
73
74         int view[4];
75         glGetIntegerv(GL_VIEWPORT, view);
76         width = view[2];
77         height = view[3];
78 }
79
80 Framebuffer::Framebuffer()
81 {
82         init();
83 }
84
85 Framebuffer::Framebuffer(FrameAttachment fa)
86 {
87         init();
88         set_format(fa);
89 }
90
91 Framebuffer::Framebuffer(const FrameFormat &f)
92 {
93         init();
94         set_format(f);
95 }
96
97 void Framebuffer::init()
98 {
99         static Require _req(EXT_framebuffer_object);
100
101         width = 0;
102         height = 0;
103         status = FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
104         dirty = 0;
105
106         if(ARB_direct_state_access)
107                 glCreateFramebuffers(1, &id);
108         else
109                 glGenFramebuffers(1, &id);
110 }
111
112 Framebuffer::~Framebuffer()
113 {
114         if(id)
115                 glDeleteFramebuffers(1, &id);
116 }
117
118 void Framebuffer::set_format(const FrameFormat &fmt)
119 {
120         if(!format.empty())
121                 throw invalid_operation("Framebuffer::set_format");
122         if(fmt.empty())
123                 throw invalid_argument("Framebuffer::set_format");
124
125         format = fmt;
126         attachments.resize(format.size());
127 }
128
129 void Framebuffer::update() const
130 {
131         vector<GLenum> color_bufs;
132         color_bufs.reserve(format.size());
133         unsigned i = 0;
134         for(FrameAttachment a: format)
135         {
136                 GLenum gl_attach_point = get_gl_attachment(a);
137                 if(dirty&(1<<i))
138                 {
139                         const Attachment &attch = attachments[i];
140                         if(attch.tex)
141                         {
142                                 if(ARB_direct_state_access)
143                                 {
144                                         if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0)
145                                                 glNamedFramebufferTexture(id, gl_attach_point, attch.tex->id, attch.level);
146                                         else
147                                                 glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->id, attch.level, attch.layer);
148                                 }
149                                 else if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
150                                         glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level);
151                                 else if(attch.layer<0)
152                                         glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level);
153                                 else if(attch.tex->target==GL_TEXTURE_2D_ARRAY)
154                                         glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level, attch.layer);
155                                 else if(attch.tex->target==GL_TEXTURE_3D)
156                                         glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level, attch.layer);
157                                 else if(attch.tex->target==GL_TEXTURE_CUBE_MAP)
158                                         glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast<TextureCubeFace>(attch.layer)), attch.tex->id, attch.level);
159                         }
160                         else if(ARB_direct_state_access)
161                                 glNamedFramebufferTexture(id, gl_attach_point, 0, 0);
162                         else
163                                 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0);
164                 }
165
166                 if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
167                         color_bufs.push_back(gl_attach_point);
168
169                 ++i;
170         }
171
172         if(color_bufs.size()>1)
173                 static Require _req(ARB_draw_buffers);
174
175         GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
176         if(ARB_direct_state_access)
177         {
178                 /* ARB_direct_state_access ties the availability of these functions to
179                 framebuffers themselves, so no further checks are needed. */
180                 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
181                 glNamedFramebufferReadBuffer(id, first_buffer);
182         }
183         else
184         {
185                 if(ARB_draw_buffers)
186                         glDrawBuffers(color_bufs.size(), &color_bufs[0]);
187                 else if(MSP_buffer_control)
188                         glDrawBuffer(first_buffer);
189
190                 if(MSP_buffer_control)
191                         glReadBuffer(first_buffer);
192         }
193
194         if(ARB_direct_state_access)
195                 status = static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
196         else
197                 status = static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
198
199         dirty = 0;
200 }
201
202 void Framebuffer::check_size()
203 {
204         bool first = true;
205         for(Attachment &a: attachments)
206                 if(a.tex)
207                 {
208                         unsigned w = 0;
209                         unsigned h = 0;
210                         if(a.tex->target==GL_TEXTURE_2D)
211                         {
212                                 Texture2D *tex = static_cast<Texture2D *>(a.tex);
213                                 w = max(tex->get_width()>>a.level, 1U);
214                                 h = max(tex->get_height()>>a.level, 1U);
215                         }
216                         else if(a.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
217                         {
218                                 Texture2DMultisample *tex = static_cast<Texture2DMultisample *>(a.tex);
219                                 w = tex->get_width();
220                                 h = tex->get_height();
221                         }
222                         else if(a.tex->target==GL_TEXTURE_3D || a.tex->target==GL_TEXTURE_2D_ARRAY)
223                         {
224                                 Texture3D *tex = static_cast<Texture3D *>(a.tex);
225                                 w = max(tex->get_width()>>a.level, 1U);
226                                 h = max(tex->get_height()>>a.level, 1U);
227                         }
228                         else if(a.tex->target==GL_TEXTURE_CUBE_MAP)
229                         {
230                                 w = max(static_cast<TextureCube *>(a.tex)->get_size()>>a.level, 1U);
231                                 h = w;
232                         }
233
234                         if(first)
235                         {
236                                 width = w;
237                                 height = h;
238                                 first = false;
239                         }
240                         else
241                         {
242                                 width = min(width, w);
243                                 height = min(height, h);
244                         }
245                 }
246 }
247
248 void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, unsigned level, int layer, unsigned samples)
249 {
250         if(format.empty() || !id)
251                 throw invalid_operation("Framebuffer::attach");
252
253         if((format.get_samples()>1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples))
254                 throw incompatible_data("Framebuffer::attach");
255
256         unsigned i = 0;
257         for(FrameAttachment a: format)
258         {
259                 if(a==attch)
260                 {
261                         attachments[i].set(tex, level, layer);
262                         dirty |= 1<<i;
263                         check_size();
264                         return;
265                 }
266                 ++i;
267         }
268
269         throw incompatible_data("Framebuffer::attach");
270 }
271
272 void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
273 {
274         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, 0, 0);
275 }
276
277 void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex)
278 {
279         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, 0, tex.get_samples());
280 }
281
282 void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
283 {
284         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, layer, 0);
285 }
286
287 void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
288 {
289         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, face, 0);
290 }
291
292 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
293 {
294         static Require _req(ARB_geometry_shader4);
295         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
296 }
297
298 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
299 {
300         static Require _req(ARB_geometry_shader4);
301         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
302 }
303
304 void Framebuffer::detach(FrameAttachment attch)
305 {
306         if(!id)
307                 throw invalid_operation("Framebuffer::detach");
308
309         int i = format.index(attch);
310         if(i>=0)
311         {
312                 attachments[i].clear();
313                 dirty |= 1<<i;
314                 check_size();
315         }
316 }
317
318 void Framebuffer::resize(const WindowView &view)
319 {
320         if(id)
321                 throw invalid_operation("Framebuffer::resize");
322
323         width = view.get_width();
324         height = view.get_height();
325 }
326
327 void Framebuffer::require_complete() const
328 {
329         if(status!=FRAMEBUFFER_COMPLETE)
330                 throw framebuffer_incomplete(status);
331 }
332
333 void Framebuffer::set_debug_name(const string &name)
334 {
335 #ifdef DEBUG
336         if(KHR_debug)
337                 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
338 #else
339         (void)name;
340 #endif
341 }
342
343 Framebuffer &Framebuffer::system()
344 {
345         static Framebuffer sys_framebuf(0);
346         return sys_framebuf;
347 }
348
349
350 Framebuffer::Attachment::Attachment():
351         tex(0),
352         level(0),
353         layer(0)
354 { }
355
356 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
357 {
358         tex = &t;
359         level = l;
360         layer = z;
361 }
362
363 void Framebuffer::Attachment::clear()
364 {
365         tex = 0;
366 }
367
368 } // namespace GL
369 } // namespace Msp