]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.h
Add support for integer vertex attributes
[libs/gl.git] / source / core / framebuffer.h
1 #ifndef MSP_GL_FRAMEBUFFER_H_
2 #define MSP_GL_FRAMEBUFFER_H_
3
4 #include <vector>
5 #include "frameformat.h"
6 #include "gl.h"
7 #include "texturecube.h"
8 #include <msp/gl/extensions/arb_geometry_shader4.h>
9 #include <msp/gl/extensions/ext_framebuffer_multisample.h>
10 #include <msp/gl/extensions/ext_framebuffer_object.h>
11 #include <msp/gl/extensions/nv_fbo_color_attachments.h>
12
13 namespace Msp {
14 namespace GL {
15
16 class Texture;
17 class Texture2D;
18 class Texture2DMultisample;
19 class Texture3D;
20 class WindowView;
21
22 enum FramebufferStatus
23 {
24         FRAMEBUFFER_INCOMPLETE_ATTACHMENT         = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT,
25         FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT,
26         FRAMEBUFFER_INCOMPLETE_DIMENSIONS         = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT,
27         FRAMEBUFFER_INCOMPLETE_FORMATS            = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT,
28         FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER        = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER,
29         FRAMEBUFFER_INCOMPLETE_READ_BUFFER        = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER,
30         FRAMEBUFFER_INCOMPLETE_MULTISAMPLE        = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE,
31         FRAMEBUFFER_INCOMPLETE_LAYER_COUNT        = GL_FRAMEBUFFER_INCOMPLETE_LAYER_COUNT_ARB,
32         FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS      = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS,
33         FRAMEBUFFER_UNSUPPORTED                   = GL_FRAMEBUFFER_UNSUPPORTED,
34         FRAMEBUFFER_COMPLETE                      = GL_FRAMEBUFFER_COMPLETE
35 };
36
37 enum BufferBits
38 {
39         COLOR_BUFFER_BIT   = GL_COLOR_BUFFER_BIT,
40         DEPTH_BUFFER_BIT   = GL_DEPTH_BUFFER_BIT,
41         STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT
42 };
43
44 class framebuffer_incomplete: public std::runtime_error
45 {
46 public:
47         framebuffer_incomplete(FramebufferStatus);
48         virtual ~framebuffer_incomplete() throw() { }
49 };
50
51 /**
52 Framebuffer objects can be used to perform offscreen rendering.  The most
53 common application is rendering to a texture, which can then be used for
54 fullscreen shader effects.
55
56 A framebuffer consist of a number of logical buffers, such as color and depth
57 buffers.  Textures can be attached to the logical buffers.  At least one image
58 must be attached for the framebuffer to be usable.
59
60 Requires the GL_EXT_framebuffer_object extension.  The blit functions require
61 the GL_EXT_framebuffer_blit extension.
62 */
63 class Framebuffer
64 {
65 private:
66         struct Attachment
67         {
68                 Texture *tex;
69                 unsigned level;
70                 int layer;
71
72                 Attachment();
73                 void set(Texture &, unsigned, int);
74                 void clear();
75         };
76
77         unsigned id;
78         FrameFormat format;
79         std::vector<Attachment> attachments;
80         unsigned width;
81         unsigned height;
82         mutable FramebufferStatus status;
83         mutable unsigned dirty;
84
85         Framebuffer(unsigned);
86 public:
87         /** Creates an empty framebuffer.  Format must be set before textures can
88         be attached. */
89         Framebuffer();
90
91         /** Creates a framebuffer and sets its format to a single attachment. */
92         Framebuffer(FrameAttachment);
93
94         /** Creates a framebuffer and sets its format. */
95         Framebuffer(const FrameFormat &);
96
97 private:
98         void init();
99 public:
100         ~Framebuffer();
101
102         /** Sets the format of the framebuffer.  Once the format is set, it can't
103         be changed. */
104         void set_format(const FrameFormat &);
105
106         const FrameFormat &get_format() const { return format; }
107
108         unsigned get_width() const { return width; }
109         unsigned get_height() const { return height; }
110
111 private:
112         void update() const;
113         void check_size();
114         void set_attachment(FrameAttachment, Texture &, unsigned, int, unsigned);
115 public:
116
117         /** Attaches a texture to the framebuffer.  Only the attachment point
118         portion of attch is considered; pixel format is ignored.  The framebuffer
119         must have a format and the format of the texture must match that defined
120         in the framebuffer for this attachment point. */
121         void attach(FrameAttachment attch, Texture2D &tex, unsigned level = 0);
122
123         void attach(FrameAttachment attch, Texture2DMultisample &tex);
124         void attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level = 0);
125         void attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
126         void attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level = 0);
127         void attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level = 0);
128         void detach(FrameAttachment attch);
129
130         void resize(const WindowView &);
131
132         /** Returns FRAMEBUFFER_COMPLETE if the framebuffer is complete and can be
133         rendered to, or one of the error status codes otherwise. */
134         FramebufferStatus get_status() const { return status; }
135
136         /** Ensures that the framebuffer is complete, throwing an exception if it
137         isn't. */
138         void require_complete() const;
139
140         void refresh() const { if(dirty) update(); }
141
142         unsigned get_id() const { return id; }
143
144         void set_debug_name(const std::string &);
145
146         static Framebuffer &system();
147 };
148
149 inline BufferBits operator|(BufferBits a, BufferBits b)
150 { return static_cast<BufferBits>(static_cast<int>(a)|static_cast<int>(b)); }
151
152 } // namespace GL
153 } // namespace Msp
154
155 #endif