]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.h
0b9af1efd167e47addd3572eff9db4e80b87d3e1
[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 "framebuffer_backend.h"
7 #include "frameformat.h"
8 #include "texturecube.h"
9
10 namespace Msp {
11 namespace GL {
12
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: public FramebufferBackend
38 {
39         friend FramebufferBackend;
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         FrameFormat format;
54         std::vector<Attachment> attachments;
55         unsigned width;
56         unsigned height;
57         mutable unsigned dirty;
58
59         Framebuffer(bool);
60 public:
61         /** Creates an empty framebuffer.  Format must be set before textures can
62         be attached. */
63         Framebuffer();
64
65         /** Creates a framebuffer and sets its format to a single attachment. */
66         Framebuffer(FrameAttachment);
67
68         /** Creates a framebuffer and sets its format. */
69         Framebuffer(const FrameFormat &);
70
71         /** Sets the format of the framebuffer.  Once the format is set, it can't
72         be changed. */
73         void set_format(const FrameFormat &);
74
75         const FrameFormat &get_format() const { return format; }
76
77         unsigned get_width() const { return width; }
78         unsigned get_height() const { return height; }
79
80 private:
81         void update() const;
82         void check_size();
83         void set_attachment(FrameAttachment, Texture &, unsigned, int, unsigned);
84 public:
85
86         /** Attaches a texture to the framebuffer.  Only the attachment point
87         portion of attch is considered; pixel format is ignored.  The framebuffer
88         must have a format and the format of the texture must match that defined
89         in the framebuffer for this attachment point. */
90         void attach(FrameAttachment attch, Texture2D &tex, unsigned level = 0);
91
92         void attach(FrameAttachment attch, Texture2DMultisample &tex);
93         void attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level = 0);
94         void attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
95         void attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level = 0);
96         void attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level = 0);
97         void detach(FrameAttachment attch);
98
99         void resize(const WindowView &);
100
101         /** Ensures that the framebuffer is complete, throwing an exception if it
102         isn't. */
103         void require_complete() const;
104
105         void refresh() const { if(dirty) update(); }
106
107         using FramebufferBackend::set_debug_name;
108
109         static Framebuffer &system();
110 };
111
112
113 union ClearValue
114 {
115         Color color;
116         struct
117         {
118                 float depth;
119                 int stencil;
120         } depth_stencil;
121
122         ClearValue(): color(0.0f, 0.0f, 0.0f, 0.0f) { }
123 };
124
125 } // namespace GL
126 } // namespace Msp
127
128 #endif