]> git.tdb.fi Git - libs/gl.git/blob - source/framebuffer.h
Remove the fake MSP_stereo_rendering extension
[libs/gl.git] / source / framebuffer.h
1 #ifndef MSP_GL_FRAMEBUFFER_H_
2 #define MSP_GL_FRAMEBUFFER_H_
3
4 #include <vector>
5 #include "bindable.h"
6 #include "gl.h"
7 #include "texturecube.h"
8 #include <msp/gl/extensions/ext_framebuffer_object.h>
9 #include <msp/gl/extensions/nv_fbo_color_attachments.h>
10
11 namespace Msp {
12 namespace GL {
13
14 class Renderbuffer;
15 class Texture;
16 class Texture2D;
17 class Texture3D;
18
19 enum FramebufferAttachment
20 {
21         COLOR_ATTACHMENT0  = GL_COLOR_ATTACHMENT0,
22         COLOR_ATTACHMENT1  = GL_COLOR_ATTACHMENT1,
23         COLOR_ATTACHMENT2  = GL_COLOR_ATTACHMENT2,
24         COLOR_ATTACHMENT3  = GL_COLOR_ATTACHMENT3,
25         DEPTH_ATTACHMENT   = GL_DEPTH_ATTACHMENT,
26         STENCIL_ATTACHMENT = GL_STENCIL_ATTACHMENT
27 };
28
29 enum FramebufferStatus
30 {
31         FRAMEBUFFER_INCOMPLETE_ATTACHMENT         = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT,
32         FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT,
33         FRAMEBUFFER_INCOMPLETE_DIMENSIONS         = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT,
34         FRAMEBUFFER_INCOMPLETE_FORMATS            = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT,
35         FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER        = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER,
36         FRAMEBUFFER_INCOMPLETE_READ_BUFFER        = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER,
37         FRAMEBUFFER_UNSUPPORTED                   = GL_FRAMEBUFFER_UNSUPPORTED,
38         FRAMEBUFFER_COMPLETE                      = GL_FRAMEBUFFER_COMPLETE
39 };
40
41 enum BufferBits
42 {
43         COLOR_BUFFER_BIT   = GL_COLOR_BUFFER_BIT,
44         DEPTH_BUFFER_BIT   = GL_DEPTH_BUFFER_BIT,
45         STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT
46 };
47
48 class framebuffer_incomplete: public std::runtime_error
49 {
50 public:
51         framebuffer_incomplete(FramebufferStatus);
52         virtual ~framebuffer_incomplete() throw() { }
53 };
54
55 /**
56 Framebuffer objects can be used to perform offscreen rendering.  The most
57 common application is rendering to a texture, which can then be used for
58 fullscreen shader effects.
59
60 A framebuffer consist of a number of logical buffers, such as color and depth
61 buffers.  Renderbuffers and Textures can be attached to the logical buffers.  At
62 least one image must be attached for the framebuffer to be usable.
63
64 Requires the GL_EXT_framebuffer_object extension.  The blit functions require
65 the GL_EXT_framebuffer_blit extension.
66 */
67 class Framebuffer: public Bindable<Framebuffer>
68 {
69 private:
70         struct Attachment
71         {
72                 FramebufferAttachment attachment;
73                 GLenum type;
74                 union
75                 {
76                         Renderbuffer *rbuf;
77                         Texture *tex;
78                 };
79                 unsigned level;
80                 unsigned layer;
81
82                 Attachment(FramebufferAttachment);
83                 void set(Renderbuffer &);
84                 void set(Texture &, unsigned, unsigned);
85                 void clear();
86         };
87
88         struct Viewport
89         {
90                 int left;
91                 int bottom;
92                 unsigned width;
93                 unsigned height;
94
95                 Viewport();
96         };
97
98         unsigned id;
99         std::vector<Attachment> attachments;
100         unsigned width;
101         unsigned height;
102         Viewport view;
103         mutable unsigned dirty;
104
105         Framebuffer(unsigned);
106 public:
107         Framebuffer();
108         ~Framebuffer();
109
110         unsigned get_width() const { return width; }
111         unsigned get_height() const { return height; }
112
113 private:
114         void update_attachment(unsigned) const;
115         void check_size();
116         unsigned get_attachment_index(FramebufferAttachment);
117 public:
118         void attach(FramebufferAttachment attch, Renderbuffer &rbuf);
119         void attach(FramebufferAttachment attch, Texture2D &tex, unsigned level = 0);
120         void attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level = 0);
121         void attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
122         void detach(FramebufferAttachment attch);
123
124         /** Checks the completeness of the framebuffer.  Returns
125         FRAMEBUFFER_COMPLETE if the framebuffer is complete and can be rendered to,
126         or one of the error status codes otherwise. */
127         FramebufferStatus check_status() const;
128
129         /** Ensures that the framebuffer is complete, throwing an exception if it
130         isn't. */
131         void require_complete() const;
132
133         void viewport(int, int, unsigned, unsigned);
134         void reset_viewport();
135
136         void clear(BufferBits);
137
138         /** Blits a region from another framebuffer into this one.  If the source
139         and destination regions have different dimensions, the contents will be
140         stretched.  If filter is true, linear interpolation will be used, otherwise
141         no interpolation is done. */
142         void blit_from(const Framebuffer &other, int sx0, int sy0, int sx1, int sy1,
143                 int dx0, int dy0, int dx1, int dy1, BufferBits bits, bool filter);
144
145         /** Blits a region from another framebuffer into this one, retaining its
146         dimensions. */
147         void blit_from(const Framebuffer & other, int sx, int sy,
148                 unsigned wd, unsigned ht, int dx, int dy, BufferBits bits);
149
150         /** Blits the entire contents of another framebuffer into this one. */
151         void blit_from(const Framebuffer &other, BufferBits bits, bool filter);
152
153         void bind() const;
154
155         static const Framebuffer *current();
156         static void unbind();
157
158         static Framebuffer &system();
159 };
160
161 inline BufferBits operator|(BufferBits a, BufferBits b)
162 { return static_cast<BufferBits>(static_cast<int>(a)|static_cast<int>(b)); }
163
164 } // namespace GL
165 } // namespace Msp
166
167 #endif