]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.cpp
Use standard fixed-size integer types
[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(const uint16_t *j=format.begin(); j!=format.end(); ++j, ++i)
135         {
136                 GLenum gl_attach_point = get_gl_attachment(static_cast<FrameAttachment>(*j));
137                 if(dirty&(1<<i))
138                 {
139                         const Attachment &attch = attachments[i];
140                         if(attch.tex)
141                         {
142                                 GLenum type = attch.tex->get_target();
143                                 if(ARB_direct_state_access)
144                                 {
145                                         if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0)
146                                                 glNamedFramebufferTexture(id, gl_attach_point, attch.tex->get_id(), attch.level);
147                                         else
148                                                 glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->get_id(), attch.level, attch.layer);
149                                 }
150                                 else if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE)
151                                         glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, type, attch.tex->get_id(), attch.level);
152                                 else if(attch.layer<0)
153                                         glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->get_id(), attch.level);
154                                 else if(type==GL_TEXTURE_2D_ARRAY)
155                                         glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->get_id(), attch.level, attch.layer);
156                                 else if(type==GL_TEXTURE_3D)
157                                         glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, type, attch.tex->get_id(), attch.level, attch.layer);
158                                 else if(type==GL_TEXTURE_CUBE_MAP)
159                                         glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
160                         }
161                         else if(ARB_direct_state_access)
162                                 glNamedFramebufferTexture(id, gl_attach_point, 0, 0);
163                         else
164                                 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0);
165                 }
166
167                 if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
168                         color_bufs.push_back(gl_attach_point);
169         }
170
171         if(color_bufs.size()>1)
172                 static Require _req(ARB_draw_buffers);
173
174         GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
175         if(ARB_direct_state_access)
176         {
177                 /* ARB_direct_state_access ties the availability of these functions to
178                 framebuffers themselves, so no further checks are needed. */
179                 glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
180                 glNamedFramebufferReadBuffer(id, first_buffer);
181         }
182         else
183         {
184                 if(ARB_draw_buffers)
185                         glDrawBuffers(color_bufs.size(), &color_bufs[0]);
186                 else if(MSP_buffer_control)
187                         glDrawBuffer(first_buffer);
188
189                 if(MSP_buffer_control)
190                         glReadBuffer(first_buffer);
191         }
192
193         if(ARB_direct_state_access)
194                 status = static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
195         else
196                 status = static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
197
198         dirty = 0;
199 }
200
201 void Framebuffer::check_size()
202 {
203         bool first = true;
204         for(Attachment &a: attachments)
205                 if(a.tex)
206                 {
207                         GLenum type = a.tex->get_target();
208                         unsigned w = 0;
209                         unsigned h = 0;
210                         if(type==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(type==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(type==GL_TEXTURE_3D || type==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(type==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(const uint16_t *j=format.begin(); j!=format.end(); ++j, ++i)
258                 if(*j==attch)
259                 {
260                         attachments[i].set(tex, level, layer);
261                         dirty |= 1<<i;
262                         check_size();
263                         return;
264                 }
265
266         throw incompatible_data("Framebuffer::attach");
267 }
268
269 void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
270 {
271         tex.allocate(level);
272         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, 0, 0);
273 }
274
275 void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex)
276 {
277         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, 0, tex.get_samples());
278 }
279
280 void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
281 {
282         tex.allocate(level);
283         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, layer, 0);
284 }
285
286 void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
287 {
288         tex.allocate(level);
289         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, TextureCube::get_face_index(face), 0);
290 }
291
292 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
293 {
294         static Require _req(ARB_geometry_shader4);
295         tex.allocate(level);
296         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
297 }
298
299 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
300 {
301         static Require _req(ARB_geometry_shader4);
302         tex.allocate(level);
303         set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
304 }
305
306 void Framebuffer::detach(FrameAttachment attch)
307 {
308         if(!id)
309                 throw invalid_operation("Framebuffer::detach");
310
311         int i = format.index(attch);
312         if(i>=0)
313         {
314                 attachments[i].clear();
315                 dirty |= 1<<i;
316                 check_size();
317         }
318 }
319
320 void Framebuffer::resize(const WindowView &view)
321 {
322         if(id)
323                 throw invalid_operation("Framebuffer::resize");
324
325         width = view.get_width();
326         height = view.get_height();
327 }
328
329 void Framebuffer::require_complete() const
330 {
331         if(status!=FRAMEBUFFER_COMPLETE)
332                 throw framebuffer_incomplete(status);
333 }
334
335 void Framebuffer::set_debug_name(const string &name)
336 {
337 #ifdef DEBUG
338         if(KHR_debug)
339                 glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
340 #else
341         (void)name;
342 #endif
343 }
344
345 Framebuffer &Framebuffer::system()
346 {
347         static Framebuffer sys_framebuf(0);
348         return sys_framebuf;
349 }
350
351
352 Framebuffer::Attachment::Attachment():
353         tex(0),
354         level(0),
355         layer(0)
356 { }
357
358 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
359 {
360         tex = &t;
361         level = l;
362         layer = z;
363 }
364
365 void Framebuffer::Attachment::clear()
366 {
367         tex = 0;
368 }
369
370 } // namespace GL
371 } // namespace Msp