]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.h
Move all OpenGL-specific code to a separate directory
[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 private:
72         void init();
73
74 public:
75         /** Sets the format of the framebuffer.  Once the format is set, it can't
76         be changed. */
77         void set_format(const FrameFormat &);
78
79         const FrameFormat &get_format() const { return format; }
80
81         unsigned get_width() const { return width; }
82         unsigned get_height() const { return height; }
83
84 private:
85         void update() const;
86         void check_size();
87         void set_attachment(FrameAttachment, Texture &, unsigned, int, unsigned);
88 public:
89
90         /** Attaches a texture to the framebuffer.  Only the attachment point
91         portion of attch is considered; pixel format is ignored.  The framebuffer
92         must have a format and the format of the texture must match that defined
93         in the framebuffer for this attachment point. */
94         void attach(FrameAttachment attch, Texture2D &tex, unsigned level = 0);
95
96         void attach(FrameAttachment attch, Texture2DMultisample &tex);
97         void attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level = 0);
98         void attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
99         void attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level = 0);
100         void attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level = 0);
101         void detach(FrameAttachment attch);
102
103         void resize(const WindowView &);
104
105         /** Ensures that the framebuffer is complete, throwing an exception if it
106         isn't. */
107         void require_complete() const;
108
109         void refresh() const { if(dirty) update(); }
110
111         using FramebufferBackend::set_debug_name;
112
113         static Framebuffer &system();
114 };
115
116
117 union ClearValue
118 {
119         Color color;
120         struct
121         {
122                 float depth;
123                 int stencil;
124         } depth_stencil;
125
126         ClearValue(): color(0.0f, 0.0f, 0.0f, 0.0f) { }
127 };
128
129 } // namespace GL
130 } // namespace Msp
131
132 #endif