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