]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.h
Remove the now unused BufferBits enum
[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 "color.h"
6 #include "frameformat.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 class framebuffer_incomplete: public std::runtime_error
23 {
24 public:
25         framebuffer_incomplete(const std::string &);
26         virtual ~framebuffer_incomplete() throw() { }
27 };
28
29 /**
30 Framebuffer objects can be used to perform offscreen rendering.  The most
31 common application is rendering to a texture, which can then be used for
32 fullscreen shader effects.
33
34 A framebuffer consist of a number of logical buffers, such as color and depth
35 buffers.  Textures can be attached to the logical buffers.  At least one image
36 must be attached for the framebuffer to be usable.
37
38 Requires the GL_EXT_framebuffer_object extension.  The blit functions require
39 the GL_EXT_framebuffer_blit extension.
40 */
41 class Framebuffer
42 {
43         friend class Commands;
44         friend class PipelineState;
45
46 private:
47         struct Attachment
48         {
49                 Texture *tex;
50                 unsigned level;
51                 int layer;
52
53                 Attachment();
54                 void set(Texture &, unsigned, int);
55                 void clear();
56         };
57
58         unsigned id;
59         FrameFormat format;
60         std::vector<Attachment> attachments;
61         unsigned width;
62         unsigned height;
63         mutable unsigned status;
64         mutable unsigned dirty;
65
66         Framebuffer(unsigned);
67 public:
68         /** Creates an empty framebuffer.  Format must be set before textures can
69         be attached. */
70         Framebuffer();
71
72         /** Creates a framebuffer and sets its format to a single attachment. */
73         Framebuffer(FrameAttachment);
74
75         /** Creates a framebuffer and sets its format. */
76         Framebuffer(const FrameFormat &);
77
78 private:
79         void init();
80 public:
81         ~Framebuffer();
82
83         /** Sets the format of the framebuffer.  Once the format is set, it can't
84         be changed. */
85         void set_format(const FrameFormat &);
86
87         const FrameFormat &get_format() const { return format; }
88
89         unsigned get_width() const { return width; }
90         unsigned get_height() const { return height; }
91
92 private:
93         void update() const;
94         void check_size();
95         void set_attachment(FrameAttachment, Texture &, unsigned, int, unsigned);
96 public:
97
98         /** Attaches a texture to the framebuffer.  Only the attachment point
99         portion of attch is considered; pixel format is ignored.  The framebuffer
100         must have a format and the format of the texture must match that defined
101         in the framebuffer for this attachment point. */
102         void attach(FrameAttachment attch, Texture2D &tex, unsigned level = 0);
103
104         void attach(FrameAttachment attch, Texture2DMultisample &tex);
105         void attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level = 0);
106         void attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
107         void attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level = 0);
108         void attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level = 0);
109         void detach(FrameAttachment attch);
110
111         void resize(const WindowView &);
112
113         /** Ensures that the framebuffer is complete, throwing an exception if it
114         isn't. */
115         void require_complete() const;
116
117         void refresh() const { if(dirty) update(); }
118
119         void set_debug_name(const std::string &);
120
121         static Framebuffer &system();
122 };
123
124
125 union ClearValue
126 {
127         Color color;
128         struct
129         {
130                 float depth;
131                 int stencil;
132         } depth_stencil;
133
134         ClearValue(): color(0.0f, 0.0f, 0.0f, 0.0f) { }
135 };
136
137 } // namespace GL
138 } // namespace Msp
139
140 #endif