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