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